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 __array(type, item, len) type item[len];
25 #define __field(type, item) type item;
28 #define __string(item, src) int __str_loc_##item;
30 #undef TP_STRUCT__entry
31 #define TP_STRUCT__entry(args...) args
34 #define TRACE_EVENT(name, proto, args, tstruct, assign, print) \
35 struct ftrace_raw_##name { \
36 struct trace_entry ent; \
40 static struct ftrace_event_call event_##name
42 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
46 * Stage 2 of the trace events.
48 * Include the following:
50 * struct ftrace_str_offsets_<call> {
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.
63 #define __array(type, item, len)
66 #define __field(type, item);
69 #define __string(item, src) int item;
72 #define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
73 struct ftrace_str_offsets_##call { \
77 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
80 * Stage 3 of the trace events.
82 * Override the macros in <trace/trace_events.h> to include the following:
85 * ftrace_raw_output_<call>(struct trace_iterator *iter, int flags)
87 * struct trace_seq *s = &iter->seq;
88 * struct ftrace_raw_<call> *field; <-- defined in stage 1
89 * struct trace_entry *entry;
94 * if (entry->type != event_<call>.id) {
96 * return TRACE_TYPE_UNHANDLED;
99 * field = (typeof(field))entry;
101 * ret = trace_seq_printf(s, <TP_printk> "\n");
103 * return TRACE_TYPE_PARTIAL_LINE;
105 * return TRACE_TYPE_HANDLED;
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
114 #define __entry field
117 #define TP_printk(fmt, args...) fmt "\n", args
120 #define __get_str(field) ((char *)__entry + __entry->__str_loc_##field)
123 #define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
125 ftrace_raw_output_##call(struct trace_iterator *iter, int flags) \
127 struct trace_seq *s = &iter->seq; \
128 struct ftrace_raw_##call *field; \
129 struct trace_entry *entry; \
134 if (entry->type != event_##call.id) { \
136 return TRACE_TYPE_UNHANDLED; \
139 field = (typeof(field))entry; \
141 ret = trace_seq_printf(s, #call ": " print); \
143 return TRACE_TYPE_PARTIAL_LINE; \
145 return TRACE_TYPE_HANDLED; \
148 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
151 * Setup the showing format of trace point.
154 * ftrace_format_##call(struct trace_seq *s)
156 * struct ftrace_raw_##call field;
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));
167 #undef TP_STRUCT__entry
168 #define TP_STRUCT__entry(args...) args
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)); \
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)); \
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), \
194 (unsigned int)sizeof(field.__str_loc_##item)); \
202 #define TP_printk(fmt, args...) "%s, %s\n", #fmt, __stringify(args)
204 #undef TP_fast_assign
205 #define TP_fast_assign(args...) args
208 #define TRACE_EVENT(call, proto, args, tstruct, func, print) \
210 ftrace_format_##call(struct trace_seq *s) \
212 struct ftrace_raw_##call field __attribute__((unused)); \
217 trace_seq_printf(s, "\nprint fmt: " print); \
222 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
225 #define __field(type, item) \
226 ret = trace_define_field(event_call, #type, #item, \
227 offsetof(typeof(field), item), \
228 sizeof(field.item)); \
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), \
237 sizeof(field.item)); \
242 #define __string(item, src) \
243 ret = trace_define_field(event_call, "__str_loc", #item, \
244 offsetof(typeof(field), __str_loc_##item), \
245 sizeof(field.__str_loc_##item));
248 #define TRACE_EVENT(call, proto, args, tstruct, func, print) \
250 ftrace_define_fields_##call(void) \
252 struct ftrace_raw_##call field; \
253 struct ftrace_event_call *event_call = &event_##call; \
256 __common_field(int, type); \
257 __common_field(unsigned char, flags); \
258 __common_field(unsigned char, preempt_count); \
259 __common_field(int, pid); \
260 __common_field(int, tgid); \
267 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
270 * Stage 4 of the trace events.
272 * Override the macros in <trace/trace_events.h> to include the following:
274 * static void ftrace_event_<call>(proto)
276 * event_trace_printk(_RET_IP_, "<call>: " <fmt>);
279 * static int ftrace_reg_event_<call>(void)
283 * ret = register_trace_<call>(ftrace_event_<call>);
285 * pr_info("event trace: Could not activate trace point "
286 * "probe to <call>");
290 * static void ftrace_unreg_event_<call>(void)
292 * unregister_trace_<call>(ftrace_event_<call>);
296 * For those macros defined with TRACE_EVENT:
298 * static struct ftrace_event_call event_<call>;
300 * static void ftrace_raw_event_<call>(proto)
302 * struct ring_buffer_event *event;
303 * struct ftrace_raw_<call> *entry; <-- defined in stage 1
304 * unsigned long irq_flags;
307 * local_save_flags(irq_flags);
308 * pc = preempt_count();
310 * event = trace_current_buffer_lock_reserve(event_<call>.id,
311 * sizeof(struct ftrace_raw_<call>),
315 * entry = ring_buffer_event_data(event);
317 * <assign>; <-- Here we assign the entries by the __field and
320 * trace_current_buffer_unlock_commit(event, irq_flags, pc);
323 * static int ftrace_raw_reg_event_<call>(void)
327 * ret = register_trace_<call>(ftrace_raw_event_<call>);
329 * pr_info("event trace: Could not activate trace point "
330 * "probe to <call>");
334 * static void ftrace_unreg_event_<call>(void)
336 * unregister_trace_<call>(ftrace_raw_event_<call>);
339 * static struct trace_event ftrace_event_type_<call> = {
340 * .trace = ftrace_raw_output_<call>, <-- stage 2
343 * static int ftrace_raw_init_event_<call>(void)
347 * id = register_ftrace_event(&ftrace_event_type_<call>);
350 * event_<call>.id = id;
354 * static struct ftrace_event_call __used
355 * __attribute__((__aligned__(4)))
356 * __attribute__((section("_ftrace_events"))) event_<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>,
368 #define TP_FMT(fmt, args...) fmt "\n", ##args
370 #ifdef CONFIG_EVENT_PROFILE
371 #define _TRACE_PROFILE(call, proto, args) \
372 static void ftrace_profile_##call(proto) \
374 extern void perf_tpcounter_event(int); \
375 perf_tpcounter_event(event_##call.id); \
378 static int ftrace_profile_enable_##call(struct ftrace_event_call *call) \
382 if (!atomic_inc_return(&call->profile_count)) \
383 ret = register_trace_##call(ftrace_profile_##call); \
388 static void ftrace_profile_disable_##call(struct ftrace_event_call *call) \
390 if (atomic_add_negative(-1, &call->profile_count)) \
391 unregister_trace_##call(ftrace_profile_##call); \
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,
400 #define _TRACE_PROFILE(call, proto, args)
401 #define _TRACE_PROFILE_INIT(call)
405 #define __entry entry
408 #define __field(type, item)
411 #define __array(type, item, len)
414 #define __string(item, src) \
415 __str_offsets.item = __str_size + \
416 offsetof(typeof(*entry), __str_data); \
417 __str_size += strlen(src) + 1;
420 #define __assign_str(dst, src) \
421 __entry->__str_loc_##dst = __str_offsets.dst; \
422 strcpy(__get_str(dst), src);
425 #define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
426 _TRACE_PROFILE(call, PARAMS(proto), PARAMS(args)) \
428 static struct ftrace_event_call event_##call; \
430 static void ftrace_raw_event_##call(proto) \
432 struct ftrace_str_offsets_##call __maybe_unused __str_offsets; \
433 struct ftrace_event_call *call = &event_##call; \
434 struct ring_buffer_event *event; \
435 struct ftrace_raw_##call *entry; \
436 unsigned long irq_flags; \
437 int __str_size = 0; \
440 local_save_flags(irq_flags); \
441 pc = preempt_count(); \
445 event = trace_current_buffer_lock_reserve(event_##call.id, \
446 sizeof(struct ftrace_raw_##call) + __str_size,\
450 entry = ring_buffer_event_data(event); \
454 if (!filter_current_check_discard(call, entry, event)) \
455 trace_nowake_buffer_unlock_commit(event, irq_flags, pc); \
458 static int ftrace_raw_reg_event_##call(void) \
462 ret = register_trace_##call(ftrace_raw_event_##call); \
464 pr_info("event trace: Could not activate trace point " \
465 "probe to " #call "\n"); \
469 static void ftrace_raw_unreg_event_##call(void) \
471 unregister_trace_##call(ftrace_raw_event_##call); \
474 static struct trace_event ftrace_event_type_##call = { \
475 .trace = ftrace_raw_output_##call, \
478 static int ftrace_raw_init_event_##call(void) \
482 id = register_ftrace_event(&ftrace_event_type_##call); \
485 event_##call.id = id; \
486 INIT_LIST_HEAD(&event_##call.fields); \
487 init_preds(&event_##call); \
491 static struct ftrace_event_call __used \
492 __attribute__((__aligned__(4))) \
493 __attribute__((section("_ftrace_events"))) event_##call = { \
495 .system = __stringify(TRACE_SYSTEM), \
496 .event = &ftrace_event_type_##call, \
497 .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) \
505 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
507 #undef _TRACE_PROFILE
508 #undef _TRACE_PROFILE_INIT