fix compiler warning
[ladish.git] / daemon / sigsegv.c
blobb0a2d0fe1fb50d8f6597679608cf50c7e9f9aa9e
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
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.
27 #include "config.h"
29 #define NO_CPP_DEMANGLE
30 #define SIGSEGV_NO_AUTO_INIT
32 #ifndef _GNU_SOURCE
33 # define _GNU_SOURCE
34 #endif
36 #include <memory.h>
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <signal.h>
40 #include <ucontext.h>
41 #include <dlfcn.h>
42 #include <execinfo.h>
43 #include <errno.h>
44 #ifndef NO_CPP_DEMANGLE
45 //#include <cxxabi.h>
46 char * __cxa_demangle(const char * __mangled_name, char * __output_buffer, size_t * __length, int * __status);
47 #endif
49 #include "../log.h"
51 #if defined(REG_RIP)
52 # define SIGSEGV_STACK_IA64
53 # define REGFORMAT "%016lx"
54 #elif defined(REG_EIP)
55 # define SIGSEGV_STACK_X86
56 # define REGFORMAT "%08x"
57 #else
58 # define SIGSEGV_STACK_GENERIC
59 # define REGFORMAT "%x"
60 #endif
62 static void signal_segv(int signum, siginfo_t* info, void*ptr) {
63 static const char *si_codes[3] = {"", "SEGV_MAPERR", "SEGV_ACCERR"};
65 size_t i;
66 ucontext_t *ucontext = (ucontext_t*)ptr;
68 #if defined(SIGSEGV_STACK_X86) || defined(SIGSEGV_STACK_IA64)
69 int f = 0;
70 Dl_info dlinfo;
71 void **bp = 0;
72 void *ip = 0;
73 #else
74 void *bt[20];
75 char **strings;
76 size_t sz;
77 #endif
79 if (signum == SIGSEGV)
81 log_error("Segmentation Fault!");
83 else if (signum == SIGABRT)
85 log_error("Abort!");
87 else if (signum == SIGILL)
89 log_error("Illegal instruction!");
91 else if (signum == SIGFPE)
93 log_error("Floating point exception!");
95 else
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 #if defined(__arm__) || defined(__powerpc__) || defined (__ia64__) || defined (__alpha__) || defined (__FreeBSD_kernel__) || defined (__sh__)
105 log_error("No stack trace");
106 #else
107 for(i = 0; i < NGREG; i++)
108 log_error("reg[%02d] = 0x" REGFORMAT, i, ucontext->uc_mcontext.gregs[i]);
110 #if defined(SIGSEGV_STACK_X86) || defined(SIGSEGV_STACK_IA64)
111 # if defined(SIGSEGV_STACK_IA64)
112 ip = (void*)ucontext->uc_mcontext.gregs[REG_RIP];
113 bp = (void**)ucontext->uc_mcontext.gregs[REG_RBP];
114 # elif defined(SIGSEGV_STACK_X86)
115 ip = (void*)ucontext->uc_mcontext.gregs[REG_EIP];
116 bp = (void**)ucontext->uc_mcontext.gregs[REG_EBP];
117 # endif
119 log_error("Stack trace:");
120 while(bp && ip) {
121 if(!dladdr(ip, &dlinfo))
122 break;
124 const char *symname = dlinfo.dli_sname;
125 #ifndef NO_CPP_DEMANGLE
126 int status;
127 char *tmp = __cxa_demangle(symname, NULL, 0, &status);
129 if(status == 0 && tmp)
130 symname = tmp;
131 #endif
133 log_error("% 2d: %p <%s+%u> (%s)",
134 ++f,
136 symname,
137 (unsigned)(ip - dlinfo.dli_saddr),
138 dlinfo.dli_fname);
140 #ifndef NO_CPP_DEMANGLE
141 if(tmp)
142 free(tmp);
143 #endif
145 if(dlinfo.dli_sname && !strcmp(dlinfo.dli_sname, "main"))
146 break;
148 ip = bp[1];
149 bp = (void**)bp[0];
151 #else
152 log_error("Stack trace (non-dedicated):");
153 sz = backtrace(bt, 20);
154 strings = backtrace_symbols(bt, sz);
156 for(i = 0; i < sz; ++i)
157 log_error("%s", strings[i]);
158 #endif
159 log_error("End of stack trace");
160 #endif
161 exit (-1);
164 int setup_sigsegv() {
165 struct sigaction action;
167 memset(&action, 0, sizeof(action));
168 action.sa_sigaction = signal_segv;
169 action.sa_flags = SA_SIGINFO;
170 if(sigaction(SIGSEGV, &action, NULL) < 0) {
171 log_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
172 return 0;
175 if(sigaction(SIGILL, &action, NULL) < 0) {
176 log_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
177 return 0;
180 if(sigaction(SIGABRT, &action, NULL) < 0) {
181 log_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
182 return 0;
185 if(sigaction(SIGFPE, &action, NULL) < 0) {
186 log_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
187 return 0;
190 return 1;
193 #ifndef SIGSEGV_NO_AUTO_INIT
194 static void __attribute((constructor)) init(void) {
195 setup_sigsegv();
197 #endif