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