1 /* SPDX-License-Identifier: GPL-2.0 */
3 * This is similar to the trace_events.h file, but is to only
4 * create custom trace events to be attached to existing tracepoints.
5 * Where as the TRACE_EVENT() macro (from trace_events.h) will create
6 * both the trace event and the tracepoint it will attach the event to,
7 * TRACE_CUSTOM_EVENT() is to create only a custom version of an existing
8 * trace event (created by TRACE_EVENT() or DEFINE_EVENT()), and will
9 * be placed in the "custom" system.
12 #include <linux/trace_events.h>
14 /* All custom events are placed in the custom group */
16 #define TRACE_SYSTEM custom
18 #ifndef TRACE_SYSTEM_VAR
19 #define TRACE_SYSTEM_VAR TRACE_SYSTEM
22 /* The init stage creates the system string and enum mappings */
24 #include "stages/init.h"
26 #undef TRACE_CUSTOM_EVENT
27 #define TRACE_CUSTOM_EVENT(name, proto, args, tstruct, assign, print) \
28 DECLARE_CUSTOM_EVENT_CLASS(name, \
34 DEFINE_CUSTOM_EVENT(name, name, PARAMS(proto), PARAMS(args));
36 /* Stage 1 creates the structure of the recorded event layout */
38 #include "stages/stage1_struct_define.h"
40 #undef DECLARE_CUSTOM_EVENT_CLASS
41 #define DECLARE_CUSTOM_EVENT_CLASS(name, proto, args, tstruct, assign, print) \
42 struct trace_custom_event_raw_##name { \
43 struct trace_entry ent; \
48 static struct trace_event_class custom_event_class_##name;
50 #undef DEFINE_CUSTOM_EVENT
51 #define DEFINE_CUSTOM_EVENT(template, name, proto, args) \
52 static struct trace_event_call __used \
53 __attribute__((__aligned__(4))) custom_event_##name
55 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
57 /* Stage 2 creates the custom class */
59 #include "stages/stage2_data_offsets.h"
61 #undef DECLARE_CUSTOM_EVENT_CLASS
62 #define DECLARE_CUSTOM_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
63 struct trace_custom_event_data_offsets_##call { \
67 #undef DEFINE_CUSTOM_EVENT
68 #define DEFINE_CUSTOM_EVENT(template, name, proto, args)
70 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
72 /* Stage 3 create the way to print the custom event */
74 #include "stages/stage3_trace_output.h"
76 #undef DECLARE_CUSTOM_EVENT_CLASS
77 #define DECLARE_CUSTOM_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
78 static notrace enum print_line_t \
79 trace_custom_raw_output_##call(struct trace_iterator *iter, int flags, \
80 struct trace_event *trace_event) \
82 struct trace_seq *s = &iter->seq; \
83 struct trace_seq __maybe_unused *p = &iter->tmp_seq; \
84 struct trace_custom_event_raw_##call *field; \
87 field = (typeof(field))iter->ent; \
89 ret = trace_raw_output_prep(iter, trace_event); \
90 if (ret != TRACE_TYPE_HANDLED) \
93 trace_event_printf(iter, print); \
95 return trace_handle_return(s); \
97 static struct trace_event_functions trace_custom_event_type_funcs_##call = { \
98 .trace = trace_custom_raw_output_##call, \
101 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
103 /* Stage 4 creates the offset layout for the fields */
105 #include "stages/stage4_event_fields.h"
107 #undef DECLARE_CUSTOM_EVENT_CLASS
108 #define DECLARE_CUSTOM_EVENT_CLASS(call, proto, args, tstruct, func, print) \
109 static struct trace_event_fields trace_custom_event_fields_##call[] = { \
113 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
115 /* Stage 5 creates the helper function for dynamic fields */
117 #include "stages/stage5_get_offsets.h"
119 #undef DECLARE_CUSTOM_EVENT_CLASS
120 #define DECLARE_CUSTOM_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
121 static inline notrace int trace_custom_event_get_offsets_##call( \
122 struct trace_custom_event_data_offsets_##call *__data_offsets, proto) \
124 int __data_size = 0; \
125 int __maybe_unused __item_length; \
126 struct trace_custom_event_raw_##call __maybe_unused *entry; \
130 return __data_size; \
133 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
135 /* Stage 6 creates the probe function that records the event */
137 #include "stages/stage6_event_callback.h"
139 #undef DECLARE_CUSTOM_EVENT_CLASS
140 #define DECLARE_CUSTOM_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
142 static notrace void \
143 trace_custom_event_raw_event_##call(void *__data, proto) \
145 struct trace_event_file *trace_file = __data; \
146 struct trace_custom_event_data_offsets_##call __maybe_unused __data_offsets; \
147 struct trace_event_buffer fbuffer; \
148 struct trace_custom_event_raw_##call *entry; \
151 if (trace_trigger_soft_disabled(trace_file)) \
154 __data_size = trace_custom_event_get_offsets_##call(&__data_offsets, args); \
156 entry = trace_event_buffer_reserve(&fbuffer, trace_file, \
157 sizeof(*entry) + __data_size); \
166 trace_event_buffer_commit(&fbuffer); \
169 * The ftrace_test_custom_probe is compiled out, it is only here as a build time check
170 * to make sure that if the tracepoint handling changes, the ftrace probe will
171 * fail to compile unless it too is updated.
174 #undef DEFINE_CUSTOM_EVENT
175 #define DEFINE_CUSTOM_EVENT(template, call, proto, args) \
176 static inline void ftrace_test_custom_probe_##call(void) \
178 check_trace_callback_type_##call(trace_custom_event_raw_event_##template); \
181 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
183 /* Stage 7 creates the actual class and event structure for the custom event */
185 #include "stages/stage7_class_define.h"
187 #undef DECLARE_CUSTOM_EVENT_CLASS
188 #define DECLARE_CUSTOM_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
189 static char custom_print_fmt_##call[] = print; \
190 static struct trace_event_class __used __refdata custom_event_class_##call = { \
191 .system = TRACE_SYSTEM_STRING, \
192 .fields_array = trace_custom_event_fields_##call, \
193 .fields = LIST_HEAD_INIT(custom_event_class_##call.fields),\
194 .raw_init = trace_event_raw_init, \
195 .probe = trace_custom_event_raw_event_##call, \
196 .reg = trace_event_reg, \
199 #undef DEFINE_CUSTOM_EVENT
200 #define DEFINE_CUSTOM_EVENT(template, call, proto, args) \
202 static struct trace_event_call __used custom_event_##call = { \
204 .class = &custom_event_class_##template, \
205 .event.funcs = &trace_custom_event_type_funcs_##template, \
206 .print_fmt = custom_print_fmt_##template, \
207 .flags = TRACE_EVENT_FL_CUSTOM, \
209 static inline int trace_custom_event_##call##_update(struct tracepoint *tp) \
211 if (tp->name && strcmp(tp->name, #call) == 0) { \
212 custom_event_##call.tp = tp; \
213 custom_event_##call.flags = TRACE_EVENT_FL_TRACEPOINT; \
218 static struct trace_event_call __used \
219 __section("_ftrace_events") *__custom_event_##call = &custom_event_##call
221 #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)