2019-01-07 11:33:07 +01:00
|
|
|
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of Parity Ethereum.
|
2017-07-03 07:31:29 +02:00
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is free software: you can redistribute it and/or modify
|
2017-07-03 07:31:29 +02:00
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is distributed in the hope that it will be useful,
|
2017-07-03 07:31:29 +02:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2018-06-04 10:19:50 +02:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2017-07-03 07:31:29 +02:00
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
2019-01-07 11:33:07 +01:00
|
|
|
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2017-07-03 07:31:29 +02:00
|
|
|
|
|
|
|
//! Custom panic hook with bug report link
|
|
|
|
|
|
|
|
extern crate backtrace;
|
|
|
|
|
2018-01-20 20:28:25 +01:00
|
|
|
use backtrace::Backtrace;
|
2020-08-05 06:08:03 +02:00
|
|
|
use std::{
|
|
|
|
panic::{self, PanicInfo},
|
|
|
|
process, thread,
|
|
|
|
};
|
2017-07-03 07:31:29 +02:00
|
|
|
|
2018-08-10 11:06:30 +02:00
|
|
|
/// Set the panic hook to write to stderr and abort the process when a panic happens.
|
2018-07-02 11:53:50 +02:00
|
|
|
pub fn set_abort() {
|
2020-08-05 06:08:03 +02:00
|
|
|
set_with(|msg| {
|
|
|
|
eprintln!("{}", msg);
|
|
|
|
process::abort()
|
|
|
|
});
|
2018-07-02 11:53:50 +02:00
|
|
|
}
|
|
|
|
|
2018-08-10 11:06:30 +02:00
|
|
|
/// Set the panic hook with a closure to be called. The closure receives the panic message.
|
|
|
|
///
|
|
|
|
/// Depending on how Parity was compiled, after the closure has been executed, either the process
|
|
|
|
/// aborts or unwinding starts.
|
|
|
|
///
|
|
|
|
/// If you panic within the closure, a double panic happens and the process will stop.
|
|
|
|
pub fn set_with<F>(f: F)
|
2020-08-05 06:08:03 +02:00
|
|
|
where
|
|
|
|
F: Fn(&str) + Send + Sync + 'static,
|
2018-08-10 11:06:30 +02:00
|
|
|
{
|
2020-08-05 06:08:03 +02:00
|
|
|
panic::set_hook(Box::new(move |info| {
|
|
|
|
let msg = gen_panic_msg(info);
|
|
|
|
f(&msg);
|
|
|
|
}));
|
2017-07-03 07:31:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static ABOUT_PANIC: &str = "
|
|
|
|
This is a bug. Please report it at:
|
|
|
|
|
2018-07-23 10:18:49 +02:00
|
|
|
https://github.com/paritytech/parity-ethereum/issues/new
|
2017-07-03 07:31:29 +02:00
|
|
|
";
|
|
|
|
|
2018-08-10 11:06:30 +02:00
|
|
|
fn gen_panic_msg(info: &PanicInfo) -> String {
|
2020-08-05 06:08:03 +02:00
|
|
|
let location = info.location();
|
|
|
|
let file = location.as_ref().map(|l| l.file()).unwrap_or("<unknown>");
|
|
|
|
let line = location.as_ref().map(|l| l.line()).unwrap_or(0);
|
2017-07-03 07:31:29 +02:00
|
|
|
|
2020-08-05 06:08:03 +02:00
|
|
|
let msg = match info.payload().downcast_ref::<&'static str>() {
|
|
|
|
Some(s) => *s,
|
|
|
|
None => match info.payload().downcast_ref::<String>() {
|
|
|
|
Some(s) => &s[..],
|
|
|
|
None => "Box<Any>",
|
|
|
|
},
|
|
|
|
};
|
2017-07-03 07:31:29 +02:00
|
|
|
|
2020-08-05 06:08:03 +02:00
|
|
|
let thread = thread::current();
|
|
|
|
let name = thread.name().unwrap_or("<unnamed>");
|
2017-07-03 07:31:29 +02:00
|
|
|
|
2020-08-05 06:08:03 +02:00
|
|
|
let backtrace = Backtrace::new();
|
2017-07-03 07:31:29 +02:00
|
|
|
|
2020-08-05 06:08:03 +02:00
|
|
|
format!(
|
|
|
|
r#"
|
2017-07-03 07:31:29 +02:00
|
|
|
|
2018-08-10 11:06:30 +02:00
|
|
|
====================
|
2017-07-03 07:31:29 +02:00
|
|
|
|
2018-08-10 11:06:30 +02:00
|
|
|
{backtrace:?}
|
|
|
|
|
|
|
|
Thread '{name}' panicked at '{msg}', {file}:{line}
|
|
|
|
{about}
|
2020-08-05 06:08:03 +02:00
|
|
|
"#,
|
|
|
|
backtrace = backtrace,
|
|
|
|
name = name,
|
|
|
|
msg = msg,
|
|
|
|
file = file,
|
|
|
|
line = line,
|
|
|
|
about = ABOUT_PANIC
|
|
|
|
)
|
2017-07-03 07:31:29 +02:00
|
|
|
}
|