std/panic.rs
1//! Panic support in the standard library.
2
3#![stable(feature = "std_panic", since = "1.9.0")]
4
5use crate::any::Any;
6use crate::sync::atomic::{AtomicU8, Ordering};
7use crate::sync::{Condvar, Mutex, RwLock};
8use crate::thread::Result;
9use crate::{collections, fmt, panicking};
10
11#[stable(feature = "panic_hooks", since = "1.10.0")]
12#[deprecated(
13 since = "1.82.0",
14 note = "use `PanicHookInfo` instead",
15 suggestion = "std::panic::PanicHookInfo"
16)]
17/// A struct providing information about a panic.
18///
19/// `PanicInfo` has been renamed to [`PanicHookInfo`] to avoid confusion with
20/// [`core::panic::PanicInfo`].
21pub type PanicInfo<'a> = PanicHookInfo<'a>;
22
23/// A struct providing information about a panic.
24///
25/// `PanicHookInfo` structure is passed to a panic hook set by the [`set_hook`] function.
26///
27/// # Examples
28///
29/// ```should_panic
30/// use std::panic;
31///
32/// panic::set_hook(Box::new(|panic_info| {
33/// println!("panic occurred: {panic_info}");
34/// }));
35///
36/// panic!("critical system failure");
37/// ```
38///
39/// [`set_hook`]: ../../std/panic/fn.set_hook.html
40#[stable(feature = "panic_hook_info", since = "1.81.0")]
41#[derive(Debug)]
42pub struct PanicHookInfo<'a> {
43 payload: &'a (dyn Any + Send),
44 location: &'a Location<'a>,
45 can_unwind: bool,
46 force_no_backtrace: bool,
47}
48
49impl<'a> PanicHookInfo<'a> {
50 #[inline]
51 pub(crate) fn new(
52 location: &'a Location<'a>,
53 payload: &'a (dyn Any + Send),
54 can_unwind: bool,
55 force_no_backtrace: bool,
56 ) -> Self {
57 PanicHookInfo { payload, location, can_unwind, force_no_backtrace }
58 }
59
60 /// Returns the payload associated with the panic.
61 ///
62 /// This will commonly, but not always, be a `&'static str` or [`String`].
63 ///
64 /// A invocation of the `panic!()` macro in Rust 2021 or later will always result in a
65 /// panic payload of type `&'static str` or `String`.
66 ///
67 /// Only an invocation of [`panic_any`]
68 /// (or, in Rust 2018 and earlier, `panic!(x)` where `x` is something other than a string)
69 /// can result in a panic payload other than a `&'static str` or `String`.
70 ///
71 /// [`String`]: ../../std/string/struct.String.html
72 ///
73 /// # Examples
74 ///
75 /// ```should_panic
76 /// use std::panic;
77 ///
78 /// panic::set_hook(Box::new(|panic_info| {
79 /// if let Some(s) = panic_info.payload().downcast_ref::<&str>() {
80 /// println!("panic occurred: {s:?}");
81 /// } else if let Some(s) = panic_info.payload().downcast_ref::<String>() {
82 /// println!("panic occurred: {s:?}");
83 /// } else {
84 /// println!("panic occurred");
85 /// }
86 /// }));
87 ///
88 /// panic!("Normal panic");
89 /// ```
90 #[must_use]
91 #[inline]
92 #[stable(feature = "panic_hooks", since = "1.10.0")]
93 pub fn payload(&self) -> &(dyn Any + Send) {
94 self.payload
95 }
96
97 /// Returns the payload associated with the panic, if it is a string.
98 ///
99 /// This returns the payload if it is of type `&'static str` or `String`.
100 ///
101 /// A invocation of the `panic!()` macro in Rust 2021 or later will always result in a
102 /// panic payload where `payload_as_str` returns `Some`.
103 ///
104 /// Only an invocation of [`panic_any`]
105 /// (or, in Rust 2018 and earlier, `panic!(x)` where `x` is something other than a string)
106 /// can result in a panic payload where `payload_as_str` returns `None`.
107 ///
108 /// # Example
109 ///
110 /// ```should_panic
111 /// #![feature(panic_payload_as_str)]
112 ///
113 /// std::panic::set_hook(Box::new(|panic_info| {
114 /// if let Some(s) = panic_info.payload_as_str() {
115 /// println!("panic occurred: {s:?}");
116 /// } else {
117 /// println!("panic occurred");
118 /// }
119 /// }));
120 ///
121 /// panic!("Normal panic");
122 /// ```
123 #[must_use]
124 #[inline]
125 #[unstable(feature = "panic_payload_as_str", issue = "125175")]
126 pub fn payload_as_str(&self) -> Option<&str> {
127 if let Some(s) = self.payload.downcast_ref::<&str>() {
128 Some(s)
129 } else if let Some(s) = self.payload.downcast_ref::<String>() {
130 Some(s)
131 } else {
132 None
133 }
134 }
135
136 /// Returns information about the location from which the panic originated,
137 /// if available.
138 ///
139 /// This method will currently always return [`Some`], but this may change
140 /// in future versions.
141 ///
142 /// # Examples
143 ///
144 /// ```should_panic
145 /// use std::panic;
146 ///
147 /// panic::set_hook(Box::new(|panic_info| {
148 /// if let Some(location) = panic_info.location() {
149 /// println!("panic occurred in file '{}' at line {}",
150 /// location.file(),
151 /// location.line(),
152 /// );
153 /// } else {
154 /// println!("panic occurred but can't get location information...");
155 /// }
156 /// }));
157 ///
158 /// panic!("Normal panic");
159 /// ```
160 #[must_use]
161 #[inline]
162 #[stable(feature = "panic_hooks", since = "1.10.0")]
163 pub fn location(&self) -> Option<&Location<'_>> {
164 // NOTE: If this is changed to sometimes return None,
165 // deal with that case in std::panicking::default_hook and core::panicking::panic_fmt.
166 Some(&self.location)
167 }
168
169 /// Returns whether the panic handler is allowed to unwind the stack from
170 /// the point where the panic occurred.
171 ///
172 /// This is true for most kinds of panics with the exception of panics
173 /// caused by trying to unwind out of a `Drop` implementation or a function
174 /// whose ABI does not support unwinding.
175 ///
176 /// It is safe for a panic handler to unwind even when this function returns
177 /// false, however this will simply cause the panic handler to be called
178 /// again.
179 #[must_use]
180 #[inline]
181 #[unstable(feature = "panic_can_unwind", issue = "92988")]
182 pub fn can_unwind(&self) -> bool {
183 self.can_unwind
184 }
185
186 #[unstable(
187 feature = "panic_internals",
188 reason = "internal details of the implementation of the `panic!` and related macros",
189 issue = "none"
190 )]
191 #[doc(hidden)]
192 #[inline]
193 pub fn force_no_backtrace(&self) -> bool {
194 self.force_no_backtrace
195 }
196}
197
198#[stable(feature = "panic_hook_display", since = "1.26.0")]
199impl fmt::Display for PanicHookInfo<'_> {
200 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
201 formatter.write_str("panicked at ")?;
202 self.location.fmt(formatter)?;
203 if let Some(payload) = self.payload_as_str() {
204 formatter.write_str(":\n")?;
205 formatter.write_str(payload)?;
206 }
207 Ok(())
208 }
209}
210
211#[doc(hidden)]
212#[unstable(feature = "edition_panic", issue = "none", reason = "use panic!() instead")]
213#[allow_internal_unstable(libstd_sys_internals, const_format_args, panic_internals, rt)]
214#[cfg_attr(not(test), rustc_diagnostic_item = "std_panic_2015_macro")]
215#[rustc_macro_transparency = "semitransparent"]
216pub macro panic_2015 {
217 () => ({
218 $crate::rt::begin_panic("explicit panic")
219 }),
220 ($msg:expr $(,)?) => ({
221 $crate::rt::begin_panic($msg);
222 }),
223 // Special-case the single-argument case for const_panic.
224 ("{}", $arg:expr $(,)?) => ({
225 $crate::rt::panic_display(&$arg);
226 }),
227 ($fmt:expr, $($arg:tt)+) => ({
228 // Semicolon to prevent temporaries inside the formatting machinery from
229 // being considered alive in the caller after the panic_fmt call.
230 $crate::rt::panic_fmt($crate::const_format_args!($fmt, $($arg)+));
231 }),
232}
233
234#[stable(feature = "panic_hooks", since = "1.10.0")]
235pub use core::panic::Location;
236#[doc(hidden)]
237#[unstable(feature = "edition_panic", issue = "none", reason = "use panic!() instead")]
238pub use core::panic::panic_2021;
239#[stable(feature = "catch_unwind", since = "1.9.0")]
240pub use core::panic::{AssertUnwindSafe, RefUnwindSafe, UnwindSafe};
241
242#[unstable(feature = "panic_update_hook", issue = "92649")]
243pub use crate::panicking::update_hook;
244#[stable(feature = "panic_hooks", since = "1.10.0")]
245pub use crate::panicking::{set_hook, take_hook};
246
247/// Panics the current thread with the given message as the panic payload.
248///
249/// The message can be of any (`Any + Send`) type, not just strings.
250///
251/// The message is wrapped in a `Box<'static + Any + Send>`, which can be
252/// accessed later using [`PanicHookInfo::payload`].
253///
254/// See the [`panic!`] macro for more information about panicking.
255#[stable(feature = "panic_any", since = "1.51.0")]
256#[inline]
257#[track_caller]
258pub fn panic_any<M: 'static + Any + Send>(msg: M) -> ! {
259 crate::panicking::begin_panic(msg);
260}
261
262#[stable(feature = "catch_unwind", since = "1.9.0")]
263impl<T: ?Sized> UnwindSafe for Mutex<T> {}
264#[stable(feature = "catch_unwind", since = "1.9.0")]
265impl<T: ?Sized> UnwindSafe for RwLock<T> {}
266#[stable(feature = "catch_unwind", since = "1.9.0")]
267impl UnwindSafe for Condvar {}
268
269#[stable(feature = "unwind_safe_lock_refs", since = "1.12.0")]
270impl<T: ?Sized> RefUnwindSafe for Mutex<T> {}
271#[stable(feature = "unwind_safe_lock_refs", since = "1.12.0")]
272impl<T: ?Sized> RefUnwindSafe for RwLock<T> {}
273#[stable(feature = "unwind_safe_lock_refs", since = "1.12.0")]
274impl RefUnwindSafe for Condvar {}
275
276// https://github.com/rust-lang/rust/issues/62301
277#[stable(feature = "hashbrown", since = "1.36.0")]
278impl<K, V, S> UnwindSafe for collections::HashMap<K, V, S>
279where
280 K: UnwindSafe,
281 V: UnwindSafe,
282 S: UnwindSafe,
283{
284}
285
286#[unstable(feature = "abort_unwind", issue = "130338")]
287pub use core::panic::abort_unwind;
288
289/// Invokes a closure, capturing the cause of an unwinding panic if one occurs.
290///
291/// This function will return `Ok` with the closure's result if the closure does
292/// not panic, and will return `Err(cause)` if the closure panics. The `cause`
293/// returned is the object with which panic was originally invoked.
294///
295/// Rust functions that are expected to be called from foreign code that does
296/// not support unwinding (such as C compiled with `-fno-exceptions`) should be
297/// defined using `extern "C"`, which ensures that if the Rust code panics, it
298/// is automatically caught and the process is aborted. If this is the desired
299/// behavior, it is not necessary to use `catch_unwind` explicitly. This
300/// function should instead be used when more graceful error-handling is needed.
301///
302/// It is **not** recommended to use this function for a general try/catch
303/// mechanism. The [`Result`] type is more appropriate to use for functions that
304/// can fail on a regular basis. Additionally, this function is not guaranteed
305/// to catch all panics, see the "Notes" section below.
306///
307/// The closure provided is required to adhere to the [`UnwindSafe`] trait to
308/// ensure that all captured variables are safe to cross this boundary. The
309/// purpose of this bound is to encode the concept of [exception safety][rfc] in
310/// the type system. Most usage of this function should not need to worry about
311/// this bound as programs are naturally unwind safe without `unsafe` code. If
312/// it becomes a problem the [`AssertUnwindSafe`] wrapper struct can be used to
313/// quickly assert that the usage here is indeed unwind safe.
314///
315/// [rfc]: https://github.com/rust-lang/rfcs/blob/master/text/1236-stabilize-catch-panic.md
316///
317/// # Notes
318///
319/// This function **might not catch all Rust panics**. A Rust panic is not
320/// always implemented via unwinding, but can be implemented by aborting the
321/// process as well. This function *only* catches unwinding panics, not those
322/// that abort the process.
323///
324/// If a custom panic hook has been set, it will be invoked before the panic is
325/// caught, before unwinding.
326///
327/// Although unwinding into Rust code with a foreign exception (e.g. an
328/// exception thrown from C++ code, or a `panic!` in Rust code compiled or
329/// linked with a different runtime) via an appropriate ABI (e.g. `"C-unwind"`)
330/// is permitted, catching such an exception using this function will have one
331/// of two behaviors, and it is unspecified which will occur:
332///
333/// * The process aborts, after executing all destructors of `f` and the
334/// functions it called.
335/// * The function returns a `Result::Err` containing an opaque type.
336///
337/// Finally, be **careful in how you drop the result of this function**. If it
338/// is `Err`, it contains the panic payload, and dropping that may in turn
339/// panic!
340///
341/// # Examples
342///
343/// ```
344/// use std::panic;
345///
346/// let result = panic::catch_unwind(|| {
347/// println!("hello!");
348/// });
349/// assert!(result.is_ok());
350///
351/// let result = panic::catch_unwind(|| {
352/// panic!("oh no!");
353/// });
354/// assert!(result.is_err());
355/// ```
356#[stable(feature = "catch_unwind", since = "1.9.0")]
357pub fn catch_unwind<F: FnOnce() -> R + UnwindSafe, R>(f: F) -> Result<R> {
358 unsafe { panicking::r#try(f) }
359}
360
361/// Triggers a panic without invoking the panic hook.
362///
363/// This is designed to be used in conjunction with [`catch_unwind`] to, for
364/// example, carry a panic across a layer of C code.
365///
366/// # Notes
367///
368/// Note that panics in Rust are not always implemented via unwinding, but they
369/// may be implemented by aborting the process. If this function is called when
370/// panics are implemented this way then this function will abort the process,
371/// not trigger an unwind.
372///
373/// # Examples
374///
375/// ```should_panic
376/// use std::panic;
377///
378/// let result = panic::catch_unwind(|| {
379/// if 1 != 2 {
380/// panic!("oh no!");
381/// }
382/// });
383///
384/// if let Err(err) = result {
385/// panic::resume_unwind(err);
386/// }
387/// ```
388#[stable(feature = "resume_unwind", since = "1.9.0")]
389pub fn resume_unwind(payload: Box<dyn Any + Send>) -> ! {
390 panicking::rust_panic_without_hook(payload)
391}
392
393/// Makes all future panics abort directly without running the panic hook or unwinding.
394///
395/// There is no way to undo this; the effect lasts until the process exits or
396/// execs (or the equivalent).
397///
398/// # Use after fork
399///
400/// This function is particularly useful for calling after `libc::fork`. After `fork`, in a
401/// multithreaded program it is (on many platforms) not safe to call the allocator. It is also
402/// generally highly undesirable for an unwind to unwind past the `fork`, because that results in
403/// the unwind propagating to code that was only ever expecting to run in the parent.
404///
405/// `panic::always_abort()` helps avoid both of these. It directly avoids any further unwinding,
406/// and if there is a panic, the abort will occur without allocating provided that the arguments to
407/// panic can be formatted without allocating.
408///
409/// Examples
410///
411/// ```no_run
412/// #![feature(panic_always_abort)]
413/// use std::panic;
414///
415/// panic::always_abort();
416///
417/// let _ = panic::catch_unwind(|| {
418/// panic!("inside the catch");
419/// });
420///
421/// // We will have aborted already, due to the panic.
422/// unreachable!();
423/// ```
424#[unstable(feature = "panic_always_abort", issue = "84438")]
425pub fn always_abort() {
426 crate::panicking::panic_count::set_always_abort();
427}
428
429/// The configuration for whether and how the default panic hook will capture
430/// and display the backtrace.
431#[derive(Debug, Copy, Clone, PartialEq, Eq)]
432#[unstable(feature = "panic_backtrace_config", issue = "93346")]
433#[non_exhaustive]
434pub enum BacktraceStyle {
435 /// Prints a terser backtrace which ideally only contains relevant
436 /// information.
437 Short,
438 /// Prints a backtrace with all possible information.
439 Full,
440 /// Disable collecting and displaying backtraces.
441 Off,
442}
443
444impl BacktraceStyle {
445 pub(crate) fn full() -> Option<Self> {
446 if cfg!(feature = "backtrace") { Some(BacktraceStyle::Full) } else { None }
447 }
448
449 fn as_u8(self) -> u8 {
450 match self {
451 BacktraceStyle::Short => 1,
452 BacktraceStyle::Full => 2,
453 BacktraceStyle::Off => 3,
454 }
455 }
456
457 fn from_u8(s: u8) -> Option<Self> {
458 match s {
459 1 => Some(BacktraceStyle::Short),
460 2 => Some(BacktraceStyle::Full),
461 3 => Some(BacktraceStyle::Off),
462 _ => None,
463 }
464 }
465}
466
467// Tracks whether we should/can capture a backtrace, and how we should display
468// that backtrace.
469//
470// Internally stores equivalent of an Option<BacktraceStyle>.
471static SHOULD_CAPTURE: AtomicU8 = AtomicU8::new(0);
472
473/// Configures whether the default panic hook will capture and display a
474/// backtrace.
475///
476/// The default value for this setting may be set by the `RUST_BACKTRACE`
477/// environment variable; see the details in [`get_backtrace_style`].
478#[unstable(feature = "panic_backtrace_config", issue = "93346")]
479pub fn set_backtrace_style(style: BacktraceStyle) {
480 if cfg!(feature = "backtrace") {
481 // If the `backtrace` feature of this crate is enabled, set the backtrace style.
482 SHOULD_CAPTURE.store(style.as_u8(), Ordering::Relaxed);
483 }
484}
485
486/// Checks whether the standard library's panic hook will capture and print a
487/// backtrace.
488///
489/// This function will, if a backtrace style has not been set via
490/// [`set_backtrace_style`], read the environment variable `RUST_BACKTRACE` to
491/// determine a default value for the backtrace formatting:
492///
493/// The first call to `get_backtrace_style` may read the `RUST_BACKTRACE`
494/// environment variable if `set_backtrace_style` has not been called to
495/// override the default value. After a call to `set_backtrace_style` or
496/// `get_backtrace_style`, any changes to `RUST_BACKTRACE` will have no effect.
497///
498/// `RUST_BACKTRACE` is read according to these rules:
499///
500/// * `0` for `BacktraceStyle::Off`
501/// * `full` for `BacktraceStyle::Full`
502/// * `1` for `BacktraceStyle::Short`
503/// * Other values are currently `BacktraceStyle::Short`, but this may change in
504/// the future
505///
506/// Returns `None` if backtraces aren't currently supported.
507#[unstable(feature = "panic_backtrace_config", issue = "93346")]
508pub fn get_backtrace_style() -> Option<BacktraceStyle> {
509 if !cfg!(feature = "backtrace") {
510 // If the `backtrace` feature of this crate isn't enabled quickly return
511 // `Unsupported` so this can be constant propagated all over the place
512 // to optimize away callers.
513 return None;
514 }
515
516 let current = SHOULD_CAPTURE.load(Ordering::Relaxed);
517 if let Some(style) = BacktraceStyle::from_u8(current) {
518 return Some(style);
519 }
520
521 let format = match crate::env::var_os("RUST_BACKTRACE") {
522 Some(x) if &x == "0" => BacktraceStyle::Off,
523 Some(x) if &x == "full" => BacktraceStyle::Full,
524 Some(_) => BacktraceStyle::Short,
525 None if crate::sys::FULL_BACKTRACE_DEFAULT => BacktraceStyle::Full,
526 None => BacktraceStyle::Off,
527 };
528
529 match SHOULD_CAPTURE.compare_exchange(0, format.as_u8(), Ordering::Relaxed, Ordering::Relaxed) {
530 Ok(_) => Some(format),
531 Err(new) => BacktraceStyle::from_u8(new),
532 }
533}