From: Yafang Shao <laoar.shao@gmail.com>
To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org,
martin.lau@linux.dev, song@kernel.org, yhs@fb.com,
john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com,
haoluo@google.com, jolsa@kernel.org, quentin@isovalent.com
Cc: bpf@vger.kernel.org, Yafang Shao <laoar.shao@gmail.com>
Subject: [PATCH v2 bpf-next 11/11] bpftool: Show probed function in perf_event link info
Date: Thu, 8 Jun 2023 10:35:23 +0000 [thread overview]
Message-ID: <20230608103523.102267-12-laoar.shao@gmail.com> (raw)
In-Reply-To: <20230608103523.102267-1-laoar.shao@gmail.com>
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 --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
--
1.8.3.1
prev parent reply other threads:[~2023-06-08 10:35 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-06-08 10:35 [PATCH v2 bpf-next 00/11] bpf: Support ->fill_link_info for kprobe_multi and perf_event links Yafang Shao
2023-06-08 10:35 ` [PATCH v2 bpf-next 01/11] bpf: Support ->fill_link_info for kprobe_multi Yafang Shao
2023-06-08 23:05 ` Andrii Nakryiko
2023-06-09 9:13 ` Yafang Shao
2023-06-09 18:25 ` Andrii Nakryiko
2023-06-10 2:19 ` Yafang Shao
2023-06-08 10:35 ` [PATCH v2 bpf-next 02/11] bpftool: Add address filtering in kernel_syms_load() Yafang Shao
2023-06-08 10:35 ` [PATCH v2 bpf-next 03/11] bpftool: Show probed function in kprobe_multi link info Yafang Shao
2023-06-08 23:08 ` Andrii Nakryiko
2023-06-09 9:15 ` Yafang Shao
2023-06-08 10:35 ` [PATCH v2 bpf-next 04/11] bpf: Protect probed address based on kptr_restrict setting Yafang Shao
2023-06-08 23:08 ` Andrii Nakryiko
2023-06-09 9:16 ` Yafang Shao
2023-06-08 10:35 ` [PATCH v2 bpf-next 05/11] bpf: Clear the probe_addr for uprobe Yafang Shao
2023-06-08 10:35 ` [PATCH v2 bpf-next 06/11] bpf: Expose symbol addresses for precise identification Yafang Shao
2023-06-08 10:35 ` [PATCH v2 bpf-next 07/11] bpf: Add a common helper bpf_copy_to_user() Yafang Shao
2023-06-08 10:35 ` [PATCH v2 bpf-next 08/11] bpf: Support ->fill_link_info for perf_event Yafang Shao
2023-06-08 23:12 ` Andrii Nakryiko
2023-06-09 9:53 ` Yafang Shao
2023-06-09 9:56 ` Yafang Shao
2023-06-09 18:26 ` Andrii Nakryiko
2023-06-10 2:21 ` Yafang Shao
2023-06-08 10:35 ` [PATCH v2 bpf-next 09/11] libbpf: Add perf event names Yafang Shao
2023-06-08 23:14 ` Andrii Nakryiko
2023-06-09 4:36 ` Song Liu
2023-06-10 2:22 ` Yafang Shao
2023-06-10 20:34 ` Quentin Monnet
2023-06-11 15:52 ` Yafang Shao
2023-06-10 22:37 ` Jiri Olsa
2023-06-10 22:43 ` Jiri Olsa
2023-06-11 15:55 ` Yafang Shao
2023-06-08 10:35 ` [PATCH v2 bpf-next 10/11] bpftool: Move get_prog_info() into do_show_link() Yafang Shao
2023-06-08 10:35 ` Yafang Shao [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230608103523.102267-12-laoar.shao@gmail.com \
--to=laoar.shao@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=haoluo@google.com \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kpsingh@kernel.org \
--cc=martin.lau@linux.dev \
--cc=quentin@isovalent.com \
--cc=sdf@google.com \
--cc=song@kernel.org \
--cc=yhs@fb.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).