Merge branch 'master' into port_register_notification_defer
[jack2.git] / dbus / sigsegv.c
blob26bc204f9898e8d65938f974c8c08444a675dd70
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 "JackError.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 ucontext_t *ucontext = (ucontext_t*)ptr;
65 #if defined(SIGSEGV_STACK_X86) || defined(SIGSEGV_STACK_IA64)
66 int f = 0;
67 Dl_info dlinfo;
68 void **bp = 0;
69 void *ip = 0;
70 #else
71 void *bt[20];
72 char **strings;
73 size_t sz;
74 #endif
76 if (signum == SIGSEGV)
78 jack_error("Segmentation Fault!");
80 else if (signum == SIGABRT)
82 jack_error("Abort!");
84 else if (signum == SIGILL)
86 jack_error("Illegal instruction!");
88 else if (signum == SIGFPE)
90 jack_error("Floating point exception!");
92 else
94 jack_error("Unknown bad signal catched!");
97 jack_error("info.si_signo = %d", signum);
98 jack_error("info.si_errno = %d", info->si_errno);
99 jack_error("info.si_code = %d (%s)", info->si_code, si_codes[info->si_code]);
100 jack_error("info.si_addr = %p", info->si_addr);
101 for(i = 0; i < NGREG; i++)
102 jack_error("reg[%02d] = 0x" REGFORMAT, i, ucontext->uc_mcontext.gregs[i]);
104 #if defined(SIGSEGV_STACK_X86) || defined(SIGSEGV_STACK_IA64)
105 # if defined(SIGSEGV_STACK_IA64)
106 ip = (void*)ucontext->uc_mcontext.gregs[REG_RIP];
107 bp = (void**)ucontext->uc_mcontext.gregs[REG_RBP];
108 # elif defined(SIGSEGV_STACK_X86)
109 ip = (void*)ucontext->uc_mcontext.gregs[REG_EIP];
110 bp = (void**)ucontext->uc_mcontext.gregs[REG_EBP];
111 # endif
113 jack_error("Stack trace:");
114 while(bp && ip) {
115 if(!dladdr(ip, &dlinfo))
116 break;
118 const char *symname = dlinfo.dli_sname;
119 #ifndef NO_CPP_DEMANGLE
120 int status;
121 char *tmp = __cxa_demangle(symname, NULL, 0, &status);
123 if(status == 0 && tmp)
124 symname = tmp;
125 #endif
127 jack_error("% 2d: %p <%s+%u> (%s)",
128 ++f,
130 symname,
131 (unsigned)(ip - dlinfo.dli_saddr),
132 dlinfo.dli_fname);
134 #ifndef NO_CPP_DEMANGLE
135 if(tmp)
136 free(tmp);
137 #endif
139 if(dlinfo.dli_sname && !strcmp(dlinfo.dli_sname, "main"))
140 break;
142 ip = bp[1];
143 bp = (void**)bp[0];
145 #else
146 jack_error("Stack trace (non-dedicated):");
147 sz = backtrace(bt, 20);
148 strings = backtrace_symbols(bt, sz);
150 for(i = 0; i < sz; ++i)
151 jack_error("%s", strings[i]);
152 #endif
153 jack_error("End of stack trace");
154 exit (-1);
157 #endif
159 int setup_sigsegv() {
160 struct sigaction action;
162 memset(&action, 0, sizeof(action));
163 action.sa_sigaction = signal_segv;
164 action.sa_flags = SA_SIGINFO;
165 if(sigaction(SIGSEGV, &action, NULL) < 0) {
166 jack_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
167 return 0;
170 if(sigaction(SIGILL, &action, NULL) < 0) {
171 jack_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
172 return 0;
175 if(sigaction(SIGABRT, &action, NULL) < 0) {
176 jack_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
177 return 0;
180 if(sigaction(SIGFPE, &action, NULL) < 0) {
181 jack_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
182 return 0;
185 return 1;
188 #ifndef SIGSEGV_NO_AUTO_INIT
189 static void __attribute((constructor)) init(void) {
190 setup_sigsegv();
192 #endif