port: Make PORT_TYPES_MAX constant
[jack2.git] / dbus / sigsegv.c
blob10ddd0ac6386528f8086cd826e779db8140e7546
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 #ifdef HAVE_EXECINFO_H
31 # include <execinfo.h>
32 #endif
33 #include <errno.h>
34 #ifndef NO_CPP_DEMANGLE
35 char * __cxa_demangle(const char * __mangled_name, char * __output_buffer, size_t * __length, int * __status);
36 #endif
38 #include "jack/control.h"
40 #if defined(REG_RIP)
41 # define SIGSEGV_STACK_IA64
42 # define REGFORMAT "%016lx"
43 #elif defined(REG_EIP)
44 # define SIGSEGV_STACK_X86
45 # define REGFORMAT "%08x"
46 #else
47 # define SIGSEGV_STACK_GENERIC
48 # define REGFORMAT "%x"
49 #endif
51 #ifdef __APPLE__
53 // TODO : does not compile yet on OSX
54 static void signal_segv(int signum, siginfo_t* info, void*ptr)
57 #else
59 #include <ucontext.h>
61 static void signal_segv(int signum, siginfo_t* info, void*ptr) {
62 static const char *si_codes[3] = {"", "SEGV_MAPERR", "SEGV_ACCERR"};
64 const char *si_code_str;
65 ucontext_t *ucontext = (ucontext_t*)ptr;
67 #if (defined(HAVE_UCONTEXT) && defined(HAVE_NGREG)) || defined(HAVE_EXECINFO_H)
68 size_t i;
69 #endif
70 #if defined(SIGSEGV_STACK_X86) || defined(SIGSEGV_STACK_IA64)
71 int f = 0;
72 Dl_info dlinfo;
73 void **bp = 0;
74 void *ip = 0;
75 #else
76 void *bt[20];
77 char **strings;
78 size_t sz;
79 #endif
81 if (signum == SIGSEGV)
83 jack_error("Segmentation Fault!");
85 else if (signum == SIGABRT)
87 jack_error("Abort!");
89 else if (signum == SIGILL)
91 jack_error("Illegal instruction!");
93 else if (signum == SIGFPE)
95 jack_error("Floating point exception!");
97 else
99 jack_error("Unknown bad signal caught!");
102 if (info->si_code >= 0 && info->si_code < 3)
103 si_code_str = si_codes[info->si_code];
104 else
105 si_code_str = "unknown";
107 jack_error("info.si_signo = %d", signum);
108 jack_error("info.si_errno = %d", info->si_errno);
109 jack_error("info.si_code = %d (%s)", info->si_code, si_code_str);
110 jack_error("info.si_addr = %p", info->si_addr);
111 #if defined(HAVE_UCONTEXT) && defined(HAVE_NGREG)
112 for(i = 0; i < NGREG; i++)
113 jack_error("reg[%02d] = 0x" REGFORMAT, i,
114 #if defined(HAVE_UCONTEXT_GP_REGS)
115 ucontext->uc_mcontext.gp_regs[i]
116 #elif defined(HAVE_UCONTEXT_UC_REGS)
117 ucontext->uc_mcontext.uc_regs[i]
118 #elif defined(HAVE_UCONTEXT_MC_GREGS)
119 ucontext->uc_mcontext.mc_gregs[i]
120 #elif defined(HAVE_UCONTEXT_GREGS)
121 ucontext->uc_mcontext.gregs[i]
122 #endif
124 #endif /* defined(HAVE_UCONTEXT) && defined(HAVE_NGREG) */
126 #if defined(SIGSEGV_STACK_X86) || defined(SIGSEGV_STACK_IA64)
127 # if defined(SIGSEGV_STACK_IA64)
128 ip = (void*)ucontext->uc_mcontext.gregs[REG_RIP];
129 bp = (void**)ucontext->uc_mcontext.gregs[REG_RBP];
130 # elif defined(SIGSEGV_STACK_X86)
131 ip = (void*)ucontext->uc_mcontext.gregs[REG_EIP];
132 bp = (void**)ucontext->uc_mcontext.gregs[REG_EBP];
133 # endif
135 jack_error("Stack trace:");
136 while(bp && ip) {
137 if(!dladdr(ip, &dlinfo))
138 break;
140 const char *symname = dlinfo.dli_sname;
141 #ifndef NO_CPP_DEMANGLE
142 int status;
143 char *tmp = __cxa_demangle(symname, NULL, 0, &status);
145 if(status == 0 && tmp)
146 symname = tmp;
147 #endif
149 jack_error("% 2d: %p <%s+%u> (%s)",
150 ++f,
152 symname,
153 (unsigned)(ip - dlinfo.dli_saddr),
154 dlinfo.dli_fname);
156 #ifndef NO_CPP_DEMANGLE
157 if(tmp)
158 free(tmp);
159 #endif
161 if(dlinfo.dli_sname && !strcmp(dlinfo.dli_sname, "main"))
162 break;
164 ip = bp[1];
165 bp = (void**)bp[0];
167 #else
168 # ifdef HAVE_EXECINFO_H
169 jack_error("Stack trace (non-dedicated):");
170 sz = backtrace(bt, 20);
171 strings = backtrace_symbols(bt, sz);
173 for(i = 0; i < sz; ++i)
174 jack_error("%s", strings[i]);
175 # else
176 jack_error("Stack trace not available");
177 # endif
178 #endif
179 jack_error("End of stack trace");
180 exit (-1);
183 #endif
185 int setup_sigsegv() {
186 struct sigaction action;
188 memset(&action, 0, sizeof(action));
189 action.sa_sigaction = signal_segv;
190 #ifdef SA_SIGINFO
191 action.sa_flags = SA_SIGINFO;
192 #endif
193 if(sigaction(SIGSEGV, &action, NULL) < 0) {
194 jack_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
195 return 0;
198 if(sigaction(SIGILL, &action, NULL) < 0) {
199 jack_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
200 return 0;
203 if(sigaction(SIGABRT, &action, NULL) < 0) {
204 jack_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
205 return 0;
208 if(sigaction(SIGFPE, &action, NULL) < 0) {
209 jack_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
210 return 0;
213 return 1;
216 #ifndef SIGSEGV_NO_AUTO_INIT
217 static void __attribute((constructor)) init(void) {
218 setup_sigsegv();
220 #endif