blob: 22c94719c5695cf782c5ddbdc68d5533aba4ac9c [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 Rostedtf42c85e2009-04-13 12:25:37 -0400133#undef TRACE_EVENT
134#define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
135enum print_line_t \
136ftrace_raw_output_##call(struct trace_iterator *iter, int flags) \
137{ \
138 struct trace_seq *s = &iter->seq; \
139 struct ftrace_raw_##call *field; \
140 struct trace_entry *entry; \
Steven Rostedtbe74b732009-05-26 20:25:22 +0200141 struct trace_seq *p; \
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400142 int ret; \
143 \
144 entry = iter->ent; \
145 \
146 if (entry->type != event_##call.id) { \
147 WARN_ON_ONCE(1); \
148 return TRACE_TYPE_UNHANDLED; \
149 } \
150 \
151 field = (typeof(field))entry; \
152 \
Steven Rostedtbe74b732009-05-26 20:25:22 +0200153 p = &get_cpu_var(ftrace_event_seq); \
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400154 ret = trace_seq_printf(s, #call ": " print); \
Steven Rostedtbe74b732009-05-26 20:25:22 +0200155 put_cpu(); \
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400156 if (!ret) \
157 return TRACE_TYPE_PARTIAL_LINE; \
158 \
159 return TRACE_TYPE_HANDLED; \
160}
161
162#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
163
164/*
165 * Setup the showing format of trace point.
166 *
167 * int
168 * ftrace_format_##call(struct trace_seq *s)
169 * {
170 * struct ftrace_raw_##call field;
171 * int ret;
172 *
173 * ret = trace_seq_printf(s, #type " " #item ";"
174 * " offset:%u; size:%u;\n",
175 * offsetof(struct ftrace_raw_##call, item),
176 * sizeof(field.type));
177 *
178 * }
179 */
180
181#undef TP_STRUCT__entry
182#define TP_STRUCT__entry(args...) args
183
184#undef __field
185#define __field(type, item) \
186 ret = trace_seq_printf(s, "\tfield:" #type " " #item ";\t" \
187 "offset:%u;\tsize:%u;\n", \
188 (unsigned int)offsetof(typeof(field), item), \
189 (unsigned int)sizeof(field.item)); \
190 if (!ret) \
191 return 0;
192
193#undef __array
194#define __array(type, item, len) \
195 ret = trace_seq_printf(s, "\tfield:" #type " " #item "[" #len "];\t" \
196 "offset:%u;\tsize:%u;\n", \
197 (unsigned int)offsetof(typeof(field), item), \
198 (unsigned int)sizeof(field.item)); \
199 if (!ret) \
200 return 0;
201
Frederic Weisbecker9cbf1172009-04-19 04:51:29 +0200202#undef __string
203#define __string(item, src) \
204 ret = trace_seq_printf(s, "\tfield: __str_loc " #item ";\t" \
205 "offset:%u;tsize:%u;\n", \
206 (unsigned int)offsetof(typeof(field), \
207 __str_loc_##item), \
208 (unsigned int)sizeof(field.__str_loc_##item)); \
209 if (!ret) \
210 return 0;
211
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400212#undef __entry
213#define __entry REC
214
215#undef TP_printk
216#define TP_printk(fmt, args...) "%s, %s\n", #fmt, __stringify(args)
217
218#undef TP_fast_assign
219#define TP_fast_assign(args...) args
220
221#undef TRACE_EVENT
222#define TRACE_EVENT(call, proto, args, tstruct, func, print) \
223static int \
224ftrace_format_##call(struct trace_seq *s) \
225{ \
Jeremy Fitzhardinge76aa8112009-04-16 23:35:39 -0700226 struct ftrace_raw_##call field __attribute__((unused)); \
227 int ret = 0; \
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400228 \
229 tstruct; \
230 \
231 trace_seq_printf(s, "\nprint fmt: " print); \
232 \
233 return ret; \
234}
235
236#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
237
238#undef __field
239#define __field(type, item) \
240 ret = trace_define_field(event_call, #type, #item, \
241 offsetof(typeof(field), item), \
Tom Zanussia118e4d2009-04-28 03:04:53 -0500242 sizeof(field.item), is_signed_type(type)); \
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400243 if (ret) \
244 return ret;
245
246#undef __array
247#define __array(type, item, len) \
248 BUILD_BUG_ON(len > MAX_FILTER_STR_VAL); \
249 ret = trace_define_field(event_call, #type "[" #len "]", #item, \
250 offsetof(typeof(field), item), \
Tom Zanussia118e4d2009-04-28 03:04:53 -0500251 sizeof(field.item), 0); \
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400252 if (ret) \
253 return ret;
254
Frederic Weisbecker9cbf1172009-04-19 04:51:29 +0200255#undef __string
256#define __string(item, src) \
257 ret = trace_define_field(event_call, "__str_loc", #item, \
258 offsetof(typeof(field), __str_loc_##item), \
Tom Zanussia118e4d2009-04-28 03:04:53 -0500259 sizeof(field.__str_loc_##item), 0);
Frederic Weisbecker9cbf1172009-04-19 04:51:29 +0200260
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400261#undef TRACE_EVENT
262#define TRACE_EVENT(call, proto, args, tstruct, func, print) \
263int \
264ftrace_define_fields_##call(void) \
265{ \
266 struct ftrace_raw_##call field; \
267 struct ftrace_event_call *event_call = &event_##call; \
268 int ret; \
269 \
Tom Zanussia118e4d2009-04-28 03:04:53 -0500270 __common_field(int, type, 1); \
271 __common_field(unsigned char, flags, 0); \
272 __common_field(unsigned char, preempt_count, 0); \
273 __common_field(int, pid, 1); \
274 __common_field(int, tgid, 1); \
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400275 \
276 tstruct; \
277 \
278 return ret; \
279}
280
281#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
282
283/*
Frederic Weisbecker9cbf1172009-04-19 04:51:29 +0200284 * Stage 4 of the trace events.
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400285 *
286 * Override the macros in <trace/trace_events.h> to include the following:
287 *
288 * static void ftrace_event_<call>(proto)
289 * {
290 * event_trace_printk(_RET_IP_, "<call>: " <fmt>);
291 * }
292 *
293 * static int ftrace_reg_event_<call>(void)
294 * {
295 * int ret;
296 *
297 * ret = register_trace_<call>(ftrace_event_<call>);
298 * if (!ret)
299 * pr_info("event trace: Could not activate trace point "
300 * "probe to <call>");
301 * return ret;
302 * }
303 *
304 * static void ftrace_unreg_event_<call>(void)
305 * {
306 * unregister_trace_<call>(ftrace_event_<call>);
307 * }
308 *
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400309 *
310 * For those macros defined with TRACE_EVENT:
311 *
312 * static struct ftrace_event_call event_<call>;
313 *
314 * static void ftrace_raw_event_<call>(proto)
315 * {
316 * struct ring_buffer_event *event;
317 * struct ftrace_raw_<call> *entry; <-- defined in stage 1
318 * unsigned long irq_flags;
319 * int pc;
320 *
321 * local_save_flags(irq_flags);
322 * pc = preempt_count();
323 *
324 * event = trace_current_buffer_lock_reserve(event_<call>.id,
325 * sizeof(struct ftrace_raw_<call>),
326 * irq_flags, pc);
327 * if (!event)
328 * return;
329 * entry = ring_buffer_event_data(event);
330 *
331 * <assign>; <-- Here we assign the entries by the __field and
332 * __array macros.
333 *
334 * trace_current_buffer_unlock_commit(event, irq_flags, pc);
335 * }
336 *
337 * static int ftrace_raw_reg_event_<call>(void)
338 * {
339 * int ret;
340 *
341 * ret = register_trace_<call>(ftrace_raw_event_<call>);
342 * if (!ret)
343 * pr_info("event trace: Could not activate trace point "
344 * "probe to <call>");
345 * return ret;
346 * }
347 *
348 * static void ftrace_unreg_event_<call>(void)
349 * {
350 * unregister_trace_<call>(ftrace_raw_event_<call>);
351 * }
352 *
353 * static struct trace_event ftrace_event_type_<call> = {
354 * .trace = ftrace_raw_output_<call>, <-- stage 2
355 * };
356 *
357 * static int ftrace_raw_init_event_<call>(void)
358 * {
359 * int id;
360 *
361 * id = register_ftrace_event(&ftrace_event_type_<call>);
362 * if (!id)
363 * return -ENODEV;
364 * event_<call>.id = id;
365 * return 0;
366 * }
367 *
368 * static struct ftrace_event_call __used
369 * __attribute__((__aligned__(4)))
370 * __attribute__((section("_ftrace_events"))) event_<call> = {
371 * .name = "<call>",
372 * .system = "<system>",
373 * .raw_init = ftrace_raw_init_event_<call>,
374 * .regfunc = ftrace_reg_event_<call>,
375 * .unregfunc = ftrace_unreg_event_<call>,
376 * .show_format = ftrace_format_<call>,
377 * }
378 *
379 */
380
381#undef TP_FMT
382#define TP_FMT(fmt, args...) fmt "\n", ##args
383
384#ifdef CONFIG_EVENT_PROFILE
385#define _TRACE_PROFILE(call, proto, args) \
386static void ftrace_profile_##call(proto) \
387{ \
388 extern void perf_tpcounter_event(int); \
389 perf_tpcounter_event(event_##call.id); \
390} \
391 \
392static int ftrace_profile_enable_##call(struct ftrace_event_call *call) \
393{ \
394 int ret = 0; \
395 \
396 if (!atomic_inc_return(&call->profile_count)) \
397 ret = register_trace_##call(ftrace_profile_##call); \
398 \
399 return ret; \
400} \
401 \
402static void ftrace_profile_disable_##call(struct ftrace_event_call *call) \
403{ \
404 if (atomic_add_negative(-1, &call->profile_count)) \
405 unregister_trace_##call(ftrace_profile_##call); \
406}
407
408#define _TRACE_PROFILE_INIT(call) \
409 .profile_count = ATOMIC_INIT(-1), \
410 .profile_enable = ftrace_profile_enable_##call, \
411 .profile_disable = ftrace_profile_disable_##call,
412
413#else
414#define _TRACE_PROFILE(call, proto, args)
415#define _TRACE_PROFILE_INIT(call)
416#endif
417
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400418#undef __entry
419#define __entry entry
420
Frederic Weisbecker9cbf1172009-04-19 04:51:29 +0200421#undef __field
422#define __field(type, item)
423
424#undef __array
425#define __array(type, item, len)
426
427#undef __string
428#define __string(item, src) \
429 __str_offsets.item = __str_size + \
430 offsetof(typeof(*entry), __str_data); \
431 __str_size += strlen(src) + 1;
432
433#undef __assign_str
434#define __assign_str(dst, src) \
435 __entry->__str_loc_##dst = __str_offsets.dst; \
436 strcpy(__get_str(dst), src);
437
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400438#undef TRACE_EVENT
439#define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
440_TRACE_PROFILE(call, PARAMS(proto), PARAMS(args)) \
441 \
442static struct ftrace_event_call event_##call; \
443 \
444static void ftrace_raw_event_##call(proto) \
445{ \
Frederic Weisbecker9cbf1172009-04-19 04:51:29 +0200446 struct ftrace_str_offsets_##call __maybe_unused __str_offsets; \
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400447 struct ftrace_event_call *call = &event_##call; \
448 struct ring_buffer_event *event; \
449 struct ftrace_raw_##call *entry; \
450 unsigned long irq_flags; \
Frederic Weisbecker9cbf1172009-04-19 04:51:29 +0200451 int __str_size = 0; \
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400452 int pc; \
453 \
454 local_save_flags(irq_flags); \
455 pc = preempt_count(); \
456 \
Frederic Weisbecker9cbf1172009-04-19 04:51:29 +0200457 tstruct; \
458 \
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400459 event = trace_current_buffer_lock_reserve(event_##call.id, \
Frederic Weisbecker9cbf1172009-04-19 04:51:29 +0200460 sizeof(struct ftrace_raw_##call) + __str_size,\
461 irq_flags, pc); \
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400462 if (!event) \
463 return; \
464 entry = ring_buffer_event_data(event); \
465 \
466 assign; \
467 \
468 if (!filter_current_check_discard(call, entry, event)) \
469 trace_nowake_buffer_unlock_commit(event, irq_flags, pc); \
470} \
471 \
472static int ftrace_raw_reg_event_##call(void) \
473{ \
474 int ret; \
475 \
476 ret = register_trace_##call(ftrace_raw_event_##call); \
477 if (ret) \
478 pr_info("event trace: Could not activate trace point " \
479 "probe to " #call "\n"); \
480 return ret; \
481} \
482 \
483static void ftrace_raw_unreg_event_##call(void) \
484{ \
485 unregister_trace_##call(ftrace_raw_event_##call); \
486} \
487 \
488static struct trace_event ftrace_event_type_##call = { \
489 .trace = ftrace_raw_output_##call, \
490}; \
491 \
492static int ftrace_raw_init_event_##call(void) \
493{ \
494 int id; \
495 \
496 id = register_ftrace_event(&ftrace_event_type_##call); \
497 if (!id) \
498 return -ENODEV; \
499 event_##call.id = id; \
500 INIT_LIST_HEAD(&event_##call.fields); \
501 init_preds(&event_##call); \
502 return 0; \
503} \
504 \
505static struct ftrace_event_call __used \
506__attribute__((__aligned__(4))) \
507__attribute__((section("_ftrace_events"))) event_##call = { \
508 .name = #call, \
509 .system = __stringify(TRACE_SYSTEM), \
Steven Rostedt6d723732009-04-10 14:53:50 -0400510 .event = &ftrace_event_type_##call, \
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400511 .raw_init = ftrace_raw_init_event_##call, \
512 .regfunc = ftrace_raw_reg_event_##call, \
513 .unregfunc = ftrace_raw_unreg_event_##call, \
514 .show_format = ftrace_format_##call, \
515 .define_fields = ftrace_define_fields_##call, \
516 _TRACE_PROFILE_INIT(call) \
517}
518
519#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
520
521#undef _TRACE_PROFILE
522#undef _TRACE_PROFILE_INIT
523