-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquickstack2.h
182 lines (154 loc) · 3.77 KB
/
quickstack2.h
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#ifndef quickstack2_h
#define quickstack2_h
#include <fstream>
#include <map>
#include <string>
#include <vector>
extern "C" char* cplus_demangle(const char* mangled, int options);
#define DBG(v, format, ...) \
if (debug_level >= v) { \
print_log(v, format, ##__VA_ARGS__); \
}
void print_log(int level, const char* format, ...);
void print_stack(const char* format, ...);
struct bfd_handle;
struct symbol_ent {
ulong addr;
const char* name;
symbol_ent(const ulong addr = 0, const char* name = nullptr)
: addr(addr), name(name) {}
};
inline bool operator<(const symbol_ent& lhs, const symbol_ent& rhs) {
return lhs.addr < rhs.addr;
}
struct symbol_table {
using symbols_type = std::vector<symbol_ent>;
symbols_type symbols;
ulong text_vma;
ulong text_size;
bfd_handle* bh;
symbol_table() : text_vma(0), text_size(0) {}
~symbol_table() {}
};
struct auto_fp {
explicit auto_fp(FILE* fp) : fp(fp) {}
~auto_fp() {
if (fp) {
fclose(fp);
}
}
operator FILE*() { return fp; }
auto_fp(const auto_fp&) = delete;
auto_fp& operator=(const auto_fp&) = delete;
private:
FILE* fp;
};
struct bfd_handle {
bfd_handle(const char* filename) : filename(filename) {
size = 0;
has_debug_symbols = false;
syms = NULL;
st = NULL;
debug_file = NULL;
}
void close_all();
void init(const char* file);
void debug_init();
void close();
void load_symbols(bool relative, ulong addr_begin);
void load_debug_section_if();
bfd* get();
bool has_debug() const;
void free_syms_if();
void free_st_if();
int get_file_line(ulong addr,
const char** file,
const char** function,
uint* lineno) const;
symbol_table* st;
const char* filename;
private:
uint size;
bool dynamic;
char* debug_file;
asection* dbgsec;
bool has_debug_symbols;
bfd* abfd;
asymbol** syms;
ulong symcnt;
};
struct symbol_table_map {
symbol_table_map() {}
~symbol_table_map() {
for (auto i = m.begin(); i != m.end(); ++i) {
symbol_table* st = i->second;
bfd_handle* bh = st->bh;
bh->close_all();
delete bh;
}
}
symbol_table* get(const std::string& path) {
auto i = m.find(path);
if (i != m.end()) {
return i->second;
}
return NULL;
}
void set(const std::string& path, symbol_table* st) { m[path] = st; }
symbol_table_map(const symbol_table_map&) = delete;
symbol_table_map& operator=(const symbol_table_map&) = delete;
private:
std::map<std::string, symbol_table*> m;
};
struct proc_map_ent {
ulong addr_begin;
ulong addr_size;
ulong offset;
std::string path;
symbol_table* stbl;
bool relative : 1;
bool is_vdso : 1;
proc_map_ent(const ulong addr_begin = 0)
: addr_begin(addr_begin),
addr_size(0),
offset(0),
stbl(nullptr),
relative(false),
is_vdso(false) {}
};
inline bool operator<(const proc_map_ent& lhs, const proc_map_ent& rhs) {
return lhs.addr_begin < rhs.addr_begin;
}
struct proc_info {
std::vector<proc_map_ent> maps;
timeval tv_start;
timeval tv_end;
double stall_time;
};
struct stopper_symbol {
std::string name;
ulong addr_begin;
ulong addr_end;
};
struct thread_info {
thread_info(const int tid) : tid(tid) {
const std::string file_path = "/proc/" + std::to_string(tid) + "/comm";
std::ifstream comm_file(file_path);
if (comm_file.is_open()) {
std::getline(comm_file, name);
comm_file.close();
} else {
name = "UNKNOWN";
}
}
static inline size_t max_name_len() {
return 16U;
}
int tid;
std::string name;
};
inline bool operator<(const thread_info& lhs, const thread_info& rhs) {
return lhs.tid < rhs.tid;
}
using thread_list = std::vector<thread_info>;
#endif