Linux Audio

Check our new training course

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