ftrace.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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. * TRACE_EVENT_TEMPLATE 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 TRACE_EVENT_TEMPLATE 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. TRACE_EVENT_TEMPLATE(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 TRACE_EVENT_TEMPLATE
  50. #define TRACE_EVENT_TEMPLATE(name, proto, args, tstruct, assign, print) \
  51. struct ftrace_raw_##name { \
  52. struct trace_entry ent; \
  53. tstruct \
  54. char __data[0]; \
  55. };
  56. #undef DEFINE_EVENT
  57. #define DEFINE_EVENT(template, name, proto, args) \
  58. static struct ftrace_event_call event_##name
  59. #undef __cpparg
  60. #define __cpparg(arg...) arg
  61. /* Callbacks are meaningless to ftrace. */
  62. #undef TRACE_EVENT_FN
  63. #define TRACE_EVENT_FN(name, proto, args, tstruct, \
  64. assign, print, reg, unreg) \
  65. TRACE_EVENT(name, __cpparg(proto), __cpparg(args), \
  66. __cpparg(tstruct), __cpparg(assign), __cpparg(print)) \
  67. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
  68. /*
  69. * Stage 2 of the trace events.
  70. *
  71. * Include the following:
  72. *
  73. * struct ftrace_data_offsets_<call> {
  74. * u32 <item1>;
  75. * u32 <item2>;
  76. * [...]
  77. * };
  78. *
  79. * The __dynamic_array() macro will create each u32 <item>, this is
  80. * to keep the offset of each array from the beginning of the event.
  81. * The size of an array is also encoded, in the higher 16 bits of <item>.
  82. */
  83. #undef __field
  84. #define __field(type, item)
  85. #undef __field_ext
  86. #define __field_ext(type, item, filter_type)
  87. #undef __array
  88. #define __array(type, item, len)
  89. #undef __dynamic_array
  90. #define __dynamic_array(type, item, len) u32 item;
  91. #undef __string
  92. #define __string(item, src) __dynamic_array(char, item, -1)
  93. #undef TRACE_EVENT_TEMPLATE
  94. #define TRACE_EVENT_TEMPLATE(call, proto, args, tstruct, assign, print) \
  95. struct ftrace_data_offsets_##call { \
  96. tstruct; \
  97. };
  98. #undef DEFINE_EVENT
  99. #define DEFINE_EVENT(template, name, proto, args)
  100. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
  101. /*
  102. * Setup the showing format of trace point.
  103. *
  104. * int
  105. * ftrace_format_##call(struct trace_seq *s)
  106. * {
  107. * struct ftrace_raw_##call field;
  108. * int ret;
  109. *
  110. * ret = trace_seq_printf(s, #type " " #item ";"
  111. * " offset:%u; size:%u;\n",
  112. * offsetof(struct ftrace_raw_##call, item),
  113. * sizeof(field.type));
  114. *
  115. * }
  116. */
  117. #undef TP_STRUCT__entry
  118. #define TP_STRUCT__entry(args...) args
  119. #undef __field
  120. #define __field(type, item) \
  121. ret = trace_seq_printf(s, "\tfield:" #type " " #item ";\t" \
  122. "offset:%u;\tsize:%u;\tsigned:%u;\n", \
  123. (unsigned int)offsetof(typeof(field), item), \
  124. (unsigned int)sizeof(field.item), \
  125. (unsigned int)is_signed_type(type)); \
  126. if (!ret) \
  127. return 0;
  128. #undef __field_ext
  129. #define __field_ext(type, item, filter_type) __field(type, item)
  130. #undef __array
  131. #define __array(type, item, len) \
  132. ret = trace_seq_printf(s, "\tfield:" #type " " #item "[" #len "];\t" \
  133. "offset:%u;\tsize:%u;\tsigned:%u;\n", \
  134. (unsigned int)offsetof(typeof(field), item), \
  135. (unsigned int)sizeof(field.item), \
  136. (unsigned int)is_signed_type(type)); \
  137. if (!ret) \
  138. return 0;
  139. #undef __dynamic_array
  140. #define __dynamic_array(type, item, len) \
  141. ret = trace_seq_printf(s, "\tfield:__data_loc " #type "[] " #item ";\t"\
  142. "offset:%u;\tsize:%u;\tsigned:%u;\n", \
  143. (unsigned int)offsetof(typeof(field), \
  144. __data_loc_##item), \
  145. (unsigned int)sizeof(field.__data_loc_##item), \
  146. (unsigned int)is_signed_type(type)); \
  147. if (!ret) \
  148. return 0;
  149. #undef __string
  150. #define __string(item, src) __dynamic_array(char, item, -1)
  151. #undef __entry
  152. #define __entry REC
  153. #undef __print_symbolic
  154. #undef __get_dynamic_array
  155. #undef __get_str
  156. #undef TP_printk
  157. #define TP_printk(fmt, args...) "%s, %s\n", #fmt, __stringify(args)
  158. #undef TP_fast_assign
  159. #define TP_fast_assign(args...) args
  160. #undef TP_perf_assign
  161. #define TP_perf_assign(args...)
  162. #undef TRACE_EVENT_TEMPLATE
  163. #define TRACE_EVENT_TEMPLATE(call, proto, args, tstruct, func, print) \
  164. static int \
  165. ftrace_format_##call(struct ftrace_event_call *unused, \
  166. struct trace_seq *s) \
  167. { \
  168. struct ftrace_raw_##call field __attribute__((unused)); \
  169. int ret = 0; \
  170. \
  171. tstruct; \
  172. \
  173. trace_seq_printf(s, "\nprint fmt: " print); \
  174. \
  175. return ret; \
  176. }
  177. #undef DEFINE_EVENT
  178. #define DEFINE_EVENT(template, name, proto, args)
  179. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
  180. /*
  181. * Stage 3 of the trace events.
  182. *
  183. * Override the macros in <trace/trace_events.h> to include the following:
  184. *
  185. * enum print_line_t
  186. * ftrace_raw_output_<call>(struct trace_iterator *iter, int flags)
  187. * {
  188. * struct trace_seq *s = &iter->seq;
  189. * struct ftrace_raw_<call> *field; <-- defined in stage 1
  190. * struct trace_entry *entry;
  191. * struct trace_seq *p;
  192. * int ret;
  193. *
  194. * entry = iter->ent;
  195. *
  196. * if (entry->type != event_<call>.id) {
  197. * WARN_ON_ONCE(1);
  198. * return TRACE_TYPE_UNHANDLED;
  199. * }
  200. *
  201. * field = (typeof(field))entry;
  202. *
  203. * p = get_cpu_var(ftrace_event_seq);
  204. * trace_seq_init(p);
  205. * ret = trace_seq_printf(s, <TP_printk> "\n");
  206. * put_cpu();
  207. * if (!ret)
  208. * return TRACE_TYPE_PARTIAL_LINE;
  209. *
  210. * return TRACE_TYPE_HANDLED;
  211. * }
  212. *
  213. * This is the method used to print the raw event to the trace
  214. * output format. Note, this is not needed if the data is read
  215. * in binary.
  216. */
  217. #undef __entry
  218. #define __entry field
  219. #undef TP_printk
  220. #define TP_printk(fmt, args...) fmt "\n", args
  221. #undef __get_dynamic_array
  222. #define __get_dynamic_array(field) \
  223. ((void *)__entry + (__entry->__data_loc_##field & 0xffff))
  224. #undef __get_str
  225. #define __get_str(field) (char *)__get_dynamic_array(field)
  226. #undef __print_flags
  227. #define __print_flags(flag, delim, flag_array...) \
  228. ({ \
  229. static const struct trace_print_flags __flags[] = \
  230. { flag_array, { -1, NULL }}; \
  231. ftrace_print_flags_seq(p, delim, flag, __flags); \
  232. })
  233. #undef __print_symbolic
  234. #define __print_symbolic(value, symbol_array...) \
  235. ({ \
  236. static const struct trace_print_flags symbols[] = \
  237. { symbol_array, { -1, NULL }}; \
  238. ftrace_print_symbols_seq(p, value, symbols); \
  239. })
  240. #undef TRACE_EVENT_TEMPLATE
  241. #define TRACE_EVENT_TEMPLATE(call, proto, args, tstruct, assign, print) \
  242. static enum print_line_t \
  243. ftrace_raw_output_id_##call(int event_id, const char *name, \
  244. struct trace_iterator *iter, int flags) \
  245. { \
  246. struct trace_seq *s = &iter->seq; \
  247. struct ftrace_raw_##call *field; \
  248. struct trace_entry *entry; \
  249. struct trace_seq *p; \
  250. int ret; \
  251. \
  252. entry = iter->ent; \
  253. \
  254. if (entry->type != event_id) { \
  255. WARN_ON_ONCE(1); \
  256. return TRACE_TYPE_UNHANDLED; \
  257. } \
  258. \
  259. field = (typeof(field))entry; \
  260. \
  261. p = &get_cpu_var(ftrace_event_seq); \
  262. trace_seq_init(p); \
  263. ret = trace_seq_printf(s, "%s: ", name); \
  264. if (ret) \
  265. ret = trace_seq_printf(s, print); \
  266. put_cpu(); \
  267. if (!ret) \
  268. return TRACE_TYPE_PARTIAL_LINE; \
  269. \
  270. return TRACE_TYPE_HANDLED; \
  271. }
  272. #undef DEFINE_EVENT
  273. #define DEFINE_EVENT(template, name, proto, args) \
  274. static enum print_line_t \
  275. ftrace_raw_output_##name(struct trace_iterator *iter, int flags) \
  276. { \
  277. return ftrace_raw_output_id_##template(event_##name.id, \
  278. #name, iter, flags); \
  279. }
  280. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
  281. #undef __field_ext
  282. #define __field_ext(type, item, filter_type) \
  283. ret = trace_define_field(event_call, #type, #item, \
  284. offsetof(typeof(field), item), \
  285. sizeof(field.item), \
  286. is_signed_type(type), filter_type); \
  287. if (ret) \
  288. return ret;
  289. #undef __field
  290. #define __field(type, item) __field_ext(type, item, FILTER_OTHER)
  291. #undef __array
  292. #define __array(type, item, len) \
  293. BUILD_BUG_ON(len > MAX_FILTER_STR_VAL); \
  294. ret = trace_define_field(event_call, #type "[" #len "]", #item, \
  295. offsetof(typeof(field), item), \
  296. sizeof(field.item), 0, FILTER_OTHER); \
  297. if (ret) \
  298. return ret;
  299. #undef __dynamic_array
  300. #define __dynamic_array(type, item, len) \
  301. ret = trace_define_field(event_call, "__data_loc " #type "[]", #item, \
  302. offsetof(typeof(field), __data_loc_##item), \
  303. sizeof(field.__data_loc_##item), 0, \
  304. FILTER_OTHER);
  305. #undef __string
  306. #define __string(item, src) __dynamic_array(char, item, -1)
  307. #undef TRACE_EVENT_TEMPLATE
  308. #define TRACE_EVENT_TEMPLATE(call, proto, args, tstruct, func, print) \
  309. static int \
  310. ftrace_define_fields_##call(struct ftrace_event_call *event_call) \
  311. { \
  312. struct ftrace_raw_##call field; \
  313. int ret; \
  314. \
  315. ret = trace_define_common_fields(event_call); \
  316. if (ret) \
  317. return ret; \
  318. \
  319. tstruct; \
  320. \
  321. return ret; \
  322. }
  323. #undef DEFINE_EVENT
  324. #define DEFINE_EVENT(template, name, proto, args)
  325. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
  326. /*
  327. * remember the offset of each array from the beginning of the event.
  328. */
  329. #undef __entry
  330. #define __entry entry
  331. #undef __field
  332. #define __field(type, item)
  333. #undef __field_ext
  334. #define __field_ext(type, item, filter_type)
  335. #undef __array
  336. #define __array(type, item, len)
  337. #undef __dynamic_array
  338. #define __dynamic_array(type, item, len) \
  339. __data_offsets->item = __data_size + \
  340. offsetof(typeof(*entry), __data); \
  341. __data_offsets->item |= (len * sizeof(type)) << 16; \
  342. __data_size += (len) * sizeof(type);
  343. #undef __string
  344. #define __string(item, src) __dynamic_array(char, item, strlen(src) + 1)
  345. #undef TRACE_EVENT_TEMPLATE
  346. #define TRACE_EVENT_TEMPLATE(call, proto, args, tstruct, assign, print) \
  347. static inline int ftrace_get_offsets_##call( \
  348. struct ftrace_data_offsets_##call *__data_offsets, proto) \
  349. { \
  350. int __data_size = 0; \
  351. struct ftrace_raw_##call __maybe_unused *entry; \
  352. \
  353. tstruct; \
  354. \
  355. return __data_size; \
  356. }
  357. #undef DEFINE_EVENT
  358. #define DEFINE_EVENT(template, name, proto, args)
  359. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
  360. #ifdef CONFIG_EVENT_PROFILE
  361. /*
  362. * Generate the functions needed for tracepoint perf_event support.
  363. *
  364. * NOTE: The insertion profile callback (ftrace_profile_<call>) is defined later
  365. *
  366. * static int ftrace_profile_enable_<call>(void)
  367. * {
  368. * return register_trace_<call>(ftrace_profile_<call>);
  369. * }
  370. *
  371. * static void ftrace_profile_disable_<call>(void)
  372. * {
  373. * unregister_trace_<call>(ftrace_profile_<call>);
  374. * }
  375. *
  376. */
  377. #undef TRACE_EVENT_TEMPLATE
  378. #define TRACE_EVENT_TEMPLATE(call, proto, args, tstruct, assign, print)
  379. #undef DEFINE_EVENT
  380. #define DEFINE_EVENT(template, name, proto, args) \
  381. \
  382. static void ftrace_profile_##name(proto); \
  383. \
  384. static int ftrace_profile_enable_##name(struct ftrace_event_call *unused)\
  385. { \
  386. return register_trace_##name(ftrace_profile_##name); \
  387. } \
  388. \
  389. static void ftrace_profile_disable_##name(struct ftrace_event_call *unused)\
  390. { \
  391. unregister_trace_##name(ftrace_profile_##name); \
  392. }
  393. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
  394. #endif
  395. /*
  396. * Stage 4 of the trace events.
  397. *
  398. * Override the macros in <trace/trace_events.h> to include the following:
  399. *
  400. * static void ftrace_event_<call>(proto)
  401. * {
  402. * event_trace_printk(_RET_IP_, "<call>: " <fmt>);
  403. * }
  404. *
  405. * static int ftrace_reg_event_<call>(struct ftrace_event_call *unused)
  406. * {
  407. * int ret;
  408. *
  409. * ret = register_trace_<call>(ftrace_event_<call>);
  410. * if (!ret)
  411. * pr_info("event trace: Could not activate trace point "
  412. * "probe to <call>");
  413. * return ret;
  414. * }
  415. *
  416. * static void ftrace_unreg_event_<call>(struct ftrace_event_call *unused)
  417. * {
  418. * unregister_trace_<call>(ftrace_event_<call>);
  419. * }
  420. *
  421. *
  422. * For those macros defined with TRACE_EVENT:
  423. *
  424. * static struct ftrace_event_call event_<call>;
  425. *
  426. * static void ftrace_raw_event_<call>(proto)
  427. * {
  428. * struct ring_buffer_event *event;
  429. * struct ftrace_raw_<call> *entry; <-- defined in stage 1
  430. * struct ring_buffer *buffer;
  431. * unsigned long irq_flags;
  432. * int pc;
  433. *
  434. * local_save_flags(irq_flags);
  435. * pc = preempt_count();
  436. *
  437. * event = trace_current_buffer_lock_reserve(&buffer,
  438. * event_<call>.id,
  439. * sizeof(struct ftrace_raw_<call>),
  440. * irq_flags, pc);
  441. * if (!event)
  442. * return;
  443. * entry = ring_buffer_event_data(event);
  444. *
  445. * <assign>; <-- Here we assign the entries by the __field and
  446. * __array macros.
  447. *
  448. * trace_current_buffer_unlock_commit(buffer, event, irq_flags, pc);
  449. * }
  450. *
  451. * static int ftrace_raw_reg_event_<call>(struct ftrace_event_call *unused)
  452. * {
  453. * int ret;
  454. *
  455. * ret = register_trace_<call>(ftrace_raw_event_<call>);
  456. * if (!ret)
  457. * pr_info("event trace: Could not activate trace point "
  458. * "probe to <call>");
  459. * return ret;
  460. * }
  461. *
  462. * static void ftrace_unreg_event_<call>(struct ftrace_event_call *unused)
  463. * {
  464. * unregister_trace_<call>(ftrace_raw_event_<call>);
  465. * }
  466. *
  467. * static struct trace_event ftrace_event_type_<call> = {
  468. * .trace = ftrace_raw_output_<call>, <-- stage 2
  469. * };
  470. *
  471. * static int ftrace_raw_init_event_<call>(struct ftrace_event_call *unused)
  472. * {
  473. * int id;
  474. *
  475. * id = register_ftrace_event(&ftrace_event_type_<call>);
  476. * if (!id)
  477. * return -ENODEV;
  478. * event_<call>.id = id;
  479. * return 0;
  480. * }
  481. *
  482. * static struct ftrace_event_call __used
  483. * __attribute__((__aligned__(4)))
  484. * __attribute__((section("_ftrace_events"))) event_<call> = {
  485. * .name = "<call>",
  486. * .system = "<system>",
  487. * .raw_init = ftrace_raw_init_event_<call>,
  488. * .regfunc = ftrace_reg_event_<call>,
  489. * .unregfunc = ftrace_unreg_event_<call>,
  490. * .show_format = ftrace_format_<call>,
  491. * }
  492. *
  493. */
  494. #undef TP_FMT
  495. #define TP_FMT(fmt, args...) fmt "\n", ##args
  496. #ifdef CONFIG_EVENT_PROFILE
  497. #define _TRACE_PROFILE_INIT(call) \
  498. .profile_count = ATOMIC_INIT(-1), \
  499. .profile_enable = ftrace_profile_enable_##call, \
  500. .profile_disable = ftrace_profile_disable_##call,
  501. #else
  502. #define _TRACE_PROFILE_INIT(call)
  503. #endif
  504. #undef __entry
  505. #define __entry entry
  506. #undef __field
  507. #define __field(type, item)
  508. #undef __array
  509. #define __array(type, item, len)
  510. #undef __dynamic_array
  511. #define __dynamic_array(type, item, len) \
  512. __entry->__data_loc_##item = __data_offsets.item;
  513. #undef __string
  514. #define __string(item, src) __dynamic_array(char, item, -1) \
  515. #undef __assign_str
  516. #define __assign_str(dst, src) \
  517. strcpy(__get_str(dst), src);
  518. #undef TRACE_EVENT_TEMPLATE
  519. #define TRACE_EVENT_TEMPLATE(call, proto, args, tstruct, assign, print) \
  520. \
  521. static void ftrace_raw_event_id_##call(struct ftrace_event_call *event_call, \
  522. proto) \
  523. { \
  524. struct ftrace_data_offsets_##call __maybe_unused __data_offsets;\
  525. struct ring_buffer_event *event; \
  526. struct ftrace_raw_##call *entry; \
  527. struct ring_buffer *buffer; \
  528. unsigned long irq_flags; \
  529. int __data_size; \
  530. int pc; \
  531. \
  532. local_save_flags(irq_flags); \
  533. pc = preempt_count(); \
  534. \
  535. __data_size = ftrace_get_offsets_##call(&__data_offsets, args); \
  536. \
  537. event = trace_current_buffer_lock_reserve(&buffer, \
  538. event_call->id, \
  539. sizeof(*entry) + __data_size, \
  540. irq_flags, pc); \
  541. if (!event) \
  542. return; \
  543. entry = ring_buffer_event_data(event); \
  544. \
  545. \
  546. tstruct \
  547. \
  548. { assign; } \
  549. \
  550. if (!filter_current_check_discard(buffer, event_call, entry, event)) \
  551. trace_nowake_buffer_unlock_commit(buffer, \
  552. event, irq_flags, pc); \
  553. }
  554. #undef DEFINE_EVENT
  555. #define DEFINE_EVENT(template, call, proto, args) \
  556. \
  557. static void ftrace_raw_event_##call(proto) \
  558. { \
  559. ftrace_raw_event_id_##template(&event_##call, args); \
  560. } \
  561. \
  562. static int ftrace_raw_reg_event_##call(struct ftrace_event_call *unused)\
  563. { \
  564. int ret; \
  565. \
  566. ret = register_trace_##call(ftrace_raw_event_##call); \
  567. if (ret) \
  568. pr_info("event trace: Could not activate trace point " \
  569. "probe to " #call "\n"); \
  570. return ret; \
  571. } \
  572. \
  573. static void ftrace_raw_unreg_event_##call(struct ftrace_event_call *unused)\
  574. { \
  575. unregister_trace_##call(ftrace_raw_event_##call); \
  576. } \
  577. \
  578. static struct trace_event ftrace_event_type_##call = { \
  579. .trace = ftrace_raw_output_##call, \
  580. }; \
  581. \
  582. static int ftrace_raw_init_event_##call(struct ftrace_event_call *unused)\
  583. { \
  584. int id; \
  585. \
  586. id = register_ftrace_event(&ftrace_event_type_##call); \
  587. if (!id) \
  588. return -ENODEV; \
  589. event_##call.id = id; \
  590. INIT_LIST_HEAD(&event_##call.fields); \
  591. return 0; \
  592. } \
  593. \
  594. static struct ftrace_event_call __used \
  595. __attribute__((__aligned__(4))) \
  596. __attribute__((section("_ftrace_events"))) event_##call = { \
  597. .name = #call, \
  598. .system = __stringify(TRACE_SYSTEM), \
  599. .event = &ftrace_event_type_##call, \
  600. .raw_init = ftrace_raw_init_event_##call, \
  601. .regfunc = ftrace_raw_reg_event_##call, \
  602. .unregfunc = ftrace_raw_unreg_event_##call, \
  603. .show_format = ftrace_format_##template, \
  604. .define_fields = ftrace_define_fields_##template, \
  605. _TRACE_PROFILE_INIT(call) \
  606. }
  607. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
  608. /*
  609. * Define the insertion callback to profile events
  610. *
  611. * The job is very similar to ftrace_raw_event_<call> except that we don't
  612. * insert in the ring buffer but in a perf counter.
  613. *
  614. * static void ftrace_profile_<call>(proto)
  615. * {
  616. * struct ftrace_data_offsets_<call> __maybe_unused __data_offsets;
  617. * struct ftrace_event_call *event_call = &event_<call>;
  618. * extern void perf_tp_event(int, u64, u64, void *, int);
  619. * struct ftrace_raw_##call *entry;
  620. * struct perf_trace_buf *trace_buf;
  621. * u64 __addr = 0, __count = 1;
  622. * unsigned long irq_flags;
  623. * struct trace_entry *ent;
  624. * int __entry_size;
  625. * int __data_size;
  626. * int __cpu
  627. * int pc;
  628. *
  629. * pc = preempt_count();
  630. *
  631. * __data_size = ftrace_get_offsets_<call>(&__data_offsets, args);
  632. *
  633. * // Below we want to get the aligned size by taking into account
  634. * // the u32 field that will later store the buffer size
  635. * __entry_size = ALIGN(__data_size + sizeof(*entry) + sizeof(u32),
  636. * sizeof(u64));
  637. * __entry_size -= sizeof(u32);
  638. *
  639. * // Protect the non nmi buffer
  640. * // This also protects the rcu read side
  641. * local_irq_save(irq_flags);
  642. * __cpu = smp_processor_id();
  643. *
  644. * if (in_nmi())
  645. * trace_buf = rcu_dereference(perf_trace_buf_nmi);
  646. * else
  647. * trace_buf = rcu_dereference(perf_trace_buf);
  648. *
  649. * if (!trace_buf)
  650. * goto end;
  651. *
  652. * trace_buf = per_cpu_ptr(trace_buf, __cpu);
  653. *
  654. * // Avoid recursion from perf that could mess up the buffer
  655. * if (trace_buf->recursion++)
  656. * goto end_recursion;
  657. *
  658. * raw_data = trace_buf->buf;
  659. *
  660. * // Make recursion update visible before entering perf_tp_event
  661. * // so that we protect from perf recursions.
  662. *
  663. * barrier();
  664. *
  665. * //zero dead bytes from alignment to avoid stack leak to userspace:
  666. * *(u64 *)(&raw_data[__entry_size - sizeof(u64)]) = 0ULL;
  667. * entry = (struct ftrace_raw_<call> *)raw_data;
  668. * ent = &entry->ent;
  669. * tracing_generic_entry_update(ent, irq_flags, pc);
  670. * ent->type = event_call->id;
  671. *
  672. * <tstruct> <- do some jobs with dynamic arrays
  673. *
  674. * <assign> <- affect our values
  675. *
  676. * perf_tp_event(event_call->id, __addr, __count, entry,
  677. * __entry_size); <- submit them to perf counter
  678. *
  679. * }
  680. */
  681. #ifdef CONFIG_EVENT_PROFILE
  682. #undef __perf_addr
  683. #define __perf_addr(a) __addr = (a)
  684. #undef __perf_count
  685. #define __perf_count(c) __count = (c)
  686. #undef TRACE_EVENT_TEMPLATE
  687. #define TRACE_EVENT_TEMPLATE(call, proto, args, tstruct, assign, print) \
  688. static void \
  689. ftrace_profile_templ_##call(struct ftrace_event_call *event_call, \
  690. proto) \
  691. { \
  692. struct ftrace_data_offsets_##call __maybe_unused __data_offsets;\
  693. extern int perf_swevent_get_recursion_context(void); \
  694. extern void perf_swevent_put_recursion_context(int rctx); \
  695. extern void perf_tp_event(int, u64, u64, void *, int); \
  696. struct ftrace_raw_##call *entry; \
  697. u64 __addr = 0, __count = 1; \
  698. unsigned long irq_flags; \
  699. struct trace_entry *ent; \
  700. int __entry_size; \
  701. int __data_size; \
  702. char *trace_buf; \
  703. char *raw_data; \
  704. int __cpu; \
  705. int rctx; \
  706. int pc; \
  707. \
  708. pc = preempt_count(); \
  709. \
  710. __data_size = ftrace_get_offsets_##call(&__data_offsets, args); \
  711. __entry_size = ALIGN(__data_size + sizeof(*entry) + sizeof(u32),\
  712. sizeof(u64)); \
  713. __entry_size -= sizeof(u32); \
  714. \
  715. if (WARN_ONCE(__entry_size > FTRACE_MAX_PROFILE_SIZE, \
  716. "profile buffer not large enough")) \
  717. return; \
  718. \
  719. local_irq_save(irq_flags); \
  720. \
  721. rctx = perf_swevent_get_recursion_context(); \
  722. if (rctx < 0) \
  723. goto end_recursion; \
  724. \
  725. __cpu = smp_processor_id(); \
  726. \
  727. if (in_nmi()) \
  728. trace_buf = rcu_dereference(perf_trace_buf_nmi); \
  729. else \
  730. trace_buf = rcu_dereference(perf_trace_buf); \
  731. \
  732. if (!trace_buf) \
  733. goto end; \
  734. \
  735. raw_data = per_cpu_ptr(trace_buf, __cpu); \
  736. \
  737. *(u64 *)(&raw_data[__entry_size - sizeof(u64)]) = 0ULL; \
  738. entry = (struct ftrace_raw_##call *)raw_data; \
  739. ent = &entry->ent; \
  740. tracing_generic_entry_update(ent, irq_flags, pc); \
  741. ent->type = event_call->id; \
  742. \
  743. tstruct \
  744. \
  745. { assign; } \
  746. \
  747. perf_tp_event(event_call->id, __addr, __count, entry, \
  748. __entry_size); \
  749. \
  750. end: \
  751. perf_swevent_put_recursion_context(rctx); \
  752. end_recursion: \
  753. local_irq_restore(irq_flags); \
  754. \
  755. }
  756. #undef DEFINE_EVENT
  757. #define DEFINE_EVENT(template, call, proto, args) \
  758. static void ftrace_profile_##call(proto) \
  759. { \
  760. struct ftrace_event_call *event_call = &event_##call; \
  761. \
  762. ftrace_profile_templ_##template(event_call, args); \
  763. }
  764. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
  765. #endif /* CONFIG_EVENT_PROFILE */
  766. #undef _TRACE_PROFILE_INIT