jackdbus: Fix sigsegv handling segfaulting by itself
[jack2.git] / dbus / sigsegv.c
blob8a87dc8193cd2bb57b4682b24494c4773b5f92c4
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 <dlfcn.h>
30 #include <execinfo.h>
31 #include <errno.h>
32 #ifndef NO_CPP_DEMANGLE
33 char * __cxa_demangle(const char * __mangled_name, char * __output_buffer, size_t * __length, int * __status);
34 #endif
36 #include "jack/control.h"
38 #if defined(REG_RIP)
39 # define SIGSEGV_STACK_IA64
40 # define REGFORMAT "%016lx"
41 #elif defined(REG_EIP)
42 # define SIGSEGV_STACK_X86
43 # define REGFORMAT "%08x"
44 #else
45 # define SIGSEGV_STACK_GENERIC
46 # define REGFORMAT "%x"
47 #endif
49 #ifdef __APPLE__
51 // TODO : does not compile yet on OSX
52 static void signal_segv(int signum, siginfo_t* info, void*ptr)
55 #else
57 #include <ucontext.h>
59 static void signal_segv(int signum, siginfo_t* info, void*ptr) {
60 static const char *si_codes[3] = {"", "SEGV_MAPERR", "SEGV_ACCERR"};
62 size_t i;
63 const char *si_code_str;
64 ucontext_t *ucontext = (ucontext_t*)ptr;
66 #if defined(SIGSEGV_STACK_X86) || defined(SIGSEGV_STACK_IA64)
67 int f = 0;
68 Dl_info dlinfo;
69 void **bp = 0;
70 void *ip = 0;
71 #else
72 void *bt[20];
73 char **strings;
74 size_t sz;
75 #endif
77 if (signum == SIGSEGV)
79 jack_error("Segmentation Fault!");
81 else if (signum == SIGABRT)
83 jack_error("Abort!");
85 else if (signum == SIGILL)
87 jack_error("Illegal instruction!");
89 else if (signum == SIGFPE)
91 jack_error("Floating point exception!");
93 else
95 jack_error("Unknown bad signal catched!");
98 if (info->si_code >= 0 && info->si_code < 3)
99 si_code_str = si_codes[info->si_code];
100 else
101 si_code_str = "unknown";
103 jack_error("info.si_signo = %d", signum);
104 jack_error("info.si_errno = %d", info->si_errno);
105 jack_error("info.si_code = %d (%s)", info->si_code, si_code_str);
106 jack_error("info.si_addr = %p", info->si_addr);
107 #if !defined(__alpha__) && !defined(__ia64__) && !defined(__FreeBSD_kernel__) && !defined(__arm__) && !defined(__hppa__) && !defined(__sh__)
108 for(i = 0; i < NGREG; i++)
109 jack_error("reg[%02d] = 0x" REGFORMAT, i,
110 #if defined(__powerpc__)
111 ucontext->uc_mcontext.uc_regs[i]
112 #elif defined(__sparc__) && defined(__arch64__)
113 ucontext->uc_mcontext.mc_gregs[i]
114 #else
115 ucontext->uc_mcontext.gregs[i]
116 #endif
118 #endif /* alpha, ia64, kFreeBSD, arm, hppa */
120 #if defined(SIGSEGV_STACK_X86) || defined(SIGSEGV_STACK_IA64)
121 # if defined(SIGSEGV_STACK_IA64)
122 ip = (void*)ucontext->uc_mcontext.gregs[REG_RIP];
123 bp = (void**)ucontext->uc_mcontext.gregs[REG_RBP];
124 # elif defined(SIGSEGV_STACK_X86)
125 ip = (void*)ucontext->uc_mcontext.gregs[REG_EIP];
126 bp = (void**)ucontext->uc_mcontext.gregs[REG_EBP];
127 # endif
129 jack_error("Stack trace:");
130 while(bp && ip) {
131 if(!dladdr(ip, &dlinfo))
132 break;
134 const char *symname = dlinfo.dli_sname;
135 #ifndef NO_CPP_DEMANGLE
136 int status;
137 char *tmp = __cxa_demangle(symname, NULL, 0, &status);
139 if(status == 0 && tmp)
140 symname = tmp;
141 #endif
143 jack_error("% 2d: %p <%s+%u> (%s)",
144 ++f,
146 symname,
147 (unsigned)(ip - dlinfo.dli_saddr),
148 dlinfo.dli_fname);
150 #ifndef NO_CPP_DEMANGLE
151 if(tmp)
152 free(tmp);
153 #endif
155 if(dlinfo.dli_sname && !strcmp(dlinfo.dli_sname, "main"))
156 break;
158 ip = bp[1];
159 bp = (void**)bp[0];
161 #else
162 jack_error("Stack trace (non-dedicated):");
163 sz = backtrace(bt, 20);
164 strings = backtrace_symbols(bt, sz);
166 for(i = 0; i < sz; ++i)
167 jack_error("%s", strings[i]);
168 #endif
169 jack_error("End of stack trace");
170 exit (-1);
173 #endif
175 int setup_sigsegv() {
176 struct sigaction action;
178 memset(&action, 0, sizeof(action));
179 action.sa_sigaction = signal_segv;
180 action.sa_flags = SA_SIGINFO;
181 if(sigaction(SIGSEGV, &action, NULL) < 0) {
182 jack_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
183 return 0;
186 if(sigaction(SIGILL, &action, NULL) < 0) {
187 jack_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
188 return 0;
191 if(sigaction(SIGABRT, &action, NULL) < 0) {
192 jack_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
193 return 0;
196 if(sigaction(SIGFPE, &action, NULL) < 0) {
197 jack_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
198 return 0;
201 return 1;
204 #ifndef SIGSEGV_NO_AUTO_INIT
205 static void __attribute((constructor)) init(void) {
206 setup_sigsegv();
208 #endif