ftrace.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  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. \
  57. static struct ftrace_event_class event_class_##name;
  58. #undef DEFINE_EVENT
  59. #define DEFINE_EVENT(template, name, proto, args) \
  60. static struct ftrace_event_call \
  61. __attribute__((__aligned__(4))) event_##name
  62. #undef DEFINE_EVENT_PRINT
  63. #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
  64. DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
  65. #undef __cpparg
  66. #define __cpparg(arg...) arg
  67. /* Callbacks are meaningless to ftrace. */
  68. #undef TRACE_EVENT_FN
  69. #define TRACE_EVENT_FN(name, proto, args, tstruct, \
  70. assign, print, reg, unreg) \
  71. TRACE_EVENT(name, __cpparg(proto), __cpparg(args), \
  72. __cpparg(tstruct), __cpparg(assign), __cpparg(print)) \
  73. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
  74. /*
  75. * Stage 2 of the trace events.
  76. *
  77. * Include the following:
  78. *
  79. * struct ftrace_data_offsets_<call> {
  80. * u32 <item1>;
  81. * u32 <item2>;
  82. * [...]
  83. * };
  84. *
  85. * The __dynamic_array() macro will create each u32 <item>, this is
  86. * to keep the offset of each array from the beginning of the event.
  87. * The size of an array is also encoded, in the higher 16 bits of <item>.
  88. */
  89. #undef __field
  90. #define __field(type, item)
  91. #undef __field_ext
  92. #define __field_ext(type, item, filter_type)
  93. #undef __array
  94. #define __array(type, item, len)
  95. #undef __dynamic_array
  96. #define __dynamic_array(type, item, len) u32 item;
  97. #undef __string
  98. #define __string(item, src) __dynamic_array(char, item, -1)
  99. #undef DECLARE_EVENT_CLASS
  100. #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
  101. struct ftrace_data_offsets_##call { \
  102. tstruct; \
  103. };
  104. #undef DEFINE_EVENT
  105. #define DEFINE_EVENT(template, name, proto, args)
  106. #undef DEFINE_EVENT_PRINT
  107. #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
  108. DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
  109. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
  110. /*
  111. * Stage 3 of the trace events.
  112. *
  113. * Override the macros in <trace/trace_events.h> to include the following:
  114. *
  115. * enum print_line_t
  116. * ftrace_raw_output_<call>(struct trace_iterator *iter, int flags)
  117. * {
  118. * struct trace_seq *s = &iter->seq;
  119. * struct ftrace_raw_<call> *field; <-- defined in stage 1
  120. * struct trace_entry *entry;
  121. * struct trace_seq *p;
  122. * int ret;
  123. *
  124. * entry = iter->ent;
  125. *
  126. * if (entry->type != event_<call>.id) {
  127. * WARN_ON_ONCE(1);
  128. * return TRACE_TYPE_UNHANDLED;
  129. * }
  130. *
  131. * field = (typeof(field))entry;
  132. *
  133. * p = &get_cpu_var(ftrace_event_seq);
  134. * trace_seq_init(p);
  135. * ret = trace_seq_printf(s, "%s: ", <call>);
  136. * if (ret)
  137. * ret = trace_seq_printf(s, <TP_printk> "\n");
  138. * put_cpu();
  139. * if (!ret)
  140. * return TRACE_TYPE_PARTIAL_LINE;
  141. *
  142. * return TRACE_TYPE_HANDLED;
  143. * }
  144. *
  145. * This is the method used to print the raw event to the trace
  146. * output format. Note, this is not needed if the data is read
  147. * in binary.
  148. */
  149. #undef __entry
  150. #define __entry field
  151. #undef TP_printk
  152. #define TP_printk(fmt, args...) fmt "\n", args
  153. #undef __get_dynamic_array
  154. #define __get_dynamic_array(field) \
  155. ((void *)__entry + (__entry->__data_loc_##field & 0xffff))
  156. #undef __get_str
  157. #define __get_str(field) (char *)__get_dynamic_array(field)
  158. #undef __print_flags
  159. #define __print_flags(flag, delim, flag_array...) \
  160. ({ \
  161. static const struct trace_print_flags __flags[] = \
  162. { flag_array, { -1, NULL }}; \
  163. ftrace_print_flags_seq(p, delim, flag, __flags); \
  164. })
  165. #undef __print_symbolic
  166. #define __print_symbolic(value, symbol_array...) \
  167. ({ \
  168. static const struct trace_print_flags symbols[] = \
  169. { symbol_array, { -1, NULL }}; \
  170. ftrace_print_symbols_seq(p, value, symbols); \
  171. })
  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(void *, 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, NULL); \
  354. } \
  355. \
  356. static notrace void \
  357. perf_trace_disable_##name(struct ftrace_event_call *unused) \
  358. { \
  359. unregister_trace_##name(perf_trace_##name, NULL); \
  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. * For those macros defined with TRACE_EVENT:
  372. *
  373. * static struct ftrace_event_call event_<call>;
  374. *
  375. * static void ftrace_raw_event_<call>(proto)
  376. * {
  377. * struct ftrace_data_offsets_<call> __maybe_unused __data_offsets;
  378. * struct ring_buffer_event *event;
  379. * struct ftrace_raw_<call> *entry; <-- defined in stage 1
  380. * struct ring_buffer *buffer;
  381. * unsigned long irq_flags;
  382. * int __data_size;
  383. * int pc;
  384. *
  385. * local_save_flags(irq_flags);
  386. * pc = preempt_count();
  387. *
  388. * __data_size = ftrace_get_offsets_<call>(&__data_offsets, args);
  389. *
  390. * event = trace_current_buffer_lock_reserve(&buffer,
  391. * event_<call>.id,
  392. * sizeof(*entry) + __data_size,
  393. * irq_flags, pc);
  394. * if (!event)
  395. * return;
  396. * entry = ring_buffer_event_data(event);
  397. *
  398. * { <assign>; } <-- Here we assign the entries by the __field and
  399. * __array macros.
  400. *
  401. * if (!filter_current_check_discard(buffer, event_call, entry, event))
  402. * trace_current_buffer_unlock_commit(buffer,
  403. * event, irq_flags, pc);
  404. * }
  405. *
  406. * static int ftrace_raw_reg_event_<call>(struct ftrace_event_call *unused)
  407. * {
  408. * return register_trace_<call>(ftrace_raw_event_<call>);
  409. * }
  410. *
  411. * static void ftrace_unreg_event_<call>(struct ftrace_event_call *unused)
  412. * {
  413. * unregister_trace_<call>(ftrace_raw_event_<call>);
  414. * }
  415. *
  416. * static struct trace_event ftrace_event_type_<call> = {
  417. * .trace = ftrace_raw_output_<call>, <-- stage 2
  418. * };
  419. *
  420. * static const char print_fmt_<call>[] = <TP_printk>;
  421. *
  422. * static struct ftrace_event_class __used event_class_<template> = {
  423. * .system = "<system>",
  424. * };
  425. *
  426. * static struct ftrace_event_call __used
  427. * __attribute__((__aligned__(4)))
  428. * __attribute__((section("_ftrace_events"))) event_<call> = {
  429. * .name = "<call>",
  430. * .class = event_class_<template>,
  431. * .raw_init = trace_event_raw_init,
  432. * .regfunc = ftrace_raw_reg_event_<call>,
  433. * .unregfunc = ftrace_raw_unreg_event_<call>,
  434. * .print_fmt = print_fmt_<call>,
  435. * .define_fields = ftrace_define_fields_<call>,
  436. * };
  437. *
  438. */
  439. #ifdef CONFIG_PERF_EVENTS
  440. #define _TRACE_PERF_INIT(call) \
  441. .perf_event_enable = perf_trace_enable_##call, \
  442. .perf_event_disable = perf_trace_disable_##call,
  443. #else
  444. #define _TRACE_PERF_INIT(call)
  445. #endif /* CONFIG_PERF_EVENTS */
  446. #undef __entry
  447. #define __entry entry
  448. #undef __field
  449. #define __field(type, item)
  450. #undef __array
  451. #define __array(type, item, len)
  452. #undef __dynamic_array
  453. #define __dynamic_array(type, item, len) \
  454. __entry->__data_loc_##item = __data_offsets.item;
  455. #undef __string
  456. #define __string(item, src) __dynamic_array(char, item, -1) \
  457. #undef __assign_str
  458. #define __assign_str(dst, src) \
  459. strcpy(__get_str(dst), src);
  460. #undef TP_fast_assign
  461. #define TP_fast_assign(args...) args
  462. #undef TP_perf_assign
  463. #define TP_perf_assign(args...)
  464. #undef DECLARE_EVENT_CLASS
  465. #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
  466. \
  467. static notrace void \
  468. ftrace_raw_event_id_##call(struct ftrace_event_call *event_call, \
  469. proto) \
  470. { \
  471. struct ftrace_data_offsets_##call __maybe_unused __data_offsets;\
  472. struct ring_buffer_event *event; \
  473. struct ftrace_raw_##call *entry; \
  474. struct ring_buffer *buffer; \
  475. unsigned long irq_flags; \
  476. int __data_size; \
  477. int pc; \
  478. \
  479. local_save_flags(irq_flags); \
  480. pc = preempt_count(); \
  481. \
  482. __data_size = ftrace_get_offsets_##call(&__data_offsets, args); \
  483. \
  484. event = trace_current_buffer_lock_reserve(&buffer, \
  485. event_call->id, \
  486. sizeof(*entry) + __data_size, \
  487. irq_flags, pc); \
  488. if (!event) \
  489. return; \
  490. entry = ring_buffer_event_data(event); \
  491. \
  492. tstruct \
  493. \
  494. { assign; } \
  495. \
  496. if (!filter_current_check_discard(buffer, event_call, entry, event)) \
  497. trace_nowake_buffer_unlock_commit(buffer, \
  498. event, irq_flags, pc); \
  499. }
  500. #undef DEFINE_EVENT
  501. #define DEFINE_EVENT(template, call, proto, args) \
  502. \
  503. static notrace void ftrace_raw_event_##call(void *__ignore, proto) \
  504. { \
  505. ftrace_raw_event_id_##template(&event_##call, args); \
  506. } \
  507. \
  508. static notrace int \
  509. ftrace_raw_reg_event_##call(struct ftrace_event_call *unused) \
  510. { \
  511. return register_trace_##call(ftrace_raw_event_##call, NULL); \
  512. } \
  513. \
  514. static notrace void \
  515. ftrace_raw_unreg_event_##call(struct ftrace_event_call *unused) \
  516. { \
  517. unregister_trace_##call(ftrace_raw_event_##call, NULL); \
  518. } \
  519. \
  520. static struct trace_event ftrace_event_type_##call = { \
  521. .trace = ftrace_raw_output_##call, \
  522. };
  523. #undef DEFINE_EVENT_PRINT
  524. #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
  525. DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
  526. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
  527. #undef __entry
  528. #define __entry REC
  529. #undef __print_flags
  530. #undef __print_symbolic
  531. #undef __get_dynamic_array
  532. #undef __get_str
  533. #undef TP_printk
  534. #define TP_printk(fmt, args...) "\"" fmt "\", " __stringify(args)
  535. #undef DECLARE_EVENT_CLASS
  536. #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
  537. static const char print_fmt_##call[] = print; \
  538. static struct ftrace_event_class __used event_class_##call = { \
  539. .system = __stringify(TRACE_SYSTEM) \
  540. };
  541. #undef DEFINE_EVENT
  542. #define DEFINE_EVENT(template, call, proto, args) \
  543. \
  544. static struct ftrace_event_call __used \
  545. __attribute__((__aligned__(4))) \
  546. __attribute__((section("_ftrace_events"))) event_##call = { \
  547. .name = #call, \
  548. .class = &event_class_##template, \
  549. .event = &ftrace_event_type_##call, \
  550. .raw_init = trace_event_raw_init, \
  551. .regfunc = ftrace_raw_reg_event_##call, \
  552. .unregfunc = ftrace_raw_unreg_event_##call, \
  553. .print_fmt = print_fmt_##template, \
  554. .define_fields = ftrace_define_fields_##template, \
  555. _TRACE_PERF_INIT(call) \
  556. };
  557. #undef DEFINE_EVENT_PRINT
  558. #define DEFINE_EVENT_PRINT(template, call, proto, args, print) \
  559. \
  560. static const char print_fmt_##call[] = print; \
  561. \
  562. static struct ftrace_event_call __used \
  563. __attribute__((__aligned__(4))) \
  564. __attribute__((section("_ftrace_events"))) event_##call = { \
  565. .name = #call, \
  566. .class = &event_class_##template, \
  567. .event = &ftrace_event_type_##call, \
  568. .raw_init = trace_event_raw_init, \
  569. .regfunc = ftrace_raw_reg_event_##call, \
  570. .unregfunc = ftrace_raw_unreg_event_##call, \
  571. .print_fmt = print_fmt_##call, \
  572. .define_fields = ftrace_define_fields_##template, \
  573. _TRACE_PERF_INIT(call) \
  574. }
  575. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
  576. /*
  577. * Define the insertion callback to perf events
  578. *
  579. * The job is very similar to ftrace_raw_event_<call> except that we don't
  580. * insert in the ring buffer but in a perf counter.
  581. *
  582. * static void ftrace_perf_<call>(proto)
  583. * {
  584. * struct ftrace_data_offsets_<call> __maybe_unused __data_offsets;
  585. * struct ftrace_event_call *event_call = &event_<call>;
  586. * extern void perf_tp_event(int, u64, u64, void *, int);
  587. * struct ftrace_raw_##call *entry;
  588. * struct perf_trace_buf *trace_buf;
  589. * u64 __addr = 0, __count = 1;
  590. * unsigned long irq_flags;
  591. * struct trace_entry *ent;
  592. * int __entry_size;
  593. * int __data_size;
  594. * int __cpu
  595. * int pc;
  596. *
  597. * pc = preempt_count();
  598. *
  599. * __data_size = ftrace_get_offsets_<call>(&__data_offsets, args);
  600. *
  601. * // Below we want to get the aligned size by taking into account
  602. * // the u32 field that will later store the buffer size
  603. * __entry_size = ALIGN(__data_size + sizeof(*entry) + sizeof(u32),
  604. * sizeof(u64));
  605. * __entry_size -= sizeof(u32);
  606. *
  607. * // Protect the non nmi buffer
  608. * // This also protects the rcu read side
  609. * local_irq_save(irq_flags);
  610. * __cpu = smp_processor_id();
  611. *
  612. * if (in_nmi())
  613. * trace_buf = rcu_dereference_sched(perf_trace_buf_nmi);
  614. * else
  615. * trace_buf = rcu_dereference_sched(perf_trace_buf);
  616. *
  617. * if (!trace_buf)
  618. * goto end;
  619. *
  620. * trace_buf = per_cpu_ptr(trace_buf, __cpu);
  621. *
  622. * // Avoid recursion from perf that could mess up the buffer
  623. * if (trace_buf->recursion++)
  624. * goto end_recursion;
  625. *
  626. * raw_data = trace_buf->buf;
  627. *
  628. * // Make recursion update visible before entering perf_tp_event
  629. * // so that we protect from perf recursions.
  630. *
  631. * barrier();
  632. *
  633. * //zero dead bytes from alignment to avoid stack leak to userspace:
  634. * *(u64 *)(&raw_data[__entry_size - sizeof(u64)]) = 0ULL;
  635. * entry = (struct ftrace_raw_<call> *)raw_data;
  636. * ent = &entry->ent;
  637. * tracing_generic_entry_update(ent, irq_flags, pc);
  638. * ent->type = event_call->id;
  639. *
  640. * <tstruct> <- do some jobs with dynamic arrays
  641. *
  642. * <assign> <- affect our values
  643. *
  644. * perf_tp_event(event_call->id, __addr, __count, entry,
  645. * __entry_size); <- submit them to perf counter
  646. *
  647. * }
  648. */
  649. #ifdef CONFIG_PERF_EVENTS
  650. #undef __entry
  651. #define __entry entry
  652. #undef __get_dynamic_array
  653. #define __get_dynamic_array(field) \
  654. ((void *)__entry + (__entry->__data_loc_##field & 0xffff))
  655. #undef __get_str
  656. #define __get_str(field) (char *)__get_dynamic_array(field)
  657. #undef __perf_addr
  658. #define __perf_addr(a) __addr = (a)
  659. #undef __perf_count
  660. #define __perf_count(c) __count = (c)
  661. #undef DECLARE_EVENT_CLASS
  662. #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
  663. static notrace void \
  664. perf_trace_templ_##call(struct ftrace_event_call *event_call, \
  665. proto) \
  666. { \
  667. struct ftrace_data_offsets_##call __maybe_unused __data_offsets;\
  668. struct ftrace_raw_##call *entry; \
  669. u64 __addr = 0, __count = 1; \
  670. unsigned long irq_flags; \
  671. struct pt_regs *__regs; \
  672. int __entry_size; \
  673. int __data_size; \
  674. int rctx; \
  675. \
  676. __data_size = ftrace_get_offsets_##call(&__data_offsets, args); \
  677. __entry_size = ALIGN(__data_size + sizeof(*entry) + sizeof(u32),\
  678. sizeof(u64)); \
  679. __entry_size -= sizeof(u32); \
  680. \
  681. if (WARN_ONCE(__entry_size > PERF_MAX_TRACE_SIZE, \
  682. "profile buffer not large enough")) \
  683. return; \
  684. entry = (struct ftrace_raw_##call *)perf_trace_buf_prepare( \
  685. __entry_size, event_call->id, &rctx, &irq_flags); \
  686. if (!entry) \
  687. return; \
  688. tstruct \
  689. \
  690. { assign; } \
  691. \
  692. __regs = &__get_cpu_var(perf_trace_regs); \
  693. perf_fetch_caller_regs(__regs, 2); \
  694. \
  695. perf_trace_buf_submit(entry, __entry_size, rctx, __addr, \
  696. __count, irq_flags, __regs); \
  697. }
  698. #undef DEFINE_EVENT
  699. #define DEFINE_EVENT(template, call, proto, args) \
  700. static notrace void perf_trace_##call(void *__ignore, proto) \
  701. { \
  702. struct ftrace_event_call *event_call = &event_##call; \
  703. \
  704. perf_trace_templ_##template(event_call, args); \
  705. }
  706. #undef DEFINE_EVENT_PRINT
  707. #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
  708. DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
  709. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
  710. #endif /* CONFIG_PERF_EVENTS */
  711. #undef _TRACE_PROFILE_INIT