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