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)
18
#define NO_CPP_DEMANGLE
19
#define SIGSEGV_NO_AUTO_INIT
33
#ifndef NO_CPP_DEMANGLE
35
char * __cxa_demangle(const char * __mangled_name, char * __output_buffer, size_t * __length, int * __status);
42
# define SIGSEGV_STACK_IA64
43
# define REGFORMAT "%016lx"
44
#elif defined(REG_EIP)
45
# define SIGSEGV_STACK_X86
46
# define REGFORMAT "%08x"
48
# define SIGSEGV_STACK_GENERIC
49
# define REGFORMAT "%x"
52
static void signal_segv(int signum, siginfo_t* info, void*ptr) {
53
static const char *si_codes[3] = {"", "SEGV_MAPERR", "SEGV_ACCERR"};
56
ucontext_t *ucontext = (ucontext_t*)ptr;
58
#if defined(SIGSEGV_STACK_X86) || defined(SIGSEGV_STACK_IA64)
69
if (signum == SIGSEGV)
71
a2j_error("Segmentation Fault!");
73
else if (signum == SIGABRT)
77
else if (signum == SIGILL)
79
a2j_error("Illegal instruction!");
81
else if (signum == SIGFPE)
83
a2j_error("Floating point exception!");
87
a2j_error("Unknown bad signal catched!");
90
a2j_error("info.si_signo = %d", signum);
91
a2j_error("info.si_errno = %d", info->si_errno);
92
a2j_error("info.si_code = %d (%s)", info->si_code, si_codes[info->si_code]);
93
a2j_error("info.si_addr = %p", info->si_addr);
94
for(i = 0; i < NGREG; i++)
95
a2j_error("reg[%02d] = 0x" REGFORMAT, i, ucontext->uc_mcontext.gregs[i]);
97
#if defined(SIGSEGV_STACK_X86) || defined(SIGSEGV_STACK_IA64)
98
# if defined(SIGSEGV_STACK_IA64)
99
ip = (void*)ucontext->uc_mcontext.gregs[REG_RIP];
100
bp = (void**)ucontext->uc_mcontext.gregs[REG_RBP];
101
# elif defined(SIGSEGV_STACK_X86)
102
ip = (void*)ucontext->uc_mcontext.gregs[REG_EIP];
103
bp = (void**)ucontext->uc_mcontext.gregs[REG_EBP];
106
a2j_error("Stack trace:");
108
if(!dladdr(ip, &dlinfo))
111
const char *symname = dlinfo.dli_sname;
112
#ifndef NO_CPP_DEMANGLE
114
char *tmp = __cxa_demangle(symname, NULL, 0, &status);
116
if(status == 0 && tmp)
120
a2j_error("% 2d: %p <%s+%u> (%s)",
124
(unsigned)(ip - dlinfo.dli_saddr),
127
#ifndef NO_CPP_DEMANGLE
132
if(dlinfo.dli_sname && !strcmp(dlinfo.dli_sname, "main"))
139
a2j_error("Stack trace (non-dedicated):");
140
sz = backtrace(bt, 20);
141
strings = backtrace_symbols(bt, sz);
143
for(i = 0; i < sz; ++i)
144
a2j_error("%s", strings[i]);
146
a2j_error("End of stack trace");
150
int setup_sigsegv() {
151
struct sigaction action;
153
memset(&action, 0, sizeof(action));
154
action.sa_sigaction = signal_segv;
155
action.sa_flags = SA_SIGINFO;
156
if(sigaction(SIGSEGV, &action, NULL) < 0) {
157
a2j_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
161
if(sigaction(SIGILL, &action, NULL) < 0) {
162
a2j_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
166
if(sigaction(SIGABRT, &action, NULL) < 0) {
167
a2j_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
171
if(sigaction(SIGFPE, &action, NULL) < 0) {
172
a2j_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
179
#ifndef SIGSEGV_NO_AUTO_INIT
180
static void __attribute((constructor)) init(void) {