perf_events: Undo some recursion damage
[linux-2.6/kvm.git] / include / trace / ftrace.h
blobc3417c13e3edea3607adbf8d22545440ea861381
1 /*
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;
8 * <type> <item>;
9 * <type2> <item2>[<len>];
10 * [...]
11 * };
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
16 * in the structure.
19 #include <linux/ftrace_event.h>
21 #undef __field
22 #define __field(type, item) type item;
24 #undef __field_ext
25 #define __field_ext(type, item, filter_type) type item;
27 #undef __array
28 #define __array(type, item, len) type item[len];
30 #undef __dynamic_array
31 #define __dynamic_array(type, item, len) u32 __data_loc_##item;
33 #undef __string
34 #define __string(item, src) __dynamic_array(char, item, -1)
36 #undef TP_STRUCT__entry
37 #define TP_STRUCT__entry(args...) args
39 #undef TRACE_EVENT
40 #define TRACE_EVENT(name, proto, args, tstruct, assign, print) \
41 struct ftrace_raw_##name { \
42 struct trace_entry ent; \
43 tstruct \
44 char __data[0]; \
45 }; \
46 static struct ftrace_event_call event_##name
48 #undef __cpparg
49 #define __cpparg(arg...) arg
51 /* Callbacks are meaningless to ftrace. */
52 #undef TRACE_EVENT_FN
53 #define TRACE_EVENT_FN(name, proto, args, tstruct, \
54 assign, print, reg, unreg) \
55 TRACE_EVENT(name, __cpparg(proto), __cpparg(args), \
56 __cpparg(tstruct), __cpparg(assign), __cpparg(print)) \
58 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
62 * Stage 2 of the trace events.
64 * Include the following:
66 * struct ftrace_data_offsets_<call> {
67 * u32 <item1>;
68 * u32 <item2>;
69 * [...]
70 * };
72 * The __dynamic_array() macro will create each u32 <item>, this is
73 * to keep the offset of each array from the beginning of the event.
74 * The size of an array is also encoded, in the higher 16 bits of <item>.
77 #undef __field
78 #define __field(type, item)
80 #undef __field_ext
81 #define __field_ext(type, item, filter_type)
83 #undef __array
84 #define __array(type, item, len)
86 #undef __dynamic_array
87 #define __dynamic_array(type, item, len) u32 item;
89 #undef __string
90 #define __string(item, src) __dynamic_array(char, item, -1)
92 #undef TRACE_EVENT
93 #define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
94 struct ftrace_data_offsets_##call { \
95 tstruct; \
98 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
101 * Setup the showing format of trace point.
103 * int
104 * ftrace_format_##call(struct trace_seq *s)
106 * struct ftrace_raw_##call field;
107 * int ret;
109 * ret = trace_seq_printf(s, #type " " #item ";"
110 * " offset:%u; size:%u;\n",
111 * offsetof(struct ftrace_raw_##call, item),
112 * sizeof(field.type));
117 #undef TP_STRUCT__entry
118 #define TP_STRUCT__entry(args...) args
120 #undef __field
121 #define __field(type, item) \
122 ret = trace_seq_printf(s, "\tfield:" #type " " #item ";\t" \
123 "offset:%u;\tsize:%u;\tsigned:%u;\n", \
124 (unsigned int)offsetof(typeof(field), item), \
125 (unsigned int)sizeof(field.item), \
126 (unsigned int)is_signed_type(type)); \
127 if (!ret) \
128 return 0;
130 #undef __field_ext
131 #define __field_ext(type, item, filter_type) __field(type, item)
133 #undef __array
134 #define __array(type, item, len) \
135 ret = trace_seq_printf(s, "\tfield:" #type " " #item "[" #len "];\t" \
136 "offset:%u;\tsize:%u;\tsigned:%u;\n", \
137 (unsigned int)offsetof(typeof(field), item), \
138 (unsigned int)sizeof(field.item), \
139 (unsigned int)is_signed_type(type)); \
140 if (!ret) \
141 return 0;
143 #undef __dynamic_array
144 #define __dynamic_array(type, item, len) \
145 ret = trace_seq_printf(s, "\tfield:__data_loc " #type "[] " #item ";\t"\
146 "offset:%u;\tsize:%u;\tsigned:%u;\n", \
147 (unsigned int)offsetof(typeof(field), \
148 __data_loc_##item), \
149 (unsigned int)sizeof(field.__data_loc_##item), \
150 (unsigned int)is_signed_type(type)); \
151 if (!ret) \
152 return 0;
154 #undef __string
155 #define __string(item, src) __dynamic_array(char, item, -1)
157 #undef __entry
158 #define __entry REC
160 #undef __print_symbolic
161 #undef __get_dynamic_array
162 #undef __get_str
164 #undef TP_printk
165 #define TP_printk(fmt, args...) "%s, %s\n", #fmt, __stringify(args)
167 #undef TP_fast_assign
168 #define TP_fast_assign(args...) args
170 #undef TP_perf_assign
171 #define TP_perf_assign(args...)
173 #undef TRACE_EVENT
174 #define TRACE_EVENT(call, proto, args, tstruct, func, print) \
175 static int \
176 ftrace_format_##call(struct ftrace_event_call *unused, \
177 struct trace_seq *s) \
179 struct ftrace_raw_##call field __attribute__((unused)); \
180 int ret = 0; \
182 tstruct; \
184 trace_seq_printf(s, "\nprint fmt: " print); \
186 return ret; \
189 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
192 * Stage 3 of the trace events.
194 * Override the macros in <trace/trace_events.h> to include the following:
196 * enum print_line_t
197 * ftrace_raw_output_<call>(struct trace_iterator *iter, int flags)
199 * struct trace_seq *s = &iter->seq;
200 * struct ftrace_raw_<call> *field; <-- defined in stage 1
201 * struct trace_entry *entry;
202 * struct trace_seq *p;
203 * int ret;
205 * entry = iter->ent;
207 * if (entry->type != event_<call>.id) {
208 * WARN_ON_ONCE(1);
209 * return TRACE_TYPE_UNHANDLED;
212 * field = (typeof(field))entry;
214 * p = get_cpu_var(ftrace_event_seq);
215 * trace_seq_init(p);
216 * ret = trace_seq_printf(s, <TP_printk> "\n");
217 * put_cpu();
218 * if (!ret)
219 * return TRACE_TYPE_PARTIAL_LINE;
221 * return TRACE_TYPE_HANDLED;
224 * This is the method used to print the raw event to the trace
225 * output format. Note, this is not needed if the data is read
226 * in binary.
229 #undef __entry
230 #define __entry field
232 #undef TP_printk
233 #define TP_printk(fmt, args...) fmt "\n", args
235 #undef __get_dynamic_array
236 #define __get_dynamic_array(field) \
237 ((void *)__entry + (__entry->__data_loc_##field & 0xffff))
239 #undef __get_str
240 #define __get_str(field) (char *)__get_dynamic_array(field)
242 #undef __print_flags
243 #define __print_flags(flag, delim, flag_array...) \
244 ({ \
245 static const struct trace_print_flags __flags[] = \
246 { flag_array, { -1, NULL }}; \
247 ftrace_print_flags_seq(p, delim, flag, __flags); \
250 #undef __print_symbolic
251 #define __print_symbolic(value, symbol_array...) \
252 ({ \
253 static const struct trace_print_flags symbols[] = \
254 { symbol_array, { -1, NULL }}; \
255 ftrace_print_symbols_seq(p, value, symbols); \
258 #undef TRACE_EVENT
259 #define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
260 static enum print_line_t \
261 ftrace_raw_output_##call(struct trace_iterator *iter, int flags) \
263 struct trace_seq *s = &iter->seq; \
264 struct ftrace_raw_##call *field; \
265 struct trace_entry *entry; \
266 struct trace_seq *p; \
267 int ret; \
269 entry = iter->ent; \
271 if (entry->type != event_##call.id) { \
272 WARN_ON_ONCE(1); \
273 return TRACE_TYPE_UNHANDLED; \
276 field = (typeof(field))entry; \
278 p = &get_cpu_var(ftrace_event_seq); \
279 trace_seq_init(p); \
280 ret = trace_seq_printf(s, #call ": " print); \
281 put_cpu(); \
282 if (!ret) \
283 return TRACE_TYPE_PARTIAL_LINE; \
285 return TRACE_TYPE_HANDLED; \
288 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
290 #undef __field_ext
291 #define __field_ext(type, item, filter_type) \
292 ret = trace_define_field(event_call, #type, #item, \
293 offsetof(typeof(field), item), \
294 sizeof(field.item), \
295 is_signed_type(type), filter_type); \
296 if (ret) \
297 return ret;
299 #undef __field
300 #define __field(type, item) __field_ext(type, item, FILTER_OTHER)
302 #undef __array
303 #define __array(type, item, len) \
304 BUILD_BUG_ON(len > MAX_FILTER_STR_VAL); \
305 ret = trace_define_field(event_call, #type "[" #len "]", #item, \
306 offsetof(typeof(field), item), \
307 sizeof(field.item), 0, FILTER_OTHER); \
308 if (ret) \
309 return ret;
311 #undef __dynamic_array
312 #define __dynamic_array(type, item, len) \
313 ret = trace_define_field(event_call, "__data_loc " #type "[]", #item, \
314 offsetof(typeof(field), __data_loc_##item), \
315 sizeof(field.__data_loc_##item), 0, \
316 FILTER_OTHER);
318 #undef __string
319 #define __string(item, src) __dynamic_array(char, item, -1)
321 #undef TRACE_EVENT
322 #define TRACE_EVENT(call, proto, args, tstruct, func, print) \
323 static int \
324 ftrace_define_fields_##call(struct ftrace_event_call *event_call) \
326 struct ftrace_raw_##call field; \
327 int ret; \
329 ret = trace_define_common_fields(event_call); \
330 if (ret) \
331 return ret; \
333 tstruct; \
335 return ret; \
338 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
341 * remember the offset of each array from the beginning of the event.
344 #undef __entry
345 #define __entry entry
347 #undef __field
348 #define __field(type, item)
350 #undef __field_ext
351 #define __field_ext(type, item, filter_type)
353 #undef __array
354 #define __array(type, item, len)
356 #undef __dynamic_array
357 #define __dynamic_array(type, item, len) \
358 __data_offsets->item = __data_size + \
359 offsetof(typeof(*entry), __data); \
360 __data_offsets->item |= (len * sizeof(type)) << 16; \
361 __data_size += (len) * sizeof(type);
363 #undef __string
364 #define __string(item, src) __dynamic_array(char, item, strlen(src) + 1) \
366 #undef TRACE_EVENT
367 #define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
368 static inline int ftrace_get_offsets_##call( \
369 struct ftrace_data_offsets_##call *__data_offsets, proto) \
371 int __data_size = 0; \
372 struct ftrace_raw_##call __maybe_unused *entry; \
374 tstruct; \
376 return __data_size; \
379 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
381 #ifdef CONFIG_EVENT_PROFILE
384 * Generate the functions needed for tracepoint perf_event support.
386 * NOTE: The insertion profile callback (ftrace_profile_<call>) is defined later
388 * static int ftrace_profile_enable_<call>(void)
390 * return register_trace_<call>(ftrace_profile_<call>);
393 * static void ftrace_profile_disable_<call>(void)
395 * unregister_trace_<call>(ftrace_profile_<call>);
400 #undef TRACE_EVENT
401 #define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
403 static void ftrace_profile_##call(proto); \
405 static int ftrace_profile_enable_##call(struct ftrace_event_call *unused)\
407 return register_trace_##call(ftrace_profile_##call); \
410 static void ftrace_profile_disable_##call(struct ftrace_event_call *unused)\
412 unregister_trace_##call(ftrace_profile_##call); \
415 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
417 #endif
420 * Stage 4 of the trace events.
422 * Override the macros in <trace/trace_events.h> to include the following:
424 * static void ftrace_event_<call>(proto)
426 * event_trace_printk(_RET_IP_, "<call>: " <fmt>);
429 * static int ftrace_reg_event_<call>(struct ftrace_event_call *unused)
431 * int ret;
433 * ret = register_trace_<call>(ftrace_event_<call>);
434 * if (!ret)
435 * pr_info("event trace: Could not activate trace point "
436 * "probe to <call>");
437 * return ret;
440 * static void ftrace_unreg_event_<call>(struct ftrace_event_call *unused)
442 * unregister_trace_<call>(ftrace_event_<call>);
446 * For those macros defined with TRACE_EVENT:
448 * static struct ftrace_event_call event_<call>;
450 * static void ftrace_raw_event_<call>(proto)
452 * struct ring_buffer_event *event;
453 * struct ftrace_raw_<call> *entry; <-- defined in stage 1
454 * struct ring_buffer *buffer;
455 * unsigned long irq_flags;
456 * int pc;
458 * local_save_flags(irq_flags);
459 * pc = preempt_count();
461 * event = trace_current_buffer_lock_reserve(&buffer,
462 * event_<call>.id,
463 * sizeof(struct ftrace_raw_<call>),
464 * irq_flags, pc);
465 * if (!event)
466 * return;
467 * entry = ring_buffer_event_data(event);
469 * <assign>; <-- Here we assign the entries by the __field and
470 * __array macros.
472 * trace_current_buffer_unlock_commit(buffer, event, irq_flags, pc);
475 * static int ftrace_raw_reg_event_<call>(struct ftrace_event_call *unused)
477 * int ret;
479 * ret = register_trace_<call>(ftrace_raw_event_<call>);
480 * if (!ret)
481 * pr_info("event trace: Could not activate trace point "
482 * "probe to <call>");
483 * return ret;
486 * static void ftrace_unreg_event_<call>(struct ftrace_event_call *unused)
488 * unregister_trace_<call>(ftrace_raw_event_<call>);
491 * static struct trace_event ftrace_event_type_<call> = {
492 * .trace = ftrace_raw_output_<call>, <-- stage 2
493 * };
495 * static int ftrace_raw_init_event_<call>(struct ftrace_event_call *unused)
497 * int id;
499 * id = register_ftrace_event(&ftrace_event_type_<call>);
500 * if (!id)
501 * return -ENODEV;
502 * event_<call>.id = id;
503 * return 0;
506 * static struct ftrace_event_call __used
507 * __attribute__((__aligned__(4)))
508 * __attribute__((section("_ftrace_events"))) event_<call> = {
509 * .name = "<call>",
510 * .system = "<system>",
511 * .raw_init = ftrace_raw_init_event_<call>,
512 * .regfunc = ftrace_reg_event_<call>,
513 * .unregfunc = ftrace_unreg_event_<call>,
514 * .show_format = ftrace_format_<call>,
519 #undef TP_FMT
520 #define TP_FMT(fmt, args...) fmt "\n", ##args
522 #ifdef CONFIG_EVENT_PROFILE
524 #define _TRACE_PROFILE_INIT(call) \
525 .profile_count = ATOMIC_INIT(-1), \
526 .profile_enable = ftrace_profile_enable_##call, \
527 .profile_disable = ftrace_profile_disable_##call,
529 #else
530 #define _TRACE_PROFILE_INIT(call)
531 #endif
533 #undef __entry
534 #define __entry entry
536 #undef __field
537 #define __field(type, item)
539 #undef __array
540 #define __array(type, item, len)
542 #undef __dynamic_array
543 #define __dynamic_array(type, item, len) \
544 __entry->__data_loc_##item = __data_offsets.item;
546 #undef __string
547 #define __string(item, src) __dynamic_array(char, item, -1) \
549 #undef __assign_str
550 #define __assign_str(dst, src) \
551 strcpy(__get_str(dst), src);
553 #undef TRACE_EVENT
554 #define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
556 static struct ftrace_event_call event_##call; \
558 static void ftrace_raw_event_##call(proto) \
560 struct ftrace_data_offsets_##call __maybe_unused __data_offsets;\
561 struct ftrace_event_call *event_call = &event_##call; \
562 struct ring_buffer_event *event; \
563 struct ftrace_raw_##call *entry; \
564 struct ring_buffer *buffer; \
565 unsigned long irq_flags; \
566 int __data_size; \
567 int pc; \
569 local_save_flags(irq_flags); \
570 pc = preempt_count(); \
572 __data_size = ftrace_get_offsets_##call(&__data_offsets, args); \
574 event = trace_current_buffer_lock_reserve(&buffer, \
575 event_##call.id, \
576 sizeof(*entry) + __data_size, \
577 irq_flags, pc); \
578 if (!event) \
579 return; \
580 entry = ring_buffer_event_data(event); \
583 tstruct \
585 { assign; } \
587 if (!filter_current_check_discard(buffer, event_call, entry, event)) \
588 trace_nowake_buffer_unlock_commit(buffer, \
589 event, irq_flags, pc); \
592 static int ftrace_raw_reg_event_##call(struct ftrace_event_call *unused)\
594 int ret; \
596 ret = register_trace_##call(ftrace_raw_event_##call); \
597 if (ret) \
598 pr_info("event trace: Could not activate trace point " \
599 "probe to " #call "\n"); \
600 return ret; \
603 static void ftrace_raw_unreg_event_##call(struct ftrace_event_call *unused)\
605 unregister_trace_##call(ftrace_raw_event_##call); \
608 static struct trace_event ftrace_event_type_##call = { \
609 .trace = ftrace_raw_output_##call, \
610 }; \
612 static int ftrace_raw_init_event_##call(struct ftrace_event_call *unused)\
614 int id; \
616 id = register_ftrace_event(&ftrace_event_type_##call); \
617 if (!id) \
618 return -ENODEV; \
619 event_##call.id = id; \
620 INIT_LIST_HEAD(&event_##call.fields); \
621 return 0; \
624 static struct ftrace_event_call __used \
625 __attribute__((__aligned__(4))) \
626 __attribute__((section("_ftrace_events"))) event_##call = { \
627 .name = #call, \
628 .system = __stringify(TRACE_SYSTEM), \
629 .event = &ftrace_event_type_##call, \
630 .raw_init = ftrace_raw_init_event_##call, \
631 .regfunc = ftrace_raw_reg_event_##call, \
632 .unregfunc = ftrace_raw_unreg_event_##call, \
633 .show_format = ftrace_format_##call, \
634 .define_fields = ftrace_define_fields_##call, \
635 _TRACE_PROFILE_INIT(call) \
638 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
641 * Define the insertion callback to profile events
643 * The job is very similar to ftrace_raw_event_<call> except that we don't
644 * insert in the ring buffer but in a perf counter.
646 * static void ftrace_profile_<call>(proto)
648 * struct ftrace_data_offsets_<call> __maybe_unused __data_offsets;
649 * struct ftrace_event_call *event_call = &event_<call>;
650 * extern void perf_tp_event(int, u64, u64, void *, int);
651 * struct ftrace_raw_##call *entry;
652 * struct perf_trace_buf *trace_buf;
653 * u64 __addr = 0, __count = 1;
654 * unsigned long irq_flags;
655 * struct trace_entry *ent;
656 * int __entry_size;
657 * int __data_size;
658 * int __cpu
659 * int pc;
661 * pc = preempt_count();
663 * __data_size = ftrace_get_offsets_<call>(&__data_offsets, args);
665 * // Below we want to get the aligned size by taking into account
666 * // the u32 field that will later store the buffer size
667 * __entry_size = ALIGN(__data_size + sizeof(*entry) + sizeof(u32),
668 * sizeof(u64));
669 * __entry_size -= sizeof(u32);
671 * // Protect the non nmi buffer
672 * // This also protects the rcu read side
673 * local_irq_save(irq_flags);
674 * __cpu = smp_processor_id();
676 * if (in_nmi())
677 * trace_buf = rcu_dereference(perf_trace_buf_nmi);
678 * else
679 * trace_buf = rcu_dereference(perf_trace_buf);
681 * if (!trace_buf)
682 * goto end;
684 * trace_buf = per_cpu_ptr(trace_buf, __cpu);
686 * // Avoid recursion from perf that could mess up the buffer
687 * if (trace_buf->recursion++)
688 * goto end_recursion;
690 * raw_data = trace_buf->buf;
692 * // Make recursion update visible before entering perf_tp_event
693 * // so that we protect from perf recursions.
695 * barrier();
697 * //zero dead bytes from alignment to avoid stack leak to userspace:
698 * *(u64 *)(&raw_data[__entry_size - sizeof(u64)]) = 0ULL;
699 * entry = (struct ftrace_raw_<call> *)raw_data;
700 * ent = &entry->ent;
701 * tracing_generic_entry_update(ent, irq_flags, pc);
702 * ent->type = event_call->id;
704 * <tstruct> <- do some jobs with dynamic arrays
706 * <assign> <- affect our values
708 * perf_tp_event(event_call->id, __addr, __count, entry,
709 * __entry_size); <- submit them to perf counter
714 #ifdef CONFIG_EVENT_PROFILE
716 #undef __perf_addr
717 #define __perf_addr(a) __addr = (a)
719 #undef __perf_count
720 #define __perf_count(c) __count = (c)
722 #undef TRACE_EVENT
723 #define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
724 static void ftrace_profile_##call(proto) \
726 struct ftrace_data_offsets_##call __maybe_unused __data_offsets;\
727 extern int perf_swevent_get_recursion_context(void); \
728 extern void perf_swevent_put_recursion_context(int rctx); \
729 struct ftrace_event_call *event_call = &event_##call; \
730 extern void perf_tp_event(int, u64, u64, void *, int); \
731 struct ftrace_raw_##call *entry; \
732 u64 __addr = 0, __count = 1; \
733 unsigned long irq_flags; \
734 struct trace_entry *ent; \
735 int __entry_size; \
736 int __data_size; \
737 char *trace_buf; \
738 char *raw_data; \
739 int __cpu; \
740 int rctx; \
741 int pc; \
743 pc = preempt_count(); \
745 __data_size = ftrace_get_offsets_##call(&__data_offsets, args); \
746 __entry_size = ALIGN(__data_size + sizeof(*entry) + sizeof(u32),\
747 sizeof(u64)); \
748 __entry_size -= sizeof(u32); \
750 if (WARN_ONCE(__entry_size > FTRACE_MAX_PROFILE_SIZE, \
751 "profile buffer not large enough")) \
752 return; \
754 local_irq_save(irq_flags); \
756 rctx = perf_swevent_get_recursion_context(); \
757 if (rctx < 0) \
758 goto end_recursion; \
760 __cpu = smp_processor_id(); \
762 if (in_nmi()) \
763 trace_buf = rcu_dereference(perf_trace_buf_nmi); \
764 else \
765 trace_buf = rcu_dereference(perf_trace_buf); \
767 if (!trace_buf) \
768 goto end; \
770 raw_data = per_cpu_ptr(trace_buf, __cpu); \
772 *(u64 *)(&raw_data[__entry_size - sizeof(u64)]) = 0ULL; \
773 entry = (struct ftrace_raw_##call *)raw_data; \
774 ent = &entry->ent; \
775 tracing_generic_entry_update(ent, irq_flags, pc); \
776 ent->type = event_call->id; \
778 tstruct \
780 { assign; } \
782 perf_tp_event(event_call->id, __addr, __count, entry, \
783 __entry_size); \
785 end: \
786 perf_swevent_put_recursion_context(rctx); \
787 end_recursion: \
788 local_irq_restore(irq_flags); \
792 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
793 #endif /* CONFIG_EVENT_PROFILE */
795 #undef _TRACE_PROFILE_INIT