Commit 2147c8d0 authored by Hengqi Chen's avatar Hengqi Chen Committed by Andrii Nakryiko

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-symbolsSuggested-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Signed-off-by: default avatarHengqi Chen <hengqi.chen@gmail.com>
Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20230929155954.92448-1-hengqi.chen@gmail.com
parent 9e09b750
...@@ -11114,7 +11114,7 @@ static int attach_uprobe_multi(const struct bpf_program *prog, long cookie, stru ...@@ -11114,7 +11114,7 @@ static int attach_uprobe_multi(const struct bpf_program *prog, long cookie, stru
*link = NULL; *link = NULL;
n = sscanf(prog->sec_name, "%m[^/]/%m[^:]:%ms", n = sscanf(prog->sec_name, "%m[^/]/%m[^:]:%m[^\n]",
&probe_type, &binary_path, &func_name); &probe_type, &binary_path, &func_name);
switch (n) { switch (n) {
case 1: case 1:
...@@ -11624,14 +11624,14 @@ bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid, ...@@ -11624,14 +11624,14 @@ bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,
static int attach_uprobe(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)
{ {
DECLARE_LIBBPF_OPTS(bpf_uprobe_opts, opts); DECLARE_LIBBPF_OPTS(bpf_uprobe_opts, opts);
char *probe_type = NULL, *binary_path = NULL, *func_name = NULL; char *probe_type = NULL, *binary_path = NULL, *func_name = NULL, *func_off;
int n, ret = -EINVAL; int n, c, ret = -EINVAL;
long offset = 0; long offset = 0;
*link = NULL; *link = NULL;
n = sscanf(prog->sec_name, "%m[^/]/%m[^:]:%m[a-zA-Z0-9_.@]+%li", n = sscanf(prog->sec_name, "%m[^/]/%m[^:]:%m[^\n]",
&probe_type, &binary_path, &func_name, &offset); &probe_type, &binary_path, &func_name);
switch (n) { switch (n) {
case 1: case 1:
/* handle SEC("u[ret]probe") - format is valid, but auto-attach is impossible. */ /* 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 ...@@ -11642,7 +11642,17 @@ static int attach_uprobe(const struct bpf_program *prog, long cookie, struct bpf
prog->name, prog->sec_name); prog->name, prog->sec_name);
break; break;
case 3: case 3:
case 4: /* check if user specifies `+offset`, if yes, this should be
* the last part of the string, make sure sscanf read to EOL
*/
func_off = strrchr(func_name, '+');
if (func_off) {
n = sscanf(func_off, "+%li%n", &offset, &c);
if (n == 1 && *(func_off + c) == '\0')
func_off[0] = '\0';
else
offset = 0;
}
opts.retprobe = strcmp(probe_type, "uretprobe") == 0 || opts.retprobe = strcmp(probe_type, "uretprobe") == 0 ||
strcmp(probe_type, "uretprobe.s") == 0; strcmp(probe_type, "uretprobe.s") == 0;
if (opts.retprobe && offset != 0) { if (opts.retprobe && offset != 0) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment