tracepoint.h 17 KB

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