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