tracepoint.h 16 KB

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