-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.c
97 lines (73 loc) · 1.67 KB
/
error.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
#define _GNU_SOURCE // enable GNU feature set
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <execinfo.h> // generic stack trace
#include <errno.h>
#include "error.h"
// die and print system error
void dies(char* format, ...) {
va_list args;
va_start(args, format);
vdie(format, args);
}
// die without printing system error
void die(char* format, ...) {
va_list args;
va_start(args, format);
errno = 0;
vdie(format, args);
}
void vdie(char* format, va_list args) {
char* tmp;
unsigned int chars_left = ERROR_MSG_MAX_LENGTH;
int num;
// generic stack trace
void *bt[20];
char **strings;
size_t sz;
unsigned int i;
num = fprintf(stderr, "----------------------\n");
if(num < 0) {
exit(-1);
}
num = fprintf(stderr, "ERROR: ");
if(num < 0) {
exit(-1);
}
if(num < 0) {
exit(-1);
}
tmp = malloc(sizeof(char) * (ERROR_MSG_MAX_LENGTH+1));
if(!tmp) {
exit(-1); // XXX what if this happens from inside a thread?
}
num = vsnprintf(tmp, chars_left, format, args);
if(num < 0) {
free(tmp);
exit(-1);
}
chars_left -= num;
if(errno) {
num = snprintf(tmp+num, chars_left, "(errno indicated: %s)\n", strerror(errno)); // XXXX strerror is not thread safe
if(num < 0) {
exit(-1);
}
}
fprintf(stderr, tmp);
free(tmp);
fprintf(stderr, "Stack Trace (generic):\n");
sz = backtrace(bt, 20);
strings = backtrace_symbols(bt, sz);
for(i=1; i < sz; i++) {
fprintf(stderr, "%2d: %p %s\n", i, bt[i], strings[i]);
}
exit(-1);
}
void nprint(char* str, int num_chars) {
int i;
for(i=0; i < num_chars; i++) {
putchar((int) *(str + i));
}
}