add dbus command to allow checking of wether hardware is currently exported
[a2jmidid.git] / sigsegv.c
blob411e904853d91f85fd13f9071fdab81b6e8db488
1 /**
2 * This source file is used to print out a stack-trace when your program
3 * segfaults. It is relatively reliable and spot-on accurate.
5 * This code is in the public domain. Use it as you see fit, some credit
6 * would be appreciated, but is not a prerequisite for usage. Feedback
7 * on it's use would encourage further development and maintenance.
9 * Author: Jaco Kroon <jaco@kroon.co.za>
11 * Copyright (C) 2005 - 2008 Jaco Kroon
14 #if defined(HAVE_CONFIG_H)
15 #include "config.h"
16 #endif
18 #define NO_CPP_DEMANGLE
19 #define SIGSEGV_NO_AUTO_INIT
21 #ifndef _GNU_SOURCE
22 # define _GNU_SOURCE
23 #endif
25 #include <memory.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <signal.h>
29 #include <ucontext.h>
30 #include <dlfcn.h>
31 #include <execinfo.h>
32 #include <errno.h>
33 #ifndef NO_CPP_DEMANGLE
34 //#include <cxxabi.h>
35 char * __cxa_demangle(const char * __mangled_name, char * __output_buffer, size_t * __length, int * __status);
36 #endif
38 #include <stdbool.h>
39 #include "log.h"
41 #if defined(REG_RIP)
42 # define SIGSEGV_STACK_IA64
43 # define REGFORMAT "%016lx"
44 #elif defined(REG_EIP)
45 # define SIGSEGV_STACK_X86
46 # define REGFORMAT "%08x"
47 #else
48 # define SIGSEGV_STACK_GENERIC
49 # define REGFORMAT "%x"
50 #endif
52 static void signal_segv(int signum, siginfo_t* info, void*ptr) {
53 static const char *si_codes[3] = {"", "SEGV_MAPERR", "SEGV_ACCERR"};
55 size_t i;
56 ucontext_t *ucontext = (ucontext_t*)ptr;
58 #if defined(SIGSEGV_STACK_X86) || defined(SIGSEGV_STACK_IA64)
59 int f = 0;
60 Dl_info dlinfo;
61 void **bp = 0;
62 void *ip = 0;
63 #else
64 void *bt[20];
65 char **strings;
66 size_t sz;
67 #endif
69 if (signum == SIGSEGV)
71 a2j_error("Segmentation Fault!");
73 else if (signum == SIGABRT)
75 a2j_error("Abort!");
77 else if (signum == SIGILL)
79 a2j_error("Illegal instruction!");
81 else if (signum == SIGFPE)
83 a2j_error("Floating point exception!");
85 else
87 a2j_error("Unknown bad signal catched!");
90 a2j_error("info.si_signo = %d", signum);
91 a2j_error("info.si_errno = %d", info->si_errno);
92 a2j_error("info.si_code = %d (%s)", info->si_code, si_codes[info->si_code]);
93 a2j_error("info.si_addr = %p", info->si_addr);
94 #if !defined(__alpha__) && !defined(__ia64__) && !defined(__FreeBSD_kernel__) && !defined(__arm__) && !defined(__hppa__) && !defined(__sh__)
95 for(i = 0; i < NGREG; i++)
96 a2j_error("reg[%02d] = 0x" REGFORMAT, i,
97 #if defined(__powerpc__)
98 ucontext->uc_mcontext.uc_regs[i]
99 #elif defined(__sparc__) && defined(__arch64__)
100 ucontext->uc_mcontext.mc_gregs[i]
101 #else
102 ucontext->uc_mcontext.gregs[i]
103 #endif
105 #endif /* alpha, ia64, kFreeBSD, arm, hppa */
107 #if defined(SIGSEGV_STACK_X86) || defined(SIGSEGV_STACK_IA64)
108 # if defined(SIGSEGV_STACK_IA64)
109 ip = (void*)ucontext->uc_mcontext.gregs[REG_RIP];
110 bp = (void**)ucontext->uc_mcontext.gregs[REG_RBP];
111 # elif defined(SIGSEGV_STACK_X86)
112 ip = (void*)ucontext->uc_mcontext.gregs[REG_EIP];
113 bp = (void**)ucontext->uc_mcontext.gregs[REG_EBP];
114 # endif
116 a2j_error("Stack trace:");
117 while(bp && ip) {
118 if(!dladdr(ip, &dlinfo))
119 break;
121 const char *symname = dlinfo.dli_sname;
122 #ifndef NO_CPP_DEMANGLE
123 int status;
124 char *tmp = __cxa_demangle(symname, NULL, 0, &status);
126 if(status == 0 && tmp)
127 symname = tmp;
128 #endif
130 a2j_error("% 2d: %p <%s+%u> (%s)",
131 ++f,
133 symname,
134 (unsigned)(ip - dlinfo.dli_saddr),
135 dlinfo.dli_fname);
137 #ifndef NO_CPP_DEMANGLE
138 if(tmp)
139 free(tmp);
140 #endif
142 if(dlinfo.dli_sname && !strcmp(dlinfo.dli_sname, "main"))
143 break;
145 ip = bp[1];
146 bp = (void**)bp[0];
148 #else
149 a2j_error("Stack trace (non-dedicated):");
150 sz = backtrace(bt, 20);
151 strings = backtrace_symbols(bt, sz);
153 for(i = 0; i < sz; ++i)
154 a2j_error("%s", strings[i]);
155 #endif
156 a2j_error("End of stack trace");
157 exit (-1);
160 int setup_sigsegv() {
161 struct sigaction action;
163 memset(&action, 0, sizeof(action));
164 action.sa_sigaction = signal_segv;
165 action.sa_flags = SA_SIGINFO;
166 if(sigaction(SIGSEGV, &action, NULL) < 0) {
167 a2j_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
168 return 0;
171 if(sigaction(SIGILL, &action, NULL) < 0) {
172 a2j_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
173 return 0;
176 if(sigaction(SIGABRT, &action, NULL) < 0) {
177 a2j_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
178 return 0;
181 if(sigaction(SIGFPE, &action, NULL) < 0) {
182 a2j_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
183 return 0;
186 return 1;
189 #ifndef SIGSEGV_NO_AUTO_INIT
190 static void __attribute((constructor)) init(void) {
191 setup_sigsegv();
193 #endif