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