Skip to content

Commit

Permalink
bpflbr: Dump line info for call/jmp insn
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 bd382ae commit 0fe9357
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
43 changes: 38 additions & 5 deletions internal/bpflbr/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"log"
"os"
"strconv"
"strings"

"github.com/cilium/ebpf"
Expand All @@ -31,6 +32,26 @@ func DumpProg(pf []ProgFlag) {
log.Fatalf("No prog found")
}

kallsyms, err := NewKallsyms()
assert.NoErr(err, "Failed to read /proc/kallsyms: %v")

vmlinux, err := FindVmlinux()
assert.NoErr(err, "Failed to find vmlinux: %v")

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

kaslrOffset := textAddr - kallsyms.Stext()
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")
defer engine.Close()

bpfProgs, err := NewBPFProgs(engine, nil, false)
assert.NoErr(err, "Failed to get bpf progs: %v")

info, err := prog.Info()
assert.NoErr(err, "Failed to get prog info: %v")

Expand Down Expand Up @@ -63,10 +84,6 @@ func DumpProg(pf []ProgFlag) {
jited2LineInfos[kaddr] = lines[i]
}

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

intelSyntax := os.Getenv("BPFLBR_DUMP_INTEL_SYNTAX") == "1"
if !intelSyntax {
err = engine.SetOption(uint(gapstone.CS_OPT_SYNTAX), uint(gapstone.CS_OPT_SYNTAX_ATT))
Expand Down Expand Up @@ -98,7 +115,23 @@ func DumpProg(pf []ProgFlag) {
opcodes = append(opcodes, fmt.Sprintf("%02x", insn))
}
opcode := strings.Join(opcodes, " ")
fmt.Fprintf(&sb, "%#x: %-19s\t%s\t%s\n", kaddr, opcode, inst[0].Mnemonic, inst[0].OpStr)
opstr := inst[0].OpStr
fmt.Fprintf(&sb, "%#x: %-19s\t%s\t%s", kaddr, opcode, inst[0].Mnemonic, opstr)

var endpoint *branchEndpoint
if strings.HasPrefix(opstr, "0x") {
n, err := strconv.ParseUint(opstr, 0, 64)
if err == nil {
endpoint = getLineInfo(uintptr(n), bpfProgs, addr2line, kallsyms)
}
}
if endpoint != nil {
fmt.Fprintf(&sb, "\t; %s+%#x %s:%d", endpoint.funcName, endpoint.offset, endpoint.fileName, endpoint.fileLine)
if endpoint.isProg {
fmt.Fprintf(&sb, " [bpf]")
}
}
fmt.Fprintln(&sb)

insnSize := uint64(inst[0].Size)
pc += insnSize
Expand Down
1 change: 1 addition & 0 deletions internal/bpflbr/lbr.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ func getLineInfo(addr uintptr, progs *bpfProgs, a2l *Addr2Line, ksyms *Kallsyms)
ep.funcName = li.funcName
ep.fileName = li.fileName
ep.fileLine = li.fileLine
ep.isProg = true
ep.updateInfo()
return &ep
}
Expand Down
1 change: 1 addition & 0 deletions internal/bpflbr/lbr_stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type branchEndpoint struct {
fileName string
fileLine uint32
lineInfo string // (${fileName}:${fileLine})
isProg bool
}

func (b *branchEndpoint) updateInfo() {
Expand Down

0 comments on commit 0fe9357

Please sign in to comment.