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

std::backtrace::Backtrace should filter by __rust_end_short_backtrace and __rust_begin_short_backtrace #105413

Open
rukai opened this issue Dec 7, 2022 · 4 comments
Labels
C-bug Category: This is a bug. C-enhancement Category: An issue proposing an enhancement or a PR with one. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@rukai
Copy link
Contributor

rukai commented Dec 7, 2022

rustc inserts frames std::sys_common::backtrace::__rust_end_short_backtrace and std::sys_common::backtrace::__rust_begin_short_backtrace so that the internal backtrace implementation can filter out noise from backtrace capturing.
However those are not used when std::backtrace::Backtrace is displayed leading to incredibly verbose backtraces.

Maybe we could perform this filtering for "{}" but provide the unfiltered values for "{:#}"?

I tried this code:

fn setup() {
    std::panic::set_hook(Box::new(|panic| {
        println!("{}", std::backtrace::Backtrace::force_capture());
    }));
}

fn run() {
    panic!("OOPS");
}

fn main() {
    setup();
    run();
}

I expected to see this happen:

   0: rust_begin_unwind
             at /rustc/897e37553bba8b42751c67658967889d11ecd120/library/std/src/panicking.rs:584:5
   1: core::panicking::panic_fmt
             at /rustc/897e37553bba8b42751c67658967889d11ecd120/library/core/src/panicking.rs:142:14
   2: foo::run
             at ./src/main.rs:8:5
   3: foo::main
             at ./src/main.rs:13:5
   4: core::ops::function::FnOnce::call_once
             at /rustc/897e37553bba8b42751c67658967889d11ecd120/library/core/src/ops/function.rs:248:5

Instead, this happened:

   0: foo::setup::{{closure}}
             at ./src/main.rs:3:24
   1: std::panicking::rust_panic_with_hook
             at /rustc/897e37553bba8b42751c67658967889d11ecd120/library/std/src/panicking.rs:702:17
   2: std::panicking::begin_panic_handler::{{closure}}
             at /rustc/897e37553bba8b42751c67658967889d11ecd120/library/std/src/panicking.rs:586:13
   3: std::sys_common::backtrace::__rust_end_short_backtrace
             at /rustc/897e37553bba8b42751c67658967889d11ecd120/library/std/src/sys_common/backtrace.rs:138:18
   4: rust_begin_unwind
             at /rustc/897e37553bba8b42751c67658967889d11ecd120/library/std/src/panicking.rs:584:5
   5: core::panicking::panic_fmt
             at /rustc/897e37553bba8b42751c67658967889d11ecd120/library/core/src/panicking.rs:142:14
   6: foo::run
             at ./src/main.rs:8:5
   7: foo::main
             at ./src/main.rs:13:5
   8: core::ops::function::FnOnce::call_once
             at /rustc/897e37553bba8b42751c67658967889d11ecd120/library/core/src/ops/function.rs:248:5
   9: std::sys_common::backtrace::__rust_begin_short_backtrace
             at /rustc/897e37553bba8b42751c67658967889d11ecd120/library/std/src/sys_common/backtrace.rs:122:18
  10: std::rt::lang_start::{{closure}}
             at /rustc/897e37553bba8b42751c67658967889d11ecd120/library/std/src/rt.rs:166:18
  11: core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &F>::call_once
             at /rustc/897e37553bba8b42751c67658967889d11ecd120/library/core/src/ops/function.rs:283:13
  12: std::panicking::try::do_call
             at /rustc/897e37553bba8b42751c67658967889d11ecd120/library/std/src/panicking.rs:492:40
  13: std::panicking::try
             at /rustc/897e37553bba8b42751c67658967889d11ecd120/library/std/src/panicking.rs:456:19
  14: std::panic::catch_unwind
             at /rustc/897e37553bba8b42751c67658967889d11ecd120/library/std/src/panic.rs:137:14
  15: std::rt::lang_start_internal::{{closure}}
             at /rustc/897e37553bba8b42751c67658967889d11ecd120/library/std/src/rt.rs:148:48
  16: std::panicking::try::do_call
             at /rustc/897e37553bba8b42751c67658967889d11ecd120/library/std/src/panicking.rs:492:40
  17: std::panicking::try
             at /rustc/897e37553bba8b42751c67658967889d11ecd120/library/std/src/panicking.rs:456:19
  18: std::panic::catch_unwind
             at /rustc/897e37553bba8b42751c67658967889d11ecd120/library/std/src/panic.rs:137:14
  19: std::rt::lang_start_internal
             at /rustc/897e37553bba8b42751c67658967889d11ecd120/library/std/src/rt.rs:148:20
  20: std::rt::lang_start
             at /rustc/897e37553bba8b42751c67658967889d11ecd120/library/std/src/rt.rs:165:17
  21: main
  22: <unknown>
  23: __libc_start_main
  24: _start
             at /build/glibc/src/glibc/csu/../sysdeps/x86_64/start.S:115

Meta

rustc --version --verbose:

rustc 1.67.0-nightly (b28d30e1e 2022-12-06)
binary: rustc
commit-hash: b28d30e1e3c2b90fd08b7dd79d8e63884d1e0339
commit-date: 2022-12-06
host: x86_64-unknown-linux-gnu
release: 1.67.0-nightly
LLVM version: 15.0.4
@bjorn3
Copy link
Member

bjorn3 commented Dec 7, 2022

Do you have RUST_BACKTRACE set to 1 or full?

@rukai
Copy link
Contributor Author

rukai commented Dec 7, 2022

I have my system set to RUST_BACKTRACE=1
The example I gave has the same output regardless of whether RUST_BACKTRACE is 1 or full.

@bjorn3
Copy link
Member

bjorn3 commented Dec 7, 2022

__rust_end_short_backtrace and __rust_begin_short_backtrace seem to be handled by the default panic handler, not by impl Display for Backtrace. In any case only the first occurence of __rust_end_short_backtrace is trimmed.

@eddyb
Copy link
Member

eddyb commented Mar 31, 2023

Relatedly, I just left this comment on the backtrace issue: rust-lang/backtrace-rs#502 (comment)

what if not just the filtering, but __rust_begin_short_backtrace/__rust_end_short_backtrace themselves were provided by backtrace (as a "please hide these implementation details" bracketing system), and then libstd also used that?

That way, nothing would be hardcoding implementation details of anything else, and the backtrace crate could even check for its specific full symbol names (e.g. backtrace::short::begin/backtrace::short::end etc.), instead of just .contains with the function names.

If an API is designed for the backtrace crate, then it could similarly be exposed by std::backtrace with a similar API, without having weird divergences etc.

@jyn514 jyn514 added C-enhancement Category: An issue proposing an enhancement or a PR with one. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Apr 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. C-enhancement Category: An issue proposing an enhancement or a PR with one. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants