trace-events-sample.h 14 KB

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