forked from gozfree/gear-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibdebug.c
293 lines (277 loc) · 8.41 KB
/
libdebug.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
/******************************************************************************
* Copyright (C) 2014-2018 Zhifeng Gong <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with libraries; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
******************************************************************************/
#ifndef __cplusplus
#define _GNU_SOURCE
#endif
#include <ucontext.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <stdint.h>
#if !defined(__UCLIBC__)
#include <execinfo.h>
#endif
#include "libdebug.h"
#define BT_SIZE 100
#define CMD_SIZE 1024
/*
* from /usr/include/bits/signum.h
*/
static int signals_all[] = {
SIGHUP, /* 1 Hangup (POSIX). */
SIGINT, /* 2 Interrupt (ANSI). */
SIGQUIT, /* 3 Quit (POSIX). */
SIGILL, /* 4 Illegal instruction (ANSI). */
SIGTRAP, /* 5 Trace trap (POSIX). */
SIGABRT, /* 6 Abort (ANSI). */
SIGIOT, /* 6 IOT trap (4.2 BSD). */
SIGBUS, /* 7 BUS error (4.2 BSD). */
SIGFPE, /* 8 Floating-point exception (ANSI). */
SIGKILL, /* 9 Kill, unblockable (POSIX). */
SIGUSR1, /* 10 User-defined signal 1 (POSIX). */
SIGSEGV, /* 11 Segmentation violation (ANSI). */
SIGUSR2, /* 12 User-defined signal 2 (POSIX). */
SIGPIPE, /* 13 Broken pipe (POSIX). */
SIGALRM, /* 14 Alarm clock (POSIX). */
SIGTERM, /* 15 Termination (ANSI). */
SIGSTKFLT, /* 16 Stack fault. */
SIGCLD, /* 17 Same as SIGCHLD (System V). */
SIGCHLD, /* 17 Child status has changed (POSIX). */
SIGCONT, /* 18 Continue (POSIX). */
SIGSTOP, /* 19 Stop, unblockable (POSIX). */
SIGTSTP, /* 20 Keyboard stop (POSIX). */
SIGTTIN, /* 21 Background read from tty (POSIX). */
SIGTTOU, /* 22 Background write to tty (POSIX). */
SIGURG, /* 23 Urgent condition on socket (4.2 BSD). */
SIGXCPU, /* 24 CPU limit exceeded (4.2 BSD). */
SIGXFSZ, /* 25 File size limit exceeded (4.2 BSD). */
SIGVTALRM, /* 26 Virtual alarm clock (4.2 BSD). */
SIGPROF, /* 27 Profiling alarm clock (4.2 BSD). */
SIGWINCH, /* 28 Window size change (4.3 BSD, Sun). */
SIGPOLL, /* 29 Pollable event occurred (System V). */
SIGIO, /* 29 I/O now possible (4.2 BSD). */
SIGPWR, /* 30 Power failure restart (System V). */
SIGSYS, /* 31 Bad system call. */
};
static int signals_trace[] =
{
SIGILL, /* Illegal instruction (ANSI). */
SIGABRT, /* Abort (ANSI). */
SIGBUS, /* BUS error (4.2 BSD). (unaligned access) */
SIGFPE, /* Floating-point exception (ANSI). */
SIGSEGV, /* Segmentation violation (ANSI). */
};
#if !defined(__UCLIBC__)
static void *get_uc_mcontext_pc(ucontext_t *uc)
{
#if defined(__APPLE__) && !defined(MAC_OS_X_VERSION_10_6)
/* OSX < 10.6 */
#if defined(__x86_64__)
return (void*) uc->uc_mcontext->__ss.__rip;
#elif defined(__i386__)
return (void*) uc->uc_mcontext->__ss.__eip;
#else
return (void*) uc->uc_mcontext->__ss.__srr0;
#endif
#elif defined(__APPLE__) && defined(MAC_OS_X_VERSION_10_6)
/* OSX >= 10.6 */
#if defined(_STRUCT_X86_THREAD_STATE64) && !defined(__i386__)
return (void*) uc->uc_mcontext->__ss.__rip;
#else
return (void*) uc->uc_mcontext->__ss.__eip;
#endif
#elif defined(__linux__)
/* Linux */
#if defined(__i386__)
return (void*) uc->uc_mcontext.gregs[REG_EIP]; /* Linux 32 */
#elif defined(__X86_64__) || defined(__x86_64__)
return (void*) uc->uc_mcontext.gregs[REG_RIP]; /* Linux 64 */
#elif defined(__ia64__) /* Linux IA64 */
return (void*) uc->uc_mcontext.sc_ip;
#elif defined(__arm__)
return (void*) uc->uc_mcontext.arm_pc;
#elif defined(__aarch64__)
return (void*) uc->uc_mcontext.pc;
#else
return NULL;
#endif
#else
return NULL;
#endif
}
#endif
#if !defined(__UCLIBC__)
static void backtrace_symbols_detail(void *array[], int size)
{
int i;
char cmd[CMD_SIZE] = "addr2line -C -f -e ";
char* prog = cmd + strlen(cmd);
int r = readlink("/proc/self/exe", prog, sizeof(cmd) - (prog-cmd)-1);
if (r == -1) {
fprintf(stderr, "backtrace_symbols_detail unsupported!\n");
perror("readlink");
return;
}
prog[r] = '\0';
FILE* fp = popen(cmd, "w");
if (!fp) {
perror("popen");
return;
}
for (i = 1; i < size; ++i) {//from 1, ignore this file info
fprintf(fp, "%p\n", array[i]);
}
fclose(fp);
}
#endif
static void backtrace_handler(int sig_num, siginfo_t *info, void *ucontext)
{
#if !defined(__UCLIBC__)
void *array[BT_SIZE];
int i, size = 0;
char **strings = NULL;
void *caller_addr = get_uc_mcontext_pc((ucontext_t *)ucontext);
fprintf(stderr, "Program received signal %s.\n", strsignal(sig_num));
fprintf(stderr, "%d:%s [%p]\n", sig_num, strerror(sig_num), caller_addr);
size = backtrace(array, BT_SIZE);
/* overwrite sigaction with caller's address, but seems no needed */
array[1] = caller_addr;
strings = backtrace_symbols(array, size);
for (i = 0; i < size; ++ i) {
fprintf(stderr, "#%d %s\n", i, strings[i]);
}
backtrace_symbols_detail(array, size);
#endif
exit(EXIT_FAILURE);
}
int debug_backtrace_init(void)
{
int i;
struct sigaction sa;
memset(&sa, 0, sizeof(struct sigaction));
sa.sa_sigaction = backtrace_handler;
sa.sa_flags = SA_RESTART | SA_SIGINFO;
int ret = 0;
for (i = 0; i < (sizeof(signals_trace) / sizeof(int)); ++i) {
if (sigaction(signals_trace[i], &sa, NULL) != 0) {
fprintf(stderr, "backtrace failed to set signal handler for %s(%d)!\n",
strsignal(signals_trace[i]), signals_trace[i]);
ret = -1;
break;
}
}
return ret;
}
void debug_backtrace_dump(void)
{
#if !defined(__UCLIBC__)
void* buffer[BT_SIZE];
int size = backtrace(buffer, BT_SIZE);
backtrace_symbols_detail(buffer, size);
#endif
}
static void debug_signal_handler(int signo, siginfo_t *siginfo, void *ucontext)
{
fprintf(stderr, "signal %d (%s) received from %d\n",
signo, strsignal(signo), siginfo->si_pid);
switch (signo) {
case SIGHUP:
break;
case SIGINT:
break;
case SIGQUIT:
break;
case SIGILL:
break;
case SIGTRAP:
break;
case SIGABRT://same as SIGIOT:
break;
case SIGBUS:
break;
case SIGFPE:
break;
case SIGKILL:
break;
case SIGUSR1:
break;
case SIGSEGV:
signal(signo, SIG_IGN);
break;
case SIGUSR2:
break;
case SIGPIPE:
break;
case SIGALRM:
break;
case SIGTERM:
break;
case SIGSTKFLT:
break;
case SIGCLD://same as SIGCHLD
break;
case SIGCONT:
break;
case SIGSTOP:
break;
case SIGTSTP:
break;
case SIGTTIN:
break;
case SIGTTOU:
break;
case SIGURG:
break;
case SIGXCPU:
break;
case SIGXFSZ:
break;
case SIGVTALRM:
break;
case SIGPROF:
break;
case SIGWINCH:
break;
case SIGPOLL://same as SIGIO
break;
case SIGPWR:
break;
case SIGSYS://same as SIGUNUSED
break;
default:
break;
}
}
int debug_signals_init(void)
{
int i;
struct sigaction sa;
memset(&sa, 0, sizeof(struct sigaction));
sa.sa_sigaction = debug_signal_handler;
sa.sa_flags = SA_RESTART | SA_SIGINFO;
sigemptyset(&sa.sa_mask);
for (i = 0; i < (sizeof(signals_all) / sizeof(int)); ++i) {
if (sigaction(signals_all[i], &sa, NULL) == -1) {
fprintf(stderr, "signal failed to set signal handler for %s(%d)!\n",
strsignal(signals_all[i]), signals_all[i]);
}
}
return 0;
}