Commit 4ba52382 authored by Teng Qin's avatar Teng Qin Committed by 4ast

Try to demangle C++ symbols (#638)

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