Skip to content

Commit

Permalink
Fall back to objdump/grep if bcc is older
Browse files Browse the repository at this point in the history
  • Loading branch information
acj committed Apr 18, 2019
1 parent 382b9b7 commit fdd02ec
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions CMakeLists.txt
Expand Up @@ -62,6 +62,7 @@ endif()
set(CMAKE_REQUIRED_LIBRARIES bcc)
check_symbol_exists(bcc_prog_load "${LIBBCC_INCLUDE_DIRS}/libbpf.h" HAVE_BCC_PROG_LOAD)
check_symbol_exists(bcc_create_map "${LIBBCC_INCLUDE_DIRS}/libbpf.h" HAVE_BCC_CREATE_MAP)
check_symbol_exists(bcc_elf_foreach_sym "${LIBBCC_INCLUDE_DIRS}/bcc_elf.h" HAVE_BCC_ELF_FOREACH_SYM)

include(CheckTypeSize)
set(CMAKE_EXTRA_INCLUDE_FILES linux/bpf.h)
Expand Down
3 changes: 3 additions & 0 deletions src/CMakeLists.txt
Expand Up @@ -24,6 +24,9 @@ endif(HAVE_BCC_PROG_LOAD)
if(HAVE_BCC_CREATE_MAP)
target_compile_definitions(bpftrace PRIVATE HAVE_BCC_CREATE_MAP)
endif(HAVE_BCC_CREATE_MAP)
if(HAVE_BCC_ELF_FOREACH_SYM)
target_compile_definitions(bpftrace PRIVATE HAVE_BCC_ELF_FOREACH_SYM)
endif(HAVE_BCC_ELF_FOREACH_SYM)
if(HAVE_GET_CURRENT_CGROUP_ID)
target_compile_definitions(bpftrace PRIVATE HAVE_GET_CURRENT_CGROUP_ID)
endif(HAVE_GET_CURRENT_CGROUP_ID)
Expand Down
19 changes: 18 additions & 1 deletion src/bpftrace.cpp
Expand Up @@ -14,9 +14,13 @@
#include <sys/wait.h>
#include <fcntl.h>
#include <unistd.h>

#ifdef HAVE_BCC_ELF_FOREACH_SYM
#include <linux/elf.h>

#include "bcc_elf.h"
#endif

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

Expand Down Expand Up @@ -1558,12 +1562,18 @@ uint64_t BPFtrace::resolve_cgroupid(const std::string &path)

uint64_t BPFtrace::resolve_uname(const std::string &name, const std::string &path)
{
#ifdef HAVE_BCC_ELF_FOREACH_SYM
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);

return sym.offset;
#else
std::string call_str = std::string("objdump -tT ") + path + " | grep -w " + name;
const char *call = call_str.c_str();
auto result = exec_system(call);
return read_address_from_output(result);
#endif
}

int add_symbol(const char *symname, uint64_t start, uint64_t size, void *payload) {
Expand All @@ -1574,6 +1584,7 @@ int add_symbol(const char *symname, uint64_t start, uint64_t size, void *payload

std::string BPFtrace::extract_func_symbols_from_path(const std::string &path)
{
#ifdef HAVE_BCC_ELF_FOREACH_SYM
bcc_symbol_option symbol_option = {
.use_debug_file = 1,
.check_debug_file_crc = 1,
Expand All @@ -1586,6 +1597,12 @@ std::string BPFtrace::extract_func_symbols_from_path(const std::string &path)
throw std::runtime_error("Could not list function symbols: " + path);

return syms.str();
#else
std::string call_str = std::string("objdump -tT ") + path +
+ " | " + "grep \"F .text\" | grep -oE '[^[:space:]]+$'";
const char *call = call_str.c_str();
return exec_system(call);
#endif
}

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

0 comments on commit fdd02ec

Please sign in to comment.