tracing/events: introduce __dynamic_array()
[linux-2.6/x86.git] / include / trace / ftrace.h
blobb5478dab579b77906ec29e51b04876092c14831f
1 /*
2 * Stage 1 of the trace events.
4 * Override the macros in <trace/trace_events.h> to include the following:
6 * struct ftrace_raw_<call> {
7 * struct trace_entry ent;
8 * <type> <item>;
9 * <type2> <item2>[<len>];
10 * [...]
11 * };
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.
19 #include <linux/ftrace_event.h>
21 #undef __field
22 #define __field(type, item) type item;
24 #undef __array
25 #define __array(type, item, len) type item[len];
27 #undef __dynamic_array
28 #define __dynamic_array(type, item, len) unsigned short __data_loc_##item;
30 #undef __string
31 #define __string(item, src) __dynamic_array(char, item, -1)
33 #undef TP_STRUCT__entry
34 #define TP_STRUCT__entry(args...) args
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
45 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
49 * Stage 2 of the trace events.
51 * Include the following:
53 * struct ftrace_data_offsets_<call> {
54 * int <item1>;
55 * int <item2>;
56 * [...]
57 * };
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.
63 #undef __field
64 #define __field(type, item);
66 #undef __array
67 #define __array(type, item, len)
69 #undef __dynamic_array
70 #define __dynamic_array(type, item, len) int item;
72 #undef __string
73 #define __string(item, src) __dynamic_array(char, item, -1)
75 #undef TRACE_EVENT
76 #define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
77 struct ftrace_data_offsets_##call { \
78 tstruct; \
81 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
84 * Stage 3 of the trace events.
86 * Override the macros in <trace/trace_events.h> to include the following:
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;
97 * entry = iter->ent;
99 * if (entry->type != event_<call>.id) {
100 * WARN_ON_ONCE(1);
101 * return TRACE_TYPE_UNHANDLED;
104 * field = (typeof(field))entry;
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;
112 * return TRACE_TYPE_HANDLED;
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.
120 #undef __entry
121 #define __entry field
123 #undef TP_printk
124 #define TP_printk(fmt, args...) fmt "\n", args
126 #undef __get_dynamic_array
127 #define __get_dynamic_array(field) \
128 ((void *)__entry + __entry->__data_loc_##field)
130 #undef __get_str
131 #define __get_str(field) (char *)__get_dynamic_array(field)
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); \
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); \
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) \
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; \
160 entry = iter->ent; \
162 if (entry->type != event_##call.id) { \
163 WARN_ON_ONCE(1); \
164 return TRACE_TYPE_UNHANDLED; \
167 field = (typeof(field))entry; \
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; \
175 return TRACE_TYPE_HANDLED; \
178 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
181 * Setup the showing format of trace point.
183 * int
184 * ftrace_format_##call(struct trace_seq *s)
186 * struct ftrace_raw_##call field;
187 * int ret;
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));
197 #undef TP_STRUCT__entry
198 #define TP_STRUCT__entry(args...) args
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;
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;
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;
228 #undef __string
229 #define __string(item, src) __dynamic_array(char, item, -1)
231 #undef __entry
232 #define __entry REC
234 #undef TP_printk
235 #define TP_printk(fmt, args...) "%s, %s\n", #fmt, __stringify(args)
237 #undef TP_fast_assign
238 #define TP_fast_assign(args...) args
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) \
245 struct ftrace_raw_##call field __attribute__((unused)); \
246 int ret = 0; \
248 tstruct; \
250 trace_seq_printf(s, "\nprint fmt: " print); \
252 return ret; \
255 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
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;
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;
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);
280 #undef __string
281 #define __string(item, src) __dynamic_array(char, item, -1)
283 #undef TRACE_EVENT
284 #define TRACE_EVENT(call, proto, args, tstruct, func, print) \
285 int \
286 ftrace_define_fields_##call(void) \
288 struct ftrace_raw_##call field; \
289 struct ftrace_event_call *event_call = &event_##call; \
290 int ret; \
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); \
298 tstruct; \
300 return ret; \
303 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
306 * remember the offset of each array from the beginning of the event.
309 #undef __entry
310 #define __entry entry
312 #undef __field
313 #define __field(type, item)
315 #undef __array
316 #define __array(type, item, len)
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);
324 #undef __string
325 #define __string(item, src) __dynamic_array(char, item, strlen(src) + 1) \
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) \
332 int __data_size = 0; \
333 struct ftrace_raw_##call __maybe_unused *entry; \
335 tstruct; \
337 return __data_size; \
340 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
343 * Stage 4 of the trace events.
345 * Override the macros in <trace/trace_events.h> to include the following:
347 * static void ftrace_event_<call>(proto)
349 * event_trace_printk(_RET_IP_, "<call>: " <fmt>);
352 * static int ftrace_reg_event_<call>(void)
354 * int ret;
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;
363 * static void ftrace_unreg_event_<call>(void)
365 * unregister_trace_<call>(ftrace_event_<call>);
369 * For those macros defined with TRACE_EVENT:
371 * static struct ftrace_event_call event_<call>;
373 * static void ftrace_raw_event_<call>(proto)
375 * struct ring_buffer_event *event;
376 * struct ftrace_raw_<call> *entry; <-- defined in stage 1
377 * unsigned long irq_flags;
378 * int pc;
380 * local_save_flags(irq_flags);
381 * pc = preempt_count();
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);
390 * <assign>; <-- Here we assign the entries by the __field and
391 * __array macros.
393 * trace_current_buffer_unlock_commit(event, irq_flags, pc);
396 * static int ftrace_raw_reg_event_<call>(void)
398 * int ret;
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;
407 * static void ftrace_unreg_event_<call>(void)
409 * unregister_trace_<call>(ftrace_raw_event_<call>);
412 * static struct trace_event ftrace_event_type_<call> = {
413 * .trace = ftrace_raw_output_<call>, <-- stage 2
414 * };
416 * static int ftrace_raw_init_event_<call>(void)
418 * int id;
420 * id = register_ftrace_event(&ftrace_event_type_<call>);
421 * if (!id)
422 * return -ENODEV;
423 * event_<call>.id = id;
424 * return 0;
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>,
440 #undef TP_FMT
441 #define TP_FMT(fmt, args...) fmt "\n", ##args
443 #ifdef CONFIG_EVENT_PROFILE
444 #define _TRACE_PROFILE(call, proto, args) \
445 static void ftrace_profile_##call(proto) \
447 extern void perf_tpcounter_event(int); \
448 perf_tpcounter_event(event_##call.id); \
451 static int ftrace_profile_enable_##call(struct ftrace_event_call *event_call) \
453 int ret = 0; \
455 if (!atomic_inc_return(&event_call->profile_count)) \
456 ret = register_trace_##call(ftrace_profile_##call); \
458 return ret; \
461 static void ftrace_profile_disable_##call(struct ftrace_event_call *event_call)\
463 if (atomic_add_negative(-1, &event_call->profile_count)) \
464 unregister_trace_##call(ftrace_profile_##call); \
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,
472 #else
473 #define _TRACE_PROFILE(call, proto, args)
474 #define _TRACE_PROFILE_INIT(call)
475 #endif
477 #undef __entry
478 #define __entry entry
480 #undef __field
481 #define __field(type, item)
483 #undef __array
484 #define __array(type, item, len)
486 #undef __dynamic_array
487 #define __dynamic_array(type, item, len) \
488 __entry->__data_loc_##item = __data_offsets.item;
490 #undef __string
491 #define __string(item, src) __dynamic_array(char, item, -1) \
493 #undef __assign_str
494 #define __assign_str(dst, src) \
495 strcpy(__get_str(dst), src);
497 #undef TRACE_EVENT
498 #define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
499 _TRACE_PROFILE(call, PARAMS(proto), PARAMS(args)) \
501 static struct ftrace_event_call event_##call; \
503 static void ftrace_raw_event_##call(proto) \
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; \
513 local_save_flags(irq_flags); \
514 pc = preempt_count(); \
516 __data_size = ftrace_get_offsets_##call(&__data_offsets, args); \
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); \
526 tstruct \
528 { assign; } \
530 if (!filter_current_check_discard(event_call, entry, event)) \
531 trace_nowake_buffer_unlock_commit(event, irq_flags, pc); \
534 static int ftrace_raw_reg_event_##call(void) \
536 int ret; \
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; \
545 static void ftrace_raw_unreg_event_##call(void) \
547 unregister_trace_##call(ftrace_raw_event_##call); \
550 static struct trace_event ftrace_event_type_##call = { \
551 .trace = ftrace_raw_output_##call, \
552 }; \
554 static int ftrace_raw_init_event_##call(void) \
556 int id; \
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; \
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) \
581 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
583 #undef _TRACE_PROFILE
584 #undef _TRACE_PROFILE_INIT