tracepoint.h 16 KB

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