kprobe 模块在执行 "echo 0 > /proc/sys/kernel/ftrace_enabled " 后无法工作

kprobe modules cannot work after execute "echo 0 > /proc/sys/kernel/ftrace_enabled "

只是研究一下 ftrace。

TCP 回显程序 运行 在两个主机之间。

当我关闭大开关(echo 0 > /proc/sys/kernel/ftrace_enabled)时,我自己的kprobe模块也无法工作。在内核日志文件中看不到 printk 消息。此外,pkt 修改操作失败,可以成功接收 pkt。 真的让我很困惑。

我的测试 kprobe 模块在这里:

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

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

/* kprobe pre_handler: called just before the probed instruction is executed */
static int handler_pre(struct kprobe *p, struct pt_regs *regs)
{

    struct sk_buff * skb = (struct sk_buff *)(regs->di);
    u16 dst_port;
    dst_port = ntohs(*((u16*)(skb->head + skb->transport_header + 2)));
    if(dst_port == 50000){ //50000 is the TCP port
        printk(KERN_INFO "post handler addr 0x%p skb is 0x%d\n",p->addr, regs->di);

        // modify one byte to make TCP checksum wrong and drop the pkt.
        *((u8*)(skb->head + skb->network_header +7))=0xab;
    }
    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)
{
    //printk(KERN_INFO "post handler addr 0x%p skb is 0x%d\n",p->addr, regs->di);
}

/*
 * 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");

问题已关闭。

其实在sys/kernel/debug/kprobes/list我们可以得到注册的kprobe列表。我得到这个 xxxxxxxxxxx k ip_rcv+0x0 [FTRACE],这意味着这个 kprobe 是 ftrace-based。 ftrace-based 如果禁用 ftrace,kprobe 将无法工作。