kprobetrace.txt 5.7 KB

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