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