tracepoints.rst 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. ==================================
  2. Using the Linux Kernel Tracepoints
  3. ==================================
  4. :Author: Mathieu Desnoyers
  5. This document introduces Linux Kernel Tracepoints and their use. It
  6. provides examples of how to insert tracepoints in the kernel and
  7. connect probe functions to them and provides some examples of probe
  8. functions.
  9. Purpose of tracepoints
  10. ----------------------
  11. A tracepoint placed in code provides a hook to call a function (probe)
  12. that you can provide at runtime. A tracepoint can be "on" (a probe is
  13. connected to it) or "off" (no probe is attached). When a tracepoint is
  14. "off" it has no effect, except for adding a tiny time penalty
  15. (checking a condition for a branch) and space penalty (adding a few
  16. bytes for the function call at the end of the instrumented function
  17. and adds a data structure in a separate section). When a tracepoint
  18. is "on", the function you provide is called each time the tracepoint
  19. is executed, in the execution context of the caller. When the function
  20. provided ends its execution, it returns to the caller (continuing from
  21. the tracepoint site).
  22. You can put tracepoints at important locations in the code. They are
  23. lightweight hooks that can pass an arbitrary number of parameters,
  24. which prototypes are described in a tracepoint declaration placed in a
  25. header file.
  26. They can be used for tracing and performance accounting.
  27. Usage
  28. -----
  29. Two elements are required for tracepoints :
  30. - A tracepoint definition, placed in a header file.
  31. - The tracepoint statement, in C code.
  32. In order to use tracepoints, you should include linux/tracepoint.h.
  33. In include/trace/events/subsys.h::
  34. #undef TRACE_SYSTEM
  35. #define TRACE_SYSTEM subsys
  36. #if !defined(_TRACE_SUBSYS_H) || defined(TRACE_HEADER_MULTI_READ)
  37. #define _TRACE_SUBSYS_H
  38. #include <linux/tracepoint.h>
  39. DECLARE_TRACE(subsys_eventname,
  40. TP_PROTO(int firstarg, struct task_struct *p),
  41. TP_ARGS(firstarg, p));
  42. #endif /* _TRACE_SUBSYS_H */
  43. /* This part must be outside protection */
  44. #include <trace/define_trace.h>
  45. In subsys/file.c (where the tracing statement must be added)::
  46. #include <trace/events/subsys.h>
  47. #define CREATE_TRACE_POINTS
  48. DEFINE_TRACE(subsys_eventname);
  49. void somefct(void)
  50. {
  51. ...
  52. trace_subsys_eventname(arg, task);
  53. ...
  54. }
  55. Where :
  56. - subsys_eventname is an identifier unique to your event
  57. - subsys is the name of your subsystem.
  58. - eventname is the name of the event to trace.
  59. - `TP_PROTO(int firstarg, struct task_struct *p)` is the prototype of the
  60. function called by this tracepoint.
  61. - `TP_ARGS(firstarg, p)` are the parameters names, same as found in the
  62. prototype.
  63. - if you use the header in multiple source files, `#define CREATE_TRACE_POINTS`
  64. should appear only in one source file.
  65. Connecting a function (probe) to a tracepoint is done by providing a
  66. probe (function to call) for the specific tracepoint through
  67. register_trace_subsys_eventname(). Removing a probe is done through
  68. unregister_trace_subsys_eventname(); it will remove the probe.
  69. tracepoint_synchronize_unregister() must be called before the end of
  70. the module exit function to make sure there is no caller left using
  71. the probe. This, and the fact that preemption is disabled around the
  72. probe call, make sure that probe removal and module unload are safe.
  73. The tracepoint mechanism supports inserting multiple instances of the
  74. same tracepoint, but a single definition must be made of a given
  75. tracepoint name over all the kernel to make sure no type conflict will
  76. occur. Name mangling of the tracepoints is done using the prototypes
  77. to make sure typing is correct. Verification of probe type correctness
  78. is done at the registration site by the compiler. Tracepoints can be
  79. put in inline functions, inlined static functions, and unrolled loops
  80. as well as regular functions.
  81. The naming scheme "subsys_event" is suggested here as a convention
  82. intended to limit collisions. Tracepoint names are global to the
  83. kernel: they are considered as being the same whether they are in the
  84. core kernel image or in modules.
  85. If the tracepoint has to be used in kernel modules, an
  86. EXPORT_TRACEPOINT_SYMBOL_GPL() or EXPORT_TRACEPOINT_SYMBOL() can be
  87. used to export the defined tracepoints.
  88. If you need to do a bit of work for a tracepoint parameter, and
  89. that work is only used for the tracepoint, that work can be encapsulated
  90. within an if statement with the following::
  91. if (trace_foo_bar_enabled()) {
  92. int i;
  93. int tot = 0;
  94. for (i = 0; i < count; i++)
  95. tot += calculate_nuggets();
  96. trace_foo_bar(tot);
  97. }
  98. All trace_<tracepoint>() calls have a matching trace_<tracepoint>_enabled()
  99. function defined that returns true if the tracepoint is enabled and
  100. false otherwise. The trace_<tracepoint>() should always be within the
  101. block of the if (trace_<tracepoint>_enabled()) to prevent races between
  102. the tracepoint being enabled and the check being seen.
  103. The advantage of using the trace_<tracepoint>_enabled() is that it uses
  104. the static_key of the tracepoint to allow the if statement to be implemented
  105. with jump labels and avoid conditional branches.
  106. .. note:: The convenience macro TRACE_EVENT provides an alternative way to
  107. define tracepoints. Check http://lwn.net/Articles/379903,
  108. http://lwn.net/Articles/381064 and http://lwn.net/Articles/383362
  109. for a series of articles with more details.
  110. If you require calling a tracepoint from a header file, it is not
  111. recommended to call one directly or to use the trace_<tracepoint>_enabled()
  112. function call, as tracepoints in header files can have side effects if a
  113. header is included from a file that has CREATE_TRACE_POINTS set, as
  114. well as the trace_<tracepoint>() is not that small of an inline
  115. and can bloat the kernel if used by other inlined functions. Instead,
  116. include tracepoint-defs.h and use tracepoint_enabled().
  117. In a C file::
  118. void do_trace_foo_bar_wrapper(args)
  119. {
  120. trace_foo_bar(args);
  121. }
  122. In the header file::
  123. DECLARE_TRACEPOINT(foo_bar);
  124. static inline void some_inline_function()
  125. {
  126. [..]
  127. if (tracepoint_enabled(foo_bar))
  128. do_trace_foo_bar_wrapper(args);
  129. [..]
  130. }