tracepoints.txt 3.7 KB

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