ftrace.h 21 KB

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