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