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 __array(type, item, len) type item[len];
27 #undef __dynamic_array
28 #define __dynamic_array(type, item, len) u32 __data_loc_##item;
31 #define __string(item, src) __dynamic_array(char, item, -1)
33 #undef TP_STRUCT__entry
34 #define TP_STRUCT__entry(args...) args
37 #define TRACE_EVENT(name, proto, args, tstruct, assign, print) \
38 struct ftrace_raw_##name { \
39 struct trace_entry ent; \
43 static struct ftrace_event_call event_##name
45 /* Callbacks are meaningless to ftrace. */
47 #define TRACE_EVENT_FN(name, proto, args, tstruct, \
48 assign, print, reg, unreg) \
49 TRACE_EVENT(name, TP_PROTO(proto), TP_ARGS(args), \
50 TP_STRUCT__entry(tstruct), \
51 TP_fast_assign(assign), \
54 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
58 * Stage 2 of the trace events.
60 * Include the following:
62 * struct ftrace_data_offsets_<call> {
68 * The __dynamic_array() macro will create each u32 <item>, this is
69 * to keep the offset of each array from the beginning of the event.
70 * The size of an array is also encoded, in the higher 16 bits of <item>.
74 #define __field(type, item);
77 #define __array(type, item, len)
79 #undef __dynamic_array
80 #define __dynamic_array(type, item, len) u32 item;
83 #define __string(item, src) __dynamic_array(char, item, -1)
86 #define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
87 struct ftrace_data_offsets_##call { \
91 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
94 * Setup the showing format of trace point.
97 * ftrace_format_##call(struct trace_seq *s)
99 * struct ftrace_raw_##call field;
102 * ret = trace_seq_printf(s, #type " " #item ";"
103 * " offset:%u; size:%u;\n",
104 * offsetof(struct ftrace_raw_##call, item),
105 * sizeof(field.type));
110 #undef TP_STRUCT__entry
111 #define TP_STRUCT__entry(args...) args
114 #define __field(type, item) \
115 ret = trace_seq_printf(s, "\tfield:" #type " " #item ";\t" \
116 "offset:%u;\tsize:%u;\n", \
117 (unsigned int)offsetof(typeof(field), item), \
118 (unsigned int)sizeof(field.item)); \
123 #define __array(type, item, len) \
124 ret = trace_seq_printf(s, "\tfield:" #type " " #item "[" #len "];\t" \
125 "offset:%u;\tsize:%u;\n", \
126 (unsigned int)offsetof(typeof(field), item), \
127 (unsigned int)sizeof(field.item)); \
131 #undef __dynamic_array
132 #define __dynamic_array(type, item, len) \
133 ret = trace_seq_printf(s, "\tfield:__data_loc " #type "[] " #item ";\t"\
134 "offset:%u;\tsize:%u;\n", \
135 (unsigned int)offsetof(typeof(field), \
136 __data_loc_##item), \
137 (unsigned int)sizeof(field.__data_loc_##item)); \
142 #define __string(item, src) __dynamic_array(char, item, -1)
147 #undef __print_symbolic
148 #undef __get_dynamic_array
152 #define TP_printk(fmt, args...) "%s, %s\n", #fmt, __stringify(args)
154 #undef TP_fast_assign
155 #define TP_fast_assign(args...) args
157 #undef TP_perf_assign
158 #define TP_perf_assign(args...)
161 #define TRACE_EVENT(call, proto, args, tstruct, func, print) \
163 ftrace_format_##call(struct ftrace_event_call *unused, \
164 struct trace_seq *s) \
166 struct ftrace_raw_##call field __attribute__((unused)); \
171 trace_seq_printf(s, "\nprint fmt: " print); \
176 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
179 * Stage 3 of the trace events.
181 * Override the macros in <trace/trace_events.h> to include the following:
184 * ftrace_raw_output_<call>(struct trace_iterator *iter, int flags)
186 * struct trace_seq *s = &iter->seq;
187 * struct ftrace_raw_<call> *field; <-- defined in stage 1
188 * struct trace_entry *entry;
189 * struct trace_seq *p;
194 * if (entry->type != event_<call>.id) {
196 * return TRACE_TYPE_UNHANDLED;
199 * field = (typeof(field))entry;
201 * p = get_cpu_var(ftrace_event_seq);
203 * ret = trace_seq_printf(s, <TP_printk> "\n");
206 * return TRACE_TYPE_PARTIAL_LINE;
208 * return TRACE_TYPE_HANDLED;
211 * This is the method used to print the raw event to the trace
212 * output format. Note, this is not needed if the data is read
217 #define __entry field
220 #define TP_printk(fmt, args...) fmt "\n", args
222 #undef __get_dynamic_array
223 #define __get_dynamic_array(field) \
224 ((void *)__entry + (__entry->__data_loc_##field & 0xffff))
227 #define __get_str(field) (char *)__get_dynamic_array(field)
230 #define __print_flags(flag, delim, flag_array...) \
232 static const struct trace_print_flags flags[] = \
233 { flag_array, { -1, NULL }}; \
234 ftrace_print_flags_seq(p, delim, flag, flags); \
237 #undef __print_symbolic
238 #define __print_symbolic(value, symbol_array...) \
240 static const struct trace_print_flags symbols[] = \
241 { symbol_array, { -1, NULL }}; \
242 ftrace_print_symbols_seq(p, value, symbols); \
246 #define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
248 ftrace_raw_output_##call(struct trace_iterator *iter, int flags) \
250 struct trace_seq *s = &iter->seq; \
251 struct ftrace_raw_##call *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, #call ": " print); \
270 return TRACE_TYPE_PARTIAL_LINE; \
272 return TRACE_TYPE_HANDLED; \
275 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
278 #define __field(type, item) \
279 ret = trace_define_field(event_call, #type, #item, \
280 offsetof(typeof(field), item), \
281 sizeof(field.item), is_signed_type(type)); \
286 #define __array(type, item, len) \
287 BUILD_BUG_ON(len > MAX_FILTER_STR_VAL); \
288 ret = trace_define_field(event_call, #type "[" #len "]", #item, \
289 offsetof(typeof(field), item), \
290 sizeof(field.item), 0); \
294 #undef __dynamic_array
295 #define __dynamic_array(type, item, len) \
296 ret = trace_define_field(event_call, "__data_loc " #type "[]", #item, \
297 offsetof(typeof(field), __data_loc_##item), \
298 sizeof(field.__data_loc_##item), 0);
301 #define __string(item, src) __dynamic_array(char, item, -1)
304 #define TRACE_EVENT(call, proto, args, tstruct, func, print) \
306 ftrace_define_fields_##call(struct ftrace_event_call *event_call) \
308 struct ftrace_raw_##call field; \
311 ret = trace_define_common_fields(event_call); \
320 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
323 * remember the offset of each array from the beginning of the event.
327 #define __entry entry
330 #define __field(type, item)
333 #define __array(type, item, len)
335 #undef __dynamic_array
336 #define __dynamic_array(type, item, len) \
337 __data_offsets->item = __data_size + \
338 offsetof(typeof(*entry), __data); \
339 __data_offsets->item |= (len * sizeof(type)) << 16; \
340 __data_size += (len) * sizeof(type);
343 #define __string(item, src) __dynamic_array(char, item, strlen(src) + 1) \
346 #define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
347 static inline int ftrace_get_offsets_##call( \
348 struct ftrace_data_offsets_##call *__data_offsets, proto) \
350 int __data_size = 0; \
351 struct ftrace_raw_##call __maybe_unused *entry; \
355 return __data_size; \
358 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
360 #ifdef CONFIG_EVENT_PROFILE
363 * Generate the functions needed for tracepoint perf_counter support.
365 * NOTE: The insertion profile callback (ftrace_profile_<call>) is defined later
367 * static int ftrace_profile_enable_<call>(struct ftrace_event_call *event_call)
371 * if (!atomic_inc_return(&event_call->profile_count))
372 * ret = register_trace_<call>(ftrace_profile_<call>);
377 * static void ftrace_profile_disable_<call>(struct ftrace_event_call *event_call)
379 * if (atomic_add_negative(-1, &event->call->profile_count))
380 * unregister_trace_<call>(ftrace_profile_<call>);
386 #define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
388 static void ftrace_profile_##call(proto); \
390 static int ftrace_profile_enable_##call(struct ftrace_event_call *event_call) \
394 if (!atomic_inc_return(&event_call->profile_count)) \
395 ret = register_trace_##call(ftrace_profile_##call); \
400 static void ftrace_profile_disable_##call(struct ftrace_event_call *event_call)\
402 if (atomic_add_negative(-1, &event_call->profile_count)) \
403 unregister_trace_##call(ftrace_profile_##call); \
406 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
411 * Stage 4 of the trace events.
413 * Override the macros in <trace/trace_events.h> to include the following:
415 * static void ftrace_event_<call>(proto)
417 * event_trace_printk(_RET_IP_, "<call>: " <fmt>);
420 * static int ftrace_reg_event_<call>(void)
424 * ret = register_trace_<call>(ftrace_event_<call>);
426 * pr_info("event trace: Could not activate trace point "
427 * "probe to <call>");
431 * static void ftrace_unreg_event_<call>(void)
433 * unregister_trace_<call>(ftrace_event_<call>);
437 * For those macros defined with TRACE_EVENT:
439 * static struct ftrace_event_call event_<call>;
441 * static void ftrace_raw_event_<call>(proto)
443 * struct ring_buffer_event *event;
444 * struct ftrace_raw_<call> *entry; <-- defined in stage 1
445 * unsigned long irq_flags;
448 * local_save_flags(irq_flags);
449 * pc = preempt_count();
451 * event = trace_current_buffer_lock_reserve(event_<call>.id,
452 * sizeof(struct ftrace_raw_<call>),
456 * entry = ring_buffer_event_data(event);
458 * <assign>; <-- Here we assign the entries by the __field and
461 * trace_current_buffer_unlock_commit(event, irq_flags, pc);
464 * static int ftrace_raw_reg_event_<call>(void)
468 * ret = register_trace_<call>(ftrace_raw_event_<call>);
470 * pr_info("event trace: Could not activate trace point "
471 * "probe to <call>");
475 * static void ftrace_unreg_event_<call>(void)
477 * unregister_trace_<call>(ftrace_raw_event_<call>);
480 * static struct trace_event ftrace_event_type_<call> = {
481 * .trace = ftrace_raw_output_<call>, <-- stage 2
484 * static int ftrace_raw_init_event_<call>(void)
488 * id = register_ftrace_event(&ftrace_event_type_<call>);
491 * event_<call>.id = id;
495 * static struct ftrace_event_call __used
496 * __attribute__((__aligned__(4)))
497 * __attribute__((section("_ftrace_events"))) event_<call> = {
499 * .system = "<system>",
500 * .raw_init = ftrace_raw_init_event_<call>,
501 * .regfunc = ftrace_reg_event_<call>,
502 * .unregfunc = ftrace_unreg_event_<call>,
503 * .show_format = ftrace_format_<call>,
509 #define TP_FMT(fmt, args...) fmt "\n", ##args
511 #ifdef CONFIG_EVENT_PROFILE
513 #define _TRACE_PROFILE_INIT(call) \
514 .profile_count = ATOMIC_INIT(-1), \
515 .profile_enable = ftrace_profile_enable_##call, \
516 .profile_disable = ftrace_profile_disable_##call,
519 #define _TRACE_PROFILE_INIT(call)
523 #define __entry entry
526 #define __field(type, item)
529 #define __array(type, item, len)
531 #undef __dynamic_array
532 #define __dynamic_array(type, item, len) \
533 __entry->__data_loc_##item = __data_offsets.item;
536 #define __string(item, src) __dynamic_array(char, item, -1) \
539 #define __assign_str(dst, src) \
540 strcpy(__get_str(dst), src);
543 #define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
545 static struct ftrace_event_call event_##call; \
547 static void ftrace_raw_event_##call(proto) \
549 struct ftrace_data_offsets_##call __maybe_unused __data_offsets;\
550 struct ftrace_event_call *event_call = &event_##call; \
551 struct ring_buffer_event *event; \
552 struct ftrace_raw_##call *entry; \
553 unsigned long irq_flags; \
557 local_save_flags(irq_flags); \
558 pc = preempt_count(); \
560 __data_size = ftrace_get_offsets_##call(&__data_offsets, args); \
562 event = trace_current_buffer_lock_reserve(event_##call.id, \
563 sizeof(*entry) + __data_size, \
567 entry = ring_buffer_event_data(event); \
574 if (!filter_current_check_discard(event_call, entry, event)) \
575 trace_nowake_buffer_unlock_commit(event, irq_flags, pc); \
578 static int ftrace_raw_reg_event_##call(void *ptr) \
582 ret = register_trace_##call(ftrace_raw_event_##call); \
584 pr_info("event trace: Could not activate trace point " \
585 "probe to " #call "\n"); \
589 static void ftrace_raw_unreg_event_##call(void *ptr) \
591 unregister_trace_##call(ftrace_raw_event_##call); \
594 static struct trace_event ftrace_event_type_##call = { \
595 .trace = ftrace_raw_output_##call, \
598 static int ftrace_raw_init_event_##call(void) \
602 id = register_ftrace_event(&ftrace_event_type_##call); \
605 event_##call.id = id; \
606 INIT_LIST_HEAD(&event_##call.fields); \
607 init_preds(&event_##call); \
611 static struct ftrace_event_call __used \
612 __attribute__((__aligned__(4))) \
613 __attribute__((section("_ftrace_events"))) event_##call = { \
615 .system = __stringify(TRACE_SYSTEM), \
616 .event = &ftrace_event_type_##call, \
617 .raw_init = ftrace_raw_init_event_##call, \
618 .regfunc = ftrace_raw_reg_event_##call, \
619 .unregfunc = ftrace_raw_unreg_event_##call, \
620 .show_format = ftrace_format_##call, \
621 .define_fields = ftrace_define_fields_##call, \
622 _TRACE_PROFILE_INIT(call) \
625 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
628 * Define the insertion callback to profile events
630 * The job is very similar to ftrace_raw_event_<call> except that we don't
631 * insert in the ring buffer but in a perf counter.
633 * static void ftrace_profile_<call>(proto)
635 * struct ftrace_data_offsets_<call> __maybe_unused __data_offsets;
636 * struct ftrace_event_call *event_call = &event_<call>;
637 * extern void perf_tpcounter_event(int, u64, u64, void *, int);
638 * struct ftrace_raw_##call *entry;
639 * u64 __addr = 0, __count = 1;
640 * unsigned long irq_flags;
645 * local_save_flags(irq_flags);
646 * pc = preempt_count();
648 * __data_size = ftrace_get_offsets_<call>(&__data_offsets, args);
650 * // Below we want to get the aligned size by taking into account
651 * // the u32 field that will later store the buffer size
652 * __entry_size = ALIGN(__data_size + sizeof(*entry) + sizeof(u32),
654 * __entry_size -= sizeof(u32);
657 * char raw_data[__entry_size]; <- allocate our sample in the stack
658 * struct trace_entry *ent;
660 * zero dead bytes from alignment to avoid stack leak to userspace:
662 * *(u64 *)(&raw_data[__entry_size - sizeof(u64)]) = 0ULL;
663 * entry = (struct ftrace_raw_<call> *)raw_data;
665 * tracing_generic_entry_update(ent, irq_flags, pc);
666 * ent->type = event_call->id;
668 * <tstruct> <- do some jobs with dynamic arrays
670 * <assign> <- affect our values
672 * perf_tpcounter_event(event_call->id, __addr, __count, entry,
673 * __entry_size); <- submit them to perf counter
679 #ifdef CONFIG_EVENT_PROFILE
682 #define __perf_addr(a) __addr = (a)
685 #define __perf_count(c) __count = (c)
688 #define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
689 static void ftrace_profile_##call(proto) \
691 struct ftrace_data_offsets_##call __maybe_unused __data_offsets;\
692 struct ftrace_event_call *event_call = &event_##call; \
693 extern void perf_tpcounter_event(int, u64, u64, void *, int); \
694 struct ftrace_raw_##call *entry; \
695 u64 __addr = 0, __count = 1; \
696 unsigned long irq_flags; \
701 local_save_flags(irq_flags); \
702 pc = preempt_count(); \
704 __data_size = ftrace_get_offsets_##call(&__data_offsets, args); \
705 __entry_size = ALIGN(__data_size + sizeof(*entry) + sizeof(u32),\
707 __entry_size -= sizeof(u32); \
710 char raw_data[__entry_size]; \
711 struct trace_entry *ent; \
713 *(u64 *)(&raw_data[__entry_size - sizeof(u64)]) = 0ULL; \
714 entry = (struct ftrace_raw_##call *)raw_data; \
716 tracing_generic_entry_update(ent, irq_flags, pc); \
717 ent->type = event_call->id; \
723 perf_tpcounter_event(event_call->id, __addr, __count, entry,\
729 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
730 #endif /* CONFIG_EVENT_PROFILE */
732 #undef _TRACE_PROFILE_INIT