使用Kprobe工具
1.kprobe的用途
使用kprobe可以向正在运行的内核中插入一个探测器,监视内核函数的执行,取得内核数据及其它需要的诊断信息。
跟踪内核函数的执行。可在进入(
handler_pre)和退出(
handler_post)要跟踪的的函数时,打印调试信息。如果加上执行时的时间,则所跟踪的函数的执行时间也一目了然。
2.kprobe使用例子
系统环境:Ubuntu 10.04.2 LTS/2.6.32-30-generic
下面是Makefile
ifneq ($(KERNELRELEASE),)
obj-m := kprobe_clock_settime.o
else
KERNELDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
endif
clean:
rm -r *.ko *.o .*.cmd .tmp_versions *.mod.c *.symvers modules.order
|
下面是模块代码
kprobe_clock_settime.c:
/*
* NOTE: This example is works on x86 and powerpc.
* Here's a sample kernel module showing the use of kprobes to dump a
* stack trace and selected registers when do_fork() is called.
*
* For more information on theory of operation of kprobes, see
* Documentation/kprobes.txt
*
* You will see the trace data in /var/log/messages and on the console
* whenever do_fork() is invoked to create a new process.
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/kprobes.h>
/* For each probe you need to allocate a kprobe structure */
static struct kprobe kp = {
.symbol_name = "sys_clock_settime", /*只要在这里写上要监视的内核函数的名字即可*/
};
/* kprobe pre_handler: called just before the probed instruction is executed */
static int handler_pre(struct kprobe *p, struct pt_regs *regs)
{
#ifdef CONFIG_X86
printk(KERN_INFO "pre_handler: p->addr = 0x%p, ip = %lx,"
" flags = 0x%lx\n",
p->addr, regs->ip, regs->flags);
printk("++++++++process_name:[%s], pid = %d.\n", current->comm, current->pid);
#endif
dump_stack(); /* A dump_stack() here will give a stack backtrace */
return 0;
}
/* kprobe post_handler: called after the probed instruction is executed */
static void handler_post(struct kprobe *p, struct pt_regs *regs,
unsigned long flags)
{
#ifdef CONFIG_X86
printk(KERN_INFO "post_handler: p->addr = 0x%p, flags = 0x%lx\n",
p->addr, regs->flags);
#endif
}
/*
* fault_handler: this is called if an exception is generated for any
* instruction within the pre- or post-handler, or when Kprobes
* single-steps the probed instruction.
*/
static int handler_fault(struct kprobe *p, struct pt_regs *regs, int trapnr)
{
printk(KERN_INFO "fault_handler: p->addr = 0x%p, trap #%dn",
p->addr, trapnr);
/* Return 0 because we don't handle the fault. */
return 0;
}
static int __init kprobe_init(void)
{
int ret;
kp.pre_handler = handler_pre;
kp.post_handler = handler_post;
kp.fault_handler = handler_fault;
ret = register_kprobe(&kp);
if (ret < 0) {
printk(KERN_INFO "register_kprobe failed, returned %d\n", ret);
return ret;
}
printk(KERN_INFO "Planted kprobe at %p\n", kp.addr);
return 0;
}
static void __exit kprobe_exit(void)
{
unregister_kprobe(&kp);
printk(KERN_INFO "kprobe at %p unregistered\n", kp.addr);
}
module_init(kprobe_init)
module_exit(kprobe_exit)
MODULE_LICENSE("GPL");
|
执行"make"命令生成文件
kprobe_clock_settime.ko
执行"sudo insmod kprobe_clock_settime.ko"命令插入模块
执行"date -s 2011-03-19 16:52:30"
执行"demsg(或cat /var/log/messages)"查看输出的信息。
几乎每一个系统调用,都调用一个函数执行其主要功能,如:名为clock_settime()的系统调用有一个名为sys_clock_settime的
函数执行其主要功能。这样可以通过用kprobe对sys_clock_settime函数打点监视clock_settime的使用。
阅读(1702) | 评论(0) | 转发(0) |