-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStacktrace.cpp
More file actions
441 lines (411 loc) · 14 KB
/
Stacktrace.cpp
File metadata and controls
441 lines (411 loc) · 14 KB
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
/**
* \file Stacktrace.cpp
* \brief
*
* \todo
*
* Compile with:
* - on linux: gcc -g stack_traces.c
* - on OS X: gcc -g -fno-pie stack_traces.c
* - on windows: gcc -g stack_traces.c -limagehlp
*/
//-------------------------------------------------------------------------------------------------
#include <StdStream/StdStream.h>
#include <StdTest/StdTest.h>
#include <Stl.h>
#ifdef _WIN32
#include <windows.h>
#include <imagehlp.h>
#else
#include <err.h>
#include <execinfo.h>
#endif
//-------------------------------------------------------------------------------------------------
// void almost_c99_signal_handler(int sig)
// {
// switch(sig)
// {
// case SIGABRT:
// fputs("Caught SIGABRT: usually caused by an abort() or assert()\n", stderr);
// break;
// case SIGFPE:
// fputs("Caught SIGFPE: arithmetic exception, such as divide by zero\n", stderr);
// break;
// case SIGILL:
// fputs("Caught SIGILL: illegal instruction\n", stderr);
// break;
// case SIGINT:
// fputs("Caught SIGINT: interactive attention signal, probably a ctrl+c\n", stderr);
// break;
// case SIGSEGV:
// fputs("Caught SIGSEGV: segfault\n", stderr);
// break;
// case SIGTERM:
// default:
// fputs("Caught SIGTERM: a termination request was sent to the program\n", stderr);
// break;
// }
// _Exit(1);
// }
//-------------------------------------------------------------------------------------------------
// void set_signal_handler()
// {
// signal(SIGABRT, almost_c99_signal_handler);
// signal(SIGFPE, almost_c99_signal_handler);
// signal(SIGILL, almost_c99_signal_handler);
// signal(SIGINT, almost_c99_signal_handler);
// signal(SIGSEGV, almost_c99_signal_handler);
// signal(SIGTERM, almost_c99_signal_handler);
// }
//-------------------------------------------------------------------------------------------------
static char const * icky_global_program_name;
//-------------------------------------------------------------------------------------------------
/* Resolve symbol name and source location given the path to the executable
and an address */
int
addr2line(char const * const program_name, void const * const addr)
{
char addr2line_cmd[512] = {0};
/* have addr2line map the address to the relent line in the code */
#ifdef __APPLE__
/* apple does things differently... */
sprintf(addr2line_cmd,"atos -o %.256s %p", program_name, addr);
#else
sprintf(addr2line_cmd,"addr2line -f -p -e %.256s %p", program_name, addr);
#endif
return system(addr2line_cmd);
}
//-------------------------------------------------------------------------------------------------
#ifdef _WIN32
void
windows_print_stacktrace(CONTEXT* context)
{
SymInitialize(GetCurrentProcess(), 0, true);
STACKFRAME frame = { 0 };
/* setup initial stack frame */
frame.AddrPC.Offset = context->Eip;
frame.AddrPC.Mode = AddrModeFlat;
frame.AddrStack.Offset = context->Esp;
frame.AddrStack.Mode = AddrModeFlat;
frame.AddrFrame.Offset = context->Ebp;
frame.AddrFrame.Mode = AddrModeFlat;
while (StackWalk(IMAGE_FILE_MACHINE_I386 ,
GetCurrentProcess(),
GetCurrentThread(),
&frame,
context,
0,
SymFunctionTableAccess,
SymGetModuleBase,
0 ) )
{
addr2line(icky_global_program_name, (void*)frame.AddrPC.Offset);
}
SymCleanup( GetCurrentProcess() );
}
//-------------------------------------------------------------------------------------------------
LONG WINAPI
windows_exception_handler(EXCEPTION_POINTERS * ExceptionInfo)
{
switch(ExceptionInfo->ExceptionRecord->ExceptionCode)
{
case EXCEPTION_ACCESS_VIOLATION:
fputs("Error: EXCEPTION_ACCESS_VIOLATION\n", stderr);
break;
case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
fputs("Error: EXCEPTION_ARRAY_BOUNDS_EXCEEDED\n", stderr);
break;
case EXCEPTION_BREAKPOINT:
fputs("Error: EXCEPTION_BREAKPOINT\n", stderr);
break;
case EXCEPTION_DATATYPE_MISALIGNMENT:
fputs("Error: EXCEPTION_DATATYPE_MISALIGNMENT\n", stderr);
break;
case EXCEPTION_FLT_DENORMAL_OPERAND:
fputs("Error: EXCEPTION_FLT_DENORMAL_OPERAND\n", stderr);
break;
case EXCEPTION_FLT_DIVIDE_BY_ZERO:
fputs("Error: EXCEPTION_FLT_DIVIDE_BY_ZERO\n", stderr);
break;
case EXCEPTION_FLT_INEXACT_RESULT:
fputs("Error: EXCEPTION_FLT_INEXACT_RESULT\n", stderr);
break;
case EXCEPTION_FLT_INVALID_OPERATION:
fputs("Error: EXCEPTION_FLT_INVALID_OPERATION\n", stderr);
break;
case EXCEPTION_FLT_OVERFLOW:
fputs("Error: EXCEPTION_FLT_OVERFLOW\n", stderr);
break;
case EXCEPTION_FLT_STACK_CHECK:
fputs("Error: EXCEPTION_FLT_STACK_CHECK\n", stderr);
break;
case EXCEPTION_FLT_UNDERFLOW:
fputs("Error: EXCEPTION_FLT_UNDERFLOW\n", stderr);
break;
case EXCEPTION_ILLEGAL_INSTRUCTION:
fputs("Error: EXCEPTION_ILLEGAL_INSTRUCTION\n", stderr);
break;
case EXCEPTION_IN_PAGE_ERROR:
#include <signal.h>
#include <iostream>
#include <cstdlib>tderr);
break;
case EXCEPTION_INT_DIVIDE_BY_ZERO:
fputs("Error: EXCEPTION_INT_DIVIDE_BY_ZERO\n", stderr);
break;
case EXCEPTION_INT_OVERFLOW:
fputs("Error: EXCEPTION_INT_OVERFLOW\n", stderr);
break;
case EXCEPTION_INVALID_DISPOSITION:
fputs("Error: EXCEPTION_INVALID_DISPOSITION\n", stderr);
break;
case EXCEPTION_NONCONTINUABLE_EXCEPTION:
fputs("Error: EXCEPTION_NONCONTINUABLE_EXCEPTION\n", stderr);
break;
case EXCEPTION_PRIV_INSTRUCTION:
fputs("Error: EXCEPTION_PRIV_INSTRUCTION\n", stderr);
break;
case EXCEPTION_SINGLE_STEP:
fputs("Error: EXCEPTION_SINGLE_STEP\n", stderr);
break;
case EXCEPTION_STACK_OVERFLOW:
fputs("Error: EXCEPTION_STACK_OVERFLOW\n", stderr);
break;
default:
fputs("Error: Unrecognized Exception\n", stderr);
break;
}
fflush(stderr);
/* If this is a stack overflow then we can't walk the stack, so just show
where the error happened */
if (EXCEPTION_STACK_OVERFLOW != ExceptionInfo->ExceptionRecord->ExceptionCode)
{
windows_print_stacktrace(ExceptionInfo->ContextRecord);
}
else
{
addr2line(icky_global_program_name, (void*)ExceptionInfo->ContextRecord->Eip);
}
return EXCEPTION_EXECUTE_HANDLER;
}
//-------------------------------------------------------------------------------------------------
void
set_signal_handler()
{
SetUnhandledExceptionFilter(windows_exception_handler);
}
//-------------------------------------------------------------------------------------------------
#else
//-------------------------------------------------------------------------------------------------
#define MAX_STACK_FRAMES 64
static void *stack_traces[MAX_STACK_FRAMES];
//-------------------------------------------------------------------------------------------------
void
posix_print_stack_trace()
{
int i, trace_size = 0;
char **messages = (char **)NULL;
trace_size = backtrace(stack_traces, MAX_STACK_FRAMES);
messages = backtrace_symbols(stack_traces, trace_size);
/* skip the first couple stack frames (as they are this function and
our handler) and also skip the last frame as it's (always?) junk. */
// for (i = 3; i < (trace_size - 1); ++i)
for (i = 0; i < trace_size; ++i) // we'll use this for now so you can see what's going on
{
if (addr2line(icky_global_program_name, stack_traces[i]) != 0)
{
printf(" error determining line # for: %s\n", messages[i]);
}
}
if (messages) { free(messages); }
}
//-------------------------------------------------------------------------------------------------
void
posix_signal_handler(int sig, siginfo_t *siginfo, void *context)
{
(void)context;
switch(sig)
{
case SIGSEGV:
fputs("Caught SIGSEGV: Segmentation Fault\n", stderr);
break;
case SIGINT:
fputs("Caught SIGINT: Interactive attention signal, (usually ctrl+c)\n", stderr);
break;
case SIGFPE:
switch(siginfo->si_code)
{
case FPE_INTDIV:
fputs("Caught SIGFPE: (integer divide by zero)\n", stderr);
break;
case FPE_INTOVF:
fputs("Caught SIGFPE: (integer overflow)\n", stderr);
break;
case FPE_FLTDIV:
fputs("Caught SIGFPE: (floating-point divide by zero)\n", stderr);
break;
case FPE_FLTOVF:
fputs("Caught SIGFPE: (floating-point overflow)\n", stderr);
break;
case FPE_FLTUND:
fputs("Caught SIGFPE: (floating-point underflow)\n", stderr);
break;
case FPE_FLTRES:
fputs("Caught SIGFPE: (floating-point inexact result)\n", stderr);
break;
case FPE_FLTINV:
fputs("Caught SIGFPE: (floating-point invalid operation)\n", stderr);
break;
case FPE_FLTSUB:
fputs("Caught SIGFPE: (subscript out of range)\n", stderr);
break;
default:
fputs("Caught SIGFPE: Arithmetic Exception\n", stderr);
break;
}
break;
case SIGILL:
switch(siginfo->si_code)
{
case ILL_ILLOPC:
fputs("Caught SIGILL: (illegal opcode)\n", stderr);
break;
case ILL_ILLOPN:
fputs("Caught SIGILL: (illegal operand)\n", stderr);
break;
case ILL_ILLADR:
fputs("Caught SIGILL: (illegal addressing mode)\n", stderr);
break;
case ILL_ILLTRP:
fputs("Caught SIGILL: (illegal trap)\n", stderr);
break;
case ILL_PRVOPC:
fputs("Caught SIGILL: (privileged opcode)\n", stderr);
break;
case ILL_PRVREG:
fputs("Caught SIGILL: (privileged register)\n", stderr);
break;
case ILL_COPROC:
fputs("Caught SIGILL: (coprocessor error)\n", stderr);
break;
case ILL_BADSTK:
fputs("Caught SIGILL: (internal stack error)\n", stderr);
break;
default:
fputs("Caught SIGILL: Illegal Instruction\n", stderr);
break;
}
break;
case SIGTERM:
fputs("Caught SIGTERM: a termination request was sent to the program\n", stderr);
break;
case SIGABRT:
fputs("Caught SIGABRT: usually caused by an abort() or assert()\n", stderr);
break;
default:
break;
}
posix_print_stack_trace();
_Exit(1);
}
//-------------------------------------------------------------------------------------------------
constexpr std::size_t CUSTOM_SIGSTKSZ = 8192; // as SIGSTKSZ
static uint8_t alternate_stack[CUSTOM_SIGSTKSZ];
//-------------------------------------------------------------------------------------------------
void
set_signal_handler()
{
/* setup alternate stack */
{
stack_t ss = {};
/* malloc is usually used here, I'm not 100% sure my static allocation
is valid but it seems to work just fine. */
ss.ss_sp = (void*)alternate_stack;
ss.ss_size = CUSTOM_SIGSTKSZ;
ss.ss_flags = 0;
if (sigaltstack(&ss, NULL) != 0) { err(1, "sigaltstack"); }
}
/* register our signal handlers */
{
struct sigaction sig_action = {};
sig_action.sa_sigaction = posix_signal_handler;
sigemptyset(&sig_action.sa_mask);
#ifdef __APPLE__
/* for some reason we backtrace() doesn't work on osx
when we use an alternate stack */
sig_action.sa_flags = SA_SIGINFO;
#else
sig_action.sa_flags = SA_SIGINFO | SA_ONSTACK;
#endif
if (sigaction(SIGSEGV, &sig_action, NULL) != 0) { err(1, "sigaction"); }
if (sigaction(SIGFPE, &sig_action, NULL) != 0) { err(1, "sigaction"); }
if (sigaction(SIGINT, &sig_action, NULL) != 0) { err(1, "sigaction"); }
if (sigaction(SIGILL, &sig_action, NULL) != 0) { err(1, "sigaction"); }
if (sigaction(SIGTERM, &sig_action, NULL) != 0) { err(1, "sigaction"); }
if (sigaction(SIGABRT, &sig_action, NULL) != 0) { err(1, "sigaction"); }
}
}
//-------------------------------------------------------------------------------------------------
#endif
//-------------------------------------------------------------------------------------------------
int divide_by_zero();
void cause_segfault();
void stack_overflow();
void infinite_loop();
void illegal_instruction();
void cause_calamity();
//-------------------------------------------------------------------------------------------------
int main(int, char **argv)
{
/* store off program path so we can use it later */
icky_global_program_name = argv[0];
::set_signal_handler();
::cause_calamity();
puts("OMG! Nothing bad happend!");
return 0;
}
//-------------------------------------------------------------------------------------------------
void cause_calamity()
{
/* uncomment one of the following error conditions to cause a calamity of
your choosing! */
// (void)divide_by_zero();
cause_segfault();
// assert(false);
// infinite_loop();
// illegal_instruction();
// stack_overflow();
}
//-------------------------------------------------------------------------------------------------
int divide_by_zero()
{
int a = 1;
int b = 0;
return a / b;
}
//-------------------------------------------------------------------------------------------------
void cause_segfault()
{
int * p = (int*)0x12345678;
*p = 0;
}
//-------------------------------------------------------------------------------------------------
void stack_overflow()
{
int foo[1000];
(void)foo;
stack_overflow();
}
//-------------------------------------------------------------------------------------------------
/* break out with ctrl+c to test SIGINT handling */
void infinite_loop()
{
while(1) {};
}
//-------------------------------------------------------------------------------------------------
void illegal_instruction()
{
/* I couldn't find an easy way to cause this one, so I'm cheating */
raise(SIGILL);
}
//-------------------------------------------------------------------------------------------------