-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmojocrash.c
515 lines (418 loc) · 13.7 KB
/
mojocrash.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/**
* MojoCrash; a problem-reporting tool.
*
* Documentation is in mojocrash.h. It's verbose, honest. :)
*
* Please see the file LICENSE.txt in the source's root directory.
*
* This file written by Ryan C. Gordon.
*/
#define __MOJOCRASH_INTERNAL__ 1
#include "mojocrash_internal.h"
int MOJOCRASH_StringLength(const char *str)
{
int retval = 0;
while (*(str++))
retval++;
return retval;
} /* MOJOCRASH_StringLength */
void MOJOCRASH_StringCopy(char *dst, const char *src)
{
while (1)
{
const char ch = *(src++);
*(dst++) = ch;
if (ch == '\0')
break;
} /* while */
} /* MOJOCRASH_StringCopy */
char *MOJOCRASH_StringChar(const char *str, const char ch)
{
while (1)
{
const char strch = *str;
if (strch == ch)
return (char *) str;
else if (strch == '\0')
return NULL;
else
str++;
} /* while */
} /* MOJOCRASH_StringChar */
int MOJOCRASH_StringNCompare(const char *a, const char *b, const int n)
{
int i;
for (i = 0; i < n; i++)
{
const char ch1 = a[i];
const char ch2 = b[i];
if (ch1 < ch2)
return -1;
else if (ch1 > ch2)
return 1;
else if (ch1 == '\0')
return 0;
/* we're equal and neither string is terminated. Go on. */
} /* for */
return 0; /* matched to n chars, without a terminator. */
} /* MOJOCRASH_StringNCompare */
int MOJOCRASH_StringCompare(const char *a, const char *b)
{
while (1)
{
const char ch1 = *a;
const char ch2 = *b;
if (ch1 < ch2)
return -1;
else if (ch1 > ch2)
return 1;
else if (ch1 == '\0')
return 0;
/* we're equal and neither string is terminated. Go on. */
a++;
b++;
} /* for */
return -1; /* shouldn't happen. */
} /* MOJOCRASH_StringCompare */
void MOJOCRASH_StringAppend(char **_dst, int *avail, const char *src)
{
if (*avail > 0)
{
char *dst = *_dst;
while (*src && (*avail > 1))
{
*(dst++) = *(src++);
(*avail)--;
} /* while */
*dst = '\0';
*_dst = dst;
} /* if */
} /* MOJOCRASH_StringAppend */
static char *flipstring(char *strstart, char *str)
{
char *retval = strstart;
while (str > strstart)
{
const char tmp = *str;
*(str--) = *strstart;
*(strstart++) = tmp;
} /* while */
return retval;
} /* flipstring */
long MOJOCRASH_StringToLong(const char *str)
{
long retval = 0;
long mult = 1;
int i = 0;
while (*str == ' ')
str++;
if (*str == '-')
{
mult = -1;
str++;
} /* if */
while (1)
{
const char ch = str[i];
if ((ch < '0') || (ch > '9'))
break;
i++;
} /* while */
while (--i >= 0)
{
const char ch = str[i];
retval += ((long) (ch - '0')) * mult;
mult *= 10;
} /* while */
return retval;
} /* MOJOCRASH_StringToLong */
char *MOJOCRASH_LongToString(long num, char *str)
{
char *strstart = str;
const int negative = (num < 0);
if (negative)
num = -num;
/* write out the string reversed. */
*(str++) = '\0'; /* write out the string reversed. */
do
{
*(str++) = ('0' + (num % 10));
num /= 10;
} while (num);
if (negative)
*(str++) = '-';
return flipstring(strstart, str-1);
} /* MOJOCRASH_LongToString */
char *MOJOCRASH_ULongToString(unsigned long num, char *str)
{
char *strstart = str;
/* write out the string reversed. */
*(str++) = '\0'; /* write out the string reversed. */
do
{
*(str++) = ('0' + (num % 10));
num /= 10;
} while (num);
return flipstring(strstart, str-1);
} /* MOJOCRASH_ULongToString */
char *MOJOCRASH_PtrToString(const void *ptr, char *str)
{
/* !!! FIXME: "long" isn't right on win64, probably other places. */
return MOJOCRASH_ULongToString((const unsigned long) ptr, str);
} /* MOJOCRASH_PtrToString */
static int installed = 0;
static MOJOCRASH_hooks hooks;
static char scratch[256];
static char appname[MOJOCRASH_MAX_APPNAME_STRING];
static char version[MOJOCRASH_MAX_VERSION_STRING];
static int callstack_callback(const void *addr)
{
char numcvt[32];
char *str = scratch;
int avail = sizeof (scratch);
MOJOCRASH_StringAppend(&str, &avail, "CALLSTACK ");
MOJOCRASH_StringAppend(&str, &avail, MOJOCRASH_PtrToString(addr, numcvt));
return hooks.new_crashlog_line(scratch);
} /* callstack_callback */
static int objects_callback(const char *fname, const void *addr,
unsigned long len, unsigned long slide)
{
char *str = scratch;
int avail = sizeof (scratch);
char numcvt[32];
MOJOCRASH_StringAppend(&str, &avail, "OBJECT ");
MOJOCRASH_StringAppend(&str, &avail, fname);
MOJOCRASH_StringAppend(&str, &avail, "/");
MOJOCRASH_StringAppend(&str, &avail, MOJOCRASH_PtrToString(addr, numcvt));
MOJOCRASH_StringAppend(&str, &avail, "/");
MOJOCRASH_StringAppend(&str, &avail, MOJOCRASH_ULongToString(len, numcvt));
MOJOCRASH_StringAppend(&str, &avail, "/");
MOJOCRASH_StringAppend(&str, &avail, MOJOCRASH_ULongToString(slide,numcvt));
return hooks.new_crashlog_line(scratch);
} /* objects_callback */
static int etc_callback(const char *key, const char *value)
{
char *str = NULL;
int avail = 0;
str = scratch;
avail = sizeof (scratch);
MOJOCRASH_StringAppend(&str, &avail, "ETC_KEY ");
MOJOCRASH_StringAppend(&str, &avail, key);
if (!hooks.new_crashlog_line(scratch))
return 0;
str = scratch;
avail = sizeof (scratch);
MOJOCRASH_StringAppend(&str, &avail, "ETC_VALUE ");
MOJOCRASH_StringAppend(&str, &avail, value);
if (!hooks.new_crashlog_line(scratch))
return 0;
return 1;
} /* etc_callback */
void MOJOCRASH_StringAppendMojoCrashVersion(char **dst, int *avail)
{
char numcvt[32];
MOJOCRASH_StringAppend(dst, avail,
MOJOCRASH_ULongToString(MOJOCRASH_VERSION_MAJOR, numcvt));
MOJOCRASH_StringAppend(dst, avail, ".");
MOJOCRASH_StringAppend(dst, avail,
MOJOCRASH_ULongToString(MOJOCRASH_VERSION_MINOR, numcvt));
MOJOCRASH_StringAppend(dst, avail, ".");
MOJOCRASH_StringAppend(dst, avail,
MOJOCRASH_ULongToString(MOJOCRASH_VERSION_PATCH, numcvt));
} /* MOJOCRASH_StringAppendMojoCrashVersion */
static int get_basics(int sig)
{
char numcvt[32];
char *str = NULL;
int avail = 0;
#define INTRO(x) if (!hooks.new_crashlog_line(x)) return 0
INTRO("");
INTRO("# This is a log of a program crash. It is meant to help the");
INTRO("# developers debug the problem that caused the crash.");
INTRO("#");
INTRO("# This log was generated by software called \"MojoCrash\" but");
INTRO("# please note that MojoCrash did NOT cause the crash in the");
INTRO("# first place. It only helped report the problem. Please do not");
INTRO("# contact the MojoCrash developers about this problem, we didn't");
INTRO("# cause it and probably haven't heard of the crashing program.");
INTRO("#");
INTRO("# If you need to contact someone about a bug, please contact the");
INTRO("# original vendor of your program instead. Thanks!");
INTRO("");
#undef INTRO
str = scratch;
avail = sizeof (scratch);
MOJOCRASH_StringAppend(&str, &avail, "MOJOCRASH ");
MOJOCRASH_StringAppendMojoCrashVersion(&str, &avail);
if (!hooks.new_crashlog_line(scratch))
return 0;
str = scratch;
avail = sizeof (scratch);
MOJOCRASH_StringAppend(&str, &avail, "CRASHLOG_VERSION ");
MOJOCRASH_StringAppend(&str, &avail,
MOJOCRASH_ULongToString(MOJOCRASH_LOG_FORMAT_VERSION, numcvt));
if (!hooks.new_crashlog_line(scratch))
return 0;
str = scratch;
avail = sizeof (scratch);
MOJOCRASH_StringAppend(&str, &avail, "APPLICATION_NAME ");
MOJOCRASH_StringAppend(&str, &avail, appname);
if (!hooks.new_crashlog_line(scratch))
return 0;
str = scratch;
avail = sizeof (scratch);
MOJOCRASH_StringAppend(&str, &avail, "APPLICATION_VERSION ");
MOJOCRASH_StringAppend(&str, &avail, version);
if (!hooks.new_crashlog_line(scratch))
return 0;
if (!hooks.new_crashlog_line("PLATFORM_NAME " MOJOCRASH_PLATFORM_NAME))
return 0;
if (!hooks.new_crashlog_line("CPUARCH_NAME " MOJOCRASH_PLATFORM_CPUARCH))
return 0;
str = scratch;
avail = sizeof (scratch);
MOJOCRASH_StringAppend(&str, &avail, "PLATFORM_VERSION ");
MOJOCRASH_StringAppend(&str, &avail, MOJOCRASH_platform_version());
if (!hooks.new_crashlog_line(scratch))
return 0;
str = scratch;
avail = sizeof (scratch);
MOJOCRASH_StringAppend(&str, &avail, "CRASH_SIGNAL ");
MOJOCRASH_StringAppend(&str, &avail, MOJOCRASH_LongToString(sig, numcvt));
if (!hooks.new_crashlog_line(scratch))
return 0;
str = scratch;
avail = sizeof (scratch);
MOJOCRASH_StringAppend(&str, &avail, "APPLICATION_UPTIME ");
MOJOCRASH_StringAppend(&str, &avail,
MOJOCRASH_LongToString(MOJOCRASH_platform_appuptime(), numcvt));
if (!hooks.new_crashlog_line(scratch))
return 0;
str = scratch;
avail = sizeof (scratch);
MOJOCRASH_StringAppend(&str, &avail, "CRASH_TIME ");
MOJOCRASH_StringAppend(&str, &avail,
MOJOCRASH_LongToString(MOJOCRASH_platform_now(), numcvt));
if (!hooks.new_crashlog_line(scratch))
return 0;
return 1;
} /* get_basics */
static int crash_catcher_internal(int sig, int crash_count)
{
if (!hooks.preflight(sig, crash_count)) return 0;
if (!hooks.start_crashlog(appname)) return 0;
if (!get_basics(sig)) return 0;
if (!hooks.new_crashlog_line("")) return 0;
if (!hooks.get_objects(objects_callback)) return 0;
if (!hooks.new_crashlog_line("")) return 0;
if (!hooks.get_callstack(callstack_callback)) return 0;
if (!hooks.new_crashlog_line("")) return 0;
if (!hooks.get_etc(etc_callback)) return 0;
if (!hooks.new_crashlog_line("")) return 0;
if (!hooks.new_crashlog_line("END")) return 0;
if (!hooks.new_crashlog_line("")) return 0;
if (!hooks.end_crashlog()) return 0;
if (!hooks.postflight()) return 0;
return 1;
} /* crash_catcher_internal */
/*
* This is the actual crash catcher entry point that should be invoked from
* from a signal handler or whatnot.
*/
static void crash_catcher(int sig)
{
static int crash_count = 0;
crash_count++;
crash_catcher_internal(sig, crash_count);
hooks.die(); /* may not return. */
crash_count--; /* in case we did... */
} /* crash_catcher */
/* The default hooks... */
static int defhook_install_crash_catcher(MOJOCRASH_catcher catcher)
{
return MOJOCRASH_platform_install_crash_catcher(catcher);
} /* defhook_install_crash_catcher */
static int defhook_preflight(int sig, int crash_count)
{
if (crash_count == 1)
return 1; /* first crash, go forward. */
else if (crash_count == 2)
return 0; /* try to fail. */
else if (crash_count == 3)
hooks.die(); /* try to die. */
else if (crash_count == 4)
MOJOCRASH_platform_die(0); /* Really try to die. */
else if (crash_count == 5)
MOJOCRASH_platform_die(1); /* REALLY try to die! */
return 0; /* oh well. */
} /* defhook_preflight */
static int defhook_start_crashlog(const char *appname)
{
return MOJOCRASH_platform_start_crashlog(appname);
} /* defhook_start_crashlog */
static int defhook_new_crashlog_line(const char *str)
{
return MOJOCRASH_platform_new_crashlog_line(str);
} /* defhook_new_crashlog_line */
static int defhook_get_objects(MOJOCRASH_get_objects_callback callback)
{
return MOJOCRASH_platform_get_objects(callback);
} /* defhook_get_objects */
static int defhook_get_callstack(MOJOCRASH_get_callstack_callback callback)
{
return MOJOCRASH_platform_get_callstack(callback);
} /* defhook_get_callstack */
static int defhook_get_etc(MOJOCRASH_get_etc_callback callback)
{
return 1; /* nothing to report here, just go on. */
} /* defhook_get_etc */
static int defhook_end_crashlog(void)
{
return MOJOCRASH_platform_end_crashlog();
} /* defhook_end_crashlog */
static int defhook_postflight(void)
{
return 1; /* tell normal processing to go forward. */
} /* defhook_postflight */
static void defhook_die(void)
{
MOJOCRASH_platform_die(0);
} /* defhook_die */
static void init_hooks(const MOJOCRASH_hooks *h)
{
#define INIT_HOOK(H) hooks.H = ((h && h->H) ? h->H : defhook_##H)
INIT_HOOK(install_crash_catcher);
INIT_HOOK(preflight);
INIT_HOOK(start_crashlog);
INIT_HOOK(new_crashlog_line);
INIT_HOOK(get_objects);
INIT_HOOK(get_callstack);
INIT_HOOK(get_etc);
INIT_HOOK(end_crashlog);
INIT_HOOK(postflight);
INIT_HOOK(die);
#undef INIT_HOOK
} /* init_hooks */
/* Entry point(s) ... */
int MOJOCRASH_install(const char *_appname, const char *_version,
const MOJOCRASH_hooks *_hooks)
{
if (installed)
return 0; /* don't double-call this! */
/* sanity-check parameters. */
if (_appname == NULL) return 0;
if (_version == NULL) return 0;
if (strlen(_appname) >= MOJOCRASH_MAX_APPNAME_STRING) return 0;
if (strlen(_version) >= MOJOCRASH_MAX_VERSION_STRING) return 0;
/* set up state. */
strcpy(appname, _appname);
strcpy(version, _version);
init_hooks(_hooks);
if (!MOJOCRASH_platform_init())
return 0;
if (!hooks.install_crash_catcher(crash_catcher))
return 0;
installed = 1;
return 1; /* success! */
} /* MOJOCRASH_install */
/* end of mojocrash.c ... */