forked from yoshinorim/quickstack
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickstack.h
More file actions
Latest commit
235 lines (205 loc) · 5.08 KB
/
Copy pathquickstack.h
File metadata and controls
235 lines (205 loc) · 5.08 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#ifndef quickstack_h
#definequickstack_h
#definePACKAGE1
#definePACKAGE_VERSION1
#include<bfd.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
#include<stdarg.h>
#include<errno.h>
#include<sys/ptrace.h>
#include<sys/user.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<sys/reg.h>
#include<sys/time.h>
#include<unistd.h>
#include<limits.h>
#include<iostream>
#include<fstream>
#include<dirent.h>
#include<vector>
#include<map>
#include<string>
#include<sstream>
#include<list>
#include<algorithm>
#include<libelf.h>
using std::string;
using std::vector;
using std::map;
// From binutils/include/demangle.h
#defineDMGL_PARAMS (1 << 0) /* Include function args */
#defineDMGL_ANSI (1 << 1) /* Include const, volatile, etc */
#defineDMGL_VERBOSE (1 << 3) /* Include implementation details. */
#defineDMGL_TYPES (1 << 4) /* Also try to demangle type encodings. */
extern"C"char* cplus_demangle(constchar* mangled, int options);
#defineDBG(v, format, ...) \
if (debug_level >= v) { \
print_log(v, format, ##__VA_ARGS__); \
}
voidprint_log(int level, constchar* format, ...);
voidprint_stack(constchar* format, ...);
structbfd_handle;
structsymbol_ent {
ulong addr;
string name;
symbol_ent(const ulong addr = 0, const string& name = "")
: addr(addr), name(name) {}
};
inlinebooloperator<(const symbol_ent& lhs, const symbol_ent& rhs) {
return lhs.addr < rhs.addr;
}
structsymbol_table {
typedef vector<symbol_ent> symbols_type;
symbols_type symbols;
ulong text_vma;
ulong text_size;
bfd_handle* bh;
symbol_table() : text_vma(0), text_size(0) {}
~symbol_table() {}
};
structauto_fp {
explicitauto_fp(FILE* fp) : fp(fp) {}
~auto_fp() {
if (fp) {
fclose(fp);
}
}
operatorFILE*() { return fp; }
private:
FILE* fp;
auto_fp(const auto_fp&);
auto_fp& operator=(const auto_fp&);
};
structbfd_handle {
bfd_handle(constchar* filename) : filename(filename) {
size = 0;
has_debug_symbols = false;
syms = NULL;
st = NULL;
debug_file = NULL;
}
voidclose_all();
voidinit(constchar* file);
voiddebug_init();
voidclose();
voidload_symbols(bool relative, ulong addr_begin);
voidload_debug_section_if();
bfd* get();
boolhas_debug() const;
voidfree_syms_if();
voidfree_st_if();
intget_file_line(ulong addr,
constchar** file,
constchar** function,
uint* lineno) const;
symbol_table* st;
constchar* filename;
private:
uint size;
bool dynamic;
char* debug_file;
asection* dbgsec;
bool has_debug_symbols;
bfd* abfd;
asymbol** syms;
ulong symcnt;
};
structsymbol_table_map {
symbol_table_map() {}
~symbol_table_map() {
for (m_type::iterator 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) {
m_type::iterator i = m.find(path);
if (i != m.end()) {
return i->second;
}
returnNULL;
}
voidset(const std::string& path, symbol_table* st) { m[path] = st; }
private:
typedef map<std::string, symbol_table*> m_type;
m_type m;
private:
symbol_table_map(const symbol_table_map&);
symbol_table_map& operator=(const symbol_table_map&);
};
structproc_map_ent {
ulong addr_begin;
ulong addr_size;
ulong offset;
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) {}
};
inlinebooloperator<(const proc_map_ent& lhs, const proc_map_ent& rhs) {
return lhs.addr_begin < rhs.addr_begin;
}
structproc_info {
typedef vector<proc_map_ent> maps_type;
maps_type maps;
structtimeval tv_start;
structtimeval tv_end;
double stall_time;
};
typedefstructstopper_symbol {
string name;
ulong addr_begin;
ulong addr_end;
} stopper_symbol;
typedefstructthread_info {
thread_info(constint tid) : tid(tid) {
const 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";
}
}
staticinlinesize_tmax_name_len() {
return16U;
}
int tid;
string name;
} thread_info;
inlinebooloperator<(const thread_info& lhs, const thread_info& rhs) {
return lhs.tid < rhs.tid;
}
typedefstructvector<thread_info> thread_list;
externint target_pid;
externint debug_level;
externconstchar* debug_dir;
extern stopper_symbol stopper[];
externint num_stopper_symbol;
externint print_arg;
externint single_line;
externint trace_multiple_procs;
externint basename_only;
externint max_ptrace_calls;
externint frame_check;
externint debug_print_time_level;
externconstchar* stack_out;
externint flush_log;
externint timeout_seconds;
externbool lock_all;
#endif