Fundamentals 40 min read

Understanding and Using Ftrace for Linux Kernel Tracing

This article provides a comprehensive guide to Linux's ftrace tool, explaining its purpose, various tracers, how to set up and use it via debugfs, detailed command examples, implementation details, practical use cases for performance tuning and debugging, and a comparison with other tracing utilities.

Deepin Linux
Deepin Linux
Deepin Linux
Understanding and Using Ftrace for Linux Kernel Tracing

In the vast and complex world of the Linux kernel, developers often need a powerful tool to illuminate the inner workings of system processes, scheduling, and interrupts. Ftrace serves as this "kernel tracing magic wand," evolving from a simple function tracer into a full-featured tracing framework.

1. What is Ftrace?

According to the Linux documentation, ftrace is an internal tracing tool that helps developers and system designers understand what the kernel is doing at any moment, facilitating performance analysis and debugging. It acts like a Swiss army knife for kernel developers, allowing precise code‑path identification and performance bottleneck analysis.

2. How to Use Ftrace

First, mount debugfs or tracefs (most distributions enable it by default). Example for older kernels:

mkdir /debug
mount -t debugfs nodev /debug

Navigate to /debug/tracing (or /sys/kernel/tracing ) and configure tracing via control files:

Set the tracer: echo function > current_tracer or echo function_graph > current_tracer .

Enable tracing: echo 1 > tracing_on .

Filter functions: echo schedule > set_ftrace_filter or exclude with echo '!schedule' > set_ftrace_filter .

View output: cat trace | head -n 15 .

Various tracers (function, function_graph, irqsoff, sched_switch, etc.) can be selected, and events can be enabled by writing 1 to the corresponding enable file under events/ .

3. Ftrace Working Principle

Ftrace uses both static and dynamic instrumentation. With CONFIG_FUNCTION_TRACER , the compiler inserts a call _mcount at each function entry. At boot, these calls are replaced by nop instructions (dynamic ftrace). When tracing is enabled, the nop is swapped back to a jump to ftrace_caller , which records the call stack into a ring buffer.

The ring buffer stores trace records in a circular fashion, exposing them via debugfs files such as trace , trace_pipe , and latency_trace . Users can read, filter, and analyze the data without recompiling the kernel.

4. Practical Applications

Ftrace is invaluable for performance optimization (e.g., analyzing file‑system read/write latency), fault isolation (tracking long interrupt‑off periods), and kernel development (verifying function call sequences). Tools like trace‑cmd , funcgraph , funccount , and kprobe build on ftrace to provide richer analysis and visualization.

5. Comparison with Other Tracing Tools

Compared to SystemTap and LTTng, ftrace is lightweight, built‑in, and requires only simple file operations, making it easy to adopt for quick kernel investigations. SystemTap offers more scripting flexibility but incurs higher overhead, while LTTng provides low‑impact, high‑volume event tracing for both kernel and user space.

Overall, ftrace illuminates the opaque kernel internals, enabling developers to diagnose, tune, and understand Linux systems effectively.

debuggingLinux KerneltracingPerformance analysisSystem Tracingftrace
Deepin Linux
Written by

Deepin Linux

Research areas: Windows & Linux platforms, C/C++ backend development, embedded systems and Linux kernel, etc.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.