trace_events.h 15 KB

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