Skip to content

Commit cbdaf71

Browse files
erthalionanakryiko
authored andcommitted
bpftool: Add bpf_cookie to link output
Commit 82e6b1e ("bpf: Allow to specify user-provided bpf_cookie for BPF perf links") introduced the concept of user specified bpf_cookie, which could be accessed by BPF programs using bpf_get_attach_cookie(). For troubleshooting purposes it is convenient to expose bpf_cookie via bpftool as well, so there is no need to meddle with the target BPF program itself. Implemented using the pid iterator BPF program to actually fetch bpf_cookies, which allows constraining code changes only to bpftool. $ bpftool link 1: type 7 prog 5 bpf_cookie 123 pids bootstrap(81) Signed-off-by: Dmitrii Dolgov <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Acked-by: Yonghong Song <[email protected]> Acked-by: Quentin Monnet <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent f98d6dd commit cbdaf71

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

tools/bpf/bpftool/main.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@ struct obj_ref {
113113

114114
struct obj_refs {
115115
int ref_cnt;
116+
bool has_bpf_cookie;
116117
struct obj_ref *refs;
118+
__u64 bpf_cookie;
117119
};
118120

119121
struct btf;

tools/bpf/bpftool/pids.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ static void add_ref(struct hashmap *map, struct pid_iter_entry *e)
7878
ref->pid = e->pid;
7979
memcpy(ref->comm, e->comm, sizeof(ref->comm));
8080
refs->ref_cnt = 1;
81+
refs->has_bpf_cookie = e->has_bpf_cookie;
82+
refs->bpf_cookie = e->bpf_cookie;
8183

8284
err = hashmap__append(map, u32_as_hash_field(e->id), refs);
8385
if (err)
@@ -205,6 +207,9 @@ void emit_obj_refs_json(struct hashmap *map, __u32 id,
205207
if (refs->ref_cnt == 0)
206208
break;
207209

210+
if (refs->has_bpf_cookie)
211+
jsonw_lluint_field(json_writer, "bpf_cookie", refs->bpf_cookie);
212+
208213
jsonw_name(json_writer, "pids");
209214
jsonw_start_array(json_writer);
210215
for (i = 0; i < refs->ref_cnt; i++) {
@@ -234,6 +239,9 @@ void emit_obj_refs_plain(struct hashmap *map, __u32 id, const char *prefix)
234239
if (refs->ref_cnt == 0)
235240
break;
236241

242+
if (refs->has_bpf_cookie)
243+
printf("\n\tbpf_cookie %llu", (unsigned long long) refs->bpf_cookie);
244+
237245
printf("%s", prefix);
238246
for (i = 0; i < refs->ref_cnt; i++) {
239247
struct obj_ref *ref = &refs->refs[i];

tools/bpf/bpftool/skeleton/pid_iter.bpf.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@ static __always_inline __u32 get_obj_id(void *ent, enum bpf_obj_type type)
3838
}
3939
}
4040

41+
/* could be used only with BPF_LINK_TYPE_PERF_EVENT links */
42+
static __u64 get_bpf_cookie(struct bpf_link *link)
43+
{
44+
struct bpf_perf_link *perf_link;
45+
struct perf_event *event;
46+
47+
perf_link = container_of(link, struct bpf_perf_link, link);
48+
event = BPF_CORE_READ(perf_link, perf_file, private_data);
49+
return BPF_CORE_READ(event, bpf_cookie);
50+
}
51+
4152
SEC("iter/task_file")
4253
int iter(struct bpf_iter__task_file *ctx)
4354
{
@@ -69,8 +80,19 @@ int iter(struct bpf_iter__task_file *ctx)
6980
if (file->f_op != fops)
7081
return 0;
7182

83+
__builtin_memset(&e, 0, sizeof(e));
7284
e.pid = task->tgid;
7385
e.id = get_obj_id(file->private_data, obj_type);
86+
87+
if (obj_type == BPF_OBJ_LINK) {
88+
struct bpf_link *link = (struct bpf_link *) file->private_data;
89+
90+
if (BPF_CORE_READ(link, type) == BPF_LINK_TYPE_PERF_EVENT) {
91+
e.has_bpf_cookie = true;
92+
e.bpf_cookie = get_bpf_cookie(link);
93+
}
94+
}
95+
7496
bpf_probe_read_kernel_str(&e.comm, sizeof(e.comm),
7597
task->group_leader->comm);
7698
bpf_seq_write(ctx->meta->seq, &e, sizeof(e));

tools/bpf/bpftool/skeleton/pid_iter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
struct pid_iter_entry {
77
__u32 id;
88
int pid;
9+
__u64 bpf_cookie;
10+
bool has_bpf_cookie;
911
char comm[16];
1012
};
1113

0 commit comments

Comments
 (0)