Skip to content

Commit 97ebac5

Browse files
Tao Chenanakryiko
authored andcommitted
bpf: Add show_fdinfo for perf_event
After commit 1b715e1 ("bpf: Support ->fill_link_info for perf_event") add perf_event info, we can also show the info with the method of cat /proc/[fd]/fdinfo. kprobe fdinfo: link_type: perf link_id: 10 prog_tag: bcf7977d3b93787c prog_id: 20 name: bpf_fentry_test1 offset: 0x0 missed: 0 addr: 0xffffffffa28a2904 event_type: kprobe cookie: 3735928559 uprobe fdinfo: link_type: perf link_id: 13 prog_tag: bcf7977d3b93787c prog_id: 21 name: /proc/self/exe offset: 0x63dce4 ref_ctr_offset: 0x33eee2a event_type: uprobe cookie: 3735928559 tracepoint fdinfo: link_type: perf link_id: 11 prog_tag: bcf7977d3b93787c prog_id: 22 tp_name: sched_switch event_type: tracepoint cookie: 3735928559 perf_event fdinfo: link_type: perf link_id: 12 prog_tag: bcf7977d3b93787c prog_id: 23 type: 1 config: 2 event_type: event cookie: 3735928559 Signed-off-by: Tao Chen <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 4d2815a commit 97ebac5

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed

kernel/bpf/syscall.c

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3795,6 +3795,32 @@ static int bpf_perf_link_fill_kprobe(const struct perf_event *event,
37953795
info->perf_event.kprobe.cookie = event->bpf_cookie;
37963796
return 0;
37973797
}
3798+
3799+
static void bpf_perf_link_fdinfo_kprobe(const struct perf_event *event,
3800+
struct seq_file *seq)
3801+
{
3802+
const char *name;
3803+
int err;
3804+
u32 prog_id, type;
3805+
u64 offset, addr;
3806+
unsigned long missed;
3807+
3808+
err = bpf_get_perf_event_info(event, &prog_id, &type, &name,
3809+
&offset, &addr, &missed);
3810+
if (err)
3811+
return;
3812+
3813+
seq_printf(seq,
3814+
"name:\t%s\n"
3815+
"offset:\t%#llx\n"
3816+
"missed:\t%lu\n"
3817+
"addr:\t%#llx\n"
3818+
"event_type:\t%s\n"
3819+
"cookie:\t%llu\n",
3820+
name, offset, missed, addr,
3821+
type == BPF_FD_TYPE_KRETPROBE ? "kretprobe" : "kprobe",
3822+
event->bpf_cookie);
3823+
}
37983824
#endif
37993825

38003826
#ifdef CONFIG_UPROBE_EVENTS
@@ -3823,6 +3849,31 @@ static int bpf_perf_link_fill_uprobe(const struct perf_event *event,
38233849
info->perf_event.uprobe.ref_ctr_offset = ref_ctr_offset;
38243850
return 0;
38253851
}
3852+
3853+
static void bpf_perf_link_fdinfo_uprobe(const struct perf_event *event,
3854+
struct seq_file *seq)
3855+
{
3856+
const char *name;
3857+
int err;
3858+
u32 prog_id, type;
3859+
u64 offset, ref_ctr_offset;
3860+
unsigned long missed;
3861+
3862+
err = bpf_get_perf_event_info(event, &prog_id, &type, &name,
3863+
&offset, &ref_ctr_offset, &missed);
3864+
if (err)
3865+
return;
3866+
3867+
seq_printf(seq,
3868+
"name:\t%s\n"
3869+
"offset:\t%#llx\n"
3870+
"ref_ctr_offset:\t%#llx\n"
3871+
"event_type:\t%s\n"
3872+
"cookie:\t%llu\n",
3873+
name, offset, ref_ctr_offset,
3874+
type == BPF_FD_TYPE_URETPROBE ? "uretprobe" : "uprobe",
3875+
event->bpf_cookie);
3876+
}
38263877
#endif
38273878

38283879
static int bpf_perf_link_fill_probe(const struct perf_event *event,
@@ -3891,10 +3942,79 @@ static int bpf_perf_link_fill_link_info(const struct bpf_link *link,
38913942
}
38923943
}
38933944

3945+
static void bpf_perf_event_link_show_fdinfo(const struct perf_event *event,
3946+
struct seq_file *seq)
3947+
{
3948+
seq_printf(seq,
3949+
"type:\t%u\n"
3950+
"config:\t%llu\n"
3951+
"event_type:\t%s\n"
3952+
"cookie:\t%llu\n",
3953+
event->attr.type, event->attr.config,
3954+
"event", event->bpf_cookie);
3955+
}
3956+
3957+
static void bpf_tracepoint_link_show_fdinfo(const struct perf_event *event,
3958+
struct seq_file *seq)
3959+
{
3960+
int err;
3961+
const char *name;
3962+
u32 prog_id;
3963+
3964+
err = bpf_get_perf_event_info(event, &prog_id, NULL, &name, NULL,
3965+
NULL, NULL);
3966+
if (err)
3967+
return;
3968+
3969+
seq_printf(seq,
3970+
"tp_name:\t%s\n"
3971+
"event_type:\t%s\n"
3972+
"cookie:\t%llu\n",
3973+
name, "tracepoint", event->bpf_cookie);
3974+
}
3975+
3976+
static void bpf_probe_link_show_fdinfo(const struct perf_event *event,
3977+
struct seq_file *seq)
3978+
{
3979+
#ifdef CONFIG_KPROBE_EVENTS
3980+
if (event->tp_event->flags & TRACE_EVENT_FL_KPROBE)
3981+
return bpf_perf_link_fdinfo_kprobe(event, seq);
3982+
#endif
3983+
3984+
#ifdef CONFIG_UPROBE_EVENTS
3985+
if (event->tp_event->flags & TRACE_EVENT_FL_UPROBE)
3986+
return bpf_perf_link_fdinfo_uprobe(event, seq);
3987+
#endif
3988+
}
3989+
3990+
static void bpf_perf_link_show_fdinfo(const struct bpf_link *link,
3991+
struct seq_file *seq)
3992+
{
3993+
struct bpf_perf_link *perf_link;
3994+
const struct perf_event *event;
3995+
3996+
perf_link = container_of(link, struct bpf_perf_link, link);
3997+
event = perf_get_event(perf_link->perf_file);
3998+
if (IS_ERR(event))
3999+
return;
4000+
4001+
switch (event->prog->type) {
4002+
case BPF_PROG_TYPE_PERF_EVENT:
4003+
return bpf_perf_event_link_show_fdinfo(event, seq);
4004+
case BPF_PROG_TYPE_TRACEPOINT:
4005+
return bpf_tracepoint_link_show_fdinfo(event, seq);
4006+
case BPF_PROG_TYPE_KPROBE:
4007+
return bpf_probe_link_show_fdinfo(event, seq);
4008+
default:
4009+
return;
4010+
}
4011+
}
4012+
38944013
static const struct bpf_link_ops bpf_perf_link_lops = {
38954014
.release = bpf_perf_link_release,
38964015
.dealloc = bpf_perf_link_dealloc,
38974016
.fill_link_info = bpf_perf_link_fill_link_info,
4017+
.show_fdinfo = bpf_perf_link_show_fdinfo,
38984018
};
38994019

39004020
static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)

0 commit comments

Comments
 (0)