From 27c3189fda83259aa69b8c8fc20b802a5eacc837 Mon Sep 17 00:00:00 2001 From: Leon Hwang Date: Sun, 24 Nov 2024 17:00:26 +0800 Subject: [PATCH] bpflbr: Add verbose log when --verbose Signed-off-by: Leon Hwang --- internal/bpflbr/disasm.go | 6 ++++++ internal/bpflbr/dump.go | 6 ++++++ internal/bpflbr/log.go | 12 ++++++++++++ main.go | 7 +++++++ 4 files changed, 31 insertions(+) create mode 100644 internal/bpflbr/log.go diff --git a/internal/bpflbr/disasm.go b/internal/bpflbr/disasm.go index 0db2ff9..ad2453a 100644 --- a/internal/bpflbr/disasm.go +++ b/internal/bpflbr/disasm.go @@ -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") @@ -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") @@ -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() diff --git a/internal/bpflbr/dump.go b/internal/bpflbr/dump.go index e6f5716..78ef285 100644 --- a/internal/bpflbr/dump.go +++ b/internal/bpflbr/dump.go @@ -31,16 +31,21 @@ 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") @@ -48,6 +53,7 @@ func DumpProg(pf []ProgFlag) { 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() diff --git a/internal/bpflbr/log.go b/internal/bpflbr/log.go new file mode 100644 index 0000000..802c7da --- /dev/null +++ b/internal/bpflbr/log.go @@ -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...) + } +} diff --git a/main.go b/main.go index f0ce06e..9134710 100644 --- a/main.go +++ b/main.go @@ -54,16 +54,21 @@ 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") @@ -71,6 +76,7 @@ func main() { 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() @@ -78,6 +84,7 @@ func main() { 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()))