Skip to content

Commit 43bfbb1

Browse files
committed
Add column number support to Backtrace
Backtrace frames might include column numbers. Print them if they are included.
1 parent 7504256 commit 43bfbb1

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

library/std/src/backtrace.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ struct BacktraceSymbol {
161161
name: Option<Vec<u8>>,
162162
filename: Option<BytesOrWide>,
163163
lineno: Option<u32>,
164+
colno: Option<u32>,
164165
}
165166

166167
enum BytesOrWide {
@@ -197,6 +198,10 @@ impl fmt::Debug for Backtrace {
197198

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

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

212-
if let Some(line) = self.lineno.as_ref() {
217+
if let Some(line) = self.lineno {
213218
write!(fmt, ", line: {:?}", line)?;
214219
}
215220

@@ -381,14 +386,15 @@ impl fmt::Display for Backtrace {
381386
f.print_raw(frame.frame.ip(), None, None, None)?;
382387
} else {
383388
for symbol in frame.symbols.iter() {
384-
f.print_raw(
389+
f.print_raw_with_column(
385390
frame.frame.ip(),
386391
symbol.name.as_ref().map(|b| backtrace_rs::SymbolName::new(b)),
387392
symbol.filename.as_ref().map(|b| match b {
388393
BytesOrWide::Bytes(w) => BytesOrWideString::Bytes(w),
389394
BytesOrWide::Wide(w) => BytesOrWideString::Wide(w),
390395
}),
391396
symbol.lineno,
397+
symbol.colno,
392398
)?;
393399
}
394400
}
@@ -427,6 +433,7 @@ impl Capture {
427433
BytesOrWideString::Wide(b) => BytesOrWide::Wide(b.to_owned()),
428434
}),
429435
lineno: symbol.lineno(),
436+
colno: symbol.colno(),
430437
});
431438
});
432439
}

library/std/src/backtrace/tests.rs

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ fn test_debug() {
1313
name: Some(b"std::backtrace::Backtrace::create".to_vec()),
1414
filename: Some(BytesOrWide::Bytes(b"rust/backtrace.rs".to_vec())),
1515
lineno: Some(100),
16+
colno: None,
1617
}],
1718
},
1819
BacktraceFrame {
@@ -21,6 +22,7 @@ fn test_debug() {
2122
name: Some(b"__rust_maybe_catch_panic".to_vec()),
2223
filename: None,
2324
lineno: None,
25+
colno: None,
2426
}],
2527
},
2628
BacktraceFrame {
@@ -30,11 +32,13 @@ fn test_debug() {
3032
name: Some(b"std::rt::lang_start_internal".to_vec()),
3133
filename: Some(BytesOrWide::Bytes(b"rust/rt.rs".to_vec())),
3234
lineno: Some(300),
35+
colno: Some(5),
3336
},
3437
BacktraceSymbol {
3538
name: Some(b"std::rt::lang_start".to_vec()),
3639
filename: Some(BytesOrWide::Bytes(b"rust/rt.rs".to_vec())),
3740
lineno: Some(400),
41+
colno: None,
3842
},
3943
],
4044
},

0 commit comments

Comments
 (0)