1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Stage 1 of the trace events.
5 * Override the macros in the event tracepoint header <trace/events/XXX.h>
6 * to include the following:
8 * struct trace_event_raw_<call> {
9 * struct trace_entry ent;
11 * <type2> <item2>[<len>];
15 * The <type> <item> is created by the __field(type, item) macro or
16 * the __array(type2, item2, len) macro.
17 * We simply do "type item;", and that will create the fields
21 #include <linux/trace_events.h>
23 #ifndef TRACE_SYSTEM_VAR
24 #define TRACE_SYSTEM_VAR TRACE_SYSTEM
27 #include "stages/init.h"
30 * DECLARE_EVENT_CLASS can be used to add a generic function
31 * handlers for events. That is, if all events have the same
32 * parameters and just have distinct trace points.
33 * Each tracepoint can be defined with DEFINE_EVENT and that
34 * will map the DECLARE_EVENT_CLASS to the tracepoint.
36 * TRACE_EVENT is a one to one mapping between tracepoint and template.
39 #define TRACE_EVENT(name, proto, args, tstruct, assign, print) \
40 DECLARE_EVENT_CLASS(name, \
46 DEFINE_EVENT(name, name, PARAMS(proto), PARAMS(args));
48 #include "stages/stage1_struct_define.h"
50 #undef DECLARE_EVENT_CLASS
51 #define DECLARE_EVENT_CLASS(name, proto, args, tstruct, assign, print) \
52 struct trace_event_raw_##name { \
53 struct trace_entry ent; \
58 static struct trace_event_class event_class_##name;
61 #define DEFINE_EVENT(template, name, proto, args) \
62 static struct trace_event_call __used \
63 __attribute__((__aligned__(4))) event_##name
65 #undef DEFINE_EVENT_FN
66 #define DEFINE_EVENT_FN(template, name, proto, args, reg, unreg) \
67 DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
69 #undef DEFINE_EVENT_PRINT
70 #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
71 DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
73 /* Callbacks are meaningless to ftrace. */
75 #define TRACE_EVENT_FN(name, proto, args, tstruct, \
76 assign, print, reg, unreg) \
77 TRACE_EVENT(name, PARAMS(proto), PARAMS(args), \
78 PARAMS(tstruct), PARAMS(assign), PARAMS(print)) \
80 #undef TRACE_EVENT_FN_COND
81 #define TRACE_EVENT_FN_COND(name, proto, args, cond, tstruct, \
82 assign, print, reg, unreg) \
83 TRACE_EVENT_CONDITION(name, PARAMS(proto), PARAMS(args), PARAMS(cond), \
84 PARAMS(tstruct), PARAMS(assign), PARAMS(print)) \
86 #undef TRACE_EVENT_FLAGS
87 #define TRACE_EVENT_FLAGS(name, value) \
88 __TRACE_EVENT_FLAGS(name, value)
90 #undef TRACE_EVENT_PERF_PERM
91 #define TRACE_EVENT_PERF_PERM(name, expr...) \
92 __TRACE_EVENT_PERF_PERM(name, expr)
94 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
97 * Stage 2 of the trace events.
99 * Include the following:
101 * struct trace_event_data_offsets_<call> {
107 * The __dynamic_array() macro will create each u32 <item>, this is
108 * to keep the offset of each array from the beginning of the event.
109 * The size of an array is also encoded, in the higher 16 bits of <item>.
112 #include "stages/stage2_data_offsets.h"
114 #undef DECLARE_EVENT_CLASS
115 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
116 struct trace_event_data_offsets_##call { \
121 #define DEFINE_EVENT(template, name, proto, args)
123 #undef DEFINE_EVENT_PRINT
124 #define DEFINE_EVENT_PRINT(template, name, proto, args, print)
126 #undef TRACE_EVENT_FLAGS
127 #define TRACE_EVENT_FLAGS(event, flag)
129 #undef TRACE_EVENT_PERF_PERM
130 #define TRACE_EVENT_PERF_PERM(event, expr...)
132 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
135 * Stage 3 of the trace events.
137 * Override the macros in the event tracepoint header <trace/events/XXX.h>
138 * to include the following:
141 * trace_raw_output_<call>(struct trace_iterator *iter, int flags)
143 * struct trace_seq *s = &iter->seq;
144 * struct trace_event_raw_<call> *field; <-- defined in stage 1
145 * struct trace_seq *p = &iter->tmp_seq;
147 * -------(for event)-------
149 * struct trace_entry *entry;
153 * if (entry->type != event_<call>->event.type) {
155 * return TRACE_TYPE_UNHANDLED;
158 * field = (typeof(field))entry;
161 * return trace_output_call(iter, <call>, <TP_printk> "\n");
163 * ------(or, for event class)------
167 * field = (typeof(field))iter->ent;
169 * ret = trace_raw_output_prep(iter, trace_event);
170 * if (ret != TRACE_TYPE_HANDLED)
173 * trace_event_printf(iter, <TP_printk> "\n");
175 * return trace_handle_return(s);
179 * This is the method used to print the raw event to the trace
180 * output format. Note, this is not needed if the data is read
184 #include "stages/stage3_trace_output.h"
186 #undef DECLARE_EVENT_CLASS
187 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
188 static notrace enum print_line_t \
189 trace_raw_output_##call(struct trace_iterator *iter, int flags, \
190 struct trace_event *trace_event) \
192 struct trace_seq *s = &iter->seq; \
193 struct trace_seq __maybe_unused *p = &iter->tmp_seq; \
194 struct trace_event_raw_##call *field; \
197 field = (typeof(field))iter->ent; \
199 ret = trace_raw_output_prep(iter, trace_event); \
200 if (ret != TRACE_TYPE_HANDLED) \
203 trace_event_printf(iter, print); \
205 return trace_handle_return(s); \
207 static struct trace_event_functions trace_event_type_funcs_##call = { \
208 .trace = trace_raw_output_##call, \
211 #undef DEFINE_EVENT_PRINT
212 #define DEFINE_EVENT_PRINT(template, call, proto, args, print) \
213 static notrace enum print_line_t \
214 trace_raw_output_##call(struct trace_iterator *iter, int flags, \
215 struct trace_event *event) \
217 struct trace_event_raw_##template *field; \
218 struct trace_entry *entry; \
219 struct trace_seq *p = &iter->tmp_seq; \
223 if (entry->type != event_##call.event.type) { \
225 return TRACE_TYPE_UNHANDLED; \
228 field = (typeof(field))entry; \
231 return trace_output_call(iter, #call, print); \
233 static struct trace_event_functions trace_event_type_funcs_##call = { \
234 .trace = trace_raw_output_##call, \
237 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
239 #include "stages/stage4_event_fields.h"
241 #undef DECLARE_EVENT_CLASS
242 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, func, print) \
243 static struct trace_event_fields trace_event_fields_##call[] = { \
247 #undef DEFINE_EVENT_PRINT
248 #define DEFINE_EVENT_PRINT(template, name, proto, args, print)
250 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
252 #include "stages/stage5_get_offsets.h"
254 #undef DECLARE_EVENT_CLASS
255 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
256 static inline notrace int trace_event_get_offsets_##call( \
257 struct trace_event_data_offsets_##call *__data_offsets, proto) \
259 int __data_size = 0; \
260 int __maybe_unused __item_length; \
261 struct trace_event_raw_##call __maybe_unused *entry; \
265 return __data_size; \
268 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
271 * Stage 4 of the trace events.
273 * Override the macros in the event tracepoint header <trace/events/XXX.h>
274 * to include the following:
276 * For those macros defined with TRACE_EVENT:
278 * static struct trace_event_call event_<call>;
280 * static void trace_event_raw_event_<call>(void *__data, proto)
282 * struct trace_event_file *trace_file = __data;
283 * struct trace_event_call *event_call = trace_file->event_call;
284 * struct trace_event_data_offsets_<call> __maybe_unused __data_offsets;
285 * unsigned long eflags = trace_file->flags;
286 * enum event_trigger_type __tt = ETT_NONE;
287 * struct ring_buffer_event *event;
288 * struct trace_event_raw_<call> *entry; <-- defined in stage 1
289 * struct trace_buffer *buffer;
290 * unsigned long irq_flags;
294 * if (!(eflags & EVENT_FILE_FL_TRIGGER_COND)) {
295 * if (eflags & EVENT_FILE_FL_TRIGGER_MODE)
296 * event_triggers_call(trace_file, NULL);
297 * if (eflags & EVENT_FILE_FL_SOFT_DISABLED)
301 * local_save_flags(irq_flags);
302 * pc = preempt_count();
304 * __data_size = trace_event_get_offsets_<call>(&__data_offsets, args);
306 * event = trace_event_buffer_lock_reserve(&buffer, trace_file,
307 * event_<call>->event.type,
308 * sizeof(*entry) + __data_size,
312 * entry = ring_buffer_event_data(event);
314 * { <assign>; } <-- Here we assign the entries by the __field and
317 * if (eflags & EVENT_FILE_FL_TRIGGER_COND)
318 * __tt = event_triggers_call(trace_file, entry);
320 * if (test_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT,
321 * &trace_file->flags))
322 * ring_buffer_discard_commit(buffer, event);
323 * else if (!filter_check_discard(trace_file, entry, buffer, event))
324 * trace_buffer_unlock_commit(buffer, event, irq_flags, pc);
327 * event_triggers_post_call(trace_file, __tt);
330 * static struct trace_event ftrace_event_type_<call> = {
331 * .trace = trace_raw_output_<call>, <-- stage 2
334 * static char print_fmt_<call>[] = <TP_printk>;
336 * static struct trace_event_class __used event_class_<template> = {
337 * .system = "<system>",
338 * .fields_array = trace_event_fields_<call>,
339 * .fields = LIST_HEAD_INIT(event_class_##call.fields),
340 * .raw_init = trace_event_raw_init,
341 * .probe = trace_event_raw_event_##call,
342 * .reg = trace_event_reg,
345 * static struct trace_event_call event_<call> = {
346 * .class = event_class_<template>,
348 * .tp = &__tracepoint_<call>,
350 * .event = &ftrace_event_type_<call>,
351 * .print_fmt = print_fmt_<call>,
352 * .flags = TRACE_EVENT_FL_TRACEPOINT,
354 * // its only safe to use pointers when doing linker tricks to
355 * // create an array.
356 * static struct trace_event_call __used
357 * __section("_ftrace_events") *__event_<call> = &event_<call>;
361 #ifdef CONFIG_PERF_EVENTS
363 #define _TRACE_PERF_PROTO(call, proto) \
364 static notrace void \
365 perf_trace_##call(void *__data, proto);
367 #define _TRACE_PERF_INIT(call) \
368 .perf_probe = perf_trace_##call,
371 #define _TRACE_PERF_PROTO(call, proto)
372 #define _TRACE_PERF_INIT(call)
373 #endif /* CONFIG_PERF_EVENTS */
375 #include "stages/stage6_event_callback.h"
377 #undef DECLARE_EVENT_CLASS
378 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
380 static notrace void \
381 trace_event_raw_event_##call(void *__data, proto) \
383 struct trace_event_file *trace_file = __data; \
384 struct trace_event_data_offsets_##call __maybe_unused __data_offsets;\
385 struct trace_event_buffer fbuffer; \
386 struct trace_event_raw_##call *entry; \
389 if (trace_trigger_soft_disabled(trace_file)) \
392 __data_size = trace_event_get_offsets_##call(&__data_offsets, args); \
394 entry = trace_event_buffer_reserve(&fbuffer, trace_file, \
395 sizeof(*entry) + __data_size); \
404 trace_event_buffer_commit(&fbuffer); \
407 * The ftrace_test_probe is compiled out, it is only here as a build time check
408 * to make sure that if the tracepoint handling changes, the ftrace probe will
409 * fail to compile unless it too is updated.
413 #define DEFINE_EVENT(template, call, proto, args) \
414 static inline void ftrace_test_probe_##call(void) \
416 check_trace_callback_type_##call(trace_event_raw_event_##template); \
419 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
421 #include "stages/stage7_class_define.h"
423 #undef DECLARE_EVENT_CLASS
424 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
425 _TRACE_PERF_PROTO(call, PARAMS(proto)); \
426 static char print_fmt_##call[] = print; \
427 static struct trace_event_class __used __refdata event_class_##call = { \
428 .system = TRACE_SYSTEM_STRING, \
429 .fields_array = trace_event_fields_##call, \
430 .fields = LIST_HEAD_INIT(event_class_##call.fields),\
431 .raw_init = trace_event_raw_init, \
432 .probe = trace_event_raw_event_##call, \
433 .reg = trace_event_reg, \
434 _TRACE_PERF_INIT(call) \
438 #define DEFINE_EVENT(template, call, proto, args) \
440 static struct trace_event_call __used event_##call = { \
441 .class = &event_class_##template, \
443 .tp = &__tracepoint_##call, \
445 .event.funcs = &trace_event_type_funcs_##template, \
446 .print_fmt = print_fmt_##template, \
447 .flags = TRACE_EVENT_FL_TRACEPOINT, \
449 static struct trace_event_call __used \
450 __section("_ftrace_events") *__event_##call = &event_##call
452 #undef DEFINE_EVENT_PRINT
453 #define DEFINE_EVENT_PRINT(template, call, proto, args, print) \
455 static char print_fmt_##call[] = print; \
457 static struct trace_event_call __used event_##call = { \
458 .class = &event_class_##template, \
460 .tp = &__tracepoint_##call, \
462 .event.funcs = &trace_event_type_funcs_##call, \
463 .print_fmt = print_fmt_##call, \
464 .flags = TRACE_EVENT_FL_TRACEPOINT, \
466 static struct trace_event_call __used \
467 __section("_ftrace_events") *__event_##call = &event_##call
469 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)