kprobe是什么?

kprobe是一种轻量级内核调试技术,用于跟踪内核函数执行状态,基本不影响内核原有的执行流程。

kprobe有啥用?

调试内核或者模块时,如何知道一些函数是否被调用、何时被调用,如何获取函数的参数和返回值?
可以添加日志,重新编译,重启系统,但是操作复杂,甚至可能破坏原有的代码执行过程。
利用kprobe技术可以动态插入探测点,定义调用前、调用后和访存出错3种回调函数,操作方便,影响小。
开发人员自行编写内核模块,向内核注册探测点,探测函数可自行定制。
使用kprobe event跟踪函数参数和返回值,无需再编写内核模块。

kprobe怎么用?

下面是一个简单的kprobe示例程序,用来探测内核函数,用户自定义调用前和调用后回调函数。

loongson@linux:~$ cat kprobe_example.c
#define pr_fmt(fmt) "%s: " fmt, __func__

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/kprobes.h>

static char symbol[KSYM_NAME_LEN] = "copy_process";
module_param_string(symbol, symbol, KSYM_NAME_LEN, 0644);

/* For each probe you need to allocate a kprobe structure */
static struct kprobe kp = {
	.symbol_name	= symbol,
};

/* kprobe pre_handler: called just before the probed instruction is executed */
static int __kprobes handler_pre(struct kprobe *p, struct pt_regs *regs)
{
#ifdef CONFIG_LOONGARCH
	pr_info("<%s> p->addr = 0x%p, epc = 0x%lx, status = 0x%lx\n",
		p->symbol_name, p->addr, regs->csr_era, regs->csr_estat);
#endif
	/* A dump_stack() here will give a stack backtrace */
	return 0;
}

/* kprobe post_handler: called after the probed instruction is executed */
static void __kprobes handler_post(struct kprobe *p, struct pt_regs *regs,
				unsigned long flags)
{
#ifdef CONFIG_LOONGARCH
	pr_info("<%s> p->addr = 0x%p, status = 0x%lx\n",
		p->symbol_name, p->addr, regs->csr_estat);
#endif
}

static int __init kprobe_init(void)
{
	int ret;
	kp.pre_handler = handler_pre;
	kp.post_handler = handler_post;

	ret = register_kprobe(&kp);
	if (ret < 0) {
		pr_err("register_kprobe failed, returned %d\n", ret);
		return ret;
	}
	pr_info("Planted kprobe at %p\n", kp.addr);
	return 0;
}

static void __exit kprobe_exit(void)
{
	unregister_kprobe(&kp);
	pr_info("kprobe at %p unregistered\n", kp.addr);
}

module_init(kprobe_init)
module_exit(kprobe_exit)
MODULE_LICENSE("GPL");

下面是对应的Makefile文件:

loongson@linux:~$ cat Makefile
obj-m += kprobe_example.o

all:
	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

执行make编译后会生成kprobe_example.ko,插入模块后使用dmesg命令可以看到回调函数的执行结果:

loongson@linux:~$ sudo insmod kprobe_example.ko
loongson@linux:~$ dmesg
[ 2269.546415] kprobe_init: Planted kprobe at 0000000006741118
[ 2269.546709] handler_pre: <copy_process> p->addr = 0x0000000006741118, epc = 0x9000000000224854, status = 0x10
[ 2269.546711] handler_post: <copy_process> p->addr = 0x0000000006741118, status = 0x10
[ 2270.021170] handler_pre: <copy_process> p->addr = 0x0000000006741118, epc = 0x9000000000224854, status = 0x1
[ 2270.021173] handler_post: <copy_process> p->addr = 0x0000000006741118, status = 0x1
[ 2276.032533] handler_pre: <copy_process> p->addr = 0x0000000006741118, epc = 0x9000000000224854, status = 0x12f3b08e0
[ 2276.032536] handler_post: <copy_process> p->addr = 0x0000000006741118, status = 0x12f3b08e0