簡介
uprobe 事件tracer工具是在核心3.5開發期間何入核心主線版本的,雖然uprobe已經存在很久了。uprobe是和kprobe類似的調試方法。編譯核心時通過開啟CONFIG_UPROBE_EVENT=y來時能該特性。和kprobe類似,使用時不需要通過current_tracer來啟用,而是檢測點通過/sys/kernel/debug/tracing/uprobe_events設定,通過/sys/kernel/debug/tracing/events/uprobes/<EVENT>/enabled來使能。
然而,和kprobe不同的是,使用時需要使用者自己計算探測點在使用者態檔案中的位移,可以通過nm等工具,這還是有點麻煩的。
命令格式
p[:[GRP/]EVENT] PATH:SYMBOL[+offs] [FETCHARGS] : Set a probe
GRP : Group name. If omitted, use "uprobes" for it.
EVENT : Event name. If omitted, the event name is generated
based on SYMBOL+offs.
PATH : path to an executable or a library.
SYMBOL[+offs] : Symbol+offset where the probe is inserted.
FETCHARGS : Arguments. Each probe can have up to 128 args.
%REG : Fetch register REG
查看事件監控檔案
可以通過/sys/kernel/debug/tracing/uprobe_profile來查看某一檢測事件命中的總數和沒有命中的總數。第一列是事件名稱,第二列是事件命中的次數,第三列是事件miss-hits的次數。
使用舉例
如下定義一個新的檢測事件:
echo 'p: /bin/bash:0x4245c0' > /sys/kernel/debug/tracing/uprobe_events
這樣就在可執行檔/bin/bash的位移0x4245c0處設定了檢測點。
要清空所有的檢測點,如下:
echo > /sys/kernel/debug/tracing/uprobe_events
下面的例子給出了獲得某可執行檔符號地址的方法:
# cd /sys/kernel/debug/tracing/
# cat /proc/`pgrep bash`/maps | grep /bin/bash | grep r-xp
00400000-004e1000 r-xp 00000000 08:01 786439 /bin/bash
# objdump -T /bin/zsh | grep -w free
00000000004ab500 g DF .text 0000000000000009 Base free
0x4ab500是可執行檔/bin/zsh中哦給你free函數的位移,可以看出/bin/bash的載入地址是0x00400000,因此設定free處為檢測點的命令如下:
echo 'p /bin/bash:0x4ab500 %ip %ax' > uprobe_events
可以通過uprobe_events來查看註冊的事件:
# cat uprobe_events
p:uprobes/p_bash_0x4ab500 /bin/bash:0x00000000004ab500 arg1=%ip arg2=%ax
在正確的註冊後,每個檢測時間點事件都是禁止的,要檢測這個事件,需要手動去啟用它:
# echo 1 > events/uprobes/enable
在程式執行了一段事件後,禁止它,然後可以查看監控到的事件:
# sleep 20
# echo 0 > events/uprobes/enable
# cat trace
# tracer: nop
#
# TASK-PID CPU# TIMESTAMP FUNCTION
# | | | | |
zsh-24842 [006] 258544.995456: p_bash_0x46420: (0x446420) arg1=446421 arg2=79
zsh-24842 [007] 258545.000270: p_bash_0x46420: (0x446420) arg1=446421 arg2=79
zsh-24842 [002] 258545.043929: p_bash_0x46420: (0x446420) arg1=446421 arg2=79
zsh-24842 [004] 258547.046129: p_bash_0x46420: (0x446420) arg1=446421 arg2=79