trace_events.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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 DECLARE_EVENT_SYSCALL_CLASS
  218. #define DECLARE_EVENT_SYSCALL_CLASS DECLARE_EVENT_CLASS
  219. #undef DEFINE_EVENT_PRINT
  220. #define DEFINE_EVENT_PRINT(template, name, proto, args, print)
  221. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
  222. #include "stages/stage5_get_offsets.h"
  223. #undef DECLARE_EVENT_CLASS
  224. #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
  225. static inline notrace int trace_event_get_offsets_##call( \
  226. struct trace_event_data_offsets_##call *__data_offsets, proto) \
  227. { \
  228. int __data_size = 0; \
  229. int __maybe_unused __item_length; \
  230. struct trace_event_raw_##call __maybe_unused *entry; \
  231. \
  232. tstruct; \
  233. \
  234. return __data_size; \
  235. }
  236. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
  237. /*
  238. * Stage 4 of the trace events.
  239. *
  240. * Override the macros in the event tracepoint header <trace/events/XXX.h>
  241. * to include the following:
  242. *
  243. * For those macros defined with TRACE_EVENT:
  244. *
  245. * static struct trace_event_call event_<call>;
  246. *
  247. * static void trace_event_raw_event_<call>(void *__data, proto)
  248. * {
  249. * struct trace_event_file *trace_file = __data;
  250. * struct trace_event_call *event_call = trace_file->event_call;
  251. * struct trace_event_data_offsets_<call> __maybe_unused __data_offsets;
  252. * unsigned long eflags = trace_file->flags;
  253. * enum event_trigger_type __tt = ETT_NONE;
  254. * struct ring_buffer_event *event;
  255. * struct trace_event_raw_<call> *entry; <-- defined in stage 1
  256. * struct trace_buffer *buffer;
  257. * unsigned long irq_flags;
  258. * int __data_size;
  259. * int pc;
  260. *
  261. * if (!(eflags & EVENT_FILE_FL_TRIGGER_COND)) {
  262. * if (eflags & EVENT_FILE_FL_TRIGGER_MODE)
  263. * event_triggers_call(trace_file, NULL);
  264. * if (eflags & EVENT_FILE_FL_SOFT_DISABLED)
  265. * return;
  266. * }
  267. *
  268. * local_save_flags(irq_flags);
  269. * pc = preempt_count();
  270. *
  271. * __data_size = trace_event_get_offsets_<call>(&__data_offsets, args);
  272. *
  273. * event = trace_event_buffer_lock_reserve(&buffer, trace_file,
  274. * event_<call>->event.type,
  275. * sizeof(*entry) + __data_size,
  276. * irq_flags, pc);
  277. * if (!event)
  278. * return;
  279. * entry = ring_buffer_event_data(event);
  280. *
  281. * { <assign>; } <-- Here we assign the entries by the __field and
  282. * __array macros.
  283. *
  284. * if (eflags & EVENT_FILE_FL_TRIGGER_COND)
  285. * __tt = event_triggers_call(trace_file, entry);
  286. *
  287. * if (test_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT,
  288. * &trace_file->flags))
  289. * ring_buffer_discard_commit(buffer, event);
  290. * else if (!filter_check_discard(trace_file, entry, buffer, event))
  291. * trace_buffer_unlock_commit(buffer, event, irq_flags, pc);
  292. *
  293. * if (__tt)
  294. * event_triggers_post_call(trace_file, __tt);
  295. * }
  296. *
  297. * static struct trace_event ftrace_event_type_<call> = {
  298. * .trace = trace_raw_output_<call>, <-- stage 2
  299. * };
  300. *
  301. * static char print_fmt_<call>[] = <TP_printk>;
  302. *
  303. * static struct trace_event_class __used event_class_<template> = {
  304. * .system = "<system>",
  305. * .fields_array = trace_event_fields_<call>,
  306. * .fields = LIST_HEAD_INIT(event_class_##call.fields),
  307. * .raw_init = trace_event_raw_init,
  308. * .probe = trace_event_raw_event_##call,
  309. * .reg = trace_event_reg,
  310. * };
  311. *
  312. * static struct trace_event_call event_<call> = {
  313. * .class = event_class_<template>,
  314. * {
  315. * .tp = &__tracepoint_<call>,
  316. * },
  317. * .event = &ftrace_event_type_<call>,
  318. * .print_fmt = print_fmt_<call>,
  319. * .flags = TRACE_EVENT_FL_TRACEPOINT,
  320. * };
  321. * // its only safe to use pointers when doing linker tricks to
  322. * // create an array.
  323. * static struct trace_event_call __used
  324. * __section("_ftrace_events") *__event_<call> = &event_<call>;
  325. *
  326. */
  327. #ifdef CONFIG_PERF_EVENTS
  328. #define _TRACE_PERF_PROTO(call, proto) \
  329. static notrace void \
  330. perf_trace_##call(void *__data, proto);
  331. #define _TRACE_PERF_INIT(call) \
  332. .perf_probe = perf_trace_##call,
  333. #else
  334. #define _TRACE_PERF_PROTO(call, proto)
  335. #define _TRACE_PERF_INIT(call)
  336. #endif /* CONFIG_PERF_EVENTS */
  337. #include "stages/stage6_event_callback.h"
  338. #undef __DECLARE_EVENT_CLASS
  339. #define __DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
  340. static notrace void \
  341. do_trace_event_raw_event_##call(void *__data, proto) \
  342. { \
  343. struct trace_event_file *trace_file = __data; \
  344. struct trace_event_data_offsets_##call __maybe_unused __data_offsets;\
  345. struct trace_event_buffer fbuffer; \
  346. struct trace_event_raw_##call *entry; \
  347. int __data_size; \
  348. \
  349. if (trace_trigger_soft_disabled(trace_file)) \
  350. return; \
  351. \
  352. __data_size = trace_event_get_offsets_##call(&__data_offsets, args); \
  353. \
  354. entry = trace_event_buffer_reserve(&fbuffer, trace_file, \
  355. sizeof(*entry) + __data_size); \
  356. \
  357. if (!entry) \
  358. return; \
  359. \
  360. tstruct \
  361. \
  362. { assign; } \
  363. \
  364. trace_event_buffer_commit(&fbuffer); \
  365. }
  366. #undef DECLARE_EVENT_CLASS
  367. #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
  368. __DECLARE_EVENT_CLASS(call, PARAMS(proto), PARAMS(args), PARAMS(tstruct), \
  369. PARAMS(assign), PARAMS(print)) \
  370. static notrace void \
  371. trace_event_raw_event_##call(void *__data, proto) \
  372. { \
  373. do_trace_event_raw_event_##call(__data, args); \
  374. }
  375. #undef DECLARE_EVENT_SYSCALL_CLASS
  376. #define DECLARE_EVENT_SYSCALL_CLASS(call, proto, args, tstruct, assign, print) \
  377. __DECLARE_EVENT_CLASS(call, PARAMS(proto), PARAMS(args), PARAMS(tstruct), \
  378. PARAMS(assign), PARAMS(print)) \
  379. static notrace void \
  380. trace_event_raw_event_##call(void *__data, proto) \
  381. { \
  382. preempt_disable_notrace(); \
  383. do_trace_event_raw_event_##call(__data, args); \
  384. preempt_enable_notrace(); \
  385. }
  386. /*
  387. * The ftrace_test_probe is compiled out, it is only here as a build time check
  388. * to make sure that if the tracepoint handling changes, the ftrace probe will
  389. * fail to compile unless it too is updated.
  390. */
  391. #undef DEFINE_EVENT
  392. #define DEFINE_EVENT(template, call, proto, args) \
  393. static inline void ftrace_test_probe_##call(void) \
  394. { \
  395. check_trace_callback_type_##call(trace_event_raw_event_##template); \
  396. }
  397. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
  398. #undef __DECLARE_EVENT_CLASS
  399. #include "stages/stage7_class_define.h"
  400. #undef DECLARE_EVENT_CLASS
  401. #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
  402. _TRACE_PERF_PROTO(call, PARAMS(proto)); \
  403. static char print_fmt_##call[] = print; \
  404. static struct trace_event_class __used __refdata event_class_##call = { \
  405. .system = TRACE_SYSTEM_STRING, \
  406. .fields_array = trace_event_fields_##call, \
  407. .fields = LIST_HEAD_INIT(event_class_##call.fields),\
  408. .raw_init = trace_event_raw_init, \
  409. .probe = trace_event_raw_event_##call, \
  410. .reg = trace_event_reg, \
  411. _TRACE_PERF_INIT(call) \
  412. };
  413. #undef DEFINE_EVENT
  414. #define DEFINE_EVENT(template, call, proto, args) \
  415. \
  416. static struct trace_event_call __used event_##call = { \
  417. .class = &event_class_##template, \
  418. { \
  419. .tp = &__tracepoint_##call, \
  420. }, \
  421. .event.funcs = &trace_event_type_funcs_##template, \
  422. .print_fmt = print_fmt_##template, \
  423. .flags = TRACE_EVENT_FL_TRACEPOINT, \
  424. }; \
  425. static struct trace_event_call __used \
  426. __section("_ftrace_events") *__event_##call = &event_##call
  427. #undef DEFINE_EVENT_PRINT
  428. #define DEFINE_EVENT_PRINT(template, call, proto, args, print) \
  429. \
  430. static char print_fmt_##call[] = print; \
  431. \
  432. static struct trace_event_call __used event_##call = { \
  433. .class = &event_class_##template, \
  434. { \
  435. .tp = &__tracepoint_##call, \
  436. }, \
  437. .event.funcs = &trace_event_type_funcs_##call, \
  438. .print_fmt = print_fmt_##call, \
  439. .flags = TRACE_EVENT_FL_TRACEPOINT, \
  440. }; \
  441. static struct trace_event_call __used \
  442. __section("_ftrace_events") *__event_##call = &event_##call
  443. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)