0001 ==================================
0002 Using the Linux Kernel Tracepoints
0003 ==================================
0004
0005 :Author: Mathieu Desnoyers
0006
0007
0008 This document introduces Linux Kernel Tracepoints and their use. It
0009 provides examples of how to insert tracepoints in the kernel and
0010 connect probe functions to them and provides some examples of probe
0011 functions.
0012
0013
0014 Purpose of tracepoints
0015 ----------------------
0016 A tracepoint placed in code provides a hook to call a function (probe)
0017 that you can provide at runtime. A tracepoint can be "on" (a probe is
0018 connected to it) or "off" (no probe is attached). When a tracepoint is
0019 "off" it has no effect, except for adding a tiny time penalty
0020 (checking a condition for a branch) and space penalty (adding a few
0021 bytes for the function call at the end of the instrumented function
0022 and adds a data structure in a separate section). When a tracepoint
0023 is "on", the function you provide is called each time the tracepoint
0024 is executed, in the execution context of the caller. When the function
0025 provided ends its execution, it returns to the caller (continuing from
0026 the tracepoint site).
0027
0028 You can put tracepoints at important locations in the code. They are
0029 lightweight hooks that can pass an arbitrary number of parameters,
0030 which prototypes are described in a tracepoint declaration placed in a
0031 header file.
0032
0033 They can be used for tracing and performance accounting.
0034
0035
0036 Usage
0037 -----
0038 Two elements are required for tracepoints :
0039
0040 - A tracepoint definition, placed in a header file.
0041 - The tracepoint statement, in C code.
0042
0043 In order to use tracepoints, you should include linux/tracepoint.h.
0044
0045 In include/trace/events/subsys.h::
0046
0047 #undef TRACE_SYSTEM
0048 #define TRACE_SYSTEM subsys
0049
0050 #if !defined(_TRACE_SUBSYS_H) || defined(TRACE_HEADER_MULTI_READ)
0051 #define _TRACE_SUBSYS_H
0052
0053 #include <linux/tracepoint.h>
0054
0055 DECLARE_TRACE(subsys_eventname,
0056 TP_PROTO(int firstarg, struct task_struct *p),
0057 TP_ARGS(firstarg, p));
0058
0059 #endif /* _TRACE_SUBSYS_H */
0060
0061 /* This part must be outside protection */
0062 #include <trace/define_trace.h>
0063
0064 In subsys/file.c (where the tracing statement must be added)::
0065
0066 #include <trace/events/subsys.h>
0067
0068 #define CREATE_TRACE_POINTS
0069 DEFINE_TRACE(subsys_eventname);
0070
0071 void somefct(void)
0072 {
0073 ...
0074 trace_subsys_eventname(arg, task);
0075 ...
0076 }
0077
0078 Where :
0079 - subsys_eventname is an identifier unique to your event
0080
0081 - subsys is the name of your subsystem.
0082 - eventname is the name of the event to trace.
0083
0084 - `TP_PROTO(int firstarg, struct task_struct *p)` is the prototype of the
0085 function called by this tracepoint.
0086
0087 - `TP_ARGS(firstarg, p)` are the parameters names, same as found in the
0088 prototype.
0089
0090 - if you use the header in multiple source files, `#define CREATE_TRACE_POINTS`
0091 should appear only in one source file.
0092
0093 Connecting a function (probe) to a tracepoint is done by providing a
0094 probe (function to call) for the specific tracepoint through
0095 register_trace_subsys_eventname(). Removing a probe is done through
0096 unregister_trace_subsys_eventname(); it will remove the probe.
0097
0098 tracepoint_synchronize_unregister() must be called before the end of
0099 the module exit function to make sure there is no caller left using
0100 the probe. This, and the fact that preemption is disabled around the
0101 probe call, make sure that probe removal and module unload are safe.
0102
0103 The tracepoint mechanism supports inserting multiple instances of the
0104 same tracepoint, but a single definition must be made of a given
0105 tracepoint name over all the kernel to make sure no type conflict will
0106 occur. Name mangling of the tracepoints is done using the prototypes
0107 to make sure typing is correct. Verification of probe type correctness
0108 is done at the registration site by the compiler. Tracepoints can be
0109 put in inline functions, inlined static functions, and unrolled loops
0110 as well as regular functions.
0111
0112 The naming scheme "subsys_event" is suggested here as a convention
0113 intended to limit collisions. Tracepoint names are global to the
0114 kernel: they are considered as being the same whether they are in the
0115 core kernel image or in modules.
0116
0117 If the tracepoint has to be used in kernel modules, an
0118 EXPORT_TRACEPOINT_SYMBOL_GPL() or EXPORT_TRACEPOINT_SYMBOL() can be
0119 used to export the defined tracepoints.
0120
0121 If you need to do a bit of work for a tracepoint parameter, and
0122 that work is only used for the tracepoint, that work can be encapsulated
0123 within an if statement with the following::
0124
0125 if (trace_foo_bar_enabled()) {
0126 int i;
0127 int tot = 0;
0128
0129 for (i = 0; i < count; i++)
0130 tot += calculate_nuggets();
0131
0132 trace_foo_bar(tot);
0133 }
0134
0135 All trace_<tracepoint>() calls have a matching trace_<tracepoint>_enabled()
0136 function defined that returns true if the tracepoint is enabled and
0137 false otherwise. The trace_<tracepoint>() should always be within the
0138 block of the if (trace_<tracepoint>_enabled()) to prevent races between
0139 the tracepoint being enabled and the check being seen.
0140
0141 The advantage of using the trace_<tracepoint>_enabled() is that it uses
0142 the static_key of the tracepoint to allow the if statement to be implemented
0143 with jump labels and avoid conditional branches.
0144
0145 .. note:: The convenience macro TRACE_EVENT provides an alternative way to
0146 define tracepoints. Check http://lwn.net/Articles/379903,
0147 http://lwn.net/Articles/381064 and http://lwn.net/Articles/383362
0148 for a series of articles with more details.
0149
0150 If you require calling a tracepoint from a header file, it is not
0151 recommended to call one directly or to use the trace_<tracepoint>_enabled()
0152 function call, as tracepoints in header files can have side effects if a
0153 header is included from a file that has CREATE_TRACE_POINTS set, as
0154 well as the trace_<tracepoint>() is not that small of an inline
0155 and can bloat the kernel if used by other inlined functions. Instead,
0156 include tracepoint-defs.h and use tracepoint_enabled().
0157
0158 In a C file::
0159
0160 void do_trace_foo_bar_wrapper(args)
0161 {
0162 trace_foo_bar(args);
0163 }
0164
0165 In the header file::
0166
0167 DECLARE_TRACEPOINT(foo_bar);
0168
0169 static inline void some_inline_function()
0170 {
0171 [..]
0172 if (tracepoint_enabled(foo_bar))
0173 do_trace_foo_bar_wrapper(args);
0174 [..]
0175 }