Skip to contentSkip to conversation

libbpf: Allow Golang symbols in uprobe secdef

PATCH Superseded Hengqi Chen 3 participants 5 messages Sep 25, 2023 – Sep 27, 2023

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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

Add " ()*,-/;[]{}" (in alphabetical order) to support matching against such symbols. Note that ']' and '-' should be the first and last characters in the %m range as sscanf required.

A working example can be found at this repo ([0]).

[0]: https://github.com/chenhengqi/libbpf-go-symbols

---
 tools/lib/bpf/libbpf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +1 -1
index b4758e54a815..de0e068195ab 100644
@@ -11630,7 +11630,7 @@ static int attach_uprobe(const struct bpf_program *prog, long cookie, struct bpf
 
 	*link = NULL;
 
-	n = sscanf(prog->sec_name, "%m[^/]/%m[^:]:%m[a-zA-Z0-9_.@]+%li",
+	n = sscanf(prog->sec_name, "%m[^/]/%m[^:]:%m[]a-zA-Z0-9 ()*,./;@[_{}-]+%li",
 		   &probe_type, &binary_path, &func_name, &offset);
 	switch (n) {
 	case 1:
Signature · 1 line
2.34.1
#1 lore raw
Quoted 30 lines · Hengqi Chen

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

Add " ()*,-/;[]{}" (in alphabetical order) to support matching against such symbols. Note that ']' and '-' should be the first and last characters in the %m range as sscanf required.

A working example can be found at this repo ([0]).

[0]: https://github.com/chenhengqi/libbpf-go-symbols

---
 tools/lib/bpf/libbpf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +0 -0
index b4758e54a815..de0e068195ab 100644

tools/lib/bpf/libbpf.c static int attach_uprobe(const struct bpf_program *prog, long cookie, struct bpf · line 11634

Quoted 6 lines
tools/lib/bpf/libbpf.c +1 -1
@@ -11630,7 +11630,7 @@ static int attach_uprobe(const struct bpf_program *prog, long cookie, struct bpf
 
 	*link = NULL;
 
-	n = sscanf(prog->sec_name, "%m[^/]/%m[^:]:%m[a-zA-Z0-9_.@]+%li",
+	n = sscanf(prog->sec_name, "%m[^/]/%m[^:]:%m[]a-zA-Z0-9 ()*,./;@[_{}-]+%li",
 		   &probe_type, &binary_path, &func_name, &offset);

could you please make that work for uprobe.multi (attach_uprobe_multi) as well?

it uses %ms at the moment and it seems it won't get pass the space in the symbol name

thanks,
jirka

Quoted 6 lines
tools/lib/bpf/libbpf.c +0 -0
 	switch (n) {
 	case 1:
Signature · 1 line
2.34.1
#2 lore raw
Quoted 33 lines · Jiri Olsa
Quoted 31 lines · Hengqi Chen

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

Add " ()*,-/;[]{}" (in alphabetical order) to support matching against such symbols. Note that ']' and '-' should be the first and last characters in the %m range as sscanf required.

A working example can be found at this repo ([0]).

[0]: https://github.com/chenhengqi/libbpf-go-symbols

---
 tools/lib/bpf/libbpf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +0 -0
index b4758e54a815..de0e068195ab 100644
@@ -11630,7 +11630,7 @@ static int attach_uprobe(const struct bpf_program *prog, long cookie, struct bpf
 
Quoted 9 lines

tools/lib/bpf/libbpf.c static int attach_uprobe(const struct bpf_program *prog, long cookie, struct bpf · line 11634

Quoted 5 lines
tools/lib/bpf/libbpf.c +1 -1
      *link = NULL;
 
-     n = sscanf(prog->sec_name, "%m[^/]/%m[^:]:%m[a-zA-Z0-9_.@]+%li",
+     n = sscanf(prog->sec_name, "%m[^/]/%m[^:]:%m[]a-zA-Z0-9 ()*,./;@[_{}-]+%li",
                 &probe_type, &binary_path, &func_name, &offset);

could you please make that work for uprobe.multi (attach_uprobe_multi) as well?

I haven't used uprobe.multi before, let me try.

Quoted 12 lines

it uses %ms at the moment and it seems it won't get pass the space in the symbol name

thanks,
jirka

Quoted 6 lines
tools/lib/bpf/libbpf.c +0 -1
      switch (n) {
      case 1:
--

2.34.1

#3 lore raw
In reply to Hengqi Chen
Quoted 31 lines · Hengqi Chen

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

Add " ()*,-/;[]{}" (in alphabetical order) to support matching against such symbols. Note that ']' and '-' should be the first and last characters in the %m range as sscanf required.

A working example can be found at this repo ([0]).

[0]: https://github.com/chenhengqi/libbpf-go-symbols

---
 tools/lib/bpf/libbpf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +0 -0
index b4758e54a815..de0e068195ab 100644

tools/lib/bpf/libbpf.c static int attach_uprobe(const struct bpf_program *prog, long cookie, struct bpf · line 11633

Quoted 5 lines
tools/lib/bpf/libbpf.c +1 -1
@@ -11630,7 +11630,7 @@ static int attach_uprobe(const struct bpf_program *prog, long cookie, struct bpf
 
        *link = NULL;
 
-       n = sscanf(prog->sec_name, "%m[^/]/%m[^:]:%m[a-zA-Z0-9_.@]+%li",
+       n = sscanf(prog->sec_name, "%m[^/]/%m[^:]:%m[]a-zA-Z0-9 ()*,./;@[_{}-]+%li",

This is almost incomprehensible now... wouldn't it be clearer to just have a catch-all %ms at the end, and then internally checking if we have '+%li'? I.e., once we match everything after "uprobe/<path-to-binary>:", we can strchr('+'), if found, try sscanf("%li") on the remaining suffix. If that doesn't parse properly, then we have a choice -- either error out, or just assume that +<something> part is just a part of ELF symbol name?

That way we don't hard-code any fixes set of symbols and avoid any future crazy adjustments.

WDYT?

Quoted 6 lines
tools/lib/bpf/libbpf.c +0 -1
                   &probe_type, &binary_path, &func_name, &offset);
        switch (n) {
        case 1:
--

2.34.1

#4 lore raw
Quoted 47 lines · Andrii Nakryiko
Quoted 31 lines · Hengqi Chen

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

Add " ()*,-/;[]{}" (in alphabetical order) to support matching against such symbols. Note that ']' and '-' should be the first and last characters in the %m range as sscanf required.

A working example can be found at this repo ([0]).

[0]: https://github.com/chenhengqi/libbpf-go-symbols

---
 tools/lib/bpf/libbpf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +0 -0
index b4758e54a815..de0e068195ab 100644

tools/lib/bpf/libbpf.c static int attach_uprobe(const struct bpf_program *prog, long cookie, struct bpf · line 11633

Quoted 5 lines
tools/lib/bpf/libbpf.c +1 -1
@@ -11630,7 +11630,7 @@ static int attach_uprobe(const struct bpf_program *prog, long cookie, struct bpf
 
        *link = NULL;
 
-       n = sscanf(prog->sec_name, "%m[^/]/%m[^:]:%m[a-zA-Z0-9_.@]+%li",
+       n = sscanf(prog->sec_name, "%m[^/]/%m[^:]:%m[]a-zA-Z0-9 ()*,./;@[_{}-]+%li",

This is almost incomprehensible now... wouldn't it be clearer to just have a catch-all %ms at the end, and then internally checking if we have '+%li'? I.e., once we match everything after "uprobe/<path-to-binary>:", we can strchr('+'), if found, try sscanf("%li") on the remaining suffix. If that doesn't parse properly, then we have a choice -- either error out, or just assume that +<something> part is just a part of ELF symbol name?

Quoted 4 lines

That way we don't hard-code any fixes set of symbols and avoid any future crazy adjustments.

WDYT?

Sounds good. This also solves the matching of unicode identifiers.

As Jiri mentioned above, %ms won't match whitespaces, so I am wondering if %m[^\n] is acceptable.

Quoted 7 lines
Quoted 7 lines
tools/lib/bpf/libbpf.c +0 -1
                   &probe_type, &binary_path, &func_name, &offset);
        switch (n) {
        case 1:
--

2.34.1