From: Alan Maguire <alan.maguire@oracle.com>
To: andrii@kernel.org, ast@kernel.org, daniel@iogearbox.net
Cc: kafai@fb.com, songliubraving@fb.com, yhs@fb.com,
john.fastabend@gmail.com, kpsingh@kernel.org,
netdev@vger.kernel.org, bpf@vger.kernel.org,
Alan Maguire <alan.maguire@oracle.com>
Subject: [PATCH v2 bpf-next 2/3] libbpf: improve string parsing for uprobe auto-attach
Date: Wed, 6 Apr 2022 12:43:50 +0100 [thread overview]
Message-ID: <1649245431-29956-3-git-send-email-alan.maguire@oracle.com> (raw)
In-Reply-To: <1649245431-29956-1-git-send-email-alan.maguire@oracle.com>
For uprobe auto-attach, the parsing can be simplified for the SEC()
name to a single sscanf(); the return value of the sscanf can then
be used to distinguish between sections that simply specify
"u[ret]probe" (and thus cannot auto-attach), those that specify
"u[ret]probe/binary_path:function+offset" etc.
Suggested-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Alan Maguire <alan.maguire@oracle.com>
---
tools/lib/bpf/libbpf.c | 83 +++++++++++++++++++++-----------------------------
1 file changed, 34 insertions(+), 49 deletions(-)
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index c92226a..707dcc3 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -10913,60 +10913,45 @@ static int resolve_full_path(const char *file, char *result, size_t result_sz)
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;
+ char *probe_type = NULL, *binary_path = NULL, *func_name = NULL;
+ int n, ret = -EINVAL;
+ long offset = 0;
*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;
- if (probe_name[0] == '/')
- probe_name++;
-
- /* handle SEC("u[ret]probe") - format is valid, but auto-attach is impossible. */
- if (strlen(probe_name) == 0)
- return 0;
-
- snprintf(binary_path, sizeof(binary_path), "%s", probe_name);
- /* ':' should be prior to function+offset */
- func_name = strrchr(binary_path, ':');
- if (!func_name) {
- pr_warn("section '%s' missing ':function[+offset]' specification\n",
- prog->sec_name);
- return -EINVAL;
- }
- 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;
+ n = sscanf(prog->sec_name, "%m[^/]/%m[^:]:%m[a-zA-Z0-9_.]+%li",
+ &probe_type, &binary_path, &func_name, &offset);
+ switch (n) {
+ case 1:
+ /* handle SEC("u[ret]probe") - format is valid, but auto-attach is impossible. */
+ ret = 0;
+ break;
+ case 2:
+ pr_warn("prog '%s': section '%s' missing ':function[+offset]' specification\n",
+ prog->name, prog->sec_name);
+ break;
+ case 3:
+ case 4:
+ opts.retprobe = strcmp(probe_type, "uretprobe") == 0;
+ if (opts.retprobe && offset != 0) {
+ pr_warn("prog '%s': uretprobes do not support offset specification\n",
+ prog->name);
+ break;
+ }
+ opts.func_name = func_name;
+ *link = bpf_program__attach_uprobe_opts(prog, -1, binary_path, offset, &opts);
+ ret = libbpf_get_error(*link);
+ break;
+ default:
+ pr_warn("prog '%s': invalid format of section definition '%s'\n", prog->name,
+ prog->sec_name);
+ break;
}
- opts.func_name = func;
+ free(probe_type);
+ free(binary_path);
+ free(func_name);
- *link = bpf_program__attach_uprobe_opts(prog, -1, binary_path, offset, &opts);
- free(func);
- return libbpf_get_error(*link);
+ return ret;
}
struct bpf_link *bpf_program__attach_uprobe(const struct bpf_program *prog,
--
1.8.3.1
next prev parent reply other threads:[~2022-04-06 15:23 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-04-06 11:43 [PATCH v2 bpf-next 0/3] libbpf: uprobe name-based attach followups Alan Maguire
2022-04-06 11:43 ` [PATCH v2 bpf-next 1/3] libbpf: improve library identification for uprobe binary path resolution Alan Maguire
2022-04-06 11:43 ` Alan Maguire [this message]
2022-04-06 11:43 ` [PATCH v2 bpf-next 3/3] selftests/bpf: uprobe tests should verify param/return values Alan Maguire
2022-04-07 18:50 ` [PATCH v2 bpf-next 0/3] libbpf: uprobe name-based attach followups patchwork-bot+netdevbpf
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=1649245431-29956-3-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=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).