Skip to content

Commit

Permalink
Rollup merge of rust-lang#69038 - yaahc:backtrace-debug, r=dtolnay
Browse files Browse the repository at this point in the history
Add initial debug fmt for Backtrace

Fixes the first point in rust-lang#65280

related to rust-lang#53487
  • Loading branch information
Dylan-DPC authored Feb 12, 2020
2 parents 29dd5df + ec8ee7f commit 5a800d7
Showing 1 changed file with 63 additions and 6 deletions.
69 changes: 63 additions & 6 deletions src/libstd/backtrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,69 @@ enum BytesOrWide {
Wide(Vec<u16>),
}

impl fmt::Debug for Backtrace {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut capture = match &self.inner {
Inner::Unsupported => return fmt.write_str("unsupported backtrace"),
Inner::Disabled => return fmt.write_str("disabled backtrace"),
Inner::Captured(c) => c.lock().unwrap(),
};
capture.resolve();

let frames = &capture.frames[capture.actual_start..];

write!(fmt, "Backtrace ")?;

let mut dbg = fmt.debug_list();

for frame in frames {
if frame.frame.ip().is_null() {
continue;
}

dbg.entries(&frame.symbols);
}

dbg.finish()
}
}

impl fmt::Debug for BacktraceSymbol {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(fmt, "{{ ")?;

if let Some(fn_name) = self.name.as_ref().map(|b| backtrace::SymbolName::new(b)) {
write!(fmt, "fn: \"{:#}\"", fn_name)?;
} else {
write!(fmt, "fn: \"<unknown>\"")?;
}

if let Some(fname) = self.filename.as_ref() {
write!(fmt, ", file: {:?}", fname)?;
}

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

write!(fmt, " }}")
}
}

impl fmt::Debug for BytesOrWide {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
output_filename(
fmt,
match self {
BytesOrWide::Bytes(w) => BytesOrWideString::Bytes(w),
BytesOrWide::Wide(w) => BytesOrWideString::Wide(w),
},
backtrace::PrintFmt::Short,
crate::env::current_dir().as_ref().ok(),
)
}
}

impl Backtrace {
/// Returns whether backtrace captures are enabled through environment
/// variables.
Expand Down Expand Up @@ -267,12 +330,6 @@ impl Backtrace {
}

impl fmt::Display for Backtrace {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(self, fmt)
}
}

impl fmt::Debug for Backtrace {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut capture = match &self.inner {
Inner::Unsupported => return fmt.write_str("unsupported backtrace"),
Expand Down

0 comments on commit 5a800d7

Please sign in to comment.