uprobetracer.txt 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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:OFFSET [FETCHARGS] : Set a uprobe
  17. r[:[GRP/]EVENT] PATH:OFFSET [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 PATH+OFFSET.
  22. PATH : Path to an executable or a library.
  23. OFFSET : Offset where the probe is inserted.
  24. FETCHARGS : Arguments. Each probe can have up to 128 args.
  25. %REG : Fetch register REG
  26. @ADDR : Fetch memory at ADDR (ADDR should be in userspace)
  27. @+OFFSET : Fetch memory at OFFSET (OFFSET from same file as PATH)
  28. $stackN : Fetch Nth entry of stack (N >= 0)
  29. $stack : Fetch stack address.
  30. $retval : Fetch return value.(*)
  31. $comm : Fetch current task comm.
  32. +|-offs(FETCHARG) : Fetch memory at FETCHARG +|- offs address.(**)
  33. NAME=FETCHARG : Set NAME as the argument name of FETCHARG.
  34. FETCHARG:TYPE : Set TYPE as the type of FETCHARG. Currently, basic types
  35. (u8/u16/u32/u64/s8/s16/s32/s64), hexadecimal types
  36. (x8/x16/x32/x64), "string" and bitfield are supported.
  37. (*) only for return probe.
  38. (**) this is useful for fetching a field of data structures.
  39. Types
  40. -----
  41. Several types are supported for fetch-args. Uprobe tracer will access memory
  42. by given type. Prefix 's' and 'u' means those types are signed and unsigned
  43. respectively. 'x' prefix implies it is unsigned. Traced arguments are shown
  44. in decimal ('s' and 'u') or hexadecimal ('x'). Without type casting, 'x32'
  45. or 'x64' is used depends on the architecture (e.g. x86-32 uses x32, and
  46. x86-64 uses x64).
  47. String type is a special type, which fetches a "null-terminated" string from
  48. user space.
  49. Bitfield is another special type, which takes 3 parameters, bit-width, bit-
  50. offset, and container-size (usually 32). The syntax is;
  51. b<bit-width>@<bit-offset>/<container-size>
  52. For $comm, the default type is "string"; any other type is invalid.
  53. Event Profiling
  54. ---------------
  55. You can check the total number of probe hits and probe miss-hits via
  56. /sys/kernel/debug/tracing/uprobe_profile.
  57. The first column is event name, the second is the number of probe hits,
  58. the third is the number of probe miss-hits.
  59. Usage examples
  60. --------------
  61. * Add a probe as a new uprobe event, write a new definition to uprobe_events
  62. as below: (sets a uprobe at an offset of 0x4245c0 in the executable /bin/bash)
  63. echo 'p: /bin/bash:0x4245c0' > /sys/kernel/debug/tracing/uprobe_events
  64. * Add a probe as a new uretprobe event:
  65. echo 'r: /bin/bash:0x4245c0' > /sys/kernel/debug/tracing/uprobe_events
  66. * Unset registered event:
  67. echo '-:bash_0x4245c0' >> /sys/kernel/debug/tracing/uprobe_events
  68. * Print out the events that are registered:
  69. cat /sys/kernel/debug/tracing/uprobe_events
  70. * Clear all events:
  71. echo > /sys/kernel/debug/tracing/uprobe_events
  72. Following example shows how to dump the instruction pointer and %ax register
  73. at the probed text address. Probe zfree function in /bin/zsh:
  74. # cd /sys/kernel/debug/tracing/
  75. # cat /proc/`pgrep zsh`/maps | grep /bin/zsh | grep r-xp
  76. 00400000-0048a000 r-xp 00000000 08:03 130904 /bin/zsh
  77. # objdump -T /bin/zsh | grep -w zfree
  78. 0000000000446420 g DF .text 0000000000000012 Base zfree
  79. 0x46420 is the offset of zfree in object /bin/zsh that is loaded at
  80. 0x00400000. Hence the command to uprobe would be:
  81. # echo 'p:zfree_entry /bin/zsh:0x46420 %ip %ax' > uprobe_events
  82. And the same for the uretprobe would be:
  83. # echo 'r:zfree_exit /bin/zsh:0x46420 %ip %ax' >> uprobe_events
  84. Please note: User has to explicitly calculate the offset of the probe-point
  85. in the object. We can see the events that are registered by looking at the
  86. uprobe_events file.
  87. # cat uprobe_events
  88. p:uprobes/zfree_entry /bin/zsh:0x00046420 arg1=%ip arg2=%ax
  89. r:uprobes/zfree_exit /bin/zsh:0x00046420 arg1=%ip arg2=%ax
  90. Format of events can be seen by viewing the file events/uprobes/zfree_entry/format
  91. # cat events/uprobes/zfree_entry/format
  92. name: zfree_entry
  93. ID: 922
  94. format:
  95. field:unsigned short common_type; offset:0; size:2; signed:0;
  96. field:unsigned char common_flags; offset:2; size:1; signed:0;
  97. field:unsigned char common_preempt_count; offset:3; size:1; signed:0;
  98. field:int common_pid; offset:4; size:4; signed:1;
  99. field:int common_padding; offset:8; size:4; signed:1;
  100. field:unsigned long __probe_ip; offset:12; size:4; signed:0;
  101. field:u32 arg1; offset:16; size:4; signed:0;
  102. field:u32 arg2; offset:20; size:4; signed:0;
  103. print fmt: "(%lx) arg1=%lx arg2=%lx", REC->__probe_ip, REC->arg1, REC->arg2
  104. Right after definition, each event is disabled by default. For tracing these
  105. events, you need to enable it by:
  106. # echo 1 > events/uprobes/enable
  107. Lets disable the event after sleeping for some time.
  108. # sleep 20
  109. # echo 0 > events/uprobes/enable
  110. And you can see the traced information via /sys/kernel/debug/tracing/trace.
  111. # cat trace
  112. # tracer: nop
  113. #
  114. # TASK-PID CPU# TIMESTAMP FUNCTION
  115. # | | | | |
  116. zsh-24842 [006] 258544.995456: zfree_entry: (0x446420) arg1=446420 arg2=79
  117. zsh-24842 [007] 258545.000270: zfree_exit: (0x446540 <- 0x446420) arg1=446540 arg2=0
  118. zsh-24842 [002] 258545.043929: zfree_entry: (0x446420) arg1=446420 arg2=79
  119. zsh-24842 [004] 258547.046129: zfree_exit: (0x446540 <- 0x446420) arg1=446540 arg2=0
  120. Output shows us uprobe was triggered for a pid 24842 with ip being 0x446420
  121. and contents of ax register being 79. And uretprobe was triggered with ip at
  122. 0x446540 with counterpart function entry at 0x446420.