blob: 93cd90e89cc1c63b21a7d5871c2c19205254917a [file] [log] [blame]
Mathieu Desnoyers24b8d832008-07-18 12:16:16 -04001 Using the Linux Kernel Tracepoints
2
3 Mathieu Desnoyers
4
5
6This document introduces Linux Kernel Tracepoints and their use. It provides
7examples of how to insert tracepoints in the kernel and connect probe functions
8to them and provides some examples of probe functions.
9
10
11* Purpose of tracepoints
12
13A tracepoint placed in code provides a hook to call a function (probe) that you
14can provide at runtime. A tracepoint can be "on" (a probe is connected to it) or
15"off" (no probe is attached). When a tracepoint is "off" it has no effect,
16except for adding a tiny time penalty (checking a condition for a branch) and
17space penalty (adding a few bytes for the function call at the end of the
18instrumented function and adds a data structure in a separate section). When a
19tracepoint is "on", the function you provide is called each time the tracepoint
20is executed, in the execution context of the caller. When the function provided
21ends its execution, it returns to the caller (continuing from the tracepoint
22site).
23
24You can put tracepoints at important locations in the code. They are
25lightweight hooks that can pass an arbitrary number of parameters,
26which prototypes are described in a tracepoint declaration placed in a header
27file.
28
29They can be used for tracing and performance accounting.
30
31
32* Usage
33
34Two elements are required for tracepoints :
35
36- A tracepoint definition, placed in a header file.
37- The tracepoint statement, in C code.
38
39In order to use tracepoints, you should include linux/tracepoint.h.
40
41In include/trace/subsys.h :
42
43#include <linux/tracepoint.h>
44
Mathieu Desnoyers7e066fb2008-11-14 17:47:47 -050045DECLARE_TRACE(subsys_eventname,
Mathieu Desnoyers24b8d832008-07-18 12:16:16 -040046 TPPTOTO(int firstarg, struct task_struct *p),
47 TPARGS(firstarg, p));
48
49In subsys/file.c (where the tracing statement must be added) :
50
51#include <trace/subsys.h>
52
Mathieu Desnoyers7e066fb2008-11-14 17:47:47 -050053DEFINE_TRACE(subsys_eventname);
54
Mathieu Desnoyers24b8d832008-07-18 12:16:16 -040055void somefct(void)
56{
57 ...
58 trace_subsys_eventname(arg, task);
59 ...
60}
61
62Where :
63- subsys_eventname is an identifier unique to your event
64 - subsys is the name of your subsystem.
65 - eventname is the name of the event to trace.
66- TPPTOTO(int firstarg, struct task_struct *p) is the prototype of the function
67 called by this tracepoint.
68- TPARGS(firstarg, p) are the parameters names, same as found in the prototype.
69
70Connecting a function (probe) to a tracepoint is done by providing a probe
71(function to call) for the specific tracepoint through
72register_trace_subsys_eventname(). Removing a probe is done through
Mathieu Desnoyers8fd88d12008-11-14 17:47:48 -050073unregister_trace_subsys_eventname(); it will remove the probe.
74marker_synchronize_unregister() must be called before the end of the module exit
75function to make sure there is no caller left using the probe. This, and the
76fact that preemption is disabled around the probe call, make sure that probe
77removal and module unload are safe. See the "Probe example" section below for a
78sample probe module.
Mathieu Desnoyers24b8d832008-07-18 12:16:16 -040079
80The tracepoint mechanism supports inserting multiple instances of the same
81tracepoint, but a single definition must be made of a given tracepoint name over
82all the kernel to make sure no type conflict will occur. Name mangling of the
83tracepoints is done using the prototypes to make sure typing is correct.
84Verification of probe type correctness is done at the registration site by the
85compiler. Tracepoints can be put in inline functions, inlined static functions,
86and unrolled loops as well as regular functions.
87
88The naming scheme "subsys_event" is suggested here as a convention intended
89to limit collisions. Tracepoint names are global to the kernel: they are
90considered as being the same whether they are in the core kernel image or in
91modules.
92
Mathieu Desnoyers7e066fb2008-11-14 17:47:47 -050093If the tracepoint has to be used in kernel modules, an
94EXPORT_TRACEPOINT_SYMBOL_GPL() or EXPORT_TRACEPOINT_SYMBOL() can be used to
95export the defined tracepoints.
Mathieu Desnoyers24b8d832008-07-18 12:16:16 -040096
97* Probe / tracepoint example
98
99See the example provided in samples/tracepoints/src
100
101Compile them with your kernel.
102
103Run, as root :
104modprobe tracepoint-example (insmod order is not important)
105modprobe tracepoint-probe-example
106cat /proc/tracepoint-example (returns an expected error)
107rmmod tracepoint-example tracepoint-probe-example
108dmesg