-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstackTrace.cpp
More file actions
62 lines (48 loc) · 1.52 KB
/
stackTrace.cpp
File metadata and controls
62 lines (48 loc) · 1.52 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
#include "stackTrace.h"
stackTrace::stackTrace() : std::ostringstream()
{
unw_cursor_t cursor;
unw_context_t context;
/// Initialize cursor to current frame for local unwinding.
unw_getcontext(&context);
unw_init_local(&cursor, &context);
*this << std::endl << ">>>>>>>>>Start of stackdump <<<<<<<<<<<<" << std::endl;
/// Unwind frames one by one, going up the frame stack.
while (unw_step(&cursor) > 0)
{
unw_word_t offset, pc;
unw_get_reg(&cursor, UNW_REG_IP, &pc);
if (pc == 0)
{
break;
}
*this << "\t0x0" << std::hex << pc << " " ;
char* line = (char *) calloc(1,LINSIZ);
if (unw_get_proc_name(&cursor, line, LINSIZ, &offset) == 0)
{
int status;
char* demangled = abi::__cxa_demangle(line, nullptr, nullptr, &status);
if (status == 0)
{
*this << demangled << " 0x0" << std::hex << offset << std::endl;
}else {
*this << " 0x0" << std::hex << offset << std::endl;
}
std::free(line);
std::free(demangled);
line=nullptr;
demangled = nullptr;
}
else
{
*this << "\t -- error: unable to obtain symbol name for this frame" << std::endl;
}
if (line != nullptr)
free(line);
} /// end while
*this << ">>>>>>>>>>>>>>> End of stackdump <<<<<<<<<<<<<" << std::endl;
}
stackTrace::~stackTrace()
{
//dtor
}