ftrace.h 22 KB

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