forked from kikidong/avbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backtrace.cpp
104 lines (88 loc) · 2.56 KB
/
backtrace.cpp
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
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE
#endif
#include <ucontext.h>
#include <stdio.h>
#include <execinfo.h>
#include <signal.h>
#include <stdlib.h>
#include <iostream>
#include <cxxabi.h>
#include <string.h>
static void avbot_seg_handler(int sig_num, siginfo_t * info, void * ucontext)
{
ucontext_t * uc = (ucontext_t *)ucontext;
void * array[50];
int size = backtrace(array, 50);
void * caller_address = (void *)(array[2]);
std::cerr << "Oooooops, avbot has recived signal " << sig_num
<< " (" << strsignal(sig_num) << ") when accessing "
<< info->si_addr << " at address " << caller_address
<< std::endl << std::endl;
std::cerr << "Need help ? then past and mail the output below to microcai: \n-----Stack Dump Begin------" << std::endl;
char ** messages = backtrace_symbols(array, size);
// skip first stack frame (points here)
for(int i = 1; i < size && messages != NULL; ++i)
{
char *mangled_name = 0, *offset_begin = 0, *offset_end = 0;
// find parantheses and +address offset surrounding mangled name
for(char *p = messages[i]; *p; ++p)
{
if(*p == '(')
{
mangled_name = p;
}
else if(*p == '+')
{
offset_begin = p;
}
else if(*p == ')')
{
offset_end = p;
break;
}
}
// if the line could be processed, attempt to demangle the symbol
if(mangled_name && offset_begin && offset_end &&
mangled_name < offset_begin)
{
*mangled_name++ = '\0';
*offset_begin++ = '\0';
*offset_end++ = '\0';
int status;
char * real_name = abi::__cxa_demangle(mangled_name, 0, 0, &status);
// if demangling is successful, output the demangled function name
if(status == 0)
{
std::cerr << "[bt]: (" << i << ") " << messages[i] << " : "
<< real_name << "+" << offset_begin << offset_end
<< std::endl;
}
// otherwise, output the mangled function name
else
{
std::cerr << "[bt]: (" << i << ") " << messages[i] << " : "
<< mangled_name << "+" << offset_begin << offset_end
<< std::endl;
}
free(real_name);
}
// otherwise, print the whole line
else
{
std::cerr << "[bt]: (" << i << ") " << messages[i] << std::endl;
}
}
std::cerr << "-----Stack Dump Complete------" << std::endl;
std::cerr << std::endl;
exit(EXIT_FAILURE);
}
extern "C" void avbot_setup_seghandler()
{
struct sigaction handler = {0};
handler.sa_flags = SA_SIGINFO | SA_ONSTACK | SA_NODEFER;
handler.sa_sigaction = avbot_seg_handler ;
sigaction(SIGSEGV, &handler, NULL); // install our handler
sigaction(SIGABRT, &handler, NULL); // install our handler
}