tracepoint.h 11 KB

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