blob: 9276ec4f34de0c9bb66abdb0ea54f0b0a7791ff9 [file] [log] [blame]
Steven Rostedtf42c85e2009-04-13 12:25:37 -04001/*
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
Steven Rostedtf42c85e2009-04-13 12:25:37 -040021#undef __array
22#define __array(type, item, len) type item[len];
23
24#undef __field
25#define __field(type, item) type item;
26
Frederic Weisbecker9cbf1172009-04-19 04:51:29 +020027#undef __string
Li Zefanb0aae682009-05-21 13:59:18 +080028#define __string(item, src) unsigned short __str_loc_##item;
Frederic Weisbecker9cbf1172009-04-19 04:51:29 +020029
Steven Rostedtf42c85e2009-04-13 12:25:37 -040030#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 \
Frederic Weisbecker9cbf1172009-04-19 04:51:29 +020038 char __str_data[0]; \
Steven Rostedtf42c85e2009-04-13 12:25:37 -040039 }; \
40 static struct ftrace_event_call event_##name
41
42#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
43
Frederic Weisbecker9cbf1172009-04-19 04:51:29 +020044
Steven Rostedtf42c85e2009-04-13 12:25:37 -040045/*
46 * Stage 2 of the trace events.
47 *
Frederic Weisbecker9cbf1172009-04-19 04:51:29 +020048 * 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
Frederic Weisbecker9cbf1172009-04-19 04:51:29 +020062#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 *
Steven Rostedtf42c85e2009-04-13 12:25:37 -040082 * 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;
Steven Rostedtbe74b732009-05-26 20:25:22 +020090 * struct trace_seq *p;
Steven Rostedtf42c85e2009-04-13 12:25:37 -040091 * 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 *
Steven Rostedtbe74b732009-05-26 20:25:22 +0200102 * p = get_cpu_var(ftrace_event_seq);
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400103 * ret = trace_seq_printf(s, <TP_printk> "\n");
Steven Rostedtbe74b732009-05-26 20:25:22 +0200104 * put_cpu();
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400105 * 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
Frederic Weisbecker9cbf1172009-04-19 04:51:29 +0200122#undef __get_str
Frederic Weisbecker6a74aa42009-04-22 00:41:09 +0200123#define __get_str(field) ((char *)__entry + __entry->__str_loc_##field)
Frederic Weisbecker9cbf1172009-04-19 04:51:29 +0200124
Steven Rostedtbe74b732009-05-26 20:25:22 +0200125#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
Steven Rostedt0f4fc292009-05-20 19:21:47 -0400133#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
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400141#undef TRACE_EVENT
142#define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
143enum print_line_t \
144ftrace_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; \
Steven Rostedtbe74b732009-05-26 20:25:22 +0200149 struct trace_seq *p; \
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400150 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 \
Steven Rostedtbe74b732009-05-26 20:25:22 +0200161 p = &get_cpu_var(ftrace_event_seq); \
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400162 ret = trace_seq_printf(s, #call ": " print); \
Steven Rostedtbe74b732009-05-26 20:25:22 +0200163 put_cpu(); \
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400164 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
Frederic Weisbecker9cbf1172009-04-19 04:51:29 +0200210#undef __string
211#define __string(item, src) \
Li Zefan6e25db42009-05-29 11:24:59 +0800212 ret = trace_seq_printf(s, "\tfield:__str_loc " #item ";\t" \
213 "offset:%u;\tsize:%u;\n", \
Frederic Weisbecker9cbf1172009-04-19 04:51:29 +0200214 (unsigned int)offsetof(typeof(field), \
215 __str_loc_##item), \
216 (unsigned int)sizeof(field.__str_loc_##item)); \
217 if (!ret) \
218 return 0;
219
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400220#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) \
231static int \
232ftrace_format_##call(struct trace_seq *s) \
233{ \
Jeremy Fitzhardinge76aa8112009-04-16 23:35:39 -0700234 struct ftrace_raw_##call field __attribute__((unused)); \
235 int ret = 0; \
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400236 \
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), \
Tom Zanussia118e4d2009-04-28 03:04:53 -0500250 sizeof(field.item), is_signed_type(type)); \
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400251 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), \
Tom Zanussia118e4d2009-04-28 03:04:53 -0500259 sizeof(field.item), 0); \
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400260 if (ret) \
261 return ret;
262
Frederic Weisbecker9cbf1172009-04-19 04:51:29 +0200263#undef __string
264#define __string(item, src) \
265 ret = trace_define_field(event_call, "__str_loc", #item, \
266 offsetof(typeof(field), __str_loc_##item), \
Tom Zanussia118e4d2009-04-28 03:04:53 -0500267 sizeof(field.__str_loc_##item), 0);
Frederic Weisbecker9cbf1172009-04-19 04:51:29 +0200268
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400269#undef TRACE_EVENT
270#define TRACE_EVENT(call, proto, args, tstruct, func, print) \
271int \
272ftrace_define_fields_##call(void) \
273{ \
274 struct ftrace_raw_##call field; \
275 struct ftrace_event_call *event_call = &event_##call; \
276 int ret; \
277 \
Tom Zanussia118e4d2009-04-28 03:04:53 -0500278 __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); \
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400283 \
284 tstruct; \
285 \
286 return ret; \
287}
288
289#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
290
291/*
Frederic Weisbecker9cbf1172009-04-19 04:51:29 +0200292 * Stage 4 of the trace events.
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400293 *
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 *
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400317 *
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) \
394static void ftrace_profile_##call(proto) \
395{ \
396 extern void perf_tpcounter_event(int); \
397 perf_tpcounter_event(event_##call.id); \
398} \
399 \
Zhaoleif2aebae2009-05-27 21:36:02 +0800400static int ftrace_profile_enable_##call(struct ftrace_event_call *event_call) \
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400401{ \
402 int ret = 0; \
403 \
Zhaoleif2aebae2009-05-27 21:36:02 +0800404 if (!atomic_inc_return(&event_call->profile_count)) \
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400405 ret = register_trace_##call(ftrace_profile_##call); \
406 \
407 return ret; \
408} \
409 \
Zhaoleif2aebae2009-05-27 21:36:02 +0800410static void ftrace_profile_disable_##call(struct ftrace_event_call *event_call)\
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400411{ \
Zhaoleif2aebae2009-05-27 21:36:02 +0800412 if (atomic_add_negative(-1, &event_call->profile_count)) \
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400413 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
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400426#undef __entry
427#define __entry entry
428
Frederic Weisbecker9cbf1172009-04-19 04:51:29 +0200429#undef __field
430#define __field(type, item)
431
432#undef __array
433#define __array(type, item, len)
434
435#undef __string
Zhaoleif2aebae2009-05-27 21:36:02 +0800436#define __string(item, src) \
437 __str_offsets.item = __str_size + \
438 offsetof(typeof(*entry), __str_data); \
Frederic Weisbecker9cbf1172009-04-19 04:51:29 +0200439 __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
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400446#undef TRACE_EVENT
447#define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
448_TRACE_PROFILE(call, PARAMS(proto), PARAMS(args)) \
449 \
450static struct ftrace_event_call event_##call; \
451 \
452static void ftrace_raw_event_##call(proto) \
453{ \
Zhaoleif2aebae2009-05-27 21:36:02 +0800454 struct ftrace_str_offsets_##call __maybe_unused __str_offsets; \
455 struct ftrace_event_call *event_call = &event_##call; \
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400456 struct ring_buffer_event *event; \
457 struct ftrace_raw_##call *entry; \
458 unsigned long irq_flags; \
Frederic Weisbecker9cbf1172009-04-19 04:51:29 +0200459 int __str_size = 0; \
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400460 int pc; \
461 \
462 local_save_flags(irq_flags); \
463 pc = preempt_count(); \
464 \
Frederic Weisbecker9cbf1172009-04-19 04:51:29 +0200465 tstruct; \
466 \
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400467 event = trace_current_buffer_lock_reserve(event_##call.id, \
Frederic Weisbecker9cbf1172009-04-19 04:51:29 +0200468 sizeof(struct ftrace_raw_##call) + __str_size,\
469 irq_flags, pc); \
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400470 if (!event) \
471 return; \
472 entry = ring_buffer_event_data(event); \
473 \
474 assign; \
475 \
Zhaoleif2aebae2009-05-27 21:36:02 +0800476 if (!filter_current_check_discard(event_call, entry, event)) \
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400477 trace_nowake_buffer_unlock_commit(event, irq_flags, pc); \
478} \
479 \
480static 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 \
491static void ftrace_raw_unreg_event_##call(void) \
492{ \
493 unregister_trace_##call(ftrace_raw_event_##call); \
494} \
495 \
496static struct trace_event ftrace_event_type_##call = { \
497 .trace = ftrace_raw_output_##call, \
498}; \
499 \
500static 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 \
513static struct ftrace_event_call __used \
514__attribute__((__aligned__(4))) \
515__attribute__((section("_ftrace_events"))) event_##call = { \
516 .name = #call, \
517 .system = __stringify(TRACE_SYSTEM), \
Steven Rostedt6d723732009-04-10 14:53:50 -0400518 .event = &ftrace_event_type_##call, \
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400519 .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