@@ -6,6 +6,18 @@ use std::{fmt, mem, ptr, slice};
6
6
7
7
use rustc_interface:: util:: { DEFAULT_STACK_SIZE , STACK_SIZE } ;
8
8
9
+ /// Signals that represent that we have a bug, and our prompt termination has
10
+ /// been ordered.
11
+ const KILL_SIGNALS : [ ( libc:: c_int , & str ) ; 7 ] = [
12
+ ( libc:: SIGILL , "SIGILL" ) ,
13
+ ( libc:: SIGTRAP , "SIGTRAP" ) ,
14
+ ( libc:: SIGABRT , "SIGABRT" ) ,
15
+ ( libc:: SIGFPE , "SIGFPE" ) ,
16
+ ( libc:: SIGBUS , "SIGBUS" ) ,
17
+ ( libc:: SIGSEGV , "SIGSEGV" ) ,
18
+ ( libc:: SIGQUIT , "SIGQUIT" ) ,
19
+ ] ;
20
+
9
21
unsafe extern "C" {
10
22
fn backtrace_symbols_fd ( buffer : * const * mut libc:: c_void , size : libc:: c_int , fd : libc:: c_int ) ;
11
23
}
@@ -39,8 +51,19 @@ macro raw_errln($tokens:tt) {
39
51
/// # Safety
40
52
///
41
53
/// Caller must ensure that this function is not re-entered.
42
- unsafe extern "C" fn print_stack_trace ( _ : libc:: c_int ) {
54
+ unsafe extern "C" fn print_stack_trace ( signum : libc:: c_int ) {
43
55
const MAX_FRAMES : usize = 256 ;
56
+
57
+ let signame = {
58
+ let mut signame = "<unknown>" ;
59
+ for sig in KILL_SIGNALS {
60
+ if sig. 0 == signum {
61
+ signame = sig. 1 ;
62
+ }
63
+ }
64
+ signame
65
+ } ;
66
+
44
67
let stack = unsafe {
45
68
// Reserve data segment so we don't have to malloc in a signal handler, which might fail
46
69
// in incredibly undesirable and unexpected ways due to e.g. the allocator deadlocking
@@ -54,7 +77,8 @@ unsafe extern "C" fn print_stack_trace(_: libc::c_int) {
54
77
} ;
55
78
56
79
// Just a stack trace is cryptic. Explain what we're doing.
57
- raw_errln ! ( "error: rustc interrupted by SIGSEGV, printing backtrace\n " ) ;
80
+ raw_errln ! ( "error: rustc interrupted by {signame}, printing backtrace\n " ) ;
81
+
58
82
let mut written = 1 ;
59
83
let mut consumed = 0 ;
60
84
// Begin elaborating return addrs into symbols and writing them directly to stderr
@@ -112,7 +136,7 @@ unsafe extern "C" fn print_stack_trace(_: libc::c_int) {
112
136
written += 2 ;
113
137
if written > 24 {
114
138
// We probably just scrolled the earlier "we got SIGSEGV" message off the terminal
115
- raw_errln ! ( "note: backtrace dumped due to SIGSEGV ! resuming signal" ) ;
139
+ raw_errln ! ( "note: backtrace dumped due to {signame} ! resuming signal" ) ;
116
140
} ;
117
141
}
118
142
@@ -129,7 +153,9 @@ pub(super) fn install() {
129
153
sa. sa_sigaction = print_stack_trace as libc:: sighandler_t ;
130
154
sa. sa_flags = libc:: SA_NODEFER | libc:: SA_RESETHAND | libc:: SA_ONSTACK ;
131
155
libc:: sigemptyset ( & mut sa. sa_mask ) ;
132
- libc:: sigaction ( libc:: SIGSEGV , & sa, ptr:: null_mut ( ) ) ;
156
+ for ( signum, _signame) in KILL_SIGNALS {
157
+ libc:: sigaction ( signum, & sa, ptr:: null_mut ( ) ) ;
158
+ }
133
159
}
134
160
}
135
161
0 commit comments