Skip to content

Commit 0d244b7

Browse files
fr0m-scratchfr0m_scratchfr0m_scratch
authored
refactor: Make base_attach_impl into a header-only library fix eunomia-bpf#233 (eunomia-bpf#245)
* refactor: Make base_attach_impl into a header-only library fix eunomia-bpf#233 * deleted source files from base_attach_impl * moved functional codes to header files * handled thread_local variables with inline specifier * modified CMakeLists to reflect the changes * formatted base_attach_impl code --------- Co-authored-by: fr0m_scratch <[email protected]> Co-authored-by: fr0m_scratch <[email protected]>
1 parent 4cb2abd commit 0d244b7

File tree

5 files changed

+65
-78
lines changed

5 files changed

+65
-78
lines changed
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1+
add_library(bpftime_base_attach_impl INTERFACE)
12

3+
set(BASE_ATTACH_IMPL_INCLUDE
4+
${CMAKE_CURRENT_SOURCE_DIR}
5+
CACHE STRING "Include path of base_attach_impl")
26

3-
add_library(bpftime_base_attach_impl STATIC
4-
base_attach_impl.cpp
5-
attach_private_data.cpp
6-
)
7-
8-
set(BASE_ATTACH_IMPL_INCLUDE ${CMAKE_CURRENT_SOURCE_DIR} CACHE STRING "Include path of base_attach_impl")
7+
target_include_directories(bpftime_base_attach_impl
8+
INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
99

1010
add_dependencies(bpftime_base_attach_impl spdlog::spdlog)
1111

12-
target_link_libraries(bpftime_base_attach_impl PRIVATE spdlog::spdlog)
13-
target_include_directories(bpftime_base_attach_impl PRIVATE ${SPDLOG_INCLUDE})
12+
target_link_libraries(bpftime_base_attach_impl INTERFACE spdlog::spdlog)
1413

1514
set_property(TARGET bpftime_base_attach_impl PROPERTY CXX_STANDARD 20)

attach/base_attach_impl/attach_private_data.cpp

Lines changed: 0 additions & 14 deletions
This file was deleted.

attach/base_attach_impl/attach_private_data.hpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,27 @@
22
#define _BPFTIME_ATTACH_PRIVATE_DATA_HPP
33

44
#include <string_view>
5+
#include "spdlog/spdlog.h"
6+
#include <stdexcept>
57
namespace bpftime
68
{
79
namespace attach
810
{
911
// A base class for all attach-independent private data
1012
struct attach_private_data {
11-
virtual ~attach_private_data();
13+
virtual ~attach_private_data(){};
1214
// Initialize this private data structure from a string.
1315
// This function should be implemented by things like
1416
// `uprobe_attach_private_data` or `syscall_attach_private_data` or
15-
// other ones. In this way, we provide a unified interface to create private data
16-
// for different attaches
17-
virtual int initialize_from_string(const std::string_view &sv);
17+
// other ones. In this way, we provide a unified interface to create
18+
// private data for different attaches.
19+
virtual int initialize_from_string(const std::string_view &sv)
20+
{
21+
SPDLOG_ERROR(
22+
"Not implemented: attach_private_data::initialize_from_string");
23+
throw std::runtime_error(
24+
"attach_private_data::initialize_from_string");
25+
}
1826
};
1927
} // namespace attach
2028
} // namespace bpftime

attach/base_attach_impl/base_attach_impl.cpp

Lines changed: 0 additions & 49 deletions
This file was deleted.

attach/base_attach_impl/base_attach_impl.hpp

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ namespace bpftime
1010
namespace attach
1111
{
1212
using override_return_set_callback = std::function<void(uint64_t, uint64_t)>;
13+
1314
// Used by filter to record the return value
14-
extern thread_local std::optional<override_return_set_callback>
15+
// Use inline thread_local to ensure ODR
16+
inline thread_local std::optional<override_return_set_callback>
1517
curr_thread_override_return_callback;
1618

1719
// A wrapper function for an entry function of an ebpf program
@@ -23,6 +25,7 @@ class base_attach_impl {
2325
public:
2426
// Detach a certain attach entry by a local attach id
2527
virtual int detach_by_id(int id) = 0;
28+
2629
// Create an attach entry with an ebpf callback. To avoid messing up the
2730
// code base, we don't use bpftime_prog here, instead, we require a
2831
// callback accept the same argument as bpftime_prog::run The callback
@@ -31,14 +34,54 @@ class base_attach_impl {
3134
virtual int create_attach_with_ebpf_callback(
3235
ebpf_run_callback &&cb, const attach_private_data &private_data,
3336
int attach_type) = 0;
37+
3438
// Allocate a new attach entry id
35-
int allocate_id();
36-
virtual ~base_attach_impl();
39+
int allocate_id()
40+
{
41+
return next_id++;
42+
}
43+
44+
virtual ~base_attach_impl(){};
3745

3846
private:
3947
int next_id = 1;
4048
};
4149
} // namespace attach
4250
} // namespace bpftime
4351

52+
// The use of extern "C" allows the function to be called from C code
53+
extern "C" {
54+
55+
// Set the return value of the current context
56+
inline uint64_t bpftime_set_retval(uint64_t value)
57+
{
58+
using namespace bpftime::attach;
59+
if (curr_thread_override_return_callback.has_value()) {
60+
curr_thread_override_return_callback.value()(0, value);
61+
} else {
62+
spdlog::error(
63+
"Called bpftime_set_retval, but no retval callback was set");
64+
assert(false);
65+
}
66+
return 0;
67+
}
68+
69+
// Override the return value of the current context
70+
inline uint64_t bpftime_override_return(uint64_t ctx, uint64_t value)
71+
{
72+
using namespace bpftime::attach;
73+
if (curr_thread_override_return_callback.has_value()) {
74+
spdlog::debug("Overriding return value for ctx {:x} with {}",
75+
ctx, value);
76+
curr_thread_override_return_callback.value()(ctx, value);
77+
} else {
78+
spdlog::error(
79+
"Called bpftime_override_return, but no retval callback was set");
80+
assert(false);
81+
}
82+
return 0;
83+
}
84+
85+
} // extern "C"
86+
4487
#endif

0 commit comments

Comments
 (0)