tracepoints.txt 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. Using the Linux Kernel Tracepoints
  2. Mathieu Desnoyers
  3. This document introduces Linux Kernel Tracepoints and their use. It
  4. provides examples of how to insert tracepoints in the kernel and
  5. connect probe functions to them and provides some examples of probe
  6. functions.
  7. * Purpose of tracepoints
  8. A tracepoint placed in code provides a hook to call a function (probe)
  9. that you can provide at runtime. A tracepoint can be "on" (a probe is
  10. connected to it) or "off" (no probe is attached). When a tracepoint is
  11. "off" it has no effect, except for adding a tiny time penalty
  12. (checking a condition for a branch) and space penalty (adding a few
  13. bytes for the function call at the end of the instrumented function
  14. and adds a data structure in a separate section). When a tracepoint
  15. is "on", the function you provide is called each time the tracepoint
  16. is executed, in the execution context of the caller. When the function
  17. provided ends its execution, it returns to the caller (continuing from
  18. the tracepoint site).
  19. You can put tracepoints at important locations in the code. They are
  20. lightweight hooks that can pass an arbitrary number of parameters,
  21. which prototypes are described in a tracepoint declaration placed in a
  22. header file.
  23. They can be used for tracing and performance accounting.
  24. * Usage
  25. Two elements are required for tracepoints :
  26. - A tracepoint definition, placed in a header file.
  27. - The tracepoint statement, in C code.
  28. In order to use tracepoints, you should include linux/tracepoint.h.
  29. In include/trace/subsys.h :
  30. #include <linux/tracepoint.h>
  31. DECLARE_TRACE(subsys_eventname,
  32. TP_PROTO(int firstarg, struct task_struct *p),
  33. TP_ARGS(firstarg, p));
  34. In subsys/file.c (where the tracing statement must be added) :
  35. #include <trace/subsys.h>
  36. DEFINE_TRACE(subsys_eventname);
  37. void somefct(void)
  38. {
  39. ...
  40. trace_subsys_eventname(arg, task);
  41. ...
  42. }
  43. Where :
  44. - subsys_eventname is an identifier unique to your event
  45. - subsys is the name of your subsystem.
  46. - eventname is the name of the event to trace.
  47. - TP_PROTO(int firstarg, struct task_struct *p) is the prototype of the
  48. function called by this tracepoint.
  49. - TP_ARGS(firstarg, p) are the parameters names, same as found in the
  50. prototype.
  51. Connecting a function (probe) to a tracepoint is done by providing a
  52. probe (function to call) for the specific tracepoint through
  53. register_trace_subsys_eventname(). Removing a probe is done through
  54. unregister_trace_subsys_eventname(); it will remove the probe.
  55. tracepoint_synchronize_unregister() must be called before the end of
  56. the module exit function to make sure there is no caller left using
  57. the probe. This, and the fact that preemption is disabled around the
  58. probe call, make sure that probe removal and module unload are safe.
  59. See the "Probe example" section below for a sample probe module.
  60. The tracepoint mechanism supports inserting multiple instances of the
  61. same tracepoint, but a single definition must be made of a given
  62. tracepoint name over all the kernel to make sure no type conflict will
  63. occur. Name mangling of the tracepoints is done using the prototypes
  64. to make sure typing is correct. Verification of probe type correctness
  65. is done at the registration site by the compiler. Tracepoints can be
  66. put in inline functions, inlined static functions, and unrolled loops
  67. as well as regular functions.
  68. The naming scheme "subsys_event" is suggested here as a convention
  69. intended to limit collisions. Tracepoint names are global to the
  70. kernel: they are considered as being the same whether they are in the
  71. core kernel image or in modules.
  72. If the tracepoint has to be used in kernel modules, an
  73. EXPORT_TRACEPOINT_SYMBOL_GPL() or EXPORT_TRACEPOINT_SYMBOL() can be
  74. used to export the defined tracepoints.
  75. * Probe / tracepoint example
  76. See the example provided in samples/tracepoints
  77. Compile them with your kernel. They are built during 'make' (not
  78. 'make modules') when CONFIG_SAMPLE_TRACEPOINTS=m.
  79. Run, as root :
  80. modprobe tracepoint-sample (insmod order is not important)
  81. modprobe tracepoint-probe-sample
  82. cat /proc/tracepoint-sample (returns an expected error)
  83. rmmod tracepoint-sample tracepoint-probe-sample
  84. dmesg