Metadata generation (partial)
[lttng-modules.git] / probes / lttng-events.h
1 #include <linux/debugfs.h>
2 #include "lttng.h"
3 #include "lttng-types.h"
4 #include "../wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
5 #include "../wrapper/ringbuffer/frontend_types.h"
6 #include "../ltt-events.h"
7 #include "../ltt-tracer-core.h"
8
9 /*
10 * Macro declarations used for all stages.
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 */
22
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), \
30 PARAMS(print)) \
31 DEFINE_EVENT(name, name, PARAMS(proto), PARAMS(args))
32
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
44 /*
45 * Stage 1 of the trace events.
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) \
62 void trace_##_name(_proto);
63
64 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
65
66 /*
67 * Stage 2 of the trace events.
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
75 /* Named field types must be defined in lttng-types.h */
76
77 #undef __field
78 #define __field(_type, _item) \
79 { \
80 .name = #_item, \
81 .type = __type_integer(_type, __BYTE_ORDER), \
82 },
83
84 #undef __field_ext
85 #define __field_ext(_type, _item, _filter_type) __field(_type, _item)
86
87 #undef __field_network
88 #define __field_network(_type, _item) \
89 { \
90 .name = #_item, \
91 .type = __type_integer(_type, __BIG_ENDIAN), \
92 },
93
94 #undef __array
95 #define __array(_type, _item, _length) \
96 { \
97 .name = #_item, \
98 .type = \
99 { \
100 .atype = atype_array, \
101 .u.array = \
102 { \
103 .length = _length, \
104 .elem_type = __type_integer(_type, __BYTE_ORDER), \
105 }, \
106 }, \
107 },
108
109 #undef __dynamic_array
110 #define __dynamic_array(_type, _item, _length) \
111 { \
112 .name = #_item, \
113 .type = \
114 { \
115 .atype = atype_sequence, \
116 .u.sequence = \
117 { \
118 .length_type = __type_integer(u32, __BYTE_ORDER), \
119 .elem_type = __type_integer(_type, __BYTE_ORDER), \
120 }, \
121 }, \
122 },
123
124 #undef __string
125 #define __string(_item, _src) \
126 { \
127 .name = #_item, \
128 .type = \
129 { \
130 .atype = atype_string, \
131 .u.basic.string.encoding = lttng_encode_UTF8, \
132 }, \
133 },
134
135 #undef TP_STRUCT__entry
136 #define TP_STRUCT__entry(args...) args /* Only one used in this phase */
137
138 #undef DECLARE_EVENT_CLASS
139 #define DECLARE_EVENT_CLASS(_name, _proto, _args, _tstruct, _assign, _print) \
140 static const struct lttng_event_field __event_fields___##_name[] = { \
141 _tstruct \
142 };
143
144 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
145
146 #undef __type_integer
147
148 /*
149 * Stage 3 of the trace events.
150 *
151 * Create probe callback prototypes.
152 */
153
154 #include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
155
156 #undef TP_PROTO
157 #define TP_PROTO(args...) args
158
159 #undef DECLARE_EVENT_CLASS
160 #define DECLARE_EVENT_CLASS(_name, _proto, _args, _tstruct, _assign, _print) \
161 static void __event_probe__##_name(void *__data, _proto);
162
163 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
164
165 /*
166 * Stage 4 of the trace events.
167 *
168 * Create an array of events.
169 */
170
171 /* Named field types must be defined in lttng-types.h */
172
173 #include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
174
175 #undef DEFINE_EVENT
176 #define DEFINE_EVENT(_template, _name, _proto, _args) \
177 { \
178 .fields = __event_fields___##_template, \
179 .name = #_name, \
180 .probe_callback = (void *) &__event_probe__##_template,\
181 .nr_fields = ARRAY_SIZE(__event_fields___##_template), \
182 },
183
184 #define TP_ID1(_token, _system) _token##_system
185 #define TP_ID(_token, _system) TP_ID1(_token, _system)
186
187 static const struct lttng_event_desc TP_ID(__event_desc___, TRACE_SYSTEM)[] = {
188 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
189 };
190
191 #undef TP_ID1
192 #undef TP_ID
193
194
195 /*
196 * Stage 5 of the trace events.
197 *
198 * Create a toplevel descriptor for the whole probe.
199 */
200
201 #define TP_ID1(_token, _system) _token##_system
202 #define TP_ID(_token, _system) TP_ID1(_token, _system)
203
204 /* non-const because list head will be modified when registered. */
205 static struct lttng_probe_desc TP_ID(__probe_desc___, TRACE_SYSTEM) = {
206 .event_desc = TP_ID(__event_desc___, TRACE_SYSTEM),
207 .nr_events = ARRAY_SIZE(TP_ID(__event_desc___, TRACE_SYSTEM)),
208 };
209
210 #undef TP_ID1
211 #undef TP_ID
212
213 /*
214 * Stage 6 of the trace events.
215 *
216 * Create static inline function that calculates event size.
217 */
218
219 #include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
220
221 /* Named field types must be defined in lttng-types.h */
222
223 #undef __field
224 #define __field(_type, _item) \
225 __event_len += lib_ring_buffer_align(__event_len, __alignof__(_type)); \
226 __event_len += sizeof(_type);
227
228 #undef __field_ext
229 #define __field_ext(_type, _item, _filter_type) __field(_type, _item)
230
231 #undef __array
232 #define __array(_type, _item, _length) \
233 __event_len += lib_ring_buffer_align(__event_len, __alignof__(_type)); \
234 __event_len += sizeof(_type) * (_length);
235
236 #undef __dynamic_array
237 #define __dynamic_array(_type, _item, _length) \
238 __event_len += lib_ring_buffer_align(__event_len, __alignof__(u32)); \
239 __event_len += sizeof(u32); \
240 __event_len += lib_ring_buffer_align(__event_len, __alignof__(_type)); \
241 __event_len += sizeof(_type) * (_length);
242
243 #undef __string
244 #define __string(_item, _src) \
245 __event_len += __dynamic_len[__dynamic_len_idx++] = strlen(_src) + 1;
246
247 #undef TP_PROTO
248 #define TP_PROTO(args...) args
249
250 #undef TP_STRUCT__entry
251 #define TP_STRUCT__entry(args...) args
252
253 #undef DECLARE_EVENT_CLASS
254 #define DECLARE_EVENT_CLASS(_name, _proto, _args, _tstruct, _assign, _print) \
255 static inline size_t __event_get_size__##_name(size_t *__dynamic_len, _proto) \
256 { \
257 size_t __event_len = 0; \
258 unsigned int __dynamic_len_idx = 0; \
259 \
260 if (0) \
261 (void) __dynamic_len_idx; /* don't warn if unused */ \
262 _tstruct \
263 return __event_len; \
264 }
265
266 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
267
268 /*
269 * Stage 7 of the trace events.
270 *
271 * Create static inline function that calculates event payload alignment.
272 */
273
274 #include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
275
276 /* Named field types must be defined in lttng-types.h */
277
278 #undef __field
279 #define __field(_type, _item) \
280 __event_align = max_t(size_t, __event_align, __alignof__(_type));
281
282 #undef __field_ext
283 #define __field_ext(_type, _item, _filter_type) __field(_type, _item)
284
285 #undef __array
286 #define __array(_type, _item, _length) \
287 __event_align = max_t(size_t, __event_align, __alignof__(_type));
288
289 #undef __dynamic_array
290 #define __dynamic_array(_type, _item, _length) \
291 __event_align = max_t(size_t, __event_align, __alignof__(u32)); \
292 __event_align = max_t(size_t, __event_align, __alignof__(_type));
293
294 #undef __string
295 #define __string(_item, _src)
296
297 #undef TP_PROTO
298 #define TP_PROTO(args...) args
299
300 #undef TP_STRUCT__entry
301 #define TP_STRUCT__entry(args...) args
302
303 #undef DECLARE_EVENT_CLASS
304 #define DECLARE_EVENT_CLASS(_name, _proto, _args, _tstruct, _assign, _print) \
305 static inline size_t __event_get_align__##_name(_proto) \
306 { \
307 size_t __event_align = 1; \
308 _tstruct \
309 return __event_align; \
310 }
311
312 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
313
314
315 /*
316 * Stage 8 of the trace events.
317 *
318 * Create structure declaration that allows the "assign" macros to access the
319 * field types.
320 */
321
322 #include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
323
324 /* Named field types must be defined in lttng-types.h */
325
326 #undef __field
327 #define __field(_type, _item) _type _item;
328
329 #undef __field_ext
330 #define __field_ext(_type, _item, _filter_type) __field(_type, _item)
331
332 #undef __array
333 #define __array(_type, _item, _length) _type _item;
334
335 #undef __dynamic_array
336 #define __dynamic_array(_type, _item, _length) _type _item;
337
338 #undef __string
339 #define __string(_item, _src) char _item;
340
341 #undef TP_STRUCT__entry
342 #define TP_STRUCT__entry(args...) args
343
344 #undef DECLARE_EVENT_CLASS
345 #define DECLARE_EVENT_CLASS(_name, _proto, _args, _tstruct, _assign, _print) \
346 struct __event_typemap__##_name { \
347 _tstruct \
348 };
349
350 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
351
352
353 /*
354 * Stage 9 of the trace events.
355 *
356 * Create the probe function : call even size calculation and write event data
357 * into the buffer.
358 *
359 * We use both the field and assignment macros to write the fields in the order
360 * defined in the field declaration. The field declarations control the
361 * execution order, jumping to the appropriate assignment block.
362 */
363
364 #include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
365
366 #undef __field
367 #define __field(_type, _item) \
368 goto __assign_##_item; \
369 __end_field_##_item:
370
371 #undef __field_ext
372 #define __field_ext(_type, _item, _filter_type) __field(_type, _item)
373
374 #undef __array
375 #define __array(_type, _item, _length) \
376 goto __assign_##_item; \
377 __end_field_##_item:
378
379 #undef __dynamic_array
380 #define __dynamic_array(_type, _item, _length) \
381 goto __assign_##_item##_1; \
382 __end_field_##_item##_1: \
383 goto __assign_##_item##_2; \
384 __end_field_##_item##_2:
385
386 #undef __string
387 #define __string(_item, _src) \
388 goto __assign_##_item; \
389 __end_field_##_item:
390
391 /*
392 * Macros mapping tp_assign() to "=", tp_memcpy() to memcpy() and tp_strcpy() to
393 * strcpy().
394 */
395 #undef tp_assign
396 #define tp_assign(dest, src) \
397 __assign_##dest: \
398 { \
399 __typeof__(__typemap.dest) __tmp = (src); \
400 lib_ring_buffer_align_ctx(&ctx, __alignof__(__tmp)); \
401 __chan->ops->event_write(&ctx, &__tmp, sizeof(__tmp)); \
402 } \
403 goto __end_field_##dest;
404
405 #undef tp_memcpy
406 #define tp_memcpy(dest, src, len) \
407 __assign_##dest: \
408 lib_ring_buffer_align_ctx(&ctx, __alignof__(__typemap.dest)); \
409 __chan->ops->event_write(&ctx, src, len); \
410 goto __end_field_##dest;
411
412 #undef tp_memcpy_dyn
413 #define tp_memcpy_dyn(dest, src, len) \
414 __assign_##dest##_1: \
415 { \
416 u32 __tmpl = (len); \
417 lib_ring_buffer_align_ctx(&ctx, __alignof__(u32)); \
418 __chan->ops->event_write(&ctx, &__tmpl, sizeof(u32)); \
419 } \
420 goto __end_field_##dest##_1; \
421 __assign_##dest##_2: \
422 lib_ring_buffer_align_ctx(&ctx, __alignof__(__typemap.dest)); \
423 __chan->ops->event_write(&ctx, src, len); \
424 goto __end_field_##dest##_2;
425
426 #undef tp_strcpy
427 #define tp_strcpy(dest, src) \
428 tp_memcpy(dest, src, __get_dynamic_array_len(dest))
429
430 /* Named field types must be defined in lttng-types.h */
431
432 #undef __get_str
433 #define __get_str(field) field
434
435 #undef __get_dynamic_array
436 #define __get_dynamic_array(field) field
437
438 /* Beware: this get len actually consumes the len value */
439 #undef __get_dynamic_array_len
440 #define __get_dynamic_array_len(field) __dynamic_len[__dynamic_len_idx++]
441
442 #undef TP_PROTO
443 #define TP_PROTO(args...) args
444
445 #undef TP_ARGS
446 #define TP_ARGS(args...) args
447
448 #undef TP_STRUCT__entry
449 #define TP_STRUCT__entry(args...) args
450
451 #undef TP_fast_assign
452 #define TP_fast_assign(args...) args
453
454 #undef DECLARE_EVENT_CLASS
455 #define DECLARE_EVENT_CLASS(_name, _proto, _args, _tstruct, _assign, _print) \
456 static void __event_probe__##_name(void *__data, _proto) \
457 { \
458 struct ltt_event *__event = __data; \
459 struct ltt_channel *__chan = __event->chan; \
460 struct lib_ring_buffer_ctx ctx; \
461 size_t __event_len, __event_align; \
462 size_t __dynamic_len_idx = 0; \
463 size_t __dynamic_len[ARRAY_SIZE(__event_fields___##_name)]; \
464 struct __event_typemap__##_name __typemap; \
465 int __ret; \
466 \
467 if (0) \
468 (void) __dynamic_len_idx; /* don't warn if unused */ \
469 if (!ACCESS_ONCE(__chan->session->active)) \
470 return; \
471 __event_len = __event_get_size__##_name(__dynamic_len, _args); \
472 __event_align = __event_get_align__##_name(_args); \
473 lib_ring_buffer_ctx_init(&ctx, __chan->chan, NULL, __event_len, \
474 __event_align, -1); \
475 __ret = __chan->ops->event_reserve(&ctx); \
476 if (__ret < 0) \
477 return; \
478 /* Control code (field ordering) */ \
479 _tstruct \
480 __chan->ops->event_commit(&ctx); \
481 return; \
482 /* Copy code, steered by control code */ \
483 _assign \
484 }
485
486 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
487
488 /*
489 * Stage 10 of the trace events.
490 *
491 * Register/unregister probes at module load/unload.
492 */
493
494 #include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
495
496 #define TP_ID1(_token, _system) _token##_system
497 #define TP_ID(_token, _system) TP_ID1(_token, _system)
498 #define module_init_eval1(_token, _system) module_init(_token##_system)
499 #define module_init_eval(_token, _system) module_init_eval1(_token, _system)
500 #define module_exit_eval1(_token, _system) module_exit(_token##_system)
501 #define module_exit_eval(_token, _system) module_exit_eval1(_token, _system)
502
503 static int TP_ID(__lttng_events_init__, TRACE_SYSTEM)(void)
504 {
505 wrapper_vmalloc_sync_all();
506 return ltt_probe_register(&TP_ID(__probe_desc___, TRACE_SYSTEM));
507 }
508
509 module_init_eval(__lttng_events_init__, TRACE_SYSTEM);
510
511 static void TP_ID(__lttng_events_exit__, TRACE_SYSTEM)(void)
512 {
513 ltt_probe_unregister(&TP_ID(__probe_desc___, TRACE_SYSTEM));
514 }
515
516 module_exit_eval(__lttng_events_exit__, TRACE_SYSTEM);
517
518 #undef module_init_eval
519 #undef module_exit_eval
520 #undef TP_ID1
521 #undef TP_ID
This page took 0.04203 seconds and 5 git commands to generate.