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