Skip to content

Commit

Permalink
Fix a uretprobe issue (eunomia-bpf#31)
Browse files Browse the repository at this point in the history
* Update a new simple example to test uretprobe

* Fix uretprobe attaching

* Remove simple_uretprobe_test
  • Loading branch information
Officeyutong committed Oct 15, 2023
1 parent d98a8ac commit ef03e9d
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 9 deletions.
1 change: 1 addition & 0 deletions example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
simple_uretprobe_test*
2 changes: 1 addition & 1 deletion runtime/include/hook_entry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ struct hook_entry {

// listener for uprobe
GumInvocationListener *listener = nullptr;
int uretprobe_id;
int uretprobe_id = -1;
std::set<const bpftime_prog *> ret_progs;
};
// get hook entry from probe context
Expand Down
9 changes: 5 additions & 4 deletions runtime/src/attach/attach_internal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ void *__frida_bpftime_filter_handler()
(void *)arg4);
}



typedef struct _UprobeListener UprobeListener;

struct _UprobeListener {
Expand All @@ -115,9 +113,9 @@ static void uprobe_listener_on_enter(GumInvocationListener *listener,
if (hook_entry->progs.size() == 0) {
return;
}
spdlog::trace("Handle uprobe at uprobe_listener_on_enter");
GumInvocationContext *ctx;
pt_regs regs;

ctx = gum_interceptor_get_current_invocation();
convert_gum_cpu_context_to_pt_regs(*ctx->cpu_context, regs);
for (auto &prog : hook_entry->progs) {
Expand All @@ -138,7 +136,7 @@ static void uprobe_listener_on_leave(GumInvocationListener *listener,
if (hook_entry->ret_progs.size() == 0) {
return;
}

spdlog::trace("Handle uretprobe at uprobe_listener_on_leave");
pt_regs regs;
GumInvocationContext *ctx;
ctx = gum_interceptor_get_current_invocation();
Expand Down Expand Up @@ -181,6 +179,8 @@ static void frida_uprobe_listener_on_enter(_GumInvocationContext *ic,
GumInvocationContext *ctx;
pt_regs regs;

spdlog::trace("Handle uprobe at frida_uprobe_listener_on_enter");

ctx = gum_interceptor_get_current_invocation();
convert_gum_cpu_context_to_pt_regs(*ctx->cpu_context, regs);
for (auto &prog : hook_entry->progs) {
Expand All @@ -201,6 +201,7 @@ static void frida_uprobe_listener_on_leave(_GumInvocationContext *ic,
return;
}

spdlog::trace("Handle uretprobe at frida_uprobe_listener_on_leave");
pt_regs regs;
GumInvocationContext *ctx;
ctx = gum_interceptor_get_current_invocation();
Expand Down
6 changes: 6 additions & 0 deletions runtime/src/attach/bpf_attach_ctx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,9 @@ int bpf_attach_ctx::init_attach_ctx_from_handlers(
}
case bpf_perf_event_handler::bpf_event_type::
BPF_TYPE_UPROBE: {
spdlog::debug(
"Creating uprobe for perf event fd {}",
i);
fd = create_uprobe(function, i, false);
break;
}
Expand Down Expand Up @@ -466,6 +469,9 @@ int bpf_attach_ctx::attach_prog(const bpftime_prog *prog, int id)
break;
}
case BPFTIME_UPROBE: {
spdlog::trace(
"Insert uprobe/uretprobe program for prog id {}, entry uretprobe id {}",
id, entry.uretprobe_id);
if (entry.uretprobe_id == id) {
entry.ret_progs.insert(prog);
} else {
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/bpftime_shm_internal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ int bpftime_shm::add_uprobe(int pid, const char *name, uint64_t offset,
int fd = open_fake_fd();
manager->set_handler(
fd,
bpftime::bpf_perf_event_handler{ false, offset, pid, name,
bpftime::bpf_perf_event_handler{ retprobe, offset, pid, name,
ref_ctr_off, segment },
segment);
return fd;
Expand Down
7 changes: 4 additions & 3 deletions runtime/syscall-server/syscall_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,14 +223,15 @@ int syscall_context::handle_perfevent(perf_event_attr *attr, pid_t pid, int cpu,
try_startup();
if ((int)attr->type == determine_uprobe_perf_type()) {
// NO legacy bpf types
bool retprobe = attr->config & determine_uprobe_retprobe_bit();
bool retprobe =
attr->config & (1 << determine_uprobe_retprobe_bit());
size_t ref_ctr_off =
attr->config >> PERF_UPROBE_REF_CTR_OFFSET_SHIFT;
const char *name = (const char *)(uintptr_t)attr->config1;
uint64_t offset = attr->config2;
spdlog::debug(
"Creating uprobe name {} offset {} retprove {} ref_ctr_off {}",
name, offset, retprobe, ref_ctr_off);
"Creating uprobe name {} offset {} retprobe {} ref_ctr_off {} attr->config={:x}",
name, offset, retprobe, ref_ctr_off, attr->config);
int id = bpftime_uprobe_create(pid, name, offset, retprobe,
ref_ctr_off);
// std::cout << "Created uprobe " << id << std::endl;
Expand Down

0 comments on commit ef03e9d

Please sign in to comment.