Skip to content

Commit

Permalink
libbpf-tools/biosnoop: simplify auto attach
Browse files Browse the repository at this point in the history
Signed-off-by: mickey_zhu <[email protected]>
  • Loading branch information
michael-chuh committed Apr 7, 2023
1 parent 3e5aae2 commit 39b0f67
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 79 deletions.
11 changes: 10 additions & 1 deletion libbpf-tools/biosnoop.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ int BPF_PROG(blk_account_io_start, struct request *rq)
return trace_pid(rq);
}

SEC("fentry/blk_account_io_merge_bio")
int BPF_PROG(blk_account_io_merge_bio, struct request *rq)
{
if (filter_cg && !bpf_current_task_under_cgroup(&cgroup_map, 0))
return 0;

return trace_pid(rq);
}

SEC("kprobe/blk_account_io_start")
int BPF_KPROBE(kprobe_blk_account_io_start, struct request *rq)
{
Expand All @@ -94,7 +103,7 @@ int BPF_KPROBE(kprobe___blk_account_io_start, struct request *rq)
}

SEC("kprobe/blk_account_io_merge_bio")
int BPF_KPROBE(blk_account_io_merge_bio, struct request *rq)
int BPF_KPROBE(kprobe_blk_account_io_merge_bio, struct request *rq)
{
if (filter_cg && !bpf_current_task_under_cgroup(&cgroup_map, 0))
return 0;
Expand Down
100 changes: 22 additions & 78 deletions libbpf-tools/biosnoop.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,6 @@ int main(int argc, char **argv)
int err;
int idx, cg_map_fd;
int cgfd = -1;
bool blk_account_io_start_symbol;
bool blk_account_io_start_fentry;

err = argp_parse(&argp, argc, argv, 0, NULL, NULL);
if (err)
Expand Down Expand Up @@ -231,39 +229,42 @@ int main(int argc, char **argv)
obj->rodata->targ_queued = env.queued;
obj->rodata->filter_cg = env.cg;

if (fentry_can_attach("blk_account_io_merge_bio", NULL)) {
bpf_program__set_autoload(obj->progs.kprobe_blk_account_io_merge_bio, false);
bpf_program__set_autoload(obj->progs.kprobe_blk_account_io_start, false);
bpf_program__set_autoload(obj->progs.kprobe___blk_account_io_start, false);

if (kprobe_exists("blk_account_io_start")) {
if (fentry_can_attach("blk_account_io_start", NULL)) {
if (fentry_can_attach("blk_account_io_start", NULL))
bpf_program__set_attach_target(obj->progs.blk_account_io_start, 0,
"blk_account_io_start");
bpf_program__set_autoload(obj->progs.kprobe_blk_account_io_start, false);
blk_account_io_start_fentry = true;
} else {
bpf_program__set_autoload(obj->progs.blk_account_io_start, false);
blk_account_io_start_fentry = false;
}
bpf_program__set_autoload(obj->progs.kprobe___blk_account_io_start, false);
blk_account_io_start_symbol = true;
} else {
if (fentry_can_attach("__blk_account_io_start", NULL)) {
else
bpf_program__set_attach_target(obj->progs.blk_account_io_start, 0,
"__blk_account_io_start");
} else {
bpf_program__set_autoload(obj->progs.blk_account_io_merge_bio, false);
bpf_program__set_autoload(obj->progs.blk_account_io_start, false);

if (!kprobe_exists("blk_account_io_start"))
bpf_program__set_autoload(obj->progs.kprobe_blk_account_io_start, false);
else
bpf_program__set_autoload(obj->progs.kprobe___blk_account_io_start, false);
blk_account_io_start_fentry = true;
} else {
bpf_program__set_autoload(obj->progs.blk_account_io_start, false);
blk_account_io_start_fentry = false;
}
bpf_program__set_autoload(obj->progs.kprobe_blk_account_io_start, false);
blk_account_io_start_symbol = false;
}

if (!env.queued)
bpf_program__set_autoload(obj->progs.block_rq_insert, false);

err = biosnoop_bpf__load(obj);
if (err) {
fprintf(stderr, "failed to load BPF object: %d\n", err);
goto cleanup;
}

err = biosnoop_bpf__attach(obj);
if (err) {
fprintf(stderr, "failed to attach BPF programs\n");
goto cleanup;
}

/* update cgroup path fd to map */
if (env.cg) {
idx = 0;
Expand All @@ -279,63 +280,6 @@ int main(int argc, char **argv)
}
}


if (blk_account_io_start_fentry)
obj->links.blk_account_io_start = bpf_program__attach(
obj->progs.blk_account_io_start);
else
if (blk_account_io_start_symbol)
obj->links.kprobe_blk_account_io_start = bpf_program__attach(
obj->progs.kprobe_blk_account_io_start);
else
obj->links.kprobe___blk_account_io_start = bpf_program__attach(
obj->progs.kprobe___blk_account_io_start);
if (!obj->links.blk_account_io_start &&
!obj->links.kprobe_blk_account_io_start &&
!obj->links.kprobe___blk_account_io_start) {
err = -errno;
fprintf(stderr, "failed to attach blk_account_io_start: %s\n",
strerror(-err));
goto cleanup;
}
ksyms = ksyms__load();
if (!ksyms) {
err = -ENOMEM;
fprintf(stderr, "failed to load kallsyms\n");
goto cleanup;
}
if (ksyms__get_symbol(ksyms, "blk_account_io_merge_bio")) {
obj->links.blk_account_io_merge_bio =
bpf_program__attach(obj->progs.blk_account_io_merge_bio);
if (!obj->links.blk_account_io_merge_bio) {
err = -errno;
fprintf(stderr, "failed to attach blk_account_io_merge_bio: %s\n",
strerror(-err));
goto cleanup;
}
}
if (env.queued) {
obj->links.block_rq_insert =
bpf_program__attach(obj->progs.block_rq_insert);
if (!obj->links.block_rq_insert) {
err = -errno;
fprintf(stderr, "failed to attach block_rq_insert: %s\n", strerror(-err));
goto cleanup;
}
}
obj->links.block_rq_issue = bpf_program__attach(obj->progs.block_rq_issue);
if (!obj->links.block_rq_issue) {
err = -errno;
fprintf(stderr, "failed to attach block_rq_issue: %s\n", strerror(-err));
goto cleanup;
}
obj->links.block_rq_complete = bpf_program__attach(obj->progs.block_rq_complete);
if (!obj->links.block_rq_complete) {
err = -errno;
fprintf(stderr, "failed to attach block_rq_complete: %s\n", strerror(-err));
goto cleanup;
}

pb = perf_buffer__new(bpf_map__fd(obj->maps.events), PERF_BUFFER_PAGES,
handle_event, handle_lost_events, NULL, NULL);
if (!pb) {
Expand Down

0 comments on commit 39b0f67

Please sign in to comment.