![]() |
|
|||
0001 /* SPDX-License-Identifier: GPL-2.0 */ 0002 /* 0003 * If TRACE_SYSTEM is defined, that will be the directory created 0004 * in the ftrace directory under /sys/kernel/tracing/events/<system> 0005 * 0006 * The define_trace.h below will also look for a file name of 0007 * TRACE_SYSTEM.h where TRACE_SYSTEM is what is defined here. 0008 * In this case, it would look for sample-trace.h 0009 * 0010 * If the header name will be different than the system name 0011 * (as in this case), then you can override the header name that 0012 * define_trace.h will look up by defining TRACE_INCLUDE_FILE 0013 * 0014 * This file is called trace-events-sample.h but we want the system 0015 * to be called "sample-trace". Therefore we must define the name of this 0016 * file: 0017 * 0018 * #define TRACE_INCLUDE_FILE trace-events-sample 0019 * 0020 * As we do an the bottom of this file. 0021 * 0022 * Notice that TRACE_SYSTEM should be defined outside of #if 0023 * protection, just like TRACE_INCLUDE_FILE. 0024 */ 0025 #undef TRACE_SYSTEM 0026 #define TRACE_SYSTEM sample-trace 0027 0028 /* 0029 * TRACE_SYSTEM is expected to be a C valid variable (alpha-numeric 0030 * and underscore), although it may start with numbers. If for some 0031 * reason it is not, you need to add the following lines: 0032 */ 0033 #undef TRACE_SYSTEM_VAR 0034 #define TRACE_SYSTEM_VAR sample_trace 0035 /* 0036 * But the above is only needed if TRACE_SYSTEM is not alpha-numeric 0037 * and underscored. By default, TRACE_SYSTEM_VAR will be equal to 0038 * TRACE_SYSTEM. As TRACE_SYSTEM_VAR must be alpha-numeric, if 0039 * TRACE_SYSTEM is not, then TRACE_SYSTEM_VAR must be defined with 0040 * only alpha-numeric and underscores. 0041 * 0042 * The TRACE_SYSTEM_VAR is only used internally and not visible to 0043 * user space. 0044 */ 0045 0046 /* 0047 * Notice that this file is not protected like a normal header. 0048 * We also must allow for rereading of this file. The 0049 * 0050 * || defined(TRACE_HEADER_MULTI_READ) 0051 * 0052 * serves this purpose. 0053 */ 0054 #if !defined(_TRACE_EVENT_SAMPLE_H) || defined(TRACE_HEADER_MULTI_READ) 0055 #define _TRACE_EVENT_SAMPLE_H 0056 0057 /* 0058 * All trace headers should include tracepoint.h, until we finally 0059 * make it into a standard header. 0060 */ 0061 #include <linux/tracepoint.h> 0062 0063 /* 0064 * The TRACE_EVENT macro is broken up into 5 parts. 0065 * 0066 * name: name of the trace point. This is also how to enable the tracepoint. 0067 * A function called trace_foo_bar() will be created. 0068 * 0069 * proto: the prototype of the function trace_foo_bar() 0070 * Here it is trace_foo_bar(char *foo, int bar). 0071 * 0072 * args: must match the arguments in the prototype. 0073 * Here it is simply "foo, bar". 0074 * 0075 * struct: This defines the way the data will be stored in the ring buffer. 0076 * The items declared here become part of a special structure 0077 * called "__entry", which can be used in the fast_assign part of the 0078 * TRACE_EVENT macro. 0079 * 0080 * Here are the currently defined types you can use: 0081 * 0082 * __field : Is broken up into type and name. Where type can be any 0083 * primitive type (integer, long or pointer). 0084 * 0085 * __field(int, foo) 0086 * 0087 * __entry->foo = 5; 0088 * 0089 * __field_struct : This can be any static complex data type (struct, union 0090 * but not an array). Be careful using complex types, as each 0091 * event is limited in size, and copying large amounts of data 0092 * into the ring buffer can slow things down. 0093 * 0094 * __field_struct(struct bar, foo) 0095 * 0096 * __entry->bar.x = y; 0097 0098 * __array: There are three fields (type, name, size). The type is the 0099 * type of elements in the array, the name is the name of the array. 0100 * size is the number of items in the array (not the total size). 0101 * 0102 * __array( char, foo, 10) is the same as saying: char foo[10]; 0103 * 0104 * Assigning arrays can be done like any array: 0105 * 0106 * __entry->foo[0] = 'a'; 0107 * 0108 * memcpy(__entry->foo, bar, 10); 0109 * 0110 * __dynamic_array: This is similar to array, but can vary its size from 0111 * instance to instance of the tracepoint being called. 0112 * Like __array, this too has three elements (type, name, size); 0113 * type is the type of the element, name is the name of the array. 0114 * The size is different than __array. It is not a static number, 0115 * but the algorithm to figure out the length of the array for the 0116 * specific instance of tracepoint. Again, size is the number of 0117 * items in the array, not the total length in bytes. 0118 * 0119 * __dynamic_array( int, foo, bar) is similar to: int foo[bar]; 0120 * 0121 * Note, unlike arrays, you must use the __get_dynamic_array() macro 0122 * to access the array. 0123 * 0124 * memcpy(__get_dynamic_array(foo), bar, 10); 0125 * 0126 * Notice, that "__entry" is not needed here. 0127 * 0128 * __string: This is a special kind of __dynamic_array. It expects to 0129 * have a null terminated character array passed to it (it allows 0130 * for NULL too, which would be converted into "(null)"). __string 0131 * takes two parameter (name, src), where name is the name of 0132 * the string saved, and src is the string to copy into the 0133 * ring buffer. 0134 * 0135 * __string(foo, bar) is similar to: strcpy(foo, bar) 0136 * 0137 * To assign a string, use the helper macro __assign_str(). 0138 * 0139 * __assign_str(foo, bar); 0140 * 0141 * In most cases, the __assign_str() macro will take the same 0142 * parameters as the __string() macro had to declare the string. 0143 * 0144 * __vstring: This is similar to __string() but instead of taking a 0145 * dynamic length, it takes a variable list va_list 'va' variable. 0146 * Some event callers already have a message from parameters saved 0147 * in a va_list. Passing in the format and the va_list variable 0148 * will save just enough on the ring buffer for that string. 0149 * Note, the va variable used is a pointer to a va_list, not 0150 * to the va_list directly. 0151 * 0152 * (va_list *va) 0153 * 0154 * __vstring(foo, fmt, va) is similar to: vsnprintf(foo, fmt, va) 0155 * 0156 * To assign the string, use the helper macro __assign_vstr(). 0157 * 0158 * __assign_vstr(foo, fmt, va); 0159 * 0160 * In most cases, the __assign_vstr() macro will take the same 0161 * parameters as the __vstring() macro had to declare the string. 0162 * Use __get_str() to retrieve the __vstring() just like it would for 0163 * __string(). 0164 * 0165 * __string_len: This is a helper to a __dynamic_array, but it understands 0166 * that the array has characters in it, and with the combined 0167 * use of __assign_str_len(), it will allocate 'len' + 1 bytes 0168 * in the ring buffer and add a '\0' to the string. This is 0169 * useful if the string being saved has no terminating '\0' byte. 0170 * It requires that the length of the string is known as it acts 0171 * like a memcpy(). 0172 * 0173 * Declared with: 0174 * 0175 * __string_len(foo, bar, len) 0176 * 0177 * To assign this string, use the helper macro __assign_str_len(). 0178 * 0179 * __assign_str_len(foo, bar, len); 0180 * 0181 * Then len + 1 is allocated to the ring buffer, and a nul terminating 0182 * byte is added. This is similar to: 0183 * 0184 * memcpy(__get_str(foo), bar, len); 0185 * __get_str(foo)[len] = 0; 0186 * 0187 * The advantage of using this over __dynamic_array, is that it 0188 * takes care of allocating the extra byte on the ring buffer 0189 * for the '\0' terminating byte, and __get_str(foo) can be used 0190 * in the TP_printk(). 0191 * 0192 * __bitmask: This is another kind of __dynamic_array, but it expects 0193 * an array of longs, and the number of bits to parse. It takes 0194 * two parameters (name, nr_bits), where name is the name of the 0195 * bitmask to save, and the nr_bits is the number of bits to record. 0196 * 0197 * __bitmask(target_cpu, nr_cpumask_bits) 0198 * 0199 * To assign a bitmask, use the __assign_bitmask() helper macro. 0200 * 0201 * __assign_bitmask(target_cpus, cpumask_bits(bar), nr_cpumask_bits); 0202 * 0203 * 0204 * fast_assign: This is a C like function that is used to store the items 0205 * into the ring buffer. A special variable called "__entry" will be the 0206 * structure that points into the ring buffer and has the same fields as 0207 * described by the struct part of TRACE_EVENT above. 0208 * 0209 * printk: This is a way to print out the data in pretty print. This is 0210 * useful if the system crashes and you are logging via a serial line, 0211 * the data can be printed to the console using this "printk" method. 0212 * This is also used to print out the data from the trace files. 0213 * Again, the __entry macro is used to access the data from the ring buffer. 0214 * 0215 * Note, __dynamic_array, __string, and __bitmask require special helpers 0216 * to access the data. 0217 * 0218 * For __dynamic_array(int, foo, bar) use __get_dynamic_array(foo) 0219 * Use __get_dynamic_array_len(foo) to get the length of the array 0220 * saved. Note, __get_dynamic_array_len() returns the total allocated 0221 * length of the dynamic array; __print_array() expects the second 0222 * parameter to be the number of elements. To get that, the array length 0223 * needs to be divided by the element size. 0224 * 0225 * For __string(foo, bar) use __get_str(foo) 0226 * 0227 * For __bitmask(target_cpus, nr_cpumask_bits) use __get_bitmask(target_cpus) 0228 * 0229 * 0230 * Note, that for both the assign and the printk, __entry is the handler 0231 * to the data structure in the ring buffer, and is defined by the 0232 * TP_STRUCT__entry. 0233 */ 0234 0235 /* 0236 * It is OK to have helper functions in the file, but they need to be protected 0237 * from being defined more than once. Remember, this file gets included more 0238 * than once. 0239 */ 0240 #ifndef __TRACE_EVENT_SAMPLE_HELPER_FUNCTIONS 0241 #define __TRACE_EVENT_SAMPLE_HELPER_FUNCTIONS 0242 static inline int __length_of(const int *list) 0243 { 0244 int i; 0245 0246 if (!list) 0247 return 0; 0248 0249 for (i = 0; list[i]; i++) 0250 ; 0251 return i; 0252 } 0253 0254 enum { 0255 TRACE_SAMPLE_FOO = 2, 0256 TRACE_SAMPLE_BAR = 4, 0257 TRACE_SAMPLE_ZOO = 8, 0258 }; 0259 #endif 0260 0261 /* 0262 * If enums are used in the TP_printk(), their names will be shown in 0263 * format files and not their values. This can cause problems with user 0264 * space programs that parse the format files to know how to translate 0265 * the raw binary trace output into human readable text. 0266 * 0267 * To help out user space programs, any enum that is used in the TP_printk() 0268 * should be defined by TRACE_DEFINE_ENUM() macro. All that is needed to 0269 * be done is to add this macro with the enum within it in the trace 0270 * header file, and it will be converted in the output. 0271 */ 0272 0273 TRACE_DEFINE_ENUM(TRACE_SAMPLE_FOO); 0274 TRACE_DEFINE_ENUM(TRACE_SAMPLE_BAR); 0275 TRACE_DEFINE_ENUM(TRACE_SAMPLE_ZOO); 0276 0277 TRACE_EVENT(foo_bar, 0278 0279 TP_PROTO(const char *foo, int bar, const int *lst, 0280 const char *string, const struct cpumask *mask, 0281 const char *fmt, va_list *va), 0282 0283 TP_ARGS(foo, bar, lst, string, mask, fmt, va), 0284 0285 TP_STRUCT__entry( 0286 __array( char, foo, 10 ) 0287 __field( int, bar ) 0288 __dynamic_array(int, list, __length_of(lst)) 0289 __string( str, string ) 0290 __bitmask( cpus, num_possible_cpus() ) 0291 __vstring( vstr, fmt, va ) 0292 ), 0293 0294 TP_fast_assign( 0295 strlcpy(__entry->foo, foo, 10); 0296 __entry->bar = bar; 0297 memcpy(__get_dynamic_array(list), lst, 0298 __length_of(lst) * sizeof(int)); 0299 __assign_str(str, string); 0300 __assign_vstr(vstr, fmt, va); 0301 __assign_bitmask(cpus, cpumask_bits(mask), num_possible_cpus()); 0302 ), 0303 0304 TP_printk("foo %s %d %s %s %s %s (%s) %s", __entry->foo, __entry->bar, 0305 0306 /* 0307 * Notice here the use of some helper functions. This includes: 0308 * 0309 * __print_symbolic( variable, { value, "string" }, ... ), 0310 * 0311 * The variable is tested against each value of the { } pair. If 0312 * the variable matches one of the values, then it will print the 0313 * string in that pair. If non are matched, it returns a string 0314 * version of the number (if __entry->bar == 7 then "7" is returned). 0315 */ 0316 __print_symbolic(__entry->bar, 0317 { 0, "zero" }, 0318 { TRACE_SAMPLE_FOO, "TWO" }, 0319 { TRACE_SAMPLE_BAR, "FOUR" }, 0320 { TRACE_SAMPLE_ZOO, "EIGHT" }, 0321 { 10, "TEN" } 0322 ), 0323 0324 /* 0325 * __print_flags( variable, "delim", { value, "flag" }, ... ), 0326 * 0327 * This is similar to __print_symbolic, except that it tests the bits 0328 * of the value. If ((FLAG & variable) == FLAG) then the string is 0329 * printed. If more than one flag matches, then each one that does is 0330 * also printed with delim in between them. 0331 * If not all bits are accounted for, then the not found bits will be 0332 * added in hex format: 0x506 will show BIT2|BIT4|0x500 0333 */ 0334 __print_flags(__entry->bar, "|", 0335 { 1, "BIT1" }, 0336 { 2, "BIT2" }, 0337 { 4, "BIT3" }, 0338 { 8, "BIT4" } 0339 ), 0340 /* 0341 * __print_array( array, len, element_size ) 0342 * 0343 * This prints out the array that is defined by __array in a nice format. 0344 */ 0345 __print_array(__get_dynamic_array(list), 0346 __get_dynamic_array_len(list) / sizeof(int), 0347 sizeof(int)), 0348 __get_str(str), __get_bitmask(cpus), __get_str(vstr)) 0349 ); 0350 0351 /* 0352 * There may be a case where a tracepoint should only be called if 0353 * some condition is set. Otherwise the tracepoint should not be called. 0354 * But to do something like: 0355 * 0356 * if (cond) 0357 * trace_foo(); 0358 * 0359 * Would cause a little overhead when tracing is not enabled, and that 0360 * overhead, even if small, is not something we want. As tracepoints 0361 * use static branch (aka jump_labels), where no branch is taken to 0362 * skip the tracepoint when not enabled, and a jmp is placed to jump 0363 * to the tracepoint code when it is enabled, having a if statement 0364 * nullifies that optimization. It would be nice to place that 0365 * condition within the static branch. This is where TRACE_EVENT_CONDITION 0366 * comes in. 0367 * 0368 * TRACE_EVENT_CONDITION() is just like TRACE_EVENT, except it adds another 0369 * parameter just after args. Where TRACE_EVENT has: 0370 * 0371 * TRACE_EVENT(name, proto, args, struct, assign, printk) 0372 * 0373 * the CONDITION version has: 0374 * 0375 * TRACE_EVENT_CONDITION(name, proto, args, cond, struct, assign, printk) 0376 * 0377 * Everything is the same as TRACE_EVENT except for the new cond. Think 0378 * of the cond variable as: 0379 * 0380 * if (cond) 0381 * trace_foo_bar_with_cond(); 0382 * 0383 * Except that the logic for the if branch is placed after the static branch. 0384 * That is, the if statement that processes the condition will not be 0385 * executed unless that traecpoint is enabled. Otherwise it still remains 0386 * a nop. 0387 */ 0388 TRACE_EVENT_CONDITION(foo_bar_with_cond, 0389 0390 TP_PROTO(const char *foo, int bar), 0391 0392 TP_ARGS(foo, bar), 0393 0394 TP_CONDITION(!(bar % 10)), 0395 0396 TP_STRUCT__entry( 0397 __string( foo, foo ) 0398 __field( int, bar ) 0399 ), 0400 0401 TP_fast_assign( 0402 __assign_str(foo, foo); 0403 __entry->bar = bar; 0404 ), 0405 0406 TP_printk("foo %s %d", __get_str(foo), __entry->bar) 0407 ); 0408 0409 int foo_bar_reg(void); 0410 void foo_bar_unreg(void); 0411 0412 /* 0413 * Now in the case that some function needs to be called when the 0414 * tracepoint is enabled and/or when it is disabled, the 0415 * TRACE_EVENT_FN() serves this purpose. This is just like TRACE_EVENT() 0416 * but adds two more parameters at the end: 0417 * 0418 * TRACE_EVENT_FN( name, proto, args, struct, assign, printk, reg, unreg) 0419 * 0420 * reg and unreg are functions with the prototype of: 0421 * 0422 * void reg(void) 0423 * 0424 * The reg function gets called before the tracepoint is enabled, and 0425 * the unreg function gets called after the tracepoint is disabled. 0426 * 0427 * Note, reg and unreg are allowed to be NULL. If you only need to 0428 * call a function before enabling, or after disabling, just set one 0429 * function and pass in NULL for the other parameter. 0430 */ 0431 TRACE_EVENT_FN(foo_bar_with_fn, 0432 0433 TP_PROTO(const char *foo, int bar), 0434 0435 TP_ARGS(foo, bar), 0436 0437 TP_STRUCT__entry( 0438 __string( foo, foo ) 0439 __field( int, bar ) 0440 ), 0441 0442 TP_fast_assign( 0443 __assign_str(foo, foo); 0444 __entry->bar = bar; 0445 ), 0446 0447 TP_printk("foo %s %d", __get_str(foo), __entry->bar), 0448 0449 foo_bar_reg, foo_bar_unreg 0450 ); 0451 0452 /* 0453 * Each TRACE_EVENT macro creates several helper functions to produce 0454 * the code to add the tracepoint, create the files in the trace 0455 * directory, hook it to perf, assign the values and to print out 0456 * the raw data from the ring buffer. To prevent too much bloat, 0457 * if there are more than one tracepoint that uses the same format 0458 * for the proto, args, struct, assign and printk, and only the name 0459 * is different, it is highly recommended to use the DECLARE_EVENT_CLASS 0460 * 0461 * DECLARE_EVENT_CLASS() macro creates most of the functions for the 0462 * tracepoint. Then DEFINE_EVENT() is use to hook a tracepoint to those 0463 * functions. This DEFINE_EVENT() is an instance of the class and can 0464 * be enabled and disabled separately from other events (either TRACE_EVENT 0465 * or other DEFINE_EVENT()s). 0466 * 0467 * Note, TRACE_EVENT() itself is simply defined as: 0468 * 0469 * #define TRACE_EVENT(name, proto, args, tstruct, assign, printk) \ 0470 * DECLARE_EVENT_CLASS(name, proto, args, tstruct, assign, printk); \ 0471 * DEFINE_EVENT(name, name, proto, args) 0472 * 0473 * The DEFINE_EVENT() also can be declared with conditions and reg functions: 0474 * 0475 * DEFINE_EVENT_CONDITION(template, name, proto, args, cond); 0476 * DEFINE_EVENT_FN(template, name, proto, args, reg, unreg); 0477 */ 0478 DECLARE_EVENT_CLASS(foo_template, 0479 0480 TP_PROTO(const char *foo, int bar), 0481 0482 TP_ARGS(foo, bar), 0483 0484 TP_STRUCT__entry( 0485 __string( foo, foo ) 0486 __field( int, bar ) 0487 ), 0488 0489 TP_fast_assign( 0490 __assign_str(foo, foo); 0491 __entry->bar = bar; 0492 ), 0493 0494 TP_printk("foo %s %d", __get_str(foo), __entry->bar) 0495 ); 0496 0497 /* 0498 * Here's a better way for the previous samples (except, the first 0499 * example had more fields and could not be used here). 0500 */ 0501 DEFINE_EVENT(foo_template, foo_with_template_simple, 0502 TP_PROTO(const char *foo, int bar), 0503 TP_ARGS(foo, bar)); 0504 0505 DEFINE_EVENT_CONDITION(foo_template, foo_with_template_cond, 0506 TP_PROTO(const char *foo, int bar), 0507 TP_ARGS(foo, bar), 0508 TP_CONDITION(!(bar % 8))); 0509 0510 0511 DEFINE_EVENT_FN(foo_template, foo_with_template_fn, 0512 TP_PROTO(const char *foo, int bar), 0513 TP_ARGS(foo, bar), 0514 foo_bar_reg, foo_bar_unreg); 0515 0516 /* 0517 * Anytime two events share basically the same values and have 0518 * the same output, use the DECLARE_EVENT_CLASS() and DEFINE_EVENT() 0519 * when ever possible. 0520 */ 0521 0522 /* 0523 * If the event is similar to the DECLARE_EVENT_CLASS, but you need 0524 * to have a different output, then use DEFINE_EVENT_PRINT() which 0525 * lets you override the TP_printk() of the class. 0526 */ 0527 0528 DEFINE_EVENT_PRINT(foo_template, foo_with_template_print, 0529 TP_PROTO(const char *foo, int bar), 0530 TP_ARGS(foo, bar), 0531 TP_printk("bar %s %d", __get_str(foo), __entry->bar)); 0532 0533 /* 0534 * There are yet another __rel_loc dynamic data attribute. If you 0535 * use __rel_dynamic_array() and __rel_string() etc. macros, you 0536 * can use this attribute. There is no difference from the viewpoint 0537 * of functionality with/without 'rel' but the encoding is a bit 0538 * different. This is expected to be used with user-space event, 0539 * there is no reason that the kernel event use this, but only for 0540 * testing. 0541 */ 0542 0543 TRACE_EVENT(foo_rel_loc, 0544 0545 TP_PROTO(const char *foo, int bar, unsigned long *mask), 0546 0547 TP_ARGS(foo, bar, mask), 0548 0549 TP_STRUCT__entry( 0550 __rel_string( foo, foo ) 0551 __field( int, bar ) 0552 __rel_bitmask( bitmask, 0553 BITS_PER_BYTE * sizeof(unsigned long) ) 0554 ), 0555 0556 TP_fast_assign( 0557 __assign_rel_str(foo, foo); 0558 __entry->bar = bar; 0559 __assign_rel_bitmask(bitmask, mask, 0560 BITS_PER_BYTE * sizeof(unsigned long)); 0561 ), 0562 0563 TP_printk("foo_rel_loc %s, %d, %s", __get_rel_str(foo), __entry->bar, 0564 __get_rel_bitmask(bitmask)) 0565 ); 0566 #endif 0567 0568 /***** NOTICE! The #if protection ends here. *****/ 0569 0570 0571 /* 0572 * There are several ways I could have done this. If I left out the 0573 * TRACE_INCLUDE_PATH, then it would default to the kernel source 0574 * include/trace/events directory. 0575 * 0576 * I could specify a path from the define_trace.h file back to this 0577 * file. 0578 * 0579 * #define TRACE_INCLUDE_PATH ../../samples/trace_events 0580 * 0581 * But the safest and easiest way to simply make it use the directory 0582 * that the file is in is to add in the Makefile: 0583 * 0584 * CFLAGS_trace-events-sample.o := -I$(src) 0585 * 0586 * This will make sure the current path is part of the include 0587 * structure for our file so that define_trace.h can find it. 0588 * 0589 * I could have made only the top level directory the include: 0590 * 0591 * CFLAGS_trace-events-sample.o := -I$(PWD) 0592 * 0593 * And then let the path to this directory be the TRACE_INCLUDE_PATH: 0594 * 0595 * #define TRACE_INCLUDE_PATH samples/trace_events 0596 * 0597 * But then if something defines "samples" or "trace_events" as a macro 0598 * then we could risk that being converted too, and give us an unexpected 0599 * result. 0600 */ 0601 #undef TRACE_INCLUDE_PATH 0602 #undef TRACE_INCLUDE_FILE 0603 #define TRACE_INCLUDE_PATH . 0604 /* 0605 * TRACE_INCLUDE_FILE is not needed if the filename and TRACE_SYSTEM are equal 0606 */ 0607 #define TRACE_INCLUDE_FILE trace-events-sample 0608 #include <trace/define_trace.h>
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.1.0 LXR engine. The LXR team |
![]() ![]() |