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.
Agent 可直接安装
这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。
npx -y tokrepo@latest install fac5cfb3-398f-11f1-9bc6-00163e2b0d79 --target codex先 dry-run 确认安装计划,再运行此命令。
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.
常见问题
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.
引用来源 (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
讨论
相关资产
Julia — High-Performance Language for Scientific Computing
Julia is a high-level, high-performance dynamic language designed for numerical analysis, computational science, and general-purpose programming with C-like speed.
RWKV — RNN Language Model with Transformer-Level Performance
RWKV is an open source large language model architecture that combines the training parallelism of Transformers with the constant-memory inference of RNNs, achieving competitive quality with linear time complexity and no KV cache.
Triton — GPU Kernel Programming Language for Deep Learning
Triton is an open-source programming language and compiler for writing efficient GPU kernels, originally developed by OpenAI. It provides a Python-like syntax that compiles to optimized CUDA, ROCm, and other GPU backends, making custom kernel development accessible without low-level GPU expertise.
wasm-bindgen — High-Level Rust and WebAssembly Interop
wasm-bindgen facilitates communication between Rust-compiled WebAssembly modules and JavaScript. It generates binding code that allows Rust functions to be called from JS and vice versa, handling type conversions, memory management, and DOM access automatically.