Skip to content

Commit 2147c8d

Browse files
chenhengqianakryiko
authored andcommitted
libbpf: Allow Golang symbols in uprobe secdef
Golang symbols in ELF files are different from C/C++ which contains special characters like '*', '(' and ')'. With generics, things get more complicated, there are symbols like: github.com/cilium/ebpf/internal.(*Deque[go.shape.interface { Format(fmt.State, int32); TypeName() string;github.com/cilium/ebpf/btf.copy() github.com/cilium/ebpf/btf.Type}]).Grow Matching such symbols using `%m[^\n]` in sscanf, this excludes newline which typically does not appear in ELF symbols. This should work in most use-cases and also work for unicode letters in identifiers. If newline do show up in ELF symbols, users can still attach to such symbol by specifying bpf_uprobe_opts::func_name. A working example can be found at this repo ([0]). [0]: https://github.com/chenhengqi/libbpf-go-symbols Suggested-by: Andrii Nakryiko <andrii@kernel.org> Signed-off-by: Hengqi Chen <hengqi.chen@gmail.com> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/bpf/20230929155954.92448-1-hengqi.chen@gmail.com
1 parent 9e09b75 commit 2147c8d

1 file changed

Lines changed: 16 additions & 6 deletions

File tree

tools/lib/bpf/libbpf.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11114,7 +11114,7 @@ static int attach_uprobe_multi(const struct bpf_program *prog, long cookie, stru
1111411114

1111511115
*link = NULL;
1111611116

11117-
n = sscanf(prog->sec_name, "%m[^/]/%m[^:]:%ms",
11117+
n = sscanf(prog->sec_name, "%m[^/]/%m[^:]:%m[^\n]",
1111811118
&probe_type, &binary_path, &func_name);
1111911119
switch (n) {
1112011120
case 1:
@@ -11624,14 +11624,14 @@ bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,
1162411624
static int attach_uprobe(const struct bpf_program *prog, long cookie, struct bpf_link **link)
1162511625
{
1162611626
DECLARE_LIBBPF_OPTS(bpf_uprobe_opts, opts);
11627-
char *probe_type = NULL, *binary_path = NULL, *func_name = NULL;
11628-
int n, ret = -EINVAL;
11627+
char *probe_type = NULL, *binary_path = NULL, *func_name = NULL, *func_off;
11628+
int n, c, ret = -EINVAL;
1162911629
long offset = 0;
1163011630

1163111631
*link = NULL;
1163211632

11633-
n = sscanf(prog->sec_name, "%m[^/]/%m[^:]:%m[a-zA-Z0-9_.@]+%li",
11634-
&probe_type, &binary_path, &func_name, &offset);
11633+
n = sscanf(prog->sec_name, "%m[^/]/%m[^:]:%m[^\n]",
11634+
&probe_type, &binary_path, &func_name);
1163511635
switch (n) {
1163611636
case 1:
1163711637
/* handle SEC("u[ret]probe") - format is valid, but auto-attach is impossible. */
@@ -11642,7 +11642,17 @@ static int attach_uprobe(const struct bpf_program *prog, long cookie, struct bpf
1164211642
prog->name, prog->sec_name);
1164311643
break;
1164411644
case 3:
11645-
case 4:
11645+
/* check if user specifies `+offset`, if yes, this should be
11646+
* the last part of the string, make sure sscanf read to EOL
11647+
*/
11648+
func_off = strrchr(func_name, '+');
11649+
if (func_off) {
11650+
n = sscanf(func_off, "+%li%n", &offset, &c);
11651+
if (n == 1 && *(func_off + c) == '\0')
11652+
func_off[0] = '\0';
11653+
else
11654+
offset = 0;
11655+
}
1164611656
opts.retprobe = strcmp(probe_type, "uretprobe") == 0 ||
1164711657
strcmp(probe_type, "uretprobe.s") == 0;
1164811658
if (opts.retprobe && offset != 0) {

0 commit comments

Comments
 (0)