1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Stage 1 of the trace events.
5 * Override the macros in the event tracepoint header <trace/events/XXX.h>
6 * to include the following:
8 * struct trace_event_raw_<call> {
9 * struct trace_entry ent;
11 * <type2> <item2>[<len>];
15 * The <type> <item> is created by the __field(type, item) macro or
16 * the __array(type2, item2, len) macro.
17 * We simply do "type item;", and that will create the fields
21 #include <linux/trace_events.h>
23 #ifndef TRACE_SYSTEM_VAR
24 #define TRACE_SYSTEM_VAR TRACE_SYSTEM
27 #define __app__(x, y) str__##x##y
28 #define __app(x, y) __app__(x, y)
30 #define TRACE_SYSTEM_STRING __app(TRACE_SYSTEM_VAR,__trace_system_name)
32 #define TRACE_MAKE_SYSTEM_STR() \
33 static const char TRACE_SYSTEM_STRING[] = \
34 __stringify(TRACE_SYSTEM)
36 TRACE_MAKE_SYSTEM_STR();
38 #undef TRACE_DEFINE_ENUM
39 #define TRACE_DEFINE_ENUM(a) \
40 static struct trace_eval_map __used __initdata \
41 __##TRACE_SYSTEM##_##a = \
43 .system = TRACE_SYSTEM_STRING, \
47 static struct trace_eval_map __used \
48 __section("_ftrace_eval_map") \
49 *TRACE_SYSTEM##_##a = &__##TRACE_SYSTEM##_##a
51 #undef TRACE_DEFINE_SIZEOF
52 #define TRACE_DEFINE_SIZEOF(a) \
53 static struct trace_eval_map __used __initdata \
54 __##TRACE_SYSTEM##_##a = \
56 .system = TRACE_SYSTEM_STRING, \
57 .eval_string = "sizeof(" #a ")", \
58 .eval_value = sizeof(a) \
60 static struct trace_eval_map __used \
61 __section("_ftrace_eval_map") \
62 *TRACE_SYSTEM##_##a = &__##TRACE_SYSTEM##_##a
65 * DECLARE_EVENT_CLASS can be used to add a generic function
66 * handlers for events. That is, if all events have the same
67 * parameters and just have distinct trace points.
68 * Each tracepoint can be defined with DEFINE_EVENT and that
69 * will map the DECLARE_EVENT_CLASS to the tracepoint.
71 * TRACE_EVENT is a one to one mapping between tracepoint and template.
74 #define TRACE_EVENT(name, proto, args, tstruct, assign, print) \
75 DECLARE_EVENT_CLASS(name, \
81 DEFINE_EVENT(name, name, PARAMS(proto), PARAMS(args));
85 #define __field(type, item) type item;
88 #define __field_ext(type, item, filter_type) type item;
91 #define __field_struct(type, item) type item;
93 #undef __field_struct_ext
94 #define __field_struct_ext(type, item, filter_type) type item;
97 #define __array(type, item, len) type item[len];
99 #undef __dynamic_array
100 #define __dynamic_array(type, item, len) u32 __data_loc_##item;
103 #define __string(item, src) __dynamic_array(char, item, -1)
106 #define __bitmask(item, nr_bits) __dynamic_array(char, item, -1)
108 #undef TP_STRUCT__entry
109 #define TP_STRUCT__entry(args...) args
111 #undef DECLARE_EVENT_CLASS
112 #define DECLARE_EVENT_CLASS(name, proto, args, tstruct, assign, print) \
113 struct trace_event_raw_##name { \
114 struct trace_entry ent; \
119 static struct trace_event_class event_class_##name;
122 #define DEFINE_EVENT(template, name, proto, args) \
123 static struct trace_event_call __used \
124 __attribute__((__aligned__(4))) event_##name
126 #undef DEFINE_EVENT_FN
127 #define DEFINE_EVENT_FN(template, name, proto, args, reg, unreg) \
128 DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
130 #undef DEFINE_EVENT_PRINT
131 #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
132 DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
134 /* Callbacks are meaningless to ftrace. */
135 #undef TRACE_EVENT_FN
136 #define TRACE_EVENT_FN(name, proto, args, tstruct, \
137 assign, print, reg, unreg) \
138 TRACE_EVENT(name, PARAMS(proto), PARAMS(args), \
139 PARAMS(tstruct), PARAMS(assign), PARAMS(print)) \
141 #undef TRACE_EVENT_FN_COND
142 #define TRACE_EVENT_FN_COND(name, proto, args, cond, tstruct, \
143 assign, print, reg, unreg) \
144 TRACE_EVENT_CONDITION(name, PARAMS(proto), PARAMS(args), PARAMS(cond), \
145 PARAMS(tstruct), PARAMS(assign), PARAMS(print)) \
147 #undef TRACE_EVENT_FLAGS
148 #define TRACE_EVENT_FLAGS(name, value) \
149 __TRACE_EVENT_FLAGS(name, value)
151 #undef TRACE_EVENT_PERF_PERM
152 #define TRACE_EVENT_PERF_PERM(name, expr...) \
153 __TRACE_EVENT_PERF_PERM(name, expr)
155 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
158 * Stage 2 of the trace events.
160 * Include the following:
162 * struct trace_event_data_offsets_<call> {
168 * The __dynamic_array() macro will create each u32 <item>, this is
169 * to keep the offset of each array from the beginning of the event.
170 * The size of an array is also encoded, in the higher 16 bits of <item>.
173 #undef TRACE_DEFINE_ENUM
174 #define TRACE_DEFINE_ENUM(a)
176 #undef TRACE_DEFINE_SIZEOF
177 #define TRACE_DEFINE_SIZEOF(a)
180 #define __field(type, item)
183 #define __field_ext(type, item, filter_type)
185 #undef __field_struct
186 #define __field_struct(type, item)
188 #undef __field_struct_ext
189 #define __field_struct_ext(type, item, filter_type)
192 #define __array(type, item, len)
194 #undef __dynamic_array
195 #define __dynamic_array(type, item, len) u32 item;
198 #define __string(item, src) __dynamic_array(char, item, -1)
201 #define __bitmask(item, nr_bits) __dynamic_array(unsigned long, item, -1)
203 #undef DECLARE_EVENT_CLASS
204 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
205 struct trace_event_data_offsets_##call { \
210 #define DEFINE_EVENT(template, name, proto, args)
212 #undef DEFINE_EVENT_PRINT
213 #define DEFINE_EVENT_PRINT(template, name, proto, args, print)
215 #undef TRACE_EVENT_FLAGS
216 #define TRACE_EVENT_FLAGS(event, flag)
218 #undef TRACE_EVENT_PERF_PERM
219 #define TRACE_EVENT_PERF_PERM(event, expr...)
221 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
224 * Stage 3 of the trace events.
226 * Override the macros in the event tracepoint header <trace/events/XXX.h>
227 * to include the following:
230 * trace_raw_output_<call>(struct trace_iterator *iter, int flags)
232 * struct trace_seq *s = &iter->seq;
233 * struct trace_event_raw_<call> *field; <-- defined in stage 1
234 * struct trace_entry *entry;
235 * struct trace_seq *p = &iter->tmp_seq;
240 * if (entry->type != event_<call>->event.type) {
242 * return TRACE_TYPE_UNHANDLED;
245 * field = (typeof(field))entry;
248 * ret = trace_seq_printf(s, "%s: ", <call>);
250 * ret = trace_seq_printf(s, <TP_printk> "\n");
252 * return TRACE_TYPE_PARTIAL_LINE;
254 * return TRACE_TYPE_HANDLED;
257 * This is the method used to print the raw event to the trace
258 * output format. Note, this is not needed if the data is read
263 #define __entry field
266 #define TP_printk(fmt, args...) fmt "\n", args
268 #undef __get_dynamic_array
269 #define __get_dynamic_array(field) \
270 ((void *)__entry + (__entry->__data_loc_##field & 0xffff))
272 #undef __get_dynamic_array_len
273 #define __get_dynamic_array_len(field) \
274 ((__entry->__data_loc_##field >> 16) & 0xffff)
277 #define __get_str(field) ((char *)__get_dynamic_array(field))
280 #define __get_bitmask(field) \
282 void *__bitmask = __get_dynamic_array(field); \
283 unsigned int __bitmask_size; \
284 __bitmask_size = __get_dynamic_array_len(field); \
285 trace_print_bitmask_seq(p, __bitmask, __bitmask_size); \
289 #define __print_flags(flag, delim, flag_array...) \
291 static const struct trace_print_flags __flags[] = \
292 { flag_array, { -1, NULL }}; \
293 trace_print_flags_seq(p, delim, flag, __flags); \
296 #undef __print_symbolic
297 #define __print_symbolic(value, symbol_array...) \
299 static const struct trace_print_flags symbols[] = \
300 { symbol_array, { -1, NULL }}; \
301 trace_print_symbols_seq(p, value, symbols); \
304 #undef __print_flags_u64
305 #undef __print_symbolic_u64
306 #if BITS_PER_LONG == 32
307 #define __print_flags_u64(flag, delim, flag_array...) \
309 static const struct trace_print_flags_u64 __flags[] = \
310 { flag_array, { -1, NULL } }; \
311 trace_print_flags_seq_u64(p, delim, flag, __flags); \
314 #define __print_symbolic_u64(value, symbol_array...) \
316 static const struct trace_print_flags_u64 symbols[] = \
317 { symbol_array, { -1, NULL } }; \
318 trace_print_symbols_seq_u64(p, value, symbols); \
321 #define __print_flags_u64(flag, delim, flag_array...) \
322 __print_flags(flag, delim, flag_array)
324 #define __print_symbolic_u64(value, symbol_array...) \
325 __print_symbolic(value, symbol_array)
329 #define __print_hex(buf, buf_len) \
330 trace_print_hex_seq(p, buf, buf_len, false)
332 #undef __print_hex_str
333 #define __print_hex_str(buf, buf_len) \
334 trace_print_hex_seq(p, buf, buf_len, true)
337 #define __print_array(array, count, el_size) \
339 BUILD_BUG_ON(el_size != 1 && el_size != 2 && \
340 el_size != 4 && el_size != 8); \
341 trace_print_array_seq(p, array, count, el_size); \
344 #undef __print_hex_dump
345 #define __print_hex_dump(prefix_str, prefix_type, \
346 rowsize, groupsize, buf, len, ascii) \
347 trace_print_hex_dump_seq(p, prefix_str, prefix_type, \
348 rowsize, groupsize, buf, len, ascii)
350 #undef DECLARE_EVENT_CLASS
351 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
352 static notrace enum print_line_t \
353 trace_raw_output_##call(struct trace_iterator *iter, int flags, \
354 struct trace_event *trace_event) \
356 struct trace_seq *s = &iter->seq; \
357 struct trace_seq __maybe_unused *p = &iter->tmp_seq; \
358 struct trace_event_raw_##call *field; \
361 field = (typeof(field))iter->ent; \
363 ret = trace_raw_output_prep(iter, trace_event); \
364 if (ret != TRACE_TYPE_HANDLED) \
367 trace_seq_printf(s, print); \
369 return trace_handle_return(s); \
371 static struct trace_event_functions trace_event_type_funcs_##call = { \
372 .trace = trace_raw_output_##call, \
375 #undef DEFINE_EVENT_PRINT
376 #define DEFINE_EVENT_PRINT(template, call, proto, args, print) \
377 static notrace enum print_line_t \
378 trace_raw_output_##call(struct trace_iterator *iter, int flags, \
379 struct trace_event *event) \
381 struct trace_event_raw_##template *field; \
382 struct trace_entry *entry; \
383 struct trace_seq *p = &iter->tmp_seq; \
387 if (entry->type != event_##call.event.type) { \
389 return TRACE_TYPE_UNHANDLED; \
392 field = (typeof(field))entry; \
395 return trace_output_call(iter, #call, print); \
397 static struct trace_event_functions trace_event_type_funcs_##call = { \
398 .trace = trace_raw_output_##call, \
401 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
404 #define __field_ext(_type, _item, _filter_type) { \
405 .type = #_type, .name = #_item, \
406 .size = sizeof(_type), .align = __alignof__(_type), \
407 .is_signed = is_signed_type(_type), .filter_type = _filter_type },
409 #undef __field_struct_ext
410 #define __field_struct_ext(_type, _item, _filter_type) { \
411 .type = #_type, .name = #_item, \
412 .size = sizeof(_type), .align = __alignof__(_type), \
413 0, .filter_type = _filter_type },
416 #define __field(type, item) __field_ext(type, item, FILTER_OTHER)
418 #undef __field_struct
419 #define __field_struct(type, item) __field_struct_ext(type, item, FILTER_OTHER)
422 #define __array(_type, _item, _len) { \
423 .type = #_type"["__stringify(_len)"]", .name = #_item, \
424 .size = sizeof(_type[_len]), .align = __alignof__(_type), \
425 .is_signed = is_signed_type(_type), .filter_type = FILTER_OTHER },
427 #undef __dynamic_array
428 #define __dynamic_array(_type, _item, _len) { \
429 .type = "__data_loc " #_type "[]", .name = #_item, \
430 .size = 4, .align = 4, \
431 .is_signed = is_signed_type(_type), .filter_type = FILTER_OTHER },
434 #define __string(item, src) __dynamic_array(char, item, -1)
437 #define __bitmask(item, nr_bits) __dynamic_array(unsigned long, item, -1)
439 #undef DECLARE_EVENT_CLASS
440 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, func, print) \
441 static struct trace_event_fields trace_event_fields_##call[] = { \
445 #undef DEFINE_EVENT_PRINT
446 #define DEFINE_EVENT_PRINT(template, name, proto, args, print)
448 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
451 * remember the offset of each array from the beginning of the event.
455 #define __entry entry
458 #define __field(type, item)
461 #define __field_ext(type, item, filter_type)
463 #undef __field_struct
464 #define __field_struct(type, item)
466 #undef __field_struct_ext
467 #define __field_struct_ext(type, item, filter_type)
470 #define __array(type, item, len)
472 #undef __dynamic_array
473 #define __dynamic_array(type, item, len) \
474 __item_length = (len) * sizeof(type); \
475 __data_offsets->item = __data_size + \
476 offsetof(typeof(*entry), __data); \
477 __data_offsets->item |= __item_length << 16; \
478 __data_size += __item_length;
481 #define __string(item, src) __dynamic_array(char, item, \
482 strlen((src) ? (const char *)(src) : "(null)") + 1)
485 * __bitmask_size_in_bytes_raw is the number of bytes needed to hold
486 * num_possible_cpus().
488 #define __bitmask_size_in_bytes_raw(nr_bits) \
489 (((nr_bits) + 7) / 8)
491 #define __bitmask_size_in_longs(nr_bits) \
492 ((__bitmask_size_in_bytes_raw(nr_bits) + \
493 ((BITS_PER_LONG / 8) - 1)) / (BITS_PER_LONG / 8))
496 * __bitmask_size_in_bytes is the number of bytes needed to hold
497 * num_possible_cpus() padded out to the nearest long. This is what
498 * is saved in the buffer, just to be consistent.
500 #define __bitmask_size_in_bytes(nr_bits) \
501 (__bitmask_size_in_longs(nr_bits) * (BITS_PER_LONG / 8))
504 #define __bitmask(item, nr_bits) __dynamic_array(unsigned long, item, \
505 __bitmask_size_in_longs(nr_bits))
507 #undef DECLARE_EVENT_CLASS
508 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
509 static inline notrace int trace_event_get_offsets_##call( \
510 struct trace_event_data_offsets_##call *__data_offsets, proto) \
512 int __data_size = 0; \
513 int __maybe_unused __item_length; \
514 struct trace_event_raw_##call __maybe_unused *entry; \
518 return __data_size; \
521 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
524 * Stage 4 of the trace events.
526 * Override the macros in the event tracepoint header <trace/events/XXX.h>
527 * to include the following:
529 * For those macros defined with TRACE_EVENT:
531 * static struct trace_event_call event_<call>;
533 * static void trace_event_raw_event_<call>(void *__data, proto)
535 * struct trace_event_file *trace_file = __data;
536 * struct trace_event_call *event_call = trace_file->event_call;
537 * struct trace_event_data_offsets_<call> __maybe_unused __data_offsets;
538 * unsigned long eflags = trace_file->flags;
539 * enum event_trigger_type __tt = ETT_NONE;
540 * struct ring_buffer_event *event;
541 * struct trace_event_raw_<call> *entry; <-- defined in stage 1
542 * struct trace_buffer *buffer;
543 * unsigned long irq_flags;
547 * if (!(eflags & EVENT_FILE_FL_TRIGGER_COND)) {
548 * if (eflags & EVENT_FILE_FL_TRIGGER_MODE)
549 * event_triggers_call(trace_file, NULL);
550 * if (eflags & EVENT_FILE_FL_SOFT_DISABLED)
554 * local_save_flags(irq_flags);
555 * pc = preempt_count();
557 * __data_size = trace_event_get_offsets_<call>(&__data_offsets, args);
559 * event = trace_event_buffer_lock_reserve(&buffer, trace_file,
560 * event_<call>->event.type,
561 * sizeof(*entry) + __data_size,
565 * entry = ring_buffer_event_data(event);
567 * { <assign>; } <-- Here we assign the entries by the __field and
570 * if (eflags & EVENT_FILE_FL_TRIGGER_COND)
571 * __tt = event_triggers_call(trace_file, entry);
573 * if (test_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT,
574 * &trace_file->flags))
575 * ring_buffer_discard_commit(buffer, event);
576 * else if (!filter_check_discard(trace_file, entry, buffer, event))
577 * trace_buffer_unlock_commit(buffer, event, irq_flags, pc);
580 * event_triggers_post_call(trace_file, __tt);
583 * static struct trace_event ftrace_event_type_<call> = {
584 * .trace = trace_raw_output_<call>, <-- stage 2
587 * static char print_fmt_<call>[] = <TP_printk>;
589 * static struct trace_event_class __used event_class_<template> = {
590 * .system = "<system>",
591 * .fields_array = trace_event_fields_<call>,
592 * .fields = LIST_HEAD_INIT(event_class_##call.fields),
593 * .raw_init = trace_event_raw_init,
594 * .probe = trace_event_raw_event_##call,
595 * .reg = trace_event_reg,
598 * static struct trace_event_call event_<call> = {
599 * .class = event_class_<template>,
601 * .tp = &__tracepoint_<call>,
603 * .event = &ftrace_event_type_<call>,
604 * .print_fmt = print_fmt_<call>,
605 * .flags = TRACE_EVENT_FL_TRACEPOINT,
607 * // its only safe to use pointers when doing linker tricks to
608 * // create an array.
609 * static struct trace_event_call __used
610 * __section("_ftrace_events") *__event_<call> = &event_<call>;
614 #ifdef CONFIG_PERF_EVENTS
616 #define _TRACE_PERF_PROTO(call, proto) \
617 static notrace void \
618 perf_trace_##call(void *__data, proto);
620 #define _TRACE_PERF_INIT(call) \
621 .perf_probe = perf_trace_##call,
624 #define _TRACE_PERF_PROTO(call, proto)
625 #define _TRACE_PERF_INIT(call)
626 #endif /* CONFIG_PERF_EVENTS */
629 #define __entry entry
632 #define __field(type, item)
634 #undef __field_struct
635 #define __field_struct(type, item)
638 #define __array(type, item, len)
640 #undef __dynamic_array
641 #define __dynamic_array(type, item, len) \
642 __entry->__data_loc_##item = __data_offsets.item;
645 #define __string(item, src) __dynamic_array(char, item, -1)
648 #define __assign_str(dst, src) \
649 strcpy(__get_str(dst), (src) ? (const char *)(src) : "(null)");
652 #define __bitmask(item, nr_bits) __dynamic_array(unsigned long, item, -1)
655 #define __get_bitmask(field) (char *)__get_dynamic_array(field)
657 #undef __assign_bitmask
658 #define __assign_bitmask(dst, src, nr_bits) \
659 memcpy(__get_bitmask(dst), (src), __bitmask_size_in_bytes(nr_bits))
661 #undef TP_fast_assign
662 #define TP_fast_assign(args...) args
665 #define __perf_count(c) (c)
668 #define __perf_task(t) (t)
670 #undef DECLARE_EVENT_CLASS
671 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
673 static notrace void \
674 trace_event_raw_event_##call(void *__data, proto) \
676 struct trace_event_file *trace_file = __data; \
677 struct trace_event_data_offsets_##call __maybe_unused __data_offsets;\
678 struct trace_event_buffer fbuffer; \
679 struct trace_event_raw_##call *entry; \
682 if (trace_trigger_soft_disabled(trace_file)) \
685 __data_size = trace_event_get_offsets_##call(&__data_offsets, args); \
687 entry = trace_event_buffer_reserve(&fbuffer, trace_file, \
688 sizeof(*entry) + __data_size); \
697 trace_event_buffer_commit(&fbuffer); \
700 * The ftrace_test_probe is compiled out, it is only here as a build time check
701 * to make sure that if the tracepoint handling changes, the ftrace probe will
702 * fail to compile unless it too is updated.
706 #define DEFINE_EVENT(template, call, proto, args) \
707 static inline void ftrace_test_probe_##call(void) \
709 check_trace_callback_type_##call(trace_event_raw_event_##template); \
712 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
718 #undef __print_symbolic
720 #undef __print_hex_str
721 #undef __get_dynamic_array
722 #undef __get_dynamic_array_len
726 #undef __print_hex_dump
729 #define TP_printk(fmt, args...) "\"" fmt "\", " __stringify(args)
731 #undef DECLARE_EVENT_CLASS
732 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
733 _TRACE_PERF_PROTO(call, PARAMS(proto)); \
734 static char print_fmt_##call[] = print; \
735 static struct trace_event_class __used __refdata event_class_##call = { \
736 .system = TRACE_SYSTEM_STRING, \
737 .fields_array = trace_event_fields_##call, \
738 .fields = LIST_HEAD_INIT(event_class_##call.fields),\
739 .raw_init = trace_event_raw_init, \
740 .probe = trace_event_raw_event_##call, \
741 .reg = trace_event_reg, \
742 _TRACE_PERF_INIT(call) \
746 #define DEFINE_EVENT(template, call, proto, args) \
748 static struct trace_event_call __used event_##call = { \
749 .class = &event_class_##template, \
751 .tp = &__tracepoint_##call, \
753 .event.funcs = &trace_event_type_funcs_##template, \
754 .print_fmt = print_fmt_##template, \
755 .flags = TRACE_EVENT_FL_TRACEPOINT, \
757 static struct trace_event_call __used \
758 __section("_ftrace_events") *__event_##call = &event_##call
760 #undef DEFINE_EVENT_PRINT
761 #define DEFINE_EVENT_PRINT(template, call, proto, args, print) \
763 static char print_fmt_##call[] = print; \
765 static struct trace_event_call __used event_##call = { \
766 .class = &event_class_##template, \
768 .tp = &__tracepoint_##call, \
770 .event.funcs = &trace_event_type_funcs_##call, \
771 .print_fmt = print_fmt_##call, \
772 .flags = TRACE_EVENT_FL_TRACEPOINT, \
774 static struct trace_event_call __used \
775 __section("_ftrace_events") *__event_##call = &event_##call
777 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)