uprobetracer.txt 6.3 KB

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