ftrace.h 15 KB

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