Notes about trace-cmd.

1. Motivation

ftrace isn’t a program exactly – you don’t just run ftrace my_cool_function.

If you read Debugging the kernel using Ftrace it starts out by telling you to cd /sys/kernel/debug/tracing and then do various filesystem manipulations.

For me this is way too annoying – a simple example of using ftrace this way is something like this

This filesystem interface to the tracing system (“put values in these magic files and things will happen”) seems theoretically possible to use but really not my preference.

Luckily, team ftrace also thought this interface wasn’t that user friendly and so there is an easier-to-use interface called trace-cmd!!! trace-cmd is a normal program with command line arguments. We’ll use that! I found an intro to trace-cmd on LWN at trace-cmd: A front-end for Ftrace.

2. Installation and manual

1
sudo apt-get install trace-cmd -y
1
2
3
trace-cmd record --help
man trace-cmd-record
man trace-cmd-report

3. Trace functions

How to know what functions you can trace?

If you run sudo trace-cmd list -f you’ll get a list of all the functions you can trace.

1
2
sudo trace-cmd record -p function -l __do_fault
sudo trace-cmd report

4. Trace a process

1
2
sudo trace-cmd record -p function -P 25314 # record for PID 25314
sudo trace-cmd report

5. Trace function graph

1
2
sudo trace-cmd record -p function_graph -P 25314
sudo trace-cmd report

6. Trace events

1
2
sudo trace-cmd record -e sched:sched_switch
sudo trace-cmd report

7. MISC

  • trace-cmd report -F filter

参考资料:

  1. ftrace: trace your kernel functions!
  2. trace-cmd: A front-end for Ftrace