blob: c0e1ceed75a441b692bf447b3fcdc49d8abdd1cc [file] [log] [blame]
Mathieu Desnoyers24b8d832008-07-18 12:16:16 -04001 Using the Linux Kernel Tracepoints
2
3 Mathieu Desnoyers
4
5
Ingo Molnar0a7ad642008-11-16 08:54:36 +01006This document introduces Linux Kernel Tracepoints and their use. It
7provides examples of how to insert tracepoints in the kernel and
8connect probe functions to them and provides some examples of probe
9functions.
Mathieu Desnoyers24b8d832008-07-18 12:16:16 -040010
11
12* Purpose of tracepoints
13
Ingo Molnar0a7ad642008-11-16 08:54:36 +010014A tracepoint placed in code provides a hook to call a function (probe)
15that you can provide at runtime. A tracepoint can be "on" (a probe is
16connected to it) or "off" (no probe is attached). When a tracepoint is
17"off" it has no effect, except for adding a tiny time penalty
18(checking a condition for a branch) and space penalty (adding a few
19bytes for the function call at the end of the instrumented function
20and adds a data structure in a separate section). When a tracepoint
21is "on", the function you provide is called each time the tracepoint
22is executed, in the execution context of the caller. When the function
23provided ends its execution, it returns to the caller (continuing from
24the tracepoint site).
Mathieu Desnoyers24b8d832008-07-18 12:16:16 -040025
26You can put tracepoints at important locations in the code. They are
27lightweight hooks that can pass an arbitrary number of parameters,
Ingo Molnar0a7ad642008-11-16 08:54:36 +010028which prototypes are described in a tracepoint declaration placed in a
29header file.
Mathieu Desnoyers24b8d832008-07-18 12:16:16 -040030
31They can be used for tracing and performance accounting.
32
33
34* Usage
35
36Two elements are required for tracepoints :
37
38- A tracepoint definition, placed in a header file.
39- The tracepoint statement, in C code.
40
41In order to use tracepoints, you should include linux/tracepoint.h.
42
43In include/trace/subsys.h :
44
45#include <linux/tracepoint.h>
46
Mathieu Desnoyers7e066fb2008-11-14 17:47:47 -050047DECLARE_TRACE(subsys_eventname,
Steven Rostedt2939b042009-03-09 15:47:18 -040048 TP_PROTO(int firstarg, struct task_struct *p),
49 TP_ARGS(firstarg, p));
Mathieu Desnoyers24b8d832008-07-18 12:16:16 -040050
51In subsys/file.c (where the tracing statement must be added) :
52
53#include <trace/subsys.h>
54
Mathieu Desnoyers7e066fb2008-11-14 17:47:47 -050055DEFINE_TRACE(subsys_eventname);
56
Mathieu Desnoyers24b8d832008-07-18 12:16:16 -040057void somefct(void)
58{
59 ...
60 trace_subsys_eventname(arg, task);
61 ...
62}
63
64Where :
65- subsys_eventname is an identifier unique to your event
66 - subsys is the name of your subsystem.
67 - eventname is the name of the event to trace.
Mathieu Desnoyers24b8d832008-07-18 12:16:16 -040068
Steven Rostedt2939b042009-03-09 15:47:18 -040069- TP_PROTO(int firstarg, struct task_struct *p) is the prototype of the
Ingo Molnar0a7ad642008-11-16 08:54:36 +010070 function called by this tracepoint.
71
Steven Rostedt2939b042009-03-09 15:47:18 -040072- TP_ARGS(firstarg, p) are the parameters names, same as found in the
Ingo Molnar0a7ad642008-11-16 08:54:36 +010073 prototype.
74
75Connecting a function (probe) to a tracepoint is done by providing a
76probe (function to call) for the specific tracepoint through
Mathieu Desnoyers24b8d832008-07-18 12:16:16 -040077register_trace_subsys_eventname(). Removing a probe is done through
Mathieu Desnoyers8fd88d12008-11-14 17:47:48 -050078unregister_trace_subsys_eventname(); it will remove the probe.
Mathieu Desnoyers24b8d832008-07-18 12:16:16 -040079
Ingo Molnar0a7ad642008-11-16 08:54:36 +010080tracepoint_synchronize_unregister() must be called before the end of
81the module exit function to make sure there is no caller left using
82the probe. This, and the fact that preemption is disabled around the
83probe call, make sure that probe removal and module unload are safe.
84See the "Probe example" section below for a sample probe module.
Mathieu Desnoyers24b8d832008-07-18 12:16:16 -040085
Ingo Molnar0a7ad642008-11-16 08:54:36 +010086The tracepoint mechanism supports inserting multiple instances of the
87same tracepoint, but a single definition must be made of a given
88tracepoint name over all the kernel to make sure no type conflict will
89occur. Name mangling of the tracepoints is done using the prototypes
90to make sure typing is correct. Verification of probe type correctness
91is done at the registration site by the compiler. Tracepoints can be
92put in inline functions, inlined static functions, and unrolled loops
93as well as regular functions.
94
95The naming scheme "subsys_event" is suggested here as a convention
96intended to limit collisions. Tracepoint names are global to the
97kernel: they are considered as being the same whether they are in the
98core kernel image or in modules.
Mathieu Desnoyers24b8d832008-07-18 12:16:16 -040099
Mathieu Desnoyers7e066fb2008-11-14 17:47:47 -0500100If the tracepoint has to be used in kernel modules, an
Ingo Molnar0a7ad642008-11-16 08:54:36 +0100101EXPORT_TRACEPOINT_SYMBOL_GPL() or EXPORT_TRACEPOINT_SYMBOL() can be
102used to export the defined tracepoints.
Mathieu Desnoyers24b8d832008-07-18 12:16:16 -0400103
104* Probe / tracepoint example
105
Jody McIntyre0a5d6492009-03-24 16:00:28 -0400106See the example provided in samples/tracepoints
Mathieu Desnoyers24b8d832008-07-18 12:16:16 -0400107
Jody McIntyre0a5d6492009-03-24 16:00:28 -0400108Compile them with your kernel. They are built during 'make' (not
109'make modules') when CONFIG_SAMPLE_TRACEPOINTS=m.
Mathieu Desnoyers24b8d832008-07-18 12:16:16 -0400110
111Run, as root :
Jody McIntyre0a5d6492009-03-24 16:00:28 -0400112modprobe tracepoint-sample (insmod order is not important)
113modprobe tracepoint-probe-sample
114cat /proc/tracepoint-sample (returns an expected error)
115rmmod tracepoint-sample tracepoint-probe-sample
Mathieu Desnoyers24b8d832008-07-18 12:16:16 -0400116dmesg