eBPF and bpftrace

Advanced · 17 min

What eBPF is, why its verifier makes in-kernel tracing safe for production, and how to use bcc tools like execsnoop and bpftrace one-liners.

Before thisTracing system calls with straceProfiling with perf and flame graphsDisk and I/O bottlenecks

eBPF & Cilium Community · 17 min

eBPF and bpftrace

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by eBPF & Cilium CommunityReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • eBPF runs small, sandboxed programs inside the kernel when an event fires, such as a syscall, a kernel function, a tracepoint or a packet, without changing kernel code or loading a module.
  • A verifier checks every program before it loads: it must finish, and it must not crash or harm the kernel. Then it is JIT-compiled to native code, which is why eBPF tools are cheap enough for production.
  • Programs summarise in the kernel using maps, such as counts or histograms, and send only the summary to user space. That is the big difference from strace, which stops the process on every call.
  • bcc ships ready-made tools: `execsnoop` for new processes, `opensnoop` for file opens, `biolatency` for a disk latency histogram, and many more. Ubuntu names them `execsnoop-bpfcc` and so on.
  • bpftrace is a small language for one-liners: `probe /filter/ { action }`. For example, `@[comm] = count()` on `tracepoint:raw_syscalls:sys_enter` counts syscalls by process name.

Questions the old tools cannot answer cheaply

Some questions are simple to ask and awkward to answer. Which processes started in the last minute, including ones that lived for 50 milliseconds? What is the distribution of disk latency, not just the average? Which files is this service opening, on a busy production box where strace would slow it down too much?

Before eBPF, the answers needed either a kernel module, which is risky to load on production, or a tracer that copied every event to user space, which is slow. eBPF changed that.

What eBPF is

ebpf.io describes eBPF as technology that can “run sandboxed programs in a privileged context”, meaning in the kernel, extending it “without changing kernel source code or loading kernel modules”. You write a small program and attach it to a hook: a system call, a kernel tracepoint, the entry or exit of any kernel function (a kprobe), a function in a user program (a uprobe), or a network event. When the event fires, your program runs.

Two pieces make that safe. The verifier checks the program before it loads. It must run to completion, so no unbounded loops, and must not crash or harm the system. If the verifier cannot prove that, the program is rejected. Then a JIT compiler turns the bytecode into native machine code. Think of a building that lets visiting contractors install equipment only after an inspector has checked the plans. The inspection is strict, but once passed, the work runs at full speed.

The third piece is maps, kernel data structures that programs update and user space can read. Instead of sending every event out, an eBPF program can increment a counter or a histogram bucket in a map, and the tool reads the summary once a second. That is where the low overhead comes from. Loading programs requires privilege, usually root or CAP_BPF with related capabilities, from the capabilities lesson.

eBPF also powers networking and security tools, including Kubernetes network plugins and runtime security agents. This lesson stays with observability.

bcc: ready-made tools

The bcc project ships dozens of finished tools. Its install notes say Ubuntu’s bpfcc-tools package installs them with a -bpfcc suffix, and the upstream packages for Fedora and RHEL put them under /usr/share/bcc/tools/. A few to know:

sudo execsnoop-bpfcc        # every new process, with arguments and parent
sudo opensnoop-bpfcc -p 1234   # files opened, and the result
sudo biolatency-bpfcc 10 1  # one 10-second histogram of block I/O latency
sudo tcplife-bpfcc          # TCP sessions with duration and bytes
sudo runqlat-bpfcc          # how long tasks wait for a CPU

execsnoop is the classic first win: it catches short-lived processes that top never shows, such as a misbehaving cron job that forks thousands of times. biolatency answers the question iostat from the disk lesson cannot. An average of 4 ms can hide a tail where 1% of I/Os take 200 ms, and the histogram shows it. runqlat measures CPU saturation directly, which is what the load average lesson said load average only hints at.

bpftrace: one-liners

When no ready-made tool fits, bpftrace lets you write one. Its syntax is probe /filter/ { action }. From the bpftrace one-liner tutorial:

sudo bpftrace -l 'tracepoint:syscalls:sys_enter_*'
sudo bpftrace -e 'tracepoint:syscalls:sys_enter_openat { printf("%s %s\n", comm, str(args.filename)); }'
sudo bpftrace -e 'tracepoint:raw_syscalls:sys_enter { @[comm] = count(); }'
sudo bpftrace -e 'tracepoint:syscalls:sys_exit_read /pid == 1234/ { @bytes = hist(args.ret); }'

The first lists available probes. The second prints every file opened, by process name. The third counts system calls per process name in a map, @, and prints the totals when you press Ctrl-C. The fourth draws a histogram of read sizes for one process. The tutorial notes that bpftrace 0.19 changed probe arguments from args->filename to args.filename, so older guides use the arrow form.

Brendan Gregg’s books and the bcc and bpftrace repositories contain many more tools and one-liners. Read a tool’s source before running it on a critical box. They are small, and seeing which probe a tool uses tells you what it can and cannot see.

Nothing here was run, because eBPF needs a Linux kernel. Commands come from the bpftrace tutorial and the bcc install notes, and the description of the verifier from ebpf.io, all read 2026-11-28.

About the video

The embed is all of “Getting Started with BPF observability – Brendan Gregg, Netflix”, a 17-minute keynote published by the eBPF & Cilium Community channel in August 2021. Gregg wrote many of the bcc and bpftrace tools, and the talk is pitched at getting started. It is a few years old, so the bpftrace argument syntax changed afterwards, as noted above. I judged it from the title and speaker, since the upload has no description or chapters, and English from its English caption track. Hack the Clown’s six-minute 2026 video in deeper demonstrates current tools for TCP lifetimes, syscalls, disk I/O and DNS latency.

Where this fits

You now have tools for a running system that is slow. The last lesson is for a system that will not run at all: recovering a system that will not boot.

Check yourself

  1. 01A server's load spikes every few minutes, but `top` never shows anything unusual. Which bcc tool might catch the cause, and why?

    `execsnoop`, because it prints every new process as it starts. Short-lived processes, such as a cron job or a health check that forks many children, finish between `top` refreshes and never appear there.

  2. 02Why is a bpftrace histogram of read sizes far cheaper than getting the same data from strace?

    The eBPF program updates the histogram in a kernel map and never stops the process. strace pauses the process twice per syscall and copies each one out to user space.

  3. 03What does the eBPF verifier protect against?

    Programs that could loop forever, read invalid memory or otherwise crash or harm the kernel. If it cannot prove a program is safe, it refuses to load it.

  4. 04`iostat` says average disk latency is 4 ms, but users report occasional very slow requests. Which tool helps, and what does it show that iostat cannot?

    `biolatency`, which prints a histogram of block I/O latency. An average hides a long tail, and the histogram shows if, say, 1% of I/Os take 200 ms.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark