Extract crash handler

Moved the crash handler code into its own file since it is a distinct concept.  It
simplifies the main method a little.
This commit is contained in:
Michael Mc Donnell 2021-02-15 17:45:51 -08:00
parent 41f8b47a53
commit 304fe775b3
No known key found for this signature in database
GPG key ID: CCA3DA738F37CD7D
2 changed files with 39 additions and 34 deletions

View file

@ -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
}

View file

@ -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();