a739bbc8e20dce85b35fdaa3476ab0f591bc220e
[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 0.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 1 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 { .name = #_item, .type = { .atype = atype_integer, .name = #_type} },
80
81 #undef __field_ext
82 #define __field_ext(_type, _item, _filter_type) __field(_type, _item)
83
84 #undef __array
85 #define __array(_type, _item, _length) \
86 { \
87 .name = #_item, \
88 .type = { \
89 .atype = atype_array, \
90 .name = NULL, \
91 .u.array.elem_type = #_type, \
92 .u.array.length = _length, \
93 }, \
94 },
95
96 #undef __dynamic_array
97 #define __dynamic_array(_type, _item, _length) \
98 { \
99 .name = #_item, \
100 .type = { \
101 .atype = atype_sequence, \
102 .name = NULL, \
103 .u.sequence.elem_type = #_type, \
104 .u.sequence.length_type = "u32", \
105 }, \
106 },
107
108 #undef __string
109 #define __string(_item, _src) \
110 { \
111 .name = #_item, \
112 .type = { \
113 .atype = atype_string, \
114 .name = NULL, \
115 .u.string.encoding = lttng_encode_UTF8, \
116 }, \
117 },
118
119 #undef TP_STRUCT__entry
120 #define TP_STRUCT__entry(args...) args /* Only one used in this phase */
121
122 #undef DECLARE_EVENT_CLASS
123 #define DECLARE_EVENT_CLASS(_name, _proto, _args, _tstruct, _assign, _print) \
124 static const struct lttng_event_field __event_fields___##_name[] = { \
125 _tstruct \
126 };
127
128 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
129
130 /*
131 * Stage 1.1 of the trace events.
132 *
133 * Create probe callback prototypes.
134 */
135
136 #include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
137
138 #undef TP_PROTO
139 #define TP_PROTO(args...) args
140
141 #undef DECLARE_EVENT_CLASS
142 #define DECLARE_EVENT_CLASS(_name, _proto, _args, _tstruct, _assign, _print) \
143 static void __event_probe__##_name(void *__data, _proto);
144
145 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
146
147 /*
148 * Stage 2 of the trace events.
149 *
150 * Create an array of events.
151 */
152
153 /* Named field types must be defined in lttng-types.h */
154
155 #include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
156
157 #undef DEFINE_EVENT
158 #define DEFINE_EVENT(_template, _name, _proto, _args) \
159 { \
160 .fields = __event_fields___##_template, \
161 .name = #_name, \
162 .probe_callback = (void *) &__event_probe__##_template,\
163 .nr_fields = ARRAY_SIZE(__event_fields___##_template), \
164 },
165
166 #define TP_ID1(_token, _system) _token##_system
167 #define TP_ID(_token, _system) TP_ID1(_token, _system)
168
169 static const struct lttng_event_desc TP_ID(__event_desc___, TRACE_SYSTEM)[] = {
170 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
171 };
172
173 #undef TP_ID1
174 #undef TP_ID
175
176
177 /*
178 * Stage 2.1 of the trace events.
179 *
180 * Create a toplevel descriptor for the whole probe.
181 */
182
183 #define TP_ID1(_token, _system) _token##_system
184 #define TP_ID(_token, _system) TP_ID1(_token, _system)
185
186 /* non-const because list head will be modified when registered. */
187 static struct lttng_probe_desc TP_ID(__probe_desc___, TRACE_SYSTEM) = {
188 .event_desc = TP_ID(__event_desc___, TRACE_SYSTEM),
189 .nr_events = ARRAY_SIZE(TP_ID(__event_desc___, TRACE_SYSTEM)),
190 };
191
192 #undef TP_ID1
193 #undef TP_ID
194
195 /*
196 * Stage 3 of the trace events.
197 *
198 * Create seq file metadata output.
199 */
200
201 #define TP_ID1(_token, _system) _token##_system
202 #define TP_ID(_token, _system) TP_ID1(_token, _system)
203
204 static void *TP_ID(__lttng_seq_start__, TRACE_SYSTEM)(struct seq_file *m,
205 loff_t *pos)
206 {
207 const struct lttng_event_desc *desc =
208 &TP_ID(__event_desc___, TRACE_SYSTEM)[*pos];
209
210 if (desc > &TP_ID(__event_desc___, TRACE_SYSTEM)
211 [ARRAY_SIZE(TP_ID(__event_desc___, TRACE_SYSTEM)) - 1])
212 return NULL;
213 return (void *) desc;
214 }
215
216 static void *TP_ID(__lttng_seq_next__, TRACE_SYSTEM)(struct seq_file *m,
217 void *p, loff_t *ppos)
218 {
219 const struct lttng_event_desc *desc =
220 &TP_ID(__event_desc___, TRACE_SYSTEM)[++(*ppos)];
221
222 if (desc > &TP_ID(__event_desc___, TRACE_SYSTEM)
223 [ARRAY_SIZE(TP_ID(__event_desc___, TRACE_SYSTEM)) - 1])
224 return NULL;
225 return (void *) desc;
226 }
227
228 static void TP_ID(__lttng_seq_stop__, TRACE_SYSTEM)(struct seq_file *m,
229 void *p)
230 {
231 }
232
233 static int TP_ID(__lttng_seq_show__, TRACE_SYSTEM)(struct seq_file *m,
234 void *p)
235 {
236 const struct lttng_event_desc *desc = p;
237 int i;
238
239 seq_printf(m, "event {\n"
240 "\tname = %s;\n"
241 "\tid = UNKNOWN;\n"
242 "\tstream = UNKNOWN;\n"
243 "\tfields = {\n",
244 desc->name);
245 for (i = 0; i < desc->nr_fields; i++) {
246 if (desc->fields[i].type.name) /* Named type */
247 seq_printf(m, "\t\t%s",
248 desc->fields[i].type.name);
249 else /* Nameless type */
250 lttng_print_event_type(m, 2, &desc->fields[i].type);
251 seq_printf(m, " %s;\n", desc->fields[i].name);
252 }
253 seq_printf(m, "\t};\n");
254 seq_printf(m, "};\n");
255 return 0;
256 }
257
258 static const
259 struct seq_operations TP_ID(__lttng_types_seq_ops__, TRACE_SYSTEM) = {
260 .start = TP_ID(__lttng_seq_start__, TRACE_SYSTEM),
261 .next = TP_ID(__lttng_seq_next__, TRACE_SYSTEM),
262 .stop = TP_ID(__lttng_seq_stop__, TRACE_SYSTEM),
263 .show = TP_ID(__lttng_seq_show__, TRACE_SYSTEM),
264 };
265
266 static int
267 TP_ID(__lttng_types_open__, TRACE_SYSTEM)(struct inode *inode, struct file *file)
268 {
269 return seq_open(file, &TP_ID(__lttng_types_seq_ops__, TRACE_SYSTEM));
270 }
271
272 static const
273 struct file_operations TP_ID(__lttng_types_fops__, TRACE_SYSTEM) = {
274 .open = TP_ID(__lttng_types_open__, TRACE_SYSTEM),
275 .read = seq_read,
276 .llseek = seq_lseek,
277 .release = seq_release_private,
278 };
279
280 static struct dentry *TP_ID(__lttng_types_dentry__, TRACE_SYSTEM);
281
282 static int TP_ID(__lttng_types_init__, TRACE_SYSTEM)(void)
283 {
284 int ret = 0;
285
286 TP_ID(__lttng_types_dentry__, TRACE_SYSTEM) =
287 debugfs_create_file("lttng-events-" __stringify(TRACE_SYSTEM),
288 S_IWUSR, NULL, NULL,
289 &TP_ID(__lttng_types_fops__, TRACE_SYSTEM));
290 if (IS_ERR(TP_ID(__lttng_types_dentry__, TRACE_SYSTEM))
291 || !TP_ID(__lttng_types_dentry__, TRACE_SYSTEM)) {
292 printk(KERN_ERR "Error creating LTTng type export file\n");
293 ret = -ENOMEM;
294 goto error;
295 }
296 error:
297 return ret;
298 }
299
300 static void TP_ID(__lttng_types_exit__, TRACE_SYSTEM)(void)
301 {
302 debugfs_remove(TP_ID(__lttng_types_dentry__, TRACE_SYSTEM));
303 }
304
305 #undef TP_ID1
306 #undef TP_ID
307
308 /*
309 * Stage 4 of the trace events.
310 *
311 * Create static inline function that calculates event size.
312 */
313
314 #include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
315
316 /* Named field types must be defined in lttng-types.h */
317
318 #undef __field
319 #define __field(_type, _item) \
320 __event_len += lib_ring_buffer_align(__event_len, __alignof__(_type)); \
321 __event_len += sizeof(_type);
322
323 #undef __field_ext
324 #define __field_ext(_type, _item, _filter_type) __field(_type, _item)
325
326 #undef __array
327 #define __array(_type, _item, _length) \
328 __event_len += lib_ring_buffer_align(__event_len, __alignof__(_type)); \
329 __event_len += sizeof(_type) * (_length);
330
331 #undef __dynamic_array
332 #define __dynamic_array(_type, _item, _length) \
333 __event_len += lib_ring_buffer_align(__event_len, __alignof__(u32)); \
334 __event_len += sizeof(u32); \
335 __event_len += lib_ring_buffer_align(__event_len, __alignof__(_type)); \
336 __event_len += sizeof(_type) * (_length);
337
338 #undef __string
339 #define __string(_item, _src) \
340 __event_len += __dynamic_len[__dynamic_len_idx++] = strlen(_src) + 1;
341
342 #undef TP_PROTO
343 #define TP_PROTO(args...) args
344
345 #undef TP_STRUCT__entry
346 #define TP_STRUCT__entry(args...) args
347
348 #undef DECLARE_EVENT_CLASS
349 #define DECLARE_EVENT_CLASS(_name, _proto, _args, _tstruct, _assign, _print) \
350 static inline size_t __event_get_size__##_name(size_t *__dynamic_len, _proto) \
351 { \
352 size_t __event_len = 0; \
353 unsigned int __dynamic_len_idx = 0; \
354 \
355 if (0) \
356 (void) __dynamic_len_idx; /* don't warn if unused */ \
357 _tstruct \
358 return __event_len; \
359 }
360
361 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
362
363
364
365 /*
366 * Stage 5 of the trace events.
367 *
368 * Create static inline function that calculates event payload alignment.
369 */
370
371 #include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
372
373 /* Named field types must be defined in lttng-types.h */
374
375 #undef __field
376 #define __field(_type, _item) \
377 __event_align = max_t(size_t, __event_align, __alignof__(_type));
378
379 #undef __field_ext
380 #define __field_ext(_type, _item, _filter_type) __field(_type, _item)
381
382 #undef __array
383 #define __array(_type, _item, _length) \
384 __event_align = max_t(size_t, __event_align, __alignof__(_type));
385
386 #undef __dynamic_array
387 #define __dynamic_array(_type, _item, _length) \
388 __event_align = max_t(size_t, __event_align, __alignof__(u32)); \
389 __event_align = max_t(size_t, __event_align, __alignof__(_type));
390
391 #undef __string
392 #define __string(_item, _src)
393
394 #undef TP_PROTO
395 #define TP_PROTO(args...) args
396
397 #undef TP_STRUCT__entry
398 #define TP_STRUCT__entry(args...) args
399
400 #undef DECLARE_EVENT_CLASS
401 #define DECLARE_EVENT_CLASS(_name, _proto, _args, _tstruct, _assign, _print) \
402 static inline size_t __event_get_align__##_name(_proto) \
403 { \
404 size_t __event_align = 1; \
405 _tstruct \
406 return __event_align; \
407 }
408
409 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
410
411
412 /*
413 * Stage 6 of the trace events.
414 *
415 * Create structure declaration that allows the "assign" macros to access the
416 * field types.
417 */
418
419 #include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
420
421 /* Named field types must be defined in lttng-types.h */
422
423 #undef __field
424 #define __field(_type, _item) _type _item;
425
426 #undef __field_ext
427 #define __field_ext(_type, _item, _filter_type) __field(_type, _item)
428
429 #undef __array
430 #define __array(_type, _item, _length) _type _item;
431
432 #undef __dynamic_array
433 #define __dynamic_array(_type, _item, _length) _type _item;
434
435 #undef __string
436 #define __string(_item, _src) char _item;
437
438 #undef TP_STRUCT__entry
439 #define TP_STRUCT__entry(args...) args
440
441 #undef DECLARE_EVENT_CLASS
442 #define DECLARE_EVENT_CLASS(_name, _proto, _args, _tstruct, _assign, _print) \
443 struct __event_typemap__##_name { \
444 _tstruct \
445 };
446
447 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
448
449
450 /*
451 * Stage 7 of the trace events.
452 *
453 * Create the probe function : call even size calculation and write event data
454 * into the buffer.
455 *
456 * We use both the field and assignment macros to write the fields in the order
457 * defined in the field declaration. The field declarations control the
458 * execution order, jumping to the appropriate assignment block.
459 */
460
461 #include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
462
463 #undef __field
464 #define __field(_type, _item) \
465 goto __assign_##_item; \
466 __end_field_##_item:
467
468 #undef __field_ext
469 #define __field_ext(_type, _item, _filter_type) __field(_type, _item)
470
471 #undef __array
472 #define __array(_type, _item, _length) \
473 goto __assign_##_item; \
474 __end_field_##_item:
475
476 #undef __dynamic_array
477 #define __dynamic_array(_type, _item, _length) \
478 goto __assign_##_item##_1; \
479 __end_field_##_item##_1: \
480 goto __assign_##_item##_2; \
481 __end_field_##_item##_2:
482
483 #undef __string
484 #define __string(_item, _src) \
485 goto __assign_##_item; \
486 __end_field_##_item:
487
488 /*
489 * Macros mapping tp_assign() to "=", tp_memcpy() to memcpy() and tp_strcpy() to
490 * strcpy().
491 */
492 #undef tp_assign
493 #define tp_assign(dest, src) \
494 __assign_##dest: \
495 { \
496 __typeof__(__typemap.dest) __tmp = (src); \
497 lib_ring_buffer_align_ctx(&ctx, __alignof__(__tmp)); \
498 __chan->ops->event_write(&ctx, &__tmp, sizeof(__tmp)); \
499 } \
500 goto __end_field_##dest;
501
502 #undef tp_memcpy
503 #define tp_memcpy(dest, src, len) \
504 __assign_##dest: \
505 lib_ring_buffer_align_ctx(&ctx, __alignof__(__typemap.dest)); \
506 __chan->ops->event_write(&ctx, src, len); \
507 goto __end_field_##dest;
508
509 #undef tp_memcpy_dyn
510 #define tp_memcpy_dyn(dest, src, len) \
511 __assign_##dest##_1: \
512 { \
513 u32 __tmpl = (len); \
514 lib_ring_buffer_align_ctx(&ctx, __alignof__(u32)); \
515 __chan->ops->event_write(&ctx, &__tmpl, sizeof(u32)); \
516 } \
517 goto __end_field_##dest##_1; \
518 __assign_##dest##_2: \
519 lib_ring_buffer_align_ctx(&ctx, __alignof__(__typemap.dest)); \
520 __chan->ops->event_write(&ctx, src, len); \
521 goto __end_field_##dest##_2;
522
523 #undef tp_strcpy
524 #define tp_strcpy(dest, src) \
525 tp_memcpy(dest, src, __get_dynamic_array_len(dest))
526
527 /* Named field types must be defined in lttng-types.h */
528
529 #undef __get_str
530 #define __get_str(field) field
531
532 #undef __get_dynamic_array
533 #define __get_dynamic_array(field) field
534
535 /* Beware: this get len actually consumes the len value */
536 #undef __get_dynamic_array_len
537 #define __get_dynamic_array_len(field) __dynamic_len[__dynamic_len_idx++]
538
539 #undef TP_PROTO
540 #define TP_PROTO(args...) args
541
542 #undef TP_ARGS
543 #define TP_ARGS(args...) args
544
545 #undef TP_STRUCT__entry
546 #define TP_STRUCT__entry(args...) args
547
548 #undef TP_fast_assign
549 #define TP_fast_assign(args...) args
550
551 #undef DECLARE_EVENT_CLASS
552 #define DECLARE_EVENT_CLASS(_name, _proto, _args, _tstruct, _assign, _print) \
553 static void __event_probe__##_name(void *__data, _proto) \
554 { \
555 struct ltt_event *__event = __data; \
556 struct ltt_channel *__chan = __event->chan; \
557 struct lib_ring_buffer_ctx ctx; \
558 size_t __event_len, __event_align; \
559 size_t __dynamic_len_idx = 0; \
560 size_t __dynamic_len[ARRAY_SIZE(__event_fields___##_name)]; \
561 struct __event_typemap__##_name __typemap; \
562 int __ret; \
563 \
564 if (0) \
565 (void) __dynamic_len_idx; /* don't warn if unused */ \
566 if (!ACCESS_ONCE(__chan->session->active)) \
567 return; \
568 __event_len = __event_get_size__##_name(__dynamic_len, _args); \
569 __event_align = __event_get_align__##_name(_args); \
570 lib_ring_buffer_ctx_init(&ctx, __chan->chan, NULL, __event_len, \
571 __event_align, -1); \
572 __ret = __chan->ops->event_reserve(&ctx); \
573 if (__ret < 0) \
574 return; \
575 /* Control code (field ordering) */ \
576 _tstruct \
577 __chan->ops->event_commit(&ctx); \
578 return; \
579 /* Copy code, steered by control code */ \
580 _assign \
581 }
582
583 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
584
585 /*
586 * Stage 8 of the trace events.
587 *
588 * Register/unregister probes at module load/unload.
589 */
590
591 #include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
592
593 #define TP_ID1(_token, _system) _token##_system
594 #define TP_ID(_token, _system) TP_ID1(_token, _system)
595 #define module_init_eval1(_token, _system) module_init(_token##_system)
596 #define module_init_eval(_token, _system) module_init_eval1(_token, _system)
597 #define module_exit_eval1(_token, _system) module_exit(_token##_system)
598 #define module_exit_eval(_token, _system) module_exit_eval1(_token, _system)
599
600 static int TP_ID(__lttng_events_init__, TRACE_SYSTEM)(void)
601 {
602 int ret;
603
604 wrapper_vmalloc_sync_all();
605 ret = TP_ID(__lttng_types_init__, TRACE_SYSTEM)();
606 if (ret)
607 return ret;
608 return ltt_probe_register(&TP_ID(__probe_desc___, TRACE_SYSTEM));
609 }
610
611 module_init_eval(__lttng_events_init__, TRACE_SYSTEM);
612
613 static void TP_ID(__lttng_events_exit__, TRACE_SYSTEM)(void)
614 {
615 TP_ID(__lttng_types_exit__, TRACE_SYSTEM)();
616 ltt_probe_unregister(&TP_ID(__probe_desc___, TRACE_SYSTEM));
617 }
618
619 module_exit_eval(__lttng_events_exit__, TRACE_SYSTEM);
620
621 #undef module_init_eval
622 #undef module_exit_eval
623 #undef TP_ID1
624 #undef TP_ID
This page took 0.045597 seconds and 4 git commands to generate.