tracepoint.h 17 KB

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