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
48 /* Callbacks are meaningless to ftrace. */
50 #define TRACE_EVENT_FN(name, proto, args, tstruct, \
51 assign, print, reg, unreg) \
52 TRACE_EVENT(name, TP_PROTO(proto), TP_ARGS(args), \
53 TP_STRUCT__entry(tstruct), \
54 TP_fast_assign(assign), \
57 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
61 * Stage 2 of the trace events.
63 * Include the following:
65 * struct ftrace_data_offsets_<call> {
71 * The __dynamic_array() macro will create each u32 <item>, this is
72 * to keep the offset of each array from the beginning of the event.
73 * The size of an array is also encoded, in the higher 16 bits of <item>.
77 #define __field(type, item)
80 #define __field_ext(type, item, filter_type)
83 #define __array(type, item, len)
85 #undef __dynamic_array
86 #define __dynamic_array(type, item, len) u32 item;
89 #define __string(item, src) __dynamic_array(char, item, -1)
92 #define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
93 struct ftrace_data_offsets_##call { \
97 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
100 * Setup the showing format of trace point.
103 * ftrace_format_##call(struct trace_seq *s)
105 * struct ftrace_raw_##call field;
108 * ret = trace_seq_printf(s, #type " " #item ";"
109 * " offset:%u; size:%u;\n",
110 * offsetof(struct ftrace_raw_##call, item),
111 * sizeof(field.type));
116 #undef TP_STRUCT__entry
117 #define TP_STRUCT__entry(args...) args
120 #define __field(type, item) \
121 ret = trace_seq_printf(s, "\tfield:" #type " " #item ";\t" \
122 "offset:%u;\tsize:%u;\n", \
123 (unsigned int)offsetof(typeof(field), item), \
124 (unsigned int)sizeof(field.item)); \
129 #define __field_ext(type, item, filter_type) __field(type, item)
132 #define __array(type, item, len) \
133 ret = trace_seq_printf(s, "\tfield:" #type " " #item "[" #len "];\t" \
134 "offset:%u;\tsize:%u;\n", \
135 (unsigned int)offsetof(typeof(field), item), \
136 (unsigned int)sizeof(field.item)); \
140 #undef __dynamic_array
141 #define __dynamic_array(type, item, len) \
142 ret = trace_seq_printf(s, "\tfield:__data_loc " #type "[] " #item ";\t"\
143 "offset:%u;\tsize:%u;\n", \
144 (unsigned int)offsetof(typeof(field), \
145 __data_loc_##item), \
146 (unsigned int)sizeof(field.__data_loc_##item)); \
151 #define __string(item, src) __dynamic_array(char, item, -1)
156 #undef __print_symbolic
157 #undef __get_dynamic_array
161 #define TP_printk(fmt, args...) "%s, %s\n", #fmt, __stringify(args)
163 #undef TP_fast_assign
164 #define TP_fast_assign(args...) args
166 #undef TP_perf_assign
167 #define TP_perf_assign(args...)
170 #define TRACE_EVENT(call, proto, args, tstruct, func, print) \
172 ftrace_format_##call(struct ftrace_event_call *unused, \
173 struct trace_seq *s) \
175 struct ftrace_raw_##call field __attribute__((unused)); \
180 trace_seq_printf(s, "\nprint fmt: " print); \
185 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
188 * Stage 3 of the trace events.
190 * Override the macros in <trace/trace_events.h> to include the following:
193 * ftrace_raw_output_<call>(struct trace_iterator *iter, int flags)
195 * struct trace_seq *s = &iter->seq;
196 * struct ftrace_raw_<call> *field; <-- defined in stage 1
197 * struct trace_entry *entry;
198 * struct trace_seq *p;
203 * if (entry->type != event_<call>.id) {
205 * return TRACE_TYPE_UNHANDLED;
208 * field = (typeof(field))entry;
210 * p = get_cpu_var(ftrace_event_seq);
212 * ret = trace_seq_printf(s, <TP_printk> "\n");
215 * return TRACE_TYPE_PARTIAL_LINE;
217 * return TRACE_TYPE_HANDLED;
220 * This is the method used to print the raw event to the trace
221 * output format. Note, this is not needed if the data is read
226 #define __entry field
229 #define TP_printk(fmt, args...) fmt "\n", args
231 #undef __get_dynamic_array
232 #define __get_dynamic_array(field) \
233 ((void *)__entry + (__entry->__data_loc_##field & 0xffff))
236 #define __get_str(field) (char *)__get_dynamic_array(field)
239 #define __print_flags(flag, delim, flag_array...) \
241 static const struct trace_print_flags flags[] = \
242 { flag_array, { -1, NULL }}; \
243 ftrace_print_flags_seq(p, delim, flag, flags); \
246 #undef __print_symbolic
247 #define __print_symbolic(value, symbol_array...) \
249 static const struct trace_print_flags symbols[] = \
250 { symbol_array, { -1, NULL }}; \
251 ftrace_print_symbols_seq(p, value, symbols); \
255 #define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
257 ftrace_raw_output_##call(struct trace_iterator *iter, int flags) \
259 struct trace_seq *s = &iter->seq; \
260 struct ftrace_raw_##call *field; \
261 struct trace_entry *entry; \
262 struct trace_seq *p; \
267 if (entry->type != event_##call.id) { \
269 return TRACE_TYPE_UNHANDLED; \
272 field = (typeof(field))entry; \
274 p = &get_cpu_var(ftrace_event_seq); \
276 ret = trace_seq_printf(s, #call ": " print); \
279 return TRACE_TYPE_PARTIAL_LINE; \
281 return TRACE_TYPE_HANDLED; \
284 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
287 #define __field_ext(type, item, filter_type) \
288 ret = trace_define_field(event_call, #type, #item, \
289 offsetof(typeof(field), item), \
290 sizeof(field.item), \
291 is_signed_type(type), filter_type); \
296 #define __field(type, item) __field_ext(type, item, FILTER_OTHER)
299 #define __array(type, item, len) \
300 BUILD_BUG_ON(len > MAX_FILTER_STR_VAL); \
301 ret = trace_define_field(event_call, #type "[" #len "]", #item, \
302 offsetof(typeof(field), item), \
303 sizeof(field.item), 0, FILTER_OTHER); \
307 #undef __dynamic_array
308 #define __dynamic_array(type, item, len) \
309 ret = trace_define_field(event_call, "__data_loc " #type "[]", #item, \
310 offsetof(typeof(field), __data_loc_##item), \
311 sizeof(field.__data_loc_##item), 0, \
315 #define __string(item, src) __dynamic_array(char, item, -1)
318 #define TRACE_EVENT(call, proto, args, tstruct, func, print) \
320 ftrace_define_fields_##call(struct ftrace_event_call *event_call) \
322 struct ftrace_raw_##call field; \
325 ret = trace_define_common_fields(event_call); \
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) \
363 #define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
364 static inline 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; \
375 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
377 #ifdef CONFIG_EVENT_PROFILE
380 * Generate the functions needed for tracepoint perf_counter support.
382 * NOTE: The insertion profile callback (ftrace_profile_<call>) is defined later
384 * static int ftrace_profile_enable_<call>(struct ftrace_event_call *event_call)
388 * if (!atomic_inc_return(&event_call->profile_count))
389 * ret = register_trace_<call>(ftrace_profile_<call>);
394 * static void ftrace_profile_disable_<call>(struct ftrace_event_call *event_call)
396 * if (atomic_add_negative(-1, &event->call->profile_count))
397 * unregister_trace_<call>(ftrace_profile_<call>);
403 #define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
405 static void ftrace_profile_##call(proto); \
407 static int ftrace_profile_enable_##call(struct ftrace_event_call *event_call) \
411 if (!atomic_inc_return(&event_call->profile_count)) \
412 ret = register_trace_##call(ftrace_profile_##call); \
417 static void ftrace_profile_disable_##call(struct ftrace_event_call *event_call)\
419 if (atomic_add_negative(-1, &event_call->profile_count)) \
420 unregister_trace_##call(ftrace_profile_##call); \
423 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
428 * Stage 4 of the trace events.
430 * Override the macros in <trace/trace_events.h> to include the following:
432 * static void ftrace_event_<call>(proto)
434 * event_trace_printk(_RET_IP_, "<call>: " <fmt>);
437 * static int ftrace_reg_event_<call>(void)
441 * ret = register_trace_<call>(ftrace_event_<call>);
443 * pr_info("event trace: Could not activate trace point "
444 * "probe to <call>");
448 * static void ftrace_unreg_event_<call>(void)
450 * unregister_trace_<call>(ftrace_event_<call>);
454 * For those macros defined with TRACE_EVENT:
456 * static struct ftrace_event_call event_<call>;
458 * static void ftrace_raw_event_<call>(proto)
460 * struct ring_buffer_event *event;
461 * struct ftrace_raw_<call> *entry; <-- defined in stage 1
462 * unsigned long irq_flags;
465 * local_save_flags(irq_flags);
466 * pc = preempt_count();
468 * event = trace_current_buffer_lock_reserve(event_<call>.id,
469 * sizeof(struct ftrace_raw_<call>),
473 * entry = ring_buffer_event_data(event);
475 * <assign>; <-- Here we assign the entries by the __field and
478 * trace_current_buffer_unlock_commit(event, irq_flags, pc);
481 * static int ftrace_raw_reg_event_<call>(void)
485 * ret = register_trace_<call>(ftrace_raw_event_<call>);
487 * pr_info("event trace: Could not activate trace point "
488 * "probe to <call>");
492 * static void ftrace_unreg_event_<call>(void)
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 int ftrace_raw_init_event_<call>(void)
505 * id = register_ftrace_event(&ftrace_event_type_<call>);
508 * event_<call>.id = id;
512 * static struct ftrace_event_call __used
513 * __attribute__((__aligned__(4)))
514 * __attribute__((section("_ftrace_events"))) event_<call> = {
516 * .system = "<system>",
517 * .raw_init = ftrace_raw_init_event_<call>,
518 * .regfunc = ftrace_reg_event_<call>,
519 * .unregfunc = ftrace_unreg_event_<call>,
520 * .show_format = ftrace_format_<call>,
526 #define TP_FMT(fmt, args...) fmt "\n", ##args
528 #ifdef CONFIG_EVENT_PROFILE
530 #define _TRACE_PROFILE_INIT(call) \
531 .profile_count = ATOMIC_INIT(-1), \
532 .profile_enable = ftrace_profile_enable_##call, \
533 .profile_disable = ftrace_profile_disable_##call,
536 #define _TRACE_PROFILE_INIT(call)
540 #define __entry entry
543 #define __field(type, item)
546 #define __array(type, item, len)
548 #undef __dynamic_array
549 #define __dynamic_array(type, item, len) \
550 __entry->__data_loc_##item = __data_offsets.item;
553 #define __string(item, src) __dynamic_array(char, item, -1) \
556 #define __assign_str(dst, src) \
557 strcpy(__get_str(dst), src);
560 #define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
562 static struct ftrace_event_call event_##call; \
564 static void ftrace_raw_event_##call(proto) \
566 struct ftrace_data_offsets_##call __maybe_unused __data_offsets;\
567 struct ftrace_event_call *event_call = &event_##call; \
568 struct ring_buffer_event *event; \
569 struct ftrace_raw_##call *entry; \
570 unsigned long irq_flags; \
574 local_save_flags(irq_flags); \
575 pc = preempt_count(); \
577 __data_size = ftrace_get_offsets_##call(&__data_offsets, args); \
579 event = trace_current_buffer_lock_reserve(event_##call.id, \
580 sizeof(*entry) + __data_size, \
584 entry = ring_buffer_event_data(event); \
591 if (!filter_current_check_discard(event_call, entry, event)) \
592 trace_nowake_buffer_unlock_commit(event, irq_flags, pc); \
595 static int ftrace_raw_reg_event_##call(void *ptr) \
599 ret = register_trace_##call(ftrace_raw_event_##call); \
601 pr_info("event trace: Could not activate trace point " \
602 "probe to " #call "\n"); \
606 static void ftrace_raw_unreg_event_##call(void *ptr) \
608 unregister_trace_##call(ftrace_raw_event_##call); \
611 static struct trace_event ftrace_event_type_##call = { \
612 .trace = ftrace_raw_output_##call, \
615 static int ftrace_raw_init_event_##call(void) \
619 id = register_ftrace_event(&ftrace_event_type_##call); \
622 event_##call.id = id; \
623 INIT_LIST_HEAD(&event_##call.fields); \
624 init_preds(&event_##call); \
628 static struct ftrace_event_call __used \
629 __attribute__((__aligned__(4))) \
630 __attribute__((section("_ftrace_events"))) event_##call = { \
632 .system = __stringify(TRACE_SYSTEM), \
633 .event = &ftrace_event_type_##call, \
634 .raw_init = ftrace_raw_init_event_##call, \
635 .regfunc = ftrace_raw_reg_event_##call, \
636 .unregfunc = ftrace_raw_unreg_event_##call, \
637 .show_format = ftrace_format_##call, \
638 .define_fields = ftrace_define_fields_##call, \
639 _TRACE_PROFILE_INIT(call) \
642 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
645 * Define the insertion callback to profile events
647 * The job is very similar to ftrace_raw_event_<call> except that we don't
648 * insert in the ring buffer but in a perf counter.
650 * static void ftrace_profile_<call>(proto)
652 * struct ftrace_data_offsets_<call> __maybe_unused __data_offsets;
653 * struct ftrace_event_call *event_call = &event_<call>;
654 * extern void perf_tpcounter_event(int, u64, u64, void *, int);
655 * struct ftrace_raw_##call *entry;
656 * u64 __addr = 0, __count = 1;
657 * unsigned long irq_flags;
662 * local_save_flags(irq_flags);
663 * pc = preempt_count();
665 * __data_size = ftrace_get_offsets_<call>(&__data_offsets, args);
667 * // Below we want to get the aligned size by taking into account
668 * // the u32 field that will later store the buffer size
669 * __entry_size = ALIGN(__data_size + sizeof(*entry) + sizeof(u32),
671 * __entry_size -= sizeof(u32);
674 * char raw_data[__entry_size]; <- allocate our sample in the stack
675 * struct trace_entry *ent;
677 * zero dead bytes from alignment to avoid stack leak to userspace:
679 * *(u64 *)(&raw_data[__entry_size - sizeof(u64)]) = 0ULL;
680 * entry = (struct ftrace_raw_<call> *)raw_data;
682 * tracing_generic_entry_update(ent, irq_flags, pc);
683 * ent->type = event_call->id;
685 * <tstruct> <- do some jobs with dynamic arrays
687 * <assign> <- affect our values
689 * perf_tpcounter_event(event_call->id, __addr, __count, entry,
690 * __entry_size); <- submit them to perf counter
696 #ifdef CONFIG_EVENT_PROFILE
699 #define __perf_addr(a) __addr = (a)
702 #define __perf_count(c) __count = (c)
705 #define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
706 static void ftrace_profile_##call(proto) \
708 struct ftrace_data_offsets_##call __maybe_unused __data_offsets;\
709 struct ftrace_event_call *event_call = &event_##call; \
710 extern void perf_tpcounter_event(int, u64, u64, void *, int); \
711 struct ftrace_raw_##call *entry; \
712 u64 __addr = 0, __count = 1; \
713 unsigned long irq_flags; \
718 local_save_flags(irq_flags); \
719 pc = preempt_count(); \
721 __data_size = ftrace_get_offsets_##call(&__data_offsets, args); \
722 __entry_size = ALIGN(__data_size + sizeof(*entry) + sizeof(u32),\
724 __entry_size -= sizeof(u32); \
727 char raw_data[__entry_size]; \
728 struct trace_entry *ent; \
730 *(u64 *)(&raw_data[__entry_size - sizeof(u64)]) = 0ULL; \
731 entry = (struct ftrace_raw_##call *)raw_data; \
733 tracing_generic_entry_update(ent, irq_flags, pc); \
734 ent->type = event_call->id; \
740 perf_tpcounter_event(event_call->id, __addr, __count, entry,\
746 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
747 #endif /* CONFIG_EVENT_PROFILE */
749 #undef _TRACE_PROFILE_INIT