tracepoint.h 9.1 KB

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