ftrace.h 15 KB

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