blob: a3efac621c5ad59e50167345cd4ce48538fc6ddf [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
Zoltan Kissfd8176e2013-08-22 22:49:31 +010043In include/trace/events/subsys.h :
44
45#undef TRACE_SYSTEM
46#define TRACE_SYSTEM subsys
47
48#if !defined(_TRACE_SUBSYS_H) || defined(TRACE_HEADER_MULTI_READ)
49#define _TRACE_SUBSYS_H
Mathieu Desnoyers24b8d832008-07-18 12:16:16 -040050
51#include <linux/tracepoint.h>
52
Mathieu Desnoyers7e066fb2008-11-14 17:47:47 -050053DECLARE_TRACE(subsys_eventname,
Steven Rostedt2939b042009-03-09 15:47:18 -040054 TP_PROTO(int firstarg, struct task_struct *p),
55 TP_ARGS(firstarg, p));
Mathieu Desnoyers24b8d832008-07-18 12:16:16 -040056
Zoltan Kissfd8176e2013-08-22 22:49:31 +010057#endif /* _TRACE_SUBSYS_H */
58
59/* This part must be outside protection */
60#include <trace/define_trace.h>
61
Mathieu Desnoyers24b8d832008-07-18 12:16:16 -040062In subsys/file.c (where the tracing statement must be added) :
63
Zoltan Kissfd8176e2013-08-22 22:49:31 +010064#include <trace/events/subsys.h>
Mathieu Desnoyers24b8d832008-07-18 12:16:16 -040065
Zoltan Kissfd8176e2013-08-22 22:49:31 +010066#define CREATE_TRACE_POINTS
Mathieu Desnoyers7e066fb2008-11-14 17:47:47 -050067DEFINE_TRACE(subsys_eventname);
68
Mathieu Desnoyers24b8d832008-07-18 12:16:16 -040069void somefct(void)
70{
71 ...
72 trace_subsys_eventname(arg, task);
73 ...
74}
75
76Where :
77- subsys_eventname is an identifier unique to your event
78 - subsys is the name of your subsystem.
79 - eventname is the name of the event to trace.
Mathieu Desnoyers24b8d832008-07-18 12:16:16 -040080
Steven Rostedt2939b042009-03-09 15:47:18 -040081- TP_PROTO(int firstarg, struct task_struct *p) is the prototype of the
Ingo Molnar0a7ad642008-11-16 08:54:36 +010082 function called by this tracepoint.
83
Steven Rostedt2939b042009-03-09 15:47:18 -040084- TP_ARGS(firstarg, p) are the parameters names, same as found in the
Ingo Molnar0a7ad642008-11-16 08:54:36 +010085 prototype.
86
Zoltan Kissfd8176e2013-08-22 22:49:31 +010087- if you use the header in multiple source files, #define CREATE_TRACE_POINTS
88 should appear only in one source file.
89
Ingo Molnar0a7ad642008-11-16 08:54:36 +010090Connecting a function (probe) to a tracepoint is done by providing a
91probe (function to call) for the specific tracepoint through
Mathieu Desnoyers24b8d832008-07-18 12:16:16 -040092register_trace_subsys_eventname(). Removing a probe is done through
Mathieu Desnoyers8fd88d12008-11-14 17:47:48 -050093unregister_trace_subsys_eventname(); it will remove the probe.
Mathieu Desnoyers24b8d832008-07-18 12:16:16 -040094
Ingo Molnar0a7ad642008-11-16 08:54:36 +010095tracepoint_synchronize_unregister() must be called before the end of
96the module exit function to make sure there is no caller left using
97the probe. This, and the fact that preemption is disabled around the
98probe call, make sure that probe removal and module unload are safe.
Mathieu Desnoyers24b8d832008-07-18 12:16:16 -040099
Ingo Molnar0a7ad642008-11-16 08:54:36 +0100100The tracepoint mechanism supports inserting multiple instances of the
101same tracepoint, but a single definition must be made of a given
102tracepoint name over all the kernel to make sure no type conflict will
103occur. Name mangling of the tracepoints is done using the prototypes
104to make sure typing is correct. Verification of probe type correctness
105is done at the registration site by the compiler. Tracepoints can be
106put in inline functions, inlined static functions, and unrolled loops
107as well as regular functions.
108
109The naming scheme "subsys_event" is suggested here as a convention
110intended to limit collisions. Tracepoint names are global to the
111kernel: they are considered as being the same whether they are in the
112core kernel image or in modules.
Mathieu Desnoyers24b8d832008-07-18 12:16:16 -0400113
Mathieu Desnoyers7e066fb2008-11-14 17:47:47 -0500114If the tracepoint has to be used in kernel modules, an
Ingo Molnar0a7ad642008-11-16 08:54:36 +0100115EXPORT_TRACEPOINT_SYMBOL_GPL() or EXPORT_TRACEPOINT_SYMBOL() can be
116used to export the defined tracepoints.
Stefan Rasplc7708642013-11-12 15:11:11 -0800117
Steven Rostedt (Red Hat)7c65bbc2014-05-06 09:26:30 -0400118If you need to do a bit of work for a tracepoint parameter, and
119that work is only used for the tracepoint, that work can be encapsulated
120within an if statement with the following:
121
122 if (trace_foo_bar_enabled()) {
123 int i;
124 int tot = 0;
125
126 for (i = 0; i < count; i++)
127 tot += calculate_nuggets();
128
129 trace_foo_bar(tot);
130 }
131
132All trace_<tracepoint>() calls have a matching trace_<tracepoint>_enabled()
133function defined that returns true if the tracepoint is enabled and
134false otherwise. The trace_<tracepoint>() should always be within the
135block of the if (trace_<tracepoint>_enabled()) to prevent races between
136the tracepoint being enabled and the check being seen.
137
138The advantage of using the trace_<tracepoint>_enabled() is that it uses
139the static_key of the tracepoint to allow the if statement to be implemented
140with jump labels and avoid conditional branches.
141
Stefan Rasplc7708642013-11-12 15:11:11 -0800142Note: The convenience macro TRACE_EVENT provides an alternative way to
143 define tracepoints. Check http://lwn.net/Articles/379903,
144 http://lwn.net/Articles/381064 and http://lwn.net/Articles/383362
145 for a series of articles with more details.