diff --git a/src/bin/fish.rs b/src/bin/fish.rs index 812f15bd3..ae3c85079 100644 --- a/src/bin/fish.rs +++ b/src/bin/fish.rs @@ -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(); diff --git a/src/bin/fish_indent.rs b/src/bin/fish_indent.rs index 84dd8b9ca..44c0712d7 100644 --- a/src/bin/fish_indent.rs +++ b/src/bin/fish_indent.rs @@ -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 diff --git a/src/bin/fish_key_reader.rs b/src/bin/fish_key_reader.rs index 0ea355974..af7f1d48b 100644 --- a/src/bin/fish_key_reader.rs +++ b/src/bin/fish_key_reader.rs @@ -363,11 +363,11 @@ fn parse_flags(continuous_mode: &mut bool, verbose: &mut bool) -> ControlFlow i32 { - PROGRAM_NAME.set(L!("fish_key_reader")).unwrap(); let mut continuous_mode = false; let mut verbose = false; diff --git a/src/panic.rs b/src/panic.rs index 9503f4eca..6a34228ff 100644 --- a/src/panic.rs +++ b/src/panic.rs @@ -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 @@ }; 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 -}