Skip to content

Commit

Permalink
Free demangle_name after use
Browse files Browse the repository at this point in the history
  • Loading branch information
palmtenor committed Apr 5, 2017
1 parent 304b75d commit 9f066e4
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 6 deletions.
6 changes: 4 additions & 2 deletions src/cc/BPFTable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ std::vector<std::string> BPFStackTable::get_stack_symbol(int stack_id,
bcc_symbol symbol;
for (auto addr : addresses)
if (bcc_symcache_resolve(cache, addr, &symbol) != 0)
res.push_back("[UNKNOWN]");
else
res.emplace_back("[UNKNOWN]");
else {
res.push_back(symbol.demangle_name);
bcc_symbol_free_demangle_name(&symbol);
}

return res;
}
Expand Down
5 changes: 5 additions & 0 deletions src/cc/bcc_syms.cc
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,11 @@ void bcc_free_symcache(void *symcache, int pid) {
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);
Expand Down
4 changes: 4 additions & 0 deletions src/cc/bcc_syms.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ typedef int (*SYM_CB)(const char *symname, uint64_t addr);
void *bcc_symcache_new(int pid);
void bcc_free_symcache(void *symcache, int pid);

// The demangle_name pointer in bcc_symbol struct is returned from the
// __cxa_demangle function call, which is supposed to be freed by caller. Call
// this function after done using returned result of bcc_symcache_resolve.
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_no_demangle(void *symcache, uint64_t addr,
struct bcc_symbol *sym);
Expand Down
1 change: 1 addition & 0 deletions src/lua/bcc/libbcc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ int bcc_resolve_symname(const char *module, const char *symname, const uint64_t
int pid, struct bcc_symbol *sym);
void bcc_procutils_free(const char *ptr);
void *bcc_symcache_new(int pid);
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);
]]
Expand Down
4 changes: 3 additions & 1 deletion src/lua/bcc/sym.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ 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].demangle_name), sym[0].offset
local name_res = ffi.string(sym[0].demangle_name)
libbcc.bcc_symbol_free_demangle_name(sym);
return name_res, sym[0].offset
end
}
end
Expand Down
10 changes: 7 additions & 3 deletions src/python/bcc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,13 @@ def resolve(self, addr, demangle):
return (None, sym.offset,
ct.cast(sym.module, ct.c_char_p).value.decode())
return (None, addr, None)
return (
sym.demangle_name.decode() if demangle else sym.name.decode(),
sym.offset, ct.cast(sym.module, ct.c_char_p).value.decode())
if demangle:
name_res = sym.demangle_name.decode()
lib.bcc_symbol_free_demangle_name(psym)
else:
name_res = sym.name.decode()
return (name_res, sym.offset,
ct.cast(sym.module, ct.c_char_p).value.decode())

def resolve_name(self, module, name):
addr = ct.c_ulonglong()
Expand Down
3 changes: 3 additions & 0 deletions src/python/bcc/libbcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ class bcc_symbol(ct.Structure):
lib.bcc_free_symcache.restype = ct.c_void_p
lib.bcc_free_symcache.argtypes = [ct.c_void_p, ct.c_int]

lib.bcc_symbol_free_demangle_name.restype = ct.c_void_p
lib.bcc_symbol_free_demangle_name.argtypes = [ct.POINTER(bcc_symbol)]

lib.bcc_symcache_resolve.restype = ct.c_int
lib.bcc_symcache_resolve.argtypes = [ct.c_void_p, ct.c_ulonglong, ct.POINTER(bcc_symbol)]

Expand Down

0 comments on commit 9f066e4

Please sign in to comment.