trace-events-sample.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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-trace.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-trace". 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. * TRACE_SYSTEM is expected to be a C valid variable (alpha-numeric
  28. * and underscore), although it may start with numbers. If for some
  29. * reason it is not, you need to add the following lines:
  30. */
  31. #undef TRACE_SYSTEM_VAR
  32. #define TRACE_SYSTEM_VAR sample_trace
  33. /*
  34. * But the above is only needed if TRACE_SYSTEM is not alpha-numeric
  35. * and underscored. By default, TRACE_SYSTEM_VAR will be equal to
  36. * TRACE_SYSTEM. As TRACE_SYSTEM_VAR must be alpha-numeric, if
  37. * TRACE_SYSTEM is not, then TRACE_SYSTEM_VAR must be defined with
  38. * only alpha-numeric and underscores.
  39. *
  40. * The TRACE_SYSTEM_VAR is only used internally and not visible to
  41. * user space.
  42. */
  43. /*
  44. * Notice that this file is not protected like a normal header.
  45. * We also must allow for rereading of this file. The
  46. *
  47. * || defined(TRACE_HEADER_MULTI_READ)
  48. *
  49. * serves this purpose.
  50. */
  51. #if !defined(_TRACE_EVENT_SAMPLE_H) || defined(TRACE_HEADER_MULTI_READ)
  52. #define _TRACE_EVENT_SAMPLE_H
  53. /*
  54. * All trace headers should include tracepoint.h, until we finally
  55. * make it into a standard header.
  56. */
  57. #include <linux/tracepoint.h>
  58. /*
  59. * The TRACE_EVENT macro is broken up into 5 parts.
  60. *
  61. * name: name of the trace point. This is also how to enable the tracepoint.
  62. * A function called trace_foo_bar() will be created.
  63. *
  64. * proto: the prototype of the function trace_foo_bar()
  65. * Here it is trace_foo_bar(char *foo, int bar).
  66. *
  67. * args: must match the arguments in the prototype.
  68. * Here it is simply "foo, bar".
  69. *
  70. * struct: This defines the way the data will be stored in the ring buffer.
  71. * The items declared here become part of a special structure
  72. * called "__entry", which can be used in the fast_assign part of the
  73. * TRACE_EVENT macro.
  74. *
  75. * Here are the currently defined types you can use:
  76. *
  77. * __field : Is broken up into type and name. Where type can be any
  78. * primitive type (integer, long or pointer).
  79. *
  80. * __field(int, foo)
  81. *
  82. * __entry->foo = 5;
  83. *
  84. * __field_struct : This can be any static complex data type (struct, union
  85. * but not an array). Be careful using complex types, as each
  86. * event is limited in size, and copying large amounts of data
  87. * into the ring buffer can slow things down.
  88. *
  89. * __field_struct(struct bar, foo)
  90. *
  91. * __entry->bar.x = y;
  92. * __array: There are three fields (type, name, size). The type is the
  93. * type of elements in teh array, the name is the name of the array.
  94. * size is the number of items in the array (not the total size).
  95. *
  96. * __array( char, foo, 10) is the same as saying: char foo[10];
  97. *
  98. * Assigning arrays can be done like any array:
  99. *
  100. * __entry->foo[0] = 'a';
  101. *
  102. * memcpy(__entry->foo, bar, 10);
  103. *
  104. * __dynamic_array: This is similar to array, but can vary its size from
  105. * instance to instance of the tracepoint being called.
  106. * Like __array, this too has three elements (type, name, size);
  107. * type is the type of the element, name is the name of the array.
  108. * The size is different than __array. It is not a static number,
  109. * but the algorithm to figure out the length of the array for the
  110. * specific instance of tracepoint. Again, size is the numebr of
  111. * items in the array, not the total length in bytes.
  112. *
  113. * __dynamic_array( int, foo, bar) is similar to: int foo[bar];
  114. *
  115. * Note, unlike arrays, you must use the __get_dynamic_array() macro
  116. * to access the array.
  117. *
  118. * memcpy(__get_dynamic_array(foo), bar, 10);
  119. *
  120. * Notice, that "__entry" is not needed here.
  121. *
  122. * __string: This is a special kind of __dynamic_array. It expects to
  123. * have a nul terminated character array passed to it (it allows
  124. * for NULL too, which would be converted into "(null)"). __string
  125. * takes two paramenter (name, src), where name is the name of
  126. * the string saved, and src is the string to copy into the
  127. * ring buffer.
  128. *
  129. * __string(foo, bar) is similar to: strcpy(foo, bar)
  130. *
  131. * To assign a string, use the helper macro __assign_str().
  132. *
  133. * __assign_str(foo, bar);
  134. *
  135. * In most cases, the __assign_str() macro will take the same
  136. * parameters as the __string() macro had to declare the string.
  137. *
  138. * __bitmask: This is another kind of __dynamic_array, but it expects
  139. * an array of longs, and the number of bits to parse. It takes
  140. * two parameters (name, nr_bits), where name is the name of the
  141. * bitmask to save, and the nr_bits is the number of bits to record.
  142. *
  143. * __bitmask(target_cpu, nr_cpumask_bits)
  144. *
  145. * To assign a bitmask, use the __assign_bitmask() helper macro.
  146. *
  147. * __assign_bitmask(target_cpus, cpumask_bits(bar), nr_cpumask_bits);
  148. *
  149. *
  150. * fast_assign: This is a C like function that is used to store the items
  151. * into the ring buffer. A special variable called "__entry" will be the
  152. * structure that points into the ring buffer and has the same fields as
  153. * described by the struct part of TRACE_EVENT above.
  154. *
  155. * printk: This is a way to print out the data in pretty print. This is
  156. * useful if the system crashes and you are logging via a serial line,
  157. * the data can be printed to the console using this "printk" method.
  158. * This is also used to print out the data from the trace files.
  159. * Again, the __entry macro is used to access the data from the ring buffer.
  160. *
  161. * Note, __dynamic_array, __string, and __bitmask require special helpers
  162. * to access the data.
  163. *
  164. * For __dynamic_array(int, foo, bar) use __get_dynamic_array(foo)
  165. * Use __get_dynamic_array_len(foo) to get the length of the array
  166. * saved. Note, __get_dynamic_array_len() returns the total allocated
  167. * length of the dynamic array; __print_array() expects the second
  168. * parameter to be the number of elements. To get that, the array length
  169. * needs to be divided by the element size.
  170. *
  171. * For __string(foo, bar) use __get_str(foo)
  172. *
  173. * For __bitmask(target_cpus, nr_cpumask_bits) use __get_bitmask(target_cpus)
  174. *
  175. *
  176. * Note, that for both the assign and the printk, __entry is the handler
  177. * to the data structure in the ring buffer, and is defined by the
  178. * TP_STRUCT__entry.
  179. */
  180. /*
  181. * It is OK to have helper functions in the file, but they need to be protected
  182. * from being defined more than once. Remember, this file gets included more
  183. * than once.
  184. */
  185. #ifndef __TRACE_EVENT_SAMPLE_HELPER_FUNCTIONS
  186. #define __TRACE_EVENT_SAMPLE_HELPER_FUNCTIONS
  187. static inline int __length_of(const int *list)
  188. {
  189. int i;
  190. if (!list)
  191. return 0;
  192. for (i = 0; list[i]; i++)
  193. ;
  194. return i;
  195. }
  196. enum {
  197. TRACE_SAMPLE_FOO = 2,
  198. TRACE_SAMPLE_BAR = 4,
  199. TRACE_SAMPLE_ZOO = 8,
  200. };
  201. #endif
  202. /*
  203. * If enums are used in the TP_printk(), their names will be shown in
  204. * format files and not their values. This can cause problems with user
  205. * space programs that parse the format files to know how to translate
  206. * the raw binary trace output into human readable text.
  207. *
  208. * To help out user space programs, any enum that is used in the TP_printk()
  209. * should be defined by TRACE_DEFINE_ENUM() macro. All that is needed to
  210. * be done is to add this macro with the enum within it in the trace
  211. * header file, and it will be converted in the output.
  212. */
  213. TRACE_DEFINE_ENUM(TRACE_SAMPLE_FOO);
  214. TRACE_DEFINE_ENUM(TRACE_SAMPLE_BAR);
  215. TRACE_DEFINE_ENUM(TRACE_SAMPLE_ZOO);
  216. TRACE_EVENT(foo_bar,
  217. TP_PROTO(const char *foo, int bar, const int *lst,
  218. const char *string, const struct cpumask *mask),
  219. TP_ARGS(foo, bar, lst, string, mask),
  220. TP_STRUCT__entry(
  221. __array( char, foo, 10 )
  222. __field( int, bar )
  223. __dynamic_array(int, list, __length_of(lst))
  224. __string( str, string )
  225. __bitmask( cpus, num_possible_cpus() )
  226. ),
  227. TP_fast_assign(
  228. strlcpy(__entry->foo, foo, 10);
  229. __entry->bar = bar;
  230. memcpy(__get_dynamic_array(list), lst,
  231. __length_of(lst) * sizeof(int));
  232. __assign_str(str, string);
  233. __assign_bitmask(cpus, cpumask_bits(mask), num_possible_cpus());
  234. ),
  235. TP_printk("foo %s %d %s %s %s %s (%s)", __entry->foo, __entry->bar,
  236. /*
  237. * Notice here the use of some helper functions. This includes:
  238. *
  239. * __print_symbolic( variable, { value, "string" }, ... ),
  240. *
  241. * The variable is tested against each value of the { } pair. If
  242. * the variable matches one of the values, then it will print the
  243. * string in that pair. If non are matched, it returns a string
  244. * version of the number (if __entry->bar == 7 then "7" is returned).
  245. */
  246. __print_symbolic(__entry->bar,
  247. { 0, "zero" },
  248. { TRACE_SAMPLE_FOO, "TWO" },
  249. { TRACE_SAMPLE_BAR, "FOUR" },
  250. { TRACE_SAMPLE_ZOO, "EIGHT" },
  251. { 10, "TEN" }
  252. ),
  253. /*
  254. * __print_flags( variable, "delim", { value, "flag" }, ... ),
  255. *
  256. * This is similar to __print_symbolic, except that it tests the bits
  257. * of the value. If ((FLAG & variable) == FLAG) then the string is
  258. * printed. If more than one flag matches, then each one that does is
  259. * also printed with delim in between them.
  260. * If not all bits are accounted for, then the not found bits will be
  261. * added in hex format: 0x506 will show BIT2|BIT4|0x500
  262. */
  263. __print_flags(__entry->bar, "|",
  264. { 1, "BIT1" },
  265. { 2, "BIT2" },
  266. { 4, "BIT3" },
  267. { 8, "BIT4" }
  268. ),
  269. /*
  270. * __print_array( array, len, element_size )
  271. *
  272. * This prints out the array that is defined by __array in a nice format.
  273. */
  274. __print_array(__get_dynamic_array(list),
  275. __get_dynamic_array_len(list) / sizeof(int),
  276. sizeof(int)),
  277. __get_str(str), __get_bitmask(cpus))
  278. );
  279. /*
  280. * There may be a case where a tracepoint should only be called if
  281. * some condition is set. Otherwise the tracepoint should not be called.
  282. * But to do something like:
  283. *
  284. * if (cond)
  285. * trace_foo();
  286. *
  287. * Would cause a little overhead when tracing is not enabled, and that
  288. * overhead, even if small, is not something we want. As tracepoints
  289. * use static branch (aka jump_labels), where no branch is taken to
  290. * skip the tracepoint when not enabled, and a jmp is placed to jump
  291. * to the tracepoint code when it is enabled, having a if statement
  292. * nullifies that optimization. It would be nice to place that
  293. * condition within the static branch. This is where TRACE_EVENT_CONDITION
  294. * comes in.
  295. *
  296. * TRACE_EVENT_CONDITION() is just like TRACE_EVENT, except it adds another
  297. * parameter just after args. Where TRACE_EVENT has:
  298. *
  299. * TRACE_EVENT(name, proto, args, struct, assign, printk)
  300. *
  301. * the CONDITION version has:
  302. *
  303. * TRACE_EVENT_CONDITION(name, proto, args, cond, struct, assign, printk)
  304. *
  305. * Everything is the same as TRACE_EVENT except for the new cond. Think
  306. * of the cond variable as:
  307. *
  308. * if (cond)
  309. * trace_foo_bar_with_cond();
  310. *
  311. * Except that the logic for the if branch is placed after the static branch.
  312. * That is, the if statement that processes the condition will not be
  313. * executed unless that traecpoint is enabled. Otherwise it still remains
  314. * a nop.
  315. */
  316. TRACE_EVENT_CONDITION(foo_bar_with_cond,
  317. TP_PROTO(const char *foo, int bar),
  318. TP_ARGS(foo, bar),
  319. TP_CONDITION(!(bar % 10)),
  320. TP_STRUCT__entry(
  321. __string( foo, foo )
  322. __field( int, bar )
  323. ),
  324. TP_fast_assign(
  325. __assign_str(foo, foo);
  326. __entry->bar = bar;
  327. ),
  328. TP_printk("foo %s %d", __get_str(foo), __entry->bar)
  329. );
  330. void foo_bar_reg(void);
  331. void foo_bar_unreg(void);
  332. /*
  333. * Now in the case that some function needs to be called when the
  334. * tracepoint is enabled and/or when it is disabled, the
  335. * TRACE_EVENT_FN() serves this purpose. This is just like TRACE_EVENT()
  336. * but adds two more parameters at the end:
  337. *
  338. * TRACE_EVENT_FN( name, proto, args, struct, assign, printk, reg, unreg)
  339. *
  340. * reg and unreg are functions with the prototype of:
  341. *
  342. * void reg(void)
  343. *
  344. * The reg function gets called before the tracepoint is enabled, and
  345. * the unreg function gets called after the tracepoint is disabled.
  346. *
  347. * Note, reg and unreg are allowed to be NULL. If you only need to
  348. * call a function before enabling, or after disabling, just set one
  349. * function and pass in NULL for the other parameter.
  350. */
  351. TRACE_EVENT_FN(foo_bar_with_fn,
  352. TP_PROTO(const char *foo, int bar),
  353. TP_ARGS(foo, bar),
  354. TP_STRUCT__entry(
  355. __string( foo, foo )
  356. __field( int, bar )
  357. ),
  358. TP_fast_assign(
  359. __assign_str(foo, foo);
  360. __entry->bar = bar;
  361. ),
  362. TP_printk("foo %s %d", __get_str(foo), __entry->bar),
  363. foo_bar_reg, foo_bar_unreg
  364. );
  365. /*
  366. * Each TRACE_EVENT macro creates several helper functions to produce
  367. * the code to add the tracepoint, create the files in the trace
  368. * directory, hook it to perf, assign the values and to print out
  369. * the raw data from the ring buffer. To prevent too much bloat,
  370. * if there are more than one tracepoint that uses the same format
  371. * for the proto, args, struct, assign and printk, and only the name
  372. * is different, it is highly recommended to use the DECLARE_EVENT_CLASS
  373. *
  374. * DECLARE_EVENT_CLASS() macro creates most of the functions for the
  375. * tracepoint. Then DEFINE_EVENT() is use to hook a tracepoint to those
  376. * functions. This DEFINE_EVENT() is an instance of the class and can
  377. * be enabled and disabled separately from other events (either TRACE_EVENT
  378. * or other DEFINE_EVENT()s).
  379. *
  380. * Note, TRACE_EVENT() itself is simply defined as:
  381. *
  382. * #define TRACE_EVENT(name, proto, args, tstruct, assign, printk) \
  383. * DEFINE_EVENT_CLASS(name, proto, args, tstruct, assign, printk); \
  384. * DEFINE_EVENT(name, name, proto, args)
  385. *
  386. * The DEFINE_EVENT() also can be declared with conditions and reg functions:
  387. *
  388. * DEFINE_EVENT_CONDITION(template, name, proto, args, cond);
  389. * DEFINE_EVENT_FN(template, name, proto, args, reg, unreg);
  390. */
  391. DECLARE_EVENT_CLASS(foo_template,
  392. TP_PROTO(const char *foo, int bar),
  393. TP_ARGS(foo, bar),
  394. TP_STRUCT__entry(
  395. __string( foo, foo )
  396. __field( int, bar )
  397. ),
  398. TP_fast_assign(
  399. __assign_str(foo, foo);
  400. __entry->bar = bar;
  401. ),
  402. TP_printk("foo %s %d", __get_str(foo), __entry->bar)
  403. );
  404. /*
  405. * Here's a better way for the previous samples (except, the first
  406. * exmaple had more fields and could not be used here).
  407. */
  408. DEFINE_EVENT(foo_template, foo_with_template_simple,
  409. TP_PROTO(const char *foo, int bar),
  410. TP_ARGS(foo, bar));
  411. DEFINE_EVENT_CONDITION(foo_template, foo_with_template_cond,
  412. TP_PROTO(const char *foo, int bar),
  413. TP_ARGS(foo, bar),
  414. TP_CONDITION(!(bar % 8)));
  415. DEFINE_EVENT_FN(foo_template, foo_with_template_fn,
  416. TP_PROTO(const char *foo, int bar),
  417. TP_ARGS(foo, bar),
  418. foo_bar_reg, foo_bar_unreg);
  419. /*
  420. * Anytime two events share basically the same values and have
  421. * the same output, use the DECLARE_EVENT_CLASS() and DEFINE_EVENT()
  422. * when ever possible.
  423. */
  424. /*
  425. * If the event is similar to the DECLARE_EVENT_CLASS, but you need
  426. * to have a different output, then use DEFINE_EVENT_PRINT() which
  427. * lets you override the TP_printk() of the class.
  428. */
  429. DEFINE_EVENT_PRINT(foo_template, foo_with_template_print,
  430. TP_PROTO(const char *foo, int bar),
  431. TP_ARGS(foo, bar),
  432. TP_printk("bar %s %d", __get_str(foo), __entry->bar));
  433. #endif
  434. /***** NOTICE! The #if protection ends here. *****/
  435. /*
  436. * There are several ways I could have done this. If I left out the
  437. * TRACE_INCLUDE_PATH, then it would default to the kernel source
  438. * include/trace/events directory.
  439. *
  440. * I could specify a path from the define_trace.h file back to this
  441. * file.
  442. *
  443. * #define TRACE_INCLUDE_PATH ../../samples/trace_events
  444. *
  445. * But the safest and easiest way to simply make it use the directory
  446. * that the file is in is to add in the Makefile:
  447. *
  448. * CFLAGS_trace-events-sample.o := -I$(src)
  449. *
  450. * This will make sure the current path is part of the include
  451. * structure for our file so that define_trace.h can find it.
  452. *
  453. * I could have made only the top level directory the include:
  454. *
  455. * CFLAGS_trace-events-sample.o := -I$(PWD)
  456. *
  457. * And then let the path to this directory be the TRACE_INCLUDE_PATH:
  458. *
  459. * #define TRACE_INCLUDE_PATH samples/trace_events
  460. *
  461. * But then if something defines "samples" or "trace_events" as a macro
  462. * then we could risk that being converted too, and give us an unexpected
  463. * result.
  464. */
  465. #undef TRACE_INCLUDE_PATH
  466. #undef TRACE_INCLUDE_FILE
  467. #define TRACE_INCLUDE_PATH .
  468. /*
  469. * TRACE_INCLUDE_FILE is not needed if the filename and TRACE_SYSTEM are equal
  470. */
  471. #define TRACE_INCLUDE_FILE trace-events-sample
  472. #include <trace/define_trace.h>