ftrace.h 14 KB

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