Add PID context
[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, 10, none),\
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, 10, none),\
92 },
93
94 #undef __array_enc
95 #define __array_enc(_type, _item, _length, _encoding) \
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, 10, _encoding), \
105 }, \
106 }, \
107 },
108
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) \
119 { \
120 .name = #_item, \
121 .type = \
122 { \
123 .atype = atype_sequence, \
124 .u.sequence = \
125 { \
126 .length_type = __type_integer(u32, __BYTE_ORDER, 10, none), \
127 .elem_type = __type_integer(_type, __BYTE_ORDER, 10, _encoding), \
128 }, \
129 }, \
130 },
131
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
140 #undef __string
141 #define __string(_item, _src) \
142 { \
143 .name = #_item, \
144 .type = \
145 { \
146 .atype = atype_string, \
147 .u.basic.string.encoding = lttng_encode_UTF8, \
148 }, \
149 },
150
151 #undef TP_STRUCT__entry
152 #define TP_STRUCT__entry(args...) args /* Only one used in this phase */
153
154 #undef DECLARE_EVENT_CLASS
155 #define DECLARE_EVENT_CLASS(_name, _proto, _args, _tstruct, _assign, _print) \
156 static const struct lttng_event_field __event_fields___##_name[] = { \
157 _tstruct \
158 };
159
160 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
161
162 #undef __type_integer
163
164 /*
165 * Stage 3 of the trace events.
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) \
177 static void __event_probe__##_name(void *__data, _proto);
178
179 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
180
181 /*
182 * Stage 4 of the trace events.
183 *
184 * Create an array of events.
185 */
186
187 /* Named field types must be defined in lttng-types.h */
188
189 #include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
190
191 #undef DEFINE_EVENT
192 #define DEFINE_EVENT(_template, _name, _proto, _args) \
193 { \
194 .fields = __event_fields___##_template, \
195 .name = #_name, \
196 .probe_callback = (void *) &__event_probe__##_template,\
197 .nr_fields = ARRAY_SIZE(__event_fields___##_template), \
198 .owner = THIS_MODULE, \
199 },
200
201 #define TP_ID1(_token, _system) _token##_system
202 #define TP_ID(_token, _system) TP_ID1(_token, _system)
203
204 static const struct lttng_event_desc TP_ID(__event_desc___, TRACE_SYSTEM)[] = {
205 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
206 };
207
208 #undef TP_ID1
209 #undef TP_ID
210
211
212 /*
213 * Stage 5 of the trace events.
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. */
222 static 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
230 /*
231 * Stage 6 of the trace events.
232 *
233 * Create static inline function that calculates event size.
234 */
235
236 #include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
237
238 /* Named field types must be defined in lttng-types.h */
239
240 #undef __field
241 #define __field(_type, _item) \
242 __event_len += lib_ring_buffer_align(__event_len, ltt_alignof(_type)); \
243 __event_len += sizeof(_type);
244
245 #undef __field_ext
246 #define __field_ext(_type, _item, _filter_type) __field(_type, _item)
247
248 #undef __array
249 #define __array(_type, _item, _length) \
250 __event_len += lib_ring_buffer_align(__event_len, ltt_alignof(_type)); \
251 __event_len += sizeof(_type) * (_length);
252
253 #undef __array_text
254 #define __array_text(_type, _item, _length) \
255 __array(_type, _item, _length)
256
257 #undef __dynamic_array
258 #define __dynamic_array(_type, _item, _length) \
259 __event_len += lib_ring_buffer_align(__event_len, ltt_alignof(u32)); \
260 __event_len += sizeof(u32); \
261 __event_len += lib_ring_buffer_align(__event_len, ltt_alignof(_type)); \
262 __event_len += sizeof(_type) * (_length);
263
264 #undef __dynamic_array_text
265 #define __dynamic_array_text(_type, _item, _length) \
266 __dynamic_array(_type, _item, _length)
267
268 #undef __string
269 #define __string(_item, _src) \
270 __event_len += __dynamic_len[__dynamic_len_idx++] = strlen(_src) + 1;
271
272 #undef TP_PROTO
273 #define TP_PROTO(args...) args
274
275 #undef TP_STRUCT__entry
276 #define TP_STRUCT__entry(args...) args
277
278 #undef DECLARE_EVENT_CLASS
279 #define DECLARE_EVENT_CLASS(_name, _proto, _args, _tstruct, _assign, _print) \
280 static 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; \
289 }
290
291 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
292
293 /*
294 * Stage 7 of the trace events.
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) \
305 __event_align = max_t(size_t, __event_align, ltt_alignof(_type));
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) \
312 __event_align = max_t(size_t, __event_align, ltt_alignof(_type));
313
314 #undef __array_text
315 #define __array_text(_type, _item, _length) \
316 __array(_type, _item, _length)
317
318 #undef __dynamic_array
319 #define __dynamic_array(_type, _item, _length) \
320 __event_align = max_t(size_t, __event_align, ltt_alignof(u32)); \
321 __event_align = max_t(size_t, __event_align, ltt_alignof(_type));
322
323 #undef __dynamic_array_text
324 #define __dynamic_array_text(_type, _item, _length) \
325 __dynamic_array(_type, _item, _length)
326
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) \
338 static 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
348 /*
349 * Stage 8 of the trace events.
350 *
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
368 #undef __array_text
369 #define __array_text(_type, _item, _length) \
370 __array(_type, _item, _length)
371
372 #undef __dynamic_array
373 #define __dynamic_array(_type, _item, _length) _type _item;
374
375 #undef __dynamic_array_text
376 #define __dynamic_array_text(_type, _item, _length) \
377 __dynamic_array(_type, _item, _length)
378
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) \
387 struct __event_typemap__##_name { \
388 _tstruct \
389 };
390
391 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
392
393
394 /*
395 * Stage 9 of the trace events.
396 *
397 * Create the probe function : call even size calculation and write event data
398 * into the buffer.
399 *
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.
403 */
404
405 #include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
406
407 #undef __field
408 #define __field(_type, _item) \
409 goto __assign_##_item; \
410 __end_field_##_item:
411
412 #undef __field_ext
413 #define __field_ext(_type, _item, _filter_type) __field(_type, _item)
414
415 #undef __array
416 #define __array(_type, _item, _length) \
417 goto __assign_##_item; \
418 __end_field_##_item:
419
420 #undef __array_text
421 #define __array_text(_type, _item, _length) \
422 __array(_type, _item, _length)
423
424 #undef __dynamic_array
425 #define __dynamic_array(_type, _item, _length) \
426 goto __assign_##_item##_1; \
427 __end_field_##_item##_1: \
428 goto __assign_##_item##_2; \
429 __end_field_##_item##_2:
430
431 #undef __dynamic_array_text
432 #define __dynamic_array_text(_type, _item, _length) \
433 __dynamic_array(_type, _item, _length)
434
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 { \
448 __typeof__(__typemap.dest) __tmp = (src); \
449 lib_ring_buffer_align_ctx(&ctx, ltt_alignof(__tmp)); \
450 __chan->ops->event_write(&ctx, &__tmp, sizeof(__tmp)); \
451 } \
452 goto __end_field_##dest;
453
454 #undef tp_memcpy
455 #define tp_memcpy(dest, src, len) \
456 __assign_##dest: \
457 if (0) \
458 (void) __typemap.dest; \
459 lib_ring_buffer_align_ctx(&ctx, ltt_alignof(__typemap.dest)); \
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 { \
467 u32 __tmpl = (len); \
468 lib_ring_buffer_align_ctx(&ctx, ltt_alignof(u32)); \
469 __chan->ops->event_write(&ctx, &__tmpl, sizeof(u32)); \
470 } \
471 goto __end_field_##dest##_1; \
472 __assign_##dest##_2: \
473 lib_ring_buffer_align_ctx(&ctx, ltt_alignof(__typemap.dest)); \
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) \
479 tp_memcpy(dest, src, __get_dynamic_array_len(dest))
480
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) \
507 static 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)]; \
515 struct __event_typemap__##_name __typemap; \
516 int __ret; \
517 \
518 if (0) \
519 (void) __dynamic_len_idx; /* don't warn if unused */ \
520 if (!ACCESS_ONCE(__chan->session->active)) \
521 return; \
522 __event_len = __event_get_size__##_name(__dynamic_len, _args); \
523 __event_align = __event_get_align__##_name(_args); \
524 lib_ring_buffer_ctx_init(&ctx, __chan->chan, NULL, __event_len, \
525 __event_align, -1); \
526 __ret = __chan->ops->event_reserve(&ctx, __event->id); \
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
539 /*
540 * Stage 10 of the trace events.
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
554 static int TP_ID(__lttng_events_init__, TRACE_SYSTEM)(void)
555 {
556 wrapper_vmalloc_sync_all();
557 return ltt_probe_register(&TP_ID(__probe_desc___, TRACE_SYSTEM));
558 }
559
560 module_init_eval(__lttng_events_init__, TRACE_SYSTEM);
561
562 static void TP_ID(__lttng_events_exit__, TRACE_SYSTEM)(void)
563 {
564 ltt_probe_unregister(&TP_ID(__probe_desc___, TRACE_SYSTEM));
565 }
566
567 module_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.08462 seconds and 5 git commands to generate.