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