Skip to content

Commit

Permalink
Fix an attaching issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Officeyutong committed Nov 8, 2023
1 parent f19ce74 commit c7c303a
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 11 deletions.
32 changes: 24 additions & 8 deletions runtime/src/attach/bpf_attach_ctx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<bpf_perf_event_handler>(
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);
Expand All @@ -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)) {
Expand Down
32 changes: 30 additions & 2 deletions runtime/src/handler/handler_manager.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
#include "handler/perf_event_handler.hpp"
#include "handler/prog_handler.hpp"
#include "spdlog/spdlog.h"
#include <handler/handler_manager.hpp>
#include <variant>
#include <algorithm>
namespace bpftime
{
handler_manager::handler_manager(managed_shared_memory &mem,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -58,11 +63,34 @@ void handler_manager::clear_fd_at(int fd, managed_shared_memory &memory)
}
if (std::holds_alternative<bpf_map_handler>(handlers[fd])) {
std::get<bpf_map_handler>(handlers[fd]).map_free(memory);
} else if (std::holds_alternative<bpf_perf_event_handler>(
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<bpf_prog_handler>(handler)) {
auto &prog_handler =
std::get<bpf_prog_handler>(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;
Expand Down
3 changes: 3 additions & 0 deletions runtime/src/handler/perf_event_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion runtime/src/handler/perf_event_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit c7c303a

Please sign in to comment.