tracepoint.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. #define DEFINE_TRACE_FN(name, reg, unreg) \
  123. static const char __tpstrtab_##name[] \
  124. __attribute__((section("__tracepoints_strings"))) = #name; \
  125. struct tracepoint __tracepoint_##name \
  126. __attribute__((section("__tracepoints"), aligned(32))) = \
  127. { __tpstrtab_##name, 0, reg, unreg, NULL }
  128. #define DEFINE_TRACE(name) \
  129. DEFINE_TRACE_FN(name, NULL, NULL);
  130. #define EXPORT_TRACEPOINT_SYMBOL_GPL(name) \
  131. EXPORT_SYMBOL_GPL(__tracepoint_##name)
  132. #define EXPORT_TRACEPOINT_SYMBOL(name) \
  133. EXPORT_SYMBOL(__tracepoint_##name)
  134. #else /* !CONFIG_TRACEPOINTS */
  135. #define DECLARE_TRACE(name, proto, args) \
  136. static inline void _do_trace_##name(struct tracepoint *tp, proto) \
  137. { } \
  138. static inline void trace_##name(proto) \
  139. { } \
  140. static inline int register_trace_##name(void (*probe)(proto)) \
  141. { \
  142. return -ENOSYS; \
  143. } \
  144. static inline int unregister_trace_##name(void (*probe)(proto)) \
  145. { \
  146. return -ENOSYS; \
  147. }
  148. #define DEFINE_TRACE_FN(name, reg, unreg)
  149. #define DEFINE_TRACE(name)
  150. #define EXPORT_TRACEPOINT_SYMBOL_GPL(name)
  151. #define EXPORT_TRACEPOINT_SYMBOL(name)
  152. #endif /* CONFIG_TRACEPOINTS */
  153. #endif /* DECLARE_TRACE */
  154. #ifndef TRACE_EVENT
  155. /*
  156. * For use with the TRACE_EVENT macro:
  157. *
  158. * We define a tracepoint, its arguments, its printk format
  159. * and its 'fast binay record' layout.
  160. *
  161. * Firstly, name your tracepoint via TRACE_EVENT(name : the
  162. * 'subsystem_event' notation is fine.
  163. *
  164. * Think about this whole construct as the
  165. * 'trace_sched_switch() function' from now on.
  166. *
  167. *
  168. * TRACE_EVENT(sched_switch,
  169. *
  170. * *
  171. * * A function has a regular function arguments
  172. * * prototype, declare it via TP_PROTO():
  173. * *
  174. *
  175. * TP_PROTO(struct rq *rq, struct task_struct *prev,
  176. * struct task_struct *next),
  177. *
  178. * *
  179. * * Define the call signature of the 'function'.
  180. * * (Design sidenote: we use this instead of a
  181. * * TP_PROTO1/TP_PROTO2/TP_PROTO3 ugliness.)
  182. * *
  183. *
  184. * TP_ARGS(rq, prev, next),
  185. *
  186. * *
  187. * * Fast binary tracing: define the trace record via
  188. * * TP_STRUCT__entry(). You can think about it like a
  189. * * regular C structure local variable definition.
  190. * *
  191. * * This is how the trace record is structured and will
  192. * * be saved into the ring buffer. These are the fields
  193. * * that will be exposed to user-space in
  194. * * /sys/kernel/debug/tracing/events/<*>/format.
  195. * *
  196. * * The declared 'local variable' is called '__entry'
  197. * *
  198. * * __field(pid_t, prev_prid) is equivalent to a standard declariton:
  199. * *
  200. * * pid_t prev_pid;
  201. * *
  202. * * __array(char, prev_comm, TASK_COMM_LEN) is equivalent to:
  203. * *
  204. * * char prev_comm[TASK_COMM_LEN];
  205. * *
  206. *
  207. * TP_STRUCT__entry(
  208. * __array( char, prev_comm, TASK_COMM_LEN )
  209. * __field( pid_t, prev_pid )
  210. * __field( int, prev_prio )
  211. * __array( char, next_comm, TASK_COMM_LEN )
  212. * __field( pid_t, next_pid )
  213. * __field( int, next_prio )
  214. * ),
  215. *
  216. * *
  217. * * Assign the entry into the trace record, by embedding
  218. * * a full C statement block into TP_fast_assign(). You
  219. * * can refer to the trace record as '__entry' -
  220. * * otherwise you can put arbitrary C code in here.
  221. * *
  222. * * Note: this C code will execute every time a trace event
  223. * * happens, on an active tracepoint.
  224. * *
  225. *
  226. * TP_fast_assign(
  227. * memcpy(__entry->next_comm, next->comm, TASK_COMM_LEN);
  228. * __entry->prev_pid = prev->pid;
  229. * __entry->prev_prio = prev->prio;
  230. * memcpy(__entry->prev_comm, prev->comm, TASK_COMM_LEN);
  231. * __entry->next_pid = next->pid;
  232. * __entry->next_prio = next->prio;
  233. * )
  234. *
  235. * *
  236. * * Formatted output of a trace record via TP_printk().
  237. * * This is how the tracepoint will appear under ftrace
  238. * * plugins that make use of this tracepoint.
  239. * *
  240. * * (raw-binary tracing wont actually perform this step.)
  241. * *
  242. *
  243. * TP_printk("task %s:%d [%d] ==> %s:%d [%d]",
  244. * __entry->prev_comm, __entry->prev_pid, __entry->prev_prio,
  245. * __entry->next_comm, __entry->next_pid, __entry->next_prio),
  246. *
  247. * );
  248. *
  249. * This macro construct is thus used for the regular printk format
  250. * tracing setup, it is used to construct a function pointer based
  251. * tracepoint callback (this is used by programmatic plugins and
  252. * can also by used by generic instrumentation like SystemTap), and
  253. * it is also used to expose a structured trace record in
  254. * /sys/kernel/debug/tracing/events/.
  255. *
  256. * A set of (un)registration functions can be passed to the variant
  257. * TRACE_EVENT_FN to perform any (un)registration work.
  258. */
  259. #define DECLARE_EVENT_CLASS(name, proto, args, tstruct, assign, print)
  260. #define DEFINE_EVENT(template, name, proto, args) \
  261. DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
  262. #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
  263. DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
  264. #define TRACE_EVENT(name, proto, args, struct, assign, print) \
  265. DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
  266. #define TRACE_EVENT_FN(name, proto, args, struct, \
  267. assign, print, reg, unreg) \
  268. DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
  269. #endif /* ifdef TRACE_EVENT (see note above) */