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 static void signal_segv(int signum
, siginfo_t
* info
, void*ptr
) {
63 static const char *si_codes
[3] = {"", "SEGV_MAPERR", "SEGV_ACCERR"};
66 ucontext_t
*ucontext
= (ucontext_t
*)ptr
;
68 #if defined(SIGSEGV_STACK_X86) || defined(SIGSEGV_STACK_IA64)
79 if (signum
== SIGSEGV
)
81 log_error("Segmentation Fault!");
83 else if (signum
== SIGABRT
)
87 else if (signum
== SIGILL
)
89 log_error("Illegal instruction!");
91 else if (signum
== SIGFPE
)
93 log_error("Floating point exception!");
97 log_error("Unknown bad signal catched!");
100 log_error("info.si_signo = %d", signum
);
101 log_error("info.si_errno = %d", info
->si_errno
);
102 log_error("info.si_code = %d (%s)", info
->si_code
, si_codes
[info
->si_code
]);
103 log_error("info.si_addr = %p", info
->si_addr
);
104 for(i
= 0; i
< NGREG
; i
++)
105 log_error("reg[%02d] = 0x" REGFORMAT
, i
, ucontext
->uc_mcontext
.gregs
[i
]);
107 #if defined(SIGSEGV_STACK_X86) || defined(SIGSEGV_STACK_IA64)
108 # if defined(SIGSEGV_STACK_IA64)
109 ip
= (void*)ucontext
->uc_mcontext
.gregs
[REG_RIP
];
110 bp
= (void**)ucontext
->uc_mcontext
.gregs
[REG_RBP
];
111 # elif defined(SIGSEGV_STACK_X86)
112 ip
= (void*)ucontext
->uc_mcontext
.gregs
[REG_EIP
];
113 bp
= (void**)ucontext
->uc_mcontext
.gregs
[REG_EBP
];
116 log_error("Stack trace:");
118 if(!dladdr(ip
, &dlinfo
))
121 const char *symname
= dlinfo
.dli_sname
;
122 #ifndef NO_CPP_DEMANGLE
124 char *tmp
= __cxa_demangle(symname
, NULL
, 0, &status
);
126 if(status
== 0 && tmp
)
130 log_error("% 2d: %p <%s+%u> (%s)",
134 (unsigned)(ip
- dlinfo
.dli_saddr
),
137 #ifndef NO_CPP_DEMANGLE
142 if(dlinfo
.dli_sname
&& !strcmp(dlinfo
.dli_sname
, "main"))
149 log_error("Stack trace (non-dedicated):");
150 sz
= backtrace(bt
, 20);
151 strings
= backtrace_symbols(bt
, sz
);
153 for(i
= 0; i
< sz
; ++i
)
154 log_error("%s", strings
[i
]);
156 log_error("End of stack trace");
160 int setup_sigsegv() {
161 struct sigaction action
;
163 memset(&action
, 0, sizeof(action
));
164 action
.sa_sigaction
= signal_segv
;
165 action
.sa_flags
= SA_SIGINFO
;
166 if(sigaction(SIGSEGV
, &action
, NULL
) < 0) {
167 log_error("sigaction failed. errno is %d (%s)", errno
, strerror(errno
));
171 if(sigaction(SIGILL
, &action
, NULL
) < 0) {
172 log_error("sigaction failed. errno is %d (%s)", errno
, strerror(errno
));
176 if(sigaction(SIGABRT
, &action
, NULL
) < 0) {
177 log_error("sigaction failed. errno is %d (%s)", errno
, strerror(errno
));
181 if(sigaction(SIGFPE
, &action
, NULL
) < 0) {
182 log_error("sigaction failed. errno is %d (%s)", errno
, strerror(errno
));
189 #ifndef SIGSEGV_NO_AUTO_INIT
190 static void __attribute((constructor
)) init(void) {