Skip to content

Commit

Permalink
Add column number support to Backtrace
Browse files Browse the repository at this point in the history
Backtrace frames might include column numbers.
Print them if they are included.
  • Loading branch information
est31 committed Nov 15, 2020
1 parent 7504256 commit 43bfbb1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
11 changes: 9 additions & 2 deletions library/std/src/backtrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ struct BacktraceSymbol {
name: Option<Vec<u8>>,
filename: Option<BytesOrWide>,
lineno: Option<u32>,
colno: Option<u32>,
}

enum BytesOrWide {
Expand Down Expand Up @@ -197,6 +198,10 @@ impl fmt::Debug for Backtrace {

impl fmt::Debug for BacktraceSymbol {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
// FIXME: improve formatting: https://github.com/rust-lang/rust/issues/65280
// FIXME: Also, include column numbers into the debug format as Display already has them.
// Until there are stable per-frame accessors, the format shouldn't be changed:
// https://github.com/rust-lang/rust/issues/65280#issuecomment-638966585
write!(fmt, "{{ ")?;

if let Some(fn_name) = self.name.as_ref().map(|b| backtrace_rs::SymbolName::new(b)) {
Expand All @@ -209,7 +214,7 @@ impl fmt::Debug for BacktraceSymbol {
write!(fmt, ", file: \"{:?}\"", fname)?;
}

if let Some(line) = self.lineno.as_ref() {
if let Some(line) = self.lineno {
write!(fmt, ", line: {:?}", line)?;
}

Expand Down Expand Up @@ -381,14 +386,15 @@ impl fmt::Display for Backtrace {
f.print_raw(frame.frame.ip(), None, None, None)?;
} else {
for symbol in frame.symbols.iter() {
f.print_raw(
f.print_raw_with_column(
frame.frame.ip(),
symbol.name.as_ref().map(|b| backtrace_rs::SymbolName::new(b)),
symbol.filename.as_ref().map(|b| match b {
BytesOrWide::Bytes(w) => BytesOrWideString::Bytes(w),
BytesOrWide::Wide(w) => BytesOrWideString::Wide(w),
}),
symbol.lineno,
symbol.colno,
)?;
}
}
Expand Down Expand Up @@ -427,6 +433,7 @@ impl Capture {
BytesOrWideString::Wide(b) => BytesOrWide::Wide(b.to_owned()),
}),
lineno: symbol.lineno(),
colno: symbol.colno(),
});
});
}
Expand Down
4 changes: 4 additions & 0 deletions library/std/src/backtrace/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ fn test_debug() {
name: Some(b"std::backtrace::Backtrace::create".to_vec()),
filename: Some(BytesOrWide::Bytes(b"rust/backtrace.rs".to_vec())),
lineno: Some(100),
colno: None,
}],
},
BacktraceFrame {
Expand All @@ -21,6 +22,7 @@ fn test_debug() {
name: Some(b"__rust_maybe_catch_panic".to_vec()),
filename: None,
lineno: None,
colno: None,
}],
},
BacktraceFrame {
Expand All @@ -30,11 +32,13 @@ fn test_debug() {
name: Some(b"std::rt::lang_start_internal".to_vec()),
filename: Some(BytesOrWide::Bytes(b"rust/rt.rs".to_vec())),
lineno: Some(300),
colno: Some(5),
},
BacktraceSymbol {
name: Some(b"std::rt::lang_start".to_vec()),
filename: Some(BytesOrWide::Bytes(b"rust/rt.rs".to_vec())),
lineno: Some(400),
colno: None,
},
],
},
Expand Down

0 comments on commit 43bfbb1

Please sign in to comment.