Skip to content

Commit

Permalink
bpftool: Use a local copy of BPF_LINK_TYPE_PERF_EVENT in pid_iter.bpf.c
Browse files Browse the repository at this point in the history
In order to allow the BPF program in bpftool's pid_iter.bpf.c to compile
correctly on hosts where vmlinux.h does not define
BPF_LINK_TYPE_PERF_EVENT (running kernel versions lower than 5.15, for
example), define and use a local copy of the enum value. This requires
LLVM 12 or newer to build the BPF program.

Fixes: cbdaf71f7e65 ("bpftool: Add bpf_cookie to link output")
Signed-off-by: Quentin Monnet <[email protected]>
Signed-off-by: Andrii Nakryiko <[email protected]>
Link: https://lore.kernel.org/bpf/[email protected]
  • Loading branch information
qmonnet committed Jul 12, 2023
1 parent 0773871 commit fc96090
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/skeleton/pid_iter.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ struct perf_event___local {
u64 bpf_cookie;
} __attribute__((preserve_access_index));

enum bpf_link_type___local {
BPF_LINK_TYPE_PERF_EVENT___local = 7,
};

extern const void bpf_link_fops __ksym;
extern const void bpf_map_fops __ksym;
extern const void bpf_prog_fops __ksym;
Expand Down Expand Up @@ -93,10 +97,13 @@ int iter(struct bpf_iter__task_file *ctx)
e.pid = task->tgid;
e.id = get_obj_id(file->private_data, obj_type);

if (obj_type == BPF_OBJ_LINK) {
if (obj_type == BPF_OBJ_LINK &&
bpf_core_enum_value_exists(enum bpf_link_type___local,
BPF_LINK_TYPE_PERF_EVENT___local)) {
struct bpf_link *link = (struct bpf_link *) file->private_data;

if (BPF_CORE_READ(link, type) == BPF_LINK_TYPE_PERF_EVENT) {
if (link->type == bpf_core_enum_value(enum bpf_link_type___local,
BPF_LINK_TYPE_PERF_EVENT___local)) {
e.has_bpf_cookie = true;
e.bpf_cookie = get_bpf_cookie(link);
}
Expand Down

0 comments on commit fc96090

Please sign in to comment.