kprobe与perf probe使用简单记录

  • Post author:
  • Post category:其他



kprobe事件添加格式:

p[:[GRP/]EVENT] [MOD:]SYM[+offs]|MEMADDR [FETCHARGS] –增加探测点

r[:[GRP/]EVENT] [MOD:]SYM[+0] [FETCHARGS] –增加return探测点

-:[GRP/]EVENT –删除探测点


各字段说明:

GRP : Group name. If omitted, use “kprobes” for it. –指定后会在events/kprobes目录下生成对应名字的目录,一般不设

EVENT : Event name. If omitted, the event name is generated?based on SYM+offs or MEMADDR. –指定后会在events/kprobes/<GRP>目录下生成对应名字的目录

MOD : Module name which has given SYM. –模块名,一般不设

SYM[+offs] : Symbol+offset where the probe is inserted. –指定被探测函数和偏移

MEMADDR : Address where the probe is inserted. –指定被探测的内存绝对地址

FETCHARGS : Arguments. Each probe can have up to 128 args. –指定要获取的参数信息

%REG : Fetch register REG –获取指定寄存器值

@ADDR : Fetch memory at ADDR (ADDR should be in kernel) –获取指定内存地址的值

@SYM[+|-offs] : Fetch memory at SYM +|- offs (SYM should be a data symbol) ?–获取全局变量的值

$stackN : Fetch Nth entry of stack (N >= 0) –获取指定栈空间值,即sp寄存器+N后的位置值

$stack : Fetch stack address. –获取sp寄存器值

$retval : Fetch return value.(*) –获取返回值,仅用于return probe

+|-offs(FETCHARG) : Fetch memory at FETCHARG +|- offs address.(**) –以下可以由于获取指定地址的结构体参数内容,可以设定具体的参数名和偏移地址

NAME=FETCHARG : Set NAME as the argument name of FETCHARG.

FETCHARG:TYPE : Set TYPE as the type of FETCHARG. Currently, basic types (u8/u16/u32/u64/s8/s16/s32/s64), “string” and bit field are supported.


使用例子:


切换目录:

[root@localhost ~]# cd /sys/kernel/debug/tracing

添加返回事件探测点:

[root@localhost tracing]# echo ‘r:myretprobe acl_permission_check ret=$retval’ >> kprobe_events

查看输出格式:

[root@localhost tracing]# cat events/kprobes/myretprobe/format

启用跟踪:

[root@localhost tracing]# echo 1 > events/kprobes/myretprobe/enable

执行相关命令

停止跟踪:

[root@localhost tracing]# echo 0 > events/kprobes/myretprobe/enable

查看结果:

[root@localhost tracing]# cat trace

删除探测点:

[root@localhost tracing]# echo ‘-:myretprobe’ >> kprobe_events

通过以上方法添加事件探测点后,也可以通过perf probe查到相关事件,并可以跟踪和移除该事件:

[root@localhost ~]# perf probe –list

/sys/kernel/debug/tracing/uprobe_events file does not exist – please rebuild kernel with CONFIG_UPROBE_EVENTS.

kprobes:myretprobe (on acl_permission_check%return with ret)

跟踪:

[root@localhost ~]# perf record -e kprobes:myretprobe -aR

获得结果:

[root@localhost ~]# perf script

移除事件:

root@localhost ~]# perf probe -d kprobes:myretprobe


PS:在生产系统中使用perf probe时居然不能添加return探测点(测试系统可以正常添加),原因未知,因此使用了kprobe来增加return探测点,添加后一样可以使用perf record进行跟踪

参考:

https://blog.csdn.net/luckyapple1028/article/details/52972315/

http://www.brendangregg.com/perf.html

man perf-probe



版权声明:本文为bbccaaac原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。