tracepoint.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. #ifndef _LINUX_TRACEPOINT_H
  2. #define _LINUX_TRACEPOINT_H
  3. /*
  4. * Kernel Tracepoint API.
  5. *
  6. * See Documentation/trace/tracepoints.txt.
  7. *
  8. * (C) Copyright 2008 Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
  9. *
  10. * Heavily inspired from the Linux Kernel Markers.
  11. *
  12. * This file is released under the GPLv2.
  13. * See the file COPYING for more details.
  14. */
  15. #include <linux/errno.h>
  16. #include <linux/types.h>
  17. #include <linux/rcupdate.h>
  18. #include <linux/static_key.h>
  19. struct module;
  20. struct tracepoint;
  21. struct tracepoint_func {
  22. void *func;
  23. void *data;
  24. };
  25. struct tracepoint {
  26. const char *name; /* Tracepoint name */
  27. struct static_key key;
  28. void (*regfunc)(void);
  29. void (*unregfunc)(void);
  30. struct tracepoint_func __rcu *funcs;
  31. };
  32. /*
  33. * Connect a probe to a tracepoint.
  34. * Internal API, should not be used directly.
  35. */
  36. extern int tracepoint_probe_register(const char *name, void *probe, void *data);
  37. /*
  38. * Disconnect a probe from a tracepoint.
  39. * Internal API, should not be used directly.
  40. */
  41. extern int
  42. tracepoint_probe_unregister(const char *name, void *probe, void *data);
  43. #ifdef CONFIG_MODULES
  44. struct tp_module {
  45. struct list_head list;
  46. unsigned int num_tracepoints;
  47. struct tracepoint * const *tracepoints_ptrs;
  48. };
  49. #endif /* CONFIG_MODULES */
  50. /*
  51. * tracepoint_synchronize_unregister must be called between the last tracepoint
  52. * probe unregistration and the end of module exit to make sure there is no
  53. * caller executing a probe when it is freed.
  54. */
  55. static inline void tracepoint_synchronize_unregister(void)
  56. {
  57. synchronize_sched();
  58. }
  59. #define PARAMS(args...) args
  60. #endif /* _LINUX_TRACEPOINT_H */
  61. /*
  62. * Note: we keep the TRACE_EVENT and DECLARE_TRACE outside the include
  63. * file ifdef protection.
  64. * This is due to the way trace events work. If a file includes two
  65. * trace event headers under one "CREATE_TRACE_POINTS" the first include
  66. * will override the TRACE_EVENT and break the second include.
  67. */
  68. #ifndef DECLARE_TRACE
  69. #define TP_PROTO(args...) args
  70. #define TP_ARGS(args...) args
  71. #define TP_CONDITION(args...) args
  72. #ifdef CONFIG_TRACEPOINTS
  73. /*
  74. * it_func[0] is never NULL because there is at least one element in the array
  75. * when the array itself is non NULL.
  76. *
  77. * Note, the proto and args passed in includes "__data" as the first parameter.
  78. * The reason for this is to handle the "void" prototype. If a tracepoint
  79. * has a "void" prototype, then it is invalid to declare a function
  80. * as "(void *, void)". The DECLARE_TRACE_NOARGS() will pass in just
  81. * "void *data", where as the DECLARE_TRACE() will pass in "void *data, proto".
  82. */
  83. #define __DO_TRACE(tp, proto, args, cond, prercu, postrcu) \
  84. do { \
  85. struct tracepoint_func *it_func_ptr; \
  86. void *it_func; \
  87. void *__data; \
  88. \
  89. if (!(cond)) \
  90. return; \
  91. prercu; \
  92. rcu_read_lock_sched_notrace(); \
  93. it_func_ptr = rcu_dereference_sched((tp)->funcs); \
  94. if (it_func_ptr) { \
  95. do { \
  96. it_func = (it_func_ptr)->func; \
  97. __data = (it_func_ptr)->data; \
  98. ((void(*)(proto))(it_func))(args); \
  99. } while ((++it_func_ptr)->func); \
  100. } \
  101. rcu_read_unlock_sched_notrace(); \
  102. postrcu; \
  103. } while (0)
  104. #ifndef MODULE
  105. #define __DECLARE_TRACE_RCU(name, proto, args, cond, data_proto, data_args) \
  106. static inline void trace_##name##_rcuidle(proto) \
  107. { \
  108. if (static_key_false(&__tracepoint_##name.key)) \
  109. __DO_TRACE(&__tracepoint_##name, \
  110. TP_PROTO(data_proto), \
  111. TP_ARGS(data_args), \
  112. TP_CONDITION(cond), \
  113. rcu_irq_enter(), \
  114. rcu_irq_exit()); \
  115. }
  116. #else
  117. #define __DECLARE_TRACE_RCU(name, proto, args, cond, data_proto, data_args)
  118. #endif
  119. /*
  120. * Make sure the alignment of the structure in the __tracepoints section will
  121. * not add unwanted padding between the beginning of the section and the
  122. * structure. Force alignment to the same alignment as the section start.
  123. */
  124. #define __DECLARE_TRACE(name, proto, args, cond, data_proto, data_args) \
  125. extern struct tracepoint __tracepoint_##name; \
  126. static inline void trace_##name(proto) \
  127. { \
  128. if (static_key_false(&__tracepoint_##name.key)) \
  129. __DO_TRACE(&__tracepoint_##name, \
  130. TP_PROTO(data_proto), \
  131. TP_ARGS(data_args), \
  132. TP_CONDITION(cond),,); \
  133. } \
  134. __DECLARE_TRACE_RCU(name, PARAMS(proto), PARAMS(args), \
  135. PARAMS(cond), PARAMS(data_proto), PARAMS(data_args)) \
  136. static inline int \
  137. register_trace_##name(void (*probe)(data_proto), void *data) \
  138. { \
  139. return tracepoint_probe_register(#name, (void *)probe, \
  140. data); \
  141. } \
  142. static inline int \
  143. unregister_trace_##name(void (*probe)(data_proto), void *data) \
  144. { \
  145. return tracepoint_probe_unregister(#name, (void *)probe, \
  146. data); \
  147. } \
  148. static inline void \
  149. check_trace_callback_type_##name(void (*cb)(data_proto)) \
  150. { \
  151. }
  152. /*
  153. * We have no guarantee that gcc and the linker won't up-align the tracepoint
  154. * structures, so we create an array of pointers that will be used for iteration
  155. * on the tracepoints.
  156. */
  157. #define DEFINE_TRACE_FN(name, reg, unreg) \
  158. static const char __tpstrtab_##name[] \
  159. __attribute__((section("__tracepoints_strings"))) = #name; \
  160. struct tracepoint __tracepoint_##name \
  161. __attribute__((section("__tracepoints"))) = \
  162. { __tpstrtab_##name, STATIC_KEY_INIT_FALSE, reg, unreg, NULL };\
  163. static struct tracepoint * const __tracepoint_ptr_##name __used \
  164. __attribute__((section("__tracepoints_ptrs"))) = \
  165. &__tracepoint_##name;
  166. #define DEFINE_TRACE(name) \
  167. DEFINE_TRACE_FN(name, NULL, NULL);
  168. #define EXPORT_TRACEPOINT_SYMBOL_GPL(name) \
  169. EXPORT_SYMBOL_GPL(__tracepoint_##name)
  170. #define EXPORT_TRACEPOINT_SYMBOL(name) \
  171. EXPORT_SYMBOL(__tracepoint_##name)
  172. #else /* !CONFIG_TRACEPOINTS */
  173. #define __DECLARE_TRACE(name, proto, args, cond, data_proto, data_args) \
  174. static inline void trace_##name(proto) \
  175. { } \
  176. static inline void trace_##name##_rcuidle(proto) \
  177. { } \
  178. static inline int \
  179. register_trace_##name(void (*probe)(data_proto), \
  180. void *data) \
  181. { \
  182. return -ENOSYS; \
  183. } \
  184. static inline int \
  185. unregister_trace_##name(void (*probe)(data_proto), \
  186. void *data) \
  187. { \
  188. return -ENOSYS; \
  189. } \
  190. static inline void check_trace_callback_type_##name(void (*cb)(data_proto)) \
  191. { \
  192. }
  193. #define DEFINE_TRACE_FN(name, reg, unreg)
  194. #define DEFINE_TRACE(name)
  195. #define EXPORT_TRACEPOINT_SYMBOL_GPL(name)
  196. #define EXPORT_TRACEPOINT_SYMBOL(name)
  197. #endif /* CONFIG_TRACEPOINTS */
  198. /*
  199. * The need for the DECLARE_TRACE_NOARGS() is to handle the prototype
  200. * (void). "void" is a special value in a function prototype and can
  201. * not be combined with other arguments. Since the DECLARE_TRACE()
  202. * macro adds a data element at the beginning of the prototype,
  203. * we need a way to differentiate "(void *data, proto)" from
  204. * "(void *data, void)". The second prototype is invalid.
  205. *
  206. * DECLARE_TRACE_NOARGS() passes "void" as the tracepoint prototype
  207. * and "void *__data" as the callback prototype.
  208. *
  209. * DECLARE_TRACE() passes "proto" as the tracepoint protoype and
  210. * "void *__data, proto" as the callback prototype.
  211. */
  212. #define DECLARE_TRACE_NOARGS(name) \
  213. __DECLARE_TRACE(name, void, , 1, void *__data, __data)
  214. #define DECLARE_TRACE(name, proto, args) \
  215. __DECLARE_TRACE(name, PARAMS(proto), PARAMS(args), 1, \
  216. PARAMS(void *__data, proto), \
  217. PARAMS(__data, args))
  218. #define DECLARE_TRACE_CONDITION(name, proto, args, cond) \
  219. __DECLARE_TRACE(name, PARAMS(proto), PARAMS(args), PARAMS(cond), \
  220. PARAMS(void *__data, proto), \
  221. PARAMS(__data, args))
  222. #define TRACE_EVENT_FLAGS(event, flag)
  223. #define TRACE_EVENT_PERF_PERM(event, expr...)
  224. #endif /* DECLARE_TRACE */
  225. #ifndef TRACE_EVENT
  226. /*
  227. * For use with the TRACE_EVENT macro:
  228. *
  229. * We define a tracepoint, its arguments, its printk format
  230. * and its 'fast binary record' layout.
  231. *
  232. * Firstly, name your tracepoint via TRACE_EVENT(name : the
  233. * 'subsystem_event' notation is fine.
  234. *
  235. * Think about this whole construct as the
  236. * 'trace_sched_switch() function' from now on.
  237. *
  238. *
  239. * TRACE_EVENT(sched_switch,
  240. *
  241. * *
  242. * * A function has a regular function arguments
  243. * * prototype, declare it via TP_PROTO():
  244. * *
  245. *
  246. * TP_PROTO(struct rq *rq, struct task_struct *prev,
  247. * struct task_struct *next),
  248. *
  249. * *
  250. * * Define the call signature of the 'function'.
  251. * * (Design sidenote: we use this instead of a
  252. * * TP_PROTO1/TP_PROTO2/TP_PROTO3 ugliness.)
  253. * *
  254. *
  255. * TP_ARGS(rq, prev, next),
  256. *
  257. * *
  258. * * Fast binary tracing: define the trace record via
  259. * * TP_STRUCT__entry(). You can think about it like a
  260. * * regular C structure local variable definition.
  261. * *
  262. * * This is how the trace record is structured and will
  263. * * be saved into the ring buffer. These are the fields
  264. * * that will be exposed to user-space in
  265. * * /sys/kernel/debug/tracing/events/<*>/format.
  266. * *
  267. * * The declared 'local variable' is called '__entry'
  268. * *
  269. * * __field(pid_t, prev_prid) is equivalent to a standard declariton:
  270. * *
  271. * * pid_t prev_pid;
  272. * *
  273. * * __array(char, prev_comm, TASK_COMM_LEN) is equivalent to:
  274. * *
  275. * * char prev_comm[TASK_COMM_LEN];
  276. * *
  277. *
  278. * TP_STRUCT__entry(
  279. * __array( char, prev_comm, TASK_COMM_LEN )
  280. * __field( pid_t, prev_pid )
  281. * __field( int, prev_prio )
  282. * __array( char, next_comm, TASK_COMM_LEN )
  283. * __field( pid_t, next_pid )
  284. * __field( int, next_prio )
  285. * ),
  286. *
  287. * *
  288. * * Assign the entry into the trace record, by embedding
  289. * * a full C statement block into TP_fast_assign(). You
  290. * * can refer to the trace record as '__entry' -
  291. * * otherwise you can put arbitrary C code in here.
  292. * *
  293. * * Note: this C code will execute every time a trace event
  294. * * happens, on an active tracepoint.
  295. * *
  296. *
  297. * TP_fast_assign(
  298. * memcpy(__entry->next_comm, next->comm, TASK_COMM_LEN);
  299. * __entry->prev_pid = prev->pid;
  300. * __entry->prev_prio = prev->prio;
  301. * memcpy(__entry->prev_comm, prev->comm, TASK_COMM_LEN);
  302. * __entry->next_pid = next->pid;
  303. * __entry->next_prio = next->prio;
  304. * ),
  305. *
  306. * *
  307. * * Formatted output of a trace record via TP_printk().
  308. * * This is how the tracepoint will appear under ftrace
  309. * * plugins that make use of this tracepoint.
  310. * *
  311. * * (raw-binary tracing wont actually perform this step.)
  312. * *
  313. *
  314. * TP_printk("task %s:%d [%d] ==> %s:%d [%d]",
  315. * __entry->prev_comm, __entry->prev_pid, __entry->prev_prio,
  316. * __entry->next_comm, __entry->next_pid, __entry->next_prio),
  317. *
  318. * );
  319. *
  320. * This macro construct is thus used for the regular printk format
  321. * tracing setup, it is used to construct a function pointer based
  322. * tracepoint callback (this is used by programmatic plugins and
  323. * can also by used by generic instrumentation like SystemTap), and
  324. * it is also used to expose a structured trace record in
  325. * /sys/kernel/debug/tracing/events/.
  326. *
  327. * A set of (un)registration functions can be passed to the variant
  328. * TRACE_EVENT_FN to perform any (un)registration work.
  329. */
  330. #define DECLARE_EVENT_CLASS(name, proto, args, tstruct, assign, print)
  331. #define DEFINE_EVENT(template, name, proto, args) \
  332. DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
  333. #define DEFINE_EVENT_FN(template, name, proto, args, reg, unreg)\
  334. DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
  335. #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
  336. DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
  337. #define DEFINE_EVENT_CONDITION(template, name, proto, \
  338. args, cond) \
  339. DECLARE_TRACE_CONDITION(name, PARAMS(proto), \
  340. PARAMS(args), PARAMS(cond))
  341. #define TRACE_EVENT(name, proto, args, struct, assign, print) \
  342. DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
  343. #define TRACE_EVENT_FN(name, proto, args, struct, \
  344. assign, print, reg, unreg) \
  345. DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
  346. #define TRACE_EVENT_CONDITION(name, proto, args, cond, \
  347. struct, assign, print) \
  348. DECLARE_TRACE_CONDITION(name, PARAMS(proto), \
  349. PARAMS(args), PARAMS(cond))
  350. #define TRACE_EVENT_FLAGS(event, flag)
  351. #define TRACE_EVENT_PERF_PERM(event, expr...)
  352. #endif /* ifdef TRACE_EVENT (see note above) */