blob: edb02bc9f8fffe0afa208095c83b562cf90960ba [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
28#define __string(item, src) int __str_loc_##item;
29
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;
90 * int ret;
91 *
92 * entry = iter->ent;
93 *
94 * if (entry->type != event_<call>.id) {
95 * WARN_ON_ONCE(1);
96 * return TRACE_TYPE_UNHANDLED;
97 * }
98 *
99 * field = (typeof(field))entry;
100 *
101 * ret = trace_seq_printf(s, <TP_printk> "\n");
102 * if (!ret)
103 * return TRACE_TYPE_PARTIAL_LINE;
104 *
105 * return TRACE_TYPE_HANDLED;
106 * }
107 *
108 * This is the method used to print the raw event to the trace
109 * output format. Note, this is not needed if the data is read
110 * in binary.
111 */
112
113#undef __entry
114#define __entry field
115
116#undef TP_printk
117#define TP_printk(fmt, args...) fmt "\n", args
118
Frederic Weisbecker9cbf1172009-04-19 04:51:29 +0200119#undef __get_str
Frederic Weisbecker6a74aa42009-04-22 00:41:09 +0200120#define __get_str(field) ((char *)__entry + __entry->__str_loc_##field)
Frederic Weisbecker9cbf1172009-04-19 04:51:29 +0200121
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400122#undef TRACE_EVENT
123#define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
124enum print_line_t \
125ftrace_raw_output_##call(struct trace_iterator *iter, int flags) \
126{ \
127 struct trace_seq *s = &iter->seq; \
128 struct ftrace_raw_##call *field; \
129 struct trace_entry *entry; \
130 int ret; \
131 \
132 entry = iter->ent; \
133 \
134 if (entry->type != event_##call.id) { \
135 WARN_ON_ONCE(1); \
136 return TRACE_TYPE_UNHANDLED; \
137 } \
138 \
139 field = (typeof(field))entry; \
140 \
141 ret = trace_seq_printf(s, #call ": " print); \
142 if (!ret) \
143 return TRACE_TYPE_PARTIAL_LINE; \
144 \
145 return TRACE_TYPE_HANDLED; \
146}
147
148#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
149
150/*
151 * Setup the showing format of trace point.
152 *
153 * int
154 * ftrace_format_##call(struct trace_seq *s)
155 * {
156 * struct ftrace_raw_##call field;
157 * int ret;
158 *
159 * ret = trace_seq_printf(s, #type " " #item ";"
160 * " offset:%u; size:%u;\n",
161 * offsetof(struct ftrace_raw_##call, item),
162 * sizeof(field.type));
163 *
164 * }
165 */
166
167#undef TP_STRUCT__entry
168#define TP_STRUCT__entry(args...) args
169
170#undef __field
171#define __field(type, item) \
172 ret = trace_seq_printf(s, "\tfield:" #type " " #item ";\t" \
173 "offset:%u;\tsize:%u;\n", \
174 (unsigned int)offsetof(typeof(field), item), \
175 (unsigned int)sizeof(field.item)); \
176 if (!ret) \
177 return 0;
178
179#undef __array
180#define __array(type, item, len) \
181 ret = trace_seq_printf(s, "\tfield:" #type " " #item "[" #len "];\t" \
182 "offset:%u;\tsize:%u;\n", \
183 (unsigned int)offsetof(typeof(field), item), \
184 (unsigned int)sizeof(field.item)); \
185 if (!ret) \
186 return 0;
187
Frederic Weisbecker9cbf1172009-04-19 04:51:29 +0200188#undef __string
189#define __string(item, src) \
190 ret = trace_seq_printf(s, "\tfield: __str_loc " #item ";\t" \
191 "offset:%u;tsize:%u;\n", \
192 (unsigned int)offsetof(typeof(field), \
193 __str_loc_##item), \
194 (unsigned int)sizeof(field.__str_loc_##item)); \
195 if (!ret) \
196 return 0;
197
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400198#undef __entry
199#define __entry REC
200
201#undef TP_printk
202#define TP_printk(fmt, args...) "%s, %s\n", #fmt, __stringify(args)
203
204#undef TP_fast_assign
205#define TP_fast_assign(args...) args
206
207#undef TRACE_EVENT
208#define TRACE_EVENT(call, proto, args, tstruct, func, print) \
209static int \
210ftrace_format_##call(struct trace_seq *s) \
211{ \
Jeremy Fitzhardinge76aa8112009-04-16 23:35:39 -0700212 struct ftrace_raw_##call field __attribute__((unused)); \
213 int ret = 0; \
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400214 \
215 tstruct; \
216 \
217 trace_seq_printf(s, "\nprint fmt: " print); \
218 \
219 return ret; \
220}
221
222#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
223
224#undef __field
225#define __field(type, item) \
226 ret = trace_define_field(event_call, #type, #item, \
227 offsetof(typeof(field), item), \
Tom Zanussia118e4d2009-04-28 03:04:53 -0500228 sizeof(field.item), is_signed_type(type)); \
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400229 if (ret) \
230 return ret;
231
232#undef __array
233#define __array(type, item, len) \
234 BUILD_BUG_ON(len > MAX_FILTER_STR_VAL); \
235 ret = trace_define_field(event_call, #type "[" #len "]", #item, \
236 offsetof(typeof(field), item), \
Tom Zanussia118e4d2009-04-28 03:04:53 -0500237 sizeof(field.item), 0); \
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400238 if (ret) \
239 return ret;
240
Frederic Weisbecker9cbf1172009-04-19 04:51:29 +0200241#undef __string
242#define __string(item, src) \
243 ret = trace_define_field(event_call, "__str_loc", #item, \
244 offsetof(typeof(field), __str_loc_##item), \
Tom Zanussia118e4d2009-04-28 03:04:53 -0500245 sizeof(field.__str_loc_##item), 0);
Frederic Weisbecker9cbf1172009-04-19 04:51:29 +0200246
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400247#undef TRACE_EVENT
248#define TRACE_EVENT(call, proto, args, tstruct, func, print) \
249int \
250ftrace_define_fields_##call(void) \
251{ \
252 struct ftrace_raw_##call field; \
253 struct ftrace_event_call *event_call = &event_##call; \
254 int ret; \
255 \
Tom Zanussia118e4d2009-04-28 03:04:53 -0500256 __common_field(int, type, 1); \
257 __common_field(unsigned char, flags, 0); \
258 __common_field(unsigned char, preempt_count, 0); \
259 __common_field(int, pid, 1); \
260 __common_field(int, tgid, 1); \
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400261 \
262 tstruct; \
263 \
264 return ret; \
265}
266
267#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
268
269/*
Frederic Weisbecker9cbf1172009-04-19 04:51:29 +0200270 * Stage 4 of the trace events.
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400271 *
272 * Override the macros in <trace/trace_events.h> to include the following:
273 *
274 * static void ftrace_event_<call>(proto)
275 * {
276 * event_trace_printk(_RET_IP_, "<call>: " <fmt>);
277 * }
278 *
279 * static int ftrace_reg_event_<call>(void)
280 * {
281 * int ret;
282 *
283 * ret = register_trace_<call>(ftrace_event_<call>);
284 * if (!ret)
285 * pr_info("event trace: Could not activate trace point "
286 * "probe to <call>");
287 * return ret;
288 * }
289 *
290 * static void ftrace_unreg_event_<call>(void)
291 * {
292 * unregister_trace_<call>(ftrace_event_<call>);
293 * }
294 *
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400295 *
296 * For those macros defined with TRACE_EVENT:
297 *
298 * static struct ftrace_event_call event_<call>;
299 *
300 * static void ftrace_raw_event_<call>(proto)
301 * {
302 * struct ring_buffer_event *event;
303 * struct ftrace_raw_<call> *entry; <-- defined in stage 1
304 * unsigned long irq_flags;
305 * int pc;
306 *
307 * local_save_flags(irq_flags);
308 * pc = preempt_count();
309 *
310 * event = trace_current_buffer_lock_reserve(event_<call>.id,
311 * sizeof(struct ftrace_raw_<call>),
312 * irq_flags, pc);
313 * if (!event)
314 * return;
315 * entry = ring_buffer_event_data(event);
316 *
317 * <assign>; <-- Here we assign the entries by the __field and
318 * __array macros.
319 *
320 * trace_current_buffer_unlock_commit(event, irq_flags, pc);
321 * }
322 *
323 * static int ftrace_raw_reg_event_<call>(void)
324 * {
325 * int ret;
326 *
327 * ret = register_trace_<call>(ftrace_raw_event_<call>);
328 * if (!ret)
329 * pr_info("event trace: Could not activate trace point "
330 * "probe to <call>");
331 * return ret;
332 * }
333 *
334 * static void ftrace_unreg_event_<call>(void)
335 * {
336 * unregister_trace_<call>(ftrace_raw_event_<call>);
337 * }
338 *
339 * static struct trace_event ftrace_event_type_<call> = {
340 * .trace = ftrace_raw_output_<call>, <-- stage 2
341 * };
342 *
343 * static int ftrace_raw_init_event_<call>(void)
344 * {
345 * int id;
346 *
347 * id = register_ftrace_event(&ftrace_event_type_<call>);
348 * if (!id)
349 * return -ENODEV;
350 * event_<call>.id = id;
351 * return 0;
352 * }
353 *
354 * static struct ftrace_event_call __used
355 * __attribute__((__aligned__(4)))
356 * __attribute__((section("_ftrace_events"))) event_<call> = {
357 * .name = "<call>",
358 * .system = "<system>",
359 * .raw_init = ftrace_raw_init_event_<call>,
360 * .regfunc = ftrace_reg_event_<call>,
361 * .unregfunc = ftrace_unreg_event_<call>,
362 * .show_format = ftrace_format_<call>,
363 * }
364 *
365 */
366
367#undef TP_FMT
368#define TP_FMT(fmt, args...) fmt "\n", ##args
369
370#ifdef CONFIG_EVENT_PROFILE
371#define _TRACE_PROFILE(call, proto, args) \
372static void ftrace_profile_##call(proto) \
373{ \
374 extern void perf_tpcounter_event(int); \
375 perf_tpcounter_event(event_##call.id); \
376} \
377 \
378static int ftrace_profile_enable_##call(struct ftrace_event_call *call) \
379{ \
380 int ret = 0; \
381 \
382 if (!atomic_inc_return(&call->profile_count)) \
383 ret = register_trace_##call(ftrace_profile_##call); \
384 \
385 return ret; \
386} \
387 \
388static void ftrace_profile_disable_##call(struct ftrace_event_call *call) \
389{ \
390 if (atomic_add_negative(-1, &call->profile_count)) \
391 unregister_trace_##call(ftrace_profile_##call); \
392}
393
394#define _TRACE_PROFILE_INIT(call) \
395 .profile_count = ATOMIC_INIT(-1), \
396 .profile_enable = ftrace_profile_enable_##call, \
397 .profile_disable = ftrace_profile_disable_##call,
398
399#else
400#define _TRACE_PROFILE(call, proto, args)
401#define _TRACE_PROFILE_INIT(call)
402#endif
403
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400404#undef __entry
405#define __entry entry
406
Frederic Weisbecker9cbf1172009-04-19 04:51:29 +0200407#undef __field
408#define __field(type, item)
409
410#undef __array
411#define __array(type, item, len)
412
413#undef __string
414#define __string(item, src) \
415 __str_offsets.item = __str_size + \
416 offsetof(typeof(*entry), __str_data); \
417 __str_size += strlen(src) + 1;
418
419#undef __assign_str
420#define __assign_str(dst, src) \
421 __entry->__str_loc_##dst = __str_offsets.dst; \
422 strcpy(__get_str(dst), src);
423
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400424#undef TRACE_EVENT
425#define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
426_TRACE_PROFILE(call, PARAMS(proto), PARAMS(args)) \
427 \
428static struct ftrace_event_call event_##call; \
429 \
430static void ftrace_raw_event_##call(proto) \
431{ \
Frederic Weisbecker9cbf1172009-04-19 04:51:29 +0200432 struct ftrace_str_offsets_##call __maybe_unused __str_offsets; \
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400433 struct ftrace_event_call *call = &event_##call; \
434 struct ring_buffer_event *event; \
435 struct ftrace_raw_##call *entry; \
436 unsigned long irq_flags; \
Frederic Weisbecker9cbf1172009-04-19 04:51:29 +0200437 int __str_size = 0; \
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400438 int pc; \
439 \
440 local_save_flags(irq_flags); \
441 pc = preempt_count(); \
442 \
Frederic Weisbecker9cbf1172009-04-19 04:51:29 +0200443 tstruct; \
444 \
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400445 event = trace_current_buffer_lock_reserve(event_##call.id, \
Frederic Weisbecker9cbf1172009-04-19 04:51:29 +0200446 sizeof(struct ftrace_raw_##call) + __str_size,\
447 irq_flags, pc); \
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400448 if (!event) \
449 return; \
450 entry = ring_buffer_event_data(event); \
451 \
452 assign; \
453 \
454 if (!filter_current_check_discard(call, entry, event)) \
455 trace_nowake_buffer_unlock_commit(event, irq_flags, pc); \
456} \
457 \
458static int ftrace_raw_reg_event_##call(void) \
459{ \
460 int ret; \
461 \
462 ret = register_trace_##call(ftrace_raw_event_##call); \
463 if (ret) \
464 pr_info("event trace: Could not activate trace point " \
465 "probe to " #call "\n"); \
466 return ret; \
467} \
468 \
469static void ftrace_raw_unreg_event_##call(void) \
470{ \
471 unregister_trace_##call(ftrace_raw_event_##call); \
472} \
473 \
474static struct trace_event ftrace_event_type_##call = { \
475 .trace = ftrace_raw_output_##call, \
476}; \
477 \
478static int ftrace_raw_init_event_##call(void) \
479{ \
480 int id; \
481 \
482 id = register_ftrace_event(&ftrace_event_type_##call); \
483 if (!id) \
484 return -ENODEV; \
485 event_##call.id = id; \
486 INIT_LIST_HEAD(&event_##call.fields); \
487 init_preds(&event_##call); \
488 return 0; \
489} \
490 \
491static struct ftrace_event_call __used \
492__attribute__((__aligned__(4))) \
493__attribute__((section("_ftrace_events"))) event_##call = { \
494 .name = #call, \
495 .system = __stringify(TRACE_SYSTEM), \
Steven Rostedt6d723732009-04-10 14:53:50 -0400496 .event = &ftrace_event_type_##call, \
Steven Rostedtf42c85e2009-04-13 12:25:37 -0400497 .raw_init = ftrace_raw_init_event_##call, \
498 .regfunc = ftrace_raw_reg_event_##call, \
499 .unregfunc = ftrace_raw_unreg_event_##call, \
500 .show_format = ftrace_format_##call, \
501 .define_fields = ftrace_define_fields_##call, \
502 _TRACE_PROFILE_INIT(call) \
503}
504
505#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
506
507#undef _TRACE_PROFILE
508#undef _TRACE_PROFILE_INIT
509