ftrace & bpftrace Tracing
Kernel function tracing with ftrace, event tracing, bpftrace one-liners for production observability without kernel modules.
Overview
Linux gives you two main ways to watch the kernel while it runs. ftrace, built into the kernel, records what the kernel did. bpftrace runs small programs inside the kernel that summarize events cheaply. Both attach to the same two kinds of hooks. Static tracepoints are compiled into the kernel with a stable ABI, in areas such as sched, block and syscalls. Dynamic probes, kprobes for kernel functions and uprobes for user-space ones, can attach to any symbol but have no stable ABI, so they can break between versions.
The ftrace Tracer
ftrace is the Linux kernel's built-in tracing framework, accessible through the tracefs virtual filesystem at /sys/kernel/debug/tracing/ (or /sys/kernel/tracing/ on modern kernels). It offers function graph and latency tracers and needs no compiler. Events go into a ring buffer in the kernel, and you read them as raw text from trace_pipe.
Key ftrace Tracers
A tracer decides what ftrace records, and you choose one by writing its name to current_tracer.
- function: traces every kernel function call, at very high overhead.
- function_graph: traces entry and exit, with call graph indentation.
- nop: disables tracing. It is the default.
- blk: records block I/O events.
The session below puts these to work: it switches to function_graph, filters to the TCP functions, streams the live output and then resets the tracer.
Basic ftrace Usage
The trace-cmd Tool (ftrace frontend)
Key concepts
- tracefs
- function_graph tracer
- kprobe vs tracepoint
- bpftrace @maps
Interview questions
- How would you trace what system calls a specific process is making in production without using strace?
Sign up free to read the full topic
More Linux topics
Start free on Cariara · Coding problems · System design studio