diff --git a/runtime/src/attach/bpf_attach_ctx.cpp b/runtime/src/attach/bpf_attach_ctx.cpp index b5aa5bed..173f22e9 100644 --- a/runtime/src/attach/bpf_attach_ctx.cpp +++ b/runtime/src/attach/bpf_attach_ctx.cpp @@ -189,7 +189,30 @@ int bpf_attach_ctx::init_attach_ctx_from_handlers( return res; } for (auto v : prog_handler.attach_fds) { - handler_prog_fds[v].emplace_back(i, prog); + if (std::holds_alternative< + bpf_perf_event_handler>( + manager->get_handler(v))) { + const auto &perf_handler = + std::get( + manager->get_handler( + v)); + if (perf_handler.enabled) { + handler_prog_fds[v].emplace_back( + i, prog); + spdlog::debug( + "Program fd {} attached to perf event handler {}", + i, v); + } else { + spdlog::info( + "Ignore perf {} attached by prog fd {}. It's not enabled", + v, i); + } + + } else { + spdlog::warn( + "Program fd {} attached to a non-perf event handler {}", + i, v); + } } spdlog::debug("Load prog fd={} name={}", i, prog_handler.name); @@ -209,13 +232,6 @@ int bpf_attach_ctx::init_attach_ctx_from_handlers( return -1; } } - for (const auto &[k, v] : handler_prog_fds) { - for (auto y : v) { - spdlog::debug( - "Program fd {} attached to perf event handler {}", - y.first, k); - } - } // Second, we create bpf perf event handlers for (std::size_t i = 0; i < manager->size(); i++) { if (!manager->is_allocated(i)) { diff --git a/runtime/src/handler/handler_manager.cpp b/runtime/src/handler/handler_manager.cpp index de071632..ec5e3121 100644 --- a/runtime/src/handler/handler_manager.cpp +++ b/runtime/src/handler/handler_manager.cpp @@ -1,4 +1,9 @@ +#include "handler/perf_event_handler.hpp" +#include "handler/prog_handler.hpp" +#include "spdlog/spdlog.h" #include +#include +#include namespace bpftime { handler_manager::handler_manager(managed_shared_memory &mem, @@ -30,7 +35,7 @@ std::size_t handler_manager::size() const } int handler_manager::set_handler(int fd, handler_variant &&handler, - managed_shared_memory &memory) + managed_shared_memory &memory) { if (is_allocated(fd)) { spdlog::error("set_handler failed for fd {} aleady exists", fd); @@ -58,11 +63,34 @@ void handler_manager::clear_fd_at(int fd, managed_shared_memory &memory) } if (std::holds_alternative(handlers[fd])) { std::get(handlers[fd]).map_free(memory); + } else if (std::holds_alternative( + handlers[fd])) { + // Clean attached programs.. + spdlog::debug("Destroying perf event handler {}", fd); + for (size_t i = 0; i < handlers.size(); i++) { + auto &handler = handlers[i]; + if (std::holds_alternative(handler)) { + auto &prog_handler = + std::get(handler); + auto &attach_fds = prog_handler.attach_fds; + auto new_tail = + std::remove(attach_fds.begin(), + attach_fds.end(), fd); + if (new_tail != attach_fds.end()) { + spdlog::debug( + "Destroy attach of perf event {} to prog {}", + fd, i); + attach_fds.resize(new_tail - + attach_fds.begin()); + } + } + } } handlers[fd] = unused_handler(); } -int handler_manager::find_minimal_unused_idx() const { +int handler_manager::find_minimal_unused_idx() const +{ for (std::size_t i = 0; i < handlers.size(); i++) { if (!is_allocated(i)) { return i; diff --git a/runtime/src/handler/perf_event_handler.cpp b/runtime/src/handler/perf_event_handler.cpp index c228c96b..2ee3ffb8 100644 --- a/runtime/src/handler/perf_event_handler.cpp +++ b/runtime/src/handler/perf_event_handler.cpp @@ -59,6 +59,9 @@ bpf_perf_event_handler::bpf_perf_event_handler( type = bpf_event_type::BPF_TYPE_UPROBE; } this->_module_name = module_name; + spdlog::info( + "Created uprobe/uretprobe perf event handler, module name {}, offset {:x}", + module_name, offset); } // create tracepoint diff --git a/runtime/src/handler/perf_event_handler.hpp b/runtime/src/handler/perf_event_handler.hpp index 3fcf30b0..e09dbc4c 100644 --- a/runtime/src/handler/perf_event_handler.hpp +++ b/runtime/src/handler/perf_event_handler.hpp @@ -92,16 +92,25 @@ using software_perf_event_weak_ptr = boost::interprocess::managed_weak_ptr< // perf event handler struct bpf_perf_event_handler { bpf_event_type type; + mutable bool enabled = false; int enable() const { + enabled = true; // TODO: implement enable logic. // If This is a server, should inject the agent into the target // process. + + spdlog::info( + "Enabling perf event for module name: {}, offset {:x}", + _module_name.c_str(), offset); return 0; } int disable() const { - spdlog::debug("Disabling perf event, but nothing todo"); + spdlog::info( + "Disabling perf event for module name: {}, offset {:x}", + _module_name.c_str(), offset); + enabled = false; return 0; } uint64_t offset;