Correct Changelog.
[jack2.git] / dbus / sigsegv.c
blobd41091c952864ebc64ea6379b22423d111899f62
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(__powerpc64__)
111 ucontext->uc_mcontext.gp_regs[i]
112 #elif defined(__powerpc__)
113 ucontext->uc_mcontext.uc_regs[i]
114 #elif defined(__sparc__) && defined(__arch64__)
115 ucontext->uc_mcontext.mc_gregs[i]
116 #else
117 ucontext->uc_mcontext.gregs[i]
118 #endif
120 #endif /* alpha, ia64, kFreeBSD, arm, hppa */
122 #if defined(SIGSEGV_STACK_X86) || defined(SIGSEGV_STACK_IA64)
123 # if defined(SIGSEGV_STACK_IA64)
124 ip = (void*)ucontext->uc_mcontext.gregs[REG_RIP];
125 bp = (void**)ucontext->uc_mcontext.gregs[REG_RBP];
126 # elif defined(SIGSEGV_STACK_X86)
127 ip = (void*)ucontext->uc_mcontext.gregs[REG_EIP];
128 bp = (void**)ucontext->uc_mcontext.gregs[REG_EBP];
129 # endif
131 jack_error("Stack trace:");
132 while(bp && ip) {
133 if(!dladdr(ip, &dlinfo))
134 break;
136 const char *symname = dlinfo.dli_sname;
137 #ifndef NO_CPP_DEMANGLE
138 int status;
139 char *tmp = __cxa_demangle(symname, NULL, 0, &status);
141 if(status == 0 && tmp)
142 symname = tmp;
143 #endif
145 jack_error("% 2d: %p <%s+%u> (%s)",
146 ++f,
148 symname,
149 (unsigned)(ip - dlinfo.dli_saddr),
150 dlinfo.dli_fname);
152 #ifndef NO_CPP_DEMANGLE
153 if(tmp)
154 free(tmp);
155 #endif
157 if(dlinfo.dli_sname && !strcmp(dlinfo.dli_sname, "main"))
158 break;
160 ip = bp[1];
161 bp = (void**)bp[0];
163 #else
164 jack_error("Stack trace (non-dedicated):");
165 sz = backtrace(bt, 20);
166 strings = backtrace_symbols(bt, sz);
168 for(i = 0; i < sz; ++i)
169 jack_error("%s", strings[i]);
170 #endif
171 jack_error("End of stack trace");
172 exit (-1);
175 #endif
177 int setup_sigsegv() {
178 struct sigaction action;
180 memset(&action, 0, sizeof(action));
181 action.sa_sigaction = signal_segv;
182 #ifdef SA_SIGINFO
183 action.sa_flags = SA_SIGINFO;
184 #endif
185 if(sigaction(SIGSEGV, &action, NULL) < 0) {
186 jack_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
187 return 0;
190 if(sigaction(SIGILL, &action, NULL) < 0) {
191 jack_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
192 return 0;
195 if(sigaction(SIGABRT, &action, NULL) < 0) {
196 jack_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
197 return 0;
200 if(sigaction(SIGFPE, &action, NULL) < 0) {
201 jack_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
202 return 0;
205 return 1;
208 #ifndef SIGSEGV_NO_AUTO_INIT
209 static void __attribute((constructor)) init(void) {
210 setup_sigsegv();
212 #endif