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