]> git.lizzy.rs Git - rust.git/blobdiff - src/libstd/backtrace.rs
Auto merge of #68943 - ecstatic-morse:no-useless-drop-on-enum-variants, r=matthewjasper
[rust.git] / src / libstd / backtrace.rs
index 5ba1c940251dc3b92233fd62c59d301009ad39e7..97db0ff3791d728f00fa24312607c7f0409ca157 100644 (file)
@@ -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.
@@ -241,7 +304,7 @@ fn create(ip: usize) -> Backtrace {
         // If no frames came out assume that this is an unsupported platform
         // since `backtrace` doesn't provide a way of learning this right now,
         // and this should be a good enough approximation.
-        let inner = if frames.len() == 0 {
+        let inner = if frames.is_empty() {
             Inner::Unsupported
         } else {
             Inner::Captured(Mutex::new(Capture {
@@ -267,12 +330,6 @@ pub fn status(&self) -> BacktraceStatus {
 }
 
 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"),