Skip to content

Commit

Permalink
bpflbr: Add --output
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 03e7c09 commit 15eeae0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
7 changes: 7 additions & 0 deletions internal/bpflbr/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ func parseProgsFlag(progs []string) ([]ProgFlag, error) {
type Flags struct {
progs []string

outputFile string

dumpProg bool
}

Expand All @@ -105,6 +107,7 @@ func ParseFlags() (*Flags, error) {

f := flag.NewFlagSet("bpflbr", flag.ExitOnError)
f.StringSliceVarP(&flags.progs, "prog", "p", nil, "bpf prog info for bpflbr in format PROG[,PROG,..], PROG: PROGID[:<prog function name>], PROGID: <prog ID> or 'i/id:<prog ID>' or 'p/pinned:<pinned file>' or 't/tag:<prog tag>' or 'n/name:<prog full name>'; all bpf progs will be traced by default")
f.StringVarP(&flags.outputFile, "output", "o", "", "output file for the result, default is stdout")
f.BoolVar(&flags.dumpProg, "dump-jited", false, "dump native insn info of bpf prog, the one bpf prog must be provided by --prog (its function name will be ignored)")

return &flags, f.Parse(os.Args)
Expand All @@ -114,6 +117,10 @@ func (f *Flags) ParseProgs() ([]ProgFlag, error) {
return parseProgsFlag(f.progs)
}

func (f *Flags) OutputFile() string {
return f.outputFile
}

func (f *Flags) DumpProg() bool {
return f.dumpProg
}
5 changes: 3 additions & 2 deletions internal/bpflbr/lbr.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ package bpflbr
import (
"errors"
"fmt"
"io"
"strings"
"unsafe"

"github.com/cilium/ebpf/ringbuf"
"golang.org/x/sys/unix"
)

func Run(reader *ringbuf.Reader, progs *bpfProgs, addr2line *Addr2Line, ksyms *Kallsyms) error {
func Run(reader *ringbuf.Reader, progs *bpfProgs, addr2line *Addr2Line, ksyms *Kallsyms, w io.Writer) error {
type LbrEntry struct {
From uintptr
To uintptr
Expand Down Expand Up @@ -85,7 +86,7 @@ func Run(reader *ringbuf.Reader, progs *bpfProgs, addr2line *Addr2Line, ksyms *K

fmt.Fprintf(&sb, "Recv a record for %s with retval=%d :\n", progName, event.Retval)
stack.output(&sb)
fmt.Println(sb.String())
fmt.Fprintln(w, sb.String())

sb.Reset()
stack.reset()
Expand Down
10 changes: 9 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ func main() {
assert.NoErr(err, "Failed to create ringbuf reader: %v")
defer reader.Close()

w := os.Stdout
if flags.OutputFile() != "" {
f, err := os.OpenFile(flags.OutputFile(), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o644)
assert.NoErr(err, "Failed to create output file: %v")
defer f.Close()
w = f
}

log.Print("bpflbr is running..")

ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
Expand All @@ -98,7 +106,7 @@ func main() {
})

errg.Go(func() error {
return bpflbr.Run(reader, bpfProgs, addr2line, kallsyms)
return bpflbr.Run(reader, bpfProgs, addr2line, kallsyms, w)
})

err = errg.Wait()
Expand Down

0 comments on commit 15eeae0

Please sign in to comment.