Skip to content

Commit

Permalink
Add more preprocessors
Browse files Browse the repository at this point in the history
  • Loading branch information
hp77-creator committed Jun 8, 2024
1 parent 3872dd5 commit b1d110f
Show file tree
Hide file tree
Showing 20 changed files with 194 additions and 33 deletions.
11 changes: 9 additions & 2 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 Down Expand Up @@ -156,7 +161,9 @@ 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_static_lib_component_command(bpftime_syscall_trace_attach_impl)
endif(BPFTIME_BUILD_WITH_LIBBPF)
bpftime_add_static_lib_component_command(runtime)
bpftime_add_static_lib_component_command(spdlog)
add_custom_command(OUTPUT "libbpftime.a"
Expand All @@ -182,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 @@ -200,5 +207,5 @@ set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
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)
install(TARGETS CONFIGURATIONS Release Debug RelWithDebInfo DESTINATION ~/.bpftime)
endif()
18 changes: 18 additions & 0 deletions daemon/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ else()
)
endif()

if(${BPFTIME_BUILD_WITH_LIBBPF})
target_include_directories(libbpftime_daemon PRIVATE
${CMAKE_CURRENT_BINARY_DIR}
${LIBBPF_INCLUDE_DIRS}
Expand All @@ -61,6 +62,23 @@ target_link_libraries(libbpftime_daemon PRIVATE
PUBLIC
rt
)
else()
target_include_directories(libbpftime_daemon PRIVATE
${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/../vm/include
${CMAKE_CURRENT_SOURCE_DIR}/../runtime/include
)
target_link_libraries(libbpftime_daemon PRIVATE
elf
z
spdlog::spdlog
runtime
PUBLIC
rt
)
endif()

set_property(TARGET libbpftime_daemon PROPERTY CXX_STANDARD 20)

add_dependencies(bpftime_daemon libbpftime_daemon)
Expand Down
11 changes: 3 additions & 8 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,6 @@ 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 @@ -83,7 +76,7 @@ set(sources
extension/extension_helper.cpp
)

if(BPFTIME_BUILD_KERNEL_BPF)
if(BPFTIME_BUILD_KERNEL_BPF AND BPFTIME_BUILD_WITH_LIBBPF)
list(APPEND sources
src/bpf_map/shared/array_map_kernel_user.cpp
src/bpf_map/shared/hash_map_kernel_user.cpp
Expand Down Expand Up @@ -204,9 +197,11 @@ message(DEBUG "Successfully added all dependencies and linked against them.")

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

if(BPFTIME_BUILD_WITH_LIBBPF)
add_subdirectory(object)
add_subdirectory(agent)
add_subdirectory(syscall-server)
endif(BPFTIME_BUILD_WITH_LIBBPF)

#
# Unit testing setup
Expand Down
22 changes: 21 additions & 1 deletion runtime/agent/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ else()
add_dependencies(bpftime-agent FridaGum spdlog::spdlog bpftime_frida_uprobe_attach_impl)
endif()
set_property(TARGET bpftime-agent PROPERTY CXX_STANDARD 20)
if(BPFTIME_BUILD_WITH_LIBBPF)
target_include_directories(bpftime-agent
PRIVATE
${FRIDA_GUM_INSTALL_DIR}
Expand All @@ -24,5 +25,24 @@ target_link_libraries(bpftime-agent
-ldl
spdlog::spdlog
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()
2 changes: 2 additions & 0 deletions runtime/agent/agent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
#include "spdlog/common.h"
#include "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/sinks/stdout_sinks.h"
#if __linux__
#include "syscall_trace_attach_impl.hpp"
#include "syscall_trace_attach_private_data.hpp"
#endif
#include <chrono>
#include <csignal>
#include <exception>
Expand Down
7 changes: 6 additions & 1 deletion runtime/extension/extension_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
#include <vector>

#ifdef BPFTIME_ENABLE_IOURING_EXT
#ifdef USE_BPF
#include "liburing.h"
#endif
#endif

using namespace std;

Expand All @@ -47,7 +49,7 @@ uint64_t bpftime_path_join(const char *filename1, const char *filename2,
namespace bpftime
{
#ifdef BPFTIME_ENABLE_IOURING_EXT

#ifdef USE_BPF
static int submit_io_uring_write(struct io_uring *ring, int fd, char *buf,
size_t size)
{
Expand Down Expand Up @@ -126,6 +128,7 @@ uint64_t bpftime_io_uring_submit(void)
return io_uring_submit(&ring);
}
#endif
#endif

extern const bpftime_helper_group extesion_group = { {
{ UFUNC_HELPER_ID_FIND_ID,
Expand Down Expand Up @@ -155,6 +158,7 @@ extern const bpftime_helper_group extesion_group = { {
} },
#endif
#ifdef BPFTIME_ENABLE_IOURING_EXT
#ifdef USE_BPF
{ EXTENDED_HELPER_IOURING_INIT,
bpftime_helper_info{
.index = EXTENDED_HELPER_IOURING_INIT,
Expand Down Expand Up @@ -186,6 +190,7 @@ extern const bpftime_helper_group extesion_group = { {
.fn = (void *)bpftime_io_uring_submit,
} },
#endif
#endif
} };

} // namespace bpftime
4 changes: 4 additions & 0 deletions runtime/include/bpftime_epoll.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
<linux/perf_event.h>
<linux/types.h>
*/

#define EPOLLIN 0x001

union epoll_data {
void *ptr;
int fd;
Expand Down Expand Up @@ -592,4 +595,5 @@ enum perf_event_type {
PERF_RECORD_AUX_OUTPUT_HW_ID = 21,

PERF_RECORD_MAX, /* non-ABI */

};
10 changes: 6 additions & 4 deletions runtime/include/platform_utils.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#pragma once

#ifndef PLATFORM_UTIL_H
#define PLATFORM_UTIL_H
#include <cstdlib>
#include <functional>

#if defined(__linux__)
#if __linux__
#include <sched.h>
#elif defined(__APPLE__) && defined(__MACH__)
#elif __APPLE__
#include <sys/sysctl.h>
#include <pthread.h>
typedef int cpu_set_t;
Expand All @@ -23,8 +23,10 @@
}
int sched_getaffinity(pid_t pid, size_t cpusetsize, cpu_set_t *mask);
int sched_setaffinity(pid_t pid, size_t cpusetsize, const cpu_set_t *mask);
int sched_getcpu();
#else
#error "Unsupported platform"
#endif

int get_current_cpu();
#endif
8 changes: 5 additions & 3 deletions runtime/object/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@

add_library(bpftime-object OBJECT
bpf_object.cpp
)



if(${BPFTIME_BUILD_WITH_LIBBPF})
add_library(bpftime-object OBJECT
bpf_object.cpp
)
add_dependencies(bpftime-object copy_headers libbpf spdlog::spdlog bpftime_vm)
else()
add_dependencies(bpftime-object spdlog::spdlog bpftime_vm)
Expand Down
4 changes: 4 additions & 0 deletions runtime/object/bpf_object.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#if __linux__
#include <linux/bpf.h>
#endif
#include <string>
#include <list>
#include <memory>
Expand All @@ -8,11 +10,13 @@
using namespace std;
using namespace bpftime;

#ifdef USE_BPF
extern "C" {
#include <bpf/libbpf.h>
#include <bpf/btf.h>
#include <bpf/bpf.h>
}
#endif

namespace bpftime
{
Expand Down
43 changes: 36 additions & 7 deletions runtime/src/bpf_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,23 @@
* Copyright (c) 2022, eunomia-bpf org
* All rights reserved.
*/
#include <_types/_uint64_t.h>
#include <pthread.h>
#ifdef USE_BPF
#include "bpf/bpf.h"
#include "bpf/libbpf_common.h"
#endif
#include "bpftime_helper_group.hpp"
#include <cerrno>
#include <sched.h>
#ifdef ENABLE_BPFTIME_VERIFIER
#include "bpftime-verifier.hpp"
#endif

#if __linux__
#include <sched.h>
#elif __APPLE__
#include "platform_utils.hpp"
#endif
#include "spdlog/spdlog.h"
#include <map>
#include <stdio.h>
Expand Down Expand Up @@ -82,17 +91,31 @@ uint64_t bpftime_ktime_get_coarse_ns(uint64_t, uint64_t, uint64_t, uint64_t,
uint64_t)
{
timespec spec;
#ifdef __APPLE__
clock_gettime(CLOCK_MONOTONIC, &spec); // or CLOCK_MONOTONIC_RAW
#else
clock_gettime(CLOCK_MONOTONIC_COARSE, &spec);
#endif
return spec.tv_sec * (uint64_t)1000000000 + spec.tv_nsec;
}

uint64_t bpftime_get_current_pid_tgid(uint64_t, uint64_t, uint64_t, uint64_t,
uint64_t)
{
static int tgid = getpid();
#if __linux__
static thread_local int tid = -1;
#elif __APPLE__
static thread_local uint64_t tid = -1;
#endif
if (tid == -1)
tid = gettid();
{
#if __linux__
tid = gettid();
#elif __APPLE__
pthread_threadid_np(NULL, &tid);
#endif
}
return ((uint64_t)tgid << 32) | tid;
}

Expand Down Expand Up @@ -175,7 +198,11 @@ uint64_t bpf_ktime_get_coarse_ns(uint64_t, uint64_t, uint64_t, uint64_t,
uint64_t)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC_COARSE, &ts);
#ifdef __APPLE__
clock_gettime(CLOCK_MONOTONIC, &ts); // or CLOCK_MONOTONIC_RAW
#else
clock_gettime(CLOCK_MONOTONIC_COARSE, &spec);
#endif
return (uint64_t)ts.tv_sec * 1000000000 + ts.tv_nsec;
}

Expand Down Expand Up @@ -292,7 +319,7 @@ uint64_t bpf_perf_event_output(uint64_t ctx, uint64_t map, uint64_t flags,
sched_setaffinity(0, sizeof(orig), &orig);
return (uint64_t)ret;
}

#if __linux__
uint64_t bpftime_tail_call(uint64_t ctx, uint64_t prog_array, uint64_t index)
{
int fd = prog_array >> 32;
Expand Down Expand Up @@ -330,6 +357,7 @@ uint64_t bpftime_tail_call(uint64_t ctx, uint64_t prog_array, uint64_t index)
close(to_call_fd);
return run_opts.retval;
}
#endif

uint64_t bpftime_get_attach_cookie(uint64_t ctx, uint64_t, uint64_t, uint64_t,
uint64_t)
Expand Down Expand Up @@ -727,7 +755,7 @@ const bpftime_helper_group shm_maps_group = { {
} };

extern const bpftime_helper_group extesion_group;

#if __linux
const bpftime_helper_group kernel_helper_group = {
{ { BPF_FUNC_probe_read,
bpftime_helper_info{
Expand Down Expand Up @@ -886,20 +914,21 @@ const bpftime_helper_group kernel_helper_group = {
.fn = (void *)bpftime_get_attach_cookie } } }

};

#endif
// Utility function to get the UFUNC helper group
const bpftime_helper_group &bpftime_helper_group::get_ufunc_helper_group()
{
return extesion_group;
}

#if __linux
// Utility function to get the kernel utilities helper group
const bpftime_helper_group &
bpftime_helper_group::get_kernel_utils_helper_group()
{
return kernel_helper_group;
}

#endif
const bpftime_helper_group &bpftime_helper_group::get_shm_maps_helper_group()
{
return shm_maps_group;
Expand Down
Loading

0 comments on commit b1d110f

Please sign in to comment.