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, <TP_printk> "\n");
162 * return TRACE_TYPE_PARTIAL_LINE;
164 * return TRACE_TYPE_HANDLED;
167 * This is the method used to print the raw event to the trace
168 * output format. Note, this is not needed if the data is read
173 #define __entry field
176 #define TP_printk(fmt, args...) fmt "\n", args
178 #undef __get_dynamic_array
179 #define __get_dynamic_array(field) \
180 ((void *)__entry + (__entry->__data_loc_##field & 0xffff))
183 #define __get_str(field) (char *)__get_dynamic_array(field)
186 #define __print_flags(flag, delim, flag_array...) \
188 static const struct trace_print_flags __flags[] = \
189 { flag_array, { -1, NULL }}; \
190 ftrace_print_flags_seq(p, delim, flag, __flags); \
193 #undef __print_symbolic
194 #define __print_symbolic(value, symbol_array...) \
196 static const struct trace_print_flags symbols[] = \
197 { symbol_array, { -1, NULL }}; \
198 ftrace_print_symbols_seq(p, value, symbols); \
201 #undef DECLARE_EVENT_CLASS
202 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
203 static notrace enum print_line_t \
204 ftrace_raw_output_id_##call(int event_id, const char *name, \
205 struct trace_iterator *iter, int flags) \
207 struct trace_seq *s = &iter->seq; \
208 struct ftrace_raw_##call *field; \
209 struct trace_entry *entry; \
210 struct trace_seq *p; \
215 if (entry->type != event_id) { \
217 return TRACE_TYPE_UNHANDLED; \
220 field = (typeof(field))entry; \
222 p = &get_cpu_var(ftrace_event_seq); \
224 ret = trace_seq_printf(s, "%s: ", name); \
226 ret = trace_seq_printf(s, print); \
229 return TRACE_TYPE_PARTIAL_LINE; \
231 return TRACE_TYPE_HANDLED; \
235 #define DEFINE_EVENT(template, name, proto, args) \
236 static notrace enum print_line_t \
237 ftrace_raw_output_##name(struct trace_iterator *iter, int flags) \
239 return ftrace_raw_output_id_##template(event_##name.id, \
240 #name, iter, flags); \
243 #undef DEFINE_EVENT_PRINT
244 #define DEFINE_EVENT_PRINT(template, call, proto, args, print) \
245 static notrace enum print_line_t \
246 ftrace_raw_output_##call(struct trace_iterator *iter, int flags) \
248 struct trace_seq *s = &iter->seq; \
249 struct ftrace_raw_##template *field; \
250 struct trace_entry *entry; \
251 struct trace_seq *p; \
256 if (entry->type != event_##call.id) { \
258 return TRACE_TYPE_UNHANDLED; \
261 field = (typeof(field))entry; \
263 p = &get_cpu_var(ftrace_event_seq); \
265 ret = trace_seq_printf(s, "%s: ", #call); \
267 ret = trace_seq_printf(s, print); \
270 return TRACE_TYPE_PARTIAL_LINE; \
272 return TRACE_TYPE_HANDLED; \
275 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
278 #define __field_ext(type, item, filter_type) \
279 ret = trace_define_field(event_call, #type, #item, \
280 offsetof(typeof(field), item), \
281 sizeof(field.item), \
282 is_signed_type(type), filter_type); \
287 #define __field(type, item) __field_ext(type, item, FILTER_OTHER)
290 #define __array(type, item, len) \
291 BUILD_BUG_ON(len > MAX_FILTER_STR_VAL); \
292 ret = trace_define_field(event_call, #type "[" #len "]", #item, \
293 offsetof(typeof(field), item), \
294 sizeof(field.item), \
295 is_signed_type(type), FILTER_OTHER); \
299 #undef __dynamic_array
300 #define __dynamic_array(type, item, len) \
301 ret = trace_define_field(event_call, "__data_loc " #type "[]", #item, \
302 offsetof(typeof(field), __data_loc_##item), \
303 sizeof(field.__data_loc_##item), \
304 is_signed_type(type), FILTER_OTHER);
307 #define __string(item, src) __dynamic_array(char, item, -1)
309 #undef DECLARE_EVENT_CLASS
310 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, func, print) \
312 ftrace_define_fields_##call(struct ftrace_event_call *event_call) \
314 struct ftrace_raw_##call field; \
323 #define DEFINE_EVENT(template, name, proto, args)
325 #undef DEFINE_EVENT_PRINT
326 #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
327 DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
329 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
332 * remember the offset of each array from the beginning of the event.
336 #define __entry entry
339 #define __field(type, item)
342 #define __field_ext(type, item, filter_type)
345 #define __array(type, item, len)
347 #undef __dynamic_array
348 #define __dynamic_array(type, item, len) \
349 __data_offsets->item = __data_size + \
350 offsetof(typeof(*entry), __data); \
351 __data_offsets->item |= (len * sizeof(type)) << 16; \
352 __data_size += (len) * sizeof(type);
355 #define __string(item, src) __dynamic_array(char, item, strlen(src) + 1)
357 #undef DECLARE_EVENT_CLASS
358 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
359 static inline notrace int ftrace_get_offsets_##call( \
360 struct ftrace_data_offsets_##call *__data_offsets, proto) \
362 int __data_size = 0; \
363 struct ftrace_raw_##call __maybe_unused *entry; \
367 return __data_size; \
371 #define DEFINE_EVENT(template, name, proto, args)
373 #undef DEFINE_EVENT_PRINT
374 #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
375 DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
377 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
379 #ifdef CONFIG_PERF_EVENTS
382 * Generate the functions needed for tracepoint perf_event support.
384 * NOTE: The insertion profile callback (ftrace_profile_<call>) is defined later
386 * static int ftrace_profile_enable_<call>(void)
388 * return register_trace_<call>(ftrace_profile_<call>);
391 * static void ftrace_profile_disable_<call>(void)
393 * unregister_trace_<call>(ftrace_profile_<call>);
398 #undef DECLARE_EVENT_CLASS
399 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print)
402 #define DEFINE_EVENT(template, name, proto, args) \
404 static void ftrace_profile_##name(proto); \
407 ftrace_profile_enable_##name(struct ftrace_event_call *unused) \
409 return register_trace_##name(ftrace_profile_##name); \
412 static notrace void \
413 ftrace_profile_disable_##name(struct ftrace_event_call *unused) \
415 unregister_trace_##name(ftrace_profile_##name); \
418 #undef DEFINE_EVENT_PRINT
419 #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
420 DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
422 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
424 #endif /* CONFIG_PERF_EVENTS */
427 * Stage 4 of the trace events.
429 * Override the macros in <trace/trace_events.h> to include the following:
431 * static void ftrace_event_<call>(proto)
433 * event_trace_printk(_RET_IP_, "<call>: " <fmt>);
436 * static int ftrace_reg_event_<call>(struct ftrace_event_call *unused)
438 * return register_trace_<call>(ftrace_event_<call>);
441 * static void ftrace_unreg_event_<call>(struct ftrace_event_call *unused)
443 * unregister_trace_<call>(ftrace_event_<call>);
447 * For those macros defined with TRACE_EVENT:
449 * static struct ftrace_event_call event_<call>;
451 * static void ftrace_raw_event_<call>(proto)
453 * struct ring_buffer_event *event;
454 * struct ftrace_raw_<call> *entry; <-- defined in stage 1
455 * struct ring_buffer *buffer;
456 * unsigned long irq_flags;
459 * local_save_flags(irq_flags);
460 * pc = preempt_count();
462 * event = trace_current_buffer_lock_reserve(&buffer,
464 * sizeof(struct ftrace_raw_<call>),
468 * entry = ring_buffer_event_data(event);
470 * <assign>; <-- Here we assign the entries by the __field and
473 * trace_current_buffer_unlock_commit(buffer, event, irq_flags, pc);
476 * static int ftrace_raw_reg_event_<call>(struct ftrace_event_call *unused)
480 * ret = register_trace_<call>(ftrace_raw_event_<call>);
482 * pr_info("event trace: Could not activate trace point "
483 * "probe to <call>");
487 * static void ftrace_unreg_event_<call>(struct ftrace_event_call *unused)
489 * unregister_trace_<call>(ftrace_raw_event_<call>);
492 * static struct trace_event ftrace_event_type_<call> = {
493 * .trace = ftrace_raw_output_<call>, <-- stage 2
496 * static struct ftrace_event_call __used
497 * __attribute__((__aligned__(4)))
498 * __attribute__((section("_ftrace_events"))) event_<call> = {
500 * .system = "<system>",
501 * .raw_init = trace_event_raw_init,
502 * .regfunc = ftrace_reg_event_<call>,
503 * .unregfunc = ftrace_unreg_event_<call>,
508 #ifdef CONFIG_PERF_EVENTS
510 #define _TRACE_PROFILE_INIT(call) \
511 .profile_enable = ftrace_profile_enable_##call, \
512 .profile_disable = ftrace_profile_disable_##call,
515 #define _TRACE_PROFILE_INIT(call)
516 #endif /* CONFIG_PERF_EVENTS */
519 #define __entry entry
522 #define __field(type, item)
525 #define __array(type, item, len)
527 #undef __dynamic_array
528 #define __dynamic_array(type, item, len) \
529 __entry->__data_loc_##item = __data_offsets.item;
532 #define __string(item, src) __dynamic_array(char, item, -1) \
535 #define __assign_str(dst, src) \
536 strcpy(__get_str(dst), src);
538 #undef TP_fast_assign
539 #define TP_fast_assign(args...) args
541 #undef TP_perf_assign
542 #define TP_perf_assign(args...)
544 #undef DECLARE_EVENT_CLASS
545 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
547 static notrace void \
548 ftrace_raw_event_id_##call(struct ftrace_event_call *event_call, \
551 struct ftrace_data_offsets_##call __maybe_unused __data_offsets;\
552 struct ring_buffer_event *event; \
553 struct ftrace_raw_##call *entry; \
554 struct ring_buffer *buffer; \
555 unsigned long irq_flags; \
559 local_save_flags(irq_flags); \
560 pc = preempt_count(); \
562 __data_size = ftrace_get_offsets_##call(&__data_offsets, args); \
564 event = trace_current_buffer_lock_reserve(&buffer, \
566 sizeof(*entry) + __data_size, \
570 entry = ring_buffer_event_data(event); \
577 if (!filter_current_check_discard(buffer, event_call, entry, event)) \
578 trace_nowake_buffer_unlock_commit(buffer, \
579 event, irq_flags, pc); \
583 #define DEFINE_EVENT(template, call, proto, args) \
585 static notrace void ftrace_raw_event_##call(proto) \
587 ftrace_raw_event_id_##template(&event_##call, args); \
591 ftrace_raw_reg_event_##call(struct ftrace_event_call *unused) \
593 return register_trace_##call(ftrace_raw_event_##call); \
596 static notrace void \
597 ftrace_raw_unreg_event_##call(struct ftrace_event_call *unused) \
599 unregister_trace_##call(ftrace_raw_event_##call); \
602 static struct trace_event ftrace_event_type_##call = { \
603 .trace = ftrace_raw_output_##call, \
606 #undef DEFINE_EVENT_PRINT
607 #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
608 DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
610 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
616 #undef __print_symbolic
617 #undef __get_dynamic_array
621 #define TP_printk(fmt, args...) "\"" fmt "\", " __stringify(args)
623 #undef DECLARE_EVENT_CLASS
624 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
625 static const char print_fmt_##call[] = print;
628 #define DEFINE_EVENT(template, call, proto, args) \
630 static struct ftrace_event_call __used \
631 __attribute__((__aligned__(4))) \
632 __attribute__((section("_ftrace_events"))) event_##call = { \
634 .system = __stringify(TRACE_SYSTEM), \
635 .event = &ftrace_event_type_##call, \
636 .raw_init = trace_event_raw_init, \
637 .regfunc = ftrace_raw_reg_event_##call, \
638 .unregfunc = ftrace_raw_unreg_event_##call, \
639 .print_fmt = print_fmt_##template, \
640 .define_fields = ftrace_define_fields_##template, \
641 _TRACE_PROFILE_INIT(call) \
644 #undef DEFINE_EVENT_PRINT
645 #define DEFINE_EVENT_PRINT(template, call, proto, args, print) \
647 static const char print_fmt_##call[] = print; \
649 static struct ftrace_event_call __used \
650 __attribute__((__aligned__(4))) \
651 __attribute__((section("_ftrace_events"))) event_##call = { \
653 .system = __stringify(TRACE_SYSTEM), \
654 .event = &ftrace_event_type_##call, \
655 .raw_init = trace_event_raw_init, \
656 .regfunc = ftrace_raw_reg_event_##call, \
657 .unregfunc = ftrace_raw_unreg_event_##call, \
658 .print_fmt = print_fmt_##call, \
659 .define_fields = ftrace_define_fields_##template, \
660 _TRACE_PROFILE_INIT(call) \
663 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
666 * Define the insertion callback to profile events
668 * The job is very similar to ftrace_raw_event_<call> except that we don't
669 * insert in the ring buffer but in a perf counter.
671 * static void ftrace_profile_<call>(proto)
673 * struct ftrace_data_offsets_<call> __maybe_unused __data_offsets;
674 * struct ftrace_event_call *event_call = &event_<call>;
675 * extern void perf_tp_event(int, u64, u64, void *, int);
676 * struct ftrace_raw_##call *entry;
677 * struct perf_trace_buf *trace_buf;
678 * u64 __addr = 0, __count = 1;
679 * unsigned long irq_flags;
680 * struct trace_entry *ent;
686 * pc = preempt_count();
688 * __data_size = ftrace_get_offsets_<call>(&__data_offsets, args);
690 * // Below we want to get the aligned size by taking into account
691 * // the u32 field that will later store the buffer size
692 * __entry_size = ALIGN(__data_size + sizeof(*entry) + sizeof(u32),
694 * __entry_size -= sizeof(u32);
696 * // Protect the non nmi buffer
697 * // This also protects the rcu read side
698 * local_irq_save(irq_flags);
699 * __cpu = smp_processor_id();
702 * trace_buf = rcu_dereference(perf_trace_buf_nmi);
704 * trace_buf = rcu_dereference(perf_trace_buf);
709 * trace_buf = per_cpu_ptr(trace_buf, __cpu);
711 * // Avoid recursion from perf that could mess up the buffer
712 * if (trace_buf->recursion++)
713 * goto end_recursion;
715 * raw_data = trace_buf->buf;
717 * // Make recursion update visible before entering perf_tp_event
718 * // so that we protect from perf recursions.
722 * //zero dead bytes from alignment to avoid stack leak to userspace:
723 * *(u64 *)(&raw_data[__entry_size - sizeof(u64)]) = 0ULL;
724 * entry = (struct ftrace_raw_<call> *)raw_data;
726 * tracing_generic_entry_update(ent, irq_flags, pc);
727 * ent->type = event_call->id;
729 * <tstruct> <- do some jobs with dynamic arrays
731 * <assign> <- affect our values
733 * perf_tp_event(event_call->id, __addr, __count, entry,
734 * __entry_size); <- submit them to perf counter
739 #ifdef CONFIG_PERF_EVENTS
742 #define __entry entry
744 #undef __get_dynamic_array
745 #define __get_dynamic_array(field) \
746 ((void *)__entry + (__entry->__data_loc_##field & 0xffff))
749 #define __get_str(field) (char *)__get_dynamic_array(field)
752 #define __perf_addr(a) __addr = (a)
755 #define __perf_count(c) __count = (c)
757 #undef DECLARE_EVENT_CLASS
758 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
759 static notrace void \
760 ftrace_profile_templ_##call(struct ftrace_event_call *event_call, \
763 struct ftrace_data_offsets_##call __maybe_unused __data_offsets;\
764 struct ftrace_raw_##call *entry; \
765 u64 __addr = 0, __count = 1; \
766 unsigned long irq_flags; \
771 __data_size = ftrace_get_offsets_##call(&__data_offsets, args); \
772 __entry_size = ALIGN(__data_size + sizeof(*entry) + sizeof(u32),\
774 __entry_size -= sizeof(u32); \
776 if (WARN_ONCE(__entry_size > FTRACE_MAX_PROFILE_SIZE, \
777 "profile buffer not large enough")) \
779 entry = (struct ftrace_raw_##call *)ftrace_perf_buf_prepare( \
780 __entry_size, event_call->id, &rctx, &irq_flags); \
787 ftrace_perf_buf_submit(entry, __entry_size, rctx, __addr, \
788 __count, irq_flags); \
792 #define DEFINE_EVENT(template, call, proto, args) \
793 static notrace void ftrace_profile_##call(proto) \
795 struct ftrace_event_call *event_call = &event_##call; \
797 ftrace_profile_templ_##template(event_call, args); \
800 #undef DEFINE_EVENT_PRINT
801 #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
802 DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
804 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
805 #endif /* CONFIG_PERF_EVENTS */
807 #undef _TRACE_PROFILE_INIT