ftrace.h 13 KB

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