Use debuginfo for short backtrace printing · Issue #818 · rust-lang/compiler-team (original) (raw)

Proposal

The problem

This MCP is intended to solve rust-lang/rust#124586 in an extensible way.

Currently, short backtrace printing (the part of the default panic handler in std that hides frames like lang_start and std::panicking) works like this:

This has two drawbacks:

  1. because the functions are marked with inline(never), they form an optimization barrier, because most of LLVM's optimizations are hamstrung when they can't do inlining.
  2. the state is global across all functions in the callgraph, not local to the functions being annotated.

both of these make me reluctant to add more start/end function pairs, because it will hurt performance for everyone using rust and also it's hard to be sure that the panic state machine is working as expected.

Note these drawbacks only to new __rust_{start,end} frames. in std itself we only call these functions once, so we don't care about performance, and we are reasonably confident the existing state machine is correct for those two frames.

My proposed solution

Additionally, expose a new #[rustc_skip_short_backtrace] attribute. When applied to a function, this generates debuginfo that says to skip that frame only, but not to change the state of the panic handler state machine. When applied to a module, this generates debuginfo that says to skip any function defined in that module (e.g. DWARF will use DW_TAG_namespace).

Note that this allows other languages besides Rust to read and write functions marked as skippable, as long as their compiler understands the format we use for the debuginfo.

Drawbacks

Alternatives

  1. The same as above, but we remove __rust_start_short_backtrace altogether and only use the new attributes. This will regress anyone who was using those frames outside std. It also will break if debuginfo is disabled.
  2. The same as above, but only the new #[rustc_skip_short_backtrace] attribute is represented with debuginfo; start/end attributes are still represented as inline(never) frames. This avoids missing start/end frames when debuginfo is disabled, but has a slight performance hit anywhere a start/end pair appears.
  3. Use a builtin macro to expand #[rustc_skip_short_backtrace] to a static injected into std that contains a slice of function names (or addresses). Continue using frames for start/end pairs. This is platform independent, but prevents the new attribute from being used anywhere outside std(/alloc/core). Has a small performance hit for start/end pairs. Does not allow other languages to mark frames as skippable, or to read the list of skippable frames.
  4. Inject the static from 2. just before link time, instead of when building std. This allows using the attribute outside std, but makes dynamic linking hard. No performance hit. Does not allow other languages to mark frames as skippable.
    • One possible approach is to use a separate object file for this static, instead of combining it with any other CGU. If multiple dynamic libs are linked into a Rust binary, the compiler merges the two object files into a single symbol. If rustc does not control the final link, the external build system is responsible for linking in the object files, similar to how #[global_allocator] works today.

Future work

Mentors or Reviewers

@bjorn3, @saethlin

cc @rust-lang/libs

Process

The main points of the Major Change Process are as follows:

You can read more about Major Change Proposals on forge.

Comments

This issue is not meant to be used for technical discussion. There is a Zulip stream for that. Use this issue to leave procedural comments, such as volunteering to review, indicating that you second the proposal (or third, etc), or raising a concern that you would like to be addressed.