initial commit with v2.6.32.60
[linux-2.6.32.60-moxart.git] / include / trace / ftrace.h
blob4b6a4a3001677d1482caac5df7e1d6167dd025c2
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 \
47 __attribute__((__aligned__(4))) event_##name
49 #undef __cpparg
50 #define __cpparg(arg...) arg
52 /* Callbacks are meaningless to ftrace. */
53 #undef TRACE_EVENT_FN
54 #define TRACE_EVENT_FN(name, proto, args, tstruct, \
55 assign, print, reg, unreg) \
56 TRACE_EVENT(name, __cpparg(proto), __cpparg(args), \
57 __cpparg(tstruct), __cpparg(assign), __cpparg(print)) \
59 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
63 * Stage 2 of the trace events.
65 * Include the following:
67 * struct ftrace_data_offsets_<call> {
68 * u32 <item1>;
69 * u32 <item2>;
70 * [...]
71 * };
73 * The __dynamic_array() macro will create each u32 <item>, this is
74 * to keep the offset of each array from the beginning of the event.
75 * The size of an array is also encoded, in the higher 16 bits of <item>.
78 #undef __field
79 #define __field(type, item)
81 #undef __field_ext
82 #define __field_ext(type, item, filter_type)
84 #undef __array
85 #define __array(type, item, len)
87 #undef __dynamic_array
88 #define __dynamic_array(type, item, len) u32 item;
90 #undef __string
91 #define __string(item, src) __dynamic_array(char, item, -1)
93 #undef TRACE_EVENT
94 #define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
95 struct ftrace_data_offsets_##call { \
96 tstruct; \
99 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
102 * Setup the showing format of trace point.
104 * int
105 * ftrace_format_##call(struct trace_seq *s)
107 * struct ftrace_raw_##call field;
108 * int ret;
110 * ret = trace_seq_printf(s, #type " " #item ";"
111 * " offset:%u; size:%u;\n",
112 * offsetof(struct ftrace_raw_##call, item),
113 * sizeof(field.type));
118 #undef TP_STRUCT__entry
119 #define TP_STRUCT__entry(args...) args
121 #undef __field
122 #define __field(type, item) \
123 ret = trace_seq_printf(s, "\tfield:" #type " " #item ";\t" \
124 "offset:%u;\tsize:%u;\n", \
125 (unsigned int)offsetof(typeof(field), item), \
126 (unsigned int)sizeof(field.item)); \
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;\n", \
137 (unsigned int)offsetof(typeof(field), item), \
138 (unsigned int)sizeof(field.item)); \
139 if (!ret) \
140 return 0;
142 #undef __dynamic_array
143 #define __dynamic_array(type, item, len) \
144 ret = trace_seq_printf(s, "\tfield:__data_loc " #type "[] " #item ";\t"\
145 "offset:%u;\tsize:%u;\n", \
146 (unsigned int)offsetof(typeof(field), \
147 __data_loc_##item), \
148 (unsigned int)sizeof(field.__data_loc_##item)); \
149 if (!ret) \
150 return 0;
152 #undef __string
153 #define __string(item, src) __dynamic_array(char, item, -1)
155 #undef __entry
156 #define __entry REC
158 #undef __print_symbolic
159 #undef __get_dynamic_array
160 #undef __get_str
162 #undef TP_printk
163 #define TP_printk(fmt, args...) "\"%s\", %s\n", fmt, __stringify(args)
165 #undef TP_fast_assign
166 #define TP_fast_assign(args...) args
168 #undef TP_perf_assign
169 #define TP_perf_assign(args...)
171 #undef TRACE_EVENT
172 #define TRACE_EVENT(call, proto, args, tstruct, func, print) \
173 static int \
174 ftrace_format_##call(struct ftrace_event_call *unused, \
175 struct trace_seq *s) \
177 struct ftrace_raw_##call field __attribute__((unused)); \
178 int ret = 0; \
180 tstruct; \
182 trace_seq_printf(s, "\nprint fmt: " print); \
184 return ret; \
187 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
190 * Stage 3 of the trace events.
192 * Override the macros in <trace/trace_events.h> to include the following:
194 * enum print_line_t
195 * ftrace_raw_output_<call>(struct trace_iterator *iter, int flags)
197 * struct trace_seq *s = &iter->seq;
198 * struct ftrace_raw_<call> *field; <-- defined in stage 1
199 * struct trace_entry *entry;
200 * struct trace_seq *p;
201 * int ret;
203 * entry = iter->ent;
205 * if (entry->type != event_<call>.id) {
206 * WARN_ON_ONCE(1);
207 * return TRACE_TYPE_UNHANDLED;
210 * field = (typeof(field))entry;
212 * p = get_cpu_var(ftrace_event_seq);
213 * trace_seq_init(p);
214 * ret = trace_seq_printf(s, <TP_printk> "\n");
215 * put_cpu();
216 * if (!ret)
217 * return TRACE_TYPE_PARTIAL_LINE;
219 * return TRACE_TYPE_HANDLED;
222 * This is the method used to print the raw event to the trace
223 * output format. Note, this is not needed if the data is read
224 * in binary.
227 #undef __entry
228 #define __entry field
230 #undef TP_printk
231 #define TP_printk(fmt, args...) fmt "\n", args
233 #undef __get_dynamic_array
234 #define __get_dynamic_array(field) \
235 ((void *)__entry + (__entry->__data_loc_##field & 0xffff))
237 #undef __get_str
238 #define __get_str(field) (char *)__get_dynamic_array(field)
240 #undef __print_flags
241 #define __print_flags(flag, delim, flag_array...) \
242 ({ \
243 static const struct trace_print_flags __flags[] = \
244 { flag_array, { -1, NULL }}; \
245 ftrace_print_flags_seq(p, delim, flag, __flags); \
248 #undef __print_symbolic
249 #define __print_symbolic(value, symbol_array...) \
250 ({ \
251 static const struct trace_print_flags symbols[] = \
252 { symbol_array, { -1, NULL }}; \
253 ftrace_print_symbols_seq(p, value, symbols); \
256 #undef TRACE_EVENT
257 #define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
258 static enum print_line_t \
259 ftrace_raw_output_##call(struct trace_iterator *iter, int flags) \
261 struct trace_seq *s = &iter->seq; \
262 struct ftrace_raw_##call *field; \
263 struct trace_entry *entry; \
264 struct trace_seq *p; \
265 int ret; \
267 entry = iter->ent; \
269 if (entry->type != event_##call.id) { \
270 WARN_ON_ONCE(1); \
271 return TRACE_TYPE_UNHANDLED; \
274 field = (typeof(field))entry; \
276 p = &get_cpu_var(ftrace_event_seq); \
277 trace_seq_init(p); \
278 ret = trace_seq_printf(s, #call ": " print); \
279 put_cpu(); \
280 if (!ret) \
281 return TRACE_TYPE_PARTIAL_LINE; \
283 return TRACE_TYPE_HANDLED; \
286 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
288 #undef __field_ext
289 #define __field_ext(type, item, filter_type) \
290 ret = trace_define_field(event_call, #type, #item, \
291 offsetof(typeof(field), item), \
292 sizeof(field.item), \
293 is_signed_type(type), filter_type); \
294 if (ret) \
295 return ret;
297 #undef __field
298 #define __field(type, item) __field_ext(type, item, FILTER_OTHER)
300 #undef __array
301 #define __array(type, item, len) \
302 BUILD_BUG_ON(len > MAX_FILTER_STR_VAL); \
303 ret = trace_define_field(event_call, #type "[" #len "]", #item, \
304 offsetof(typeof(field), item), \
305 sizeof(field.item), 0, FILTER_OTHER); \
306 if (ret) \
307 return ret;
309 #undef __dynamic_array
310 #define __dynamic_array(type, item, len) \
311 ret = trace_define_field(event_call, "__data_loc " #type "[]", #item, \
312 offsetof(typeof(field), __data_loc_##item), \
313 sizeof(field.__data_loc_##item), 0, \
314 FILTER_OTHER);
316 #undef __string
317 #define __string(item, src) __dynamic_array(char, item, -1)
319 #undef TRACE_EVENT
320 #define TRACE_EVENT(call, proto, args, tstruct, func, print) \
321 static int \
322 ftrace_define_fields_##call(struct ftrace_event_call *event_call) \
324 struct ftrace_raw_##call field; \
325 int ret; \
327 ret = trace_define_common_fields(event_call); \
328 if (ret) \
329 return ret; \
331 tstruct; \
333 return ret; \
336 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
339 * remember the offset of each array from the beginning of the event.
342 #undef __entry
343 #define __entry entry
345 #undef __field
346 #define __field(type, item)
348 #undef __field_ext
349 #define __field_ext(type, item, filter_type)
351 #undef __array
352 #define __array(type, item, len)
354 #undef __dynamic_array
355 #define __dynamic_array(type, item, len) \
356 __data_offsets->item = __data_size + \
357 offsetof(typeof(*entry), __data); \
358 __data_offsets->item |= (len * sizeof(type)) << 16; \
359 __data_size += (len) * sizeof(type);
361 #undef __string
362 #define __string(item, src) __dynamic_array(char, item, strlen(src) + 1) \
364 #undef TRACE_EVENT
365 #define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
366 static inline int ftrace_get_offsets_##call( \
367 struct ftrace_data_offsets_##call *__data_offsets, proto) \
369 int __data_size = 0; \
370 struct ftrace_raw_##call __maybe_unused *entry; \
372 tstruct; \
374 return __data_size; \
377 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
379 #ifdef CONFIG_EVENT_PROFILE
382 * Generate the functions needed for tracepoint perf_event support.
384 * NOTE: The insertion profile callback (ftrace_profile_<call>) is defined later
386 * static int ftrace_profile_enable_<call>(void)
388 * return register_trace_<call>(ftrace_profile_<call>);
391 * static void ftrace_profile_disable_<call>(void)
393 * unregister_trace_<call>(ftrace_profile_<call>);
398 #undef TRACE_EVENT
399 #define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
401 static void ftrace_profile_##call(proto); \
403 static int ftrace_profile_enable_##call(void) \
405 return register_trace_##call(ftrace_profile_##call); \
408 static void ftrace_profile_disable_##call(void) \
410 unregister_trace_##call(ftrace_profile_##call); \
413 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
415 #endif
418 * Stage 4 of the trace events.
420 * Override the macros in <trace/trace_events.h> to include the following:
422 * static void ftrace_event_<call>(proto)
424 * event_trace_printk(_RET_IP_, "<call>: " <fmt>);
427 * static int ftrace_reg_event_<call>(void)
429 * int ret;
431 * ret = register_trace_<call>(ftrace_event_<call>);
432 * if (!ret)
433 * pr_info("event trace: Could not activate trace point "
434 * "probe to <call>");
435 * return ret;
438 * static void ftrace_unreg_event_<call>(void)
440 * unregister_trace_<call>(ftrace_event_<call>);
444 * For those macros defined with TRACE_EVENT:
446 * static struct ftrace_event_call event_<call>;
448 * static void ftrace_raw_event_<call>(proto)
450 * struct ring_buffer_event *event;
451 * struct ftrace_raw_<call> *entry; <-- defined in stage 1
452 * struct ring_buffer *buffer;
453 * unsigned long irq_flags;
454 * int pc;
456 * local_save_flags(irq_flags);
457 * pc = preempt_count();
459 * event = trace_current_buffer_lock_reserve(&buffer,
460 * event_<call>.id,
461 * sizeof(struct ftrace_raw_<call>),
462 * irq_flags, pc);
463 * if (!event)
464 * return;
465 * entry = ring_buffer_event_data(event);
467 * <assign>; <-- Here we assign the entries by the __field and
468 * __array macros.
470 * trace_current_buffer_unlock_commit(buffer, event, irq_flags, pc);
473 * static int ftrace_raw_reg_event_<call>(void)
475 * int ret;
477 * ret = register_trace_<call>(ftrace_raw_event_<call>);
478 * if (!ret)
479 * pr_info("event trace: Could not activate trace point "
480 * "probe to <call>");
481 * return ret;
484 * static void ftrace_unreg_event_<call>(void)
486 * unregister_trace_<call>(ftrace_raw_event_<call>);
489 * static struct trace_event ftrace_event_type_<call> = {
490 * .trace = ftrace_raw_output_<call>, <-- stage 2
491 * };
493 * static int ftrace_raw_init_event_<call>(void)
495 * int id;
497 * id = register_ftrace_event(&ftrace_event_type_<call>);
498 * if (!id)
499 * return -ENODEV;
500 * event_<call>.id = id;
501 * return 0;
504 * static struct ftrace_event_call __used
505 * __attribute__((__aligned__(4)))
506 * __attribute__((section("_ftrace_events"))) event_<call> = {
507 * .name = "<call>",
508 * .system = "<system>",
509 * .raw_init = ftrace_raw_init_event_<call>,
510 * .regfunc = ftrace_reg_event_<call>,
511 * .unregfunc = ftrace_unreg_event_<call>,
512 * .show_format = ftrace_format_<call>,
517 #undef TP_FMT
518 #define TP_FMT(fmt, args...) fmt "\n", ##args
520 #ifdef CONFIG_EVENT_PROFILE
522 #define _TRACE_PROFILE_INIT(call) \
523 .profile_count = ATOMIC_INIT(-1), \
524 .profile_enable = ftrace_profile_enable_##call, \
525 .profile_disable = ftrace_profile_disable_##call,
527 #else
528 #define _TRACE_PROFILE_INIT(call)
529 #endif
531 #undef __entry
532 #define __entry entry
534 #undef __field
535 #define __field(type, item)
537 #undef __array
538 #define __array(type, item, len)
540 #undef __dynamic_array
541 #define __dynamic_array(type, item, len) \
542 __entry->__data_loc_##item = __data_offsets.item;
544 #undef __string
545 #define __string(item, src) __dynamic_array(char, item, -1) \
547 #undef __assign_str
548 #define __assign_str(dst, src) \
549 strcpy(__get_str(dst), src);
551 #undef TRACE_EVENT
552 #define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
554 static struct ftrace_event_call event_##call; \
556 static void ftrace_raw_event_##call(proto) \
558 struct ftrace_data_offsets_##call __maybe_unused __data_offsets;\
559 struct ftrace_event_call *event_call = &event_##call; \
560 struct ring_buffer_event *event; \
561 struct ftrace_raw_##call *entry; \
562 struct ring_buffer *buffer; \
563 unsigned long irq_flags; \
564 int __data_size; \
565 int pc; \
567 local_save_flags(irq_flags); \
568 pc = preempt_count(); \
570 __data_size = ftrace_get_offsets_##call(&__data_offsets, args); \
572 event = trace_current_buffer_lock_reserve(&buffer, \
573 event_##call.id, \
574 sizeof(*entry) + __data_size, \
575 irq_flags, pc); \
576 if (!event) \
577 return; \
578 entry = ring_buffer_event_data(event); \
581 tstruct \
583 { assign; } \
585 if (!filter_current_check_discard(buffer, event_call, entry, event)) \
586 trace_nowake_buffer_unlock_commit(buffer, \
587 event, irq_flags, pc); \
590 static int ftrace_raw_reg_event_##call(void *ptr) \
592 int ret; \
594 ret = register_trace_##call(ftrace_raw_event_##call); \
595 if (ret) \
596 pr_info("event trace: Could not activate trace point " \
597 "probe to " #call "\n"); \
598 return ret; \
601 static void ftrace_raw_unreg_event_##call(void *ptr) \
603 unregister_trace_##call(ftrace_raw_event_##call); \
606 static struct trace_event ftrace_event_type_##call = { \
607 .trace = ftrace_raw_output_##call, \
608 }; \
610 static int ftrace_raw_init_event_##call(void) \
612 int id; \
614 id = register_ftrace_event(&ftrace_event_type_##call); \
615 if (!id) \
616 return -ENODEV; \
617 event_##call.id = id; \
618 INIT_LIST_HEAD(&event_##call.fields); \
619 return 0; \
622 static struct ftrace_event_call __used \
623 __attribute__((__aligned__(4))) \
624 __attribute__((section("_ftrace_events"))) event_##call = { \
625 .name = #call, \
626 .system = __stringify(TRACE_SYSTEM), \
627 .event = &ftrace_event_type_##call, \
628 .raw_init = ftrace_raw_init_event_##call, \
629 .regfunc = ftrace_raw_reg_event_##call, \
630 .unregfunc = ftrace_raw_unreg_event_##call, \
631 .show_format = ftrace_format_##call, \
632 .define_fields = ftrace_define_fields_##call, \
633 _TRACE_PROFILE_INIT(call) \
636 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
639 * Define the insertion callback to profile events
641 * The job is very similar to ftrace_raw_event_<call> except that we don't
642 * insert in the ring buffer but in a perf counter.
644 * static void ftrace_profile_<call>(proto)
646 * struct ftrace_data_offsets_<call> __maybe_unused __data_offsets;
647 * struct ftrace_event_call *event_call = &event_<call>;
648 * extern void perf_tp_event(int, u64, u64, void *, int);
649 * struct ftrace_raw_##call *entry;
650 * u64 __addr = 0, __count = 1;
651 * unsigned long irq_flags;
652 * struct trace_entry *ent;
653 * int __entry_size;
654 * int __data_size;
655 * int __cpu
656 * int pc;
658 * pc = preempt_count();
660 * __data_size = ftrace_get_offsets_<call>(&__data_offsets, args);
662 * // Below we want to get the aligned size by taking into account
663 * // the u32 field that will later store the buffer size
664 * __entry_size = ALIGN(__data_size + sizeof(*entry) + sizeof(u32),
665 * sizeof(u64));
666 * __entry_size -= sizeof(u32);
668 * // Protect the non nmi buffer
669 * // This also protects the rcu read side
670 * local_irq_save(irq_flags);
671 * __cpu = smp_processor_id();
673 * if (in_nmi())
674 * raw_data = rcu_dereference(trace_profile_buf_nmi);
675 * else
676 * raw_data = rcu_dereference(trace_profile_buf);
678 * if (!raw_data)
679 * goto end;
681 * raw_data = per_cpu_ptr(raw_data, __cpu);
683 * //zero dead bytes from alignment to avoid stack leak to userspace:
684 * *(u64 *)(&raw_data[__entry_size - sizeof(u64)]) = 0ULL;
685 * entry = (struct ftrace_raw_<call> *)raw_data;
686 * ent = &entry->ent;
687 * tracing_generic_entry_update(ent, irq_flags, pc);
688 * ent->type = event_call->id;
690 * <tstruct> <- do some jobs with dynamic arrays
692 * <assign> <- affect our values
694 * perf_tp_event(event_call->id, __addr, __count, entry,
695 * __entry_size); <- submit them to perf counter
700 #ifdef CONFIG_EVENT_PROFILE
702 #undef __perf_addr
703 #define __perf_addr(a) __addr = (a)
705 #undef __perf_count
706 #define __perf_count(c) __count = (c)
708 #undef TRACE_EVENT
709 #define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
710 static void ftrace_profile_##call(proto) \
712 struct ftrace_data_offsets_##call __maybe_unused __data_offsets;\
713 struct ftrace_event_call *event_call = &event_##call; \
714 extern void perf_tp_event(int, u64, u64, void *, int); \
715 struct ftrace_raw_##call *entry; \
716 u64 __addr = 0, __count = 1; \
717 unsigned long irq_flags; \
718 struct trace_entry *ent; \
719 int __entry_size; \
720 int __data_size; \
721 char *raw_data; \
722 int __cpu; \
723 int pc; \
725 pc = preempt_count(); \
727 __data_size = ftrace_get_offsets_##call(&__data_offsets, args); \
728 __entry_size = ALIGN(__data_size + sizeof(*entry) + sizeof(u32),\
729 sizeof(u64)); \
730 __entry_size -= sizeof(u32); \
732 if (WARN_ONCE(__entry_size > FTRACE_MAX_PROFILE_SIZE, \
733 "profile buffer not large enough")) \
734 return; \
736 local_irq_save(irq_flags); \
737 __cpu = smp_processor_id(); \
739 if (in_nmi()) \
740 raw_data = rcu_dereference(trace_profile_buf_nmi); \
741 else \
742 raw_data = rcu_dereference(trace_profile_buf); \
744 if (!raw_data) \
745 goto end; \
747 raw_data = per_cpu_ptr(raw_data, __cpu); \
749 *(u64 *)(&raw_data[__entry_size - sizeof(u64)]) = 0ULL; \
750 entry = (struct ftrace_raw_##call *)raw_data; \
751 ent = &entry->ent; \
752 tracing_generic_entry_update(ent, irq_flags, pc); \
753 ent->type = event_call->id; \
755 tstruct \
757 { assign; } \
759 perf_tp_event(event_call->id, __addr, __count, entry, \
760 __entry_size); \
762 end: \
763 local_irq_restore(irq_flags); \
767 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
768 #endif /* CONFIG_EVENT_PROFILE */
770 #undef _TRACE_PROFILE_INIT