Write context fields into trace
[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), \
dc7f600a 198 .owner = THIS_MODULE, \
6db3d13b 199 },
40652b65 200
d0dd2ecb
MD
201#define TP_ID1(_token, _system) _token##_system
202#define TP_ID(_token, _system) TP_ID1(_token, _system)
40652b65 203
d0dd2ecb 204static const struct lttng_event_desc TP_ID(__event_desc___, TRACE_SYSTEM)[] = {
40652b65 205#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
299338c8
MD
206};
207
d0dd2ecb
MD
208#undef TP_ID1
209#undef TP_ID
210
85a9ca7f
MD
211
212/*
c099397a 213 * Stage 5 of the trace events.
85a9ca7f
MD
214 *
215 * Create a toplevel descriptor for the whole probe.
216 */
217
218#define TP_ID1(_token, _system) _token##_system
219#define TP_ID(_token, _system) TP_ID1(_token, _system)
220
221/* non-const because list head will be modified when registered. */
222static struct lttng_probe_desc TP_ID(__probe_desc___, TRACE_SYSTEM) = {
223 .event_desc = TP_ID(__event_desc___, TRACE_SYSTEM),
224 .nr_events = ARRAY_SIZE(TP_ID(__event_desc___, TRACE_SYSTEM)),
225};
226
227#undef TP_ID1
228#undef TP_ID
229
d0dd2ecb 230/*
c099397a 231 * Stage 6 of the trace events.
40652b65
MD
232 *
233 * Create static inline function that calculates event size.
234 */
235
6db3d13b 236#include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
40652b65 237
6db3d13b
MD
238/* Named field types must be defined in lttng-types.h */
239
240#undef __field
85a80742 241#define __field(_type, _item) \
d793d5e1 242 __event_len += lib_ring_buffer_align(__event_len, ltt_alignof(_type)); \
0d1d4002 243 __event_len += sizeof(_type);
6db3d13b
MD
244
245#undef __field_ext
246#define __field_ext(_type, _item, _filter_type) __field(_type, _item)
247
248#undef __array
85a80742 249#define __array(_type, _item, _length) \
d793d5e1 250 __event_len += lib_ring_buffer_align(__event_len, ltt_alignof(_type)); \
0d1d4002 251 __event_len += sizeof(_type) * (_length);
6db3d13b 252
64c796d8
MD
253#undef __array_text
254#define __array_text(_type, _item, _length) \
255 __array(_type, _item, _length)
256
6db3d13b 257#undef __dynamic_array
85a80742 258#define __dynamic_array(_type, _item, _length) \
d793d5e1 259 __event_len += lib_ring_buffer_align(__event_len, ltt_alignof(u32)); \
85a80742 260 __event_len += sizeof(u32); \
d793d5e1 261 __event_len += lib_ring_buffer_align(__event_len, ltt_alignof(_type)); \
0d1d4002 262 __event_len += sizeof(_type) * (_length);
6db3d13b 263
64c796d8
MD
264#undef __dynamic_array_text
265#define __dynamic_array_text(_type, _item, _length) \
266 __dynamic_array(_type, _item, _length)
267
6db3d13b 268#undef __string
85a80742 269#define __string(_item, _src) \
0d1d4002
MD
270 __event_len += __dynamic_len[__dynamic_len_idx++] = strlen(_src) + 1;
271
272#undef TP_PROTO
273#define TP_PROTO(args...) args
6db3d13b
MD
274
275#undef TP_STRUCT__entry
0d1d4002 276#define TP_STRUCT__entry(args...) args
6db3d13b
MD
277
278#undef DECLARE_EVENT_CLASS
0d1d4002
MD
279#define DECLARE_EVENT_CLASS(_name, _proto, _args, _tstruct, _assign, _print) \
280static inline size_t __event_get_size__##_name(size_t *__dynamic_len, _proto) \
281{ \
282 size_t __event_len = 0; \
283 unsigned int __dynamic_len_idx = 0; \
284 \
285 if (0) \
286 (void) __dynamic_len_idx; /* don't warn if unused */ \
287 _tstruct \
288 return __event_len; \
6db3d13b 289}
40652b65
MD
290
291#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
292
293/*
c099397a 294 * Stage 7 of the trace events.
e763dbf5
MD
295 *
296 * Create static inline function that calculates event payload alignment.
297 */
298
299#include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
300
301/* Named field types must be defined in lttng-types.h */
302
303#undef __field
304#define __field(_type, _item) \
d793d5e1 305 __event_align = max_t(size_t, __event_align, ltt_alignof(_type));
e763dbf5
MD
306
307#undef __field_ext
308#define __field_ext(_type, _item, _filter_type) __field(_type, _item)
309
310#undef __array
311#define __array(_type, _item, _length) \
d793d5e1 312 __event_align = max_t(size_t, __event_align, ltt_alignof(_type));
e763dbf5 313
64c796d8
MD
314#undef __array_text
315#define __array_text(_type, _item, _length) \
316 __array(_type, _item, _length)
317
e763dbf5
MD
318#undef __dynamic_array
319#define __dynamic_array(_type, _item, _length) \
d793d5e1
MD
320 __event_align = max_t(size_t, __event_align, ltt_alignof(u32)); \
321 __event_align = max_t(size_t, __event_align, ltt_alignof(_type));
e763dbf5 322
64c796d8
MD
323#undef __dynamic_array_text
324#define __dynamic_array_text(_type, _item, _length) \
325 __dynamic_array(_type, _item, _length)
326
e763dbf5
MD
327#undef __string
328#define __string(_item, _src)
329
330#undef TP_PROTO
331#define TP_PROTO(args...) args
332
333#undef TP_STRUCT__entry
334#define TP_STRUCT__entry(args...) args
335
336#undef DECLARE_EVENT_CLASS
337#define DECLARE_EVENT_CLASS(_name, _proto, _args, _tstruct, _assign, _print) \
338static inline size_t __event_get_align__##_name(_proto) \
339{ \
340 size_t __event_align = 1; \
341 _tstruct \
342 return __event_align; \
343}
344
345#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
346
347
e763dbf5 348/*
c099397a 349 * Stage 8 of the trace events.
40652b65 350 *
3c4ffab9
MD
351 * Create structure declaration that allows the "assign" macros to access the
352 * field types.
353 */
354
355#include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
356
357/* Named field types must be defined in lttng-types.h */
358
359#undef __field
360#define __field(_type, _item) _type _item;
361
362#undef __field_ext
363#define __field_ext(_type, _item, _filter_type) __field(_type, _item)
364
365#undef __array
366#define __array(_type, _item, _length) _type _item;
367
64c796d8
MD
368#undef __array_text
369#define __array_text(_type, _item, _length) \
370 __array(_type, _item, _length)
371
3c4ffab9
MD
372#undef __dynamic_array
373#define __dynamic_array(_type, _item, _length) _type _item;
374
64c796d8
MD
375#undef __dynamic_array_text
376#define __dynamic_array_text(_type, _item, _length) \
377 __dynamic_array(_type, _item, _length)
378
3c4ffab9
MD
379#undef __string
380#define __string(_item, _src) char _item;
381
382#undef TP_STRUCT__entry
383#define TP_STRUCT__entry(args...) args
384
385#undef DECLARE_EVENT_CLASS
386#define DECLARE_EVENT_CLASS(_name, _proto, _args, _tstruct, _assign, _print) \
387struct __event_typemap__##_name { \
388 _tstruct \
389};
390
391#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
392
393
394/*
c099397a 395 * Stage 9 of the trace events.
3c4ffab9 396 *
40652b65
MD
397 * Create the probe function : call even size calculation and write event data
398 * into the buffer.
e763dbf5 399 *
67e5e60c
MD
400 * We use both the field and assignment macros to write the fields in the order
401 * defined in the field declaration. The field declarations control the
402 * execution order, jumping to the appropriate assignment block.
40652b65
MD
403 */
404
e763dbf5
MD
405#include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
406
407#undef __field
408#define __field(_type, _item) \
e763dbf5
MD
409 goto __assign_##_item; \
410__end_field_##_item:
40652b65 411
e763dbf5
MD
412#undef __field_ext
413#define __field_ext(_type, _item, _filter_type) __field(_type, _item)
40652b65 414
e763dbf5
MD
415#undef __array
416#define __array(_type, _item, _length) \
e763dbf5
MD
417 goto __assign_##_item; \
418__end_field_##_item:
40652b65 419
64c796d8
MD
420#undef __array_text
421#define __array_text(_type, _item, _length) \
422 __array(_type, _item, _length)
423
e763dbf5
MD
424#undef __dynamic_array
425#define __dynamic_array(_type, _item, _length) \
e763dbf5
MD
426 goto __assign_##_item##_1; \
427__end_field_##_item##_1: \
e763dbf5
MD
428 goto __assign_##_item##_2; \
429__end_field_##_item##_2:
40652b65 430
64c796d8
MD
431#undef __dynamic_array_text
432#define __dynamic_array_text(_type, _item, _length) \
433 __dynamic_array(_type, _item, _length)
434
e763dbf5
MD
435#undef __string
436#define __string(_item, _src) \
437 goto __assign_##_item; \
438__end_field_##_item:
439
440/*
441 * Macros mapping tp_assign() to "=", tp_memcpy() to memcpy() and tp_strcpy() to
442 * strcpy().
443 */
444#undef tp_assign
445#define tp_assign(dest, src) \
446__assign_##dest: \
447 { \
3c4ffab9 448 __typeof__(__typemap.dest) __tmp = (src); \
d793d5e1 449 lib_ring_buffer_align_ctx(&ctx, ltt_alignof(__tmp)); \
3c4ffab9 450 __chan->ops->event_write(&ctx, &__tmp, sizeof(__tmp)); \
e763dbf5
MD
451 } \
452 goto __end_field_##dest;
453
454#undef tp_memcpy
455#define tp_memcpy(dest, src, len) \
456__assign_##dest: \
d793d5e1
MD
457 if (0) \
458 (void) __typemap.dest; \
459 lib_ring_buffer_align_ctx(&ctx, ltt_alignof(__typemap.dest)); \
e763dbf5
MD
460 __chan->ops->event_write(&ctx, src, len); \
461 goto __end_field_##dest;
462
463#undef tp_memcpy_dyn
464#define tp_memcpy_dyn(dest, src, len) \
465__assign_##dest##_1: \
466 { \
3c4ffab9 467 u32 __tmpl = (len); \
d793d5e1 468 lib_ring_buffer_align_ctx(&ctx, ltt_alignof(u32)); \
e763dbf5
MD
469 __chan->ops->event_write(&ctx, &__tmpl, sizeof(u32)); \
470 } \
471 goto __end_field_##dest##_1; \
472__assign_##dest##_2: \
d793d5e1 473 lib_ring_buffer_align_ctx(&ctx, ltt_alignof(__typemap.dest)); \
e763dbf5
MD
474 __chan->ops->event_write(&ctx, src, len); \
475 goto __end_field_##dest##_2;
476
477#undef tp_strcpy
478#define tp_strcpy(dest, src) \
3c4ffab9 479 tp_memcpy(dest, src, __get_dynamic_array_len(dest))
40652b65 480
e763dbf5
MD
481/* Named field types must be defined in lttng-types.h */
482
483#undef __get_str
484#define __get_str(field) field
485
486#undef __get_dynamic_array
487#define __get_dynamic_array(field) field
488
489/* Beware: this get len actually consumes the len value */
490#undef __get_dynamic_array_len
491#define __get_dynamic_array_len(field) __dynamic_len[__dynamic_len_idx++]
492
493#undef TP_PROTO
494#define TP_PROTO(args...) args
495
496#undef TP_ARGS
497#define TP_ARGS(args...) args
498
499#undef TP_STRUCT__entry
500#define TP_STRUCT__entry(args...) args
501
502#undef TP_fast_assign
503#define TP_fast_assign(args...) args
504
505#undef DECLARE_EVENT_CLASS
506#define DECLARE_EVENT_CLASS(_name, _proto, _args, _tstruct, _assign, _print) \
507static void __event_probe__##_name(void *__data, _proto) \
508{ \
509 struct ltt_event *__event = __data; \
510 struct ltt_channel *__chan = __event->chan; \
511 struct lib_ring_buffer_ctx ctx; \
512 size_t __event_len, __event_align; \
513 size_t __dynamic_len_idx = 0; \
514 size_t __dynamic_len[ARRAY_SIZE(__event_fields___##_name)]; \
3c4ffab9 515 struct __event_typemap__##_name __typemap; \
e763dbf5
MD
516 int __ret; \
517 \
518 if (0) \
519 (void) __dynamic_len_idx; /* don't warn if unused */ \
52fc2e1f
MD
520 if (!ACCESS_ONCE(__chan->session->active)) \
521 return; \
e763dbf5
MD
522 __event_len = __event_get_size__##_name(__dynamic_len, _args); \
523 __event_align = __event_get_align__##_name(_args); \
f1676205 524 lib_ring_buffer_ctx_init(&ctx, __chan->chan, __event, __event_len, \
e763dbf5 525 __event_align, -1); \
4e1f08f4 526 __ret = __chan->ops->event_reserve(&ctx, __event->id); \
e763dbf5
MD
527 if (__ret < 0) \
528 return; \
529 /* Control code (field ordering) */ \
530 _tstruct \
531 __chan->ops->event_commit(&ctx); \
532 return; \
533 /* Copy code, steered by control code */ \
534 _assign \
535}
536
537#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
538
3afe7aac 539/*
c099397a 540 * Stage 10 of the trace events.
3afe7aac
MD
541 *
542 * Register/unregister probes at module load/unload.
543 */
544
545#include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
546
547#define TP_ID1(_token, _system) _token##_system
548#define TP_ID(_token, _system) TP_ID1(_token, _system)
549#define module_init_eval1(_token, _system) module_init(_token##_system)
550#define module_init_eval(_token, _system) module_init_eval1(_token, _system)
551#define module_exit_eval1(_token, _system) module_exit(_token##_system)
552#define module_exit_eval(_token, _system) module_exit_eval1(_token, _system)
553
3afe7aac
MD
554static int TP_ID(__lttng_events_init__, TRACE_SYSTEM)(void)
555{
6d2a620c 556 wrapper_vmalloc_sync_all();
85a9ca7f 557 return ltt_probe_register(&TP_ID(__probe_desc___, TRACE_SYSTEM));
3afe7aac
MD
558}
559
560module_init_eval(__lttng_events_init__, TRACE_SYSTEM);
561
3afe7aac
MD
562static void TP_ID(__lttng_events_exit__, TRACE_SYSTEM)(void)
563{
85a9ca7f 564 ltt_probe_unregister(&TP_ID(__probe_desc___, TRACE_SYSTEM));
3afe7aac
MD
565}
566
567module_exit_eval(__lttng_events_exit__, TRACE_SYSTEM);
568
569#undef module_init_eval
570#undef module_exit_eval
571#undef TP_ID1
572#undef TP_ID
This page took 0.058457 seconds and 4 git commands to generate.