Skip to content

Commit

Permalink
fix: Get bpf prog entry func name from insns
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Asphaltt authored and brb committed Jul 5, 2024
1 parent c857506 commit 0ab0dc1
Showing 1 changed file with 6 additions and 18 deletions.
24 changes: 6 additions & 18 deletions internal/pwru/bpf_prog.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"

"github.com/cilium/ebpf"
"github.com/cilium/ebpf/btf"
"golang.org/x/sys/unix"
)

Expand Down Expand Up @@ -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
}
}

Expand Down

0 comments on commit 0ab0dc1

Please sign in to comment.