bpftool: Add bpf_cookie to link output
Commit 82e6b1eee6a8 ("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 <9erthalion6@gmail.com> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Acked-by: Yonghong Song <yhs@fb.com> Acked-by: Quentin Monnet <quentin@isovalent.com> Link: https://lore.kernel.org/bpf/20220309163112.24141-1-9erthalion6@gmail.com
This commit is contained in:
parent
f98d6dd1e7
commit
cbdaf71f7e
@ -113,7 +113,9 @@ struct obj_ref {
|
|||||||
|
|
||||||
struct obj_refs {
|
struct obj_refs {
|
||||||
int ref_cnt;
|
int ref_cnt;
|
||||||
|
bool has_bpf_cookie;
|
||||||
struct obj_ref *refs;
|
struct obj_ref *refs;
|
||||||
|
__u64 bpf_cookie;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct btf;
|
struct btf;
|
||||||
|
@ -78,6 +78,8 @@ static void add_ref(struct hashmap *map, struct pid_iter_entry *e)
|
|||||||
ref->pid = e->pid;
|
ref->pid = e->pid;
|
||||||
memcpy(ref->comm, e->comm, sizeof(ref->comm));
|
memcpy(ref->comm, e->comm, sizeof(ref->comm));
|
||||||
refs->ref_cnt = 1;
|
refs->ref_cnt = 1;
|
||||||
|
refs->has_bpf_cookie = e->has_bpf_cookie;
|
||||||
|
refs->bpf_cookie = e->bpf_cookie;
|
||||||
|
|
||||||
err = hashmap__append(map, u32_as_hash_field(e->id), refs);
|
err = hashmap__append(map, u32_as_hash_field(e->id), refs);
|
||||||
if (err)
|
if (err)
|
||||||
@ -205,6 +207,9 @@ void emit_obj_refs_json(struct hashmap *map, __u32 id,
|
|||||||
if (refs->ref_cnt == 0)
|
if (refs->ref_cnt == 0)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
if (refs->has_bpf_cookie)
|
||||||
|
jsonw_lluint_field(json_writer, "bpf_cookie", refs->bpf_cookie);
|
||||||
|
|
||||||
jsonw_name(json_writer, "pids");
|
jsonw_name(json_writer, "pids");
|
||||||
jsonw_start_array(json_writer);
|
jsonw_start_array(json_writer);
|
||||||
for (i = 0; i < refs->ref_cnt; i++) {
|
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)
|
|||||||
if (refs->ref_cnt == 0)
|
if (refs->ref_cnt == 0)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
if (refs->has_bpf_cookie)
|
||||||
|
printf("\n\tbpf_cookie %llu", (unsigned long long) refs->bpf_cookie);
|
||||||
|
|
||||||
printf("%s", prefix);
|
printf("%s", prefix);
|
||||||
for (i = 0; i < refs->ref_cnt; i++) {
|
for (i = 0; i < refs->ref_cnt; i++) {
|
||||||
struct obj_ref *ref = &refs->refs[i];
|
struct obj_ref *ref = &refs->refs[i];
|
||||||
|
@ -38,6 +38,17 @@ static __always_inline __u32 get_obj_id(void *ent, enum bpf_obj_type type)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* could be used only with BPF_LINK_TYPE_PERF_EVENT links */
|
||||||
|
static __u64 get_bpf_cookie(struct bpf_link *link)
|
||||||
|
{
|
||||||
|
struct bpf_perf_link *perf_link;
|
||||||
|
struct perf_event *event;
|
||||||
|
|
||||||
|
perf_link = container_of(link, struct bpf_perf_link, link);
|
||||||
|
event = BPF_CORE_READ(perf_link, perf_file, private_data);
|
||||||
|
return BPF_CORE_READ(event, bpf_cookie);
|
||||||
|
}
|
||||||
|
|
||||||
SEC("iter/task_file")
|
SEC("iter/task_file")
|
||||||
int iter(struct bpf_iter__task_file *ctx)
|
int iter(struct bpf_iter__task_file *ctx)
|
||||||
{
|
{
|
||||||
@ -69,8 +80,19 @@ int iter(struct bpf_iter__task_file *ctx)
|
|||||||
if (file->f_op != fops)
|
if (file->f_op != fops)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
__builtin_memset(&e, 0, sizeof(e));
|
||||||
e.pid = task->tgid;
|
e.pid = task->tgid;
|
||||||
e.id = get_obj_id(file->private_data, obj_type);
|
e.id = get_obj_id(file->private_data, obj_type);
|
||||||
|
|
||||||
|
if (obj_type == BPF_OBJ_LINK) {
|
||||||
|
struct bpf_link *link = (struct bpf_link *) file->private_data;
|
||||||
|
|
||||||
|
if (BPF_CORE_READ(link, type) == BPF_LINK_TYPE_PERF_EVENT) {
|
||||||
|
e.has_bpf_cookie = true;
|
||||||
|
e.bpf_cookie = get_bpf_cookie(link);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bpf_probe_read_kernel_str(&e.comm, sizeof(e.comm),
|
bpf_probe_read_kernel_str(&e.comm, sizeof(e.comm),
|
||||||
task->group_leader->comm);
|
task->group_leader->comm);
|
||||||
bpf_seq_write(ctx->meta->seq, &e, sizeof(e));
|
bpf_seq_write(ctx->meta->seq, &e, sizeof(e));
|
||||||
|
@ -6,6 +6,8 @@
|
|||||||
struct pid_iter_entry {
|
struct pid_iter_entry {
|
||||||
__u32 id;
|
__u32 id;
|
||||||
int pid;
|
int pid;
|
||||||
|
__u64 bpf_cookie;
|
||||||
|
bool has_bpf_cookie;
|
||||||
char comm[16];
|
char comm[16];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user