ftrace.h 15 KB

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