|
| 1 | +use crate::BytesOrWideString; |
| 2 | +use core::ffi::c_void; |
| 3 | +use core::fmt; |
| 4 | + |
| 5 | +const HEX_WIDTH: usize = 2 + 2 * core::mem::size_of::<usize>(); |
| 6 | + |
| 7 | +#[cfg(target_os = "fuchsia")] |
| 8 | +mod fuchsia; |
| 9 | + |
| 10 | +/// A formatter for backtraces. |
| 11 | +pub struct BacktraceFmt<'a, 'b> { |
| 12 | + fmt: &'a mut fmt::Formatter<'b>, |
| 13 | + frame_index: usize, |
| 14 | + format: PrintFmt, |
| 15 | + print_path: &'a mut (FnMut(&mut fmt::Formatter, BytesOrWideString) -> fmt::Result + 'b), |
| 16 | +} |
| 17 | + |
| 18 | +/// The styles of printing that we can print |
| 19 | +pub enum PrintFmt { |
| 20 | + /// Prints a terser backtrace which ideally only contains relevant information |
| 21 | + Short, |
| 22 | + /// Prints a backtrace that contains all possible information |
| 23 | + Full, |
| 24 | + #[doc(hidden)] |
| 25 | + __Nonexhaustive, |
| 26 | +} |
| 27 | + |
| 28 | +impl<'a, 'b> BacktraceFmt<'a, 'b> { |
| 29 | + /// Create a new `BacktraceFmt` which will write output to the provided `fmt`. |
| 30 | + pub fn new( |
| 31 | + fmt: &'a mut fmt::Formatter<'b>, |
| 32 | + format: PrintFmt, |
| 33 | + print_path: &'a mut (FnMut(&mut fmt::Formatter, BytesOrWideString) -> fmt::Result + 'b), |
| 34 | + ) -> Self { |
| 35 | + BacktraceFmt { |
| 36 | + fmt, |
| 37 | + frame_index: 0, |
| 38 | + format, |
| 39 | + print_path, |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /// Adds the current shared library context to the output. |
| 44 | + /// |
| 45 | + /// This is required for the resulting frames to be symbolized. |
| 46 | + pub fn add_context(&mut self) -> fmt::Result { |
| 47 | + self.fmt.write_str("stack backtrace:\n")?; |
| 48 | + #[cfg(target_os = "fuchsia")] |
| 49 | + fuchsia::print_dso_context(self.fmt)?; |
| 50 | + Ok(()) |
| 51 | + } |
| 52 | + |
| 53 | + /// Adds a frame to the backtrace output. |
| 54 | + pub fn frame(&mut self) -> Result<FrameFmt<'_, 'a, 'b>, fmt::Error> { |
| 55 | + Ok(FrameFmt { |
| 56 | + fmt: self, |
| 57 | + symbol_index: 0, |
| 58 | + }) |
| 59 | + } |
| 60 | + |
| 61 | + /// Completes the backtrace output. |
| 62 | + pub fn finish(&mut self) -> fmt::Result { |
| 63 | + // Currently a no-op-- including this hook to allow for future additions. |
| 64 | + Ok(()) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +/// A formatter for backtraces. |
| 69 | +pub struct FrameFmt<'fmt, 'a, 'b> { |
| 70 | + fmt: &'fmt mut BacktraceFmt<'a, 'b>, |
| 71 | + symbol_index: usize, |
| 72 | +} |
| 73 | + |
| 74 | +impl FrameFmt<'_, '_, '_> { |
| 75 | + /// Prints a `BacktraceFrame` with this frame formatter. |
| 76 | + /// |
| 77 | + /// This will recusrively print all `BacktraceSymbol` instances within the |
| 78 | + /// `BacktraceFrame`. |
| 79 | + #[cfg(feature = "std")] |
| 80 | + pub fn backtrace_frame(&mut self, frame: &crate::BacktraceFrame) -> fmt::Result { |
| 81 | + let symbols = frame.symbols(); |
| 82 | + for symbol in symbols { |
| 83 | + self.backtrace_symbol(frame, symbol)?; |
| 84 | + } |
| 85 | + if symbols.is_empty() { |
| 86 | + self.print_raw(frame.ip(), None, None, None)?; |
| 87 | + } |
| 88 | + Ok(()) |
| 89 | + } |
| 90 | + |
| 91 | + /// Prints a `BacktraceSymbol` within a `BacktraceFrame`. |
| 92 | + #[cfg(feature = "std")] |
| 93 | + pub fn backtrace_symbol( |
| 94 | + &mut self, |
| 95 | + frame: &crate::BacktraceFrame, |
| 96 | + symbol: &crate::BacktraceSymbol, |
| 97 | + ) -> fmt::Result { |
| 98 | + self.print_raw( |
| 99 | + frame.ip(), |
| 100 | + symbol.name(), |
| 101 | + symbol |
| 102 | + .filename() |
| 103 | + .and_then(|p| Some(BytesOrWideString::Bytes(p.to_str()?.as_bytes()))), |
| 104 | + symbol.lineno(), |
| 105 | + )?; |
| 106 | + Ok(()) |
| 107 | + } |
| 108 | + |
| 109 | + /// Prints a raw traced `Frame` and `Symbol`, typically from within the raw |
| 110 | + /// callbacks of this crate. |
| 111 | + pub fn symbol(&mut self, frame: &crate::Frame, symbol: &crate::Symbol) -> fmt::Result { |
| 112 | + self.print_raw( |
| 113 | + frame.ip(), |
| 114 | + symbol.name(), |
| 115 | + symbol.filename_raw(), |
| 116 | + symbol.lineno(), |
| 117 | + )?; |
| 118 | + Ok(()) |
| 119 | + } |
| 120 | + |
| 121 | + /// Adds a frame to the backtrace output. |
| 122 | + pub fn print_raw( |
| 123 | + &mut self, |
| 124 | + frame_ip: *mut c_void, |
| 125 | + symbol_name: Option<crate::SymbolName>, |
| 126 | + filename: Option<BytesOrWideString>, |
| 127 | + lineno: Option<u32>, |
| 128 | + ) -> fmt::Result { |
| 129 | + if cfg!(target_os = "fuchsia") { |
| 130 | + self.print_raw_fuchsia(frame_ip)?; |
| 131 | + } else { |
| 132 | + self.print_raw_generic(frame_ip, symbol_name, filename, lineno)?; |
| 133 | + } |
| 134 | + self.symbol_index += 1; |
| 135 | + Ok(()) |
| 136 | + } |
| 137 | + |
| 138 | + #[allow(unused_mut)] |
| 139 | + fn print_raw_generic( |
| 140 | + &mut self, |
| 141 | + mut frame_ip: *mut c_void, |
| 142 | + symbol_name: Option<crate::SymbolName>, |
| 143 | + filename: Option<BytesOrWideString>, |
| 144 | + lineno: Option<u32>, |
| 145 | + ) -> fmt::Result { |
| 146 | + // No need to print "null" frames, it basically just means that the |
| 147 | + // system backtrace was a bit eager to trace back super far. |
| 148 | + if let PrintFmt::Short = self.fmt.format { |
| 149 | + if frame_ip.is_null() { |
| 150 | + return Ok(()); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + // To reduce TCB size in Sgx enclave, we do not want to implement symbol |
| 155 | + // resolution functionality. Rather, we can print the offset of the |
| 156 | + // address here, which could be later mapped to correct function. |
| 157 | + #[cfg(all(feature = "std", target_env = "sgx"))] |
| 158 | + { |
| 159 | + frame_ip = |
| 160 | + usize::wrapping_sub(frame_ip, std::os::fortanix_sgx::mem::image_base() as _) as _; |
| 161 | + } |
| 162 | + |
| 163 | + if self.symbol_index == 0 { |
| 164 | + write!(self.fmt.fmt, "{:4}: ", self.fmt.frame_index)?; |
| 165 | + if let PrintFmt::Full = self.fmt.format { |
| 166 | + write!(self.fmt.fmt, "{:1$?} - ", frame_ip, HEX_WIDTH)?; |
| 167 | + } |
| 168 | + } else { |
| 169 | + write!(self.fmt.fmt, " ")?; |
| 170 | + if let PrintFmt::Full = self.fmt.format { |
| 171 | + write!(self.fmt.fmt, "{:1$}", "", HEX_WIDTH + 3)?; |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + match (symbol_name, &self.fmt.format) { |
| 176 | + (Some(name), PrintFmt::Short) => write!(self.fmt.fmt, "{}", name)?, |
| 177 | + (Some(name), PrintFmt::Full) => write!(self.fmt.fmt, "{:#}", name)?, |
| 178 | + (None, _) | (_, PrintFmt::__Nonexhaustive) => write!(self.fmt.fmt, "<unknown>")?, |
| 179 | + } |
| 180 | + self.fmt.fmt.write_str("\n")?; |
| 181 | + |
| 182 | + if let (Some(file), Some(line)) = (filename, lineno) { |
| 183 | + self.print_fileline(file, line)?; |
| 184 | + } |
| 185 | + |
| 186 | + Ok(()) |
| 187 | + } |
| 188 | + |
| 189 | + fn print_fileline(&mut self, file: BytesOrWideString, line: u32) -> fmt::Result { |
| 190 | + match self.fmt.format { |
| 191 | + PrintFmt::Full => write!(self.fmt.fmt, "{:1$}", "", HEX_WIDTH)?, |
| 192 | + PrintFmt::Short | PrintFmt::__Nonexhaustive => {} |
| 193 | + } |
| 194 | + write!(self.fmt.fmt, " at ")?; |
| 195 | + (self.fmt.print_path)(self.fmt.fmt, file)?; |
| 196 | + write!(self.fmt.fmt, ":{}\n", line)?; |
| 197 | + Ok(()) |
| 198 | + } |
| 199 | + |
| 200 | + fn print_raw_fuchsia(&mut self, frame_ip: *mut c_void) -> fmt::Result { |
| 201 | + // We only care about the first symbol of a frame |
| 202 | + if self.symbol_index == 0 { |
| 203 | + self.fmt.fmt.write_str("{{{bt:")?; |
| 204 | + write!(self.fmt.fmt, "{}:{:?}", self.fmt.frame_index, frame_ip)?; |
| 205 | + self.fmt.fmt.write_str("}}}\n")?; |
| 206 | + } |
| 207 | + Ok(()) |
| 208 | + } |
| 209 | +} |
| 210 | + |
| 211 | +impl Drop for FrameFmt<'_, '_, '_> { |
| 212 | + fn drop(&mut self) { |
| 213 | + self.fmt.frame_index += 1; |
| 214 | + } |
| 215 | +} |
0 commit comments