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
30 #ifdef HAVE_EXECINFO_H
31 # include <execinfo.h>
34 #ifndef NO_CPP_DEMANGLE
35 char * __cxa_demangle(const char * __mangled_name
, char * __output_buffer
, size_t * __length
, int * __status
);
38 #include "jack/control.h"
41 # define SIGSEGV_STACK_IA64
42 # define REGFORMAT "%016lx"
43 #elif defined(REG_EIP)
44 # define SIGSEGV_STACK_X86
45 # define REGFORMAT "%08x"
47 # define SIGSEGV_STACK_GENERIC
48 # define REGFORMAT "%x"
53 // TODO : does not compile yet on OSX
54 static void signal_segv(int signum
, siginfo_t
* info
, void*ptr
)
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)
70 #if defined(SIGSEGV_STACK_X86) || defined(SIGSEGV_STACK_IA64)
81 if (signum
== SIGSEGV
)
83 jack_error("Segmentation Fault!");
85 else if (signum
== SIGABRT
)
89 else if (signum
== SIGILL
)
91 jack_error("Illegal instruction!");
93 else if (signum
== SIGFPE
)
95 jack_error("Floating point exception!");
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
];
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
]
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
];
135 jack_error("Stack trace:");
137 if(!dladdr(ip
, &dlinfo
))
140 const char *symname
= dlinfo
.dli_sname
;
141 #ifndef NO_CPP_DEMANGLE
143 char *tmp
= __cxa_demangle(symname
, NULL
, 0, &status
);
145 if(status
== 0 && tmp
)
149 jack_error("% 2d: %p <%s+%u> (%s)",
153 (unsigned)(ip
- dlinfo
.dli_saddr
),
156 #ifndef NO_CPP_DEMANGLE
161 if(dlinfo
.dli_sname
&& !strcmp(dlinfo
.dli_sname
, "main"))
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
]);
176 jack_error("Stack trace not available");
179 jack_error("End of stack trace");
185 int setup_sigsegv() {
186 struct sigaction action
;
188 memset(&action
, 0, sizeof(action
));
189 action
.sa_sigaction
= signal_segv
;
191 action
.sa_flags
= SA_SIGINFO
;
193 if(sigaction(SIGSEGV
, &action
, NULL
) < 0) {
194 jack_error("sigaction failed. errno is %d (%s)", errno
, strerror(errno
));
198 if(sigaction(SIGILL
, &action
, NULL
) < 0) {
199 jack_error("sigaction failed. errno is %d (%s)", errno
, strerror(errno
));
203 if(sigaction(SIGABRT
, &action
, NULL
) < 0) {
204 jack_error("sigaction failed. errno is %d (%s)", errno
, strerror(errno
));
208 if(sigaction(SIGFPE
, &action
, NULL
) < 0) {
209 jack_error("sigaction failed. errno is %d (%s)", errno
, strerror(errno
));
216 #ifndef SIGSEGV_NO_AUTO_INIT
217 static void __attribute((constructor
)) init(void) {