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.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". 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 is 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.
172  *
173  *      For __string(foo, bar) use __get_str(foo)
174  *
175  *      For __bitmask(target_cpus, nr_cpumask_bits) use __get_bitmask(target_cpus)
176  *
177  *
178  * Note, that for both the assign and the printk, __entry is the handler
179  * to the data structure in the ring buffer, and is defined by the
180  * TP_STRUCT__entry.
181  */
182 
183 /*
184  * It is OK to have helper functions in the file, but they need to be protected
185  * from being defined more than once. Remember, this file gets included more
186  * than once.
187  */
188 #ifndef __TRACE_EVENT_SAMPLE_HELPER_FUNCTIONS
189 #define __TRACE_EVENT_SAMPLE_HELPER_FUNCTIONS
190 static inline int __length_of(const int *list)
191 {
192 	int i;
193 
194 	if (!list)
195 		return 0;
196 
197 	for (i = 0; list[i]; i++)
198 		;
199 	return i;
200 }
201 #endif
202 
203 TRACE_EVENT(foo_bar,
204 
205 	TP_PROTO(const char *foo, int bar, const int *lst,
206 		 const char *string, const struct cpumask *mask),
207 
208 	TP_ARGS(foo, bar, lst, string, mask),
209 
210 	TP_STRUCT__entry(
211 		__array(	char,	foo,    10		)
212 		__field(	int,	bar			)
213 		__dynamic_array(int,	list,   __length_of(lst))
214 		__string(	str,	string			)
215 		__bitmask(	cpus,	num_possible_cpus()	)
216 	),
217 
218 	TP_fast_assign(
219 		strlcpy(__entry->foo, foo, 10);
220 		__entry->bar	= bar;
221 		memcpy(__get_dynamic_array(list), lst,
222 		       __length_of(lst) * sizeof(int));
223 		__assign_str(str, string);
224 		__assign_bitmask(cpus, cpumask_bits(mask), num_possible_cpus());
225 	),
226 
227 	TP_printk("foo %s %d %s %s (%s)", __entry->foo, __entry->bar,
228 		  __print_array(__get_dynamic_array(list),
229 				__get_dynamic_array_len(list),
230 				sizeof(int)),
231 		  __get_str(str), __get_bitmask(cpus))
232 );
233 
234 /*
235  * There may be a case where a tracepoint should only be called if
236  * some condition is set. Otherwise the tracepoint should not be called.
237  * But to do something like:
238  *
239  *  if (cond)
240  *     trace_foo();
241  *
242  * Would cause a little overhead when tracing is not enabled, and that
243  * overhead, even if small, is not something we want. As tracepoints
244  * use static branch (aka jump_labels), where no branch is taken to
245  * skip the tracepoint when not enabled, and a jmp is placed to jump
246  * to the tracepoint code when it is enabled, having a if statement
247  * nullifies that optimization. It would be nice to place that
248  * condition within the static branch. This is where TRACE_EVENT_CONDITION
249  * comes in.
250  *
251  * TRACE_EVENT_CONDITION() is just like TRACE_EVENT, except it adds another
252  * parameter just after args. Where TRACE_EVENT has:
253  *
254  * TRACE_EVENT(name, proto, args, struct, assign, printk)
255  *
256  * the CONDITION version has:
257  *
258  * TRACE_EVENT_CONDITION(name, proto, args, cond, struct, assign, printk)
259  *
260  * Everything is the same as TRACE_EVENT except for the new cond. Think
261  * of the cond variable as:
262  *
263  *   if (cond)
264  *      trace_foo_bar_with_cond();
265  *
266  * Except that the logic for the if branch is placed after the static branch.
267  * That is, the if statement that processes the condition will not be
268  * executed unless that traecpoint is enabled. Otherwise it still remains
269  * a nop.
270  */
271 TRACE_EVENT_CONDITION(foo_bar_with_cond,
272 
273 	TP_PROTO(const char *foo, int bar),
274 
275 	TP_ARGS(foo, bar),
276 
277 	TP_CONDITION(!(bar % 10)),
278 
279 	TP_STRUCT__entry(
280 		__string(	foo,    foo		)
281 		__field(	int,	bar			)
282 	),
283 
284 	TP_fast_assign(
285 		__assign_str(foo, foo);
286 		__entry->bar	= bar;
287 	),
288 
289 	TP_printk("foo %s %d", __get_str(foo), __entry->bar)
290 );
291 
292 void foo_bar_reg(void);
293 void foo_bar_unreg(void);
294 
295 /*
296  * Now in the case that some function needs to be called when the
297  * tracepoint is enabled and/or when it is disabled, the
298  * TRACE_EVENT_FN() serves this purpose. This is just like TRACE_EVENT()
299  * but adds two more parameters at the end:
300  *
301  * TRACE_EVENT_FN( name, proto, args, struct, assign, printk, reg, unreg)
302  *
303  * reg and unreg are functions with the prototype of:
304  *
305  *    void reg(void)
306  *
307  * The reg function gets called before the tracepoint is enabled, and
308  * the unreg function gets called after the tracepoint is disabled.
309  *
310  * Note, reg and unreg are allowed to be NULL. If you only need to
311  * call a function before enabling, or after disabling, just set one
312  * function and pass in NULL for the other parameter.
313  */
314 TRACE_EVENT_FN(foo_bar_with_fn,
315 
316 	TP_PROTO(const char *foo, int bar),
317 
318 	TP_ARGS(foo, bar),
319 
320 	TP_STRUCT__entry(
321 		__string(	foo,    foo		)
322 		__field(	int,	bar		)
323 	),
324 
325 	TP_fast_assign(
326 		__assign_str(foo, foo);
327 		__entry->bar	= bar;
328 	),
329 
330 	TP_printk("foo %s %d", __get_str(foo), __entry->bar),
331 
332 	foo_bar_reg, foo_bar_unreg
333 );
334 
335 /*
336  * Each TRACE_EVENT macro creates several helper functions to produce
337  * the code to add the tracepoint, create the files in the trace
338  * directory, hook it to perf, assign the values and to print out
339  * the raw data from the ring buffer. To prevent too much bloat,
340  * if there are more than one tracepoint that uses the same format
341  * for the proto, args, struct, assign and printk, and only the name
342  * is different, it is highly recommended to use the DECLARE_EVENT_CLASS
343  *
344  * DECLARE_EVENT_CLASS() macro creates most of the functions for the
345  * tracepoint. Then DEFINE_EVENT() is use to hook a tracepoint to those
346  * functions. This DEFINE_EVENT() is an instance of the class and can
347  * be enabled and disabled separately from other events (either TRACE_EVENT
348  * or other DEFINE_EVENT()s).
349  *
350  * Note, TRACE_EVENT() itself is simply defined as:
351  *
352  * #define TRACE_EVENT(name, proto, args, tstruct, assign, printk)  \
353  *  DEFINE_EVENT_CLASS(name, proto, args, tstruct, assign, printk); \
354  *  DEFINE_EVENT(name, name, proto, args)
355  *
356  * The DEFINE_EVENT() also can be declared with conditions and reg functions:
357  *
358  * DEFINE_EVENT_CONDITION(template, name, proto, args, cond);
359  * DEFINE_EVENT_FN(template, name, proto, args, reg, unreg);
360  */
361 DECLARE_EVENT_CLASS(foo_template,
362 
363 	TP_PROTO(const char *foo, int bar),
364 
365 	TP_ARGS(foo, bar),
366 
367 	TP_STRUCT__entry(
368 		__string(	foo,    foo		)
369 		__field(	int,	bar		)
370 	),
371 
372 	TP_fast_assign(
373 		__assign_str(foo, foo);
374 		__entry->bar	= bar;
375 	),
376 
377 	TP_printk("foo %s %d", __get_str(foo), __entry->bar)
378 );
379 
380 /*
381  * Here's a better way for the previous samples (except, the first
382  * exmaple had more fields and could not be used here).
383  */
384 DEFINE_EVENT(foo_template, foo_with_template_simple,
385 	TP_PROTO(const char *foo, int bar),
386 	TP_ARGS(foo, bar));
387 
388 DEFINE_EVENT_CONDITION(foo_template, foo_with_template_cond,
389 	TP_PROTO(const char *foo, int bar),
390 	TP_ARGS(foo, bar),
391 	TP_CONDITION(!(bar % 8)));
392 
393 
394 DEFINE_EVENT_FN(foo_template, foo_with_template_fn,
395 	TP_PROTO(const char *foo, int bar),
396 	TP_ARGS(foo, bar),
397 	foo_bar_reg, foo_bar_unreg);
398 
399 /*
400  * Anytime two events share basically the same values and have
401  * the same output, use the DECLARE_EVENT_CLASS() and DEFINE_EVENT()
402  * when ever possible.
403  */
404 
405 /*
406  * If the event is similar to the DECLARE_EVENT_CLASS, but you need
407  * to have a different output, then use DEFINE_EVENT_PRINT() which
408  * lets you override the TP_printk() of the class.
409  */
410 
411 DEFINE_EVENT_PRINT(foo_template, foo_with_template_print,
412 	TP_PROTO(const char *foo, int bar),
413 	TP_ARGS(foo, bar),
414 	TP_printk("bar %s %d", __get_str(foo), __entry->bar));
415 
416 #endif
417 
418 /***** NOTICE! The #if protection ends here. *****/
419 
420 
421 /*
422  * There are several ways I could have done this. If I left out the
423  * TRACE_INCLUDE_PATH, then it would default to the kernel source
424  * include/trace/events directory.
425  *
426  * I could specify a path from the define_trace.h file back to this
427  * file.
428  *
429  * #define TRACE_INCLUDE_PATH ../../samples/trace_events
430  *
431  * But the safest and easiest way to simply make it use the directory
432  * that the file is in is to add in the Makefile:
433  *
434  * CFLAGS_trace-events-sample.o := -I$(src)
435  *
436  * This will make sure the current path is part of the include
437  * structure for our file so that define_trace.h can find it.
438  *
439  * I could have made only the top level directory the include:
440  *
441  * CFLAGS_trace-events-sample.o := -I$(PWD)
442  *
443  * And then let the path to this directory be the TRACE_INCLUDE_PATH:
444  *
445  * #define TRACE_INCLUDE_PATH samples/trace_events
446  *
447  * But then if something defines "samples" or "trace_events" as a macro
448  * then we could risk that being converted too, and give us an unexpected
449  * result.
450  */
451 #undef TRACE_INCLUDE_PATH
452 #undef TRACE_INCLUDE_FILE
453 #define TRACE_INCLUDE_PATH .
454 /*
455  * TRACE_INCLUDE_FILE is not needed if the filename and TRACE_SYSTEM are equal
456  */
457 #define TRACE_INCLUDE_FILE trace-events-sample
458 #include <trace/define_trace.h>
459