-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.c
91 lines (74 loc) · 1.51 KB
/
debug.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
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <signal.h> /* for signal */
#include <execinfo.h> /* for backtrace() */
#include <errno.h>
#define BACKTRACE_SIZE 16
void dump_stack(void)
{
int j, nptrs;
void *buffer[BACKTRACE_SIZE];
char **strings;
nptrs = backtrace(buffer, BACKTRACE_SIZE);
printf("backtrace() returned %d addresses\n", nptrs);
strings = backtrace_symbols(buffer, nptrs);
if (strings == NULL) {
perror("backtrace_symbols");
exit(EXIT_FAILURE);
}
for (j = 0; j < nptrs; j++)
printf(" [%02d] %s\n", j, strings[j]);
free(strings);
}
void signal_handler(int signo)
{
printf("\n---------->catch signal %d<----------\n", signo);
printf("dump stack start...\n");
dump_stack();
printf("dump stack end...\n");
signal(signo, SIG_DFL);
raise(signo);
}
static void __error_msg(int err_no, const char *fmt, va_list p)
{
char *msg;
fflush(NULL);
msg = NULL;
if (vasprintf(&msg, fmt, p) >= 0) {
if (err_no)
fprintf(stderr, "%s: %s\n", msg, strerror(err_no));
else
fprintf(stderr, "%s\n", msg);
free(msg);
}
else {
vfprintf(stderr, fmt, p);
if (err_no)
fprintf(stderr, ": %s\n", strerror(err_no));
else
putc('\n', stderr);
}
}
void error_msg(const char *fmt, ...)
{
va_list p;
va_start(p, fmt);
__error_msg(errno, fmt, p);
va_end(p);
}
#ifdef DEBUG
void debug_msg(const char *fmt, ...)
{
va_list p;
va_start(p, fmt);
printf(fmt, p);
va_end(p);
}
#else
void debug_msg(const char *fmt, ...)
{
}
#endif