-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCallStack.h
More file actions
121 lines (101 loc) · 3.21 KB
/
Copy pathCallStack.h
File metadata and controls
121 lines (101 loc) · 3.21 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
/** ======================================================================+
+ Copyright @2020-2021 Arjun Ray
+ Released under MIT License
+ see https://mit-license.org
+========================================================================*/
#pragma once
#include <string>
#include <cstring>
#include <cstdlib>
#include <cxxabi.h>
#include <execinfo.h>
namespace Utility
{
// CallStack.
// Aggregates GNU compiler callstack.
//
class CallStack
{
enum { ST_SIZE = 100 }; // deeper callstacks are in trouble anyway
public:
CallStack()
: offset_(1)
, depth_(::backtrace( stack_, ST_SIZE ))
, symbols_(::backtrace_symbols( stack_, depth_ ))
{}
template<typename Action>
CallStack(Action&& action, size_t delta = 1)
: CallStack()
{
offset_ += delta;
for_each( action );
}
template<typename Action>
CallStack(Action const& action, size_t delta = 1)
: CallStack()
{
offset_ += delta;
for_each( action );
}
~CallStack() { ::free( symbols_ ); }
using iterator = char**;
iterator begin() const { return symbols_ + offset_; }
iterator end() const { return symbols_ + depth_; }
template<typename Action>
void for_each( Action const& action ) const
{
for ( auto const& _symbol : *this ) { action( _symbol ); }
}
private:
size_t offset_; // ignore our ctor(s) at top of call stack
int depth_;
char** symbols_;
void* stack_[ST_SIZE];
};
// Demangler.
// Tries to demangle a C++ ABI name.
//
class Demangler
{
public:
Demangler(std::string const& symbol)
: buffer_(abi::__cxa_demangle( symbol.c_str(), nullptr, nullptr, &status_ ))
{
switch ( status_ )
{
case 0 : str_ = std::string(buffer_); break;
case -1 : str_.assign( "(Memory allocation failure!)" ); break;
case -2 : str_.assign( symbol ); break; // invalid mangled name
default : str_.assign( "(Invalid argument in call!)" ); break;
}
}
Demangler(char const* begin, char const* end)
: Demangler(std::string(begin, end))
{}
~Demangler() { ::free( buffer_ ); }
operator std::string const& () const { return str_; }
private:
int status_{0};
char* buffer_;
std::string str_;
};
// SymbolAnalyser.
// Tries to determine whether a callstack entry has a name symbol.
//
class SymbolAnalyser
{
public:
SymbolAnalyser(char* symbol)
: begin_(::strchr( symbol, '(' ))
, end_(::strchr( symbol, '+' ))
, str_(symbol) // default
{
if ( begin_ and end_ ) { str_ = Demangler(begin_ + 1, end_); }
}
operator std::string const& () const { return str_; }
private:
char* begin_;
char* end_;
std::string str_;
};
} // namespace Utility