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 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
52 * Stage 2 of the trace events.
54 * Include the following:
56 * struct ftrace_data_offsets_<call> {
62 * The __dynamic_array() macro will create each u32 <item>, this is
63 * to keep the offset of each array from the beginning of the event.
64 * The size of an array is also encoded, in the higher 16 bits of <item>.
68 #define __field(type, item)
71 #define __field_ext(type, item, filter_type)
74 #define __array(type, item, len)
76 #undef __dynamic_array
77 #define __dynamic_array(type, item, len) u32 item;
80 #define __string(item, src) __dynamic_array(char, item, -1)
83 #define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
84 struct ftrace_data_offsets_##call { \
88 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
91 * Setup the showing format of trace point.
94 * ftrace_format_##call(struct trace_seq *s)
96 * struct ftrace_raw_##call field;
99 * ret = trace_seq_printf(s, #type " " #item ";"
100 * " offset:%u; size:%u;\n",
101 * offsetof(struct ftrace_raw_##call, item),
102 * sizeof(field.type));
107 #undef TP_STRUCT__entry
108 #define TP_STRUCT__entry(args...) args
111 #define __field(type, item) \
112 ret = trace_seq_printf(s, "\tfield:" #type " " #item ";\t" \
113 "offset:%u;\tsize:%u;\n", \
114 (unsigned int)offsetof(typeof(field), item), \
115 (unsigned int)sizeof(field.item)); \
120 #define __field_ext(type, item, filter_type) __field(type, 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_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), 0, FILTER_OTHER); \
298 #undef __dynamic_array
299 #define __dynamic_array(type, item, len) \
300 ret = trace_define_field(event_call, "__data_loc " #type "[]", #item, \
301 offsetof(typeof(field), __data_loc_##item), \
302 sizeof(field.__data_loc_##item), 0, \
306 #define __string(item, src) __dynamic_array(char, item, -1)
309 #define TRACE_EVENT(call, proto, args, tstruct, func, print) \
311 ftrace_define_fields_##call(struct ftrace_event_call *event_call) \
313 struct ftrace_raw_##call field; \
316 ret = trace_define_common_fields(event_call); \
325 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
328 * remember the offset of each array from the beginning of the event.
332 #define __entry entry
335 #define __field(type, item)
338 #define __field_ext(type, item, filter_type)
341 #define __array(type, item, len)
343 #undef __dynamic_array
344 #define __dynamic_array(type, item, len) \
345 __data_offsets->item = __data_size + \
346 offsetof(typeof(*entry), __data); \
347 __data_offsets->item |= (len * sizeof(type)) << 16; \
348 __data_size += (len) * sizeof(type);
351 #define __string(item, src) __dynamic_array(char, item, strlen(src) + 1) \
354 #define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
355 static inline int ftrace_get_offsets_##call( \
356 struct ftrace_data_offsets_##call *__data_offsets, proto) \
358 int __data_size = 0; \
359 struct ftrace_raw_##call __maybe_unused *entry; \
363 return __data_size; \
366 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
368 #ifdef CONFIG_EVENT_PROFILE
371 * Generate the functions needed for tracepoint perf_counter support.
373 * NOTE: The insertion profile callback (ftrace_profile_<call>) is defined later
375 * static int ftrace_profile_enable_<call>(struct ftrace_event_call *event_call)
379 * if (!atomic_inc_return(&event_call->profile_count))
380 * ret = register_trace_<call>(ftrace_profile_<call>);
385 * static void ftrace_profile_disable_<call>(struct ftrace_event_call *event_call)
387 * if (atomic_add_negative(-1, &event->call->profile_count))
388 * unregister_trace_<call>(ftrace_profile_<call>);
394 #define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
396 static void ftrace_profile_##call(proto); \
398 static int ftrace_profile_enable_##call(struct ftrace_event_call *event_call) \
402 if (!atomic_inc_return(&event_call->profile_count)) \
403 ret = register_trace_##call(ftrace_profile_##call); \
408 static void ftrace_profile_disable_##call(struct ftrace_event_call *event_call)\
410 if (atomic_add_negative(-1, &event_call->profile_count)) \
411 unregister_trace_##call(ftrace_profile_##call); \
414 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
419 * Stage 4 of the trace events.
421 * Override the macros in <trace/trace_events.h> to include the following:
423 * static void ftrace_event_<call>(proto)
425 * event_trace_printk(_RET_IP_, "<call>: " <fmt>);
428 * static int ftrace_reg_event_<call>(void)
432 * ret = register_trace_<call>(ftrace_event_<call>);
434 * pr_info("event trace: Could not activate trace point "
435 * "probe to <call>");
439 * static void ftrace_unreg_event_<call>(void)
441 * unregister_trace_<call>(ftrace_event_<call>);
445 * For those macros defined with TRACE_EVENT:
447 * static struct ftrace_event_call event_<call>;
449 * static void ftrace_raw_event_<call>(proto)
451 * struct ring_buffer_event *event;
452 * struct ftrace_raw_<call> *entry; <-- defined in stage 1
453 * unsigned long irq_flags;
456 * local_save_flags(irq_flags);
457 * pc = preempt_count();
459 * event = trace_current_buffer_lock_reserve(event_<call>.id,
460 * sizeof(struct ftrace_raw_<call>),
464 * entry = ring_buffer_event_data(event);
466 * <assign>; <-- Here we assign the entries by the __field and
469 * trace_current_buffer_unlock_commit(event, irq_flags, pc);
472 * static int ftrace_raw_reg_event_<call>(void)
476 * ret = register_trace_<call>(ftrace_raw_event_<call>);
478 * pr_info("event trace: Could not activate trace point "
479 * "probe to <call>");
483 * static void ftrace_unreg_event_<call>(void)
485 * unregister_trace_<call>(ftrace_raw_event_<call>);
488 * static struct trace_event ftrace_event_type_<call> = {
489 * .trace = ftrace_raw_output_<call>, <-- stage 2
492 * static int ftrace_raw_init_event_<call>(void)
496 * id = register_ftrace_event(&ftrace_event_type_<call>);
499 * event_<call>.id = id;
503 * static struct ftrace_event_call __used
504 * __attribute__((__aligned__(4)))
505 * __attribute__((section("_ftrace_events"))) event_<call> = {
507 * .system = "<system>",
508 * .raw_init = ftrace_raw_init_event_<call>,
509 * .regfunc = ftrace_reg_event_<call>,
510 * .unregfunc = ftrace_unreg_event_<call>,
511 * .show_format = ftrace_format_<call>,
517 #define TP_FMT(fmt, args...) fmt "\n", ##args
519 #ifdef CONFIG_EVENT_PROFILE
521 #define _TRACE_PROFILE_INIT(call) \
522 .profile_count = ATOMIC_INIT(-1), \
523 .profile_enable = ftrace_profile_enable_##call, \
524 .profile_disable = ftrace_profile_disable_##call,
527 #define _TRACE_PROFILE_INIT(call)
531 #define __entry entry
534 #define __field(type, item)
537 #define __array(type, item, len)
539 #undef __dynamic_array
540 #define __dynamic_array(type, item, len) \
541 __entry->__data_loc_##item = __data_offsets.item;
544 #define __string(item, src) __dynamic_array(char, item, -1) \
547 #define __assign_str(dst, src) \
548 strcpy(__get_str(dst), src);
551 #define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
553 static struct ftrace_event_call event_##call; \
555 static void ftrace_raw_event_##call(proto) \
557 struct ftrace_data_offsets_##call __maybe_unused __data_offsets;\
558 struct ftrace_event_call *event_call = &event_##call; \
559 struct ring_buffer_event *event; \
560 struct ftrace_raw_##call *entry; \
561 unsigned long irq_flags; \
565 local_save_flags(irq_flags); \
566 pc = preempt_count(); \
568 __data_size = ftrace_get_offsets_##call(&__data_offsets, args); \
570 event = trace_current_buffer_lock_reserve(event_##call.id, \
571 sizeof(*entry) + __data_size, \
575 entry = ring_buffer_event_data(event); \
582 if (!filter_current_check_discard(event_call, entry, event)) \
583 trace_nowake_buffer_unlock_commit(event, irq_flags, pc); \
586 static int ftrace_raw_reg_event_##call(void *ptr) \
590 ret = register_trace_##call(ftrace_raw_event_##call); \
592 pr_info("event trace: Could not activate trace point " \
593 "probe to " #call "\n"); \
597 static void ftrace_raw_unreg_event_##call(void *ptr) \
599 unregister_trace_##call(ftrace_raw_event_##call); \
602 static struct trace_event ftrace_event_type_##call = { \
603 .trace = ftrace_raw_output_##call, \
606 static int ftrace_raw_init_event_##call(void) \
610 id = register_ftrace_event(&ftrace_event_type_##call); \
613 event_##call.id = id; \
614 INIT_LIST_HEAD(&event_##call.fields); \
615 init_preds(&event_##call); \
619 static struct ftrace_event_call __used \
620 __attribute__((__aligned__(4))) \
621 __attribute__((section("_ftrace_events"))) event_##call = { \
623 .system = __stringify(TRACE_SYSTEM), \
624 .event = &ftrace_event_type_##call, \
625 .raw_init = ftrace_raw_init_event_##call, \
626 .regfunc = ftrace_raw_reg_event_##call, \
627 .unregfunc = ftrace_raw_unreg_event_##call, \
628 .show_format = ftrace_format_##call, \
629 .define_fields = ftrace_define_fields_##call, \
630 _TRACE_PROFILE_INIT(call) \
633 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
636 * Define the insertion callback to profile events
638 * The job is very similar to ftrace_raw_event_<call> except that we don't
639 * insert in the ring buffer but in a perf counter.
641 * static void ftrace_profile_<call>(proto)
643 * struct ftrace_data_offsets_<call> __maybe_unused __data_offsets;
644 * struct ftrace_event_call *event_call = &event_<call>;
645 * extern void perf_tpcounter_event(int, u64, u64, void *, int);
646 * struct ftrace_raw_##call *entry;
647 * u64 __addr = 0, __count = 1;
648 * unsigned long irq_flags;
653 * local_save_flags(irq_flags);
654 * pc = preempt_count();
656 * __data_size = ftrace_get_offsets_<call>(&__data_offsets, args);
658 * // Below we want to get the aligned size by taking into account
659 * // the u32 field that will later store the buffer size
660 * __entry_size = ALIGN(__data_size + sizeof(*entry) + sizeof(u32),
662 * __entry_size -= sizeof(u32);
665 * char raw_data[__entry_size]; <- allocate our sample in the stack
666 * struct trace_entry *ent;
668 * zero dead bytes from alignment to avoid stack leak to userspace:
670 * *(u64 *)(&raw_data[__entry_size - sizeof(u64)]) = 0ULL;
671 * entry = (struct ftrace_raw_<call> *)raw_data;
673 * tracing_generic_entry_update(ent, irq_flags, pc);
674 * ent->type = event_call->id;
676 * <tstruct> <- do some jobs with dynamic arrays
678 * <assign> <- affect our values
680 * perf_tpcounter_event(event_call->id, __addr, __count, entry,
681 * __entry_size); <- submit them to perf counter
687 #ifdef CONFIG_EVENT_PROFILE
690 #define __perf_addr(a) __addr = (a)
693 #define __perf_count(c) __count = (c)
696 #define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
697 static void ftrace_profile_##call(proto) \
699 struct ftrace_data_offsets_##call __maybe_unused __data_offsets;\
700 struct ftrace_event_call *event_call = &event_##call; \
701 extern void perf_tpcounter_event(int, u64, u64, void *, int); \
702 struct ftrace_raw_##call *entry; \
703 u64 __addr = 0, __count = 1; \
704 unsigned long irq_flags; \
709 local_save_flags(irq_flags); \
710 pc = preempt_count(); \
712 __data_size = ftrace_get_offsets_##call(&__data_offsets, args); \
713 __entry_size = ALIGN(__data_size + sizeof(*entry) + sizeof(u32),\
715 __entry_size -= sizeof(u32); \
718 char raw_data[__entry_size]; \
719 struct trace_entry *ent; \
721 *(u64 *)(&raw_data[__entry_size - sizeof(u64)]) = 0ULL; \
722 entry = (struct ftrace_raw_##call *)raw_data; \
724 tracing_generic_entry_update(ent, irq_flags, pc); \
725 ent->type = event_call->id; \
731 perf_tpcounter_event(event_call->id, __addr, __count, entry,\
737 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
738 #endif /* CONFIG_EVENT_PROFILE */
740 #undef _TRACE_PROFILE_INIT