Skip to content

Commit

Permalink
Fix #48 (libbpf building issue) (#58)
Browse files Browse the repository at this point in the history
* Revert changes

Try fixing

Update

Fix building of old libbpf

* Fix CI

* Fix CI

* Fix CI

* Remove bundled libbpf; use bpftool instead
  • Loading branch information
Officeyutong authored Oct 28, 2023
1 parent 3471e62 commit e0c3ae7
Show file tree
Hide file tree
Showing 16 changed files with 67 additions and 33 deletions.
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
[submodule "third_party/libbpf"]
path = third_party/libbpf
url = https://github.com/libbpf/libbpf.git
[submodule "third_party/bpftool"]
path = third_party/bpftool
url = https://github.com/libbpf/bpftool
Expand Down
17 changes: 9 additions & 8 deletions cmake/libbpf.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,41 @@ ExternalProject_Add(libbpf

# Set BpfObject input parameters -- note this is usually not necessary unless
# you're in a highly vendored environment (like libbpf-bootstrap)
set(LIBBPF_INCLUDE_DIRS ${CMAKE_CURRENT_BINARY_DIR}/libbpf)
set(LIBBPF_INCLUDE_DIRS ${CMAKE_CURRENT_BINARY_DIR}/libbpf/)
set(LIBBPF_LIBRARIES ${CMAKE_CURRENT_BINARY_DIR}/libbpf/libbpf.a)

set(header_output_list)

function(copy_header TARGET SRC_DIR TARGET_DIR)
function(copy_header SRC_DIR TARGET_DIR)
file(GLOB_RECURSE FILES RELATIVE "${SRC_DIR}" "${SRC_DIR}/*")
message(STATUS "copying ${FILES} from ${SRC_DIR} to ${TARGET_DIR}")

foreach(file ${FILES})
get_filename_component(PARENT_DIR "${TARGET_DIR}/${file}" DIRECTORY)
add_custom_command(
PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory ${PARENT_DIR}
COMMAND ${CMAKE_COMMAND} -E copy
${SRC_DIR}/${file}
${TARGET_DIR}/${file}
COMMENT "Copying file ${HEADER_DIRS}/${file} to ${TARGET_DIR}/${file}"
BYPRODUCTS ${TARGET_DIR}/${file}
COMMENT "Copying file ${SRC_DIR}/${file} to ${TARGET_DIR}/${file}"
OUTPUT ${TARGET_DIR}/${file}
DEPENDS ${SRC_DIR}/${file}
)
list(APPEND header_output_list ${TARGET_DIR}/${file})
set(header_output_list ${header_output_list} PARENT_SCOPE)
endforeach()
endfunction()

copy_header(copy_headers "${LIBBPF_DIR}/include/linux" "${LIBBPF_INCLUDE_DIRS}/linux")
copy_header(copy_headers "${LIBBPF_DIR}/include/uapi/linux" "${LIBBPF_INCLUDE_DIRS}/linux")

copy_header("${LIBBPF_DIR}/include/linux" "${LIBBPF_INCLUDE_DIRS}/linux")
copy_header("${LIBBPF_DIR}/include/uapi/" "${LIBBPF_INCLUDE_DIRS}/uapi")
copy_header("${LIBBPF_DIR}/include/uapi/linux" "${LIBBPF_INCLUDE_DIRS}/linux")
add_custom_target(copy_headers ALL
COMMENT "Copying headers"
DEPENDS ${header_output_list}
)

message(STATUS "All headers to copy: ${header_output_list}")

set(HEADER_FILES relo_core.h hashmap.h nlattr.h libbpf_internal.h)

foreach(file ${HEADER_FILES})
Expand Down
1 change: 0 additions & 1 deletion daemon/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ add_dependencies(libbpftime_daemon

target_include_directories(libbpftime_daemon PRIVATE
${CMAKE_CURRENT_BINARY_DIR}
${LIBBPF_INCLUDE_DIRS}/uapi
${LIBBPF_INCLUDE_DIRS}
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/../vm/include
Expand Down
25 changes: 12 additions & 13 deletions daemon/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,27 @@ set(TEST_SOURCES
add_executable(bpftime_daemon_tests ${TEST_SOURCES})
set_property(TARGET bpftime_daemon_tests PROPERTY CXX_STANDARD 20)
add_dependencies(bpftime_daemon_tests runtime bpftime-object)
target_link_libraries(bpftime_daemon_tests PRIVATE
runtime
bpftime-object
Catch2::Catch2WithMain
libbpftime_daemon
target_link_libraries(bpftime_daemon_tests PRIVATE
runtime
bpftime-object
Catch2::Catch2WithMain
libbpftime_daemon
)
target_include_directories(bpftime_daemon_tests
target_include_directories(bpftime_daemon_tests
PRIVATE
${BPFTIME_RUNTIME_INCLUDE}
${BPFTIME_OBJECT_INCLUDE_DIRS}
${Catch2_INCLUDE}
${BPFTIME_RUNTIME_INCLUDE}
${BPFTIME_OBJECT_INCLUDE_DIRS}
${Catch2_INCLUDE}
${Boost_INCLUDE}
../
)
add_test(NAME bpftime_daemon_tests COMMAND bpftime_daemon_tests)

# These are necessary ebpf program required by the test
# set(used_ebpf_programs
# uprobe
# # replace # Not used now
# filter)

# uprobe
# # replace # Not used now
# filter)
foreach(current ${used_ebpf_programs})
set(curr_target_name bpftime_test_ebpf_prog_${current})
add_ebpf_program_target(${curr_target_name} ${CMAKE_CURRENT_SOURCE_DIR}/assets/${current}.bpf.c ${CMAKE_CURRENT_BINARY_DIR}/${current}.bpf.o)
Expand Down
4 changes: 2 additions & 2 deletions daemon/user/bpftime_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ int bpftime_driver::check_and_create_prog_related_maps(
map_ids.resize(info->nr_map_ids);
new_info.nr_map_ids = info->nr_map_ids;
new_info.map_ids = (unsigned long long)(uintptr_t)map_ids.data();
int res = bpf_prog_get_info_by_fd(fd, &new_info, &info_len);
int res = bpf_obj_get_info_by_fd(fd, &new_info, &info_len);
if (res < 0) {
spdlog::error(
"Failed to get prog info for fd {} to find related maps",
Expand Down Expand Up @@ -204,7 +204,7 @@ int bpftime_driver::bpftime_maps_create_server(int kernel_id)
}
bpf_map_info info = {};
uint32_t info_len = sizeof(info);
int res = bpf_map_get_info_by_fd(map_fd, &info, &info_len);
int res = bpf_obj_get_info_by_fd(map_fd, &info, &info_len);
if (res < 0) {
spdlog::error("Failed to get map info for id {}", kernel_id);
return -1;
Expand Down
2 changes: 1 addition & 1 deletion daemon/user/handle_bpf_event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,4 +380,4 @@ bpf_event_handler::bpf_event_handler(struct daemon_config config,
}

perf_type_id_strings[kprobe_type] = "PERF_TYPE_KPROBE";
}
}
2 changes: 1 addition & 1 deletion example/malloc/malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ int main(int argc, char **argv)
goto cleanup;
}
LIBBPF_OPTS(bpf_uprobe_opts, attach_opts, .func_name = "malloc",
.retprobe = false, .attach_mode = PROBE_ATTACH_MODE_PERF);
.retprobe = false);
struct bpf_link *attach = bpf_program__attach_uprobe_opts(
skel->progs.do_count, -1, "/lib/x86_64-linux-gnu/libc.so.6", 0,
&attach_opts);
Expand Down
8 changes: 8 additions & 0 deletions example/sslsniff/sslsniff.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
#include <bpf/bpf_tracing.h>
#include "sslsniff.h"

#ifndef BPF_UPROBE
#define BPF_UPROBE BPF_KPROBE
#endif
#ifndef BPF_URETPROBE
#define BPF_URETPROBE BPF_KRETPROBE
#endif


struct {
__uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
__uint(key_size, sizeof(__u32));
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/bpf_map/array_map_kernel_user.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void array_map_kernel_user_impl::init_map_fd()
}
bpf_map_info info = {};
unsigned int info_len = sizeof(info);
int res = bpf_map_get_info_by_fd(map_fd, &info, &info_len);
int res = bpf_obj_get_info_by_fd(map_fd, &info, &info_len);
if (res < 0) {
spdlog::error("Failed to get info for kernel map id {}",
kernel_map_id);
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/bpf_map/hash_map_kernel_user.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void hash_map_kernel_user_impl::init_map_fd()
}
bpf_map_info info = {};
unsigned int info_len = sizeof(info);
int res = bpf_map_get_info_by_fd(map_fd, &info, &info_len);
int res = bpf_obj_get_info_by_fd(map_fd, &info, &info_len);
if (res < 0) {
spdlog::error("Failed to get info for kernel map id {}",
kernel_map_id);
Expand Down
7 changes: 7 additions & 0 deletions runtime/test/bpf/replace.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
#include "bpf/bpf_helpers.h"
#include "ffi.bpf.h"

#ifndef BPF_UPROBE
#define BPF_UPROBE BPF_KPROBE
#endif
#ifndef BPF_URETPROBE
#define BPF_URETPROBE BPF_KRETPROBE
#endif

static inline int add_func(int a, int b)
{
return FFI_CALL_NAME_2("add_func", a, b);
Expand Down
7 changes: 7 additions & 0 deletions runtime/unit-test/assets/filter.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
#include "bpf/bpf_helpers.h"
#include "ffi.bpf.h"

#ifndef BPF_UPROBE
#define BPF_UPROBE BPF_KPROBE
#endif
#ifndef BPF_URETPROBE
#define BPF_URETPROBE BPF_KRETPROBE
#endif

enum PatchOp {
OP_SKIP,
OP_RESUME,
Expand Down
7 changes: 7 additions & 0 deletions runtime/unit-test/assets/replace.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
#include "bpf/bpf_helpers.h"
#include "ffi.bpf.h"

#ifndef BPF_UPROBE
#define BPF_UPROBE BPF_KPROBE
#endif
#ifndef BPF_URETPROBE
#define BPF_URETPROBE BPF_KRETPROBE
#endif

static inline int add_func(int a, int b)
{
return FFI_CALL_NAME_2("add_func", a, b);
Expand Down
9 changes: 9 additions & 0 deletions runtime/unit-test/assets/uprobe.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_helpers.h>

#ifndef BPF_UPROBE
#define BPF_UPROBE BPF_KPROBE
#endif
#ifndef BPF_URETPROBE
#define BPF_URETPROBE BPF_KRETPROBE
#endif



SEC("uprobe")
int BPF_UPROBE(my_function_uprobe, int parm1, char* str, char c) {
bpf_printk("BPF_UPROBE: %d %s %c\n", parm1, str, c);
Expand Down
2 changes: 1 addition & 1 deletion third_party/bpftool
1 change: 0 additions & 1 deletion third_party/libbpf
Submodule libbpf deleted from e26b84
1 change: 1 addition & 0 deletions third_party/libbpf

0 comments on commit e0c3ae7

Please sign in to comment.