tracepoints.txt 4.3 KB
Newer Older
1 2 3 4 5
	             Using the Linux Kernel Tracepoints

			    Mathieu Desnoyers


I
Ingo Molnar 已提交
6 7 8 9
This document introduces Linux Kernel Tracepoints and their use. It
provides examples of how to insert tracepoints in the kernel and
connect probe functions to them and provides some examples of probe
functions.
10 11 12 13


* Purpose of tracepoints

I
Ingo Molnar 已提交
14 15 16 17 18 19 20 21 22 23 24
A tracepoint placed in code provides a hook to call a function (probe)
that you can provide at runtime. A tracepoint can be "on" (a probe is
connected to it) or "off" (no probe is attached). When a tracepoint is
"off" it has no effect, except for adding a tiny time penalty
(checking a condition for a branch) and space penalty (adding a few
bytes for the function call at the end of the instrumented function
and adds a data structure in a separate section).  When a tracepoint
is "on", the function you provide is called each time the tracepoint
is executed, in the execution context of the caller. When the function
provided ends its execution, it returns to the caller (continuing from
the tracepoint site).
25 26 27

You can put tracepoints at important locations in the code. They are
lightweight hooks that can pass an arbitrary number of parameters,
I
Ingo Molnar 已提交
28 29
which prototypes are described in a tracepoint declaration placed in a
header file.
30 31 32 33 34 35 36 37 38 39 40 41 42

They can be used for tracing and performance accounting.


* Usage

Two elements are required for tracepoints :

- A tracepoint definition, placed in a header file.
- The tracepoint statement, in C code.

In order to use tracepoints, you should include linux/tracepoint.h.

43 44 45 46 47 48 49
In include/trace/events/subsys.h :

#undef TRACE_SYSTEM
#define TRACE_SYSTEM subsys

#if !defined(_TRACE_SUBSYS_H) || defined(TRACE_HEADER_MULTI_READ)
#define _TRACE_SUBSYS_H
50 51 52

#include <linux/tracepoint.h>

53
DECLARE_TRACE(subsys_eventname,
54 55
	TP_PROTO(int firstarg, struct task_struct *p),
	TP_ARGS(firstarg, p));
56

57 58 59 60 61
#endif /* _TRACE_SUBSYS_H */

/* This part must be outside protection */
#include <trace/define_trace.h>

62 63
In subsys/file.c (where the tracing statement must be added) :

64
#include <trace/events/subsys.h>
65

66
#define CREATE_TRACE_POINTS
67 68
DEFINE_TRACE(subsys_eventname);

69 70 71 72 73 74 75 76 77 78 79 80
void somefct(void)
{
	...
	trace_subsys_eventname(arg, task);
	...
}

Where :
- subsys_eventname is an identifier unique to your event
    - subsys is the name of your subsystem.
    - eventname is the name of the event to trace.

81
- TP_PROTO(int firstarg, struct task_struct *p) is the prototype of the
I
Ingo Molnar 已提交
82 83
  function called by this tracepoint.

84
- TP_ARGS(firstarg, p) are the parameters names, same as found in the
I
Ingo Molnar 已提交
85 86
  prototype.

87 88 89
- if you use the header in multiple source files, #define CREATE_TRACE_POINTS
  should appear only in one source file.

I
Ingo Molnar 已提交
90 91
Connecting a function (probe) to a tracepoint is done by providing a
probe (function to call) for the specific tracepoint through
92
register_trace_subsys_eventname().  Removing a probe is done through
93
unregister_trace_subsys_eventname(); it will remove the probe.
I
Ingo Molnar 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112

tracepoint_synchronize_unregister() must be called before the end of
the module exit function to make sure there is no caller left using
the probe. This, and the fact that preemption is disabled around the
probe call, make sure that probe removal and module unload are safe.

The tracepoint mechanism supports inserting multiple instances of the
same tracepoint, but a single definition must be made of a given
tracepoint name over all the kernel to make sure no type conflict will
occur. Name mangling of the tracepoints is done using the prototypes
to make sure typing is correct. Verification of probe type correctness
is done at the registration site by the compiler. Tracepoints can be
put in inline functions, inlined static functions, and unrolled loops
as well as regular functions.

The naming scheme "subsys_event" is suggested here as a convention
intended to limit collisions. Tracepoint names are global to the
kernel: they are considered as being the same whether they are in the
core kernel image or in modules.
113

114
If the tracepoint has to be used in kernel modules, an
I
Ingo Molnar 已提交
115 116
EXPORT_TRACEPOINT_SYMBOL_GPL() or EXPORT_TRACEPOINT_SYMBOL() can be
used to export the defined tracepoints.
117 118 119 120 121

Note: The convenience macro TRACE_EVENT provides an alternative way to
      define tracepoints. Check http://lwn.net/Articles/379903,
      http://lwn.net/Articles/381064 and http://lwn.net/Articles/383362
      for a series of articles with more details.