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 * TRACE_EVENT_TEMPLATE 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 TRACE_EVENT_TEMPLATE 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 TRACE_EVENT_TEMPLATE(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 TRACE_EVENT_TEMPLATE
60 #define TRACE_EVENT_TEMPLATE(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 event_##name
71 #define __cpparg(arg...) arg
73 /* Callbacks are meaningless to ftrace. */
75 #define TRACE_EVENT_FN(name, proto, args, tstruct, \
76 assign, print, reg, unreg) \
77 TRACE_EVENT(name, __cpparg(proto), __cpparg(args), \
78 __cpparg(tstruct), __cpparg(assign), __cpparg(print)) \
80 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
84 * Stage 2 of the trace events.
86 * Include the following:
88 * struct ftrace_data_offsets_<call> {
94 * The __dynamic_array() macro will create each u32 <item>, this is
95 * to keep the offset of each array from the beginning of the event.
96 * The size of an array is also encoded, in the higher 16 bits of <item>.
100 #define __field(type, item)
103 #define __field_ext(type, item, filter_type)
106 #define __array(type, item, len)
108 #undef __dynamic_array
109 #define __dynamic_array(type, item, len) u32 item;
112 #define __string(item, src) __dynamic_array(char, item, -1)
114 #undef TRACE_EVENT_TEMPLATE
115 #define TRACE_EVENT_TEMPLATE(call, proto, args, tstruct, assign, print) \
116 struct ftrace_data_offsets_##call { \
121 #define DEFINE_EVENT(template, name, proto, args)
123 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
126 * Setup the showing format of trace point.
129 * ftrace_format_##call(struct trace_seq *s)
131 * struct ftrace_raw_##call field;
134 * ret = trace_seq_printf(s, #type " " #item ";"
135 * " offset:%u; size:%u;\n",
136 * offsetof(struct ftrace_raw_##call, item),
137 * sizeof(field.type));
142 #undef TP_STRUCT__entry
143 #define TP_STRUCT__entry(args...) args
146 #define __field(type, item) \
147 ret = trace_seq_printf(s, "\tfield:" #type " " #item ";\t" \
148 "offset:%u;\tsize:%u;\tsigned:%u;\n", \
149 (unsigned int)offsetof(typeof(field), item), \
150 (unsigned int)sizeof(field.item), \
151 (unsigned int)is_signed_type(type)); \
156 #define __field_ext(type, item, filter_type) __field(type, item)
159 #define __array(type, item, len) \
160 ret = trace_seq_printf(s, "\tfield:" #type " " #item "[" #len "];\t" \
161 "offset:%u;\tsize:%u;\tsigned:%u;\n", \
162 (unsigned int)offsetof(typeof(field), item), \
163 (unsigned int)sizeof(field.item), \
164 (unsigned int)is_signed_type(type)); \
168 #undef __dynamic_array
169 #define __dynamic_array(type, item, len) \
170 ret = trace_seq_printf(s, "\tfield:__data_loc " #type "[] " #item ";\t"\
171 "offset:%u;\tsize:%u;\tsigned:%u;\n", \
172 (unsigned int)offsetof(typeof(field), \
173 __data_loc_##item), \
174 (unsigned int)sizeof(field.__data_loc_##item), \
175 (unsigned int)is_signed_type(type)); \
180 #define __string(item, src) __dynamic_array(char, item, -1)
185 #undef __print_symbolic
186 #undef __get_dynamic_array
190 #define TP_printk(fmt, args...) "%s, %s\n", #fmt, __stringify(args)
192 #undef TP_fast_assign
193 #define TP_fast_assign(args...) args
195 #undef TP_perf_assign
196 #define TP_perf_assign(args...)
198 #undef TRACE_EVENT_TEMPLATE
199 #define TRACE_EVENT_TEMPLATE(call, proto, args, tstruct, func, print) \
201 ftrace_format_##call(struct ftrace_event_call *unused, \
202 struct trace_seq *s) \
204 struct ftrace_raw_##call field __attribute__((unused)); \
209 trace_seq_printf(s, "\nprint fmt: " print); \
215 #define DEFINE_EVENT(template, name, proto, args)
217 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
220 * Stage 3 of the trace events.
222 * Override the macros in <trace/trace_events.h> to include the following:
225 * ftrace_raw_output_<call>(struct trace_iterator *iter, int flags)
227 * struct trace_seq *s = &iter->seq;
228 * struct ftrace_raw_<call> *field; <-- defined in stage 1
229 * struct trace_entry *entry;
230 * struct trace_seq *p;
235 * if (entry->type != event_<call>.id) {
237 * return TRACE_TYPE_UNHANDLED;
240 * field = (typeof(field))entry;
242 * p = get_cpu_var(ftrace_event_seq);
244 * ret = trace_seq_printf(s, <TP_printk> "\n");
247 * return TRACE_TYPE_PARTIAL_LINE;
249 * return TRACE_TYPE_HANDLED;
252 * This is the method used to print the raw event to the trace
253 * output format. Note, this is not needed if the data is read
258 #define __entry field
261 #define TP_printk(fmt, args...) fmt "\n", args
263 #undef __get_dynamic_array
264 #define __get_dynamic_array(field) \
265 ((void *)__entry + (__entry->__data_loc_##field & 0xffff))
268 #define __get_str(field) (char *)__get_dynamic_array(field)
271 #define __print_flags(flag, delim, flag_array...) \
273 static const struct trace_print_flags __flags[] = \
274 { flag_array, { -1, NULL }}; \
275 ftrace_print_flags_seq(p, delim, flag, __flags); \
278 #undef __print_symbolic
279 #define __print_symbolic(value, symbol_array...) \
281 static const struct trace_print_flags symbols[] = \
282 { symbol_array, { -1, NULL }}; \
283 ftrace_print_symbols_seq(p, value, symbols); \
286 #undef TRACE_EVENT_TEMPLATE
287 #define TRACE_EVENT_TEMPLATE(call, proto, args, tstruct, assign, print) \
288 static enum print_line_t \
289 ftrace_raw_output_id_##call(int event_id, const char *name, \
290 struct trace_iterator *iter, int flags) \
292 struct trace_seq *s = &iter->seq; \
293 struct ftrace_raw_##call *field; \
294 struct trace_entry *entry; \
295 struct trace_seq *p; \
300 if (entry->type != event_id) { \
302 return TRACE_TYPE_UNHANDLED; \
305 field = (typeof(field))entry; \
307 p = &get_cpu_var(ftrace_event_seq); \
309 ret = trace_seq_printf(s, "%s: ", name); \
311 ret = trace_seq_printf(s, print); \
314 return TRACE_TYPE_PARTIAL_LINE; \
316 return TRACE_TYPE_HANDLED; \
320 #define DEFINE_EVENT(template, name, proto, args) \
321 static enum print_line_t \
322 ftrace_raw_output_##name(struct trace_iterator *iter, int flags) \
324 return ftrace_raw_output_id_##template(event_##name.id, \
325 #name, iter, flags); \
328 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
331 #define __field_ext(type, item, filter_type) \
332 ret = trace_define_field(event_call, #type, #item, \
333 offsetof(typeof(field), item), \
334 sizeof(field.item), \
335 is_signed_type(type), filter_type); \
340 #define __field(type, item) __field_ext(type, item, FILTER_OTHER)
343 #define __array(type, item, len) \
344 BUILD_BUG_ON(len > MAX_FILTER_STR_VAL); \
345 ret = trace_define_field(event_call, #type "[" #len "]", #item, \
346 offsetof(typeof(field), item), \
347 sizeof(field.item), 0, FILTER_OTHER); \
351 #undef __dynamic_array
352 #define __dynamic_array(type, item, len) \
353 ret = trace_define_field(event_call, "__data_loc " #type "[]", #item, \
354 offsetof(typeof(field), __data_loc_##item), \
355 sizeof(field.__data_loc_##item), 0, \
359 #define __string(item, src) __dynamic_array(char, item, -1)
361 #undef TRACE_EVENT_TEMPLATE
362 #define TRACE_EVENT_TEMPLATE(call, proto, args, tstruct, func, print) \
364 ftrace_define_fields_##call(struct ftrace_event_call *event_call) \
366 struct ftrace_raw_##call field; \
369 ret = trace_define_common_fields(event_call); \
379 #define DEFINE_EVENT(template, name, proto, args)
381 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
384 * remember the offset of each array from the beginning of the event.
388 #define __entry entry
391 #define __field(type, item)
394 #define __field_ext(type, item, filter_type)
397 #define __array(type, item, len)
399 #undef __dynamic_array
400 #define __dynamic_array(type, item, len) \
401 __data_offsets->item = __data_size + \
402 offsetof(typeof(*entry), __data); \
403 __data_offsets->item |= (len * sizeof(type)) << 16; \
404 __data_size += (len) * sizeof(type);
407 #define __string(item, src) __dynamic_array(char, item, strlen(src) + 1)
409 #undef TRACE_EVENT_TEMPLATE
410 #define TRACE_EVENT_TEMPLATE(call, proto, args, tstruct, assign, print) \
411 static inline int ftrace_get_offsets_##call( \
412 struct ftrace_data_offsets_##call *__data_offsets, proto) \
414 int __data_size = 0; \
415 struct ftrace_raw_##call __maybe_unused *entry; \
419 return __data_size; \
423 #define DEFINE_EVENT(template, name, proto, args)
425 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
427 #ifdef CONFIG_EVENT_PROFILE
430 * Generate the functions needed for tracepoint perf_event support.
432 * NOTE: The insertion profile callback (ftrace_profile_<call>) is defined later
434 * static int ftrace_profile_enable_<call>(void)
436 * return register_trace_<call>(ftrace_profile_<call>);
439 * static void ftrace_profile_disable_<call>(void)
441 * unregister_trace_<call>(ftrace_profile_<call>);
446 #undef TRACE_EVENT_TEMPLATE
447 #define TRACE_EVENT_TEMPLATE(call, proto, args, tstruct, assign, print)
450 #define DEFINE_EVENT(template, name, proto, args) \
452 static void ftrace_profile_##name(proto); \
454 static int ftrace_profile_enable_##name(struct ftrace_event_call *unused)\
456 return register_trace_##name(ftrace_profile_##name); \
459 static void ftrace_profile_disable_##name(struct ftrace_event_call *unused)\
461 unregister_trace_##name(ftrace_profile_##name); \
464 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
469 * Stage 4 of the trace events.
471 * Override the macros in <trace/trace_events.h> to include the following:
473 * static void ftrace_event_<call>(proto)
475 * event_trace_printk(_RET_IP_, "<call>: " <fmt>);
478 * static int ftrace_reg_event_<call>(struct ftrace_event_call *unused)
482 * ret = register_trace_<call>(ftrace_event_<call>);
484 * pr_info("event trace: Could not activate trace point "
485 * "probe to <call>");
489 * static void ftrace_unreg_event_<call>(struct ftrace_event_call *unused)
491 * unregister_trace_<call>(ftrace_event_<call>);
495 * For those macros defined with TRACE_EVENT:
497 * static struct ftrace_event_call event_<call>;
499 * static void ftrace_raw_event_<call>(proto)
501 * struct ring_buffer_event *event;
502 * struct ftrace_raw_<call> *entry; <-- defined in stage 1
503 * struct ring_buffer *buffer;
504 * unsigned long irq_flags;
507 * local_save_flags(irq_flags);
508 * pc = preempt_count();
510 * event = trace_current_buffer_lock_reserve(&buffer,
512 * sizeof(struct ftrace_raw_<call>),
516 * entry = ring_buffer_event_data(event);
518 * <assign>; <-- Here we assign the entries by the __field and
521 * trace_current_buffer_unlock_commit(buffer, event, irq_flags, pc);
524 * static int ftrace_raw_reg_event_<call>(struct ftrace_event_call *unused)
528 * ret = register_trace_<call>(ftrace_raw_event_<call>);
530 * pr_info("event trace: Could not activate trace point "
531 * "probe to <call>");
535 * static void ftrace_unreg_event_<call>(struct ftrace_event_call *unused)
537 * unregister_trace_<call>(ftrace_raw_event_<call>);
540 * static struct trace_event ftrace_event_type_<call> = {
541 * .trace = ftrace_raw_output_<call>, <-- stage 2
544 * static int ftrace_raw_init_event_<call>(struct ftrace_event_call *unused)
548 * id = register_ftrace_event(&ftrace_event_type_<call>);
551 * event_<call>.id = id;
555 * static struct ftrace_event_call __used
556 * __attribute__((__aligned__(4)))
557 * __attribute__((section("_ftrace_events"))) event_<call> = {
559 * .system = "<system>",
560 * .raw_init = ftrace_raw_init_event_<call>,
561 * .regfunc = ftrace_reg_event_<call>,
562 * .unregfunc = ftrace_unreg_event_<call>,
563 * .show_format = ftrace_format_<call>,
569 #define TP_FMT(fmt, args...) fmt "\n", ##args
571 #ifdef CONFIG_EVENT_PROFILE
573 #define _TRACE_PROFILE_INIT(call) \
574 .profile_count = ATOMIC_INIT(-1), \
575 .profile_enable = ftrace_profile_enable_##call, \
576 .profile_disable = ftrace_profile_disable_##call,
579 #define _TRACE_PROFILE_INIT(call)
583 #define __entry entry
586 #define __field(type, item)
589 #define __array(type, item, len)
591 #undef __dynamic_array
592 #define __dynamic_array(type, item, len) \
593 __entry->__data_loc_##item = __data_offsets.item;
596 #define __string(item, src) __dynamic_array(char, item, -1) \
599 #define __assign_str(dst, src) \
600 strcpy(__get_str(dst), src);
602 #undef TRACE_EVENT_TEMPLATE
603 #define TRACE_EVENT_TEMPLATE(call, proto, args, tstruct, assign, print) \
605 static void ftrace_raw_event_id_##call(struct ftrace_event_call *event_call, \
608 struct ftrace_data_offsets_##call __maybe_unused __data_offsets;\
609 struct ring_buffer_event *event; \
610 struct ftrace_raw_##call *entry; \
611 struct ring_buffer *buffer; \
612 unsigned long irq_flags; \
616 local_save_flags(irq_flags); \
617 pc = preempt_count(); \
619 __data_size = ftrace_get_offsets_##call(&__data_offsets, args); \
621 event = trace_current_buffer_lock_reserve(&buffer, \
623 sizeof(*entry) + __data_size, \
627 entry = ring_buffer_event_data(event); \
634 if (!filter_current_check_discard(buffer, event_call, entry, event)) \
635 trace_nowake_buffer_unlock_commit(buffer, \
636 event, irq_flags, pc); \
640 #define DEFINE_EVENT(template, call, proto, args) \
642 static void ftrace_raw_event_##call(proto) \
644 ftrace_raw_event_id_##template(&event_##call, args); \
647 static int ftrace_raw_reg_event_##call(struct ftrace_event_call *unused)\
651 ret = register_trace_##call(ftrace_raw_event_##call); \
653 pr_info("event trace: Could not activate trace point " \
654 "probe to " #call "\n"); \
658 static void ftrace_raw_unreg_event_##call(struct ftrace_event_call *unused)\
660 unregister_trace_##call(ftrace_raw_event_##call); \
663 static struct trace_event ftrace_event_type_##call = { \
664 .trace = ftrace_raw_output_##call, \
667 static int ftrace_raw_init_event_##call(struct ftrace_event_call *unused)\
671 id = register_ftrace_event(&ftrace_event_type_##call); \
674 event_##call.id = id; \
675 INIT_LIST_HEAD(&event_##call.fields); \
679 static struct ftrace_event_call __used \
680 __attribute__((__aligned__(4))) \
681 __attribute__((section("_ftrace_events"))) event_##call = { \
683 .system = __stringify(TRACE_SYSTEM), \
684 .event = &ftrace_event_type_##call, \
685 .raw_init = ftrace_raw_init_event_##call, \
686 .regfunc = ftrace_raw_reg_event_##call, \
687 .unregfunc = ftrace_raw_unreg_event_##call, \
688 .show_format = ftrace_format_##template, \
689 .define_fields = ftrace_define_fields_##template, \
690 _TRACE_PROFILE_INIT(call) \
693 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
696 * Define the insertion callback to profile events
698 * The job is very similar to ftrace_raw_event_<call> except that we don't
699 * insert in the ring buffer but in a perf counter.
701 * static void ftrace_profile_<call>(proto)
703 * struct ftrace_data_offsets_<call> __maybe_unused __data_offsets;
704 * struct ftrace_event_call *event_call = &event_<call>;
705 * extern void perf_tp_event(int, u64, u64, void *, int);
706 * struct ftrace_raw_##call *entry;
707 * struct perf_trace_buf *trace_buf;
708 * u64 __addr = 0, __count = 1;
709 * unsigned long irq_flags;
710 * struct trace_entry *ent;
716 * pc = preempt_count();
718 * __data_size = ftrace_get_offsets_<call>(&__data_offsets, args);
720 * // Below we want to get the aligned size by taking into account
721 * // the u32 field that will later store the buffer size
722 * __entry_size = ALIGN(__data_size + sizeof(*entry) + sizeof(u32),
724 * __entry_size -= sizeof(u32);
726 * // Protect the non nmi buffer
727 * // This also protects the rcu read side
728 * local_irq_save(irq_flags);
729 * __cpu = smp_processor_id();
732 * trace_buf = rcu_dereference(perf_trace_buf_nmi);
734 * trace_buf = rcu_dereference(perf_trace_buf);
739 * trace_buf = per_cpu_ptr(trace_buf, __cpu);
741 * // Avoid recursion from perf that could mess up the buffer
742 * if (trace_buf->recursion++)
743 * goto end_recursion;
745 * raw_data = trace_buf->buf;
747 * // Make recursion update visible before entering perf_tp_event
748 * // so that we protect from perf recursions.
752 * //zero dead bytes from alignment to avoid stack leak to userspace:
753 * *(u64 *)(&raw_data[__entry_size - sizeof(u64)]) = 0ULL;
754 * entry = (struct ftrace_raw_<call> *)raw_data;
756 * tracing_generic_entry_update(ent, irq_flags, pc);
757 * ent->type = event_call->id;
759 * <tstruct> <- do some jobs with dynamic arrays
761 * <assign> <- affect our values
763 * perf_tp_event(event_call->id, __addr, __count, entry,
764 * __entry_size); <- submit them to perf counter
769 #ifdef CONFIG_EVENT_PROFILE
772 #define __perf_addr(a) __addr = (a)
775 #define __perf_count(c) __count = (c)
777 #undef TRACE_EVENT_TEMPLATE
778 #define TRACE_EVENT_TEMPLATE(call, proto, args, tstruct, assign, print) \
780 ftrace_profile_templ_##call(struct ftrace_event_call *event_call, \
783 struct ftrace_data_offsets_##call __maybe_unused __data_offsets;\
784 extern int perf_swevent_get_recursion_context(void); \
785 extern void perf_swevent_put_recursion_context(int rctx); \
786 extern void perf_tp_event(int, u64, u64, void *, int); \
787 struct ftrace_raw_##call *entry; \
788 u64 __addr = 0, __count = 1; \
789 unsigned long irq_flags; \
790 struct trace_entry *ent; \
799 pc = preempt_count(); \
801 __data_size = ftrace_get_offsets_##call(&__data_offsets, args); \
802 __entry_size = ALIGN(__data_size + sizeof(*entry) + sizeof(u32),\
804 __entry_size -= sizeof(u32); \
806 if (WARN_ONCE(__entry_size > FTRACE_MAX_PROFILE_SIZE, \
807 "profile buffer not large enough")) \
810 local_irq_save(irq_flags); \
812 rctx = perf_swevent_get_recursion_context(); \
814 goto end_recursion; \
816 __cpu = smp_processor_id(); \
819 trace_buf = rcu_dereference(perf_trace_buf_nmi); \
821 trace_buf = rcu_dereference(perf_trace_buf); \
826 raw_data = per_cpu_ptr(trace_buf, __cpu); \
828 *(u64 *)(&raw_data[__entry_size - sizeof(u64)]) = 0ULL; \
829 entry = (struct ftrace_raw_##call *)raw_data; \
831 tracing_generic_entry_update(ent, irq_flags, pc); \
832 ent->type = event_call->id; \
838 perf_tp_event(event_call->id, __addr, __count, entry, \
842 perf_swevent_put_recursion_context(rctx); \
844 local_irq_restore(irq_flags); \
849 #define DEFINE_EVENT(template, call, proto, args) \
850 static void ftrace_profile_##call(proto) \
852 struct ftrace_event_call *event_call = &event_##call; \
854 ftrace_profile_templ_##template(event_call, args); \
857 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
858 #endif /* CONFIG_EVENT_PROFILE */
860 #undef _TRACE_PROFILE_INIT