2 * Stage 1 of the trace events.
4 * Override the macros in <trace/trace_events.h> to include the following:
6 * struct trace_event_raw_<call> {
7 * struct trace_entry ent;
9 * <type2> <item2>[<len>];
13 * The <type> <item> is created by the __field(type, item) macro or
14 * the __array(type2, item2, len) macro.
15 * We simply do "type item;", and that will create the fields
19 #include <linux/trace_events.h>
21 #ifndef TRACE_SYSTEM_VAR
22 #define TRACE_SYSTEM_VAR TRACE_SYSTEM
25 #define __app__(x, y) str__##x##y
26 #define __app(x, y) __app__(x, y)
28 #define TRACE_SYSTEM_STRING __app(TRACE_SYSTEM_VAR,__trace_system_name)
30 #define TRACE_MAKE_SYSTEM_STR() \
31 static const char TRACE_SYSTEM_STRING[] = \
32 __stringify(TRACE_SYSTEM)
34 TRACE_MAKE_SYSTEM_STR();
36 #undef TRACE_DEFINE_ENUM
37 #define TRACE_DEFINE_ENUM(a) \
38 static struct trace_eval_map __used __initdata \
39 __##TRACE_SYSTEM##_##a = \
41 .system = TRACE_SYSTEM_STRING, \
45 static struct trace_eval_map __used \
46 __attribute__((section("_ftrace_eval_map"))) \
47 *TRACE_SYSTEM##_##a = &__##TRACE_SYSTEM##_##a
49 #undef TRACE_DEFINE_SIZEOF
50 #define TRACE_DEFINE_SIZEOF(a) \
51 static struct trace_eval_map __used __initdata \
52 __##TRACE_SYSTEM##_##a = \
54 .system = TRACE_SYSTEM_STRING, \
55 .eval_string = "sizeof(" #a ")", \
56 .eval_value = sizeof(a) \
58 static struct trace_eval_map __used \
59 __attribute__((section("_ftrace_eval_map"))) \
60 *TRACE_SYSTEM##_##a = &__##TRACE_SYSTEM##_##a
63 * DECLARE_EVENT_CLASS can be used to add a generic function
64 * handlers for events. That is, if all events have the same
65 * parameters and just have distinct trace points.
66 * Each tracepoint can be defined with DEFINE_EVENT and that
67 * will map the DECLARE_EVENT_CLASS to the tracepoint.
69 * TRACE_EVENT is a one to one mapping between tracepoint and template.
72 #define TRACE_EVENT(name, proto, args, tstruct, assign, print) \
73 DECLARE_EVENT_CLASS(name, \
79 DEFINE_EVENT(name, name, PARAMS(proto), PARAMS(args));
83 #define __field(type, item) type item;
86 #define __field_ext(type, item, filter_type) type item;
89 #define __field_struct(type, item) type item;
91 #undef __field_struct_ext
92 #define __field_struct_ext(type, item, filter_type) type item;
95 #define __array(type, item, len) type item[len];
97 #undef __dynamic_array
98 #define __dynamic_array(type, item, len) u32 __data_loc_##item;
101 #define __string(item, src) __dynamic_array(char, item, -1)
104 #define __bitmask(item, nr_bits) __dynamic_array(char, item, -1)
106 #undef TP_STRUCT__entry
107 #define TP_STRUCT__entry(args...) args
109 #undef DECLARE_EVENT_CLASS
110 #define DECLARE_EVENT_CLASS(name, proto, args, tstruct, assign, print) \
111 struct trace_event_raw_##name { \
112 struct trace_entry ent; \
117 static struct trace_event_class event_class_##name;
120 #define DEFINE_EVENT(template, name, proto, args) \
121 static struct trace_event_call __used \
122 __attribute__((__aligned__(4))) event_##name
124 #undef DEFINE_EVENT_FN
125 #define DEFINE_EVENT_FN(template, name, proto, args, reg, unreg) \
126 DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
128 #undef DEFINE_EVENT_PRINT
129 #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
130 DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
132 /* Callbacks are meaningless to ftrace. */
133 #undef TRACE_EVENT_FN
134 #define TRACE_EVENT_FN(name, proto, args, tstruct, \
135 assign, print, reg, unreg) \
136 TRACE_EVENT(name, PARAMS(proto), PARAMS(args), \
137 PARAMS(tstruct), PARAMS(assign), PARAMS(print)) \
139 #undef TRACE_EVENT_FN_COND
140 #define TRACE_EVENT_FN_COND(name, proto, args, cond, tstruct, \
141 assign, print, reg, unreg) \
142 TRACE_EVENT_CONDITION(name, PARAMS(proto), PARAMS(args), PARAMS(cond), \
143 PARAMS(tstruct), PARAMS(assign), PARAMS(print)) \
145 #undef TRACE_EVENT_FLAGS
146 #define TRACE_EVENT_FLAGS(name, value) \
147 __TRACE_EVENT_FLAGS(name, value)
149 #undef TRACE_EVENT_PERF_PERM
150 #define TRACE_EVENT_PERF_PERM(name, expr...) \
151 __TRACE_EVENT_PERF_PERM(name, expr)
153 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
156 * Stage 2 of the trace events.
158 * Include the following:
160 * struct trace_event_data_offsets_<call> {
166 * The __dynamic_array() macro will create each u32 <item>, this is
167 * to keep the offset of each array from the beginning of the event.
168 * The size of an array is also encoded, in the higher 16 bits of <item>.
171 #undef TRACE_DEFINE_ENUM
172 #define TRACE_DEFINE_ENUM(a)
174 #undef TRACE_DEFINE_SIZEOF
175 #define TRACE_DEFINE_SIZEOF(a)
178 #define __field(type, item)
181 #define __field_ext(type, item, filter_type)
183 #undef __field_struct
184 #define __field_struct(type, item)
186 #undef __field_struct_ext
187 #define __field_struct_ext(type, item, filter_type)
190 #define __array(type, item, len)
192 #undef __dynamic_array
193 #define __dynamic_array(type, item, len) u32 item;
196 #define __string(item, src) __dynamic_array(char, item, -1)
199 #define __bitmask(item, nr_bits) __dynamic_array(unsigned long, item, -1)
201 #undef DECLARE_EVENT_CLASS
202 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
203 struct trace_event_data_offsets_##call { \
208 #define DEFINE_EVENT(template, name, proto, args)
210 #undef DEFINE_EVENT_PRINT
211 #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
212 DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
214 #undef TRACE_EVENT_FLAGS
215 #define TRACE_EVENT_FLAGS(event, flag)
217 #undef TRACE_EVENT_PERF_PERM
218 #define TRACE_EVENT_PERF_PERM(event, expr...)
220 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
223 * Stage 3 of the trace events.
225 * Override the macros in <trace/trace_events.h> to include the following:
228 * trace_raw_output_<call>(struct trace_iterator *iter, int flags)
230 * struct trace_seq *s = &iter->seq;
231 * struct trace_event_raw_<call> *field; <-- defined in stage 1
232 * struct trace_entry *entry;
233 * struct trace_seq *p = &iter->tmp_seq;
238 * if (entry->type != event_<call>->event.type) {
240 * return TRACE_TYPE_UNHANDLED;
243 * field = (typeof(field))entry;
246 * ret = trace_seq_printf(s, "%s: ", <call>);
248 * ret = trace_seq_printf(s, <TP_printk> "\n");
250 * return TRACE_TYPE_PARTIAL_LINE;
252 * return TRACE_TYPE_HANDLED;
255 * This is the method used to print the raw event to the trace
256 * output format. Note, this is not needed if the data is read
261 #define __entry field
264 #define TP_printk(fmt, args...) fmt "\n", args
266 #undef __get_dynamic_array
267 #define __get_dynamic_array(field) \
268 ((void *)__entry + (__entry->__data_loc_##field & 0xffff))
270 #undef __get_dynamic_array_len
271 #define __get_dynamic_array_len(field) \
272 ((__entry->__data_loc_##field >> 16) & 0xffff)
275 #define __get_str(field) ((char *)__get_dynamic_array(field))
278 #define __get_bitmask(field) \
280 void *__bitmask = __get_dynamic_array(field); \
281 unsigned int __bitmask_size; \
282 __bitmask_size = __get_dynamic_array_len(field); \
283 trace_print_bitmask_seq(p, __bitmask, __bitmask_size); \
287 #define __print_flags(flag, delim, flag_array...) \
289 static const struct trace_print_flags __flags[] = \
290 { flag_array, { -1, NULL }}; \
291 trace_print_flags_seq(p, delim, flag, __flags); \
294 #undef __print_symbolic
295 #define __print_symbolic(value, symbol_array...) \
297 static const struct trace_print_flags symbols[] = \
298 { symbol_array, { -1, NULL }}; \
299 trace_print_symbols_seq(p, value, symbols); \
302 #undef __print_flags_u64
303 #undef __print_symbolic_u64
304 #if BITS_PER_LONG == 32
305 #define __print_flags_u64(flag, delim, flag_array...) \
307 static const struct trace_print_flags_u64 __flags[] = \
308 { flag_array, { -1, NULL } }; \
309 trace_print_flags_seq_u64(p, delim, flag, __flags); \
312 #define __print_symbolic_u64(value, symbol_array...) \
314 static const struct trace_print_flags_u64 symbols[] = \
315 { symbol_array, { -1, NULL } }; \
316 trace_print_symbols_seq_u64(p, value, symbols); \
319 #define __print_flags_u64(flag, delim, flag_array...) \
320 __print_flags(flag, delim, flag_array)
322 #define __print_symbolic_u64(value, symbol_array...) \
323 __print_symbolic(value, symbol_array)
327 #define __print_hex(buf, buf_len) \
328 trace_print_hex_seq(p, buf, buf_len, false)
330 #undef __print_hex_str
331 #define __print_hex_str(buf, buf_len) \
332 trace_print_hex_seq(p, buf, buf_len, true)
335 #define __print_array(array, count, el_size) \
337 BUILD_BUG_ON(el_size != 1 && el_size != 2 && \
338 el_size != 4 && el_size != 8); \
339 trace_print_array_seq(p, array, count, el_size); \
342 #undef DECLARE_EVENT_CLASS
343 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
344 static notrace enum print_line_t \
345 trace_raw_output_##call(struct trace_iterator *iter, int flags, \
346 struct trace_event *trace_event) \
348 struct trace_seq *s = &iter->seq; \
349 struct trace_seq __maybe_unused *p = &iter->tmp_seq; \
350 struct trace_event_raw_##call *field; \
353 field = (typeof(field))iter->ent; \
355 ret = trace_raw_output_prep(iter, trace_event); \
356 if (ret != TRACE_TYPE_HANDLED) \
359 trace_seq_printf(s, print); \
361 return trace_handle_return(s); \
363 static struct trace_event_functions trace_event_type_funcs_##call = { \
364 .trace = trace_raw_output_##call, \
367 #undef DEFINE_EVENT_PRINT
368 #define DEFINE_EVENT_PRINT(template, call, proto, args, print) \
369 static notrace enum print_line_t \
370 trace_raw_output_##call(struct trace_iterator *iter, int flags, \
371 struct trace_event *event) \
373 struct trace_event_raw_##template *field; \
374 struct trace_entry *entry; \
375 struct trace_seq *p = &iter->tmp_seq; \
379 if (entry->type != event_##call.event.type) { \
381 return TRACE_TYPE_UNHANDLED; \
384 field = (typeof(field))entry; \
387 return trace_output_call(iter, #call, print); \
389 static struct trace_event_functions trace_event_type_funcs_##call = { \
390 .trace = trace_raw_output_##call, \
393 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
396 #define __field_ext(type, item, filter_type) \
397 ret = trace_define_field(event_call, #type, #item, \
398 offsetof(typeof(field), item), \
399 sizeof(field.item), \
400 is_signed_type(type), filter_type); \
404 #undef __field_struct_ext
405 #define __field_struct_ext(type, item, filter_type) \
406 ret = trace_define_field(event_call, #type, #item, \
407 offsetof(typeof(field), item), \
408 sizeof(field.item), \
414 #define __field(type, item) __field_ext(type, item, FILTER_OTHER)
416 #undef __field_struct
417 #define __field_struct(type, item) __field_struct_ext(type, item, FILTER_OTHER)
420 #define __array(type, item, len) \
422 char *type_str = #type"["__stringify(len)"]"; \
423 BUILD_BUG_ON(len > MAX_FILTER_STR_VAL); \
424 ret = trace_define_field(event_call, type_str, #item, \
425 offsetof(typeof(field), item), \
426 sizeof(field.item), \
427 is_signed_type(type), FILTER_OTHER); \
432 #undef __dynamic_array
433 #define __dynamic_array(type, item, len) \
434 ret = trace_define_field(event_call, "__data_loc " #type "[]", #item, \
435 offsetof(typeof(field), __data_loc_##item), \
436 sizeof(field.__data_loc_##item), \
437 is_signed_type(type), FILTER_OTHER);
440 #define __string(item, src) __dynamic_array(char, item, -1)
443 #define __bitmask(item, nr_bits) __dynamic_array(unsigned long, item, -1)
445 #undef DECLARE_EVENT_CLASS
446 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, func, print) \
447 static int notrace __init \
448 trace_event_define_fields_##call(struct trace_event_call *event_call) \
450 struct trace_event_raw_##call field; \
459 #define DEFINE_EVENT(template, name, proto, args)
461 #undef DEFINE_EVENT_PRINT
462 #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
463 DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
465 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
468 * remember the offset of each array from the beginning of the event.
472 #define __entry entry
475 #define __field(type, item)
478 #define __field_ext(type, item, filter_type)
480 #undef __field_struct
481 #define __field_struct(type, item)
483 #undef __field_struct_ext
484 #define __field_struct_ext(type, item, filter_type)
487 #define __array(type, item, len)
489 #undef __dynamic_array
490 #define __dynamic_array(type, item, len) \
491 __item_length = (len) * sizeof(type); \
492 __data_offsets->item = __data_size + \
493 offsetof(typeof(*entry), __data); \
494 __data_offsets->item |= __item_length << 16; \
495 __data_size += __item_length;
498 #define __string(item, src) __dynamic_array(char, item, \
499 strlen((src) ? (const char *)(src) : "(null)") + 1)
502 * __bitmask_size_in_bytes_raw is the number of bytes needed to hold
503 * num_possible_cpus().
505 #define __bitmask_size_in_bytes_raw(nr_bits) \
506 (((nr_bits) + 7) / 8)
508 #define __bitmask_size_in_longs(nr_bits) \
509 ((__bitmask_size_in_bytes_raw(nr_bits) + \
510 ((BITS_PER_LONG / 8) - 1)) / (BITS_PER_LONG / 8))
513 * __bitmask_size_in_bytes is the number of bytes needed to hold
514 * num_possible_cpus() padded out to the nearest long. This is what
515 * is saved in the buffer, just to be consistent.
517 #define __bitmask_size_in_bytes(nr_bits) \
518 (__bitmask_size_in_longs(nr_bits) * (BITS_PER_LONG / 8))
521 #define __bitmask(item, nr_bits) __dynamic_array(unsigned long, item, \
522 __bitmask_size_in_longs(nr_bits))
524 #undef DECLARE_EVENT_CLASS
525 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
526 static inline notrace int trace_event_get_offsets_##call( \
527 struct trace_event_data_offsets_##call *__data_offsets, proto) \
529 int __data_size = 0; \
530 int __maybe_unused __item_length; \
531 struct trace_event_raw_##call __maybe_unused *entry; \
535 return __data_size; \
539 #define DEFINE_EVENT(template, name, proto, args)
541 #undef DEFINE_EVENT_PRINT
542 #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
543 DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
545 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
548 * Stage 4 of the trace events.
550 * Override the macros in <trace/trace_events.h> to include the following:
552 * For those macros defined with TRACE_EVENT:
554 * static struct trace_event_call event_<call>;
556 * static void trace_event_raw_event_<call>(void *__data, proto)
558 * struct trace_event_file *trace_file = __data;
559 * struct trace_event_call *event_call = trace_file->event_call;
560 * struct trace_event_data_offsets_<call> __maybe_unused __data_offsets;
561 * unsigned long eflags = trace_file->flags;
562 * enum event_trigger_type __tt = ETT_NONE;
563 * struct ring_buffer_event *event;
564 * struct trace_event_raw_<call> *entry; <-- defined in stage 1
565 * struct ring_buffer *buffer;
566 * unsigned long irq_flags;
570 * if (!(eflags & EVENT_FILE_FL_TRIGGER_COND)) {
571 * if (eflags & EVENT_FILE_FL_TRIGGER_MODE)
572 * event_triggers_call(trace_file, NULL);
573 * if (eflags & EVENT_FILE_FL_SOFT_DISABLED)
577 * local_save_flags(irq_flags);
578 * pc = preempt_count();
580 * __data_size = trace_event_get_offsets_<call>(&__data_offsets, args);
582 * event = trace_event_buffer_lock_reserve(&buffer, trace_file,
583 * event_<call>->event.type,
584 * sizeof(*entry) + __data_size,
588 * entry = ring_buffer_event_data(event);
590 * { <assign>; } <-- Here we assign the entries by the __field and
593 * if (eflags & EVENT_FILE_FL_TRIGGER_COND)
594 * __tt = event_triggers_call(trace_file, entry);
596 * if (test_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT,
597 * &trace_file->flags))
598 * ring_buffer_discard_commit(buffer, event);
599 * else if (!filter_check_discard(trace_file, entry, buffer, event))
600 * trace_buffer_unlock_commit(buffer, event, irq_flags, pc);
603 * event_triggers_post_call(trace_file, __tt);
606 * static struct trace_event ftrace_event_type_<call> = {
607 * .trace = trace_raw_output_<call>, <-- stage 2
610 * static char print_fmt_<call>[] = <TP_printk>;
612 * static struct trace_event_class __used event_class_<template> = {
613 * .system = "<system>",
614 * .define_fields = trace_event_define_fields_<call>,
615 * .fields = LIST_HEAD_INIT(event_class_##call.fields),
616 * .raw_init = trace_event_raw_init,
617 * .probe = trace_event_raw_event_##call,
618 * .reg = trace_event_reg,
621 * static struct trace_event_call event_<call> = {
622 * .class = event_class_<template>,
624 * .tp = &__tracepoint_<call>,
626 * .event = &ftrace_event_type_<call>,
627 * .print_fmt = print_fmt_<call>,
628 * .flags = TRACE_EVENT_FL_TRACEPOINT,
630 * // its only safe to use pointers when doing linker tricks to
631 * // create an array.
632 * static struct trace_event_call __used
633 * __attribute__((section("_ftrace_events"))) *__event_<call> = &event_<call>;
637 #ifdef CONFIG_PERF_EVENTS
639 #define _TRACE_PERF_PROTO(call, proto) \
640 static notrace void \
641 perf_trace_##call(void *__data, proto);
643 #define _TRACE_PERF_INIT(call) \
644 .perf_probe = perf_trace_##call,
647 #define _TRACE_PERF_PROTO(call, proto)
648 #define _TRACE_PERF_INIT(call)
649 #endif /* CONFIG_PERF_EVENTS */
652 #define __entry entry
655 #define __field(type, item)
657 #undef __field_struct
658 #define __field_struct(type, item)
661 #define __array(type, item, len)
663 #undef __dynamic_array
664 #define __dynamic_array(type, item, len) \
665 __entry->__data_loc_##item = __data_offsets.item;
668 #define __string(item, src) __dynamic_array(char, item, -1)
671 #define __assign_str(dst, src) \
672 strcpy(__get_str(dst), (src) ? (const char *)(src) : "(null)");
675 #define __bitmask(item, nr_bits) __dynamic_array(unsigned long, item, -1)
678 #define __get_bitmask(field) (char *)__get_dynamic_array(field)
680 #undef __assign_bitmask
681 #define __assign_bitmask(dst, src, nr_bits) \
682 memcpy(__get_bitmask(dst), (src), __bitmask_size_in_bytes(nr_bits))
684 #undef TP_fast_assign
685 #define TP_fast_assign(args...) args
688 #define __perf_count(c) (c)
691 #define __perf_task(t) (t)
693 #undef DECLARE_EVENT_CLASS
694 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
696 static notrace void \
697 trace_event_raw_event_##call(void *__data, proto) \
699 struct trace_event_file *trace_file = __data; \
700 struct trace_event_data_offsets_##call __maybe_unused __data_offsets;\
701 struct trace_event_buffer fbuffer; \
702 struct trace_event_raw_##call *entry; \
705 if (trace_trigger_soft_disabled(trace_file)) \
708 __data_size = trace_event_get_offsets_##call(&__data_offsets, args); \
710 entry = trace_event_buffer_reserve(&fbuffer, trace_file, \
711 sizeof(*entry) + __data_size); \
720 trace_event_buffer_commit(&fbuffer); \
723 * The ftrace_test_probe is compiled out, it is only here as a build time check
724 * to make sure that if the tracepoint handling changes, the ftrace probe will
725 * fail to compile unless it too is updated.
729 #define DEFINE_EVENT(template, call, proto, args) \
730 static inline void ftrace_test_probe_##call(void) \
732 check_trace_callback_type_##call(trace_event_raw_event_##template); \
735 #undef DEFINE_EVENT_PRINT
736 #define DEFINE_EVENT_PRINT(template, name, proto, args, print)
738 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
744 #undef __print_symbolic
746 #undef __print_hex_str
747 #undef __get_dynamic_array
748 #undef __get_dynamic_array_len
754 #define TP_printk(fmt, args...) "\"" fmt "\", " __stringify(args)
756 #undef DECLARE_EVENT_CLASS
757 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
758 _TRACE_PERF_PROTO(call, PARAMS(proto)); \
759 static char print_fmt_##call[] = print; \
760 static struct trace_event_class __used __refdata event_class_##call = { \
761 .system = TRACE_SYSTEM_STRING, \
762 .define_fields = trace_event_define_fields_##call, \
763 .fields = LIST_HEAD_INIT(event_class_##call.fields),\
764 .raw_init = trace_event_raw_init, \
765 .probe = trace_event_raw_event_##call, \
766 .reg = trace_event_reg, \
767 _TRACE_PERF_INIT(call) \
771 #define DEFINE_EVENT(template, call, proto, args) \
773 static struct trace_event_call __used event_##call = { \
774 .class = &event_class_##template, \
776 .tp = &__tracepoint_##call, \
778 .event.funcs = &trace_event_type_funcs_##template, \
779 .print_fmt = print_fmt_##template, \
780 .flags = TRACE_EVENT_FL_TRACEPOINT, \
782 static struct trace_event_call __used \
783 __attribute__((section("_ftrace_events"))) *__event_##call = &event_##call
785 #undef DEFINE_EVENT_PRINT
786 #define DEFINE_EVENT_PRINT(template, call, proto, args, print) \
788 static char print_fmt_##call[] = print; \
790 static struct trace_event_call __used event_##call = { \
791 .class = &event_class_##template, \
793 .tp = &__tracepoint_##call, \
795 .event.funcs = &trace_event_type_funcs_##call, \
796 .print_fmt = print_fmt_##call, \
797 .flags = TRACE_EVENT_FL_TRACEPOINT, \
799 static struct trace_event_call __used \
800 __attribute__((section("_ftrace_events"))) *__event_##call = &event_##call
802 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)