Skip to content

Commit

Permalink
Merge branch 'master' into add-xdp-example
Browse files Browse the repository at this point in the history
  • Loading branch information
yunwei37 authored Aug 30, 2024
2 parents 8bb0e86 + 0d8402e commit 72db983
Show file tree
Hide file tree
Showing 16 changed files with 442 additions and 110 deletions.
16 changes: 15 additions & 1 deletion .github/workflows/test-examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,24 @@ jobs:
needs: [build-runtime]
container:
image: "manjusakalza/bpftime-base-image:${{matrix.container.image}}"
options: --privileged -v /sys/kernel/debug/:/sys/kernel/debug:rw -v /sys/kernel/tracing:/sys/kernel/tracing:rw
options: " ${{matrix.privilege_options.options}}"
strategy:
fail-fast: true
matrix:
exclude:
- privilege_options:
enable: false
examples:
syscall_trace: true
- privilege_options:
enable: false
examples:
path: tailcall_minimal
privilege_options:
- options: "--privileged -v /sys/kernel/debug/:/sys/kernel/debug:rw -v /sys/kernel/tracing:/sys/kernel/tracing:rw"
enable: true
- options: ""
enable: false
container:
- image: ubuntu-2204
name: ubuntu
Expand Down
92 changes: 50 additions & 42 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@ function(bpftime_add_static_lib_component_command target)
)
set(BPFTIME_STATIC_LIB_AR_CMDS ${BPFTIME_STATIC_LIB_AR_CMDS} ${CMDS} PARENT_SCOPE)
endif()

set(BPFTIME_STATIC_LIB_DEPS ${BPFTIME_STATIC_LIB_DEPS} ${target} PARENT_SCOPE)
endfunction()

function(bpftime_add_libs_component_command target_path)
get_filename_component(target_name ${target_path} NAME)
string(REGEX REPLACE "^lib" "" target_name ${target_name})
string(REGEX REPLACE "\.a$" "" target_name ${target_name})

if(APPLE)
if(CMAKE_AR_NAME STREQUAL "ar")
list(APPEND CMDS
Expand Down Expand Up @@ -117,22 +119,27 @@ endif()
if(${BPFTIME_BUILD_WITH_LIBBPF})
include(cmake/libbpf.cmake)
endif()

# install frida
include(cmake/frida.cmake)


set(CMAKE_POSITION_INDEPENDENT_CODE YES)

if(${ENABLE_EBPF_VERIFIER})
add_subdirectory(bpftime-verifier)
else()
message(STATUS "Skipping ebpf verifier")

# Catch2
add_subdirectory(third_party/Catch2)
if(NOT DEFINED Catch2_INCLUDE)
message(STATUS "Adding Catch2 by subdirectory")
add_subdirectory(third_party/Catch2)
endif()
endif()

# spdlog
add_subdirectory(third_party/spdlog)

if(NOT DEFINED SPDLOG_ACTIVE_LEVEL)
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
add_compile_definitions(SPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_TRACE)
Expand All @@ -141,8 +148,6 @@ if(NOT DEFINED SPDLOG_ACTIVE_LEVEL)
endif()
endif()



set(SPDLOG_INCLUDE ${CMAKE_CURRENT_SOURCE_DIR}/third_party/spdlog/include)

# argparse
Expand All @@ -155,52 +160,55 @@ add_subdirectory(attach)

add_subdirectory(runtime)


# add to single archive if option is enabled
if (${BPFTIME_BUILD_STATIC_LIB})
set(UBPF_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR}/vm/ubpf-vm/ubpf)
message(STATUS " Adding libraries to single static archive file")
bpftime_add_static_lib_component_command(bpftime_vm)
if(${BPFTIME_LLVM_JIT})
bpftime_add_static_lib_component_command(bpftime_llvm_vm)
else()
bpftime_add_static_lib_component_command(bpftime_ubpf_vm)
bpftime_add_libs_component_command(${UBPF_BUILD_DIR}/lib/libubpf.a)
endif()
bpftime_add_libs_component_command(${FRIDA_GUM_INSTALL_DIR}/libfrida-gum.a)
bpftime_add_static_lib_component_command(bpftime_frida_uprobe_attach_impl)
if(${BPFTIME_BUILD_WITH_LIBBPF})
bpftime_add_libs_component_command(${CMAKE_CURRENT_BUILD_DIR}/libbpf/libbpf/libbpf.a)
bpftime_add_static_lib_component_command(bpftime_syscall_trace_attach_impl)
endif()
bpftime_add_static_lib_component_command(runtime)
bpftime_add_static_lib_component_command(spdlog)
add_custom_command(OUTPUT "libbpftime.a"
if(${BPFTIME_BUILD_STATIC_LIB})
set(UBPF_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR}/vm/ubpf-vm/ubpf)
message(STATUS " Adding libraries to single static archive file")
bpftime_add_static_lib_component_command(bpftime_vm)

if(${BPFTIME_LLVM_JIT})
bpftime_add_static_lib_component_command(bpftime_llvm_vm)
else()
bpftime_add_static_lib_component_command(bpftime_ubpf_vm)
bpftime_add_libs_component_command(${UBPF_BUILD_DIR}/lib/libubpf.a)
endif()

bpftime_add_libs_component_command(${FRIDA_GUM_INSTALL_DIR}/libfrida-gum.a)
bpftime_add_static_lib_component_command(bpftime_frida_uprobe_attach_impl)

if(${BPFTIME_BUILD_WITH_LIBBPF})
bpftime_add_libs_component_command(${CMAKE_CURRENT_BUILD_DIR}/libbpf/libbpf/libbpf.a)
bpftime_add_static_lib_component_command(bpftime_syscall_trace_attach_impl)
endif()

bpftime_add_static_lib_component_command(runtime)
bpftime_add_static_lib_component_command(spdlog)
add_custom_command(OUTPUT "libbpftime.a"
${BPFTIME_STATIC_LIB_AR_CMDS}
${BPFTIME_STATIC_LLVM_LIB_AR_CMDS}
COMMAND ${CMAKE_AR} -qcs libbpftime.a objs/*/*.o
COMMAND ${CMAKE_COMMAND} -E remove_directory objs
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
DEPENDS ${BPFTIME_STATIC_LIB_DEPS}
)
add_custom_target(bpftime_static_target ALL DEPENDS "libbpftime.a")
add_library(bpftime_static STATIC IMPORTED GLOBAL)
add_dependencies(bpftime_static bpftime_static_target)

set_target_properties(bpftime_static
PROPERTIES
IMPORTED_LOCATION "${CMAKE_CURRENT_BINARY_DIR}/libbpftime.a"
)
)
add_custom_target(bpftime_static_target ALL DEPENDS "libbpftime.a")
add_library(bpftime_static STATIC IMPORTED GLOBAL)
add_dependencies(bpftime_static bpftime_static_target)

install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libbpftime.a
DESTINATION ~/.bpftime
)
set_target_properties(bpftime_static
PROPERTIES
IMPORTED_LOCATION "${CMAKE_CURRENT_BINARY_DIR}/libbpftime.a"
)

install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libbpftime.a
DESTINATION ~/.bpftime
)
endif()

if (${BUILD_BPFTIME_DAEMON} AND ${BPFTIME_BUILD_WITH_LIBBPF})
if(${BUILD_BPFTIME_DAEMON} AND ${BPFTIME_BUILD_WITH_LIBBPF})
add_subdirectory(daemon)
endif()

add_subdirectory(tools)

if(${BUILD_ATTACH_IMPL_EXAMPLE})
Expand All @@ -209,16 +217,16 @@ endif()

# benchmark that requires bpftime libraries
if(${BPFTIME_BUILD_WITH_LIBBPF})
# Currently benchmark is using libbpf
add_subdirectory(benchmark)
# Currently benchmark is using libbpf
add_subdirectory(benchmark)
endif()


set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")

set(DEST_DIR "$ENV{HOME}/.bpftime")

if(${BPFTIME_BUILD_WITH_LIBBPF})
install(TARGETS bpftime-agent bpftime_text_segment_transformer bpftime-syscall-server CONFIGURATIONS Release Debug RelWithDebInfo DESTINATION ${DEST_DIR})
install(TARGETS bpftime-agent bpftime_text_segment_transformer bpftime-syscall-server CONFIGURATIONS Release Debug RelWithDebInfo DESTINATION ${DEST_DIR})
else()
install(TARGETS bpftime-agent bpftime-syscall-server CONFIGURATIONS Release Debug RelWithDebInfo DESTINATION ${DEST_DIR})
install(TARGETS bpftime-agent bpftime-syscall-server CONFIGURATIONS Release Debug RelWithDebInfo DESTINATION ${DEST_DIR})
endif()
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ RUN git submodule update --init --recursive
ENV CXX=g++
ENV CC=gcc
RUN make release
ENV PATH="${PATH}:/root/.bpftime/"
ENV PATH="${PATH}:/root/.bpftime/"
3 changes: 2 additions & 1 deletion attach/frida_uprobe_attach_impl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ set(TEST_SOURCES
)
add_executable(bpftime_frida_uprobe_attach_tests ${TEST_SOURCES})

if(${ENABLE_EBPF_VERIFIER})
if(${ENABLE_EBPF_VERIFIER} AND NOT TARGET Catch2)
message(STATUS "Adding Catch2 by FetchContent for frida_uprobe_attach_impl")
Include(FetchContent)
FetchContent_Declare(
Catch2
Expand Down
3 changes: 2 additions & 1 deletion attach/syscall_trace_attach_impl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ set(TEST_SOURCES
)
add_executable(bpftime_syscall_trace_attach_tests ${TEST_SOURCES})

if(${ENABLE_EBPF_VERIFIER})
if(${ENABLE_EBPF_VERIFIER} AND NOT TARGET Catch2)
message(STATUS "Adding Catch2 by FetchContent at syscall_trace_attach_impl")
Include(FetchContent)
FetchContent_Declare(
Catch2
Expand Down
21 changes: 12 additions & 9 deletions bpftime-verifier/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,18 @@ set(TEST_SOURCES
test/non_kernel_helper.cpp
)

Include(FetchContent)

FetchContent_Declare(
Catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v3.0.1
)

FetchContent_MakeAvailable(Catch2)
if(NOT TARGET Catch2)
message(STATUS "Adding Catch2 by FetchContent at bpftime-verifier")
Include(FetchContent)

FetchContent_Declare(
Catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v3.0.1
)

FetchContent_MakeAvailable(Catch2)
endif()

add_executable(bpftime_verifier_tests ${TEST_SOURCES})
add_dependencies(bpftime_verifier_tests bpftime-verifier)
Expand Down
4 changes: 3 additions & 1 deletion daemon/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
if(${ENABLE_EBPF_VERIFIER})
if(${ENABLE_EBPF_VERIFIER} AND NOT TARGET Catch2)
message(STATUS "Adding Catch2 by FetchContent at bpftime-daemon")
Include(FetchContent)
FetchContent_Declare(
Catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v3.4.0
)
FetchContent_MakeAvailable(Catch2)

# if not enable verifier, we will use the catch2 from submodule
endif()

Expand Down
57 changes: 30 additions & 27 deletions runtime/src/bpftime_shm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,35 @@

#ifdef __APPLE__
// Custom implementation for sigtimedwait
int sigtimedwait(const sigset_t *set, siginfo_t *info, const struct timespec *timeout) {
struct timespec start, now;
clock_gettime(CLOCK_REALTIME, &start);
int sig;

while (true) {
// Try to wait for a signal
if (sigwait(set, &sig) == 0) {
if (info != nullptr) {
memset(info, 0, sizeof(*info));
info->si_signo = sig;
}
return sig;
}

// Check if the timeout has expired
clock_gettime(CLOCK_REALTIME, &now);
if ((now.tv_sec - start.tv_sec) > timeout->tv_sec ||
((now.tv_sec - start.tv_sec) == timeout->tv_sec && (now.tv_nsec - start.tv_nsec) > timeout->tv_nsec)) {
errno = EAGAIN;
return -1;
}

// Sleep for a short time before retrying
usleep(1000); // Sleep for 1ms before retrying
}
int sigtimedwait(const sigset_t *set, siginfo_t *info,
const struct timespec *timeout)
{
struct timespec start, now;
clock_gettime(CLOCK_REALTIME, &start);
int sig;

while (true) {
// Try to wait for a signal
if (sigwait(set, &sig) == 0) {
if (info != nullptr) {
memset(info, 0, sizeof(*info));
info->si_signo = sig;
}
return sig;
}

// Check if the timeout has expired
clock_gettime(CLOCK_REALTIME, &now);
if ((now.tv_sec - start.tv_sec) > timeout->tv_sec ||
((now.tv_sec - start.tv_sec) == timeout->tv_sec &&
(now.tv_nsec - start.tv_nsec) > timeout->tv_nsec)) {
errno = EAGAIN;
return -1;
}

// Sleep for a short time before retrying
usleep(1000); // Sleep for 1ms before retrying
}
}
#endif

Expand Down Expand Up @@ -544,7 +547,7 @@ int bpftime_shared_perf_event_output(int map_fd, const void *buf, size_t sz)
return -1;
}
}
#endif
#endif

int bpftime_is_prog_array(int fd)
{
Expand Down
2 changes: 1 addition & 1 deletion runtime/syscall-server/syscall-server.version
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
global: epoll_wait; epoll_ctl; epoll_create1; ioctl; mmap64; mmap; close; syscall; munmap;
global: epoll_wait; epoll_ctl; epoll_create1; ioctl; mmap64; mmap; close; syscall; munmap; open; openat; read; fopen; fopen64; _IO_new_fopen;
local: *;
};
Loading

0 comments on commit 72db983

Please sign in to comment.