kprobetrace.txt 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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[:[GRP/]EVENT] SYMBOL[+offs]|MEMADDR [FETCHARGS] : Set a probe
  21. r[:[GRP/]EVENT] SYMBOL[+0] [FETCHARGS] : Set a return probe
  22. GRP : Group name. If omitted, use "kprobes" for it.
  23. EVENT : Event name. If omitted, the event name is generated
  24. based on SYMBOL+offs or MEMADDR.
  25. SYMBOL[+offs] : Symbol+offset where the probe is inserted.
  26. MEMADDR : Address where the probe is inserted.
  27. FETCHARGS : Arguments. Each probe can have up to 128 args.
  28. %REG : Fetch register REG
  29. @ADDR : Fetch memory at ADDR (ADDR should be in kernel)
  30. @SYM[+|-offs] : Fetch memory at SYM +|- offs (SYM should be a data symbol)
  31. $sN : Fetch Nth entry of stack (N >= 0)
  32. $sa : Fetch stack address.
  33. $aN : Fetch function argument. (N >= 0)(*)
  34. $rv : Fetch return value.(**)
  35. $ra : Fetch return address.(**)
  36. +|-offs(FETCHARG) : Fetch memory at FETCHARG +|- offs address.(***)
  37. NAME=FETCHARG: Set NAME as the argument name of FETCHARG.
  38. (*) aN may not correct on asmlinkaged functions and at the middle of
  39. function body.
  40. (**) only for return probe.
  41. (***) this is useful for fetching a field of data structures.
  42. Per-Probe Event Filtering
  43. -------------------------
  44. Per-probe event filtering feature allows you to set different filter on each
  45. probe and gives you what arguments will be shown in trace buffer. If an event
  46. name is specified right after 'p:' or 'r:' in kprobe_events, the tracer adds
  47. an event under tracing/events/kprobes/<EVENT>, at the directory you can see
  48. 'id', 'enabled', 'format' and 'filter'.
  49. enabled:
  50. You can enable/disable the probe by writing 1 or 0 on it.
  51. format:
  52. This shows the format of this probe event.
  53. filter:
  54. You can write filtering rules of this event.
  55. id:
  56. This shows the id of this probe event.
  57. Event Profiling
  58. ---------------
  59. You can check the total number of probe hits and probe miss-hits via
  60. /sys/kernel/debug/tracing/kprobe_profile.
  61. The first column is event name, the second is the number of probe hits,
  62. the third is the number of probe miss-hits.
  63. Usage examples
  64. --------------
  65. To add a probe as a new event, write a new definition to kprobe_events
  66. as below.
  67. echo p:myprobe do_sys_open dfd=$a0 filename=$a1 flags=$a2 mode=$a3 > /sys/kernel/debug/tracing/kprobe_events
  68. This sets a kprobe on the top of do_sys_open() function with recording
  69. 1st to 4th arguments as "myprobe" event. As this example shows, users can
  70. choose more familiar names for each arguments.
  71. echo r:myretprobe do_sys_open $rv $ra >> /sys/kernel/debug/tracing/kprobe_events
  72. This sets a kretprobe on the return point of do_sys_open() function with
  73. recording return value and return address as "myretprobe" event.
  74. You can see the format of these events via
  75. /sys/kernel/debug/tracing/events/kprobes/<EVENT>/format.
  76. cat /sys/kernel/debug/tracing/events/kprobes/myprobe/format
  77. name: myprobe
  78. ID: 75
  79. format:
  80. field:unsigned short common_type; offset:0; size:2;
  81. field:unsigned char common_flags; offset:2; size:1;
  82. field:unsigned char common_preempt_count; offset:3; size:1;
  83. field:int common_pid; offset:4; size:4;
  84. field:int common_tgid; offset:8; size:4;
  85. field: unsigned long ip; offset:16;tsize:8;
  86. field: int nargs; offset:24;tsize:4;
  87. field: unsigned long dfd; offset:32;tsize:8;
  88. field: unsigned long filename; offset:40;tsize:8;
  89. field: unsigned long flags; offset:48;tsize:8;
  90. field: unsigned long mode; offset:56;tsize:8;
  91. print fmt: "(%lx) dfd=%lx filename=%lx flags=%lx mode=%lx", REC->ip, REC->dfd, REC->filename, REC->flags, REC->mode
  92. You can see that the event has 4 arguments as in the expressions you specified.
  93. echo > /sys/kernel/debug/tracing/kprobe_events
  94. This clears all probe points.
  95. Right after definition, each event is disabled by default. For tracing these
  96. events, you need to enable it.
  97. echo 1 > /sys/kernel/debug/tracing/events/kprobes/myprobe/enable
  98. echo 1 > /sys/kernel/debug/tracing/events/kprobes/myretprobe/enable
  99. And you can see the traced information via /sys/kernel/debug/tracing/trace.
  100. cat /sys/kernel/debug/tracing/trace
  101. # tracer: nop
  102. #
  103. # TASK-PID CPU# TIMESTAMP FUNCTION
  104. # | | | | |
  105. <...>-1447 [001] 1038282.286875: myprobe: (do_sys_open+0x0/0xd6) dfd=3 filename=7fffd1ec4440 flags=8000 mode=0
  106. <...>-1447 [001] 1038282.286878: myretprobe: (sys_openat+0xc/0xe <- do_sys_open) $rv=fffffffffffffffe $ra=ffffffff81367a3a
  107. <...>-1447 [001] 1038282.286885: myprobe: (do_sys_open+0x0/0xd6) dfd=ffffff9c filename=40413c flags=8000 mode=1b6
  108. <...>-1447 [001] 1038282.286915: myretprobe: (sys_open+0x1b/0x1d <- do_sys_open) $rv=3 $ra=ffffffff81367a3a
  109. <...>-1447 [001] 1038282.286969: myprobe: (do_sys_open+0x0/0xd6) dfd=ffffff9c filename=4041c6 flags=98800 mode=10
  110. <...>-1447 [001] 1038282.286976: myretprobe: (sys_open+0x1b/0x1d <- do_sys_open) $rv=3 $ra=ffffffff81367a3a
  111. Each line shows when the kernel hits an event, and <- SYMBOL means kernel
  112. returns from SYMBOL(e.g. "sys_open+0x1b/0x1d <- do_sys_open" means kernel
  113. returns from do_sys_open to sys_open+0x1b).