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))
79 #define __cpparg(arg...) arg
81 /* Callbacks are meaningless to ftrace. */
83 #define TRACE_EVENT_FN(name, proto, args, tstruct, \
84 assign, print, reg, unreg) \
85 TRACE_EVENT(name, __cpparg(proto), __cpparg(args), \
86 __cpparg(tstruct), __cpparg(assign), __cpparg(print)) \
88 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
92 * Stage 2 of the trace events.
94 * Include the following:
96 * struct ftrace_data_offsets_<call> {
102 * The __dynamic_array() macro will create each u32 <item>, this is
103 * to keep the offset of each array from the beginning of the event.
104 * The size of an array is also encoded, in the higher 16 bits of <item>.
108 #define __field(type, item)
111 #define __field_ext(type, item, filter_type)
114 #define __array(type, item, len)
116 #undef __dynamic_array
117 #define __dynamic_array(type, item, len) u32 item;
120 #define __string(item, src) __dynamic_array(char, item, -1)
122 #undef DECLARE_EVENT_CLASS
123 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
124 struct ftrace_data_offsets_##call { \
129 #define DEFINE_EVENT(template, name, proto, args)
131 #undef DEFINE_EVENT_PRINT
132 #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
133 DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
135 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
138 * Stage 3 of the trace events.
140 * Override the macros in <trace/trace_events.h> to include the following:
143 * ftrace_raw_output_<call>(struct trace_iterator *iter, int flags)
145 * struct trace_seq *s = &iter->seq;
146 * struct ftrace_raw_<call> *field; <-- defined in stage 1
147 * struct trace_entry *entry;
148 * struct trace_seq *p = &iter->tmp_seq;
153 * if (entry->type != event_<call>->event.type) {
155 * return TRACE_TYPE_UNHANDLED;
158 * field = (typeof(field))entry;
161 * ret = trace_seq_printf(s, "%s: ", <call>);
163 * ret = trace_seq_printf(s, <TP_printk> "\n");
165 * return TRACE_TYPE_PARTIAL_LINE;
167 * return TRACE_TYPE_HANDLED;
170 * This is the method used to print the raw event to the trace
171 * output format. Note, this is not needed if the data is read
176 #define __entry field
179 #define TP_printk(fmt, args...) fmt "\n", args
181 #undef __get_dynamic_array
182 #define __get_dynamic_array(field) \
183 ((void *)__entry + (__entry->__data_loc_##field & 0xffff))
186 #define __get_str(field) (char *)__get_dynamic_array(field)
189 #define __print_flags(flag, delim, flag_array...) \
191 static const struct trace_print_flags __flags[] = \
192 { flag_array, { -1, NULL }}; \
193 ftrace_print_flags_seq(p, delim, flag, __flags); \
196 #undef __print_symbolic
197 #define __print_symbolic(value, symbol_array...) \
199 static const struct trace_print_flags symbols[] = \
200 { symbol_array, { -1, NULL }}; \
201 ftrace_print_symbols_seq(p, value, symbols); \
205 #define __print_hex(buf, buf_len) ftrace_print_hex_seq(p, buf, buf_len)
207 #undef DECLARE_EVENT_CLASS
208 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
209 static notrace enum print_line_t \
210 ftrace_raw_output_##call(struct trace_iterator *iter, int flags, \
211 struct trace_event *trace_event) \
213 struct ftrace_event_call *event; \
214 struct trace_seq *s = &iter->seq; \
215 struct ftrace_raw_##call *field; \
216 struct trace_entry *entry; \
217 struct trace_seq *p = &iter->tmp_seq; \
220 event = container_of(trace_event, struct ftrace_event_call, \
225 if (entry->type != event->event.type) { \
227 return TRACE_TYPE_UNHANDLED; \
230 field = (typeof(field))entry; \
233 ret = trace_seq_printf(s, "%s: ", event->name); \
235 ret = trace_seq_printf(s, print); \
237 return TRACE_TYPE_PARTIAL_LINE; \
239 return TRACE_TYPE_HANDLED; \
241 static struct trace_event_functions ftrace_event_type_funcs_##call = { \
242 .trace = ftrace_raw_output_##call, \
245 #undef DEFINE_EVENT_PRINT
246 #define DEFINE_EVENT_PRINT(template, call, proto, args, print) \
247 static notrace enum print_line_t \
248 ftrace_raw_output_##call(struct trace_iterator *iter, int flags, \
249 struct trace_event *event) \
251 struct trace_seq *s = &iter->seq; \
252 struct ftrace_raw_##template *field; \
253 struct trace_entry *entry; \
254 struct trace_seq *p = &iter->tmp_seq; \
259 if (entry->type != event_##call.event.type) { \
261 return TRACE_TYPE_UNHANDLED; \
264 field = (typeof(field))entry; \
267 ret = trace_seq_printf(s, "%s: ", #call); \
269 ret = trace_seq_printf(s, print); \
271 return TRACE_TYPE_PARTIAL_LINE; \
273 return TRACE_TYPE_HANDLED; \
275 static struct trace_event_functions ftrace_event_type_funcs_##call = { \
276 .trace = ftrace_raw_output_##call, \
279 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
282 #define __field_ext(type, item, filter_type) \
283 ret = trace_define_field(event_call, #type, #item, \
284 offsetof(typeof(field), item), \
285 sizeof(field.item), \
286 is_signed_type(type), filter_type); \
291 #define __field(type, item) __field_ext(type, item, FILTER_OTHER)
294 #define __array(type, item, len) \
295 BUILD_BUG_ON(len > MAX_FILTER_STR_VAL); \
296 ret = trace_define_field(event_call, #type "[" #len "]", #item, \
297 offsetof(typeof(field), item), \
298 sizeof(field.item), \
299 is_signed_type(type), FILTER_OTHER); \
303 #undef __dynamic_array
304 #define __dynamic_array(type, item, len) \
305 ret = trace_define_field(event_call, "__data_loc " #type "[]", #item, \
306 offsetof(typeof(field), __data_loc_##item), \
307 sizeof(field.__data_loc_##item), \
308 is_signed_type(type), FILTER_OTHER);
311 #define __string(item, src) __dynamic_array(char, item, -1)
313 #undef DECLARE_EVENT_CLASS
314 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, func, print) \
316 ftrace_define_fields_##call(struct ftrace_event_call *event_call) \
318 struct ftrace_raw_##call field; \
327 #define DEFINE_EVENT(template, name, proto, args)
329 #undef DEFINE_EVENT_PRINT
330 #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
331 DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
333 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
336 * remember the offset of each array from the beginning of the event.
340 #define __entry entry
343 #define __field(type, item)
346 #define __field_ext(type, item, filter_type)
349 #define __array(type, item, len)
351 #undef __dynamic_array
352 #define __dynamic_array(type, item, len) \
353 __data_offsets->item = __data_size + \
354 offsetof(typeof(*entry), __data); \
355 __data_offsets->item |= (len * sizeof(type)) << 16; \
356 __data_size += (len) * sizeof(type);
359 #define __string(item, src) __dynamic_array(char, item, strlen(src) + 1)
361 #undef DECLARE_EVENT_CLASS
362 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
363 static inline notrace int ftrace_get_offsets_##call( \
364 struct ftrace_data_offsets_##call *__data_offsets, proto) \
366 int __data_size = 0; \
367 struct ftrace_raw_##call __maybe_unused *entry; \
371 return __data_size; \
375 #define DEFINE_EVENT(template, name, proto, args)
377 #undef DEFINE_EVENT_PRINT
378 #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
379 DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
381 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
384 * Stage 4 of the trace events.
386 * Override the macros in <trace/trace_events.h> to include the following:
388 * For those macros defined with TRACE_EVENT:
390 * static struct ftrace_event_call event_<call>;
392 * static void ftrace_raw_event_<call>(void *__data, proto)
394 * struct ftrace_event_call *event_call = __data;
395 * struct ftrace_data_offsets_<call> __maybe_unused __data_offsets;
396 * struct ring_buffer_event *event;
397 * struct ftrace_raw_<call> *entry; <-- defined in stage 1
398 * struct ring_buffer *buffer;
399 * unsigned long irq_flags;
403 * local_save_flags(irq_flags);
404 * pc = preempt_count();
406 * __data_size = ftrace_get_offsets_<call>(&__data_offsets, args);
408 * event = trace_current_buffer_lock_reserve(&buffer,
409 * event_<call>->event.type,
410 * sizeof(*entry) + __data_size,
414 * entry = ring_buffer_event_data(event);
416 * { <assign>; } <-- Here we assign the entries by the __field and
419 * if (!filter_current_check_discard(buffer, event_call, entry, event))
420 * trace_current_buffer_unlock_commit(buffer,
421 * event, irq_flags, pc);
424 * static struct trace_event ftrace_event_type_<call> = {
425 * .trace = ftrace_raw_output_<call>, <-- stage 2
428 * static const char print_fmt_<call>[] = <TP_printk>;
430 * static struct ftrace_event_class __used event_class_<template> = {
431 * .system = "<system>",
432 * .define_fields = ftrace_define_fields_<call>,
433 * .fields = LIST_HEAD_INIT(event_class_##call.fields),
434 * .raw_init = trace_event_raw_init,
435 * .probe = ftrace_raw_event_##call,
436 * .reg = ftrace_event_reg,
439 * static struct ftrace_event_call __used
440 * __attribute__((__aligned__(4)))
441 * __attribute__((section("_ftrace_events"))) event_<call> = {
443 * .class = event_class_<template>,
444 * .event = &ftrace_event_type_<call>,
445 * .print_fmt = print_fmt_<call>,
450 #ifdef CONFIG_PERF_EVENTS
452 #define _TRACE_PERF_PROTO(call, proto) \
453 static notrace void \
454 perf_trace_##call(void *__data, proto);
456 #define _TRACE_PERF_INIT(call) \
457 .perf_probe = perf_trace_##call,
460 #define _TRACE_PERF_PROTO(call, proto)
461 #define _TRACE_PERF_INIT(call)
462 #endif /* CONFIG_PERF_EVENTS */
465 #define __entry entry
468 #define __field(type, item)
471 #define __array(type, item, len)
473 #undef __dynamic_array
474 #define __dynamic_array(type, item, len) \
475 __entry->__data_loc_##item = __data_offsets.item;
478 #define __string(item, src) __dynamic_array(char, item, -1) \
481 #define __assign_str(dst, src) \
482 strcpy(__get_str(dst), src);
484 #undef TP_fast_assign
485 #define TP_fast_assign(args...) args
487 #undef TP_perf_assign
488 #define TP_perf_assign(args...)
490 #undef DECLARE_EVENT_CLASS
491 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
493 static notrace void \
494 ftrace_raw_event_##call(void *__data, proto) \
496 struct ftrace_event_call *event_call = __data; \
497 struct ftrace_data_offsets_##call __maybe_unused __data_offsets;\
498 struct ring_buffer_event *event; \
499 struct ftrace_raw_##call *entry; \
500 struct ring_buffer *buffer; \
501 unsigned long irq_flags; \
505 local_save_flags(irq_flags); \
506 pc = preempt_count(); \
508 __data_size = ftrace_get_offsets_##call(&__data_offsets, args); \
510 event = trace_current_buffer_lock_reserve(&buffer, \
511 event_call->event.type, \
512 sizeof(*entry) + __data_size, \
516 entry = ring_buffer_event_data(event); \
522 if (!filter_current_check_discard(buffer, event_call, entry, event)) \
523 trace_nowake_buffer_unlock_commit(buffer, \
524 event, irq_flags, pc); \
527 * The ftrace_test_probe is compiled out, it is only here as a build time check
528 * to make sure that if the tracepoint handling changes, the ftrace probe will
529 * fail to compile unless it too is updated.
533 #define DEFINE_EVENT(template, call, proto, args) \
534 static inline void ftrace_test_probe_##call(void) \
536 check_trace_callback_type_##call(ftrace_raw_event_##template); \
539 #undef DEFINE_EVENT_PRINT
540 #define DEFINE_EVENT_PRINT(template, name, proto, args, print)
542 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
548 #undef __print_symbolic
549 #undef __get_dynamic_array
553 #define TP_printk(fmt, args...) "\"" fmt "\", " __stringify(args)
555 #undef DECLARE_EVENT_CLASS
556 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
557 _TRACE_PERF_PROTO(call, PARAMS(proto)); \
558 static const char print_fmt_##call[] = print; \
559 static struct ftrace_event_class __used event_class_##call = { \
560 .system = __stringify(TRACE_SYSTEM), \
561 .define_fields = ftrace_define_fields_##call, \
562 .fields = LIST_HEAD_INIT(event_class_##call.fields),\
563 .raw_init = trace_event_raw_init, \
564 .probe = ftrace_raw_event_##call, \
565 .reg = ftrace_event_reg, \
566 _TRACE_PERF_INIT(call) \
570 #define DEFINE_EVENT(template, call, proto, args) \
572 static struct ftrace_event_call __used \
573 __attribute__((__aligned__(4))) \
574 __attribute__((section("_ftrace_events"))) event_##call = { \
576 .class = &event_class_##template, \
577 .event.funcs = &ftrace_event_type_funcs_##template, \
578 .print_fmt = print_fmt_##template, \
581 #undef DEFINE_EVENT_PRINT
582 #define DEFINE_EVENT_PRINT(template, call, proto, args, print) \
584 static const char print_fmt_##call[] = print; \
586 static struct ftrace_event_call __used \
587 __attribute__((__aligned__(4))) \
588 __attribute__((section("_ftrace_events"))) event_##call = { \
590 .class = &event_class_##template, \
591 .event.funcs = &ftrace_event_type_funcs_##call, \
592 .print_fmt = print_fmt_##call, \
595 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
598 * Define the insertion callback to perf events
600 * The job is very similar to ftrace_raw_event_<call> except that we don't
601 * insert in the ring buffer but in a perf counter.
603 * static void ftrace_perf_<call>(proto)
605 * struct ftrace_data_offsets_<call> __maybe_unused __data_offsets;
606 * struct ftrace_event_call *event_call = &event_<call>;
607 * extern void perf_tp_event(int, u64, u64, void *, int);
608 * struct ftrace_raw_##call *entry;
609 * struct perf_trace_buf *trace_buf;
610 * u64 __addr = 0, __count = 1;
611 * unsigned long irq_flags;
612 * struct trace_entry *ent;
618 * pc = preempt_count();
620 * __data_size = ftrace_get_offsets_<call>(&__data_offsets, args);
622 * // Below we want to get the aligned size by taking into account
623 * // the u32 field that will later store the buffer size
624 * __entry_size = ALIGN(__data_size + sizeof(*entry) + sizeof(u32),
626 * __entry_size -= sizeof(u32);
628 * // Protect the non nmi buffer
629 * // This also protects the rcu read side
630 * local_irq_save(irq_flags);
631 * __cpu = smp_processor_id();
634 * trace_buf = rcu_dereference_sched(perf_trace_buf_nmi);
636 * trace_buf = rcu_dereference_sched(perf_trace_buf);
641 * trace_buf = per_cpu_ptr(trace_buf, __cpu);
643 * // Avoid recursion from perf that could mess up the buffer
644 * if (trace_buf->recursion++)
645 * goto end_recursion;
647 * raw_data = trace_buf->buf;
649 * // Make recursion update visible before entering perf_tp_event
650 * // so that we protect from perf recursions.
654 * //zero dead bytes from alignment to avoid stack leak to userspace:
655 * *(u64 *)(&raw_data[__entry_size - sizeof(u64)]) = 0ULL;
656 * entry = (struct ftrace_raw_<call> *)raw_data;
658 * tracing_generic_entry_update(ent, irq_flags, pc);
659 * ent->type = event_call->id;
661 * <tstruct> <- do some jobs with dynamic arrays
663 * <assign> <- affect our values
665 * perf_tp_event(event_call->id, __addr, __count, entry,
666 * __entry_size); <- submit them to perf counter
671 #ifdef CONFIG_PERF_EVENTS
674 #define __entry entry
676 #undef __get_dynamic_array
677 #define __get_dynamic_array(field) \
678 ((void *)__entry + (__entry->__data_loc_##field & 0xffff))
681 #define __get_str(field) (char *)__get_dynamic_array(field)
684 #define __perf_addr(a) __addr = (a)
687 #define __perf_count(c) __count = (c)
689 #undef DECLARE_EVENT_CLASS
690 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
691 static notrace void \
692 perf_trace_##call(void *__data, proto) \
694 struct ftrace_event_call *event_call = __data; \
695 struct ftrace_data_offsets_##call __maybe_unused __data_offsets;\
696 struct ftrace_raw_##call *entry; \
697 struct pt_regs __regs; \
698 u64 __addr = 0, __count = 1; \
699 struct hlist_head *head; \
704 perf_fetch_caller_regs(&__regs); \
706 __data_size = ftrace_get_offsets_##call(&__data_offsets, args); \
707 __entry_size = ALIGN(__data_size + sizeof(*entry) + sizeof(u32),\
709 __entry_size -= sizeof(u32); \
711 if (WARN_ONCE(__entry_size > PERF_MAX_TRACE_SIZE, \
712 "profile buffer not large enough")) \
715 entry = (struct ftrace_raw_##call *)perf_trace_buf_prepare( \
716 __entry_size, event_call->event.type, &__regs, &rctx); \
724 head = this_cpu_ptr(event_call->perf_events); \
725 perf_trace_buf_submit(entry, __entry_size, rctx, __addr, \
726 __count, &__regs, head); \
730 * This part is compiled out, it is only here as a build time check
731 * to make sure that if the tracepoint handling changes, the
732 * perf probe will fail to compile unless it too is updated.
735 #define DEFINE_EVENT(template, call, proto, args) \
736 static inline void perf_test_probe_##call(void) \
738 check_trace_callback_type_##call(perf_trace_##template); \
742 #undef DEFINE_EVENT_PRINT
743 #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
744 DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
746 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
747 #endif /* CONFIG_PERF_EVENTS */
749 #undef _TRACE_PROFILE_INIT