tracepoint.h 8.8 KB

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