1+ // LD_PRELOAD crash reporter probe: on SIGABRT/SIGSEGV/SIGBUS/SIGILL write to
2+ // $ABORT_TRACE_DIR/trace.<pid>.<sig>.txt: siginfo, registers, the executable's text
3+ // mappings (for offset math), libgcc backtrace, a frame-pointer walk, and a scan of
4+ // the stack for return addresses inside the executable. No memory contents beyond
5+ // code addresses are written. Then re-raise with the default action.
6+ #define _GNU_SOURCE
7+ #include <execinfo.h>
8+ #include <fcntl.h>
9+ #include <signal.h>
10+ #include <stdio.h>
11+ #include <stdlib.h>
12+ #include <string.h>
13+ #include <ucontext.h>
14+ #include <unistd.h>
15+ #include <link.h>
16+
17+ static unsigned long exe_base ; static unsigned long stack_lo , stack_hi ;
18+ static unsigned long seg_lo [16 ], seg_hi [16 ]; static int nseg ;
19+ static int phdr_cb (struct dl_phdr_info * i , size_t n , void * d ) { (void )n ; (void )d ; exe_base = i -> dlpi_addr ;
20+ for (int k = 0 ; k < i -> dlpi_phnum && nseg < 16 ; k ++ ) if (i -> dlpi_phdr [k ].p_type == PT_LOAD && (i -> dlpi_phdr [k ].p_flags & PF_X )) {
21+ seg_lo [nseg ] = exe_base + i -> dlpi_phdr [k ].p_vaddr ; seg_hi [nseg ] = seg_lo [nseg ] + i -> dlpi_phdr [k ].p_memsz ; nseg ++ ; }
22+ return 1 ; }
23+
24+ typedef struct { unsigned long lo , hi , off ; } map_t ;
25+ static map_t text [16 ]; static int ntext ;
26+ static char exe [512 ];
27+
28+ static unsigned long cur_rsp ;
29+ static void load_maps (int fd ) {
30+ int mfd = open ("/proc/self/maps" , O_RDONLY );
31+ if (mfd < 0 ) return ;
32+ static char buf [1 << 16 ]; ssize_t n = 0 , r ;
33+ while (n < (ssize_t )sizeof buf - 1 && (r = read (mfd , buf + n , sizeof buf - 1 - n )) > 0 ) n += r ;
34+ close (mfd ); buf [n ] = 0 ;
35+ readlink ("/proc/self/exe" , exe , sizeof exe - 1 );
36+ char * line = buf ;
37+ while (line && * line ) {
38+ char * nl = strchr (line , '\n' ); if (nl ) * nl = 0 ;
39+ unsigned long lo , hi , off ; char perms [8 ];
40+ if (sscanf (line , "%lx-%lx %7s %lx" , & lo , & hi , perms , & off ) == 4 && cur_rsp >= lo && cur_rsp < hi ) { stack_lo = lo ; stack_hi = hi ; dprintf (fd , "stack-map %s\n" , line ); }
41+ if (sscanf (line , "%lx-%lx %7s %lx" , & lo , & hi , perms , & off ) == 4 && perms [2 ] == 'x' && strstr (line , exe ) && ntext < 16 ) {
42+ text [ntext ].lo = lo ; text [ntext ].hi = hi ; text [ntext ].off = off ; ntext ++ ;
43+ dprintf (fd , "text-map %s\n" , line );
44+ }
45+ line = nl ? nl + 1 : NULL ;
46+ }
47+ }
48+ static long exe_off (unsigned long a ) {
49+ for (int i = 0 ; i < nseg ; i ++ ) if (a >= seg_lo [i ] && a < seg_hi [i ]) return (long )(a - exe_base );
50+ return -1 ;
51+ }
52+
53+ // Probe-only watchdog recommended in the design, preserving SIGABRT on expiry.
54+ static void watchdog (int unused ) {
55+ (void )unused ;
56+ signal (SIGABRT , SIG_DFL );
57+ sigset_t set ; sigemptyset (& set ); sigaddset (& set , SIGABRT );
58+ sigprocmask (SIG_UNBLOCK , & set , NULL );
59+ raise (SIGABRT );
60+ _exit (134 );
61+ }
62+ static void handler (int sig , siginfo_t * info , void * ctx ) {
63+ alarm (5 );
64+ #ifdef PROBE_STALL
65+ sleep (20 );
66+ #endif
67+ const char * dir = getenv ("ABORT_TRACE_DIR" );
68+ if (dir ) {
69+ char path [600 ]; snprintf (path , sizeof path , "%s/trace.%d.%d.txt" , dir , (int )getpid (), sig );
70+ int fd = open (path , O_WRONLY | O_CREAT | O_TRUNC , 0600 );
71+ if (fd >= 0 ) {
72+ ucontext_t * uc = (ucontext_t * )ctx ;
73+ unsigned long rip = uc -> uc_mcontext .gregs [REG_RIP ], rsp = uc -> uc_mcontext .gregs [REG_RSP ], rbp = uc -> uc_mcontext .gregs [REG_RBP ];
74+ dprintf (fd , "signal=%d si_code=%d si_pid=%d pid=%d tid=%ld\n" , sig , info -> si_code , (int )info -> si_pid , (int )getpid (), (long )gettid ());
75+ dprintf (fd , "rip=%#lx rsp=%#lx rbp=%#lx\n" , rip , rsp , rbp );
76+ cur_rsp = rsp ; load_maps (fd );
77+ for (int i = 0 ; i < nseg ; i ++ ) dprintf (fd , "exec-segment %#lx-%#lx (vaddr %#lx)\n" , seg_lo [i ], seg_hi [i ], seg_lo [i ] - exe_base );
78+ dprintf (fd , "exe=%s base=%#lx (offsets below are vaddr-relative, same convention as backtrace_symbols)\n" , exe , exe_base );
79+ dprintf (fd , "--- libgcc backtrace\n" );
80+ void * frames [64 ]; int cnt = backtrace (frames , 64 ); backtrace_symbols_fd (frames , cnt , fd );
81+ dprintf (fd , "--- frame-pointer walk\n" );
82+ unsigned long * fp = (unsigned long * )rbp ;
83+ for (int i = 0 ; i < 64 && fp && (unsigned long )fp > rsp && (unsigned long )fp < rsp + (64UL << 20 ); i ++ ) {
84+ unsigned long ret = fp [1 ]; long off = exe_off (ret );
85+ dprintf (fd , "fp#%d ret=%#lx exe+%#lx\n" , i , ret , off < 0 ? 0UL : (unsigned long )off );
86+ unsigned long * next = (unsigned long * )fp [0 ]; if (next <= fp ) break ; fp = next ;
87+ }
88+ dprintf (fd , "--- stack scan (return addresses inside the executable, rsp upwards, 64 KiB)\n" );
89+ unsigned long * sp = (unsigned long * )(rsp & ~7UL );
90+ unsigned long limit = stack_hi ? (stack_hi - (unsigned long )sp ) / 8 : 0 ; if (limit > 8192 ) limit = 8192 ;
91+ for (unsigned long i = 0 ; i < limit ; i ++ ) { long off = exe_off (sp [i ]); if (off >= 0 ) dprintf (fd , "sp+%#x exe+%#lx\n" , i * 8 , (unsigned long )off ); }
92+ close (fd );
93+ }
94+ }
95+ signal (sig , SIG_DFL ); raise (sig );
96+ }
97+ __attribute__((constructor )) static void init (void ) {
98+ struct sigaction sa ; memset (& sa , 0 , sizeof sa );
99+ sa .sa_sigaction = handler ; sa .sa_flags = SA_SIGINFO | SA_ONSTACK | SA_RESETHAND ;
100+ signal (SIGALRM , watchdog );
101+ dl_iterate_phdr (phdr_cb , NULL );
102+ sigaction (SIGABRT , & sa , NULL ); sigaction (SIGSEGV , & sa , NULL ); sigaction (SIGBUS , & sa , NULL ); sigaction (SIGILL , & sa , NULL );
103+ }
0 commit comments