uprobetracer.rst 6.6 KB

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