1
/*kprobe_example.c*/
#define FUNCNAME alloc_file /* Find something better. printk() isnt recommended */
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/kprobes.h>
#include <linux/kallsyms.h>
#include <linux/sched.h>
/*For each probe you need to allocate a kprobe structure*/
static struct kprobe kp;

/*kprobe pre_handler: called just before the probed instruction is
executed*/
int handler_pre(struct kprobe *p, struct pt_regs *regs)
{
//      dump_stack();
        return 0;
}

/*kprobe post_handler: called after the probed instruction is executed*/
void handler_post(struct kprobe *p, struct pt_regs *regs, unsigned long
flags)
{

}

/* 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.
 */
int handler_fault(struct kprobe *p, struct pt_regs *regs, int trapnr)
{
        /* Return 0 because we don't handle the fault. */
        return 0;
}

int init_module(void)
{
        int ret;
        kp.pre_handler = handler_pre;
        kp.post_handler = handler_post;
        kp.fault_handler = handler_fault;
        kp.addr = (kprobe_opcode_t*) kallsyms_lookup_name(FUNCNAME);
        /* register the kprobe now */
        if (!kp.addr) {
                printk("Couldn't find %s to plant kprobe\n", FUNCNAME);
                return -1;
        }
        if ((ret = register_kprobe(&kp) < 0)) {
                printk("register_kprobe failed, returned %d\n", ret);
                return -1;
        }
        printk("kprobe registered\n");
        return 0;
}

void cleanup_module(void)
{
        unregister_kprobe(&kp);
        printk("kprobe unregistered\n");
}

MODULE_LICENSE("GPL");

问题很简单:我需要指向探测(拦截)函数参数的指针。有没有办法从寄存器中获取/恢复它们?

4

1 回答 1

0

答案是jprobes。

redhat的示例代码丢失my_jprobe.kp.addr = (kprobe_opcode_t *) kallsyms_lookup_name(FUNCAME);

另一个问题是,要捕获和修改返回值,应该找到一个更好的探测位置(请参阅http://lwn.net/Articles/132196/以获得更好的理解)。

于 2011-03-08T00:01:16.320 回答