-
Notifications
You must be signed in to change notification settings - Fork 1
/
error.c
52 lines (43 loc) · 1.13 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
#define MAX_BACKTRACE_LENGTH 256
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <syslog.h>
#include <regex.h>
#if defined(__GLIBC__) && !defined(__UCLIBC__)
#include <execinfo.h>
#endif
void print_trace(void)
{
#if defined(__GLIBC__) && !defined(__UCLIBC__)
void *array[MAX_BACKTRACE_LENGTH];
size_t size;
size = backtrace(array, MAX_BACKTRACE_LENGTH);
printf("Obtained %zd stack frames:\n", size);
fflush(NULL);
backtrace_symbols_fd(array, size, 1);
#endif
}
void error_exit(char *format, ...)
{
char buffer[4096];
va_list ap;
va_start(ap, format);
vsnprintf(buffer, sizeof(buffer), format, ap);
va_end(ap);
fprintf(stderr, "%s\n", buffer);
syslog(LOG_INFO, "%s", buffer);
printf("\n\n\nDebug information:\n");
if (errno) fprintf(stderr, "errno: %d=%s (if applicable)\n", errno, strerror(errno));
#if defined(__GLIBC__) && !defined(__UCLIBC__)
print_trace();
#endif
fflush(NULL);
(void)kill(0, SIGTERM); /* terminate every process in the process group of the current process */
exit(EXIT_FAILURE);
}