Use panic::set_hook instead of catch_unwind to help debug panics

This commit is contained in:
Johannes Altmanninger 2024-04-01 12:03:03 +02:00
parent 698d8bd315
commit af6dc9221f
4 changed files with 26 additions and 33 deletions

View File

@ -464,6 +464,7 @@ fn cstr_from_osstr(s: &OsStr) -> CString {
}
fn main() {
PROGRAM_NAME.set(L!("fish")).unwrap();
panic_handler(throwing_main)
}
@ -472,10 +473,6 @@ fn throwing_main() -> i32 {
.map(|osstr| str2wcstring(osstr.as_bytes()))
.collect();
PROGRAM_NAME
.set(L!("fish"))
.expect("multiple entrypoints setting PROGRAM_NAME");
let mut res = 1;
signal_unblock_all();

View File

@ -738,11 +738,11 @@ fn char_is_escaped(text: &wstr, idx: usize) -> bool {
}
fn main() {
PROGRAM_NAME.set(L!("fish_indent")).unwrap();
panic_handler(throwing_main)
}
fn throwing_main() -> i32 {
PROGRAM_NAME.set(L!("fish_indent")).unwrap();
topic_monitor_init();
threads::init();
// Using the user's default locale could be a problem if it doesn't use UTF-8 encoding. That's

View File

@ -363,11 +363,11 @@ fn parse_flags(continuous_mode: &mut bool, verbose: &mut bool) -> ControlFlow<i3
}
fn main() {
PROGRAM_NAME.set(L!("fish_key_reader")).unwrap();
panic_handler(throwing_main)
}
fn throwing_main() -> i32 {
PROGRAM_NAME.set(L!("fish_key_reader")).unwrap();
let mut continuous_mode = false;
let mut verbose = false;

View File

@ -1,4 +1,4 @@
use std::panic::{catch_unwind, UnwindSafe};
use std::panic::{set_hook, take_hook, UnwindSafe};
use libc::STDIN_FILENO;
@ -9,32 +9,28 @@ use crate::{
};
pub fn panic_handler(main: impl FnOnce() -> i32 + UnwindSafe) -> ! {
let exit_status = panic_handler_impl(main);
if isatty(STDIN_FILENO) {
let standard_hook = take_hook();
set_hook(Box::new(move |panic_info| {
standard_hook(panic_info);
printf!(
"%s with crashed, please report a bug. Debug PID %d or press Enter to exit",
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') {
printf!("\n");
break;
}
}
}));
}
let exit_status = main();
asan_maybe_exit(exit_status);
std::process::exit(exit_status)
}
fn panic_handler_impl(main: impl FnOnce() -> i32 + UnwindSafe) -> i32 {
if !isatty(STDIN_FILENO) {
return main();
}
if let Ok(status) = catch_unwind(main) {
return status;
}
printf!(
"%s with PID %d crashed, please report a bug. Press Enter to exit",
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') {
printf!("\n");
break;
}
}
110
}