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
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.
28 #include <sys/types.h>
33 #include <sys/ucontext.h>
35 static const char *cc_logfile
= NULL
;
37 static char respfile
[256];
39 static char user_info_buf
[1024];
45 { "Segmentation fault", SIGSEGV
},
46 { "Illegal instruction", SIGILL
},
47 { "FPU exception", SIGFPE
},
48 { "System BUS error", SIGBUS
},
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" },
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" },
86 { SEGV_MAPERR
, "Address not mapped to object" },
87 { SEGV_ACCERR
, "Invalid permissions for mapped object" },
95 { BUS_ADRALN
, "Invalid address alignment" },
96 { BUS_ADRERR
, "Non-existent physical address" },
97 { BUS_OBJERR
, "Object specific hardware error" },
101 static int (*cc_user_info
)(char*, char*);
103 static void gdb_info(pid_t pid
)
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"
114 "shell echo \"* Loaded Libraries\"\n"
115 "info sharedlibrary\n"
117 "shell echo \"* Threads\"\n"
120 "shell echo \"* FPU Status\"\n"
123 "shell echo \"* Registers\"\n"
126 "shell echo \"* Bytes near %%eip:\"\n"
130 "shell echo \"* Backtrace\"\n"
132 #if 0 /* This sorta works to print out the core, but is too slow and skips 0's.. */
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"
143 " set var $_i = $_sp\n"
144 " while $_i < $_sp + 16\n"
145 " printf \"%%c\", {int} $_i\n"
148 " set var $_sp += 16\n"
152 " printf \"%%08x: \", $esp\n"
153 " while $_sp <= $ebp\n"
154 " printf \"%%08x \", {int} $_i\n"
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
);
175 /* Error creating temp file */
181 printf("Could not create gdb command file\n");
187 /* Generic system info */
188 static void sys_info(void)
190 #if (defined __unix__)
191 system("echo \"System: `uname -a`\"");
193 system("echo \"GCC version: `gcc -dumpversion`\"");
198 void cc_crash_log(const char* errdesc
)
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
);
212 /* Create crash log file */
215 f
= fopen(cc_logfile
, "w");
218 fprintf(stderr
, "Could not create %s following signal.\n", cc_logfile
);
225 /* Get current process id and fork off */
227 switch ((dbg_pid
= fork()))
233 /* Child process, start writing an error log */
235 fprintf(stderr
, "\n\n*** Fatal Error ***\n"
238 /* Redirect shell output */
239 close(STDOUT_FILENO
);
240 dup2(fileno(f
), STDOUT_FILENO
);
244 fprintf(stderr
, "\nGenerating %s and killing process %i, please wait... ", cc_logfile
, pid
);
245 fprintf(f
, "*** Fatal Error ***\n"
255 if(cc_user_info(user_info_buf
, user_info_buf
+sizeof(user_info_buf
)) > 0)
257 fprintf(f
, "%s\n", user_info_buf
);
263 #if 0 /* Why won't this work? */
266 unsigned char *ptr
= ucontext
->uc_stack
.ss_sp
;
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]);
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
++));
293 #if 0 /* Create a pop-up window for the error; this might not make sense for a window manager crash. */
297 #if (defined __unix__)
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
);
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
)
324 ucontext_t
*ucontext
= (ucontext_t
*)context
;
326 const char *sigdesc
= NULL
;
331 /* Get the signal description */
334 for(i
= 0;signals
[i
].name
;++i
)
336 if(signals
[i
].signum
== signum
)
338 sigdesc
= signals
[i
].name
;
348 for(i
= 0;sigsegv_codes
[i
].name
;++i
)
350 if(sigsegv_codes
[i
].code
== siginfo
->si_code
)
352 sigdesc
= sigsegv_codes
[i
].name
;
359 for(i
= 0;sigfpe_codes
[i
].name
;++i
)
361 if(sigfpe_codes
[i
].code
== siginfo
->si_code
)
363 sigdesc
= sigfpe_codes
[i
].name
;
370 for(i
= 0;sigill_codes
[i
].name
;++i
)
372 if(sigill_codes
[i
].code
== siginfo
->si_code
)
374 sigdesc
= sigill_codes
[i
].name
;
381 for(i
= 0;sigbus_codes
[i
].name
;++i
)
383 if(sigbus_codes
[i
].code
== siginfo
->si_code
)
385 sigdesc
= sigbus_codes
[i
].name
;
395 /* Unknown signal, let the default handler deal with it */
401 snprintf(addrdesc
, sizeof(addrdesc
)/sizeof(char), " at Address: %p\n", siginfo
->si_addr
);
405 snprintf(errdesc
, sizeof(errdesc
)/sizeof(char), "%s (signal %i)%s\n", sigdesc
, signum
, addrdesc
);
407 cc_crash_log(errdesc
);
412 int cc_install_handlers(int num_signals
, int *signals
, const char *logfile
, int (*user_info
)(char*, char*))
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
;
426 if(sigaction(*signals
, &sa
, NULL
) == -1)