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 "jack/control.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 const char *si_code_str
;
64 ucontext_t
*ucontext
= (ucontext_t
*)ptr
;
66 #if defined(SIGSEGV_STACK_X86) || defined(SIGSEGV_STACK_IA64)
77 if (signum
== SIGSEGV
)
79 jack_error("Segmentation Fault!");
81 else if (signum
== SIGABRT
)
85 else if (signum
== SIGILL
)
87 jack_error("Illegal instruction!");
89 else if (signum
== SIGFPE
)
91 jack_error("Floating point exception!");
95 jack_error("Unknown bad signal catched!");
98 if (info
->si_code
>= 0 && info
->si_code
< 3)
99 si_code_str
= si_codes
[info
->si_code
];
101 si_code_str
= "unknown";
103 jack_error("info.si_signo = %d", signum
);
104 jack_error("info.si_errno = %d", info
->si_errno
);
105 jack_error("info.si_code = %d (%s)", info
->si_code
, si_code_str
);
106 jack_error("info.si_addr = %p", info
->si_addr
);
107 #if !defined(__alpha__) && !defined(__ia64__) && !defined(__FreeBSD_kernel__) && !defined(__arm__) && !defined(__hppa__) && !defined(__sh__)
108 for(i
= 0; i
< NGREG
; i
++)
109 jack_error("reg[%02d] = 0x" REGFORMAT
, i
,
110 #if defined(__powerpc__)
111 ucontext
->uc_mcontext
.uc_regs
[i
]
112 #elif defined(__sparc__) && defined(__arch64__)
113 ucontext
->uc_mcontext
.mc_gregs
[i
]
115 ucontext
->uc_mcontext
.gregs
[i
]
118 #endif /* alpha, ia64, kFreeBSD, arm, hppa */
120 #if defined(SIGSEGV_STACK_X86) || defined(SIGSEGV_STACK_IA64)
121 # if defined(SIGSEGV_STACK_IA64)
122 ip
= (void*)ucontext
->uc_mcontext
.gregs
[REG_RIP
];
123 bp
= (void**)ucontext
->uc_mcontext
.gregs
[REG_RBP
];
124 # elif defined(SIGSEGV_STACK_X86)
125 ip
= (void*)ucontext
->uc_mcontext
.gregs
[REG_EIP
];
126 bp
= (void**)ucontext
->uc_mcontext
.gregs
[REG_EBP
];
129 jack_error("Stack trace:");
131 if(!dladdr(ip
, &dlinfo
))
134 const char *symname
= dlinfo
.dli_sname
;
135 #ifndef NO_CPP_DEMANGLE
137 char *tmp
= __cxa_demangle(symname
, NULL
, 0, &status
);
139 if(status
== 0 && tmp
)
143 jack_error("% 2d: %p <%s+%u> (%s)",
147 (unsigned)(ip
- dlinfo
.dli_saddr
),
150 #ifndef NO_CPP_DEMANGLE
155 if(dlinfo
.dli_sname
&& !strcmp(dlinfo
.dli_sname
, "main"))
162 jack_error("Stack trace (non-dedicated):");
163 sz
= backtrace(bt
, 20);
164 strings
= backtrace_symbols(bt
, sz
);
166 for(i
= 0; i
< sz
; ++i
)
167 jack_error("%s", strings
[i
]);
169 jack_error("End of stack trace");
175 int setup_sigsegv() {
176 struct sigaction action
;
178 memset(&action
, 0, sizeof(action
));
179 action
.sa_sigaction
= signal_segv
;
180 action
.sa_flags
= SA_SIGINFO
;
181 if(sigaction(SIGSEGV
, &action
, NULL
) < 0) {
182 jack_error("sigaction failed. errno is %d (%s)", errno
, strerror(errno
));
186 if(sigaction(SIGILL
, &action
, NULL
) < 0) {
187 jack_error("sigaction failed. errno is %d (%s)", errno
, strerror(errno
));
191 if(sigaction(SIGABRT
, &action
, NULL
) < 0) {
192 jack_error("sigaction failed. errno is %d (%s)", errno
, strerror(errno
));
196 if(sigaction(SIGFPE
, &action
, NULL
) < 0) {
197 jack_error("sigaction failed. errno is %d (%s)", errno
, strerror(errno
));
204 #ifndef SIGSEGV_NO_AUTO_INIT
205 static void __attribute((constructor
)) init(void) {