设为首页 加入收藏

TOP

kprobe_events shell模式使用教程
2023-07-23 13:29:01 】 浏览:14
Tags:kprobe_events shell

kprobe_events shell模式使用教程

使用前提

需要内核启用以下配置
CONFIG_KPROBES=y
CONFIG_HAVE_KPROBES=y
CONFIG_KPROBE_EVENT=y

kprobe_events

kprobe_events有两种类型:kprobe,kretprobe
kprobe_evnets规则添加的入口:/sys/kernel/debug/tracing/kprobe_events

  1. kprobe
    在函数调用时进行探测,可以用来判断函数是否被调用以及调用函数的传参值是什么.
    用法:
    echo "p:probe1 input_event type=%x1 code=%x2 value=%x3" > /sys/kernel/debug/tracing/kprobe_events
  2. kretprobe
    在函数调用完成后探测,可以用来判断函数执行的结果.
    用法:echo "r:probe2 input_event $retval" > /sys/kernel/debug/tracing/kprobe_events

用法说明:
p代表kprobe模式
r代表kretprobe模式
probe1,probe2为探测点的名称,可以任意命名
input_event被探测的函数
type,code,value自定义字段
%x1,%x2,%x3ARM架构下传参寄存器分别代表探测函数的第2,3,4个参数,用于给自定义字段赋值
$retval用于kretprobe模式,代表探测函数的返回值
如果有多个探测点可以将重定向>替换成追加>>

kprobe模式下如何查看探测函数参数列表中的结构体成员的值
例如:探测函数void hidinput_hid_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value),获取usage->hid,usage->type,usage->code的值,并根据hid过滤.

在kernel 4.4.131中:

struct hid_usage {
	unsigned  hid;			/* hid usage code */
	unsigned  collection_index;	/* index into collection array */
	unsigned  usage_index;		/* index into usage array */
	/* hidinput data */
	__u16     code;			/* input driver code */
	__u8      type;			/* input driver type */
	__s8	  hat_min;		/* hat switch fun */
	__s8	  hat_max;		/* ditto */
	__s8	  hat_dir;		/* ditto */
};

其中hid字段结构体内offset=0
code offset=12
type offset=14
echo "p:probeG6 hidinput_hid_event hid=+0(%x2):u32 type=+14(%x2):u8 code=+12(%x2):u16" >> /sys/kernel/debug/tracing/kprobe_events
echo "hid < 0x700e0 || hid > 0x700e7" > /sys/kernel/debug/tracing/events/kprobes/probeG6/filter

%x2获取hidinput_hid_event函数的第3个参数
+0(%x2)对第3个参数进行取值操作
:u32定义数据类型为u32,如果字段为字符串可定义为string
+14(%x2)先对第3个参数进行取值操作,然后+14个字节的偏移

filter

对探测结果进行条件过滤

echo "p:probe1 input_event type=%x1 code=%x2 value=%x3" > /sys/kernel/debug/tracing/kprobe_events
echo "type == 1" > /sys/kernel/debug/tracing/events/kprobes/probe1/filter

type == 1仅输出type值为1的条目,在kretprobe模式下也可以对探测函数的返回值进行过滤.

查看探测输出

cat /sys/kernel/debug/tracing/trace_pipe该操作会阻塞
kprobe输出字段的含义

                                                                                       # tracer: nop
#
# entries-in-buffer/entries-written: 0/0 #P:4
#
# _-----=> irqs-off
# / _----=> need-resched
# | / _---=> hardirq/softirq
# || / _--=> preempt-depth
# ||| / delay
# TASK-PID CPU# |||| TIMESTAMP FUNCTION
# | | | |||| | |

启动kprobe

echo "1" > /sys/kernel/debug/tracing/events/kprobes/enable

关闭kprobe

echo "0" > /sys/kernel/debug/tracing/events/enable
echo "" > /sys/kernel/debug/tracing/kprobe_events

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇logging 模块因权限问题写入日志.. 下一篇【网络知识】虚拟机的桥接、NAT、..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目