tracepoint.h 7.8 KB

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