@@ -195,6 +195,100 @@ static int get_prog_info(int prog_id, struct bpf_prog_info *info)
kernel_syms_destroy(&dd);
}
+static void
+show_perf_event_probe_json(struct bpf_link_info *info, json_writer_t *wtr)
+{
+ jsonw_uint_field(wtr, "retprobe", info->perf_event.probe.retprobe);
+ jsonw_string_field(wtr, "name",
+ u64_to_ptr(info->perf_event.probe.name));
+ jsonw_uint_field(wtr, "offset", info->perf_event.probe.offset);
+ jsonw_uint_field(wtr, "addr", info->perf_event.probe.addr);
+}
+
+static void
+show_perf_event_tp_json(struct bpf_link_info *info, json_writer_t *wtr)
+{
+ jsonw_string_field(wtr, "tp_name",
+ u64_to_ptr(info->perf_event.tp.tp_name));
+}
+
+static const char *perf_config_hw_cache_str(__u64 config)
+{
+#define PERF_HW_CACHE_LEN 128
+ const char *hw_cache, *result, *op;
+ char *str = malloc(PERF_HW_CACHE_LEN);
+
+ if (!str) {
+ p_err("mem alloc failed");
+ return NULL;
+ }
+ hw_cache = libbpf_perf_hw_cache_str(config & 0xff);
+ if (hw_cache)
+ snprintf(str, PERF_HW_CACHE_LEN, "%s-", hw_cache);
+ else
+ snprintf(str, PERF_HW_CACHE_LEN, "%lld-", config & 0xff);
+ op = libbpf_perf_hw_cache_op_str((config >> 8) & 0xff);
+ if (op)
+ snprintf(str + strlen(str), PERF_HW_CACHE_LEN - strlen(str),
+ "%s-", op);
+ else
+ snprintf(str + strlen(str), PERF_HW_CACHE_LEN - strlen(str),
+ "%lld-", (config >> 8) & 0xff);
+ result = libbpf_perf_hw_cache_op_result_str(config >> 16);
+ if (result)
+ snprintf(str + strlen(str), PERF_HW_CACHE_LEN - strlen(str),
+ "%s", result);
+ else
+ snprintf(str + strlen(str), PERF_HW_CACHE_LEN - strlen(str),
+ "%lld", config >> 16);
+
+ return str;
+}
+
+static const char *perf_config_str(__u32 type, __u64 config)
+{
+ const char *perf_config;
+
+ switch (type) {
+ case PERF_TYPE_HARDWARE:
+ perf_config = libbpf_perf_hw_str(config);
+ break;
+ case PERF_TYPE_SOFTWARE:
+ perf_config = libbpf_perf_sw_str(config);
+ break;
+ case PERF_TYPE_HW_CACHE:
+ perf_config = perf_config_hw_cache_str(config);
+ break;
+ default:
+ perf_config = NULL;
+ break;
+ }
+ return perf_config;
+}
+
+static void
+show_perf_event_event_json(struct bpf_link_info *info, json_writer_t *wtr)
+{
+ __u64 config = info->perf_event.event.config;
+ __u32 type = info->perf_event.event.type;
+ const char *perf_type, *perf_config;
+
+ perf_type = libbpf_perf_type_str(type);
+ if (perf_type)
+ jsonw_string_field(wtr, "type", perf_type);
+ else
+ jsonw_uint_field(wtr, "type", type);
+
+ perf_config = perf_config_str(type, config);
+ if (perf_config)
+ jsonw_string_field(wtr, "config", perf_config);
+ else
+ jsonw_uint_field(wtr, "config", config);
+
+ if (type == PERF_TYPE_HW_CACHE && perf_config)
+ free((void *)perf_config);
+}
+
static int show_link_close_json(int fd, struct bpf_link_info *info,
const struct bpf_prog_info *prog_info)
{
@@ -245,6 +339,14 @@ static int show_link_close_json(int fd, struct bpf_link_info *info,
case BPF_LINK_TYPE_KPROBE_MULTI:
show_kprobe_multi_json(info, json_wtr);
break;
+ case BPF_LINK_TYPE_PERF_EVENT:
+ if (prog_info->type == BPF_PROG_TYPE_PERF_EVENT)
+ show_perf_event_event_json(info, json_wtr);
+ else if (prog_info->type == BPF_PROG_TYPE_TRACEPOINT)
+ show_perf_event_tp_json(info, json_wtr);
+ else
+ show_perf_event_probe_json(info, json_wtr);
+ break;
default:
break;
}
@@ -407,6 +509,56 @@ static void show_kprobe_multi_plain(struct bpf_link_info *info)
kernel_syms_destroy(&dd);
}
+static void show_perf_event_probe_plain(struct bpf_link_info *info)
+{
+ const char *buf;
+ __u32 retprobe;
+
+ buf = (const char *)u64_to_ptr(info->perf_event.probe.name);
+ if (buf[0] == '\0' && !info->perf_event.probe.addr)
+ return;
+
+ retprobe = info->perf_event.probe.retprobe;
+ printf("\n\tretprobe %u name %s ", retprobe, buf);
+ if (info->perf_event.probe.offset)
+ printf("offset %#x ", info->perf_event.probe.offset);
+ if (info->perf_event.probe.addr)
+ printf("addr %llx ", info->perf_event.probe.addr);
+}
+
+static void show_perf_event_tp_plain(struct bpf_link_info *info)
+{
+ const char *buf;
+
+ buf = (const char *)u64_to_ptr(info->perf_event.tp.tp_name);
+ if (buf[0] == '\0')
+ return;
+
+ printf("\n\ttp_name %s ", buf);
+}
+
+static void show_perf_event_event_plain(struct bpf_link_info *info)
+{
+ __u64 config = info->perf_event.event.config;
+ __u32 type = info->perf_event.event.type;
+ const char *perf_type, *perf_config;
+
+ perf_type = libbpf_perf_type_str(type);
+ if (perf_type)
+ printf("\n\ttype %s ", perf_type);
+ else
+ printf("\n\ttype %u ", type);
+
+ perf_config = perf_config_str(type, config);
+ if (perf_config)
+ printf("config %s ", perf_config);
+ else
+ printf("config %llu ", config);
+
+ if (type == PERF_TYPE_HW_CACHE && perf_config)
+ free((void *)perf_config);
+}
+
static int show_link_close_plain(int fd, struct bpf_link_info *info,
const struct bpf_prog_info *prog_info)
{
@@ -450,6 +602,14 @@ static int show_link_close_plain(int fd, struct bpf_link_info *info,
case BPF_LINK_TYPE_KPROBE_MULTI:
show_kprobe_multi_plain(info);
break;
+ case BPF_LINK_TYPE_PERF_EVENT:
+ if (prog_info->type == BPF_PROG_TYPE_PERF_EVENT)
+ show_perf_event_event_plain(info);
+ else if (prog_info->type == BPF_PROG_TYPE_TRACEPOINT)
+ show_perf_event_tp_plain(info);
+ else
+ show_perf_event_probe_plain(info);
+ break;
default:
break;
}
@@ -479,6 +639,7 @@ static int do_show_link(int fd)
memset(&prog_info, 0, sizeof(info));
memset(&info, 0, sizeof(info));
+ buf[0] = '\0';
again:
err = bpf_link_get_info_by_fd(fd, &info, &len);
if (err) {
@@ -520,7 +681,24 @@ static int do_show_link(int fd)
goto again;
}
}
+ if (info.type == BPF_LINK_TYPE_PERF_EVENT) {
+ if (prog_info.type == BPF_PROG_TYPE_PERF_EVENT)
+ goto out;
+ if (prog_info.type == BPF_PROG_TYPE_TRACEPOINT &&
+ !info.perf_event.tp.tp_name) {
+ info.perf_event.tp.tp_name = (unsigned long)&buf;
+ info.perf_event.tp.name_len = sizeof(buf);
+ goto again;
+ }
+ if (prog_info.type == BPF_PROG_TYPE_KPROBE &&
+ !info.perf_event.probe.name) {
+ info.perf_event.probe.name = (unsigned long)&buf;
+ info.perf_event.probe.name_len = sizeof(buf);
+ goto again;
+ }
+ }
+out:
if (json_output)
show_link_close_json(fd, &info, &prog_info);
else
Enhance bpftool to display comprehensive information about exposed perf_event links, covering uprobe, kprobe, tracepoint, and generic perf event. The resulting output will include the following details: $ tools/bpf/bpftool/bpftool link show 4: perf_event prog 22 retprobe 0 name kernel_clone addr ffffffff940a9660 bpf_cookie 0 pids kprobe(133008) 5: perf_event prog 28 tp_name sched_switch bpf_cookie 0 pids tracepoint(133199) 7: perf_event prog 42 type software config cpu_clock bpf_cookie 0 pids perf_event(134187) 8: perf_event prog 42 type hw_cache config ll-read-miss bpf_cookie 0 pids perf_event(134187) 9: perf_event prog 42 type hardware config cpu_cycles bpf_cookie 0 pids perf_event(134187) 13: perf_event prog 72 retprobe 0 name /home/waken/bpf/uprobe/a.out offset 0x1338 bpf_cookie 0 pids uprobe(164506) 14: perf_event prog 73 retprobe 1 name /home/waken/bpf/uprobe/a.out offset 0x1338 bpf_cookie 0 pids uprobe(164506) $ tools/bpf/bpftool/bpftool link show -j [{"id":4,"type":"perf_event","prog_id":22,"retprobe":0,"name":"kernel_clone","offset":0,"addr":18446744071898306144,"bpf_cookie":0,"pids":[{"pid":133008,"comm":"kprobe"}]},{"id":5,"type":"perf_event","prog_id":28,"tp_name":"sched_switch","bpf_cookie":0,"pids":[{"pid":133199,"comm":"tracepoint"}]},{"id":7,"type":"perf_event","prog_id":42,"type":"software","config":"cpu_clock","bpf_cookie":0,"pids":[{"pid":134187,"comm":"perf_event"}]},{"id":8,"type":"perf_event","prog_id":42,"type":"hw_cache","config":"ll-read-miss","bpf_cookie":0,"pids":[{"pid":134187,"comm":"perf_event"}]},{"id":9,"type":"perf_event","prog_id":42,"type":"hardware","config":"cpu_cycles","bpf_cookie":0,"pids":[{"pid":134187,"comm":"perf_event"}]},{"id":13,"type":"perf_event","prog_id":72,"retprobe":0,"name":"/home/waken/bpf/uprobe/a.out","offset":4920,"addr":0,"bpf_cookie":0,"pids":[{"pid":164506,"comm":"uprobe"}]},{"id":14,"type":"perf_event","prog_id":73,"retprobe":1,"name":"/home/waken/bpf/uprobe/a.out","offset":4920,"addr":0,"bpf_cookie":0,"pids":[{"pid":164506,"comm":"uprobe"}]}] For generic perf events, the displayed information in bpftool is limited to the type and configuration, while other attributes such as sample_period, sample_freq, etc., are not included. Signed-off-by: Yafang Shao <laoar.shao@gmail.com> --- tools/bpf/bpftool/link.c | 178 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 178 insertions(+)