tracepoint.h 12 KB

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