From: Yafang Shao <laoar.shao@gmail.com>
To: andrii@kernel.org, eddyz87@gmail.com, ast@kernel.org,
daniel@iogearbox.net, martin.lau@linux.dev, song@kernel.org,
yonghong.song@linux.dev, john.fastabend@gmail.com,
kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com,
jolsa@kernel.org, edumazet@google.com, dxu@dxuuu.xyz
Cc: bpf@vger.kernel.org, netdev@vger.kernel.org,
Yafang Shao <laoar.shao@gmail.com>,
Jiri Olsa <olsajiri@gmail.com>
Subject: [RFC PATCH v2 1/2] libbpf: Add support for dynamic tracepoint
Date: Sun, 12 Jan 2025 14:45:12 +0800 [thread overview]
Message-ID: <20250112064513.883-2-laoar.shao@gmail.com> (raw)
In-Reply-To: <20250112064513.883-1-laoar.shao@gmail.com>
Dynamic tracepoints can be created using debugfs, perf or similar tools.
For example:
$ perf probe -a 'tcp_listendrop sk'
This command creates a new tracepoint under debugfs:
$ ls /sys/kernel/debug/tracing/events/probe/tcp_listendrop/
enable filter format hist id trigger
Notably, the probed function tcp_listendrop() is an inlined kernel function.
Although this dynamic tracepoint appears as a tracepoint, it is internally
implemented as a kprobe. Therefore, if we want to attach a bpf prog to
it, the bpf prog must be loaded as a kprobe prog.
The primary motivation for adding support for dynamic tracepoints is to
simplify tracing of inlined kernel functions using BPF tools, such as
bpftrace. By leveraging tools like perf, users can create a dynamic
tracepoint for an inlined kernel function and then attach a BPF program to
it.
To achieve this, a new section, SEC("kprobe/SUBSYSTEM/PROBE"), has been
introduced.
Suggested-by: Jiri Olsa <olsajiri@gmail.com>
Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
Cc: Daniel Xu <dxu@dxuuu.xyz>
---
tools/lib/bpf/libbpf.c | 29 ++++++++++++++++++++++++++++-
1 file changed, 28 insertions(+), 1 deletion(-)
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 66173ddb5a2d..23ea9272491b 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -11600,11 +11600,34 @@ bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog,
return libbpf_err_ptr(err);
}
+/* A dynamic tracepoint: "kprobe/SUBSYSTEM/PROBE" */
+static int attach_dynamic_tracepoint(const struct bpf_program *prog, const char *func_name,
+ struct bpf_link **link)
+{
+ char *tp_subsys, *tp_name;
+
+ tp_subsys = strdup(func_name);
+ if (!tp_subsys)
+ return -ENOMEM;
+
+ tp_name = strchr(tp_subsys, '/');
+ if (!tp_name) {
+ free(tp_subsys);
+ return -EINVAL;
+ }
+
+ *tp_name = '\0';
+ tp_name++;
+ *link = bpf_program__attach_tracepoint(prog, tp_subsys, tp_name);
+ free(tp_subsys);
+ return libbpf_get_error(*link);
+}
+
static int attach_kprobe(const struct bpf_program *prog, long cookie, struct bpf_link **link)
{
DECLARE_LIBBPF_OPTS(bpf_kprobe_opts, opts);
+ const char *func_name, *dynamic_tp;
unsigned long offset = 0;
- const char *func_name;
char *func;
int n;
@@ -11620,6 +11643,10 @@ static int attach_kprobe(const struct bpf_program *prog, long cookie, struct bpf
else
func_name = prog->sec_name + sizeof("kprobe/") - 1;
+ dynamic_tp = strchr(func_name, '/');
+ if (dynamic_tp)
+ return attach_dynamic_tracepoint(prog, func_name, link);
+
n = sscanf(func_name, "%m[a-zA-Z0-9_.]+%li", &func, &offset);
if (n < 1) {
pr_warn("kprobe name is invalid: %s\n", func_name);
--
2.43.5
next prev parent reply other threads:[~2025-01-12 6:45 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-01-12 6:45 [RFC PATCH v2 0/2] libbpf: Add support for dynamic tracepoints Yafang Shao
2025-01-12 6:45 ` Yafang Shao [this message]
2025-01-12 6:45 ` [RFC PATCH v2 2/2] selftests/bpf: Add selftest for dynamic tracepoint Yafang Shao
2025-01-14 22:32 ` [RFC PATCH v2 0/2] libbpf: Add support for dynamic tracepoints Andrii Nakryiko
2025-01-15 3:13 ` Yafang Shao
2025-01-16 23:05 ` Andrii Nakryiko
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=20250112064513.883-2-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=dxu@dxuuu.xyz \
--cc=eddyz87@gmail.com \
--cc=edumazet@google.com \
--cc=haoluo@google.com \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kpsingh@kernel.org \
--cc=martin.lau@linux.dev \
--cc=netdev@vger.kernel.org \
--cc=olsajiri@gmail.com \
--cc=sdf@fomichev.me \
--cc=song@kernel.org \
--cc=yonghong.song@linux.dev \
/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