diff --git a/CMakeLists.txt b/CMakeLists.txt index fc1e4cd2..733d46eb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) @@ -196,4 +197,8 @@ add_subdirectory(benchmark) set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$: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() diff --git a/Makefile b/Makefile index 87f787a7..3db7d0a7 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/attach/CMakeLists.txt b/attach/CMakeLists.txt index 70654644..a808370d 100644 --- a/attach/CMakeLists.txt +++ b/attach/CMakeLists.txt @@ -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() diff --git a/attach/frida_uprobe_attach_impl/src/frida_attach_utils.cpp b/attach/frida_uprobe_attach_impl/src/frida_attach_utils.cpp index 90093db7..68a4cfac 100644 --- a/attach/frida_uprobe_attach_impl/src/frida_attach_utils.cpp +++ b/attach/frida_uprobe_attach_impl/src/frida_attach_utils.cpp @@ -3,17 +3,31 @@ #include #include #include +#include +#if __APPLE__ +#include +#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 diff --git a/attach/syscall_trace_attach_impl/src/syscall_trace_attach_impl.cpp b/attach/syscall_trace_attach_impl/src/syscall_trace_attach_impl.cpp index a7db98cd..22703d64 100644 --- a/attach/syscall_trace_attach_impl/src/syscall_trace_attach_impl.cpp +++ b/attach/syscall_trace_attach_impl/src/syscall_trace_attach_impl.cpp @@ -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 diff --git a/attach/text_segment_transformer/agent-transformer.cpp b/attach/text_segment_transformer/agent-transformer.cpp index 9ce9bfea..1ddcd687 100644 --- a/attach/text_segment_transformer/agent-transformer.cpp +++ b/attach/text_segment_transformer/agent-transformer.cpp @@ -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); diff --git a/benchmark/CMakeLists.txt b/benchmark/CMakeLists.txt index 7ef2c10d..c83aa9ce 100644 --- a/benchmark/CMakeLists.txt +++ b/benchmark/CMakeLists.txt @@ -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 ) diff --git a/cmake/StandardSettings.cmake b/cmake/StandardSettings.cmake index 2404225f..95f29580 100644 --- a/cmake/StandardSettings.cmake +++ b/cmake/StandardSettings.cmake @@ -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) \ No newline at end of file +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) \ No newline at end of file diff --git a/cmake/frida.cmake b/cmake/frida.cmake index 67419a2a..06318846 100644 --- a/cmake/frida.cmake +++ b/cmake/frida.cmake @@ -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() diff --git a/daemon/CMakeLists.txt b/daemon/CMakeLists.txt index 12cc6c03..8af91fb5 100644 --- a/daemon/CMakeLists.txt +++ b/daemon/CMakeLists.txt @@ -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) @@ -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} diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 4ec1f325..426357b7 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -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}) @@ -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} diff --git a/runtime/agent/CMakeLists.txt b/runtime/agent/CMakeLists.txt index 3aa4013a..657cb20f 100644 --- a/runtime/agent/CMakeLists.txt +++ b/runtime/agent/CMakeLists.txt @@ -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 diff --git a/runtime/include/bpftime_shm.hpp b/runtime/include/bpftime_shm.hpp index e2c58fcf..45b6a23e 100644 --- a/runtime/include/bpftime_shm.hpp +++ b/runtime/include/bpftime_shm.hpp @@ -11,7 +11,9 @@ #include #include #include +#if __linux__ #include +#endif namespace bpftime { diff --git a/runtime/object/CMakeLists.txt b/runtime/object/CMakeLists.txt index b57f956f..7f092901 100644 --- a/runtime/object/CMakeLists.txt +++ b/runtime/object/CMakeLists.txt @@ -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