-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #93101 - Mark-Simulacrum:library-backtrace, r=yaahc
Support configuring whether to capture backtraces at runtime Tracking issue: #93346 This adds a new API to the `std::panic` module which configures whether and how the default panic hook will emit a backtrace when a panic occurs. After discussion with `@yaahc` on [Zulip](https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/backtrace.20lib.20vs.2E.20panic), this PR chooses to avoid adjusting or seeking to provide a similar API for the (currently unstable) std::backtrace API. It seems likely that the users of that API may wish to expose more specific settings rather than just a global one (e.g., emulating the `env_logger`, `tracing` per-module configuration) to avoid the cost of capture in hot code. The API added here could plausibly be copied and/or re-exported directly from std::backtrace relatively easily, but I don't think that's the right call as of now. ```rust mod panic { #[derive(Copy, Clone, Debug, PartialEq, Eq)] #[non_exhaustive] pub enum BacktraceStyle { Short, Full, Off, } fn set_backtrace_style(BacktraceStyle); fn get_backtrace_style() -> Option<BacktraceStyle>; } ``` Several unresolved questions: * Do we need to move to a thread-local or otherwise more customizable strategy for whether to capture backtraces? See [this comment](#79085 (comment)) for some potential use cases for this. * Proposed answer: no, leave this for third-party hooks. * Bikeshed on naming of all the options, as usual. * Should BacktraceStyle be moved into `std::backtrace`? * It's already somewhat annoying to import and/or re-type the `std::panic::` prefix necessary to use these APIs, probably adding a second module to the mix isn't worth it. Note that PR #79085 proposed a much simpler API, but particularly in light of the desire to fully replace setting environment variables via `env::set_var` to control the backtrace API, a more complete API seems preferable. This PR likely subsumes that one.
- Loading branch information
Showing
7 changed files
with
165 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
thread 'main' panicked at 'explicit panic', $DIR/runtime-switch.rs:24:5 | ||
stack backtrace: | ||
0: std::panicking::begin_panic | ||
1: runtime_switch::main | ||
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Test for std::panic::set_backtrace_style. | ||
|
||
// compile-flags: -O | ||
// run-fail | ||
// check-run-results | ||
// exec-env:RUST_BACKTRACE=0 | ||
|
||
// ignore-msvc see #62897 and `backtrace-debuginfo.rs` test | ||
// ignore-android FIXME #17520 | ||
// ignore-openbsd no support for libbacktrace without filename | ||
// ignore-wasm no panic or subprocess support | ||
// ignore-emscripten no panic or subprocess support | ||
// ignore-sgx no subprocess support | ||
|
||
// NOTE(eddyb) output differs between symbol mangling schemes | ||
// revisions: legacy v0 | ||
// [legacy] compile-flags: -Zunstable-options -Csymbol-mangling-version=legacy | ||
// [v0] compile-flags: -Csymbol-mangling-version=v0 | ||
|
||
#![feature(panic_backtrace_config)] | ||
|
||
fn main() { | ||
std::panic::set_backtrace_style(std::panic::BacktraceStyle::Short); | ||
panic!() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
thread 'main' panicked at 'explicit panic', $DIR/runtime-switch.rs:24:5 | ||
stack backtrace: | ||
0: std::panicking::begin_panic::<&str> | ||
1: runtime_switch::main | ||
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters