PageRenderTime 54ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/include/trace/ftrace.h

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