Mathieu Desnoyers | 24b8d83 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 1 | Using the Linux Kernel Tracepoints |
| 2 | |
| 3 | Mathieu Desnoyers |
| 4 | |
| 5 | |
Ingo Molnar | 0a7ad64 | 2008-11-16 08:54:36 +0100 | [diff] [blame] | 6 | This document introduces Linux Kernel Tracepoints and their use. It |
| 7 | provides examples of how to insert tracepoints in the kernel and |
| 8 | connect probe functions to them and provides some examples of probe |
| 9 | functions. |
Mathieu Desnoyers | 24b8d83 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 10 | |
| 11 | |
| 12 | * Purpose of tracepoints |
| 13 | |
Ingo Molnar | 0a7ad64 | 2008-11-16 08:54:36 +0100 | [diff] [blame] | 14 | A tracepoint placed in code provides a hook to call a function (probe) |
| 15 | that you can provide at runtime. A tracepoint can be "on" (a probe is |
| 16 | connected 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 |
| 19 | bytes for the function call at the end of the instrumented function |
| 20 | and adds a data structure in a separate section). When a tracepoint |
| 21 | is "on", the function you provide is called each time the tracepoint |
| 22 | is executed, in the execution context of the caller. When the function |
| 23 | provided ends its execution, it returns to the caller (continuing from |
| 24 | the tracepoint site). |
Mathieu Desnoyers | 24b8d83 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 25 | |
| 26 | You can put tracepoints at important locations in the code. They are |
| 27 | lightweight hooks that can pass an arbitrary number of parameters, |
Ingo Molnar | 0a7ad64 | 2008-11-16 08:54:36 +0100 | [diff] [blame] | 28 | which prototypes are described in a tracepoint declaration placed in a |
| 29 | header file. |
Mathieu Desnoyers | 24b8d83 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 30 | |
| 31 | They can be used for tracing and performance accounting. |
| 32 | |
| 33 | |
| 34 | * Usage |
| 35 | |
| 36 | Two elements are required for tracepoints : |
| 37 | |
| 38 | - A tracepoint definition, placed in a header file. |
| 39 | - The tracepoint statement, in C code. |
| 40 | |
| 41 | In order to use tracepoints, you should include linux/tracepoint.h. |
| 42 | |
Zoltan Kiss | fd8176e | 2013-08-22 22:49:31 +0100 | [diff] [blame] | 43 | In 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 Desnoyers | 24b8d83 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 50 | |
| 51 | #include <linux/tracepoint.h> |
| 52 | |
Mathieu Desnoyers | 7e066fb | 2008-11-14 17:47:47 -0500 | [diff] [blame] | 53 | DECLARE_TRACE(subsys_eventname, |
Steven Rostedt | 2939b04 | 2009-03-09 15:47:18 -0400 | [diff] [blame] | 54 | TP_PROTO(int firstarg, struct task_struct *p), |
| 55 | TP_ARGS(firstarg, p)); |
Mathieu Desnoyers | 24b8d83 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 56 | |
Zoltan Kiss | fd8176e | 2013-08-22 22:49:31 +0100 | [diff] [blame] | 57 | #endif /* _TRACE_SUBSYS_H */ |
| 58 | |
| 59 | /* This part must be outside protection */ |
| 60 | #include <trace/define_trace.h> |
| 61 | |
Mathieu Desnoyers | 24b8d83 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 62 | In subsys/file.c (where the tracing statement must be added) : |
| 63 | |
Zoltan Kiss | fd8176e | 2013-08-22 22:49:31 +0100 | [diff] [blame] | 64 | #include <trace/events/subsys.h> |
Mathieu Desnoyers | 24b8d83 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 65 | |
Zoltan Kiss | fd8176e | 2013-08-22 22:49:31 +0100 | [diff] [blame] | 66 | #define CREATE_TRACE_POINTS |
Mathieu Desnoyers | 7e066fb | 2008-11-14 17:47:47 -0500 | [diff] [blame] | 67 | DEFINE_TRACE(subsys_eventname); |
| 68 | |
Mathieu Desnoyers | 24b8d83 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 69 | void somefct(void) |
| 70 | { |
| 71 | ... |
| 72 | trace_subsys_eventname(arg, task); |
| 73 | ... |
| 74 | } |
| 75 | |
| 76 | Where : |
| 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 Desnoyers | 24b8d83 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 80 | |
Steven Rostedt | 2939b04 | 2009-03-09 15:47:18 -0400 | [diff] [blame] | 81 | - TP_PROTO(int firstarg, struct task_struct *p) is the prototype of the |
Ingo Molnar | 0a7ad64 | 2008-11-16 08:54:36 +0100 | [diff] [blame] | 82 | function called by this tracepoint. |
| 83 | |
Steven Rostedt | 2939b04 | 2009-03-09 15:47:18 -0400 | [diff] [blame] | 84 | - TP_ARGS(firstarg, p) are the parameters names, same as found in the |
Ingo Molnar | 0a7ad64 | 2008-11-16 08:54:36 +0100 | [diff] [blame] | 85 | prototype. |
| 86 | |
Zoltan Kiss | fd8176e | 2013-08-22 22:49:31 +0100 | [diff] [blame] | 87 | - if you use the header in multiple source files, #define CREATE_TRACE_POINTS |
| 88 | should appear only in one source file. |
| 89 | |
Ingo Molnar | 0a7ad64 | 2008-11-16 08:54:36 +0100 | [diff] [blame] | 90 | Connecting a function (probe) to a tracepoint is done by providing a |
| 91 | probe (function to call) for the specific tracepoint through |
Mathieu Desnoyers | 24b8d83 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 92 | register_trace_subsys_eventname(). Removing a probe is done through |
Mathieu Desnoyers | 8fd88d1 | 2008-11-14 17:47:48 -0500 | [diff] [blame] | 93 | unregister_trace_subsys_eventname(); it will remove the probe. |
Mathieu Desnoyers | 24b8d83 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 94 | |
Ingo Molnar | 0a7ad64 | 2008-11-16 08:54:36 +0100 | [diff] [blame] | 95 | tracepoint_synchronize_unregister() must be called before the end of |
| 96 | the module exit function to make sure there is no caller left using |
| 97 | the probe. This, and the fact that preemption is disabled around the |
| 98 | probe call, make sure that probe removal and module unload are safe. |
Mathieu Desnoyers | 24b8d83 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 99 | |
Ingo Molnar | 0a7ad64 | 2008-11-16 08:54:36 +0100 | [diff] [blame] | 100 | The tracepoint mechanism supports inserting multiple instances of the |
| 101 | same tracepoint, but a single definition must be made of a given |
| 102 | tracepoint name over all the kernel to make sure no type conflict will |
| 103 | occur. Name mangling of the tracepoints is done using the prototypes |
| 104 | to make sure typing is correct. Verification of probe type correctness |
| 105 | is done at the registration site by the compiler. Tracepoints can be |
| 106 | put in inline functions, inlined static functions, and unrolled loops |
| 107 | as well as regular functions. |
| 108 | |
| 109 | The naming scheme "subsys_event" is suggested here as a convention |
| 110 | intended to limit collisions. Tracepoint names are global to the |
| 111 | kernel: they are considered as being the same whether they are in the |
| 112 | core kernel image or in modules. |
Mathieu Desnoyers | 24b8d83 | 2008-07-18 12:16:16 -0400 | [diff] [blame] | 113 | |
Mathieu Desnoyers | 7e066fb | 2008-11-14 17:47:47 -0500 | [diff] [blame] | 114 | If the tracepoint has to be used in kernel modules, an |
Ingo Molnar | 0a7ad64 | 2008-11-16 08:54:36 +0100 | [diff] [blame] | 115 | EXPORT_TRACEPOINT_SYMBOL_GPL() or EXPORT_TRACEPOINT_SYMBOL() can be |
| 116 | used to export the defined tracepoints. |