tracepoint.h 15 KB

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