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