1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * If TRACE_SYSTEM is defined, that will be the directory created
4*4882a593Smuzhiyun * in the ftrace directory under /sys/kernel/tracing/events/<system>
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * The define_trace.h below will also look for a file name of
7*4882a593Smuzhiyun * TRACE_SYSTEM.h where TRACE_SYSTEM is what is defined here.
8*4882a593Smuzhiyun * In this case, it would look for sample-trace.h
9*4882a593Smuzhiyun *
10*4882a593Smuzhiyun * If the header name will be different than the system name
11*4882a593Smuzhiyun * (as in this case), then you can override the header name that
12*4882a593Smuzhiyun * define_trace.h will look up by defining TRACE_INCLUDE_FILE
13*4882a593Smuzhiyun *
14*4882a593Smuzhiyun * This file is called trace-events-sample.h but we want the system
15*4882a593Smuzhiyun * to be called "sample-trace". Therefore we must define the name of this
16*4882a593Smuzhiyun * file:
17*4882a593Smuzhiyun *
18*4882a593Smuzhiyun * #define TRACE_INCLUDE_FILE trace-events-sample
19*4882a593Smuzhiyun *
20*4882a593Smuzhiyun * As we do an the bottom of this file.
21*4882a593Smuzhiyun *
22*4882a593Smuzhiyun * Notice that TRACE_SYSTEM should be defined outside of #if
23*4882a593Smuzhiyun * protection, just like TRACE_INCLUDE_FILE.
24*4882a593Smuzhiyun */
25*4882a593Smuzhiyun #undef TRACE_SYSTEM
26*4882a593Smuzhiyun #define TRACE_SYSTEM sample-trace
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun /*
29*4882a593Smuzhiyun * TRACE_SYSTEM is expected to be a C valid variable (alpha-numeric
30*4882a593Smuzhiyun * and underscore), although it may start with numbers. If for some
31*4882a593Smuzhiyun * reason it is not, you need to add the following lines:
32*4882a593Smuzhiyun */
33*4882a593Smuzhiyun #undef TRACE_SYSTEM_VAR
34*4882a593Smuzhiyun #define TRACE_SYSTEM_VAR sample_trace
35*4882a593Smuzhiyun /*
36*4882a593Smuzhiyun * But the above is only needed if TRACE_SYSTEM is not alpha-numeric
37*4882a593Smuzhiyun * and underscored. By default, TRACE_SYSTEM_VAR will be equal to
38*4882a593Smuzhiyun * TRACE_SYSTEM. As TRACE_SYSTEM_VAR must be alpha-numeric, if
39*4882a593Smuzhiyun * TRACE_SYSTEM is not, then TRACE_SYSTEM_VAR must be defined with
40*4882a593Smuzhiyun * only alpha-numeric and underscores.
41*4882a593Smuzhiyun *
42*4882a593Smuzhiyun * The TRACE_SYSTEM_VAR is only used internally and not visible to
43*4882a593Smuzhiyun * user space.
44*4882a593Smuzhiyun */
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun /*
47*4882a593Smuzhiyun * Notice that this file is not protected like a normal header.
48*4882a593Smuzhiyun * We also must allow for rereading of this file. The
49*4882a593Smuzhiyun *
50*4882a593Smuzhiyun * || defined(TRACE_HEADER_MULTI_READ)
51*4882a593Smuzhiyun *
52*4882a593Smuzhiyun * serves this purpose.
53*4882a593Smuzhiyun */
54*4882a593Smuzhiyun #if !defined(_TRACE_EVENT_SAMPLE_H) || defined(TRACE_HEADER_MULTI_READ)
55*4882a593Smuzhiyun #define _TRACE_EVENT_SAMPLE_H
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun /*
58*4882a593Smuzhiyun * All trace headers should include tracepoint.h, until we finally
59*4882a593Smuzhiyun * make it into a standard header.
60*4882a593Smuzhiyun */
61*4882a593Smuzhiyun #include <linux/tracepoint.h>
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun /*
64*4882a593Smuzhiyun * The TRACE_EVENT macro is broken up into 5 parts.
65*4882a593Smuzhiyun *
66*4882a593Smuzhiyun * name: name of the trace point. This is also how to enable the tracepoint.
67*4882a593Smuzhiyun * A function called trace_foo_bar() will be created.
68*4882a593Smuzhiyun *
69*4882a593Smuzhiyun * proto: the prototype of the function trace_foo_bar()
70*4882a593Smuzhiyun * Here it is trace_foo_bar(char *foo, int bar).
71*4882a593Smuzhiyun *
72*4882a593Smuzhiyun * args: must match the arguments in the prototype.
73*4882a593Smuzhiyun * Here it is simply "foo, bar".
74*4882a593Smuzhiyun *
75*4882a593Smuzhiyun * struct: This defines the way the data will be stored in the ring buffer.
76*4882a593Smuzhiyun * The items declared here become part of a special structure
77*4882a593Smuzhiyun * called "__entry", which can be used in the fast_assign part of the
78*4882a593Smuzhiyun * TRACE_EVENT macro.
79*4882a593Smuzhiyun *
80*4882a593Smuzhiyun * Here are the currently defined types you can use:
81*4882a593Smuzhiyun *
82*4882a593Smuzhiyun * __field : Is broken up into type and name. Where type can be any
83*4882a593Smuzhiyun * primitive type (integer, long or pointer).
84*4882a593Smuzhiyun *
85*4882a593Smuzhiyun * __field(int, foo)
86*4882a593Smuzhiyun *
87*4882a593Smuzhiyun * __entry->foo = 5;
88*4882a593Smuzhiyun *
89*4882a593Smuzhiyun * __field_struct : This can be any static complex data type (struct, union
90*4882a593Smuzhiyun * but not an array). Be careful using complex types, as each
91*4882a593Smuzhiyun * event is limited in size, and copying large amounts of data
92*4882a593Smuzhiyun * into the ring buffer can slow things down.
93*4882a593Smuzhiyun *
94*4882a593Smuzhiyun * __field_struct(struct bar, foo)
95*4882a593Smuzhiyun *
96*4882a593Smuzhiyun * __entry->bar.x = y;
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun * __array: There are three fields (type, name, size). The type is the
99*4882a593Smuzhiyun * type of elements in the array, the name is the name of the array.
100*4882a593Smuzhiyun * size is the number of items in the array (not the total size).
101*4882a593Smuzhiyun *
102*4882a593Smuzhiyun * __array( char, foo, 10) is the same as saying: char foo[10];
103*4882a593Smuzhiyun *
104*4882a593Smuzhiyun * Assigning arrays can be done like any array:
105*4882a593Smuzhiyun *
106*4882a593Smuzhiyun * __entry->foo[0] = 'a';
107*4882a593Smuzhiyun *
108*4882a593Smuzhiyun * memcpy(__entry->foo, bar, 10);
109*4882a593Smuzhiyun *
110*4882a593Smuzhiyun * __dynamic_array: This is similar to array, but can vary its size from
111*4882a593Smuzhiyun * instance to instance of the tracepoint being called.
112*4882a593Smuzhiyun * Like __array, this too has three elements (type, name, size);
113*4882a593Smuzhiyun * type is the type of the element, name is the name of the array.
114*4882a593Smuzhiyun * The size is different than __array. It is not a static number,
115*4882a593Smuzhiyun * but the algorithm to figure out the length of the array for the
116*4882a593Smuzhiyun * specific instance of tracepoint. Again, size is the number of
117*4882a593Smuzhiyun * items in the array, not the total length in bytes.
118*4882a593Smuzhiyun *
119*4882a593Smuzhiyun * __dynamic_array( int, foo, bar) is similar to: int foo[bar];
120*4882a593Smuzhiyun *
121*4882a593Smuzhiyun * Note, unlike arrays, you must use the __get_dynamic_array() macro
122*4882a593Smuzhiyun * to access the array.
123*4882a593Smuzhiyun *
124*4882a593Smuzhiyun * memcpy(__get_dynamic_array(foo), bar, 10);
125*4882a593Smuzhiyun *
126*4882a593Smuzhiyun * Notice, that "__entry" is not needed here.
127*4882a593Smuzhiyun *
128*4882a593Smuzhiyun * __string: This is a special kind of __dynamic_array. It expects to
129*4882a593Smuzhiyun * have a null terminated character array passed to it (it allows
130*4882a593Smuzhiyun * for NULL too, which would be converted into "(null)"). __string
131*4882a593Smuzhiyun * takes two parameter (name, src), where name is the name of
132*4882a593Smuzhiyun * the string saved, and src is the string to copy into the
133*4882a593Smuzhiyun * ring buffer.
134*4882a593Smuzhiyun *
135*4882a593Smuzhiyun * __string(foo, bar) is similar to: strcpy(foo, bar)
136*4882a593Smuzhiyun *
137*4882a593Smuzhiyun * To assign a string, use the helper macro __assign_str().
138*4882a593Smuzhiyun *
139*4882a593Smuzhiyun * __assign_str(foo, bar);
140*4882a593Smuzhiyun *
141*4882a593Smuzhiyun * In most cases, the __assign_str() macro will take the same
142*4882a593Smuzhiyun * parameters as the __string() macro had to declare the string.
143*4882a593Smuzhiyun *
144*4882a593Smuzhiyun * __bitmask: This is another kind of __dynamic_array, but it expects
145*4882a593Smuzhiyun * an array of longs, and the number of bits to parse. It takes
146*4882a593Smuzhiyun * two parameters (name, nr_bits), where name is the name of the
147*4882a593Smuzhiyun * bitmask to save, and the nr_bits is the number of bits to record.
148*4882a593Smuzhiyun *
149*4882a593Smuzhiyun * __bitmask(target_cpu, nr_cpumask_bits)
150*4882a593Smuzhiyun *
151*4882a593Smuzhiyun * To assign a bitmask, use the __assign_bitmask() helper macro.
152*4882a593Smuzhiyun *
153*4882a593Smuzhiyun * __assign_bitmask(target_cpus, cpumask_bits(bar), nr_cpumask_bits);
154*4882a593Smuzhiyun *
155*4882a593Smuzhiyun *
156*4882a593Smuzhiyun * fast_assign: This is a C like function that is used to store the items
157*4882a593Smuzhiyun * into the ring buffer. A special variable called "__entry" will be the
158*4882a593Smuzhiyun * structure that points into the ring buffer and has the same fields as
159*4882a593Smuzhiyun * described by the struct part of TRACE_EVENT above.
160*4882a593Smuzhiyun *
161*4882a593Smuzhiyun * printk: This is a way to print out the data in pretty print. This is
162*4882a593Smuzhiyun * useful if the system crashes and you are logging via a serial line,
163*4882a593Smuzhiyun * the data can be printed to the console using this "printk" method.
164*4882a593Smuzhiyun * This is also used to print out the data from the trace files.
165*4882a593Smuzhiyun * Again, the __entry macro is used to access the data from the ring buffer.
166*4882a593Smuzhiyun *
167*4882a593Smuzhiyun * Note, __dynamic_array, __string, and __bitmask require special helpers
168*4882a593Smuzhiyun * to access the data.
169*4882a593Smuzhiyun *
170*4882a593Smuzhiyun * For __dynamic_array(int, foo, bar) use __get_dynamic_array(foo)
171*4882a593Smuzhiyun * Use __get_dynamic_array_len(foo) to get the length of the array
172*4882a593Smuzhiyun * saved. Note, __get_dynamic_array_len() returns the total allocated
173*4882a593Smuzhiyun * length of the dynamic array; __print_array() expects the second
174*4882a593Smuzhiyun * parameter to be the number of elements. To get that, the array length
175*4882a593Smuzhiyun * needs to be divided by the element size.
176*4882a593Smuzhiyun *
177*4882a593Smuzhiyun * For __string(foo, bar) use __get_str(foo)
178*4882a593Smuzhiyun *
179*4882a593Smuzhiyun * For __bitmask(target_cpus, nr_cpumask_bits) use __get_bitmask(target_cpus)
180*4882a593Smuzhiyun *
181*4882a593Smuzhiyun *
182*4882a593Smuzhiyun * Note, that for both the assign and the printk, __entry is the handler
183*4882a593Smuzhiyun * to the data structure in the ring buffer, and is defined by the
184*4882a593Smuzhiyun * TP_STRUCT__entry.
185*4882a593Smuzhiyun */
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun /*
188*4882a593Smuzhiyun * It is OK to have helper functions in the file, but they need to be protected
189*4882a593Smuzhiyun * from being defined more than once. Remember, this file gets included more
190*4882a593Smuzhiyun * than once.
191*4882a593Smuzhiyun */
192*4882a593Smuzhiyun #ifndef __TRACE_EVENT_SAMPLE_HELPER_FUNCTIONS
193*4882a593Smuzhiyun #define __TRACE_EVENT_SAMPLE_HELPER_FUNCTIONS
__length_of(const int * list)194*4882a593Smuzhiyun static inline int __length_of(const int *list)
195*4882a593Smuzhiyun {
196*4882a593Smuzhiyun int i;
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun if (!list)
199*4882a593Smuzhiyun return 0;
200*4882a593Smuzhiyun
201*4882a593Smuzhiyun for (i = 0; list[i]; i++)
202*4882a593Smuzhiyun ;
203*4882a593Smuzhiyun return i;
204*4882a593Smuzhiyun }
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun enum {
207*4882a593Smuzhiyun TRACE_SAMPLE_FOO = 2,
208*4882a593Smuzhiyun TRACE_SAMPLE_BAR = 4,
209*4882a593Smuzhiyun TRACE_SAMPLE_ZOO = 8,
210*4882a593Smuzhiyun };
211*4882a593Smuzhiyun #endif
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun /*
214*4882a593Smuzhiyun * If enums are used in the TP_printk(), their names will be shown in
215*4882a593Smuzhiyun * format files and not their values. This can cause problems with user
216*4882a593Smuzhiyun * space programs that parse the format files to know how to translate
217*4882a593Smuzhiyun * the raw binary trace output into human readable text.
218*4882a593Smuzhiyun *
219*4882a593Smuzhiyun * To help out user space programs, any enum that is used in the TP_printk()
220*4882a593Smuzhiyun * should be defined by TRACE_DEFINE_ENUM() macro. All that is needed to
221*4882a593Smuzhiyun * be done is to add this macro with the enum within it in the trace
222*4882a593Smuzhiyun * header file, and it will be converted in the output.
223*4882a593Smuzhiyun */
224*4882a593Smuzhiyun
225*4882a593Smuzhiyun TRACE_DEFINE_ENUM(TRACE_SAMPLE_FOO);
226*4882a593Smuzhiyun TRACE_DEFINE_ENUM(TRACE_SAMPLE_BAR);
227*4882a593Smuzhiyun TRACE_DEFINE_ENUM(TRACE_SAMPLE_ZOO);
228*4882a593Smuzhiyun
229*4882a593Smuzhiyun TRACE_EVENT(foo_bar,
230*4882a593Smuzhiyun
231*4882a593Smuzhiyun TP_PROTO(const char *foo, int bar, const int *lst,
232*4882a593Smuzhiyun const char *string, const struct cpumask *mask),
233*4882a593Smuzhiyun
234*4882a593Smuzhiyun TP_ARGS(foo, bar, lst, string, mask),
235*4882a593Smuzhiyun
236*4882a593Smuzhiyun TP_STRUCT__entry(
237*4882a593Smuzhiyun __array( char, foo, 10 )
238*4882a593Smuzhiyun __field( int, bar )
239*4882a593Smuzhiyun __dynamic_array(int, list, __length_of(lst))
240*4882a593Smuzhiyun __string( str, string )
241*4882a593Smuzhiyun __bitmask( cpus, num_possible_cpus() )
242*4882a593Smuzhiyun ),
243*4882a593Smuzhiyun
244*4882a593Smuzhiyun TP_fast_assign(
245*4882a593Smuzhiyun strlcpy(__entry->foo, foo, 10);
246*4882a593Smuzhiyun __entry->bar = bar;
247*4882a593Smuzhiyun memcpy(__get_dynamic_array(list), lst,
248*4882a593Smuzhiyun __length_of(lst) * sizeof(int));
249*4882a593Smuzhiyun __assign_str(str, string);
250*4882a593Smuzhiyun __assign_bitmask(cpus, cpumask_bits(mask), num_possible_cpus());
251*4882a593Smuzhiyun ),
252*4882a593Smuzhiyun
253*4882a593Smuzhiyun TP_printk("foo %s %d %s %s %s %s (%s)", __entry->foo, __entry->bar,
254*4882a593Smuzhiyun
255*4882a593Smuzhiyun /*
256*4882a593Smuzhiyun * Notice here the use of some helper functions. This includes:
257*4882a593Smuzhiyun *
258*4882a593Smuzhiyun * __print_symbolic( variable, { value, "string" }, ... ),
259*4882a593Smuzhiyun *
260*4882a593Smuzhiyun * The variable is tested against each value of the { } pair. If
261*4882a593Smuzhiyun * the variable matches one of the values, then it will print the
262*4882a593Smuzhiyun * string in that pair. If non are matched, it returns a string
263*4882a593Smuzhiyun * version of the number (if __entry->bar == 7 then "7" is returned).
264*4882a593Smuzhiyun */
265*4882a593Smuzhiyun __print_symbolic(__entry->bar,
266*4882a593Smuzhiyun { 0, "zero" },
267*4882a593Smuzhiyun { TRACE_SAMPLE_FOO, "TWO" },
268*4882a593Smuzhiyun { TRACE_SAMPLE_BAR, "FOUR" },
269*4882a593Smuzhiyun { TRACE_SAMPLE_ZOO, "EIGHT" },
270*4882a593Smuzhiyun { 10, "TEN" }
271*4882a593Smuzhiyun ),
272*4882a593Smuzhiyun
273*4882a593Smuzhiyun /*
274*4882a593Smuzhiyun * __print_flags( variable, "delim", { value, "flag" }, ... ),
275*4882a593Smuzhiyun *
276*4882a593Smuzhiyun * This is similar to __print_symbolic, except that it tests the bits
277*4882a593Smuzhiyun * of the value. If ((FLAG & variable) == FLAG) then the string is
278*4882a593Smuzhiyun * printed. If more than one flag matches, then each one that does is
279*4882a593Smuzhiyun * also printed with delim in between them.
280*4882a593Smuzhiyun * If not all bits are accounted for, then the not found bits will be
281*4882a593Smuzhiyun * added in hex format: 0x506 will show BIT2|BIT4|0x500
282*4882a593Smuzhiyun */
283*4882a593Smuzhiyun __print_flags(__entry->bar, "|",
284*4882a593Smuzhiyun { 1, "BIT1" },
285*4882a593Smuzhiyun { 2, "BIT2" },
286*4882a593Smuzhiyun { 4, "BIT3" },
287*4882a593Smuzhiyun { 8, "BIT4" }
288*4882a593Smuzhiyun ),
289*4882a593Smuzhiyun /*
290*4882a593Smuzhiyun * __print_array( array, len, element_size )
291*4882a593Smuzhiyun *
292*4882a593Smuzhiyun * This prints out the array that is defined by __array in a nice format.
293*4882a593Smuzhiyun */
294*4882a593Smuzhiyun __print_array(__get_dynamic_array(list),
295*4882a593Smuzhiyun __get_dynamic_array_len(list) / sizeof(int),
296*4882a593Smuzhiyun sizeof(int)),
297*4882a593Smuzhiyun __get_str(str), __get_bitmask(cpus))
298*4882a593Smuzhiyun );
299*4882a593Smuzhiyun
300*4882a593Smuzhiyun /*
301*4882a593Smuzhiyun * There may be a case where a tracepoint should only be called if
302*4882a593Smuzhiyun * some condition is set. Otherwise the tracepoint should not be called.
303*4882a593Smuzhiyun * But to do something like:
304*4882a593Smuzhiyun *
305*4882a593Smuzhiyun * if (cond)
306*4882a593Smuzhiyun * trace_foo();
307*4882a593Smuzhiyun *
308*4882a593Smuzhiyun * Would cause a little overhead when tracing is not enabled, and that
309*4882a593Smuzhiyun * overhead, even if small, is not something we want. As tracepoints
310*4882a593Smuzhiyun * use static branch (aka jump_labels), where no branch is taken to
311*4882a593Smuzhiyun * skip the tracepoint when not enabled, and a jmp is placed to jump
312*4882a593Smuzhiyun * to the tracepoint code when it is enabled, having a if statement
313*4882a593Smuzhiyun * nullifies that optimization. It would be nice to place that
314*4882a593Smuzhiyun * condition within the static branch. This is where TRACE_EVENT_CONDITION
315*4882a593Smuzhiyun * comes in.
316*4882a593Smuzhiyun *
317*4882a593Smuzhiyun * TRACE_EVENT_CONDITION() is just like TRACE_EVENT, except it adds another
318*4882a593Smuzhiyun * parameter just after args. Where TRACE_EVENT has:
319*4882a593Smuzhiyun *
320*4882a593Smuzhiyun * TRACE_EVENT(name, proto, args, struct, assign, printk)
321*4882a593Smuzhiyun *
322*4882a593Smuzhiyun * the CONDITION version has:
323*4882a593Smuzhiyun *
324*4882a593Smuzhiyun * TRACE_EVENT_CONDITION(name, proto, args, cond, struct, assign, printk)
325*4882a593Smuzhiyun *
326*4882a593Smuzhiyun * Everything is the same as TRACE_EVENT except for the new cond. Think
327*4882a593Smuzhiyun * of the cond variable as:
328*4882a593Smuzhiyun *
329*4882a593Smuzhiyun * if (cond)
330*4882a593Smuzhiyun * trace_foo_bar_with_cond();
331*4882a593Smuzhiyun *
332*4882a593Smuzhiyun * Except that the logic for the if branch is placed after the static branch.
333*4882a593Smuzhiyun * That is, the if statement that processes the condition will not be
334*4882a593Smuzhiyun * executed unless that traecpoint is enabled. Otherwise it still remains
335*4882a593Smuzhiyun * a nop.
336*4882a593Smuzhiyun */
337*4882a593Smuzhiyun TRACE_EVENT_CONDITION(foo_bar_with_cond,
338*4882a593Smuzhiyun
339*4882a593Smuzhiyun TP_PROTO(const char *foo, int bar),
340*4882a593Smuzhiyun
341*4882a593Smuzhiyun TP_ARGS(foo, bar),
342*4882a593Smuzhiyun
343*4882a593Smuzhiyun TP_CONDITION(!(bar % 10)),
344*4882a593Smuzhiyun
345*4882a593Smuzhiyun TP_STRUCT__entry(
346*4882a593Smuzhiyun __string( foo, foo )
347*4882a593Smuzhiyun __field( int, bar )
348*4882a593Smuzhiyun ),
349*4882a593Smuzhiyun
350*4882a593Smuzhiyun TP_fast_assign(
351*4882a593Smuzhiyun __assign_str(foo, foo);
352*4882a593Smuzhiyun __entry->bar = bar;
353*4882a593Smuzhiyun ),
354*4882a593Smuzhiyun
355*4882a593Smuzhiyun TP_printk("foo %s %d", __get_str(foo), __entry->bar)
356*4882a593Smuzhiyun );
357*4882a593Smuzhiyun
358*4882a593Smuzhiyun int foo_bar_reg(void);
359*4882a593Smuzhiyun void foo_bar_unreg(void);
360*4882a593Smuzhiyun
361*4882a593Smuzhiyun /*
362*4882a593Smuzhiyun * Now in the case that some function needs to be called when the
363*4882a593Smuzhiyun * tracepoint is enabled and/or when it is disabled, the
364*4882a593Smuzhiyun * TRACE_EVENT_FN() serves this purpose. This is just like TRACE_EVENT()
365*4882a593Smuzhiyun * but adds two more parameters at the end:
366*4882a593Smuzhiyun *
367*4882a593Smuzhiyun * TRACE_EVENT_FN( name, proto, args, struct, assign, printk, reg, unreg)
368*4882a593Smuzhiyun *
369*4882a593Smuzhiyun * reg and unreg are functions with the prototype of:
370*4882a593Smuzhiyun *
371*4882a593Smuzhiyun * void reg(void)
372*4882a593Smuzhiyun *
373*4882a593Smuzhiyun * The reg function gets called before the tracepoint is enabled, and
374*4882a593Smuzhiyun * the unreg function gets called after the tracepoint is disabled.
375*4882a593Smuzhiyun *
376*4882a593Smuzhiyun * Note, reg and unreg are allowed to be NULL. If you only need to
377*4882a593Smuzhiyun * call a function before enabling, or after disabling, just set one
378*4882a593Smuzhiyun * function and pass in NULL for the other parameter.
379*4882a593Smuzhiyun */
380*4882a593Smuzhiyun TRACE_EVENT_FN(foo_bar_with_fn,
381*4882a593Smuzhiyun
382*4882a593Smuzhiyun TP_PROTO(const char *foo, int bar),
383*4882a593Smuzhiyun
384*4882a593Smuzhiyun TP_ARGS(foo, bar),
385*4882a593Smuzhiyun
386*4882a593Smuzhiyun TP_STRUCT__entry(
387*4882a593Smuzhiyun __string( foo, foo )
388*4882a593Smuzhiyun __field( int, bar )
389*4882a593Smuzhiyun ),
390*4882a593Smuzhiyun
391*4882a593Smuzhiyun TP_fast_assign(
392*4882a593Smuzhiyun __assign_str(foo, foo);
393*4882a593Smuzhiyun __entry->bar = bar;
394*4882a593Smuzhiyun ),
395*4882a593Smuzhiyun
396*4882a593Smuzhiyun TP_printk("foo %s %d", __get_str(foo), __entry->bar),
397*4882a593Smuzhiyun
398*4882a593Smuzhiyun foo_bar_reg, foo_bar_unreg
399*4882a593Smuzhiyun );
400*4882a593Smuzhiyun
401*4882a593Smuzhiyun /*
402*4882a593Smuzhiyun * Each TRACE_EVENT macro creates several helper functions to produce
403*4882a593Smuzhiyun * the code to add the tracepoint, create the files in the trace
404*4882a593Smuzhiyun * directory, hook it to perf, assign the values and to print out
405*4882a593Smuzhiyun * the raw data from the ring buffer. To prevent too much bloat,
406*4882a593Smuzhiyun * if there are more than one tracepoint that uses the same format
407*4882a593Smuzhiyun * for the proto, args, struct, assign and printk, and only the name
408*4882a593Smuzhiyun * is different, it is highly recommended to use the DECLARE_EVENT_CLASS
409*4882a593Smuzhiyun *
410*4882a593Smuzhiyun * DECLARE_EVENT_CLASS() macro creates most of the functions for the
411*4882a593Smuzhiyun * tracepoint. Then DEFINE_EVENT() is use to hook a tracepoint to those
412*4882a593Smuzhiyun * functions. This DEFINE_EVENT() is an instance of the class and can
413*4882a593Smuzhiyun * be enabled and disabled separately from other events (either TRACE_EVENT
414*4882a593Smuzhiyun * or other DEFINE_EVENT()s).
415*4882a593Smuzhiyun *
416*4882a593Smuzhiyun * Note, TRACE_EVENT() itself is simply defined as:
417*4882a593Smuzhiyun *
418*4882a593Smuzhiyun * #define TRACE_EVENT(name, proto, args, tstruct, assign, printk) \
419*4882a593Smuzhiyun * DECLARE_EVENT_CLASS(name, proto, args, tstruct, assign, printk); \
420*4882a593Smuzhiyun * DEFINE_EVENT(name, name, proto, args)
421*4882a593Smuzhiyun *
422*4882a593Smuzhiyun * The DEFINE_EVENT() also can be declared with conditions and reg functions:
423*4882a593Smuzhiyun *
424*4882a593Smuzhiyun * DEFINE_EVENT_CONDITION(template, name, proto, args, cond);
425*4882a593Smuzhiyun * DEFINE_EVENT_FN(template, name, proto, args, reg, unreg);
426*4882a593Smuzhiyun */
427*4882a593Smuzhiyun DECLARE_EVENT_CLASS(foo_template,
428*4882a593Smuzhiyun
429*4882a593Smuzhiyun TP_PROTO(const char *foo, int bar),
430*4882a593Smuzhiyun
431*4882a593Smuzhiyun TP_ARGS(foo, bar),
432*4882a593Smuzhiyun
433*4882a593Smuzhiyun TP_STRUCT__entry(
434*4882a593Smuzhiyun __string( foo, foo )
435*4882a593Smuzhiyun __field( int, bar )
436*4882a593Smuzhiyun ),
437*4882a593Smuzhiyun
438*4882a593Smuzhiyun TP_fast_assign(
439*4882a593Smuzhiyun __assign_str(foo, foo);
440*4882a593Smuzhiyun __entry->bar = bar;
441*4882a593Smuzhiyun ),
442*4882a593Smuzhiyun
443*4882a593Smuzhiyun TP_printk("foo %s %d", __get_str(foo), __entry->bar)
444*4882a593Smuzhiyun );
445*4882a593Smuzhiyun
446*4882a593Smuzhiyun /*
447*4882a593Smuzhiyun * Here's a better way for the previous samples (except, the first
448*4882a593Smuzhiyun * example had more fields and could not be used here).
449*4882a593Smuzhiyun */
450*4882a593Smuzhiyun DEFINE_EVENT(foo_template, foo_with_template_simple,
451*4882a593Smuzhiyun TP_PROTO(const char *foo, int bar),
452*4882a593Smuzhiyun TP_ARGS(foo, bar));
453*4882a593Smuzhiyun
454*4882a593Smuzhiyun DEFINE_EVENT_CONDITION(foo_template, foo_with_template_cond,
455*4882a593Smuzhiyun TP_PROTO(const char *foo, int bar),
456*4882a593Smuzhiyun TP_ARGS(foo, bar),
457*4882a593Smuzhiyun TP_CONDITION(!(bar % 8)));
458*4882a593Smuzhiyun
459*4882a593Smuzhiyun
460*4882a593Smuzhiyun DEFINE_EVENT_FN(foo_template, foo_with_template_fn,
461*4882a593Smuzhiyun TP_PROTO(const char *foo, int bar),
462*4882a593Smuzhiyun TP_ARGS(foo, bar),
463*4882a593Smuzhiyun foo_bar_reg, foo_bar_unreg);
464*4882a593Smuzhiyun
465*4882a593Smuzhiyun /*
466*4882a593Smuzhiyun * Anytime two events share basically the same values and have
467*4882a593Smuzhiyun * the same output, use the DECLARE_EVENT_CLASS() and DEFINE_EVENT()
468*4882a593Smuzhiyun * when ever possible.
469*4882a593Smuzhiyun */
470*4882a593Smuzhiyun
471*4882a593Smuzhiyun /*
472*4882a593Smuzhiyun * If the event is similar to the DECLARE_EVENT_CLASS, but you need
473*4882a593Smuzhiyun * to have a different output, then use DEFINE_EVENT_PRINT() which
474*4882a593Smuzhiyun * lets you override the TP_printk() of the class.
475*4882a593Smuzhiyun */
476*4882a593Smuzhiyun
477*4882a593Smuzhiyun DEFINE_EVENT_PRINT(foo_template, foo_with_template_print,
478*4882a593Smuzhiyun TP_PROTO(const char *foo, int bar),
479*4882a593Smuzhiyun TP_ARGS(foo, bar),
480*4882a593Smuzhiyun TP_printk("bar %s %d", __get_str(foo), __entry->bar));
481*4882a593Smuzhiyun
482*4882a593Smuzhiyun #endif
483*4882a593Smuzhiyun
484*4882a593Smuzhiyun /***** NOTICE! The #if protection ends here. *****/
485*4882a593Smuzhiyun
486*4882a593Smuzhiyun
487*4882a593Smuzhiyun /*
488*4882a593Smuzhiyun * There are several ways I could have done this. If I left out the
489*4882a593Smuzhiyun * TRACE_INCLUDE_PATH, then it would default to the kernel source
490*4882a593Smuzhiyun * include/trace/events directory.
491*4882a593Smuzhiyun *
492*4882a593Smuzhiyun * I could specify a path from the define_trace.h file back to this
493*4882a593Smuzhiyun * file.
494*4882a593Smuzhiyun *
495*4882a593Smuzhiyun * #define TRACE_INCLUDE_PATH ../../samples/trace_events
496*4882a593Smuzhiyun *
497*4882a593Smuzhiyun * But the safest and easiest way to simply make it use the directory
498*4882a593Smuzhiyun * that the file is in is to add in the Makefile:
499*4882a593Smuzhiyun *
500*4882a593Smuzhiyun * CFLAGS_trace-events-sample.o := -I$(src)
501*4882a593Smuzhiyun *
502*4882a593Smuzhiyun * This will make sure the current path is part of the include
503*4882a593Smuzhiyun * structure for our file so that define_trace.h can find it.
504*4882a593Smuzhiyun *
505*4882a593Smuzhiyun * I could have made only the top level directory the include:
506*4882a593Smuzhiyun *
507*4882a593Smuzhiyun * CFLAGS_trace-events-sample.o := -I$(PWD)
508*4882a593Smuzhiyun *
509*4882a593Smuzhiyun * And then let the path to this directory be the TRACE_INCLUDE_PATH:
510*4882a593Smuzhiyun *
511*4882a593Smuzhiyun * #define TRACE_INCLUDE_PATH samples/trace_events
512*4882a593Smuzhiyun *
513*4882a593Smuzhiyun * But then if something defines "samples" or "trace_events" as a macro
514*4882a593Smuzhiyun * then we could risk that being converted too, and give us an unexpected
515*4882a593Smuzhiyun * result.
516*4882a593Smuzhiyun */
517*4882a593Smuzhiyun #undef TRACE_INCLUDE_PATH
518*4882a593Smuzhiyun #undef TRACE_INCLUDE_FILE
519*4882a593Smuzhiyun #define TRACE_INCLUDE_PATH .
520*4882a593Smuzhiyun /*
521*4882a593Smuzhiyun * TRACE_INCLUDE_FILE is not needed if the filename and TRACE_SYSTEM are equal
522*4882a593Smuzhiyun */
523*4882a593Smuzhiyun #define TRACE_INCLUDE_FILE trace-events-sample
524*4882a593Smuzhiyun #include <trace/define_trace.h>
525