@@ -1296,10 +1296,55 @@ pub fn init_env_logger(env: &str) {
12961296 tracing:: subscriber:: set_global_default ( subscriber) . unwrap ( ) ;
12971297}
12981298
1299+ #[ cfg( unix) ]
1300+ extern "C" {
1301+ fn backtrace_symbols_fd ( buffer : * const * mut libc:: c_void , size : libc:: c_int , fd : libc:: c_int ) ;
1302+ }
1303+
1304+ #[ cfg( unix) ]
1305+ extern "C" fn print_stack_trace ( _: libc:: c_int ) {
1306+ const MAX_FRAMES : usize = 256 ;
1307+ static mut STACK_TRACE : [ * mut libc:: c_void ; MAX_FRAMES ] = [ std:: ptr:: null_mut ( ) ; MAX_FRAMES ] ;
1308+ unsafe {
1309+ let depth = libc:: backtrace ( STACK_TRACE . as_mut_ptr ( ) , MAX_FRAMES as i32 ) ;
1310+ if depth == 0 {
1311+ return ;
1312+ }
1313+ backtrace_symbols_fd ( STACK_TRACE . as_ptr ( ) , depth, 2 ) ;
1314+ }
1315+ }
1316+
1317+ #[ cfg( unix) ]
1318+ // When an error signal (such as SIGABRT or SIGSEGV) is delivered to the
1319+ // process, print a stack trace and then exit.
1320+ fn install_signal_handler ( ) {
1321+ unsafe {
1322+ const ALT_STACK_SIZE : usize = libc:: MINSIGSTKSZ + 64 * 1024 ;
1323+ let mut alt_stack: libc:: stack_t = std:: mem:: zeroed ( ) ;
1324+ alt_stack. ss_sp =
1325+ std:: alloc:: alloc ( std:: alloc:: Layout :: from_size_align ( ALT_STACK_SIZE , 1 ) . unwrap ( ) )
1326+ as * mut libc:: c_void ;
1327+ alt_stack. ss_size = ALT_STACK_SIZE ;
1328+ libc:: sigaltstack ( & mut alt_stack, std:: ptr:: null_mut ( ) ) ;
1329+
1330+ let mut sa: libc:: sigaction = std:: mem:: zeroed ( ) ;
1331+ sa. sa_sigaction = print_stack_trace as libc:: sighandler_t ;
1332+ sa. sa_flags = libc:: SA_NODEFER | libc:: SA_RESETHAND | libc:: SA_ONSTACK ;
1333+ libc:: sigemptyset ( & mut sa. sa_mask ) ;
1334+ libc:: sigaction ( libc:: SIGSEGV , & sa, std:: ptr:: null_mut ( ) ) ;
1335+ }
1336+ }
1337+
1338+ #[ cfg( not( unix) ) ]
1339+ // When an error signal (such as SIGABRT or SIGSEGV) is delivered to the
1340+ // process, print a stack trace and then exit.
1341+ fn install_signal_handler ( ) { }
1342+
12991343pub fn main ( ) -> ! {
13001344 let start_time = Instant :: now ( ) ;
13011345 let start_rss = get_resident_set_size ( ) ;
13021346 init_rustc_env_logger ( ) ;
1347+ install_signal_handler ( ) ;
13031348 let mut callbacks = TimePassesCallbacks :: default ( ) ;
13041349 install_ice_hook ( ) ;
13051350 let exit_code = catch_with_exit_code ( || {
0 commit comments