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 12, 2020
1 parent f7801d6 commit 016146d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
7 changes: 5 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 @@ -209,7 +210,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 +382,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 +429,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 016146d

Please sign in to comment.