]> git.lizzy.rs Git - rust.git/blob - src/libstd/panicking.rs
std: Push process stdio setup in std::sys
[rust.git] / src / libstd / panicking.rs
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.
4 //
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.
10
11 use prelude::v1::*;
12 use io::prelude::*;
13
14 use any::Any;
15 use cell::Cell;
16 use cell::RefCell;
17 use intrinsics;
18 use sync::StaticRwLock;
19 use sync::atomic::{AtomicBool, Ordering};
20 use sys::stdio::Stderr;
21 use sys_common::backtrace;
22 use sys_common::thread_info;
23 use sys_common::util;
24 use thread;
25
26 thread_local! { pub static PANIC_COUNT: Cell<usize> = Cell::new(0) }
27
28 thread_local! {
29     pub static LOCAL_STDERR: RefCell<Option<Box<Write + Send>>> = {
30         RefCell::new(None)
31     }
32 }
33
34 #[derive(Copy, Clone)]
35 enum Handler {
36     Default,
37     Custom(*mut (Fn(&PanicInfo) + 'static + Sync + Send)),
38 }
39
40 static HANDLER_LOCK: StaticRwLock = StaticRwLock::new();
41 static mut HANDLER: Handler = Handler::Default;
42 static FIRST_PANIC: AtomicBool = AtomicBool::new(true);
43
44 /// Registers a custom panic handler, replacing any that was previously
45 /// registered.
46 ///
47 /// The panic handler is invoked when a thread panics, but before it begins
48 /// unwinding the stack. The default handler prints a message to standard error
49 /// and generates a backtrace if requested, but this behavior can be customized
50 /// with the `set_handler` and `take_handler` functions.
51 ///
52 /// The handler is provided with a `PanicInfo` struct which contains information
53 /// about the origin of the panic, including the payload passed to `panic!` and
54 /// the source code location from which the panic originated.
55 ///
56 /// The panic handler is a global resource.
57 ///
58 /// # Panics
59 ///
60 /// Panics if called from a panicking thread.
61 #[unstable(feature = "panic_handler", reason = "awaiting feedback", issue = "30449")]
62 pub fn set_handler<F>(handler: F) where F: Fn(&PanicInfo) + 'static + Sync + Send {
63     if thread::panicking() {
64         panic!("cannot modify the panic handler from a panicking thread");
65     }
66
67     let handler = Box::new(handler);
68     unsafe {
69         let lock = HANDLER_LOCK.write();
70         let old_handler = HANDLER;
71         HANDLER = Handler::Custom(Box::into_raw(handler));
72         drop(lock);
73
74         if let Handler::Custom(ptr) = old_handler {
75             Box::from_raw(ptr);
76         }
77     }
78 }
79
80 /// Unregisters the current panic handler, returning it.
81 ///
82 /// If no custom handler is registered, the default handler will be returned.
83 ///
84 /// # Panics
85 ///
86 /// Panics if called from a panicking thread.
87 #[unstable(feature = "panic_handler", reason = "awaiting feedback", issue = "30449")]
88 pub fn take_handler() -> Box<Fn(&PanicInfo) + 'static + Sync + Send> {
89     if thread::panicking() {
90         panic!("cannot modify the panic handler from a panicking thread");
91     }
92
93     unsafe {
94         let lock = HANDLER_LOCK.write();
95         let handler = HANDLER;
96         HANDLER = Handler::Default;
97         drop(lock);
98
99         match handler {
100             Handler::Default => Box::new(default_handler),
101             Handler::Custom(ptr) => {Box::from_raw(ptr)} // FIXME #30530
102         }
103     }
104 }
105
106 /// A struct providing information about a panic.
107 #[unstable(feature = "panic_handler", reason = "awaiting feedback", issue = "30449")]
108 pub struct PanicInfo<'a> {
109     payload: &'a (Any + Send),
110     location: Location<'a>,
111 }
112
113 impl<'a> PanicInfo<'a> {
114     /// Returns the payload associated with the panic.
115     ///
116     /// This will commonly, but not always, be a `&'static str` or `String`.
117     #[unstable(feature = "panic_handler", reason = "awaiting feedback", issue = "30449")]
118     pub fn payload(&self) -> &(Any + Send) {
119         self.payload
120     }
121
122     /// Returns information about the location from which the panic originated,
123     /// if available.
124     ///
125     /// This method will currently always return `Some`, but this may change
126     /// in future versions.
127     #[unstable(feature = "panic_handler", reason = "awaiting feedback", issue = "30449")]
128     pub fn location(&self) -> Option<&Location> {
129         Some(&self.location)
130     }
131 }
132
133 /// A struct containing information about the location of a panic.
134 #[unstable(feature = "panic_handler", reason = "awaiting feedback", issue = "30449")]
135 pub struct Location<'a> {
136     file: &'a str,
137     line: u32,
138 }
139
140 impl<'a> Location<'a> {
141     /// Returns the name of the source file from which the panic originated.
142     #[unstable(feature = "panic_handler", reason = "awaiting feedback", issue = "30449")]
143     pub fn file(&self) -> &str {
144         self.file
145     }
146
147     /// Returns the line number from which the panic originated.
148     #[unstable(feature = "panic_handler", reason = "awaiting feedback", issue = "30449")]
149     pub fn line(&self) -> u32 {
150         self.line
151     }
152 }
153
154 fn default_handler(info: &PanicInfo) {
155     let panics = PANIC_COUNT.with(|s| s.get());
156
157     // If this is a double panic, make sure that we print a backtrace
158     // for this panic. Otherwise only print it if logging is enabled.
159     let log_backtrace = panics >= 2 || backtrace::log_enabled();
160
161     let file = info.location.file;
162     let line = info.location.line;
163
164     let msg = match info.payload.downcast_ref::<&'static str>() {
165         Some(s) => *s,
166         None => match info.payload.downcast_ref::<String>() {
167             Some(s) => &s[..],
168             None => "Box<Any>",
169         }
170     };
171     let mut err = Stderr::new().ok();
172     let thread = thread_info::current_thread();
173     let name = thread.as_ref().and_then(|t| t.name()).unwrap_or("<unnamed>");
174
175     let write = |err: &mut ::io::Write| {
176         let _ = writeln!(err, "thread '{}' panicked at '{}', {}:{}",
177                          name, msg, file, line);
178
179         if log_backtrace {
180             let _ = backtrace::write(err);
181         } else if FIRST_PANIC.compare_and_swap(true, false, Ordering::SeqCst) {
182             let _ = writeln!(err, "note: Run with `RUST_BACKTRACE=1` for a backtrace.");
183         }
184     };
185
186     let prev = LOCAL_STDERR.with(|s| s.borrow_mut().take());
187     match (prev, err.as_mut()) {
188         (Some(mut stderr), _) => {
189             write(&mut *stderr);
190             let mut s = Some(stderr);
191             LOCAL_STDERR.with(|slot| {
192                 *slot.borrow_mut() = s.take();
193             });
194         }
195         (None, Some(ref mut err)) => { write(err) }
196         _ => {}
197     }
198 }
199
200 pub fn on_panic(obj: &(Any+Send), file: &'static str, line: u32) {
201     let panics = PANIC_COUNT.with(|s| {
202         let count = s.get() + 1;
203         s.set(count);
204         count
205     });
206
207     // If this is the third nested call, on_panic triggered the last panic,
208     // otherwise the double-panic check would have aborted the process.
209     // Even if it is likely that on_panic was unable to log the backtrace,
210     // abort immediately to avoid infinite recursion, so that attaching a
211     // debugger provides a useable stacktrace.
212     if panics >= 3 {
213         util::dumb_print(format_args!("thread panicked while processing \
214                                        panic. aborting.\n"));
215         unsafe { intrinsics::abort() }
216     }
217
218     let info = PanicInfo {
219         payload: obj,
220         location: Location {
221             file: file,
222             line: line,
223         },
224     };
225
226     unsafe {
227         let _lock = HANDLER_LOCK.read();
228         match HANDLER {
229             Handler::Default => default_handler(&info),
230             Handler::Custom(ptr) => (*ptr)(&info),
231         }
232     }
233
234     if panics >= 2 {
235         // If a thread panics while it's already unwinding then we
236         // have limited options. Currently our preference is to
237         // just abort. In the future we may consider resuming
238         // unwinding or otherwise exiting the thread cleanly.
239         util::dumb_print(format_args!("thread panicked while panicking. \
240                                        aborting.\n"));
241         unsafe { intrinsics::abort() }
242     }
243 }