tracepoint.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. */
  55. #define DECLARE_TRACE(name, proto, args) \
  56. extern struct tracepoint __tracepoint_##name; \
  57. static inline void trace_##name(proto) \
  58. { \
  59. if (unlikely(__tracepoint_##name.state)) \
  60. __DO_TRACE(&__tracepoint_##name, \
  61. TP_PROTO(proto), TP_ARGS(args)); \
  62. } \
  63. static inline int register_trace_##name(void (*probe)(proto)) \
  64. { \
  65. return tracepoint_probe_register(#name, (void *)probe); \
  66. } \
  67. static inline int unregister_trace_##name(void (*probe)(proto)) \
  68. { \
  69. return tracepoint_probe_unregister(#name, (void *)probe);\
  70. }
  71. #define DEFINE_TRACE(name) \
  72. static const char __tpstrtab_##name[] \
  73. __attribute__((section("__tracepoints_strings"))) = #name; \
  74. struct tracepoint __tracepoint_##name \
  75. __attribute__((section("__tracepoints"), aligned(32))) = \
  76. { __tpstrtab_##name, 0, NULL }
  77. #define EXPORT_TRACEPOINT_SYMBOL_GPL(name) \
  78. EXPORT_SYMBOL_GPL(__tracepoint_##name)
  79. #define EXPORT_TRACEPOINT_SYMBOL(name) \
  80. EXPORT_SYMBOL(__tracepoint_##name)
  81. extern void tracepoint_update_probe_range(struct tracepoint *begin,
  82. struct tracepoint *end);
  83. #else /* !CONFIG_TRACEPOINTS */
  84. #define DECLARE_TRACE(name, proto, args) \
  85. static inline void _do_trace_##name(struct tracepoint *tp, proto) \
  86. { } \
  87. static inline void trace_##name(proto) \
  88. { } \
  89. static inline int register_trace_##name(void (*probe)(proto)) \
  90. { \
  91. return -ENOSYS; \
  92. } \
  93. static inline int unregister_trace_##name(void (*probe)(proto)) \
  94. { \
  95. return -ENOSYS; \
  96. }
  97. #define DEFINE_TRACE(name)
  98. #define EXPORT_TRACEPOINT_SYMBOL_GPL(name)
  99. #define EXPORT_TRACEPOINT_SYMBOL(name)
  100. static inline void tracepoint_update_probe_range(struct tracepoint *begin,
  101. struct tracepoint *end)
  102. { }
  103. #endif /* CONFIG_TRACEPOINTS */
  104. #endif /* DECLARE_TRACE */
  105. /*
  106. * Connect a probe to a tracepoint.
  107. * Internal API, should not be used directly.
  108. */
  109. extern int tracepoint_probe_register(const char *name, void *probe);
  110. /*
  111. * Disconnect a probe from a tracepoint.
  112. * Internal API, should not be used directly.
  113. */
  114. extern int tracepoint_probe_unregister(const char *name, void *probe);
  115. extern int tracepoint_probe_register_noupdate(const char *name, void *probe);
  116. extern int tracepoint_probe_unregister_noupdate(const char *name, void *probe);
  117. extern void tracepoint_probe_update_all(void);
  118. struct tracepoint_iter {
  119. struct module *module;
  120. struct tracepoint *tracepoint;
  121. };
  122. extern void tracepoint_iter_start(struct tracepoint_iter *iter);
  123. extern void tracepoint_iter_next(struct tracepoint_iter *iter);
  124. extern void tracepoint_iter_stop(struct tracepoint_iter *iter);
  125. extern void tracepoint_iter_reset(struct tracepoint_iter *iter);
  126. extern int tracepoint_get_iter_range(struct tracepoint **tracepoint,
  127. struct tracepoint *begin, struct tracepoint *end);
  128. /*
  129. * tracepoint_synchronize_unregister must be called between the last tracepoint
  130. * probe unregistration and the end of module exit to make sure there is no
  131. * caller executing a probe when it is freed.
  132. */
  133. static inline void tracepoint_synchronize_unregister(void)
  134. {
  135. synchronize_sched();
  136. }
  137. #define PARAMS(args...) args
  138. #ifndef TRACE_EVENT
  139. /*
  140. * For use with the TRACE_EVENT macro:
  141. *
  142. * We define a tracepoint, its arguments, its printk format
  143. * and its 'fast binay record' layout.
  144. *
  145. * Firstly, name your tracepoint via TRACE_EVENT(name : the
  146. * 'subsystem_event' notation is fine.
  147. *
  148. * Think about this whole construct as the
  149. * 'trace_sched_switch() function' from now on.
  150. *
  151. *
  152. * TRACE_EVENT(sched_switch,
  153. *
  154. * *
  155. * * A function has a regular function arguments
  156. * * prototype, declare it via TP_PROTO():
  157. * *
  158. *
  159. * TP_PROTO(struct rq *rq, struct task_struct *prev,
  160. * struct task_struct *next),
  161. *
  162. * *
  163. * * Define the call signature of the 'function'.
  164. * * (Design sidenote: we use this instead of a
  165. * * TP_PROTO1/TP_PROTO2/TP_PROTO3 ugliness.)
  166. * *
  167. *
  168. * TP_ARGS(rq, prev, next),
  169. *
  170. * *
  171. * * Fast binary tracing: define the trace record via
  172. * * TP_STRUCT__entry(). You can think about it like a
  173. * * regular C structure local variable definition.
  174. * *
  175. * * This is how the trace record is structured and will
  176. * * be saved into the ring buffer. These are the fields
  177. * * that will be exposed to user-space in
  178. * * /debug/tracing/events/<*>/format.
  179. * *
  180. * * The declared 'local variable' is called '__entry'
  181. * *
  182. * * __field(pid_t, prev_prid) is equivalent to a standard declariton:
  183. * *
  184. * * pid_t prev_pid;
  185. * *
  186. * * __array(char, prev_comm, TASK_COMM_LEN) is equivalent to:
  187. * *
  188. * * char prev_comm[TASK_COMM_LEN];
  189. * *
  190. *
  191. * TP_STRUCT__entry(
  192. * __array( char, prev_comm, TASK_COMM_LEN )
  193. * __field( pid_t, prev_pid )
  194. * __field( int, prev_prio )
  195. * __array( char, next_comm, TASK_COMM_LEN )
  196. * __field( pid_t, next_pid )
  197. * __field( int, next_prio )
  198. * ),
  199. *
  200. * *
  201. * * Assign the entry into the trace record, by embedding
  202. * * a full C statement block into TP_fast_assign(). You
  203. * * can refer to the trace record as '__entry' -
  204. * * otherwise you can put arbitrary C code in here.
  205. * *
  206. * * Note: this C code will execute every time a trace event
  207. * * happens, on an active tracepoint.
  208. * *
  209. *
  210. * TP_fast_assign(
  211. * memcpy(__entry->next_comm, next->comm, TASK_COMM_LEN);
  212. * __entry->prev_pid = prev->pid;
  213. * __entry->prev_prio = prev->prio;
  214. * memcpy(__entry->prev_comm, prev->comm, TASK_COMM_LEN);
  215. * __entry->next_pid = next->pid;
  216. * __entry->next_prio = next->prio;
  217. * )
  218. *
  219. * *
  220. * * Formatted output of a trace record via TP_printk().
  221. * * This is how the tracepoint will appear under ftrace
  222. * * plugins that make use of this tracepoint.
  223. * *
  224. * * (raw-binary tracing wont actually perform this step.)
  225. * *
  226. *
  227. * TP_printk("task %s:%d [%d] ==> %s:%d [%d]",
  228. * __entry->prev_comm, __entry->prev_pid, __entry->prev_prio,
  229. * __entry->next_comm, __entry->next_pid, __entry->next_prio),
  230. *
  231. * );
  232. *
  233. * This macro construct is thus used for the regular printk format
  234. * tracing setup, it is used to construct a function pointer based
  235. * tracepoint callback (this is used by programmatic plugins and
  236. * can also by used by generic instrumentation like SystemTap), and
  237. * it is also used to expose a structured trace record in
  238. * /debug/tracing/events/.
  239. */
  240. #define TRACE_EVENT(name, proto, args, struct, assign, print) \
  241. DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
  242. #endif
  243. #endif