Skip to content

Commit 9fd3886

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

File tree

3 files changed

+57
-47
lines changed

3 files changed

+57
-47
lines changed

library/std/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ backtrace = [
9292
'object/rustc-dep-of-std',
9393
'miniz_oxide/rustc-dep-of-std',
9494
]
95+
# Disable symbolization in backtraces. For use with -Zbuild-std.
96+
# FIXME: Ideally this should be an additive backtrace-symbolization feature
97+
backtrace-trace-only = []
9598

9699
panic-unwind = ["dep:panic_unwind"]
97100
compiler-builtins-c = ["alloc/compiler-builtins-c"]

library/std/src/sys/backtrace.rs

Lines changed: 53 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -68,61 +68,67 @@ unsafe fn _print_fmt(fmt: &mut fmt::Formatter<'_>, print_fmt: PrintFmt) -> fmt::
6868
return false;
6969
}
7070

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;
71+
if cfg!(feature = "backtrace-trace-only") {
72+
const HEX_WIDTH: usize = 2 + 2 * size_of::<usize>();
73+
let frame_ip = frame.ip();
74+
res = writeln!(bt_fmt.formatter(), "{idx:4}: {frame_ip:HEX_WIDTH$?}");
75+
} else {
76+
let mut hit = false;
77+
backtrace_rs::resolve_frame_unsynchronized(frame, |symbol| {
78+
hit = true;
79+
80+
// `__rust_end_short_backtrace` means we are done hiding symbols
81+
// for now. Print until we see `__rust_begin_short_backtrace`.
82+
if print_fmt == PrintFmt::Short {
83+
if let Some(sym) = symbol.name().and_then(|s| s.as_str()) {
84+
if sym.contains("__rust_end_short_backtrace") {
85+
print = true;
86+
return;
87+
}
88+
if print && sym.contains("__rust_begin_short_backtrace") {
89+
print = false;
90+
return;
91+
}
92+
if !print {
93+
omitted_count += 1;
94+
}
8995
}
9096
}
91-
}
9297

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-
);
98+
if print {
99+
if omitted_count > 0 {
100+
debug_assert!(print_fmt == PrintFmt::Short);
101+
// only print the message between the middle of frames
102+
if !first_omit {
103+
let _ = writeln!(
104+
bt_fmt.formatter(),
105+
" [... omitted {} frame{} ...]",
106+
omitted_count,
107+
if omitted_count > 1 { "s" } else { "" }
108+
);
109+
}
110+
first_omit = false;
111+
omitted_count = 0;
104112
}
105-
first_omit = false;
106-
omitted_count = 0;
113+
res = bt_fmt.frame().symbol(frame, symbol);
107114
}
108-
res = bt_fmt.frame().symbol(frame, symbol);
115+
});
116+
#[cfg(target_os = "nto")]
117+
if libc::__my_thread_exit as *mut libc::c_void == frame.ip() {
118+
if !hit && print {
119+
use crate::backtrace_rs::SymbolName;
120+
res = bt_fmt.frame().print_raw(
121+
frame.ip(),
122+
Some(SymbolName::new("__my_thread_exit".as_bytes())),
123+
None,
124+
None,
125+
);
126+
}
127+
return false;
109128
}
110-
});
111-
#[cfg(target_os = "nto")]
112-
if libc::__my_thread_exit as *mut libc::c_void == frame.ip() {
113129
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-
);
130+
res = bt_fmt.frame().print_raw(frame.ip(), None, None, None);
121131
}
122-
return false;
123-
}
124-
if !hit && print {
125-
res = bt_fmt.frame().print_raw(frame.ip(), None, None, None);
126132
}
127133

128134
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)