kprobetrace.txt 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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.
  23. SYMBOL[+offs|-offs] : Symbol+offset where the probe is inserted.
  24. MEMADDR : Address where the probe is inserted.
  25. FETCHARGS : Arguments.
  26. %REG : Fetch register REG
  27. sN : Fetch Nth entry of stack (N >= 0)
  28. sa : Fetch stack address.
  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. aN : Fetch function argument. (N >= 0)(*)
  32. rv : Fetch return value.(**)
  33. ra : Fetch return address.(**)
  34. +|-offs(FETCHARG) : fetch memory at FETCHARG +|- offs address.(***)
  35. (*) aN may not correct on asmlinkaged functions and at the middle of
  36. function body.
  37. (**) only for return probe.
  38. (***) this is useful for fetching a field of data structures.
  39. Per-Probe Event Filtering
  40. -------------------------
  41. Per-probe event filtering feature allows you to set different filter on each
  42. probe and gives you what arguments will be shown in trace buffer. If an event
  43. name is specified right after 'p:' or 'r:' in kprobe_events, the tracer adds
  44. an event under tracing/events/kprobes/<EVENT>, at the directory you can see
  45. 'id', 'enabled', 'format' and 'filter'.
  46. enabled:
  47. You can enable/disable the probe by writing 1 or 0 on it.
  48. format:
  49. It shows the format of this probe event. It also shows aliases of arguments
  50. which you specified to kprobe_events.
  51. filter:
  52. You can write filtering rules of this event. And you can use both of aliase
  53. names and field names for describing filters.
  54. Usage examples
  55. --------------
  56. To add a probe as a new event, write a new definition to kprobe_events
  57. as below.
  58. echo p:myprobe do_sys_open a0 a1 a2 a3 > /sys/kernel/debug/tracing/kprobe_events
  59. This sets a kprobe on the top of do_sys_open() function with recording
  60. 1st to 4th arguments as "myprobe" event.
  61. echo r:myretprobe do_sys_open rv ra >> /sys/kernel/debug/tracing/kprobe_events
  62. This sets a kretprobe on the return point of do_sys_open() function with
  63. recording return value and return address as "myretprobe" event.
  64. You can see the format of these events via
  65. /sys/kernel/debug/tracing/events/kprobes/<EVENT>/format.
  66. cat /sys/kernel/debug/tracing/events/kprobes/myprobe/format
  67. name: myprobe
  68. ID: 23
  69. format:
  70. field:unsigned short common_type; offset:0; size:2;
  71. field:unsigned char common_flags; offset:2; size:1;
  72. field:unsigned char common_preempt_count; offset:3; size:1;
  73. field:int common_pid; offset:4; size:4;
  74. field:int common_tgid; offset:8; size:4;
  75. field: unsigned long ip; offset:16;tsize:8;
  76. field: int nargs; offset:24;tsize:4;
  77. field: unsigned long arg0; offset:32;tsize:8;
  78. field: unsigned long arg1; offset:40;tsize:8;
  79. field: unsigned long arg2; offset:48;tsize:8;
  80. field: unsigned long arg3; offset:56;tsize:8;
  81. alias: a0; original: arg0;
  82. alias: a1; original: arg1;
  83. alias: a2; original: arg2;
  84. alias: a3; original: arg3;
  85. print fmt: "%lx: 0x%lx 0x%lx 0x%lx 0x%lx", ip, arg0, arg1, arg2, arg3
  86. You can see that the event has 4 arguments and alias expressions
  87. corresponding to it.
  88. echo > /sys/kernel/debug/tracing/kprobe_events
  89. This clears all probe points. and you can see the traced information via
  90. /sys/kernel/debug/tracing/trace.
  91. cat /sys/kernel/debug/tracing/trace
  92. # tracer: nop
  93. #
  94. # TASK-PID CPU# TIMESTAMP FUNCTION
  95. # | | | | |
  96. <...>-1447 [001] 1038282.286875: do_sys_open+0x0/0xd6: 0x3 0x7fffd1ec4440 0x8000 0x0
  97. <...>-1447 [001] 1038282.286878: sys_openat+0xc/0xe <- do_sys_open: 0xfffffffffffffffe 0xffffffff81367a3a
  98. <...>-1447 [001] 1038282.286885: do_sys_open+0x0/0xd6: 0xffffff9c 0x40413c 0x8000 0x1b6
  99. <...>-1447 [001] 1038282.286915: sys_open+0x1b/0x1d <- do_sys_open: 0x3 0xffffffff81367a3a
  100. <...>-1447 [001] 1038282.286969: do_sys_open+0x0/0xd6: 0xffffff9c 0x4041c6 0x98800 0x10
  101. <...>-1447 [001] 1038282.286976: sys_open+0x1b/0x1d <- do_sys_open: 0x3 0xffffffff81367a3a
  102. Each line shows when the kernel hits a probe, and <- SYMBOL means kernel
  103. returns from SYMBOL(e.g. "sys_open+0x1b/0x1d <- do_sys_open" means kernel
  104. returns from do_sys_open to sys_open+0x1b).