update alignment computation
[deliverable/lttng-modules.git] / probes / lttng-events.h
1 #include <lttng.h>
2 #include <lttng-types.h>
3 #include <linux/debugfs.h>
4 #include <linux/ringbuffer/frontend_types.h>
5 #include "../ltt-events.h"
6 #include "../ltt-tracer-core.h"
7
8 struct lttng_event_field {
9 const char *name;
10 const struct lttng_type type;
11 };
12
13 struct lttng_event_desc {
14 const struct lttng_event_field *fields;
15 const char *name;
16 unsigned int nr_fields;
17 };
18
19 /*
20 * Macro declarations used for all stages.
21 */
22
23 /*
24 * DECLARE_EVENT_CLASS can be used to add a generic function
25 * handlers for events. That is, if all events have the same
26 * parameters and just have distinct trace points.
27 * Each tracepoint can be defined with DEFINE_EVENT and that
28 * will map the DECLARE_EVENT_CLASS to the tracepoint.
29 *
30 * TRACE_EVENT is a one to one mapping between tracepoint and template.
31 */
32
33 #undef TRACE_EVENT
34 #define TRACE_EVENT(name, proto, args, tstruct, assign, print) \
35 DECLARE_EVENT_CLASS(name, \
36 PARAMS(proto), \
37 PARAMS(args), \
38 PARAMS(tstruct), \
39 PARAMS(assign), \
40 PARAMS(print)) \
41 DEFINE_EVENT(name, name, PARAMS(proto), PARAMS(args))
42
43 #undef DEFINE_EVENT_PRINT
44 #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
45 DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
46
47 /* Callbacks are meaningless to LTTng. */
48 #undef TRACE_EVENT_FN
49 #define TRACE_EVENT_FN(name, proto, args, tstruct, \
50 assign, print, reg, unreg) \
51 TRACE_EVENT(name, PARAMS(proto), PARAMS(args), \
52 PARAMS(tstruct), PARAMS(assign), PARAMS(print)) \
53
54 /*
55 * Stage 1 of the trace events.
56 *
57 * Create event field type metadata section.
58 * Each event produce an array of fields.
59 */
60
61 #include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
62
63 /* Named field types must be defined in lttng-types.h */
64
65 #undef __field
66 #define __field(_type, _item) \
67 { .name = #_item, .type = { .atype = atype_integer, .name = #_type} },
68
69 #undef __field_ext
70 #define __field_ext(_type, _item, _filter_type) __field(_type, _item)
71
72 #undef __array
73 #define __array(_type, _item, _length) \
74 { \
75 .name = #_item, \
76 .type = { \
77 .atype = atype_array, \
78 .name = NULL, \
79 .u.array.elem_type = #_type, \
80 .u.array.length = _length, \
81 }, \
82 },
83
84 #undef __dynamic_array
85 #define __dynamic_array(_type, _item, _length) \
86 { \
87 .name = #_item, \
88 .type = { \
89 .atype = atype_sequence, \
90 .name = NULL, \
91 .u.sequence.elem_type = #_type, \
92 .u.sequence.length_type = "u32", \
93 }, \
94 },
95
96 #undef __string
97 #define __string(_item, _src) \
98 { \
99 .name = #_item, \
100 .type = { \
101 .atype = atype_string, \
102 .name = NULL, \
103 .u.string.encoding = lttng_encode_UTF8, \
104 }, \
105 },
106
107 #undef TP_STRUCT__entry
108 #define TP_STRUCT__entry(args...) args /* Only one used in this phase */
109
110 #undef DECLARE_EVENT_CLASS
111 #define DECLARE_EVENT_CLASS(_name, _proto, _args, _tstruct, _assign, _print) \
112 static const struct lttng_event_field __event_fields___##_name[] = { \
113 _tstruct \
114 };
115
116 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
117
118 /*
119 * Stage 2 of the trace events.
120 *
121 * Create an array of events.
122 */
123
124 /* Named field types must be defined in lttng-types.h */
125
126 #include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
127
128 #undef DEFINE_EVENT
129 #define DEFINE_EVENT(_template, _name, _proto, _args) \
130 { \
131 .fields = __event_fields___##_template, \
132 .name = #_name, \
133 .nr_fields = ARRAY_SIZE(__event_fields___##_template), \
134 },
135
136 #define TP_ID1(_token, _system) _token##_system
137 #define TP_ID(_token, _system) TP_ID1(_token, _system)
138
139 static const struct lttng_event_desc TP_ID(__event_desc___, TRACE_SYSTEM)[] = {
140 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
141 };
142
143 #undef TP_ID1
144 #undef TP_ID
145
146 /*
147 * Stage 3 of the trace events.
148 *
149 * Create seq file metadata output.
150 */
151
152 #define TP_ID1(_token, _system) _token##_system
153 #define TP_ID(_token, _system) TP_ID1(_token, _system)
154 #define module_init_eval1(_token, _system) module_init(_token##_system)
155 #define module_init_eval(_token, _system) module_init_eval1(_token, _system)
156 #define module_exit_eval1(_token, _system) module_exit(_token##_system)
157 #define module_exit_eval(_token, _system) module_exit_eval1(_token, _system)
158
159 static void *TP_ID(__lttng_seq_start__, TRACE_SYSTEM)(struct seq_file *m,
160 loff_t *pos)
161 {
162 const struct lttng_event_desc *desc =
163 &TP_ID(__event_desc___, TRACE_SYSTEM)[*pos];
164
165 if (desc > &TP_ID(__event_desc___, TRACE_SYSTEM)
166 [ARRAY_SIZE(TP_ID(__event_desc___, TRACE_SYSTEM)) - 1])
167 return NULL;
168 return (void *) desc;
169 }
170
171 static void *TP_ID(__lttng_seq_next__, TRACE_SYSTEM)(struct seq_file *m,
172 void *p, loff_t *ppos)
173 {
174 const struct lttng_event_desc *desc =
175 &TP_ID(__event_desc___, TRACE_SYSTEM)[++(*ppos)];
176
177 if (desc > &TP_ID(__event_desc___, TRACE_SYSTEM)
178 [ARRAY_SIZE(TP_ID(__event_desc___, TRACE_SYSTEM)) - 1])
179 return NULL;
180 return (void *) desc;
181 }
182
183 static void TP_ID(__lttng_seq_stop__, TRACE_SYSTEM)(struct seq_file *m,
184 void *p)
185 {
186 }
187
188 static int TP_ID(__lttng_seq_show__, TRACE_SYSTEM)(struct seq_file *m,
189 void *p)
190 {
191 const struct lttng_event_desc *desc = p;
192 int i;
193
194 seq_printf(m, "event {\n"
195 "\tname = %s;\n"
196 "\tid = UNKNOWN;\n"
197 "\tstream = UNKNOWN;\n"
198 "\tfields = {\n",
199 desc->name);
200 for (i = 0; i < desc->nr_fields; i++) {
201 if (desc->fields[i].type.name) /* Named type */
202 seq_printf(m, "\t\t%s",
203 desc->fields[i].type.name);
204 else /* Nameless type */
205 lttng_print_event_type(m, 2, &desc->fields[i].type);
206 seq_printf(m, " %s;\n", desc->fields[i].name);
207 }
208 seq_printf(m, "\t};\n");
209 seq_printf(m, "};\n");
210 return 0;
211 }
212
213 static const
214 struct seq_operations TP_ID(__lttng_types_seq_ops__, TRACE_SYSTEM) = {
215 .start = TP_ID(__lttng_seq_start__, TRACE_SYSTEM),
216 .next = TP_ID(__lttng_seq_next__, TRACE_SYSTEM),
217 .stop = TP_ID(__lttng_seq_stop__, TRACE_SYSTEM),
218 .show = TP_ID(__lttng_seq_show__, TRACE_SYSTEM),
219 };
220
221 static int
222 TP_ID(__lttng_types_open__, TRACE_SYSTEM)(struct inode *inode, struct file *file)
223 {
224 return seq_open(file, &TP_ID(__lttng_types_seq_ops__, TRACE_SYSTEM));
225 }
226
227 static const
228 struct file_operations TP_ID(__lttng_types_fops__, TRACE_SYSTEM) = {
229 .open = TP_ID(__lttng_types_open__, TRACE_SYSTEM),
230 .read = seq_read,
231 .llseek = seq_lseek,
232 .release = seq_release_private,
233 };
234
235 static struct dentry *TP_ID(__lttng_types_dentry__, TRACE_SYSTEM);
236
237 static int TP_ID(__lttng_types_init__, TRACE_SYSTEM)(void)
238 {
239 int ret = 0;
240
241 TP_ID(__lttng_types_dentry__, TRACE_SYSTEM) =
242 debugfs_create_file("lttng-events-" __stringify(TRACE_SYSTEM),
243 S_IWUSR, NULL, NULL,
244 &TP_ID(__lttng_types_fops__, TRACE_SYSTEM));
245 if (IS_ERR(TP_ID(__lttng_types_dentry__, TRACE_SYSTEM))
246 || !TP_ID(__lttng_types_dentry__, TRACE_SYSTEM)) {
247 printk(KERN_ERR "Error creating LTTng type export file\n");
248 ret = -ENOMEM;
249 goto error;
250 }
251 error:
252 return ret;
253 }
254
255 module_init_eval(__lttng_types_init__, TRACE_SYSTEM);
256
257 static void TP_ID(__lttng_types_exit__, TRACE_SYSTEM)(void)
258 {
259 debugfs_remove(TP_ID(__lttng_types_dentry__, TRACE_SYSTEM));
260 }
261
262 module_exit_eval(__lttng_types_exit__, TRACE_SYSTEM);
263
264 #undef module_init_eval
265 #undef module_exit_eval
266 #undef TP_ID1
267 #undef TP_ID
268
269 /*
270 * Stage 4 of the trace events.
271 *
272 * Create static inline function that calculates event size.
273 */
274
275 #include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
276
277 /* Named field types must be defined in lttng-types.h */
278
279 #undef __field
280 #define __field(_type, _item) \
281 __event_len += lib_ring_buffer_align(__event_len, sizeof(_type)); \
282 __event_len += sizeof(_type);
283
284 #undef __field_ext
285 #define __field_ext(_type, _item, _filter_type) __field(_type, _item)
286
287 #undef __array
288 #define __array(_type, _item, _length) \
289 __event_len += lib_ring_buffer_align(__event_len, sizeof(_type)); \
290 __event_len += sizeof(_type) * (_length);
291
292 #undef __dynamic_array
293 #define __dynamic_array(_type, _item, _length) \
294 __event_len += lib_ring_buffer_align(__event_len, sizeof(u32)); \
295 __event_len += sizeof(u32); \
296 __event_len += lib_ring_buffer_align(__event_len, sizeof(_type)); \
297 __event_len += sizeof(_type) * (_length);
298
299 #undef __string
300 #define __string(_item, _src) \
301 __event_len += __dynamic_len[__dynamic_len_idx++] = strlen(_src) + 1;
302
303 #undef TP_PROTO
304 #define TP_PROTO(args...) args
305
306 #undef TP_STRUCT__entry
307 #define TP_STRUCT__entry(args...) args
308
309 #undef DECLARE_EVENT_CLASS
310 #define DECLARE_EVENT_CLASS(_name, _proto, _args, _tstruct, _assign, _print) \
311 static inline size_t __event_get_size__##_name(size_t *__dynamic_len, _proto) \
312 { \
313 size_t __event_len = 0; \
314 unsigned int __dynamic_len_idx = 0; \
315 \
316 if (0) \
317 (void) __dynamic_len_idx; /* don't warn if unused */ \
318 _tstruct \
319 return __event_len; \
320 }
321
322 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
323
324
325
326 /*
327 * Stage 5 of the trace events.
328 *
329 * Create static inline function that calculates event payload alignment.
330 */
331
332 #include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
333
334 /* Named field types must be defined in lttng-types.h */
335
336 #undef __field
337 #define __field(_type, _item) \
338 __event_align = max_t(size_t, __event_align, __alignof__(_type));
339
340 #undef __field_ext
341 #define __field_ext(_type, _item, _filter_type) __field(_type, _item)
342
343 #undef __array
344 #define __array(_type, _item, _length) \
345 __event_align = max_t(size_t, __event_align, __alignof__(_type));
346
347 #undef __dynamic_array
348 #define __dynamic_array(_type, _item, _length) \
349 __event_align = max_t(size_t, __event_align, __alignof__(u32)); \
350 __event_align = max_t(size_t, __event_align, __alignof__(_type));
351
352 #undef __string
353 #define __string(_item, _src)
354
355 #undef TP_PROTO
356 #define TP_PROTO(args...) args
357
358 #undef TP_STRUCT__entry
359 #define TP_STRUCT__entry(args...) args
360
361 #undef DECLARE_EVENT_CLASS
362 #define DECLARE_EVENT_CLASS(_name, _proto, _args, _tstruct, _assign, _print) \
363 static inline size_t __event_get_align__##_name(_proto) \
364 { \
365 size_t __event_align = 1; \
366 _tstruct \
367 return __event_align; \
368 }
369
370 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
371
372
373
374 /*
375 * Stage 6 of the trace events.
376 *
377 * Create the probe function : call even size calculation and write event data
378 * into the buffer.
379 *
380 * We use both the field and assignment macros to write the fields in the order
381 * defined in the field declaration. The field declarations control the
382 * execution order, jumping to the appropriate assignment block.
383 */
384
385 #include "lttng-events-reset.h" /* Reset all macros within TRACE_EVENT */
386
387 #undef __field
388 #define __field(_type, _item) \
389 lib_ring_buffer_align_ctx(&ctx, sizeof(_type)); \
390 goto __assign_##_item; \
391 __end_field_##_item:
392
393 #undef __field_ext
394 #define __field_ext(_type, _item, _filter_type) __field(_type, _item)
395
396 #undef __array
397 #define __array(_type, _item, _length) \
398 lib_ring_buffer_align_ctx(&ctx, sizeof(_type)); \
399 goto __assign_##_item; \
400 __end_field_##_item:
401
402 #undef __dynamic_array
403 #define __dynamic_array(_type, _item, _length) \
404 lib_ring_buffer_align_ctx(&ctx, sizeof(u32)); \
405 goto __assign_##_item##_1; \
406 __end_field_##_item##_1: \
407 lib_ring_buffer_align_ctx(&ctx, sizeof(_type)); \
408 goto __assign_##_item##_2; \
409 __end_field_##_item##_2:
410
411 #undef __string
412 #define __string(_item, _src) \
413 goto __assign_##_item; \
414 __end_field_##_item:
415
416 /*
417 * Macros mapping tp_assign() to "=", tp_memcpy() to memcpy() and tp_strcpy() to
418 * strcpy().
419 */
420 #undef tp_assign
421 #define tp_assign(dest, src) \
422 __assign_##dest: \
423 { \
424 __typeof__(src) __tmp = (src); \
425 __chan->ops->event_write(&ctx, &__tmp, sizeof(src)); \
426 } \
427 goto __end_field_##dest;
428
429 #undef tp_memcpy
430 #define tp_memcpy(dest, src, len) \
431 __assign_##dest: \
432 __chan->ops->event_write(&ctx, src, len); \
433 goto __end_field_##dest;
434
435 #undef tp_memcpy_dyn
436 #define tp_memcpy_dyn(dest, src, len) \
437 __assign_##dest##_1: \
438 { \
439 __typeof__(len) __tmpl = (len); \
440 __chan->ops->event_write(&ctx, &__tmpl, sizeof(u32)); \
441 } \
442 goto __end_field_##dest##_1; \
443 __assign_##dest##_2: \
444 __chan->ops->event_write(&ctx, src, len); \
445 goto __end_field_##dest##_2;
446
447 #undef tp_strcpy
448 #define tp_strcpy(dest, src) \
449 tp_memcpy(dest, src, __get_dynamic_array_len(dest)); \
450
451 /* Named field types must be defined in lttng-types.h */
452
453 #undef __get_str
454 #define __get_str(field) field
455
456 #undef __get_dynamic_array
457 #define __get_dynamic_array(field) field
458
459 /* Beware: this get len actually consumes the len value */
460 #undef __get_dynamic_array_len
461 #define __get_dynamic_array_len(field) __dynamic_len[__dynamic_len_idx++]
462
463 #undef TP_PROTO
464 #define TP_PROTO(args...) args
465
466 #undef TP_ARGS
467 #define TP_ARGS(args...) args
468
469 #undef TP_STRUCT__entry
470 #define TP_STRUCT__entry(args...) args
471
472 #undef TP_fast_assign
473 #define TP_fast_assign(args...) args
474
475 #undef DECLARE_EVENT_CLASS
476 #define DECLARE_EVENT_CLASS(_name, _proto, _args, _tstruct, _assign, _print) \
477 static void __event_probe__##_name(void *__data, _proto) \
478 { \
479 struct ltt_event *__event = __data; \
480 struct ltt_channel *__chan = __event->chan; \
481 struct lib_ring_buffer_ctx ctx; \
482 size_t __event_len, __event_align; \
483 size_t __dynamic_len_idx = 0; \
484 size_t __dynamic_len[ARRAY_SIZE(__event_fields___##_name)]; \
485 int __ret; \
486 \
487 if (0) \
488 (void) __dynamic_len_idx; /* don't warn if unused */ \
489 __event_len = __event_get_size__##_name(__dynamic_len, _args); \
490 __event_align = __event_get_align__##_name(_args); \
491 lib_ring_buffer_ctx_init(&ctx, __chan->chan, NULL, __event_len, \
492 __event_align, -1); \
493 __ret = __chan->ops->event_reserve(&ctx); \
494 if (__ret < 0) \
495 return; \
496 /* Control code (field ordering) */ \
497 _tstruct \
498 __chan->ops->event_commit(&ctx); \
499 return; \
500 /* Copy code, steered by control code */ \
501 _assign \
502 }
503
504 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
505
506
507 #if 0
508
509 #include <linux/ftrace_event.h>
510
511 /*
512 * DECLARE_EVENT_CLASS can be used to add a generic function
513 * handlers for events. That is, if all events have the same
514 * parameters and just have distinct trace points.
515 * Each tracepoint can be defined with DEFINE_EVENT and that
516 * will map the DECLARE_EVENT_CLASS to the tracepoint.
517 *
518 * TRACE_EVENT is a one to one mapping between tracepoint and template.
519 */
520 #undef TRACE_EVENT
521 #define TRACE_EVENT(name, proto, args, tstruct, assign, print) \
522 DECLARE_EVENT_CLASS(name, \
523 PARAMS(proto), \
524 PARAMS(args), \
525 PARAMS(tstruct), \
526 PARAMS(assign), \
527 PARAMS(print)); \
528 DEFINE_EVENT(name, name, PARAMS(proto), PARAMS(args));
529
530
531 #undef __field
532 #define __field(type, item) type item;
533
534 #undef __field_ext
535 #define __field_ext(type, item, filter_type) type item;
536
537 #undef __array
538 #define __array(type, item, len) type item[len];
539
540 #undef __dynamic_array
541 #define __dynamic_array(type, item, len) u32 __data_loc_##item;
542
543 #undef __string
544 #define __string(item, src) __dynamic_array(char, item, -1)
545
546 #undef TP_STRUCT__entry
547 #define TP_STRUCT__entry(args...) args
548
549 #undef DECLARE_EVENT_CLASS
550 #define DECLARE_EVENT_CLASS(name, proto, args, tstruct, assign, print) \
551 struct ftrace_raw_##name { \
552 struct trace_entry ent; \
553 tstruct \
554 char __data[0]; \
555 }; \
556 \
557 static struct ftrace_event_class event_class_##name;
558
559 #undef DEFINE_EVENT
560 #define DEFINE_EVENT(template, name, proto, args) \
561 static struct ftrace_event_call __used \
562 __attribute__((__aligned__(4))) event_##name
563
564 #undef DEFINE_EVENT_PRINT
565 #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
566 DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
567
568 /* Callbacks are meaningless to ftrace. */
569 #undef TRACE_EVENT_FN
570 #define TRACE_EVENT_FN(name, proto, args, tstruct, \
571 assign, print, reg, unreg) \
572 TRACE_EVENT(name, PARAMS(proto), PARAMS(args), \
573 PARAMS(tstruct), PARAMS(assign), PARAMS(print)) \
574
575 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
576
577
578 /*
579 * Stage 2 of the trace events.
580 *
581 * Create static inline function that calculates event size.
582 */
583
584 #undef __field
585 #define __field(type, item)
586
587 #undef __field_ext
588 #define __field_ext(type, item, filter_type)
589
590 #undef __array
591 #define __array(type, item, len)
592
593 #undef __dynamic_array
594 #define __dynamic_array(type, item, len) u32 item;
595
596 #undef __string
597 #define __string(item, src) __dynamic_array(char, item, -1)
598
599 #undef DECLARE_EVENT_CLASS
600 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
601 struct ftrace_data_offsets_##call { \
602 tstruct; \
603 };
604
605 #undef DEFINE_EVENT
606 #define DEFINE_EVENT(template, name, proto, args)
607
608 #undef DEFINE_EVENT_PRINT
609 #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
610 DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
611
612 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
613
614 /*
615 * Stage 3 of the trace events.
616 *
617 * Create the probe function : call even size calculation and write event data
618 * into the buffer.
619 */
620
621 #undef __entry
622 #define __entry field
623
624 #undef TP_printk
625 #define TP_printk(fmt, args...) fmt "\n", args
626
627 #undef __get_dynamic_array
628 #define __get_dynamic_array(field) \
629 ((void *)__entry + (__entry->__data_loc_##field & 0xffff))
630
631 #undef __get_str
632 #define __get_str(field) (char *)__get_dynamic_array(field)
633
634 #undef __print_flags
635 #define __print_flags(flag, delim, flag_array...) \
636 ({ \
637 static const struct trace_print_flags __flags[] = \
638 { flag_array, { -1, NULL }}; \
639 ftrace_print_flags_seq(p, delim, flag, __flags); \
640 })
641
642 #undef __print_symbolic
643 #define __print_symbolic(value, symbol_array...) \
644 ({ \
645 static const struct trace_print_flags symbols[] = \
646 { symbol_array, { -1, NULL }}; \
647 ftrace_print_symbols_seq(p, value, symbols); \
648 })
649
650 #undef __print_hex
651 #define __print_hex(buf, buf_len) ftrace_print_hex_seq(p, buf, buf_len)
652
653 #undef DECLARE_EVENT_CLASS
654 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
655 static notrace enum print_line_t \
656 ftrace_raw_output_##call(struct trace_iterator *iter, int flags, \
657 struct trace_event *trace_event) \
658 { \
659 struct ftrace_event_call *event; \
660 struct trace_seq *s = &iter->seq; \
661 struct ftrace_raw_##call *field; \
662 struct trace_entry *entry; \
663 struct trace_seq *p = &iter->tmp_seq; \
664 int ret; \
665 \
666 event = container_of(trace_event, struct ftrace_event_call, \
667 event); \
668 \
669 entry = iter->ent; \
670 \
671 if (entry->type != event->event.type) { \
672 WARN_ON_ONCE(1); \
673 return TRACE_TYPE_UNHANDLED; \
674 } \
675 \
676 field = (typeof(field))entry; \
677 \
678 trace_seq_init(p); \
679 ret = trace_seq_printf(s, "%s: ", event->name); \
680 if (ret) \
681 ret = trace_seq_printf(s, print); \
682 if (!ret) \
683 return TRACE_TYPE_PARTIAL_LINE; \
684 \
685 return TRACE_TYPE_HANDLED; \
686 } \
687 static struct trace_event_functions ftrace_event_type_funcs_##call = { \
688 .trace = ftrace_raw_output_##call, \
689 };
690
691 #undef DEFINE_EVENT_PRINT
692 #define DEFINE_EVENT_PRINT(template, call, proto, args, print) \
693 static notrace enum print_line_t \
694 ftrace_raw_output_##call(struct trace_iterator *iter, int flags, \
695 struct trace_event *event) \
696 { \
697 struct trace_seq *s = &iter->seq; \
698 struct ftrace_raw_##template *field; \
699 struct trace_entry *entry; \
700 struct trace_seq *p = &iter->tmp_seq; \
701 int ret; \
702 \
703 entry = iter->ent; \
704 \
705 if (entry->type != event_##call.event.type) { \
706 WARN_ON_ONCE(1); \
707 return TRACE_TYPE_UNHANDLED; \
708 } \
709 \
710 field = (typeof(field))entry; \
711 \
712 trace_seq_init(p); \
713 ret = trace_seq_printf(s, "%s: ", #call); \
714 if (ret) \
715 ret = trace_seq_printf(s, print); \
716 if (!ret) \
717 return TRACE_TYPE_PARTIAL_LINE; \
718 \
719 return TRACE_TYPE_HANDLED; \
720 } \
721 static struct trace_event_functions ftrace_event_type_funcs_##call = { \
722 .trace = ftrace_raw_output_##call, \
723 };
724
725 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
726
727 #undef __field_ext
728 #define __field_ext(type, item, filter_type) \
729 ret = trace_define_field(event_call, #type, #item, \
730 offsetof(typeof(field), item), \
731 sizeof(field.item), \
732 is_signed_type(type), filter_type); \
733 if (ret) \
734 return ret;
735
736 #undef __field
737 #define __field(type, item) __field_ext(type, item, FILTER_OTHER)
738
739 #undef __array
740 #define __array(type, item, len) \
741 BUILD_BUG_ON(len > MAX_FILTER_STR_VAL); \
742 ret = trace_define_field(event_call, #type "[" #len "]", #item, \
743 offsetof(typeof(field), item), \
744 sizeof(field.item), \
745 is_signed_type(type), FILTER_OTHER); \
746 if (ret) \
747 return ret;
748
749 #undef __dynamic_array
750 #define __dynamic_array(type, item, len) \
751 ret = trace_define_field(event_call, "__data_loc " #type "[]", #item, \
752 offsetof(typeof(field), __data_loc_##item), \
753 sizeof(field.__data_loc_##item), \
754 is_signed_type(type), FILTER_OTHER);
755
756 #undef __string
757 #define __string(item, src) __dynamic_array(char, item, -1)
758
759 #undef DECLARE_EVENT_CLASS
760 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, func, print) \
761 static int notrace \
762 ftrace_define_fields_##call(struct ftrace_event_call *event_call) \
763 { \
764 struct ftrace_raw_##call field; \
765 int ret; \
766 \
767 tstruct; \
768 \
769 return ret; \
770 }
771
772 #undef DEFINE_EVENT
773 #define DEFINE_EVENT(template, name, proto, args)
774
775 #undef DEFINE_EVENT_PRINT
776 #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
777 DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
778
779 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
780
781 /*
782 * remember the offset of each array from the beginning of the event.
783 */
784
785 #undef __entry
786 #define __entry entry
787
788 #undef __field
789 #define __field(type, item)
790
791 #undef __field_ext
792 #define __field_ext(type, item, filter_type)
793
794 #undef __array
795 #define __array(type, item, len)
796
797 #undef __dynamic_array
798 #define __dynamic_array(type, item, len) \
799 __data_offsets->item = __data_size + \
800 offsetof(typeof(*entry), __data); \
801 __data_offsets->item |= (len * sizeof(type)) << 16; \
802 __data_size += (len) * sizeof(type);
803
804 #undef __string
805 #define __string(item, src) __dynamic_array(char, item, strlen(src) + 1)
806
807 #undef DECLARE_EVENT_CLASS
808 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
809 static inline notrace int ftrace_get_offsets_##call( \
810 struct ftrace_data_offsets_##call *__data_offsets, proto) \
811 { \
812 int __data_size = 0; \
813 struct ftrace_raw_##call __maybe_unused *entry; \
814 \
815 tstruct; \
816 \
817 return __data_size; \
818 }
819
820 #undef DEFINE_EVENT
821 #define DEFINE_EVENT(template, name, proto, args)
822
823 #undef DEFINE_EVENT_PRINT
824 #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
825 DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
826
827 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
828
829 /*
830 * Stage 4 of the trace events.
831 *
832 * Override the macros in <trace/trace_events.h> to include the following:
833 *
834 * For those macros defined with TRACE_EVENT:
835 *
836 * static struct ftrace_event_call event_<call>;
837 *
838 * static void ftrace_raw_event_<call>(void *__data, proto)
839 * {
840 * struct ftrace_event_call *event_call = __data;
841 * struct ftrace_data_offsets_<call> __maybe_unused __data_offsets;
842 * struct ring_buffer_event *event;
843 * struct ftrace_raw_<call> *entry; <-- defined in stage 1
844 * struct ring_buffer *buffer;
845 * unsigned long irq_flags;
846 * int __data_size;
847 * int pc;
848 *
849 * local_save_flags(irq_flags);
850 * pc = preempt_count();
851 *
852 * __data_size = ftrace_get_offsets_<call>(&__data_offsets, args);
853 *
854 * event = trace_current_buffer_lock_reserve(&buffer,
855 * event_<call>->event.type,
856 * sizeof(*entry) + __data_size,
857 * irq_flags, pc);
858 * if (!event)
859 * return;
860 * entry = ring_buffer_event_data(event);
861 *
862 * { <assign>; } <-- Here we assign the entries by the __field and
863 * __array macros.
864 *
865 * if (!filter_current_check_discard(buffer, event_call, entry, event))
866 * trace_current_buffer_unlock_commit(buffer,
867 * event, irq_flags, pc);
868 * }
869 *
870 * static struct trace_event ftrace_event_type_<call> = {
871 * .trace = ftrace_raw_output_<call>, <-- stage 2
872 * };
873 *
874 * static const char print_fmt_<call>[] = <TP_printk>;
875 *
876 * static struct ftrace_event_class __used event_class_<template> = {
877 * .system = "<system>",
878 * .define_fields = ftrace_define_fields_<call>,
879 * .fields = LIST_HEAD_INIT(event_class_##call.fields),
880 * .raw_init = trace_event_raw_init,
881 * .probe = ftrace_raw_event_##call,
882 * .reg = ftrace_event_reg,
883 * };
884 *
885 * static struct ftrace_event_call __used
886 * __attribute__((__aligned__(4)))
887 * __attribute__((section("_ftrace_events"))) event_<call> = {
888 * .name = "<call>",
889 * .class = event_class_<template>,
890 * .event = &ftrace_event_type_<call>,
891 * .print_fmt = print_fmt_<call>,
892 * };
893 *
894 */
895
896 #ifdef CONFIG_PERF_EVENTS
897
898 #define _TRACE_PERF_PROTO(call, proto) \
899 static notrace void \
900 perf_trace_##call(void *__data, proto);
901
902 #define _TRACE_PERF_INIT(call) \
903 .perf_probe = perf_trace_##call,
904
905 #else
906 #define _TRACE_PERF_PROTO(call, proto)
907 #define _TRACE_PERF_INIT(call)
908 #endif /* CONFIG_PERF_EVENTS */
909
910 #undef __entry
911 #define __entry entry
912
913 #undef __field
914 #define __field(type, item)
915
916 #undef __array
917 #define __array(type, item, len)
918
919 #undef __dynamic_array
920 #define __dynamic_array(type, item, len) \
921 __entry->__data_loc_##item = __data_offsets.item;
922
923 #undef __string
924 #define __string(item, src) __dynamic_array(char, item, -1) \
925
926 #undef __assign_str
927 #define __assign_str(dst, src) \
928 strcpy(__get_str(dst), src);
929
930 #undef TP_fast_assign
931 #define TP_fast_assign(args...) args
932
933 #undef TP_perf_assign
934 #define TP_perf_assign(args...)
935
936 #undef DECLARE_EVENT_CLASS
937 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
938 \
939 static notrace void \
940 ftrace_raw_event_##call(void *__data, proto) \
941 { \
942 struct ftrace_event_call *event_call = __data; \
943 struct ftrace_data_offsets_##call __maybe_unused __data_offsets;\
944 struct ring_buffer_event *event; \
945 struct ftrace_raw_##call *entry; \
946 struct ring_buffer *buffer; \
947 unsigned long irq_flags; \
948 int __data_size; \
949 int pc; \
950 \
951 local_save_flags(irq_flags); \
952 pc = preempt_count(); \
953 \
954 __data_size = ftrace_get_offsets_##call(&__data_offsets, args); \
955 \
956 event = trace_current_buffer_lock_reserve(&buffer, \
957 event_call->event.type, \
958 sizeof(*entry) + __data_size, \
959 irq_flags, pc); \
960 if (!event) \
961 return; \
962 entry = ring_buffer_event_data(event); \
963 \
964 tstruct \
965 \
966 { assign; } \
967 \
968 if (!filter_current_check_discard(buffer, event_call, entry, event)) \
969 trace_nowake_buffer_unlock_commit(buffer, \
970 event, irq_flags, pc); \
971 }
972 /*
973 * The ftrace_test_probe is compiled out, it is only here as a build time check
974 * to make sure that if the tracepoint handling changes, the ftrace probe will
975 * fail to compile unless it too is updated.
976 */
977
978 #undef DEFINE_EVENT
979 #define DEFINE_EVENT(template, call, proto, args) \
980 static inline void ftrace_test_probe_##call(void) \
981 { \
982 check_trace_callback_type_##call(ftrace_raw_event_##template); \
983 }
984
985 #undef DEFINE_EVENT_PRINT
986 #define DEFINE_EVENT_PRINT(template, name, proto, args, print)
987
988 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
989
990 #undef __entry
991 #define __entry REC
992
993 #undef __print_flags
994 #undef __print_symbolic
995 #undef __get_dynamic_array
996 #undef __get_str
997
998 #undef TP_printk
999 #define TP_printk(fmt, args...) "\"" fmt "\", " __stringify(args)
1000
1001 #undef DECLARE_EVENT_CLASS
1002 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
1003 _TRACE_PERF_PROTO(call, PARAMS(proto)); \
1004 static const char print_fmt_##call[] = print; \
1005 static struct ftrace_event_class __used event_class_##call = { \
1006 .system = __stringify(TRACE_SYSTEM), \
1007 .define_fields = ftrace_define_fields_##call, \
1008 .fields = LIST_HEAD_INIT(event_class_##call.fields),\
1009 .raw_init = trace_event_raw_init, \
1010 .probe = ftrace_raw_event_##call, \
1011 .reg = ftrace_event_reg, \
1012 _TRACE_PERF_INIT(call) \
1013 };
1014
1015 #undef DEFINE_EVENT
1016 #define DEFINE_EVENT(template, call, proto, args) \
1017 \
1018 static struct ftrace_event_call __used \
1019 __attribute__((__aligned__(4))) \
1020 __attribute__((section("_ftrace_events"))) event_##call = { \
1021 .name = #call, \
1022 .class = &event_class_##template, \
1023 .event.funcs = &ftrace_event_type_funcs_##template, \
1024 .print_fmt = print_fmt_##template, \
1025 };
1026
1027 #undef DEFINE_EVENT_PRINT
1028 #define DEFINE_EVENT_PRINT(template, call, proto, args, print) \
1029 \
1030 static const char print_fmt_##call[] = print; \
1031 \
1032 static struct ftrace_event_call __used \
1033 __attribute__((__aligned__(4))) \
1034 __attribute__((section("_ftrace_events"))) event_##call = { \
1035 .name = #call, \
1036 .class = &event_class_##template, \
1037 .event.funcs = &ftrace_event_type_funcs_##call, \
1038 .print_fmt = print_fmt_##call, \
1039 }
1040
1041 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
1042
1043 /*
1044 * Define the insertion callback to perf events
1045 *
1046 * The job is very similar to ftrace_raw_event_<call> except that we don't
1047 * insert in the ring buffer but in a perf counter.
1048 *
1049 * static void ftrace_perf_<call>(proto)
1050 * {
1051 * struct ftrace_data_offsets_<call> __maybe_unused __data_offsets;
1052 * struct ftrace_event_call *event_call = &event_<call>;
1053 * extern void perf_tp_event(int, u64, u64, void *, int);
1054 * struct ftrace_raw_##call *entry;
1055 * struct perf_trace_buf *trace_buf;
1056 * u64 __addr = 0, __count = 1;
1057 * unsigned long irq_flags;
1058 * struct trace_entry *ent;
1059 * int __entry_size;
1060 * int __data_size;
1061 * int __cpu
1062 * int pc;
1063 *
1064 * pc = preempt_count();
1065 *
1066 * __data_size = ftrace_get_offsets_<call>(&__data_offsets, args);
1067 *
1068 * // Below we want to get the aligned size by taking into account
1069 * // the u32 field that will later store the buffer size
1070 * __entry_size = ALIGN(__data_size + sizeof(*entry) + sizeof(u32),
1071 * sizeof(u64));
1072 * __entry_size -= sizeof(u32);
1073 *
1074 * // Protect the non nmi buffer
1075 * // This also protects the rcu read side
1076 * local_irq_save(irq_flags);
1077 * __cpu = smp_processor_id();
1078 *
1079 * if (in_nmi())
1080 * trace_buf = rcu_dereference_sched(perf_trace_buf_nmi);
1081 * else
1082 * trace_buf = rcu_dereference_sched(perf_trace_buf);
1083 *
1084 * if (!trace_buf)
1085 * goto end;
1086 *
1087 * trace_buf = per_cpu_ptr(trace_buf, __cpu);
1088 *
1089 * // Avoid recursion from perf that could mess up the buffer
1090 * if (trace_buf->recursion++)
1091 * goto end_recursion;
1092 *
1093 * raw_data = trace_buf->buf;
1094 *
1095 * // Make recursion update visible before entering perf_tp_event
1096 * // so that we protect from perf recursions.
1097 *
1098 * barrier();
1099 *
1100 * //zero dead bytes from alignment to avoid stack leak to userspace:
1101 * *(u64 *)(&raw_data[__entry_size - sizeof(u64)]) = 0ULL;
1102 * entry = (struct ftrace_raw_<call> *)raw_data;
1103 * ent = &entry->ent;
1104 * tracing_generic_entry_update(ent, irq_flags, pc);
1105 * ent->type = event_call->id;
1106 *
1107 * <tstruct> <- do some jobs with dynamic arrays
1108 *
1109 * <assign> <- affect our values
1110 *
1111 * perf_tp_event(event_call->id, __addr, __count, entry,
1112 * __entry_size); <- submit them to perf counter
1113 *
1114 * }
1115 */
1116
1117 #ifdef CONFIG_PERF_EVENTS
1118
1119 #undef __entry
1120 #define __entry entry
1121
1122 #undef __get_dynamic_array
1123 #define __get_dynamic_array(field) \
1124 ((void *)__entry + (__entry->__data_loc_##field & 0xffff))
1125
1126 #undef __get_str
1127 #define __get_str(field) (char *)__get_dynamic_array(field)
1128
1129 #undef __perf_addr
1130 #define __perf_addr(a) __addr = (a)
1131
1132 #undef __perf_count
1133 #define __perf_count(c) __count = (c)
1134
1135 #undef DECLARE_EVENT_CLASS
1136 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
1137 static notrace void \
1138 perf_trace_##call(void *__data, proto) \
1139 { \
1140 struct ftrace_event_call *event_call = __data; \
1141 struct ftrace_data_offsets_##call __maybe_unused __data_offsets;\
1142 struct ftrace_raw_##call *entry; \
1143 struct pt_regs __regs; \
1144 u64 __addr = 0, __count = 1; \
1145 struct hlist_head *head; \
1146 int __entry_size; \
1147 int __data_size; \
1148 int rctx; \
1149 \
1150 perf_fetch_caller_regs(&__regs); \
1151 \
1152 __data_size = ftrace_get_offsets_##call(&__data_offsets, args); \
1153 __entry_size = ALIGN(__data_size + sizeof(*entry) + sizeof(u32),\
1154 sizeof(u64)); \
1155 __entry_size -= sizeof(u32); \
1156 \
1157 if (WARN_ONCE(__entry_size > PERF_MAX_TRACE_SIZE, \
1158 "profile buffer not large enough")) \
1159 return; \
1160 \
1161 entry = (struct ftrace_raw_##call *)perf_trace_buf_prepare( \
1162 __entry_size, event_call->event.type, &__regs, &rctx); \
1163 if (!entry) \
1164 return; \
1165 \
1166 tstruct \
1167 \
1168 { assign; } \
1169 \
1170 head = this_cpu_ptr(event_call->perf_events); \
1171 perf_trace_buf_submit(entry, __entry_size, rctx, __addr, \
1172 __count, &__regs, head); \
1173 }
1174
1175 /*
1176 * This part is compiled out, it is only here as a build time check
1177 * to make sure that if the tracepoint handling changes, the
1178 * perf probe will fail to compile unless it too is updated.
1179 */
1180 #undef DEFINE_EVENT
1181 #define DEFINE_EVENT(template, call, proto, args) \
1182 static inline void perf_test_probe_##call(void) \
1183 { \
1184 check_trace_callback_type_##call(perf_trace_##template); \
1185 }
1186
1187
1188 #undef DEFINE_EVENT_PRINT
1189 #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
1190 DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
1191
1192 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
1193 #endif /* CONFIG_PERF_EVENTS */
1194
1195 #undef _TRACE_PROFILE_INIT
1196 #endif //0
This page took 0.081384 seconds and 6 git commands to generate.