Commit 89d901c6 authored by Teng Qin's avatar Teng Qin

Use bcc_symbol_option in ProcSyms

This commit makes `ProcSyms` constructor takes a `bcc_symbol_option`,
and pass it down to underlying calls to control symboling behavior.
If `nullptr` is passed, `ProcSyms` will use default setting, which is
to use debug file, verify debug file checksum, and only load function symbols.

This commit also makes `bcc_symcache_new` take a `bcc_symbol_option`
parameter and pass it to the underlying `ProcSyms` constructor.
parent aff6ce70
...@@ -110,7 +110,7 @@ std::vector<std::string> BPFStackTable::get_stack_symbol(int stack_id, ...@@ -110,7 +110,7 @@ std::vector<std::string> BPFStackTable::get_stack_symbol(int stack_id,
if (pid < 0) if (pid < 0)
pid = -1; pid = -1;
if (pid_sym_.find(pid) == pid_sym_.end()) if (pid_sym_.find(pid) == pid_sym_.end())
pid_sym_[pid] = bcc_symcache_new(pid); pid_sym_[pid] = bcc_symcache_new(pid, nullptr);
void* cache = pid_sym_[pid]; void* cache = pid_sym_[pid];
bcc_symbol symbol; bcc_symbol symbol;
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
*/ */
#include <cxxabi.h> #include <cxxabi.h>
#include <cstring>
#include <fcntl.h> #include <fcntl.h>
#include <linux/elf.h> #include <linux/elf.h>
#include <string.h> #include <string.h>
...@@ -159,8 +160,16 @@ ProcMountNSGuard::~ProcMountNSGuard() { ...@@ -159,8 +160,16 @@ ProcMountNSGuard::~ProcMountNSGuard() {
setns(mount_ns_->self_fd_, CLONE_NEWNS); setns(mount_ns_->self_fd_, CLONE_NEWNS);
} }
ProcSyms::ProcSyms(int pid) ProcSyms::ProcSyms(int pid, struct bcc_symbol_option *option)
: pid_(pid), procstat_(pid), mount_ns_instance_(new ProcMountNS(pid_)) { : 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(); load_modules();
} }
...@@ -183,7 +192,8 @@ int ProcSyms::_add_module(const char *modname, uint64_t start, uint64_t end, ...@@ -183,7 +192,8 @@ int ProcSyms::_add_module(const char *modname, uint64_t start, uint64_t end,
[=](const ProcSyms::Module &m) { return m.name_ == modname; }); [=](const ProcSyms::Module &m) { return m.name_ == modname; });
if (it == ps->modules_.end()) { if (it == ps->modules_.end()) {
auto module = Module( auto module = Module(
modname, check_mount_ns ? ps->mount_ns_instance_.get() : nullptr); modname, check_mount_ns ? ps->mount_ns_instance_.get() : nullptr,
&ps->symbol_option_);
if (module.init()) if (module.init())
it = ps->modules_.insert(ps->modules_.end(), std::move(module)); it = ps->modules_.insert(ps->modules_.end(), std::move(module));
else else
...@@ -248,10 +258,12 @@ bool ProcSyms::resolve_name(const char *module, const char *name, ...@@ -248,10 +258,12 @@ bool ProcSyms::resolve_name(const char *module, const char *name,
return false; return false;
} }
ProcSyms::Module::Module(const char *name, ProcMountNS *mount_ns) ProcSyms::Module::Module(const char *name, ProcMountNS *mount_ns,
struct bcc_symbol_option *option)
: name_(name), : name_(name),
loaded_(false), loaded_(false),
mount_ns_(mount_ns), mount_ns_(mount_ns),
symbol_option_(option),
type_(ModuleType::UNKNOWN) {} type_(ModuleType::UNKNOWN) {}
bool ProcSyms::Module::init() { bool ProcSyms::Module::init() {
...@@ -295,7 +307,7 @@ void ProcSyms::Module::load_sym_table() { ...@@ -295,7 +307,7 @@ void ProcSyms::Module::load_sym_table() {
if (type_ == ModuleType::PERF_MAP) if (type_ == ModuleType::PERF_MAP)
bcc_perf_map_foreach_sym(name_.c_str(), _add_symbol, this); bcc_perf_map_foreach_sym(name_.c_str(), _add_symbol, this);
if (type_ == ModuleType::EXEC || type_ == ModuleType::SO) if (type_ == ModuleType::EXEC || type_ == ModuleType::SO)
bcc_elf_foreach_sym(name_.c_str(), _add_symbol, this); bcc_elf_foreach_sym(name_.c_str(), _add_symbol, symbol_option_, this);
std::sort(syms_.begin(), syms_.end()); std::sort(syms_.begin(), syms_.end());
} }
...@@ -371,10 +383,10 @@ bool ProcSyms::Module::find_addr(uint64_t offset, struct bcc_symbol *sym) { ...@@ -371,10 +383,10 @@ bool ProcSyms::Module::find_addr(uint64_t offset, struct bcc_symbol *sym) {
extern "C" { extern "C" {
void *bcc_symcache_new(int pid) { void *bcc_symcache_new(int pid, struct bcc_symbol_option *option) {
if (pid < 0) if (pid < 0)
return static_cast<void *>(new KSyms()); return static_cast<void *>(new KSyms());
return static_cast<void *>(new ProcSyms(pid)); return static_cast<void *>(new ProcSyms(pid, option));
} }
void bcc_free_symcache(void *symcache, int pid) { void bcc_free_symcache(void *symcache, int pid) {
......
...@@ -39,7 +39,7 @@ struct bcc_symbol_option { ...@@ -39,7 +39,7 @@ struct bcc_symbol_option {
uint32_t use_symbol_type; uint32_t use_symbol_type;
}; };
void *bcc_symcache_new(int pid); void *bcc_symcache_new(int pid, struct bcc_symbol_option *option);
void bcc_free_symcache(void *symcache, int pid); void bcc_free_symcache(void *symcache, int pid);
// The demangle_name pointer in bcc_symbol struct is returned from the // The demangle_name pointer in bcc_symbol struct is returned from the
......
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include <vector> #include <vector>
#include "file_desc.h" #include "file_desc.h"
#include "bcc_syms.h"
class ProcStat { class ProcStat {
std::string procfs_; std::string procfs_;
...@@ -123,13 +124,15 @@ class ProcSyms : SymbolCache { ...@@ -123,13 +124,15 @@ class ProcSyms : SymbolCache {
Range(uint64_t s, uint64_t e) : start(s), end(e) {} Range(uint64_t s, uint64_t e) : start(s), end(e) {}
}; };
Module(const char *name, ProcMountNS* mount_ns); Module(const char *name, ProcMountNS* mount_ns,
struct bcc_symbol_option *option);
bool init(); bool init();
std::string name_; std::string name_;
std::vector<Range> ranges_; std::vector<Range> ranges_;
bool loaded_; bool loaded_;
ProcMountNS *mount_ns_; ProcMountNS *mount_ns_;
bcc_symbol_option *symbol_option_;
ModuleType type_; ModuleType type_;
std::unordered_set<std::string> symnames_; std::unordered_set<std::string> symnames_;
...@@ -149,12 +152,13 @@ class ProcSyms : SymbolCache { ...@@ -149,12 +152,13 @@ class ProcSyms : SymbolCache {
std::vector<Module> modules_; std::vector<Module> modules_;
ProcStat procstat_; ProcStat procstat_;
std::unique_ptr<ProcMountNS> mount_ns_instance_; std::unique_ptr<ProcMountNS> mount_ns_instance_;
bcc_symbol_option symbol_option_;
static int _add_module(const char *, uint64_t, uint64_t, bool, void *); static int _add_module(const char *, uint64_t, uint64_t, bool, void *);
bool load_modules(); bool load_modules();
public: public:
ProcSyms(int pid); ProcSyms(int pid, struct bcc_symbol_option *option = nullptr);
virtual void refresh(); virtual void refresh();
virtual bool resolve_addr(uint64_t addr, struct bcc_symbol *sym, bool demangle = true); virtual bool resolve_addr(uint64_t addr, struct bcc_symbol *sym, bool demangle = true);
virtual bool resolve_name(const char *module, const char *name, virtual bool resolve_name(const char *module, const char *name,
......
...@@ -35,7 +35,12 @@ std::string Argument::ctype() const { ...@@ -35,7 +35,12 @@ std::string Argument::ctype() const {
bool Argument::get_global_address(uint64_t *address, const std::string &binpath, bool Argument::get_global_address(uint64_t *address, const std::string &binpath,
const optional<int> &pid) const { const optional<int> &pid) const {
if (pid) { if (pid) {
return ProcSyms(*pid) static struct bcc_symbol_option default_option = {
.use_debug_file = 1,
.check_debug_file_crc = 1,
.use_symbol_type = BCC_SYM_ALL_TYPES
};
return ProcSyms(*pid, &default_option)
.resolve_name(binpath.c_str(), deref_ident_->c_str(), address); .resolve_name(binpath.c_str(), deref_ident_->c_str(), address);
} }
......
...@@ -124,7 +124,7 @@ int bcc_resolve_symname(const char *module, const char *symname, const uint64_t ...@@ -124,7 +124,7 @@ int bcc_resolve_symname(const char *module, const char *symname, const uint64_t
int pid, struct bcc_symbol_option *option, int pid, struct bcc_symbol_option *option,
struct bcc_symbol *sym); struct bcc_symbol *sym);
void bcc_procutils_free(const char *ptr); void bcc_procutils_free(const char *ptr);
void *bcc_symcache_new(int pid); void *bcc_symcache_new(int pid, struct bcc_symbol_option *option);
void bcc_symbol_free_demangle_name(struct bcc_symbol *sym); void bcc_symbol_free_demangle_name(struct bcc_symbol *sym);
int bcc_symcache_resolve(void *symcache, uint64_t addr, struct bcc_symbol *sym); int bcc_symcache_resolve(void *symcache, uint64_t addr, struct bcc_symbol *sym);
void bcc_symcache_refresh(void *resolver); void bcc_symcache_refresh(void *resolver);
......
...@@ -19,7 +19,7 @@ local SYM = ffi.typeof("struct bcc_symbol[1]") ...@@ -19,7 +19,7 @@ local SYM = ffi.typeof("struct bcc_symbol[1]")
local function create_cache(pid) local function create_cache(pid)
return { return {
_CACHE = libbcc.bcc_symcache_new(pid or -1), _CACHE = libbcc.bcc_symcache_new(pid or -1, nil),
resolve = function(self, addr) resolve = function(self, addr)
local sym = SYM() local sym = SYM()
if libbcc.bcc_symcache_resolve(self._CACHE, addr, sym) < 0 then if libbcc.bcc_symcache_resolve(self._CACHE, addr, sym) < 0 then
......
...@@ -46,7 +46,8 @@ LOG_BUFFER_SIZE = 65536 ...@@ -46,7 +46,8 @@ LOG_BUFFER_SIZE = 65536
class SymbolCache(object): class SymbolCache(object):
def __init__(self, pid): def __init__(self, pid):
self.cache = lib.bcc_symcache_new(pid) self.cache = lib.bcc_symcache_new(
pid, ct.cast(None, ct.POINTER(bcc_symbol_option)))
def resolve(self, addr, demangle): def resolve(self, addr, demangle):
""" """
......
...@@ -156,7 +156,7 @@ lib.bcc_foreach_function_symbol.restype = ct.c_int ...@@ -156,7 +156,7 @@ lib.bcc_foreach_function_symbol.restype = ct.c_int
lib.bcc_foreach_function_symbol.argtypes = [ct.c_char_p, _SYM_CB_TYPE] lib.bcc_foreach_function_symbol.argtypes = [ct.c_char_p, _SYM_CB_TYPE]
lib.bcc_symcache_new.restype = ct.c_void_p lib.bcc_symcache_new.restype = ct.c_void_p
lib.bcc_symcache_new.argtypes = [ct.c_int] lib.bcc_symcache_new.argtypes = [ct.c_int, ct.POINTER(bcc_symbol_option)]
lib.bcc_free_symcache.restype = ct.c_void_p lib.bcc_free_symcache.restype = ct.c_void_p
lib.bcc_free_symcache.argtypes = [ct.c_void_p, ct.c_int] lib.bcc_free_symcache.argtypes = [ct.c_void_p, ct.c_int]
......
...@@ -195,7 +195,7 @@ static int mntns_func(void *arg) { ...@@ -195,7 +195,7 @@ static int mntns_func(void *arg) {
TEST_CASE("resolve symbol addresses for a given PID", "[c_api]") { TEST_CASE("resolve symbol addresses for a given PID", "[c_api]") {
struct bcc_symbol sym; struct bcc_symbol sym;
void *resolver = bcc_symcache_new(getpid()); void *resolver = bcc_symcache_new(getpid(), nullptr);
REQUIRE(resolver); REQUIRE(resolver);
...@@ -240,7 +240,7 @@ TEST_CASE("resolve symbol addresses for a given PID", "[c_api]") { ...@@ -240,7 +240,7 @@ TEST_CASE("resolve symbol addresses for a given PID", "[c_api]") {
child = spawn_child(0, true, true, mntns_func); child = spawn_child(0, true, true, mntns_func);
REQUIRE(child > 0); REQUIRE(child > 0);
void *resolver = bcc_symcache_new(child); void *resolver = bcc_symcache_new(child, nullptr);
REQUIRE(resolver); REQUIRE(resolver);
REQUIRE(bcc_symcache_resolve_name(resolver, "/tmp/libz.so.1", "zlibVersion", REQUIRE(bcc_symcache_resolve_name(resolver, "/tmp/libz.so.1", "zlibVersion",
...@@ -335,7 +335,7 @@ TEST_CASE("resolve symbols using /tmp/perf-pid.map", "[c_api]") { ...@@ -335,7 +335,7 @@ TEST_CASE("resolve symbols using /tmp/perf-pid.map", "[c_api]") {
child = spawn_child(map_addr, /* own_pidns */ false, false, perf_map_func); child = spawn_child(map_addr, /* own_pidns */ false, false, perf_map_func);
REQUIRE(child > 0); REQUIRE(child > 0);
void *resolver = bcc_symcache_new(child); void *resolver = bcc_symcache_new(child, nullptr);
REQUIRE(resolver); REQUIRE(resolver);
REQUIRE(bcc_symcache_resolve(resolver, (unsigned long long)map_addr, REQUIRE(bcc_symcache_resolve(resolver, (unsigned long long)map_addr,
...@@ -355,7 +355,7 @@ TEST_CASE("resolve symbols using /tmp/perf-pid.map", "[c_api]") { ...@@ -355,7 +355,7 @@ TEST_CASE("resolve symbols using /tmp/perf-pid.map", "[c_api]") {
child = spawn_child(map_addr, /* own_pidns */ true, false, perf_map_func); child = spawn_child(map_addr, /* own_pidns */ true, false, perf_map_func);
REQUIRE(child > 0); REQUIRE(child > 0);
void *resolver = bcc_symcache_new(child); void *resolver = bcc_symcache_new(child, nullptr);
REQUIRE(resolver); REQUIRE(resolver);
REQUIRE(bcc_symcache_resolve(resolver, (unsigned long long)map_addr, REQUIRE(bcc_symcache_resolve(resolver, (unsigned long long)map_addr,
...@@ -372,7 +372,7 @@ TEST_CASE("resolve symbols using /tmp/perf-pid.map", "[c_api]") { ...@@ -372,7 +372,7 @@ TEST_CASE("resolve symbols using /tmp/perf-pid.map", "[c_api]") {
perf_map_func_mntns); perf_map_func_mntns);
REQUIRE(child > 0); REQUIRE(child > 0);
void *resolver = bcc_symcache_new(child); void *resolver = bcc_symcache_new(child, nullptr);
REQUIRE(resolver); REQUIRE(resolver);
REQUIRE(bcc_symcache_resolve(resolver, (unsigned long long)map_addr, REQUIRE(bcc_symcache_resolve(resolver, (unsigned long long)map_addr,
...@@ -391,7 +391,7 @@ TEST_CASE("resolve symbols using /tmp/perf-pid.map", "[c_api]") { ...@@ -391,7 +391,7 @@ TEST_CASE("resolve symbols using /tmp/perf-pid.map", "[c_api]") {
string path = perf_map_path(child); string path = perf_map_path(child);
REQUIRE(make_perf_map_file(path, (unsigned long long)map_addr) == 0); REQUIRE(make_perf_map_file(path, (unsigned long long)map_addr) == 0);
void *resolver = bcc_symcache_new(child); void *resolver = bcc_symcache_new(child, nullptr);
REQUIRE(resolver); REQUIRE(resolver);
REQUIRE(bcc_symcache_resolve(resolver, (unsigned long long)map_addr, REQUIRE(bcc_symcache_resolve(resolver, (unsigned long long)map_addr,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment