Skip to content

Commit

Permalink
hp77 | Add options to include build without libbpf
Browse files Browse the repository at this point in the history
  • Loading branch information
hp77-creator committed Jun 2, 2024
1 parent b4e37a2 commit 71c72f3
Show file tree
Hide file tree
Showing 14 changed files with 101 additions and 33 deletions.
11 changes: 8 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,9 @@ if(PROJECT_SOURCE_DIR STREQUAL PROJECT_BINARY_DIR)
message(FATAL_ERROR "In-source builds not allowed. Please make a new directory (called a build directory) and run CMake from there.\n")
endif()

include(cmake/libbpf.cmake)

if(${BPFTIME_BUILD_WITH_LIBBPF})
include(cmake/libbpf.cmake)
endif()
# install frida
include(cmake/frida.cmake)

Expand Down Expand Up @@ -196,4 +197,8 @@ add_subdirectory(benchmark)

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

install(TARGETS bpftime-agent bpftime_text_segment_transformer bpftime-syscall-server CONFIGURATIONS Release Debug RelWithDebInfo DESTINATION ~/.bpftime)
if(${BPFTIME_BUILD_WITH_LIBBPF})
install(TARGETS bpftime-agent bpftime_text_segment_transformer bpftime-syscall-server CONFIGURATIONS Release Debug RelWithDebInfo DESTINATION ~/.bpftime)
else()
install(TARGETS bpftime-agent CONFIGURATIONS Release Debug RelWithDebInfo DESTINATION ~/.bpftime)
endif()
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ build-iouring: ## build the package with iouring extension
cmake -Bbuild -DBPFTIME_ENABLE_IOURING_EXT=1 -DCMAKE_BUILD_TYPE:STRING=RelWithDebInfo
cmake --build build --config RelWithDebInfo -j$(JOBS)

build-wo-libbpf: ## build the package with iouring extension
cmake -Bbuild -DBPFTIME_ENABLE_IOURING_EXT=1 -DCMAKE_BUILD_TYPE:STRING=RelWithDebInfo -DBPFTIME_BUILD_WITH_LIBBPF=OFF
cmake --build build --config RelWithDebInfo -j$(JOBS)

release: ## build the release version
cmake -Bbuild -DCMAKE_BUILD_TYPE:STRING=RelWithDebInfo \
-DSPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_INFO
Expand Down
6 changes: 4 additions & 2 deletions attach/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ if(CMAKE_BUILD_TYPE STREQUAL "Debug")
endif()
add_subdirectory(base_attach_impl)
add_subdirectory(frida_uprobe_attach_impl)
add_subdirectory(syscall_trace_attach_impl)
add_subdirectory(text_segment_transformer)
if(${BPFTIME_BUILD_WITH_LIBBPF})
add_subdirectory(syscall_trace_attach_impl)
add_subdirectory(text_segment_transformer)
endif()
30 changes: 22 additions & 8 deletions attach/frida_uprobe_attach_impl/src/frida_attach_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,31 @@
#include <filesystem>
#include <spdlog/spdlog.h>
#include <frida-gum.h>
#include <unistd.h>
#if __APPLE__
#include <libproc.h>
#endif
static std::string get_executable_path()
{
char exec_path[PATH_MAX] = { 0 };
ssize_t len =
readlink("/proc/self/exe", exec_path, sizeof(exec_path) - 1);
if (len != -1) {
exec_path[len] = '\0'; // Null-terminate the string
SPDLOG_INFO("Executable path: {}", exec_path);
} else {
SPDLOG_ERROR("Error retrieving executable path: {}", errno);
}

#if __linux__
ssize_t len =
readlink("/proc/self/exe", exec_path, sizeof(exec_path) - 1);
if (len != -1) {
exec_path[len] = '\0'; // Null-terminate the string
SPDLOG_INFO("Executable path: {}", exec_path);
} else {
SPDLOG_ERROR("Error retrieving executable path: {}", errno);
}
#elif __APPLE__
pid_t pid = getpid();
if (proc_pidpath(pid, exec_path, sizeof(exec_path)) > 0) {
SPDLOG_INFO("Executable path: {}", exec_path);
} else {
SPDLOG_ERROR("Error retrieving executable path: {}", errno);
}
#endif
return exec_path;
}
namespace bpftime
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ int64_t syscall_trace_attach_impl::dispatch_syscall(int64_t sys_nr,
int64_t arg3, int64_t arg4,
int64_t arg5, int64_t arg6)
{
#if __linux__
if (sys_nr == __NR_exit_group || sys_nr == __NR_exit)
return orig_syscall(sys_nr, arg1, arg2, arg3, arg4, arg5, arg6);
#endif
SPDLOG_DEBUG("Syscall callback {} {} {} {} {} {} {}", sys_nr, arg1,
arg2, arg3, arg4, arg5, arg6);
// Indicate whether the return value is overridden
Expand Down
6 changes: 5 additions & 1 deletion attach/text_segment_transformer/agent-transformer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,12 @@ extern "C" void bpftime_agent_main(const gchar *data, gboolean *stay_resident)
cs_arch_register_x86();
bpftime::setup_syscall_tracer();
SPDLOG_DEBUG("Loading dynamic library..");
auto next_handle =
#if __linux__
auto next_handle =
dlmopen(LM_ID_NEWLM, agent_so, RTLD_NOW | RTLD_LOCAL);
#elif __APPLE__
auto next_handle = dlopen(agent_so, RTLD_NOW | RTLD_LOCAL);
#endif
if (next_handle == nullptr) {
SPDLOG_ERROR("Failed to open agent: {}", dlerror());
exit(1);
Expand Down
6 changes: 5 additions & 1 deletion benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
add_executable(simple-benchmark-with-embed-ebpf-calling test_embed.c)

add_dependencies(simple-benchmark-with-embed-ebpf-calling bpftime_vm libbpf)
if(${BPFTIME_BUILD_WITH_LIBBPF})
add_dependencies(simple-benchmark-with-embed-ebpf-calling bpftime_vm libbpf)
else()
add_dependencies(simple-benchmark-with-embed-ebpf-calling bpftime_vm)
endif()
target_compile_definitions(simple-benchmark-with-embed-ebpf-calling
PRIVATE
)
Expand Down
5 changes: 4 additions & 1 deletion cmake/StandardSettings.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,7 @@ option(BUILD_BPFTIME_DAEMON "Whether to build the bpftime daemon" ON)
option(BPFTIME_BUILD_KERNEL_BPF "Whether to build with bpf share maps" ON)

# whether to build single static library
option(BPFTIME_BUILD_STATIC_LIB "Whether to build a single static library for different archive files" OFF)
option(BPFTIME_BUILD_STATIC_LIB "Whether to build a single static library for different archive files" OFF)

# whether to build bpftime with libbpf and other linux headers
option(BPFTIME_BUILD_WITH_LIBBPF "Whether to build with libbpf and other linux headers" ON)
6 changes: 3 additions & 3 deletions cmake/frida.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ elseif(${FRIDA_OS_ARCH} MATCHES "linux-arm.*")
# Frida only has armhf builds..
set(FRIDA_OS_ARCH "linux-armhf")
elseif(${FRIDA_OS_ARCH} MATCHES "darwin-arm.*")
set(FRIDA_CORE_DEVKIT_SHA256 "50aea2d5dfff000ed1f1dc1fdb2db67a02e4c4f44e782fa311f8afa31df327b6")
set(FRIDA_GUM_DEVKIT_SHA256 "b40c08bebd6958d41d91b7819171a457448cccad5faf417c9b4901be54b6c633")
set(FRIDA_CORE_DEVKIT_SHA256 "7811e516e6b7bbc0153d30095560e0b1133f154060c5542764100d3e0eb2ab2b")
set(FRIDA_GUM_DEVKIT_SHA256 "03f6085ae5330cf38e0a498784500675fc5bd7361bb551a9097ba5fe397aceda")
# for macos-arm m* chip series
set(FRIDA_OS_ARCH "macos-arm64e")
set(FRIDA_OS_ARCH "macos-arm64")
else()
message(FATAL_ERROR "Unsupported frida arch ${FRIDA_OS_ARCH}")
endif()
Expand Down
31 changes: 20 additions & 11 deletions daemon/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# Create a target that builds the ebpf program
add_ebpf_program_target(bpftime_daemon_ebpf_target ${CMAKE_CURRENT_SOURCE_DIR}/kernel/bpf_tracer.bpf.c ${CMAKE_CURRENT_BINARY_DIR}/bpf_tracer.bpf.o)
if(${BPFTIME_BUILD_WITH_LIBBPF})
# Create a target that builds the ebpf program
add_ebpf_program_target(bpftime_daemon_ebpf_target ${CMAKE_CURRENT_SOURCE_DIR}/kernel/bpf_tracer.bpf.c ${CMAKE_CURRENT_BINARY_DIR}/bpf_tracer.bpf.o)

# Create a target that generated the bpf skeleton
add_bpf_skel_generating_target(bpftime_daemon_ebpf_skel ${CMAKE_CURRENT_BINARY_DIR}/bpf_tracer.bpf.o ${CMAKE_CURRENT_BINARY_DIR}/bpf_tracer.skel.h)
# Create a target that generated the bpf skeleton
add_bpf_skel_generating_target(bpftime_daemon_ebpf_skel ${CMAKE_CURRENT_BINARY_DIR}/bpf_tracer.bpf.o ${CMAKE_CURRENT_BINARY_DIR}/bpf_tracer.skel.h)

add_dependencies(bpftime_daemon_ebpf_skel bpftime_daemon_ebpf_target)
add_dependencies(bpftime_daemon_ebpf_skel bpftime_daemon_ebpf_target)
endif()

add_executable(embedfile ${CMAKE_CURRENT_SOURCE_DIR}/assets/embedfile.c)

Expand All @@ -27,12 +29,19 @@ add_executable(bpftime_daemon
user/main.cpp
)

add_dependencies(libbpftime_daemon
bpftime_daemon_ebpf_skel
libbpf
spdlog::spdlog
runtime
)
if(${BPFTIME_BUILD_WITH_LIBBPF})
add_dependencies(libbpftime_daemon
bpftime_daemon_ebpf_skel
libbpf
spdlog::spdlog
runtime
)
else()
add_dependencies(libbpftime_daemon
spdlog::spdlog
runtime
)
endif()

target_include_directories(libbpftime_daemon PRIVATE
${CMAKE_CURRENT_BINARY_DIR}
Expand Down
13 changes: 12 additions & 1 deletion runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ endif()
#
find_package(Boost REQUIRED)

if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
message(STATUS "Boost include dirs: ${Boost_INCLUDE_DIRS}")
message(STATUS "Boost libraries: ${Boost_LIBRARIES}")
else()
message(FATAL_ERROR "Boost not found. Please install Boost and set BOOST_ROOT if necessary.")
endif()
# Find all headers and implementation files
if(NOT DEFINED ARCH)
set(ARCH ${CMAKE_SYSTEM_PROCESSOR})
Expand Down Expand Up @@ -130,7 +137,11 @@ target_link_libraries(${PROJECT_NAME}
spdlog::spdlog
bpftime_base_attach_impl
)
add_dependencies(${PROJECT_NAME} bpftime_vm FridaGum spdlog::spdlog libbpf bpftime_base_attach_impl)
if(${BPFTIME_BUILD_WITH_LIBBPF})
add_dependencies(${PROJECT_NAME} bpftime_vm FridaGum spdlog::spdlog libbpf bpftime_base_attach_impl)
else()
add_dependencies(${PROJECT_NAME} bpftime_vm FridaGum spdlog::spdlog bpftime_base_attach_impl)
endif()

if(BPFTIME_ENABLE_IOURING_EXT)
target_link_libraries(${PROJECT_NAME}
Expand Down
6 changes: 5 additions & 1 deletion runtime/agent/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
add_library(bpftime-agent SHARED
agent.cpp
)
add_dependencies(bpftime-agent FridaGum spdlog::spdlog bpftime_frida_uprobe_attach_impl bpftime_syscall_trace_attach_impl)
if(${BPFTIME_BUILD_WITH_LIBBPF})
add_dependencies(bpftime-agent FridaGum spdlog::spdlog bpftime_frida_uprobe_attach_impl bpftime_syscall_trace_attach_impl)
else()
add_dependencies(bpftime-agent FridaGum spdlog::spdlog bpftime_frida_uprobe_attach_impl)
endif()
set_property(TARGET bpftime-agent PROPERTY CXX_STANDARD 20)
target_include_directories(bpftime-agent
PRIVATE
Expand Down
2 changes: 2 additions & 0 deletions runtime/include/bpftime_shm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
#include <boost/interprocess/containers/string.hpp>
#include <cstdint>
#include <ebpf-vm.h>
#if __Linux__
#include <sys/epoll.h>
#endif

namespace bpftime
{
Expand Down
6 changes: 5 additions & 1 deletion runtime/object/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ add_library(bpftime-object OBJECT
bpf_object.cpp
)

add_dependencies(bpftime-object copy_headers libbpf spdlog::spdlog bpftime_vm)
if(${BPFTIME_BUILD_WITH_LIBBPF})
add_dependencies(bpftime-object copy_headers libbpf spdlog::spdlog bpftime_vm)
else()
add_dependencies(bpftime-object spdlog::spdlog bpftime_vm)
endif()

target_link_libraries(bpftime-object
PUBLIC
Expand Down

0 comments on commit 71c72f3

Please sign in to comment.