Skip to content

Commit

Permalink
Do kprobe.multi on addresses instead of symbols
Browse files Browse the repository at this point in the history
As Martynas P. suggests, it's better to use func addrs instead of symbols
for `kprobe.multi`.

Signed-off-by: Leon Hwang <hffilwlqm@gmail.com>
  • Loading branch information
Asphaltt authored and brb committed Feb 16, 2024
1 parent 7b444b3 commit f7c6303
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
3 changes: 3 additions & 0 deletions internal/pwru/ksym.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func (a byAddr) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
type Addr2Name struct {
Addr2NameMap map[uint64]*ksym
Addr2NameSlice []*ksym
Name2AddrMap map[string][]uintptr
}

func (a *Addr2Name) findNearestSym(ip uint64) string {
Expand All @@ -44,6 +45,7 @@ func (a *Addr2Name) findNearestSym(ip uint64) string {
func GetAddrs(funcs Funcs, all bool) (Addr2Name, error) {
a2n := Addr2Name{
Addr2NameMap: make(map[uint64]*ksym),
Name2AddrMap: make(map[string][]uintptr),
}

file, err := os.Open("/proc/kallsyms")
Expand All @@ -66,6 +68,7 @@ func GetAddrs(funcs Funcs, all bool) (Addr2Name, error) {
name: name,
}
a2n.Addr2NameMap[addr] = sym
a2n.Name2AddrMap[name] = append(a2n.Name2AddrMap[name], uintptr(addr))
if all {
a2n.Addr2NameSlice = append(a2n.Addr2NameSlice, sym)
}
Expand Down
21 changes: 16 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,14 +295,25 @@ func main() {
default:
}

opts := link.KprobeMultiOptions{Symbols: funcsByPos[pos]}
addrs := make([]uintptr, 0, len(fns))
for _, fn := range fns {
if addr, ok := addr2name.Name2AddrMap[fn]; ok {
addrs = append(addrs, addr...)
} else {
ignored += 1
bar.Increment()
continue
}
}

if len(addrs) == 0 {
continue
}

opts := link.KprobeMultiOptions{Addresses: addrs}
kp, err := link.KprobeMulti(fn, opts)
bar.Add(len(fns))
if err != nil {
if errors.Is(err, syscall.EADDRNOTAVAIL) {
log.Fatalf("Found duplicate function name in the kernel (%s). Set --backend=kprobe to fix the loading error until https://github.com/cilium/pwru/issues/284 has been fixed",
err)
}
log.Fatalf("Opening kprobe-multi for pos %d: %s\n", pos, err)
}
kprobes = append(kprobes, kp)
Expand Down

0 comments on commit f7c6303

Please sign in to comment.