diff mbox series

[v2,bpf-next,11/11] bpftool: Show probed function in perf_event link info

Message ID 20230608103523.102267-12-laoar.shao@gmail.com (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series bpf: Support ->fill_link_info for kprobe_multi and perf_event links | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-2 success Logs for build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-4 success Logs for build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-5 success Logs for build for x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-6 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-3 success Logs for build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-7 success Logs for test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-19 success Logs for test_progs_no_alu32_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-22 success Logs for test_progs_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-25 success Logs for test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-9 success Logs for test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-10 success Logs for test_maps on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-11 success Logs for test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-13 success Logs for test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-14 success Logs for test_progs on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-15 success Logs for test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-17 success Logs for test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-18 success Logs for test_progs_no_alu32 on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-20 success Logs for test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-21 success Logs for test_progs_no_alu32_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-23 success Logs for test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-24 success Logs for test_progs_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-27 success Logs for test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-28 success Logs for test_verifier on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-29 success Logs for veristat
bpf/vmtest-bpf-next-VM_Test-26 success Logs for test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-16 success Logs for test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-12 success Logs for test_progs on s390x with gcc
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for bpf-next, async
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 8 this patch: 8
netdev/cc_maintainers success CCed 13 of 13 maintainers
netdev/build_clang success Errors and warnings before: 8 this patch: 8
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 8 this patch: 8
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 215 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-VM_Test-8 success Logs for test_maps on s390x with gcc

Commit Message

Yafang Shao June 8, 2023, 10:35 a.m. UTC
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(+)
diff mbox series

Patch

diff --git a/tools/bpf/bpftool/link.c b/tools/bpf/bpftool/link.c
index a2b75f4..7c02540 100644
--- a/tools/bpf/bpftool/link.c
+++ b/tools/bpf/bpftool/link.c
@@ -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