Skip to content

Commit

Permalink
bpflbr: Add verbose log when --verbose
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 0414e9f commit 27c3189
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
6 changes: 6 additions & 0 deletions internal/bpflbr/disasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func readKcore(kaddr uint64, bytes uint) ([]byte, bool) {
}

func dumpKfunc(kfunc string, bytes uint) {
VerboseLog("Reading /proc/kallsyms ..")
kallsyms, err := NewKallsyms()
assert.NoErr(err, "Failed to read /proc/kallsyms: %v")

Expand All @@ -82,11 +83,15 @@ func dumpKfunc(kfunc string, bytes uint) {

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

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

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

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

Expand All @@ -99,6 +104,7 @@ func dumpKfunc(kfunc string, bytes uint) {
assert.NoErr(err, "Failed to set syntax: %v")
}

VerboseLog("Disassembling bpf progs ..")
bpfProgs, err := NewBPFProgs(engine, nil, false)
assert.NoErr(err, "Failed to get bpf progs: %v")
defer bpfProgs.Close()
Expand Down
6 changes: 6 additions & 0 deletions internal/bpflbr/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,29 @@ func DumpProg(pf []ProgFlag) {
log.Fatalf("No prog found")
}

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

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

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

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

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")
defer engine.Close()

VerboseLog("Disassembling bpf progs ..")
bpfProgs, err := NewBPFProgs(engine, nil, false)
assert.NoErr(err, "Failed to get bpf progs: %v")
defer bpfProgs.Close()
Expand Down
12 changes: 12 additions & 0 deletions internal/bpflbr/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright 2024 Leon Hwang.
// SPDX-License-Identifier: Apache-2.0

package bpflbr

import "log"

func VerboseLog(format string, args ...interface{}) {
if verbose {
log.Printf(format, args...)
}
}
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,30 +54,37 @@ func main() {
assert.NoErr(err, "Failed to open LBR perf event: %v")
defer lbrPerfEvents.Close()

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

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

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)

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")
defer engine.Close()

bpflbr.VerboseLog("Disassembling bpf progs ..")
bpfProgs, err := bpflbr.NewBPFProgs(engine, progs, false)
assert.NoErr(err, "Failed to get bpf progs: %v")
defer bpfProgs.Close()

tracingTargets := bpfProgs.Tracings()
assert.True(len(tracingTargets)+len(flags.Kfuncs()) != 0, "No tracing target")

bpflbr.VerboseLog("Tracing bpf progs or kernel functions ..")
bpfSpec, err := loadLbr()
assert.NoErr(err, "Failed to load bpf spec: %v")
delete(bpfSpec.Programs, bpflbr.TracingProgName(flags.OtherMode()))
Expand Down

0 comments on commit 27c3189

Please sign in to comment.