A formatter for bpftrace scripts with VS Code integration.
- Format bpftrace scripts with consistent indentation, spacing, and structure
- VS Code extension with bundled binary - install and use immediately
- Language Server Protocol (LSP) support for editor integration
- Configurable formatting rules via JSON configuration file
- Preserves comments and shebangs
Install the btfmt-lsp extension from the Releases page:
- Download the
.vsixfile for your platform (e.g.,btfmt-lsp-0.0.2@linux-x64.vsix) - In VS Code, press
Ctrl+Shift+Pand run "Extensions: Install from VSIX..." - Select the downloaded file
The extension includes the btfmt binary - no additional installation required.
Download the pre-built binary from Releases:
| Platform | File |
|---|---|
| Linux x64 | btfmt-linux-amd64.tar.gz |
| Linux ARM64 | btfmt-linux-arm64.tar.gz |
| macOS x64 | btfmt-darwin-amd64.tar.gz |
| macOS ARM64 | btfmt-darwin-arm64.tar.gz |
| Windows x64 | btfmt-windows-amd64.zip |
| Windows ARM64 | btfmt-windows-arm64.zip |
Extract and add to your PATH:
tar -xzf btfmt-linux-amd64.tar.gz
sudo mv btfmt /usr/local/bin/go install github.com/fanyang89/bpftrace-formatter/cmd/btfmt@latestOr clone and build:
git clone https://github.com/fanyang89/bpftrace-formatter.git
cd bpftrace-formatter
go build ./cmd/btfmtbtfmt script.bt # Print formatted output to stdout
btfmt -w script.bt # Write result back to file
btfmt -w *.bt # Format multiple filesBefore:
#!/usr/bin/env bpftrace
tracepoint:syscalls:sys_enter_openat{printf("openat: %s\n",str(args.filename));}
tracepoint:syscalls:sys_enter_openat/pid==1234/{@opens[pid]=count();}
After:
#!/usr/bin/env bpftrace
tracepoint:syscalls:sys_enter_openat
{
printf("openat: %s\n", str(args.filename));
}
tracepoint:syscalls:sys_enter_openat /pid == 1234/
{
@opens[pid] = count();
}
btfmt [options] <file.bt> [file2.bt ...]
Options:
-w Write result to source file
-i Edit files in place (same as -w)
-c, -config <file> Path to configuration file
-v, -verbose Enable verbose output
-generate-config Generate default configuration file
-version Show version information
-help Show help message
btfmt looks for configuration in this order:
- File specified with
-configflag .btfmt.jsonin current directory or parent directories~/.btfmt.jsonin home directory- Built-in defaults
Generate a default configuration file:
btfmt -generate-configExample .btfmt.json:
{
"indent": {
"size": 4,
"use_spaces": true
},
"spacing": {
"around_operators": true,
"around_commas": true
},
"line_breaks": {
"empty_lines_between_probes": 1
},
"blocks": {
"brace_style": "next_line"
}
}| Section | Option | Default | Description |
|---|---|---|---|
indent |
size |
4 | Spaces/tabs per indent level |
indent |
use_spaces |
true | Use spaces instead of tabs |
spacing |
around_operators |
true | Space around =, +, -, etc. |
spacing |
around_commas |
true | Space after commas |
spacing |
before_block_start |
true | Space before { |
spacing |
after_keywords |
true | Space after if, while, etc. |
line_breaks |
empty_lines_between_probes |
1 | Empty lines between probe blocks |
line_breaks |
empty_lines_after_shebang |
1 | Empty lines after shebang |
blocks |
brace_style |
"next_line" | "same_line", "next_line", or "gnu" |
The VS Code extension provides:
- Syntax highlighting for
.btfiles - Format on save (enable in VS Code settings)
- Format document command (
Shift+Alt+F)
| Setting | Default | Description |
|---|---|---|
btfmt.serverPath |
btfmt |
Path to btfmt binary (uses bundled binary by default) |
btfmt.configPath |
"" |
Path to .btfmt.json configuration file |
Unlicense (Public Domain)