Fix warning (try again)
[ladish.git] / daemon / sigsegv.c
blobacb176f491a8af09899c75f3fae2c3a9da01655a
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 #if defined(__arm__) || defined(__powerpc__) || defined (__ia64__) || defined (__alpha__) || defined (__FreeBSD_kernel__) || defined (__sh__)
63 # define DISABLE_STACKTRACE
64 #endif
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)
70 size_t i;
71 ucontext_t *ucontext = (ucontext_t*)ptr;
73 #if defined(SIGSEGV_STACK_X86) || defined(SIGSEGV_STACK_IA64)
74 int f = 0;
75 Dl_info dlinfo;
76 void **bp = 0;
77 void *ip = 0;
78 #else
79 void *bt[20];
80 char **strings;
81 size_t sz;
82 #endif
83 #endif
85 if (signum == SIGSEGV)
87 log_error("Segmentation Fault!");
89 else if (signum == SIGABRT)
91 log_error("Abort!");
93 else if (signum == SIGILL)
95 log_error("Illegal instruction!");
97 else if (signum == SIGFPE)
99 log_error("Floating point exception!");
101 else
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");
112 #else
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];
125 # endif
127 log_error("Stack trace:");
128 while(bp && ip) {
129 if(!dladdr(ip, &dlinfo))
130 break;
132 const char *symname = dlinfo.dli_sname;
133 #ifndef NO_CPP_DEMANGLE
134 int status;
135 char *tmp = __cxa_demangle(symname, NULL, 0, &status);
137 if(status == 0 && tmp)
138 symname = tmp;
139 #endif
141 log_error("% 2d: %p <%s+%u> (%s)",
142 ++f,
144 symname,
145 (unsigned)(ip - dlinfo.dli_saddr),
146 dlinfo.dli_fname);
148 #ifndef NO_CPP_DEMANGLE
149 if(tmp)
150 free(tmp);
151 #endif
153 if(dlinfo.dli_sname && !strcmp(dlinfo.dli_sname, "main"))
154 break;
156 ip = bp[1];
157 bp = (void**)bp[0];
159 #else
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]);
166 #endif
167 log_error("End of stack trace");
168 #endif
169 exit (-1);
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));
180 return 0;
183 if(sigaction(SIGILL, &action, NULL) < 0) {
184 log_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
185 return 0;
188 if(sigaction(SIGABRT, &action, NULL) < 0) {
189 log_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
190 return 0;
193 if(sigaction(SIGFPE, &action, NULL) < 0) {
194 log_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
195 return 0;
198 return 1;
201 #ifndef SIGSEGV_NO_AUTO_INIT
202 static void __attribute((constructor)) init(void) {
203 setup_sigsegv();
205 #endif