Skip to content

Commit

Permalink
Address TODO items related to objdump dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
acj committed Apr 18, 2019
1 parent 0f636c9 commit 382b9b7
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions src/bpftrace.cpp
Expand Up @@ -14,7 +14,9 @@
#include <sys/wait.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/elf.h>

#include "bcc_elf.h"
#include "bcc_syms.h"
#include "perf_reader.h"

Expand Down Expand Up @@ -1556,25 +1558,34 @@ uint64_t BPFtrace::resolve_cgroupid(const std::string &path)

uint64_t BPFtrace::resolve_uname(const std::string &name, const std::string &path)
{
uint64_t addr = 0;
bcc_symbol sym;
int err = bcc_resolve_symname(path.c_str(), name.c_str(), 0, 0, nullptr, &sym);
if (err)
throw std::runtime_error("Could not resolve symbol: " + path + ":" + name);

// TODO: switch from objdump to library call, perhaps bcc_resolve_symname()
std::string call_str = std::string("objdump -tT ") + path + " | grep -w " + name;
const char *call = call_str.c_str();
auto result = exec_system(call);
addr = read_address_from_output(result);
return sym.offset;
}

return addr;
int add_symbol(const char *symname, uint64_t start, uint64_t size, void *payload) {
auto syms = static_cast<std::ostringstream*>(payload);
*syms << std::string(symname) << std::endl;
return 0;
}

std::string BPFtrace::extract_func_symbols_from_path(const std::string &path)
{
// TODO: switch from objdump to library call, perhaps bcc_resolve_symname()
std::string call_str = std::string("objdump -tT ") + path +
+ " | " + "grep \"F .text\" | grep -oE '[^[:space:]]+$'";
bcc_symbol_option symbol_option = {
.use_debug_file = 1,
.check_debug_file_crc = 1,
.use_symbol_type = (1 << STT_FUNC) | (1 << STT_GNU_IFUNC)
};

std::ostringstream syms;
int err = bcc_elf_foreach_sym(path.c_str(), add_symbol, &symbol_option, &syms);
if (err)
throw std::runtime_error("Could not list function symbols: " + path);

const char *call = call_str.c_str();
return exec_system(call);
return syms.str();
}

uint64_t BPFtrace::read_address_from_output(std::string output)
Expand Down

0 comments on commit 382b9b7

Please sign in to comment.