Skip to main content

TECH VEDA

Corporate on-site training - Submit proposal Pick your modulesSharpen your kernel skills: deep dives, drivers, Yocto, CVEs, careers — updated daily. Read the blog →
Tool Spotlight

trace-cmd record: What Ran While Your Thread Waited?

trace-cmd record captures a kernel trace on the board; trace-cmd report reads it on the build host. Commands in 3.4, one workflow, the limits.

trace-cmd record: What Ran While Your Thread Waited?

A thread on your board runs late a few times a minute, and the kernel log does not say what occupied the CPU. trace-cmd record captures scheduler and interrupt events from the kernel’s Ftrace buffers into one file on the target, and trace-cmd report reads that file on the build host. The commands below are those of version 3.4.

You can get the same answer by hand from the Ftrace files under /sys/kernel/tracing, and it is worth doing that once to see how the parts fit. After that it is a list of writes to get in the right order, with a clean-up step that is easy to forget. trace-cmd record does the set-up, the capture and the clean-up in one command, and leaves a binary file that you can copy off the board.

The problem: a late thread and no record of what ran

Take a control thread with a 10 ms period that sometimes starts several milliseconds late. The application log shows the late cycle; the kernel log shows nothing. Explaining one late start needs three facts: when the thread was woken, when it was given the CPU, and what ran on that CPU in between. Three kernel events record this: sched_wakeup, sched_switch and irq_handler_entry.

The TECH VEDA tutorial on function tracing with ftrace shows the tracefs interface underneath.

What trace-cmd is

trace-cmd is a command-line front end to Ftrace. Its author is Steven Rostedt, who also tagged the 3.4 release. The repository is on git.kernel.org under utils/trace-cmd.

The current release is 3.4, tagged on 5 February 2026. The master branch had 13 later commits, the latest dated 21 October 2026. The README places the applications under GPL-2.0 and the libraries under LGPL-2.1. The build needs libtracefs and libtraceevent, the latter at 1.9 or newer.

Get it

On Debian the package is named trace-cmd. On 24 November 2026 the Debian package tracker listed 3.3.1 in stable and 3.4 in testing and unstable. This command installs software on the build host:

raghu@techveda.org:~$ sudo apt install trace-cmd

To build from source, the README lists libtracefs-dev and libtraceevent-dev as dependencies on Debian and Ubuntu, then make and make install. The Makefile accepts CROSS_COMPILE as a compiler prefix.

For Yocto, meta-openembedded carries a recipe named trace-cmd in the meta-oe layer. The recipe file found there on 24 November 2026 was trace-cmd_3.1.5.bb, so it builds 3.1.5. Buildroot has a package named trace-cmd, at 3.4 on its master branch.

trace-cmd record and the other commands worth knowing

The commands that run on the board need root.

trace-cmd list and stat: look, change nothing

root@beagleplay:~# trace-cmd list -t
root@beagleplay:~# trace-cmd list -e sched
root@beagleplay:~# trace-cmd stat

list -t prints the tracers that the running kernel offers. list -e prints the available events and takes an optional regular expression; adding -F shows the fields of each event. stat reports the active tracer, the enabled events and any filters. Run it first. By default record clears the events and triggers that are already set, and on a shared board those may be a colleague’s. The -K option, new in 3.4, keeps them.

trace-cmd record: capture into a file

This command changes the tracing state of the kernel and writes a file.

root@beagleplay:~# trace-cmd record -e sched_switch -e sched_wakeup -o /tmp/late.dat sleep 10

Two options are enough to begin with. -e enables one event or a whole subsystem and may be repeated; -o names the output file, which is trace.dat if you do not. Recording lasts as long as the command at the end of the line, or until Ctrl-C when there is none. The next one to learn is -p, which selects a tracer such as function or function_graph.

When recording ends, trace-cmd disables the tracing that it enabled, unless -k is given.

trace-cmd report: read the file

raghu@techveda.org:~$ trace-cmd report -i late.dat --cpu 2 --ts-diff

report converts the binary file to text and changes nothing. --cpu limits the output to the listed CPUs, --ts-diff prints the time difference between events, and -F filters on event fields. The file stores its endianness and the target’s long size and page size, and the manual page states that report reads big-endian files on little-endian machines and the reverse.

trace-cmd start, stop and extract: no recorder running

These commands change the tracing state; extract also writes a file.

root@beagleplay:~# trace-cmd start -e sched_switch -e sched_wakeup
root@beagleplay:~# trace-cmd stop
root@beagleplay:~# trace-cmd extract -o /tmp/after.dat

start enables tracing as trace-cmd record does, but creates no recording processes, so the events remain in the kernel ring buffer. stop ends writes to the buffer, and extract turns its contents into a file. Use this form when the fault is rare: enable tracing, wait for the failure, then stop and extract.

trace-cmd reset: switch everything off

This command discards the contents of the ring buffer.

root@beagleplay:~# trace-cmd reset

stop only ends the writing; the tracing overhead may remain. reset turns off all tracing, and the recorded data is lost.

One workflow: what delayed the control thread?

The question: how long did the thread wait after its wake-up, and what was on its CPU? First confirm that tracing is idle:

root@beagleplay:~# trace-cmd stat

Record for one minute while the application runs. This changes the tracing state and writes a file:

root@beagleplay:~# trace-cmd record -e sched_switch -e sched_wakeup -e irq_handler_entry -o /tmp/late.dat sleep 60

Copy the file to the build host and ask for wake-up latencies:

raghu@techveda.org:~$ scp root@beagleplay:/tmp/late.dat .
raghu@techveda.org:~$ trace-cmd report -i late.dat -w

With -w, and with both scheduler events in the file, report adds to a sched_switch line the time between the wake-up of the incoming task and the moment it was scheduled in. The two lines below are the example from the report manual page:

          <idle>-0     [001] dNh4. 162583.268747: sched_wakeup:         sleep:89142 [120] CPU:001
          <idle>-0     [001] d..2. 162583.268833: sched_switch:         swapper/1:0 [120] R ==> sleep:89142 [120] Latency: 85.751 usecs

Find a line for your thread with a large latency value and note its CPU and timestamp. Then list that CPU, 1 in this example, and read the lines that precede the switch, as far back as the latency value reaches:

raghu@techveda.org:~$ trace-cmd report -i late.dat --cpu 1 --ts-diff

The task names in the sched_switch lines and the interrupt names in the irq_handler_entry lines within that interval show what ran while the thread waited. To see only the switches into your thread, filter on its process ID, here the placeholder 812:

raghu@techveda.org:~$ trace-cmd report -i late.dat -F 'sched/sched_switch : next_pid == 812'

Limits

  • Kernel configuration. trace-cmd can only use what the kernel was built with. The function tracer needs CONFIG_FUNCTION_TRACER, the function graph tracer CONFIG_FUNCTION_GRAPH_TRACER, and the irqsoff and wakeup tracers CONFIG_IRQSOFF_TRACER and CONFIG_SCHED_TRACER. trace-cmd list -t shows which are present.
  • The recorder is a load. record starts one recording process per CPU. They wake every millisecond by default (-s changes the interval) and write to storage, which on a small board alters the timing being measured. On a busy system they may be starved and events lost; -r raises their priority, at the risk of changing the system’s behaviour.
  • Storage. If the board has little of it, record -N host:port sends the data to a build host that runs trace-cmd listen -p with the same port. This route does not work with the latency tracers, such as irqsoff.
  • Task names. The kernel keeps 128 command names by default, and a trace shows <...> for the rest. --cmdlines-size raises the number.
  • What it cannot tell you. It records what the kernel did, not where an application spends time in its own code; use perf for that.
  • Older versions. The meta-oe recipe builds 3.1.5 and Debian stable carries 3.3.1, so an option described here, -K for example, may be missing on your board. Read the manual page of the version you have.

Where the manual pages disagree

The reset manual page says that record leaves the tracer enabled. The record manual page says it is disabled when recording ends unless -k is given, and the code at the end of the record path in trace-record.c agrees with the record page. When you are unsure what state a board is in, trace-cmd stat settles it.

Key takeaways

  • Capture with trace-cmd record on the target; analyse with trace-cmd report on the build host.
  • Run trace-cmd stat before and trace-cmd reset after.
  • For a rare fault, use start, stop and extract.
Was this worth your time?

Frequently asked questions

Does trace-cmd record leave tracing enabled when it finishes?

No. It disables the tracing that it enabled, unless -k is given. Tracing enabled with trace-cmd start stays on until trace-cmd reset is run.

Can a trace file from a board be read on a build host of another architecture?

Yes. The file stores its endianness and the target’s long size and page size, and trace-cmd report reads big-endian files on little-endian machines and the reverse.

Which Debian package provides trace-cmd?

The package is named trace-cmd. On 24 November 2026 Debian stable carried 3.3.1; testing and unstable carried 3.4.

References

RB
Raghu Bharadwaj

Founder, TECH VEDA — 20+ years teaching the Linux kernel, device drivers and embedded systems.

Follow on LinkedIn

Get new posts by email

Kernel, embedded Linux and AI-era engineering — a few sharp reads a month. No spam.

We email occasionally and never share your address.