Skip to content

Commit 29e3e46

Browse files
alan-maguireNobody
authored and
Nobody
committed
libbpf: improve string parsing for uprobe auto-attach
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 <[email protected]> Signed-off-by: Alan Maguire <[email protected]>
1 parent b4bfcbf commit 29e3e46

File tree

1 file changed

+33
-48
lines changed

1 file changed

+33
-48
lines changed

tools/lib/bpf/libbpf.c

Lines changed: 33 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -10913,60 +10913,45 @@ bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,
1091310913
static int attach_uprobe(const struct bpf_program *prog, long cookie, struct bpf_link **link)
1091410914
{
1091510915
DECLARE_LIBBPF_OPTS(bpf_uprobe_opts, opts);
10916-
char *func, *probe_name, *func_end;
10917-
char *func_name, binary_path[512];
10918-
unsigned long long raw_offset;
10919-
size_t offset = 0;
10920-
int n;
10916+
char *probe_type = NULL, *binary_path = NULL, *func_name = NULL;
10917+
int n, ret = -EINVAL;
10918+
long offset = 0;
1092110919

1092210920
*link = NULL;
1092310921

10924-
opts.retprobe = str_has_pfx(prog->sec_name, "uretprobe");
10925-
if (opts.retprobe)
10926-
probe_name = prog->sec_name + sizeof("uretprobe") - 1;
10927-
else
10928-
probe_name = prog->sec_name + sizeof("uprobe") - 1;
10929-
if (probe_name[0] == '/')
10930-
probe_name++;
10931-
10932-
/* handle SEC("u[ret]probe") - format is valid, but auto-attach is impossible. */
10933-
if (strlen(probe_name) == 0)
10934-
return 0;
10935-
10936-
snprintf(binary_path, sizeof(binary_path), "%s", probe_name);
10937-
/* ':' should be prior to function+offset */
10938-
func_name = strrchr(binary_path, ':');
10939-
if (!func_name) {
10940-
pr_warn("section '%s' missing ':function[+offset]' specification\n",
10922+
n = sscanf(prog->sec_name, "%m[^/]/%m[^:]:%m[a-zA-Z0-9_.]+%li",
10923+
&probe_type, &binary_path, &func_name, &offset);
10924+
switch (n) {
10925+
case 1:
10926+
/* handle SEC("u[ret]probe") - format is valid, but auto-attach is impossible. */
10927+
ret = 0;
10928+
break;
10929+
case 2:
10930+
pr_warn("prog '%s': section '%s' missing ':function[+offset]' specification\n",
10931+
prog->name, prog->sec_name);
10932+
break;
10933+
case 3:
10934+
case 4:
10935+
opts.retprobe = strcmp(probe_type, "uretprobe") == 0;
10936+
if (opts.retprobe && offset != 0) {
10937+
pr_warn("prog '%s': uretprobes do not support offset specification\n",
10938+
prog->name);
10939+
break;
10940+
}
10941+
opts.func_name = func_name;
10942+
*link = bpf_program__attach_uprobe_opts(prog, -1, binary_path, offset, &opts);
10943+
ret = libbpf_get_error(*link);
10944+
break;
10945+
default:
10946+
pr_warn("prog '%s': invalid format of section definition '%s'\n", prog->name,
1094110947
prog->sec_name);
10942-
return -EINVAL;
10943-
}
10944-
func_name[0] = '\0';
10945-
func_name++;
10946-
n = sscanf(func_name, "%m[a-zA-Z0-9_.]+%li", &func, &offset);
10947-
if (n < 1) {
10948-
pr_warn("uprobe name '%s' is invalid\n", func_name);
10949-
return -EINVAL;
10950-
}
10951-
if (opts.retprobe && offset != 0) {
10952-
free(func);
10953-
pr_warn("uretprobes do not support offset specification\n");
10954-
return -EINVAL;
10955-
}
10956-
10957-
/* Is func a raw address? */
10958-
errno = 0;
10959-
raw_offset = strtoull(func, &func_end, 0);
10960-
if (!errno && !*func_end) {
10961-
free(func);
10962-
func = NULL;
10963-
offset = (size_t)raw_offset;
10948+
break;
1096410949
}
10965-
opts.func_name = func;
10950+
free(probe_type);
10951+
free(binary_path);
10952+
free(func_name);
1096610953

10967-
*link = bpf_program__attach_uprobe_opts(prog, -1, binary_path, offset, &opts);
10968-
free(func);
10969-
return libbpf_get_error(*link);
10954+
return ret;
1097010955
}
1097110956

1097210957
struct bpf_link *bpf_program__attach_uprobe(const struct bpf_program *prog,

0 commit comments

Comments
 (0)