ftrace.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  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 __used \
  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. /* 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, PARAMS(proto), PARAMS(args), \
  70. PARAMS(tstruct), PARAMS(assign), PARAMS(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 = &iter->tmp_seq;
  120. * int ret;
  121. *
  122. * entry = iter->ent;
  123. *
  124. * if (entry->type != event_<call>->event.type) {
  125. * WARN_ON_ONCE(1);
  126. * return TRACE_TYPE_UNHANDLED;
  127. * }
  128. *
  129. * field = (typeof(field))entry;
  130. *
  131. * trace_seq_init(p);
  132. * ret = trace_seq_printf(s, "%s: ", <call>);
  133. * if (ret)
  134. * ret = trace_seq_printf(s, <TP_printk> "\n");
  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 __print_hex
  169. #define __print_hex(buf, buf_len) ftrace_print_hex_seq(p, buf, buf_len)
  170. #undef DECLARE_EVENT_CLASS
  171. #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
  172. static notrace enum print_line_t \
  173. ftrace_raw_output_##call(struct trace_iterator *iter, int flags, \
  174. struct trace_event *trace_event) \
  175. { \
  176. struct ftrace_event_call *event; \
  177. struct trace_seq *s = &iter->seq; \
  178. struct ftrace_raw_##call *field; \
  179. struct trace_entry *entry; \
  180. struct trace_seq *p = &iter->tmp_seq; \
  181. int ret; \
  182. \
  183. event = container_of(trace_event, struct ftrace_event_call, \
  184. event); \
  185. \
  186. entry = iter->ent; \
  187. \
  188. if (entry->type != event->event.type) { \
  189. WARN_ON_ONCE(1); \
  190. return TRACE_TYPE_UNHANDLED; \
  191. } \
  192. \
  193. field = (typeof(field))entry; \
  194. \
  195. trace_seq_init(p); \
  196. ret = trace_seq_printf(s, "%s: ", event->name); \
  197. if (ret) \
  198. ret = trace_seq_printf(s, print); \
  199. if (!ret) \
  200. return TRACE_TYPE_PARTIAL_LINE; \
  201. \
  202. return TRACE_TYPE_HANDLED; \
  203. } \
  204. static struct trace_event_functions ftrace_event_type_funcs_##call = { \
  205. .trace = ftrace_raw_output_##call, \
  206. };
  207. #undef DEFINE_EVENT_PRINT
  208. #define DEFINE_EVENT_PRINT(template, call, proto, args, print) \
  209. static notrace enum print_line_t \
  210. ftrace_raw_output_##call(struct trace_iterator *iter, int flags, \
  211. struct trace_event *event) \
  212. { \
  213. struct trace_seq *s = &iter->seq; \
  214. struct ftrace_raw_##template *field; \
  215. struct trace_entry *entry; \
  216. struct trace_seq *p = &iter->tmp_seq; \
  217. int ret; \
  218. \
  219. entry = iter->ent; \
  220. \
  221. if (entry->type != event_##call.event.type) { \
  222. WARN_ON_ONCE(1); \
  223. return TRACE_TYPE_UNHANDLED; \
  224. } \
  225. \
  226. field = (typeof(field))entry; \
  227. \
  228. trace_seq_init(p); \
  229. ret = trace_seq_printf(s, "%s: ", #call); \
  230. if (ret) \
  231. ret = trace_seq_printf(s, print); \
  232. if (!ret) \
  233. return TRACE_TYPE_PARTIAL_LINE; \
  234. \
  235. return TRACE_TYPE_HANDLED; \
  236. } \
  237. static struct trace_event_functions ftrace_event_type_funcs_##call = { \
  238. .trace = ftrace_raw_output_##call, \
  239. };
  240. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
  241. #undef __field_ext
  242. #define __field_ext(type, item, filter_type) \
  243. ret = trace_define_field(event_call, #type, #item, \
  244. offsetof(typeof(field), item), \
  245. sizeof(field.item), \
  246. is_signed_type(type), filter_type); \
  247. if (ret) \
  248. return ret;
  249. #undef __field
  250. #define __field(type, item) __field_ext(type, item, FILTER_OTHER)
  251. #undef __array
  252. #define __array(type, item, len) \
  253. BUILD_BUG_ON(len > MAX_FILTER_STR_VAL); \
  254. ret = trace_define_field(event_call, #type "[" #len "]", #item, \
  255. offsetof(typeof(field), item), \
  256. sizeof(field.item), \
  257. is_signed_type(type), FILTER_OTHER); \
  258. if (ret) \
  259. return ret;
  260. #undef __dynamic_array
  261. #define __dynamic_array(type, item, len) \
  262. ret = trace_define_field(event_call, "__data_loc " #type "[]", #item, \
  263. offsetof(typeof(field), __data_loc_##item), \
  264. sizeof(field.__data_loc_##item), \
  265. is_signed_type(type), FILTER_OTHER);
  266. #undef __string
  267. #define __string(item, src) __dynamic_array(char, item, -1)
  268. #undef DECLARE_EVENT_CLASS
  269. #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, func, print) \
  270. static int notrace \
  271. ftrace_define_fields_##call(struct ftrace_event_call *event_call) \
  272. { \
  273. struct ftrace_raw_##call field; \
  274. int ret; \
  275. \
  276. tstruct; \
  277. \
  278. return ret; \
  279. }
  280. #undef DEFINE_EVENT
  281. #define DEFINE_EVENT(template, name, proto, args)
  282. #undef DEFINE_EVENT_PRINT
  283. #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
  284. DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
  285. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
  286. /*
  287. * remember the offset of each array from the beginning of the event.
  288. */
  289. #undef __entry
  290. #define __entry entry
  291. #undef __field
  292. #define __field(type, item)
  293. #undef __field_ext
  294. #define __field_ext(type, item, filter_type)
  295. #undef __array
  296. #define __array(type, item, len)
  297. #undef __dynamic_array
  298. #define __dynamic_array(type, item, len) \
  299. __data_offsets->item = __data_size + \
  300. offsetof(typeof(*entry), __data); \
  301. __data_offsets->item |= (len * sizeof(type)) << 16; \
  302. __data_size += (len) * sizeof(type);
  303. #undef __string
  304. #define __string(item, src) __dynamic_array(char, item, strlen(src) + 1)
  305. #undef DECLARE_EVENT_CLASS
  306. #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
  307. static inline notrace int ftrace_get_offsets_##call( \
  308. struct ftrace_data_offsets_##call *__data_offsets, proto) \
  309. { \
  310. int __data_size = 0; \
  311. struct ftrace_raw_##call __maybe_unused *entry; \
  312. \
  313. tstruct; \
  314. \
  315. return __data_size; \
  316. }
  317. #undef DEFINE_EVENT
  318. #define DEFINE_EVENT(template, name, proto, args)
  319. #undef DEFINE_EVENT_PRINT
  320. #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
  321. DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
  322. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
  323. /*
  324. * Stage 4 of the trace events.
  325. *
  326. * Override the macros in <trace/trace_events.h> to include the following:
  327. *
  328. * For those macros defined with TRACE_EVENT:
  329. *
  330. * static struct ftrace_event_call event_<call>;
  331. *
  332. * static void ftrace_raw_event_<call>(void *__data, proto)
  333. * {
  334. * struct ftrace_event_call *event_call = __data;
  335. * struct ftrace_data_offsets_<call> __maybe_unused __data_offsets;
  336. * struct ring_buffer_event *event;
  337. * struct ftrace_raw_<call> *entry; <-- defined in stage 1
  338. * struct ring_buffer *buffer;
  339. * unsigned long irq_flags;
  340. * int __data_size;
  341. * int pc;
  342. *
  343. * local_save_flags(irq_flags);
  344. * pc = preempt_count();
  345. *
  346. * __data_size = ftrace_get_offsets_<call>(&__data_offsets, args);
  347. *
  348. * event = trace_current_buffer_lock_reserve(&buffer,
  349. * event_<call>->event.type,
  350. * sizeof(*entry) + __data_size,
  351. * irq_flags, pc);
  352. * if (!event)
  353. * return;
  354. * entry = ring_buffer_event_data(event);
  355. *
  356. * { <assign>; } <-- Here we assign the entries by the __field and
  357. * __array macros.
  358. *
  359. * if (!filter_current_check_discard(buffer, event_call, entry, event))
  360. * trace_current_buffer_unlock_commit(buffer,
  361. * event, irq_flags, pc);
  362. * }
  363. *
  364. * static struct trace_event ftrace_event_type_<call> = {
  365. * .trace = ftrace_raw_output_<call>, <-- stage 2
  366. * };
  367. *
  368. * static const char print_fmt_<call>[] = <TP_printk>;
  369. *
  370. * static struct ftrace_event_class __used event_class_<template> = {
  371. * .system = "<system>",
  372. * .define_fields = ftrace_define_fields_<call>,
  373. * .fields = LIST_HEAD_INIT(event_class_##call.fields),
  374. * .raw_init = trace_event_raw_init,
  375. * .probe = ftrace_raw_event_##call,
  376. * .reg = ftrace_event_reg,
  377. * };
  378. *
  379. * static struct ftrace_event_call __used
  380. * __attribute__((__aligned__(4)))
  381. * __attribute__((section("_ftrace_events"))) event_<call> = {
  382. * .name = "<call>",
  383. * .class = event_class_<template>,
  384. * .event = &ftrace_event_type_<call>,
  385. * .print_fmt = print_fmt_<call>,
  386. * };
  387. *
  388. */
  389. #ifdef CONFIG_PERF_EVENTS
  390. #define _TRACE_PERF_PROTO(call, proto) \
  391. static notrace void \
  392. perf_trace_##call(void *__data, proto);
  393. #define _TRACE_PERF_INIT(call) \
  394. .perf_probe = perf_trace_##call,
  395. #else
  396. #define _TRACE_PERF_PROTO(call, proto)
  397. #define _TRACE_PERF_INIT(call)
  398. #endif /* CONFIG_PERF_EVENTS */
  399. #undef __entry
  400. #define __entry entry
  401. #undef __field
  402. #define __field(type, item)
  403. #undef __array
  404. #define __array(type, item, len)
  405. #undef __dynamic_array
  406. #define __dynamic_array(type, item, len) \
  407. __entry->__data_loc_##item = __data_offsets.item;
  408. #undef __string
  409. #define __string(item, src) __dynamic_array(char, item, -1) \
  410. #undef __assign_str
  411. #define __assign_str(dst, src) \
  412. strcpy(__get_str(dst), src);
  413. #undef TP_fast_assign
  414. #define TP_fast_assign(args...) args
  415. #undef TP_perf_assign
  416. #define TP_perf_assign(args...)
  417. #undef DECLARE_EVENT_CLASS
  418. #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
  419. \
  420. static notrace void \
  421. ftrace_raw_event_##call(void *__data, proto) \
  422. { \
  423. struct ftrace_event_call *event_call = __data; \
  424. struct ftrace_data_offsets_##call __maybe_unused __data_offsets;\
  425. struct ring_buffer_event *event; \
  426. struct ftrace_raw_##call *entry; \
  427. struct ring_buffer *buffer; \
  428. unsigned long irq_flags; \
  429. int __data_size; \
  430. int pc; \
  431. \
  432. local_save_flags(irq_flags); \
  433. pc = preempt_count(); \
  434. \
  435. __data_size = ftrace_get_offsets_##call(&__data_offsets, args); \
  436. \
  437. event = trace_current_buffer_lock_reserve(&buffer, \
  438. event_call->event.type, \
  439. sizeof(*entry) + __data_size, \
  440. irq_flags, pc); \
  441. if (!event) \
  442. return; \
  443. entry = ring_buffer_event_data(event); \
  444. \
  445. tstruct \
  446. \
  447. { assign; } \
  448. \
  449. if (!filter_current_check_discard(buffer, event_call, entry, event)) \
  450. trace_nowake_buffer_unlock_commit(buffer, \
  451. event, irq_flags, pc); \
  452. }
  453. /*
  454. * The ftrace_test_probe is compiled out, it is only here as a build time check
  455. * to make sure that if the tracepoint handling changes, the ftrace probe will
  456. * fail to compile unless it too is updated.
  457. */
  458. #undef DEFINE_EVENT
  459. #define DEFINE_EVENT(template, call, proto, args) \
  460. static inline void ftrace_test_probe_##call(void) \
  461. { \
  462. check_trace_callback_type_##call(ftrace_raw_event_##template); \
  463. }
  464. #undef DEFINE_EVENT_PRINT
  465. #define DEFINE_EVENT_PRINT(template, name, proto, args, print)
  466. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
  467. #undef __entry
  468. #define __entry REC
  469. #undef __print_flags
  470. #undef __print_symbolic
  471. #undef __get_dynamic_array
  472. #undef __get_str
  473. #undef TP_printk
  474. #define TP_printk(fmt, args...) "\"" fmt "\", " __stringify(args)
  475. #undef DECLARE_EVENT_CLASS
  476. #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
  477. _TRACE_PERF_PROTO(call, PARAMS(proto)); \
  478. static const char print_fmt_##call[] = print; \
  479. static struct ftrace_event_class __used event_class_##call = { \
  480. .system = __stringify(TRACE_SYSTEM), \
  481. .define_fields = ftrace_define_fields_##call, \
  482. .fields = LIST_HEAD_INIT(event_class_##call.fields),\
  483. .raw_init = trace_event_raw_init, \
  484. .probe = ftrace_raw_event_##call, \
  485. .reg = ftrace_event_reg, \
  486. _TRACE_PERF_INIT(call) \
  487. };
  488. #undef DEFINE_EVENT
  489. #define DEFINE_EVENT(template, call, proto, args) \
  490. \
  491. static struct ftrace_event_call __used \
  492. __attribute__((__aligned__(4))) \
  493. __attribute__((section("_ftrace_events"))) event_##call = { \
  494. .name = #call, \
  495. .class = &event_class_##template, \
  496. .event.funcs = &ftrace_event_type_funcs_##template, \
  497. .print_fmt = print_fmt_##template, \
  498. };
  499. #undef DEFINE_EVENT_PRINT
  500. #define DEFINE_EVENT_PRINT(template, call, proto, args, print) \
  501. \
  502. static const char print_fmt_##call[] = print; \
  503. \
  504. static struct ftrace_event_call __used \
  505. __attribute__((__aligned__(4))) \
  506. __attribute__((section("_ftrace_events"))) event_##call = { \
  507. .name = #call, \
  508. .class = &event_class_##template, \
  509. .event.funcs = &ftrace_event_type_funcs_##call, \
  510. .print_fmt = print_fmt_##call, \
  511. }
  512. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
  513. /*
  514. * Define the insertion callback to perf events
  515. *
  516. * The job is very similar to ftrace_raw_event_<call> except that we don't
  517. * insert in the ring buffer but in a perf counter.
  518. *
  519. * static void ftrace_perf_<call>(proto)
  520. * {
  521. * struct ftrace_data_offsets_<call> __maybe_unused __data_offsets;
  522. * struct ftrace_event_call *event_call = &event_<call>;
  523. * extern void perf_tp_event(int, u64, u64, void *, int);
  524. * struct ftrace_raw_##call *entry;
  525. * struct perf_trace_buf *trace_buf;
  526. * u64 __addr = 0, __count = 1;
  527. * unsigned long irq_flags;
  528. * struct trace_entry *ent;
  529. * int __entry_size;
  530. * int __data_size;
  531. * int __cpu
  532. * int pc;
  533. *
  534. * pc = preempt_count();
  535. *
  536. * __data_size = ftrace_get_offsets_<call>(&__data_offsets, args);
  537. *
  538. * // Below we want to get the aligned size by taking into account
  539. * // the u32 field that will later store the buffer size
  540. * __entry_size = ALIGN(__data_size + sizeof(*entry) + sizeof(u32),
  541. * sizeof(u64));
  542. * __entry_size -= sizeof(u32);
  543. *
  544. * // Protect the non nmi buffer
  545. * // This also protects the rcu read side
  546. * local_irq_save(irq_flags);
  547. * __cpu = smp_processor_id();
  548. *
  549. * if (in_nmi())
  550. * trace_buf = rcu_dereference_sched(perf_trace_buf_nmi);
  551. * else
  552. * trace_buf = rcu_dereference_sched(perf_trace_buf);
  553. *
  554. * if (!trace_buf)
  555. * goto end;
  556. *
  557. * trace_buf = per_cpu_ptr(trace_buf, __cpu);
  558. *
  559. * // Avoid recursion from perf that could mess up the buffer
  560. * if (trace_buf->recursion++)
  561. * goto end_recursion;
  562. *
  563. * raw_data = trace_buf->buf;
  564. *
  565. * // Make recursion update visible before entering perf_tp_event
  566. * // so that we protect from perf recursions.
  567. *
  568. * barrier();
  569. *
  570. * //zero dead bytes from alignment to avoid stack leak to userspace:
  571. * *(u64 *)(&raw_data[__entry_size - sizeof(u64)]) = 0ULL;
  572. * entry = (struct ftrace_raw_<call> *)raw_data;
  573. * ent = &entry->ent;
  574. * tracing_generic_entry_update(ent, irq_flags, pc);
  575. * ent->type = event_call->id;
  576. *
  577. * <tstruct> <- do some jobs with dynamic arrays
  578. *
  579. * <assign> <- affect our values
  580. *
  581. * perf_tp_event(event_call->id, __addr, __count, entry,
  582. * __entry_size); <- submit them to perf counter
  583. *
  584. * }
  585. */
  586. #ifdef CONFIG_PERF_EVENTS
  587. #undef __entry
  588. #define __entry entry
  589. #undef __get_dynamic_array
  590. #define __get_dynamic_array(field) \
  591. ((void *)__entry + (__entry->__data_loc_##field & 0xffff))
  592. #undef __get_str
  593. #define __get_str(field) (char *)__get_dynamic_array(field)
  594. #undef __perf_addr
  595. #define __perf_addr(a) __addr = (a)
  596. #undef __perf_count
  597. #define __perf_count(c) __count = (c)
  598. #undef DECLARE_EVENT_CLASS
  599. #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
  600. static notrace void \
  601. perf_trace_##call(void *__data, proto) \
  602. { \
  603. struct ftrace_event_call *event_call = __data; \
  604. struct ftrace_data_offsets_##call __maybe_unused __data_offsets;\
  605. struct ftrace_raw_##call *entry; \
  606. struct pt_regs __regs; \
  607. u64 __addr = 0, __count = 1; \
  608. struct hlist_head *head; \
  609. int __entry_size; \
  610. int __data_size; \
  611. int rctx; \
  612. \
  613. perf_fetch_caller_regs(&__regs); \
  614. \
  615. __data_size = ftrace_get_offsets_##call(&__data_offsets, args); \
  616. __entry_size = ALIGN(__data_size + sizeof(*entry) + sizeof(u32),\
  617. sizeof(u64)); \
  618. __entry_size -= sizeof(u32); \
  619. \
  620. if (WARN_ONCE(__entry_size > PERF_MAX_TRACE_SIZE, \
  621. "profile buffer not large enough")) \
  622. return; \
  623. \
  624. entry = (struct ftrace_raw_##call *)perf_trace_buf_prepare( \
  625. __entry_size, event_call->event.type, &__regs, &rctx); \
  626. if (!entry) \
  627. return; \
  628. \
  629. tstruct \
  630. \
  631. { assign; } \
  632. \
  633. head = this_cpu_ptr(event_call->perf_events); \
  634. perf_trace_buf_submit(entry, __entry_size, rctx, __addr, \
  635. __count, &__regs, head); \
  636. }
  637. /*
  638. * This part is compiled out, it is only here as a build time check
  639. * to make sure that if the tracepoint handling changes, the
  640. * perf probe will fail to compile unless it too is updated.
  641. */
  642. #undef DEFINE_EVENT
  643. #define DEFINE_EVENT(template, call, proto, args) \
  644. static inline void perf_test_probe_##call(void) \
  645. { \
  646. check_trace_callback_type_##call(perf_trace_##template); \
  647. }
  648. #undef DEFINE_EVENT_PRINT
  649. #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
  650. DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
  651. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
  652. #endif /* CONFIG_PERF_EVENTS */
  653. #undef _TRACE_PROFILE_INIT