Skip to content

Commit

Permalink
bpflbr: Remove vmlinux requirement for disasm
Browse files Browse the repository at this point in the history
Signed-off-by: Leon Hwang <[email protected]>
  • Loading branch information
Asphaltt committed Dec 5, 2024
1 parent 27c3189 commit f997662
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 29 deletions.
29 changes: 20 additions & 9 deletions internal/bpflbr/disasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package bpflbr

import (
"debug/elf"
"errors"
"fmt"
"os"
"strconv"
Expand Down Expand Up @@ -81,19 +82,29 @@ func dumpKfunc(kfunc string, bytes uint) {
data, ok := readKcore(kaddr, uint(bytes))
assert.True(ok, "Failed to read kcore for %s", kfunc)

var addr2line *Addr2Line

vmlinux, err := FindVmlinux()
assert.NoErr(err, "Failed to find vmlinux: %v")
VerboseLog("Found vmlinux: %s", vmlinux)
if err != nil {
if errors.Is(err, ErrNotFound) {
VerboseLog("Dbgsym vmlinux not found")
} else {
assert.NoErr(err, "Failed to find vmlinux: %v")
}
}
if err == nil {
VerboseLog("Found vmlinux: %s", vmlinux)

textAddr, err := ReadTextAddrFromVmlinux(vmlinux)
assert.NoErr(err, "Failed to read .text address from vmlinux: %v")
textAddr, err := ReadTextAddrFromVmlinux(vmlinux)
assert.NoErr(err, "Failed to read .text address from vmlinux: %v")

kaslrOffset := textAddr - kallsyms.Stext()
VerboseLog("KASLR offset: %#x", kaslrOffset)
kaslrOffset := textAddr - kallsyms.Stext()
VerboseLog("KASLR offset: 0x%x", kaslrOffset)

VerboseLog("Creating addr2line from vmlinux ..")
addr2line, err := NewAddr2Line(vmlinux, kaslrOffset, kallsyms.SysBPF())
assert.NoErr(err, "Failed to create addr2line: %v")
VerboseLog("Creating addr2line from vmlinux ..")
addr2line, err = NewAddr2Line(vmlinux, kaslrOffset, kallsyms.SysBPF())
assert.NoErr(err, "Failed to create addr2line: %v")
}

engine, err := gapstone.New(int(gapstone.CS_ARCH_X86), int(gapstone.CS_MODE_64))
assert.NoErr(err, "Failed to create engine: %v")
Expand Down
34 changes: 24 additions & 10 deletions internal/bpflbr/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package bpflbr

import (
"errors"
"fmt"
"log"
"strconv"
Expand Down Expand Up @@ -35,19 +36,29 @@ func DumpProg(pf []ProgFlag) {
kallsyms, err := NewKallsyms()
assert.NoErr(err, "Failed to read /proc/kallsyms: %v")

var addr2line *Addr2Line

vmlinux, err := FindVmlinux()
assert.NoErr(err, "Failed to find vmlinux: %v")
VerboseLog("Found vmlinux: %s", vmlinux)
if err != nil {
if errors.Is(err, ErrNotFound) {
VerboseLog("Dbgsym vmlinux not found")
} else {
assert.NoErr(err, "Failed to find vmlinux: %v")
}
}
if err == nil {
VerboseLog("Found vmlinux: %s", vmlinux)

textAddr, err := ReadTextAddrFromVmlinux(vmlinux)
assert.NoErr(err, "Failed to read .text address from vmlinux: %v")
textAddr, err := ReadTextAddrFromVmlinux(vmlinux)
assert.NoErr(err, "Failed to read .text address from vmlinux: %v")

kaslrOffset := textAddr - kallsyms.Stext()
VerboseLog("KASLR offset: %#x", kaslrOffset)
kaslrOffset := textAddr - kallsyms.Stext()
VerboseLog("KASLR offset: 0x%x", kaslrOffset)

VerboseLog("Creating addr2line from vmlinux ..")
addr2line, err := NewAddr2Line(vmlinux, kaslrOffset, kallsyms.SysBPF())
assert.NoErr(err, "Failed to create addr2line: %v")
VerboseLog("Creating addr2line from vmlinux ..")
addr2line, err = NewAddr2Line(vmlinux, kaslrOffset, kallsyms.SysBPF())
assert.NoErr(err, "Failed to create addr2line: %v")
}

engine, err := gapstone.New(int(gapstone.CS_ARCH_X86), int(gapstone.CS_MODE_64))
assert.NoErr(err, "Failed to create engine: %v")
Expand Down Expand Up @@ -134,7 +145,10 @@ func DumpProg(pf []ProgFlag) {
}
}
if endpoint != nil {
fmt.Fprintf(&sb, "\t; %s+%#x %s:%d", endpoint.funcName, endpoint.offset, endpoint.fileName, endpoint.fileLine)
fmt.Fprintf(&sb, "\t; %s+%#x", endpoint.funcName, endpoint.offset)
if endpoint.fileName != "" {
fmt.Fprintf(&sb, " %s:%d", endpoint.fileName, endpoint.fileLine)
}
if endpoint.isProg {
fmt.Fprintf(&sb, " [bpf]")
}
Expand Down
8 changes: 8 additions & 0 deletions internal/bpflbr/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright 2024 Leon Hwang.
// SPDX-License-Identifier: Apache-2.0

package bpflbr

import "errors"

var ErrNotFound = errors.New("not found")
4 changes: 4 additions & 0 deletions internal/bpflbr/lbr.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ func getLineInfo(addr uintptr, progs *bpfProgs, a2l *Addr2Line, ksyms *Kallsyms)
ep.offset = addr - uintptr(ksym.addr)
}

if a2l == nil {
return &ep
}

li, err := a2l.get(addr)
if err != nil {
return &ep
Expand Down
2 changes: 1 addition & 1 deletion internal/bpflbr/vmlinux.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func FindVmlinux() (string, error) {
}
}

return "", errors.New("vmlinux file not found")
return "", ErrNotFound
}

// ReadTextAddrFromVmlinux reads .text section address from vmlinux file.
Expand Down
28 changes: 19 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,29 @@ func main() {
kallsyms, err := bpflbr.NewKallsyms()
assert.NoErr(err, "Failed to read /proc/kallsyms: %v")

var addr2line *bpflbr.Addr2Line

vmlinux, err := bpflbr.FindVmlinux()
assert.NoErr(err, "Failed to find vmlinux: %v")
bpflbr.VerboseLog("Found vmlinux: %s", vmlinux)
if err != nil {
if errors.Is(err, bpflbr.ErrNotFound) {
bpflbr.VerboseLog("Dbgsym vmlinux not found")
} else {
assert.NoErr(err, "Failed to find vmlinux: %v")
}
}
if err == nil {
bpflbr.VerboseLog("Found vmlinux: %s", vmlinux)

textAddr, err := bpflbr.ReadTextAddrFromVmlinux(vmlinux)
assert.NoErr(err, "Failed to read .text address from vmlinux: %v")
textAddr, err := bpflbr.ReadTextAddrFromVmlinux(vmlinux)
assert.NoErr(err, "Failed to read .text address from vmlinux: %v")

kaslrOffset := textAddr - kallsyms.Stext()
bpflbr.VerboseLog("KASLR offset: 0x%x", kaslrOffset)
kaslrOffset := textAddr - kallsyms.Stext()
bpflbr.VerboseLog("KASLR offset: 0x%x", kaslrOffset)

bpflbr.VerboseLog("Creating addr2line from vmlinux ..")
addr2line, err := bpflbr.NewAddr2Line(vmlinux, kaslrOffset, kallsyms.SysBPF())
assert.NoErr(err, "Failed to create addr2line: %v")
bpflbr.VerboseLog("Creating addr2line from vmlinux ..")
addr2line, err = bpflbr.NewAddr2Line(vmlinux, kaslrOffset, kallsyms.SysBPF())
assert.NoErr(err, "Failed to create addr2line: %v")
}

engine, err := gapstone.New(int(gapstone.CS_ARCH_X86), int(gapstone.CS_MODE_64))
assert.NoErr(err, "Failed to create capstone engine: %v")
Expand Down

0 comments on commit f997662

Please sign in to comment.