Skip to content

Commit f7963bb

Browse files
committed
Add experimental backtrace-trace-only std feature
1 parent e9182f1 commit f7963bb

File tree

3 files changed

+59
-48
lines changed

3 files changed

+59
-48
lines changed

library/std/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ backtrace = [
9292
'object/rustc-dep-of-std',
9393
'miniz_oxide/rustc-dep-of-std',
9494
]
95+
backtrace-trace-only = []
9596

9697
panic-unwind = ["dep:panic_unwind"]
9798
compiler-builtins-c = ["alloc/compiler-builtins-c"]

library/std/src/sys/backtrace.rs

Lines changed: 57 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,12 @@ impl BacktraceLock<'_> {
4343
}
4444
}
4545

46-
unsafe fn _print_fmt(fmt: &mut fmt::Formatter<'_>, print_fmt: PrintFmt) -> fmt::Result {
46+
unsafe fn _print_fmt(fmt: &mut fmt::Formatter<'_>, mut print_fmt: PrintFmt) -> fmt::Result {
47+
// If we're printing without symbols then always print full backtraces
48+
// as otherwise there's no useful information to show.
49+
if cfg!(feature = "backtrace-trace-only") {
50+
print_fmt = PrintFmt::Full;
51+
}
4752
// Always 'fail' to get the cwd when running under Miri -
4853
// this allows Miri to display backtraces in isolation mode
4954
let cwd = if !cfg!(miri) { env::current_dir().ok() } else { None };
@@ -68,61 +73,65 @@ unsafe fn _print_fmt(fmt: &mut fmt::Formatter<'_>, print_fmt: PrintFmt) -> fmt::
6873
return false;
6974
}
7075

71-
let mut hit = false;
72-
backtrace_rs::resolve_frame_unsynchronized(frame, |symbol| {
73-
hit = true;
74-
75-
// `__rust_end_short_backtrace` means we are done hiding symbols
76-
// for now. Print until we see `__rust_begin_short_backtrace`.
77-
if print_fmt == PrintFmt::Short {
78-
if let Some(sym) = symbol.name().and_then(|s| s.as_str()) {
79-
if sym.contains("__rust_end_short_backtrace") {
80-
print = true;
81-
return;
82-
}
83-
if print && sym.contains("__rust_begin_short_backtrace") {
84-
print = false;
85-
return;
86-
}
87-
if !print {
88-
omitted_count += 1;
76+
if cfg!(feature = "backtrace-trace-only") {
77+
res = bt_fmt.frame().print_raw(frame.ip(), None, None, None);
78+
} else {
79+
let mut hit = false;
80+
backtrace_rs::resolve_frame_unsynchronized(frame, |symbol| {
81+
hit = true;
82+
83+
// `__rust_end_short_backtrace` means we are done hiding symbols
84+
// for now. Print until we see `__rust_begin_short_backtrace`.
85+
if print_fmt == PrintFmt::Short {
86+
if let Some(sym) = symbol.name().and_then(|s| s.as_str()) {
87+
if sym.contains("__rust_end_short_backtrace") {
88+
print = true;
89+
return;
90+
}
91+
if print && sym.contains("__rust_begin_short_backtrace") {
92+
print = false;
93+
return;
94+
}
95+
if !print {
96+
omitted_count += 1;
97+
}
8998
}
9099
}
91-
}
92100

93-
if print {
94-
if omitted_count > 0 {
95-
debug_assert!(print_fmt == PrintFmt::Short);
96-
// only print the message between the middle of frames
97-
if !first_omit {
98-
let _ = writeln!(
99-
bt_fmt.formatter(),
100-
" [... omitted {} frame{} ...]",
101-
omitted_count,
102-
if omitted_count > 1 { "s" } else { "" }
103-
);
101+
if print {
102+
if omitted_count > 0 {
103+
debug_assert!(print_fmt == PrintFmt::Short);
104+
// only print the message between the middle of frames
105+
if !first_omit {
106+
let _ = writeln!(
107+
bt_fmt.formatter(),
108+
" [... omitted {} frame{} ...]",
109+
omitted_count,
110+
if omitted_count > 1 { "s" } else { "" }
111+
);
112+
}
113+
first_omit = false;
114+
omitted_count = 0;
104115
}
105-
first_omit = false;
106-
omitted_count = 0;
116+
res = bt_fmt.frame().symbol(frame, symbol);
107117
}
108-
res = bt_fmt.frame().symbol(frame, symbol);
118+
});
119+
#[cfg(target_os = "nto")]
120+
if libc::__my_thread_exit as *mut libc::c_void == frame.ip() {
121+
if !hit && print {
122+
use crate::backtrace_rs::SymbolName;
123+
res = bt_fmt.frame().print_raw(
124+
frame.ip(),
125+
Some(SymbolName::new("__my_thread_exit".as_bytes())),
126+
None,
127+
None,
128+
);
129+
}
130+
return false;
109131
}
110-
});
111-
#[cfg(target_os = "nto")]
112-
if libc::__my_thread_exit as *mut libc::c_void == frame.ip() {
113132
if !hit && print {
114-
use crate::backtrace_rs::SymbolName;
115-
res = bt_fmt.frame().print_raw(
116-
frame.ip(),
117-
Some(SymbolName::new("__my_thread_exit".as_bytes())),
118-
None,
119-
None,
120-
);
133+
res = bt_fmt.frame().print_raw(frame.ip(), None, None, None);
121134
}
122-
return false;
123-
}
124-
if !hit && print {
125-
res = bt_fmt.frame().print_raw(frame.ip(), None, None, None);
126135
}
127136

128137
idx += 1;

library/sysroot/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ test = { path = "../test", public = true }
2020
[features]
2121
default = ["std_detect_file_io", "std_detect_dlsym_getauxval", "panic-unwind"]
2222
backtrace = ["std/backtrace"]
23+
backtrace-trace-only = ["std/backtrace-trace-only"]
2324
compiler-builtins-c = ["std/compiler-builtins-c"]
2425
compiler-builtins-mem = ["std/compiler-builtins-mem"]
2526
compiler-builtins-no-asm = ["std/compiler-builtins-no-asm"]

0 commit comments

Comments
 (0)