Skip to content

Commit

Permalink
Support resolving cgroupid only if name_to_handle_at exists
Browse files Browse the repository at this point in the history
Currently used image of alpine has a version of musl libc that does
not have name_to_handle_at function that is required to resolve the
cgroup id. In such case we just use an implementation that says that
resolving cgroupid is not supported.
  • Loading branch information
krnowak committed Oct 15, 2018
1 parent 4a0e7de commit 86f6ca4
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Expand Up @@ -60,6 +60,11 @@ add_flex_bison_dependency(flex_lexer bison_parser)
add_library(parser ${BISON_bison_parser_OUTPUTS} ${FLEX_flex_lexer_OUTPUTS})
target_include_directories(parser PUBLIC src src/ast ${CMAKE_BINARY_DIR})

include(CheckSymbolExists)
set(CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
check_symbol_exists(name_to_handle_at "sys/types.h;sys/stat.h;fcntl.h" HAVE_NAME_TO_HANDLE_AT)
set(CMAKE_REQUIRED_DEFINITIONS)

find_package(LLVM REQUIRED)
include_directories(${LLVM_INCLUDE_DIRS})
add_definitions(${LLVM_DEFINITIONS})
Expand Down
3 changes: 3 additions & 0 deletions src/CMakeLists.txt
Expand Up @@ -13,6 +13,9 @@ add_executable(bpftrace
list.cpp
)

if(HAVE_NAME_TO_HANDLE_AT)
target_compile_definitions(bpftrace PRIVATE HAVE_NAME_TO_HANDLE_AT=1)
endif(HAVE_NAME_TO_HANDLE_AT)
target_link_libraries(bpftrace arch ast parser resources)

ExternalProject_Get_Property(bcc source_dir binary_dir)
Expand Down
11 changes: 11 additions & 0 deletions src/bpftrace.cpp
Expand Up @@ -1287,6 +1287,8 @@ uint64_t BPFtrace::resolve_kname(const std::string &name)
return addr;
}

#ifdef HAVE_NAME_TO_HANDLE_AT

namespace
{

Expand Down Expand Up @@ -1333,6 +1335,15 @@ uint64_t BPFtrace::resolve_cgroupid(const std::string &path)
return cfh.cgid;
}

#else

uint64_t BPFtrace::resolve_cgroupid(const std::string &path)
{
throw std::runtime_error("cgroupid is not supported on this system");
}

#endif

uint64_t BPFtrace::resolve_uname(const std::string &name, const std::string &path)
{
uint64_t addr = 0;
Expand Down
3 changes: 3 additions & 0 deletions tests/CMakeLists.txt
Expand Up @@ -24,6 +24,9 @@ add_executable(bpftrace_test
${CMAKE_SOURCE_DIR}/src/types.cpp
)

if(HAVE_NAME_TO_HANDLE_AT)
target_compile_definitions(bpftrace_test PRIVATE HAVE_NAME_TO_HANDLE_AT=1)
endif(HAVE_NAME_TO_HANDLE_AT)
target_link_libraries(bpftrace_test arch ast parser resources)

ExternalProject_Get_Property(bcc source_dir binary_dir)
Expand Down
4 changes: 4 additions & 0 deletions tests/codegen.cpp
Expand Up @@ -1128,6 +1128,8 @@ TEST(codegen, call_uaddr)
// TODO: test uaddr()
}

#ifdef HAVE_NAME_TO_HANDLE_AT

TEST(codegen, call_cgroup)
{
test("tracepoint:syscalls:sys_enter_openat /cgroup == 0x100000001/ { @x = cgroup }",
Expand Down Expand Up @@ -1172,6 +1174,8 @@ attributes #1 = { argmemonly nounwind }
)EXPECTED");
}

#endif

TEST(codegen, call_hist)
{
test("kprobe:f { @x = hist(pid) }",
Expand Down

0 comments on commit 86f6ca4

Please sign in to comment.