trace_events.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /*
  2. * Stage 1 of the trace events.
  3. *
  4. * Override the macros in <trace/trace_events.h> to include the following:
  5. *
  6. * struct trace_event_raw_<call> {
  7. * struct trace_entry ent;
  8. * <type> <item>;
  9. * <type2> <item2>[<len>];
  10. * [...]
  11. * };
  12. *
  13. * The <type> <item> is created by the __field(type, item) macro or
  14. * the __array(type2, item2, len) macro.
  15. * We simply do "type item;", and that will create the fields
  16. * in the structure.
  17. */
  18. #include <linux/trace_events.h>
  19. #ifndef TRACE_SYSTEM_VAR
  20. #define TRACE_SYSTEM_VAR TRACE_SYSTEM
  21. #endif
  22. #define __app__(x, y) str__##x##y
  23. #define __app(x, y) __app__(x, y)
  24. #define TRACE_SYSTEM_STRING __app(TRACE_SYSTEM_VAR,__trace_system_name)
  25. #define TRACE_MAKE_SYSTEM_STR() \
  26. static const char TRACE_SYSTEM_STRING[] = \
  27. __stringify(TRACE_SYSTEM)
  28. TRACE_MAKE_SYSTEM_STR();
  29. #undef TRACE_DEFINE_ENUM
  30. #define TRACE_DEFINE_ENUM(a) \
  31. static struct trace_enum_map __used __initdata \
  32. __##TRACE_SYSTEM##_##a = \
  33. { \
  34. .system = TRACE_SYSTEM_STRING, \
  35. .enum_string = #a, \
  36. .enum_value = a \
  37. }; \
  38. static struct trace_enum_map __used \
  39. __attribute__((section("_ftrace_enum_map"))) \
  40. *TRACE_SYSTEM##_##a = &__##TRACE_SYSTEM##_##a
  41. /*
  42. * DECLARE_EVENT_CLASS can be used to add a generic function
  43. * handlers for events. That is, if all events have the same
  44. * parameters and just have distinct trace points.
  45. * Each tracepoint can be defined with DEFINE_EVENT and that
  46. * will map the DECLARE_EVENT_CLASS to the tracepoint.
  47. *
  48. * TRACE_EVENT is a one to one mapping between tracepoint and template.
  49. */
  50. #undef TRACE_EVENT
  51. #define TRACE_EVENT(name, proto, args, tstruct, assign, print) \
  52. DECLARE_EVENT_CLASS(name, \
  53. PARAMS(proto), \
  54. PARAMS(args), \
  55. PARAMS(tstruct), \
  56. PARAMS(assign), \
  57. PARAMS(print)); \
  58. DEFINE_EVENT(name, name, PARAMS(proto), PARAMS(args));
  59. #undef __field
  60. #define __field(type, item) type item;
  61. #undef __field_ext
  62. #define __field_ext(type, item, filter_type) type item;
  63. #undef __field_struct
  64. #define __field_struct(type, item) type item;
  65. #undef __field_struct_ext
  66. #define __field_struct_ext(type, item, filter_type) type item;
  67. #undef __array
  68. #define __array(type, item, len) type item[len];
  69. #undef __dynamic_array
  70. #define __dynamic_array(type, item, len) u32 __data_loc_##item;
  71. #undef __string
  72. #define __string(item, src) __dynamic_array(char, item, -1)
  73. #undef __bitmask
  74. #define __bitmask(item, nr_bits) __dynamic_array(char, item, -1)
  75. #undef TP_STRUCT__entry
  76. #define TP_STRUCT__entry(args...) args
  77. #undef DECLARE_EVENT_CLASS
  78. #define DECLARE_EVENT_CLASS(name, proto, args, tstruct, assign, print) \
  79. struct trace_event_raw_##name { \
  80. struct trace_entry ent; \
  81. tstruct \
  82. char __data[0]; \
  83. }; \
  84. \
  85. static struct trace_event_class event_class_##name;
  86. #undef DEFINE_EVENT
  87. #define DEFINE_EVENT(template, name, proto, args) \
  88. static struct trace_event_call __used \
  89. __attribute__((__aligned__(4))) event_##name
  90. #undef DEFINE_EVENT_FN
  91. #define DEFINE_EVENT_FN(template, name, proto, args, reg, unreg) \
  92. DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
  93. #undef DEFINE_EVENT_PRINT
  94. #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
  95. DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
  96. /* Callbacks are meaningless to ftrace. */
  97. #undef TRACE_EVENT_FN
  98. #define TRACE_EVENT_FN(name, proto, args, tstruct, \
  99. assign, print, reg, unreg) \
  100. TRACE_EVENT(name, PARAMS(proto), PARAMS(args), \
  101. PARAMS(tstruct), PARAMS(assign), PARAMS(print)) \
  102. #undef TRACE_EVENT_FLAGS
  103. #define TRACE_EVENT_FLAGS(name, value) \
  104. __TRACE_EVENT_FLAGS(name, value)
  105. #undef TRACE_EVENT_PERF_PERM
  106. #define TRACE_EVENT_PERF_PERM(name, expr...) \
  107. __TRACE_EVENT_PERF_PERM(name, expr)
  108. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
  109. /*
  110. * Stage 2 of the trace events.
  111. *
  112. * Include the following:
  113. *
  114. * struct trace_event_data_offsets_<call> {
  115. * u32 <item1>;
  116. * u32 <item2>;
  117. * [...]
  118. * };
  119. *
  120. * The __dynamic_array() macro will create each u32 <item>, this is
  121. * to keep the offset of each array from the beginning of the event.
  122. * The size of an array is also encoded, in the higher 16 bits of <item>.
  123. */
  124. #undef TRACE_DEFINE_ENUM
  125. #define TRACE_DEFINE_ENUM(a)
  126. #undef __field
  127. #define __field(type, item)
  128. #undef __field_ext
  129. #define __field_ext(type, item, filter_type)
  130. #undef __field_struct
  131. #define __field_struct(type, item)
  132. #undef __field_struct_ext
  133. #define __field_struct_ext(type, item, filter_type)
  134. #undef __array
  135. #define __array(type, item, len)
  136. #undef __dynamic_array
  137. #define __dynamic_array(type, item, len) u32 item;
  138. #undef __string
  139. #define __string(item, src) __dynamic_array(char, item, -1)
  140. #undef __bitmask
  141. #define __bitmask(item, nr_bits) __dynamic_array(unsigned long, item, -1)
  142. #undef DECLARE_EVENT_CLASS
  143. #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
  144. struct trace_event_data_offsets_##call { \
  145. tstruct; \
  146. };
  147. #undef DEFINE_EVENT
  148. #define DEFINE_EVENT(template, name, proto, args)
  149. #undef DEFINE_EVENT_PRINT
  150. #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
  151. DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
  152. #undef TRACE_EVENT_FLAGS
  153. #define TRACE_EVENT_FLAGS(event, flag)
  154. #undef TRACE_EVENT_PERF_PERM
  155. #define TRACE_EVENT_PERF_PERM(event, expr...)
  156. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
  157. /*
  158. * Stage 3 of the trace events.
  159. *
  160. * Override the macros in <trace/trace_events.h> to include the following:
  161. *
  162. * enum print_line_t
  163. * trace_raw_output_<call>(struct trace_iterator *iter, int flags)
  164. * {
  165. * struct trace_seq *s = &iter->seq;
  166. * struct trace_event_raw_<call> *field; <-- defined in stage 1
  167. * struct trace_entry *entry;
  168. * struct trace_seq *p = &iter->tmp_seq;
  169. * int ret;
  170. *
  171. * entry = iter->ent;
  172. *
  173. * if (entry->type != event_<call>->event.type) {
  174. * WARN_ON_ONCE(1);
  175. * return TRACE_TYPE_UNHANDLED;
  176. * }
  177. *
  178. * field = (typeof(field))entry;
  179. *
  180. * trace_seq_init(p);
  181. * ret = trace_seq_printf(s, "%s: ", <call>);
  182. * if (ret)
  183. * ret = trace_seq_printf(s, <TP_printk> "\n");
  184. * if (!ret)
  185. * return TRACE_TYPE_PARTIAL_LINE;
  186. *
  187. * return TRACE_TYPE_HANDLED;
  188. * }
  189. *
  190. * This is the method used to print the raw event to the trace
  191. * output format. Note, this is not needed if the data is read
  192. * in binary.
  193. */
  194. #undef __entry
  195. #define __entry field
  196. #undef TP_printk
  197. #define TP_printk(fmt, args...) fmt "\n", args
  198. #undef __get_dynamic_array
  199. #define __get_dynamic_array(field) \
  200. ((void *)__entry + (__entry->__data_loc_##field & 0xffff))
  201. #undef __get_dynamic_array_len
  202. #define __get_dynamic_array_len(field) \
  203. ((__entry->__data_loc_##field >> 16) & 0xffff)
  204. #undef __get_str
  205. #define __get_str(field) (char *)__get_dynamic_array(field)
  206. #undef __get_bitmask
  207. #define __get_bitmask(field) \
  208. ({ \
  209. void *__bitmask = __get_dynamic_array(field); \
  210. unsigned int __bitmask_size; \
  211. __bitmask_size = __get_dynamic_array_len(field); \
  212. trace_print_bitmask_seq(p, __bitmask, __bitmask_size); \
  213. })
  214. #undef __print_flags
  215. #define __print_flags(flag, delim, flag_array...) \
  216. ({ \
  217. static const struct trace_print_flags __flags[] = \
  218. { flag_array, { -1, NULL }}; \
  219. trace_print_flags_seq(p, delim, flag, __flags); \
  220. })
  221. #undef __print_symbolic
  222. #define __print_symbolic(value, symbol_array...) \
  223. ({ \
  224. static const struct trace_print_flags symbols[] = \
  225. { symbol_array, { -1, NULL }}; \
  226. trace_print_symbols_seq(p, value, symbols); \
  227. })
  228. #undef __print_symbolic_u64
  229. #if BITS_PER_LONG == 32
  230. #define __print_symbolic_u64(value, symbol_array...) \
  231. ({ \
  232. static const struct trace_print_flags_u64 symbols[] = \
  233. { symbol_array, { -1, NULL } }; \
  234. trace_print_symbols_seq_u64(p, value, symbols); \
  235. })
  236. #else
  237. #define __print_symbolic_u64(value, symbol_array...) \
  238. __print_symbolic(value, symbol_array)
  239. #endif
  240. #undef __print_hex
  241. #define __print_hex(buf, buf_len) trace_print_hex_seq(p, buf, buf_len)
  242. #undef __print_array
  243. #define __print_array(array, count, el_size) \
  244. ({ \
  245. BUILD_BUG_ON(el_size != 1 && el_size != 2 && \
  246. el_size != 4 && el_size != 8); \
  247. trace_print_array_seq(p, array, count, el_size); \
  248. })
  249. #undef DECLARE_EVENT_CLASS
  250. #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
  251. static notrace enum print_line_t \
  252. trace_raw_output_##call(struct trace_iterator *iter, int flags, \
  253. struct trace_event *trace_event) \
  254. { \
  255. struct trace_seq *s = &iter->seq; \
  256. struct trace_seq __maybe_unused *p = &iter->tmp_seq; \
  257. struct trace_event_raw_##call *field; \
  258. int ret; \
  259. \
  260. field = (typeof(field))iter->ent; \
  261. \
  262. ret = trace_raw_output_prep(iter, trace_event); \
  263. if (ret != TRACE_TYPE_HANDLED) \
  264. return ret; \
  265. \
  266. trace_seq_printf(s, print); \
  267. \
  268. return trace_handle_return(s); \
  269. } \
  270. static struct trace_event_functions trace_event_type_funcs_##call = { \
  271. .trace = trace_raw_output_##call, \
  272. };
  273. #undef DEFINE_EVENT_PRINT
  274. #define DEFINE_EVENT_PRINT(template, call, proto, args, print) \
  275. static notrace enum print_line_t \
  276. trace_raw_output_##call(struct trace_iterator *iter, int flags, \
  277. struct trace_event *event) \
  278. { \
  279. struct trace_event_raw_##template *field; \
  280. struct trace_entry *entry; \
  281. struct trace_seq *p = &iter->tmp_seq; \
  282. \
  283. entry = iter->ent; \
  284. \
  285. if (entry->type != event_##call.event.type) { \
  286. WARN_ON_ONCE(1); \
  287. return TRACE_TYPE_UNHANDLED; \
  288. } \
  289. \
  290. field = (typeof(field))entry; \
  291. \
  292. trace_seq_init(p); \
  293. return trace_output_call(iter, #call, print); \
  294. } \
  295. static struct trace_event_functions trace_event_type_funcs_##call = { \
  296. .trace = trace_raw_output_##call, \
  297. };
  298. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
  299. #undef __field_ext
  300. #define __field_ext(type, item, filter_type) \
  301. ret = trace_define_field(event_call, #type, #item, \
  302. offsetof(typeof(field), item), \
  303. sizeof(field.item), \
  304. is_signed_type(type), filter_type); \
  305. if (ret) \
  306. return ret;
  307. #undef __field_struct_ext
  308. #define __field_struct_ext(type, item, filter_type) \
  309. ret = trace_define_field(event_call, #type, #item, \
  310. offsetof(typeof(field), item), \
  311. sizeof(field.item), \
  312. 0, filter_type); \
  313. if (ret) \
  314. return ret;
  315. #undef __field
  316. #define __field(type, item) __field_ext(type, item, FILTER_OTHER)
  317. #undef __field_struct
  318. #define __field_struct(type, item) __field_struct_ext(type, item, FILTER_OTHER)
  319. #undef __array
  320. #define __array(type, item, len) \
  321. do { \
  322. char *type_str = #type"["__stringify(len)"]"; \
  323. BUILD_BUG_ON(len > MAX_FILTER_STR_VAL); \
  324. ret = trace_define_field(event_call, type_str, #item, \
  325. offsetof(typeof(field), item), \
  326. sizeof(field.item), \
  327. is_signed_type(type), FILTER_OTHER); \
  328. if (ret) \
  329. return ret; \
  330. } while (0);
  331. #undef __dynamic_array
  332. #define __dynamic_array(type, item, len) \
  333. ret = trace_define_field(event_call, "__data_loc " #type "[]", #item, \
  334. offsetof(typeof(field), __data_loc_##item), \
  335. sizeof(field.__data_loc_##item), \
  336. is_signed_type(type), FILTER_OTHER);
  337. #undef __string
  338. #define __string(item, src) __dynamic_array(char, item, -1)
  339. #undef __bitmask
  340. #define __bitmask(item, nr_bits) __dynamic_array(unsigned long, item, -1)
  341. #undef DECLARE_EVENT_CLASS
  342. #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, func, print) \
  343. static int notrace __init \
  344. trace_event_define_fields_##call(struct trace_event_call *event_call) \
  345. { \
  346. struct trace_event_raw_##call field; \
  347. int ret; \
  348. \
  349. tstruct; \
  350. \
  351. return ret; \
  352. }
  353. #undef DEFINE_EVENT
  354. #define DEFINE_EVENT(template, name, proto, args)
  355. #undef DEFINE_EVENT_PRINT
  356. #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
  357. DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
  358. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
  359. /*
  360. * remember the offset of each array from the beginning of the event.
  361. */
  362. #undef __entry
  363. #define __entry entry
  364. #undef __field
  365. #define __field(type, item)
  366. #undef __field_ext
  367. #define __field_ext(type, item, filter_type)
  368. #undef __field_struct
  369. #define __field_struct(type, item)
  370. #undef __field_struct_ext
  371. #define __field_struct_ext(type, item, filter_type)
  372. #undef __array
  373. #define __array(type, item, len)
  374. #undef __dynamic_array
  375. #define __dynamic_array(type, item, len) \
  376. __item_length = (len) * sizeof(type); \
  377. __data_offsets->item = __data_size + \
  378. offsetof(typeof(*entry), __data); \
  379. __data_offsets->item |= __item_length << 16; \
  380. __data_size += __item_length;
  381. #undef __string
  382. #define __string(item, src) __dynamic_array(char, item, \
  383. strlen((src) ? (const char *)(src) : "(null)") + 1)
  384. /*
  385. * __bitmask_size_in_bytes_raw is the number of bytes needed to hold
  386. * num_possible_cpus().
  387. */
  388. #define __bitmask_size_in_bytes_raw(nr_bits) \
  389. (((nr_bits) + 7) / 8)
  390. #define __bitmask_size_in_longs(nr_bits) \
  391. ((__bitmask_size_in_bytes_raw(nr_bits) + \
  392. ((BITS_PER_LONG / 8) - 1)) / (BITS_PER_LONG / 8))
  393. /*
  394. * __bitmask_size_in_bytes is the number of bytes needed to hold
  395. * num_possible_cpus() padded out to the nearest long. This is what
  396. * is saved in the buffer, just to be consistent.
  397. */
  398. #define __bitmask_size_in_bytes(nr_bits) \
  399. (__bitmask_size_in_longs(nr_bits) * (BITS_PER_LONG / 8))
  400. #undef __bitmask
  401. #define __bitmask(item, nr_bits) __dynamic_array(unsigned long, item, \
  402. __bitmask_size_in_longs(nr_bits))
  403. #undef DECLARE_EVENT_CLASS
  404. #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
  405. static inline notrace int trace_event_get_offsets_##call( \
  406. struct trace_event_data_offsets_##call *__data_offsets, proto) \
  407. { \
  408. int __data_size = 0; \
  409. int __maybe_unused __item_length; \
  410. struct trace_event_raw_##call __maybe_unused *entry; \
  411. \
  412. tstruct; \
  413. \
  414. return __data_size; \
  415. }
  416. #undef DEFINE_EVENT
  417. #define DEFINE_EVENT(template, name, proto, args)
  418. #undef DEFINE_EVENT_PRINT
  419. #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
  420. DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
  421. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)