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 * DECLARE_EVENT_CLASS 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 DECLARE_EVENT_CLASS 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 DECLARE_EVENT_CLASS(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 DECLARE_EVENT_CLASS
60 #define DECLARE_EVENT_CLASS(name, proto, args, tstruct, assign, print) \
61 struct ftrace_raw_##name { \
62 struct trace_entry ent; \
67 static struct ftrace_event_class event_class_##name;
70 #define DEFINE_EVENT(template, name, proto, args) \
71 static struct ftrace_event_call __used \
72 __attribute__((__aligned__(4))) event_##name
74 #undef DEFINE_EVENT_PRINT
75 #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
76 DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
78 /* Callbacks are meaningless to ftrace. */
80 #define TRACE_EVENT_FN(name, proto, args, tstruct, \
81 assign, print, reg, unreg) \
82 TRACE_EVENT(name, PARAMS(proto), PARAMS(args), \
83 PARAMS(tstruct), PARAMS(assign), PARAMS(print)) \
85 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
89 * Stage 2 of the trace events.
91 * Include the following:
93 * struct ftrace_data_offsets_<call> {
99 * The __dynamic_array() macro will create each u32 <item>, this is
100 * to keep the offset of each array from the beginning of the event.
101 * The size of an array is also encoded, in the higher 16 bits of <item>.
105 #define __field(type, item)
108 #define __field_ext(type, item, filter_type)
111 #define __array(type, item, len)
113 #undef __dynamic_array
114 #define __dynamic_array(type, item, len) u32 item;
117 #define __string(item, src) __dynamic_array(char, item, -1)
119 #undef DECLARE_EVENT_CLASS
120 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
121 struct ftrace_data_offsets_##call { \
126 #define DEFINE_EVENT(template, name, proto, args)
128 #undef DEFINE_EVENT_PRINT
129 #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
130 DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
132 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
135 * Stage 3 of the trace events.
137 * Override the macros in <trace/trace_events.h> to include the following:
140 * ftrace_raw_output_<call>(struct trace_iterator *iter, int flags)
142 * struct trace_seq *s = &iter->seq;
143 * struct ftrace_raw_<call> *field; <-- defined in stage 1
144 * struct trace_entry *entry;
145 * struct trace_seq *p = &iter->tmp_seq;
150 * if (entry->type != event_<call>->event.type) {
152 * return TRACE_TYPE_UNHANDLED;
155 * field = (typeof(field))entry;
158 * ret = trace_seq_printf(s, "%s: ", <call>);
160 * ret = trace_seq_printf(s, <TP_printk> "\n");
162 * return TRACE_TYPE_PARTIAL_LINE;
164 * return TRACE_TYPE_HANDLED;
167 * This is the method used to print the raw event to the trace
168 * output format. Note, this is not needed if the data is read
173 #define __entry field
176 #define TP_printk(fmt, args...) fmt "\n", args
178 #undef __get_dynamic_array
179 #define __get_dynamic_array(field) \
180 ((void *)__entry + (__entry->__data_loc_##field & 0xffff))
183 #define __get_str(field) (char *)__get_dynamic_array(field)
186 #define __print_flags(flag, delim, flag_array...) \
188 static const struct trace_print_flags __flags[] = \
189 { flag_array, { -1, NULL }}; \
190 ftrace_print_flags_seq(p, delim, flag, __flags); \
193 #undef __print_symbolic
194 #define __print_symbolic(value, symbol_array...) \
196 static const struct trace_print_flags symbols[] = \
197 { symbol_array, { -1, NULL }}; \
198 ftrace_print_symbols_seq(p, value, symbols); \
202 #define __print_hex(buf, buf_len) ftrace_print_hex_seq(p, buf, buf_len)
204 #undef DECLARE_EVENT_CLASS
205 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
206 static notrace enum print_line_t \
207 ftrace_raw_output_##call(struct trace_iterator *iter, int flags, \
208 struct trace_event *trace_event) \
210 struct ftrace_event_call *event; \
211 struct trace_seq *s = &iter->seq; \
212 struct ftrace_raw_##call *field; \
213 struct trace_entry *entry; \
214 struct trace_seq *p = &iter->tmp_seq; \
217 event = container_of(trace_event, struct ftrace_event_call, \
222 if (entry->type != event->event.type) { \
224 return TRACE_TYPE_UNHANDLED; \
227 field = (typeof(field))entry; \
230 ret = trace_seq_printf(s, "%s: ", event->name); \
232 ret = trace_seq_printf(s, print); \
234 return TRACE_TYPE_PARTIAL_LINE; \
236 return TRACE_TYPE_HANDLED; \
238 static struct trace_event_functions ftrace_event_type_funcs_##call = { \
239 .trace = ftrace_raw_output_##call, \
242 #undef DEFINE_EVENT_PRINT
243 #define DEFINE_EVENT_PRINT(template, call, proto, args, print) \
244 static notrace enum print_line_t \
245 ftrace_raw_output_##call(struct trace_iterator *iter, int flags, \
246 struct trace_event *event) \
248 struct trace_seq *s = &iter->seq; \
249 struct ftrace_raw_##template *field; \
250 struct trace_entry *entry; \
251 struct trace_seq *p = &iter->tmp_seq; \
256 if (entry->type != event_##call.event.type) { \
258 return TRACE_TYPE_UNHANDLED; \
261 field = (typeof(field))entry; \
264 ret = trace_seq_printf(s, "%s: ", #call); \
266 ret = trace_seq_printf(s, print); \
268 return TRACE_TYPE_PARTIAL_LINE; \
270 return TRACE_TYPE_HANDLED; \
272 static struct trace_event_functions ftrace_event_type_funcs_##call = { \
273 .trace = ftrace_raw_output_##call, \
276 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
279 #define __field_ext(type, item, filter_type) \
280 ret = trace_define_field(event_call, #type, #item, \
281 offsetof(typeof(field), item), \
282 sizeof(field.item), \
283 is_signed_type(type), filter_type); \
288 #define __field(type, item) __field_ext(type, item, FILTER_OTHER)
291 #define __array(type, item, len) \
292 BUILD_BUG_ON(len > MAX_FILTER_STR_VAL); \
293 ret = trace_define_field(event_call, #type "[" #len "]", #item, \
294 offsetof(typeof(field), item), \
295 sizeof(field.item), \
296 is_signed_type(type), FILTER_OTHER); \
300 #undef __dynamic_array
301 #define __dynamic_array(type, item, len) \
302 ret = trace_define_field(event_call, "__data_loc " #type "[]", #item, \
303 offsetof(typeof(field), __data_loc_##item), \
304 sizeof(field.__data_loc_##item), \
305 is_signed_type(type), FILTER_OTHER);
308 #define __string(item, src) __dynamic_array(char, item, -1)
310 #undef DECLARE_EVENT_CLASS
311 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, func, print) \
313 ftrace_define_fields_##call(struct ftrace_event_call *event_call) \
315 struct ftrace_raw_##call field; \
324 #define DEFINE_EVENT(template, name, proto, args)
326 #undef DEFINE_EVENT_PRINT
327 #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
328 DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
330 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
333 * remember the offset of each array from the beginning of the event.
337 #define __entry entry
340 #define __field(type, item)
343 #define __field_ext(type, item, filter_type)
346 #define __array(type, item, len)
348 #undef __dynamic_array
349 #define __dynamic_array(type, item, len) \
350 __data_offsets->item = __data_size + \
351 offsetof(typeof(*entry), __data); \
352 __data_offsets->item |= (len * sizeof(type)) << 16; \
353 __data_size += (len) * sizeof(type);
356 #define __string(item, src) __dynamic_array(char, item, strlen(src) + 1)
358 #undef DECLARE_EVENT_CLASS
359 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
360 static inline notrace int ftrace_get_offsets_##call( \
361 struct ftrace_data_offsets_##call *__data_offsets, proto) \
363 int __data_size = 0; \
364 struct ftrace_raw_##call __maybe_unused *entry; \
368 return __data_size; \
372 #define DEFINE_EVENT(template, name, proto, args)
374 #undef DEFINE_EVENT_PRINT
375 #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
376 DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
378 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
381 * Stage 4 of the trace events.
383 * Override the macros in <trace/trace_events.h> to include the following:
385 * For those macros defined with TRACE_EVENT:
387 * static struct ftrace_event_call event_<call>;
389 * static void ftrace_raw_event_<call>(void *__data, proto)
391 * struct ftrace_event_call *event_call = __data;
392 * struct ftrace_data_offsets_<call> __maybe_unused __data_offsets;
393 * struct ring_buffer_event *event;
394 * struct ftrace_raw_<call> *entry; <-- defined in stage 1
395 * struct ring_buffer *buffer;
396 * unsigned long irq_flags;
400 * local_save_flags(irq_flags);
401 * pc = preempt_count();
403 * __data_size = ftrace_get_offsets_<call>(&__data_offsets, args);
405 * event = trace_current_buffer_lock_reserve(&buffer,
406 * event_<call>->event.type,
407 * sizeof(*entry) + __data_size,
411 * entry = ring_buffer_event_data(event);
413 * { <assign>; } <-- Here we assign the entries by the __field and
416 * if (!filter_current_check_discard(buffer, event_call, entry, event))
417 * trace_current_buffer_unlock_commit(buffer,
418 * event, irq_flags, pc);
421 * static struct trace_event ftrace_event_type_<call> = {
422 * .trace = ftrace_raw_output_<call>, <-- stage 2
425 * static const char print_fmt_<call>[] = <TP_printk>;
427 * static struct ftrace_event_class __used event_class_<template> = {
428 * .system = "<system>",
429 * .define_fields = ftrace_define_fields_<call>,
430 * .fields = LIST_HEAD_INIT(event_class_##call.fields),
431 * .raw_init = trace_event_raw_init,
432 * .probe = ftrace_raw_event_##call,
433 * .reg = ftrace_event_reg,
436 * static struct ftrace_event_call __used
437 * __attribute__((__aligned__(4)))
438 * __attribute__((section("_ftrace_events"))) event_<call> = {
440 * .class = event_class_<template>,
441 * .event = &ftrace_event_type_<call>,
442 * .print_fmt = print_fmt_<call>,
447 #ifdef CONFIG_PERF_EVENTS
449 #define _TRACE_PERF_PROTO(call, proto) \
450 static notrace void \
451 perf_trace_##call(void *__data, proto);
453 #define _TRACE_PERF_INIT(call) \
454 .perf_probe = perf_trace_##call,
457 #define _TRACE_PERF_PROTO(call, proto)
458 #define _TRACE_PERF_INIT(call)
459 #endif /* CONFIG_PERF_EVENTS */
462 #define __entry entry
465 #define __field(type, item)
468 #define __array(type, item, len)
470 #undef __dynamic_array
471 #define __dynamic_array(type, item, len) \
472 __entry->__data_loc_##item = __data_offsets.item;
475 #define __string(item, src) __dynamic_array(char, item, -1) \
478 #define __assign_str(dst, src) \
479 strcpy(__get_str(dst), src);
481 #undef TP_fast_assign
482 #define TP_fast_assign(args...) args
484 #undef TP_perf_assign
485 #define TP_perf_assign(args...)
487 #undef DECLARE_EVENT_CLASS
488 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
490 static notrace void \
491 ftrace_raw_event_##call(void *__data, proto) \
493 struct ftrace_event_call *event_call = __data; \
494 struct ftrace_data_offsets_##call __maybe_unused __data_offsets;\
495 struct ring_buffer_event *event; \
496 struct ftrace_raw_##call *entry; \
497 struct ring_buffer *buffer; \
498 unsigned long irq_flags; \
502 local_save_flags(irq_flags); \
503 pc = preempt_count(); \
505 __data_size = ftrace_get_offsets_##call(&__data_offsets, args); \
507 event = trace_current_buffer_lock_reserve(&buffer, \
508 event_call->event.type, \
509 sizeof(*entry) + __data_size, \
513 entry = ring_buffer_event_data(event); \
519 if (!filter_current_check_discard(buffer, event_call, entry, event)) \
520 trace_nowake_buffer_unlock_commit(buffer, \
521 event, irq_flags, pc); \
524 * The ftrace_test_probe is compiled out, it is only here as a build time check
525 * to make sure that if the tracepoint handling changes, the ftrace probe will
526 * fail to compile unless it too is updated.
530 #define DEFINE_EVENT(template, call, proto, args) \
531 static inline void ftrace_test_probe_##call(void) \
533 check_trace_callback_type_##call(ftrace_raw_event_##template); \
536 #undef DEFINE_EVENT_PRINT
537 #define DEFINE_EVENT_PRINT(template, name, proto, args, print)
539 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
545 #undef __print_symbolic
546 #undef __get_dynamic_array
550 #define TP_printk(fmt, args...) "\"" fmt "\", " __stringify(args)
552 #undef DECLARE_EVENT_CLASS
553 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
554 _TRACE_PERF_PROTO(call, PARAMS(proto)); \
555 static const char print_fmt_##call[] = print; \
556 static struct ftrace_event_class __used event_class_##call = { \
557 .system = __stringify(TRACE_SYSTEM), \
558 .define_fields = ftrace_define_fields_##call, \
559 .fields = LIST_HEAD_INIT(event_class_##call.fields),\
560 .raw_init = trace_event_raw_init, \
561 .probe = ftrace_raw_event_##call, \
562 .reg = ftrace_event_reg, \
563 _TRACE_PERF_INIT(call) \
567 #define DEFINE_EVENT(template, call, proto, args) \
569 static struct ftrace_event_call __used \
570 __attribute__((__aligned__(4))) \
571 __attribute__((section("_ftrace_events"))) event_##call = { \
573 .class = &event_class_##template, \
574 .event.funcs = &ftrace_event_type_funcs_##template, \
575 .print_fmt = print_fmt_##template, \
578 #undef DEFINE_EVENT_PRINT
579 #define DEFINE_EVENT_PRINT(template, call, proto, args, print) \
581 static const char print_fmt_##call[] = print; \
583 static struct ftrace_event_call __used \
584 __attribute__((__aligned__(4))) \
585 __attribute__((section("_ftrace_events"))) event_##call = { \
587 .class = &event_class_##template, \
588 .event.funcs = &ftrace_event_type_funcs_##call, \
589 .print_fmt = print_fmt_##call, \
592 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
595 * Define the insertion callback to perf events
597 * The job is very similar to ftrace_raw_event_<call> except that we don't
598 * insert in the ring buffer but in a perf counter.
600 * static void ftrace_perf_<call>(proto)
602 * struct ftrace_data_offsets_<call> __maybe_unused __data_offsets;
603 * struct ftrace_event_call *event_call = &event_<call>;
604 * extern void perf_tp_event(int, u64, u64, void *, int);
605 * struct ftrace_raw_##call *entry;
606 * struct perf_trace_buf *trace_buf;
607 * u64 __addr = 0, __count = 1;
608 * unsigned long irq_flags;
609 * struct trace_entry *ent;
615 * pc = preempt_count();
617 * __data_size = ftrace_get_offsets_<call>(&__data_offsets, args);
619 * // Below we want to get the aligned size by taking into account
620 * // the u32 field that will later store the buffer size
621 * __entry_size = ALIGN(__data_size + sizeof(*entry) + sizeof(u32),
623 * __entry_size -= sizeof(u32);
625 * // Protect the non nmi buffer
626 * // This also protects the rcu read side
627 * local_irq_save(irq_flags);
628 * __cpu = smp_processor_id();
631 * trace_buf = rcu_dereference_sched(perf_trace_buf_nmi);
633 * trace_buf = rcu_dereference_sched(perf_trace_buf);
638 * trace_buf = per_cpu_ptr(trace_buf, __cpu);
640 * // Avoid recursion from perf that could mess up the buffer
641 * if (trace_buf->recursion++)
642 * goto end_recursion;
644 * raw_data = trace_buf->buf;
646 * // Make recursion update visible before entering perf_tp_event
647 * // so that we protect from perf recursions.
651 * //zero dead bytes from alignment to avoid stack leak to userspace:
652 * *(u64 *)(&raw_data[__entry_size - sizeof(u64)]) = 0ULL;
653 * entry = (struct ftrace_raw_<call> *)raw_data;
655 * tracing_generic_entry_update(ent, irq_flags, pc);
656 * ent->type = event_call->id;
658 * <tstruct> <- do some jobs with dynamic arrays
660 * <assign> <- affect our values
662 * perf_tp_event(event_call->id, __addr, __count, entry,
663 * __entry_size); <- submit them to perf counter
668 #ifdef CONFIG_PERF_EVENTS
671 #define __entry entry
673 #undef __get_dynamic_array
674 #define __get_dynamic_array(field) \
675 ((void *)__entry + (__entry->__data_loc_##field & 0xffff))
678 #define __get_str(field) (char *)__get_dynamic_array(field)
681 #define __perf_addr(a) __addr = (a)
684 #define __perf_count(c) __count = (c)
686 #undef DECLARE_EVENT_CLASS
687 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
688 static notrace void \
689 perf_trace_##call(void *__data, proto) \
691 struct ftrace_event_call *event_call = __data; \
692 struct ftrace_data_offsets_##call __maybe_unused __data_offsets;\
693 struct ftrace_raw_##call *entry; \
694 struct pt_regs __regs; \
695 u64 __addr = 0, __count = 1; \
696 struct hlist_head *head; \
701 perf_fetch_caller_regs(&__regs); \
703 __data_size = ftrace_get_offsets_##call(&__data_offsets, args); \
704 __entry_size = ALIGN(__data_size + sizeof(*entry) + sizeof(u32),\
706 __entry_size -= sizeof(u32); \
708 if (WARN_ONCE(__entry_size > PERF_MAX_TRACE_SIZE, \
709 "profile buffer not large enough")) \
712 entry = (struct ftrace_raw_##call *)perf_trace_buf_prepare( \
713 __entry_size, event_call->event.type, &__regs, &rctx); \
721 head = this_cpu_ptr(event_call->perf_events); \
722 perf_trace_buf_submit(entry, __entry_size, rctx, __addr, \
723 __count, &__regs, head); \
727 * This part is compiled out, it is only here as a build time check
728 * to make sure that if the tracepoint handling changes, the
729 * perf probe will fail to compile unless it too is updated.
732 #define DEFINE_EVENT(template, call, proto, args) \
733 static inline void perf_test_probe_##call(void) \
735 check_trace_callback_type_##call(perf_trace_##template); \
739 #undef DEFINE_EVENT_PRINT
740 #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
741 DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
743 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
744 #endif /* CONFIG_PERF_EVENTS */
746 #undef _TRACE_PROFILE_INIT