Skip to main content
eBPF Tools for Everyday Ops: bpftrace Patterns We Use

eBPF Tools for Everyday Ops: bpftrace Patterns We Use

KU
· Founder & Chief Editor
• 8 min read•Updated •

bpftrace one-liners replace strace, perf top, and a half-dozen ad-hoc debugging scripts. The patterns that actually earn their place when you're troubleshooting at 2 AM.

Key takeaways

  • bpftrace runs small eBPF programs that count and filter kernel events in-kernel, so you can watch syscalls, file opens, TCP retransmits and latency histograms on a live host with far less overhead than strace.
  • It needs root or CAP_BPF plus CAP_PERFMON and a BTF-enabled kernel.
  • Use it for ad-hoc investigation, not long-term metrics.

Use bpftrace when you need to see what a live process is doing without slowing it down: one-liners like bpftrace -e 'tracepoint:raw_syscalls:sys_enter /pid == 1234/ { @[probe] = count(); }' count, filter and histogram kernel events in-kernel. It replaces most everyday strace use, needs root or CAP_BPF, and belongs to investigation, not monitoring.

Verified against

bpftrace 0.27.1 (released 2026-10-24), against the bpftrace one-liner tutorial, the bpftrace stdlib reference and release notes, and the Linux capabilities(7) man page. Checked October 2026.

The biggest behavior change since we started running bpftrace in production isn't "we now use eBPF." It's that we stopped reaching for strace for half the things we used to use it for. bpftrace one-liners do the same job, usually with much less impact on the target process, and you can leave them running for hours instead of minutes. These are the patterns we actually run, with the reason each one earns its keep.

Why bpftrace beats strace for everyday cases

strace works by attaching ptrace to the process and stopping it on every syscall. That gives you complete visibility at a high cost:

  • The target can slow down by an order of magnitude or more on syscall-heavy workloads.
  • You can't leave it attached to a production process for long.
  • It's single-process; you can't easily watch a fleet.

bpftrace attaches eBPF programs to kernel tracepoints and kprobes. The kernel filters and aggregates events before they reach userspace, so you can watch every syscall on every process and only the matching results get streamed out. On a filtered probe the overhead is usually small enough to run on a busy box; an unfiltered probe on a hot path is not free, so filter first.

The trade: bpftrace doesn't pretty-print every syscall's arguments out of the box; you write the formatting yourself for non-trivial syscalls. But for most "what is this process actually doing" debugging, bpftrace is the right tool.

The patterns we keep coming back to

All of these use the args.field syntax, which bpftrace 0.25 made the preferred form over args->field. Older bpftrace builds accept -> instead.

1. Which syscalls is a process making, and how often?

Bash
sudo bpftrace -e 'tracepoint:syscalls:sys_enter_* /pid == 12345/ { @[probe] = count(); }'

Press Ctrl-C and bpftrace prints a count per syscall tracepoint. It answers the same question as strace -c -p 12345 at a fraction of the cost. Useful when an app is doing something unexpected: you immediately see "it's doing tens of thousands of read calls a second."

2. Show every file a process opens

Bash
sudo bpftrace -e 'tracepoint:syscalls:sys_enter_openat /comm == "myservice"/ { printf("%s: %s\n", comm, str(args.filename)); }'

Filter by process name (comm). Useful for "what config files does this thing actually read at startup?" or "why is it reading from /tmp constantly?"

3. Trace TCP retransmits in real time

Bash
sudo bpftrace -e 'tracepoint:tcp:tcp_retransmit_skb { @[ntop(args.saddr), ntop(args.daddr)] = count(); }'

Builds a count of (source, destination) pairs that are retransmitting. It catches network trouble that doesn't show up in latency dashboards until much later, and points at which connections are affected. For the wider network toolkit around this, see tcpdump, ss and eBPF for Linux network debugging.

4. Slow file reads

Bash
sudo bpftrace -e 'kprobe:vfs_read { @start[tid] = nsecs; }
  kretprobe:vfs_read /@start[tid]/ { @usecs = hist((nsecs - @start[tid]) / 1000); delete(@start, tid); }'

Histogram of vfs_read latency in microseconds, including page-cache hits, so it describes what the application experiences rather than raw disk time. Press Ctrl-C to dump it. A bimodal shape (most reads under 50µs, a tail out in the tens of milliseconds) usually means cache misses landing on a slow or contended device.

5. New processes being spawned

Bash
sudo bpftrace -e 'tracepoint:syscalls:sys_enter_execve { printf("%-8d %s\n", pid, str(args.filename)); }'

A one-line execsnoop. Useful for "what's actually being run on this host?": it catches surprise cron jobs, init scripts, and debug processes someone left running.

6. Count outbound TCP connects by destination

Bash
sudo bpftrace -e 'kprobe:tcp_connect { @[ntop(((struct sock *)arg0)->__sk_common.skc_daddr)] = count(); }'

Counts outbound TCP connects by destination IPv4 address. Useful for "this service is talking to places it shouldn't." It's the core of bcc's tcpconnect without the bcc dependency.

7. Function latency via uprobes

Bash
sudo bpftrace -e 'uprobe:/usr/local/bin/myservice:handle_request { @start[tid] = nsecs; }
  uretprobe:/usr/local/bin/myservice:handle_request /@start[tid]/ { @usecs = hist((nsecs - @start[tid]) / 1000); delete(@start, tid); }'

Histogram of latency for one function in a native binary with symbols. It answers "is the slow endpoint slow inside the handler or somewhere downstream?" without a redeploy. Don't use uretprobe on Go binaries: the Go runtime moves goroutine stacks, and return probes can crash the process. Use entry probes only there, or the app's own tracing. For a full latency investigation built on these probes, see debugging latency with eBPF and bpftrace.

8. Off-CPU time (when is a thread blocked, not running?)

Bash
sudo bpftrace -e 'tracepoint:sched:sched_switch {
  @start[args.prev_pid] = nsecs;
  if (has_key(@start, args.next_pid)) {
    @offcpu_us[args.next_comm] = hist((nsecs - @start[args.next_pid]) / 1000);
    delete(@start, args.next_pid);
  }
}'

For a process you suspect is blocked on something (lock, IO, sleep). On each context switch it timestamps the thread leaving the CPU and, when a thread comes back, records how long it was away. The classic "100% CPU" investigation has a counterpart, "low CPU but nothing's happening," and off-CPU time is the tool for it.

What we don't use bpftrace for

Long-term metrics. Prometheus and standard exporters handle metrics that need history over hours. bpftrace is for ad-hoc investigation, not continuous collection.

Distributed tracing. OpenTelemetry for traces that span services. eBPF can do some of this (Pixie does), but in-app instrumentation gives you trace context propagation that eBPF can't easily reconstruct.

App logging. Logs are logs. Don't try to trace them with bpftrace.

Production scripts you'd commit. If you find yourself running the same one-liner daily, formalize it as an exporter, an alert, or a dedicated tool.

Operational caveats

The kernel matters more than the distro. bpftrace 0.25 made tracepoint args depend on BTF, and 0.24 requires ring buffer maps, so you want a reasonably current kernel with BTF type info. Check with ls /sys/kernel/btf/vmlinux; if the file is missing, many of the one-liners above won't compile.

You need root or the right capabilities. Most of these run as root. Since Linux 5.8, CAP_BPF and CAP_PERFMON split those privileges out of CAP_SYS_ADMIN, so hardened systems can grant them explicitly.

Containers. bpftrace runs against the host kernel. For Kubernetes, run it on the node (or in a privileged debug pod on that node), not inside a workload container.

Output volume. A trace that fires on every syscall can be very chatty. Filter aggressively by pid, comm, or specific args, or you'll flood your own terminal.

Questions people ask

Is bpftrace safe to run in production?

Generally yes, with care. The kernel verifier checks every eBPF program before it loads, so a bad one-liner is rejected at load time rather than crashing the kernel. The real risks are overhead from unfiltered probes on hot paths and uretprobe on Go binaries, which can crash the target. Filter by pid or comm, run for minutes rather than days, and prefer tracepoints over kprobes when both exist.

What is the difference between bpftrace and bcc?

Both build eBPF programs, at different altitudes. bcc is a toolkit and library: you get packaged tools like execsnoop and tcpconnect, and you write new ones in Python or C. bpftrace is a high-level tracing language for one-liners and short scripts, closer to awk for kernel events. We reach for bpftrace for ad-hoc questions and bcc's prebuilt tools when one already answers it.

Does bpftrace need root?

Effectively yes. Loading tracing programs requires privileges, and the bpftrace man page notes that processes it spawns run with its own privileges, usually root. On Linux 5.8 and later you can grant CAP_BPF and CAP_PERFMON instead of full root, though kprobes and some helpers may still need more. Inside containers, it needs those capabilities on the host kernel.

bpftrace doesn't replace your observability stack. It replaces the half-dozen tools you reach for when you're staring at a process and asking "what are you actually doing right now." The patterns above are the ones we keep using; everything else is a variation on them.

Sources

Explore topics:Linux
React
Share this post
Cite this article
Plain text

Kiril Urbonas (2026). eBPF Tools for Everyday Ops: bpftrace Patterns We Use. DevOpsNess. Updated 2026-10-31. https://www.devopsness.com/blog/ebpf-tools-for-everyday-ops-bpftrace-patterns

Markdown

[eBPF Tools for Everyday Ops: bpftrace Patterns We Use](https://www.devopsness.com/blog/ebpf-tools-for-everyday-ops-bpftrace-patterns) (Kiril Urbonas, DevOpsNess, updated 2026-10-31)

KU

Kiril Urbonas

Founder & Chief Editor

607 articles
View all articles by Kiril Urbonas

You might have missed

Evergreen posts worth revisiting.