uprobetracer.txt 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. Uprobe-tracer: Uprobe-based Event Tracing
  2. =========================================
  3. Documentation written by Srikar Dronamraju
  4. Overview
  5. --------
  6. Uprobe based trace events are similar to kprobe based trace events.
  7. To enable this feature, build your kernel with CONFIG_UPROBE_EVENT=y.
  8. Similar to the kprobe-event tracer, this doesn't need to be activated via
  9. current_tracer. Instead of that, add probe points via
  10. /sys/kernel/debug/tracing/uprobe_events, and enable it via
  11. /sys/kernel/debug/tracing/events/uprobes/<EVENT>/enabled.
  12. However unlike kprobe-event tracer, the uprobe event interface expects the
  13. user to calculate the offset of the probepoint in the object.
  14. Synopsis of uprobe_tracer
  15. -------------------------
  16. p[:[GRP/]EVENT] PATH:SYMBOL[+offs] [FETCHARGS] : Set a uprobe
  17. r[:[GRP/]EVENT] PATH:SYMBOL[+offs] [FETCHARGS] : Set a return uprobe (uretprobe)
  18. -:[GRP/]EVENT : Clear uprobe or uretprobe event
  19. GRP : Group name. If omitted, "uprobes" is the default value.
  20. EVENT : Event name. If omitted, the event name is generated based
  21. on SYMBOL+offs.
  22. PATH : Path to an executable or a library.
  23. SYMBOL[+offs] : Symbol+offset where the probe is inserted.
  24. FETCHARGS : Arguments. Each probe can have up to 128 args.
  25. %REG : Fetch register REG
  26. Event Profiling
  27. ---------------
  28. You can check the total number of probe hits and probe miss-hits via
  29. /sys/kernel/debug/tracing/uprobe_profile.
  30. The first column is event name, the second is the number of probe hits,
  31. the third is the number of probe miss-hits.
  32. Usage examples
  33. --------------
  34. * Add a probe as a new uprobe event, write a new definition to uprobe_events
  35. as below: (sets a uprobe at an offset of 0x4245c0 in the executable /bin/bash)
  36. echo 'p: /bin/bash:0x4245c0' > /sys/kernel/debug/tracing/uprobe_events
  37. * Add a probe as a new uretprobe event:
  38. echo 'r: /bin/bash:0x4245c0' > /sys/kernel/debug/tracing/uprobe_events
  39. * Unset registered event:
  40. echo '-:bash_0x4245c0' >> /sys/kernel/debug/tracing/uprobe_events
  41. * Print out the events that are registered:
  42. cat /sys/kernel/debug/tracing/uprobe_events
  43. * Clear all events:
  44. echo > /sys/kernel/debug/tracing/uprobe_events
  45. Following example shows how to dump the instruction pointer and %ax register
  46. at the probed text address. Probe zfree function in /bin/zsh:
  47. # cd /sys/kernel/debug/tracing/
  48. # cat /proc/`pgrep zsh`/maps | grep /bin/zsh | grep r-xp
  49. 00400000-0048a000 r-xp 00000000 08:03 130904 /bin/zsh
  50. # objdump -T /bin/zsh | grep -w zfree
  51. 0000000000446420 g DF .text 0000000000000012 Base zfree
  52. 0x46420 is the offset of zfree in object /bin/zsh that is loaded at
  53. 0x00400000. Hence the command to uprobe would be:
  54. # echo 'p:zfree_entry /bin/zsh:0x46420 %ip %ax' > uprobe_events
  55. And the same for the uretprobe would be:
  56. # echo 'r:zfree_exit /bin/zsh:0x46420 %ip %ax' >> uprobe_events
  57. Please note: User has to explicitly calculate the offset of the probe-point
  58. in the object. We can see the events that are registered by looking at the
  59. uprobe_events file.
  60. # cat uprobe_events
  61. p:uprobes/zfree_entry /bin/zsh:0x00046420 arg1=%ip arg2=%ax
  62. r:uprobes/zfree_exit /bin/zsh:0x00046420 arg1=%ip arg2=%ax
  63. Format of events can be seen by viewing the file events/uprobes/zfree_entry/format
  64. # cat events/uprobes/zfree_entry/format
  65. name: zfree_entry
  66. ID: 922
  67. format:
  68. field:unsigned short common_type; offset:0; size:2; signed:0;
  69. field:unsigned char common_flags; offset:2; size:1; signed:0;
  70. field:unsigned char common_preempt_count; offset:3; size:1; signed:0;
  71. field:int common_pid; offset:4; size:4; signed:1;
  72. field:int common_padding; offset:8; size:4; signed:1;
  73. field:unsigned long __probe_ip; offset:12; size:4; signed:0;
  74. field:u32 arg1; offset:16; size:4; signed:0;
  75. field:u32 arg2; offset:20; size:4; signed:0;
  76. print fmt: "(%lx) arg1=%lx arg2=%lx", REC->__probe_ip, REC->arg1, REC->arg2
  77. Right after definition, each event is disabled by default. For tracing these
  78. events, you need to enable it by:
  79. # echo 1 > events/uprobes/enable
  80. Lets disable the event after sleeping for some time.
  81. # sleep 20
  82. # echo 0 > events/uprobes/enable
  83. And you can see the traced information via /sys/kernel/debug/tracing/trace.
  84. # cat trace
  85. # tracer: nop
  86. #
  87. # TASK-PID CPU# TIMESTAMP FUNCTION
  88. # | | | | |
  89. zsh-24842 [006] 258544.995456: zfree_entry: (0x446420) arg1=446420 arg2=79
  90. zsh-24842 [007] 258545.000270: zfree_exit: (0x446540 <- 0x446420) arg1=446540 arg2=0
  91. zsh-24842 [002] 258545.043929: zfree_entry: (0x446420) arg1=446420 arg2=79
  92. zsh-24842 [004] 258547.046129: zfree_exit: (0x446540 <- 0x446420) arg1=446540 arg2=0
  93. Output shows us uprobe was triggered for a pid 24842 with ip being 0x446420
  94. and contents of ax register being 79. And uretprobe was triggered with ip at
  95. 0x446540 with counterpart function entry at 0x446420.