switch from bld.new_task_gen() to bld()
[jack2.git] / dbus / sigsegv.c
blob03b1183b2435718ca002d5881c71f5fffb46ab41
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 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 #if !defined(__alpha__) && !defined(__ia64__) && !defined(__FreeBSD_kernel__) && !defined(__arm__) && !defined(__hppa__) && !defined(__sh__)
102 for(i = 0; i < NGREG; i++)
103 jack_error("reg[%02d] = 0x" REGFORMAT, i,
104 #if defined(__powerpc__)
105 ucontext->uc_mcontext.uc_regs[i]
106 #elif defined(__sparc__) && defined(__arch64__)
107 ucontext->uc_mcontext.mc_gregs[i]
108 #else
109 ucontext->uc_mcontext.gregs[i]
110 #endif
112 #endif /* alpha, ia64, kFreeBSD, arm, hppa */
114 #if defined(SIGSEGV_STACK_X86) || defined(SIGSEGV_STACK_IA64)
115 # if defined(SIGSEGV_STACK_IA64)
116 ip = (void*)ucontext->uc_mcontext.gregs[REG_RIP];
117 bp = (void**)ucontext->uc_mcontext.gregs[REG_RBP];
118 # elif defined(SIGSEGV_STACK_X86)
119 ip = (void*)ucontext->uc_mcontext.gregs[REG_EIP];
120 bp = (void**)ucontext->uc_mcontext.gregs[REG_EBP];
121 # endif
123 jack_error("Stack trace:");
124 while(bp && ip) {
125 if(!dladdr(ip, &dlinfo))
126 break;
128 const char *symname = dlinfo.dli_sname;
129 #ifndef NO_CPP_DEMANGLE
130 int status;
131 char *tmp = __cxa_demangle(symname, NULL, 0, &status);
133 if(status == 0 && tmp)
134 symname = tmp;
135 #endif
137 jack_error("% 2d: %p <%s+%u> (%s)",
138 ++f,
140 symname,
141 (unsigned)(ip - dlinfo.dli_saddr),
142 dlinfo.dli_fname);
144 #ifndef NO_CPP_DEMANGLE
145 if(tmp)
146 free(tmp);
147 #endif
149 if(dlinfo.dli_sname && !strcmp(dlinfo.dli_sname, "main"))
150 break;
152 ip = bp[1];
153 bp = (void**)bp[0];
155 #else
156 jack_error("Stack trace (non-dedicated):");
157 sz = backtrace(bt, 20);
158 strings = backtrace_symbols(bt, sz);
160 for(i = 0; i < sz; ++i)
161 jack_error("%s", strings[i]);
162 #endif
163 jack_error("End of stack trace");
164 exit (-1);
167 #endif
169 int setup_sigsegv() {
170 struct sigaction action;
172 memset(&action, 0, sizeof(action));
173 action.sa_sigaction = signal_segv;
174 action.sa_flags = SA_SIGINFO;
175 if(sigaction(SIGSEGV, &action, NULL) < 0) {
176 jack_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
177 return 0;
180 if(sigaction(SIGILL, &action, NULL) < 0) {
181 jack_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
182 return 0;
185 if(sigaction(SIGABRT, &action, NULL) < 0) {
186 jack_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
187 return 0;
190 if(sigaction(SIGFPE, &action, NULL) < 0) {
191 jack_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
192 return 0;
195 return 1;
198 #ifndef SIGSEGV_NO_AUTO_INIT
199 static void __attribute((constructor)) init(void) {
200 setup_sigsegv();
202 #endif