tracepoint.h 16 KB

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