tracepoint.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. #ifndef _LINUX_TRACEPOINT_H
  2. #define _LINUX_TRACEPOINT_H
  3. /*
  4. * Kernel Tracepoint API.
  5. *
  6. * See Documentation/trace/tracepoints.txt.
  7. *
  8. * Copyright (C) 2008-2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
  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/errno.h>
  16. #include <linux/types.h>
  17. #include <linux/rcupdate.h>
  18. #include <linux/static_key.h>
  19. struct module;
  20. struct tracepoint;
  21. struct notifier_block;
  22. struct tracepoint_func {
  23. void *func;
  24. void *data;
  25. };
  26. struct tracepoint {
  27. const char *name; /* Tracepoint name */
  28. struct static_key key;
  29. void (*regfunc)(void);
  30. void (*unregfunc)(void);
  31. struct tracepoint_func __rcu *funcs;
  32. };
  33. extern int
  34. tracepoint_probe_register(struct tracepoint *tp, void *probe, void *data);
  35. extern int
  36. tracepoint_probe_unregister(struct tracepoint *tp, void *probe, void *data);
  37. extern void
  38. for_each_kernel_tracepoint(void (*fct)(struct tracepoint *tp, void *priv),
  39. void *priv);
  40. #ifdef CONFIG_MODULES
  41. struct tp_module {
  42. struct list_head list;
  43. struct module *mod;
  44. };
  45. bool trace_module_has_bad_taint(struct module *mod);
  46. extern int register_tracepoint_module_notifier(struct notifier_block *nb);
  47. extern int unregister_tracepoint_module_notifier(struct notifier_block *nb);
  48. #else
  49. static inline bool trace_module_has_bad_taint(struct module *mod)
  50. {
  51. return false;
  52. }
  53. static inline
  54. int register_tracepoint_module_notifier(struct notifier_block *nb)
  55. {
  56. return 0;
  57. }
  58. static inline
  59. int unregister_tracepoint_module_notifier(struct notifier_block *nb)
  60. {
  61. return 0;
  62. }
  63. #endif /* CONFIG_MODULES */
  64. /*
  65. * tracepoint_synchronize_unregister must be called between the last tracepoint
  66. * probe unregistration and the end of module exit to make sure there is no
  67. * caller executing a probe when it is freed.
  68. */
  69. static inline void tracepoint_synchronize_unregister(void)
  70. {
  71. synchronize_sched();
  72. }
  73. #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
  74. extern void syscall_regfunc(void);
  75. extern void syscall_unregfunc(void);
  76. #endif /* CONFIG_HAVE_SYSCALL_TRACEPOINTS */
  77. #define PARAMS(args...) args
  78. #endif /* _LINUX_TRACEPOINT_H */
  79. /*
  80. * Note: we keep the TRACE_EVENT and DECLARE_TRACE outside the include
  81. * file ifdef protection.
  82. * This is due to the way trace events work. If a file includes two
  83. * trace event headers under one "CREATE_TRACE_POINTS" the first include
  84. * will override the TRACE_EVENT and break the second include.
  85. */
  86. #ifndef DECLARE_TRACE
  87. #define TP_PROTO(args...) args
  88. #define TP_ARGS(args...) args
  89. #define TP_CONDITION(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, cond, prercu, postrcu) \
  102. do { \
  103. struct tracepoint_func *it_func_ptr; \
  104. void *it_func; \
  105. void *__data; \
  106. \
  107. if (!(cond)) \
  108. return; \
  109. prercu; \
  110. rcu_read_lock_sched_notrace(); \
  111. it_func_ptr = rcu_dereference_sched((tp)->funcs); \
  112. if (it_func_ptr) { \
  113. do { \
  114. it_func = (it_func_ptr)->func; \
  115. __data = (it_func_ptr)->data; \
  116. ((void(*)(proto))(it_func))(args); \
  117. } while ((++it_func_ptr)->func); \
  118. } \
  119. rcu_read_unlock_sched_notrace(); \
  120. postrcu; \
  121. } while (0)
  122. #ifndef MODULE
  123. #define __DECLARE_TRACE_RCU(name, proto, args, cond, data_proto, data_args) \
  124. static inline void trace_##name##_rcuidle(proto) \
  125. { \
  126. if (static_key_false(&__tracepoint_##name.key)) \
  127. __DO_TRACE(&__tracepoint_##name, \
  128. TP_PROTO(data_proto), \
  129. TP_ARGS(data_args), \
  130. TP_CONDITION(cond), \
  131. rcu_irq_enter(), \
  132. rcu_irq_exit()); \
  133. }
  134. #else
  135. #define __DECLARE_TRACE_RCU(name, proto, args, cond, data_proto, data_args)
  136. #endif
  137. /*
  138. * Make sure the alignment of the structure in the __tracepoints section will
  139. * not add unwanted padding between the beginning of the section and the
  140. * structure. Force alignment to the same alignment as the section start.
  141. */
  142. #define __DECLARE_TRACE(name, proto, args, cond, data_proto, data_args) \
  143. extern struct tracepoint __tracepoint_##name; \
  144. static inline void trace_##name(proto) \
  145. { \
  146. if (static_key_false(&__tracepoint_##name.key)) \
  147. __DO_TRACE(&__tracepoint_##name, \
  148. TP_PROTO(data_proto), \
  149. TP_ARGS(data_args), \
  150. TP_CONDITION(cond),,); \
  151. } \
  152. __DECLARE_TRACE_RCU(name, PARAMS(proto), PARAMS(args), \
  153. PARAMS(cond), PARAMS(data_proto), PARAMS(data_args)) \
  154. static inline int \
  155. register_trace_##name(void (*probe)(data_proto), void *data) \
  156. { \
  157. return tracepoint_probe_register(&__tracepoint_##name, \
  158. (void *)probe, data); \
  159. } \
  160. static inline int \
  161. unregister_trace_##name(void (*probe)(data_proto), void *data) \
  162. { \
  163. return tracepoint_probe_unregister(&__tracepoint_##name,\
  164. (void *)probe, data); \
  165. } \
  166. static inline void \
  167. check_trace_callback_type_##name(void (*cb)(data_proto)) \
  168. { \
  169. } \
  170. static inline bool \
  171. trace_##name##_enabled(void) \
  172. { \
  173. return static_key_false(&__tracepoint_##name.key); \
  174. }
  175. /*
  176. * We have no guarantee that gcc and the linker won't up-align the tracepoint
  177. * structures, so we create an array of pointers that will be used for iteration
  178. * on the tracepoints.
  179. */
  180. #define DEFINE_TRACE_FN(name, reg, unreg) \
  181. static const char __tpstrtab_##name[] \
  182. __attribute__((section("__tracepoints_strings"))) = #name; \
  183. struct tracepoint __tracepoint_##name \
  184. __attribute__((section("__tracepoints"))) = \
  185. { __tpstrtab_##name, STATIC_KEY_INIT_FALSE, reg, unreg, NULL };\
  186. static struct tracepoint * const __tracepoint_ptr_##name __used \
  187. __attribute__((section("__tracepoints_ptrs"))) = \
  188. &__tracepoint_##name;
  189. #define DEFINE_TRACE(name) \
  190. DEFINE_TRACE_FN(name, NULL, NULL);
  191. #define EXPORT_TRACEPOINT_SYMBOL_GPL(name) \
  192. EXPORT_SYMBOL_GPL(__tracepoint_##name)
  193. #define EXPORT_TRACEPOINT_SYMBOL(name) \
  194. EXPORT_SYMBOL(__tracepoint_##name)
  195. #else /* !CONFIG_TRACEPOINTS */
  196. #define __DECLARE_TRACE(name, proto, args, cond, data_proto, data_args) \
  197. static inline void trace_##name(proto) \
  198. { } \
  199. static inline void trace_##name##_rcuidle(proto) \
  200. { } \
  201. static inline int \
  202. register_trace_##name(void (*probe)(data_proto), \
  203. void *data) \
  204. { \
  205. return -ENOSYS; \
  206. } \
  207. static inline int \
  208. unregister_trace_##name(void (*probe)(data_proto), \
  209. void *data) \
  210. { \
  211. return -ENOSYS; \
  212. } \
  213. static inline void check_trace_callback_type_##name(void (*cb)(data_proto)) \
  214. { \
  215. } \
  216. static inline bool \
  217. trace_##name##_enabled(void) \
  218. { \
  219. return false; \
  220. }
  221. #define DEFINE_TRACE_FN(name, reg, unreg)
  222. #define DEFINE_TRACE(name)
  223. #define EXPORT_TRACEPOINT_SYMBOL_GPL(name)
  224. #define EXPORT_TRACEPOINT_SYMBOL(name)
  225. #endif /* CONFIG_TRACEPOINTS */
  226. #ifdef CONFIG_TRACING
  227. /**
  228. * tracepoint_string - register constant persistent string to trace system
  229. * @str - a constant persistent string that will be referenced in tracepoints
  230. *
  231. * If constant strings are being used in tracepoints, it is faster and
  232. * more efficient to just save the pointer to the string and reference
  233. * that with a printf "%s" instead of saving the string in the ring buffer
  234. * and wasting space and time.
  235. *
  236. * The problem with the above approach is that userspace tools that read
  237. * the binary output of the trace buffers do not have access to the string.
  238. * Instead they just show the address of the string which is not very
  239. * useful to users.
  240. *
  241. * With tracepoint_string(), the string will be registered to the tracing
  242. * system and exported to userspace via the debugfs/tracing/printk_formats
  243. * file that maps the string address to the string text. This way userspace
  244. * tools that read the binary buffers have a way to map the pointers to
  245. * the ASCII strings they represent.
  246. *
  247. * The @str used must be a constant string and persistent as it would not
  248. * make sense to show a string that no longer exists. But it is still fine
  249. * to be used with modules, because when modules are unloaded, if they
  250. * had tracepoints, the ring buffers are cleared too. As long as the string
  251. * does not change during the life of the module, it is fine to use
  252. * tracepoint_string() within a module.
  253. */
  254. #define tracepoint_string(str) \
  255. ({ \
  256. static const char *___tp_str __tracepoint_string = str; \
  257. ___tp_str; \
  258. })
  259. #define __tracepoint_string __attribute__((section("__tracepoint_str")))
  260. #else
  261. /*
  262. * tracepoint_string() is used to save the string address for userspace
  263. * tracing tools. When tracing isn't configured, there's no need to save
  264. * anything.
  265. */
  266. # define tracepoint_string(str) str
  267. # define __tracepoint_string
  268. #endif
  269. /*
  270. * The need for the DECLARE_TRACE_NOARGS() is to handle the prototype
  271. * (void). "void" is a special value in a function prototype and can
  272. * not be combined with other arguments. Since the DECLARE_TRACE()
  273. * macro adds a data element at the beginning of the prototype,
  274. * we need a way to differentiate "(void *data, proto)" from
  275. * "(void *data, void)". The second prototype is invalid.
  276. *
  277. * DECLARE_TRACE_NOARGS() passes "void" as the tracepoint prototype
  278. * and "void *__data" as the callback prototype.
  279. *
  280. * DECLARE_TRACE() passes "proto" as the tracepoint protoype and
  281. * "void *__data, proto" as the callback prototype.
  282. */
  283. #define DECLARE_TRACE_NOARGS(name) \
  284. __DECLARE_TRACE(name, void, , 1, void *__data, __data)
  285. #define DECLARE_TRACE(name, proto, args) \
  286. __DECLARE_TRACE(name, PARAMS(proto), PARAMS(args), 1, \
  287. PARAMS(void *__data, proto), \
  288. PARAMS(__data, args))
  289. #define DECLARE_TRACE_CONDITION(name, proto, args, cond) \
  290. __DECLARE_TRACE(name, PARAMS(proto), PARAMS(args), PARAMS(cond), \
  291. PARAMS(void *__data, proto), \
  292. PARAMS(__data, args))
  293. #define TRACE_EVENT_FLAGS(event, flag)
  294. #define TRACE_EVENT_PERF_PERM(event, expr...)
  295. #endif /* DECLARE_TRACE */
  296. #ifndef TRACE_EVENT
  297. /*
  298. * For use with the TRACE_EVENT macro:
  299. *
  300. * We define a tracepoint, its arguments, its printk format
  301. * and its 'fast binary record' layout.
  302. *
  303. * Firstly, name your tracepoint via TRACE_EVENT(name : the
  304. * 'subsystem_event' notation is fine.
  305. *
  306. * Think about this whole construct as the
  307. * 'trace_sched_switch() function' from now on.
  308. *
  309. *
  310. * TRACE_EVENT(sched_switch,
  311. *
  312. * *
  313. * * A function has a regular function arguments
  314. * * prototype, declare it via TP_PROTO():
  315. * *
  316. *
  317. * TP_PROTO(struct rq *rq, struct task_struct *prev,
  318. * struct task_struct *next),
  319. *
  320. * *
  321. * * Define the call signature of the 'function'.
  322. * * (Design sidenote: we use this instead of a
  323. * * TP_PROTO1/TP_PROTO2/TP_PROTO3 ugliness.)
  324. * *
  325. *
  326. * TP_ARGS(rq, prev, next),
  327. *
  328. * *
  329. * * Fast binary tracing: define the trace record via
  330. * * TP_STRUCT__entry(). You can think about it like a
  331. * * regular C structure local variable definition.
  332. * *
  333. * * This is how the trace record is structured and will
  334. * * be saved into the ring buffer. These are the fields
  335. * * that will be exposed to user-space in
  336. * * /sys/kernel/debug/tracing/events/<*>/format.
  337. * *
  338. * * The declared 'local variable' is called '__entry'
  339. * *
  340. * * __field(pid_t, prev_prid) is equivalent to a standard declariton:
  341. * *
  342. * * pid_t prev_pid;
  343. * *
  344. * * __array(char, prev_comm, TASK_COMM_LEN) is equivalent to:
  345. * *
  346. * * char prev_comm[TASK_COMM_LEN];
  347. * *
  348. *
  349. * TP_STRUCT__entry(
  350. * __array( char, prev_comm, TASK_COMM_LEN )
  351. * __field( pid_t, prev_pid )
  352. * __field( int, prev_prio )
  353. * __array( char, next_comm, TASK_COMM_LEN )
  354. * __field( pid_t, next_pid )
  355. * __field( int, next_prio )
  356. * ),
  357. *
  358. * *
  359. * * Assign the entry into the trace record, by embedding
  360. * * a full C statement block into TP_fast_assign(). You
  361. * * can refer to the trace record as '__entry' -
  362. * * otherwise you can put arbitrary C code in here.
  363. * *
  364. * * Note: this C code will execute every time a trace event
  365. * * happens, on an active tracepoint.
  366. * *
  367. *
  368. * TP_fast_assign(
  369. * memcpy(__entry->next_comm, next->comm, TASK_COMM_LEN);
  370. * __entry->prev_pid = prev->pid;
  371. * __entry->prev_prio = prev->prio;
  372. * memcpy(__entry->prev_comm, prev->comm, TASK_COMM_LEN);
  373. * __entry->next_pid = next->pid;
  374. * __entry->next_prio = next->prio;
  375. * ),
  376. *
  377. * *
  378. * * Formatted output of a trace record via TP_printk().
  379. * * This is how the tracepoint will appear under ftrace
  380. * * plugins that make use of this tracepoint.
  381. * *
  382. * * (raw-binary tracing wont actually perform this step.)
  383. * *
  384. *
  385. * TP_printk("task %s:%d [%d] ==> %s:%d [%d]",
  386. * __entry->prev_comm, __entry->prev_pid, __entry->prev_prio,
  387. * __entry->next_comm, __entry->next_pid, __entry->next_prio),
  388. *
  389. * );
  390. *
  391. * This macro construct is thus used for the regular printk format
  392. * tracing setup, it is used to construct a function pointer based
  393. * tracepoint callback (this is used by programmatic plugins and
  394. * can also by used by generic instrumentation like SystemTap), and
  395. * it is also used to expose a structured trace record in
  396. * /sys/kernel/debug/tracing/events/.
  397. *
  398. * A set of (un)registration functions can be passed to the variant
  399. * TRACE_EVENT_FN to perform any (un)registration work.
  400. */
  401. #define DECLARE_EVENT_CLASS(name, proto, args, tstruct, assign, print)
  402. #define DEFINE_EVENT(template, name, proto, args) \
  403. DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
  404. #define DEFINE_EVENT_FN(template, name, proto, args, reg, unreg)\
  405. DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
  406. #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
  407. DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
  408. #define DEFINE_EVENT_CONDITION(template, name, proto, \
  409. args, cond) \
  410. DECLARE_TRACE_CONDITION(name, PARAMS(proto), \
  411. PARAMS(args), PARAMS(cond))
  412. #define TRACE_EVENT(name, proto, args, struct, assign, print) \
  413. DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
  414. #define TRACE_EVENT_FN(name, proto, args, struct, \
  415. assign, print, reg, unreg) \
  416. DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
  417. #define TRACE_EVENT_CONDITION(name, proto, args, cond, \
  418. struct, assign, print) \
  419. DECLARE_TRACE_CONDITION(name, PARAMS(proto), \
  420. PARAMS(args), PARAMS(cond))
  421. #define TRACE_EVENT_FLAGS(event, flag)
  422. #define TRACE_EVENT_PERF_PERM(event, expr...)
  423. #endif /* ifdef TRACE_EVENT (see note above) */