2 * sigcatcher.c --- print a backtrace on a SIGSEGV, et. al
4 * Copyright (C) 2011 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Public
17 #ifdef HAVE_EXECINFO_H
28 #define DEFINE_ENTRY(SYM) { SYM, #SYM },
29 #define END_TABLE { 0, 0 }
31 static struct str_table sig_table[] = {
81 DEFINE_ENTRY(SIGSTKFLT)
105 DEFINE_ENTRY(SIGXCPU)
108 DEFINE_ENTRY(SIGXFSZ)
111 DEFINE_ENTRY(SIGVTALRM)
114 DEFINE_ENTRY(SIGPROF)
117 DEFINE_ENTRY(SIGWINCH)
123 DEFINE_ENTRY(SIGPOLL)
134 static struct str_table generic_code_table[] = {
136 DEFINE_ENTRY(SI_ASYNCNL)
139 DEFINE_ENTRY(SI_TKILL)
142 DEFINE_ENTRY(SI_SIGIO)
145 DEFINE_ENTRY(SI_ASYNCIO)
148 DEFINE_ENTRY(SI_MESGQ)
151 DEFINE_ENTRY(SI_TIMER)
154 DEFINE_ENTRY(SI_QUEUE)
157 DEFINE_ENTRY(SI_USER)
160 DEFINE_ENTRY(SI_KERNEL)
165 static struct str_table sigill_code_table[] = {
167 DEFINE_ENTRY(ILL_ILLOPC)
170 DEFINE_ENTRY(ILL_ILLOPN)
173 DEFINE_ENTRY(ILL_ILLADR)
176 DEFINE_ENTRY(ILL_ILLTRP)
179 DEFINE_ENTRY(ILL_PRVOPC)
182 DEFINE_ENTRY(ILL_PRVREG)
185 DEFINE_ENTRY(ILL_COPROC)
188 DEFINE_ENTRY(ILL_BADSTK)
191 DEFINE_ENTRY(BUS_ADRALN)
194 DEFINE_ENTRY(BUS_ADRERR)
197 DEFINE_ENTRY(BUS_OBJERR)
202 static struct str_table sigfpe_code_table[] = {
204 DEFINE_ENTRY(FPE_INTDIV)
207 DEFINE_ENTRY(FPE_INTOVF)
210 DEFINE_ENTRY(FPE_FLTDIV)
213 DEFINE_ENTRY(FPE_FLTOVF)
216 DEFINE_ENTRY(FPE_FLTUND)
219 DEFINE_ENTRY(FPE_FLTRES)
222 DEFINE_ENTRY(FPE_FLTINV)
225 DEFINE_ENTRY(FPE_FLTSUB)
230 static struct str_table sigsegv_code_table[] = {
232 DEFINE_ENTRY(SEGV_MAPERR)
235 DEFINE_ENTRY(SEGV_ACCERR)
241 static struct str_table sigbus_code_table[] = {
243 DEFINE_ENTRY(BUS_ADRALN)
246 DEFINE_ENTRY(BUS_ADRERR)
249 DEFINE_ENTRY(BUS_OBJERR)
254 static struct str_table sigstrap_code_table[] = {
256 DEFINE_ENTRY(TRAP_BRKPT)
259 DEFINE_ENTRY(TRAP_TRACE)
264 static struct str_table sigcld_code_table[] = {
266 DEFINE_ENTRY(CLD_EXITED)
269 DEFINE_ENTRY(CLD_KILLED)
272 DEFINE_ENTRY(CLD_DUMPED)
275 DEFINE_ENTRY(CLD_TRAPPED)
278 DEFINE_ENTRY(CLD_STOPPED)
281 DEFINE_ENTRY(CLD_CONTINUED)
286 static struct str_table sigpoll_code_table[] = {
288 DEFINE_ENTRY(POLL_IN)
291 DEFINE_ENTRY(POLL_OUT)
294 DEFINE_ENTRY(POLL_MSG)
297 DEFINE_ENTRY(POLL_ERR)
300 DEFINE_ENTRY(POLL_PRI)
303 DEFINE_ENTRY(POLL_HUP)
308 static const char *lookup_table(int num, struct str_table *table)
312 for (p=table; p->name; p++)
318 static const char *lookup_table_fallback(int num, struct str_table *table)
321 const char *ret = lookup_table(num, table);
325 snprintf(buf, sizeof(buf), "%d", num);
326 buf[sizeof(buf)-1] = 0;
330 static void die_signal_handler(int signum, siginfo_t *siginfo, void *context)
332 void *stack_syms[32];
336 fprintf(stderr, "Signal (%d) %s ", signum,
337 lookup_table_fallback(signum, sig_table));
338 if (siginfo->si_code == SI_USER)
339 fprintf(stderr, "(sent from pid %u) ", siginfo->si_pid);
340 cp = lookup_table(siginfo->si_code, generic_code_table);
342 fprintf(stderr, "si_code=%s ", cp);
343 else if (signum == SIGILL)
344 fprintf(stderr, "si_code=%s ",
345 lookup_table_fallback(siginfo->si_code,
347 else if (signum == SIGFPE)
348 fprintf(stderr, "si_code=%s ",
349 lookup_table_fallback(siginfo->si_code,
351 else if (signum == SIGSEGV)
352 fprintf(stderr, "si_code=%s ",
353 lookup_table_fallback(siginfo->si_code,
354 sigsegv_code_table));
355 else if (signum == SIGBUS)
356 fprintf(stderr, "si_code=%s ",
357 lookup_table_fallback(siginfo->si_code,
359 else if (signum == SIGCHLD)
360 fprintf(stderr, "si_code=%s ",
361 lookup_table_fallback(siginfo->si_code,
364 fprintf(stderr, "si code=%d ", siginfo->si_code);
365 if ((siginfo->si_code != SI_USER) &&
366 (signum == SIGILL || signum == SIGFPE ||
367 signum == SIGSEGV || signum == SIGBUS))
368 fprintf(stderr, "fault addr=%p", siginfo->si_addr);
369 fprintf(stderr, "\n");
371 #ifdef HAVE_BACKTRACE
372 frames = backtrace(stack_syms, 32);
373 backtrace_symbols_fd(stack_syms, frames, 2);
378 void sigcatcher_setup(void)
382 memset(&sa, 0, sizeof(struct sigaction));
383 sa.sa_sigaction = die_signal_handler;
384 sa.sa_flags = SA_SIGINFO;
386 sigaction(SIGFPE, &sa, 0);
387 sigaction(SIGILL, &sa, 0);
388 sigaction(SIGBUS, &sa, 0);
389 sigaction(SIGSEGV, &sa, 0);
398 fprintf(stderr, "tst_sigcatcher: [-akfn]\n");
402 int main(int argc, char** argv)
409 memset(&sa, 0, sizeof(struct sigaction));
410 sa.sa_sigaction = die_signal_handler;
411 sa.sa_flags = SA_SIGINFO;
412 for (i=1; i < 31; i++)
413 sigaction(i, &sa, 0);
415 while ((c = getopt (argc, argv, "afkn")) != EOF)
421 printf("%d\n", 42/x);
423 kill(getpid(), SIGTERM);
431 printf("Sleeping for 10 seconds, send kill signal to pid %u...\n",