forked from iovisor/bcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bcc_syms.cc
531 lines (455 loc) · 15 KB
/
bcc_syms.cc
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
/*
* Copyright (c) 2016 GitHub, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <algorithm>
#include <cxxabi.h>
#include <cstring>
#include <fcntl.h>
#include <linux/elf.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <cstdio>
#include "bcc_elf.h"
#include "bcc_perf_map.h"
#include "bcc_proc.h"
#include "bcc_syms.h"
#include "common.h"
#include "vendor/tinyformat.hpp"
#include "syms.h"
ino_t ProcStat::getinode_() {
struct stat s;
return (!stat(procfs_.c_str(), &s)) ? s.st_ino : -1;
}
bool ProcStat::is_stale() {
ino_t cur_inode = getinode_();
return (cur_inode > 0) && (cur_inode != inode_);
}
ProcStat::ProcStat(int pid)
: procfs_(tfm::format("/proc/%d/exe", pid)), inode_(getinode_()) {}
void KSyms::_add_symbol(const char *symname, uint64_t addr, void *p) {
KSyms *ks = static_cast<KSyms *>(p);
ks->syms_.emplace_back(symname, addr);
}
void KSyms::refresh() {
if (syms_.empty()) {
bcc_procutils_each_ksym(_add_symbol, this);
std::sort(syms_.begin(), syms_.end());
}
}
bool KSyms::resolve_addr(uint64_t addr, struct bcc_symbol *sym, bool demangle) {
refresh();
if (syms_.empty()) {
sym->name = nullptr;
sym->demangle_name = nullptr;
sym->module = nullptr;
sym->offset = 0x0;
return false;
}
auto it = std::upper_bound(syms_.begin(), syms_.end(), Symbol("", addr)) - 1;
sym->name = (*it).name.c_str();
if (demangle)
sym->demangle_name = sym->name;
sym->module = "kernel";
sym->offset = addr - (*it).addr;
return true;
}
bool KSyms::resolve_name(const char *_unused, const char *name,
uint64_t *addr) {
refresh();
if (syms_.size() != symnames_.size()) {
symnames_.clear();
for (Symbol &sym : syms_) {
symnames_[sym.name] = sym.addr;
}
}
auto it = symnames_.find(name);
if (it == symnames_.end())
return false;
*addr = it->second;
return true;
}
ProcSyms::ProcSyms(int pid, struct bcc_symbol_option *option)
: pid_(pid), procstat_(pid), mount_ns_instance_(new ProcMountNS(pid_)) {
if (option)
std::memcpy(&symbol_option_, option, sizeof(bcc_symbol_option));
else
symbol_option_ = {
.use_debug_file = 1,
.check_debug_file_crc = 1,
.use_symbol_type = (1 << STT_FUNC) | (1 << STT_GNU_IFUNC)
};
load_modules();
}
int ProcSyms::_add_load_sections(uint64_t v_addr, uint64_t mem_sz,
uint64_t file_offset, void *payload) {
auto module = static_cast<Module *>(payload);
module->add_range(v_addr, v_addr + mem_sz);
return 0;
}
void ProcSyms::load_exe() {
std::string exe = ebpf::get_pid_exe(pid_);
Module module(exe.c_str(), mount_ns_instance_.get(), &symbol_option_);
if (!module.init())
return;
if (module.type_ != ModuleType::EXEC)
return;
ProcMountNSGuard g(mount_ns_instance_.get());
bcc_elf_foreach_load_section(exe.c_str(), &_add_load_sections, &module);
if (!module.ranges_.empty())
modules_.emplace_back(std::move(module));
}
void ProcSyms::load_modules() {
load_exe();
bcc_procutils_each_module(pid_, _add_module, this);
}
void ProcSyms::refresh() {
modules_.clear();
mount_ns_instance_.reset(new ProcMountNS(pid_));
load_modules();
procstat_.reset();
}
int ProcSyms::_add_module(const char *modname, uint64_t start, uint64_t end,
uint64_t offset, bool check_mount_ns, void *payload) {
ProcSyms *ps = static_cast<ProcSyms *>(payload);
auto it = std::find_if(
ps->modules_.begin(), ps->modules_.end(),
[=](const ProcSyms::Module &m) { return m.name_ == modname; });
if (it == ps->modules_.end()) {
auto module = Module(
modname, check_mount_ns ? ps->mount_ns_instance_.get() : nullptr,
&ps->symbol_option_);
if (module.init())
it = ps->modules_.insert(ps->modules_.end(), std::move(module));
else
return 0;
}
it->add_range(start, end);
return 0;
}
bool ProcSyms::resolve_addr(uint64_t addr, struct bcc_symbol *sym,
bool demangle) {
if (procstat_.is_stale())
refresh();
sym->module = nullptr;
sym->name = nullptr;
sym->demangle_name = nullptr;
sym->offset = 0x0;
const char *original_module = nullptr;
uint64_t offset;
for (Module &mod : modules_) {
if (mod.contains(addr, offset)) {
bool res = mod.find_addr(offset, sym);
if (demangle) {
if (sym->name)
sym->demangle_name =
abi::__cxa_demangle(sym->name, nullptr, nullptr, nullptr);
if (!sym->demangle_name)
sym->demangle_name = sym->name;
}
// If we have a match, return right away. But if we don't have a match in
// this module, we might have a match in the perf map (even though the
// module itself doesn't have symbols). Wait until we see the perf map if
// any, but keep the original module name for reporting.
if (res) {
// If we have already seen this module, report the original name rather
// than the perf map name:
if (original_module)
sym->module = original_module;
return res;
} else if (mod.type_ != ModuleType::PERF_MAP) {
// Record the module to which this symbol belongs, so that even if it's
// later found using a perf map, we still report the right module name.
original_module = mod.name_.c_str();
}
}
}
return false;
}
bool ProcSyms::resolve_name(const char *module, const char *name,
uint64_t *addr) {
if (procstat_.is_stale())
refresh();
for (Module &mod : modules_) {
if (mod.name_ == module)
return mod.find_name(name, addr);
}
return false;
}
ProcSyms::Module::Module(const char *name, ProcMountNS *mount_ns,
struct bcc_symbol_option *option)
: name_(name),
loaded_(false),
mount_ns_(mount_ns),
symbol_option_(option),
type_(ModuleType::UNKNOWN) {}
bool ProcSyms::Module::init() {
ProcMountNSGuard g(mount_ns_);
int elf_type = bcc_elf_get_type(name_.c_str());
if (elf_type >= 0) {
if (elf_type == ET_EXEC) {
type_ = ModuleType::EXEC;
return true;
}
if (elf_type == ET_DYN) {
type_ = ModuleType::SO;
return true;
}
return false;
}
if (bcc_is_perf_map(name_.c_str()) == 1) {
type_ = ModuleType::PERF_MAP;
return true;
}
return false;
}
int ProcSyms::Module::_add_symbol(const char *symname, uint64_t start,
uint64_t size, void *p) {
Module *m = static_cast<Module *>(p);
auto res = m->symnames_.emplace(symname);
m->syms_.emplace_back(&*(res.first), start, size);
return 0;
}
void ProcSyms::Module::load_sym_table() {
if (loaded_)
return;
loaded_ = true;
ProcMountNSGuard g(mount_ns_);
if (type_ == ModuleType::PERF_MAP)
bcc_perf_map_foreach_sym(name_.c_str(), _add_symbol, this);
if (type_ == ModuleType::EXEC || type_ == ModuleType::SO)
bcc_elf_foreach_sym(name_.c_str(), _add_symbol, symbol_option_, this);
std::sort(syms_.begin(), syms_.end());
}
void ProcSyms::Module::add_range(uint64_t st, uint64_t en) {
if (!ranges_.empty()) {
Range &last = ranges_.back();
if (st >= last.start && st <= last.end) {
last.end = std::max(en, last.end);
return;
}
}
ranges_.emplace_back(st, en);
}
bool ProcSyms::Module::contains(uint64_t addr, uint64_t &offset) const {
for (const auto &range : ranges_)
if (addr >= range.start && addr < range.end) {
offset = type_ == ModuleType::SO ? addr - range.start : addr;
return true;
}
return false;
}
bool ProcSyms::Module::find_name(const char *symname, uint64_t *addr) {
load_sym_table();
for (Symbol &s : syms_) {
if (*(s.name) == symname) {
*addr = type_ == ModuleType::SO ? start() + s.start : s.start;
return true;
}
}
return false;
}
bool ProcSyms::Module::find_addr(uint64_t offset, struct bcc_symbol *sym) {
load_sym_table();
sym->module = name_.c_str();
sym->offset = offset;
auto it = std::upper_bound(syms_.begin(), syms_.end(), Symbol(nullptr, offset, 0));
if (it == syms_.begin())
return false;
// 'it' points to the symbol whose start address is strictly greater than
// the address we're looking for. Start stepping backwards as long as the
// current symbol is still below the desired address, and see if the end
// of the current symbol (start + size) is above the desired address. Once
// we have a matching symbol, return it. Note that simply looking at '--it'
// is not enough, because symbols can be nested. For example, we could be
// looking for offset 0x12 with the following symbols available:
// SYMBOL START SIZE END
// goo 0x0 0x6 0x0 + 0x6 = 0x6
// foo 0x6 0x10 0x6 + 0x10 = 0x16
// bar 0x8 0x4 0x8 + 0x4 = 0xc
// baz 0x16 0x10 0x16 + 0x10 = 0x26
// The upper_bound lookup will return baz, and then going one symbol back
// brings us to bar, which does not contain offset 0x12 and is nested inside
// foo. Going back one more symbol brings us to foo, which contains 0x12
// and is a match.
// However, we also don't want to walk through the entire symbol list for
// unknown / missing symbols. So we will break if we reach a function that
// doesn't cover the function immediately before 'it', which means it is
// not possibly a nested function containing the address we're looking for.
--it;
uint64_t limit = it->start;
for (; offset >= it->start; --it) {
if (offset < it->start + it->size) {
sym->name = it->name->c_str();
sym->offset = (offset - it->start);
return true;
}
if (limit > it->start + it->size)
break;
// But don't step beyond begin()!
if (it == syms_.begin())
break;
}
return false;
}
extern "C" {
void *bcc_symcache_new(int pid, struct bcc_symbol_option *option) {
if (pid < 0)
return static_cast<void *>(new KSyms());
return static_cast<void *>(new ProcSyms(pid, option));
}
void bcc_free_symcache(void *symcache, int pid) {
if (pid < 0)
delete static_cast<KSyms*>(symcache);
else
delete static_cast<ProcSyms*>(symcache);
}
void bcc_symbol_free_demangle_name(struct bcc_symbol *sym) {
if (sym->demangle_name && (sym->demangle_name != sym->name))
free(const_cast<char*>(sym->demangle_name));
}
int bcc_symcache_resolve(void *resolver, uint64_t addr,
struct bcc_symbol *sym) {
SymbolCache *cache = static_cast<SymbolCache *>(resolver);
return cache->resolve_addr(addr, sym) ? 0 : -1;
}
int bcc_symcache_resolve_no_demangle(void *resolver, uint64_t addr,
struct bcc_symbol *sym) {
SymbolCache *cache = static_cast<SymbolCache *>(resolver);
return cache->resolve_addr(addr, sym, false) ? 0 : -1;
}
int bcc_symcache_resolve_name(void *resolver, const char *module,
const char *name, uint64_t *addr) {
SymbolCache *cache = static_cast<SymbolCache *>(resolver);
return cache->resolve_name(module, name, addr) ? 0 : -1;
}
void bcc_symcache_refresh(void *resolver) {
SymbolCache *cache = static_cast<SymbolCache *>(resolver);
cache->refresh();
}
struct mod_st {
const char *name;
uint64_t start;
};
static int _find_module(const char *modname, uint64_t start, uint64_t end,
uint64_t offset, bool, void *p) {
struct mod_st *mod = (struct mod_st *)p;
if (!strcmp(modname, mod->name)) {
mod->start = start;
return -1;
}
return 0;
}
int bcc_resolve_global_addr(int pid, const char *module, const uint64_t address,
uint64_t *global) {
struct mod_st mod = {module, 0x0};
if (bcc_procutils_each_module(pid, _find_module, &mod) < 0 ||
mod.start == 0x0)
return -1;
*global = mod.start + address;
return 0;
}
static int _sym_cb_wrapper(const char *symname, uint64_t addr, uint64_t,
void *payload) {
SYM_CB cb = (SYM_CB) payload;
return cb(symname, addr);
}
int bcc_foreach_function_symbol(const char *module, SYM_CB cb) {
if (module == 0 || cb == 0)
return -1;
static struct bcc_symbol_option default_option = {
.use_debug_file = 1,
.check_debug_file_crc = 1,
.use_symbol_type = (1 << STT_FUNC) | (1 << STT_GNU_IFUNC)
};
return bcc_elf_foreach_sym(
module, _sym_cb_wrapper, &default_option, (void *)cb);
}
static int _find_sym(const char *symname, uint64_t addr, uint64_t,
void *payload) {
struct bcc_symbol *sym = (struct bcc_symbol *)payload;
if (!strcmp(sym->name, symname)) {
sym->offset = addr;
return -1;
}
return 0;
}
struct load_addr_t {
uint64_t target_addr;
uint64_t binary_addr;
};
int _find_load(uint64_t v_addr, uint64_t mem_sz, uint64_t file_offset,
void *payload) {
struct load_addr_t *addr = static_cast<load_addr_t *>(payload);
if (addr->target_addr >= v_addr && addr->target_addr < (v_addr + mem_sz)) {
addr->binary_addr = addr->target_addr - v_addr + file_offset;
return -1;
}
return 0;
}
int bcc_resolve_symname(const char *module, const char *symname,
const uint64_t addr, int pid,
struct bcc_symbol_option *option,
struct bcc_symbol *sym) {
static struct bcc_symbol_option default_option = {
.use_debug_file = 1,
.check_debug_file_crc = 1,
.use_symbol_type = BCC_SYM_ALL_TYPES,
};
if (module == NULL)
return -1;
memset(sym, 0, sizeof(bcc_symbol));
if (strchr(module, '/')) {
sym->module = strdup(module);
} else {
sym->module = bcc_procutils_which_so(module, pid);
}
if (sym->module == NULL)
return -1;
ProcMountNSGuard g(pid);
sym->name = symname;
sym->offset = addr;
if (option == NULL)
option = &default_option;
if (sym->name && sym->offset == 0x0)
if (bcc_elf_foreach_sym(sym->module, _find_sym, option, sym) < 0)
goto invalid_module;
if (sym->offset == 0x0)
goto invalid_module;
// For executable (ET_EXEC) binaries, translate the virtual address
// to physical address in the binary file.
// For shared object binaries (ET_DYN), the address from symbol table should
// already be physical address in the binary file.
if (bcc_elf_get_type(sym->module) == ET_EXEC) {
struct load_addr_t addr = {
.target_addr = sym->offset,
.binary_addr = 0x0,
};
if (bcc_elf_foreach_load_section(sym->module, &_find_load, &addr) < 0)
goto invalid_module;
if (!addr.binary_addr)
goto invalid_module;
sym->offset = addr.binary_addr;
}
return 0;
invalid_module:
if (sym->module) {
::free(const_cast<char*>(sym->module));
sym->module = NULL;
}
return -1;
}
}