1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2005 - 2008 Jaco Kroon <jaco@kroon.co.za>
7 **************************************************************************
8 * This file contains code to print out a stack-trace when program segfaults.
9 **************************************************************************
11 * LADI Session Handler is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * LADI Session Handler is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with LADI Session Handler. If not, see <http://www.gnu.org/licenses/>
23 * or write to the Free Software Foundation, Inc.,
24 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
29 #define NO_CPP_DEMANGLE
30 #define SIGSEGV_NO_AUTO_INIT
44 #ifndef NO_CPP_DEMANGLE
46 char * __cxa_demangle(const char * __mangled_name
, char * __output_buffer
, size_t * __length
, int * __status
);
52 # define SIGSEGV_STACK_IA64
53 # define REGFORMAT "%016lx"
54 #elif defined(REG_EIP)
55 # define SIGSEGV_STACK_X86
56 # define REGFORMAT "%08x"
58 # define SIGSEGV_STACK_GENERIC
59 # define REGFORMAT "%x"
62 #if defined(__arm__) || defined(__powerpc__) || defined (__ia64__) || defined (__alpha__) || defined (__FreeBSD_kernel__) || defined (__sh__)
63 # define DISABLE_STACKTRACE
66 static void signal_segv(int signum
, siginfo_t
* info
, void*ptr
) {
67 static const char *si_codes
[3] = {"", "SEGV_MAPERR", "SEGV_ACCERR"};
69 #if !defined(DISABLE_STACKTRACE)
71 ucontext_t
*ucontext
= (ucontext_t
*)ptr
;
73 #if defined(SIGSEGV_STACK_X86) || defined(SIGSEGV_STACK_IA64)
85 if (signum
== SIGSEGV
)
87 log_error("Segmentation Fault!");
89 else if (signum
== SIGABRT
)
93 else if (signum
== SIGILL
)
95 log_error("Illegal instruction!");
97 else if (signum
== SIGFPE
)
99 log_error("Floating point exception!");
103 log_error("Unknown bad signal catched!");
106 log_error("info.si_signo = %d", signum
);
107 log_error("info.si_errno = %d", info
->si_errno
);
108 log_error("info.si_code = %d (%s)", info
->si_code
, si_codes
[info
->si_code
]);
109 log_error("info.si_addr = %p", info
->si_addr
);
110 #if defined(DISABLE_STACKTRACE)
111 log_error("No stack trace");
113 for(i
= 0; i
< NGREG
; i
++)
115 log_error("reg[%02d] = 0x" REGFORMAT
, (int)i
, ucontext
->uc_mcontext
.gregs
[i
]);
118 #if defined(SIGSEGV_STACK_X86) || defined(SIGSEGV_STACK_IA64)
119 # if defined(SIGSEGV_STACK_IA64)
120 ip
= (void*)ucontext
->uc_mcontext
.gregs
[REG_RIP
];
121 bp
= (void**)ucontext
->uc_mcontext
.gregs
[REG_RBP
];
122 # elif defined(SIGSEGV_STACK_X86)
123 ip
= (void*)ucontext
->uc_mcontext
.gregs
[REG_EIP
];
124 bp
= (void**)ucontext
->uc_mcontext
.gregs
[REG_EBP
];
127 log_error("Stack trace:");
129 if(!dladdr(ip
, &dlinfo
))
132 const char *symname
= dlinfo
.dli_sname
;
133 #ifndef NO_CPP_DEMANGLE
135 char *tmp
= __cxa_demangle(symname
, NULL
, 0, &status
);
137 if(status
== 0 && tmp
)
141 log_error("% 2d: %p <%s+%u> (%s)",
145 (unsigned)(ip
- dlinfo
.dli_saddr
),
148 #ifndef NO_CPP_DEMANGLE
153 if(dlinfo
.dli_sname
&& !strcmp(dlinfo
.dli_sname
, "main"))
160 log_error("Stack trace (non-dedicated):");
161 sz
= backtrace(bt
, 20);
162 strings
= backtrace_symbols(bt
, sz
);
164 for(i
= 0; i
< sz
; ++i
)
165 log_error("%s", strings
[i
]);
167 log_error("End of stack trace");
172 int setup_sigsegv() {
173 struct sigaction action
;
175 memset(&action
, 0, sizeof(action
));
176 action
.sa_sigaction
= signal_segv
;
177 action
.sa_flags
= SA_SIGINFO
;
178 if(sigaction(SIGSEGV
, &action
, NULL
) < 0) {
179 log_error("sigaction failed. errno is %d (%s)", errno
, strerror(errno
));
183 if(sigaction(SIGILL
, &action
, NULL
) < 0) {
184 log_error("sigaction failed. errno is %d (%s)", errno
, strerror(errno
));
188 if(sigaction(SIGABRT
, &action
, NULL
) < 0) {
189 log_error("sigaction failed. errno is %d (%s)", errno
, strerror(errno
));
193 if(sigaction(SIGFPE
, &action
, NULL
) < 0) {
194 log_error("sigaction failed. errno is %d (%s)", errno
, strerror(errno
));
201 #ifndef SIGSEGV_NO_AUTO_INIT
202 static void __attribute((constructor
)) init(void) {