trace_events.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Stage 1 of the trace events.
  4. *
  5. * Override the macros in the event tracepoint header <trace/events/XXX.h>
  6. * to include the following:
  7. *
  8. * struct trace_event_raw_<call> {
  9. * struct trace_entry ent;
  10. * <type> <item>;
  11. * <type2> <item2>[<len>];
  12. * [...]
  13. * };
  14. *
  15. * The <type> <item> is created by the __field(type, item) macro or
  16. * the __array(type2, item2, len) macro.
  17. * We simply do "type item;", and that will create the fields
  18. * in the structure.
  19. */
  20. #include <linux/trace_events.h>
  21. #ifndef TRACE_SYSTEM_VAR
  22. #define TRACE_SYSTEM_VAR TRACE_SYSTEM
  23. #endif
  24. #include "stages/init.h"
  25. /*
  26. * DECLARE_EVENT_CLASS can be used to add a generic function
  27. * handlers for events. That is, if all events have the same
  28. * parameters and just have distinct trace points.
  29. * Each tracepoint can be defined with DEFINE_EVENT and that
  30. * will map the DECLARE_EVENT_CLASS to the tracepoint.
  31. *
  32. * TRACE_EVENT is a one to one mapping between tracepoint and template.
  33. */
  34. #undef TRACE_EVENT
  35. #define TRACE_EVENT(name, proto, args, tstruct, assign, print) \
  36. DECLARE_EVENT_CLASS(name, \
  37. PARAMS(proto), \
  38. PARAMS(args), \
  39. PARAMS(tstruct), \
  40. PARAMS(assign), \
  41. PARAMS(print)); \
  42. DEFINE_EVENT(name, name, PARAMS(proto), PARAMS(args));
  43. #include "stages/stage1_struct_define.h"
  44. #undef DECLARE_EVENT_CLASS
  45. #define DECLARE_EVENT_CLASS(name, proto, args, tstruct, assign, print) \
  46. struct trace_event_raw_##name { \
  47. struct trace_entry ent; \
  48. tstruct \
  49. char __data[]; \
  50. }; \
  51. \
  52. static struct trace_event_class event_class_##name;
  53. #undef DEFINE_EVENT
  54. #define DEFINE_EVENT(template, name, proto, args) \
  55. static struct trace_event_call __used \
  56. __attribute__((__aligned__(4))) event_##name
  57. #undef DEFINE_EVENT_FN
  58. #define DEFINE_EVENT_FN(template, name, proto, args, reg, unreg) \
  59. DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
  60. #undef DEFINE_EVENT_PRINT
  61. #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
  62. DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
  63. /* Callbacks are meaningless to ftrace. */
  64. #undef TRACE_EVENT_FN
  65. #define TRACE_EVENT_FN(name, proto, args, tstruct, \
  66. assign, print, reg, unreg) \
  67. TRACE_EVENT(name, PARAMS(proto), PARAMS(args), \
  68. PARAMS(tstruct), PARAMS(assign), PARAMS(print)) \
  69. #undef TRACE_EVENT_FN_COND
  70. #define TRACE_EVENT_FN_COND(name, proto, args, cond, tstruct, \
  71. assign, print, reg, unreg) \
  72. TRACE_EVENT_CONDITION(name, PARAMS(proto), PARAMS(args), PARAMS(cond), \
  73. PARAMS(tstruct), PARAMS(assign), PARAMS(print)) \
  74. #undef TRACE_EVENT_FLAGS
  75. #define TRACE_EVENT_FLAGS(name, value) \
  76. __TRACE_EVENT_FLAGS(name, value)
  77. #undef TRACE_EVENT_PERF_PERM
  78. #define TRACE_EVENT_PERF_PERM(name, expr...) \
  79. __TRACE_EVENT_PERF_PERM(name, expr)
  80. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
  81. /*
  82. * Stage 2 of the trace events.
  83. *
  84. * Include the following:
  85. *
  86. * struct trace_event_data_offsets_<call> {
  87. * u32 <item1>;
  88. * u32 <item2>;
  89. * [...]
  90. * };
  91. *
  92. * The __dynamic_array() macro will create each u32 <item>, this is
  93. * to keep the offset of each array from the beginning of the event.
  94. * The size of an array is also encoded, in the higher 16 bits of <item>.
  95. */
  96. #include "stages/stage2_data_offsets.h"
  97. #undef DECLARE_EVENT_CLASS
  98. #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
  99. struct trace_event_data_offsets_##call { \
  100. tstruct; \
  101. };
  102. #undef DEFINE_EVENT
  103. #define DEFINE_EVENT(template, name, proto, args)
  104. #undef DEFINE_EVENT_PRINT
  105. #define DEFINE_EVENT_PRINT(template, name, proto, args, print)
  106. #undef TRACE_EVENT_FLAGS
  107. #define TRACE_EVENT_FLAGS(event, flag)
  108. #undef TRACE_EVENT_PERF_PERM
  109. #define TRACE_EVENT_PERF_PERM(event, expr...)
  110. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
  111. /*
  112. * Stage 3 of the trace events.
  113. *
  114. * Override the macros in the event tracepoint header <trace/events/XXX.h>
  115. * to include the following:
  116. *
  117. * enum print_line_t
  118. * trace_raw_output_<call>(struct trace_iterator *iter, int flags)
  119. * {
  120. * struct trace_seq *s = &iter->seq;
  121. * struct trace_event_raw_<call> *field; <-- defined in stage 1
  122. * struct trace_seq *p = &iter->tmp_seq;
  123. *
  124. * -------(for event)-------
  125. *
  126. * struct trace_entry *entry;
  127. *
  128. * entry = iter->ent;
  129. *
  130. * if (entry->type != event_<call>->event.type) {
  131. * WARN_ON_ONCE(1);
  132. * return TRACE_TYPE_UNHANDLED;
  133. * }
  134. *
  135. * field = (typeof(field))entry;
  136. *
  137. * trace_seq_init(p);
  138. * return trace_output_call(iter, <call>, <TP_printk> "\n");
  139. *
  140. * ------(or, for event class)------
  141. *
  142. * int ret;
  143. *
  144. * field = (typeof(field))iter->ent;
  145. *
  146. * ret = trace_raw_output_prep(iter, trace_event);
  147. * if (ret != TRACE_TYPE_HANDLED)
  148. * return ret;
  149. *
  150. * trace_event_printf(iter, <TP_printk> "\n");
  151. *
  152. * return trace_handle_return(s);
  153. * -------
  154. * }
  155. *
  156. * This is the method used to print the raw event to the trace
  157. * output format. Note, this is not needed if the data is read
  158. * in binary.
  159. */
  160. #include "stages/stage3_trace_output.h"
  161. #undef DECLARE_EVENT_CLASS
  162. #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
  163. static notrace enum print_line_t \
  164. trace_raw_output_##call(struct trace_iterator *iter, int flags, \
  165. struct trace_event *trace_event) \
  166. { \
  167. struct trace_seq *s = &iter->seq; \
  168. struct trace_seq __maybe_unused *p = &iter->tmp_seq; \
  169. struct trace_event_raw_##call *field; \
  170. int ret; \
  171. \
  172. field = (typeof(field))iter->ent; \
  173. \
  174. ret = trace_raw_output_prep(iter, trace_event); \
  175. if (ret != TRACE_TYPE_HANDLED) \
  176. return ret; \
  177. \
  178. trace_event_printf(iter, print); \
  179. \
  180. return trace_handle_return(s); \
  181. } \
  182. static struct trace_event_functions trace_event_type_funcs_##call = { \
  183. .trace = trace_raw_output_##call, \
  184. };
  185. #undef DEFINE_EVENT_PRINT
  186. #define DEFINE_EVENT_PRINT(template, call, proto, args, print) \
  187. static notrace enum print_line_t \
  188. trace_raw_output_##call(struct trace_iterator *iter, int flags, \
  189. struct trace_event *event) \
  190. { \
  191. struct trace_event_raw_##template *field; \
  192. struct trace_entry *entry; \
  193. struct trace_seq *p = &iter->tmp_seq; \
  194. \
  195. entry = iter->ent; \
  196. \
  197. if (entry->type != event_##call.event.type) { \
  198. WARN_ON_ONCE(1); \
  199. return TRACE_TYPE_UNHANDLED; \
  200. } \
  201. \
  202. field = (typeof(field))entry; \
  203. \
  204. trace_seq_init(p); \
  205. return trace_output_call(iter, #call, print); \
  206. } \
  207. static struct trace_event_functions trace_event_type_funcs_##call = { \
  208. .trace = trace_raw_output_##call, \
  209. };
  210. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
  211. #include "stages/stage4_event_fields.h"
  212. #undef DECLARE_EVENT_CLASS
  213. #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, func, print) \
  214. static struct trace_event_fields trace_event_fields_##call[] = { \
  215. tstruct \
  216. {} };
  217. #undef DEFINE_EVENT_PRINT
  218. #define DEFINE_EVENT_PRINT(template, name, proto, args, print)
  219. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
  220. #include "stages/stage5_get_offsets.h"
  221. #undef DECLARE_EVENT_CLASS
  222. #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
  223. static inline notrace int trace_event_get_offsets_##call( \
  224. struct trace_event_data_offsets_##call *__data_offsets, proto) \
  225. { \
  226. int __data_size = 0; \
  227. int __maybe_unused __item_length; \
  228. struct trace_event_raw_##call __maybe_unused *entry; \
  229. \
  230. tstruct; \
  231. \
  232. return __data_size; \
  233. }
  234. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
  235. /*
  236. * Stage 4 of the trace events.
  237. *
  238. * Override the macros in the event tracepoint header <trace/events/XXX.h>
  239. * to include the following:
  240. *
  241. * For those macros defined with TRACE_EVENT:
  242. *
  243. * static struct trace_event_call event_<call>;
  244. *
  245. * static void trace_event_raw_event_<call>(void *__data, proto)
  246. * {
  247. * struct trace_event_file *trace_file = __data;
  248. * struct trace_event_call *event_call = trace_file->event_call;
  249. * struct trace_event_data_offsets_<call> __maybe_unused __data_offsets;
  250. * unsigned long eflags = trace_file->flags;
  251. * enum event_trigger_type __tt = ETT_NONE;
  252. * struct ring_buffer_event *event;
  253. * struct trace_event_raw_<call> *entry; <-- defined in stage 1
  254. * struct trace_buffer *buffer;
  255. * unsigned long irq_flags;
  256. * int __data_size;
  257. * int pc;
  258. *
  259. * if (!(eflags & EVENT_FILE_FL_TRIGGER_COND)) {
  260. * if (eflags & EVENT_FILE_FL_TRIGGER_MODE)
  261. * event_triggers_call(trace_file, NULL);
  262. * if (eflags & EVENT_FILE_FL_SOFT_DISABLED)
  263. * return;
  264. * }
  265. *
  266. * local_save_flags(irq_flags);
  267. * pc = preempt_count();
  268. *
  269. * __data_size = trace_event_get_offsets_<call>(&__data_offsets, args);
  270. *
  271. * event = trace_event_buffer_lock_reserve(&buffer, trace_file,
  272. * event_<call>->event.type,
  273. * sizeof(*entry) + __data_size,
  274. * irq_flags, pc);
  275. * if (!event)
  276. * return;
  277. * entry = ring_buffer_event_data(event);
  278. *
  279. * { <assign>; } <-- Here we assign the entries by the __field and
  280. * __array macros.
  281. *
  282. * if (eflags & EVENT_FILE_FL_TRIGGER_COND)
  283. * __tt = event_triggers_call(trace_file, entry);
  284. *
  285. * if (test_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT,
  286. * &trace_file->flags))
  287. * ring_buffer_discard_commit(buffer, event);
  288. * else if (!filter_check_discard(trace_file, entry, buffer, event))
  289. * trace_buffer_unlock_commit(buffer, event, irq_flags, pc);
  290. *
  291. * if (__tt)
  292. * event_triggers_post_call(trace_file, __tt);
  293. * }
  294. *
  295. * static struct trace_event ftrace_event_type_<call> = {
  296. * .trace = trace_raw_output_<call>, <-- stage 2
  297. * };
  298. *
  299. * static char print_fmt_<call>[] = <TP_printk>;
  300. *
  301. * static struct trace_event_class __used event_class_<template> = {
  302. * .system = "<system>",
  303. * .fields_array = trace_event_fields_<call>,
  304. * .fields = LIST_HEAD_INIT(event_class_##call.fields),
  305. * .raw_init = trace_event_raw_init,
  306. * .probe = trace_event_raw_event_##call,
  307. * .reg = trace_event_reg,
  308. * };
  309. *
  310. * static struct trace_event_call event_<call> = {
  311. * .class = event_class_<template>,
  312. * {
  313. * .tp = &__tracepoint_<call>,
  314. * },
  315. * .event = &ftrace_event_type_<call>,
  316. * .print_fmt = print_fmt_<call>,
  317. * .flags = TRACE_EVENT_FL_TRACEPOINT,
  318. * };
  319. * // its only safe to use pointers when doing linker tricks to
  320. * // create an array.
  321. * static struct trace_event_call __used
  322. * __section("_ftrace_events") *__event_<call> = &event_<call>;
  323. *
  324. */
  325. #ifdef CONFIG_PERF_EVENTS
  326. #define _TRACE_PERF_PROTO(call, proto) \
  327. static notrace void \
  328. perf_trace_##call(void *__data, proto);
  329. #define _TRACE_PERF_INIT(call) \
  330. .perf_probe = perf_trace_##call,
  331. #else
  332. #define _TRACE_PERF_PROTO(call, proto)
  333. #define _TRACE_PERF_INIT(call)
  334. #endif /* CONFIG_PERF_EVENTS */
  335. #include "stages/stage6_event_callback.h"
  336. #undef DECLARE_EVENT_CLASS
  337. #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
  338. \
  339. static notrace void \
  340. trace_event_raw_event_##call(void *__data, proto) \
  341. { \
  342. struct trace_event_file *trace_file = __data; \
  343. struct trace_event_data_offsets_##call __maybe_unused __data_offsets;\
  344. struct trace_event_buffer fbuffer; \
  345. struct trace_event_raw_##call *entry; \
  346. int __data_size; \
  347. \
  348. if (trace_trigger_soft_disabled(trace_file)) \
  349. return; \
  350. \
  351. __data_size = trace_event_get_offsets_##call(&__data_offsets, args); \
  352. \
  353. entry = trace_event_buffer_reserve(&fbuffer, trace_file, \
  354. sizeof(*entry) + __data_size); \
  355. \
  356. if (!entry) \
  357. return; \
  358. \
  359. tstruct \
  360. \
  361. { assign; } \
  362. \
  363. trace_event_buffer_commit(&fbuffer); \
  364. }
  365. /*
  366. * The ftrace_test_probe is compiled out, it is only here as a build time check
  367. * to make sure that if the tracepoint handling changes, the ftrace probe will
  368. * fail to compile unless it too is updated.
  369. */
  370. #undef DEFINE_EVENT
  371. #define DEFINE_EVENT(template, call, proto, args) \
  372. static inline void ftrace_test_probe_##call(void) \
  373. { \
  374. check_trace_callback_type_##call(trace_event_raw_event_##template); \
  375. }
  376. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
  377. #include "stages/stage7_class_define.h"
  378. #undef DECLARE_EVENT_CLASS
  379. #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
  380. _TRACE_PERF_PROTO(call, PARAMS(proto)); \
  381. static char print_fmt_##call[] = print; \
  382. static struct trace_event_class __used __refdata event_class_##call = { \
  383. .system = TRACE_SYSTEM_STRING, \
  384. .fields_array = trace_event_fields_##call, \
  385. .fields = LIST_HEAD_INIT(event_class_##call.fields),\
  386. .raw_init = trace_event_raw_init, \
  387. .probe = trace_event_raw_event_##call, \
  388. .reg = trace_event_reg, \
  389. _TRACE_PERF_INIT(call) \
  390. };
  391. #undef DEFINE_EVENT
  392. #define DEFINE_EVENT(template, call, proto, args) \
  393. \
  394. static struct trace_event_call __used event_##call = { \
  395. .class = &event_class_##template, \
  396. { \
  397. .tp = &__tracepoint_##call, \
  398. }, \
  399. .event.funcs = &trace_event_type_funcs_##template, \
  400. .print_fmt = print_fmt_##template, \
  401. .flags = TRACE_EVENT_FL_TRACEPOINT, \
  402. }; \
  403. static struct trace_event_call __used \
  404. __section("_ftrace_events") *__event_##call = &event_##call
  405. #undef DEFINE_EVENT_PRINT
  406. #define DEFINE_EVENT_PRINT(template, call, proto, args, print) \
  407. \
  408. static char print_fmt_##call[] = print; \
  409. \
  410. static struct trace_event_call __used event_##call = { \
  411. .class = &event_class_##template, \
  412. { \
  413. .tp = &__tracepoint_##call, \
  414. }, \
  415. .event.funcs = &trace_event_type_funcs_##call, \
  416. .print_fmt = print_fmt_##call, \
  417. .flags = TRACE_EVENT_FL_TRACEPOINT, \
  418. }; \
  419. static struct trace_event_call __used \
  420. __section("_ftrace_events") *__event_##call = &event_##call
  421. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)