925e12daaec0eb249a029abab1c57a577c221b6e
[lttng-modules.git] / probes / lttng-events.h
1 /*
2 * lttng-events.h
3 *
4 * Copyright (C) 2009 Steven Rostedt <rostedt@goodmis.org>
5 * Copyright (C) 2010-2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 *
7 * Dual LGPL v2.1/GPL v2 license.
8 */
9
10 #include <linux/debugfs.h>
11 #include "lttng.h"
12 #include "lttng-types.h"
13 #include "../wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
14 #include "../wrapper/ringbuffer/frontend_types.h"
15 #include "../ltt-events.h"
16 #include "../ltt-tracer-core.h"
17
18 /*
19 * Macro declarations used for all stages.
20 */
21
22 /*
23 * DECLARE_EVENT_CLASS can be used to add a generic function
24 * handlers for events. That is, if all events have the same
25 * parameters and just have distinct trace points.
26 * Each tracepoint can be defined with DEFINE_EVENT and that
27 * will map the DECLARE_EVENT_CLASS to the tracepoint.
28 *
29 * TRACE_EVENT is a one to one mapping between tracepoint and template.
30 */
31
32 #undef TRACE_EVENT
33 #define TRACE_EVENT(name, proto, args, tstruct, assign, print) \
34 DECLARE_EVENT_CLASS(name, \
35 PARAMS(proto), \
36 PARAMS(args), \
37 PARAMS(tstruct), \
38 PARAMS(assign), \
39 PARAMS(print)) \
40 DEFINE_EVENT(name, name, PARAMS(proto), PARAMS(args))
41
42 #undef DEFINE_EVENT_PRINT
43 #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
44 DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
45
46 /* Callbacks are meaningless to LTTng. */
47 #undef TRACE_EVENT_FN
48 #define TRACE_EVENT_FN(name, proto, args, tstruct, \
49 assign, print, reg, unreg) \
50 TRACE_EVENT(name, PARAMS(proto), PARAMS(args), \
51 PARAMS(tstruct), PARAMS(assign), PARAMS(print)) \
52
53 /*
54 * Stage 1 of the trace events.
55 *
56 * Create dummy trace calls for each events, verifying that the LTTng module
57 * TRACE_EVENT headers match the kernel arguments. Will be optimized out by the
58 * compiler.
59 */
60
61 #include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
62
63 #undef TP_PROTO
64 #define TP_PROTO(args...) args
65
66 #undef TP_ARGS
67 #define TP_ARGS(args...) args
68
69 #undef DEFINE_EVENT
70 #define DEFINE_EVENT(_template, _name, _proto, _args) \
71 void trace_##_name(_proto);
72
73 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
74
75 /*
76 * Stage 2 of the trace events.
77 *
78 * Create event field type metadata section.
79 * Each event produce an array of fields.
80 */
81
82 #include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
83
84 /* Named field types must be defined in lttng-types.h */
85
86 #undef __field_full
87 #define __field_full(_type, _item, _order, _base) \
88 { \
89 .name = #_item, \
90 .type = __type_integer(_type, _order, _base, none), \
91 },
92
93 #undef __field
94 #define __field(_type, _item) \
95 __field_full(_type, _item, __BYTE_ORDER, 10)
96
97 #undef __field_ext
98 #define __field_ext(_type, _item, _filter_type) \
99 __field(_type, _item)
100
101 #undef __field_network
102 #define __field_network(_type, _item) \
103 __field_full(_type, _item, __BIG_ENDIAN, 10)
104
105 #undef __field_network_hex
106 #define __field_network_hex(_type, _item) \
107 __field_full(_type, _item, __BIG_ENDIAN, 16)
108
109 #undef __array_enc_ext
110 #define __array_enc_ext(_type, _item, _length, _order, _base, _encoding)\
111 { \
112 .name = #_item, \
113 .type = \
114 { \
115 .atype = atype_array, \
116 .u.array = \
117 { \
118 .length = _length, \
119 .elem_type = __type_integer(_type, _order, _base, _encoding), \
120 }, \
121 }, \
122 },
123
124 #undef __array
125 #define __array(_type, _item, _length) \
126 __array_enc_ext(_type, _item, _length, __BYTE_ORDER, 10, none)
127
128 #undef __array_text
129 #define __array_text(_type, _item, _length) \
130 __array_enc_ext(_type, _item, _length, __BYTE_ORDER, 10, UTF8)
131
132 #undef __array_hex
133 #define __array_hex(_type, _item, _length) \
134 __array_enc_ext(_type, _item, _length, __BYTE_ORDER, 16, none)
135
136 #undef __dynamic_array_enc_ext
137 #define __dynamic_array_enc_ext(_type, _item, _length, _order, _base, _encoding) \
138 { \
139 .name = #_item, \
140 .type = \
141 { \
142 .atype = atype_sequence, \
143 .u.sequence = \
144 { \
145 .length_type = __type_integer(u32, __BYTE_ORDER, 10, none), \
146 .elem_type = __type_integer(_type, _order, _base, _encoding), \
147 }, \
148 }, \
149 },
150
151 #undef __dynamic_array
152 #define __dynamic_array(_type, _item, _length) \
153 __dynamic_array_enc_ext(_type, _item, _length, __BYTE_ORDER, 10, none)
154
155 #undef __dynamic_array_text
156 #define __dynamic_array_text(_type, _item, _length) \
157 __dynamic_array_enc_ext(_type, _item, _length, __BYTE_ORDER, 10, UTF8)
158
159 #undef __dynamic_array_hex
160 #define __dynamic_array_hex(_type, _item, _length) \
161 __dynamic_array_enc_ext(_type, _item, _length, __BYTE_ORDER, 16, none)
162
163 #undef __string
164 #define __string(_item, _src) \
165 { \
166 .name = #_item, \
167 .type = \
168 { \
169 .atype = atype_string, \
170 .u.basic.string.encoding = lttng_encode_UTF8, \
171 }, \
172 },
173
174 #undef TP_STRUCT__entry
175 #define TP_STRUCT__entry(args...) args /* Only one used in this phase */
176
177 #undef DECLARE_EVENT_CLASS
178 #define DECLARE_EVENT_CLASS(_name, _proto, _args, _tstruct, _assign, _print) \
179 static const struct lttng_event_field __event_fields___##_name[] = { \
180 _tstruct \
181 };
182
183 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
184
185 #undef __type_integer
186
187 /*
188 * Stage 3 of the trace events.
189 *
190 * Create probe callback prototypes.
191 */
192
193 #include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
194
195 #undef TP_PROTO
196 #define TP_PROTO(args...) args
197
198 #undef DECLARE_EVENT_CLASS
199 #define DECLARE_EVENT_CLASS(_name, _proto, _args, _tstruct, _assign, _print) \
200 static void __event_probe__##_name(void *__data, _proto);
201
202 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
203
204 /*
205 * Stage 4 of the trace events.
206 *
207 * Create an array of events.
208 */
209
210 /* Named field types must be defined in lttng-types.h */
211
212 #include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
213
214 #ifndef TP_PROBE_CB
215 #define TP_PROBE_CB(_template) &__event_probe__##_template
216 #endif
217
218 #undef DEFINE_EVENT
219 #define DEFINE_EVENT(_template, _name, _proto, _args) \
220 { \
221 .fields = __event_fields___##_template, \
222 .name = #_name, \
223 .probe_callback = (void *) TP_PROBE_CB(_template), \
224 .nr_fields = ARRAY_SIZE(__event_fields___##_template), \
225 .owner = THIS_MODULE, \
226 },
227
228 #define TP_ID1(_token, _system) _token##_system
229 #define TP_ID(_token, _system) TP_ID1(_token, _system)
230
231 static const struct lttng_event_desc TP_ID(__event_desc___, TRACE_SYSTEM)[] = {
232 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
233 };
234
235 #undef TP_ID1
236 #undef TP_ID
237
238
239 /*
240 * Stage 5 of the trace events.
241 *
242 * Create a toplevel descriptor for the whole probe.
243 */
244
245 #define TP_ID1(_token, _system) _token##_system
246 #define TP_ID(_token, _system) TP_ID1(_token, _system)
247
248 /* non-const because list head will be modified when registered. */
249 static struct lttng_probe_desc TP_ID(__probe_desc___, TRACE_SYSTEM) = {
250 .event_desc = TP_ID(__event_desc___, TRACE_SYSTEM),
251 .nr_events = ARRAY_SIZE(TP_ID(__event_desc___, TRACE_SYSTEM)),
252 };
253
254 #undef TP_ID1
255 #undef TP_ID
256
257 /*
258 * Stage 6 of the trace events.
259 *
260 * Create static inline function that calculates event size.
261 */
262
263 #include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
264
265 /* Named field types must be defined in lttng-types.h */
266
267 #undef __field_full
268 #define __field_full(_type, _item, _order, _base) \
269 __event_len += lib_ring_buffer_align(__event_len, ltt_alignof(_type)); \
270 __event_len += sizeof(_type);
271
272 #undef __array_enc_ext
273 #define __array_enc_ext(_type, _item, _length, _order, _base, _encoding) \
274 __event_len += lib_ring_buffer_align(__event_len, ltt_alignof(_type)); \
275 __event_len += sizeof(_type) * (_length);
276
277 #undef __dynamic_array_enc_ext
278 #define __dynamic_array_enc_ext(_type, _item, _length, _order, _base, _encoding)\
279 __event_len += lib_ring_buffer_align(__event_len, ltt_alignof(u32)); \
280 __event_len += sizeof(u32); \
281 __event_len += lib_ring_buffer_align(__event_len, ltt_alignof(_type)); \
282 __dynamic_len[__dynamic_len_idx] = (_length); \
283 __event_len += sizeof(_type) * __dynamic_len[__dynamic_len_idx]; \
284 __dynamic_len_idx++;
285
286 #undef __string
287 #define __string(_item, _src) \
288 __event_len += __dynamic_len[__dynamic_len_idx++] = strlen(_src) + 1;
289
290 #undef TP_PROTO
291 #define TP_PROTO(args...) args
292
293 #undef TP_STRUCT__entry
294 #define TP_STRUCT__entry(args...) args
295
296 #undef DECLARE_EVENT_CLASS
297 #define DECLARE_EVENT_CLASS(_name, _proto, _args, _tstruct, _assign, _print) \
298 static inline size_t __event_get_size__##_name(size_t *__dynamic_len, _proto) \
299 { \
300 size_t __event_len = 0; \
301 unsigned int __dynamic_len_idx = 0; \
302 \
303 if (0) \
304 (void) __dynamic_len_idx; /* don't warn if unused */ \
305 _tstruct \
306 return __event_len; \
307 }
308
309 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
310
311 /*
312 * Stage 7 of the trace events.
313 *
314 * Create static inline function that calculates event payload alignment.
315 */
316
317 #include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
318
319 /* Named field types must be defined in lttng-types.h */
320
321 #undef __field_full
322 #define __field_full(_type, _item, _order, _base) \
323 __event_align = max_t(size_t, __event_align, ltt_alignof(_type));
324
325 #undef __array_enc_ext
326 #define __array_enc_ext(_type, _item, _length, _order, _base, _encoding) \
327 __event_align = max_t(size_t, __event_align, ltt_alignof(_type));
328
329 #undef __dynamic_array_enc_ext
330 #define __dynamic_array_enc_ext(_type, _item, _length, _order, _base, _encoding)\
331 __event_align = max_t(size_t, __event_align, ltt_alignof(u32)); \
332 __event_align = max_t(size_t, __event_align, ltt_alignof(_type));
333
334 #undef __string
335 #define __string(_item, _src)
336
337 #undef TP_PROTO
338 #define TP_PROTO(args...) args
339
340 #undef TP_STRUCT__entry
341 #define TP_STRUCT__entry(args...) args
342
343 #undef DECLARE_EVENT_CLASS
344 #define DECLARE_EVENT_CLASS(_name, _proto, _args, _tstruct, _assign, _print) \
345 static inline size_t __event_get_align__##_name(_proto) \
346 { \
347 size_t __event_align = 1; \
348 _tstruct \
349 return __event_align; \
350 }
351
352 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
353
354
355 /*
356 * Stage 8 of the trace events.
357 *
358 * Create structure declaration that allows the "assign" macros to access the
359 * field types.
360 */
361
362 #include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
363
364 /* Named field types must be defined in lttng-types.h */
365
366 #undef __field_full
367 #define __field_full(_type, _item, _order, _base) _type _item;
368
369 #undef __array_enc_ext
370 #define __array_enc_ext(_type, _item, _length, _order, _base, _encoding) \
371 _type _item;
372
373 #undef __dynamic_array_enc_ext
374 #define __dynamic_array_enc_ext(_type, _item, _length, _order, _base, _encoding)\
375 _type _item;
376
377 #undef __string
378 #define __string(_item, _src) char _item;
379
380 #undef TP_STRUCT__entry
381 #define TP_STRUCT__entry(args...) args
382
383 #undef DECLARE_EVENT_CLASS
384 #define DECLARE_EVENT_CLASS(_name, _proto, _args, _tstruct, _assign, _print) \
385 struct __event_typemap__##_name { \
386 _tstruct \
387 };
388
389 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
390
391
392 /*
393 * Stage 9 of the trace events.
394 *
395 * Create the probe function : call even size calculation and write event data
396 * into the buffer.
397 *
398 * We use both the field and assignment macros to write the fields in the order
399 * defined in the field declaration. The field declarations control the
400 * execution order, jumping to the appropriate assignment block.
401 */
402
403 #include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
404
405 #undef __field_full
406 #define __field_full(_type, _item, _order, _base) \
407 goto __assign_##_item; \
408 __end_field_##_item:
409
410 #undef __array_enc_ext
411 #define __array_enc_ext(_type, _item, _length, _order, _base, _encoding)\
412 goto __assign_##_item; \
413 __end_field_##_item:
414
415 #undef __dynamic_array_enc_ext
416 #define __dynamic_array_enc_ext(_type, _item, _length, _order, _base, _encoding)\
417 goto __assign_##_item##_1; \
418 __end_field_##_item##_1: \
419 goto __assign_##_item##_2; \
420 __end_field_##_item##_2:
421
422 #undef __string
423 #define __string(_item, _src) \
424 goto __assign_##_item; \
425 __end_field_##_item:
426
427 /*
428 * Macros mapping tp_assign() to "=", tp_memcpy() to memcpy() and tp_strcpy() to
429 * strcpy().
430 */
431 #undef tp_assign
432 #define tp_assign(dest, src) \
433 __assign_##dest: \
434 { \
435 __typeof__(__typemap.dest) __tmp = (src); \
436 lib_ring_buffer_align_ctx(&__ctx, ltt_alignof(__tmp)); \
437 __chan->ops->event_write(&__ctx, &__tmp, sizeof(__tmp));\
438 } \
439 goto __end_field_##dest;
440
441 #undef tp_memcpy
442 #define tp_memcpy(dest, src, len) \
443 __assign_##dest: \
444 if (0) \
445 (void) __typemap.dest; \
446 lib_ring_buffer_align_ctx(&__ctx, ltt_alignof(__typemap.dest)); \
447 __chan->ops->event_write(&__ctx, src, len); \
448 goto __end_field_##dest;
449
450 #undef tp_memcpy_dyn
451 #define tp_memcpy_dyn(dest, src) \
452 __assign_##dest##_1: \
453 { \
454 u32 __tmpl = __dynamic_len[__dynamic_len_idx]; \
455 lib_ring_buffer_align_ctx(&__ctx, ltt_alignof(u32)); \
456 __chan->ops->event_write(&__ctx, &__tmpl, sizeof(u32)); \
457 } \
458 goto __end_field_##dest##_1; \
459 __assign_##dest##_2: \
460 lib_ring_buffer_align_ctx(&__ctx, ltt_alignof(__typemap.dest)); \
461 __chan->ops->event_write(&__ctx, src, \
462 sizeof(__typemap.dest) * __get_dynamic_array_len(dest));\
463 goto __end_field_##dest##_2;
464
465 #undef tp_strcpy
466 #define tp_strcpy(dest, src) \
467 tp_memcpy(dest, src, __get_dynamic_array_len(dest))
468
469 /* Named field types must be defined in lttng-types.h */
470
471 #undef __get_str
472 #define __get_str(field) field
473
474 #undef __get_dynamic_array
475 #define __get_dynamic_array(field) field
476
477 /* Beware: this get len actually consumes the len value */
478 #undef __get_dynamic_array_len
479 #define __get_dynamic_array_len(field) __dynamic_len[__dynamic_len_idx++]
480
481 #undef TP_PROTO
482 #define TP_PROTO(args...) args
483
484 #undef TP_ARGS
485 #define TP_ARGS(args...) args
486
487 #undef TP_STRUCT__entry
488 #define TP_STRUCT__entry(args...) args
489
490 #undef TP_fast_assign
491 #define TP_fast_assign(args...) args
492
493 #undef DECLARE_EVENT_CLASS
494 #define DECLARE_EVENT_CLASS(_name, _proto, _args, _tstruct, _assign, _print) \
495 static void __event_probe__##_name(void *__data, _proto) \
496 { \
497 struct ltt_event *__event = __data; \
498 struct ltt_channel *__chan = __event->chan; \
499 struct lib_ring_buffer_ctx __ctx; \
500 size_t __event_len, __event_align; \
501 size_t __dynamic_len_idx = 0; \
502 size_t __dynamic_len[ARRAY_SIZE(__event_fields___##_name)]; \
503 struct __event_typemap__##_name __typemap; \
504 int __ret; \
505 \
506 if (0) \
507 (void) __dynamic_len_idx; /* don't warn if unused */ \
508 if (unlikely(!ACCESS_ONCE(__chan->session->active))) \
509 return; \
510 if (unlikely(!ACCESS_ONCE(__chan->enabled))) \
511 return; \
512 if (unlikely(!ACCESS_ONCE(__event->enabled))) \
513 return; \
514 __event_len = __event_get_size__##_name(__dynamic_len, _args); \
515 __event_align = __event_get_align__##_name(_args); \
516 lib_ring_buffer_ctx_init(&__ctx, __chan->chan, __event, __event_len, \
517 __event_align, -1); \
518 __ret = __chan->ops->event_reserve(&__ctx, __event->id); \
519 if (__ret < 0) \
520 return; \
521 /* Control code (field ordering) */ \
522 _tstruct \
523 __chan->ops->event_commit(&__ctx); \
524 return; \
525 /* Copy code, steered by control code */ \
526 _assign \
527 }
528
529 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
530
531 /*
532 * Stage 10 of the trace events.
533 *
534 * Register/unregister probes at module load/unload.
535 */
536
537 #include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
538
539 #define TP_ID1(_token, _system) _token##_system
540 #define TP_ID(_token, _system) TP_ID1(_token, _system)
541 #define module_init_eval1(_token, _system) module_init(_token##_system)
542 #define module_init_eval(_token, _system) module_init_eval1(_token, _system)
543 #define module_exit_eval1(_token, _system) module_exit(_token##_system)
544 #define module_exit_eval(_token, _system) module_exit_eval1(_token, _system)
545
546 #ifndef TP_MODULE_OVERRIDE
547 static int TP_ID(__lttng_events_init__, TRACE_SYSTEM)(void)
548 {
549 wrapper_vmalloc_sync_all();
550 return ltt_probe_register(&TP_ID(__probe_desc___, TRACE_SYSTEM));
551 }
552
553 module_init_eval(__lttng_events_init__, TRACE_SYSTEM);
554
555 static void TP_ID(__lttng_events_exit__, TRACE_SYSTEM)(void)
556 {
557 ltt_probe_unregister(&TP_ID(__probe_desc___, TRACE_SYSTEM));
558 }
559
560 module_exit_eval(__lttng_events_exit__, TRACE_SYSTEM);
561 #endif
562
563 #undef module_init_eval
564 #undef module_exit_eval
565 #undef TP_ID1
566 #undef TP_ID
This page took 0.040313 seconds and 4 git commands to generate.