ftrace.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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. #undef TRACE_FORMAT
  20. #define TRACE_FORMAT(call, proto, args, fmt)
  21. #undef __array
  22. #define __array(type, item, len) type item[len];
  23. #undef __field
  24. #define __field(type, item) type item;
  25. #undef TP_STRUCT__entry
  26. #define TP_STRUCT__entry(args...) args
  27. #undef TRACE_EVENT
  28. #define TRACE_EVENT(name, proto, args, tstruct, assign, print) \
  29. struct ftrace_raw_##name { \
  30. struct trace_entry ent; \
  31. tstruct \
  32. }; \
  33. static struct ftrace_event_call event_##name
  34. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
  35. /*
  36. * Stage 2 of the trace events.
  37. *
  38. * Override the macros in <trace/trace_events.h> to include the following:
  39. *
  40. * enum print_line_t
  41. * ftrace_raw_output_<call>(struct trace_iterator *iter, int flags)
  42. * {
  43. * struct trace_seq *s = &iter->seq;
  44. * struct ftrace_raw_<call> *field; <-- defined in stage 1
  45. * struct trace_entry *entry;
  46. * int ret;
  47. *
  48. * entry = iter->ent;
  49. *
  50. * if (entry->type != event_<call>.id) {
  51. * WARN_ON_ONCE(1);
  52. * return TRACE_TYPE_UNHANDLED;
  53. * }
  54. *
  55. * field = (typeof(field))entry;
  56. *
  57. * ret = trace_seq_printf(s, <TP_printk> "\n");
  58. * if (!ret)
  59. * return TRACE_TYPE_PARTIAL_LINE;
  60. *
  61. * return TRACE_TYPE_HANDLED;
  62. * }
  63. *
  64. * This is the method used to print the raw event to the trace
  65. * output format. Note, this is not needed if the data is read
  66. * in binary.
  67. */
  68. #undef __entry
  69. #define __entry field
  70. #undef TP_printk
  71. #define TP_printk(fmt, args...) fmt "\n", args
  72. #undef TRACE_EVENT
  73. #define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
  74. enum print_line_t \
  75. ftrace_raw_output_##call(struct trace_iterator *iter, int flags) \
  76. { \
  77. struct trace_seq *s = &iter->seq; \
  78. struct ftrace_raw_##call *field; \
  79. struct trace_entry *entry; \
  80. int ret; \
  81. \
  82. entry = iter->ent; \
  83. \
  84. if (entry->type != event_##call.id) { \
  85. WARN_ON_ONCE(1); \
  86. return TRACE_TYPE_UNHANDLED; \
  87. } \
  88. \
  89. field = (typeof(field))entry; \
  90. \
  91. ret = trace_seq_printf(s, #call ": " print); \
  92. if (!ret) \
  93. return TRACE_TYPE_PARTIAL_LINE; \
  94. \
  95. return TRACE_TYPE_HANDLED; \
  96. }
  97. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
  98. /*
  99. * Setup the showing format of trace point.
  100. *
  101. * int
  102. * ftrace_format_##call(struct trace_seq *s)
  103. * {
  104. * struct ftrace_raw_##call field;
  105. * int ret;
  106. *
  107. * ret = trace_seq_printf(s, #type " " #item ";"
  108. * " offset:%u; size:%u;\n",
  109. * offsetof(struct ftrace_raw_##call, item),
  110. * sizeof(field.type));
  111. *
  112. * }
  113. */
  114. #undef TP_STRUCT__entry
  115. #define TP_STRUCT__entry(args...) args
  116. #undef __field
  117. #define __field(type, item) \
  118. ret = trace_seq_printf(s, "\tfield:" #type " " #item ";\t" \
  119. "offset:%u;\tsize:%u;\n", \
  120. (unsigned int)offsetof(typeof(field), item), \
  121. (unsigned int)sizeof(field.item)); \
  122. if (!ret) \
  123. return 0;
  124. #undef __array
  125. #define __array(type, item, len) \
  126. ret = trace_seq_printf(s, "\tfield:" #type " " #item "[" #len "];\t" \
  127. "offset:%u;\tsize:%u;\n", \
  128. (unsigned int)offsetof(typeof(field), item), \
  129. (unsigned int)sizeof(field.item)); \
  130. if (!ret) \
  131. return 0;
  132. #undef __entry
  133. #define __entry REC
  134. #undef TP_printk
  135. #define TP_printk(fmt, args...) "%s, %s\n", #fmt, __stringify(args)
  136. #undef TP_fast_assign
  137. #define TP_fast_assign(args...) args
  138. #undef TRACE_EVENT
  139. #define TRACE_EVENT(call, proto, args, tstruct, func, print) \
  140. static int \
  141. ftrace_format_##call(struct trace_seq *s) \
  142. { \
  143. struct ftrace_raw_##call field __attribute__((unused)); \
  144. int ret = 0; \
  145. \
  146. tstruct; \
  147. \
  148. trace_seq_printf(s, "\nprint fmt: " print); \
  149. \
  150. return ret; \
  151. }
  152. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
  153. #undef __field
  154. #define __field(type, item) \
  155. ret = trace_define_field(event_call, #type, #item, \
  156. offsetof(typeof(field), item), \
  157. sizeof(field.item)); \
  158. if (ret) \
  159. return ret;
  160. #undef __array
  161. #define __array(type, item, len) \
  162. BUILD_BUG_ON(len > MAX_FILTER_STR_VAL); \
  163. ret = trace_define_field(event_call, #type "[" #len "]", #item, \
  164. offsetof(typeof(field), item), \
  165. sizeof(field.item)); \
  166. if (ret) \
  167. return ret;
  168. #undef TRACE_EVENT
  169. #define TRACE_EVENT(call, proto, args, tstruct, func, print) \
  170. int \
  171. ftrace_define_fields_##call(void) \
  172. { \
  173. struct ftrace_raw_##call field; \
  174. struct ftrace_event_call *event_call = &event_##call; \
  175. int ret; \
  176. \
  177. __common_field(unsigned char, type); \
  178. __common_field(unsigned char, flags); \
  179. __common_field(unsigned char, preempt_count); \
  180. __common_field(int, pid); \
  181. __common_field(int, tgid); \
  182. \
  183. tstruct; \
  184. \
  185. return ret; \
  186. }
  187. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
  188. /*
  189. * Stage 3 of the trace events.
  190. *
  191. * Override the macros in <trace/trace_events.h> to include the following:
  192. *
  193. * static void ftrace_event_<call>(proto)
  194. * {
  195. * event_trace_printk(_RET_IP_, "<call>: " <fmt>);
  196. * }
  197. *
  198. * static int ftrace_reg_event_<call>(void)
  199. * {
  200. * int ret;
  201. *
  202. * ret = register_trace_<call>(ftrace_event_<call>);
  203. * if (!ret)
  204. * pr_info("event trace: Could not activate trace point "
  205. * "probe to <call>");
  206. * return ret;
  207. * }
  208. *
  209. * static void ftrace_unreg_event_<call>(void)
  210. * {
  211. * unregister_trace_<call>(ftrace_event_<call>);
  212. * }
  213. *
  214. * For those macros defined with TRACE_FORMAT:
  215. *
  216. * static struct ftrace_event_call __used
  217. * __attribute__((__aligned__(4)))
  218. * __attribute__((section("_ftrace_events"))) event_<call> = {
  219. * .name = "<call>",
  220. * .regfunc = ftrace_reg_event_<call>,
  221. * .unregfunc = ftrace_unreg_event_<call>,
  222. * }
  223. *
  224. *
  225. * For those macros defined with TRACE_EVENT:
  226. *
  227. * static struct ftrace_event_call event_<call>;
  228. *
  229. * static void ftrace_raw_event_<call>(proto)
  230. * {
  231. * struct ring_buffer_event *event;
  232. * struct ftrace_raw_<call> *entry; <-- defined in stage 1
  233. * unsigned long irq_flags;
  234. * int pc;
  235. *
  236. * local_save_flags(irq_flags);
  237. * pc = preempt_count();
  238. *
  239. * event = trace_current_buffer_lock_reserve(event_<call>.id,
  240. * sizeof(struct ftrace_raw_<call>),
  241. * irq_flags, pc);
  242. * if (!event)
  243. * return;
  244. * entry = ring_buffer_event_data(event);
  245. *
  246. * <assign>; <-- Here we assign the entries by the __field and
  247. * __array macros.
  248. *
  249. * trace_current_buffer_unlock_commit(event, irq_flags, pc);
  250. * }
  251. *
  252. * static int ftrace_raw_reg_event_<call>(void)
  253. * {
  254. * int ret;
  255. *
  256. * ret = register_trace_<call>(ftrace_raw_event_<call>);
  257. * if (!ret)
  258. * pr_info("event trace: Could not activate trace point "
  259. * "probe to <call>");
  260. * return ret;
  261. * }
  262. *
  263. * static void ftrace_unreg_event_<call>(void)
  264. * {
  265. * unregister_trace_<call>(ftrace_raw_event_<call>);
  266. * }
  267. *
  268. * static struct trace_event ftrace_event_type_<call> = {
  269. * .trace = ftrace_raw_output_<call>, <-- stage 2
  270. * };
  271. *
  272. * static int ftrace_raw_init_event_<call>(void)
  273. * {
  274. * int id;
  275. *
  276. * id = register_ftrace_event(&ftrace_event_type_<call>);
  277. * if (!id)
  278. * return -ENODEV;
  279. * event_<call>.id = id;
  280. * return 0;
  281. * }
  282. *
  283. * static struct ftrace_event_call __used
  284. * __attribute__((__aligned__(4)))
  285. * __attribute__((section("_ftrace_events"))) event_<call> = {
  286. * .name = "<call>",
  287. * .system = "<system>",
  288. * .raw_init = ftrace_raw_init_event_<call>,
  289. * .regfunc = ftrace_reg_event_<call>,
  290. * .unregfunc = ftrace_unreg_event_<call>,
  291. * .show_format = ftrace_format_<call>,
  292. * }
  293. *
  294. */
  295. #undef TP_FMT
  296. #define TP_FMT(fmt, args...) fmt "\n", ##args
  297. #ifdef CONFIG_EVENT_PROFILE
  298. #define _TRACE_PROFILE(call, proto, args) \
  299. static void ftrace_profile_##call(proto) \
  300. { \
  301. extern void perf_tpcounter_event(int); \
  302. perf_tpcounter_event(event_##call.id); \
  303. } \
  304. \
  305. static int ftrace_profile_enable_##call(struct ftrace_event_call *call) \
  306. { \
  307. int ret = 0; \
  308. \
  309. if (!atomic_inc_return(&call->profile_count)) \
  310. ret = register_trace_##call(ftrace_profile_##call); \
  311. \
  312. return ret; \
  313. } \
  314. \
  315. static void ftrace_profile_disable_##call(struct ftrace_event_call *call) \
  316. { \
  317. if (atomic_add_negative(-1, &call->profile_count)) \
  318. unregister_trace_##call(ftrace_profile_##call); \
  319. }
  320. #define _TRACE_PROFILE_INIT(call) \
  321. .profile_count = ATOMIC_INIT(-1), \
  322. .profile_enable = ftrace_profile_enable_##call, \
  323. .profile_disable = ftrace_profile_disable_##call,
  324. #else
  325. #define _TRACE_PROFILE(call, proto, args)
  326. #define _TRACE_PROFILE_INIT(call)
  327. #endif
  328. #define _TRACE_FORMAT(call, proto, args, fmt) \
  329. static void ftrace_event_##call(proto) \
  330. { \
  331. event_trace_printk(_RET_IP_, #call ": " fmt); \
  332. } \
  333. \
  334. static int ftrace_reg_event_##call(void) \
  335. { \
  336. int ret; \
  337. \
  338. ret = register_trace_##call(ftrace_event_##call); \
  339. if (ret) \
  340. pr_info("event trace: Could not activate trace point " \
  341. "probe to " #call "\n"); \
  342. return ret; \
  343. } \
  344. \
  345. static void ftrace_unreg_event_##call(void) \
  346. { \
  347. unregister_trace_##call(ftrace_event_##call); \
  348. } \
  349. \
  350. static struct ftrace_event_call event_##call; \
  351. \
  352. static int ftrace_init_event_##call(void) \
  353. { \
  354. int id; \
  355. \
  356. id = register_ftrace_event(NULL); \
  357. if (!id) \
  358. return -ENODEV; \
  359. event_##call.id = id; \
  360. return 0; \
  361. }
  362. #undef TRACE_FORMAT
  363. #define TRACE_FORMAT(call, proto, args, fmt) \
  364. _TRACE_FORMAT(call, PARAMS(proto), PARAMS(args), PARAMS(fmt)) \
  365. _TRACE_PROFILE(call, PARAMS(proto), PARAMS(args)) \
  366. static struct ftrace_event_call __used \
  367. __attribute__((__aligned__(4))) \
  368. __attribute__((section("_ftrace_events"))) event_##call = { \
  369. .name = #call, \
  370. .system = __stringify(TRACE_SYSTEM), \
  371. .raw_init = ftrace_init_event_##call, \
  372. .regfunc = ftrace_reg_event_##call, \
  373. .unregfunc = ftrace_unreg_event_##call, \
  374. _TRACE_PROFILE_INIT(call) \
  375. }
  376. #undef __entry
  377. #define __entry entry
  378. #undef TRACE_EVENT
  379. #define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
  380. _TRACE_PROFILE(call, PARAMS(proto), PARAMS(args)) \
  381. \
  382. static struct ftrace_event_call event_##call; \
  383. \
  384. static void ftrace_raw_event_##call(proto) \
  385. { \
  386. struct ftrace_event_call *call = &event_##call; \
  387. struct ring_buffer_event *event; \
  388. struct ftrace_raw_##call *entry; \
  389. unsigned long irq_flags; \
  390. int pc; \
  391. \
  392. local_save_flags(irq_flags); \
  393. pc = preempt_count(); \
  394. \
  395. event = trace_current_buffer_lock_reserve(event_##call.id, \
  396. sizeof(struct ftrace_raw_##call), \
  397. irq_flags, pc); \
  398. if (!event) \
  399. return; \
  400. entry = ring_buffer_event_data(event); \
  401. \
  402. assign; \
  403. \
  404. if (!filter_current_check_discard(call, entry, event)) \
  405. trace_nowake_buffer_unlock_commit(event, irq_flags, pc); \
  406. } \
  407. \
  408. static int ftrace_raw_reg_event_##call(void) \
  409. { \
  410. int ret; \
  411. \
  412. ret = register_trace_##call(ftrace_raw_event_##call); \
  413. if (ret) \
  414. pr_info("event trace: Could not activate trace point " \
  415. "probe to " #call "\n"); \
  416. return ret; \
  417. } \
  418. \
  419. static void ftrace_raw_unreg_event_##call(void) \
  420. { \
  421. unregister_trace_##call(ftrace_raw_event_##call); \
  422. } \
  423. \
  424. static struct trace_event ftrace_event_type_##call = { \
  425. .trace = ftrace_raw_output_##call, \
  426. }; \
  427. \
  428. static int ftrace_raw_init_event_##call(void) \
  429. { \
  430. int id; \
  431. \
  432. id = register_ftrace_event(&ftrace_event_type_##call); \
  433. if (!id) \
  434. return -ENODEV; \
  435. event_##call.id = id; \
  436. INIT_LIST_HEAD(&event_##call.fields); \
  437. init_preds(&event_##call); \
  438. return 0; \
  439. } \
  440. \
  441. static struct ftrace_event_call __used \
  442. __attribute__((__aligned__(4))) \
  443. __attribute__((section("_ftrace_events"))) event_##call = { \
  444. .name = #call, \
  445. .system = __stringify(TRACE_SYSTEM), \
  446. .event = &ftrace_event_type_##call, \
  447. .raw_init = ftrace_raw_init_event_##call, \
  448. .regfunc = ftrace_raw_reg_event_##call, \
  449. .unregfunc = ftrace_raw_unreg_event_##call, \
  450. .show_format = ftrace_format_##call, \
  451. .define_fields = ftrace_define_fields_##call, \
  452. _TRACE_PROFILE_INIT(call) \
  453. }
  454. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
  455. #undef _TRACE_PROFILE
  456. #undef _TRACE_PROFILE_INIT