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 #define __field(type, item) type item;
25 #define __field_ext(type, item, filter_type) type item;
28 #define __array(type, item, len) type item[len];
30 #undef __dynamic_array
31 #define __dynamic_array(type, item, len) u32 __data_loc_##item;
34 #define __string(item, src) __dynamic_array(char, item, -1)
36 #undef TP_STRUCT__entry
37 #define TP_STRUCT__entry(args...) args
40 #define TRACE_EVENT(name, proto, args, tstruct, assign, print) \
41 struct ftrace_raw_##name { \
42 struct trace_entry ent; \
46 static struct ftrace_event_call event_##name
49 #define __cpparg(arg...) arg
51 /* Callbacks are meaningless to ftrace. */
53 #define TRACE_EVENT_FN(name, proto, args, tstruct, \
54 assign, print, reg, unreg) \
55 TRACE_EVENT(name, __cpparg(proto), __cpparg(args), \
56 __cpparg(tstruct), __cpparg(assign), __cpparg(print)) \
58 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
62 * Stage 2 of the trace events.
64 * Include the following:
66 * struct ftrace_data_offsets_<call> {
72 * The __dynamic_array() macro will create each u32 <item>, this is
73 * to keep the offset of each array from the beginning of the event.
74 * The size of an array is also encoded, in the higher 16 bits of <item>.
78 #define __field(type, item)
81 #define __field_ext(type, item, filter_type)
84 #define __array(type, item, len)
86 #undef __dynamic_array
87 #define __dynamic_array(type, item, len) u32 item;
90 #define __string(item, src) __dynamic_array(char, item, -1)
93 #define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
94 struct ftrace_data_offsets_##call { \
98 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
101 * Setup the showing format of trace point.
104 * ftrace_format_##call(struct trace_seq *s)
106 * struct ftrace_raw_##call field;
109 * ret = trace_seq_printf(s, #type " " #item ";"
110 * " offset:%u; size:%u;\n",
111 * offsetof(struct ftrace_raw_##call, item),
112 * sizeof(field.type));
117 #undef TP_STRUCT__entry
118 #define TP_STRUCT__entry(args...) args
121 #define __field(type, item) \
122 ret = trace_seq_printf(s, "\tfield:" #type " " #item ";\t" \
123 "offset:%u;\tsize:%u;\n", \
124 (unsigned int)offsetof(typeof(field), item), \
125 (unsigned int)sizeof(field.item)); \
130 #define __field_ext(type, item, filter_type) __field(type, item)
133 #define __array(type, item, len) \
134 ret = trace_seq_printf(s, "\tfield:" #type " " #item "[" #len "];\t" \
135 "offset:%u;\tsize:%u;\n", \
136 (unsigned int)offsetof(typeof(field), item), \
137 (unsigned int)sizeof(field.item)); \
141 #undef __dynamic_array
142 #define __dynamic_array(type, item, len) \
143 ret = trace_seq_printf(s, "\tfield:__data_loc " #type "[] " #item ";\t"\
144 "offset:%u;\tsize:%u;\n", \
145 (unsigned int)offsetof(typeof(field), \
146 __data_loc_##item), \
147 (unsigned int)sizeof(field.__data_loc_##item)); \
152 #define __string(item, src) __dynamic_array(char, item, -1)
157 #undef __print_symbolic
158 #undef __get_dynamic_array
162 #define TP_printk(fmt, args...) "%s, %s\n", #fmt, __stringify(args)
164 #undef TP_fast_assign
165 #define TP_fast_assign(args...) args
167 #undef TP_perf_assign
168 #define TP_perf_assign(args...)
171 #define TRACE_EVENT(call, proto, args, tstruct, func, print) \
173 ftrace_format_##call(struct ftrace_event_call *unused, \
174 struct trace_seq *s) \
176 struct ftrace_raw_##call field __attribute__((unused)); \
181 trace_seq_printf(s, "\nprint fmt: " print); \
186 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
189 * Stage 3 of the trace events.
191 * Override the macros in <trace/trace_events.h> to include the following:
194 * ftrace_raw_output_<call>(struct trace_iterator *iter, int flags)
196 * struct trace_seq *s = &iter->seq;
197 * struct ftrace_raw_<call> *field; <-- defined in stage 1
198 * struct trace_entry *entry;
199 * struct trace_seq *p;
204 * if (entry->type != event_<call>.id) {
206 * return TRACE_TYPE_UNHANDLED;
209 * field = (typeof(field))entry;
211 * p = get_cpu_var(ftrace_event_seq);
213 * ret = trace_seq_printf(s, <TP_printk> "\n");
216 * return TRACE_TYPE_PARTIAL_LINE;
218 * return TRACE_TYPE_HANDLED;
221 * This is the method used to print the raw event to the trace
222 * output format. Note, this is not needed if the data is read
227 #define __entry field
230 #define TP_printk(fmt, args...) fmt "\n", args
232 #undef __get_dynamic_array
233 #define __get_dynamic_array(field) \
234 ((void *)__entry + (__entry->__data_loc_##field & 0xffff))
237 #define __get_str(field) (char *)__get_dynamic_array(field)
240 #define __print_flags(flag, delim, flag_array...) \
242 static const struct trace_print_flags flags[] = \
243 { flag_array, { -1, NULL }}; \
244 ftrace_print_flags_seq(p, delim, flag, flags); \
247 #undef __print_symbolic
248 #define __print_symbolic(value, symbol_array...) \
250 static const struct trace_print_flags symbols[] = \
251 { symbol_array, { -1, NULL }}; \
252 ftrace_print_symbols_seq(p, value, symbols); \
256 #define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
258 ftrace_raw_output_##call(struct trace_iterator *iter, int flags) \
260 struct trace_seq *s = &iter->seq; \
261 struct ftrace_raw_##call *field; \
262 struct trace_entry *entry; \
263 struct trace_seq *p; \
268 if (entry->type != event_##call.id) { \
270 return TRACE_TYPE_UNHANDLED; \
273 field = (typeof(field))entry; \
275 p = &get_cpu_var(ftrace_event_seq); \
277 ret = trace_seq_printf(s, #call ": " print); \
280 return TRACE_TYPE_PARTIAL_LINE; \
282 return TRACE_TYPE_HANDLED; \
285 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
288 #define __field_ext(type, item, filter_type) \
289 ret = trace_define_field(event_call, #type, #item, \
290 offsetof(typeof(field), item), \
291 sizeof(field.item), \
292 is_signed_type(type), filter_type); \
297 #define __field(type, item) __field_ext(type, item, FILTER_OTHER)
300 #define __array(type, item, len) \
301 BUILD_BUG_ON(len > MAX_FILTER_STR_VAL); \
302 ret = trace_define_field(event_call, #type "[" #len "]", #item, \
303 offsetof(typeof(field), item), \
304 sizeof(field.item), 0, FILTER_OTHER); \
308 #undef __dynamic_array
309 #define __dynamic_array(type, item, len) \
310 ret = trace_define_field(event_call, "__data_loc " #type "[]", #item, \
311 offsetof(typeof(field), __data_loc_##item), \
312 sizeof(field.__data_loc_##item), 0, \
316 #define __string(item, src) __dynamic_array(char, item, -1)
319 #define TRACE_EVENT(call, proto, args, tstruct, func, print) \
321 ftrace_define_fields_##call(struct ftrace_event_call *event_call) \
323 struct ftrace_raw_##call field; \
326 ret = trace_define_common_fields(event_call); \
335 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
338 * remember the offset of each array from the beginning of the event.
342 #define __entry entry
345 #define __field(type, item)
348 #define __field_ext(type, item, filter_type)
351 #define __array(type, item, len)
353 #undef __dynamic_array
354 #define __dynamic_array(type, item, len) \
355 __data_offsets->item = __data_size + \
356 offsetof(typeof(*entry), __data); \
357 __data_offsets->item |= (len * sizeof(type)) << 16; \
358 __data_size += (len) * sizeof(type);
361 #define __string(item, src) __dynamic_array(char, item, strlen(src) + 1) \
364 #define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
365 static inline int ftrace_get_offsets_##call( \
366 struct ftrace_data_offsets_##call *__data_offsets, proto) \
368 int __data_size = 0; \
369 struct ftrace_raw_##call __maybe_unused *entry; \
373 return __data_size; \
376 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
378 #ifdef CONFIG_EVENT_PROFILE
381 * Generate the functions needed for tracepoint perf_counter support.
383 * NOTE: The insertion profile callback (ftrace_profile_<call>) is defined later
385 * static int ftrace_profile_enable_<call>(struct ftrace_event_call *event_call)
389 * if (!atomic_inc_return(&event_call->profile_count))
390 * ret = register_trace_<call>(ftrace_profile_<call>);
395 * static void ftrace_profile_disable_<call>(struct ftrace_event_call *event_call)
397 * if (atomic_add_negative(-1, &event->call->profile_count))
398 * unregister_trace_<call>(ftrace_profile_<call>);
404 #define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
406 static void ftrace_profile_##call(proto); \
408 static int ftrace_profile_enable_##call(struct ftrace_event_call *event_call) \
412 if (!atomic_inc_return(&event_call->profile_count)) \
413 ret = register_trace_##call(ftrace_profile_##call); \
418 static void ftrace_profile_disable_##call(struct ftrace_event_call *event_call)\
420 if (atomic_add_negative(-1, &event_call->profile_count)) \
421 unregister_trace_##call(ftrace_profile_##call); \
424 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
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>(void)
442 * ret = register_trace_<call>(ftrace_event_<call>);
444 * pr_info("event trace: Could not activate trace point "
445 * "probe to <call>");
449 * static void ftrace_unreg_event_<call>(void)
451 * unregister_trace_<call>(ftrace_event_<call>);
455 * For those macros defined with TRACE_EVENT:
457 * static struct ftrace_event_call event_<call>;
459 * static void ftrace_raw_event_<call>(proto)
461 * struct ring_buffer_event *event;
462 * struct ftrace_raw_<call> *entry; <-- defined in stage 1
463 * unsigned long irq_flags;
466 * local_save_flags(irq_flags);
467 * pc = preempt_count();
469 * event = trace_current_buffer_lock_reserve(event_<call>.id,
470 * sizeof(struct ftrace_raw_<call>),
474 * entry = ring_buffer_event_data(event);
476 * <assign>; <-- Here we assign the entries by the __field and
479 * trace_current_buffer_unlock_commit(event, irq_flags, pc);
482 * static int ftrace_raw_reg_event_<call>(void)
486 * ret = register_trace_<call>(ftrace_raw_event_<call>);
488 * pr_info("event trace: Could not activate trace point "
489 * "probe to <call>");
493 * static void ftrace_unreg_event_<call>(void)
495 * unregister_trace_<call>(ftrace_raw_event_<call>);
498 * static struct trace_event ftrace_event_type_<call> = {
499 * .trace = ftrace_raw_output_<call>, <-- stage 2
502 * static int ftrace_raw_init_event_<call>(void)
506 * id = register_ftrace_event(&ftrace_event_type_<call>);
509 * event_<call>.id = id;
513 * static struct ftrace_event_call __used
514 * __attribute__((__aligned__(4)))
515 * __attribute__((section("_ftrace_events"))) event_<call> = {
517 * .system = "<system>",
518 * .raw_init = ftrace_raw_init_event_<call>,
519 * .regfunc = ftrace_reg_event_<call>,
520 * .unregfunc = ftrace_unreg_event_<call>,
521 * .show_format = ftrace_format_<call>,
527 #define TP_FMT(fmt, args...) fmt "\n", ##args
529 #ifdef CONFIG_EVENT_PROFILE
531 #define _TRACE_PROFILE_INIT(call) \
532 .profile_count = ATOMIC_INIT(-1), \
533 .profile_enable = ftrace_profile_enable_##call, \
534 .profile_disable = ftrace_profile_disable_##call,
537 #define _TRACE_PROFILE_INIT(call)
541 #define __entry entry
544 #define __field(type, item)
547 #define __array(type, item, len)
549 #undef __dynamic_array
550 #define __dynamic_array(type, item, len) \
551 __entry->__data_loc_##item = __data_offsets.item;
554 #define __string(item, src) __dynamic_array(char, item, -1) \
557 #define __assign_str(dst, src) \
558 strcpy(__get_str(dst), src);
561 #define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
563 static struct ftrace_event_call event_##call; \
565 static void ftrace_raw_event_##call(proto) \
567 struct ftrace_data_offsets_##call __maybe_unused __data_offsets;\
568 struct ftrace_event_call *event_call = &event_##call; \
569 struct ring_buffer_event *event; \
570 struct ftrace_raw_##call *entry; \
571 unsigned long irq_flags; \
575 local_save_flags(irq_flags); \
576 pc = preempt_count(); \
578 __data_size = ftrace_get_offsets_##call(&__data_offsets, args); \
580 event = trace_current_buffer_lock_reserve(event_##call.id, \
581 sizeof(*entry) + __data_size, \
585 entry = ring_buffer_event_data(event); \
592 if (!filter_current_check_discard(event_call, entry, event)) \
593 trace_nowake_buffer_unlock_commit(event, irq_flags, pc); \
596 static int ftrace_raw_reg_event_##call(void *ptr) \
600 ret = register_trace_##call(ftrace_raw_event_##call); \
602 pr_info("event trace: Could not activate trace point " \
603 "probe to " #call "\n"); \
607 static void ftrace_raw_unreg_event_##call(void *ptr) \
609 unregister_trace_##call(ftrace_raw_event_##call); \
612 static struct trace_event ftrace_event_type_##call = { \
613 .trace = ftrace_raw_output_##call, \
616 static int ftrace_raw_init_event_##call(void) \
620 id = register_ftrace_event(&ftrace_event_type_##call); \
623 event_##call.id = id; \
624 INIT_LIST_HEAD(&event_##call.fields); \
625 init_preds(&event_##call); \
629 static struct ftrace_event_call __used \
630 __attribute__((__aligned__(4))) \
631 __attribute__((section("_ftrace_events"))) event_##call = { \
633 .system = __stringify(TRACE_SYSTEM), \
634 .event = &ftrace_event_type_##call, \
635 .raw_init = ftrace_raw_init_event_##call, \
636 .regfunc = ftrace_raw_reg_event_##call, \
637 .unregfunc = ftrace_raw_unreg_event_##call, \
638 .show_format = ftrace_format_##call, \
639 .define_fields = ftrace_define_fields_##call, \
640 _TRACE_PROFILE_INIT(call) \
643 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
646 * Define the insertion callback to profile events
648 * The job is very similar to ftrace_raw_event_<call> except that we don't
649 * insert in the ring buffer but in a perf counter.
651 * static void ftrace_profile_<call>(proto)
653 * struct ftrace_data_offsets_<call> __maybe_unused __data_offsets;
654 * struct ftrace_event_call *event_call = &event_<call>;
655 * extern void perf_tpcounter_event(int, u64, u64, void *, int);
656 * struct ftrace_raw_##call *entry;
657 * u64 __addr = 0, __count = 1;
658 * unsigned long irq_flags;
663 * local_save_flags(irq_flags);
664 * pc = preempt_count();
666 * __data_size = ftrace_get_offsets_<call>(&__data_offsets, args);
668 * // Below we want to get the aligned size by taking into account
669 * // the u32 field that will later store the buffer size
670 * __entry_size = ALIGN(__data_size + sizeof(*entry) + sizeof(u32),
672 * __entry_size -= sizeof(u32);
675 * char raw_data[__entry_size]; <- allocate our sample in the stack
676 * struct trace_entry *ent;
678 * zero dead bytes from alignment to avoid stack leak to userspace:
680 * *(u64 *)(&raw_data[__entry_size - sizeof(u64)]) = 0ULL;
681 * entry = (struct ftrace_raw_<call> *)raw_data;
683 * tracing_generic_entry_update(ent, irq_flags, pc);
684 * ent->type = event_call->id;
686 * <tstruct> <- do some jobs with dynamic arrays
688 * <assign> <- affect our values
690 * perf_tpcounter_event(event_call->id, __addr, __count, entry,
691 * __entry_size); <- submit them to perf counter
697 #ifdef CONFIG_EVENT_PROFILE
700 #define __perf_addr(a) __addr = (a)
703 #define __perf_count(c) __count = (c)
706 #define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
707 static void ftrace_profile_##call(proto) \
709 struct ftrace_data_offsets_##call __maybe_unused __data_offsets;\
710 struct ftrace_event_call *event_call = &event_##call; \
711 extern void perf_tpcounter_event(int, u64, u64, void *, int); \
712 struct ftrace_raw_##call *entry; \
713 u64 __addr = 0, __count = 1; \
714 unsigned long irq_flags; \
719 local_save_flags(irq_flags); \
720 pc = preempt_count(); \
722 __data_size = ftrace_get_offsets_##call(&__data_offsets, args); \
723 __entry_size = ALIGN(__data_size + sizeof(*entry) + sizeof(u32),\
725 __entry_size -= sizeof(u32); \
728 char raw_data[__entry_size]; \
729 struct trace_entry *ent; \
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; \
741 perf_tpcounter_event(event_call->id, __addr, __count, entry,\
747 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
748 #endif /* CONFIG_EVENT_PROFILE */
750 #undef _TRACE_PROFILE_INIT