ftrace.h 21 KB

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