bpftrace — High-Level Tracing Language for Linux eBPF
A high-level tracing language for Linux that uses eBPF to instrument the kernel and user-space programs. Write powerful one-liner performance analysis scripts with an awk-like syntax that compiles to eBPF bytecode.
What it is
bpftrace is a high-level tracing language for Linux that uses eBPF to instrument the kernel and user-space programs. It provides an awk-like syntax for writing powerful one-liner performance analysis scripts that compile directly to eBPF bytecode.
System administrators, SREs, and performance engineers who need to diagnose production issues without restarting services or adding custom instrumentation will find bpftrace indispensable.
How it saves time or tokens
bpftrace replaces complex custom C-based eBPF programs with concise one-liners. What previously required writing BPF C code, compiling with clang, and loading with libbpf can now be expressed in a single command. This reduces debugging time from hours to minutes for common performance investigations.
How to use
- Install bpftrace from your distribution's package manager.
- Write a one-liner or script file targeting a kernel probe, tracepoint, or USDT probe.
- Run with root privileges to attach the eBPF program.
# Install on Ubuntu/Debian
sudo apt-get install bpftrace
# Trace all open() syscalls with the filename argument
sudo bpftrace -e 'tracepoint:syscalls:sys_enter_openat { printf("%s %s\n", comm, str(args->filename)); }'
# Histogram of read() sizes by process
sudo bpftrace -e 'tracepoint:syscalls:sys_exit_read /args->ret > 0/ { @bytes[comm] = hist(args->ret); }'
Example
Count syscalls by process name in real time:
sudo bpftrace -e 'tracepoint:raw_syscalls:sys_enter { @[comm] = count(); }'
This attaches to every syscall entry, groups by the calling process name, and prints a sorted count table when you press Ctrl-C.
Related on TokRepo
- DevOps tools — Infrastructure and operations tooling
- Monitoring tools — Observability and performance monitoring
Common pitfalls
- bpftrace requires root (or CAP_BPF) and a kernel version 4.9+ with BTF support for best results.
- Some probes generate high overhead when attached to very hot paths like scheduler functions.
- The awk-like syntax has limits; for complex multi-probe programs, consider BCC or libbpf-based tools.
Frequently Asked Questions
bpftrace works on Linux kernel 4.9 and later, but many features require 5.x kernels. For the best experience with BTF (BPF Type Format) support, use kernel 5.2 or newer.
BCC uses Python frontends with C-based BPF programs for complex tools. bpftrace is designed for ad-hoc one-liners and short scripts. BCC is better for reusable tools; bpftrace is better for quick investigations.
Yes. bpftrace supports uprobes for tracing arbitrary functions in user-space binaries and USDT probes for applications that expose static tracepoints (like Python, Ruby, MySQL, and PostgreSQL).
bpftrace programs are verified by the kernel BPF verifier before execution, which prevents crashes and infinite loops. However, attaching to hot code paths can add measurable overhead, so test on staging first.
Overhead depends on the probe type and frequency. Tracepoints on rare events add negligible overhead. Probes on high-frequency syscalls or function calls can add noticeable latency. Always scope probes with filters to minimize impact.
Citations (3)
- bpftrace GitHub— bpftrace is a high-level tracing language using eBPF
- bpftrace Reference Guide— awk-like syntax that compiles to eBPF bytecode
- eBPF Official Site— eBPF enables safe kernel-level tracing without kernel modules
Related on TokRepo
Discussion
Related Assets
Cucumber.js — BDD Testing with Plain Language Scenarios
Cucumber.js is a JavaScript implementation of Cucumber that runs automated tests written in Gherkin plain language.
WireMock — Flexible API Mocking for Java and Beyond
WireMock is an HTTP mock server for stubbing and verifying API calls in integration tests and development.
Google Benchmark — Microbenchmark Library for C++
Google Benchmark is a library for measuring and reporting the performance of C++ code with statistical rigor.