ConfigsApr 16, 2026·3 min read

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.

TL;DR
bpftrace lets you write one-liner eBPF scripts to instrument the Linux kernel and user-space programs.
§01

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.

§02

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.

§03

How to use

  1. Install bpftrace from your distribution's package manager.
  2. Write a one-liner or script file targeting a kernel probe, tracepoint, or USDT probe.
  3. 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); }'
§04

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.

§05

Related on TokRepo

§06

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

What Linux kernel version does bpftrace require?+

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.

How does bpftrace compare to BCC?+

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.

Can bpftrace trace user-space applications?+

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).

Is bpftrace safe to use in production?+

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.

What is the performance overhead of bpftrace?+

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)

Discussion

Sign in to join the discussion.
No comments yet. Be the first to share your thoughts.

Related Assets