Commit c1c2373f authored by Vicent Marti's avatar Vicent Marti

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

parent 1ad2cef6
...@@ -5,253 +5,243 @@ ...@@ -5,253 +5,243 @@
#include <string.h> #include <string.h>
#include <gelf.h> #include <gelf.h>
#include "bcc_helpers.h" #include "bcc_elf.h"
#define NT_STAPSDT 3 #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)
if (elf_version(EV_CURRENT) == EV_NONE) return -1;
return -1;
*fd_out = open(path, O_RDONLY); *fd_out = open(path, O_RDONLY);
if (*fd_out < 0) if (*fd_out < 0)
return -1; return -1;
*elf_out = elf_begin(*fd_out, ELF_C_READ, 0); *elf_out = elf_begin(*fd_out, ELF_C_READ, 0);
if (*elf_out == 0) { if (*elf_out == 0) {
close(*fd_out); close(*fd_out);
return -1; return -1;
} }
return 0; return 0;
} }
static const char * static const char *parse_stapsdt_note(struct bcc_elf_usdt *probe,
parse_stapsdt_note(struct bcc_elf_usdt *probe, const char *desc, int elf_class) const char *desc, int elf_class) {
{ if (elf_class == ELFCLASS32) {
if (elf_class == ELFCLASS32) { probe->pc = *((uint32_t *)(desc));
probe->pc = *((uint32_t *)(desc)); probe->base_addr = *((uint32_t *)(desc + 4));
probe->base_addr = *((uint32_t *)(desc + 4)); probe->semaphore = *((uint32_t *)(desc + 8));
probe->semaphore = *((uint32_t *)(desc + 8)); desc = desc + 12;
desc = desc + 12; } else {
} else { probe->pc = *((uint64_t *)(desc));
probe->pc = *((uint64_t *)(desc)); probe->base_addr = *((uint64_t *)(desc + 8));
probe->base_addr = *((uint64_t *)(desc + 8)); probe->semaphore = *((uint64_t *)(desc + 16));
probe->semaphore = *((uint64_t *)(desc + 16)); desc = desc + 24;
desc = desc + 24; }
}
probe->provider = desc;
probe->provider = desc; desc += strlen(desc) + 1;
desc += strlen(desc) + 1;
probe->name = desc;
probe->name = desc; desc += strlen(desc) + 1;
desc += strlen(desc) + 1;
probe->arg_fmt = desc;
probe->arg_fmt = desc; desc += strlen(desc) + 1;
desc += strlen(desc) + 1;
return desc;
return desc;
} }
static int do_note_segment( static int do_note_segment(Elf_Scn *section, int elf_class,
Elf_Scn *section, int elf_class, bcc_elf_probecb callback, const char *binpath,
bcc_elf_probecb callback, const char *binpath, void *payload) void *payload) {
{ Elf_Data *data = NULL;
Elf_Data *data = NULL;
while ((data = elf_getdata(section, data)) != 0) { while ((data = elf_getdata(section, data)) != 0) {
size_t offset = 0; size_t offset = 0;
GElf_Nhdr hdr; GElf_Nhdr hdr;
size_t name_off, desc_off; 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)) !=
const char *desc, *desc_end; 0) {
struct bcc_elf_usdt probe; const char *desc, *desc_end;
struct bcc_elf_usdt probe;
if (hdr.n_type != NT_STAPSDT) if (hdr.n_type != NT_STAPSDT)
continue; continue;
if (hdr.n_namesz != 8) if (hdr.n_namesz != 8)
continue; continue;
if (memcmp((const char *)data->d_buf + name_off, "stapsdt", 8) != 0) if (memcmp((const char *)data->d_buf + name_off, "stapsdt", 8) != 0)
continue; continue;
desc = (const char *)data->d_buf + desc_off; desc = (const char *)data->d_buf + desc_off;
desc_end = desc + hdr.n_descsz; desc_end = desc + hdr.n_descsz;
if (parse_stapsdt_note(&probe, desc, elf_class) == desc_end) if (parse_stapsdt_note(&probe, desc, elf_class) == desc_end)
callback(binpath, &probe, payload); callback(binpath, &probe, payload);
} }
} }
return 0; 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; Elf_Scn *section = NULL;
size_t stridx; size_t stridx;
int elf_class = gelf_getclass(e); int elf_class = gelf_getclass(e);
if (elf_getshdrstrndx(e, &stridx) != 0) if (elf_getshdrstrndx(e, &stridx) != 0)
return -1; return -1;
while ((section = elf_nextscn(e, section)) != 0) { while ((section = elf_nextscn(e, section)) != 0) {
GElf_Shdr header; GElf_Shdr header;
char *name; char *name;
if (!gelf_getshdr(section, &header)) if (!gelf_getshdr(section, &header))
continue; continue;
if (header.sh_type != SHT_NOTE) if (header.sh_type != SHT_NOTE)
continue; continue;
name = elf_strptr(e, stridx, header.sh_name); name = elf_strptr(e, stridx, header.sh_name);
if (name && !strcmp(name, ".note.stapsdt")) { if (name && !strcmp(name, ".note.stapsdt")) {
if (do_note_segment(section, elf_class, callback, binpath, payload) < 0) if (do_note_segment(section, elf_class, callback, binpath, payload) < 0)
return -1; return -1;
} }
} }
return 0; 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; Elf *e;
int fd, res; int fd, res;
if (openelf(path, &e, &fd) < 0) if (openelf(path, &e, &fd) < 0)
return -1; return -1;
res = listprobes(e, callback, path, payload); res = listprobes(e, callback, path, payload);
elf_end(e); elf_end(e);
close(fd); close(fd);
return res; return res;
} }
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;
static int list_in_scn( while ((data = elf_getdata(section, data)) != 0) {
Elf *e, Elf_Scn *section, size_t i, symcount = data->d_size / symsize;
size_t stridx, size_t symsize,
bcc_elf_symcb callback, void *payload)
{
Elf_Data *data = NULL;
while ((data = elf_getdata(section, data)) != 0) {
size_t i, symcount = data->d_size / symsize;
if (data->d_size % symsize) if (data->d_size % symsize)
return -1; return -1;
for (i = 0; i < symcount; ++i) { for (i = 0; i < symcount; ++i) {
GElf_Sym sym; GElf_Sym sym;
const char *name; const char *name;
if (!gelf_getsym(data, (int)i, &sym)) if (!gelf_getsym(data, (int)i, &sym))
continue; continue;
if ((name = elf_strptr(e, stridx, sym.st_name)) == NULL) if ((name = elf_strptr(e, stridx, sym.st_name)) == NULL)
continue; continue;
if (callback(name, sym.st_value, sym.st_size, sym.st_info, payload) < 0) if (callback(name, sym.st_value, sym.st_size, sym.st_info, payload) < 0)
break; break;
} }
} }
return 0; 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;
Elf_Scn *section = NULL;
while ((section = elf_nextscn(e, section)) != 0) { while ((section = elf_nextscn(e, section)) != 0) {
GElf_Shdr header; GElf_Shdr header;
if (!gelf_getshdr(section, &header)) if (!gelf_getshdr(section, &header))
continue; continue;
if (header.sh_type != SHT_SYMTAB && header.sh_type != SHT_DYNSYM) if (header.sh_type != SHT_SYMTAB && header.sh_type != SHT_DYNSYM)
continue; continue;
if (list_in_scn(e, section, if (list_in_scn(e, section, header.sh_link, header.sh_entsize, callback,
header.sh_link, header.sh_entsize, callback, payload) < 0) payload) < 0)
return -1; return -1;
} }
return 0; 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; Elf *e;
int fd, res; int fd, res;
if (openelf(path, &e, &fd) < 0) if (openelf(path, &e, &fd) < 0)
return -1; return -1;
res = listsymbols(e, callback, payload); res = listsymbols(e, callback, payload);
elf_end(e); elf_end(e);
close(fd); close(fd);
return res; return res;
} }
static int loadaddr(Elf *e, uint64_t *addr) static int loadaddr(Elf *e, uint64_t *addr) {
{ size_t phnum, i;
size_t phnum, i;
if (elf_getphdrnum(e, &phnum) != 0) if (elf_getphdrnum(e, &phnum) != 0)
return -1; return -1;
for (i = 0; i < phnum; ++i) { for (i = 0; i < phnum; ++i) {
GElf_Phdr header; GElf_Phdr header;
if (!gelf_getphdr(e, (int)i, &header)) if (!gelf_getphdr(e, (int)i, &header))
continue; continue;
if (header.p_type != PT_LOAD) if (header.p_type != PT_LOAD)
continue; continue;
*addr = (uint64_t)header.p_vaddr; *addr = (uint64_t)header.p_vaddr;
return 0; return 0;
} }
return -1; return -1;
} }
int bcc_elf_loadaddr(const char *path, uint64_t *address) int bcc_elf_loadaddr(const char *path, uint64_t *address) {
{ Elf *e;
Elf *e; int fd, res;
int fd, res;
if (openelf(path, &e, &fd) < 0) if (openelf(path, &e, &fd) < 0)
return -1; return -1;
res = loadaddr(e, address); res = loadaddr(e, address);
elf_end(e); elf_end(e);
close(fd); close(fd);
return res; return res;
} }
int bcc_elf_is_shared_obj(const char *path) int bcc_elf_is_shared_obj(const char *path) {
{ Elf *e;
Elf *e; GElf_Ehdr hdr;
GElf_Ehdr hdr; int fd, res = -1;
int fd, res = -1;
if (openelf(path, &e, &fd) < 0) if (openelf(path, &e, &fd) < 0)
return -1; return -1;
if (gelf_getehdr(e, &hdr)) if (gelf_getehdr(e, &hdr))
res = (hdr.e_type == ET_DYN); res = (hdr.e_type == ET_DYN);
elf_end(e); elf_end(e);
close(fd); close(fd);
return res; return res;
} }
#if 0 #if 0
......
#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,121 +10,117 @@ ...@@ -10,121 +10,117 @@
#include <ctype.h> #include <ctype.h>
#include <stdio.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;
struct stat s; if (access(path, X_OK) < 0)
if (access(path, X_OK) < 0) return false;
return false;
if (stat(path, &s) < 0) if (stat(path, &s) < 0)
return false; return false;
return S_ISREG(s.st_mode); return S_ISREG(s.st_mode);
} }
char *bcc_procutils_which(const char *binpath) char *bcc_procutils_which(const char *binpath) {
{ char buffer[4096];
char buffer[4096]; const char *PATH;
const char *PATH;
if (strchr(binpath, '/')) if (strchr(binpath, '/'))
return is_exe(binpath) ? strdup(binpath) : 0; return is_exe(binpath) ? strdup(binpath) : 0;
if (!(PATH = getenv("PATH"))) if (!(PATH = getenv("PATH")))
return 0; return 0;
while (PATH) { while (PATH) {
const char *next = strchr(PATH, ':') ?: strchr(PATH, '\0'); const char *next = strchr(PATH, ':') ?: strchr(PATH, '\0');
const size_t path_len = next - PATH; const size_t path_len = next - PATH;
if (path_len) { if (path_len) {
memcpy(buffer, PATH, path_len); memcpy(buffer, PATH, path_len);
buffer[path_len] = '/'; buffer[path_len] = '/';
strcpy(buffer + path_len + 1, binpath); strcpy(buffer + path_len + 1, binpath);
if (is_exe(buffer)) if (is_exe(buffer))
return strdup(buffer); return strdup(buffer);
} }
PATH = *next ? (next + 1) : 0; PATH = *next ? (next + 1) : 0;
} }
return 0; 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]; char procmap_filename[128];
FILE *procmap; FILE *procmap;
int ret; int ret;
sprintf(procmap_filename, "/proc/%ld/maps", (long)pid); sprintf(procmap_filename, "/proc/%ld/maps", (long)pid);
procmap = fopen(procmap_filename, "r"); procmap = fopen(procmap_filename, "r");
if (!procmap) if (!procmap)
return -1; return -1;
do { do {
char endline[4096]; char endline[4096];
char perm[8], dev[8]; char perm[8], dev[8];
long long begin, end, size, inode; long long begin, end, size, inode;
ret = fscanf(procmap, "%llx-%llx %s %llx %s %llx", ret = fscanf(procmap, "%llx-%llx %s %llx %s %llx", &begin, &end, perm,
&begin, &end, perm, &size, dev, &inode); &size, dev, &inode);
if (!fgets(endline, sizeof(endline), procmap)) if (!fgets(endline, sizeof(endline), procmap))
break; break;
if (ret == 6) { if (ret == 6) {
char *mapname = endline; char *mapname = endline;
char *newline = strchr(endline, '\n'); char *newline = strchr(endline, '\n');
if (newline) if (newline)
newline[0] = '\0'; newline[0] = '\0';
while (isspace(mapname[0])) while (isspace(mapname[0])) mapname++;
mapname++;
if (strchr(perm, 'x') && mapname[0] && mapname[0] != '[') if (strchr(perm, 'x') && mapname[0] && mapname[0] != '[')
callback(mapname, (uint64_t)begin, (uint64_t)end, payload); callback(mapname, (uint64_t)begin, (uint64_t)end, payload);
} }
} while (ret && ret != EOF); } while (ret && ret != EOF);
fclose(procmap); fclose(procmap);
return 0; 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];
char line[2048]; FILE *kallsyms = fopen("/proc/kallsyms", "r");
FILE *kallsyms = fopen("/proc/kallsyms", "r");
if (!kallsyms) if (!kallsyms)
return -1; return -1;
if (!fgets(line, sizeof(line), kallsyms)) { if (!fgets(line, sizeof(line), kallsyms)) {
fclose(kallsyms); fclose(kallsyms);
return -1; return -1;
} }
while (fgets(line, sizeof(line), kallsyms)) { while (fgets(line, sizeof(line), kallsyms)) {
char *symname, *endsym; char *symname, *endsym;
unsigned long long addr; unsigned long long addr;
addr = strtoull(line, &symname, 16); addr = strtoull(line, &symname, 16);
endsym = symname = symname + 3; endsym = symname = symname + 3;
while (*endsym && !isspace(*endsym)) while (*endsym && !isspace(*endsym)) endsym++;
endsym++;
*endsym = '\0'; *endsym = '\0';
callback(symname, addr, payload); callback(symname, addr, payload);
} }
fclose(kallsyms); fclose(kallsyms);
return 0; return 0;
} }
#define CACHE1_HEADER "ld.so-1.7.0" #define CACHE1_HEADER "ld.so-1.7.0"
...@@ -135,124 +131,124 @@ int bcc_procutils_each_ksym(bcc_procutils_ksymcb callback, void *payload) ...@@ -135,124 +131,124 @@ int bcc_procutils_each_ksym(bcc_procutils_ksymcb callback, void *payload)
#define CACHE2_VERSION "1.1" #define CACHE2_VERSION "1.1"
struct ld_cache1_entry { struct ld_cache1_entry {
int32_t flags; int32_t flags;
uint32_t key; uint32_t key;
uint32_t value; uint32_t value;
}; };
struct ld_cache1 { struct ld_cache1 {
char header[CACHE1_HEADER_LEN]; char header[CACHE1_HEADER_LEN];
uint32_t entry_count; uint32_t entry_count;
struct ld_cache1_entry entries[0]; struct ld_cache1_entry entries[0];
}; };
struct ld_cache2_entry { struct ld_cache2_entry {
int32_t flags; int32_t flags;
uint32_t key; uint32_t key;
uint32_t value; uint32_t value;
uint32_t pad1_; uint32_t pad1_;
uint64_t pad2_; uint64_t pad2_;
}; };
struct ld_cache2 { struct ld_cache2 {
char header[CACHE2_HEADER_LEN]; char header[CACHE2_HEADER_LEN];
char version[3]; char version[3];
uint32_t entry_count; uint32_t entry_count;
uint32_t string_table_len; uint32_t string_table_len;
uint32_t pad_[5]; uint32_t pad_[5];
struct ld_cache2_entry entries[0]; struct ld_cache2_entry entries[0];
}; };
static int lib_cache_count; static int lib_cache_count;
static struct ld_lib { static struct ld_lib {
char *libname; char *libname;
char *path; char *path;
int flags; 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;
struct ld_cache1 *ldcache = (struct ld_cache1 *)ld_map; const char *ldstrings =
const char *ldstrings = (const char *)(ldcache->entries + ldcache->entry_count); (const char *)(ldcache->entries + ldcache->entry_count);
uint32_t i; uint32_t i;
lib_cache = (struct ld_lib *)malloc(ldcache->entry_count * sizeof(struct ld_lib)); lib_cache =
lib_cache_count = (int)ldcache->entry_count; (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) {
const char *key = ldstrings + ldcache->entries[i].key; for (i = 0; i < ldcache->entry_count; ++i) {
const char *val = ldstrings + ldcache->entries[i].value; const char *key = ldstrings + ldcache->entries[i].key;
const int flags = ldcache->entries[i].flags; const char *val = ldstrings + ldcache->entries[i].value;
const int flags = ldcache->entries[i].flags;
lib_cache[i].libname = strdup(key);
lib_cache[i].path = strdup(val); lib_cache[i].libname = strdup(key);
lib_cache[i].flags = flags; lib_cache[i].path = strdup(val);
} lib_cache[i].flags = flags;
return 0; }
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;
struct ld_cache2 *ldcache = (struct ld_cache2 *)ld_map; uint32_t i;
uint32_t i;
if (memcmp(ld_map, CACHE2_HEADER, CACHE2_HEADER_LEN)) if (memcmp(ld_map, CACHE2_HEADER, CACHE2_HEADER_LEN))
return -1; return -1;
lib_cache = (struct ld_lib *)malloc(ldcache->entry_count * sizeof(struct ld_lib)); lib_cache =
lib_cache_count = (int)ldcache->entry_count; (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) { for (i = 0; i < ldcache->entry_count; ++i) {
const char *key = ld_map + ldcache->entries[i].key; const char *key = ld_map + ldcache->entries[i].key;
const char *val = ld_map + ldcache->entries[i].value; const char *val = ld_map + ldcache->entries[i].value;
const int flags = ldcache->entries[i].flags; const int flags = ldcache->entries[i].flags;
lib_cache[i].libname = strdup(key); lib_cache[i].libname = strdup(key);
lib_cache[i].path = strdup(val); lib_cache[i].path = strdup(val);
lib_cache[i].flags = flags; lib_cache[i].flags = flags;
} }
return 0; return 0;
} }
static int load_ld_cache(const char *cache_path) static int load_ld_cache(const char *cache_path) {
{ struct stat st;
struct stat st; size_t ld_size;
size_t ld_size; const char *ld_map;
const char *ld_map; int ret, fd = open(cache_path, O_RDONLY);
int ret, fd = open(cache_path, O_RDONLY);
if (fd < 0)
if (fd < 0) return -1;
return -1;
if (fstat(fd, &st) < 0 || st.st_size < sizeof(struct ld_cache1)) {
if (fstat(fd, &st) < 0 || st.st_size < sizeof(struct ld_cache1)) { close(fd);
close(fd); return -1;
return -1; }
}
ld_size = st.st_size;
ld_size = st.st_size; ld_map = (const char *)mmap(NULL, ld_size, PROT_READ, MAP_PRIVATE, fd, 0);
ld_map = (const char *)mmap(NULL, ld_size, PROT_READ, MAP_PRIVATE, fd, 0); if (ld_map == MAP_FAILED) {
if (ld_map == MAP_FAILED) { close(fd);
close(fd); return -1;
return -1; }
}
if (memcmp(ld_map, CACHE1_HEADER, CACHE1_HEADER_LEN) == 0) {
if (memcmp(ld_map, CACHE1_HEADER, CACHE1_HEADER_LEN) == 0) { const struct ld_cache1 *cache1 = (struct ld_cache1 *)ld_map;
const struct ld_cache1 *cache1 = (struct ld_cache1 *)ld_map; size_t cache1_len = sizeof(struct ld_cache1) +
size_t cache1_len = sizeof(struct ld_cache1) + (cache1->entry_count * sizeof(struct ld_cache1_entry));
(cache1->entry_count * sizeof(struct ld_cache1_entry)); cache1_len = (cache1_len + 0x7) & ~0x7ULL;
cache1_len = (cache1_len + 0x7) & ~0x7ULL;
if (ld_size > (cache1_len + sizeof(struct ld_cache2)))
if (ld_size > (cache1_len + sizeof(struct ld_cache2))) ret = read_cache2(ld_map + cache1_len);
ret = read_cache2(ld_map + cache1_len); else
else ret = read_cache1(ld_map);
ret = read_cache1(ld_map); } else {
} else { ret = read_cache2(ld_map);
ret = read_cache2(ld_map); }
}
munmap((void *)ld_map, ld_size);
munmap((void *)ld_map, ld_size); close(fd);
close(fd); return ret;
return ret;
} }
#define LD_SO_CACHE "/etc/ld.so.cache" #define LD_SO_CACHE "/etc/ld.so.cache"
...@@ -265,96 +261,44 @@ static int load_ld_cache(const char *cache_path) ...@@ -265,96 +261,44 @@ static int load_ld_cache(const char *cache_path)
#define ABI_S390_LIB64 0x0400 #define ABI_S390_LIB64 0x0400
#define ABI_POWERPC_LIB64 0x0500 #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)
if ((flags & FLAG_TYPE_MASK) != TYPE_ELF_LIBC6) return false;
return false;
switch (flags & FLAG_ABI_MASK) {
case ABI_SPARC_LIB64:
case ABI_IA64_LIB64:
case ABI_X8664_LIB64:
case ABI_S390_LIB64:
case ABI_POWERPC_LIB64:
return (sizeof(void *) == 8);
}
return true;
}
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;
if (strchr(libname, '/'))
return libname;
if (lib_cache_count < 0) switch (flags & FLAG_ABI_MASK) {
return NULL; case ABI_SPARC_LIB64:
case ABI_IA64_LIB64:
case ABI_X8664_LIB64:
case ABI_S390_LIB64:
case ABI_POWERPC_LIB64:
return (sizeof(void *) == 8);
}
if (!lib_cache_count && load_ld_cache(LD_SO_CACHE) < 0) { return true;
lib_cache_count = -1;
return NULL;
}
snprintf(soname, soname_len + 1, "lib%s.so", libname);
for (i = 0; i < lib_cache_count; ++i) {
if (!strncmp(lib_cache[i].libname, soname, soname_len) &&
match_so_flags(lib_cache[i].flags))
return lib_cache[i].path;
}
return NULL;
} }
static int _find_sym(const char *symname, const char *bcc_procutils_which_so(const char *libname) {
uint64_t addr, uint64_t end, int flags, void *payload) const size_t soname_len = strlen(libname) + strlen("lib.so");
{ char soname[soname_len + 1];
struct bcc_symbol *sym = (struct bcc_symbol *)payload; int i;
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) { if (strchr(libname, '/'))
sym->module = NULL; return libname;
return -1;
}
sym->name = symname; if (lib_cache_count < 0)
sym->offset = addr; return NULL;
if (sym->name && sym->offset == 0x0) if (!lib_cache_count && load_ld_cache(LD_SO_CACHE) < 0) {
bcc_elf_foreach_sym(sym->module, _find_sym, sym); lib_cache_count = -1;
return NULL;
}
if (sym->offset == 0x0) snprintf(soname, soname_len + 1, "lib%s.so", libname);
return -1;
sym->offset = (sym->offset - load_addr); for (i = 0; i < lib_cache_count; ++i) {
return 0; if (!strncmp(lib_cache[i].libname, soname, soname_len) &&
match_so_flags(lib_cache[i].flags))
return lib_cache[i].path;
}
return NULL;
} }
#ifndef LIBBCC_ELF_H #ifndef LIBBCC_PROC_H
#define LIBBCC_ELF_H #define LIBBCC_PROC_H
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
#include <stdint.h> typedef void (*bcc_procutils_modulecb)(const char *, uint64_t, uint64_t,
void *);
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_ksymcb)(const char *, uint64_t, void *); typedef void (*bcc_procutils_ksymcb)(const char *, uint64_t, void *);
const char *bcc_procutils_which_so(const char *libname); const char *bcc_procutils_which_so(const char *libname);
char *bcc_procutils_which(const char *binpath); 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_procutils_each_ksym(bcc_procutils_ksymcb callback, void *payload);
int bcc_resolve_symname(const char *module, const char *symname, const uint64_t addr, int bcc_resolve_symname(const char *module, const char *symname,
struct bcc_symbol *sym); 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);
#ifdef __cplusplus #ifdef __cplusplus
} }
......
...@@ -6,253 +6,283 @@ ...@@ -6,253 +6,283 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.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 { class SymbolCache {
public: public:
virtual void refresh() = 0; virtual void refresh() = 0;
virtual bool resolve_addr(uint64_t addr, struct bcc_symbol *sym) = 0; virtual bool resolve_addr(uint64_t addr, struct bcc_symbol *sym) = 0;
virtual bool resolve_name(const char *name, uint64_t *addr) = 0; virtual bool resolve_name(const char *name, uint64_t *addr) = 0;
}; };
class KSyms : SymbolCache { class KSyms : SymbolCache {
struct Symbol { struct Symbol {
Symbol(const char *name, uint64_t addr) : Symbol(const char *name, uint64_t addr) : name(name), addr(addr) {}
name(name), addr(addr) {} std::string name;
std::string name; uint64_t addr;
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::vector<Symbol> _syms; std::unordered_map<std::string, uint64_t> symnames_;
std::unordered_map<std::string, uint64_t> _sym_names; static void _add_symbol(const char *, uint64_t, void *);
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_addr(uint64_t addr, struct bcc_symbol *sym); virtual bool resolve_name(const char *name, uint64_t *addr);
virtual bool resolve_name(const char *name, uint64_t *addr); virtual void refresh() {
virtual void refresh() if (syms_.empty()) {
{ bcc_procutils_each_ksym(_add_symbol, this);
if (_syms.empty()) { std::sort(syms_.begin(), syms_.end());
bcc_procutils_each_ksym(_add_symbol, this); }
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);
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();
refresh();
if (syms_.empty()) {
if (_syms.empty()) { sym->name = nullptr;
sym->name = nullptr; sym->module = nullptr;
sym->module = nullptr; sym->offset = 0x0;
sym->offset = 0x0; return false;
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->name = (*it).name.c_str(); sym->module = "[kernel]";
sym->module = "[kernel]"; sym->offset = addr - (*it).addr;
sym->offset = addr - (*it).addr; return true;
return true;
} }
bool KSyms::resolve_name(const char *name, uint64_t *addr) bool KSyms::resolve_name(const char *name, uint64_t *addr) {
{ refresh();
refresh();
if (_syms.size() != _sym_names.size()) { if (syms_.size() != symnames_.size()) {
_sym_names.clear(); symnames_.clear();
for (Symbol &sym : _syms) { for (Symbol &sym : syms_) {
_sym_names[sym.name] = sym.addr; symnames_[sym.name] = sym.addr;
} }
} }
auto it = _sym_names.find(name); auto it = symnames_.find(name);
if (it == _sym_names.end()) if (it == symnames_.end())
return false; return false;
*addr = it->second; *addr = it->second;
return true; return true;
} }
class ProcStat { class ProcStat {
std::string _procfs; std::string procfs_;
ino_t _inode; ino_t inode_;
ino_t get_inode() ino_t getinode_() {
{ struct stat s;
struct stat s; return (!stat(procfs_.c_str(), &s)) ? s.st_ino : -1;
return (!stat(_procfs.c_str(), &s)) ? s.st_ino : -1; }
}
public:
public: ProcStat(int pid) : inode_(-1) {
ProcStat(int pid) : _inode(-1) char buffer[128];
{ snprintf(buffer, sizeof(buffer), "/proc/%d/exe", pid);
char buffer[128]; procfs_ = buffer;
snprintf(buffer, sizeof(buffer), "/proc/%d/exe", pid); }
_procfs = buffer;
} bool is_stale() { return inode_ != getinode_(); }
void reset() { inode_ = getinode_(); }
bool is_stale() { return _inode != get_inode(); }
void reset() { _inode = get_inode(); }
}; };
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() &&
return str.size() >= suffix.size() && str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
} }
class ProcSyms : SymbolCache { class ProcSyms : SymbolCache {
struct Symbol { struct Symbol {
Symbol(const char *name, uint64_t start, uint64_t size, int flags = 0) : Symbol(const char *name, uint64_t start, uint64_t size, int flags = 0)
name(name), start(start), size(size), flags(flags) {} : name(name), start(start), size(size), flags(flags) {}
std::string name; std::string name;
uint64_t start; uint64_t start;
uint64_t size; uint64_t size;
int flags; int flags;
}; };
struct Module { struct Module {
Module(const char *name, uint64_t start, uint64_t end) : Module(const char *name, uint64_t start, uint64_t end)
_name(name), _start(start), _end(end) {} : name_(name), start_(start), end_(end) {}
std::string _name; std::string name_;
uint64_t _start; uint64_t start_;
uint64_t _end; uint64_t end_;
std::vector<Symbol> _syms; std::vector<Symbol> syms_;
void load_sym_table(); void load_sym_table();
bool decode_sym(uint64_t addr, struct bcc_symbol *sym); 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, static int _add_symbol(const char *symname, uint64_t start, uint64_t end,
uint64_t start, uint64_t end, int flags, void *p); int flags, void *p);
}; };
int _pid; int pid_;
std::vector<Module> _modules; std::vector<Module> modules_;
ProcStat _procstat; 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); ProcSyms(int pid);
virtual void refresh(); virtual void refresh();
virtual bool resolve_addr(uint64_t addr, struct bcc_symbol *sym); virtual bool resolve_addr(uint64_t addr, struct bcc_symbol *sym);
virtual bool resolve_name(const char *name, uint64_t *addr); virtual bool resolve_name(const char *name, uint64_t *addr);
}; };
ProcSyms::ProcSyms(int pid) : _pid(pid), _procstat(pid) ProcSyms::ProcSyms(int pid) : pid_(pid), procstat_(pid) { refresh(); }
{
refresh();
}
void ProcSyms::refresh() void ProcSyms::refresh() {
{ modules_.clear();
_modules.clear(); bcc_procutils_each_module(pid_, _add_module, this);
bcc_procutils_each_module(_pid, _add_module, this); procstat_.reset();
_procstat.reset();
} }
void ProcSyms::_add_module( void ProcSyms::_add_module(const char *modname, uint64_t start, uint64_t end,
const char *modname, uint64_t start, uint64_t end, void *payload) void *payload) {
{ ProcSyms *ps = static_cast<ProcSyms *>(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) bool ProcSyms::resolve_addr(uint64_t addr, struct bcc_symbol *sym) {
{ if (procstat_.is_stale())
if (_procstat.is_stale()) refresh();
refresh();
sym->module = nullptr; sym->module = nullptr;
sym->name = nullptr; sym->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.decode_sym(addr, sym); return mod.decode_sym(addr, sym);
} }
return false; return false;
} }
bool ProcSyms::resolve_name(const char *name, uint64_t *addr) bool ProcSyms::resolve_name(const char *name, uint64_t *addr) {
{ *addr = 0x0;
*addr = 0x0; return false;
return false;
} }
int ProcSyms::Module::_add_symbol(const char *symname, int ProcSyms::Module::_add_symbol(const char *symname, uint64_t start,
uint64_t start, uint64_t end, int flags, void *p) uint64_t end, int flags, void *p) {
{ Module *m = static_cast<Module *>(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;
return 0;
} }
void ProcSyms::Module::load_sym_table() void ProcSyms::Module::load_sym_table() {
{ if (syms_.size())
if (_syms.size()) return;
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) bool ProcSyms::Module::decode_sym(uint64_t addr, struct bcc_symbol *sym) {
{ uint64_t offset = is_so() ? (addr - start_) : addr;
uint64_t offset = is_so() ? (addr - _start) : addr;
load_sym_table(); load_sym_table();
sym->module = _name.c_str(); sym->module = name_.c_str();
sym->offset = offset; sym->offset = offset;
for (Symbol &s : _syms) { for (Symbol &s : syms_) {
if (offset >= s.start && offset <= (s.start + s.size)) { if (offset >= s.start && offset <= (s.start + s.size)) {
sym->name = s.name.c_str(); sym->name = s.name.c_str();
sym->offset = (offset - s.start); sym->offset = (offset - s.start);
return true; return true;
} }
} }
return false; return false;
} }
extern "C" { extern "C" {
void *bcc_symcache_new(int pid) void *bcc_symcache_new(int pid) {
{ if (pid < 0)
if (pid < 0) return static_cast<void *>(new KSyms());
return static_cast<void *>(new KSyms()); return static_cast<void *>(new ProcSyms(pid));
return static_cast<void *>(new ProcSyms(pid)); }
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(void *resolver, uint64_t addr, struct bcc_symbol *sym) int bcc_symcache_resolve_name(void *resolver, const char *name,
{ uint64_t *addr) {
SymbolCache *cache = static_cast<SymbolCache *>(resolver); SymbolCache *cache = static_cast<SymbolCache *>(resolver);
return cache->resolve_addr(addr, sym) ? 0 : -1; return cache->resolve_name(name, addr) ? 0 : -1;
} }
int bcc_symcache_resolve_name(void *resolver, const char *name, uint64_t *addr) void bcc_symcache_refresh(void *resolver) {
{ SymbolCache *cache = static_cast<SymbolCache *>(resolver);
SymbolCache *cache = static_cast<SymbolCache *>(resolver); cache->refresh();
return cache->resolve_name(name, addr) ? 0 : -1;
} }
void bcc_symcache_refresh(void *resolver) static int _find_sym(const char *symname, uint64_t addr, uint64_t end,
{ int flags, void *payload) {
SymbolCache *cache = static_cast<SymbolCache *>(resolver); struct bcc_symbol *sym = (struct bcc_symbol *)payload;
cache->refresh(); 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