-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.c
370 lines (330 loc) · 10.3 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/param.h>
#include <sys/ucontext.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
#ifdef __linux__
#include <sys/prctl.h>
#ifndef PR_SET_PTRACER
#define PR_SET_PTRACER 0x59616d61
#endif
#endif
#include "main.h"
#include "crashcatcher.h"
#define UNUSED(x) UNUSED_##x __attribute__((unused))
static const struct {
const char *name;
int signum;
} signals[] = {
{ "Segmentation fault", SIGSEGV },
{ "Illegal instruction", SIGILL },
{ "FPU exception", SIGFPE },
{ "System BUS error", SIGBUS },
{ "Abort", SIGABRT },
{ NULL, 0 }
};
static const struct {
int code;
const char *name;
} sigsegv_codes[] = {
#ifndef __FreeBSD__
{ SEGV_MAPERR, "address not mapped to object" },
{ SEGV_ACCERR, "invalid permissions for mapped object" },
#endif
{ 0, NULL }
}, sigill_codes[] = {
#ifndef __FreeBSD__
{ ILL_ILLOPC, "illegal opcode" },
{ ILL_ILLOPN, "illegal operand" },
{ ILL_ILLADR, "illegal addressing mode" },
{ ILL_ILLTRP, "illegal trap" },
{ ILL_PRVOPC, "privileged opcode" },
{ ILL_PRVREG, "privileged register" },
{ ILL_COPROC, "coprocessor error" },
{ ILL_BADSTK, "internal stack error" },
#endif
{ 0, NULL }
}, sigfpe_codes[] = {
{ FPE_INTDIV, "integer divide by zero" },
{ FPE_INTOVF, "integer overflow" },
{ FPE_FLTDIV, "floating point divide by zero" },
{ FPE_FLTOVF, "floating point overflow" },
{ FPE_FLTUND, "floating point underflow" },
{ FPE_FLTRES, "floating point inexact result" },
{ FPE_FLTINV, "floating point invalid operation" },
{ FPE_FLTSUB, "subscript out of range" },
{ 0, NULL }
}, sigbus_codes[] = {
#ifndef __FreeBSD__
{ BUS_ADRALN, "invalid address alignment" },
{ BUS_ADRERR, "non-existent physical address" },
{ BUS_OBJERR, "object specific hardware error" },
#endif
{ 0, NULL }
};
static const struct {
int code;
const char *name;
} generic_codes[] = {
{ SI_USER, "kill() function" },
{ SI_KERNEL, "sent by kernel" },
{ SI_QUEUE, "sigqueue() function" },
{ SI_TKILL , "tkill() or tgkill() function" },
{ 0, NULL }
};
static int show_kde(const struct crash_info *info, const char *sigdesc, const char *logfile)
{
char buf[512] = {'\0'};
int ret;
snprintf(buf, sizeof(buf), "kdialog --title \"%s - process %d\" --yes-label \"Show log...\" --no-label \"Close\" --yesno \"The application has crashed.\n\nA crash log was written to %s\"", sigdesc, info->pid, logfile);
ret = system(buf);
if(ret == -1 || ret == 127)
return 0;
if(ret == 0)
{
snprintf(buf, sizeof(buf), "kdialog --title \"%s - crash log\" --textbox \"%s\" 800 600", logfile, logfile);
system(buf);
}
return 1;
}
static int show_gtk(const struct crash_info *info, const char *sigdesc, const char *logfile)
{
char buf[512] = {'\0'};
int ret;
snprintf(buf, sizeof(buf), "gxmessage -title \"%s - process %d\" -buttons \"Show log...:0,Close:1\" -center \"The application has crashed.\n\nA crash log was written to %s\"", sigdesc, info->pid, logfile);
ret = system(buf);
if(ret == -1 || ret == 127)
return 0;
if(ret == 0)
{
snprintf(buf, sizeof(buf), "gxmessage -title \"%s - crash log\" -buttons \"Okay:0\" -font monospace -geometry 800x600 -center -file \"%s\"", logfile, logfile);
system(buf);
}
return 1;
}
static int show_x11(const struct crash_info *UNUSED(info), const char *UNUSED(sigdesc), const char *logfile)
{
char buf[512] = {'\0'};
int ret;
snprintf(buf, sizeof(buf), "xmessage -buttons \"Show log...:0,Close:1\" -center \"The application has crashed.\n\nA crash log was written to %s\"", logfile);
ret = system(buf);
if(ret == -1 || ret == 127)
return 0;
if(ret == 0)
{
snprintf(buf, sizeof(buf), "xmessage -buttons \"Okay:0\" -center -file \"%s\"", logfile);
system(buf);
}
return 1;
}
static void gdb_info(pid_t pid)
{
char respfile[64];
char cmd_buf[128];
FILE *f;
int fd;
/* Create a temp file to put gdb commands into */
strcpy(respfile, "/tmp/gdb-respfile-XXXXXX");
if((fd=mkstemp(respfile)) >= 0 && (f=fdopen(fd, "w")) != NULL)
{
fprintf(f, "attach %d\n", pid);
fputs("shell echo \"\"\n"
"shell echo \"* Loaded Libraries\"\n"
"info sharedlibrary\n"
"shell echo \"\"\n"
"shell echo \"* Threads\"\n"
"info threads\n"
"shell echo \"\"\n"
"shell echo \"* FPU Status\"\n"
"info float\n"
"shell echo \"\"\n"
"shell echo \"* Registers\"\n"
"info registers\n"
"shell echo \"\"\n"
"shell echo \"* Backtrace\"\n"
"thread apply all backtrace full\n"
"detach\n"
"quit\n", f);
fflush(f);
/* Run gdb and print process info. */
snprintf(cmd_buf, sizeof(cmd_buf), "gdb --quiet --batch --command=%s", respfile);
printf("Executing: %s\n", cmd_buf);
fflush(stdout);
system(cmd_buf);
/* Clean up */
fclose(f);
remove(respfile);
}
else
{
/* Error creating temp file */
if(fd >= 0)
{
close(fd);
remove(respfile);
}
puts("!!! Could not create gdb command file");
}
fflush(stdout);
}
static void sys_info(void)
{
#ifdef __unix__
system("echo \"System: `uname -a`\"");
putchar('\n');
fflush(stdout);
#endif
}
static struct crash_info crash_info;
static void crash_handler(const char *logfile)
{
const char *sigdesc = "Unknown signal";
const char *codedesc = "unknown code";
int showlog = 0;
int i;
if(fread(&crash_info, sizeof(crash_info), 1, stdin) != 1)
{
fputs("!!! Failed to retrieve info from crashed process\n", stderr);
exit(1);
}
if(crash_info.version != CRASH_INFO_VERSION)
{
fputs("!!! Incompatible crash_info structure (library mismatch)\n", stderr);
exit(1);
}
/* Get the signal description */
for(i = 0;signals[i].name;++i)
{
if(signals[i].signum == crash_info.signum)
{
sigdesc = signals[i].name;
break;
}
}
if(crash_info.has_siginfo)
{
for(i = 0;generic_codes[i].name;++i)
{
if(generic_codes[i].code == crash_info.siginfo.si_code)
{
codedesc = generic_codes[i].name;
break;
}
}
if(!generic_codes[i].name)
{
switch(crash_info.signum)
{
case SIGSEGV:
for(i = 0;sigsegv_codes[i].name;++i)
{
if(sigsegv_codes[i].code == crash_info.siginfo.si_code)
{
codedesc = sigsegv_codes[i].name;
break;
}
}
break;
case SIGFPE:
for(i = 0;sigfpe_codes[i].name;++i)
{
if(sigfpe_codes[i].code == crash_info.siginfo.si_code)
{
codedesc = sigfpe_codes[i].name;
break;
}
}
break;
case SIGILL:
for(i = 0;sigill_codes[i].name;++i)
{
if(sigill_codes[i].code == crash_info.siginfo.si_code)
{
codedesc = sigill_codes[i].name;
break;
}
}
break;
case SIGBUS:
for(i = 0;sigbus_codes[i].name;++i)
{
if(sigbus_codes[i].code == crash_info.siginfo.si_code)
{
codedesc = sigbus_codes[i].name;
break;
}
}
break;
}
}
fprintf(stderr, "%s, %s (signal %i, code 0x%02x)\n", sigdesc, codedesc, crash_info.signum, crash_info.siginfo.si_code);
if(crash_info.signum != SIGABRT)
fprintf(stderr, "Address: %p\n", crash_info.siginfo.si_addr);
fputc('\n', stderr);
}
else
fprintf(stderr, "%s (signal %i)\n\n", sigdesc, crash_info.signum);
if(logfile && *logfile)
{
/* Create crash log file and redirect shell output to it */
if(freopen(logfile, "wa", stdout) != stdout)
fprintf(stderr, "!!! Could not create %s following signal\n", logfile);
else
{
fprintf(stderr, "Generating %s and killing process %d, please wait...\n", logfile, crash_info.pid);
puts("*** Fatal Error ***");
if(!crash_info.has_siginfo)
printf("%s (signal %i)\n\n", sigdesc, crash_info.signum);
else
{
printf("%s, %s (signal %i, code 0x%02x)\n", sigdesc, codedesc, crash_info.signum, crash_info.siginfo.si_code);
if(crash_info.signum != SIGABRT)
printf("Address: %p\n", crash_info.siginfo.si_addr);
putchar('\n');
}
fflush(stdout);
showlog = 1;
}
}
sys_info();
if(crash_info.buf[0] != '\0')
{
crash_info.buf[sizeof(crash_info.buf)-1] = '\0';
puts(crash_info.buf);
fflush(stdout);
}
if(crash_info.pid > 0)
{
gdb_info(crash_info.pid);
kill(crash_info.pid, SIGKILL);
}
if(showlog)
{
const char *str;
int ret = 0;
if((str=getenv("KDE_FULL_SESSION")) && strcmp(str, "true") == 0)
ret = show_kde(&crash_info, sigdesc, logfile);
if(!ret)
{
ret = show_gtk(&crash_info, sigdesc, logfile);
if(!ret)
ret = show_x11(&crash_info, sigdesc, logfile);
}
}
}
int main(int argc, char **argv)
{
if(argc == 3 && strcmp(argv[1], CRASH_SWITCH) == 0)
{
cc_disable();
crash_handler(argv[2]);
exit(0);
}
fprintf(stderr, "%s: Do not run directly, will be run by crashing applications.\n", argv[0]);
return 1;
}