perf.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. * Stage 4 of the trace events.
  3. *
  4. * Override the macros in <trace/trace_events.h> to include the following:
  5. *
  6. * For those macros defined with TRACE_EVENT:
  7. *
  8. * static struct trace_event_call event_<call>;
  9. *
  10. * static void trace_event_raw_event_<call>(void *__data, proto)
  11. * {
  12. * struct trace_event_file *trace_file = __data;
  13. * struct trace_event_call *event_call = trace_file->event_call;
  14. * struct trace_event_data_offsets_<call> __maybe_unused __data_offsets;
  15. * unsigned long eflags = trace_file->flags;
  16. * enum event_trigger_type __tt = ETT_NONE;
  17. * struct ring_buffer_event *event;
  18. * struct trace_event_raw_<call> *entry; <-- defined in stage 1
  19. * struct ring_buffer *buffer;
  20. * unsigned long irq_flags;
  21. * int __data_size;
  22. * int pc;
  23. *
  24. * if (!(eflags & EVENT_FILE_FL_TRIGGER_COND)) {
  25. * if (eflags & EVENT_FILE_FL_TRIGGER_MODE)
  26. * event_triggers_call(trace_file, NULL);
  27. * if (eflags & EVENT_FILE_FL_SOFT_DISABLED)
  28. * return;
  29. * }
  30. *
  31. * local_save_flags(irq_flags);
  32. * pc = preempt_count();
  33. *
  34. * __data_size = trace_event_get_offsets_<call>(&__data_offsets, args);
  35. *
  36. * event = trace_event_buffer_lock_reserve(&buffer, trace_file,
  37. * event_<call>->event.type,
  38. * sizeof(*entry) + __data_size,
  39. * irq_flags, pc);
  40. * if (!event)
  41. * return;
  42. * entry = ring_buffer_event_data(event);
  43. *
  44. * { <assign>; } <-- Here we assign the entries by the __field and
  45. * __array macros.
  46. *
  47. * if (eflags & EVENT_FILE_FL_TRIGGER_COND)
  48. * __tt = event_triggers_call(trace_file, entry);
  49. *
  50. * if (test_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT,
  51. * &trace_file->flags))
  52. * ring_buffer_discard_commit(buffer, event);
  53. * else if (!filter_check_discard(trace_file, entry, buffer, event))
  54. * trace_buffer_unlock_commit(buffer, event, irq_flags, pc);
  55. *
  56. * if (__tt)
  57. * event_triggers_post_call(trace_file, __tt);
  58. * }
  59. *
  60. * static struct trace_event ftrace_event_type_<call> = {
  61. * .trace = trace_raw_output_<call>, <-- stage 2
  62. * };
  63. *
  64. * static char print_fmt_<call>[] = <TP_printk>;
  65. *
  66. * static struct trace_event_class __used event_class_<template> = {
  67. * .system = "<system>",
  68. * .define_fields = trace_event_define_fields_<call>,
  69. * .fields = LIST_HEAD_INIT(event_class_##call.fields),
  70. * .raw_init = trace_event_raw_init,
  71. * .probe = trace_event_raw_event_##call,
  72. * .reg = trace_event_reg,
  73. * };
  74. *
  75. * static struct trace_event_call event_<call> = {
  76. * .class = event_class_<template>,
  77. * {
  78. * .tp = &__tracepoint_<call>,
  79. * },
  80. * .event = &ftrace_event_type_<call>,
  81. * .print_fmt = print_fmt_<call>,
  82. * .flags = TRACE_EVENT_FL_TRACEPOINT,
  83. * };
  84. * // its only safe to use pointers when doing linker tricks to
  85. * // create an array.
  86. * static struct trace_event_call __used
  87. * __attribute__((section("_ftrace_events"))) *__event_<call> = &event_<call>;
  88. *
  89. */
  90. #ifdef CONFIG_PERF_EVENTS
  91. #define _TRACE_PERF_PROTO(call, proto) \
  92. static notrace void \
  93. perf_trace_##call(void *__data, proto);
  94. #define _TRACE_PERF_INIT(call) \
  95. .perf_probe = perf_trace_##call,
  96. #else
  97. #define _TRACE_PERF_PROTO(call, proto)
  98. #define _TRACE_PERF_INIT(call)
  99. #endif /* CONFIG_PERF_EVENTS */
  100. #undef __entry
  101. #define __entry entry
  102. #undef __field
  103. #define __field(type, item)
  104. #undef __field_struct
  105. #define __field_struct(type, item)
  106. #undef __array
  107. #define __array(type, item, len)
  108. #undef __dynamic_array
  109. #define __dynamic_array(type, item, len) \
  110. __entry->__data_loc_##item = __data_offsets.item;
  111. #undef __string
  112. #define __string(item, src) __dynamic_array(char, item, -1)
  113. #undef __assign_str
  114. #define __assign_str(dst, src) \
  115. strcpy(__get_str(dst), (src) ? (const char *)(src) : "(null)");
  116. #undef __bitmask
  117. #define __bitmask(item, nr_bits) __dynamic_array(unsigned long, item, -1)
  118. #undef __get_bitmask
  119. #define __get_bitmask(field) (char *)__get_dynamic_array(field)
  120. #undef __assign_bitmask
  121. #define __assign_bitmask(dst, src, nr_bits) \
  122. memcpy(__get_bitmask(dst), (src), __bitmask_size_in_bytes(nr_bits))
  123. #undef TP_fast_assign
  124. #define TP_fast_assign(args...) args
  125. #undef __perf_addr
  126. #define __perf_addr(a) (a)
  127. #undef __perf_count
  128. #define __perf_count(c) (c)
  129. #undef __perf_task
  130. #define __perf_task(t) (t)
  131. #undef DECLARE_EVENT_CLASS
  132. #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
  133. \
  134. static notrace void \
  135. trace_event_raw_event_##call(void *__data, proto) \
  136. { \
  137. struct trace_event_file *trace_file = __data; \
  138. struct trace_event_data_offsets_##call __maybe_unused __data_offsets;\
  139. struct trace_event_buffer fbuffer; \
  140. struct trace_event_raw_##call *entry; \
  141. int __data_size; \
  142. \
  143. if (trace_trigger_soft_disabled(trace_file)) \
  144. return; \
  145. \
  146. __data_size = trace_event_get_offsets_##call(&__data_offsets, args); \
  147. \
  148. entry = trace_event_buffer_reserve(&fbuffer, trace_file, \
  149. sizeof(*entry) + __data_size); \
  150. \
  151. if (!entry) \
  152. return; \
  153. \
  154. tstruct \
  155. \
  156. { assign; } \
  157. \
  158. trace_event_buffer_commit(&fbuffer); \
  159. }
  160. /*
  161. * The ftrace_test_probe is compiled out, it is only here as a build time check
  162. * to make sure that if the tracepoint handling changes, the ftrace probe will
  163. * fail to compile unless it too is updated.
  164. */
  165. #undef DEFINE_EVENT
  166. #define DEFINE_EVENT(template, call, proto, args) \
  167. static inline void ftrace_test_probe_##call(void) \
  168. { \
  169. check_trace_callback_type_##call(trace_event_raw_event_##template); \
  170. }
  171. #undef DEFINE_EVENT_PRINT
  172. #define DEFINE_EVENT_PRINT(template, name, proto, args, print)
  173. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
  174. #undef __entry
  175. #define __entry REC
  176. #undef __print_flags
  177. #undef __print_symbolic
  178. #undef __print_hex
  179. #undef __get_dynamic_array
  180. #undef __get_dynamic_array_len
  181. #undef __get_str
  182. #undef __get_bitmask
  183. #undef __print_array
  184. #undef TP_printk
  185. #define TP_printk(fmt, args...) "\"" fmt "\", " __stringify(args)
  186. #undef DECLARE_EVENT_CLASS
  187. #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
  188. _TRACE_PERF_PROTO(call, PARAMS(proto)); \
  189. static char print_fmt_##call[] = print; \
  190. static struct trace_event_class __used __refdata event_class_##call = { \
  191. .system = TRACE_SYSTEM_STRING, \
  192. .define_fields = trace_event_define_fields_##call, \
  193. .fields = LIST_HEAD_INIT(event_class_##call.fields),\
  194. .raw_init = trace_event_raw_init, \
  195. .probe = trace_event_raw_event_##call, \
  196. .reg = trace_event_reg, \
  197. _TRACE_PERF_INIT(call) \
  198. };
  199. #undef DEFINE_EVENT
  200. #define DEFINE_EVENT(template, call, proto, args) \
  201. \
  202. static struct trace_event_call __used event_##call = { \
  203. .class = &event_class_##template, \
  204. { \
  205. .tp = &__tracepoint_##call, \
  206. }, \
  207. .event.funcs = &trace_event_type_funcs_##template, \
  208. .print_fmt = print_fmt_##template, \
  209. .flags = TRACE_EVENT_FL_TRACEPOINT, \
  210. }; \
  211. static struct trace_event_call __used \
  212. __attribute__((section("_ftrace_events"))) *__event_##call = &event_##call
  213. #undef DEFINE_EVENT_PRINT
  214. #define DEFINE_EVENT_PRINT(template, call, proto, args, print) \
  215. \
  216. static char print_fmt_##call[] = print; \
  217. \
  218. static struct trace_event_call __used event_##call = { \
  219. .class = &event_class_##template, \
  220. { \
  221. .tp = &__tracepoint_##call, \
  222. }, \
  223. .event.funcs = &trace_event_type_funcs_##call, \
  224. .print_fmt = print_fmt_##call, \
  225. .flags = TRACE_EVENT_FL_TRACEPOINT, \
  226. }; \
  227. static struct trace_event_call __used \
  228. __attribute__((section("_ftrace_events"))) *__event_##call = &event_##call
  229. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
  230. #undef TRACE_SYSTEM_VAR
  231. #ifdef CONFIG_PERF_EVENTS
  232. #undef __entry
  233. #define __entry entry
  234. #undef __get_dynamic_array
  235. #define __get_dynamic_array(field) \
  236. ((void *)__entry + (__entry->__data_loc_##field & 0xffff))
  237. #undef __get_dynamic_array_len
  238. #define __get_dynamic_array_len(field) \
  239. ((__entry->__data_loc_##field >> 16) & 0xffff)
  240. #undef __get_str
  241. #define __get_str(field) (char *)__get_dynamic_array(field)
  242. #undef __get_bitmask
  243. #define __get_bitmask(field) (char *)__get_dynamic_array(field)
  244. #undef __perf_addr
  245. #define __perf_addr(a) (__addr = (a))
  246. #undef __perf_count
  247. #define __perf_count(c) (__count = (c))
  248. #undef __perf_task
  249. #define __perf_task(t) (__task = (t))
  250. #undef DECLARE_EVENT_CLASS
  251. #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print) \
  252. static notrace void \
  253. perf_trace_##call(void *__data, proto) \
  254. { \
  255. struct trace_event_call *event_call = __data; \
  256. struct trace_event_data_offsets_##call __maybe_unused __data_offsets;\
  257. struct trace_event_raw_##call *entry; \
  258. struct pt_regs *__regs; \
  259. u64 __addr = 0, __count = 1; \
  260. struct task_struct *__task = NULL; \
  261. struct hlist_head *head; \
  262. int __entry_size; \
  263. int __data_size; \
  264. int rctx; \
  265. \
  266. __data_size = trace_event_get_offsets_##call(&__data_offsets, args); \
  267. \
  268. head = this_cpu_ptr(event_call->perf_events); \
  269. if (__builtin_constant_p(!__task) && !__task && \
  270. hlist_empty(head)) \
  271. return; \
  272. \
  273. __entry_size = ALIGN(__data_size + sizeof(*entry) + sizeof(u32),\
  274. sizeof(u64)); \
  275. __entry_size -= sizeof(u32); \
  276. \
  277. entry = perf_trace_buf_prepare(__entry_size, \
  278. event_call->event.type, &__regs, &rctx); \
  279. if (!entry) \
  280. return; \
  281. \
  282. perf_fetch_caller_regs(__regs); \
  283. \
  284. tstruct \
  285. \
  286. { assign; } \
  287. \
  288. perf_trace_buf_submit(entry, __entry_size, rctx, __addr, \
  289. __count, __regs, head, __task); \
  290. }
  291. /*
  292. * This part is compiled out, it is only here as a build time check
  293. * to make sure that if the tracepoint handling changes, the
  294. * perf probe will fail to compile unless it too is updated.
  295. */
  296. #undef DEFINE_EVENT
  297. #define DEFINE_EVENT(template, call, proto, args) \
  298. static inline void perf_test_probe_##call(void) \
  299. { \
  300. check_trace_callback_type_##call(perf_trace_##template); \
  301. }
  302. #undef DEFINE_EVENT_PRINT
  303. #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
  304. DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
  305. #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
  306. #endif /* CONFIG_PERF_EVENTS */