]> git.lizzy.rs Git - rust.git/blob - library/std/src/backtrace.rs
Rollup merge of #104641 - tshepang:grammar, r=Mark-Simulacrum
[rust.git] / library / std / src / backtrace.rs
1 //! Support for capturing a stack backtrace of an OS thread
2 //!
3 //! This module contains the support necessary to capture a stack backtrace of a
4 //! running OS thread from the OS thread itself. The `Backtrace` type supports
5 //! capturing a stack trace via the `Backtrace::capture` and
6 //! `Backtrace::force_capture` functions.
7 //!
8 //! A backtrace is typically quite handy to attach to errors (e.g. types
9 //! implementing `std::error::Error`) to get a causal chain of where an error
10 //! was generated.
11 //!
12 //! ## Accuracy
13 //!
14 //! Backtraces are attempted to be as accurate as possible, but no guarantees
15 //! are provided about the exact accuracy of a backtrace. Instruction pointers,
16 //! symbol names, filenames, line numbers, etc, may all be incorrect when
17 //! reported. Accuracy is attempted on a best-effort basis, however, any bug
18 //! reports are always welcome to indicate areas of improvement!
19 //!
20 //! For most platforms a backtrace with a filename/line number requires that
21 //! programs be compiled with debug information. Without debug information
22 //! filenames/line numbers will not be reported.
23 //!
24 //! ## Platform support
25 //!
26 //! Not all platforms that libstd compiles for support capturing backtraces.
27 //! Some platforms simply do nothing when capturing a backtrace. To check
28 //! whether the platform supports capturing backtraces you can consult the
29 //! `BacktraceStatus` enum as a result of `Backtrace::status`.
30 //!
31 //! Like above with accuracy platform support is done on a best effort basis.
32 //! Sometimes libraries might not be available at runtime or something may go
33 //! wrong which would cause a backtrace to not be captured. Please feel free to
34 //! report issues with platforms where a backtrace cannot be captured though!
35 //!
36 //! ## Environment Variables
37 //!
38 //! The `Backtrace::capture` function might not actually capture a backtrace by
39 //! default. Its behavior is governed by two environment variables:
40 //!
41 //! * `RUST_LIB_BACKTRACE` - if this is set to `0` then `Backtrace::capture`
42 //!   will never capture a backtrace. Any other value set will enable
43 //!   `Backtrace::capture`.
44 //!
45 //! * `RUST_BACKTRACE` - if `RUST_LIB_BACKTRACE` is not set, then this variable
46 //!   is consulted with the same rules of `RUST_LIB_BACKTRACE`.
47 //!
48 //! * If neither of the above env vars are set, then `Backtrace::capture` will
49 //!   be disabled.
50 //!
51 //! Capturing a backtrace can be a quite expensive runtime operation, so the
52 //! environment variables allow either forcibly disabling this runtime
53 //! performance hit or allow selectively enabling it in some programs.
54 //!
55 //! Note that the `Backtrace::force_capture` function can be used to ignore
56 //! these environment variables. Also note that the state of environment
57 //! variables is cached once the first backtrace is created, so altering
58 //! `RUST_LIB_BACKTRACE` or `RUST_BACKTRACE` at runtime might not actually change
59 //! how backtraces are captured.
60
61 #![stable(feature = "backtrace", since = "1.65.0")]
62
63 #[cfg(test)]
64 mod tests;
65
66 // NB: A note on resolution of a backtrace:
67 //
68 // Backtraces primarily happen in two steps, one is where we actually capture
69 // the stack backtrace, giving us a list of instruction pointers corresponding
70 // to stack frames. Next we take these instruction pointers and, one-by-one,
71 // turn them into a human readable name (like `main`).
72 //
73 // The first phase can be somewhat expensive (walking the stack), especially
74 // on MSVC where debug information is consulted to return inline frames each as
75 // their own frame. The second phase, however, is almost always extremely
76 // expensive (on the order of milliseconds sometimes) when it's consulting debug
77 // information.
78 //
79 // We attempt to amortize this cost as much as possible by delaying resolution
80 // of an address to a human readable name for as long as possible. When
81 // `Backtrace::create` is called to capture a backtrace it doesn't actually
82 // perform any symbol resolution, but rather we lazily resolve symbols only just
83 // before they're needed for printing. This way we can make capturing a
84 // backtrace and throwing it away much cheaper, but actually printing a
85 // backtrace is still basically the same cost.
86 //
87 // This strategy comes at the cost of some synchronization required inside of a
88 // `Backtrace`, but that's a relatively small price to pay relative to capturing
89 // a backtrace or actually symbolizing it.
90
91 use crate::backtrace_rs::{self, BytesOrWideString};
92 use crate::cell::UnsafeCell;
93 use crate::env;
94 use crate::ffi::c_void;
95 use crate::fmt;
96 use crate::sync::atomic::{AtomicUsize, Ordering::Relaxed};
97 use crate::sync::Once;
98 use crate::sys_common::backtrace::{lock, output_filename};
99 use crate::vec::Vec;
100
101 /// A captured OS thread stack backtrace.
102 ///
103 /// This type represents a stack backtrace for an OS thread captured at a
104 /// previous point in time. In some instances the `Backtrace` type may
105 /// internally be empty due to configuration. For more information see
106 /// `Backtrace::capture`.
107 #[stable(feature = "backtrace", since = "1.65.0")]
108 #[must_use]
109 pub struct Backtrace {
110     inner: Inner,
111 }
112
113 /// The current status of a backtrace, indicating whether it was captured or
114 /// whether it is empty for some other reason.
115 #[stable(feature = "backtrace", since = "1.65.0")]
116 #[non_exhaustive]
117 #[derive(Debug, PartialEq, Eq)]
118 pub enum BacktraceStatus {
119     /// Capturing a backtrace is not supported, likely because it's not
120     /// implemented for the current platform.
121     #[stable(feature = "backtrace", since = "1.65.0")]
122     Unsupported,
123     /// Capturing a backtrace has been disabled through either the
124     /// `RUST_LIB_BACKTRACE` or `RUST_BACKTRACE` environment variables.
125     #[stable(feature = "backtrace", since = "1.65.0")]
126     Disabled,
127     /// A backtrace has been captured and the `Backtrace` should print
128     /// reasonable information when rendered.
129     #[stable(feature = "backtrace", since = "1.65.0")]
130     Captured,
131 }
132
133 enum Inner {
134     Unsupported,
135     Disabled,
136     Captured(LazilyResolvedCapture),
137 }
138
139 struct Capture {
140     actual_start: usize,
141     resolved: bool,
142     frames: Vec<BacktraceFrame>,
143 }
144
145 fn _assert_send_sync() {
146     fn _assert<T: Send + Sync>() {}
147     _assert::<Backtrace>();
148 }
149
150 /// A single frame of a backtrace.
151 #[unstable(feature = "backtrace_frames", issue = "79676")]
152 pub struct BacktraceFrame {
153     frame: RawFrame,
154     symbols: Vec<BacktraceSymbol>,
155 }
156
157 #[derive(Debug)]
158 enum RawFrame {
159     Actual(backtrace_rs::Frame),
160     #[cfg(test)]
161     Fake,
162 }
163
164 struct BacktraceSymbol {
165     name: Option<Vec<u8>>,
166     filename: Option<BytesOrWide>,
167     lineno: Option<u32>,
168     colno: Option<u32>,
169 }
170
171 enum BytesOrWide {
172     Bytes(Vec<u8>),
173     Wide(Vec<u16>),
174 }
175
176 #[stable(feature = "backtrace", since = "1.65.0")]
177 impl fmt::Debug for Backtrace {
178     fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
179         let capture = match &self.inner {
180             Inner::Unsupported => return fmt.write_str("<unsupported>"),
181             Inner::Disabled => return fmt.write_str("<disabled>"),
182             Inner::Captured(c) => c.force(),
183         };
184
185         let frames = &capture.frames[capture.actual_start..];
186
187         write!(fmt, "Backtrace ")?;
188
189         let mut dbg = fmt.debug_list();
190
191         for frame in frames {
192             if frame.frame.ip().is_null() {
193                 continue;
194             }
195
196             dbg.entries(&frame.symbols);
197         }
198
199         dbg.finish()
200     }
201 }
202
203 #[unstable(feature = "backtrace_frames", issue = "79676")]
204 impl fmt::Debug for BacktraceFrame {
205     fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
206         let mut dbg = fmt.debug_list();
207         dbg.entries(&self.symbols);
208         dbg.finish()
209     }
210 }
211
212 impl fmt::Debug for BacktraceSymbol {
213     fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
214         // FIXME: improve formatting: https://github.com/rust-lang/rust/issues/65280
215         // FIXME: Also, include column numbers into the debug format as Display already has them.
216         // Until there are stable per-frame accessors, the format shouldn't be changed:
217         // https://github.com/rust-lang/rust/issues/65280#issuecomment-638966585
218         write!(fmt, "{{ ")?;
219
220         if let Some(fn_name) = self.name.as_ref().map(|b| backtrace_rs::SymbolName::new(b)) {
221             write!(fmt, "fn: \"{:#}\"", fn_name)?;
222         } else {
223             write!(fmt, "fn: <unknown>")?;
224         }
225
226         if let Some(fname) = self.filename.as_ref() {
227             write!(fmt, ", file: \"{:?}\"", fname)?;
228         }
229
230         if let Some(line) = self.lineno {
231             write!(fmt, ", line: {:?}", line)?;
232         }
233
234         write!(fmt, " }}")
235     }
236 }
237
238 impl fmt::Debug for BytesOrWide {
239     fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
240         output_filename(
241             fmt,
242             match self {
243                 BytesOrWide::Bytes(w) => BytesOrWideString::Bytes(w),
244                 BytesOrWide::Wide(w) => BytesOrWideString::Wide(w),
245             },
246             backtrace_rs::PrintFmt::Short,
247             crate::env::current_dir().as_ref().ok(),
248         )
249     }
250 }
251
252 impl Backtrace {
253     /// Returns whether backtrace captures are enabled through environment
254     /// variables.
255     fn enabled() -> bool {
256         // Cache the result of reading the environment variables to make
257         // backtrace captures speedy, because otherwise reading environment
258         // variables every time can be somewhat slow.
259         static ENABLED: AtomicUsize = AtomicUsize::new(0);
260         match ENABLED.load(Relaxed) {
261             0 => {}
262             1 => return false,
263             _ => return true,
264         }
265         let enabled = match env::var("RUST_LIB_BACKTRACE") {
266             Ok(s) => s != "0",
267             Err(_) => match env::var("RUST_BACKTRACE") {
268                 Ok(s) => s != "0",
269                 Err(_) => false,
270             },
271         };
272         ENABLED.store(enabled as usize + 1, Relaxed);
273         enabled
274     }
275
276     /// Capture a stack backtrace of the current thread.
277     ///
278     /// This function will capture a stack backtrace of the current OS thread of
279     /// execution, returning a `Backtrace` type which can be later used to print
280     /// the entire stack trace or render it to a string.
281     ///
282     /// This function will be a noop if the `RUST_BACKTRACE` or
283     /// `RUST_LIB_BACKTRACE` backtrace variables are both not set. If either
284     /// environment variable is set and enabled then this function will actually
285     /// capture a backtrace. Capturing a backtrace can be both memory intensive
286     /// and slow, so these environment variables allow liberally using
287     /// `Backtrace::capture` and only incurring a slowdown when the environment
288     /// variables are set.
289     ///
290     /// To forcibly capture a backtrace regardless of environment variables, use
291     /// the `Backtrace::force_capture` function.
292     #[stable(feature = "backtrace", since = "1.65.0")]
293     #[inline(never)] // want to make sure there's a frame here to remove
294     pub fn capture() -> Backtrace {
295         if !Backtrace::enabled() {
296             return Backtrace { inner: Inner::Disabled };
297         }
298         Backtrace::create(Backtrace::capture as usize)
299     }
300
301     /// Forcibly captures a full backtrace, regardless of environment variable
302     /// configuration.
303     ///
304     /// This function behaves the same as `capture` except that it ignores the
305     /// values of the `RUST_BACKTRACE` and `RUST_LIB_BACKTRACE` environment
306     /// variables, always capturing a backtrace.
307     ///
308     /// Note that capturing a backtrace can be an expensive operation on some
309     /// platforms, so this should be used with caution in performance-sensitive
310     /// parts of code.
311     #[stable(feature = "backtrace", since = "1.65.0")]
312     #[inline(never)] // want to make sure there's a frame here to remove
313     pub fn force_capture() -> Backtrace {
314         Backtrace::create(Backtrace::force_capture as usize)
315     }
316
317     /// Forcibly captures a disabled backtrace, regardless of environment
318     /// variable configuration.
319     #[stable(feature = "backtrace", since = "1.65.0")]
320     #[rustc_const_stable(feature = "backtrace", since = "1.65.0")]
321     pub const fn disabled() -> Backtrace {
322         Backtrace { inner: Inner::Disabled }
323     }
324
325     // Capture a backtrace which start just before the function addressed by
326     // `ip`
327     fn create(ip: usize) -> Backtrace {
328         let _lock = lock();
329         let mut frames = Vec::new();
330         let mut actual_start = None;
331         unsafe {
332             backtrace_rs::trace_unsynchronized(|frame| {
333                 frames.push(BacktraceFrame {
334                     frame: RawFrame::Actual(frame.clone()),
335                     symbols: Vec::new(),
336                 });
337                 if frame.symbol_address().addr() == ip && actual_start.is_none() {
338                     actual_start = Some(frames.len());
339                 }
340                 true
341             });
342         }
343
344         // If no frames came out assume that this is an unsupported platform
345         // since `backtrace` doesn't provide a way of learning this right now,
346         // and this should be a good enough approximation.
347         let inner = if frames.is_empty() {
348             Inner::Unsupported
349         } else {
350             Inner::Captured(LazilyResolvedCapture::new(Capture {
351                 actual_start: actual_start.unwrap_or(0),
352                 frames,
353                 resolved: false,
354             }))
355         };
356
357         Backtrace { inner }
358     }
359
360     /// Returns the status of this backtrace, indicating whether this backtrace
361     /// request was unsupported, disabled, or a stack trace was actually
362     /// captured.
363     #[stable(feature = "backtrace", since = "1.65.0")]
364     #[must_use]
365     pub fn status(&self) -> BacktraceStatus {
366         match self.inner {
367             Inner::Unsupported => BacktraceStatus::Unsupported,
368             Inner::Disabled => BacktraceStatus::Disabled,
369             Inner::Captured(_) => BacktraceStatus::Captured,
370         }
371     }
372 }
373
374 impl<'a> Backtrace {
375     /// Returns an iterator over the backtrace frames.
376     #[must_use]
377     #[unstable(feature = "backtrace_frames", issue = "79676")]
378     pub fn frames(&'a self) -> &'a [BacktraceFrame] {
379         if let Inner::Captured(c) = &self.inner { &c.force().frames } else { &[] }
380     }
381 }
382
383 #[stable(feature = "backtrace", since = "1.65.0")]
384 impl fmt::Display for Backtrace {
385     fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
386         let capture = match &self.inner {
387             Inner::Unsupported => return fmt.write_str("unsupported backtrace"),
388             Inner::Disabled => return fmt.write_str("disabled backtrace"),
389             Inner::Captured(c) => c.force(),
390         };
391
392         let full = fmt.alternate();
393         let (frames, style) = if full {
394             (&capture.frames[..], backtrace_rs::PrintFmt::Full)
395         } else {
396             (&capture.frames[capture.actual_start..], backtrace_rs::PrintFmt::Short)
397         };
398
399         // When printing paths we try to strip the cwd if it exists, otherwise
400         // we just print the path as-is. Note that we also only do this for the
401         // short format, because if it's full we presumably want to print
402         // everything.
403         let cwd = crate::env::current_dir();
404         let mut print_path = move |fmt: &mut fmt::Formatter<'_>, path: BytesOrWideString<'_>| {
405             output_filename(fmt, path, style, cwd.as_ref().ok())
406         };
407
408         let mut f = backtrace_rs::BacktraceFmt::new(fmt, style, &mut print_path);
409         f.add_context()?;
410         for frame in frames {
411             if frame.symbols.is_empty() {
412                 f.frame().print_raw(frame.frame.ip(), None, None, None)?;
413             } else {
414                 for symbol in frame.symbols.iter() {
415                     f.frame().print_raw_with_column(
416                         frame.frame.ip(),
417                         symbol.name.as_ref().map(|b| backtrace_rs::SymbolName::new(b)),
418                         symbol.filename.as_ref().map(|b| match b {
419                             BytesOrWide::Bytes(w) => BytesOrWideString::Bytes(w),
420                             BytesOrWide::Wide(w) => BytesOrWideString::Wide(w),
421                         }),
422                         symbol.lineno,
423                         symbol.colno,
424                     )?;
425                 }
426             }
427         }
428         f.finish()?;
429         Ok(())
430     }
431 }
432
433 struct LazilyResolvedCapture {
434     sync: Once,
435     capture: UnsafeCell<Capture>,
436 }
437
438 impl LazilyResolvedCapture {
439     fn new(capture: Capture) -> Self {
440         LazilyResolvedCapture { sync: Once::new(), capture: UnsafeCell::new(capture) }
441     }
442
443     fn force(&self) -> &Capture {
444         self.sync.call_once(|| {
445             // SAFETY: This exclusive reference can't overlap with any others
446             // `Once` guarantees callers will block until this closure returns
447             // `Once` also guarantees only a single caller will enter this closure
448             unsafe { &mut *self.capture.get() }.resolve();
449         });
450
451         // SAFETY: This shared reference can't overlap with the exclusive reference above
452         unsafe { &*self.capture.get() }
453     }
454 }
455
456 // SAFETY: Access to the inner value is synchronized using a thread-safe `Once`
457 // So long as `Capture` is `Sync`, `LazilyResolvedCapture` is too
458 unsafe impl Sync for LazilyResolvedCapture where Capture: Sync {}
459
460 impl Capture {
461     fn resolve(&mut self) {
462         // If we're already resolved, nothing to do!
463         if self.resolved {
464             return;
465         }
466         self.resolved = true;
467
468         // Use the global backtrace lock to synchronize this as it's a
469         // requirement of the `backtrace` crate, and then actually resolve
470         // everything.
471         let _lock = lock();
472         for frame in self.frames.iter_mut() {
473             let symbols = &mut frame.symbols;
474             let frame = match &frame.frame {
475                 RawFrame::Actual(frame) => frame,
476                 #[cfg(test)]
477                 RawFrame::Fake => unimplemented!(),
478             };
479             unsafe {
480                 backtrace_rs::resolve_frame_unsynchronized(frame, |symbol| {
481                     symbols.push(BacktraceSymbol {
482                         name: symbol.name().map(|m| m.as_bytes().to_vec()),
483                         filename: symbol.filename_raw().map(|b| match b {
484                             BytesOrWideString::Bytes(b) => BytesOrWide::Bytes(b.to_owned()),
485                             BytesOrWideString::Wide(b) => BytesOrWide::Wide(b.to_owned()),
486                         }),
487                         lineno: symbol.lineno(),
488                         colno: symbol.colno(),
489                     });
490                 });
491             }
492         }
493     }
494 }
495
496 impl RawFrame {
497     fn ip(&self) -> *mut c_void {
498         match self {
499             RawFrame::Actual(frame) => frame.ip(),
500             #[cfg(test)]
501             RawFrame::Fake => crate::ptr::invalid_mut(1),
502         }
503     }
504 }