tracepoint.h 11 KB

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