From: Alan Maguire <alan.maguire@oracle.com>
To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org
Cc: kafai@fb.com, songliubraving@fb.com, yhs@fb.com,
john.fastabend@gmail.com, kpsingh@kernel.org, toke@redhat.com,
sunyucong@gmail.com, netdev@vger.kernel.org, bpf@vger.kernel.org,
Alan Maguire <alan.maguire@oracle.com>
Subject: [PATCH v4 bpf-next 3/5] libbpf: add auto-attach for uprobes based on section name
Date: Fri, 11 Mar 2022 12:10:56 +0000 [thread overview]
Message-ID: <1647000658-16149-4-git-send-email-alan.maguire@oracle.com> (raw)
In-Reply-To: <1647000658-16149-1-git-send-email-alan.maguire@oracle.com>
Now that u[ret]probes can use name-based specification, it makes
sense to add support for auto-attach based on SEC() definition.
The format proposed is
SEC("u[ret]probe/prog:[raw_offset|[function_name[+offset]]")
For example, to trace malloc() in libc:
SEC("uprobe/libc.so.6:malloc")
...or to trace function foo2 in /usr/bin/foo:
SEC("uprobe//usr/bin/foo:foo2")
Auto-attach is done for all tasks (pid -1). prog can be an absolute
path or simply a program/library name; in the latter case, we use
PATH/LD_LIBRARY_PATH to resolve the full path, falling back to
standard locations (/usr/bin:/usr/sbin or /usr/lib64:/usr/lib) if
the file is not found via environment-variable specified locations.
Signed-off-by: Alan Maguire <alan.maguire@oracle.com>
---
tools/lib/bpf/libbpf.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 66 insertions(+), 2 deletions(-)
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 2b50b01..0dcbca8 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -8593,6 +8593,7 @@ int bpf_program__set_log_buf(struct bpf_program *prog, char *log_buf, size_t log
}
static int attach_kprobe(const struct bpf_program *prog, long cookie, struct bpf_link **link);
+static int attach_uprobe(const struct bpf_program *prog, long cookie, struct bpf_link **link);
static int attach_tp(const struct bpf_program *prog, long cookie, struct bpf_link **link);
static int attach_raw_tp(const struct bpf_program *prog, long cookie, struct bpf_link **link);
static int attach_trace(const struct bpf_program *prog, long cookie, struct bpf_link **link);
@@ -8604,9 +8605,9 @@ int bpf_program__set_log_buf(struct bpf_program *prog, char *log_buf, size_t log
SEC_DEF("sk_reuseport/migrate", SK_REUSEPORT, BPF_SK_REUSEPORT_SELECT_OR_MIGRATE, SEC_ATTACHABLE | SEC_SLOPPY_PFX),
SEC_DEF("sk_reuseport", SK_REUSEPORT, BPF_SK_REUSEPORT_SELECT, SEC_ATTACHABLE | SEC_SLOPPY_PFX),
SEC_DEF("kprobe/", KPROBE, 0, SEC_NONE, attach_kprobe),
- SEC_DEF("uprobe/", KPROBE, 0, SEC_NONE),
+ SEC_DEF("uprobe/", KPROBE, 0, SEC_NONE, attach_uprobe),
SEC_DEF("kretprobe/", KPROBE, 0, SEC_NONE, attach_kprobe),
- SEC_DEF("uretprobe/", KPROBE, 0, SEC_NONE),
+ SEC_DEF("uretprobe/", KPROBE, 0, SEC_NONE, attach_uprobe),
SEC_DEF("tc", SCHED_CLS, 0, SEC_NONE),
SEC_DEF("classifier", SCHED_CLS, 0, SEC_NONE | SEC_SLOPPY_PFX | SEC_DEPRECATED),
SEC_DEF("action", SCHED_ACT, 0, SEC_NONE | SEC_SLOPPY_PFX),
@@ -10761,6 +10762,69 @@ struct bpf_link *bpf_program__attach_uprobe(const struct bpf_program *prog,
return bpf_program__attach_uprobe_opts(prog, pid, binary_path, func_offset, &opts);
}
+/* Format of u[ret]probe section definition supporting auto-attach:
+ * u[ret]probe/prog:function[+offset]
+ *
+ * prog can be an absolute/relative path or a filename; the latter is resolved to a
+ * full path via bpf_program__attach_uprobe_opts.
+ *
+ * Many uprobe programs do not avail of auto-attach, so we need to handle the
+ * case where the format is uprobe/myfunc by returning 0 with *link set to NULL
+ * to identify the case where auto-attach is not supported.
+ */
+static int attach_uprobe(const struct bpf_program *prog, long cookie, struct bpf_link **link)
+{
+ DECLARE_LIBBPF_OPTS(bpf_uprobe_opts, opts);
+ char *func, *probe_name, *func_end;
+ char *func_name, binary_path[512];
+ unsigned long long raw_offset;
+ size_t offset = 0;
+ int n;
+
+ *link = NULL;
+
+ opts.retprobe = str_has_pfx(prog->sec_name, "uretprobe/");
+ if (opts.retprobe)
+ probe_name = prog->sec_name + sizeof("uretprobe/") - 1;
+ else
+ probe_name = prog->sec_name + sizeof("uprobe/") - 1;
+
+ snprintf(binary_path, sizeof(binary_path), "%s", probe_name);
+ /* ':' should be prior to function+offset */
+ func_name = strrchr(binary_path, ':');
+ if (!func_name) {
+ pr_debug("section '%s' is old-style u[ret]probe/function, cannot auto-attach\n",
+ prog->sec_name);
+ return 0;
+ }
+ func_name[0] = '\0';
+ func_name++;
+ n = sscanf(func_name, "%m[a-zA-Z0-9_.]+%li", &func, &offset);
+ if (n < 1) {
+ pr_warn("uprobe name '%s' is invalid\n", func_name);
+ return -EINVAL;
+ }
+ if (opts.retprobe && offset != 0) {
+ free(func);
+ pr_warn("uretprobes do not support offset specification\n");
+ return -EINVAL;
+ }
+
+ /* Is func a raw address? */
+ errno = 0;
+ raw_offset = strtoull(func, &func_end, 0);
+ if (!errno && !*func_end) {
+ free(func);
+ func = NULL;
+ offset = (size_t)raw_offset;
+ }
+ opts.func_name = func;
+
+ *link = bpf_program__attach_uprobe_opts(prog, -1, binary_path, offset, &opts);
+ free(func);
+ return 0;
+}
+
static int determine_tracepoint_id(const char *tp_category,
const char *tp_name)
{
--
1.8.3.1
next prev parent reply other threads:[~2022-03-11 12:11 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-03-11 12:10 [PATCH v4 bpf-next 0/5] libbpf: name-based u[ret]probe attach Alan Maguire
2022-03-11 12:10 ` [PATCH v4 bpf-next 1/5] libbpf: bpf_program__attach_uprobe_opts() should determine paths for programs/libraries where possible Alan Maguire
2022-03-16 4:33 ` Andrii Nakryiko
2022-03-11 12:10 ` [PATCH v4 bpf-next 2/5] libbpf: support function name-based attach uprobes Alan Maguire
2022-03-16 4:33 ` Andrii Nakryiko
2022-03-11 12:10 ` Alan Maguire [this message]
2022-03-16 4:33 ` [PATCH v4 bpf-next 3/5] libbpf: add auto-attach for uprobes based on section name Andrii Nakryiko
2022-03-11 12:10 ` [PATCH v4 bpf-next 4/5] selftests/bpf: add tests for u[ret]probe attach by name Alan Maguire
2022-03-16 4:33 ` Andrii Nakryiko
2022-03-11 12:10 ` [PATCH v4 bpf-next 5/5] selftests/bpf: add tests for uprobe auto-attach via skeleton Alan Maguire
2022-03-14 22:26 ` Daniel Borkmann
2022-03-16 4:33 ` 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=1647000658-16149-4-git-send-email-alan.maguire@oracle.com \
--to=alan.maguire@oracle.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=john.fastabend@gmail.com \
--cc=kafai@fb.com \
--cc=kpsingh@kernel.org \
--cc=netdev@vger.kernel.org \
--cc=songliubraving@fb.com \
--cc=sunyucong@gmail.com \
--cc=toke@redhat.com \
--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).