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[] = {
135 DEFINE_ENTRY(SI_ASYNCNL)
136 DEFINE_ENTRY(SI_TKILL)
137 DEFINE_ENTRY(SI_SIGIO)
138 DEFINE_ENTRY(SI_ASYNCIO)
139 DEFINE_ENTRY(SI_MESGQ)
140 DEFINE_ENTRY(SI_TIMER)
141 DEFINE_ENTRY(SI_QUEUE)
142 DEFINE_ENTRY(SI_USER)
143 DEFINE_ENTRY(SI_KERNEL)
147 static struct str_table sigill_code_table[] = {
149 DEFINE_ENTRY(ILL_ILLOPC)
152 DEFINE_ENTRY(ILL_ILLOPN)
155 DEFINE_ENTRY(ILL_ILLADR)
158 DEFINE_ENTRY(ILL_ILLTRP)
161 DEFINE_ENTRY(ILL_PRVOPC)
164 DEFINE_ENTRY(ILL_PRVREG)
167 DEFINE_ENTRY(ILL_COPROC)
170 DEFINE_ENTRY(ILL_BADSTK)
173 DEFINE_ENTRY(BUS_ADRALN)
176 DEFINE_ENTRY(BUS_ADRERR)
179 DEFINE_ENTRY(BUS_OBJERR)
184 static struct str_table sigfpe_code_table[] = {
186 DEFINE_ENTRY(FPE_INTDIV)
189 DEFINE_ENTRY(FPE_INTOVF)
192 DEFINE_ENTRY(FPE_FLTDIV)
195 DEFINE_ENTRY(FPE_FLTOVF)
198 DEFINE_ENTRY(FPE_FLTUND)
201 DEFINE_ENTRY(FPE_FLTRES)
204 DEFINE_ENTRY(FPE_FLTINV)
207 DEFINE_ENTRY(FPE_FLTSUB)
212 static struct str_table sigsegv_code_table[] = {
214 DEFINE_ENTRY(SEGV_MAPERR)
217 DEFINE_ENTRY(SEGV_ACCERR)
223 static struct str_table sigbus_code_table[] = {
225 DEFINE_ENTRY(BUS_ADRALN)
228 DEFINE_ENTRY(BUS_ADRERR)
231 DEFINE_ENTRY(BUS_OBJERR)
236 static struct str_table sigstrap_code_table[] = {
238 DEFINE_ENTRY(TRAP_BRKPT)
241 DEFINE_ENTRY(TRAP_TRACE)
246 static struct str_table sigcld_code_table[] = {
248 DEFINE_ENTRY(CLD_EXITED)
251 DEFINE_ENTRY(CLD_KILLED)
254 DEFINE_ENTRY(CLD_DUMPED)
257 DEFINE_ENTRY(CLD_TRAPPED)
260 DEFINE_ENTRY(CLD_STOPPED)
263 DEFINE_ENTRY(CLD_CONTINUED)
268 static struct str_table sigpoll_code_table[] = {
270 DEFINE_ENTRY(POLL_IN)
273 DEFINE_ENTRY(POLL_OUT)
276 DEFINE_ENTRY(POLL_MSG)
279 DEFINE_ENTRY(POLL_ERR)
282 DEFINE_ENTRY(POLL_PRI)
285 DEFINE_ENTRY(POLL_HUP)
290 static const char *lookup_table(int num, struct str_table *table)
294 for (p=table; p->name; p++)
300 static const char *lookup_table_fallback(int num, struct str_table *table)
303 const char *ret = lookup_table(num, table);
307 snprintf(buf, sizeof(buf), "%d", num);
308 buf[sizeof(buf)-1] = 0;
312 static void die_signal_handler(int signum, siginfo_t *siginfo, void *context)
314 void *stack_syms[32];
318 fprintf(stderr, "Signal (%d) %s ", signum,
319 lookup_table_fallback(signum, sig_table));
320 if (siginfo->si_code == SI_USER)
321 fprintf(stderr, "(sent from pid %u) ", siginfo->si_pid);
322 cp = lookup_table(siginfo->si_code, generic_code_table);
324 fprintf(stderr, "si_code=%s ", cp);
325 else if (signum == SIGILL)
326 fprintf(stderr, "si_code=%s ",
327 lookup_table_fallback(siginfo->si_code,
329 else if (signum == SIGFPE)
330 fprintf(stderr, "si_code=%s ",
331 lookup_table_fallback(siginfo->si_code,
333 else if (signum == SIGSEGV)
334 fprintf(stderr, "si_code=%s ",
335 lookup_table_fallback(siginfo->si_code,
336 sigsegv_code_table));
337 else if (signum == SIGBUS)
338 fprintf(stderr, "si_code=%s ",
339 lookup_table_fallback(siginfo->si_code,
341 else if (signum == SIGCLD)
342 fprintf(stderr, "si_code=%s ",
343 lookup_table_fallback(siginfo->si_code,
346 fprintf(stderr, "si code=%d ", siginfo->si_code);
347 if ((siginfo->si_code != SI_USER) &&
348 (signum == SIGILL || signum == SIGFPE ||
349 signum == SIGSEGV || signum == SIGBUS))
350 fprintf(stderr, "fault addr=%p", siginfo->si_addr);
351 fprintf(stderr, "\n");
353 #ifdef HAVE_BACKTRACE
354 frames = backtrace(stack_syms, 32);
355 backtrace_symbols_fd(stack_syms, frames, 2);
360 void sigcatcher_setup(void)
364 memset(&sa, 0, sizeof(struct sigaction));
365 sa.sa_sigaction = die_signal_handler;
366 sa.sa_flags = SA_SIGINFO;
368 sigaction(SIGFPE, &sa, 0);
369 sigaction(SIGILL, &sa, 0);
370 sigaction(SIGBUS, &sa, 0);
371 sigaction(SIGSEGV, &sa, 0);
380 fprintf(stderr, "tst_sigcatcher: [-akfn]\n");
384 int main(int argc, char** argv)
391 memset(&sa, 0, sizeof(struct sigaction));
392 sa.sa_sigaction = die_signal_handler;
393 sa.sa_flags = SA_SIGINFO;
394 for (i=1; i < 31; i++)
395 sigaction(i, &sa, 0);
397 while ((c = getopt (argc, argv, "afkn")) != EOF)
403 printf("%d\n", 42/x);
405 kill(getpid(), SIGTERM);
413 printf("Sleeping for 10 seconds, send kill signal to pid %u...\n",