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); \
203 #undef DECLARE_EVENT_CLASS
204 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
205 static notrace enum print_line_t \
206 ftrace_raw_output_id_##call(int event_id, const char *name, \
207 struct trace_iterator *iter, int flags) \
209 struct trace_seq *s = &iter->seq; \
210 struct ftrace_raw_##call *field; \
211 struct trace_entry *entry; \
212 struct trace_seq *p; \
217 if (entry->type != event_id) { \
219 return TRACE_TYPE_UNHANDLED; \
222 field = (typeof(field))entry; \
224 p = &get_cpu_var(ftrace_event_seq); \
226 ret = trace_seq_printf(s, "%s: ", name); \
228 ret = trace_seq_printf(s, print); \
231 return TRACE_TYPE_PARTIAL_LINE; \
233 return TRACE_TYPE_HANDLED; \
237 #define DEFINE_EVENT(template, name, proto, args) \
238 static notrace enum print_line_t \
239 ftrace_raw_output_##name(struct trace_iterator *iter, int flags) \
241 return ftrace_raw_output_id_##template(event_##name.id, \
242 #name, iter, flags); \
245 #undef DEFINE_EVENT_PRINT
246 #define DEFINE_EVENT_PRINT(template, call, proto, args, print) \
247 static notrace enum print_line_t \
248 ftrace_raw_output_##call(struct trace_iterator *iter, int flags) \
250 struct trace_seq *s = &iter->seq; \
251 struct ftrace_raw_##template *field; \
252 struct trace_entry *entry; \
253 struct trace_seq *p; \
258 if (entry->type != event_##call.id) { \
260 return TRACE_TYPE_UNHANDLED; \
263 field = (typeof(field))entry; \
265 p = &get_cpu_var(ftrace_event_seq); \
267 ret = trace_seq_printf(s, "%s: ", #call); \
269 ret = trace_seq_printf(s, print); \
272 return TRACE_TYPE_PARTIAL_LINE; \
274 return TRACE_TYPE_HANDLED; \
277 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
280 #define __field_ext(type, item, filter_type) \
281 ret = trace_define_field(event_call, #type, #item, \
282 offsetof(typeof(field), item), \
283 sizeof(field.item), \
284 is_signed_type(type), filter_type); \
289 #define __field(type, item) __field_ext(type, item, FILTER_OTHER)
292 #define __array(type, item, len) \
293 BUILD_BUG_ON(len > MAX_FILTER_STR_VAL); \
294 ret = trace_define_field(event_call, #type "[" #len "]", #item, \
295 offsetof(typeof(field), item), \
296 sizeof(field.item), \
297 is_signed_type(type), FILTER_OTHER); \
301 #undef __dynamic_array
302 #define __dynamic_array(type, item, len) \
303 ret = trace_define_field(event_call, "__data_loc " #type "[]", #item, \
304 offsetof(typeof(field), __data_loc_##item), \
305 sizeof(field.__data_loc_##item), \
306 is_signed_type(type), FILTER_OTHER);
309 #define __string(item, src) __dynamic_array(char, item, -1)
311 #undef DECLARE_EVENT_CLASS
312 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, func, print) \
314 ftrace_define_fields_##call(struct ftrace_event_call *event_call) \
316 struct ftrace_raw_##call field; \
325 #define DEFINE_EVENT(template, name, proto, args)
327 #undef DEFINE_EVENT_PRINT
328 #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
329 DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
331 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
334 * remember the offset of each array from the beginning of the event.
338 #define __entry entry
341 #define __field(type, item)
344 #define __field_ext(type, item, filter_type)
347 #define __array(type, item, len)
349 #undef __dynamic_array
350 #define __dynamic_array(type, item, len) \
351 __data_offsets->item = __data_size + \
352 offsetof(typeof(*entry), __data); \
353 __data_offsets->item |= (len * sizeof(type)) << 16; \
354 __data_size += (len) * sizeof(type);
357 #define __string(item, src) __dynamic_array(char, item, strlen(src) + 1)
359 #undef DECLARE_EVENT_CLASS
360 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
361 static inline notrace int ftrace_get_offsets_##call( \
362 struct ftrace_data_offsets_##call *__data_offsets, proto) \
364 int __data_size = 0; \
365 struct ftrace_raw_##call __maybe_unused *entry; \
369 return __data_size; \
373 #define DEFINE_EVENT(template, name, proto, args)
375 #undef DEFINE_EVENT_PRINT
376 #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
377 DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
379 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
381 #ifdef CONFIG_PERF_EVENTS
384 * Generate the functions needed for tracepoint perf_event support.
386 * NOTE: The insertion profile callback (ftrace_profile_<call>) is defined later
388 * static int ftrace_profile_enable_<call>(void)
390 * return register_trace_<call>(ftrace_profile_<call>);
393 * static void ftrace_profile_disable_<call>(void)
395 * unregister_trace_<call>(ftrace_profile_<call>);
400 #undef DECLARE_EVENT_CLASS
401 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print)
404 #define DEFINE_EVENT(template, name, proto, args) \
406 static void perf_trace_##name(proto); \
409 perf_trace_enable_##name(struct ftrace_event_call *unused) \
411 return register_trace_##name(perf_trace_##name); \
414 static notrace void \
415 perf_trace_disable_##name(struct ftrace_event_call *unused) \
417 unregister_trace_##name(perf_trace_##name); \
420 #undef DEFINE_EVENT_PRINT
421 #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
422 DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
424 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
426 #endif /* CONFIG_PERF_EVENTS */
429 * Stage 4 of the trace events.
431 * Override the macros in <trace/trace_events.h> to include the following:
433 * static void ftrace_event_<call>(proto)
435 * event_trace_printk(_RET_IP_, "<call>: " <fmt>);
438 * static int ftrace_reg_event_<call>(struct ftrace_event_call *unused)
440 * return register_trace_<call>(ftrace_event_<call>);
443 * static void ftrace_unreg_event_<call>(struct ftrace_event_call *unused)
445 * unregister_trace_<call>(ftrace_event_<call>);
449 * For those macros defined with TRACE_EVENT:
451 * static struct ftrace_event_call event_<call>;
453 * static void ftrace_raw_event_<call>(proto)
455 * struct ftrace_data_offsets_<call> __maybe_unused __data_offsets;
456 * struct ring_buffer_event *event;
457 * struct ftrace_raw_<call> *entry; <-- defined in stage 1
458 * struct ring_buffer *buffer;
459 * unsigned long irq_flags;
463 * local_save_flags(irq_flags);
464 * pc = preempt_count();
466 * __data_size = ftrace_get_offsets_<call>(&__data_offsets, args);
468 * event = trace_current_buffer_lock_reserve(&buffer,
470 * sizeof(*entry) + __data_size,
474 * entry = ring_buffer_event_data(event);
476 * { <assign>; } <-- Here we assign the entries by the __field and
479 * if (!filter_current_check_discard(buffer, event_call, entry, event))
480 * trace_current_buffer_unlock_commit(buffer,
481 * event, irq_flags, pc);
484 * static int ftrace_raw_reg_event_<call>(struct ftrace_event_call *unused)
486 * return register_trace_<call>(ftrace_raw_event_<call>);
489 * static void ftrace_unreg_event_<call>(struct ftrace_event_call *unused)
491 * unregister_trace_<call>(ftrace_raw_event_<call>);
494 * static struct trace_event ftrace_event_type_<call> = {
495 * .trace = ftrace_raw_output_<call>, <-- stage 2
498 * static const char print_fmt_<call>[] = <TP_printk>;
500 * static struct ftrace_event_call __used
501 * __attribute__((__aligned__(4)))
502 * __attribute__((section("_ftrace_events"))) event_<call> = {
504 * .system = "<system>",
505 * .raw_init = trace_event_raw_init,
506 * .regfunc = ftrace_reg_event_<call>,
507 * .unregfunc = ftrace_unreg_event_<call>,
508 * .print_fmt = print_fmt_<call>,
509 * .define_fields = ftrace_define_fields_<call>,
514 #ifdef CONFIG_PERF_EVENTS
516 #define _TRACE_PERF_INIT(call) \
517 .perf_event_enable = perf_trace_enable_##call, \
518 .perf_event_disable = perf_trace_disable_##call,
521 #define _TRACE_PERF_INIT(call)
522 #endif /* CONFIG_PERF_EVENTS */
525 #define __entry entry
528 #define __field(type, item)
531 #define __array(type, item, len)
533 #undef __dynamic_array
534 #define __dynamic_array(type, item, len) \
535 __entry->__data_loc_##item = __data_offsets.item;
538 #define __string(item, src) __dynamic_array(char, item, -1) \
541 #define __assign_str(dst, src) \
542 strcpy(__get_str(dst), src);
544 #undef TP_fast_assign
545 #define TP_fast_assign(args...) args
547 #undef TP_perf_assign
548 #define TP_perf_assign(args...)
550 #undef DECLARE_EVENT_CLASS
551 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
553 static notrace void \
554 ftrace_raw_event_id_##call(struct ftrace_event_call *event_call, \
557 struct ftrace_data_offsets_##call __maybe_unused __data_offsets;\
558 struct ring_buffer_event *event; \
559 struct ftrace_raw_##call *entry; \
560 struct ring_buffer *buffer; \
561 unsigned long irq_flags; \
565 local_save_flags(irq_flags); \
566 pc = preempt_count(); \
568 __data_size = ftrace_get_offsets_##call(&__data_offsets, args); \
570 event = trace_current_buffer_lock_reserve(&buffer, \
572 sizeof(*entry) + __data_size, \
576 entry = ring_buffer_event_data(event); \
582 if (!filter_current_check_discard(buffer, event_call, entry, event)) \
583 trace_nowake_buffer_unlock_commit(buffer, \
584 event, irq_flags, pc); \
588 #define DEFINE_EVENT(template, call, proto, args) \
590 static notrace void ftrace_raw_event_##call(proto) \
592 ftrace_raw_event_id_##template(&event_##call, args); \
596 ftrace_raw_reg_event_##call(struct ftrace_event_call *unused) \
598 return register_trace_##call(ftrace_raw_event_##call); \
601 static notrace void \
602 ftrace_raw_unreg_event_##call(struct ftrace_event_call *unused) \
604 unregister_trace_##call(ftrace_raw_event_##call); \
607 static struct trace_event ftrace_event_type_##call = { \
608 .trace = ftrace_raw_output_##call, \
611 #undef DEFINE_EVENT_PRINT
612 #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
613 DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
615 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
621 #undef __print_symbolic
622 #undef __get_dynamic_array
626 #define TP_printk(fmt, args...) "\"" fmt "\", " __stringify(args)
628 #undef DECLARE_EVENT_CLASS
629 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
630 static const char print_fmt_##call[] = print;
633 #define DEFINE_EVENT(template, call, proto, args) \
635 static struct ftrace_event_call __used \
636 __attribute__((__aligned__(4))) \
637 __attribute__((section("_ftrace_events"))) event_##call = { \
639 .system = __stringify(TRACE_SYSTEM), \
640 .event = &ftrace_event_type_##call, \
641 .raw_init = trace_event_raw_init, \
642 .regfunc = ftrace_raw_reg_event_##call, \
643 .unregfunc = ftrace_raw_unreg_event_##call, \
644 .print_fmt = print_fmt_##template, \
645 .define_fields = ftrace_define_fields_##template, \
646 _TRACE_PERF_INIT(call) \
649 #undef DEFINE_EVENT_PRINT
650 #define DEFINE_EVENT_PRINT(template, call, proto, args, print) \
652 static const char print_fmt_##call[] = print; \
654 static struct ftrace_event_call __used \
655 __attribute__((__aligned__(4))) \
656 __attribute__((section("_ftrace_events"))) event_##call = { \
658 .system = __stringify(TRACE_SYSTEM), \
659 .event = &ftrace_event_type_##call, \
660 .raw_init = trace_event_raw_init, \
661 .regfunc = ftrace_raw_reg_event_##call, \
662 .unregfunc = ftrace_raw_unreg_event_##call, \
663 .print_fmt = print_fmt_##call, \
664 .define_fields = ftrace_define_fields_##template, \
665 _TRACE_PERF_INIT(call) \
668 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
671 * Define the insertion callback to perf events
673 * The job is very similar to ftrace_raw_event_<call> except that we don't
674 * insert in the ring buffer but in a perf counter.
676 * static void ftrace_perf_<call>(proto)
678 * struct ftrace_data_offsets_<call> __maybe_unused __data_offsets;
679 * struct ftrace_event_call *event_call = &event_<call>;
680 * extern void perf_tp_event(int, u64, u64, void *, int);
681 * struct ftrace_raw_##call *entry;
682 * struct perf_trace_buf *trace_buf;
683 * u64 __addr = 0, __count = 1;
684 * unsigned long irq_flags;
685 * struct trace_entry *ent;
691 * pc = preempt_count();
693 * __data_size = ftrace_get_offsets_<call>(&__data_offsets, args);
695 * // Below we want to get the aligned size by taking into account
696 * // the u32 field that will later store the buffer size
697 * __entry_size = ALIGN(__data_size + sizeof(*entry) + sizeof(u32),
699 * __entry_size -= sizeof(u32);
701 * // Protect the non nmi buffer
702 * // This also protects the rcu read side
703 * local_irq_save(irq_flags);
704 * __cpu = smp_processor_id();
707 * trace_buf = rcu_dereference_sched(perf_trace_buf_nmi);
709 * trace_buf = rcu_dereference_sched(perf_trace_buf);
714 * trace_buf = per_cpu_ptr(trace_buf, __cpu);
716 * // Avoid recursion from perf that could mess up the buffer
717 * if (trace_buf->recursion++)
718 * goto end_recursion;
720 * raw_data = trace_buf->buf;
722 * // Make recursion update visible before entering perf_tp_event
723 * // so that we protect from perf recursions.
727 * //zero dead bytes from alignment to avoid stack leak to userspace:
728 * *(u64 *)(&raw_data[__entry_size - sizeof(u64)]) = 0ULL;
729 * entry = (struct ftrace_raw_<call> *)raw_data;
731 * tracing_generic_entry_update(ent, irq_flags, pc);
732 * ent->type = event_call->id;
734 * <tstruct> <- do some jobs with dynamic arrays
736 * <assign> <- affect our values
738 * perf_tp_event(event_call->id, __addr, __count, entry,
739 * __entry_size); <- submit them to perf counter
744 #ifdef CONFIG_PERF_EVENTS
747 #define __entry entry
749 #undef __get_dynamic_array
750 #define __get_dynamic_array(field) \
751 ((void *)__entry + (__entry->__data_loc_##field & 0xffff))
754 #define __get_str(field) (char *)__get_dynamic_array(field)
757 #define __perf_addr(a) __addr = (a)
760 #define __perf_count(c) __count = (c)
762 #undef DECLARE_EVENT_CLASS
763 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
764 static notrace void \
765 perf_trace_templ_##call(struct ftrace_event_call *event_call, \
766 struct pt_regs *__regs, proto) \
768 struct ftrace_data_offsets_##call __maybe_unused __data_offsets;\
769 struct ftrace_raw_##call *entry; \
770 u64 __addr = 0, __count = 1; \
771 unsigned long irq_flags; \
776 __data_size = ftrace_get_offsets_##call(&__data_offsets, args); \
777 __entry_size = ALIGN(__data_size + sizeof(*entry) + sizeof(u32),\
779 __entry_size -= sizeof(u32); \
781 if (WARN_ONCE(__entry_size > PERF_MAX_TRACE_SIZE, \
782 "profile buffer not large enough")) \
784 entry = (struct ftrace_raw_##call *)perf_trace_buf_prepare( \
785 __entry_size, event_call->id, &rctx, &irq_flags); \
792 perf_trace_buf_submit(entry, __entry_size, rctx, __addr, \
793 __count, irq_flags, __regs); \
797 #define DEFINE_EVENT(template, call, proto, args) \
798 static notrace void perf_trace_##call(proto) \
800 struct ftrace_event_call *event_call = &event_##call; \
801 struct pt_regs *__regs = &get_cpu_var(perf_trace_regs); \
803 perf_fetch_caller_regs(__regs, 1); \
805 perf_trace_templ_##template(event_call, __regs, args); \
807 put_cpu_var(perf_trace_regs); \
810 #undef DEFINE_EVENT_PRINT
811 #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
812 DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
814 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
815 #endif /* CONFIG_PERF_EVENTS */
817 #undef _TRACE_PROFILE_INIT