From 0ab0dc145510c930c992032cbfeb85f779006af7 Mon Sep 17 00:00:00 2001 From: Leon Hwang Date: Thu, 30 May 2024 20:32:49 +0800 Subject: [PATCH] fix: Get bpf prog entry func name from insns It is possible to get wrong entry func name of bpf prog when there is info of multiple bpf progs in the BTF. Instead, get the very first symbol from bpf prog's instructions as its entry func name. Signed-off-by: Leon Hwang --- internal/pwru/bpf_prog.go | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/internal/pwru/bpf_prog.go b/internal/pwru/bpf_prog.go index 425d2e11..760d559d 100644 --- a/internal/pwru/bpf_prog.go +++ b/internal/pwru/bpf_prog.go @@ -8,7 +8,6 @@ import ( "fmt" "github.com/cilium/ebpf" - "github.com/cilium/ebpf/btf" "golang.org/x/sys/unix" ) @@ -47,26 +46,15 @@ func getEntryFuncName(prog *ebpf.Program) (string, string, error) { return "", "", fmt.Errorf("failed to get program info: %w", err) } - id, ok := info.BTFID() - if !ok { - return "", "", fmt.Errorf("bpf program %s does not have BTF", info.Name) - } - - handle, err := btf.NewHandleFromID(id) - if err != nil { - return "", "", fmt.Errorf("failed to get BTF handle: %w", err) - } - defer handle.Close() - - spec, err := handle.Spec(nil) + insns, err := info.Instructions() if err != nil { - return "", "", fmt.Errorf("failed to get BTF spec: %w", err) + return "", "", fmt.Errorf("failed to get program instructions: %w", err) } - iter := spec.Iterate() - for iter.Next() { - if fn, ok := iter.Type.(*btf.Func); ok { - return fn.Name, info.Name, nil + for _, insn := range insns { + sym := insn.Symbol() + if sym != "" { + return sym, info.Name, nil } }