]>
Commit | Line | Data |
---|---|---|
97e1c18e MD |
1 | #ifndef _LINUX_TRACEPOINT_H |
2 | #define _LINUX_TRACEPOINT_H | |
3 | ||
4 | /* | |
5 | * Kernel Tracepoint API. | |
6 | * | |
7 | * See Documentation/tracepoint.txt. | |
8 | * | |
9 | * (C) Copyright 2008 Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca> | |
10 | * | |
11 | * Heavily inspired from the Linux Kernel Markers. | |
12 | * | |
13 | * This file is released under the GPLv2. | |
14 | * See the file COPYING for more details. | |
15 | */ | |
16 | ||
17 | #include <linux/types.h> | |
18 | #include <linux/rcupdate.h> | |
19 | ||
20 | struct module; | |
21 | struct tracepoint; | |
22 | ||
23 | struct tracepoint { | |
24 | const char *name; /* Tracepoint name */ | |
25 | int state; /* State. */ | |
26 | void **funcs; | |
7e066fb8 MD |
27 | } __attribute__((aligned(32))); /* |
28 | * Aligned on 32 bytes because it is | |
29 | * globally visible and gcc happily | |
30 | * align these on the structure size. | |
31 | * Keep in sync with vmlinux.lds.h. | |
32 | */ | |
97e1c18e | 33 | |
ea20d929 SR |
34 | #ifndef DECLARE_TRACE |
35 | ||
2939b046 SR |
36 | #define TP_PROTO(args...) args |
37 | #define TP_ARGS(args...) args | |
97e1c18e MD |
38 | |
39 | #ifdef CONFIG_TRACEPOINTS | |
40 | ||
41 | /* | |
42 | * it_func[0] is never NULL because there is at least one element in the array | |
43 | * when the array itself is non NULL. | |
44 | */ | |
45 | #define __DO_TRACE(tp, proto, args) \ | |
46 | do { \ | |
47 | void **it_func; \ | |
48 | \ | |
da7b3eab | 49 | rcu_read_lock_sched_notrace(); \ |
97e1c18e MD |
50 | it_func = rcu_dereference((tp)->funcs); \ |
51 | if (it_func) { \ | |
52 | do { \ | |
53 | ((void(*)(proto))(*it_func))(args); \ | |
54 | } while (*(++it_func)); \ | |
55 | } \ | |
da7b3eab | 56 | rcu_read_unlock_sched_notrace(); \ |
97e1c18e MD |
57 | } while (0) |
58 | ||
59 | /* | |
60 | * Make sure the alignment of the structure in the __tracepoints section will | |
61 | * not add unwanted padding between the beginning of the section and the | |
62 | * structure. Force alignment to the same alignment as the section start. | |
63fbdab3 JB |
63 | * An optional set of (un)registration functions can be passed to perform any |
64 | * additional (un)registration work. | |
97e1c18e | 65 | */ |
63fbdab3 | 66 | #define DECLARE_TRACE_WITH_CALLBACK(name, proto, args, reg, unreg) \ |
7e066fb8 | 67 | extern struct tracepoint __tracepoint_##name; \ |
97e1c18e MD |
68 | static inline void trace_##name(proto) \ |
69 | { \ | |
97e1c18e MD |
70 | if (unlikely(__tracepoint_##name.state)) \ |
71 | __DO_TRACE(&__tracepoint_##name, \ | |
2939b046 | 72 | TP_PROTO(proto), TP_ARGS(args)); \ |
97e1c18e MD |
73 | } \ |
74 | static inline int register_trace_##name(void (*probe)(proto)) \ | |
75 | { \ | |
63fbdab3 JB |
76 | int ret; \ |
77 | void (*func)(void) = reg; \ | |
78 | \ | |
79 | ret = tracepoint_probe_register(#name, (void *)probe); \ | |
80 | if (func && !ret) \ | |
81 | func(); \ | |
82 | return ret; \ | |
97e1c18e | 83 | } \ |
c420970e | 84 | static inline int unregister_trace_##name(void (*probe)(proto)) \ |
97e1c18e | 85 | { \ |
63fbdab3 JB |
86 | int ret; \ |
87 | void (*func)(void) = unreg; \ | |
88 | \ | |
89 | ret = tracepoint_probe_unregister(#name, (void *)probe);\ | |
90 | if (func && !ret) \ | |
91 | func(); \ | |
92 | return ret; \ | |
97e1c18e MD |
93 | } |
94 | ||
63fbdab3 JB |
95 | |
96 | #define DECLARE_TRACE(name, proto, args) \ | |
97 | DECLARE_TRACE_WITH_CALLBACK(name, TP_PROTO(proto), TP_ARGS(args),\ | |
98 | NULL, NULL); | |
99 | ||
7e066fb8 MD |
100 | #define DEFINE_TRACE(name) \ |
101 | static const char __tpstrtab_##name[] \ | |
102 | __attribute__((section("__tracepoints_strings"))) = #name; \ | |
103 | struct tracepoint __tracepoint_##name \ | |
104 | __attribute__((section("__tracepoints"), aligned(32))) = \ | |
105 | { __tpstrtab_##name, 0, NULL } | |
106 | ||
107 | #define EXPORT_TRACEPOINT_SYMBOL_GPL(name) \ | |
108 | EXPORT_SYMBOL_GPL(__tracepoint_##name) | |
109 | #define EXPORT_TRACEPOINT_SYMBOL(name) \ | |
110 | EXPORT_SYMBOL(__tracepoint_##name) | |
111 | ||
97e1c18e MD |
112 | extern void tracepoint_update_probe_range(struct tracepoint *begin, |
113 | struct tracepoint *end); | |
114 | ||
115 | #else /* !CONFIG_TRACEPOINTS */ | |
63fbdab3 | 116 | #define DECLARE_TRACE_WITH_CALLBACK(name, proto, args, reg, unreg) \ |
97e1c18e MD |
117 | static inline void _do_trace_##name(struct tracepoint *tp, proto) \ |
118 | { } \ | |
119 | static inline void trace_##name(proto) \ | |
120 | { } \ | |
121 | static inline int register_trace_##name(void (*probe)(proto)) \ | |
122 | { \ | |
123 | return -ENOSYS; \ | |
124 | } \ | |
c420970e MD |
125 | static inline int unregister_trace_##name(void (*probe)(proto)) \ |
126 | { \ | |
127 | return -ENOSYS; \ | |
128 | } | |
97e1c18e | 129 | |
63fbdab3 JB |
130 | #define DECLARE_TRACE(name, proto, args) \ |
131 | DECLARE_TRACE_WITH_CALLBACK(name, TP_PROTO(proto), TP_ARGS(args),\ | |
132 | NULL, NULL); | |
133 | ||
7e066fb8 MD |
134 | #define DEFINE_TRACE(name) |
135 | #define EXPORT_TRACEPOINT_SYMBOL_GPL(name) | |
136 | #define EXPORT_TRACEPOINT_SYMBOL(name) | |
137 | ||
97e1c18e MD |
138 | static inline void tracepoint_update_probe_range(struct tracepoint *begin, |
139 | struct tracepoint *end) | |
140 | { } | |
141 | #endif /* CONFIG_TRACEPOINTS */ | |
ea20d929 | 142 | #endif /* DECLARE_TRACE */ |
97e1c18e MD |
143 | |
144 | /* | |
145 | * Connect a probe to a tracepoint. | |
146 | * Internal API, should not be used directly. | |
147 | */ | |
148 | extern int tracepoint_probe_register(const char *name, void *probe); | |
149 | ||
150 | /* | |
151 | * Disconnect a probe from a tracepoint. | |
152 | * Internal API, should not be used directly. | |
153 | */ | |
154 | extern int tracepoint_probe_unregister(const char *name, void *probe); | |
155 | ||
127cafbb LJ |
156 | extern int tracepoint_probe_register_noupdate(const char *name, void *probe); |
157 | extern int tracepoint_probe_unregister_noupdate(const char *name, void *probe); | |
158 | extern void tracepoint_probe_update_all(void); | |
159 | ||
97e1c18e MD |
160 | struct tracepoint_iter { |
161 | struct module *module; | |
162 | struct tracepoint *tracepoint; | |
163 | }; | |
164 | ||
165 | extern void tracepoint_iter_start(struct tracepoint_iter *iter); | |
166 | extern void tracepoint_iter_next(struct tracepoint_iter *iter); | |
167 | extern void tracepoint_iter_stop(struct tracepoint_iter *iter); | |
168 | extern void tracepoint_iter_reset(struct tracepoint_iter *iter); | |
169 | extern int tracepoint_get_iter_range(struct tracepoint **tracepoint, | |
170 | struct tracepoint *begin, struct tracepoint *end); | |
171 | ||
f2461fc8 MD |
172 | /* |
173 | * tracepoint_synchronize_unregister must be called between the last tracepoint | |
174 | * probe unregistration and the end of module exit to make sure there is no | |
175 | * caller executing a probe when it is freed. | |
176 | */ | |
231375cc MD |
177 | static inline void tracepoint_synchronize_unregister(void) |
178 | { | |
179 | synchronize_sched(); | |
180 | } | |
f2461fc8 | 181 | |
3cdfdf91 | 182 | #define PARAMS(args...) args |
ea20d929 | 183 | |
ea20d929 | 184 | #ifndef TRACE_EVENT |
823f9124 SR |
185 | /* |
186 | * For use with the TRACE_EVENT macro: | |
187 | * | |
188 | * We define a tracepoint, its arguments, its printk format | |
189 | * and its 'fast binay record' layout. | |
190 | * | |
191 | * Firstly, name your tracepoint via TRACE_EVENT(name : the | |
192 | * 'subsystem_event' notation is fine. | |
193 | * | |
194 | * Think about this whole construct as the | |
195 | * 'trace_sched_switch() function' from now on. | |
196 | * | |
197 | * | |
198 | * TRACE_EVENT(sched_switch, | |
199 | * | |
200 | * * | |
201 | * * A function has a regular function arguments | |
202 | * * prototype, declare it via TP_PROTO(): | |
203 | * * | |
204 | * | |
ef18012b SR |
205 | * TP_PROTO(struct rq *rq, struct task_struct *prev, |
206 | * struct task_struct *next), | |
823f9124 SR |
207 | * |
208 | * * | |
209 | * * Define the call signature of the 'function'. | |
210 | * * (Design sidenote: we use this instead of a | |
211 | * * TP_PROTO1/TP_PROTO2/TP_PROTO3 ugliness.) | |
212 | * * | |
213 | * | |
ef18012b | 214 | * TP_ARGS(rq, prev, next), |
823f9124 SR |
215 | * |
216 | * * | |
217 | * * Fast binary tracing: define the trace record via | |
218 | * * TP_STRUCT__entry(). You can think about it like a | |
219 | * * regular C structure local variable definition. | |
220 | * * | |
221 | * * This is how the trace record is structured and will | |
222 | * * be saved into the ring buffer. These are the fields | |
223 | * * that will be exposed to user-space in | |
156f5a78 | 224 | * * /sys/kernel/debug/tracing/events/<*>/format. |
823f9124 SR |
225 | * * |
226 | * * The declared 'local variable' is called '__entry' | |
227 | * * | |
228 | * * __field(pid_t, prev_prid) is equivalent to a standard declariton: | |
229 | * * | |
230 | * * pid_t prev_pid; | |
231 | * * | |
232 | * * __array(char, prev_comm, TASK_COMM_LEN) is equivalent to: | |
233 | * * | |
234 | * * char prev_comm[TASK_COMM_LEN]; | |
235 | * * | |
236 | * | |
237 | * TP_STRUCT__entry( | |
238 | * __array( char, prev_comm, TASK_COMM_LEN ) | |
239 | * __field( pid_t, prev_pid ) | |
240 | * __field( int, prev_prio ) | |
241 | * __array( char, next_comm, TASK_COMM_LEN ) | |
242 | * __field( pid_t, next_pid ) | |
243 | * __field( int, next_prio ) | |
244 | * ), | |
245 | * | |
246 | * * | |
247 | * * Assign the entry into the trace record, by embedding | |
248 | * * a full C statement block into TP_fast_assign(). You | |
249 | * * can refer to the trace record as '__entry' - | |
250 | * * otherwise you can put arbitrary C code in here. | |
251 | * * | |
252 | * * Note: this C code will execute every time a trace event | |
253 | * * happens, on an active tracepoint. | |
254 | * * | |
255 | * | |
ef18012b SR |
256 | * TP_fast_assign( |
257 | * memcpy(__entry->next_comm, next->comm, TASK_COMM_LEN); | |
258 | * __entry->prev_pid = prev->pid; | |
259 | * __entry->prev_prio = prev->prio; | |
823f9124 SR |
260 | * memcpy(__entry->prev_comm, prev->comm, TASK_COMM_LEN); |
261 | * __entry->next_pid = next->pid; | |
ef18012b | 262 | * __entry->next_prio = next->prio; |
823f9124 SR |
263 | * ) |
264 | * | |
265 | * * | |
266 | * * Formatted output of a trace record via TP_printk(). | |
267 | * * This is how the tracepoint will appear under ftrace | |
268 | * * plugins that make use of this tracepoint. | |
269 | * * | |
270 | * * (raw-binary tracing wont actually perform this step.) | |
271 | * * | |
272 | * | |
273 | * TP_printk("task %s:%d [%d] ==> %s:%d [%d]", | |
274 | * __entry->prev_comm, __entry->prev_pid, __entry->prev_prio, | |
275 | * __entry->next_comm, __entry->next_pid, __entry->next_prio), | |
276 | * | |
277 | * ); | |
278 | * | |
279 | * This macro construct is thus used for the regular printk format | |
280 | * tracing setup, it is used to construct a function pointer based | |
281 | * tracepoint callback (this is used by programmatic plugins and | |
282 | * can also by used by generic instrumentation like SystemTap), and | |
283 | * it is also used to expose a structured trace record in | |
156f5a78 | 284 | * /sys/kernel/debug/tracing/events/. |
823f9124 SR |
285 | */ |
286 | ||
30a8fecc | 287 | #define TRACE_EVENT(name, proto, args, struct, assign, print) \ |
da4d0302 | 288 | DECLARE_TRACE(name, PARAMS(proto), PARAMS(args)) |
ea20d929 | 289 | #endif |
da4d0302 | 290 | |
97e1c18e | 291 | #endif |