Commit c1c2373f authored by Vicent Marti's avatar Vicent Marti

cc: Format C and C++ code according to Google's styleguide

parent 1ad2cef6
......@@ -5,11 +5,10 @@
#include <string.h>
#include <gelf.h>
#include "bcc_helpers.h"
#include "bcc_elf.h"
#define NT_STAPSDT 3
static int openelf(const char *path, Elf **elf_out, int *fd_out)
{
static int openelf(const char *path, Elf **elf_out, int *fd_out) {
if (elf_version(EV_CURRENT) == EV_NONE)
return -1;
......@@ -26,9 +25,8 @@ static int openelf(const char *path, Elf **elf_out, int *fd_out)
return 0;
}
static const char *
parse_stapsdt_note(struct bcc_elf_usdt *probe, const char *desc, int elf_class)
{
static const char *parse_stapsdt_note(struct bcc_elf_usdt *probe,
const char *desc, int elf_class) {
if (elf_class == ELFCLASS32) {
probe->pc = *((uint32_t *)(desc));
probe->base_addr = *((uint32_t *)(desc + 4));
......@@ -53,10 +51,9 @@ parse_stapsdt_note(struct bcc_elf_usdt *probe, const char *desc, int elf_class)
return desc;
}
static int do_note_segment(
Elf_Scn *section, int elf_class,
bcc_elf_probecb callback, const char *binpath, void *payload)
{
static int do_note_segment(Elf_Scn *section, int elf_class,
bcc_elf_probecb callback, const char *binpath,
void *payload) {
Elf_Data *data = NULL;
while ((data = elf_getdata(section, data)) != 0) {
......@@ -64,7 +61,8 @@ static int do_note_segment(
GElf_Nhdr hdr;
size_t name_off, desc_off;
while ((offset = gelf_getnote(data, offset, &hdr, &name_off, &desc_off)) != 0) {
while ((offset = gelf_getnote(data, offset, &hdr, &name_off, &desc_off)) !=
0) {
const char *desc, *desc_end;
struct bcc_elf_usdt probe;
......@@ -87,8 +85,8 @@ static int do_note_segment(
return 0;
}
static int listprobes(Elf *e, bcc_elf_probecb callback, const char *binpath, void *payload)
{
static int listprobes(Elf *e, bcc_elf_probecb callback, const char *binpath,
void *payload) {
Elf_Scn *section = NULL;
size_t stridx;
int elf_class = gelf_getclass(e);
......@@ -116,8 +114,8 @@ static int listprobes(Elf *e, bcc_elf_probecb callback, const char *binpath, voi
return 0;
}
int bcc_elf_foreach_usdt(const char *path, bcc_elf_probecb callback, void *payload)
{
int bcc_elf_foreach_usdt(const char *path, bcc_elf_probecb callback,
void *payload) {
Elf *e;
int fd, res;
......@@ -131,12 +129,8 @@ int bcc_elf_foreach_usdt(const char *path, bcc_elf_probecb callback, void *paylo
return res;
}
static int list_in_scn(
Elf *e, Elf_Scn *section,
size_t stridx, size_t symsize,
bcc_elf_symcb callback, void *payload)
{
static int list_in_scn(Elf *e, Elf_Scn *section, size_t stridx, size_t symsize,
bcc_elf_symcb callback, void *payload) {
Elf_Data *data = NULL;
while ((data = elf_getdata(section, data)) != 0) {
......@@ -163,8 +157,7 @@ static int list_in_scn(
return 0;
}
static int listsymbols(Elf *e, bcc_elf_symcb callback, void *payload)
{
static int listsymbols(Elf *e, bcc_elf_symcb callback, void *payload) {
Elf_Scn *section = NULL;
while ((section = elf_nextscn(e, section)) != 0) {
......@@ -176,16 +169,16 @@ static int listsymbols(Elf *e, bcc_elf_symcb callback, void *payload)
if (header.sh_type != SHT_SYMTAB && header.sh_type != SHT_DYNSYM)
continue;
if (list_in_scn(e, section,
header.sh_link, header.sh_entsize, callback, payload) < 0)
if (list_in_scn(e, section, header.sh_link, header.sh_entsize, callback,
payload) < 0)
return -1;
}
return 0;
}
int bcc_elf_foreach_sym(const char *path, bcc_elf_symcb callback, void *payload)
{
int bcc_elf_foreach_sym(const char *path, bcc_elf_symcb callback,
void *payload) {
Elf *e;
int fd, res;
......@@ -198,8 +191,7 @@ int bcc_elf_foreach_sym(const char *path, bcc_elf_symcb callback, void *payload)
return res;
}
static int loadaddr(Elf *e, uint64_t *addr)
{
static int loadaddr(Elf *e, uint64_t *addr) {
size_t phnum, i;
if (elf_getphdrnum(e, &phnum) != 0)
......@@ -221,8 +213,7 @@ static int loadaddr(Elf *e, uint64_t *addr)
return -1;
}
int bcc_elf_loadaddr(const char *path, uint64_t *address)
{
int bcc_elf_loadaddr(const char *path, uint64_t *address) {
Elf *e;
int fd, res;
......@@ -236,8 +227,7 @@ int bcc_elf_loadaddr(const char *path, uint64_t *address)
return res;
}
int bcc_elf_is_shared_obj(const char *path)
{
int bcc_elf_is_shared_obj(const char *path) {
Elf *e;
GElf_Ehdr hdr;
int fd, res = -1;
......
#ifndef LIBBCC_ELF_H
#define LIBBCC_ELF_H
#ifdef __cplusplus
extern "C" {
#endif
struct bcc_elf_usdt {
uint64_t pc;
uint64_t base_addr;
uint64_t semaphore;
const char *provider;
const char *name;
const char *arg_fmt;
};
typedef void (*bcc_elf_probecb)(const char *, const struct bcc_elf_usdt *,
void *);
typedef int (*bcc_elf_symcb)(const char *, uint64_t, uint64_t, int, void *);
int bcc_elf_foreach_usdt(const char *path, bcc_elf_probecb callback,
void *payload);
int bcc_elf_loadaddr(const char *path, uint64_t *address);
int bcc_elf_foreach_sym(const char *path, bcc_elf_symcb callback,
void *payload);
int bcc_elf_is_shared_obj(const char *path);
#ifdef __cplusplus
}
#endif
#endif
......@@ -10,10 +10,10 @@
#include <ctype.h>
#include <stdio.h>
#include "bcc_helpers.h"
#include "bcc_proc.h"
#include "bcc_elf.h"
static bool is_exe(const char *path)
{
static bool is_exe(const char *path) {
struct stat s;
if (access(path, X_OK) < 0)
return false;
......@@ -24,8 +24,7 @@ static bool is_exe(const char *path)
return S_ISREG(s.st_mode);
}
char *bcc_procutils_which(const char *binpath)
{
char *bcc_procutils_which(const char *binpath) {
char buffer[4096];
const char *PATH;
......@@ -54,8 +53,8 @@ char *bcc_procutils_which(const char *binpath)
return 0;
}
int bcc_procutils_each_module(int pid, bcc_procutils_modulecb callback, void *payload)
{
int bcc_procutils_each_module(int pid, bcc_procutils_modulecb callback,
void *payload) {
char procmap_filename[128];
FILE *procmap;
int ret;
......@@ -71,8 +70,8 @@ int bcc_procutils_each_module(int pid, bcc_procutils_modulecb callback, void *pa
char perm[8], dev[8];
long long begin, end, size, inode;
ret = fscanf(procmap, "%llx-%llx %s %llx %s %llx",
&begin, &end, perm, &size, dev, &inode);
ret = fscanf(procmap, "%llx-%llx %s %llx %s %llx", &begin, &end, perm,
&size, dev, &inode);
if (!fgets(endline, sizeof(endline), procmap))
break;
......@@ -84,8 +83,7 @@ int bcc_procutils_each_module(int pid, bcc_procutils_modulecb callback, void *pa
if (newline)
newline[0] = '\0';
while (isspace(mapname[0]))
mapname++;
while (isspace(mapname[0])) mapname++;
if (strchr(perm, 'x') && mapname[0] && mapname[0] != '[')
callback(mapname, (uint64_t)begin, (uint64_t)end, payload);
......@@ -96,8 +94,7 @@ int bcc_procutils_each_module(int pid, bcc_procutils_modulecb callback, void *pa
return 0;
}
int bcc_procutils_each_ksym(bcc_procutils_ksymcb callback, void *payload)
{
int bcc_procutils_each_ksym(bcc_procutils_ksymcb callback, void *payload) {
char line[2048];
FILE *kallsyms = fopen("/proc/kallsyms", "r");
......@@ -116,8 +113,7 @@ int bcc_procutils_each_ksym(bcc_procutils_ksymcb callback, void *payload)
addr = strtoull(line, &symname, 16);
endsym = symname = symname + 3;
while (*endsym && !isspace(*endsym))
endsym++;
while (*endsym && !isspace(*endsym)) endsym++;
*endsym = '\0';
callback(symname, addr, payload);
......@@ -168,15 +164,16 @@ static struct ld_lib {
char *libname;
char *path;
int flags;
} *lib_cache;
} * lib_cache;
static int read_cache1(const char *ld_map)
{
static int read_cache1(const char *ld_map) {
struct ld_cache1 *ldcache = (struct ld_cache1 *)ld_map;
const char *ldstrings = (const char *)(ldcache->entries + ldcache->entry_count);
const char *ldstrings =
(const char *)(ldcache->entries + ldcache->entry_count);
uint32_t i;
lib_cache = (struct ld_lib *)malloc(ldcache->entry_count * sizeof(struct ld_lib));
lib_cache =
(struct ld_lib *)malloc(ldcache->entry_count * sizeof(struct ld_lib));
lib_cache_count = (int)ldcache->entry_count;
for (i = 0; i < ldcache->entry_count; ++i) {
......@@ -191,15 +188,15 @@ static int read_cache1(const char *ld_map)
return 0;
}
static int read_cache2(const char *ld_map)
{
static int read_cache2(const char *ld_map) {
struct ld_cache2 *ldcache = (struct ld_cache2 *)ld_map;
uint32_t i;
if (memcmp(ld_map, CACHE2_HEADER, CACHE2_HEADER_LEN))
return -1;
lib_cache = (struct ld_lib *)malloc(ldcache->entry_count * sizeof(struct ld_lib));
lib_cache =
(struct ld_lib *)malloc(ldcache->entry_count * sizeof(struct ld_lib));
lib_cache_count = (int)ldcache->entry_count;
for (i = 0; i < ldcache->entry_count; ++i) {
......@@ -214,8 +211,7 @@ static int read_cache2(const char *ld_map)
return 0;
}
static int load_ld_cache(const char *cache_path)
{
static int load_ld_cache(const char *cache_path) {
struct stat st;
size_t ld_size;
const char *ld_map;
......@@ -265,8 +261,7 @@ static int load_ld_cache(const char *cache_path)
#define ABI_S390_LIB64 0x0400
#define ABI_POWERPC_LIB64 0x0500
static bool match_so_flags(int flags)
{
static bool match_so_flags(int flags) {
if ((flags & FLAG_TYPE_MASK) != TYPE_ELF_LIBC6)
return false;
......@@ -282,8 +277,7 @@ static bool match_so_flags(int flags)
return true;
}
const char *bcc_procutils_which_so(const char *libname)
{
const char *bcc_procutils_which_so(const char *libname) {
const size_t soname_len = strlen(libname) + strlen("lib.so");
char soname[soname_len + 1];
int i;
......@@ -308,53 +302,3 @@ const char *bcc_procutils_which_so(const char *libname)
}
return NULL;
}
static int _find_sym(const char *symname,
uint64_t addr, uint64_t end, int flags, void *payload)
{
struct bcc_symbol *sym = (struct bcc_symbol *)payload;
if (!strcmp(sym->name, symname)) {
sym->offset = addr;
return -1;
}
return 0;
}
int bcc_resolve_symname(const char *module, const char *symname,
const uint64_t addr, struct bcc_symbol *sym)
{
uint64_t load_addr;
sym->module = NULL;
sym->name = NULL;
sym->offset = 0x0;
if (module == NULL)
return -1;
if (strchr(module, '/')) {
sym->module = module;
} else {
sym->module = bcc_procutils_which_so(module);
}
if (sym->module == NULL)
return -1;
if (bcc_elf_loadaddr(sym->module, &load_addr) < 0) {
sym->module = NULL;
return -1;
}
sym->name = symname;
sym->offset = addr;
if (sym->name && sym->offset == 0x0)
bcc_elf_foreach_sym(sym->module, _find_sym, sym);
if (sym->offset == 0x0)
return -1;
sym->offset = (sym->offset - load_addr);
return 0;
}
#ifndef LIBBCC_ELF_H
#define LIBBCC_ELF_H
#ifndef LIBBCC_PROC_H
#define LIBBCC_PROC_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
struct bcc_symbol {
const char *name;
const char *module;
uint64_t offset;
};
struct bcc_elf_usdt {
uint64_t pc;
uint64_t base_addr;
uint64_t semaphore;
const char *provider;
const char *name;
const char *arg_fmt;
};
typedef void (*bcc_elf_probecb)(const char *, const struct bcc_elf_usdt *, void *);
typedef int (*bcc_elf_symcb)(const char *, uint64_t, uint64_t, int, void *);
int bcc_elf_foreach_usdt(const char *path, bcc_elf_probecb callback, void *payload);
int bcc_elf_loadaddr(const char *path, uint64_t *address);
int bcc_elf_foreach_sym(const char *path, bcc_elf_symcb callback, void *payload);
int bcc_elf_is_shared_obj(const char *path);
typedef void (*bcc_procutils_modulecb)(const char *, uint64_t, uint64_t, void *);
typedef void (*bcc_procutils_modulecb)(const char *, uint64_t, uint64_t,
void *);
typedef void (*bcc_procutils_ksymcb)(const char *, uint64_t, void *);
const char *bcc_procutils_which_so(const char *libname);
char *bcc_procutils_which(const char *binpath);
int bcc_procutils_each_module(int pid, bcc_procutils_modulecb callback, void *payload);
int bcc_procutils_each_module(int pid, bcc_procutils_modulecb callback,
void *payload);
int bcc_procutils_each_ksym(bcc_procutils_ksymcb callback, void *payload);
int bcc_resolve_symname(const char *module, const char *symname, const uint64_t addr,
struct bcc_symbol *sym);
void *bcc_symcache_new(int pid);
int bcc_symcache_resolve(void *symcache, uint64_t addr, struct bcc_symbol *sym);
int bcc_symcache_resolve_name(void *resolver, const char *name, uint64_t *addr);
void bcc_symcache_refresh(void *resolver);
int bcc_resolve_symname(const char *module, const char *symname,
const uint64_t addr, struct bcc_symbol *sym);
#ifdef __cplusplus
}
......
......@@ -6,11 +6,14 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include "bcc_helpers.h"
#include "bcc_syms.h"
#include "bcc_proc.h"
#include "bcc_elf.h"
class SymbolCache {
public:
public:
virtual void refresh() = 0;
virtual bool resolve_addr(uint64_t addr, struct bcc_symbol *sym) = 0;
virtual bool resolve_name(const char *name, uint64_t *addr) = 0;
......@@ -18,67 +21,62 @@ public:
class KSyms : SymbolCache {
struct Symbol {
Symbol(const char *name, uint64_t addr) :
name(name), addr(addr) {}
Symbol(const char *name, uint64_t addr) : name(name), addr(addr) {}
std::string name;
uint64_t addr;
bool operator<(const Symbol& rhs) const { return addr < rhs.addr; }
bool operator<(const Symbol &rhs) const { return addr < rhs.addr; }
};
std::vector<Symbol> _syms;
std::unordered_map<std::string, uint64_t> _sym_names;
std::vector<Symbol> syms_;
std::unordered_map<std::string, uint64_t> symnames_;
static void _add_symbol(const char *, uint64_t, void *);
public:
public:
virtual bool resolve_addr(uint64_t addr, struct bcc_symbol *sym);
virtual bool resolve_name(const char *name, uint64_t *addr);
virtual void refresh()
{
if (_syms.empty()) {
virtual void refresh() {
if (syms_.empty()) {
bcc_procutils_each_ksym(_add_symbol, this);
std::sort(_syms.begin(), _syms.end());
std::sort(syms_.begin(), syms_.end());
}
}
};
void KSyms::_add_symbol(const char *symname, uint64_t addr, void *p)
{
void KSyms::_add_symbol(const char *symname, uint64_t addr, void *p) {
KSyms *ks = static_cast<KSyms *>(p);
ks->_syms.emplace_back(symname, addr);
ks->syms_.emplace_back(symname, addr);
}
bool KSyms::resolve_addr(uint64_t addr, struct bcc_symbol *sym)
{
bool KSyms::resolve_addr(uint64_t addr, struct bcc_symbol *sym) {
refresh();
if (_syms.empty()) {
if (syms_.empty()) {
sym->name = nullptr;
sym->module = nullptr;
sym->offset = 0x0;
return false;
}
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->module = "[kernel]";
sym->offset = addr - (*it).addr;
return true;
}
bool KSyms::resolve_name(const char *name, uint64_t *addr)
{
bool KSyms::resolve_name(const char *name, uint64_t *addr) {
refresh();
if (_syms.size() != _sym_names.size()) {
_sym_names.clear();
for (Symbol &sym : _syms) {
_sym_names[sym.name] = sym.addr;
if (syms_.size() != symnames_.size()) {
symnames_.clear();
for (Symbol &sym : syms_) {
symnames_[sym.name] = sym.addr;
}
}
auto it = _sym_names.find(name);
if (it == _sym_names.end())
auto it = symnames_.find(name);
if (it == symnames_.end())
return false;
*addr = it->second;
......@@ -86,37 +84,34 @@ bool KSyms::resolve_name(const char *name, uint64_t *addr)
}
class ProcStat {
std::string _procfs;
ino_t _inode;
std::string procfs_;
ino_t inode_;
ino_t get_inode()
{
ino_t getinode_() {
struct stat s;
return (!stat(_procfs.c_str(), &s)) ? s.st_ino : -1;
return (!stat(procfs_.c_str(), &s)) ? s.st_ino : -1;
}
public:
ProcStat(int pid) : _inode(-1)
{
public:
ProcStat(int pid) : inode_(-1) {
char buffer[128];
snprintf(buffer, sizeof(buffer), "/proc/%d/exe", pid);
_procfs = buffer;
procfs_ = buffer;
}
bool is_stale() { return _inode != get_inode(); }
void reset() { _inode = get_inode(); }
bool is_stale() { return inode_ != getinode_(); }
void reset() { inode_ = getinode_(); }
};
static bool has_suffix(const std::string &str, const std::string &suffix)
{
static bool has_suffix(const std::string &str, const std::string &suffix) {
return str.size() >= suffix.size() &&
str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
}
class ProcSyms : SymbolCache {
struct Symbol {
Symbol(const char *name, uint64_t start, uint64_t size, int flags = 0) :
name(name), start(start), size(size), flags(flags) {}
Symbol(const char *name, uint64_t start, uint64_t size, int flags = 0)
: name(name), start(start), size(size), flags(flags) {}
std::string name;
uint64_t start;
uint64_t size;
......@@ -124,101 +119,91 @@ class ProcSyms : SymbolCache {
};
struct Module {
Module(const char *name, uint64_t start, uint64_t end) :
_name(name), _start(start), _end(end) {}
std::string _name;
uint64_t _start;
uint64_t _end;
std::vector<Symbol> _syms;
Module(const char *name, uint64_t start, uint64_t end)
: name_(name), start_(start), end_(end) {}
std::string name_;
uint64_t start_;
uint64_t end_;
std::vector<Symbol> syms_;
void load_sym_table();
bool decode_sym(uint64_t addr, struct bcc_symbol *sym);
bool is_so() { return has_suffix(_name, ".so"); }
bool is_so() { return has_suffix(name_, ".so"); }
static int _add_symbol(const char *symname,
uint64_t start, uint64_t end, int flags, void *p);
static int _add_symbol(const char *symname, uint64_t start, uint64_t end,
int flags, void *p);
};
int _pid;
std::vector<Module> _modules;
ProcStat _procstat;
int pid_;
std::vector<Module> modules_;
ProcStat procstat_;
static void _add_module(const char *, uint64_t, uint64_t, void*);
static void _add_module(const char *, uint64_t, uint64_t, void *);
public:
public:
ProcSyms(int pid);
virtual void refresh();
virtual bool resolve_addr(uint64_t addr, struct bcc_symbol *sym);
virtual bool resolve_name(const char *name, uint64_t *addr);
};
ProcSyms::ProcSyms(int pid) : _pid(pid), _procstat(pid)
{
refresh();
}
ProcSyms::ProcSyms(int pid) : pid_(pid), procstat_(pid) { refresh(); }
void ProcSyms::refresh()
{
_modules.clear();
bcc_procutils_each_module(_pid, _add_module, this);
_procstat.reset();
void ProcSyms::refresh() {
modules_.clear();
bcc_procutils_each_module(pid_, _add_module, this);
procstat_.reset();
}
void ProcSyms::_add_module(
const char *modname, uint64_t start, uint64_t end, void *payload)
{
void ProcSyms::_add_module(const char *modname, uint64_t start, uint64_t end,
void *payload) {
ProcSyms *ps = static_cast<ProcSyms *>(payload);
ps->_modules.emplace_back(modname, start, end);
ps->modules_.emplace_back(modname, start, end);
}
bool ProcSyms::resolve_addr(uint64_t addr, struct bcc_symbol *sym)
{
if (_procstat.is_stale())
bool ProcSyms::resolve_addr(uint64_t addr, struct bcc_symbol *sym) {
if (procstat_.is_stale())
refresh();
sym->module = nullptr;
sym->name = nullptr;
sym->offset = 0x0;
for (Module &mod : _modules) {
if (addr >= mod._start && addr <= mod._end)
for (Module &mod : modules_) {
if (addr >= mod.start_ && addr <= mod.end_)
return mod.decode_sym(addr, sym);
}
return false;
}
bool ProcSyms::resolve_name(const char *name, uint64_t *addr)
{
bool ProcSyms::resolve_name(const char *name, uint64_t *addr) {
*addr = 0x0;
return false;
}
int ProcSyms::Module::_add_symbol(const char *symname,
uint64_t start, uint64_t end, int flags, void *p)
{
int ProcSyms::Module::_add_symbol(const char *symname, uint64_t start,
uint64_t end, int flags, void *p) {
Module *m = static_cast<Module *>(p);
m->_syms.emplace_back(symname, start, end, flags);
m->syms_.emplace_back(symname, start, end, flags);
return 0;
}
void ProcSyms::Module::load_sym_table()
{
if (_syms.size())
void ProcSyms::Module::load_sym_table() {
if (syms_.size())
return;
bcc_elf_foreach_sym(_name.c_str(), _add_symbol, this);
bcc_elf_foreach_sym(name_.c_str(), _add_symbol, this);
}
bool ProcSyms::Module::decode_sym(uint64_t addr, struct bcc_symbol *sym)
{
uint64_t offset = is_so() ? (addr - _start) : addr;
bool ProcSyms::Module::decode_sym(uint64_t addr, struct bcc_symbol *sym) {
uint64_t offset = is_so() ? (addr - start_) : addr;
load_sym_table();
sym->module = _name.c_str();
sym->module = name_.c_str();
sym->offset = offset;
for (Symbol &s : _syms) {
for (Symbol &s : syms_) {
if (offset >= s.start && offset <= (s.start + s.size)) {
sym->name = s.name.c_str();
sym->offset = (offset - s.start);
......@@ -230,29 +215,74 @@ bool ProcSyms::Module::decode_sym(uint64_t addr, struct bcc_symbol *sym)
extern "C" {
void *bcc_symcache_new(int pid)
{
void *bcc_symcache_new(int pid) {
if (pid < 0)
return static_cast<void *>(new KSyms());
return static_cast<void *>(new ProcSyms(pid));
}
int bcc_symcache_resolve(void *resolver, uint64_t addr, struct bcc_symbol *sym)
{
int bcc_symcache_resolve(void *resolver, uint64_t addr,
struct bcc_symbol *sym) {
SymbolCache *cache = static_cast<SymbolCache *>(resolver);
return cache->resolve_addr(addr, sym) ? 0 : -1;
}
int bcc_symcache_resolve_name(void *resolver, const char *name, uint64_t *addr)
{
int bcc_symcache_resolve_name(void *resolver, const char *name,
uint64_t *addr) {
SymbolCache *cache = static_cast<SymbolCache *>(resolver);
return cache->resolve_name(name, addr) ? 0 : -1;
}
void bcc_symcache_refresh(void *resolver)
{
void bcc_symcache_refresh(void *resolver) {
SymbolCache *cache = static_cast<SymbolCache *>(resolver);
cache->refresh();
}
static int _find_sym(const char *symname, uint64_t addr, uint64_t end,
int flags, void *payload) {
struct bcc_symbol *sym = (struct bcc_symbol *)payload;
if (!strcmp(sym->name, symname)) {
sym->offset = addr;
return -1;
}
return 0;
}
int bcc_resolve_symname(const char *module, const char *symname,
const uint64_t addr, struct bcc_symbol *sym) {
uint64_t load_addr;
sym->module = NULL;
sym->name = NULL;
sym->offset = 0x0;
if (module == NULL)
return -1;
if (strchr(module, '/')) {
sym->module = module;
} else {
sym->module = bcc_procutils_which_so(module);
}
if (sym->module == NULL)
return -1;
if (bcc_elf_loadaddr(sym->module, &load_addr) < 0) {
sym->module = NULL;
return -1;
}
sym->name = symname;
sym->offset = addr;
if (sym->name && sym->offset == 0x0)
bcc_elf_foreach_sym(sym->module, _find_sym, sym);
if (sym->offset == 0x0)
return -1;
sym->offset = (sym->offset - load_addr);
return 0;
}
}
#ifndef LIBBCC_SYMS_H
#define LIBBCC_SYMS_H
#ifdef __cplusplus
extern "C" {
#endif
struct bcc_symbol {
const char *name;
const char *module;
uint64_t offset;
};
void *bcc_symcache_new(int pid);
int bcc_symcache_resolve(void *symcache, uint64_t addr, struct bcc_symbol *sym);
int bcc_symcache_resolve_name(void *resolver, const char *name, uint64_t *addr);
void bcc_symcache_refresh(void *resolver);
#ifdef __cplusplus
}
#endif
#endif
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