Repository navigation
Expand file tree
/
Copy pathusyms.cpp
More file actions
452 lines (400 loc) · 13.2 KB
/
Copy pathusyms.cpp
File metadata and controls
452 lines (400 loc) · 13.2 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
#include "types.h"
#include <bcc/bcc_elf.h>
#include <bcc/bcc_syms.h>
#include <fstream>
#include <sstream>
#include "config.h"
#include "log.h"
#include "scopeguard.h"
#include "usyms.h"
#include "util/symbols.h"
#include "util/system.h"
namespace {
#ifdef HAVE_BLAZESYM
std::string stringify_addr(uint64_t addr, bool perf_mode)
{
std::ostringstream symbol;
symbol << reinterpret_cast<void *>(addr);
if (perf_mode)
symbol << " ([unknown])";
return symbol.str();
}
std::string stringify_sym(const char *name,
const blaze_symbolize_code_info *code_info,
uint64_t offset,
bool show_offset,
const char *sym_module,
bool perf_mode,
bool is_inlined)
{
std::ostringstream symbol;
if (is_inlined && !perf_mode) {
symbol << "[inlined] ";
}
symbol << name;
if (show_offset) {
symbol << "+" << offset;
}
if (perf_mode) {
if (sym_module != nullptr)
symbol << " (" << sym_module << ")";
else if (is_inlined)
symbol << " (inlined)";
else
symbol << " ([unknown])";
// Don't add the file/line if we're in perf mode
return symbol.str();
}
if (code_info != nullptr) {
if (code_info->dir != nullptr && code_info->file != nullptr) {
symbol << "@" << code_info->dir << "/" << code_info->file << ":"
<< code_info->line;
} else if (code_info->file != nullptr) {
symbol << "@" << code_info->file << ":" << code_info->line;
}
}
return symbol.str();
}
void add_symbols(const blaze_sym *sym,
bool show_offset,
bool perf_mode,
std::vector<std::string> &str_syms)
{
if (sym == nullptr || sym->name == nullptr) {
return;
}
const struct blaze_symbolize_inlined_fn *inlined;
// bpftrace prints stacks leaf first so the inlined functions
// need to come first in the list (and in reverse order)
for (int j = static_cast<int>(sym->inlined_cnt) - 1; j >= 0; j--) {
inlined = &sym->inlined[j];
if (inlined != nullptr) {
str_syms.push_back(stringify_sym(inlined->name,
&inlined->code_info,
0,
false,
nullptr,
perf_mode,
true));
}
}
str_syms.push_back(stringify_sym(sym->name,
&sym->code_info,
sym->offset,
show_offset,
sym->module,
perf_mode,
false));
}
#endif
} // namespace
namespace bpftrace {
Usyms::Usyms(const Config &config) : config_(config)
{
}
Usyms::~Usyms()
{
for (const auto &pair : exe_sym_) {
if (pair.second.second)
bcc_free_symcache(pair.second.second, pair.second.first);
}
for (const auto &pair : pid_sym_) {
if (pair.second)
bcc_free_symcache(pair.second, pair.first);
}
#ifdef HAVE_BLAZESYM
if (symbolizer_)
blaze_symbolizer_free(symbolizer_);
#endif
}
void Usyms::cache_bcc(const std::string &elf_file, std::optional<int> opt_pid)
{
const auto cache_type = config_.user_symbol_cache_type;
// preload symbol table for executable to make it available even if the
// binary is not present at symbol resolution time
// note: this only makes sense with ASLR disabled, since with ASLR offsets
// might be different
if (cache_type == UserSymbolCacheType::per_program &&
!symbol_table_cache_.contains(elf_file))
symbol_table_cache_[elf_file] = util::get_symbol_table_for_elf(elf_file);
if (cache_type == UserSymbolCacheType::per_pid) {
// preload symbol tables from running processes
// this allows symbol resolution for processes that are running at probe
// attach time (either a specific PID or all matching processes), but not at
// symbol resolution time, even with ASLR enabled, since BCC symcache
// records the offsets
auto pids = opt_pid.has_value() ? std::vector<int>{ *opt_pid }
: util::get_pids_for_program(elf_file);
if (!pids) {
LOG(WARNING) << "Unable to get pids for " << elf_file << ": "
<< pids.takeError();
return;
}
for (int pid : *pids) {
pid_sym_[pid] = bcc_symcache_new(pid, &get_symbol_opts());
}
}
}
#ifdef HAVE_BLAZESYM
struct blaze_symbolizer *Usyms::create_symbolizer() const
{
blaze_symbolizer_opts opts = {
.type_size = sizeof(opts),
.code_info = config_.show_debug_info,
.inlined_fns = config_.show_debug_info,
.demangle = config_.cpp_demangle,
};
return blaze_symbolizer_new_opts(&opts);
}
void Usyms::cache_blazesym(const std::string &elf_file,
std::optional<int> opt_pid)
{
auto cache_type = config_.user_symbol_cache_type;
if (cache_type == UserSymbolCacheType::none)
return;
if (symbolizer_ == nullptr) {
symbolizer_ = create_symbolizer();
if (symbolizer_ == nullptr)
return;
}
// preload symbol table for executable to make it available even if the
// binary is not present at symbol resolution time
// note: this only makes sense with ASLR disabled, since with ASLR offsets
// might be different
if (cache_type == UserSymbolCacheType::per_program) {
blaze_cache_src_elf cache = {
.type_size = sizeof(cache),
.path = elf_file.c_str(),
};
blaze_symbolize_cache_elf(symbolizer_, &cache);
}
if (cache_type == UserSymbolCacheType::per_pid) {
auto pids = opt_pid.has_value() ? std::vector<int>{ *opt_pid }
: util::get_pids_for_program(elf_file);
if (!pids) {
LOG(WARNING) << "Unable to get pids for " << elf_file << ": "
<< pids.takeError();
return;
}
for (int pid : *pids) {
blaze_cache_src_process cache = {
.type_size = sizeof(cache),
.pid = static_cast<uint32_t>(pid),
.cache_vmas = true,
};
blaze_symbolize_cache_process(symbolizer_, &cache);
}
}
}
#endif
void Usyms::cache(const std::string &elf_file, std::optional<int> pid)
{
#ifdef HAVE_BLAZESYM
if (config_.use_blazesym) {
cache_blazesym(elf_file, pid);
return;
}
#endif
cache_bcc(elf_file, pid);
}
std::string Usyms::resolve_bcc(uint64_t addr,
int32_t pid,
const std::string &pid_exe,
bool show_offset,
bool perf_mode)
{
const auto cache_type = config_.user_symbol_cache_type;
struct bcc_symbol usym;
std::ostringstream symbol;
void *psyms = nullptr;
if (cache_type == UserSymbolCacheType::per_program) {
if (!pid_exe.empty()) {
// try to resolve symbol directly from program file
// this might work when the process does not exist anymore, but cannot
// resolve all symbols, e.g. those in a dynamically linked library
std::map<uintptr_t, elf_symbol, std::greater<>> &symbol_table =
symbol_table_cache_.contains(pid_exe)
? symbol_table_cache_[pid_exe]
: (symbol_table_cache_[pid_exe] = util::get_symbol_table_for_elf(
pid_exe));
auto sym = symbol_table.lower_bound(addr);
// address has to be either the start of the symbol (for symbols of
// length 0) or in [start, end)
if (sym != symbol_table.end() &&
(addr == sym->second.start ||
(addr >= sym->second.start && addr < sym->second.end))) {
symbol << sym->second.name;
if (show_offset)
symbol << "+" << addr - sym->second.start;
if (perf_mode)
symbol << " (" << pid_exe << ")";
return symbol.str();
}
}
if (!exe_sym_.contains(pid_exe)) {
// not cached, create new ProcSyms cache
psyms = bcc_symcache_new(pid, &get_symbol_opts());
exe_sym_[pid_exe] = std::make_pair(pid, psyms);
} else {
psyms = exe_sym_[pid_exe].second;
}
} else if (cache_type == UserSymbolCacheType::per_pid) {
// cache user symbols per pid
if (!pid_sym_.contains(pid)) {
// not cached, create new ProcSyms cache
psyms = bcc_symcache_new(pid, &get_symbol_opts());
pid_sym_[pid] = psyms;
} else {
psyms = pid_sym_[pid];
}
} else {
// no user symbol caching, create new bcc cache
psyms = bcc_symcache_new(pid, &get_symbol_opts());
}
if (psyms && bcc_symcache_resolve(psyms, addr, &usym) == 0) {
SCOPE_EXIT
{
// This is a horrible hack to work around the fact that
// bcc does not tell if you if demangling succeeded.
// B/c if demangling failed, it returns a string that
// you cannot free.
//
// This relies on the fact that bcc will not change the
// `demangle_name = name` fallback. Since blazesym is
// coming (written 2/4/25), this should be fine for now.
if (usym.demangle_name != usym.name)
::free(const_cast<char *>(usym.demangle_name));
};
if (config_.cpp_demangle)
symbol << usym.demangle_name;
else
symbol << usym.name;
if (show_offset)
symbol << "+" << usym.offset;
if (perf_mode)
symbol << " (" << usym.module << ")";
} else {
symbol << reinterpret_cast<void *>(addr);
if (perf_mode)
symbol << " ([unknown])";
}
if (cache_type == UserSymbolCacheType::none)
bcc_free_symcache(psyms, pid);
return symbol.str();
}
#ifdef HAVE_BLAZESYM
std::vector<std::string> Usyms::resolve_blazesym_impl(
uint64_t addr,
int32_t pid,
const std::string &pid_exe,
bool show_offset,
bool perf_mode,
bool show_debug_info)
{
std::vector<std::string> str_syms;
const blaze_sym *sym;
if (symbolizer_ == nullptr) {
symbolizer_ = create_symbolizer();
if (symbolizer_ == nullptr)
return str_syms;
}
auto cache_type = config_.user_symbol_cache_type;
SCOPE_EXIT
{
if (cache_type == UserSymbolCacheType::none) {
blaze_symbolizer_free(symbolizer_);
symbolizer_ = nullptr;
}
};
if (cache_type == UserSymbolCacheType::per_program) {
if (!pid_exe.empty()) {
blaze_symbolize_src_elf src = {
.type_size = sizeof(src),
.path = pid_exe.c_str(),
.debug_syms = show_debug_info,
};
const blaze_syms *syms = blaze_symbolize_elf_virt_offsets(
symbolizer_, &src, &addr, 1);
if (syms == nullptr) {
return str_syms;
}
SCOPE_EXIT
{
blaze_syms_free(syms);
};
sym = &syms->syms[0];
add_symbols(sym, show_offset, perf_mode, str_syms);
}
return str_syms;
}
// We check that /proc/<pid>/maps has content rather than that /proc/<pid>
// merely exists: a process that has exited but not yet been reaped lingers
// as a zombie with an empty maps file and no usable map_files entries.
// See blazesym docs why setting no_map_files to true is discouraged.
bool no_map_files = false;
if (cache_type == UserSymbolCacheType::per_pid) {
std::ifstream maps("/proc/" + std::to_string(pid) + "/maps");
no_map_files = !maps.good() ||
maps.peek() == std::ifstream::traits_type::eof();
}
blaze_symbolize_src_process src = {
.type_size = sizeof(src),
.pid = static_cast<uint32_t>(pid),
.debug_syms = show_debug_info,
.perf_map = true,
.no_map_files = no_map_files,
};
const blaze_syms *syms = blaze_symbolize_process_abs_addrs(
symbolizer_, &src, &addr, 1);
if (syms == nullptr)
return str_syms;
SCOPE_EXIT
{
blaze_syms_free(syms);
};
sym = &syms->syms[0];
add_symbols(sym, show_offset, perf_mode, str_syms);
return str_syms;
}
std::vector<std::string> Usyms::resolve_blazesym(uint64_t addr,
int32_t pid,
const std::string &pid_exe,
bool show_offset,
bool perf_mode,
bool show_debug_info)
{
auto syms = resolve_blazesym_impl(
addr, pid, pid_exe, show_offset, perf_mode, show_debug_info);
if (syms.empty()) {
syms.push_back(stringify_addr(addr, perf_mode));
}
return syms;
}
#endif
std::vector<std::string> Usyms::resolve(uint64_t addr,
int32_t pid,
const std::string &pid_exe,
bool show_offset,
bool perf_mode,
[[maybe_unused]] bool show_debug_info)
{
#ifdef HAVE_BLAZESYM
if (config_.use_blazesym)
return resolve_blazesym(
addr, pid, pid_exe, show_offset, perf_mode, show_debug_info);
#endif
return std::vector<std::string>{
resolve_bcc(addr, pid, pid_exe, show_offset, perf_mode)
};
}
struct bcc_symbol_option &Usyms::get_symbol_opts()
{
static struct bcc_symbol_option symopts = {
.use_debug_file = 1,
.check_debug_file_crc = 1,
.lazy_symbolize = config_.lazy_symbolication ? 1 : 0,
.use_symbol_type = BCC_SYM_ALL_TYPES,
};
return symopts;
}
} // namespace bpftrace