Merge branch 'devel' of master.kernel.org:/home/rmk/linux-2.6-arm
[pandora-kernel.git] / include / trace / ftrace.h
1 /*
2  * Stage 1 of the trace events.
3  *
4  * Override the macros in <trace/trace_events.h> to include the following:
5  *
6  * struct ftrace_raw_<call> {
7  *      struct trace_entry              ent;
8  *      <type>                          <item>;
9  *      <type2>                         <item2>[<len>];
10  *      [...]
11  * };
12  *
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.
17  */
18
19 #include <linux/ftrace_event.h>
20
21 /*
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.
27  *
28  * TRACE_EVENT is a one to one mapping between tracepoint and template.
29  */
30 #undef TRACE_EVENT
31 #define TRACE_EVENT(name, proto, args, tstruct, assign, print) \
32         DECLARE_EVENT_CLASS(name,                              \
33                              PARAMS(proto),                    \
34                              PARAMS(args),                     \
35                              PARAMS(tstruct),                  \
36                              PARAMS(assign),                   \
37                              PARAMS(print));                   \
38         DEFINE_EVENT(name, name, PARAMS(proto), PARAMS(args));
39
40
41 #undef __field
42 #define __field(type, item)             type    item;
43
44 #undef __field_ext
45 #define __field_ext(type, item, filter_type)    type    item;
46
47 #undef __array
48 #define __array(type, item, len)        type    item[len];
49
50 #undef __dynamic_array
51 #define __dynamic_array(type, item, len) u32 __data_loc_##item;
52
53 #undef __string
54 #define __string(item, src) __dynamic_array(char, item, -1)
55
56 #undef TP_STRUCT__entry
57 #define TP_STRUCT__entry(args...) args
58
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;                            \
63                 tstruct                                                 \
64                 char                    __data[0];                      \
65         };
66 #undef DEFINE_EVENT
67 #define DEFINE_EVENT(template, name, proto, args)       \
68         static struct ftrace_event_call                 \
69         __attribute__((__aligned__(4))) event_##name
70
71 #undef DEFINE_EVENT_PRINT
72 #define DEFINE_EVENT_PRINT(template, name, proto, args, print)  \
73         DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
74
75 #undef __cpparg
76 #define __cpparg(arg...) arg
77
78 /* Callbacks are meaningless to ftrace. */
79 #undef TRACE_EVENT_FN
80 #define TRACE_EVENT_FN(name, proto, args, tstruct,                      \
81                 assign, print, reg, unreg)                              \
82         TRACE_EVENT(name, __cpparg(proto), __cpparg(args),              \
83                 __cpparg(tstruct), __cpparg(assign), __cpparg(print))   \
84
85 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
86
87
88 /*
89  * Stage 2 of the trace events.
90  *
91  * Include the following:
92  *
93  * struct ftrace_data_offsets_<call> {
94  *      u32                             <item1>;
95  *      u32                             <item2>;
96  *      [...]
97  * };
98  *
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>.
102  */
103
104 #undef __field
105 #define __field(type, item)
106
107 #undef __field_ext
108 #define __field_ext(type, item, filter_type)
109
110 #undef __array
111 #define __array(type, item, len)
112
113 #undef __dynamic_array
114 #define __dynamic_array(type, item, len)        u32 item;
115
116 #undef __string
117 #define __string(item, src) __dynamic_array(char, item, -1)
118
119 #undef DECLARE_EVENT_CLASS
120 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print)  \
121         struct ftrace_data_offsets_##call {                             \
122                 tstruct;                                                \
123         };
124
125 #undef DEFINE_EVENT
126 #define DEFINE_EVENT(template, name, proto, args)
127
128 #undef DEFINE_EVENT_PRINT
129 #define DEFINE_EVENT_PRINT(template, name, proto, args, print)  \
130         DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
131
132 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
133
134 /*
135  * Stage 3 of the trace events.
136  *
137  * Override the macros in <trace/trace_events.h> to include the following:
138  *
139  * enum print_line_t
140  * ftrace_raw_output_<call>(struct trace_iterator *iter, int flags)
141  * {
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;
146  *      int ret;
147  *
148  *      entry = iter->ent;
149  *
150  *      if (entry->type != event_<call>.id) {
151  *              WARN_ON_ONCE(1);
152  *              return TRACE_TYPE_UNHANDLED;
153  *      }
154  *
155  *      field = (typeof(field))entry;
156  *
157  *      p = &get_cpu_var(ftrace_event_seq);
158  *      trace_seq_init(p);
159  *      ret = trace_seq_printf(s, "%s: ", <call>);
160  *      if (ret)
161  *              ret = trace_seq_printf(s, <TP_printk> "\n");
162  *      put_cpu();
163  *      if (!ret)
164  *              return TRACE_TYPE_PARTIAL_LINE;
165  *
166  *      return TRACE_TYPE_HANDLED;
167  * }
168  *
169  * This is the method used to print the raw event to the trace
170  * output format. Note, this is not needed if the data is read
171  * in binary.
172  */
173
174 #undef __entry
175 #define __entry field
176
177 #undef TP_printk
178 #define TP_printk(fmt, args...) fmt "\n", args
179
180 #undef __get_dynamic_array
181 #define __get_dynamic_array(field)      \
182                 ((void *)__entry + (__entry->__data_loc_##field & 0xffff))
183
184 #undef __get_str
185 #define __get_str(field) (char *)__get_dynamic_array(field)
186
187 #undef __print_flags
188 #define __print_flags(flag, delim, flag_array...)                       \
189         ({                                                              \
190                 static const struct trace_print_flags __flags[] =       \
191                         { flag_array, { -1, NULL }};                    \
192                 ftrace_print_flags_seq(p, delim, flag, __flags);        \
193         })
194
195 #undef __print_symbolic
196 #define __print_symbolic(value, symbol_array...)                        \
197         ({                                                              \
198                 static const struct trace_print_flags symbols[] =       \
199                         { symbol_array, { -1, NULL }};                  \
200                 ftrace_print_symbols_seq(p, value, symbols);            \
201         })
202
203 #undef DECLARE_EVENT_CLASS
204 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print)  \
205 static notrace enum print_line_t                                        \
206 ftrace_raw_output_id_##call(int event_id, const char *name,             \
207                             struct trace_iterator *iter, int flags)     \
208 {                                                                       \
209         struct trace_seq *s = &iter->seq;                               \
210         struct ftrace_raw_##call *field;                                \
211         struct trace_entry *entry;                                      \
212         struct trace_seq *p;                                            \
213         int ret;                                                        \
214                                                                         \
215         entry = iter->ent;                                              \
216                                                                         \
217         if (entry->type != event_id) {                                  \
218                 WARN_ON_ONCE(1);                                        \
219                 return TRACE_TYPE_UNHANDLED;                            \
220         }                                                               \
221                                                                         \
222         field = (typeof(field))entry;                                   \
223                                                                         \
224         p = &get_cpu_var(ftrace_event_seq);                             \
225         trace_seq_init(p);                                              \
226         ret = trace_seq_printf(s, "%s: ", name);                        \
227         if (ret)                                                        \
228                 ret = trace_seq_printf(s, print);                       \
229         put_cpu();                                                      \
230         if (!ret)                                                       \
231                 return TRACE_TYPE_PARTIAL_LINE;                         \
232                                                                         \
233         return TRACE_TYPE_HANDLED;                                      \
234 }
235
236 #undef DEFINE_EVENT
237 #define DEFINE_EVENT(template, name, proto, args)                       \
238 static notrace enum print_line_t                                        \
239 ftrace_raw_output_##name(struct trace_iterator *iter, int flags)        \
240 {                                                                       \
241         return ftrace_raw_output_id_##template(event_##name.id,         \
242                                                #name, iter, flags);     \
243 }
244
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 {                                                                       \
250         struct trace_seq *s = &iter->seq;                               \
251         struct ftrace_raw_##template *field;                            \
252         struct trace_entry *entry;                                      \
253         struct trace_seq *p;                                            \
254         int ret;                                                        \
255                                                                         \
256         entry = iter->ent;                                              \
257                                                                         \
258         if (entry->type != event_##call.id) {                           \
259                 WARN_ON_ONCE(1);                                        \
260                 return TRACE_TYPE_UNHANDLED;                            \
261         }                                                               \
262                                                                         \
263         field = (typeof(field))entry;                                   \
264                                                                         \
265         p = &get_cpu_var(ftrace_event_seq);                             \
266         trace_seq_init(p);                                              \
267         ret = trace_seq_printf(s, "%s: ", #call);                       \
268         if (ret)                                                        \
269                 ret = trace_seq_printf(s, print);                       \
270         put_cpu();                                                      \
271         if (!ret)                                                       \
272                 return TRACE_TYPE_PARTIAL_LINE;                         \
273                                                                         \
274         return TRACE_TYPE_HANDLED;                                      \
275 }
276
277 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
278
279 #undef __field_ext
280 #define __field_ext(type, item, filter_type)                            \
281         ret = trace_define_field(event_call, #type, #item,              \
282                                  offsetof(typeof(field), item),         \
283                                  sizeof(field.item),                    \
284                                  is_signed_type(type), filter_type);    \
285         if (ret)                                                        \
286                 return ret;
287
288 #undef __field
289 #define __field(type, item)     __field_ext(type, item, FILTER_OTHER)
290
291 #undef __array
292 #define __array(type, item, len)                                        \
293         BUILD_BUG_ON(len > MAX_FILTER_STR_VAL);                         \
294         ret = trace_define_field(event_call, #type "[" #len "]", #item, \
295                                  offsetof(typeof(field), item),         \
296                                  sizeof(field.item),                    \
297                                  is_signed_type(type), FILTER_OTHER);   \
298         if (ret)                                                        \
299                 return ret;
300
301 #undef __dynamic_array
302 #define __dynamic_array(type, item, len)                                       \
303         ret = trace_define_field(event_call, "__data_loc " #type "[]", #item,  \
304                                  offsetof(typeof(field), __data_loc_##item),   \
305                                  sizeof(field.__data_loc_##item),              \
306                                  is_signed_type(type), FILTER_OTHER);
307
308 #undef __string
309 #define __string(item, src) __dynamic_array(char, item, -1)
310
311 #undef DECLARE_EVENT_CLASS
312 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, func, print)    \
313 static int notrace                                                      \
314 ftrace_define_fields_##call(struct ftrace_event_call *event_call)       \
315 {                                                                       \
316         struct ftrace_raw_##call field;                                 \
317         int ret;                                                        \
318                                                                         \
319         tstruct;                                                        \
320                                                                         \
321         return ret;                                                     \
322 }
323
324 #undef DEFINE_EVENT
325 #define DEFINE_EVENT(template, name, proto, args)
326
327 #undef DEFINE_EVENT_PRINT
328 #define DEFINE_EVENT_PRINT(template, name, proto, args, print)  \
329         DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
330
331 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
332
333 /*
334  * remember the offset of each array from the beginning of the event.
335  */
336
337 #undef __entry
338 #define __entry entry
339
340 #undef __field
341 #define __field(type, item)
342
343 #undef __field_ext
344 #define __field_ext(type, item, filter_type)
345
346 #undef __array
347 #define __array(type, item, len)
348
349 #undef __dynamic_array
350 #define __dynamic_array(type, item, len)                                \
351         __data_offsets->item = __data_size +                            \
352                                offsetof(typeof(*entry), __data);        \
353         __data_offsets->item |= (len * sizeof(type)) << 16;             \
354         __data_size += (len) * sizeof(type);
355
356 #undef __string
357 #define __string(item, src) __dynamic_array(char, item, strlen(src) + 1)
358
359 #undef DECLARE_EVENT_CLASS
360 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print)  \
361 static inline notrace int ftrace_get_offsets_##call(                    \
362         struct ftrace_data_offsets_##call *__data_offsets, proto)       \
363 {                                                                       \
364         int __data_size = 0;                                            \
365         struct ftrace_raw_##call __maybe_unused *entry;                 \
366                                                                         \
367         tstruct;                                                        \
368                                                                         \
369         return __data_size;                                             \
370 }
371
372 #undef DEFINE_EVENT
373 #define DEFINE_EVENT(template, name, proto, args)
374
375 #undef DEFINE_EVENT_PRINT
376 #define DEFINE_EVENT_PRINT(template, name, proto, args, print)  \
377         DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
378
379 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
380
381 #ifdef CONFIG_PERF_EVENTS
382
383 /*
384  * Generate the functions needed for tracepoint perf_event support.
385  *
386  * NOTE: The insertion profile callback (ftrace_profile_<call>) is defined later
387  *
388  * static int ftrace_profile_enable_<call>(void)
389  * {
390  *      return register_trace_<call>(ftrace_profile_<call>);
391  * }
392  *
393  * static void ftrace_profile_disable_<call>(void)
394  * {
395  *      unregister_trace_<call>(ftrace_profile_<call>);
396  * }
397  *
398  */
399
400 #undef DECLARE_EVENT_CLASS
401 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print)
402
403 #undef DEFINE_EVENT
404 #define DEFINE_EVENT(template, name, proto, args)                       \
405                                                                         \
406 static void perf_trace_##name(proto);                                   \
407                                                                         \
408 static notrace int                                                      \
409 perf_trace_enable_##name(struct ftrace_event_call *unused)              \
410 {                                                                       \
411         return register_trace_##name(perf_trace_##name);                \
412 }                                                                       \
413                                                                         \
414 static notrace void                                                     \
415 perf_trace_disable_##name(struct ftrace_event_call *unused)             \
416 {                                                                       \
417         unregister_trace_##name(perf_trace_##name);                     \
418 }
419
420 #undef DEFINE_EVENT_PRINT
421 #define DEFINE_EVENT_PRINT(template, name, proto, args, print)  \
422         DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
423
424 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
425
426 #endif /* CONFIG_PERF_EVENTS */
427
428 /*
429  * Stage 4 of the trace events.
430  *
431  * Override the macros in <trace/trace_events.h> to include the following:
432  *
433  * static void ftrace_event_<call>(proto)
434  * {
435  *      event_trace_printk(_RET_IP_, "<call>: " <fmt>);
436  * }
437  *
438  * static int ftrace_reg_event_<call>(struct ftrace_event_call *unused)
439  * {
440  *      return register_trace_<call>(ftrace_event_<call>);
441  * }
442  *
443  * static void ftrace_unreg_event_<call>(struct ftrace_event_call *unused)
444  * {
445  *      unregister_trace_<call>(ftrace_event_<call>);
446  * }
447  *
448  *
449  * For those macros defined with TRACE_EVENT:
450  *
451  * static struct ftrace_event_call event_<call>;
452  *
453  * static void ftrace_raw_event_<call>(proto)
454  * {
455  *      struct ftrace_data_offsets_<call> __maybe_unused __data_offsets;
456  *      struct ring_buffer_event *event;
457  *      struct ftrace_raw_<call> *entry; <-- defined in stage 1
458  *      struct ring_buffer *buffer;
459  *      unsigned long irq_flags;
460  *      int __data_size;
461  *      int pc;
462  *
463  *      local_save_flags(irq_flags);
464  *      pc = preempt_count();
465  *
466  *      __data_size = ftrace_get_offsets_<call>(&__data_offsets, args);
467  *
468  *      event = trace_current_buffer_lock_reserve(&buffer,
469  *                                event_<call>.id,
470  *                                sizeof(*entry) + __data_size,
471  *                                irq_flags, pc);
472  *      if (!event)
473  *              return;
474  *      entry   = ring_buffer_event_data(event);
475  *
476  *      { <assign>; }  <-- Here we assign the entries by the __field and
477  *                         __array macros.
478  *
479  *      if (!filter_current_check_discard(buffer, event_call, entry, event))
480  *              trace_current_buffer_unlock_commit(buffer,
481  *                                                 event, irq_flags, pc);
482  * }
483  *
484  * static int ftrace_raw_reg_event_<call>(struct ftrace_event_call *unused)
485  * {
486  *      return register_trace_<call>(ftrace_raw_event_<call>);
487  * }
488  *
489  * static void ftrace_unreg_event_<call>(struct ftrace_event_call *unused)
490  * {
491  *      unregister_trace_<call>(ftrace_raw_event_<call>);
492  * }
493  *
494  * static struct trace_event ftrace_event_type_<call> = {
495  *      .trace                  = ftrace_raw_output_<call>, <-- stage 2
496  * };
497  *
498  * static const char print_fmt_<call>[] = <TP_printk>;
499  *
500  * static struct ftrace_event_call __used
501  * __attribute__((__aligned__(4)))
502  * __attribute__((section("_ftrace_events"))) event_<call> = {
503  *      .name                   = "<call>",
504  *      .system                 = "<system>",
505  *      .raw_init               = trace_event_raw_init,
506  *      .regfunc                = ftrace_reg_event_<call>,
507  *      .unregfunc              = ftrace_unreg_event_<call>,
508  *      .print_fmt              = print_fmt_<call>,
509  *      .define_fields          = ftrace_define_fields_<call>,
510  * }
511  *
512  */
513
514 #ifdef CONFIG_PERF_EVENTS
515
516 #define _TRACE_PERF_INIT(call)                                          \
517         .perf_event_enable = perf_trace_enable_##call,                  \
518         .perf_event_disable = perf_trace_disable_##call,
519
520 #else
521 #define _TRACE_PERF_INIT(call)
522 #endif /* CONFIG_PERF_EVENTS */
523
524 #undef __entry
525 #define __entry entry
526
527 #undef __field
528 #define __field(type, item)
529
530 #undef __array
531 #define __array(type, item, len)
532
533 #undef __dynamic_array
534 #define __dynamic_array(type, item, len)                                \
535         __entry->__data_loc_##item = __data_offsets.item;
536
537 #undef __string
538 #define __string(item, src) __dynamic_array(char, item, -1)             \
539
540 #undef __assign_str
541 #define __assign_str(dst, src)                                          \
542         strcpy(__get_str(dst), src);
543
544 #undef TP_fast_assign
545 #define TP_fast_assign(args...) args
546
547 #undef TP_perf_assign
548 #define TP_perf_assign(args...)
549
550 #undef DECLARE_EVENT_CLASS
551 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print)  \
552                                                                         \
553 static notrace void                                                     \
554 ftrace_raw_event_id_##call(struct ftrace_event_call *event_call,        \
555                                        proto)                           \
556 {                                                                       \
557         struct ftrace_data_offsets_##call __maybe_unused __data_offsets;\
558         struct ring_buffer_event *event;                                \
559         struct ftrace_raw_##call *entry;                                \
560         struct ring_buffer *buffer;                                     \
561         unsigned long irq_flags;                                        \
562         int __data_size;                                                \
563         int pc;                                                         \
564                                                                         \
565         local_save_flags(irq_flags);                                    \
566         pc = preempt_count();                                           \
567                                                                         \
568         __data_size = ftrace_get_offsets_##call(&__data_offsets, args); \
569                                                                         \
570         event = trace_current_buffer_lock_reserve(&buffer,              \
571                                  event_call->id,                        \
572                                  sizeof(*entry) + __data_size,          \
573                                  irq_flags, pc);                        \
574         if (!event)                                                     \
575                 return;                                                 \
576         entry   = ring_buffer_event_data(event);                        \
577                                                                         \
578         tstruct                                                         \
579                                                                         \
580         { assign; }                                                     \
581                                                                         \
582         if (!filter_current_check_discard(buffer, event_call, entry, event)) \
583                 trace_nowake_buffer_unlock_commit(buffer,               \
584                                                   event, irq_flags, pc); \
585 }
586
587 #undef DEFINE_EVENT
588 #define DEFINE_EVENT(template, call, proto, args)                       \
589                                                                         \
590 static notrace void ftrace_raw_event_##call(proto)                      \
591 {                                                                       \
592         ftrace_raw_event_id_##template(&event_##call, args);            \
593 }                                                                       \
594                                                                         \
595 static notrace int                                                      \
596 ftrace_raw_reg_event_##call(struct ftrace_event_call *unused)           \
597 {                                                                       \
598         return register_trace_##call(ftrace_raw_event_##call);          \
599 }                                                                       \
600                                                                         \
601 static notrace void                                                     \
602 ftrace_raw_unreg_event_##call(struct ftrace_event_call *unused)         \
603 {                                                                       \
604         unregister_trace_##call(ftrace_raw_event_##call);               \
605 }                                                                       \
606                                                                         \
607 static struct trace_event ftrace_event_type_##call = {                  \
608         .trace                  = ftrace_raw_output_##call,             \
609 };
610
611 #undef DEFINE_EVENT_PRINT
612 #define DEFINE_EVENT_PRINT(template, name, proto, args, print)  \
613         DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
614
615 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
616
617 #undef __entry
618 #define __entry REC
619
620 #undef __print_flags
621 #undef __print_symbolic
622 #undef __get_dynamic_array
623 #undef __get_str
624
625 #undef TP_printk
626 #define TP_printk(fmt, args...) "\"" fmt "\", "  __stringify(args)
627
628 #undef DECLARE_EVENT_CLASS
629 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print)  \
630 static const char print_fmt_##call[] = print;
631
632 #undef DEFINE_EVENT
633 #define DEFINE_EVENT(template, call, proto, args)                       \
634                                                                         \
635 static struct ftrace_event_call __used                                  \
636 __attribute__((__aligned__(4)))                                         \
637 __attribute__((section("_ftrace_events"))) event_##call = {             \
638         .name                   = #call,                                \
639         .system                 = __stringify(TRACE_SYSTEM),            \
640         .event                  = &ftrace_event_type_##call,            \
641         .raw_init               = trace_event_raw_init,                 \
642         .regfunc                = ftrace_raw_reg_event_##call,          \
643         .unregfunc              = ftrace_raw_unreg_event_##call,        \
644         .print_fmt              = print_fmt_##template,                 \
645         .define_fields          = ftrace_define_fields_##template,      \
646         _TRACE_PERF_INIT(call)                                  \
647 }
648
649 #undef DEFINE_EVENT_PRINT
650 #define DEFINE_EVENT_PRINT(template, call, proto, args, print)          \
651                                                                         \
652 static const char print_fmt_##call[] = print;                           \
653                                                                         \
654 static struct ftrace_event_call __used                                  \
655 __attribute__((__aligned__(4)))                                         \
656 __attribute__((section("_ftrace_events"))) event_##call = {             \
657         .name                   = #call,                                \
658         .system                 = __stringify(TRACE_SYSTEM),            \
659         .event                  = &ftrace_event_type_##call,            \
660         .raw_init               = trace_event_raw_init,                 \
661         .regfunc                = ftrace_raw_reg_event_##call,          \
662         .unregfunc              = ftrace_raw_unreg_event_##call,        \
663         .print_fmt              = print_fmt_##call,                     \
664         .define_fields          = ftrace_define_fields_##template,      \
665         _TRACE_PERF_INIT(call)                                  \
666 }
667
668 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
669
670 /*
671  * Define the insertion callback to perf events
672  *
673  * The job is very similar to ftrace_raw_event_<call> except that we don't
674  * insert in the ring buffer but in a perf counter.
675  *
676  * static void ftrace_perf_<call>(proto)
677  * {
678  *      struct ftrace_data_offsets_<call> __maybe_unused __data_offsets;
679  *      struct ftrace_event_call *event_call = &event_<call>;
680  *      extern void perf_tp_event(int, u64, u64, void *, int);
681  *      struct ftrace_raw_##call *entry;
682  *      struct perf_trace_buf *trace_buf;
683  *      u64 __addr = 0, __count = 1;
684  *      unsigned long irq_flags;
685  *      struct trace_entry *ent;
686  *      int __entry_size;
687  *      int __data_size;
688  *      int __cpu
689  *      int pc;
690  *
691  *      pc = preempt_count();
692  *
693  *      __data_size = ftrace_get_offsets_<call>(&__data_offsets, args);
694  *
695  *      // Below we want to get the aligned size by taking into account
696  *      // the u32 field that will later store the buffer size
697  *      __entry_size = ALIGN(__data_size + sizeof(*entry) + sizeof(u32),
698  *                           sizeof(u64));
699  *      __entry_size -= sizeof(u32);
700  *
701  *      // Protect the non nmi buffer
702  *      // This also protects the rcu read side
703  *      local_irq_save(irq_flags);
704  *      __cpu = smp_processor_id();
705  *
706  *      if (in_nmi())
707  *              trace_buf = rcu_dereference_sched(perf_trace_buf_nmi);
708  *      else
709  *              trace_buf = rcu_dereference_sched(perf_trace_buf);
710  *
711  *      if (!trace_buf)
712  *              goto end;
713  *
714  *      trace_buf = per_cpu_ptr(trace_buf, __cpu);
715  *
716  *      // Avoid recursion from perf that could mess up the buffer
717  *      if (trace_buf->recursion++)
718  *              goto end_recursion;
719  *
720  *      raw_data = trace_buf->buf;
721  *
722  *      // Make recursion update visible before entering perf_tp_event
723  *      // so that we protect from perf recursions.
724  *
725  *      barrier();
726  *
727  *      //zero dead bytes from alignment to avoid stack leak to userspace:
728  *      *(u64 *)(&raw_data[__entry_size - sizeof(u64)]) = 0ULL;
729  *      entry = (struct ftrace_raw_<call> *)raw_data;
730  *      ent = &entry->ent;
731  *      tracing_generic_entry_update(ent, irq_flags, pc);
732  *      ent->type = event_call->id;
733  *
734  *      <tstruct> <- do some jobs with dynamic arrays
735  *
736  *      <assign>  <- affect our values
737  *
738  *      perf_tp_event(event_call->id, __addr, __count, entry,
739  *                   __entry_size);  <- submit them to perf counter
740  *
741  * }
742  */
743
744 #ifdef CONFIG_PERF_EVENTS
745
746 #undef __entry
747 #define __entry entry
748
749 #undef __get_dynamic_array
750 #define __get_dynamic_array(field)      \
751                 ((void *)__entry + (__entry->__data_loc_##field & 0xffff))
752
753 #undef __get_str
754 #define __get_str(field) (char *)__get_dynamic_array(field)
755
756 #undef __perf_addr
757 #define __perf_addr(a) __addr = (a)
758
759 #undef __perf_count
760 #define __perf_count(c) __count = (c)
761
762 #undef DECLARE_EVENT_CLASS
763 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print)  \
764 static notrace void                                                     \
765 perf_trace_templ_##call(struct ftrace_event_call *event_call,           \
766                         struct pt_regs *__regs, proto)                  \
767 {                                                                       \
768         struct ftrace_data_offsets_##call __maybe_unused __data_offsets;\
769         struct ftrace_raw_##call *entry;                                \
770         u64 __addr = 0, __count = 1;                                    \
771         unsigned long irq_flags;                                        \
772         int __entry_size;                                               \
773         int __data_size;                                                \
774         int rctx;                                                       \
775                                                                         \
776         __data_size = ftrace_get_offsets_##call(&__data_offsets, args); \
777         __entry_size = ALIGN(__data_size + sizeof(*entry) + sizeof(u32),\
778                              sizeof(u64));                              \
779         __entry_size -= sizeof(u32);                                    \
780                                                                         \
781         if (WARN_ONCE(__entry_size > PERF_MAX_TRACE_SIZE,               \
782                       "profile buffer not large enough"))               \
783                 return;                                                 \
784         entry = (struct ftrace_raw_##call *)perf_trace_buf_prepare(     \
785                 __entry_size, event_call->id, &rctx, &irq_flags);       \
786         if (!entry)                                                     \
787                 return;                                                 \
788         tstruct                                                         \
789                                                                         \
790         { assign; }                                                     \
791                                                                         \
792         perf_trace_buf_submit(entry, __entry_size, rctx, __addr,        \
793                                __count, irq_flags, __regs);             \
794 }
795
796 #undef DEFINE_EVENT
797 #define DEFINE_EVENT(template, call, proto, args)                       \
798 static notrace void perf_trace_##call(proto)                            \
799 {                                                                       \
800         struct ftrace_event_call *event_call = &event_##call;           \
801         struct pt_regs *__regs = &get_cpu_var(perf_trace_regs);         \
802                                                                         \
803         perf_fetch_caller_regs(__regs, 1);                              \
804                                                                         \
805         perf_trace_templ_##template(event_call, __regs, args);          \
806                                                                         \
807         put_cpu_var(perf_trace_regs);                                   \
808 }
809
810 #undef DEFINE_EVENT_PRINT
811 #define DEFINE_EVENT_PRINT(template, name, proto, args, print)  \
812         DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
813
814 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
815 #endif /* CONFIG_PERF_EVENTS */
816
817 #undef _TRACE_PROFILE_INIT
818