tracepoint.h 16 KB

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