diff options
-rw-r--r-- | ports/winit/crash_handler.rs | 37 | ||||
-rw-r--r-- | ports/winit/main2.rs | 36 |
2 files changed, 39 insertions, 34 deletions
diff --git a/ports/winit/crash_handler.rs b/ports/winit/crash_handler.rs new file mode 100644 index 00000000000..b16353b9fe1 --- /dev/null +++ b/ports/winit/crash_handler.rs @@ -0,0 +1,37 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ + +#[cfg(not(any(target_os = "macos", target_os = "linux")))] +pub fn install() {} + +#[cfg(any(target_os = "macos", target_os = "linux"))] +pub fn install() { + use crate::backtrace; + use libc::_exit; + use sig::ffi::Sig; + use std::{io::Write, sync::atomic, thread}; + + extern "C" fn handler(sig: i32) { + static BEEN_HERE_BEFORE: atomic::AtomicBool = atomic::AtomicBool::new(false); + if !BEEN_HERE_BEFORE.swap(true, atomic::Ordering::SeqCst) { + let stdout = std::io::stdout(); + let mut stdout = stdout.lock(); + let _ = write!(&mut stdout, "Stack trace"); + if let Some(name) = thread::current().name() { + let _ = write!(&mut stdout, " for thread \"{}\"", name); + } + let _ = write!(&mut stdout, "\n"); + let _ = backtrace::print(&mut stdout); + } + unsafe { + _exit(sig); + } + } + + signal!(Sig::SEGV, handler); // handle segfaults + signal!(Sig::ILL, handler); // handle stack overflow and unsupported CPUs + signal!(Sig::IOT, handler); // handle double panics + signal!(Sig::BUS, handler); // handle invalid memory access +} + diff --git a/ports/winit/main2.rs b/ports/winit/main2.rs index 1319e2cb60d..77e2c18bd0a 100644 --- a/ports/winit/main2.rs +++ b/ports/winit/main2.rs @@ -13,6 +13,7 @@ extern crate sig; mod app; mod backtrace; mod browser; +mod crash_handler; mod embedder; mod events_loop; mod headed_window; @@ -44,41 +45,8 @@ pub mod platform { pub fn deinit(_clean_shutdown: bool) {} } -#[cfg(not(any(target_os = "macos", target_os = "linux")))] -fn install_crash_handler() {} - -#[cfg(any(target_os = "macos", target_os = "linux"))] -fn install_crash_handler() { - use libc::_exit; - use sig::ffi::Sig; - use std::thread; - - extern "C" fn handler(sig: i32) { - use std::sync::atomic; - static BEEN_HERE_BEFORE: atomic::AtomicBool = atomic::AtomicBool::new(false); - if !BEEN_HERE_BEFORE.swap(true, atomic::Ordering::SeqCst) { - let stdout = std::io::stdout(); - let mut stdout = stdout.lock(); - let _ = write!(&mut stdout, "Stack trace"); - if let Some(name) = thread::current().name() { - let _ = write!(&mut stdout, " for thread \"{}\"", name); - } - let _ = write!(&mut stdout, "\n"); - let _ = backtrace::print(&mut stdout); - } - unsafe { - _exit(sig); - } - } - - signal!(Sig::SEGV, handler); // handle segfaults - signal!(Sig::ILL, handler); // handle stack overflow and unsupported CPUs - signal!(Sig::IOT, handler); // handle double panics - signal!(Sig::BUS, handler); // handle invalid memory access -} - pub fn main() { - install_crash_handler(); + crash_handler::install(); resources::init(); |