Skip to content

Add initial debug fmt for Backtrace #69038

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Feb 13, 2020
Merged
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 73 additions & 6 deletions src/libstd/backtrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,63 @@ 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();

write!(fmt, "Backtrace ")?;

let mut dbg = fmt.debug_list();

for frame in &capture.frames {
dbg.entries(&frame.symbols);
}

dbg.finish()
}
}

impl fmt::Debug for BacktraceSymbol {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut dbg = fmt.debug_struct("");

if let Some(fn_name) = self.name.as_ref().map(|b| backtrace::SymbolName::new(b)) {
dbg.field("fn", &format_args!("\"{}\"", fn_name));
} else {
dbg.field("fn", &"<unknown>");
}

if let Some(fname) = self.filename.as_ref() {
dbg.field("file", fname);
}

if let Some(line) = self.lineno.as_ref() {
dbg.field("line", line);
}

dbg.finish()
}
}

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::Full,
crate::env::current_dir().as_ref().ok(),
)
}
}

impl Backtrace {
/// Returns whether backtrace captures are enabled through environment
/// variables.
Expand Down Expand Up @@ -267,12 +324,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 Expand Up @@ -351,3 +402,19 @@ impl Capture {
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn debug_backtrace_fmt() {
let bt = Backtrace::capture();
eprintln!("uncaptured: {:?}", bt);
let bt = Backtrace::force_capture();
eprintln!("captured: {:?}", bt);
eprintln!("display print: {}", bt);
eprintln!("resolved: {:?}", bt);
unimplemented!();
}
}