2 * Stage 1 of the trace events.
4 * Override the macros in <trace/trace_events.h> to include the following:
6 * struct ftrace_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/ftrace_event.h>
22 * DECLARE_EVENT_CLASS can be used to add a generic function
23 * handlers for events. That is, if all events have the same
24 * parameters and just have distinct trace points.
25 * Each tracepoint can be defined with DEFINE_EVENT and that
26 * will map the DECLARE_EVENT_CLASS to the tracepoint.
28 * TRACE_EVENT is a one to one mapping between tracepoint and template.
31 #define TRACE_EVENT(name, proto, args, tstruct, assign, print) \
32 DECLARE_EVENT_CLASS(name, \
38 DEFINE_EVENT(name, name, PARAMS(proto), PARAMS(args));
42 #define __field(type, item) type item;
45 #define __field_ext(type, item, filter_type) type item;
48 #define __array(type, item, len) type item[len];
50 #undef __dynamic_array
51 #define __dynamic_array(type, item, len) u32 __data_loc_##item;
54 #define __string(item, src) __dynamic_array(char, item, -1)
56 #undef TP_STRUCT__entry
57 #define TP_STRUCT__entry(args...) args
59 #undef DECLARE_EVENT_CLASS
60 #define DECLARE_EVENT_CLASS(name, proto, args, tstruct, assign, print) \
61 struct ftrace_raw_##name { \
62 struct trace_entry ent; \
67 #define DEFINE_EVENT(template, name, proto, args) \
68 static struct ftrace_event_call \
69 __attribute__((__aligned__(4))) event_##name
71 #undef DEFINE_EVENT_PRINT
72 #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
73 DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
76 #define __cpparg(arg...) arg
78 /* Callbacks are meaningless to ftrace. */
80 #define TRACE_EVENT_FN(name, proto, args, tstruct, \
81 assign, print, reg, unreg) \
82 TRACE_EVENT(name, __cpparg(proto), __cpparg(args), \
83 __cpparg(tstruct), __cpparg(assign), __cpparg(print)) \
85 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
89 * Stage 2 of the trace events.
91 * Include the following:
93 * struct ftrace_data_offsets_<call> {
99 * The __dynamic_array() macro will create each u32 <item>, this is
100 * to keep the offset of each array from the beginning of the event.
101 * The size of an array is also encoded, in the higher 16 bits of <item>.
105 #define __field(type, item)
108 #define __field_ext(type, item, filter_type)
111 #define __array(type, item, len)
113 #undef __dynamic_array
114 #define __dynamic_array(type, item, len) u32 item;
117 #define __string(item, src) __dynamic_array(char, item, -1)
119 #undef DECLARE_EVENT_CLASS
120 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
121 struct ftrace_data_offsets_##call { \
126 #define DEFINE_EVENT(template, name, proto, 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 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
135 * Stage 3 of the trace events.
137 * Override the macros in <trace/trace_events.h> to include the following:
140 * ftrace_raw_output_<call>(struct trace_iterator *iter, int flags)
142 * struct trace_seq *s = &iter->seq;
143 * struct ftrace_raw_<call> *field; <-- defined in stage 1
144 * struct trace_entry *entry;
145 * struct trace_seq *p;
150 * if (entry->type != event_<call>.id) {
152 * return TRACE_TYPE_UNHANDLED;
155 * field = (typeof(field))entry;
157 * p = &get_cpu_var(ftrace_event_seq);
159 * ret = trace_seq_printf(s, "%s: ", <call>);
161 * ret = trace_seq_printf(s, <TP_printk> "\n");
164 * return TRACE_TYPE_PARTIAL_LINE;
166 * return TRACE_TYPE_HANDLED;
169 * This is the method used to print the raw event to the trace
170 * output format. Note, this is not needed if the data is read
175 #define __entry field
178 #define TP_printk(fmt, args...) fmt "\n", args
180 #undef __get_dynamic_array
181 #define __get_dynamic_array(field) \
182 ((void *)__entry + (__entry->__data_loc_##field & 0xffff))
185 #define __get_str(field) (char *)__get_dynamic_array(field)
188 #define __print_flags(flag, delim, flag_array...) \
190 static const struct trace_print_flags __flags[] = \
191 { flag_array, { -1, NULL }}; \
192 ftrace_print_flags_seq(p, delim, flag, __flags); \
195 #undef __print_symbolic
196 #define __print_symbolic(value, symbol_array...) \
198 static const struct trace_print_flags symbols[] = \
199 { symbol_array, { -1, NULL }}; \
200 ftrace_print_symbols_seq(p, value, symbols); \
204 #define __print_hex(buf, buf_len) ftrace_print_hex_seq(p, buf, buf_len)
206 #undef DECLARE_EVENT_CLASS
207 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
208 static notrace enum print_line_t \
209 ftrace_raw_output_id_##call(int event_id, const char *name, \
210 struct trace_iterator *iter, int flags) \
212 struct trace_seq *s = &iter->seq; \
213 struct ftrace_raw_##call *field; \
214 struct trace_entry *entry; \
215 struct trace_seq *p; \
220 if (entry->type != event_id) { \
222 return TRACE_TYPE_UNHANDLED; \
225 field = (typeof(field))entry; \
227 p = &get_cpu_var(ftrace_event_seq); \
229 ret = trace_seq_printf(s, "%s: ", name); \
231 ret = trace_seq_printf(s, print); \
234 return TRACE_TYPE_PARTIAL_LINE; \
236 return TRACE_TYPE_HANDLED; \
240 #define DEFINE_EVENT(template, name, proto, args) \
241 static notrace enum print_line_t \
242 ftrace_raw_output_##name(struct trace_iterator *iter, int flags) \
244 return ftrace_raw_output_id_##template(event_##name.id, \
245 #name, iter, flags); \
248 #undef DEFINE_EVENT_PRINT
249 #define DEFINE_EVENT_PRINT(template, call, proto, args, print) \
250 static notrace enum print_line_t \
251 ftrace_raw_output_##call(struct trace_iterator *iter, int flags) \
253 struct trace_seq *s = &iter->seq; \
254 struct ftrace_raw_##template *field; \
255 struct trace_entry *entry; \
256 struct trace_seq *p; \
261 if (entry->type != event_##call.id) { \
263 return TRACE_TYPE_UNHANDLED; \
266 field = (typeof(field))entry; \
268 p = &get_cpu_var(ftrace_event_seq); \
270 ret = trace_seq_printf(s, "%s: ", #call); \
272 ret = trace_seq_printf(s, print); \
275 return TRACE_TYPE_PARTIAL_LINE; \
277 return TRACE_TYPE_HANDLED; \
280 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
283 #define __field_ext(type, item, filter_type) \
284 ret = trace_define_field(event_call, #type, #item, \
285 offsetof(typeof(field), item), \
286 sizeof(field.item), \
287 is_signed_type(type), filter_type); \
292 #define __field(type, item) __field_ext(type, item, FILTER_OTHER)
295 #define __array(type, item, len) \
296 BUILD_BUG_ON(len > MAX_FILTER_STR_VAL); \
297 ret = trace_define_field(event_call, #type "[" #len "]", #item, \
298 offsetof(typeof(field), item), \
299 sizeof(field.item), \
300 is_signed_type(type), FILTER_OTHER); \
304 #undef __dynamic_array
305 #define __dynamic_array(type, item, len) \
306 ret = trace_define_field(event_call, "__data_loc " #type "[]", #item, \
307 offsetof(typeof(field), __data_loc_##item), \
308 sizeof(field.__data_loc_##item), \
309 is_signed_type(type), FILTER_OTHER);
312 #define __string(item, src) __dynamic_array(char, item, -1)
314 #undef DECLARE_EVENT_CLASS
315 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, func, print) \
317 ftrace_define_fields_##call(struct ftrace_event_call *event_call) \
319 struct ftrace_raw_##call field; \
328 #define DEFINE_EVENT(template, name, proto, args)
330 #undef DEFINE_EVENT_PRINT
331 #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
332 DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
334 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
337 * remember the offset of each array from the beginning of the event.
341 #define __entry entry
344 #define __field(type, item)
347 #define __field_ext(type, item, filter_type)
350 #define __array(type, item, len)
352 #undef __dynamic_array
353 #define __dynamic_array(type, item, len) \
354 __data_offsets->item = __data_size + \
355 offsetof(typeof(*entry), __data); \
356 __data_offsets->item |= (len * sizeof(type)) << 16; \
357 __data_size += (len) * sizeof(type);
360 #define __string(item, src) __dynamic_array(char, item, strlen(src) + 1)
362 #undef DECLARE_EVENT_CLASS
363 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
364 static inline notrace int ftrace_get_offsets_##call( \
365 struct ftrace_data_offsets_##call *__data_offsets, proto) \
367 int __data_size = 0; \
368 struct ftrace_raw_##call __maybe_unused *entry; \
372 return __data_size; \
376 #define DEFINE_EVENT(template, name, proto, args)
378 #undef DEFINE_EVENT_PRINT
379 #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
380 DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
382 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
384 #ifdef CONFIG_PERF_EVENTS
387 * Generate the functions needed for tracepoint perf_event support.
389 * NOTE: The insertion profile callback (ftrace_profile_<call>) is defined later
391 * static int ftrace_profile_enable_<call>(void)
393 * return register_trace_<call>(ftrace_profile_<call>);
396 * static void ftrace_profile_disable_<call>(void)
398 * unregister_trace_<call>(ftrace_profile_<call>);
403 #undef DECLARE_EVENT_CLASS
404 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print)
407 #define DEFINE_EVENT(template, name, proto, args) \
409 static void perf_trace_##name(proto); \
412 perf_trace_enable_##name(struct ftrace_event_call *unused) \
414 return register_trace_##name(perf_trace_##name); \
417 static notrace void \
418 perf_trace_disable_##name(struct ftrace_event_call *unused) \
420 unregister_trace_##name(perf_trace_##name); \
423 #undef DEFINE_EVENT_PRINT
424 #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
425 DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
427 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
429 #endif /* CONFIG_PERF_EVENTS */
432 * Stage 4 of the trace events.
434 * Override the macros in <trace/trace_events.h> to include the following:
436 * static void ftrace_event_<call>(proto)
438 * event_trace_printk(_RET_IP_, "<call>: " <fmt>);
441 * static int ftrace_reg_event_<call>(struct ftrace_event_call *unused)
443 * return register_trace_<call>(ftrace_event_<call>);
446 * static void ftrace_unreg_event_<call>(struct ftrace_event_call *unused)
448 * unregister_trace_<call>(ftrace_event_<call>);
452 * For those macros defined with TRACE_EVENT:
454 * static struct ftrace_event_call event_<call>;
456 * static void ftrace_raw_event_<call>(proto)
458 * struct ftrace_data_offsets_<call> __maybe_unused __data_offsets;
459 * struct ring_buffer_event *event;
460 * struct ftrace_raw_<call> *entry; <-- defined in stage 1
461 * struct ring_buffer *buffer;
462 * unsigned long irq_flags;
466 * local_save_flags(irq_flags);
467 * pc = preempt_count();
469 * __data_size = ftrace_get_offsets_<call>(&__data_offsets, args);
471 * event = trace_current_buffer_lock_reserve(&buffer,
473 * sizeof(*entry) + __data_size,
477 * entry = ring_buffer_event_data(event);
479 * { <assign>; } <-- Here we assign the entries by the __field and
482 * if (!filter_current_check_discard(buffer, event_call, entry, event))
483 * trace_current_buffer_unlock_commit(buffer,
484 * event, irq_flags, pc);
487 * static int ftrace_raw_reg_event_<call>(struct ftrace_event_call *unused)
489 * return register_trace_<call>(ftrace_raw_event_<call>);
492 * static void ftrace_unreg_event_<call>(struct ftrace_event_call *unused)
494 * unregister_trace_<call>(ftrace_raw_event_<call>);
497 * static struct trace_event ftrace_event_type_<call> = {
498 * .trace = ftrace_raw_output_<call>, <-- stage 2
501 * static const char print_fmt_<call>[] = <TP_printk>;
503 * static struct ftrace_event_call __used
504 * __attribute__((__aligned__(4)))
505 * __attribute__((section("_ftrace_events"))) event_<call> = {
507 * .system = "<system>",
508 * .raw_init = trace_event_raw_init,
509 * .regfunc = ftrace_reg_event_<call>,
510 * .unregfunc = ftrace_unreg_event_<call>,
511 * .print_fmt = print_fmt_<call>,
512 * .define_fields = ftrace_define_fields_<call>,
517 #ifdef CONFIG_PERF_EVENTS
519 #define _TRACE_PERF_INIT(call) \
520 .perf_event_enable = perf_trace_enable_##call, \
521 .perf_event_disable = perf_trace_disable_##call,
524 #define _TRACE_PERF_INIT(call)
525 #endif /* CONFIG_PERF_EVENTS */
528 #define __entry entry
531 #define __field(type, item)
534 #define __array(type, item, len)
536 #undef __dynamic_array
537 #define __dynamic_array(type, item, len) \
538 __entry->__data_loc_##item = __data_offsets.item;
541 #define __string(item, src) __dynamic_array(char, item, -1) \
544 #define __assign_str(dst, src) \
545 strcpy(__get_str(dst), src);
547 #undef TP_fast_assign
548 #define TP_fast_assign(args...) args
550 #undef TP_perf_assign
551 #define TP_perf_assign(args...)
553 #undef DECLARE_EVENT_CLASS
554 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
556 static notrace void \
557 ftrace_raw_event_id_##call(struct ftrace_event_call *event_call, \
560 struct ftrace_data_offsets_##call __maybe_unused __data_offsets;\
561 struct ring_buffer_event *event; \
562 struct ftrace_raw_##call *entry; \
563 struct ring_buffer *buffer; \
564 unsigned long irq_flags; \
568 local_save_flags(irq_flags); \
569 pc = preempt_count(); \
571 __data_size = ftrace_get_offsets_##call(&__data_offsets, args); \
573 event = trace_current_buffer_lock_reserve(&buffer, \
575 sizeof(*entry) + __data_size, \
579 entry = ring_buffer_event_data(event); \
585 if (!filter_current_check_discard(buffer, event_call, entry, event)) \
586 trace_nowake_buffer_unlock_commit(buffer, \
587 event, irq_flags, pc); \
591 #define DEFINE_EVENT(template, call, proto, args) \
593 static notrace void ftrace_raw_event_##call(proto) \
595 ftrace_raw_event_id_##template(&event_##call, args); \
599 ftrace_raw_reg_event_##call(struct ftrace_event_call *unused) \
601 return register_trace_##call(ftrace_raw_event_##call); \
604 static notrace void \
605 ftrace_raw_unreg_event_##call(struct ftrace_event_call *unused) \
607 unregister_trace_##call(ftrace_raw_event_##call); \
610 static struct trace_event ftrace_event_type_##call = { \
611 .trace = ftrace_raw_output_##call, \
614 #undef DEFINE_EVENT_PRINT
615 #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
616 DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
618 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
624 #undef __print_symbolic
625 #undef __get_dynamic_array
629 #define TP_printk(fmt, args...) "\"" fmt "\", " __stringify(args)
631 #undef DECLARE_EVENT_CLASS
632 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
633 static const char print_fmt_##call[] = print;
636 #define DEFINE_EVENT(template, call, proto, args) \
638 static struct ftrace_event_call __used \
639 __attribute__((__aligned__(4))) \
640 __attribute__((section("_ftrace_events"))) event_##call = { \
642 .system = __stringify(TRACE_SYSTEM), \
643 .event = &ftrace_event_type_##call, \
644 .raw_init = trace_event_raw_init, \
645 .regfunc = ftrace_raw_reg_event_##call, \
646 .unregfunc = ftrace_raw_unreg_event_##call, \
647 .print_fmt = print_fmt_##template, \
648 .define_fields = ftrace_define_fields_##template, \
649 _TRACE_PERF_INIT(call) \
652 #undef DEFINE_EVENT_PRINT
653 #define DEFINE_EVENT_PRINT(template, call, proto, args, print) \
655 static const char print_fmt_##call[] = print; \
657 static struct ftrace_event_call __used \
658 __attribute__((__aligned__(4))) \
659 __attribute__((section("_ftrace_events"))) event_##call = { \
661 .system = __stringify(TRACE_SYSTEM), \
662 .event = &ftrace_event_type_##call, \
663 .raw_init = trace_event_raw_init, \
664 .regfunc = ftrace_raw_reg_event_##call, \
665 .unregfunc = ftrace_raw_unreg_event_##call, \
666 .print_fmt = print_fmt_##call, \
667 .define_fields = ftrace_define_fields_##template, \
668 _TRACE_PERF_INIT(call) \
671 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
674 * Define the insertion callback to perf events
676 * The job is very similar to ftrace_raw_event_<call> except that we don't
677 * insert in the ring buffer but in a perf counter.
679 * static void ftrace_perf_<call>(proto)
681 * struct ftrace_data_offsets_<call> __maybe_unused __data_offsets;
682 * struct ftrace_event_call *event_call = &event_<call>;
683 * extern void perf_tp_event(int, u64, u64, void *, int);
684 * struct ftrace_raw_##call *entry;
685 * struct perf_trace_buf *trace_buf;
686 * u64 __addr = 0, __count = 1;
687 * unsigned long irq_flags;
688 * struct trace_entry *ent;
694 * pc = preempt_count();
696 * __data_size = ftrace_get_offsets_<call>(&__data_offsets, args);
698 * // Below we want to get the aligned size by taking into account
699 * // the u32 field that will later store the buffer size
700 * __entry_size = ALIGN(__data_size + sizeof(*entry) + sizeof(u32),
702 * __entry_size -= sizeof(u32);
704 * // Protect the non nmi buffer
705 * // This also protects the rcu read side
706 * local_irq_save(irq_flags);
707 * __cpu = smp_processor_id();
710 * trace_buf = rcu_dereference_sched(perf_trace_buf_nmi);
712 * trace_buf = rcu_dereference_sched(perf_trace_buf);
717 * trace_buf = per_cpu_ptr(trace_buf, __cpu);
719 * // Avoid recursion from perf that could mess up the buffer
720 * if (trace_buf->recursion++)
721 * goto end_recursion;
723 * raw_data = trace_buf->buf;
725 * // Make recursion update visible before entering perf_tp_event
726 * // so that we protect from perf recursions.
730 * //zero dead bytes from alignment to avoid stack leak to userspace:
731 * *(u64 *)(&raw_data[__entry_size - sizeof(u64)]) = 0ULL;
732 * entry = (struct ftrace_raw_<call> *)raw_data;
734 * tracing_generic_entry_update(ent, irq_flags, pc);
735 * ent->type = event_call->id;
737 * <tstruct> <- do some jobs with dynamic arrays
739 * <assign> <- affect our values
741 * perf_tp_event(event_call->id, __addr, __count, entry,
742 * __entry_size); <- submit them to perf counter
747 #ifdef CONFIG_PERF_EVENTS
750 #define __entry entry
752 #undef __get_dynamic_array
753 #define __get_dynamic_array(field) \
754 ((void *)__entry + (__entry->__data_loc_##field & 0xffff))
757 #define __get_str(field) (char *)__get_dynamic_array(field)
760 #define __perf_addr(a) __addr = (a)
763 #define __perf_count(c) __count = (c)
765 #undef DECLARE_EVENT_CLASS
766 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
767 static notrace void \
768 perf_trace_templ_##call(struct ftrace_event_call *event_call, \
769 struct pt_regs *__regs, proto) \
771 struct ftrace_data_offsets_##call __maybe_unused __data_offsets;\
772 struct ftrace_raw_##call *entry; \
773 u64 __addr = 0, __count = 1; \
774 unsigned long irq_flags; \
779 __data_size = ftrace_get_offsets_##call(&__data_offsets, args); \
780 __entry_size = ALIGN(__data_size + sizeof(*entry) + sizeof(u32),\
782 __entry_size -= sizeof(u32); \
784 if (WARN_ONCE(__entry_size > PERF_MAX_TRACE_SIZE, \
785 "profile buffer not large enough")) \
787 entry = (struct ftrace_raw_##call *)perf_trace_buf_prepare( \
788 __entry_size, event_call->id, &rctx, &irq_flags); \
795 perf_trace_buf_submit(entry, __entry_size, rctx, __addr, \
796 __count, irq_flags, __regs); \
800 #define DEFINE_EVENT(template, call, proto, args) \
801 static notrace void perf_trace_##call(proto) \
803 struct ftrace_event_call *event_call = &event_##call; \
804 struct pt_regs *__regs = &get_cpu_var(perf_trace_regs); \
806 perf_fetch_caller_regs(__regs, 1); \
808 perf_trace_templ_##template(event_call, __regs, args); \
810 put_cpu_var(perf_trace_regs); \
813 #undef DEFINE_EVENT_PRINT
814 #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
815 DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
817 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
818 #endif /* CONFIG_PERF_EVENTS */
820 #undef _TRACE_PROFILE_INIT