Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hide panic internals in short backtraces #124586

Open
jyn514 opened this issue May 1, 2024 · 5 comments
Open

hide panic internals in short backtraces #124586

jyn514 opened this issue May 1, 2024 · 5 comments
Labels
A-runtime Area: std's runtime and "pre-main" init for handling backtraces, unwinds, stack overflows C-enhancement Category: An issue proposing an enhancement or a PR with one. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@jyn514
Copy link
Member

jyn514 commented May 1, 2024

right now, nearly all foo.expect("bar") backtraces start like this:

stack backtrace:
   0: rust_begin_unwind
             at /rustc/7f2fc33da6633f5a764ddc263c769b6b2873d167/library/std/src/panicking.rs:652:5
   1: core::panicking::panic_fmt
             at /rustc/7f2fc33da6633f5a764ddc263c769b6b2873d167/library/core/src/panicking.rs:72:14
   2: core::panicking::panic_display
             at /rustc/7f2fc33da6633f5a764ddc263c769b6b2873d167/library/core/src/panicking.rs:256:5
   3: core::panicking::panic_str
             at /rustc/7f2fc33da6633f5a764ddc263c769b6b2873d167/library/core/src/panicking.rs:231:5
   4: core::option::expect_failed
             at /rustc/7f2fc33da6633f5a764ddc263c769b6b2873d167/library/core/src/option.rs:1994:5
   5: core::option::Option<T>::expect
             at /rustc/7f2fc33da6633f5a764ddc263c769b6b2873d167/library/core/src/option.rs:895:21

this is not super useful. it doesn't add any information, other than maybe the very last frame which says you called expect(). it would be nice to omit anything in the core::panicking module from the backtrace; and maybe expect_failed and the core::ops::function::FnOnce::call_once that show up at the end of the backtrace as well. people can always opt back in with RUST_BACKTRACE=full.

the code for this lives in

// Any frames between `__rust_begin_short_backtrace` and `__rust_end_short_backtrace`
// are omitted from the backtrace in short mode, `__rust_end_short_backtrace` will be
// called before the panic hook, so we won't ignore any frames if there is no
// invoke of `__rust_begin_short_backtrace`.
if print_fmt == PrintFmt::Short {

@rustbot label +T-libs +A-runtime

@rustbot rustbot added needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. A-runtime Area: std's runtime and "pre-main" init for handling backtraces, unwinds, stack overflows T-libs Relevant to the library team, which will review and decide on the PR/issue. labels May 1, 2024
@Nilstrieb Nilstrieb added C-enhancement Category: An issue proposing an enhancement or a PR with one. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels May 1, 2024
@workingjubilee
Copy link
Contributor

I think the easiest way to do this would be to try to move the __rust_begin_short_backtrace and __rust_end_short_backtrace symbols around. That way everything continues working as normal.

It should be fine if this accidentally results in some duplication in the full trace. The filtering must already be resilient against the fact that we are neither guaranteed that these symbols only appear once, or at all.

@jyn514
Copy link
Member Author

jyn514 commented May 1, 2024

I think the easiest way to do this would be to try to move the __rust_begin_short_backtrace and __rust_end_short_backtrace symbols around. That way everything continues working as normal.

i don't see how this helps? if you move it to expect_failed, you now need a bunch of begin_short_backtrace stubs, one for each possible approach to the panic machinery. i see 10 calls to panic_fmt alone, that doesn't seem maintainable.

the backtrace crate gives us access to the module names, that seems much simpler than trying to hack it by inserting custom frames.

@workingjubilee
Copy link
Contributor

Hmm. Making the backtrace filtering logic more fragile does not seem automatically "simpler" to me, since now it has to handle a number of other conditions and it has to be aware that it can only exclude these frames at the beginning of the (shortened) backtrace, not if they somehow appear elsewhere, but I would be willing to review such a PR.

@conradludgate
Copy link
Contributor

For short backtraces, I would also like to propose trimming the pre-main frames too

@saethlin
Copy link
Member

saethlin commented May 2, 2024

I think that's in the OP:

and the core::ops::function::FnOnce::call_once that show up at the end of the backtrace as well.

I've been thinking about how to do this. I agree with Jubilee's concern that just going off module path is fragile, but I'm not totally sure that alternative approaches are worth the complexity.

Omitting the FnOnce::call_once entry is straightforward in that the heuristic is just to omit the frame before we encounter __rust_begin_short_backtrace. I don't see a straightforward way to make this happen because the backtrace printing needs to manage state in a way it doesn't right now. I don't see any fragility, we should just do this.

Omitting frames based on the core::panicking path looks like a good heuristic based on the backtrace for assert_eq!, but I'm wary of what would happen in a backtrace that exits the module then comes back to it. Perhaps we can also make this resilient this by adding yet more state to the backtrace printing? If we keep track of the fact that saw __rust_end_short_backtrace then continue skipping frames until we get something that isn't rust_begin_unwind or in core::panicking... maybe that works well enough? At that point though I'd really like to see a refactor of the backtrace printing state machine. It's quite unwieldy already.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-runtime Area: std's runtime and "pre-main" init for handling backtraces, unwinds, stack overflows C-enhancement Category: An issue proposing an enhancement or a PR with one. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants