Use bcc_symbol_option in ProcSyms
authorTeng Qin <qinteng@fb.com>
Wed, 10 May 2017 00:26:02 +0000 (17:26 -0700)
committerTeng Qin <qinteng@fb.com>
Sat, 20 May 2017 04:55:57 +0000 (21:55 -0700)
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.

src/cc/BPFTable.cc
src/cc/bcc_syms.cc
src/cc/bcc_syms.h
src/cc/syms.h
src/cc/usdt_args.cc
src/lua/bcc/libbcc.lua
src/lua/bcc/sym.lua
src/python/bcc/__init__.py
src/python/bcc/libbcc.py
tests/cc/test_c_api.cc

index b459c26ad10bc56c3ca6fbee49430df7ebbf7dca..2fa928b197620a12f15c7efaa04ec9572ccb9b45 100644 (file)
@@ -110,7 +110,7 @@ std::vector<std::string> BPFStackTable::get_stack_symbol(int stack_id,
   if (pid < 0)
     pid = -1;
   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];
 
   bcc_symbol symbol;
index c68b68ca9aa0d406b33a1e6147854477e3dbd2f0..b8021beb0a444fe858acf9c360304143f1dfc3f3 100644 (file)
@@ -15,6 +15,7 @@
  */
 
 #include <cxxabi.h>
+#include <cstring>
 #include <fcntl.h>
 #include <linux/elf.h>
 #include <string.h>
@@ -159,8 +160,16 @@ ProcMountNSGuard::~ProcMountNSGuard() {
     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_)) {
+  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();
 }
 
@@ -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; });
   if (it == ps->modules_.end()) {
     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())
       it = ps->modules_.insert(ps->modules_.end(), std::move(module));
     else
@@ -248,10 +258,12 @@ bool ProcSyms::resolve_name(const char *module, const char *name,
   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),
       loaded_(false),
       mount_ns_(mount_ns),
+      symbol_option_(option),
       type_(ModuleType::UNKNOWN) {}
 
 bool ProcSyms::Module::init() {
@@ -295,7 +307,7 @@ void ProcSyms::Module::load_sym_table() {
   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, this);
+    bcc_elf_foreach_sym(name_.c_str(), _add_symbol, symbol_option_, this);
 
   std::sort(syms_.begin(), syms_.end());
 }
@@ -371,10 +383,10 @@ bool ProcSyms::Module::find_addr(uint64_t offset, struct bcc_symbol *sym) {
 
 extern "C" {
 
-void *bcc_symcache_new(int pid) {
+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));
+  return static_cast<void *>(new ProcSyms(pid, option));
 }
 
 void bcc_free_symcache(void *symcache, int pid) {
index 8e6984ea8e0220a3f1e578db4c486ac6fb571d58..03047f2ac9297fd0ba643d6345f6c4f398dce447 100644 (file)
@@ -39,7 +39,7 @@ struct bcc_symbol_option {
   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);
 
 // The demangle_name pointer in bcc_symbol struct is returned from the
index 9a170c194b9402e1ac2efd3dfb97927176bca42d..ea0584d36b96da50aeb48eb84512a434f2533f0e 100644 (file)
@@ -24,6 +24,7 @@
 #include <vector>
 
 #include "file_desc.h"
+#include "bcc_syms.h"
 
 class ProcStat {
   std::string procfs_;
@@ -123,13 +124,15 @@ class ProcSyms : SymbolCache {
       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();
 
     std::string name_;
     std::vector<Range> ranges_;
     bool loaded_;
     ProcMountNS *mount_ns_;
+    bcc_symbol_option *symbol_option_;
     ModuleType type_;
 
     std::unordered_set<std::string> symnames_;
@@ -149,12 +152,13 @@ class ProcSyms : SymbolCache {
   std::vector<Module> modules_;
   ProcStat procstat_;
   std::unique_ptr<ProcMountNS> mount_ns_instance_;
+  bcc_symbol_option symbol_option_;
 
   static int _add_module(const char *, uint64_t, uint64_t, bool, void *);
   bool load_modules();
 
 public:
-  ProcSyms(int pid);
+  ProcSyms(int pid, struct bcc_symbol_option *option = nullptr);
   virtual void refresh();
   virtual bool resolve_addr(uint64_t addr, struct bcc_symbol *sym, bool demangle = true);
   virtual bool resolve_name(const char *module, const char *name,
index d54b1e5d77fd82ee03c0824c7c3d621f5b78aa25..5d76c04f3ffa8f9d8d4f65c429b07fd5a6a10255 100644 (file)
@@ -35,7 +35,12 @@ std::string Argument::ctype() const {
 bool Argument::get_global_address(uint64_t *address, const std::string &binpath,
                                   const optional<int> &pid) const {
   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);
   }
 
index e41881cbc0cf3386a897a98cb9f9e835ee46231f..e0f3dd5f711c9078dc43b705f49d66a35e2dede3 100644 (file)
@@ -124,7 +124,7 @@ int bcc_resolve_symname(const char *module, const char *symname, const uint64_t
                         int pid, struct bcc_symbol_option *option,
                         struct bcc_symbol *sym);
 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);
 int bcc_symcache_resolve(void *symcache, uint64_t addr, struct bcc_symbol *sym);
 void bcc_symcache_refresh(void *resolver);
index 8d88d4e2f72afac58126b353f5c4f73d89bf89e3..d30546a359d3fec5c903be7f8d90cc6035e41cae 100644 (file)
@@ -19,7 +19,7 @@ local SYM = ffi.typeof("struct bcc_symbol[1]")
 
 local function create_cache(pid)
   return {
-    _CACHE = libbcc.bcc_symcache_new(pid or -1),
+    _CACHE = libbcc.bcc_symcache_new(pid or -1, nil),
     resolve = function(self, addr)
       local sym = SYM()
       if libbcc.bcc_symcache_resolve(self._CACHE, addr, sym) < 0 then
index 745e1c2ec6c5f4fbf43abf13758c0153987abb14..3ed970cb4a954cf510b7af13a22887793d25527a 100644 (file)
@@ -46,7 +46,8 @@ LOG_BUFFER_SIZE = 65536
 
 class SymbolCache(object):
     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):
         """
index 4fb2939c7bf53290ee74ac7831afc06bd837aa83..30aea5b17fa175b495aae62854a1c93359a0f3f5 100644 (file)
@@ -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_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.argtypes = [ct.c_void_p, ct.c_int]
index c3a8d3c01342c89b526257059fdca616b6004d67..261ecc8cdf9a162981480b884f3bd7b0423730cf 100644 (file)
@@ -195,7 +195,7 @@ static int mntns_func(void *arg) {
 
 TEST_CASE("resolve symbol addresses for a given PID", "[c_api]") {
   struct bcc_symbol sym;
-  void *resolver = bcc_symcache_new(getpid());
+  void *resolver = bcc_symcache_new(getpid(), nullptr);
 
   REQUIRE(resolver);
 
@@ -240,7 +240,7 @@ TEST_CASE("resolve symbol addresses for a given PID", "[c_api]") {
     child = spawn_child(0, true, true, mntns_func);
     REQUIRE(child > 0);
 
-    void *resolver = bcc_symcache_new(child);
+    void *resolver = bcc_symcache_new(child, nullptr);
     REQUIRE(resolver);
 
     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]") {
     child = spawn_child(map_addr, /* own_pidns */ false, false, perf_map_func);
     REQUIRE(child > 0);
 
-    void *resolver = bcc_symcache_new(child);
+    void *resolver = bcc_symcache_new(child, nullptr);
     REQUIRE(resolver);
 
     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]") {
     child = spawn_child(map_addr, /* own_pidns */ true, false, perf_map_func);
     REQUIRE(child > 0);
 
-    void *resolver = bcc_symcache_new(child);
+    void *resolver = bcc_symcache_new(child, nullptr);
     REQUIRE(resolver);
 
     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]") {
         perf_map_func_mntns);
     REQUIRE(child > 0);
 
-    void *resolver = bcc_symcache_new(child);
+    void *resolver = bcc_symcache_new(child, nullptr);
     REQUIRE(resolver);
 
     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]") {
     string path = perf_map_path(child);
     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(bcc_symcache_resolve(resolver, (unsigned long long)map_addr,