Skip to content

Commit

Permalink
hp77 | Add Option for build without libbpf (#302)
Browse files Browse the repository at this point in the history
* hp77 | Add options to include build without libbpf and fix errors

* Failing at spinlock implementation

* Fix volatile errors

* Add more preprocessors

* first successful build on macOS

* Add suggestion and fix cli issue

* Fix clock_gettime method

* Fix failing tests

* Fix definition variable

* Fix sched_getpu()

* Fix get_sched_cpu

* fix cpu_getsched

* Add deleted syscall dependency

* add changes for spinlock and include suggestions

* Add apple guard in map_handler and handler_manager

* Add syscall-server as well

* use [[maybe_unused]]

* undo changes in daemon CMakeList and remove daemon at project level

* Add static archive library under the flag condition

* Define variables at global level for cli

* remove debug log

* Remove libbpf flag from daemon

* Change nanosecond to assembly instruction

* remove for loop for spin-lock implementation

* remove gnu/lib-names.h

* remove define for consts, include gnu-lib-names-header, convert function from define to inline, change __thread to thread_local

* add error log for no cpu

* Remove benchmark checks

* Fix dependency

* Replace (void)variable name with [[maybe_unused]]

* remove guard from bpftimetool

* fix spacing

* fix spacing issue

* Remove ENABLE_IO_URING flag from makefile

* Fix indent

* Remove unnecessary header, change preprocessor directives

* Remove guard for object directory
  • Loading branch information
hp77-creator authored Jun 25, 2024
1 parent fb8402f commit 88a7958
Show file tree
Hide file tree
Showing 47 changed files with 1,969 additions and 97 deletions.
26 changes: 21 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ endif()

message(STATUS "Started CMake for ${PROJECT_NAME} v${PROJECT_VERSION}...\n")

# if option to build without libbpf is set
if(${BPFTIME_BUILD_WITH_LIBBPF})
add_definitions(-DUSE_LIBBPF)
endif()

if(UNIX)
add_compile_options("$<$<CONFIG:DEBUG>:-D_DEBUG>") # this will allow to use same _DEBUG macro available in both Linux as well as Windows - MSCV environment. Easy to put Debug specific code.
endif(UNIX)
Expand All @@ -109,8 +114,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 @@ -153,9 +159,11 @@ 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_libs_component_command(${CMAKE_CURRENT_BUILD_DIR}/libbpf/libbpf/libbpf.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"
Expand All @@ -181,7 +189,7 @@ DESTINATION ~/.bpftime

endif()

if (${BUILD_BPFTIME_DAEMON})
if (${BUILD_BPFTIME_DAEMON} AND ${BPFTIME_BUILD_WITH_LIBBPF})
add_subdirectory(daemon)
endif()
add_subdirectory(tools)
Expand All @@ -191,9 +199,17 @@ if(${BUILD_ATTACH_IMPL_EXAMPLE})
endif()

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


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)
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})
else()
install(TARGETS bpftime-agent bpftime-syscall-server CONFIGURATIONS Release Debug RelWithDebInfo DESTINATION ${DEST_DIR})
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 -DCMAKE_BUILD_TYPE:STRING=RelWithDebInfo -DBPFTIME_BUILD_WITH_LIBBPF=OFF -DBPFTIME_BUILD_KERNEL_BPF=OFF
cmake --build build --config RelWithDebInfo --target install -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(UNIX AND NOT APPLE)
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
3 changes: 1 addition & 2 deletions attach/text_segment_transformer/agent-transformer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ 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 =
dlmopen(LM_ID_NEWLM, agent_so, RTLD_NOW | RTLD_LOCAL);
auto next_handle = dlmopen(LM_ID_NEWLM, agent_so, RTLD_NOW | RTLD_LOCAL);
if (next_handle == nullptr) {
SPDLOG_ERROR("Failed to open agent: {}", dlerror());
exit(1);
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)
8 changes: 4 additions & 4 deletions cmake/frida.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ elseif(${FRIDA_OS_ARCH} MATCHES "linux-arm.*")
set(FRIDA_GUM_DEVKIT_SHA256 "6b5963eb740062aec6c22c46ec2944a68006f72d290f714fb747ffe75b448a60")
# 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")
elseif(${FRIDA_OS_ARCH} MATCHES "darwin-arm64")
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
1 change: 1 addition & 0 deletions daemon/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ target_link_libraries(libbpftime_daemon PRIVATE
PUBLIC
rt
)

set_property(TARGET libbpftime_daemon PROPERTY CXX_STANDARD 20)

add_dependencies(bpftime_daemon libbpftime_daemon)
Expand Down
53 changes: 48 additions & 5 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ endif()
# Create library, setup header and source files
#
find_package(Boost REQUIRED)

# Find all headers and implementation files
if(NOT DEFINED ARCH)
set(ARCH ${CMAKE_SYSTEM_PROCESSOR})
Expand All @@ -65,6 +64,7 @@ set(sources
src/bpftime_prog.cpp
src/ufunc.cpp
src/bpf_helper.cpp
src/platform_utils.cpp

src/bpf_map/userspace/array_map.cpp
src/bpf_map/userspace/hash_map.cpp
Expand All @@ -76,7 +76,7 @@ set(sources
extension/extension_helper.cpp
)

if(BPFTIME_BUILD_KERNEL_BPF)
if(UNIX AND NOT APPLE)
list(APPEND sources
src/bpf_map/shared/array_map_kernel_user.cpp
src/bpf_map/shared/hash_map_kernel_user.cpp
Expand All @@ -90,9 +90,9 @@ set(headers
include/
)
message(INFO " Headers: ${headers}")

message(INFO " Found the following sources: ${sources}")


if(CMAKE_BUILD_TYPE STREQUAL "Debug")
add_definitions(-DSPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_DEBUG)
else()
Expand All @@ -114,6 +114,7 @@ add_custom_target(
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/syscall_id_list.h
)

message(INFO " Found the following boost include dirs : ${Boost_INCLUDE_DIRS}")
target_include_directories(${PROJECT_NAME}
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/../vm/include
Expand All @@ -122,6 +123,7 @@ target_include_directories(${PROJECT_NAME}
${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/../third_party
${SPDLOG_INCLUDE}
${Boost_INCLUDE_DIRS}
)

target_link_libraries(${PROJECT_NAME}
Expand All @@ -130,7 +132,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 Expand Up @@ -166,6 +172,17 @@ set_project_warnings(runtime)

message(DEBUG "Applied compiler warnings. Using standard ${CMAKE_CXX_STANDARD}.")

if(APPLE)
find_library(COCOA_LIBRARY Cocoa)
find_library(FOUNDATION_LIBRARY Foundation)
find_library(APPKIT_LIBRARY AppKit)
find_library(COREF_LIBRARY CoreFoundation)
find_library(RESOLV_LIBRARY resolv)
mark_as_advanced(COCOA_LIBRARY FOUNDATION_LIBRARY APPKIT_LIBRARY COREF_LIBRARY RESOLV_LIBRARY)
set(EXTRA_LIBS ${COCOA_LIBRARY} ${FOUNDATION_LIBRARY} ${APPKIT_LIBRARY} ${COREF_LIBRARY} ${RESOLV_LIBRARY})
endif()

if(${BPFTIME_BUILD_WITH_LIBBPF})
target_link_libraries(${PROJECT_NAME}
PUBLIC
${LIBBPF_LIBRARIES}
Expand All @@ -176,8 +193,24 @@ target_link_libraries(${PROJECT_NAME}
-lz
-lelf
bpftime_base_attach_impl
${EXTRA_LIBS}
)
else()
target_link_libraries(${PROJECT_NAME}
PUBLIC
${FRIDA_GUM_INSTALL_DIR}/libfrida-gum.a
-lpthread
-lm
-ldl
-lz
bpftime_base_attach_impl
${EXTRA_LIBS}
)
endif()



if(BPFTIME_BUILD_WITH_LIBBPF)
target_include_directories(${PROJECT_NAME} PUBLIC
${LIBBPF_INCLUDE_DIRS}/uapi
${LIBBPF_INCLUDE_DIRS}
Expand All @@ -188,15 +221,25 @@ target_include_directories(${PROJECT_NAME} PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
)
else()
target_include_directories(${PROJECT_NAME} PUBLIC
${FRIDA_GUM_INSTALL_DIR}
$<INSTALL_INTERFACE:runtime>
$<INSTALL_INTERFACE:runtime/src>
$<INSTALL_INTERFACE:include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
)
endif()

message(DEBUG "Successfully added all dependencies and linked against them.")

set(BPFTIME_RUNTIME_INCLUDE ${CMAKE_CURRENT_SOURCE_DIR}/include ${CMAKE_CURRENT_SOURCE_DIR}/src)


add_subdirectory(object)
add_subdirectory(agent)
add_subdirectory(syscall-server)

#
# Unit testing setup
#
Expand Down
27 changes: 26 additions & 1 deletion runtime/agent/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
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_target_properties(bpftime-agent PROPERTIES CXX_STANDARD 20 LINK_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/agent.version)
target_link_options(bpftime-agent PRIVATE -Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/agent.version)
if(${BPFTIME_BUILD_WITH_LIBBPF})
target_include_directories(bpftime-agent
PRIVATE
${FRIDA_GUM_INSTALL_DIR}
Expand All @@ -23,3 +28,23 @@ target_link_libraries(bpftime-agent
bpftime_frida_uprobe_attach_impl
bpftime_syscall_trace_attach_impl
)

else()
target_include_directories(bpftime-agent
PRIVATE
${FRIDA_GUM_INSTALL_DIR}
../include
../../third_party/
${SPDLOG_INCLUDE}
${FRIDA_UPROBE_ATTACH_IMPL_INCLUDE}
)
target_link_libraries(bpftime-agent
${FRIDA_GUM_INSTALL_DIR}/libfrida-gum.a
runtime
-lpthread
-lm
-ldl
spdlog::spdlog
bpftime_frida_uprobe_attach_impl
)
endif()
12 changes: 10 additions & 2 deletions runtime/agent/agent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
#include "spdlog/common.h"
#include "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/sinks/stdout_sinks.h"
#include "syscall_trace_attach_impl.hpp"
#include "syscall_trace_attach_private_data.hpp"
#include <chrono>
#include <csignal>
#include <exception>
Expand All @@ -23,6 +21,10 @@
#include "bpftime_shm.hpp"
#include <spdlog/spdlog.h>
#include <spdlog/cfg/env.h>
#if __linux__
#include "syscall_trace_attach_impl.hpp"
#include "syscall_trace_attach_private_data.hpp"
#endif
using namespace bpftime;
using namespace bpftime::attach;
using main_func_t = int (*)(int, char **, char **);
Expand Down Expand Up @@ -119,6 +121,7 @@ extern "C" void bpftime_agent_main(const gchar *data, gboolean *stay_resident)
getpid());
}
ctx_holder.init();
#if __linux__
// Register syscall trace impl
auto syscall_trace_impl = std::make_unique<syscall_trace_attach_impl>();
syscall_trace_impl->set_original_syscall_function(orig_hooker);
Expand All @@ -136,6 +139,7 @@ extern "C" void bpftime_agent_main(const gchar *data, gboolean *stay_resident)
}
return priv_data;
});
#endif
// Register uprobe attach impl
ctx_holder.ctx.register_attach_impl(
{ ATTACH_UPROBE, ATTACH_URETPROBE, ATTACH_UPROBE_OVERRIDE,
Expand Down Expand Up @@ -172,6 +176,9 @@ extern "C" void bpftime_agent_main(const gchar *data, gboolean *stay_resident)
SPDLOG_INFO("Attach successfully");
}

// using definition for libbpf for syscall issues
// maybe should separate libbpf and kernel features separately
#if __linux__
extern "C" int64_t syscall_callback(int64_t sys_nr, int64_t arg1, int64_t arg2,
int64_t arg3, int64_t arg4, int64_t arg5,
int64_t arg6)
Expand All @@ -189,3 +196,4 @@ _bpftime__setup_syscall_trace_callback(syscall_hooker_func_t *hooker)
bpftime_agent_main("", &val);
SPDLOG_INFO("Agent syscall trace setup exiting..");
}
#endif
Loading

0 comments on commit 88a7958

Please sign in to comment.