1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
18 use sync::StaticRwLock;
19 use sys::stdio::Stderr;
20 use sys_common::backtrace;
21 use sys_common::thread_info;
25 thread_local! { pub static PANIC_COUNT: Cell<usize> = Cell::new(0) }
28 pub static LOCAL_STDERR: RefCell<Option<Box<Write + Send>>> = {
33 #[derive(Copy, Clone)]
36 Custom(*mut (Fn(&PanicInfo) + 'static + Sync + Send)),
39 static HANDLER_LOCK: StaticRwLock = StaticRwLock::new();
40 static mut HANDLER: Handler = Handler::Default;
42 /// Registers a custom panic handler, replacing any that was previously
45 /// The panic handler is invoked when a thread panics, but before it begins
46 /// unwinding the stack. The default handler prints a message to standard error
47 /// and generates a backtrace if requested, but this behavior can be customized
48 /// with the `set_handler` and `take_handler` functions.
50 /// The handler is provided with a `PanicInfo` struct which contains information
51 /// about the origin of the panic, including the payload passed to `panic!` and
52 /// the source code location from which the panic originated.
54 /// The panic handler is a global resource.
58 /// Panics if called from a panicking thread.
59 #[unstable(feature = "panic_handler", reason = "awaiting feedback", issue = "30449")]
60 pub fn set_handler<F>(handler: F) where F: Fn(&PanicInfo) + 'static + Sync + Send {
61 if thread::panicking() {
62 panic!("cannot modify the panic handler from a panicking thread");
65 let handler = Box::new(handler);
67 let lock = HANDLER_LOCK.write();
68 let old_handler = HANDLER;
69 HANDLER = Handler::Custom(Box::into_raw(handler));
72 if let Handler::Custom(ptr) = old_handler {
78 /// Unregisters the current panic handler, returning it.
80 /// If no custom handler is registered, the default handler will be returned.
84 /// Panics if called from a panicking thread.
85 #[unstable(feature = "panic_handler", reason = "awaiting feedback", issue = "30449")]
86 pub fn take_handler() -> Box<Fn(&PanicInfo) + 'static + Sync + Send> {
87 if thread::panicking() {
88 panic!("cannot modify the panic handler from a panicking thread");
92 let lock = HANDLER_LOCK.write();
93 let handler = HANDLER;
94 HANDLER = Handler::Default;
98 Handler::Default => Box::new(default_handler),
99 Handler::Custom(ptr) => {Box::from_raw(ptr)} // FIXME #30530
104 /// A struct providing information about a panic.
105 #[unstable(feature = "panic_handler", reason = "awaiting feedback", issue = "30449")]
106 pub struct PanicInfo<'a> {
107 payload: &'a (Any + Send),
108 location: Location<'a>,
111 impl<'a> PanicInfo<'a> {
112 /// Returns the payload associated with the panic.
114 /// This will commonly, but not always, be a `&'static str` or `String`.
115 #[unstable(feature = "panic_handler", reason = "awaiting feedback", issue = "30449")]
116 pub fn payload(&self) -> &(Any + Send) {
120 /// Returns information about the location from which the panic originated,
123 /// This method will currently always return `Some`, but this may change
124 /// in future versions.
125 #[unstable(feature = "panic_handler", reason = "awaiting feedback", issue = "30449")]
126 pub fn location(&self) -> Option<&Location> {
131 /// A struct containing information about the location of a panic.
132 #[unstable(feature = "panic_handler", reason = "awaiting feedback", issue = "30449")]
133 pub struct Location<'a> {
138 impl<'a> Location<'a> {
139 /// Returns the name of the source file from which the panic originated.
140 #[unstable(feature = "panic_handler", reason = "awaiting feedback", issue = "30449")]
141 pub fn file(&self) -> &str {
145 /// Returns the line number from which the panic originated.
146 #[unstable(feature = "panic_handler", reason = "awaiting feedback", issue = "30449")]
147 pub fn line(&self) -> u32 {
152 fn default_handler(info: &PanicInfo) {
153 let panics = PANIC_COUNT.with(|s| s.get());
155 // If this is a double panic, make sure that we print a backtrace
156 // for this panic. Otherwise only print it if logging is enabled.
157 let log_backtrace = panics >= 2 || backtrace::log_enabled();
159 let file = info.location.file;
160 let line = info.location.line;
162 let msg = match info.payload.downcast_ref::<&'static str>() {
164 None => match info.payload.downcast_ref::<String>() {
169 let mut err = Stderr::new().ok();
170 let thread = thread_info::current_thread();
171 let name = thread.as_ref().and_then(|t| t.name()).unwrap_or("<unnamed>");
173 let write = |err: &mut ::io::Write| {
174 let _ = writeln!(err, "thread '{}' panicked at '{}', {}:{}",
175 name, msg, file, line);
177 let _ = backtrace::write(err);
181 let prev = LOCAL_STDERR.with(|s| s.borrow_mut().take());
182 match (prev, err.as_mut()) {
183 (Some(mut stderr), _) => {
185 let mut s = Some(stderr);
186 LOCAL_STDERR.with(|slot| {
187 *slot.borrow_mut() = s.take();
190 (None, Some(ref mut err)) => { write(err) }
195 pub fn on_panic(obj: &(Any+Send), file: &'static str, line: u32) {
196 let panics = PANIC_COUNT.with(|s| {
197 let count = s.get() + 1;
202 // If this is the third nested call, on_panic triggered the last panic,
203 // otherwise the double-panic check would have aborted the process.
204 // Even if it is likely that on_panic was unable to log the backtrace,
205 // abort immediately to avoid infinite recursion, so that attaching a
206 // debugger provides a useable stacktrace.
208 util::dumb_print(format_args!("thread panicked while processing \
209 panic. aborting.\n"));
210 unsafe { intrinsics::abort() }
213 let info = PanicInfo {
222 let _lock = HANDLER_LOCK.read();
224 Handler::Default => default_handler(&info),
225 Handler::Custom(ptr) => (*ptr)(&info),
230 // If a thread panics while it's already unwinding then we
231 // have limited options. Currently our preference is to
232 // just abort. In the future we may consider resuming
233 // unwinding or otherwise exiting the thread cleanly.
234 util::dumb_print(format_args!("thread panicked while panicking. \
236 unsafe { intrinsics::abort() }