A Go tool for formatting bpftrace scripts.
- Automatic formatting of bpftrace probe definitions
- Support for predicates formatting
- Unified indentation and spacing
- Preserve comments and shebang
- Configurable formatting options
go mod tidy
go build -o bpftrace-formatter./bpftrace-formatter <file.bt>Input file (test_input.bt):
#!/usr/bin/env bpftrace
tracepoint:syscalls:sys_enter_openat{printf("openat: %s\n",str(args.filename));}
tracepoint:syscalls:sys_enter_openat2{printf("openat2: %s\n",str(args->filename));}
tracepoint:syscalls:sys_enter_openat/pid==1234/{@opens[pid]=count();}
Output:
#!/usr/bin/env bpftrace
tracepoint:syscalls:sys_enter_openat {
printf("openat: %s\n",str(args.filename));
}
tracepoint:syscalls:sys_enter_openat2 {
printf("openat2: %s\n",str(args->filename));
}
tracepoint:syscalls:sys_enter_openat/pid==1234/ {
@opens[pid]=count();
}
The following options can be configured through code:
SetIndentSize(int): Set indentation size (default: 4)SetUseSpaces(bool): Use spaces instead of tabs (default: true)SetProbeSpacing(int): Set number of empty lines between probes (default: 2)
go test -vmain.go: Main formatting logicmain_test.go: Unit testsexample.bt: Formatting exampletest_input.bt: Test input file
- Probe definitions (tracepoint, kprobe, uprobe, etc.)
- Predicate filtering
- Block statement formatting
- Comment preservation
- END block handling
- Multi-line statement support
- Currently mainly handles single-line probe definitions
- Complex multi-line blocks may need further optimization
- Some special bpftrace syntax may not be fully supported
This project is developed in Go. Contributions and issue reports are welcome.