kprobetrace.txt 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. Kprobe-based Event Tracer
  2. =========================
  3. Documentation is written by Masami Hiramatsu
  4. Overview
  5. --------
  6. This tracer is similar to the events tracer which is based on Tracepoint
  7. infrastructure. Instead of Tracepoint, this tracer is based on kprobes(kprobe
  8. and kretprobe). It probes anywhere where kprobes can probe(this means, all
  9. functions body except for __kprobes functions).
  10. Unlike the function tracer, this tracer can probe instructions inside of
  11. kernel functions. It allows you to check which instruction has been executed.
  12. Unlike the Tracepoint based events tracer, this tracer can add and remove
  13. probe points on the fly.
  14. Similar to the events tracer, this tracer doesn't need to be activated via
  15. current_tracer, instead of that, just set probe points via
  16. /sys/kernel/debug/tracing/kprobe_events. And you can set filters on each
  17. probe events via /sys/kernel/debug/tracing/events/kprobes/<EVENT>/filter.
  18. Synopsis of kprobe_events
  19. -------------------------
  20. p[:EVENT] SYMBOL[+offs|-offs]|MEMADDR [FETCHARGS] : Set a probe
  21. r[:EVENT] SYMBOL[+0] [FETCHARGS] : Set a return probe
  22. EVENT : Event name. If omitted, the event name is generated
  23. based on SYMBOL+offs or MEMADDR.
  24. SYMBOL[+offs|-offs] : Symbol+offset where the probe is inserted.
  25. MEMADDR : Address where the probe is inserted.
  26. FETCHARGS : Arguments. Each probe can have up to 128 args.
  27. %REG : Fetch register REG
  28. sN : Fetch Nth entry of stack (N >= 0)
  29. sa : Fetch stack address.
  30. @ADDR : Fetch memory at ADDR (ADDR should be in kernel)
  31. @SYM[+|-offs] : Fetch memory at SYM +|- offs (SYM should be a data symbol)
  32. aN : Fetch function argument. (N >= 0)(*)
  33. rv : Fetch return value.(**)
  34. ra : Fetch return address.(**)
  35. +|-offs(FETCHARG) : fetch memory at FETCHARG +|- offs address.(***)
  36. (*) aN may not correct on asmlinkaged functions and at the middle of
  37. function body.
  38. (**) only for return probe.
  39. (***) this is useful for fetching a field of data structures.
  40. Per-Probe Event Filtering
  41. -------------------------
  42. Per-probe event filtering feature allows you to set different filter on each
  43. probe and gives you what arguments will be shown in trace buffer. If an event
  44. name is specified right after 'p:' or 'r:' in kprobe_events, the tracer adds
  45. an event under tracing/events/kprobes/<EVENT>, at the directory you can see
  46. 'id', 'enabled', 'format' and 'filter'.
  47. enabled:
  48. You can enable/disable the probe by writing 1 or 0 on it.
  49. format:
  50. It shows the format of this probe event. It also shows aliases of arguments
  51. which you specified to kprobe_events.
  52. filter:
  53. You can write filtering rules of this event. And you can use both of aliase
  54. names and field names for describing filters.
  55. Event Profiling
  56. ---------------
  57. You can check the total number of probe hits and probe miss-hits via
  58. /sys/kernel/debug/tracing/kprobe_profile.
  59. The first column is event name, the second is the number of probe hits,
  60. the third is the number of probe miss-hits.
  61. Usage examples
  62. --------------
  63. To add a probe as a new event, write a new definition to kprobe_events
  64. as below.
  65. echo p:myprobe do_sys_open a0 a1 a2 a3 > /sys/kernel/debug/tracing/kprobe_events
  66. This sets a kprobe on the top of do_sys_open() function with recording
  67. 1st to 4th arguments as "myprobe" event.
  68. echo r:myretprobe do_sys_open rv ra >> /sys/kernel/debug/tracing/kprobe_events
  69. This sets a kretprobe on the return point of do_sys_open() function with
  70. recording return value and return address as "myretprobe" event.
  71. You can see the format of these events via
  72. /sys/kernel/debug/tracing/events/kprobes/<EVENT>/format.
  73. cat /sys/kernel/debug/tracing/events/kprobes/myprobe/format
  74. name: myprobe
  75. ID: 23
  76. format:
  77. field:unsigned short common_type; offset:0; size:2;
  78. field:unsigned char common_flags; offset:2; size:1;
  79. field:unsigned char common_preempt_count; offset:3; size:1;
  80. field:int common_pid; offset:4; size:4;
  81. field:int common_tgid; offset:8; size:4;
  82. field: unsigned long ip; offset:16;tsize:8;
  83. field: int nargs; offset:24;tsize:4;
  84. field: unsigned long arg0; offset:32;tsize:8;
  85. field: unsigned long arg1; offset:40;tsize:8;
  86. field: unsigned long arg2; offset:48;tsize:8;
  87. field: unsigned long arg3; offset:56;tsize:8;
  88. alias: a0; original: arg0;
  89. alias: a1; original: arg1;
  90. alias: a2; original: arg2;
  91. alias: a3; original: arg3;
  92. print fmt: "%lx: 0x%lx 0x%lx 0x%lx 0x%lx", ip, arg0, arg1, arg2, arg3
  93. You can see that the event has 4 arguments and alias expressions
  94. corresponding to it.
  95. echo > /sys/kernel/debug/tracing/kprobe_events
  96. This clears all probe points. and you can see the traced information via
  97. /sys/kernel/debug/tracing/trace.
  98. cat /sys/kernel/debug/tracing/trace
  99. # tracer: nop
  100. #
  101. # TASK-PID CPU# TIMESTAMP FUNCTION
  102. # | | | | |
  103. <...>-1447 [001] 1038282.286875: do_sys_open+0x0/0xd6: 0x3 0x7fffd1ec4440 0x8000 0x0
  104. <...>-1447 [001] 1038282.286878: sys_openat+0xc/0xe <- do_sys_open: 0xfffffffffffffffe 0xffffffff81367a3a
  105. <...>-1447 [001] 1038282.286885: do_sys_open+0x0/0xd6: 0xffffff9c 0x40413c 0x8000 0x1b6
  106. <...>-1447 [001] 1038282.286915: sys_open+0x1b/0x1d <- do_sys_open: 0x3 0xffffffff81367a3a
  107. <...>-1447 [001] 1038282.286969: do_sys_open+0x0/0xd6: 0xffffff9c 0x4041c6 0x98800 0x10
  108. <...>-1447 [001] 1038282.286976: sys_open+0x1b/0x1d <- do_sys_open: 0x3 0xffffffff81367a3a
  109. Each line shows when the kernel hits a probe, and <- SYMBOL means kernel
  110. returns from SYMBOL(e.g. "sys_open+0x1b/0x1d <- do_sys_open" means kernel
  111. returns from do_sys_open to sys_open+0x1b).