-
Notifications
You must be signed in to change notification settings - Fork 0
/
callstack.hh
84 lines (75 loc) · 1.89 KB
/
callstack.hh
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
#pragma once
#include "sys/syscall.h"
#include <execinfo.h>
#include <cxxabi.h>
#include <cstdlib>
#include <cstring>
#include <cstdio>
class FunctionName
{
public:
FunctionName(void* address)
{
m_lines = backtrace_symbols(&address, 1);
// Syntax: PATH/BINARY(SYMBOL+-OFFSET)[ABSOLUTE]
char* binary = strrchr(m_lines[0], '/');
char* mfunction = binary ? strchr(binary, '(') : nullptr;
char* offset = mfunction ? strchr(mfunction, '+') : nullptr;
offset = (mfunction && !offset) ? strchr(mfunction, '-') : offset;
char* end = offset ? strchr(offset, ')') : nullptr;
if (!end)
{
// The line doesn't have the syntax we expect. This happens
// for signal hanglers. In this case just put the original format
//
m_binary = nullptr;
m_function = nullptr;
m_offset = 0;
m_dfunction = nullptr;
}
else
{
m_binary = binary + 1;
*mfunction++ = 0;
*end = 0;
m_offset = 0;
sscanf(offset, "%llx", &m_offset);
*offset++ = 0;
m_dfunction = abi::__cxa_demangle(mfunction, nullptr, nullptr, nullptr);
m_function = m_dfunction ? m_dfunction : mfunction;
}
}
~FunctionName()
{
free(m_lines);
if (m_dfunction) free(m_dfunction);
}
bool IsResolved() { return m_function != nullptr; }
const char* Encoded() { return m_lines[0]; }
const char* Binary() { return m_binary; }
const char* Function() { return m_function; }
uint64_t Offset() { return m_offset; }
private:
const char* m_binary;
const char* m_function;
uint64_t m_offset;
char** m_lines; // allocated by backtrace_symbols
char* m_dfunction; // allocated by abi::__cxa_demangle
};
inline void PrintCallStack()
{
void* stack[20];
int size = backtrace(stack, 20);
for (int i = 0; i < size; i++)
{
FunctionName fn(stack[i]);
if (!fn.IsResolved())
{
fprintf(stderr, "%s\n", fn.Encoded());
}
else
{
fprintf(stderr, "[%s] %s 0x%llX\n", fn.Binary(), fn.Function(), fn.Offset());
}
}
}