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
32 #ifndef NO_CPP_DEMANGLE
33 char * __cxa_demangle(const char * __mangled_name
, char * __output_buffer
, size_t * __length
, int * __status
);
36 #include "JackError.h"
39 # define SIGSEGV_STACK_IA64
40 # define REGFORMAT "%016lx"
41 #elif defined(REG_EIP)
42 # define SIGSEGV_STACK_X86
43 # define REGFORMAT "%08x"
45 # define SIGSEGV_STACK_GENERIC
46 # define REGFORMAT "%x"
51 // TODO : does not compile yet on OSX
52 static void signal_segv(int signum
, siginfo_t
* info
, void*ptr
)
59 static void signal_segv(int signum
, siginfo_t
* info
, void*ptr
) {
60 static const char *si_codes
[3] = {"", "SEGV_MAPERR", "SEGV_ACCERR"};
63 ucontext_t
*ucontext
= (ucontext_t
*)ptr
;
65 #if defined(SIGSEGV_STACK_X86) || defined(SIGSEGV_STACK_IA64)
76 if (signum
== SIGSEGV
)
78 jack_error("Segmentation Fault!");
80 else if (signum
== SIGABRT
)
84 else if (signum
== SIGILL
)
86 jack_error("Illegal instruction!");
88 else if (signum
== SIGFPE
)
90 jack_error("Floating point exception!");
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
];
113 jack_error("Stack trace:");
115 if(!dladdr(ip
, &dlinfo
))
118 const char *symname
= dlinfo
.dli_sname
;
119 #ifndef NO_CPP_DEMANGLE
121 char *tmp
= __cxa_demangle(symname
, NULL
, 0, &status
);
123 if(status
== 0 && tmp
)
127 jack_error("% 2d: %p <%s+%u> (%s)",
131 (unsigned)(ip
- dlinfo
.dli_saddr
),
134 #ifndef NO_CPP_DEMANGLE
139 if(dlinfo
.dli_sname
&& !strcmp(dlinfo
.dli_sname
, "main"))
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
]);
153 jack_error("End of stack trace");
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
));
170 if(sigaction(SIGILL
, &action
, NULL
) < 0) {
171 jack_error("sigaction failed. errno is %d (%s)", errno
, strerror(errno
));
175 if(sigaction(SIGABRT
, &action
, NULL
) < 0) {
176 jack_error("sigaction failed. errno is %d (%s)", errno
, strerror(errno
));
180 if(sigaction(SIGFPE
, &action
, NULL
) < 0) {
181 jack_error("sigaction failed. errno is %d (%s)", errno
, strerror(errno
));
188 #ifndef SIGSEGV_NO_AUTO_INIT
189 static void __attribute((constructor
)) init(void) {