Merge branch 'tip/tracing/ftrace-4' of git://git.kernel.org/pub/scm/linux/kernel...
[safe/jmp/linux-2.6] / 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 #undef __field
22 #define __field(type, item)             type    item;
23
24 #undef __array
25 #define __array(type, item, len)        type    item[len];
26
27 #undef __dynamic_array
28 #define __dynamic_array(type, item, len) unsigned short __data_loc_##item;
29
30 #undef __string
31 #define __string(item, src) __dynamic_array(char, item, -1)
32
33 #undef TP_STRUCT__entry
34 #define TP_STRUCT__entry(args...) args
35
36 #undef TRACE_EVENT
37 #define TRACE_EVENT(name, proto, args, tstruct, assign, print)  \
38         struct ftrace_raw_##name {                              \
39                 struct trace_entry      ent;                    \
40                 tstruct                                         \
41                 char                    __data[0];              \
42         };                                                      \
43         static struct ftrace_event_call event_##name
44
45 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
46
47
48 /*
49  * Stage 2 of the trace events.
50  *
51  * Include the following:
52  *
53  * struct ftrace_data_offsets_<call> {
54  *      int                             <item1>;
55  *      int                             <item2>;
56  *      [...]
57  * };
58  *
59  * The __dynamic_array() macro will create each int <item>, this is
60  * to keep the offset of each array from the beginning of the event.
61  */
62
63 #undef __field
64 #define __field(type, item);
65
66 #undef __array
67 #define __array(type, item, len)
68
69 #undef __dynamic_array
70 #define __dynamic_array(type, item, len)        int item;
71
72 #undef __string
73 #define __string(item, src) __dynamic_array(char, item, -1)
74
75 #undef TRACE_EVENT
76 #define TRACE_EVENT(call, proto, args, tstruct, assign, print)          \
77         struct ftrace_data_offsets_##call {                             \
78                 tstruct;                                                \
79         };
80
81 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
82
83 /*
84  * Stage 3 of the trace events.
85  *
86  * Override the macros in <trace/trace_events.h> to include the following:
87  *
88  * enum print_line_t
89  * ftrace_raw_output_<call>(struct trace_iterator *iter, int flags)
90  * {
91  *      struct trace_seq *s = &iter->seq;
92  *      struct ftrace_raw_<call> *field; <-- defined in stage 1
93  *      struct trace_entry *entry;
94  *      struct trace_seq *p;
95  *      int ret;
96  *
97  *      entry = iter->ent;
98  *
99  *      if (entry->type != event_<call>.id) {
100  *              WARN_ON_ONCE(1);
101  *              return TRACE_TYPE_UNHANDLED;
102  *      }
103  *
104  *      field = (typeof(field))entry;
105  *
106  *      p = get_cpu_var(ftrace_event_seq);
107  *      trace_seq_init(p);
108  *      ret = trace_seq_printf(s, <TP_printk> "\n");
109  *      put_cpu();
110  *      if (!ret)
111  *              return TRACE_TYPE_PARTIAL_LINE;
112  *
113  *      return TRACE_TYPE_HANDLED;
114  * }
115  *
116  * This is the method used to print the raw event to the trace
117  * output format. Note, this is not needed if the data is read
118  * in binary.
119  */
120
121 #undef __entry
122 #define __entry field
123
124 #undef TP_printk
125 #define TP_printk(fmt, args...) fmt "\n", args
126
127 #undef __get_dynamic_array
128 #define __get_dynamic_array(field)      \
129                 ((void *)__entry + __entry->__data_loc_##field)
130
131 #undef __get_str
132 #define __get_str(field) (char *)__get_dynamic_array(field)
133
134 #undef __print_flags
135 #define __print_flags(flag, delim, flag_array...)                       \
136         ({                                                              \
137                 static const struct trace_print_flags flags[] =         \
138                         { flag_array, { -1, NULL }};                    \
139                 ftrace_print_flags_seq(p, delim, flag, flags);          \
140         })
141
142 #undef __print_symbolic
143 #define __print_symbolic(value, symbol_array...)                        \
144         ({                                                              \
145                 static const struct trace_print_flags symbols[] =       \
146                         { symbol_array, { -1, NULL }};                  \
147                 ftrace_print_symbols_seq(p, value, symbols);            \
148         })
149
150 #undef TRACE_EVENT
151 #define TRACE_EVENT(call, proto, args, tstruct, assign, print)          \
152 enum print_line_t                                                       \
153 ftrace_raw_output_##call(struct trace_iterator *iter, int flags)        \
154 {                                                                       \
155         struct trace_seq *s = &iter->seq;                               \
156         struct ftrace_raw_##call *field;                                \
157         struct trace_entry *entry;                                      \
158         struct trace_seq *p;                                            \
159         int ret;                                                        \
160                                                                         \
161         entry = iter->ent;                                              \
162                                                                         \
163         if (entry->type != event_##call.id) {                           \
164                 WARN_ON_ONCE(1);                                        \
165                 return TRACE_TYPE_UNHANDLED;                            \
166         }                                                               \
167                                                                         \
168         field = (typeof(field))entry;                                   \
169                                                                         \
170         p = &get_cpu_var(ftrace_event_seq);                             \
171         trace_seq_init(p);                                              \
172         ret = trace_seq_printf(s, #call ": " print);                    \
173         put_cpu();                                                      \
174         if (!ret)                                                       \
175                 return TRACE_TYPE_PARTIAL_LINE;                         \
176                                                                         \
177         return TRACE_TYPE_HANDLED;                                      \
178 }
179         
180 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
181
182 /*
183  * Setup the showing format of trace point.
184  *
185  * int
186  * ftrace_format_##call(struct trace_seq *s)
187  * {
188  *      struct ftrace_raw_##call field;
189  *      int ret;
190  *
191  *      ret = trace_seq_printf(s, #type " " #item ";"
192  *                             " offset:%u; size:%u;\n",
193  *                             offsetof(struct ftrace_raw_##call, item),
194  *                             sizeof(field.type));
195  *
196  * }
197  */
198
199 #undef TP_STRUCT__entry
200 #define TP_STRUCT__entry(args...) args
201
202 #undef __field
203 #define __field(type, item)                                     \
204         ret = trace_seq_printf(s, "\tfield:" #type " " #item ";\t"      \
205                                "offset:%u;\tsize:%u;\n",                \
206                                (unsigned int)offsetof(typeof(field), item), \
207                                (unsigned int)sizeof(field.item));       \
208         if (!ret)                                                       \
209                 return 0;
210
211 #undef __array
212 #define __array(type, item, len)                                                \
213         ret = trace_seq_printf(s, "\tfield:" #type " " #item "[" #len "];\t"    \
214                                "offset:%u;\tsize:%u;\n",                \
215                                (unsigned int)offsetof(typeof(field), item), \
216                                (unsigned int)sizeof(field.item));       \
217         if (!ret)                                                       \
218                 return 0;
219
220 #undef __dynamic_array
221 #define __dynamic_array(type, item, len)                                       \
222         ret = trace_seq_printf(s, "\tfield:__data_loc " #item ";\t"            \
223                                "offset:%u;\tsize:%u;\n",                       \
224                                (unsigned int)offsetof(typeof(field),           \
225                                         __data_loc_##item),                    \
226                                (unsigned int)sizeof(field.__data_loc_##item)); \
227         if (!ret)                                                              \
228                 return 0;
229
230 #undef __string
231 #define __string(item, src) __dynamic_array(char, item, -1)
232
233 #undef __entry
234 #define __entry REC
235
236 #undef TP_printk
237 #define TP_printk(fmt, args...) "%s, %s\n", #fmt, __stringify(args)
238
239 #undef TP_fast_assign
240 #define TP_fast_assign(args...) args
241
242 #undef TRACE_EVENT
243 #define TRACE_EVENT(call, proto, args, tstruct, func, print)            \
244 static int                                                              \
245 ftrace_format_##call(struct trace_seq *s)                               \
246 {                                                                       \
247         struct ftrace_raw_##call field __attribute__((unused));         \
248         int ret = 0;                                                    \
249                                                                         \
250         tstruct;                                                        \
251                                                                         \
252         trace_seq_printf(s, "\nprint fmt: " print);                     \
253                                                                         \
254         return ret;                                                     \
255 }
256
257 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
258
259 #undef __field
260 #define __field(type, item)                                             \
261         ret = trace_define_field(event_call, #type, #item,              \
262                                  offsetof(typeof(field), item),         \
263                                  sizeof(field.item), is_signed_type(type));     \
264         if (ret)                                                        \
265                 return ret;
266
267 #undef __array
268 #define __array(type, item, len)                                        \
269         BUILD_BUG_ON(len > MAX_FILTER_STR_VAL);                         \
270         ret = trace_define_field(event_call, #type "[" #len "]", #item, \
271                                  offsetof(typeof(field), item),         \
272                                  sizeof(field.item), 0);                \
273         if (ret)                                                        \
274                 return ret;
275
276 #undef __dynamic_array
277 #define __dynamic_array(type, item, len)                                       \
278         ret = trace_define_field(event_call, "__data_loc" "[" #type "]", #item,\
279                                 offsetof(typeof(field), __data_loc_##item),    \
280                                  sizeof(field.__data_loc_##item), 0);
281
282 #undef __string
283 #define __string(item, src) __dynamic_array(char, item, -1)
284
285 #undef TRACE_EVENT
286 #define TRACE_EVENT(call, proto, args, tstruct, func, print)            \
287 int                                                                     \
288 ftrace_define_fields_##call(void)                                       \
289 {                                                                       \
290         struct ftrace_raw_##call field;                                 \
291         struct ftrace_event_call *event_call = &event_##call;           \
292         int ret;                                                        \
293                                                                         \
294         __common_field(int, type, 1);                                   \
295         __common_field(unsigned char, flags, 0);                        \
296         __common_field(unsigned char, preempt_count, 0);                \
297         __common_field(int, pid, 1);                                    \
298         __common_field(int, tgid, 1);                                   \
299                                                                         \
300         tstruct;                                                        \
301                                                                         \
302         return ret;                                                     \
303 }
304
305 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
306
307 /*
308  * remember the offset of each array from the beginning of the event.
309  */
310
311 #undef __entry
312 #define __entry entry
313
314 #undef __field
315 #define __field(type, item)
316
317 #undef __array
318 #define __array(type, item, len)
319
320 #undef __dynamic_array
321 #define __dynamic_array(type, item, len)                                \
322         __data_offsets->item = __data_size +                            \
323                                offsetof(typeof(*entry), __data);        \
324         __data_size += (len) * sizeof(type);
325
326 #undef __string
327 #define __string(item, src) __dynamic_array(char, item, strlen(src) + 1)       \
328
329 #undef TRACE_EVENT
330 #define TRACE_EVENT(call, proto, args, tstruct, assign, print)          \
331 static inline int ftrace_get_offsets_##call(                            \
332         struct ftrace_data_offsets_##call *__data_offsets, proto)       \
333 {                                                                       \
334         int __data_size = 0;                                            \
335         struct ftrace_raw_##call __maybe_unused *entry;                 \
336                                                                         \
337         tstruct;                                                        \
338                                                                         \
339         return __data_size;                                             \
340 }
341
342 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
343
344 /*
345  * Stage 4 of the trace events.
346  *
347  * Override the macros in <trace/trace_events.h> to include the following:
348  *
349  * static void ftrace_event_<call>(proto)
350  * {
351  *      event_trace_printk(_RET_IP_, "<call>: " <fmt>);
352  * }
353  *
354  * static int ftrace_reg_event_<call>(void)
355  * {
356  *      int ret;
357  *
358  *      ret = register_trace_<call>(ftrace_event_<call>);
359  *      if (!ret)
360  *              pr_info("event trace: Could not activate trace point "
361  *                      "probe to  <call>");
362  *      return ret;
363  * }
364  *
365  * static void ftrace_unreg_event_<call>(void)
366  * {
367  *      unregister_trace_<call>(ftrace_event_<call>);
368  * }
369  *
370  *
371  * For those macros defined with TRACE_EVENT:
372  *
373  * static struct ftrace_event_call event_<call>;
374  *
375  * static void ftrace_raw_event_<call>(proto)
376  * {
377  *      struct ring_buffer_event *event;
378  *      struct ftrace_raw_<call> *entry; <-- defined in stage 1
379  *      unsigned long irq_flags;
380  *      int pc;
381  *
382  *      local_save_flags(irq_flags);
383  *      pc = preempt_count();
384  *
385  *      event = trace_current_buffer_lock_reserve(event_<call>.id,
386  *                                sizeof(struct ftrace_raw_<call>),
387  *                                irq_flags, pc);
388  *      if (!event)
389  *              return;
390  *      entry   = ring_buffer_event_data(event);
391  *
392  *      <assign>;  <-- Here we assign the entries by the __field and
393  *                      __array macros.
394  *
395  *      trace_current_buffer_unlock_commit(event, irq_flags, pc);
396  * }
397  *
398  * static int ftrace_raw_reg_event_<call>(void)
399  * {
400  *      int ret;
401  *
402  *      ret = register_trace_<call>(ftrace_raw_event_<call>);
403  *      if (!ret)
404  *              pr_info("event trace: Could not activate trace point "
405  *                      "probe to <call>");
406  *      return ret;
407  * }
408  *
409  * static void ftrace_unreg_event_<call>(void)
410  * {
411  *      unregister_trace_<call>(ftrace_raw_event_<call>);
412  * }
413  *
414  * static struct trace_event ftrace_event_type_<call> = {
415  *      .trace                  = ftrace_raw_output_<call>, <-- stage 2
416  * };
417  *
418  * static int ftrace_raw_init_event_<call>(void)
419  * {
420  *      int id;
421  *
422  *      id = register_ftrace_event(&ftrace_event_type_<call>);
423  *      if (!id)
424  *              return -ENODEV;
425  *      event_<call>.id = id;
426  *      return 0;
427  * }
428  *
429  * static struct ftrace_event_call __used
430  * __attribute__((__aligned__(4)))
431  * __attribute__((section("_ftrace_events"))) event_<call> = {
432  *      .name                   = "<call>",
433  *      .system                 = "<system>",
434  *      .raw_init               = ftrace_raw_init_event_<call>,
435  *      .regfunc                = ftrace_reg_event_<call>,
436  *      .unregfunc              = ftrace_unreg_event_<call>,
437  *      .show_format            = ftrace_format_<call>,
438  * }
439  *
440  */
441
442 #undef TP_FMT
443 #define TP_FMT(fmt, args...)    fmt "\n", ##args
444
445 #ifdef CONFIG_EVENT_PROFILE
446 #define _TRACE_PROFILE(call, proto, args)                               \
447 static void ftrace_profile_##call(proto)                                \
448 {                                                                       \
449         extern void perf_tpcounter_event(int);                          \
450         perf_tpcounter_event(event_##call.id);                          \
451 }                                                                       \
452                                                                         \
453 static int ftrace_profile_enable_##call(struct ftrace_event_call *event_call) \
454 {                                                                       \
455         int ret = 0;                                                    \
456                                                                         \
457         if (!atomic_inc_return(&event_call->profile_count))             \
458                 ret = register_trace_##call(ftrace_profile_##call);     \
459                                                                         \
460         return ret;                                                     \
461 }                                                                       \
462                                                                         \
463 static void ftrace_profile_disable_##call(struct ftrace_event_call *event_call)\
464 {                                                                       \
465         if (atomic_add_negative(-1, &event_call->profile_count))        \
466                 unregister_trace_##call(ftrace_profile_##call);         \
467 }
468
469 #define _TRACE_PROFILE_INIT(call)                                       \
470         .profile_count = ATOMIC_INIT(-1),                               \
471         .profile_enable = ftrace_profile_enable_##call,                 \
472         .profile_disable = ftrace_profile_disable_##call,
473
474 #else
475 #define _TRACE_PROFILE(call, proto, args)
476 #define _TRACE_PROFILE_INIT(call)
477 #endif
478
479 #undef __entry
480 #define __entry entry
481
482 #undef __field
483 #define __field(type, item)
484
485 #undef __array
486 #define __array(type, item, len)
487
488 #undef __dynamic_array
489 #define __dynamic_array(type, item, len)                                \
490         __entry->__data_loc_##item = __data_offsets.item;
491
492 #undef __string
493 #define __string(item, src) __dynamic_array(char, item, -1)             \
494
495 #undef __assign_str
496 #define __assign_str(dst, src)                                          \
497         strcpy(__get_str(dst), src);
498
499 #undef TRACE_EVENT
500 #define TRACE_EVENT(call, proto, args, tstruct, assign, print)          \
501 _TRACE_PROFILE(call, PARAMS(proto), PARAMS(args))                       \
502                                                                         \
503 static struct ftrace_event_call event_##call;                           \
504                                                                         \
505 static void ftrace_raw_event_##call(proto)                              \
506 {                                                                       \
507         struct ftrace_data_offsets_##call __maybe_unused __data_offsets;\
508         struct ftrace_event_call *event_call = &event_##call;           \
509         struct ring_buffer_event *event;                                \
510         struct ftrace_raw_##call *entry;                                \
511         unsigned long irq_flags;                                        \
512         int __data_size;                                                \
513         int pc;                                                         \
514                                                                         \
515         local_save_flags(irq_flags);                                    \
516         pc = preempt_count();                                           \
517                                                                         \
518         __data_size = ftrace_get_offsets_##call(&__data_offsets, args); \
519                                                                         \
520         event = trace_current_buffer_lock_reserve(event_##call.id,      \
521                                  sizeof(*entry) + __data_size,          \
522                                  irq_flags, pc);                        \
523         if (!event)                                                     \
524                 return;                                                 \
525         entry   = ring_buffer_event_data(event);                        \
526                                                                         \
527                                                                         \
528         tstruct                                                         \
529                                                                         \
530         { assign; }                                                     \
531                                                                         \
532         if (!filter_current_check_discard(event_call, entry, event))    \
533                 trace_nowake_buffer_unlock_commit(event, irq_flags, pc); \
534 }                                                                       \
535                                                                         \
536 static int ftrace_raw_reg_event_##call(void)                            \
537 {                                                                       \
538         int ret;                                                        \
539                                                                         \
540         ret = register_trace_##call(ftrace_raw_event_##call);           \
541         if (ret)                                                        \
542                 pr_info("event trace: Could not activate trace point "  \
543                         "probe to " #call "\n");                        \
544         return ret;                                                     \
545 }                                                                       \
546                                                                         \
547 static void ftrace_raw_unreg_event_##call(void)                         \
548 {                                                                       \
549         unregister_trace_##call(ftrace_raw_event_##call);               \
550 }                                                                       \
551                                                                         \
552 static struct trace_event ftrace_event_type_##call = {                  \
553         .trace                  = ftrace_raw_output_##call,             \
554 };                                                                      \
555                                                                         \
556 static int ftrace_raw_init_event_##call(void)                           \
557 {                                                                       \
558         int id;                                                         \
559                                                                         \
560         id = register_ftrace_event(&ftrace_event_type_##call);          \
561         if (!id)                                                        \
562                 return -ENODEV;                                         \
563         event_##call.id = id;                                           \
564         INIT_LIST_HEAD(&event_##call.fields);                           \
565         init_preds(&event_##call);                                      \
566         return 0;                                                       \
567 }                                                                       \
568                                                                         \
569 static struct ftrace_event_call __used                                  \
570 __attribute__((__aligned__(4)))                                         \
571 __attribute__((section("_ftrace_events"))) event_##call = {             \
572         .name                   = #call,                                \
573         .system                 = __stringify(TRACE_SYSTEM),            \
574         .event                  = &ftrace_event_type_##call,            \
575         .raw_init               = ftrace_raw_init_event_##call,         \
576         .regfunc                = ftrace_raw_reg_event_##call,          \
577         .unregfunc              = ftrace_raw_unreg_event_##call,        \
578         .show_format            = ftrace_format_##call,                 \
579         .define_fields          = ftrace_define_fields_##call,          \
580         _TRACE_PROFILE_INIT(call)                                       \
581 }
582
583 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
584
585 #undef _TRACE_PROFILE
586 #undef _TRACE_PROFILE_INIT
587