162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * If TRACE_SYSTEM is defined, that will be the directory created
462306a36Sopenharmony_ci * in the ftrace directory under /sys/kernel/tracing/events/<system>
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci * The define_trace.h below will also look for a file name of
762306a36Sopenharmony_ci * TRACE_SYSTEM.h where TRACE_SYSTEM is what is defined here.
862306a36Sopenharmony_ci * In this case, it would look for sample-trace.h
962306a36Sopenharmony_ci *
1062306a36Sopenharmony_ci * If the header name will be different than the system name
1162306a36Sopenharmony_ci * (as in this case), then you can override the header name that
1262306a36Sopenharmony_ci * define_trace.h will look up by defining TRACE_INCLUDE_FILE
1362306a36Sopenharmony_ci *
1462306a36Sopenharmony_ci * This file is called trace-events-sample.h but we want the system
1562306a36Sopenharmony_ci * to be called "sample-trace". Therefore we must define the name of this
1662306a36Sopenharmony_ci * file:
1762306a36Sopenharmony_ci *
1862306a36Sopenharmony_ci * #define TRACE_INCLUDE_FILE trace-events-sample
1962306a36Sopenharmony_ci *
2062306a36Sopenharmony_ci * As we do an the bottom of this file.
2162306a36Sopenharmony_ci *
2262306a36Sopenharmony_ci * Notice that TRACE_SYSTEM should be defined outside of #if
2362306a36Sopenharmony_ci * protection, just like TRACE_INCLUDE_FILE.
2462306a36Sopenharmony_ci */
2562306a36Sopenharmony_ci#undef TRACE_SYSTEM
2662306a36Sopenharmony_ci#define TRACE_SYSTEM sample-trace
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci/*
2962306a36Sopenharmony_ci * TRACE_SYSTEM is expected to be a C valid variable (alpha-numeric
3062306a36Sopenharmony_ci * and underscore), although it may start with numbers. If for some
3162306a36Sopenharmony_ci * reason it is not, you need to add the following lines:
3262306a36Sopenharmony_ci */
3362306a36Sopenharmony_ci#undef TRACE_SYSTEM_VAR
3462306a36Sopenharmony_ci#define TRACE_SYSTEM_VAR sample_trace
3562306a36Sopenharmony_ci/*
3662306a36Sopenharmony_ci * But the above is only needed if TRACE_SYSTEM is not alpha-numeric
3762306a36Sopenharmony_ci * and underscored. By default, TRACE_SYSTEM_VAR will be equal to
3862306a36Sopenharmony_ci * TRACE_SYSTEM. As TRACE_SYSTEM_VAR must be alpha-numeric, if
3962306a36Sopenharmony_ci * TRACE_SYSTEM is not, then TRACE_SYSTEM_VAR must be defined with
4062306a36Sopenharmony_ci * only alpha-numeric and underscores.
4162306a36Sopenharmony_ci *
4262306a36Sopenharmony_ci * The TRACE_SYSTEM_VAR is only used internally and not visible to
4362306a36Sopenharmony_ci * user space.
4462306a36Sopenharmony_ci */
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci/*
4762306a36Sopenharmony_ci * Notice that this file is not protected like a normal header.
4862306a36Sopenharmony_ci * We also must allow for rereading of this file. The
4962306a36Sopenharmony_ci *
5062306a36Sopenharmony_ci *  || defined(TRACE_HEADER_MULTI_READ)
5162306a36Sopenharmony_ci *
5262306a36Sopenharmony_ci * serves this purpose.
5362306a36Sopenharmony_ci */
5462306a36Sopenharmony_ci#if !defined(_TRACE_EVENT_SAMPLE_H) || defined(TRACE_HEADER_MULTI_READ)
5562306a36Sopenharmony_ci#define _TRACE_EVENT_SAMPLE_H
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci/*
5862306a36Sopenharmony_ci * All trace headers should include tracepoint.h, until we finally
5962306a36Sopenharmony_ci * make it into a standard header.
6062306a36Sopenharmony_ci */
6162306a36Sopenharmony_ci#include <linux/tracepoint.h>
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci/*
6462306a36Sopenharmony_ci * The TRACE_EVENT macro is broken up into 5 parts.
6562306a36Sopenharmony_ci *
6662306a36Sopenharmony_ci * name: name of the trace point. This is also how to enable the tracepoint.
6762306a36Sopenharmony_ci *   A function called trace_foo_bar() will be created.
6862306a36Sopenharmony_ci *
6962306a36Sopenharmony_ci * proto: the prototype of the function trace_foo_bar()
7062306a36Sopenharmony_ci *   Here it is trace_foo_bar(char *foo, int bar).
7162306a36Sopenharmony_ci *
7262306a36Sopenharmony_ci * args:  must match the arguments in the prototype.
7362306a36Sopenharmony_ci *    Here it is simply "foo, bar".
7462306a36Sopenharmony_ci *
7562306a36Sopenharmony_ci * struct:  This defines the way the data will be stored in the ring buffer.
7662306a36Sopenharmony_ci *          The items declared here become part of a special structure
7762306a36Sopenharmony_ci *          called "__entry", which can be used in the fast_assign part of the
7862306a36Sopenharmony_ci *          TRACE_EVENT macro.
7962306a36Sopenharmony_ci *
8062306a36Sopenharmony_ci *      Here are the currently defined types you can use:
8162306a36Sopenharmony_ci *
8262306a36Sopenharmony_ci *   __field : Is broken up into type and name. Where type can be any
8362306a36Sopenharmony_ci *         primitive type (integer, long or pointer).
8462306a36Sopenharmony_ci *
8562306a36Sopenharmony_ci *        __field(int, foo)
8662306a36Sopenharmony_ci *
8762306a36Sopenharmony_ci *        __entry->foo = 5;
8862306a36Sopenharmony_ci *
8962306a36Sopenharmony_ci *   __field_struct : This can be any static complex data type (struct, union
9062306a36Sopenharmony_ci *         but not an array). Be careful using complex types, as each
9162306a36Sopenharmony_ci *         event is limited in size, and copying large amounts of data
9262306a36Sopenharmony_ci *         into the ring buffer can slow things down.
9362306a36Sopenharmony_ci *
9462306a36Sopenharmony_ci *         __field_struct(struct bar, foo)
9562306a36Sopenharmony_ci *
9662306a36Sopenharmony_ci *         __entry->bar.x = y;
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ci *   __array: There are three fields (type, name, size). The type is the
9962306a36Sopenharmony_ci *         type of elements in the array, the name is the name of the array.
10062306a36Sopenharmony_ci *         size is the number of items in the array (not the total size).
10162306a36Sopenharmony_ci *
10262306a36Sopenharmony_ci *         __array( char, foo, 10) is the same as saying: char foo[10];
10362306a36Sopenharmony_ci *
10462306a36Sopenharmony_ci *         Assigning arrays can be done like any array:
10562306a36Sopenharmony_ci *
10662306a36Sopenharmony_ci *         __entry->foo[0] = 'a';
10762306a36Sopenharmony_ci *
10862306a36Sopenharmony_ci *         memcpy(__entry->foo, bar, 10);
10962306a36Sopenharmony_ci *
11062306a36Sopenharmony_ci *   __dynamic_array: This is similar to array, but can vary its size from
11162306a36Sopenharmony_ci *         instance to instance of the tracepoint being called.
11262306a36Sopenharmony_ci *         Like __array, this too has three elements (type, name, size);
11362306a36Sopenharmony_ci *         type is the type of the element, name is the name of the array.
11462306a36Sopenharmony_ci *         The size is different than __array. It is not a static number,
11562306a36Sopenharmony_ci *         but the algorithm to figure out the length of the array for the
11662306a36Sopenharmony_ci *         specific instance of tracepoint. Again, size is the number of
11762306a36Sopenharmony_ci *         items in the array, not the total length in bytes.
11862306a36Sopenharmony_ci *
11962306a36Sopenharmony_ci *         __dynamic_array( int, foo, bar) is similar to: int foo[bar];
12062306a36Sopenharmony_ci *
12162306a36Sopenharmony_ci *         Note, unlike arrays, you must use the __get_dynamic_array() macro
12262306a36Sopenharmony_ci *         to access the array.
12362306a36Sopenharmony_ci *
12462306a36Sopenharmony_ci *         memcpy(__get_dynamic_array(foo), bar, 10);
12562306a36Sopenharmony_ci *
12662306a36Sopenharmony_ci *         Notice, that "__entry" is not needed here.
12762306a36Sopenharmony_ci *
12862306a36Sopenharmony_ci *   __string: This is a special kind of __dynamic_array. It expects to
12962306a36Sopenharmony_ci *         have a null terminated character array passed to it (it allows
13062306a36Sopenharmony_ci *         for NULL too, which would be converted into "(null)"). __string
13162306a36Sopenharmony_ci *         takes two parameter (name, src), where name is the name of
13262306a36Sopenharmony_ci *         the string saved, and src is the string to copy into the
13362306a36Sopenharmony_ci *         ring buffer.
13462306a36Sopenharmony_ci *
13562306a36Sopenharmony_ci *         __string(foo, bar)  is similar to:  strcpy(foo, bar)
13662306a36Sopenharmony_ci *
13762306a36Sopenharmony_ci *         To assign a string, use the helper macro __assign_str().
13862306a36Sopenharmony_ci *
13962306a36Sopenharmony_ci *         __assign_str(foo, bar);
14062306a36Sopenharmony_ci *
14162306a36Sopenharmony_ci *         In most cases, the __assign_str() macro will take the same
14262306a36Sopenharmony_ci *         parameters as the __string() macro had to declare the string.
14362306a36Sopenharmony_ci *
14462306a36Sopenharmony_ci *   __vstring: This is similar to __string() but instead of taking a
14562306a36Sopenharmony_ci *         dynamic length, it takes a variable list va_list 'va' variable.
14662306a36Sopenharmony_ci *         Some event callers already have a message from parameters saved
14762306a36Sopenharmony_ci *         in a va_list. Passing in the format and the va_list variable
14862306a36Sopenharmony_ci *         will save just enough on the ring buffer for that string.
14962306a36Sopenharmony_ci *         Note, the va variable used is a pointer to a va_list, not
15062306a36Sopenharmony_ci *         to the va_list directly.
15162306a36Sopenharmony_ci *
15262306a36Sopenharmony_ci *           (va_list *va)
15362306a36Sopenharmony_ci *
15462306a36Sopenharmony_ci *         __vstring(foo, fmt, va)  is similar to:  vsnprintf(foo, fmt, va)
15562306a36Sopenharmony_ci *
15662306a36Sopenharmony_ci *         To assign the string, use the helper macro __assign_vstr().
15762306a36Sopenharmony_ci *
15862306a36Sopenharmony_ci *         __assign_vstr(foo, fmt, va);
15962306a36Sopenharmony_ci *
16062306a36Sopenharmony_ci *         In most cases, the __assign_vstr() macro will take the same
16162306a36Sopenharmony_ci *         parameters as the __vstring() macro had to declare the string.
16262306a36Sopenharmony_ci *         Use __get_str() to retrieve the __vstring() just like it would for
16362306a36Sopenharmony_ci *         __string().
16462306a36Sopenharmony_ci *
16562306a36Sopenharmony_ci *   __string_len: This is a helper to a __dynamic_array, but it understands
16662306a36Sopenharmony_ci *	   that the array has characters in it, and with the combined
16762306a36Sopenharmony_ci *         use of __assign_str_len(), it will allocate 'len' + 1 bytes
16862306a36Sopenharmony_ci *         in the ring buffer and add a '\0' to the string. This is
16962306a36Sopenharmony_ci *         useful if the string being saved has no terminating '\0' byte.
17062306a36Sopenharmony_ci *         It requires that the length of the string is known as it acts
17162306a36Sopenharmony_ci *         like a memcpy().
17262306a36Sopenharmony_ci *
17362306a36Sopenharmony_ci *         Declared with:
17462306a36Sopenharmony_ci *
17562306a36Sopenharmony_ci *         __string_len(foo, bar, len)
17662306a36Sopenharmony_ci *
17762306a36Sopenharmony_ci *         To assign this string, use the helper macro __assign_str_len().
17862306a36Sopenharmony_ci *
17962306a36Sopenharmony_ci *         __assign_str_len(foo, bar, len);
18062306a36Sopenharmony_ci *
18162306a36Sopenharmony_ci *         Then len + 1 is allocated to the ring buffer, and a nul terminating
18262306a36Sopenharmony_ci *         byte is added. This is similar to:
18362306a36Sopenharmony_ci *
18462306a36Sopenharmony_ci *         memcpy(__get_str(foo), bar, len);
18562306a36Sopenharmony_ci *         __get_str(foo)[len] = 0;
18662306a36Sopenharmony_ci *
18762306a36Sopenharmony_ci *        The advantage of using this over __dynamic_array, is that it
18862306a36Sopenharmony_ci *        takes care of allocating the extra byte on the ring buffer
18962306a36Sopenharmony_ci *        for the '\0' terminating byte, and __get_str(foo) can be used
19062306a36Sopenharmony_ci *        in the TP_printk().
19162306a36Sopenharmony_ci *
19262306a36Sopenharmony_ci *   __bitmask: This is another kind of __dynamic_array, but it expects
19362306a36Sopenharmony_ci *         an array of longs, and the number of bits to parse. It takes
19462306a36Sopenharmony_ci *         two parameters (name, nr_bits), where name is the name of the
19562306a36Sopenharmony_ci *         bitmask to save, and the nr_bits is the number of bits to record.
19662306a36Sopenharmony_ci *
19762306a36Sopenharmony_ci *         __bitmask(target_cpu, nr_cpumask_bits)
19862306a36Sopenharmony_ci *
19962306a36Sopenharmony_ci *         To assign a bitmask, use the __assign_bitmask() helper macro.
20062306a36Sopenharmony_ci *
20162306a36Sopenharmony_ci *         __assign_bitmask(target_cpus, cpumask_bits(bar), nr_cpumask_bits);
20262306a36Sopenharmony_ci *
20362306a36Sopenharmony_ci *   __cpumask: This is pretty much the same as __bitmask but is specific for
20462306a36Sopenharmony_ci *         CPU masks. The type displayed to the user via the format files will
20562306a36Sopenharmony_ci *         be "cpumaks_t" such that user space may deal with them differently
20662306a36Sopenharmony_ci *         if they choose to do so, and the bits is always set to nr_cpumask_bits.
20762306a36Sopenharmony_ci *
20862306a36Sopenharmony_ci *         __cpumask(target_cpu)
20962306a36Sopenharmony_ci *
21062306a36Sopenharmony_ci *         To assign a cpumask, use the __assign_cpumask() helper macro.
21162306a36Sopenharmony_ci *
21262306a36Sopenharmony_ci *         __assign_cpumask(target_cpus, cpumask_bits(bar));
21362306a36Sopenharmony_ci *
21462306a36Sopenharmony_ci * fast_assign: This is a C like function that is used to store the items
21562306a36Sopenharmony_ci *    into the ring buffer. A special variable called "__entry" will be the
21662306a36Sopenharmony_ci *    structure that points into the ring buffer and has the same fields as
21762306a36Sopenharmony_ci *    described by the struct part of TRACE_EVENT above.
21862306a36Sopenharmony_ci *
21962306a36Sopenharmony_ci * printk: This is a way to print out the data in pretty print. This is
22062306a36Sopenharmony_ci *    useful if the system crashes and you are logging via a serial line,
22162306a36Sopenharmony_ci *    the data can be printed to the console using this "printk" method.
22262306a36Sopenharmony_ci *    This is also used to print out the data from the trace files.
22362306a36Sopenharmony_ci *    Again, the __entry macro is used to access the data from the ring buffer.
22462306a36Sopenharmony_ci *
22562306a36Sopenharmony_ci *    Note, __dynamic_array, __string, __bitmask and __cpumask require special
22662306a36Sopenharmony_ci *       helpers to access the data.
22762306a36Sopenharmony_ci *
22862306a36Sopenharmony_ci *      For __dynamic_array(int, foo, bar) use __get_dynamic_array(foo)
22962306a36Sopenharmony_ci *            Use __get_dynamic_array_len(foo) to get the length of the array
23062306a36Sopenharmony_ci *            saved. Note, __get_dynamic_array_len() returns the total allocated
23162306a36Sopenharmony_ci *            length of the dynamic array; __print_array() expects the second
23262306a36Sopenharmony_ci *            parameter to be the number of elements. To get that, the array length
23362306a36Sopenharmony_ci *            needs to be divided by the element size.
23462306a36Sopenharmony_ci *
23562306a36Sopenharmony_ci *      For __string(foo, bar) use __get_str(foo)
23662306a36Sopenharmony_ci *
23762306a36Sopenharmony_ci *      For __bitmask(target_cpus, nr_cpumask_bits) use __get_bitmask(target_cpus)
23862306a36Sopenharmony_ci *
23962306a36Sopenharmony_ci *      For __cpumask(target_cpus) use __get_cpumask(target_cpus)
24062306a36Sopenharmony_ci *
24162306a36Sopenharmony_ci *
24262306a36Sopenharmony_ci * Note, that for both the assign and the printk, __entry is the handler
24362306a36Sopenharmony_ci * to the data structure in the ring buffer, and is defined by the
24462306a36Sopenharmony_ci * TP_STRUCT__entry.
24562306a36Sopenharmony_ci */
24662306a36Sopenharmony_ci
24762306a36Sopenharmony_ci/*
24862306a36Sopenharmony_ci * It is OK to have helper functions in the file, but they need to be protected
24962306a36Sopenharmony_ci * from being defined more than once. Remember, this file gets included more
25062306a36Sopenharmony_ci * than once.
25162306a36Sopenharmony_ci */
25262306a36Sopenharmony_ci#ifndef __TRACE_EVENT_SAMPLE_HELPER_FUNCTIONS
25362306a36Sopenharmony_ci#define __TRACE_EVENT_SAMPLE_HELPER_FUNCTIONS
25462306a36Sopenharmony_cistatic inline int __length_of(const int *list)
25562306a36Sopenharmony_ci{
25662306a36Sopenharmony_ci	int i;
25762306a36Sopenharmony_ci
25862306a36Sopenharmony_ci	if (!list)
25962306a36Sopenharmony_ci		return 0;
26062306a36Sopenharmony_ci
26162306a36Sopenharmony_ci	for (i = 0; list[i]; i++)
26262306a36Sopenharmony_ci		;
26362306a36Sopenharmony_ci	return i;
26462306a36Sopenharmony_ci}
26562306a36Sopenharmony_ci
26662306a36Sopenharmony_cienum {
26762306a36Sopenharmony_ci	TRACE_SAMPLE_FOO = 2,
26862306a36Sopenharmony_ci	TRACE_SAMPLE_BAR = 4,
26962306a36Sopenharmony_ci	TRACE_SAMPLE_ZOO = 8,
27062306a36Sopenharmony_ci};
27162306a36Sopenharmony_ci#endif
27262306a36Sopenharmony_ci
27362306a36Sopenharmony_ci/*
27462306a36Sopenharmony_ci * If enums are used in the TP_printk(), their names will be shown in
27562306a36Sopenharmony_ci * format files and not their values. This can cause problems with user
27662306a36Sopenharmony_ci * space programs that parse the format files to know how to translate
27762306a36Sopenharmony_ci * the raw binary trace output into human readable text.
27862306a36Sopenharmony_ci *
27962306a36Sopenharmony_ci * To help out user space programs, any enum that is used in the TP_printk()
28062306a36Sopenharmony_ci * should be defined by TRACE_DEFINE_ENUM() macro. All that is needed to
28162306a36Sopenharmony_ci * be done is to add this macro with the enum within it in the trace
28262306a36Sopenharmony_ci * header file, and it will be converted in the output.
28362306a36Sopenharmony_ci */
28462306a36Sopenharmony_ci
28562306a36Sopenharmony_ciTRACE_DEFINE_ENUM(TRACE_SAMPLE_FOO);
28662306a36Sopenharmony_ciTRACE_DEFINE_ENUM(TRACE_SAMPLE_BAR);
28762306a36Sopenharmony_ciTRACE_DEFINE_ENUM(TRACE_SAMPLE_ZOO);
28862306a36Sopenharmony_ci
28962306a36Sopenharmony_ciTRACE_EVENT(foo_bar,
29062306a36Sopenharmony_ci
29162306a36Sopenharmony_ci	TP_PROTO(const char *foo, int bar, const int *lst,
29262306a36Sopenharmony_ci		 const char *string, const struct cpumask *mask,
29362306a36Sopenharmony_ci		 const char *fmt, va_list *va),
29462306a36Sopenharmony_ci
29562306a36Sopenharmony_ci	TP_ARGS(foo, bar, lst, string, mask, fmt, va),
29662306a36Sopenharmony_ci
29762306a36Sopenharmony_ci	TP_STRUCT__entry(
29862306a36Sopenharmony_ci		__array(	char,	foo,    10		)
29962306a36Sopenharmony_ci		__field(	int,	bar			)
30062306a36Sopenharmony_ci		__dynamic_array(int,	list,   __length_of(lst))
30162306a36Sopenharmony_ci		__string(	str,	string			)
30262306a36Sopenharmony_ci		__bitmask(	cpus,	num_possible_cpus()	)
30362306a36Sopenharmony_ci		__cpumask(	cpum				)
30462306a36Sopenharmony_ci		__vstring(	vstr,	fmt,	va		)
30562306a36Sopenharmony_ci	),
30662306a36Sopenharmony_ci
30762306a36Sopenharmony_ci	TP_fast_assign(
30862306a36Sopenharmony_ci		strlcpy(__entry->foo, foo, 10);
30962306a36Sopenharmony_ci		__entry->bar	= bar;
31062306a36Sopenharmony_ci		memcpy(__get_dynamic_array(list), lst,
31162306a36Sopenharmony_ci		       __length_of(lst) * sizeof(int));
31262306a36Sopenharmony_ci		__assign_str(str, string);
31362306a36Sopenharmony_ci		__assign_vstr(vstr, fmt, va);
31462306a36Sopenharmony_ci		__assign_bitmask(cpus, cpumask_bits(mask), num_possible_cpus());
31562306a36Sopenharmony_ci		__assign_cpumask(cpum, cpumask_bits(mask));
31662306a36Sopenharmony_ci	),
31762306a36Sopenharmony_ci
31862306a36Sopenharmony_ci	TP_printk("foo %s %d %s %s %s %s (%s) (%s) %s", __entry->foo, __entry->bar,
31962306a36Sopenharmony_ci
32062306a36Sopenharmony_ci/*
32162306a36Sopenharmony_ci * Notice here the use of some helper functions. This includes:
32262306a36Sopenharmony_ci *
32362306a36Sopenharmony_ci *  __print_symbolic( variable, { value, "string" }, ... ),
32462306a36Sopenharmony_ci *
32562306a36Sopenharmony_ci *    The variable is tested against each value of the { } pair. If
32662306a36Sopenharmony_ci *    the variable matches one of the values, then it will print the
32762306a36Sopenharmony_ci *    string in that pair. If non are matched, it returns a string
32862306a36Sopenharmony_ci *    version of the number (if __entry->bar == 7 then "7" is returned).
32962306a36Sopenharmony_ci */
33062306a36Sopenharmony_ci		  __print_symbolic(__entry->bar,
33162306a36Sopenharmony_ci				   { 0, "zero" },
33262306a36Sopenharmony_ci				   { TRACE_SAMPLE_FOO, "TWO" },
33362306a36Sopenharmony_ci				   { TRACE_SAMPLE_BAR, "FOUR" },
33462306a36Sopenharmony_ci				   { TRACE_SAMPLE_ZOO, "EIGHT" },
33562306a36Sopenharmony_ci				   { 10, "TEN" }
33662306a36Sopenharmony_ci			  ),
33762306a36Sopenharmony_ci
33862306a36Sopenharmony_ci/*
33962306a36Sopenharmony_ci *  __print_flags( variable, "delim", { value, "flag" }, ... ),
34062306a36Sopenharmony_ci *
34162306a36Sopenharmony_ci *    This is similar to __print_symbolic, except that it tests the bits
34262306a36Sopenharmony_ci *    of the value. If ((FLAG & variable) == FLAG) then the string is
34362306a36Sopenharmony_ci *    printed. If more than one flag matches, then each one that does is
34462306a36Sopenharmony_ci *    also printed with delim in between them.
34562306a36Sopenharmony_ci *    If not all bits are accounted for, then the not found bits will be
34662306a36Sopenharmony_ci *    added in hex format: 0x506 will show BIT2|BIT4|0x500
34762306a36Sopenharmony_ci */
34862306a36Sopenharmony_ci		  __print_flags(__entry->bar, "|",
34962306a36Sopenharmony_ci				{ 1, "BIT1" },
35062306a36Sopenharmony_ci				{ 2, "BIT2" },
35162306a36Sopenharmony_ci				{ 4, "BIT3" },
35262306a36Sopenharmony_ci				{ 8, "BIT4" }
35362306a36Sopenharmony_ci			  ),
35462306a36Sopenharmony_ci/*
35562306a36Sopenharmony_ci *  __print_array( array, len, element_size )
35662306a36Sopenharmony_ci *
35762306a36Sopenharmony_ci *    This prints out the array that is defined by __array in a nice format.
35862306a36Sopenharmony_ci */
35962306a36Sopenharmony_ci		  __print_array(__get_dynamic_array(list),
36062306a36Sopenharmony_ci				__get_dynamic_array_len(list) / sizeof(int),
36162306a36Sopenharmony_ci				sizeof(int)),
36262306a36Sopenharmony_ci		  __get_str(str), __get_bitmask(cpus), __get_cpumask(cpum),
36362306a36Sopenharmony_ci		  __get_str(vstr))
36462306a36Sopenharmony_ci);
36562306a36Sopenharmony_ci
36662306a36Sopenharmony_ci/*
36762306a36Sopenharmony_ci * There may be a case where a tracepoint should only be called if
36862306a36Sopenharmony_ci * some condition is set. Otherwise the tracepoint should not be called.
36962306a36Sopenharmony_ci * But to do something like:
37062306a36Sopenharmony_ci *
37162306a36Sopenharmony_ci *  if (cond)
37262306a36Sopenharmony_ci *     trace_foo();
37362306a36Sopenharmony_ci *
37462306a36Sopenharmony_ci * Would cause a little overhead when tracing is not enabled, and that
37562306a36Sopenharmony_ci * overhead, even if small, is not something we want. As tracepoints
37662306a36Sopenharmony_ci * use static branch (aka jump_labels), where no branch is taken to
37762306a36Sopenharmony_ci * skip the tracepoint when not enabled, and a jmp is placed to jump
37862306a36Sopenharmony_ci * to the tracepoint code when it is enabled, having a if statement
37962306a36Sopenharmony_ci * nullifies that optimization. It would be nice to place that
38062306a36Sopenharmony_ci * condition within the static branch. This is where TRACE_EVENT_CONDITION
38162306a36Sopenharmony_ci * comes in.
38262306a36Sopenharmony_ci *
38362306a36Sopenharmony_ci * TRACE_EVENT_CONDITION() is just like TRACE_EVENT, except it adds another
38462306a36Sopenharmony_ci * parameter just after args. Where TRACE_EVENT has:
38562306a36Sopenharmony_ci *
38662306a36Sopenharmony_ci * TRACE_EVENT(name, proto, args, struct, assign, printk)
38762306a36Sopenharmony_ci *
38862306a36Sopenharmony_ci * the CONDITION version has:
38962306a36Sopenharmony_ci *
39062306a36Sopenharmony_ci * TRACE_EVENT_CONDITION(name, proto, args, cond, struct, assign, printk)
39162306a36Sopenharmony_ci *
39262306a36Sopenharmony_ci * Everything is the same as TRACE_EVENT except for the new cond. Think
39362306a36Sopenharmony_ci * of the cond variable as:
39462306a36Sopenharmony_ci *
39562306a36Sopenharmony_ci *   if (cond)
39662306a36Sopenharmony_ci *      trace_foo_bar_with_cond();
39762306a36Sopenharmony_ci *
39862306a36Sopenharmony_ci * Except that the logic for the if branch is placed after the static branch.
39962306a36Sopenharmony_ci * That is, the if statement that processes the condition will not be
40062306a36Sopenharmony_ci * executed unless that traecpoint is enabled. Otherwise it still remains
40162306a36Sopenharmony_ci * a nop.
40262306a36Sopenharmony_ci */
40362306a36Sopenharmony_ciTRACE_EVENT_CONDITION(foo_bar_with_cond,
40462306a36Sopenharmony_ci
40562306a36Sopenharmony_ci	TP_PROTO(const char *foo, int bar),
40662306a36Sopenharmony_ci
40762306a36Sopenharmony_ci	TP_ARGS(foo, bar),
40862306a36Sopenharmony_ci
40962306a36Sopenharmony_ci	TP_CONDITION(!(bar % 10)),
41062306a36Sopenharmony_ci
41162306a36Sopenharmony_ci	TP_STRUCT__entry(
41262306a36Sopenharmony_ci		__string(	foo,    foo		)
41362306a36Sopenharmony_ci		__field(	int,	bar			)
41462306a36Sopenharmony_ci	),
41562306a36Sopenharmony_ci
41662306a36Sopenharmony_ci	TP_fast_assign(
41762306a36Sopenharmony_ci		__assign_str(foo, foo);
41862306a36Sopenharmony_ci		__entry->bar	= bar;
41962306a36Sopenharmony_ci	),
42062306a36Sopenharmony_ci
42162306a36Sopenharmony_ci	TP_printk("foo %s %d", __get_str(foo), __entry->bar)
42262306a36Sopenharmony_ci);
42362306a36Sopenharmony_ci
42462306a36Sopenharmony_ciint foo_bar_reg(void);
42562306a36Sopenharmony_civoid foo_bar_unreg(void);
42662306a36Sopenharmony_ci
42762306a36Sopenharmony_ci/*
42862306a36Sopenharmony_ci * Now in the case that some function needs to be called when the
42962306a36Sopenharmony_ci * tracepoint is enabled and/or when it is disabled, the
43062306a36Sopenharmony_ci * TRACE_EVENT_FN() serves this purpose. This is just like TRACE_EVENT()
43162306a36Sopenharmony_ci * but adds two more parameters at the end:
43262306a36Sopenharmony_ci *
43362306a36Sopenharmony_ci * TRACE_EVENT_FN( name, proto, args, struct, assign, printk, reg, unreg)
43462306a36Sopenharmony_ci *
43562306a36Sopenharmony_ci * reg and unreg are functions with the prototype of:
43662306a36Sopenharmony_ci *
43762306a36Sopenharmony_ci *    void reg(void)
43862306a36Sopenharmony_ci *
43962306a36Sopenharmony_ci * The reg function gets called before the tracepoint is enabled, and
44062306a36Sopenharmony_ci * the unreg function gets called after the tracepoint is disabled.
44162306a36Sopenharmony_ci *
44262306a36Sopenharmony_ci * Note, reg and unreg are allowed to be NULL. If you only need to
44362306a36Sopenharmony_ci * call a function before enabling, or after disabling, just set one
44462306a36Sopenharmony_ci * function and pass in NULL for the other parameter.
44562306a36Sopenharmony_ci */
44662306a36Sopenharmony_ciTRACE_EVENT_FN(foo_bar_with_fn,
44762306a36Sopenharmony_ci
44862306a36Sopenharmony_ci	TP_PROTO(const char *foo, int bar),
44962306a36Sopenharmony_ci
45062306a36Sopenharmony_ci	TP_ARGS(foo, bar),
45162306a36Sopenharmony_ci
45262306a36Sopenharmony_ci	TP_STRUCT__entry(
45362306a36Sopenharmony_ci		__string(	foo,    foo		)
45462306a36Sopenharmony_ci		__field(	int,	bar		)
45562306a36Sopenharmony_ci	),
45662306a36Sopenharmony_ci
45762306a36Sopenharmony_ci	TP_fast_assign(
45862306a36Sopenharmony_ci		__assign_str(foo, foo);
45962306a36Sopenharmony_ci		__entry->bar	= bar;
46062306a36Sopenharmony_ci	),
46162306a36Sopenharmony_ci
46262306a36Sopenharmony_ci	TP_printk("foo %s %d", __get_str(foo), __entry->bar),
46362306a36Sopenharmony_ci
46462306a36Sopenharmony_ci	foo_bar_reg, foo_bar_unreg
46562306a36Sopenharmony_ci);
46662306a36Sopenharmony_ci
46762306a36Sopenharmony_ci/*
46862306a36Sopenharmony_ci * Each TRACE_EVENT macro creates several helper functions to produce
46962306a36Sopenharmony_ci * the code to add the tracepoint, create the files in the trace
47062306a36Sopenharmony_ci * directory, hook it to perf, assign the values and to print out
47162306a36Sopenharmony_ci * the raw data from the ring buffer. To prevent too much bloat,
47262306a36Sopenharmony_ci * if there are more than one tracepoint that uses the same format
47362306a36Sopenharmony_ci * for the proto, args, struct, assign and printk, and only the name
47462306a36Sopenharmony_ci * is different, it is highly recommended to use the DECLARE_EVENT_CLASS
47562306a36Sopenharmony_ci *
47662306a36Sopenharmony_ci * DECLARE_EVENT_CLASS() macro creates most of the functions for the
47762306a36Sopenharmony_ci * tracepoint. Then DEFINE_EVENT() is use to hook a tracepoint to those
47862306a36Sopenharmony_ci * functions. This DEFINE_EVENT() is an instance of the class and can
47962306a36Sopenharmony_ci * be enabled and disabled separately from other events (either TRACE_EVENT
48062306a36Sopenharmony_ci * or other DEFINE_EVENT()s).
48162306a36Sopenharmony_ci *
48262306a36Sopenharmony_ci * Note, TRACE_EVENT() itself is simply defined as:
48362306a36Sopenharmony_ci *
48462306a36Sopenharmony_ci * #define TRACE_EVENT(name, proto, args, tstruct, assign, printk)  \
48562306a36Sopenharmony_ci *  DECLARE_EVENT_CLASS(name, proto, args, tstruct, assign, printk); \
48662306a36Sopenharmony_ci *  DEFINE_EVENT(name, name, proto, args)
48762306a36Sopenharmony_ci *
48862306a36Sopenharmony_ci * The DEFINE_EVENT() also can be declared with conditions and reg functions:
48962306a36Sopenharmony_ci *
49062306a36Sopenharmony_ci * DEFINE_EVENT_CONDITION(template, name, proto, args, cond);
49162306a36Sopenharmony_ci * DEFINE_EVENT_FN(template, name, proto, args, reg, unreg);
49262306a36Sopenharmony_ci */
49362306a36Sopenharmony_ciDECLARE_EVENT_CLASS(foo_template,
49462306a36Sopenharmony_ci
49562306a36Sopenharmony_ci	TP_PROTO(const char *foo, int bar),
49662306a36Sopenharmony_ci
49762306a36Sopenharmony_ci	TP_ARGS(foo, bar),
49862306a36Sopenharmony_ci
49962306a36Sopenharmony_ci	TP_STRUCT__entry(
50062306a36Sopenharmony_ci		__string(	foo,    foo		)
50162306a36Sopenharmony_ci		__field(	int,	bar		)
50262306a36Sopenharmony_ci	),
50362306a36Sopenharmony_ci
50462306a36Sopenharmony_ci	TP_fast_assign(
50562306a36Sopenharmony_ci		__assign_str(foo, foo);
50662306a36Sopenharmony_ci		__entry->bar	= bar;
50762306a36Sopenharmony_ci	),
50862306a36Sopenharmony_ci
50962306a36Sopenharmony_ci	TP_printk("foo %s %d", __get_str(foo), __entry->bar)
51062306a36Sopenharmony_ci);
51162306a36Sopenharmony_ci
51262306a36Sopenharmony_ci/*
51362306a36Sopenharmony_ci * Here's a better way for the previous samples (except, the first
51462306a36Sopenharmony_ci * example had more fields and could not be used here).
51562306a36Sopenharmony_ci */
51662306a36Sopenharmony_ciDEFINE_EVENT(foo_template, foo_with_template_simple,
51762306a36Sopenharmony_ci	TP_PROTO(const char *foo, int bar),
51862306a36Sopenharmony_ci	TP_ARGS(foo, bar));
51962306a36Sopenharmony_ci
52062306a36Sopenharmony_ciDEFINE_EVENT_CONDITION(foo_template, foo_with_template_cond,
52162306a36Sopenharmony_ci	TP_PROTO(const char *foo, int bar),
52262306a36Sopenharmony_ci	TP_ARGS(foo, bar),
52362306a36Sopenharmony_ci	TP_CONDITION(!(bar % 8)));
52462306a36Sopenharmony_ci
52562306a36Sopenharmony_ci
52662306a36Sopenharmony_ciDEFINE_EVENT_FN(foo_template, foo_with_template_fn,
52762306a36Sopenharmony_ci	TP_PROTO(const char *foo, int bar),
52862306a36Sopenharmony_ci	TP_ARGS(foo, bar),
52962306a36Sopenharmony_ci	foo_bar_reg, foo_bar_unreg);
53062306a36Sopenharmony_ci
53162306a36Sopenharmony_ci/*
53262306a36Sopenharmony_ci * Anytime two events share basically the same values and have
53362306a36Sopenharmony_ci * the same output, use the DECLARE_EVENT_CLASS() and DEFINE_EVENT()
53462306a36Sopenharmony_ci * when ever possible.
53562306a36Sopenharmony_ci */
53662306a36Sopenharmony_ci
53762306a36Sopenharmony_ci/*
53862306a36Sopenharmony_ci * If the event is similar to the DECLARE_EVENT_CLASS, but you need
53962306a36Sopenharmony_ci * to have a different output, then use DEFINE_EVENT_PRINT() which
54062306a36Sopenharmony_ci * lets you override the TP_printk() of the class.
54162306a36Sopenharmony_ci */
54262306a36Sopenharmony_ci
54362306a36Sopenharmony_ciDEFINE_EVENT_PRINT(foo_template, foo_with_template_print,
54462306a36Sopenharmony_ci	TP_PROTO(const char *foo, int bar),
54562306a36Sopenharmony_ci	TP_ARGS(foo, bar),
54662306a36Sopenharmony_ci	TP_printk("bar %s %d", __get_str(foo), __entry->bar));
54762306a36Sopenharmony_ci
54862306a36Sopenharmony_ci/*
54962306a36Sopenharmony_ci * There are yet another __rel_loc dynamic data attribute. If you
55062306a36Sopenharmony_ci * use __rel_dynamic_array() and __rel_string() etc. macros, you
55162306a36Sopenharmony_ci * can use this attribute. There is no difference from the viewpoint
55262306a36Sopenharmony_ci * of functionality with/without 'rel' but the encoding is a bit
55362306a36Sopenharmony_ci * different. This is expected to be used with user-space event,
55462306a36Sopenharmony_ci * there is no reason that the kernel event use this, but only for
55562306a36Sopenharmony_ci * testing.
55662306a36Sopenharmony_ci */
55762306a36Sopenharmony_ci
55862306a36Sopenharmony_ciTRACE_EVENT(foo_rel_loc,
55962306a36Sopenharmony_ci
56062306a36Sopenharmony_ci	TP_PROTO(const char *foo, int bar, unsigned long *mask, const cpumask_t *cpus),
56162306a36Sopenharmony_ci
56262306a36Sopenharmony_ci	TP_ARGS(foo, bar, mask, cpus),
56362306a36Sopenharmony_ci
56462306a36Sopenharmony_ci	TP_STRUCT__entry(
56562306a36Sopenharmony_ci		__rel_string(	foo,	foo	)
56662306a36Sopenharmony_ci		__field(	int,	bar	)
56762306a36Sopenharmony_ci		__rel_bitmask(	bitmask,
56862306a36Sopenharmony_ci			BITS_PER_BYTE * sizeof(unsigned long)	)
56962306a36Sopenharmony_ci		__rel_cpumask(	cpumask )
57062306a36Sopenharmony_ci	),
57162306a36Sopenharmony_ci
57262306a36Sopenharmony_ci	TP_fast_assign(
57362306a36Sopenharmony_ci		__assign_rel_str(foo, foo);
57462306a36Sopenharmony_ci		__entry->bar = bar;
57562306a36Sopenharmony_ci		__assign_rel_bitmask(bitmask, mask,
57662306a36Sopenharmony_ci			BITS_PER_BYTE * sizeof(unsigned long));
57762306a36Sopenharmony_ci		__assign_rel_cpumask(cpumask, cpus);
57862306a36Sopenharmony_ci	),
57962306a36Sopenharmony_ci
58062306a36Sopenharmony_ci	TP_printk("foo_rel_loc %s, %d, %s, %s", __get_rel_str(foo), __entry->bar,
58162306a36Sopenharmony_ci		  __get_rel_bitmask(bitmask),
58262306a36Sopenharmony_ci		  __get_rel_cpumask(cpumask))
58362306a36Sopenharmony_ci);
58462306a36Sopenharmony_ci#endif
58562306a36Sopenharmony_ci
58662306a36Sopenharmony_ci/***** NOTICE! The #if protection ends here. *****/
58762306a36Sopenharmony_ci
58862306a36Sopenharmony_ci
58962306a36Sopenharmony_ci/*
59062306a36Sopenharmony_ci * There are several ways I could have done this. If I left out the
59162306a36Sopenharmony_ci * TRACE_INCLUDE_PATH, then it would default to the kernel source
59262306a36Sopenharmony_ci * include/trace/events directory.
59362306a36Sopenharmony_ci *
59462306a36Sopenharmony_ci * I could specify a path from the define_trace.h file back to this
59562306a36Sopenharmony_ci * file.
59662306a36Sopenharmony_ci *
59762306a36Sopenharmony_ci * #define TRACE_INCLUDE_PATH ../../samples/trace_events
59862306a36Sopenharmony_ci *
59962306a36Sopenharmony_ci * But the safest and easiest way to simply make it use the directory
60062306a36Sopenharmony_ci * that the file is in is to add in the Makefile:
60162306a36Sopenharmony_ci *
60262306a36Sopenharmony_ci * CFLAGS_trace-events-sample.o := -I$(src)
60362306a36Sopenharmony_ci *
60462306a36Sopenharmony_ci * This will make sure the current path is part of the include
60562306a36Sopenharmony_ci * structure for our file so that define_trace.h can find it.
60662306a36Sopenharmony_ci *
60762306a36Sopenharmony_ci * I could have made only the top level directory the include:
60862306a36Sopenharmony_ci *
60962306a36Sopenharmony_ci * CFLAGS_trace-events-sample.o := -I$(PWD)
61062306a36Sopenharmony_ci *
61162306a36Sopenharmony_ci * And then let the path to this directory be the TRACE_INCLUDE_PATH:
61262306a36Sopenharmony_ci *
61362306a36Sopenharmony_ci * #define TRACE_INCLUDE_PATH samples/trace_events
61462306a36Sopenharmony_ci *
61562306a36Sopenharmony_ci * But then if something defines "samples" or "trace_events" as a macro
61662306a36Sopenharmony_ci * then we could risk that being converted too, and give us an unexpected
61762306a36Sopenharmony_ci * result.
61862306a36Sopenharmony_ci */
61962306a36Sopenharmony_ci#undef TRACE_INCLUDE_PATH
62062306a36Sopenharmony_ci#undef TRACE_INCLUDE_FILE
62162306a36Sopenharmony_ci#define TRACE_INCLUDE_PATH .
62262306a36Sopenharmony_ci/*
62362306a36Sopenharmony_ci * TRACE_INCLUDE_FILE is not needed if the filename and TRACE_SYSTEM are equal
62462306a36Sopenharmony_ci */
62562306a36Sopenharmony_ci#define TRACE_INCLUDE_FILE trace-events-sample
62662306a36Sopenharmony_ci#include <trace/define_trace.h>
627