forked from bloomberg/pystack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunwinder.h
More file actions
165 lines (135 loc) Β· 4.01 KB
/
unwinder.h
File metadata and controls
165 lines (135 loc) Β· 4.01 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
#pragma once
#include <functional>
#include <map>
#include <memory>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#include "elf_common.h"
#include "mem.h"
#include "native_frame.h"
namespace pystack {
class UnwinderError : public std::exception
{
public:
explicit UnwinderError(std::string error)
: d_error(std::move(error)){};
[[nodiscard]] const char* what() const noexcept override
{
return d_error.c_str();
}
private:
std::string d_error;
};
class Frame
{
public:
Frame(Dwarf_Addr pc, bool isActivation, std::optional<Dwarf_Word> stackPointer)
: pc(pc)
, isActivation(isActivation)
, stackPointer(stackPointer)
{
}
Dwarf_Addr pc;
bool isActivation;
std::optional<Dwarf_Word> stackPointer;
};
class ModuleCuDieRanges
{
public:
// Methods
Dwarf_Die* moduleAddrDie(Dwfl_Module* mod, Dwarf_Addr addr, Dwarf_Addr* bias);
private:
// Classes
class CuDieRanges
{
public:
// Classes
struct CuDieRange
{
Dwarf_Die* cuDie;
Dwarf_Addr bias;
Dwarf_Addr low;
Dwarf_Addr high;
inline bool contains(Dwarf_Addr addr) const
{
return low <= addr && addr < high;
}
};
// Methods
explicit CuDieRanges(Dwfl_Module* mod = nullptr);
Dwarf_Die* findDie(Dwarf_Addr addr, Dwarf_Addr* bias) const;
// Data members
std::vector<CuDieRange> d_ranges;
};
// Data members
std::unordered_map<Dwfl_Module*, CuDieRanges> d_die_range_maps;
};
class AbstractUnwinder
{
public:
// Methods
virtual remote_addr_t
getAddressforSymbol(const std::string& symbol, const std::string& modulename) const;
virtual std::vector<NativeFrame> unwindThread(pid_t tid) const = 0;
// Static methods
static std::string demangleSymbol(const std::string&);
virtual ~AbstractUnwinder() = default;
protected:
// Methods
virtual struct Dwfl* Dwfl() const = 0;
std::vector<NativeFrame> gatherFrames(const std::vector<Frame>& frames) const;
private:
// Enums
enum class StatusCode {
SUCCESS,
ERROR,
};
// Aliases
using Scopes = std::shared_ptr<Dwarf_Die>;
using ScopesInfo = std::pair<int, Scopes>;
// Methods
Dwarf_Die* dwarfModuleAddrDie(Dwarf_Addr pc_adjusted, Dwfl_Module* mod, Dwarf_Addr* bias) const;
std::pair<int, Scopes> dwarfGetScopes(Dwarf_Die* cudie, Dwarf_Addr pc_adjusted) const;
std::pair<int, Scopes> dwarfGetScopesDie(Dwarf_Die* die) const;
const char* getNonInlineSymbolName(Dwfl_Module* mod, Dwarf_Addr pc) const;
StatusCode gatherInlineFrames(
std::vector<NativeFrame>& native_frames,
const std::string& noninline_symname,
Dwarf_Addr pc,
Dwarf_Addr pc_corrected,
Dwarf_Die* cudie,
const char* mod_name) const;
// Data members
mutable ModuleCuDieRanges d_range_maps_cache;
mutable std::unordered_map<Dwarf_Addr, ScopesInfo> d_dwarf_getscopes_cache;
mutable std::unordered_map<void*, ScopesInfo> d_dwarf_getscopes_die_cache;
mutable std::unordered_map<Dwarf_Addr, const char*> d_symbol_by_pc_cache;
};
class Unwinder : public AbstractUnwinder
{
public:
// Constructors
explicit Unwinder(std::shared_ptr<ProcessAnalyzer> analyzer);
// Methods
virtual struct Dwfl* Dwfl() const override;
std::vector<NativeFrame> unwindThread(pid_t tid) const override;
private:
// Data members
std::shared_ptr<ProcessAnalyzer> d_analyzer;
};
class CoreFileUnwinder : public AbstractUnwinder
{
public:
// Constructors
explicit CoreFileUnwinder(std::shared_ptr<CoreFileAnalyzer> analyzer);
// Methods
virtual struct Dwfl* Dwfl() const override;
std::vector<int> getCoreTids() const;
std::vector<NativeFrame> unwindThread(pid_t tid) const override;
private:
// Data members
std::shared_ptr<CoreFileAnalyzer> d_analyzer;
};
} // namespace pystack