Skip to content

Commit

Permalink
fix: Clean up printing of backtraces
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Oct 10, 2024
1 parent 78a613f commit 171be96
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 47 deletions.
94 changes: 48 additions & 46 deletions src/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,10 @@ impl Report {
}

fn render_backtrace() -> String {
//We skip 3 frames from backtrace library
//Then we skip 3 frames for our own library
//(including closure that we set as hook)
//Then we skip 2 functions from Rust's runtime
//that calls panic hook
const SKIP_FRAMES_NUM: usize = 8;
//We take padding for address and extra two letters
//to pad after index.
#[allow(unused_qualifications)] // needed for pre-1.80 MSRV
const HEX_WIDTH: usize = mem::size_of::<usize>() + 2;
const HEX_WIDTH: usize = mem::size_of::<usize>() * 2 + 2;
//Padding for next lines after frame's address
const NEXT_SYMBOL_PADDING: usize = HEX_WIDTH + 6;

Expand All @@ -95,50 +89,58 @@ fn render_backtrace() -> String {
//We need to print its address
//and symbol(e.g. function name),
//if it is available
for (idx, frame) in Backtrace::new()
.frames()
.iter()
.skip(SKIP_FRAMES_NUM)
.enumerate()
{
let ip = frame.ip();
let _ = write!(backtrace, "\n{idx:4}: {ip:HEX_WIDTH$?}");

let mut entry_idx = 0;
let mut skip = true;
for frame in Backtrace::new().frames().iter() {
let symbols = frame.symbols();
if symbols.is_empty() {
let _ = write!(backtrace, " - <unresolved>");
continue;
}

for (idx, symbol) in symbols.iter().enumerate() {
//Print symbols from this address,
//if there are several addresses
//we need to put it on next line
if idx != 0 {
let _ = write!(backtrace, "\n{:1$}", "", NEXT_SYMBOL_PADDING);
}

if let Some(name) = symbol.name() {
let _ = write!(backtrace, " - {name}");
} else {
let _ = write!(backtrace, " - <unknown>");
}

//See if there is debug information with file name and line
if let (Some(file), Some(line)) = (symbol.filename(), symbol.lineno()) {
let _ = write!(
backtrace,
"\n{:3$}at {}:{}",
"",
file.display(),
line,
NEXT_SYMBOL_PADDING
);
let ip = frame.ip();
let _ = writeln!(backtrace, "{entry_idx:4}: {ip:HEX_WIDTH$?} - <unresolved>");
entry_idx += 1;
} else {
let mut first = true;
for symbol in symbols.iter() {
if skip {
if symbol.name().map(|s| s.to_string()).as_deref() == Some("rust_begin_unwind")
{
skip = false;
}
continue;
}
//Print symbols from this address,
//if there are several addresses
//we need to put it on next line
if first {
let ip = frame.ip();
let _ = write!(backtrace, "{entry_idx:4}: {ip:HEX_WIDTH$?}");
first = false;
} else {
let _ = write!(backtrace, "{entry_idx:4}: {:HEX_WIDTH$?}", "");
}

if let Some(name) = symbol.name() {
let _ = write!(backtrace, " - {name}");
} else {
let _ = write!(backtrace, " - <unknown>");
}

let _ = writeln!(backtrace);
entry_idx += 1;

//See if there is debug information with file name and line
if let (Some(file), Some(line)) = (symbol.filename(), symbol.lineno()) {
let _ = writeln!(
backtrace,
"{:3$}at {}:{}",
"",
file.display(),
line,
NEXT_SYMBOL_PADDING
);
}
}
}
}

let _ = writeln!(backtrace);

backtrace
}
1 change: 0 additions & 1 deletion tests/single-panic/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ Panic occurred in file 'tests/single-panic/src/main.rs' at line [..]
"cause" = "OMG EVERYTHING IS ON FIRE!!!"
"method" = "Panic"
"backtrace" = """
...
"""
Expand Down

0 comments on commit 171be96

Please sign in to comment.