trace-events-sample.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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.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". 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 is 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.
  167. *
  168. * For __string(foo, bar) use __get_str(foo)
  169. *
  170. * For __bitmask(target_cpus, nr_cpumask_bits) use __get_bitmask(target_cpus)
  171. *
  172. *
  173. * Note, that for both the assign and the printk, __entry is the handler
  174. * to the data structure in the ring buffer, and is defined by the
  175. * TP_STRUCT__entry.
  176. */
  177. /*
  178. * It is OK to have helper functions in the file, but they need to be protected
  179. * from being defined more than once. Remember, this file gets included more
  180. * than once.
  181. */
  182. #ifndef __TRACE_EVENT_SAMPLE_HELPER_FUNCTIONS
  183. #define __TRACE_EVENT_SAMPLE_HELPER_FUNCTIONS
  184. static inline int __length_of(const int *list)
  185. {
  186. int i;
  187. if (!list)
  188. return 0;
  189. for (i = 0; list[i]; i++)
  190. ;
  191. return i;
  192. }
  193. enum {
  194. TRACE_SAMPLE_FOO = 2,
  195. TRACE_SAMPLE_BAR = 4,
  196. TRACE_SAMPLE_ZOO = 8,
  197. };
  198. #endif
  199. /*
  200. * If enums are used in the TP_printk(), their names will be shown in
  201. * format files and not their values. This can cause problems with user
  202. * space programs that parse the format files to know how to translate
  203. * the raw binary trace output into human readable text.
  204. *
  205. * To help out user space programs, any enum that is used in the TP_printk()
  206. * should be defined by TRACE_DEFINE_ENUM() macro. All that is needed to
  207. * be done is to add this macro with the enum within it in the trace
  208. * header file, and it will be converted in the output.
  209. */
  210. TRACE_DEFINE_ENUM(TRACE_SAMPLE_FOO);
  211. TRACE_DEFINE_ENUM(TRACE_SAMPLE_BAR);
  212. TRACE_DEFINE_ENUM(TRACE_SAMPLE_ZOO);
  213. TRACE_EVENT(foo_bar,
  214. TP_PROTO(const char *foo, int bar, const int *lst,
  215. const char *string, const struct cpumask *mask),
  216. TP_ARGS(foo, bar, lst, string, mask),
  217. TP_STRUCT__entry(
  218. __array( char, foo, 10 )
  219. __field( int, bar )
  220. __dynamic_array(int, list, __length_of(lst))
  221. __string( str, string )
  222. __bitmask( cpus, num_possible_cpus() )
  223. ),
  224. TP_fast_assign(
  225. strlcpy(__entry->foo, foo, 10);
  226. __entry->bar = bar;
  227. memcpy(__get_dynamic_array(list), lst,
  228. __length_of(lst) * sizeof(int));
  229. __assign_str(str, string);
  230. __assign_bitmask(cpus, cpumask_bits(mask), num_possible_cpus());
  231. ),
  232. TP_printk("foo %s %d %s %s %s %s (%s)", __entry->foo, __entry->bar,
  233. /*
  234. * Notice here the use of some helper functions. This includes:
  235. *
  236. * __print_symbolic( variable, { value, "string" }, ... ),
  237. *
  238. * The variable is tested against each value of the { } pair. If
  239. * the variable matches one of the values, then it will print the
  240. * string in that pair. If non are matched, it returns a string
  241. * version of the number (if __entry->bar == 7 then "7" is returned).
  242. */
  243. __print_symbolic(__entry->bar,
  244. { 0, "zero" },
  245. { TRACE_SAMPLE_FOO, "TWO" },
  246. { TRACE_SAMPLE_BAR, "FOUR" },
  247. { TRACE_SAMPLE_ZOO, "EIGHT" },
  248. { 10, "TEN" }
  249. ),
  250. /*
  251. * __print_flags( variable, "delim", { value, "flag" }, ... ),
  252. *
  253. * This is similar to __print_symbolic, except that it tests the bits
  254. * of the value. If ((FLAG & variable) == FLAG) then the string is
  255. * printed. If more than one flag matches, then each one that does is
  256. * also printed with delim in between them.
  257. * If not all bits are accounted for, then the not found bits will be
  258. * added in hex format: 0x506 will show BIT2|BIT4|0x500
  259. */
  260. __print_flags(__entry->bar, "|",
  261. { 1, "BIT1" },
  262. { 2, "BIT2" },
  263. { 4, "BIT3" },
  264. { 8, "BIT4" }
  265. ),
  266. /*
  267. * __print_array( array, len, element_size )
  268. *
  269. * This prints out the array that is defined by __array in a nice format.
  270. */
  271. __print_array(__get_dynamic_array(list),
  272. __get_dynamic_array_len(list),
  273. sizeof(int)),
  274. __get_str(str), __get_bitmask(cpus))
  275. );
  276. /*
  277. * There may be a case where a tracepoint should only be called if
  278. * some condition is set. Otherwise the tracepoint should not be called.
  279. * But to do something like:
  280. *
  281. * if (cond)
  282. * trace_foo();
  283. *
  284. * Would cause a little overhead when tracing is not enabled, and that
  285. * overhead, even if small, is not something we want. As tracepoints
  286. * use static branch (aka jump_labels), where no branch is taken to
  287. * skip the tracepoint when not enabled, and a jmp is placed to jump
  288. * to the tracepoint code when it is enabled, having a if statement
  289. * nullifies that optimization. It would be nice to place that
  290. * condition within the static branch. This is where TRACE_EVENT_CONDITION
  291. * comes in.
  292. *
  293. * TRACE_EVENT_CONDITION() is just like TRACE_EVENT, except it adds another
  294. * parameter just after args. Where TRACE_EVENT has:
  295. *
  296. * TRACE_EVENT(name, proto, args, struct, assign, printk)
  297. *
  298. * the CONDITION version has:
  299. *
  300. * TRACE_EVENT_CONDITION(name, proto, args, cond, struct, assign, printk)
  301. *
  302. * Everything is the same as TRACE_EVENT except for the new cond. Think
  303. * of the cond variable as:
  304. *
  305. * if (cond)
  306. * trace_foo_bar_with_cond();
  307. *
  308. * Except that the logic for the if branch is placed after the static branch.
  309. * That is, the if statement that processes the condition will not be
  310. * executed unless that traecpoint is enabled. Otherwise it still remains
  311. * a nop.
  312. */
  313. TRACE_EVENT_CONDITION(foo_bar_with_cond,
  314. TP_PROTO(const char *foo, int bar),
  315. TP_ARGS(foo, bar),
  316. TP_CONDITION(!(bar % 10)),
  317. TP_STRUCT__entry(
  318. __string( foo, foo )
  319. __field( int, bar )
  320. ),
  321. TP_fast_assign(
  322. __assign_str(foo, foo);
  323. __entry->bar = bar;
  324. ),
  325. TP_printk("foo %s %d", __get_str(foo), __entry->bar)
  326. );
  327. void foo_bar_reg(void);
  328. void foo_bar_unreg(void);
  329. /*
  330. * Now in the case that some function needs to be called when the
  331. * tracepoint is enabled and/or when it is disabled, the
  332. * TRACE_EVENT_FN() serves this purpose. This is just like TRACE_EVENT()
  333. * but adds two more parameters at the end:
  334. *
  335. * TRACE_EVENT_FN( name, proto, args, struct, assign, printk, reg, unreg)
  336. *
  337. * reg and unreg are functions with the prototype of:
  338. *
  339. * void reg(void)
  340. *
  341. * The reg function gets called before the tracepoint is enabled, and
  342. * the unreg function gets called after the tracepoint is disabled.
  343. *
  344. * Note, reg and unreg are allowed to be NULL. If you only need to
  345. * call a function before enabling, or after disabling, just set one
  346. * function and pass in NULL for the other parameter.
  347. */
  348. TRACE_EVENT_FN(foo_bar_with_fn,
  349. TP_PROTO(const char *foo, int bar),
  350. TP_ARGS(foo, bar),
  351. TP_STRUCT__entry(
  352. __string( foo, foo )
  353. __field( int, bar )
  354. ),
  355. TP_fast_assign(
  356. __assign_str(foo, foo);
  357. __entry->bar = bar;
  358. ),
  359. TP_printk("foo %s %d", __get_str(foo), __entry->bar),
  360. foo_bar_reg, foo_bar_unreg
  361. );
  362. /*
  363. * Each TRACE_EVENT macro creates several helper functions to produce
  364. * the code to add the tracepoint, create the files in the trace
  365. * directory, hook it to perf, assign the values and to print out
  366. * the raw data from the ring buffer. To prevent too much bloat,
  367. * if there are more than one tracepoint that uses the same format
  368. * for the proto, args, struct, assign and printk, and only the name
  369. * is different, it is highly recommended to use the DECLARE_EVENT_CLASS
  370. *
  371. * DECLARE_EVENT_CLASS() macro creates most of the functions for the
  372. * tracepoint. Then DEFINE_EVENT() is use to hook a tracepoint to those
  373. * functions. This DEFINE_EVENT() is an instance of the class and can
  374. * be enabled and disabled separately from other events (either TRACE_EVENT
  375. * or other DEFINE_EVENT()s).
  376. *
  377. * Note, TRACE_EVENT() itself is simply defined as:
  378. *
  379. * #define TRACE_EVENT(name, proto, args, tstruct, assign, printk) \
  380. * DEFINE_EVENT_CLASS(name, proto, args, tstruct, assign, printk); \
  381. * DEFINE_EVENT(name, name, proto, args)
  382. *
  383. * The DEFINE_EVENT() also can be declared with conditions and reg functions:
  384. *
  385. * DEFINE_EVENT_CONDITION(template, name, proto, args, cond);
  386. * DEFINE_EVENT_FN(template, name, proto, args, reg, unreg);
  387. */
  388. DECLARE_EVENT_CLASS(foo_template,
  389. TP_PROTO(const char *foo, int bar),
  390. TP_ARGS(foo, bar),
  391. TP_STRUCT__entry(
  392. __string( foo, foo )
  393. __field( int, bar )
  394. ),
  395. TP_fast_assign(
  396. __assign_str(foo, foo);
  397. __entry->bar = bar;
  398. ),
  399. TP_printk("foo %s %d", __get_str(foo), __entry->bar)
  400. );
  401. /*
  402. * Here's a better way for the previous samples (except, the first
  403. * exmaple had more fields and could not be used here).
  404. */
  405. DEFINE_EVENT(foo_template, foo_with_template_simple,
  406. TP_PROTO(const char *foo, int bar),
  407. TP_ARGS(foo, bar));
  408. DEFINE_EVENT_CONDITION(foo_template, foo_with_template_cond,
  409. TP_PROTO(const char *foo, int bar),
  410. TP_ARGS(foo, bar),
  411. TP_CONDITION(!(bar % 8)));
  412. DEFINE_EVENT_FN(foo_template, foo_with_template_fn,
  413. TP_PROTO(const char *foo, int bar),
  414. TP_ARGS(foo, bar),
  415. foo_bar_reg, foo_bar_unreg);
  416. /*
  417. * Anytime two events share basically the same values and have
  418. * the same output, use the DECLARE_EVENT_CLASS() and DEFINE_EVENT()
  419. * when ever possible.
  420. */
  421. /*
  422. * If the event is similar to the DECLARE_EVENT_CLASS, but you need
  423. * to have a different output, then use DEFINE_EVENT_PRINT() which
  424. * lets you override the TP_printk() of the class.
  425. */
  426. DEFINE_EVENT_PRINT(foo_template, foo_with_template_print,
  427. TP_PROTO(const char *foo, int bar),
  428. TP_ARGS(foo, bar),
  429. TP_printk("bar %s %d", __get_str(foo), __entry->bar));
  430. #endif
  431. /***** NOTICE! The #if protection ends here. *****/
  432. /*
  433. * There are several ways I could have done this. If I left out the
  434. * TRACE_INCLUDE_PATH, then it would default to the kernel source
  435. * include/trace/events directory.
  436. *
  437. * I could specify a path from the define_trace.h file back to this
  438. * file.
  439. *
  440. * #define TRACE_INCLUDE_PATH ../../samples/trace_events
  441. *
  442. * But the safest and easiest way to simply make it use the directory
  443. * that the file is in is to add in the Makefile:
  444. *
  445. * CFLAGS_trace-events-sample.o := -I$(src)
  446. *
  447. * This will make sure the current path is part of the include
  448. * structure for our file so that define_trace.h can find it.
  449. *
  450. * I could have made only the top level directory the include:
  451. *
  452. * CFLAGS_trace-events-sample.o := -I$(PWD)
  453. *
  454. * And then let the path to this directory be the TRACE_INCLUDE_PATH:
  455. *
  456. * #define TRACE_INCLUDE_PATH samples/trace_events
  457. *
  458. * But then if something defines "samples" or "trace_events" as a macro
  459. * then we could risk that being converted too, and give us an unexpected
  460. * result.
  461. */
  462. #undef TRACE_INCLUDE_PATH
  463. #undef TRACE_INCLUDE_FILE
  464. #define TRACE_INCLUDE_PATH .
  465. /*
  466. * TRACE_INCLUDE_FILE is not needed if the filename and TRACE_SYSTEM are equal
  467. */
  468. #define TRACE_INCLUDE_FILE trace-events-sample
  469. #include <trace/define_trace.h>