core: fix a bug where the titlebar drag and drop code would sometimes fail to release...
[luccawm.git] / crashcatcher / crashcatcher.c
blobb39941b56d38526b43bde3e06f65786ca16eb492
1 /* Copyright (c) 2008 Chris Robinson, Vincent Povirk
3 Permission is hereby granted, free of charge, to any person
4 obtaining a copy of this software and associated documentation
5 files (the "Software"), to deal in the Software without
6 restriction, including without limitation the rights to use,
7 copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the
9 Software is furnished to do so, subject to the following
10 conditions:
12 The above copyright notice and this permission notice shall be
13 included in all copies or substantial portions of the Software.
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 OTHER DEALINGS IN THE SOFTWARE.
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <sys/wait.h>
31 #include <string.h>
32 #include <errno.h>
33 #include <sys/ucontext.h>
35 static const char *cc_logfile = NULL;
37 static char respfile[256];
38 static char buf[256];
39 static char user_info_buf[1024];
41 static const struct {
42 const char *name;
43 int signum;
44 } signals[] = {
45 { "Segmentation fault", SIGSEGV },
46 { "Illegal instruction", SIGILL },
47 { "FPU exception", SIGFPE },
48 { "System BUS error", SIGBUS },
49 { NULL, 0 }
52 static const struct {
53 int code;
54 const char *name;
55 } sigill_codes[] = {
56 { ILL_ILLOPC, "Illegal opcode" },
57 { ILL_ILLOPN, "Illegal operand" },
58 { ILL_ILLADR, "Illegal addressing mode" },
59 { ILL_ILLTRP, "Illegal trap" },
60 { ILL_PRVOPC, "Privileged opcode" },
61 { ILL_PRVREG, "Privileged register" },
62 { ILL_COPROC, "Coprocessor error" },
63 { ILL_BADSTK, "Internal stack error" },
64 { 0, NULL }
67 static const struct {
68 int code;
69 const char *name;
70 } sigfpe_codes[] = {
71 { FPE_INTDIV, "Integer divide by zero" },
72 { FPE_INTOVF, "Integer overflow" },
73 { FPE_FLTDIV, "Floating point divide by zero" },
74 { FPE_FLTOVF, "Floating point overflow" },
75 { FPE_FLTUND, "Floating point underflow" },
76 { FPE_FLTRES, "Floating point inexact result" },
77 { FPE_FLTINV, "Floating point invalid operation" },
78 { FPE_FLTSUB, "Subscript out of range" },
79 { 0, NULL }
82 static const struct {
83 int code;
84 const char *name;
85 } sigsegv_codes[] = {
86 { SEGV_MAPERR, "Address not mapped to object" },
87 { SEGV_ACCERR, "Invalid permissions for mapped object" },
88 { 0, NULL }
91 static const struct {
92 int code;
93 const char *name;
94 } sigbus_codes[] = {
95 { BUS_ADRALN, "Invalid address alignment" },
96 { BUS_ADRERR, "Non-existent physical address" },
97 { BUS_OBJERR, "Object specific hardware error" },
98 { 0, NULL }
101 static int (*cc_user_info)(char*, char*);
103 static void gdb_info(pid_t pid)
105 FILE *f;
106 int fd;
108 /* Create a temp file to put gdb commands into */
109 strcpy(respfile, "gdb-respfile-XXXXXX");
110 if((fd = mkstemp(respfile)) >= 0 && (f = fdopen(fd, "w")))
112 fprintf(f, "signal SIGCHLD\n"
113 "shell echo \"\"\n"
114 "shell echo \"* Loaded Libraries\"\n"
115 "info sharedlibrary\n"
116 "shell echo \"\"\n"
117 "shell echo \"* Threads\"\n"
118 "info threads\n"
119 "shell echo \"\"\n"
120 "shell echo \"* FPU Status\"\n"
121 "info float\n"
122 "shell echo \"\"\n"
123 "shell echo \"* Registers\"\n"
124 "info registers\n"
125 "shell echo \"\"\n"
126 "shell echo \"* Bytes near %%eip:\"\n"
127 "x/x $eip-3\n"
128 "x/x $eip\n"
129 "shell echo \"\"\n"
130 "shell echo \"* Backtrace\"\n"
131 "backtrace full\n"
132 #if 0 /* This sorta works to print out the core, but is too slow and skips 0's.. */
133 "shell echo \"\"\n"
134 "shell echo \"* Stack\"\n"
135 "set var $_sp = $esp\n"
136 "while $_sp <= $ebp - 12\n"
137 " printf \"%%08x: \", $_sp\n"
138 " set var $_i = $_sp\n"
139 " while $_i < $_sp + 16\n"
140 " printf \"%%08x \", {int} $_i\n"
141 " set $_i += 4\n"
142 " end\n"
143 " set var $_i = $_sp\n"
144 " while $_i < $_sp + 16\n"
145 " printf \"%%c\", {int} $_i\n"
146 " set ++$_i\n"
147 " end\n"
148 " set var $_sp += 16\n"
149 " printf \"\\n\"\n"
150 "end\n"
151 "if $_sp <= $ebp\n"
152 " printf \"%%08x: \", $esp\n"
153 " while $_sp <= $ebp\n"
154 " printf \"%%08x \", {int} $_i\n"
155 " set $_sp += 4\n"
156 " end\n"
157 " printf \"\\n\"\n"
158 "end\n"
159 #endif
160 "kill\n"
161 "quit\n");
162 fclose(f);
164 /* Run gdb and print process info. */
165 snprintf(buf, sizeof(buf), "gdb --quiet --batch --command=%s --pid=%i", respfile, pid);
166 printf("Executing: %s\n", buf);
167 fflush(stdout);
168 system(buf);
170 /* Clean up */
171 remove(respfile);
173 else
175 /* Error creating temp file */
176 if(fd >= 0)
178 close(fd);
179 remove(respfile);
181 printf("Could not create gdb command file\n");
183 fflush(stdout);
187 /* Generic system info */
188 static void sys_info(void)
190 #if (defined __unix__)
191 system("echo \"System: `uname -a`\"");
192 #endif
193 system("echo \"GCC version: `gcc -dumpversion`\"");
194 putchar('\n');
195 fflush(stdout);
198 void cc_crash_log(const char* errdesc)
200 pid_t pid, dbg_pid;
201 FILE *f;
203 #if 0 /* Do we need this? */
204 /* Make sure the effective uid is the real uid */
205 if (getuid() != geteuid())
207 fprintf(stderr, "%s (signal %i)\ngetuid() does not match geteuid().\n", errdesc, signum);
208 _exit(-1);
210 #endif
212 /* Create crash log file */
213 if(cc_logfile)
215 f = fopen(cc_logfile, "w");
216 if(!f)
218 fprintf(stderr, "Could not create %s following signal.\n", cc_logfile);
219 return;
222 else
223 f = stderr;
225 /* Get current process id and fork off */
226 pid = getpid();
227 switch ((dbg_pid = fork()))
229 /* Error */
230 case -1:
231 break;
233 /* Child process, start writing an error log */
234 case 0:
235 fprintf(stderr, "\n\n*** Fatal Error ***\n"
236 "%s\n", errdesc);
238 /* Redirect shell output */
239 close(STDOUT_FILENO);
240 dup2(fileno(f), STDOUT_FILENO);
242 if(f != stderr)
244 fprintf(stderr, "\nGenerating %s and killing process %i, please wait... ", cc_logfile, pid);
245 fprintf(f, "*** Fatal Error ***\n"
246 "%s\n", errdesc);
248 fputc('\n', f);
249 fflush(f);
251 /* Get info */
252 sys_info();
253 if(cc_user_info)
255 if(cc_user_info(user_info_buf, user_info_buf+sizeof(user_info_buf)) > 0)
257 fprintf(f, "%s\n", user_info_buf);
258 fflush(f);
261 gdb_info(pid);
263 #if 0 /* Why won't this work? */
264 if(ucontext)
266 unsigned char *ptr = ucontext->uc_stack.ss_sp;
267 size_t len;
269 fprintf(f, "\n* Stack\n");
270 for(len = ucontext->uc_stack.ss_size/4;len > 0; len -= 4)
272 fprintf(f, "0x%08x:", (int)ptr);
273 for(i = 0;i < ((len < 4) ? len : 4);++i)
275 fprintf(f, " %02x%02x%02x%02x", ptr[i*4 + 0], ptr[i*4 + 1],
276 ptr[i*4 + 2], ptr[i*4 + 3]);
278 fputc(' ', f);
279 fflush(f);
280 for(i = 0;i < ((len < 4) ? len : 4);++i)
282 fprintf(f, "%c", *(ptr++));
283 fprintf(f, "%c", *(ptr++));
284 fprintf(f, "%c", *(ptr++));
285 fprintf(f, "%c", *(ptr++));
287 fputc('\n', f);
288 fflush(f);
291 #endif
293 #if 0 /* Create a pop-up window for the error; this might not make sense for a window manager crash. */
294 if(f != stderr)
296 fclose(f);
297 #if (defined __unix__)
298 if(cc_logfile)
300 static char buf[1024];
301 snprintf(buf, sizeof(buf),
302 "if which gxmessage &>/dev/null ; "
303 "then gxmessage -buttons \"Damn it:0\" -center -title \"Very Fatal Error\" -file %s ; "
304 "elif which xmessage &>/dev/null ; "
305 "then xmessage -buttons \"Damn it:0\" -center -file %s ; "
306 "fi", cc_logfile, cc_logfile);
307 system(buf);
309 #endif
311 #endif
313 _exit(0);
315 default:
316 /* Wait and let the child attach gdb */
317 waitpid(dbg_pid, NULL, 0);
321 static void crash_catcher(int signum, siginfo_t *siginfo, void *context)
323 #if 0
324 ucontext_t *ucontext = (ucontext_t*)context;
325 #endif
326 const char *sigdesc = NULL;
327 char addrdesc[512];
328 char errdesc[512];
329 int i;
331 /* Get the signal description */
332 if(!siginfo)
334 for(i = 0;signals[i].name;++i)
336 if(signals[i].signum == signum)
338 sigdesc = signals[i].name;
339 break;
343 else
345 switch(signum)
347 case SIGSEGV:
348 for(i = 0;sigsegv_codes[i].name;++i)
350 if(sigsegv_codes[i].code == siginfo->si_code)
352 sigdesc = sigsegv_codes[i].name;
353 break;
356 break;
358 case SIGFPE:
359 for(i = 0;sigfpe_codes[i].name;++i)
361 if(sigfpe_codes[i].code == siginfo->si_code)
363 sigdesc = sigfpe_codes[i].name;
364 break;
367 break;
369 case SIGILL:
370 for(i = 0;sigill_codes[i].name;++i)
372 if(sigill_codes[i].code == siginfo->si_code)
374 sigdesc = sigill_codes[i].name;
375 break;
378 break;
380 case SIGBUS:
381 for(i = 0;sigbus_codes[i].name;++i)
383 if(sigbus_codes[i].code == siginfo->si_code)
385 sigdesc = sigbus_codes[i].name;
386 break;
389 break;
393 if(!sigdesc)
395 /* Unknown signal, let the default handler deal with it */
396 raise(signum);
397 return;
400 if(siginfo)
401 snprintf(addrdesc, sizeof(addrdesc)/sizeof(char), " at Address: %p\n", siginfo->si_addr);
402 else
403 addrdesc[0] = '\0';
405 snprintf(errdesc, sizeof(errdesc)/sizeof(char), "%s (signal %i)%s\n", sigdesc, signum, addrdesc);
407 cc_crash_log(errdesc);
409 raise(signum);
412 int cc_install_handlers(int num_signals, int *signals, const char *logfile, int (*user_info)(char*, char*))
414 int retval = 0;
415 struct sigaction sa;
416 memset(&sa, 0, sizeof(sa));
418 sa.sa_sigaction = crash_catcher;
419 sa.sa_flags = SA_SIGINFO;
421 cc_logfile = logfile;
422 cc_user_info = user_info;
424 while(num_signals--)
426 if(sigaction(*signals, &sa, NULL) == -1)
428 *signals = 0;
429 retval = -1;
431 ++signals;
434 return retval;