ftrace.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  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 ftrace_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/ftrace_event.h>
  19. /*
  20. * DECLARE_EVENT_CLASS can be used to add a generic function
  21. * handlers for events. That is, if all events have the same
  22. * parameters and just have distinct trace points.
  23. * Each tracepoint can be defined with DEFINE_EVENT and that
  24. * will map the DECLARE_EVENT_CLASS to the tracepoint.
  25. *
  26. * TRACE_EVENT is a one to one mapping between tracepoint and template.
  27. */
  28. #undef TRACE_EVENT
  29. #define TRACE_EVENT(name, proto, args, tstruct, assign, print) \
  30. DECLARE_EVENT_CLASS(name, \
  31. PARAMS(proto), \
  32. PARAMS(args), \
  33. PARAMS(tstruct), \
  34. PARAMS(assign), \
  35. PARAMS(print)); \
  36. DEFINE_EVENT(name, name, PARAMS(proto), PARAMS(args));
  37. #undef __field
  38. #define __field(type, item) type item;
  39. #undef __field_ext
  40. #define __field_ext(type, item, filter_type) type item;
  41. #undef __array
  42. #define __array(type, item, len) type item[len];
  43. #undef __dynamic_array
  44. #define __dynamic_array(type, item, len) u32 __data_loc_##item;
  45. #undef __string
  46. #define __string(item, src) __dynamic_array(char, item, -1)
  47. #undef TP_STRUCT__entry
  48. #define TP_STRUCT__entry(args...) args
  49. #undef DECLARE_EVENT_CLASS
  50. #define DECLARE_EVENT_CLASS(name, proto, args, tstruct, assign, print) \
  51. struct ftrace_raw_##name { \
  52. struct trace_entry ent; \
  53. tstruct \
  54. char __data[0]; \
  55. };
  56. #undef DEFINE_EVENT
  57. #define DEFINE_EVENT(template, name, proto, args) \
  58. static struct ftrace_event_call \
  59. __attribute__((__aligned__(4))) event_##name
  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. #undef __cpparg
  64. #define __cpparg(arg...) arg
  65. /* Callbacks are meaningless to ftrace. */
  66. #undef TRACE_EVENT_FN
  67. #define TRACE_EVENT_FN(name, proto, args, tstruct, \
  68. assign, print, reg, unreg) \
  69. TRACE_EVENT(name, __cpparg(proto), __cpparg(args), \
  70. __cpparg(tstruct), __cpparg(assign), __cpparg(print)) \
  71. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
  72. /*
  73. * Stage 2 of the trace events.
  74. *
  75. * Include the following:
  76. *
  77. * struct ftrace_data_offsets_<call> {
  78. * u32 <item1>;
  79. * u32 <item2>;
  80. * [...]
  81. * };
  82. *
  83. * The __dynamic_array() macro will create each u32 <item>, this is
  84. * to keep the offset of each array from the beginning of the event.
  85. * The size of an array is also encoded, in the higher 16 bits of <item>.
  86. */
  87. #undef __field
  88. #define __field(type, item)
  89. #undef __field_ext
  90. #define __field_ext(type, item, filter_type)
  91. #undef __array
  92. #define __array(type, item, len)
  93. #undef __dynamic_array
  94. #define __dynamic_array(type, item, len) u32 item;
  95. #undef __string
  96. #define __string(item, src) __dynamic_array(char, item, -1)
  97. #undef DECLARE_EVENT_CLASS
  98. #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
  99. struct ftrace_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. DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
  107. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
  108. /*
  109. * Stage 3 of the trace events.
  110. *
  111. * Override the macros in <trace/trace_events.h> to include the following:
  112. *
  113. * enum print_line_t
  114. * ftrace_raw_output_<call>(struct trace_iterator *iter, int flags)
  115. * {
  116. * struct trace_seq *s = &iter->seq;
  117. * struct ftrace_raw_<call> *field; <-- defined in stage 1
  118. * struct trace_entry *entry;
  119. * struct trace_seq *p;
  120. * int ret;
  121. *
  122. * entry = iter->ent;
  123. *
  124. * if (entry->type != event_<call>.id) {
  125. * WARN_ON_ONCE(1);
  126. * return TRACE_TYPE_UNHANDLED;
  127. * }
  128. *
  129. * field = (typeof(field))entry;
  130. *
  131. * p = &get_cpu_var(ftrace_event_seq);
  132. * trace_seq_init(p);
  133. * ret = trace_seq_printf(s, "%s: ", <call>);
  134. * if (ret)
  135. * ret = trace_seq_printf(s, <TP_printk> "\n");
  136. * put_cpu();
  137. * if (!ret)
  138. * return TRACE_TYPE_PARTIAL_LINE;
  139. *
  140. * return TRACE_TYPE_HANDLED;
  141. * }
  142. *
  143. * This is the method used to print the raw event to the trace
  144. * output format. Note, this is not needed if the data is read
  145. * in binary.
  146. */
  147. #undef __entry
  148. #define __entry field
  149. #undef TP_printk
  150. #define TP_printk(fmt, args...) fmt "\n", args
  151. #undef __get_dynamic_array
  152. #define __get_dynamic_array(field) \
  153. ((void *)__entry + (__entry->__data_loc_##field & 0xffff))
  154. #undef __get_str
  155. #define __get_str(field) (char *)__get_dynamic_array(field)
  156. #undef __print_flags
  157. #define __print_flags(flag, delim, flag_array...) \
  158. ({ \
  159. static const struct trace_print_flags __flags[] = \
  160. { flag_array, { -1, NULL }}; \
  161. ftrace_print_flags_seq(p, delim, flag, __flags); \
  162. })
  163. #undef __print_symbolic
  164. #define __print_symbolic(value, symbol_array...) \
  165. ({ \
  166. static const struct trace_print_flags symbols[] = \
  167. { symbol_array, { -1, NULL }}; \
  168. ftrace_print_symbols_seq(p, value, symbols); \
  169. })
  170. #undef __print_hex
  171. #define __print_hex(buf, buf_len) ftrace_print_hex_seq(p, buf, buf_len)
  172. #undef DECLARE_EVENT_CLASS
  173. #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
  174. static notrace enum print_line_t \
  175. ftrace_raw_output_id_##call(int event_id, const char *name, \
  176. struct trace_iterator *iter, int flags) \
  177. { \
  178. struct trace_seq *s = &iter->seq; \
  179. struct ftrace_raw_##call *field; \
  180. struct trace_entry *entry; \
  181. struct trace_seq *p; \
  182. int ret; \
  183. \
  184. entry = iter->ent; \
  185. \
  186. if (entry->type != event_id) { \
  187. WARN_ON_ONCE(1); \
  188. return TRACE_TYPE_UNHANDLED; \
  189. } \
  190. \
  191. field = (typeof(field))entry; \
  192. \
  193. p = &get_cpu_var(ftrace_event_seq); \
  194. trace_seq_init(p); \
  195. ret = trace_seq_printf(s, "%s: ", name); \
  196. if (ret) \
  197. ret = trace_seq_printf(s, print); \
  198. put_cpu(); \
  199. if (!ret) \
  200. return TRACE_TYPE_PARTIAL_LINE; \
  201. \
  202. return TRACE_TYPE_HANDLED; \
  203. }
  204. #undef DEFINE_EVENT
  205. #define DEFINE_EVENT(template, name, proto, args) \
  206. static notrace enum print_line_t \
  207. ftrace_raw_output_##name(struct trace_iterator *iter, int flags) \
  208. { \
  209. return ftrace_raw_output_id_##template(event_##name.id, \
  210. #name, iter, flags); \
  211. }
  212. #undef DEFINE_EVENT_PRINT
  213. #define DEFINE_EVENT_PRINT(template, call, proto, args, print) \
  214. static notrace enum print_line_t \
  215. ftrace_raw_output_##call(struct trace_iterator *iter, int flags) \
  216. { \
  217. struct trace_seq *s = &iter->seq; \
  218. struct ftrace_raw_##template *field; \
  219. struct trace_entry *entry; \
  220. struct trace_seq *p; \
  221. int ret; \
  222. \
  223. entry = iter->ent; \
  224. \
  225. if (entry->type != event_##call.id) { \
  226. WARN_ON_ONCE(1); \
  227. return TRACE_TYPE_UNHANDLED; \
  228. } \
  229. \
  230. field = (typeof(field))entry; \
  231. \
  232. p = &get_cpu_var(ftrace_event_seq); \
  233. trace_seq_init(p); \
  234. ret = trace_seq_printf(s, "%s: ", #call); \
  235. if (ret) \
  236. ret = trace_seq_printf(s, print); \
  237. put_cpu(); \
  238. if (!ret) \
  239. return TRACE_TYPE_PARTIAL_LINE; \
  240. \
  241. return TRACE_TYPE_HANDLED; \
  242. }
  243. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
  244. #undef __field_ext
  245. #define __field_ext(type, item, filter_type) \
  246. ret = trace_define_field(event_call, #type, #item, \
  247. offsetof(typeof(field), item), \
  248. sizeof(field.item), \
  249. is_signed_type(type), filter_type); \
  250. if (ret) \
  251. return ret;
  252. #undef __field
  253. #define __field(type, item) __field_ext(type, item, FILTER_OTHER)
  254. #undef __array
  255. #define __array(type, item, len) \
  256. BUILD_BUG_ON(len > MAX_FILTER_STR_VAL); \
  257. ret = trace_define_field(event_call, #type "[" #len "]", #item, \
  258. offsetof(typeof(field), item), \
  259. sizeof(field.item), \
  260. is_signed_type(type), FILTER_OTHER); \
  261. if (ret) \
  262. return ret;
  263. #undef __dynamic_array
  264. #define __dynamic_array(type, item, len) \
  265. ret = trace_define_field(event_call, "__data_loc " #type "[]", #item, \
  266. offsetof(typeof(field), __data_loc_##item), \
  267. sizeof(field.__data_loc_##item), \
  268. is_signed_type(type), FILTER_OTHER);
  269. #undef __string
  270. #define __string(item, src) __dynamic_array(char, item, -1)
  271. #undef DECLARE_EVENT_CLASS
  272. #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, func, print) \
  273. static int notrace \
  274. ftrace_define_fields_##call(struct ftrace_event_call *event_call) \
  275. { \
  276. struct ftrace_raw_##call field; \
  277. int ret; \
  278. \
  279. tstruct; \
  280. \
  281. return ret; \
  282. }
  283. #undef DEFINE_EVENT
  284. #define DEFINE_EVENT(template, name, proto, args)
  285. #undef DEFINE_EVENT_PRINT
  286. #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
  287. DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
  288. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
  289. /*
  290. * remember the offset of each array from the beginning of the event.
  291. */
  292. #undef __entry
  293. #define __entry entry
  294. #undef __field
  295. #define __field(type, item)
  296. #undef __field_ext
  297. #define __field_ext(type, item, filter_type)
  298. #undef __array
  299. #define __array(type, item, len)
  300. #undef __dynamic_array
  301. #define __dynamic_array(type, item, len) \
  302. __data_offsets->item = __data_size + \
  303. offsetof(typeof(*entry), __data); \
  304. __data_offsets->item |= (len * sizeof(type)) << 16; \
  305. __data_size += (len) * sizeof(type);
  306. #undef __string
  307. #define __string(item, src) __dynamic_array(char, item, strlen(src) + 1)
  308. #undef DECLARE_EVENT_CLASS
  309. #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
  310. static inline notrace int ftrace_get_offsets_##call( \
  311. struct ftrace_data_offsets_##call *__data_offsets, proto) \
  312. { \
  313. int __data_size = 0; \
  314. struct ftrace_raw_##call __maybe_unused *entry; \
  315. \
  316. tstruct; \
  317. \
  318. return __data_size; \
  319. }
  320. #undef DEFINE_EVENT
  321. #define DEFINE_EVENT(template, name, proto, args)
  322. #undef DEFINE_EVENT_PRINT
  323. #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
  324. DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
  325. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
  326. #ifdef CONFIG_PERF_EVENTS
  327. /*
  328. * Generate the functions needed for tracepoint perf_event support.
  329. *
  330. * NOTE: The insertion profile callback (ftrace_profile_<call>) is defined later
  331. *
  332. * static int ftrace_profile_enable_<call>(void)
  333. * {
  334. * return register_trace_<call>(ftrace_profile_<call>);
  335. * }
  336. *
  337. * static void ftrace_profile_disable_<call>(void)
  338. * {
  339. * unregister_trace_<call>(ftrace_profile_<call>);
  340. * }
  341. *
  342. */
  343. #undef DECLARE_EVENT_CLASS
  344. #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print)
  345. #undef DEFINE_EVENT
  346. #define DEFINE_EVENT(template, name, proto, args) \
  347. \
  348. static void perf_trace_##name(proto); \
  349. \
  350. static notrace int \
  351. perf_trace_enable_##name(struct ftrace_event_call *unused) \
  352. { \
  353. return register_trace_##name(perf_trace_##name); \
  354. } \
  355. \
  356. static notrace void \
  357. perf_trace_disable_##name(struct ftrace_event_call *unused) \
  358. { \
  359. unregister_trace_##name(perf_trace_##name); \
  360. }
  361. #undef DEFINE_EVENT_PRINT
  362. #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
  363. DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
  364. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
  365. #endif /* CONFIG_PERF_EVENTS */
  366. /*
  367. * Stage 4 of the trace events.
  368. *
  369. * Override the macros in <trace/trace_events.h> to include the following:
  370. *
  371. * static void ftrace_event_<call>(proto)
  372. * {
  373. * event_trace_printk(_RET_IP_, "<call>: " <fmt>);
  374. * }
  375. *
  376. * static int ftrace_reg_event_<call>(struct ftrace_event_call *unused)
  377. * {
  378. * return register_trace_<call>(ftrace_event_<call>);
  379. * }
  380. *
  381. * static void ftrace_unreg_event_<call>(struct ftrace_event_call *unused)
  382. * {
  383. * unregister_trace_<call>(ftrace_event_<call>);
  384. * }
  385. *
  386. *
  387. * For those macros defined with TRACE_EVENT:
  388. *
  389. * static struct ftrace_event_call event_<call>;
  390. *
  391. * static void ftrace_raw_event_<call>(proto)
  392. * {
  393. * struct ftrace_data_offsets_<call> __maybe_unused __data_offsets;
  394. * struct ring_buffer_event *event;
  395. * struct ftrace_raw_<call> *entry; <-- defined in stage 1
  396. * struct ring_buffer *buffer;
  397. * unsigned long irq_flags;
  398. * int __data_size;
  399. * int pc;
  400. *
  401. * local_save_flags(irq_flags);
  402. * pc = preempt_count();
  403. *
  404. * __data_size = ftrace_get_offsets_<call>(&__data_offsets, args);
  405. *
  406. * event = trace_current_buffer_lock_reserve(&buffer,
  407. * event_<call>.id,
  408. * sizeof(*entry) + __data_size,
  409. * irq_flags, pc);
  410. * if (!event)
  411. * return;
  412. * entry = ring_buffer_event_data(event);
  413. *
  414. * { <assign>; } <-- Here we assign the entries by the __field and
  415. * __array macros.
  416. *
  417. * if (!filter_current_check_discard(buffer, event_call, entry, event))
  418. * trace_current_buffer_unlock_commit(buffer,
  419. * event, irq_flags, pc);
  420. * }
  421. *
  422. * static int ftrace_raw_reg_event_<call>(struct ftrace_event_call *unused)
  423. * {
  424. * return register_trace_<call>(ftrace_raw_event_<call>);
  425. * }
  426. *
  427. * static void ftrace_unreg_event_<call>(struct ftrace_event_call *unused)
  428. * {
  429. * unregister_trace_<call>(ftrace_raw_event_<call>);
  430. * }
  431. *
  432. * static struct trace_event ftrace_event_type_<call> = {
  433. * .trace = ftrace_raw_output_<call>, <-- stage 2
  434. * };
  435. *
  436. * static const char print_fmt_<call>[] = <TP_printk>;
  437. *
  438. * static struct ftrace_event_call __used
  439. * __attribute__((__aligned__(4)))
  440. * __attribute__((section("_ftrace_events"))) event_<call> = {
  441. * .name = "<call>",
  442. * .system = "<system>",
  443. * .raw_init = trace_event_raw_init,
  444. * .regfunc = ftrace_reg_event_<call>,
  445. * .unregfunc = ftrace_unreg_event_<call>,
  446. * .print_fmt = print_fmt_<call>,
  447. * .define_fields = ftrace_define_fields_<call>,
  448. * }
  449. *
  450. */
  451. #ifdef CONFIG_PERF_EVENTS
  452. #define _TRACE_PERF_INIT(call) \
  453. .perf_event_enable = perf_trace_enable_##call, \
  454. .perf_event_disable = perf_trace_disable_##call,
  455. #else
  456. #define _TRACE_PERF_INIT(call)
  457. #endif /* CONFIG_PERF_EVENTS */
  458. #undef __entry
  459. #define __entry entry
  460. #undef __field
  461. #define __field(type, item)
  462. #undef __array
  463. #define __array(type, item, len)
  464. #undef __dynamic_array
  465. #define __dynamic_array(type, item, len) \
  466. __entry->__data_loc_##item = __data_offsets.item;
  467. #undef __string
  468. #define __string(item, src) __dynamic_array(char, item, -1) \
  469. #undef __assign_str
  470. #define __assign_str(dst, src) \
  471. strcpy(__get_str(dst), src);
  472. #undef TP_fast_assign
  473. #define TP_fast_assign(args...) args
  474. #undef TP_perf_assign
  475. #define TP_perf_assign(args...)
  476. #undef DECLARE_EVENT_CLASS
  477. #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
  478. \
  479. static notrace void \
  480. ftrace_raw_event_id_##call(struct ftrace_event_call *event_call, \
  481. proto) \
  482. { \
  483. struct ftrace_data_offsets_##call __maybe_unused __data_offsets;\
  484. struct ring_buffer_event *event; \
  485. struct ftrace_raw_##call *entry; \
  486. struct ring_buffer *buffer; \
  487. unsigned long irq_flags; \
  488. int __data_size; \
  489. int pc; \
  490. \
  491. local_save_flags(irq_flags); \
  492. pc = preempt_count(); \
  493. \
  494. __data_size = ftrace_get_offsets_##call(&__data_offsets, args); \
  495. \
  496. event = trace_current_buffer_lock_reserve(&buffer, \
  497. event_call->id, \
  498. sizeof(*entry) + __data_size, \
  499. irq_flags, pc); \
  500. if (!event) \
  501. return; \
  502. entry = ring_buffer_event_data(event); \
  503. \
  504. tstruct \
  505. \
  506. { assign; } \
  507. \
  508. if (!filter_current_check_discard(buffer, event_call, entry, event)) \
  509. trace_nowake_buffer_unlock_commit(buffer, \
  510. event, irq_flags, pc); \
  511. }
  512. #undef DEFINE_EVENT
  513. #define DEFINE_EVENT(template, call, proto, args) \
  514. \
  515. static notrace void ftrace_raw_event_##call(proto) \
  516. { \
  517. ftrace_raw_event_id_##template(&event_##call, args); \
  518. } \
  519. \
  520. static notrace int \
  521. ftrace_raw_reg_event_##call(struct ftrace_event_call *unused) \
  522. { \
  523. return register_trace_##call(ftrace_raw_event_##call); \
  524. } \
  525. \
  526. static notrace void \
  527. ftrace_raw_unreg_event_##call(struct ftrace_event_call *unused) \
  528. { \
  529. unregister_trace_##call(ftrace_raw_event_##call); \
  530. } \
  531. \
  532. static struct trace_event ftrace_event_type_##call = { \
  533. .trace = ftrace_raw_output_##call, \
  534. };
  535. #undef DEFINE_EVENT_PRINT
  536. #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
  537. DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
  538. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
  539. #undef __entry
  540. #define __entry REC
  541. #undef __print_flags
  542. #undef __print_symbolic
  543. #undef __get_dynamic_array
  544. #undef __get_str
  545. #undef TP_printk
  546. #define TP_printk(fmt, args...) "\"" fmt "\", " __stringify(args)
  547. #undef DECLARE_EVENT_CLASS
  548. #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
  549. static const char print_fmt_##call[] = print;
  550. #undef DEFINE_EVENT
  551. #define DEFINE_EVENT(template, call, proto, args) \
  552. \
  553. static struct ftrace_event_call __used \
  554. __attribute__((__aligned__(4))) \
  555. __attribute__((section("_ftrace_events"))) event_##call = { \
  556. .name = #call, \
  557. .system = __stringify(TRACE_SYSTEM), \
  558. .event = &ftrace_event_type_##call, \
  559. .raw_init = trace_event_raw_init, \
  560. .regfunc = ftrace_raw_reg_event_##call, \
  561. .unregfunc = ftrace_raw_unreg_event_##call, \
  562. .print_fmt = print_fmt_##template, \
  563. .define_fields = ftrace_define_fields_##template, \
  564. _TRACE_PERF_INIT(call) \
  565. }
  566. #undef DEFINE_EVENT_PRINT
  567. #define DEFINE_EVENT_PRINT(template, call, proto, args, print) \
  568. \
  569. static const char print_fmt_##call[] = print; \
  570. \
  571. static struct ftrace_event_call __used \
  572. __attribute__((__aligned__(4))) \
  573. __attribute__((section("_ftrace_events"))) event_##call = { \
  574. .name = #call, \
  575. .system = __stringify(TRACE_SYSTEM), \
  576. .event = &ftrace_event_type_##call, \
  577. .raw_init = trace_event_raw_init, \
  578. .regfunc = ftrace_raw_reg_event_##call, \
  579. .unregfunc = ftrace_raw_unreg_event_##call, \
  580. .print_fmt = print_fmt_##call, \
  581. .define_fields = ftrace_define_fields_##template, \
  582. _TRACE_PERF_INIT(call) \
  583. }
  584. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
  585. /*
  586. * Define the insertion callback to perf events
  587. *
  588. * The job is very similar to ftrace_raw_event_<call> except that we don't
  589. * insert in the ring buffer but in a perf counter.
  590. *
  591. * static void ftrace_perf_<call>(proto)
  592. * {
  593. * struct ftrace_data_offsets_<call> __maybe_unused __data_offsets;
  594. * struct ftrace_event_call *event_call = &event_<call>;
  595. * extern void perf_tp_event(int, u64, u64, void *, int);
  596. * struct ftrace_raw_##call *entry;
  597. * struct perf_trace_buf *trace_buf;
  598. * u64 __addr = 0, __count = 1;
  599. * unsigned long irq_flags;
  600. * struct trace_entry *ent;
  601. * int __entry_size;
  602. * int __data_size;
  603. * int __cpu
  604. * int pc;
  605. *
  606. * pc = preempt_count();
  607. *
  608. * __data_size = ftrace_get_offsets_<call>(&__data_offsets, args);
  609. *
  610. * // Below we want to get the aligned size by taking into account
  611. * // the u32 field that will later store the buffer size
  612. * __entry_size = ALIGN(__data_size + sizeof(*entry) + sizeof(u32),
  613. * sizeof(u64));
  614. * __entry_size -= sizeof(u32);
  615. *
  616. * // Protect the non nmi buffer
  617. * // This also protects the rcu read side
  618. * local_irq_save(irq_flags);
  619. * __cpu = smp_processor_id();
  620. *
  621. * if (in_nmi())
  622. * trace_buf = rcu_dereference_sched(perf_trace_buf_nmi);
  623. * else
  624. * trace_buf = rcu_dereference_sched(perf_trace_buf);
  625. *
  626. * if (!trace_buf)
  627. * goto end;
  628. *
  629. * trace_buf = per_cpu_ptr(trace_buf, __cpu);
  630. *
  631. * // Avoid recursion from perf that could mess up the buffer
  632. * if (trace_buf->recursion++)
  633. * goto end_recursion;
  634. *
  635. * raw_data = trace_buf->buf;
  636. *
  637. * // Make recursion update visible before entering perf_tp_event
  638. * // so that we protect from perf recursions.
  639. *
  640. * barrier();
  641. *
  642. * //zero dead bytes from alignment to avoid stack leak to userspace:
  643. * *(u64 *)(&raw_data[__entry_size - sizeof(u64)]) = 0ULL;
  644. * entry = (struct ftrace_raw_<call> *)raw_data;
  645. * ent = &entry->ent;
  646. * tracing_generic_entry_update(ent, irq_flags, pc);
  647. * ent->type = event_call->id;
  648. *
  649. * <tstruct> <- do some jobs with dynamic arrays
  650. *
  651. * <assign> <- affect our values
  652. *
  653. * perf_tp_event(event_call->id, __addr, __count, entry,
  654. * __entry_size); <- submit them to perf counter
  655. *
  656. * }
  657. */
  658. #ifdef CONFIG_PERF_EVENTS
  659. #undef __entry
  660. #define __entry entry
  661. #undef __get_dynamic_array
  662. #define __get_dynamic_array(field) \
  663. ((void *)__entry + (__entry->__data_loc_##field & 0xffff))
  664. #undef __get_str
  665. #define __get_str(field) (char *)__get_dynamic_array(field)
  666. #undef __perf_addr
  667. #define __perf_addr(a) __addr = (a)
  668. #undef __perf_count
  669. #define __perf_count(c) __count = (c)
  670. #undef DECLARE_EVENT_CLASS
  671. #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
  672. static notrace void \
  673. perf_trace_templ_##call(struct ftrace_event_call *event_call, \
  674. struct pt_regs *__regs, proto) \
  675. { \
  676. struct ftrace_data_offsets_##call __maybe_unused __data_offsets;\
  677. struct ftrace_raw_##call *entry; \
  678. u64 __addr = 0, __count = 1; \
  679. unsigned long irq_flags; \
  680. int __entry_size; \
  681. int __data_size; \
  682. int rctx; \
  683. \
  684. __data_size = ftrace_get_offsets_##call(&__data_offsets, args); \
  685. __entry_size = ALIGN(__data_size + sizeof(*entry) + sizeof(u32),\
  686. sizeof(u64)); \
  687. __entry_size -= sizeof(u32); \
  688. \
  689. if (WARN_ONCE(__entry_size > PERF_MAX_TRACE_SIZE, \
  690. "profile buffer not large enough")) \
  691. return; \
  692. entry = (struct ftrace_raw_##call *)perf_trace_buf_prepare( \
  693. __entry_size, event_call->id, &rctx, &irq_flags); \
  694. if (!entry) \
  695. return; \
  696. tstruct \
  697. \
  698. { assign; } \
  699. \
  700. perf_trace_buf_submit(entry, __entry_size, rctx, __addr, \
  701. __count, irq_flags, __regs); \
  702. }
  703. #undef DEFINE_EVENT
  704. #define DEFINE_EVENT(template, call, proto, args) \
  705. static notrace void perf_trace_##call(proto) \
  706. { \
  707. struct ftrace_event_call *event_call = &event_##call; \
  708. struct pt_regs *__regs = &get_cpu_var(perf_trace_regs); \
  709. \
  710. perf_fetch_caller_regs(__regs, 1); \
  711. \
  712. perf_trace_templ_##template(event_call, __regs, args); \
  713. \
  714. put_cpu_var(perf_trace_regs); \
  715. }
  716. #undef DEFINE_EVENT_PRINT
  717. #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
  718. DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
  719. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
  720. #endif /* CONFIG_PERF_EVENTS */
  721. #undef _TRACE_PROFILE_INIT