2024-04-01 10:03:03 +00:00
|
|
|
use std::panic::{set_hook, take_hook, UnwindSafe};
|
2024-03-24 10:00:30 +00:00
|
|
|
|
|
|
|
use libc::STDIN_FILENO;
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
common::{read_blocked, PROGRAM_NAME},
|
|
|
|
nix::isatty,
|
2024-03-24 11:28:47 +00:00
|
|
|
threads::asan_maybe_exit,
|
2024-03-24 10:00:30 +00:00
|
|
|
};
|
|
|
|
|
2024-03-24 11:28:47 +00:00
|
|
|
pub fn panic_handler(main: impl FnOnce() -> i32 + UnwindSafe) -> ! {
|
2024-04-01 10:03:03 +00:00
|
|
|
if isatty(STDIN_FILENO) {
|
|
|
|
let standard_hook = take_hook();
|
|
|
|
set_hook(Box::new(move |panic_info| {
|
|
|
|
standard_hook(panic_info);
|
2024-04-02 05:33:59 +00:00
|
|
|
eprintf!(
|
2024-04-02 05:33:04 +00:00
|
|
|
"%s crashed, please report a bug. Debug PID %d or press Enter to exit",
|
2024-04-01 10:03:03 +00:00
|
|
|
PROGRAM_NAME.get().unwrap(),
|
|
|
|
unsafe { libc::getpid() }
|
|
|
|
);
|
|
|
|
let mut buf = [0_u8; 1];
|
|
|
|
loop {
|
|
|
|
let Ok(n) = read_blocked(STDIN_FILENO, &mut buf) else {
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
if n == 0 || matches!(buf[0], b'q' | b'\n' | b'\r') {
|
2024-04-02 05:33:59 +00:00
|
|
|
eprintf!("\n");
|
2024-04-01 10:03:03 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
let exit_status = main();
|
2024-03-24 11:28:47 +00:00
|
|
|
asan_maybe_exit(exit_status);
|
|
|
|
std::process::exit(exit_status)
|
|
|
|
}
|