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;
9 * <type2> <item2>[<len>];
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
19 #include <linux/ftrace_event.h>
22 #define __field(type, item) type item;
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;
31 #define __string(item, src) __dynamic_array(char, item, -1)
33 #undef TP_STRUCT__entry
34 #define TP_STRUCT__entry(args...) args
37 #define TRACE_EVENT(name, proto, args, tstruct, assign, print) \
38 struct ftrace_raw_##name { \
39 struct trace_entry ent; \
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> {
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.
64 #define __field(type, item);
67 #define __array(type, item, len)
69 #undef __dynamic_array
70 #define __dynamic_array(type, item, len) int item;
73 #define __string(item, src) __dynamic_array(char, item, -1)
76 #define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
77 struct ftrace_data_offsets_##call { \
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:
89 * ftrace_raw_output_<call>(struct trace_iterator *iter, int flags)
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;
99 * if (entry->type != event_<call>.id) {
101 * return TRACE_TYPE_UNHANDLED;
104 * field = (typeof(field))entry;
106 * p = get_cpu_var(ftrace_event_seq);
108 * ret = trace_seq_printf(s, <TP_printk> "\n");
111 * return TRACE_TYPE_PARTIAL_LINE;
113 * return TRACE_TYPE_HANDLED;
116 * This is the method used to print the raw event to the trace
117 * output format. Note, this is not needed if the data is read
122 #define __entry field
125 #define TP_printk(fmt, args...) fmt "\n", args
127 #undef __get_dynamic_array
128 #define __get_dynamic_array(field) \
129 ((void *)__entry + __entry->__data_loc_##field)
132 #define __get_str(field) (char *)__get_dynamic_array(field)
135 #define __print_flags(flag, delim, flag_array...) \
137 static const struct trace_print_flags flags[] = \
138 { flag_array, { -1, NULL }}; \
139 ftrace_print_flags_seq(p, delim, flag, flags); \
142 #undef __print_symbolic
143 #define __print_symbolic(value, symbol_array...) \
145 static const struct trace_print_flags symbols[] = \
146 { symbol_array, { -1, NULL }}; \
147 ftrace_print_symbols_seq(p, value, symbols); \
151 #define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
153 ftrace_raw_output_##call(struct trace_iterator *iter, int flags) \
155 struct trace_seq *s = &iter->seq; \
156 struct ftrace_raw_##call *field; \
157 struct trace_entry *entry; \
158 struct trace_seq *p; \
163 if (entry->type != event_##call.id) { \
165 return TRACE_TYPE_UNHANDLED; \
168 field = (typeof(field))entry; \
170 p = &get_cpu_var(ftrace_event_seq); \
172 ret = trace_seq_printf(s, #call ": " print); \
175 return TRACE_TYPE_PARTIAL_LINE; \
177 return TRACE_TYPE_HANDLED; \
180 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
183 * Setup the showing format of trace point.
186 * ftrace_format_##call(struct trace_seq *s)
188 * struct ftrace_raw_##call field;
191 * ret = trace_seq_printf(s, #type " " #item ";"
192 * " offset:%u; size:%u;\n",
193 * offsetof(struct ftrace_raw_##call, item),
194 * sizeof(field.type));
199 #undef TP_STRUCT__entry
200 #define TP_STRUCT__entry(args...) args
203 #define __field(type, item) \
204 ret = trace_seq_printf(s, "\tfield:" #type " " #item ";\t" \
205 "offset:%u;\tsize:%u;\n", \
206 (unsigned int)offsetof(typeof(field), item), \
207 (unsigned int)sizeof(field.item)); \
212 #define __array(type, item, len) \
213 ret = trace_seq_printf(s, "\tfield:" #type " " #item "[" #len "];\t" \
214 "offset:%u;\tsize:%u;\n", \
215 (unsigned int)offsetof(typeof(field), item), \
216 (unsigned int)sizeof(field.item)); \
220 #undef __dynamic_array
221 #define __dynamic_array(type, item, len) \
222 ret = trace_seq_printf(s, "\tfield:__data_loc " #item ";\t" \
223 "offset:%u;\tsize:%u;\n", \
224 (unsigned int)offsetof(typeof(field), \
225 __data_loc_##item), \
226 (unsigned int)sizeof(field.__data_loc_##item)); \
231 #define __string(item, src) __dynamic_array(char, item, -1)
237 #define TP_printk(fmt, args...) "%s, %s\n", #fmt, __stringify(args)
239 #undef TP_fast_assign
240 #define TP_fast_assign(args...) args
243 #define TRACE_EVENT(call, proto, args, tstruct, func, print) \
245 ftrace_format_##call(struct trace_seq *s) \
247 struct ftrace_raw_##call field __attribute__((unused)); \
252 trace_seq_printf(s, "\nprint fmt: " print); \
257 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
260 #define __field(type, item) \
261 ret = trace_define_field(event_call, #type, #item, \
262 offsetof(typeof(field), item), \
263 sizeof(field.item), is_signed_type(type)); \
268 #define __array(type, item, len) \
269 BUILD_BUG_ON(len > MAX_FILTER_STR_VAL); \
270 ret = trace_define_field(event_call, #type "[" #len "]", #item, \
271 offsetof(typeof(field), item), \
272 sizeof(field.item), 0); \
276 #undef __dynamic_array
277 #define __dynamic_array(type, item, len) \
278 ret = trace_define_field(event_call, "__data_loc" "[" #type "]", #item,\
279 offsetof(typeof(field), __data_loc_##item), \
280 sizeof(field.__data_loc_##item), 0);
283 #define __string(item, src) __dynamic_array(char, item, -1)
286 #define TRACE_EVENT(call, proto, args, tstruct, func, print) \
288 ftrace_define_fields_##call(void) \
290 struct ftrace_raw_##call field; \
291 struct ftrace_event_call *event_call = &event_##call; \
294 __common_field(int, type, 1); \
295 __common_field(unsigned char, flags, 0); \
296 __common_field(unsigned char, preempt_count, 0); \
297 __common_field(int, pid, 1); \
298 __common_field(int, tgid, 1); \
305 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
308 * remember the offset of each array from the beginning of the event.
312 #define __entry entry
315 #define __field(type, item)
318 #define __array(type, item, len)
320 #undef __dynamic_array
321 #define __dynamic_array(type, item, len) \
322 __data_offsets->item = __data_size + \
323 offsetof(typeof(*entry), __data); \
324 __data_size += (len) * sizeof(type);
327 #define __string(item, src) __dynamic_array(char, item, strlen(src) + 1) \
330 #define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
331 static inline int ftrace_get_offsets_##call( \
332 struct ftrace_data_offsets_##call *__data_offsets, proto) \
334 int __data_size = 0; \
335 struct ftrace_raw_##call __maybe_unused *entry; \
339 return __data_size; \
342 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
345 * Stage 4 of the trace events.
347 * Override the macros in <trace/trace_events.h> to include the following:
349 * static void ftrace_event_<call>(proto)
351 * event_trace_printk(_RET_IP_, "<call>: " <fmt>);
354 * static int ftrace_reg_event_<call>(void)
358 * ret = register_trace_<call>(ftrace_event_<call>);
360 * pr_info("event trace: Could not activate trace point "
361 * "probe to <call>");
365 * static void ftrace_unreg_event_<call>(void)
367 * unregister_trace_<call>(ftrace_event_<call>);
371 * For those macros defined with TRACE_EVENT:
373 * static struct ftrace_event_call event_<call>;
375 * static void ftrace_raw_event_<call>(proto)
377 * struct ring_buffer_event *event;
378 * struct ftrace_raw_<call> *entry; <-- defined in stage 1
379 * unsigned long irq_flags;
382 * local_save_flags(irq_flags);
383 * pc = preempt_count();
385 * event = trace_current_buffer_lock_reserve(event_<call>.id,
386 * sizeof(struct ftrace_raw_<call>),
390 * entry = ring_buffer_event_data(event);
392 * <assign>; <-- Here we assign the entries by the __field and
395 * trace_current_buffer_unlock_commit(event, irq_flags, pc);
398 * static int ftrace_raw_reg_event_<call>(void)
402 * ret = register_trace_<call>(ftrace_raw_event_<call>);
404 * pr_info("event trace: Could not activate trace point "
405 * "probe to <call>");
409 * static void ftrace_unreg_event_<call>(void)
411 * unregister_trace_<call>(ftrace_raw_event_<call>);
414 * static struct trace_event ftrace_event_type_<call> = {
415 * .trace = ftrace_raw_output_<call>, <-- stage 2
418 * static int ftrace_raw_init_event_<call>(void)
422 * id = register_ftrace_event(&ftrace_event_type_<call>);
425 * event_<call>.id = id;
429 * static struct ftrace_event_call __used
430 * __attribute__((__aligned__(4)))
431 * __attribute__((section("_ftrace_events"))) event_<call> = {
433 * .system = "<system>",
434 * .raw_init = ftrace_raw_init_event_<call>,
435 * .regfunc = ftrace_reg_event_<call>,
436 * .unregfunc = ftrace_unreg_event_<call>,
437 * .show_format = ftrace_format_<call>,
443 #define TP_FMT(fmt, args...) fmt "\n", ##args
445 #ifdef CONFIG_EVENT_PROFILE
446 #define _TRACE_PROFILE(call, proto, args) \
447 static void ftrace_profile_##call(proto) \
449 extern void perf_tpcounter_event(int); \
450 perf_tpcounter_event(event_##call.id); \
453 static int ftrace_profile_enable_##call(struct ftrace_event_call *event_call) \
457 if (!atomic_inc_return(&event_call->profile_count)) \
458 ret = register_trace_##call(ftrace_profile_##call); \
463 static void ftrace_profile_disable_##call(struct ftrace_event_call *event_call)\
465 if (atomic_add_negative(-1, &event_call->profile_count)) \
466 unregister_trace_##call(ftrace_profile_##call); \
469 #define _TRACE_PROFILE_INIT(call) \
470 .profile_count = ATOMIC_INIT(-1), \
471 .profile_enable = ftrace_profile_enable_##call, \
472 .profile_disable = ftrace_profile_disable_##call,
475 #define _TRACE_PROFILE(call, proto, args)
476 #define _TRACE_PROFILE_INIT(call)
480 #define __entry entry
483 #define __field(type, item)
486 #define __array(type, item, len)
488 #undef __dynamic_array
489 #define __dynamic_array(type, item, len) \
490 __entry->__data_loc_##item = __data_offsets.item;
493 #define __string(item, src) __dynamic_array(char, item, -1) \
496 #define __assign_str(dst, src) \
497 strcpy(__get_str(dst), src);
500 #define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
501 _TRACE_PROFILE(call, PARAMS(proto), PARAMS(args)) \
503 static struct ftrace_event_call event_##call; \
505 static void ftrace_raw_event_##call(proto) \
507 struct ftrace_data_offsets_##call __maybe_unused __data_offsets;\
508 struct ftrace_event_call *event_call = &event_##call; \
509 struct ring_buffer_event *event; \
510 struct ftrace_raw_##call *entry; \
511 unsigned long irq_flags; \
515 local_save_flags(irq_flags); \
516 pc = preempt_count(); \
518 __data_size = ftrace_get_offsets_##call(&__data_offsets, args); \
520 event = trace_current_buffer_lock_reserve(event_##call.id, \
521 sizeof(*entry) + __data_size, \
525 entry = ring_buffer_event_data(event); \
532 if (!filter_current_check_discard(event_call, entry, event)) \
533 trace_nowake_buffer_unlock_commit(event, irq_flags, pc); \
536 static int ftrace_raw_reg_event_##call(void) \
540 ret = register_trace_##call(ftrace_raw_event_##call); \
542 pr_info("event trace: Could not activate trace point " \
543 "probe to " #call "\n"); \
547 static void ftrace_raw_unreg_event_##call(void) \
549 unregister_trace_##call(ftrace_raw_event_##call); \
552 static struct trace_event ftrace_event_type_##call = { \
553 .trace = ftrace_raw_output_##call, \
556 static int ftrace_raw_init_event_##call(void) \
560 id = register_ftrace_event(&ftrace_event_type_##call); \
563 event_##call.id = id; \
564 INIT_LIST_HEAD(&event_##call.fields); \
565 init_preds(&event_##call); \
569 static struct ftrace_event_call __used \
570 __attribute__((__aligned__(4))) \
571 __attribute__((section("_ftrace_events"))) event_##call = { \
573 .system = __stringify(TRACE_SYSTEM), \
574 .event = &ftrace_event_type_##call, \
575 .raw_init = ftrace_raw_init_event_##call, \
576 .regfunc = ftrace_raw_reg_event_##call, \
577 .unregfunc = ftrace_raw_unreg_event_##call, \
578 .show_format = ftrace_format_##call, \
579 .define_fields = ftrace_define_fields_##call, \
580 _TRACE_PROFILE_INIT(call) \
583 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
585 #undef _TRACE_PROFILE
586 #undef _TRACE_PROFILE_INIT