mirror of
https://github.com/servo/servo.git
synced 2025-08-09 15:35:34 +01:00
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:
parent
41f8b47a53
commit
304fe775b3
2 changed files with 39 additions and 34 deletions
37
ports/winit/crash_handler.rs
Normal file
37
ports/winit/crash_handler.rs
Normal 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
|
||||||
|
}
|
||||||
|
|
|
@ -13,6 +13,7 @@ extern crate sig;
|
||||||
mod app;
|
mod app;
|
||||||
mod backtrace;
|
mod backtrace;
|
||||||
mod browser;
|
mod browser;
|
||||||
|
mod crash_handler;
|
||||||
mod embedder;
|
mod embedder;
|
||||||
mod events_loop;
|
mod events_loop;
|
||||||
mod headed_window;
|
mod headed_window;
|
||||||
|
@ -44,41 +45,8 @@ pub mod platform {
|
||||||
pub fn deinit(_clean_shutdown: bool) {}
|
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() {
|
pub fn main() {
|
||||||
install_crash_handler();
|
crash_handler::install();
|
||||||
|
|
||||||
resources::init();
|
resources::init();
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue