Commit | Line | Data |
---|---|---|
27b052e3 PMF |
1 | #ifndef _UST_TRACEPOINT_H |
2 | #define _UST_TRACEPOINT_H | |
f99be407 PMF |
3 | |
4 | /* | |
1f8b0dff PMF |
5 | * Copyright (C) 2008 Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca> |
6 | * Copyright (C) 2009 Pierre-Marc Fournier | |
22d72948 | 7 | * Copyright (C) 2009 Steven Rostedt <rostedt@goodmis.org> |
f99be407 | 8 | * |
8fc2d8db PMF |
9 | * This library is free software; you can redistribute it and/or |
10 | * modify it under the terms of the GNU Lesser General Public | |
11 | * License as published by the Free Software Foundation; either | |
12 | * version 2.1 of the License, or (at your option) any later version. | |
f99be407 | 13 | * |
8fc2d8db | 14 | * This library is distributed in the hope that it will be useful, |
1f8b0dff | 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
8fc2d8db PMF |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
17 | * Lesser General Public License for more details. | |
1f8b0dff | 18 | * |
8fc2d8db PMF |
19 | * You should have received a copy of the GNU Lesser General Public |
20 | * License along with this library; if not, write to the Free Software | |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
f99be407 PMF |
22 | * |
23 | * Heavily inspired from the Linux Kernel Markers. | |
24 | * | |
1f8b0dff | 25 | * Ported to userspace by Pierre-Marc Fournier. |
f99be407 PMF |
26 | */ |
27 | ||
872037bb | 28 | #define _LGPL_SOURCE |
b7ea1a1c | 29 | #include <urcu-bp.h> |
518d7abb | 30 | #include <ust/core.h> |
f99be407 | 31 | |
f99be407 PMF |
32 | struct tracepoint; |
33 | ||
9dec086e NC |
34 | struct probe { |
35 | void *func; | |
36 | void *data; | |
37 | }; | |
38 | ||
f99be407 PMF |
39 | struct tracepoint { |
40 | const char *name; /* Tracepoint name */ | |
f36c12ab | 41 | char state; /* State. */ |
9dec086e | 42 | struct probe *probes; |
f218ff28 | 43 | }; |
f99be407 | 44 | |
22d72948 NC |
45 | #define PARAMS(args...) args |
46 | ||
7166e240 PMF |
47 | #define TP_PROTO(args...) args |
48 | #define TP_ARGS(args...) args | |
f99be407 | 49 | |
cc7b66ba MD |
50 | /* |
51 | * Tracepoints should be added to the instrumented code using the | |
52 | * "tracepoint()" macro. | |
53 | */ | |
54 | #define tracepoint(name, args...) __trace_##name(args) | |
55 | ||
56 | #define register_tracepoint(name, probe, data) \ | |
57 | __register_trace_##name(probe, data) | |
58 | ||
59 | #define unregister_tracepoint(name, probe, data) \ | |
60 | __unregister_trace_##name(probe, data) | |
61 | ||
872037bb PMF |
62 | #define CONFIG_TRACEPOINTS |
63 | #ifdef CONFIG_TRACEPOINTS | |
f99be407 PMF |
64 | |
65 | /* | |
66 | * it_func[0] is never NULL because there is at least one element in the array | |
67 | * when the array itself is non NULL. | |
68 | */ | |
69 | #define __DO_TRACE(tp, proto, args) \ | |
70 | do { \ | |
9dec086e NC |
71 | struct probe *it_probe_ptr; \ |
72 | void *it_func; \ | |
73 | void *__data; \ | |
f99be407 | 74 | \ |
9dec086e NC |
75 | rcu_read_lock(); \ |
76 | it_probe_ptr = rcu_dereference((tp)->probes); \ | |
77 | if (it_probe_ptr) { \ | |
f99be407 | 78 | do { \ |
9dec086e NC |
79 | it_func = (it_probe_ptr)->func; \ |
80 | __data = (it_probe_ptr)->data; \ | |
81 | ((void(*)(proto))(it_func))(args); \ | |
82 | } while ((++it_probe_ptr)->func); \ | |
f99be407 | 83 | } \ |
9dec086e | 84 | rcu_read_unlock(); \ |
f99be407 PMF |
85 | } while (0) |
86 | ||
f36c12ab | 87 | #define __CHECK_TRACE(name, proto, args) \ |
f99be407 | 88 | do { \ |
f36c12ab MD |
89 | if (unlikely(__tracepoint_##name.state)) \ |
90 | __DO_TRACE(&__tracepoint_##name, \ | |
91 | TP_PROTO(proto), TP_ARGS(args)); \ | |
f99be407 PMF |
92 | } while (0) |
93 | ||
94 | /* | |
95 | * Make sure the alignment of the structure in the __tracepoints section will | |
96 | * not add unwanted padding between the beginning of the section and the | |
97 | * structure. Force alignment to the same alignment as the section start. | |
f99be407 | 98 | */ |
9dec086e | 99 | #define __DECLARE_TRACE(name, proto, args, data_proto, data_args) \ |
f99be407 | 100 | extern struct tracepoint __tracepoint_##name; \ |
cc7b66ba | 101 | static inline void __trace_##name(proto) \ |
f99be407 | 102 | { \ |
f36c12ab | 103 | __CHECK_TRACE(name, TP_PROTO(data_proto), \ |
9dec086e | 104 | TP_ARGS(data_args)); \ |
f99be407 | 105 | } \ |
9dec086e | 106 | static inline int \ |
cc7b66ba | 107 | __register_trace_##name(void (*probe)(data_proto), void *data) \ |
f99be407 | 108 | { \ |
9dec086e NC |
109 | return tracepoint_probe_register(#name, (void *)probe, \ |
110 | data); \ | |
111 | \ | |
f99be407 | 112 | } \ |
9dec086e | 113 | static inline int \ |
cc7b66ba | 114 | __unregister_trace_##name(void (*probe)(data_proto), void *data)\ |
f99be407 | 115 | { \ |
9dec086e NC |
116 | return tracepoint_probe_unregister(#name, (void *)probe, \ |
117 | data); \ | |
f99be407 PMF |
118 | } |
119 | ||
f218ff28 | 120 | #define DEFINE_TRACE_FN(name, reg, unreg) \ |
f99be407 PMF |
121 | static const char __tpstrtab_##name[] \ |
122 | __attribute__((section("__tracepoints_strings"))) = #name; \ | |
123 | struct tracepoint __tracepoint_##name \ | |
f218ff28 MD |
124 | __attribute__((section("__tracepoints"))) = \ |
125 | { __tpstrtab_##name, 0, NULL }; \ | |
126 | static struct tracepoint * const __tracepoint_ptr_##name \ | |
127 | __attribute__((used, section("__tracepoints_ptrs"))) = \ | |
128 | &__tracepoint_##name; | |
f99be407 | 129 | |
9dec086e | 130 | #define DEFINE_TRACE(name) \ |
f218ff28 | 131 | DEFINE_TRACE_FN(name, NULL, NULL) |
9dec086e | 132 | |
f218ff28 MD |
133 | extern void tracepoint_update_probe_range(struct tracepoint * const *begin, |
134 | struct tracepoint * const *end); | |
f99be407 | 135 | |
872037bb | 136 | #else /* !CONFIG_TRACEPOINTS */ |
9dec086e | 137 | #define __DECLARE_TRACE(name, proto, args) \ |
872037bb PMF |
138 | static inline void trace_##name(proto) \ |
139 | { } \ | |
140 | static inline void _trace_##name(proto) \ | |
141 | { } \ | |
cc7b66ba | 142 | static inline int __register_trace_##name(void (*probe)(proto), void *data) \ |
872037bb PMF |
143 | { \ |
144 | return -ENOSYS; \ | |
145 | } \ | |
cc7b66ba | 146 | static inline int __unregister_trace_##name(void (*probe)(proto), void *data) \ |
872037bb PMF |
147 | { \ |
148 | return -ENOSYS; \ | |
149 | } | |
150 | ||
151 | #define DEFINE_TRACE(name) | |
152 | #define EXPORT_TRACEPOINT_SYMBOL_GPL(name) | |
153 | #define EXPORT_TRACEPOINT_SYMBOL(name) | |
154 | ||
155 | static inline void tracepoint_update_probe_range(struct tracepoint *begin, | |
156 | struct tracepoint *end) | |
157 | { } | |
158 | #endif /* CONFIG_TRACEPOINTS */ | |
f99be407 | 159 | |
377d33ed MD |
160 | /* |
161 | * The need for the DECLARE_TRACE_NOARGS() is to handle the prototype | |
162 | * (void). "void" is a special value in a function prototype and can | |
163 | * not be combined with other arguments. Since the DECLARE_TRACE() | |
164 | * macro adds a data element at the beginning of the prototype, | |
165 | * we need a way to differentiate "(void *data, proto)" from | |
166 | * "(void *data, void)". The second prototype is invalid. | |
167 | * | |
168 | * DECLARE_TRACE_NOARGS() passes "void" as the tracepoint prototype | |
169 | * and "void *__data" as the callback prototype. | |
170 | * | |
171 | * DECLARE_TRACE() passes "proto" as the tracepoint protoype and | |
172 | * "void *__data, proto" as the callback prototype. | |
173 | */ | |
174 | #define DECLARE_TRACE_NOARGS(name) \ | |
175 | __DECLARE_TRACE(name, void, , void *__data, __data) | |
176 | ||
177 | #define DECLARE_TRACE(name, proto, args) \ | |
178 | __DECLARE_TRACE(name, PARAMS(proto), PARAMS(args), \ | |
179 | PARAMS(void *__data, proto), \ | |
180 | PARAMS(__data, args)) | |
9dec086e | 181 | |
f99be407 PMF |
182 | /* |
183 | * Connect a probe to a tracepoint. | |
184 | * Internal API, should not be used directly. | |
185 | */ | |
9dec086e | 186 | extern int tracepoint_probe_register(const char *name, void *probe, void *data); |
f99be407 PMF |
187 | |
188 | /* | |
189 | * Disconnect a probe from a tracepoint. | |
190 | * Internal API, should not be used directly. | |
191 | */ | |
9dec086e | 192 | extern int tracepoint_probe_unregister(const char *name, void *probe, void *data); |
f99be407 | 193 | |
9dec086e NC |
194 | extern int tracepoint_probe_register_noupdate(const char *name, void *probe, |
195 | void *data); | |
196 | extern int tracepoint_probe_unregister_noupdate(const char *name, void *probe, | |
197 | void *data); | |
f99be407 PMF |
198 | extern void tracepoint_probe_update_all(void); |
199 | ||
200 | struct tracepoint_iter { | |
474d745f | 201 | struct tracepoint_lib *lib; |
f218ff28 | 202 | struct tracepoint * const *tracepoint; |
f99be407 PMF |
203 | }; |
204 | ||
205 | extern void tracepoint_iter_start(struct tracepoint_iter *iter); | |
206 | extern void tracepoint_iter_next(struct tracepoint_iter *iter); | |
207 | extern void tracepoint_iter_stop(struct tracepoint_iter *iter); | |
208 | extern void tracepoint_iter_reset(struct tracepoint_iter *iter); | |
f218ff28 MD |
209 | extern int tracepoint_get_iter_range(struct tracepoint * const **tracepoint, |
210 | struct tracepoint * const *begin, struct tracepoint * const *end); | |
f99be407 PMF |
211 | |
212 | /* | |
213 | * tracepoint_synchronize_unregister must be called between the last tracepoint | |
214 | * probe unregistration and the end of module exit to make sure there is no | |
215 | * caller executing a probe when it is freed. | |
216 | */ | |
217 | static inline void tracepoint_synchronize_unregister(void) | |
218 | { | |
270c6b98 | 219 | //ust// synchronize_sched(); |
f99be407 PMF |
220 | } |
221 | ||
474d745f | 222 | struct tracepoint_lib { |
f218ff28 | 223 | struct tracepoint * const *tracepoints_start; |
474d745f | 224 | int tracepoints_count; |
0222e121 | 225 | struct cds_list_head list; |
474d745f PMF |
226 | }; |
227 | ||
f218ff28 | 228 | extern int tracepoint_register_lib(struct tracepoint * const *tracepoints_start, |
872037bb | 229 | int tracepoints_count); |
f218ff28 | 230 | extern int tracepoint_unregister_lib(struct tracepoint * const *tracepoints_start); |
872037bb PMF |
231 | |
232 | #define TRACEPOINT_LIB \ | |
bf5fe926 MD |
233 | extern struct tracepoint * const __start___tracepoints_ptrs[] __attribute__((weak, visibility("hidden"))); \ |
234 | extern struct tracepoint * const __stop___tracepoints_ptrs[] __attribute__((weak, visibility("hidden"))); \ | |
f08ebbe2 MD |
235 | static struct tracepoint * const __tracepoint_ptr_dummy \ |
236 | __attribute__((used, section("__tracepoints_ptrs"))) = NULL; \ | |
bf5fe926 MD |
237 | static void __attribute__((constructor)) __tracepoints__init(void) \ |
238 | { \ | |
103fffbc MD |
239 | tracepoint_register_lib(__start___tracepoints_ptrs, \ |
240 | __stop___tracepoints_ptrs - \ | |
241 | __start___tracepoints_ptrs); \ | |
bf5fe926 MD |
242 | } \ |
243 | \ | |
244 | static void __attribute__((destructor)) __tracepoints__destroy(void) \ | |
245 | { \ | |
103fffbc | 246 | tracepoint_unregister_lib(__start___tracepoints_ptrs); \ |
872037bb PMF |
247 | } |
248 | ||
22d72948 NC |
249 | |
250 | #ifndef TRACE_EVENT | |
251 | /* | |
252 | * For use with the TRACE_EVENT macro: | |
253 | * | |
254 | * We define a tracepoint, its arguments, its printf format | |
255 | * and its 'fast binary record' layout. | |
256 | * | |
257 | * Firstly, name your tracepoint via TRACE_EVENT(name : the | |
258 | * 'subsystem_event' notation is fine. | |
259 | * | |
260 | * Think about this whole construct as the | |
261 | * 'trace_sched_switch() function' from now on. | |
262 | * | |
263 | * | |
264 | * TRACE_EVENT(sched_switch, | |
265 | * | |
266 | * * | |
267 | * * A function has a regular function arguments | |
268 | * * prototype, declare it via TP_PROTO(): | |
269 | * * | |
270 | * | |
271 | * TP_PROTO(struct rq *rq, struct task_struct *prev, | |
272 | * struct task_struct *next), | |
273 | * | |
274 | * * | |
275 | * * Define the call signature of the 'function'. | |
276 | * * (Design sidenote: we use this instead of a | |
277 | * * TP_PROTO1/TP_PROTO2/TP_PROTO3 ugliness.) | |
278 | * * | |
279 | * | |
280 | * TP_ARGS(rq, prev, next), | |
281 | * | |
282 | * * | |
283 | * * Fast binary tracing: define the trace record via | |
284 | * * TP_STRUCT__entry(). You can think about it like a | |
285 | * * regular C structure local variable definition. | |
286 | * * | |
287 | * * This is how the trace record is structured and will | |
288 | * * be saved into the ring buffer. These are the fields | |
289 | * * that will be exposed to readers. | |
290 | * * | |
291 | * * The declared 'local variable' is called '__entry' | |
292 | * * | |
293 | * * __field(pid_t, prev_prid) is equivalent to a standard declariton: | |
294 | * * | |
295 | * * pid_t prev_pid; | |
296 | * * | |
297 | * * __array(char, prev_comm, TASK_COMM_LEN) is equivalent to: | |
298 | * * | |
299 | * * char prev_comm[TASK_COMM_LEN]; | |
300 | * * | |
301 | * | |
302 | * TP_STRUCT__entry( | |
303 | * __array( char, prev_comm, TASK_COMM_LEN ) | |
304 | * __field( pid_t, prev_pid ) | |
305 | * __field( int, prev_prio ) | |
306 | * __array( char, next_comm, TASK_COMM_LEN ) | |
307 | * __field( pid_t, next_pid ) | |
308 | * __field( int, next_prio ) | |
309 | * ), | |
310 | * | |
311 | * * | |
312 | * * Assign the entry into the trace record, by embedding | |
313 | * * a full C statement block into TP_fast_assign(). You | |
314 | * * can refer to the trace record as '__entry' - | |
315 | * * otherwise you can put arbitrary C code in here. | |
316 | * * | |
317 | * * Note: this C code will execute every time a trace event | |
318 | * * happens, on an active tracepoint. | |
319 | * * | |
320 | * | |
321 | * TP_fast_assign( | |
322 | * memcpy(__entry->next_comm, next->comm, TASK_COMM_LEN); | |
323 | * __entry->prev_pid = prev->pid; | |
324 | * __entry->prev_prio = prev->prio; | |
325 | * memcpy(__entry->prev_comm, prev->comm, TASK_COMM_LEN); | |
326 | * __entry->next_pid = next->pid; | |
327 | * __entry->next_prio = next->prio; | |
328 | * ) | |
329 | * | |
330 | * * | |
331 | * * Formatted output of a trace record via TP_printf(). | |
332 | * * This is how the tracepoint will appear under debugging | |
333 | * * of tracepoints. | |
334 | * * | |
335 | * * (raw-binary tracing wont actually perform this step.) | |
336 | * * | |
337 | * | |
338 | * TP_printf("task %s:%d [%d] ==> %s:%d [%d]", | |
339 | * __entry->prev_comm, __entry->prev_pid, __entry->prev_prio, | |
340 | * __entry->next_comm, __entry->next_pid, __entry->next_prio), | |
341 | * | |
342 | * ); | |
343 | * | |
344 | * This macro construct is thus used for the regular printf format | |
345 | * tracing setup. | |
346 | * | |
347 | * A set of (un)registration functions can be passed to the variant | |
348 | * TRACE_EVENT_FN to perform any (un)registration work. | |
349 | */ | |
350 | ||
0c0686ee NC |
351 | struct trace_event { |
352 | const char *name; | |
353 | int (*regfunc)(void *data); | |
354 | int (*unregfunc)(void *data); | |
fc1caebc | 355 | }; |
0c0686ee NC |
356 | |
357 | struct trace_event_lib { | |
fc1caebc | 358 | struct trace_event * const *trace_events_start; |
0c0686ee | 359 | int trace_events_count; |
0222e121 | 360 | struct cds_list_head list; |
0c0686ee NC |
361 | }; |
362 | ||
363 | struct trace_event_iter { | |
364 | struct trace_event_lib *lib; | |
fc1caebc | 365 | struct trace_event * const *trace_event; |
0c0686ee NC |
366 | }; |
367 | ||
368 | extern void lock_trace_events(void); | |
369 | extern void unlock_trace_events(void); | |
370 | ||
371 | extern void trace_event_iter_start(struct trace_event_iter *iter); | |
372 | extern void trace_event_iter_next(struct trace_event_iter *iter); | |
373 | extern void trace_event_iter_reset(struct trace_event_iter *iter); | |
374 | ||
fc1caebc MD |
375 | extern int trace_event_get_iter_range(struct trace_event * const **trace_event, |
376 | struct trace_event * const *begin, | |
377 | struct trace_event * const *end); | |
0c0686ee NC |
378 | |
379 | extern void trace_event_update_process(void); | |
380 | extern int is_trace_event_enabled(const char *channel, const char *name); | |
381 | ||
fc1caebc | 382 | extern int trace_event_register_lib(struct trace_event * const *start_trace_events, |
0c0686ee NC |
383 | int trace_event_count); |
384 | ||
fc1caebc | 385 | extern int trace_event_unregister_lib(struct trace_event * const *start_trace_events); |
0c0686ee NC |
386 | |
387 | #define TRACE_EVENT_LIB \ | |
fc1caebc | 388 | extern struct trace_event * const __start___trace_events_ptrs[] \ |
0c0686ee | 389 | __attribute__((weak, visibility("hidden"))); \ |
fc1caebc | 390 | extern struct trace_event * const __stop___trace_events_ptrs[] \ |
0c0686ee | 391 | __attribute__((weak, visibility("hidden"))); \ |
f08ebbe2 MD |
392 | static struct trace_event * const __event_ptrs_dummy \ |
393 | __attribute__((used, section("__trace_events_ptrs"))) = NULL; \ | |
0c0686ee NC |
394 | static void __attribute__((constructor)) \ |
395 | __trace_events__init(void) \ | |
396 | { \ | |
fc1caebc MD |
397 | trace_event_register_lib(__start___trace_events_ptrs, \ |
398 | __stop___trace_events_ptrs - \ | |
399 | __start___trace_events_ptrs); \ | |
0c0686ee NC |
400 | } \ |
401 | \ | |
402 | static void __attribute__((destructor)) \ | |
403 | __trace_event__destroy(void) \ | |
404 | { \ | |
fc1caebc | 405 | trace_event_unregister_lib(__start___trace_events_ptrs);\ |
0c0686ee NC |
406 | } |
407 | ||
22d72948 NC |
408 | #define DECLARE_TRACE_EVENT_CLASS(name, proto, args, tstruct, assign, print) |
409 | #define DEFINE_TRACE_EVENT(template, name, proto, args) \ | |
410 | DECLARE_TRACE(name, PARAMS(proto), PARAMS(args)) | |
411 | #define DEFINE_TRACE_EVENT_PRINT(template, name, proto, args, print) \ | |
412 | DECLARE_TRACE(name, PARAMS(proto), PARAMS(args)) | |
413 | ||
414 | #define TRACE_EVENT(name, proto, args, struct, assign, print) \ | |
415 | DECLARE_TRACE(name, PARAMS(proto), PARAMS(args)) | |
416 | #define TRACE_EVENT_FN(name, proto, args, struct, \ | |
417 | assign, print, reg, unreg) \ | |
418 | DECLARE_TRACE(name, PARAMS(proto), PARAMS(args)) | |
419 | ||
420 | #endif /* ifdef TRACE_EVENT (see note above) */ | |
421 | ||
422 | ||
27b052e3 | 423 | #endif /* _UST_TRACEPOINT_H */ |