blob: 6e3ce3bf3593acb224d6ed8d7709fe0f4d9cc52f [file] [log] [blame]
Changbin Du837e7162018-02-17 13:39:41 +08001==================================
2Using the Linux Kernel Tracepoints
3==================================
Mathieu Desnoyers24b8d832008-07-18 12:16:16 -04004
Changbin Du837e7162018-02-17 13:39:41 +08005:Author: Mathieu Desnoyers
Mathieu Desnoyers24b8d832008-07-18 12:16:16 -04006
7
Ingo Molnar0a7ad642008-11-16 08:54:36 +01008This document introduces Linux Kernel Tracepoints and their use. It
9provides examples of how to insert tracepoints in the kernel and
10connect probe functions to them and provides some examples of probe
11functions.
Mathieu Desnoyers24b8d832008-07-18 12:16:16 -040012
13
Changbin Du837e7162018-02-17 13:39:41 +080014Purpose of tracepoints
15----------------------
Ingo Molnar0a7ad642008-11-16 08:54:36 +010016A tracepoint placed in code provides a hook to call a function (probe)
17that you can provide at runtime. A tracepoint can be "on" (a probe is
18connected to it) or "off" (no probe is attached). When a tracepoint is
19"off" it has no effect, except for adding a tiny time penalty
20(checking a condition for a branch) and space penalty (adding a few
21bytes for the function call at the end of the instrumented function
22and adds a data structure in a separate section). When a tracepoint
23is "on", the function you provide is called each time the tracepoint
24is executed, in the execution context of the caller. When the function
25provided ends its execution, it returns to the caller (continuing from
26the tracepoint site).
Mathieu Desnoyers24b8d832008-07-18 12:16:16 -040027
28You can put tracepoints at important locations in the code. They are
29lightweight hooks that can pass an arbitrary number of parameters,
Ingo Molnar0a7ad642008-11-16 08:54:36 +010030which prototypes are described in a tracepoint declaration placed in a
31header file.
Mathieu Desnoyers24b8d832008-07-18 12:16:16 -040032
33They can be used for tracing and performance accounting.
34
35
Changbin Du837e7162018-02-17 13:39:41 +080036Usage
37-----
Mathieu Desnoyers24b8d832008-07-18 12:16:16 -040038Two elements are required for tracepoints :
39
40- A tracepoint definition, placed in a header file.
41- The tracepoint statement, in C code.
42
43In order to use tracepoints, you should include linux/tracepoint.h.
44
Changbin Du837e7162018-02-17 13:39:41 +080045In include/trace/events/subsys.h::
Zoltan Kissfd8176e2013-08-22 22:49:31 +010046
Changbin Du837e7162018-02-17 13:39:41 +080047 #undef TRACE_SYSTEM
48 #define TRACE_SYSTEM subsys
Zoltan Kissfd8176e2013-08-22 22:49:31 +010049
Changbin Du837e7162018-02-17 13:39:41 +080050 #if !defined(_TRACE_SUBSYS_H) || defined(TRACE_HEADER_MULTI_READ)
51 #define _TRACE_SUBSYS_H
Mathieu Desnoyers24b8d832008-07-18 12:16:16 -040052
Changbin Du837e7162018-02-17 13:39:41 +080053 #include <linux/tracepoint.h>
Mathieu Desnoyers24b8d832008-07-18 12:16:16 -040054
Changbin Du837e7162018-02-17 13:39:41 +080055 DECLARE_TRACE(subsys_eventname,
56 TP_PROTO(int firstarg, struct task_struct *p),
57 TP_ARGS(firstarg, p));
Mathieu Desnoyers24b8d832008-07-18 12:16:16 -040058
Changbin Du837e7162018-02-17 13:39:41 +080059 #endif /* _TRACE_SUBSYS_H */
Zoltan Kissfd8176e2013-08-22 22:49:31 +010060
Changbin Du837e7162018-02-17 13:39:41 +080061 /* This part must be outside protection */
62 #include <trace/define_trace.h>
Zoltan Kissfd8176e2013-08-22 22:49:31 +010063
Changbin Du837e7162018-02-17 13:39:41 +080064In subsys/file.c (where the tracing statement must be added)::
Mathieu Desnoyers24b8d832008-07-18 12:16:16 -040065
Changbin Du837e7162018-02-17 13:39:41 +080066 #include <trace/events/subsys.h>
Mathieu Desnoyers24b8d832008-07-18 12:16:16 -040067
Changbin Du837e7162018-02-17 13:39:41 +080068 #define CREATE_TRACE_POINTS
69 DEFINE_TRACE(subsys_eventname);
Mathieu Desnoyers7e066fb2008-11-14 17:47:47 -050070
Changbin Du837e7162018-02-17 13:39:41 +080071 void somefct(void)
72 {
73 ...
74 trace_subsys_eventname(arg, task);
75 ...
76 }
Mathieu Desnoyers24b8d832008-07-18 12:16:16 -040077
78Where :
Changbin Du837e7162018-02-17 13:39:41 +080079 - subsys_eventname is an identifier unique to your event
80
Mathieu Desnoyers24b8d832008-07-18 12:16:16 -040081 - subsys is the name of your subsystem.
82 - eventname is the name of the event to trace.
Mathieu Desnoyers24b8d832008-07-18 12:16:16 -040083
Changbin Du837e7162018-02-17 13:39:41 +080084 - `TP_PROTO(int firstarg, struct task_struct *p)` is the prototype of the
85 function called by this tracepoint.
Ingo Molnar0a7ad642008-11-16 08:54:36 +010086
Changbin Du837e7162018-02-17 13:39:41 +080087 - `TP_ARGS(firstarg, p)` are the parameters names, same as found in the
88 prototype.
Ingo Molnar0a7ad642008-11-16 08:54:36 +010089
Changbin Du837e7162018-02-17 13:39:41 +080090 - if you use the header in multiple source files, `#define CREATE_TRACE_POINTS`
91 should appear only in one source file.
Zoltan Kissfd8176e2013-08-22 22:49:31 +010092
Ingo Molnar0a7ad642008-11-16 08:54:36 +010093Connecting a function (probe) to a tracepoint is done by providing a
94probe (function to call) for the specific tracepoint through
Mathieu Desnoyers24b8d832008-07-18 12:16:16 -040095register_trace_subsys_eventname(). Removing a probe is done through
Mathieu Desnoyers8fd88d12008-11-14 17:47:48 -050096unregister_trace_subsys_eventname(); it will remove the probe.
Mathieu Desnoyers24b8d832008-07-18 12:16:16 -040097
Ingo Molnar0a7ad642008-11-16 08:54:36 +010098tracepoint_synchronize_unregister() must be called before the end of
99the module exit function to make sure there is no caller left using
100the probe. This, and the fact that preemption is disabled around the
101probe call, make sure that probe removal and module unload are safe.
Mathieu Desnoyers24b8d832008-07-18 12:16:16 -0400102
Ingo Molnar0a7ad642008-11-16 08:54:36 +0100103The tracepoint mechanism supports inserting multiple instances of the
104same tracepoint, but a single definition must be made of a given
105tracepoint name over all the kernel to make sure no type conflict will
106occur. Name mangling of the tracepoints is done using the prototypes
107to make sure typing is correct. Verification of probe type correctness
108is done at the registration site by the compiler. Tracepoints can be
109put in inline functions, inlined static functions, and unrolled loops
110as well as regular functions.
111
112The naming scheme "subsys_event" is suggested here as a convention
113intended to limit collisions. Tracepoint names are global to the
114kernel: they are considered as being the same whether they are in the
115core kernel image or in modules.
Mathieu Desnoyers24b8d832008-07-18 12:16:16 -0400116
Mathieu Desnoyers7e066fb2008-11-14 17:47:47 -0500117If the tracepoint has to be used in kernel modules, an
Ingo Molnar0a7ad642008-11-16 08:54:36 +0100118EXPORT_TRACEPOINT_SYMBOL_GPL() or EXPORT_TRACEPOINT_SYMBOL() can be
119used to export the defined tracepoints.
Stefan Rasplc7708642013-11-12 15:11:11 -0800120
Steven Rostedt (Red Hat)7c65bbc2014-05-06 09:26:30 -0400121If you need to do a bit of work for a tracepoint parameter, and
122that work is only used for the tracepoint, that work can be encapsulated
Changbin Du837e7162018-02-17 13:39:41 +0800123within an if statement with the following::
Steven Rostedt (Red Hat)7c65bbc2014-05-06 09:26:30 -0400124
125 if (trace_foo_bar_enabled()) {
126 int i;
127 int tot = 0;
128
129 for (i = 0; i < count; i++)
130 tot += calculate_nuggets();
131
132 trace_foo_bar(tot);
133 }
134
135All trace_<tracepoint>() calls have a matching trace_<tracepoint>_enabled()
136function defined that returns true if the tracepoint is enabled and
137false otherwise. The trace_<tracepoint>() should always be within the
138block of the if (trace_<tracepoint>_enabled()) to prevent races between
139the tracepoint being enabled and the check being seen.
140
141The advantage of using the trace_<tracepoint>_enabled() is that it uses
142the static_key of the tracepoint to allow the if statement to be implemented
143with jump labels and avoid conditional branches.
144
Changbin Du837e7162018-02-17 13:39:41 +0800145.. note:: The convenience macro TRACE_EVENT provides an alternative way to
Stefan Rasplc7708642013-11-12 15:11:11 -0800146 define tracepoints. Check http://lwn.net/Articles/379903,
147 http://lwn.net/Articles/381064 and http://lwn.net/Articles/383362
148 for a series of articles with more details.