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 * __string_len: This is a helper to a __dynamic_array, but it understands
145 * that the array has characters in it, and with the combined
146 * use of __assign_str_len(), it will allocate 'len' + 1 bytes
147 * in the ring buffer and add a '\0' to the string. This is
148 * useful if the string being saved has no terminating '\0' byte.
149 * It requires that the length of the string is known as it acts
150 * like a memcpy().
151 *
152 * Declared with:
153 *
154 * __string_len(foo, bar, len)
155 *
156 * To assign this string, use the helper macro __assign_str_len().
157 *
158 * __assign_str_len(foo, bar, len);
159 *
160 * Then len + 1 is allocated to the ring buffer, and a nul terminating
161 * byte is added. This is similar to:
162 *
163 * memcpy(__get_str(foo), bar, len);
164 * __get_str(foo)[len] = 0;
165 *
166 * The advantage of using this over __dynamic_array, is that it
167 * takes care of allocating the extra byte on the ring buffer
168 * for the '\0' terminating byte, and __get_str(foo) can be used
169 * in the TP_printk().
170 *
171 * __bitmask: This is another kind of __dynamic_array, but it expects
172 * an array of longs, and the number of bits to parse. It takes
173 * two parameters (name, nr_bits), where name is the name of the
174 * bitmask to save, and the nr_bits is the number of bits to record.
175 *
176 * __bitmask(target_cpu, nr_cpumask_bits)
177 *
178 * To assign a bitmask, use the __assign_bitmask() helper macro.
179 *
180 * __assign_bitmask(target_cpus, cpumask_bits(bar), nr_cpumask_bits);
181 *
182 *
183 * fast_assign: This is a C like function that is used to store the items
184 * into the ring buffer. A special variable called "__entry" will be the
185 * structure that points into the ring buffer and has the same fields as
186 * described by the struct part of TRACE_EVENT above.
187 *
188 * printk: This is a way to print out the data in pretty print. This is
189 * useful if the system crashes and you are logging via a serial line,
190 * the data can be printed to the console using this "printk" method.
191 * This is also used to print out the data from the trace files.
192 * Again, the __entry macro is used to access the data from the ring buffer.
193 *
194 * Note, __dynamic_array, __string, and __bitmask require special helpers
195 * to access the data.
196 *
197 * For __dynamic_array(int, foo, bar) use __get_dynamic_array(foo)
198 * Use __get_dynamic_array_len(foo) to get the length of the array
199 * saved. Note, __get_dynamic_array_len() returns the total allocated
200 * length of the dynamic array; __print_array() expects the second
201 * parameter to be the number of elements. To get that, the array length
202 * needs to be divided by the element size.
203 *
204 * For __string(foo, bar) use __get_str(foo)
205 *
206 * For __bitmask(target_cpus, nr_cpumask_bits) use __get_bitmask(target_cpus)
207 *
208 *
209 * Note, that for both the assign and the printk, __entry is the handler
210 * to the data structure in the ring buffer, and is defined by the
211 * TP_STRUCT__entry.
212 */
213
214 /*
215 * It is OK to have helper functions in the file, but they need to be protected
216 * from being defined more than once. Remember, this file gets included more
217 * than once.
218 */
219 #ifndef __TRACE_EVENT_SAMPLE_HELPER_FUNCTIONS
220 #define __TRACE_EVENT_SAMPLE_HELPER_FUNCTIONS
__length_of(const int * list)221 static inline int __length_of(const int *list)
222 {
223 int i;
224
225 if (!list)
226 return 0;
227
228 for (i = 0; list[i]; i++)
229 ;
230 return i;
231 }
232
233 enum {
234 TRACE_SAMPLE_FOO = 2,
235 TRACE_SAMPLE_BAR = 4,
236 TRACE_SAMPLE_ZOO = 8,
237 };
238 #endif
239
240 /*
241 * If enums are used in the TP_printk(), their names will be shown in
242 * format files and not their values. This can cause problems with user
243 * space programs that parse the format files to know how to translate
244 * the raw binary trace output into human readable text.
245 *
246 * To help out user space programs, any enum that is used in the TP_printk()
247 * should be defined by TRACE_DEFINE_ENUM() macro. All that is needed to
248 * be done is to add this macro with the enum within it in the trace
249 * header file, and it will be converted in the output.
250 */
251
252 TRACE_DEFINE_ENUM(TRACE_SAMPLE_FOO);
253 TRACE_DEFINE_ENUM(TRACE_SAMPLE_BAR);
254 TRACE_DEFINE_ENUM(TRACE_SAMPLE_ZOO);
255
256 TRACE_EVENT(foo_bar,
257
258 TP_PROTO(const char *foo, int bar, const int *lst,
259 const char *string, const struct cpumask *mask),
260
261 TP_ARGS(foo, bar, lst, string, mask),
262
263 TP_STRUCT__entry(
264 __array( char, foo, 10 )
265 __field( int, bar )
266 __dynamic_array(int, list, __length_of(lst))
267 __string( str, string )
268 __bitmask( cpus, num_possible_cpus() )
269 ),
270
271 TP_fast_assign(
272 strlcpy(__entry->foo, foo, 10);
273 __entry->bar = bar;
274 memcpy(__get_dynamic_array(list), lst,
275 __length_of(lst) * sizeof(int));
276 __assign_str(str, string);
277 __assign_bitmask(cpus, cpumask_bits(mask), num_possible_cpus());
278 ),
279
280 TP_printk("foo %s %d %s %s %s %s (%s)", __entry->foo, __entry->bar,
281
282 /*
283 * Notice here the use of some helper functions. This includes:
284 *
285 * __print_symbolic( variable, { value, "string" }, ... ),
286 *
287 * The variable is tested against each value of the { } pair. If
288 * the variable matches one of the values, then it will print the
289 * string in that pair. If non are matched, it returns a string
290 * version of the number (if __entry->bar == 7 then "7" is returned).
291 */
292 __print_symbolic(__entry->bar,
293 { 0, "zero" },
294 { TRACE_SAMPLE_FOO, "TWO" },
295 { TRACE_SAMPLE_BAR, "FOUR" },
296 { TRACE_SAMPLE_ZOO, "EIGHT" },
297 { 10, "TEN" }
298 ),
299
300 /*
301 * __print_flags( variable, "delim", { value, "flag" }, ... ),
302 *
303 * This is similar to __print_symbolic, except that it tests the bits
304 * of the value. If ((FLAG & variable) == FLAG) then the string is
305 * printed. If more than one flag matches, then each one that does is
306 * also printed with delim in between them.
307 * If not all bits are accounted for, then the not found bits will be
308 * added in hex format: 0x506 will show BIT2|BIT4|0x500
309 */
310 __print_flags(__entry->bar, "|",
311 { 1, "BIT1" },
312 { 2, "BIT2" },
313 { 4, "BIT3" },
314 { 8, "BIT4" }
315 ),
316 /*
317 * __print_array( array, len, element_size )
318 *
319 * This prints out the array that is defined by __array in a nice format.
320 */
321 __print_array(__get_dynamic_array(list),
322 __get_dynamic_array_len(list) / sizeof(int),
323 sizeof(int)),
324 __get_str(str), __get_bitmask(cpus))
325 );
326
327 /*
328 * There may be a case where a tracepoint should only be called if
329 * some condition is set. Otherwise the tracepoint should not be called.
330 * But to do something like:
331 *
332 * if (cond)
333 * trace_foo();
334 *
335 * Would cause a little overhead when tracing is not enabled, and that
336 * overhead, even if small, is not something we want. As tracepoints
337 * use static branch (aka jump_labels), where no branch is taken to
338 * skip the tracepoint when not enabled, and a jmp is placed to jump
339 * to the tracepoint code when it is enabled, having a if statement
340 * nullifies that optimization. It would be nice to place that
341 * condition within the static branch. This is where TRACE_EVENT_CONDITION
342 * comes in.
343 *
344 * TRACE_EVENT_CONDITION() is just like TRACE_EVENT, except it adds another
345 * parameter just after args. Where TRACE_EVENT has:
346 *
347 * TRACE_EVENT(name, proto, args, struct, assign, printk)
348 *
349 * the CONDITION version has:
350 *
351 * TRACE_EVENT_CONDITION(name, proto, args, cond, struct, assign, printk)
352 *
353 * Everything is the same as TRACE_EVENT except for the new cond. Think
354 * of the cond variable as:
355 *
356 * if (cond)
357 * trace_foo_bar_with_cond();
358 *
359 * Except that the logic for the if branch is placed after the static branch.
360 * That is, the if statement that processes the condition will not be
361 * executed unless that traecpoint is enabled. Otherwise it still remains
362 * a nop.
363 */
364 TRACE_EVENT_CONDITION(foo_bar_with_cond,
365
366 TP_PROTO(const char *foo, int bar),
367
368 TP_ARGS(foo, bar),
369
370 TP_CONDITION(!(bar % 10)),
371
372 TP_STRUCT__entry(
373 __string( foo, foo )
374 __field( int, bar )
375 ),
376
377 TP_fast_assign(
378 __assign_str(foo, foo);
379 __entry->bar = bar;
380 ),
381
382 TP_printk("foo %s %d", __get_str(foo), __entry->bar)
383 );
384
385 int foo_bar_reg(void);
386 void foo_bar_unreg(void);
387
388 /*
389 * Now in the case that some function needs to be called when the
390 * tracepoint is enabled and/or when it is disabled, the
391 * TRACE_EVENT_FN() serves this purpose. This is just like TRACE_EVENT()
392 * but adds two more parameters at the end:
393 *
394 * TRACE_EVENT_FN( name, proto, args, struct, assign, printk, reg, unreg)
395 *
396 * reg and unreg are functions with the prototype of:
397 *
398 * void reg(void)
399 *
400 * The reg function gets called before the tracepoint is enabled, and
401 * the unreg function gets called after the tracepoint is disabled.
402 *
403 * Note, reg and unreg are allowed to be NULL. If you only need to
404 * call a function before enabling, or after disabling, just set one
405 * function and pass in NULL for the other parameter.
406 */
407 TRACE_EVENT_FN(foo_bar_with_fn,
408
409 TP_PROTO(const char *foo, int bar),
410
411 TP_ARGS(foo, bar),
412
413 TP_STRUCT__entry(
414 __string( foo, foo )
415 __field( int, bar )
416 ),
417
418 TP_fast_assign(
419 __assign_str(foo, foo);
420 __entry->bar = bar;
421 ),
422
423 TP_printk("foo %s %d", __get_str(foo), __entry->bar),
424
425 foo_bar_reg, foo_bar_unreg
426 );
427
428 /*
429 * Each TRACE_EVENT macro creates several helper functions to produce
430 * the code to add the tracepoint, create the files in the trace
431 * directory, hook it to perf, assign the values and to print out
432 * the raw data from the ring buffer. To prevent too much bloat,
433 * if there are more than one tracepoint that uses the same format
434 * for the proto, args, struct, assign and printk, and only the name
435 * is different, it is highly recommended to use the DECLARE_EVENT_CLASS
436 *
437 * DECLARE_EVENT_CLASS() macro creates most of the functions for the
438 * tracepoint. Then DEFINE_EVENT() is use to hook a tracepoint to those
439 * functions. This DEFINE_EVENT() is an instance of the class and can
440 * be enabled and disabled separately from other events (either TRACE_EVENT
441 * or other DEFINE_EVENT()s).
442 *
443 * Note, TRACE_EVENT() itself is simply defined as:
444 *
445 * #define TRACE_EVENT(name, proto, args, tstruct, assign, printk) \
446 * DECLARE_EVENT_CLASS(name, proto, args, tstruct, assign, printk); \
447 * DEFINE_EVENT(name, name, proto, args)
448 *
449 * The DEFINE_EVENT() also can be declared with conditions and reg functions:
450 *
451 * DEFINE_EVENT_CONDITION(template, name, proto, args, cond);
452 * DEFINE_EVENT_FN(template, name, proto, args, reg, unreg);
453 */
454 DECLARE_EVENT_CLASS(foo_template,
455
456 TP_PROTO(const char *foo, int bar),
457
458 TP_ARGS(foo, bar),
459
460 TP_STRUCT__entry(
461 __string( foo, foo )
462 __field( int, bar )
463 ),
464
465 TP_fast_assign(
466 __assign_str(foo, foo);
467 __entry->bar = bar;
468 ),
469
470 TP_printk("foo %s %d", __get_str(foo), __entry->bar)
471 );
472
473 /*
474 * Here's a better way for the previous samples (except, the first
475 * example had more fields and could not be used here).
476 */
477 DEFINE_EVENT(foo_template, foo_with_template_simple,
478 TP_PROTO(const char *foo, int bar),
479 TP_ARGS(foo, bar));
480
481 DEFINE_EVENT_CONDITION(foo_template, foo_with_template_cond,
482 TP_PROTO(const char *foo, int bar),
483 TP_ARGS(foo, bar),
484 TP_CONDITION(!(bar % 8)));
485
486
487 DEFINE_EVENT_FN(foo_template, foo_with_template_fn,
488 TP_PROTO(const char *foo, int bar),
489 TP_ARGS(foo, bar),
490 foo_bar_reg, foo_bar_unreg);
491
492 /*
493 * Anytime two events share basically the same values and have
494 * the same output, use the DECLARE_EVENT_CLASS() and DEFINE_EVENT()
495 * when ever possible.
496 */
497
498 /*
499 * If the event is similar to the DECLARE_EVENT_CLASS, but you need
500 * to have a different output, then use DEFINE_EVENT_PRINT() which
501 * lets you override the TP_printk() of the class.
502 */
503
504 DEFINE_EVENT_PRINT(foo_template, foo_with_template_print,
505 TP_PROTO(const char *foo, int bar),
506 TP_ARGS(foo, bar),
507 TP_printk("bar %s %d", __get_str(foo), __entry->bar));
508
509 /*
510 * There are yet another __rel_loc dynamic data attribute. If you
511 * use __rel_dynamic_array() and __rel_string() etc. macros, you
512 * can use this attribute. There is no difference from the viewpoint
513 * of functionality with/without 'rel' but the encoding is a bit
514 * different. This is expected to be used with user-space event,
515 * there is no reason that the kernel event use this, but only for
516 * testing.
517 */
518
519 TRACE_EVENT(foo_rel_loc,
520
521 TP_PROTO(const char *foo, int bar, unsigned long *mask),
522
523 TP_ARGS(foo, bar, mask),
524
525 TP_STRUCT__entry(
526 __rel_string( foo, foo )
527 __field( int, bar )
528 __rel_bitmask( bitmask,
529 BITS_PER_BYTE * sizeof(unsigned long) )
530 ),
531
532 TP_fast_assign(
533 __assign_rel_str(foo, foo);
534 __entry->bar = bar;
535 __assign_rel_bitmask(bitmask, mask,
536 BITS_PER_BYTE * sizeof(unsigned long));
537 ),
538
539 TP_printk("foo_rel_loc %s, %d, %s", __get_rel_str(foo), __entry->bar,
540 __get_rel_bitmask(bitmask))
541 );
542 #endif
543
544 /***** NOTICE! The #if protection ends here. *****/
545
546
547 /*
548 * There are several ways I could have done this. If I left out the
549 * TRACE_INCLUDE_PATH, then it would default to the kernel source
550 * include/trace/events directory.
551 *
552 * I could specify a path from the define_trace.h file back to this
553 * file.
554 *
555 * #define TRACE_INCLUDE_PATH ../../samples/trace_events
556 *
557 * But the safest and easiest way to simply make it use the directory
558 * that the file is in is to add in the Makefile:
559 *
560 * CFLAGS_trace-events-sample.o := -I$(src)
561 *
562 * This will make sure the current path is part of the include
563 * structure for our file so that define_trace.h can find it.
564 *
565 * I could have made only the top level directory the include:
566 *
567 * CFLAGS_trace-events-sample.o := -I$(PWD)
568 *
569 * And then let the path to this directory be the TRACE_INCLUDE_PATH:
570 *
571 * #define TRACE_INCLUDE_PATH samples/trace_events
572 *
573 * But then if something defines "samples" or "trace_events" as a macro
574 * then we could risk that being converted too, and give us an unexpected
575 * result.
576 */
577 #undef TRACE_INCLUDE_PATH
578 #undef TRACE_INCLUDE_FILE
579 #define TRACE_INCLUDE_PATH .
580 /*
581 * TRACE_INCLUDE_FILE is not needed if the filename and TRACE_SYSTEM are equal
582 */
583 #define TRACE_INCLUDE_FILE trace-events-sample
584 #include <trace/define_trace.h>
585