Path: blob/master/samples/trace_events/trace-events-sample.h
26285 views
/* SPDX-License-Identifier: GPL-2.0 */1/*2* If TRACE_SYSTEM is defined, that will be the directory created3* in the ftrace directory under /sys/kernel/tracing/events/<system>4*5* The define_trace.h below will also look for a file name of6* TRACE_SYSTEM.h where TRACE_SYSTEM is what is defined here.7* In this case, it would look for sample-trace.h8*9* If the header name will be different than the system name10* (as in this case), then you can override the header name that11* define_trace.h will look up by defining TRACE_INCLUDE_FILE12*13* This file is called trace-events-sample.h but we want the system14* to be called "sample-trace". Therefore we must define the name of this15* file:16*17* #define TRACE_INCLUDE_FILE trace-events-sample18*19* As we do an the bottom of this file.20*21* Notice that TRACE_SYSTEM should be defined outside of #if22* protection, just like TRACE_INCLUDE_FILE.23*/24#undef TRACE_SYSTEM25#define TRACE_SYSTEM sample-trace2627/*28* TRACE_SYSTEM is expected to be a C valid variable (alpha-numeric29* and underscore), although it may start with numbers. If for some30* reason it is not, you need to add the following lines:31*/32#undef TRACE_SYSTEM_VAR33#define TRACE_SYSTEM_VAR sample_trace34/*35* But the above is only needed if TRACE_SYSTEM is not alpha-numeric36* and underscored. By default, TRACE_SYSTEM_VAR will be equal to37* TRACE_SYSTEM. As TRACE_SYSTEM_VAR must be alpha-numeric, if38* TRACE_SYSTEM is not, then TRACE_SYSTEM_VAR must be defined with39* only alpha-numeric and underscores.40*41* The TRACE_SYSTEM_VAR is only used internally and not visible to42* user space.43*/4445/*46* Notice that this file is not protected like a normal header.47* We also must allow for rereading of this file. The48*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_H5556/*57* All trace headers should include tracepoint.h, until we finally58* make it into a standard header.59*/60#include <linux/tracepoint.h>6162/*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 structure76* called "__entry", which can be used in the fast_assign part of the77* 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 any82* 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, union89* but not an array). Be careful using complex types, as each90* event is limited in size, and copying large amounts of data91* into the ring buffer can slow things down.92*93* __field_struct(struct bar, foo)94*95* __entry->bar.x = y;9697* __array: There are three fields (type, name, size). The type is the98* type of elements in the array, the name is the name of the array.99* size is the number of items in the array (not the total size).100*101* __array( char, foo, 10) is the same as saying: char foo[10];102*103* Assigning arrays can be done like any array:104*105* __entry->foo[0] = 'a';106*107* memcpy(__entry->foo, bar, 10);108*109* __dynamic_array: This is similar to array, but can vary its size from110* 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 the115* specific instance of tracepoint. Again, size is the number of116* 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() macro121* 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 to128* have a null terminated character array passed to it (it allows129* for NULL too, which would be converted into "(null)"). __string130* takes two parameter (name, src), where name is the name of131* the string saved, and src is the string to copy into the132* 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);139*140* The __string() macro saves off the string that is passed into141* the second parameter, and the __assign_str() will store than142* saved string into the "foo" field.143*144* __vstring: This is similar to __string() but instead of taking a145* dynamic length, it takes a variable list va_list 'va' variable.146* Some event callers already have a message from parameters saved147* in a va_list. Passing in the format and the va_list variable148* will save just enough on the ring buffer for that string.149* Note, the va variable used is a pointer to a va_list, not150* to the va_list directly.151*152* (va_list *va)153*154* __vstring(foo, fmt, va) is similar to: vsnprintf(foo, fmt, va)155*156* To assign the string, use the helper macro __assign_vstr().157*158* __assign_vstr(foo, fmt, va);159*160* In most cases, the __assign_vstr() macro will take the same161* parameters as the __vstring() macro had to declare the string.162* Use __get_str() to retrieve the __vstring() just like it would for163* __string().164*165* __string_len: This is a helper to a __dynamic_array, but it understands166* that the array has characters in it, it will allocate 'len' + 1 bytes167* in the ring buffer and add a '\0' to the string. This is168* useful if the string being saved has no terminating '\0' byte.169* It requires that the length of the string is known as it acts170* like a memcpy().171*172* Declared with:173*174* __string_len(foo, bar, len)175*176* To assign this string, use the helper macro __assign_str().177* The length is saved via the __string_len() and is retrieved in178* __assign_str().179*180* __assign_str(foo);181*182* Then len + 1 is allocated to the ring buffer, and a nul terminating183* byte is added. This is similar to:184*185* memcpy(__get_str(foo), bar, len);186* __get_str(foo)[len] = 0;187*188* The advantage of using this over __dynamic_array, is that it189* takes care of allocating the extra byte on the ring buffer190* for the '\0' terminating byte, and __get_str(foo) can be used191* in the TP_printk().192*193* __bitmask: This is another kind of __dynamic_array, but it expects194* an array of longs, and the number of bits to parse. It takes195* two parameters (name, nr_bits), where name is the name of the196* bitmask to save, and the nr_bits is the number of bits to record.197*198* __bitmask(target_cpu, nr_cpumask_bits)199*200* To assign a bitmask, use the __assign_bitmask() helper macro.201*202* __assign_bitmask(target_cpus, cpumask_bits(bar), nr_cpumask_bits);203*204* __cpumask: This is pretty much the same as __bitmask but is specific for205* CPU masks. The type displayed to the user via the format files will206* be "cpumaks_t" such that user space may deal with them differently207* if they choose to do so, and the bits is always set to nr_cpumask_bits.208*209* __cpumask(target_cpu)210*211* To assign a cpumask, use the __assign_cpumask() helper macro.212*213* __assign_cpumask(target_cpus, cpumask_bits(bar));214*215* fast_assign: This is a C like function that is used to store the items216* into the ring buffer. A special variable called "__entry" will be the217* structure that points into the ring buffer and has the same fields as218* described by the struct part of TRACE_EVENT above.219*220* printk: This is a way to print out the data in pretty print. This is221* useful if the system crashes and you are logging via a serial line,222* the data can be printed to the console using this "printk" method.223* This is also used to print out the data from the trace files.224* Again, the __entry macro is used to access the data from the ring buffer.225*226* Note, __dynamic_array, __string, __bitmask and __cpumask require special227* helpers to access the data.228*229* For __dynamic_array(int, foo, bar) use __get_dynamic_array(foo)230* Use __get_dynamic_array_len(foo) to get the length of the array231* saved. Note, __get_dynamic_array_len() returns the total allocated232* length of the dynamic array; __print_array() expects the second233* parameter to be the number of elements. To get that, the array length234* needs to be divided by the element size.235*236* For __string(foo, bar) use __get_str(foo)237*238* For __bitmask(target_cpus, nr_cpumask_bits) use __get_bitmask(target_cpus)239*240* For __cpumask(target_cpus) use __get_cpumask(target_cpus)241*242*243* Note, that for both the assign and the printk, __entry is the handler244* to the data structure in the ring buffer, and is defined by the245* TP_STRUCT__entry.246*/247248/*249* It is OK to have helper functions in the file, but they need to be protected250* from being defined more than once. Remember, this file gets included more251* than once.252*/253#ifndef __TRACE_EVENT_SAMPLE_HELPER_FUNCTIONS254#define __TRACE_EVENT_SAMPLE_HELPER_FUNCTIONS255static inline int __length_of(const int *list)256{257int i;258259if (!list)260return 0;261262for (i = 0; list[i]; i++)263;264return i;265}266267enum {268TRACE_SAMPLE_FOO = 2,269TRACE_SAMPLE_BAR = 4,270TRACE_SAMPLE_ZOO = 8,271};272#endif273274/*275* If enums are used in the TP_printk(), their names will be shown in276* format files and not their values. This can cause problems with user277* space programs that parse the format files to know how to translate278* the raw binary trace output into human readable text.279*280* To help out user space programs, any enum that is used in the TP_printk()281* should be defined by TRACE_DEFINE_ENUM() macro. All that is needed to282* be done is to add this macro with the enum within it in the trace283* header file, and it will be converted in the output.284*/285286TRACE_DEFINE_ENUM(TRACE_SAMPLE_FOO);287TRACE_DEFINE_ENUM(TRACE_SAMPLE_BAR);288TRACE_DEFINE_ENUM(TRACE_SAMPLE_ZOO);289290TRACE_EVENT(foo_bar,291292TP_PROTO(const char *foo, int bar, const int *lst,293const char *string, const struct cpumask *mask,294const char *fmt, va_list *va),295296TP_ARGS(foo, bar, lst, string, mask, fmt, va),297298TP_STRUCT__entry(299__array( char, foo, 10 )300__field( int, bar )301__dynamic_array(int, list, __length_of(lst))302__string( str, string )303__bitmask( cpus, num_possible_cpus() )304__cpumask( cpum )305__vstring( vstr, fmt, va )306__string_len( lstr, foo, bar / 2 < strlen(foo) ? bar / 2 : strlen(foo) )307),308309TP_fast_assign(310strscpy(__entry->foo, foo, 10);311__entry->bar = bar;312memcpy(__get_dynamic_array(list), lst,313__length_of(lst) * sizeof(int));314__assign_str(str);315__assign_str(lstr);316__assign_vstr(vstr, fmt, va);317__assign_bitmask(cpus, cpumask_bits(mask), num_possible_cpus());318__assign_cpumask(cpum, cpumask_bits(mask));319),320321TP_printk("foo %s %d %s %s %s %s %s %s (%s) (%s) %s [%d] %*pbl",322__entry->foo, __entry->bar,323324/*325* Notice here the use of some helper functions. This includes:326*327* __print_symbolic( variable, { value, "string" }, ... ),328*329* The variable is tested against each value of the { } pair. If330* the variable matches one of the values, then it will print the331* string in that pair. If non are matched, it returns a string332* version of the number (if __entry->bar == 7 then "7" is returned).333*/334__print_symbolic(__entry->bar,335{ 0, "zero" },336{ TRACE_SAMPLE_FOO, "TWO" },337{ TRACE_SAMPLE_BAR, "FOUR" },338{ TRACE_SAMPLE_ZOO, "EIGHT" },339{ 10, "TEN" }340),341342/*343* __print_flags( variable, "delim", { value, "flag" }, ... ),344*345* This is similar to __print_symbolic, except that it tests the bits346* of the value. If ((FLAG & variable) == FLAG) then the string is347* printed. If more than one flag matches, then each one that does is348* also printed with delim in between them.349* If not all bits are accounted for, then the not found bits will be350* added in hex format: 0x506 will show BIT2|BIT4|0x500351*/352__print_flags(__entry->bar, "|",353{ 1, "BIT1" },354{ 2, "BIT2" },355{ 4, "BIT3" },356{ 8, "BIT4" }357),358/*359* __print_array( array, len, element_size )360*361* This prints out the array that is defined by __array in a nice format.362*/363__print_array(__get_dynamic_array(list),364__get_dynamic_array_len(list) / sizeof(int),365sizeof(int)),366367/* A shortcut is to use __print_dynamic_array for dynamic arrays */368369__print_dynamic_array(list, sizeof(int)),370371__get_str(str), __get_str(lstr),372__get_bitmask(cpus), __get_cpumask(cpum),373__get_str(vstr),374__get_dynamic_array_len(cpus),375__get_dynamic_array_len(cpus),376__get_dynamic_array(cpus))377);378379/*380* There may be a case where a tracepoint should only be called if381* some condition is set. Otherwise the tracepoint should not be called.382* But to do something like:383*384* if (cond)385* trace_foo();386*387* Would cause a little overhead when tracing is not enabled, and that388* overhead, even if small, is not something we want. As tracepoints389* use static branch (aka jump_labels), where no branch is taken to390* skip the tracepoint when not enabled, and a jmp is placed to jump391* to the tracepoint code when it is enabled, having a if statement392* nullifies that optimization. It would be nice to place that393* condition within the static branch. This is where TRACE_EVENT_CONDITION394* comes in.395*396* TRACE_EVENT_CONDITION() is just like TRACE_EVENT, except it adds another397* parameter just after args. Where TRACE_EVENT has:398*399* TRACE_EVENT(name, proto, args, struct, assign, printk)400*401* the CONDITION version has:402*403* TRACE_EVENT_CONDITION(name, proto, args, cond, struct, assign, printk)404*405* Everything is the same as TRACE_EVENT except for the new cond. Think406* of the cond variable as:407*408* if (cond)409* trace_foo_bar_with_cond();410*411* Except that the logic for the if branch is placed after the static branch.412* That is, the if statement that processes the condition will not be413* executed unless that traecpoint is enabled. Otherwise it still remains414* a nop.415*/416TRACE_EVENT_CONDITION(foo_bar_with_cond,417418TP_PROTO(const char *foo, int bar),419420TP_ARGS(foo, bar),421422TP_CONDITION(!(bar % 10)),423424TP_STRUCT__entry(425__string( foo, foo )426__field( int, bar )427),428429TP_fast_assign(430__assign_str(foo);431__entry->bar = bar;432),433434TP_printk("foo %s %d", __get_str(foo), __entry->bar)435);436437int foo_bar_reg(void);438void foo_bar_unreg(void);439440/*441* Now in the case that some function needs to be called when the442* tracepoint is enabled and/or when it is disabled, the443* TRACE_EVENT_FN() serves this purpose. This is just like TRACE_EVENT()444* but adds two more parameters at the end:445*446* TRACE_EVENT_FN( name, proto, args, struct, assign, printk, reg, unreg)447*448* reg and unreg are functions with the prototype of:449*450* void reg(void)451*452* The reg function gets called before the tracepoint is enabled, and453* the unreg function gets called after the tracepoint is disabled.454*455* Note, reg and unreg are allowed to be NULL. If you only need to456* call a function before enabling, or after disabling, just set one457* function and pass in NULL for the other parameter.458*/459TRACE_EVENT_FN(foo_bar_with_fn,460461TP_PROTO(const char *foo, int bar),462463TP_ARGS(foo, bar),464465TP_STRUCT__entry(466__string( foo, foo )467__field( int, bar )468),469470TP_fast_assign(471__assign_str(foo);472__entry->bar = bar;473),474475TP_printk("foo %s %d", __get_str(foo), __entry->bar),476477foo_bar_reg, foo_bar_unreg478);479480/*481* Each TRACE_EVENT macro creates several helper functions to produce482* the code to add the tracepoint, create the files in the trace483* directory, hook it to perf, assign the values and to print out484* the raw data from the ring buffer. To prevent too much bloat,485* if there are more than one tracepoint that uses the same format486* for the proto, args, struct, assign and printk, and only the name487* is different, it is highly recommended to use the DECLARE_EVENT_CLASS488*489* DECLARE_EVENT_CLASS() macro creates most of the functions for the490* tracepoint. Then DEFINE_EVENT() is use to hook a tracepoint to those491* functions. This DEFINE_EVENT() is an instance of the class and can492* be enabled and disabled separately from other events (either TRACE_EVENT493* or other DEFINE_EVENT()s).494*495* Note, TRACE_EVENT() itself is simply defined as:496*497* #define TRACE_EVENT(name, proto, args, tstruct, assign, printk) \498* DECLARE_EVENT_CLASS(name, proto, args, tstruct, assign, printk); \499* DEFINE_EVENT(name, name, proto, args)500*501* The DEFINE_EVENT() also can be declared with conditions and reg functions:502*503* DEFINE_EVENT_CONDITION(template, name, proto, args, cond);504* DEFINE_EVENT_FN(template, name, proto, args, reg, unreg);505*/506DECLARE_EVENT_CLASS(foo_template,507508TP_PROTO(const char *foo, int bar),509510TP_ARGS(foo, bar),511512TP_STRUCT__entry(513__string( foo, foo )514__field( int, bar )515),516517TP_fast_assign(518__assign_str(foo);519__entry->bar = bar;520),521522TP_printk("foo %s %d", __get_str(foo), __entry->bar)523);524525/*526* Here's a better way for the previous samples (except, the first527* example had more fields and could not be used here).528*/529DEFINE_EVENT(foo_template, foo_with_template_simple,530TP_PROTO(const char *foo, int bar),531TP_ARGS(foo, bar));532533DEFINE_EVENT_CONDITION(foo_template, foo_with_template_cond,534TP_PROTO(const char *foo, int bar),535TP_ARGS(foo, bar),536TP_CONDITION(!(bar % 8)));537538539DEFINE_EVENT_FN(foo_template, foo_with_template_fn,540TP_PROTO(const char *foo, int bar),541TP_ARGS(foo, bar),542foo_bar_reg, foo_bar_unreg);543544/*545* Anytime two events share basically the same values and have546* the same output, use the DECLARE_EVENT_CLASS() and DEFINE_EVENT()547* when ever possible.548*/549550/*551* If the event is similar to the DECLARE_EVENT_CLASS, but you need552* to have a different output, then use DEFINE_EVENT_PRINT() which553* lets you override the TP_printk() of the class.554*/555556DEFINE_EVENT_PRINT(foo_template, foo_with_template_print,557TP_PROTO(const char *foo, int bar),558TP_ARGS(foo, bar),559TP_printk("bar %s %d", __get_str(foo), __entry->bar));560561/*562* There are yet another __rel_loc dynamic data attribute. If you563* use __rel_dynamic_array() and __rel_string() etc. macros, you564* can use this attribute. There is no difference from the viewpoint565* of functionality with/without 'rel' but the encoding is a bit566* different. This is expected to be used with user-space event,567* there is no reason that the kernel event use this, but only for568* testing.569*/570571TRACE_EVENT(foo_rel_loc,572573TP_PROTO(const char *foo, int bar, unsigned long *mask, const cpumask_t *cpus),574575TP_ARGS(foo, bar, mask, cpus),576577TP_STRUCT__entry(578__rel_string( foo, foo )579__field( int, bar )580__rel_bitmask( bitmask,581BITS_PER_BYTE * sizeof(unsigned long) )582__rel_cpumask( cpumask )583),584585TP_fast_assign(586__assign_rel_str(foo);587__entry->bar = bar;588__assign_rel_bitmask(bitmask, mask,589BITS_PER_BYTE * sizeof(unsigned long));590__assign_rel_cpumask(cpumask, cpus);591),592593TP_printk("foo_rel_loc %s, %d, %s, %s", __get_rel_str(foo), __entry->bar,594__get_rel_bitmask(bitmask),595__get_rel_cpumask(cpumask))596);597#endif598599/***** NOTICE! The #if protection ends here. *****/600601602/*603* There are several ways I could have done this. If I left out the604* TRACE_INCLUDE_PATH, then it would default to the kernel source605* include/trace/events directory.606*607* I could specify a path from the define_trace.h file back to this608* file.609*610* #define TRACE_INCLUDE_PATH ../../samples/trace_events611*612* But the safest and easiest way to simply make it use the directory613* that the file is in is to add in the Makefile:614*615* CFLAGS_trace-events-sample.o := -I$(src)616*617* This will make sure the current path is part of the include618* structure for our file so that define_trace.h can find it.619*620* I could have made only the top level directory the include:621*622* CFLAGS_trace-events-sample.o := -I$(PWD)623*624* And then let the path to this directory be the TRACE_INCLUDE_PATH:625*626* #define TRACE_INCLUDE_PATH samples/trace_events627*628* But then if something defines "samples" or "trace_events" as a macro629* then we could risk that being converted too, and give us an unexpected630* result.631*/632#undef TRACE_INCLUDE_PATH633#undef TRACE_INCLUDE_FILE634#define TRACE_INCLUDE_PATH .635/*636* TRACE_INCLUDE_FILE is not needed if the filename and TRACE_SYSTEM are equal637*/638#define TRACE_INCLUDE_FILE trace-events-sample639#include <trace/define_trace.h>640641642