ftrace.h 22 KB

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