0001 =========================================
0002 Uprobe-tracer: Uprobe-based Event Tracing
0003 =========================================
0004
0005 :Author: Srikar Dronamraju
0006
0007
0008 Overview
0009 --------
0010 Uprobe based trace events are similar to kprobe based trace events.
0011 To enable this feature, build your kernel with CONFIG_UPROBE_EVENTS=y.
0012
0013 Similar to the kprobe-event tracer, this doesn't need to be activated via
0014 current_tracer. Instead of that, add probe points via
0015 /sys/kernel/debug/tracing/uprobe_events, and enable it via
0016 /sys/kernel/debug/tracing/events/uprobes/<EVENT>/enable.
0017
0018 However unlike kprobe-event tracer, the uprobe event interface expects the
0019 user to calculate the offset of the probepoint in the object.
0020
0021 You can also use /sys/kernel/debug/tracing/dynamic_events instead of
0022 uprobe_events. That interface will provide unified access to other
0023 dynamic events too.
0024
0025 Synopsis of uprobe_tracer
0026 -------------------------
0027 ::
0028
0029 p[:[GRP/][EVENT]] PATH:OFFSET [FETCHARGS] : Set a uprobe
0030 r[:[GRP/][EVENT]] PATH:OFFSET [FETCHARGS] : Set a return uprobe (uretprobe)
0031 p[:[GRP/][EVENT]] PATH:OFFSET%return [FETCHARGS] : Set a return uprobe (uretprobe)
0032 -:[GRP/][EVENT] : Clear uprobe or uretprobe event
0033
0034 GRP : Group name. If omitted, "uprobes" is the default value.
0035 EVENT : Event name. If omitted, the event name is generated based
0036 on PATH+OFFSET.
0037 PATH : Path to an executable or a library.
0038 OFFSET : Offset where the probe is inserted.
0039 OFFSET%return : Offset where the return probe is inserted.
0040
0041 FETCHARGS : Arguments. Each probe can have up to 128 args.
0042 %REG : Fetch register REG
0043 @ADDR : Fetch memory at ADDR (ADDR should be in userspace)
0044 @+OFFSET : Fetch memory at OFFSET (OFFSET from same file as PATH)
0045 $stackN : Fetch Nth entry of stack (N >= 0)
0046 $stack : Fetch stack address.
0047 $retval : Fetch return value.(\*1)
0048 $comm : Fetch current task comm.
0049 +|-[u]OFFS(FETCHARG) : Fetch memory at FETCHARG +|- OFFS address.(\*2)(\*3)
0050 \IMM : Store an immediate value to the argument.
0051 NAME=FETCHARG : Set NAME as the argument name of FETCHARG.
0052 FETCHARG:TYPE : Set TYPE as the type of FETCHARG. Currently, basic types
0053 (u8/u16/u32/u64/s8/s16/s32/s64), hexadecimal types
0054 (x8/x16/x32/x64), "string" and bitfield are supported.
0055
0056 (\*1) only for return probe.
0057 (\*2) this is useful for fetching a field of data structures.
0058 (\*3) Unlike kprobe event, "u" prefix will just be ignored, becuse uprobe
0059 events can access only user-space memory.
0060
0061 Types
0062 -----
0063 Several types are supported for fetch-args. Uprobe tracer will access memory
0064 by given type. Prefix 's' and 'u' means those types are signed and unsigned
0065 respectively. 'x' prefix implies it is unsigned. Traced arguments are shown
0066 in decimal ('s' and 'u') or hexadecimal ('x'). Without type casting, 'x32'
0067 or 'x64' is used depends on the architecture (e.g. x86-32 uses x32, and
0068 x86-64 uses x64).
0069 String type is a special type, which fetches a "null-terminated" string from
0070 user space.
0071 Bitfield is another special type, which takes 3 parameters, bit-width, bit-
0072 offset, and container-size (usually 32). The syntax is::
0073
0074 b<bit-width>@<bit-offset>/<container-size>
0075
0076 For $comm, the default type is "string"; any other type is invalid.
0077
0078
0079 Event Profiling
0080 ---------------
0081 You can check the total number of probe hits per event via
0082 /sys/kernel/debug/tracing/uprobe_profile. The first column is the filename,
0083 the second is the event name, the third is the number of probe hits.
0084
0085 Usage examples
0086 --------------
0087 * Add a probe as a new uprobe event, write a new definition to uprobe_events
0088 as below (sets a uprobe at an offset of 0x4245c0 in the executable /bin/bash)::
0089
0090 echo 'p /bin/bash:0x4245c0' > /sys/kernel/debug/tracing/uprobe_events
0091
0092 * Add a probe as a new uretprobe event::
0093
0094 echo 'r /bin/bash:0x4245c0' > /sys/kernel/debug/tracing/uprobe_events
0095
0096 * Unset registered event::
0097
0098 echo '-:p_bash_0x4245c0' >> /sys/kernel/debug/tracing/uprobe_events
0099
0100 * Print out the events that are registered::
0101
0102 cat /sys/kernel/debug/tracing/uprobe_events
0103
0104 * Clear all events::
0105
0106 echo > /sys/kernel/debug/tracing/uprobe_events
0107
0108 Following example shows how to dump the instruction pointer and %ax register
0109 at the probed text address. Probe zfree function in /bin/zsh::
0110
0111 # cd /sys/kernel/debug/tracing/
0112 # cat /proc/`pgrep zsh`/maps | grep /bin/zsh | grep r-xp
0113 00400000-0048a000 r-xp 00000000 08:03 130904 /bin/zsh
0114 # objdump -T /bin/zsh | grep -w zfree
0115 0000000000446420 g DF .text 0000000000000012 Base zfree
0116
0117 0x46420 is the offset of zfree in object /bin/zsh that is loaded at
0118 0x00400000. Hence the command to uprobe would be::
0119
0120 # echo 'p:zfree_entry /bin/zsh:0x46420 %ip %ax' > uprobe_events
0121
0122 And the same for the uretprobe would be::
0123
0124 # echo 'r:zfree_exit /bin/zsh:0x46420 %ip %ax' >> uprobe_events
0125
0126 .. note:: User has to explicitly calculate the offset of the probe-point
0127 in the object.
0128
0129 We can see the events that are registered by looking at the uprobe_events file.
0130 ::
0131
0132 # cat uprobe_events
0133 p:uprobes/zfree_entry /bin/zsh:0x00046420 arg1=%ip arg2=%ax
0134 r:uprobes/zfree_exit /bin/zsh:0x00046420 arg1=%ip arg2=%ax
0135
0136 Format of events can be seen by viewing the file events/uprobes/zfree_entry/format.
0137 ::
0138
0139 # cat events/uprobes/zfree_entry/format
0140 name: zfree_entry
0141 ID: 922
0142 format:
0143 field:unsigned short common_type; offset:0; size:2; signed:0;
0144 field:unsigned char common_flags; offset:2; size:1; signed:0;
0145 field:unsigned char common_preempt_count; offset:3; size:1; signed:0;
0146 field:int common_pid; offset:4; size:4; signed:1;
0147 field:int common_padding; offset:8; size:4; signed:1;
0148
0149 field:unsigned long __probe_ip; offset:12; size:4; signed:0;
0150 field:u32 arg1; offset:16; size:4; signed:0;
0151 field:u32 arg2; offset:20; size:4; signed:0;
0152
0153 print fmt: "(%lx) arg1=%lx arg2=%lx", REC->__probe_ip, REC->arg1, REC->arg2
0154
0155 Right after definition, each event is disabled by default. For tracing these
0156 events, you need to enable it by::
0157
0158 # echo 1 > events/uprobes/enable
0159
0160 Lets start tracing, sleep for some time and stop tracing.
0161 ::
0162
0163 # echo 1 > tracing_on
0164 # sleep 20
0165 # echo 0 > tracing_on
0166
0167 Also, you can disable the event by::
0168
0169 # echo 0 > events/uprobes/enable
0170
0171 And you can see the traced information via /sys/kernel/debug/tracing/trace.
0172 ::
0173
0174 # cat trace
0175 # tracer: nop
0176 #
0177 # TASK-PID CPU# TIMESTAMP FUNCTION
0178 # | | | | |
0179 zsh-24842 [006] 258544.995456: zfree_entry: (0x446420) arg1=446420 arg2=79
0180 zsh-24842 [007] 258545.000270: zfree_exit: (0x446540 <- 0x446420) arg1=446540 arg2=0
0181 zsh-24842 [002] 258545.043929: zfree_entry: (0x446420) arg1=446420 arg2=79
0182 zsh-24842 [004] 258547.046129: zfree_exit: (0x446540 <- 0x446420) arg1=446540 arg2=0
0183
0184 Output shows us uprobe was triggered for a pid 24842 with ip being 0x446420
0185 and contents of ax register being 79. And uretprobe was triggered with ip at
0186 0x446540 with counterpart function entry at 0x446420.