Try to demangle C++ symbols (#638)
authorTeng Qin <palmtenor@gmail.com>
Sun, 31 Jul 2016 17:08:07 +0000 (10:08 -0700)
committer4ast <alexei.starovoitov@gmail.com>
Sun, 31 Jul 2016 17:08:07 +0000 (10:08 -0700)
Added a field `demangle_name` in the `bcc_symbol` struct. Calculate its value whenever possible. For C++ programs, this would make outputted stack traces look nicer.
Example: http://pastebin.com/LqT0nP67

src/cc/bcc_syms.cc
src/cc/bcc_syms.h
src/lua/bcc/libbcc.lua
src/lua/bcc/sym.lua
src/python/bcc/__init__.py
src/python/bcc/libbcc.py

index be60485..b1f1736 100644 (file)
@@ -14,6 +14,7 @@
  * limitations under the License.
  */
 
+#include <cxxabi.h>
 #include <string.h>
 #include <sys/stat.h>
 #include <sys/types.h>
@@ -52,6 +53,7 @@ bool KSyms::resolve_addr(uint64_t addr, struct bcc_symbol *sym) {
 
   if (syms_.empty()) {
     sym->name = nullptr;
+    sym->demangle_name = nullptr;
     sym->module = nullptr;
     sym->offset = 0x0;
     return false;
@@ -59,6 +61,7 @@ bool KSyms::resolve_addr(uint64_t addr, struct bcc_symbol *sym) {
 
   auto it = std::upper_bound(syms_.begin(), syms_.end(), Symbol("", addr)) - 1;
   sym->name = (*it).name.c_str();
+  sym->demangle_name = sym->name;
   sym->module = "[kernel]";
   sym->offset = addr - (*it).addr;
   return true;
@@ -108,11 +111,19 @@ bool ProcSyms::resolve_addr(uint64_t addr, struct bcc_symbol *sym) {
 
   sym->module = nullptr;
   sym->name = nullptr;
+  sym->demangle_name = nullptr;
   sym->offset = 0x0;
 
   for (Module &mod : modules_) {
-    if (addr >= mod.start_ && addr < mod.end_)
-      return mod.find_addr(addr, sym);
+    if (addr >= mod.start_ && addr < mod.end_) {
+      bool res = mod.find_addr(addr, sym);
+      if (sym->name) {
+        sym->demangle_name = abi::__cxa_demangle(sym->name, nullptr, nullptr, nullptr);
+        if (!sym->demangle_name)
+          sym->demangle_name = sym->name;
+      }
+      return res;
+    }
   }
   return false;
 }
index 1d42ca3..d130c5e 100644 (file)
@@ -24,6 +24,7 @@ extern "C" {
 
 struct bcc_symbol {
   const char *name;
+  const char *demangle_name;
   const char *module;
   uint64_t offset;
 };
index 1ff91bc..db356a8 100644 (file)
@@ -101,6 +101,7 @@ void perf_reader_set_fd(struct perf_reader *reader, int fd);
 ffi.cdef[[
 struct bcc_symbol {
        const char *name;
+       const char *demangle_name;
        const char *module;
        uint64_t offset;
 };
index 1f227d2..8bd6d3b 100644 (file)
@@ -25,7 +25,7 @@ local function create_cache(pid)
       if libbcc.bcc_symcache_resolve(self._CACHE, addr, sym) < 0 then
         return "[unknown]", 0x0
       end
-      return ffi.string(sym[0].name), sym[0].offset
+      return ffi.string(sym[0].demangle_name), sym[0].offset
     end
   }
 end
index 5c6ae1e..7cb5fb8 100644 (file)
@@ -54,7 +54,7 @@ class SymbolCache(object):
         psym = ct.pointer(sym)
         if lib.bcc_symcache_resolve(self.cache, addr, psym) < 0:
             return "[unknown]", 0
-        return sym.name.decode(), sym.offset
+        return sym.demangle_name.decode(), sym.offset
 
     def resolve_name(self, name):
         addr = ct.c_ulonglong()
index 59509c2..a893e19 100644 (file)
@@ -115,6 +115,7 @@ lib.bpf_attach_xdp.argtypes = [ct.c_char_p, ct.c_int]
 class bcc_symbol(ct.Structure):
     _fields_ = [
             ('name', ct.c_char_p),
+            ('demangle_name', ct.c_char_p),
             ('module', ct.c_char_p),
             ('offset', ct.c_ulonglong),
         ]