ftrace.h 15 KB

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