Skip to content

RUST_BACKTRACE truncated with -Cpanic=abort #81902

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

Open
Swatinem opened this issue Feb 8, 2021 · 5 comments
Open

RUST_BACKTRACE truncated with -Cpanic=abort #81902

Swatinem opened this issue Feb 8, 2021 · 5 comments
Labels
A-runtime Area: std's runtime and "pre-main" init for handling backtraces, unwinds, stack overflows C-bug Category: This is a bug. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@Swatinem
Copy link
Contributor

Swatinem commented Feb 8, 2021

When using -Cpanic=abort, the resulting panic backtrace is being truncated.

Interestingly, when wrapping with a custom panic hook, this problem is even worse.

fn main() {
    let next = std::panic::take_hook();
    std::panic::set_hook(Box::new(move |info| next(info)));

    panic!("oh no! anyway...");
}

Tested on nightly and stable. Only seems to be a problem on mac and linux, windows does not seem to be effected.

RUST_BACKTRACE=full with -Cpanic=unwind

thread 'main' panicked at 'oh no! anyway...', panic-backtrace.rs:5:5
stack backtrace:
   0:     0x5641e75c2937 - std::backtrace_rs::backtrace::libunwind::trace::h8bc78f7bc1f75bdb
                               at /rustc/04caa632dd10c2bf64b69524c7f9c4c30a436877/library/std/src/../../backtrace/src/backtrace/libunwind.rs:90:5
   1:     0x5641e75c2937 - std::backtrace_rs::backtrace::trace_unsynchronized::ha4c961c9576f95ba
                               at /rustc/04caa632dd10c2bf64b69524c7f9c4c30a436877/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x5641e75c2937 - std::sys_common::backtrace::_print_fmt::hc20cfdc233d6eb02
                               at /rustc/04caa632dd10c2bf64b69524c7f9c4c30a436877/library/std/src/sys_common/backtrace.rs:67:5
   3:     0x5641e75c2937 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h04bea14549780a0f
                               at /rustc/04caa632dd10c2bf64b69524c7f9c4c30a436877/library/std/src/sys_common/backtrace.rs:46:22
   4:     0x5641e75d94dc - core::fmt::write::h3868db8542c90941
                               at /rustc/04caa632dd10c2bf64b69524c7f9c4c30a436877/library/core/src/fmt/mod.rs:1096:17
   5:     0x5641e75c0b22 - std::io::Write::write_fmt::h55dd75636eef6a5c
                               at /rustc/04caa632dd10c2bf64b69524c7f9c4c30a436877/library/std/src/io/mod.rs:1568:15
   6:     0x5641e75c47f5 - std::sys_common::backtrace::_print::h6439e8525077c7da
                               at /rustc/04caa632dd10c2bf64b69524c7f9c4c30a436877/library/std/src/sys_common/backtrace.rs:49:5
   7:     0x5641e75c47f5 - std::sys_common::backtrace::print::h02b685f728ce3931
                               at /rustc/04caa632dd10c2bf64b69524c7f9c4c30a436877/library/std/src/sys_common/backtrace.rs:36:9
   8:     0x5641e75c47f5 - std::panicking::default_hook::{{closure}}::h7c29625065fafae1
                               at /rustc/04caa632dd10c2bf64b69524c7f9c4c30a436877/library/std/src/panicking.rs:208:50
   9:     0x5641e75c4353 - std::panicking::default_hook::hba441710bb71f4d5
                               at /rustc/04caa632dd10c2bf64b69524c7f9c4c30a436877/library/std/src/panicking.rs:225:9
  10:     0x5641e75ac41f - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::h96c2c6718f794857
  11:     0x5641e75abc34 - panic_backtrace::main::{{closure}}::hce20eff12a892edc
  12:     0x5641e75c4fb6 - std::panicking::rust_panic_with_hook::ha936eb00e51bc4d9
                               at /rustc/04caa632dd10c2bf64b69524c7f9c4c30a436877/library/std/src/panicking.rs:595:17
  13:     0x5641e75ac0da - std::panicking::begin_panic::{{closure}}::h7a9154edebf9ece3
  14:     0x5641e75abc85 - std::sys_common::backtrace::__rust_end_short_backtrace::h1e2014397c41a685
  15:     0x5641e75ac026 - std::panicking::begin_panic::h53c35f27b097d4f4
  16:     0x5641e75abc16 - panic_backtrace::main::h5ef5bf96a2375ff6
  17:     0x5641e75abeb6 - core::ops::function::FnOnce::call_once::hc2eccc5827063ab0
  18:     0x5641e75abcb9 - std::sys_common::backtrace::__rust_begin_short_backtrace::h2e49d68cd7dda505
  19:     0x5641e75ac91c - std::rt::lang_start::{{closure}}::he479198adea7d32d
  20:     0x5641e75c53b7 - core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &F>::call_once::h7c92107c3c98a949
                               at /rustc/04caa632dd10c2bf64b69524c7f9c4c30a436877/library/core/src/ops/function.rs:259:13
  21:     0x5641e75c53b7 - std::panicking::try::do_call::h7454c57e8fbf0cc1
                               at /rustc/04caa632dd10c2bf64b69524c7f9c4c30a436877/library/std/src/panicking.rs:379:40
  22:     0x5641e75c53b7 - std::panicking::try::he71cdc15ed83b5ec
                               at /rustc/04caa632dd10c2bf64b69524c7f9c4c30a436877/library/std/src/panicking.rs:343:19
  23:     0x5641e75c53b7 - std::panic::catch_unwind::hfc08ddb043e6e18c
                               at /rustc/04caa632dd10c2bf64b69524c7f9c4c30a436877/library/std/src/panic.rs:410:14
  24:     0x5641e75c53b7 - std::rt::lang_start_internal::he4958117ec6901fd
                               at /rustc/04caa632dd10c2bf64b69524c7f9c4c30a436877/library/std/src/rt.rs:51:25
  25:     0x5641e75ac8f8 - std::rt::lang_start::hdc838a40e951ed3a
  26:     0x5641e75abc5b - main
  27:     0x7f1653f01152 - __libc_start_main
  28:     0x5641e75ab09e - _start
  29:                0x0 - <unknown>

RUST_BACKTRACE=full with -Cpanic=abort

thread 'main' panicked at 'oh no! anyway...', panic-backtrace.rs:5:5
stack backtrace:
   0:     0x55bf3d3dc5a7 - std::backtrace_rs::backtrace::libunwind::trace::h8bc78f7bc1f75bdb
                               at /rustc/04caa632dd10c2bf64b69524c7f9c4c30a436877/library/std/src/../../backtrace/src/backtrace/libunwind.rs:90:5
   1:     0x55bf3d3dc5a7 - std::backtrace_rs::backtrace::trace_unsynchronized::ha4c961c9576f95ba
                               at /rustc/04caa632dd10c2bf64b69524c7f9c4c30a436877/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x55bf3d3dc5a7 - std::sys_common::backtrace::_print_fmt::hc20cfdc233d6eb02
                               at /rustc/04caa632dd10c2bf64b69524c7f9c4c30a436877/library/std/src/sys_common/backtrace.rs:67:5
   3:     0x55bf3d3dc5a7 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h04bea14549780a0f
                               at /rustc/04caa632dd10c2bf64b69524c7f9c4c30a436877/library/std/src/sys_common/backtrace.rs:46:22
   4:     0x55bf3d3f2a3c - core::fmt::write::h3868db8542c90941
                               at /rustc/04caa632dd10c2bf64b69524c7f9c4c30a436877/library/core/src/fmt/mod.rs:1096:17
   5:     0x55bf3d3da792 - std::io::Write::write_fmt::h55dd75636eef6a5c
                               at /rustc/04caa632dd10c2bf64b69524c7f9c4c30a436877/library/std/src/io/mod.rs:1568:15
   6:     0x55bf3d3de3c5 - std::sys_common::backtrace::_print::h6439e8525077c7da
                               at /rustc/04caa632dd10c2bf64b69524c7f9c4c30a436877/library/std/src/sys_common/backtrace.rs:49:5
   7:     0x55bf3d3de3c5 - std::sys_common::backtrace::print::h02b685f728ce3931
                               at /rustc/04caa632dd10c2bf64b69524c7f9c4c30a436877/library/std/src/sys_common/backtrace.rs:36:9
   8:     0x55bf3d3de3c5 - std::panicking::default_hook::{{closure}}::h7c29625065fafae1
                               at /rustc/04caa632dd10c2bf64b69524c7f9c4c30a436877/library/std/src/panicking.rs:208:50
   9:     0x55bf3d3ddf23 - std::panicking::default_hook::hba441710bb71f4d5
                               at /rustc/04caa632dd10c2bf64b69524c7f9c4c30a436877/library/std/src/panicking.rs:225:9
  10:     0x55bf3d3c610f - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::h96c2c6718f794857
[1]    488156 abort (core dumped)  RUST_BACKTRACE=full ./panic-backtrace

Using only RUST_BACKTRACE=1 in that case will not print any backtrace at all.

@Swatinem Swatinem added the C-bug Category: This is a bug. label Feb 8, 2021
@jonas-schievink jonas-schievink added 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 Feb 8, 2021
@bjorn3
Copy link
Member

bjorn3 commented Feb 9, 2021

This happens because -Cpanic=abort omits the unwinding tables necessary for unwinding. You can use -Cforce-unwind-tables=yes to add them back at the cost of an increased binary. The fact that you have part of a backtrace at all is because libstd is compiled with -Cpanic=unwind and as such does have unwinding tables available.

See rust-lang/backtrace-rs#397 for more information.

@m-ou-se
Copy link
Member

m-ou-se commented Mar 27, 2021

Maybe we should find a way to remove the run with `RUST_BACKTRACE=1` suggestion from the panic message when unwind tables are not available.

@bjorn3
Copy link
Member

bjorn3 commented Mar 27, 2021

How would that work? Plain checking for .eh_frame would almost always result in unwind tables for linked in native dependencies. And when one rust dependency doesn't have unwind tables, another may have. A better solution would be I think to handle this case more gracefully during unwinding by printing something like "unwind info unavailable for the next function; aborting".

@Swatinem
Copy link
Contributor Author

Thinking a bit more about this, I think linking the creation of unwind tables to the panic method is not a sensible thing.
"catching panics" and "creating stack traces" are two different concerns.

@martin-t
Copy link

I second this, I started using panic = "abort" after reading this article because my code is not written to handle random threads unwinding. I, however, still do want backtraces.

Having to add rustflags = ["-Cforce-unwind-tables=y"] to .cargo/config.toml is a bit unwieldy, especially when trying to be careful not to override other rustflags set somewhere else. Is there a better place?

Is it still possible to change the behavior of panic = "abort" to keep unwind tables? If not, then there should at least be a way to set -Cforce-unwind-tables from Cargo.toml.

I also think the behavior and the solution should be documented here.

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-bug Category: This is a bug. 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

5 participants