Skip to content

Commit e63adc7

Browse files
committed
Use signal() for Windows support
1 parent 3c0320e commit e63adc7

File tree

2 files changed

+7
-28
lines changed

2 files changed

+7
-28
lines changed

crates/ark/src/main.rs

-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,6 @@ fn main() {
260260

261261
// Register segfault handler to get a backtrace. Should be after
262262
// initialising `log!`.
263-
#[cfg(not(target_os = "windows"))]
264263
stdext::traps::register_trap_handlers();
265264

266265
// Initialize harp.

crates/stdext/src/traps.rs

+7-27
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,20 @@
99
// SIGSEGV to generate a backtrace with `info` verbosity (lowest level so
1010
// it's always reported).
1111
//
12-
// This could be supported on windows too (SIGSEGV is one of the rare
13-
// supported signals) but with `libc::signal()` instead of
14-
// `libc::sigaction()`
15-
#[cfg(not(target_os = "windows"))]
12+
// This uses `signal()` instead of `sigaction()` for Windows support
13+
// (SIGSEGV is one of the rare supported signals)
1614
pub fn register_trap_handlers() {
1715
unsafe {
18-
let mut action: libc::sigaction = std::mem::zeroed();
19-
action.sa_flags = libc::SA_SIGINFO | libc::SA_ONSTACK;
20-
action.sa_sigaction = backtrace_handler as libc::sighandler_t;
21-
22-
libc::sigaction(libc::SIGBUS, &action, std::ptr::null_mut());
23-
libc::sigaction(libc::SIGSEGV, &action, std::ptr::null_mut());
16+
libc::signal(libc::SIGBUS, backtrace_handler as libc::sighandler_t);
17+
libc::signal(libc::SIGSEGV, backtrace_handler as libc::sighandler_t);
2418
}
2519
}
2620

27-
#[cfg(not(target_os = "windows"))]
28-
pub fn reset_traps_handler() {
21+
extern "C" fn backtrace_handler(signum: libc::c_int) {
22+
// Prevent infloop into the handler
2923
unsafe {
30-
let mut action: libc::sigaction = std::mem::zeroed();
31-
action.sa_sigaction = libc::SIG_DFL;
32-
33-
libc::sigaction(libc::SIGBUS, &action, std::ptr::null_mut());
34-
libc::sigaction(libc::SIGSEGV, &action, std::ptr::null_mut());
24+
libc::signal(signum, libc::SIG_DFL);
3525
}
36-
}
37-
38-
#[cfg(not(target_os = "windows"))]
39-
extern "C" fn backtrace_handler(
40-
signum: libc::c_int,
41-
_info: *mut libc::siginfo_t,
42-
_data: *mut libc::c_void,
43-
) {
44-
// Prevent infloop into the handler
45-
reset_traps_handler();
4626

4727
let mut header = format!("\n>>> Backtrace for signal {}", signum);
4828

0 commit comments

Comments
 (0)