daemon: improve log
[ladish.git] / daemon / sigsegv.c
bloba5b4cf4054e3246b8fc6016dc864e1fc43a6f358
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 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];
114 # endif
116 log_error("Stack trace:");
117 while(bp && ip) {
118 if(!dladdr(ip, &dlinfo))
119 break;
121 const char *symname = dlinfo.dli_sname;
122 #ifndef NO_CPP_DEMANGLE
123 int status;
124 char *tmp = __cxa_demangle(symname, NULL, 0, &status);
126 if(status == 0 && tmp)
127 symname = tmp;
128 #endif
130 log_error("% 2d: %p <%s+%u> (%s)",
131 ++f,
133 symname,
134 (unsigned)(ip - dlinfo.dli_saddr),
135 dlinfo.dli_fname);
137 #ifndef NO_CPP_DEMANGLE
138 if(tmp)
139 free(tmp);
140 #endif
142 if(dlinfo.dli_sname && !strcmp(dlinfo.dli_sname, "main"))
143 break;
145 ip = bp[1];
146 bp = (void**)bp[0];
148 #else
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]);
155 #endif
156 log_error("End of stack trace");
157 exit (-1);
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));
168 return 0;
171 if(sigaction(SIGILL, &action, NULL) < 0) {
172 log_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
173 return 0;
176 if(sigaction(SIGABRT, &action, NULL) < 0) {
177 log_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
178 return 0;
181 if(sigaction(SIGFPE, &action, NULL) < 0) {
182 log_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
183 return 0;
186 return 1;
189 #ifndef SIGSEGV_NO_AUTO_INIT
190 static void __attribute((constructor)) init(void) {
191 setup_sigsegv();
193 #endif