linux - 未为 system_call 函数插入内核探测器

标签 linux linux-kernel kprobe

我可以使用 kprobe 机制通过以下示例代码附加处理程序:

#include <asm/uaccess.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/version.h>
#include <linux/kallsyms.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/kprobes.h> 

static struct kprobe kp; 

int Pre_Handler(struct kprobe *p, struct pt_regs *regs){
    printk("pre_handler\n");
    return 0;
}

void Post_Handler(struct kprobe *p, struct pt_regs *regs, unsigned long flags) {
    printk("post_handler\n");
} 

int __init init (void) {
    kp.pre_handler = Pre_Handler;
    kp.post_handler = Post_Handler;
    kp.addr = (kprobe_opcode_t *)kallsyms_lookup_name("sys_fork"); 
    printk("%d\n", register_kprobe(&kp)); 
    return 0;
}

void __exit cleanup(void) {
    unregister_kprobe(&kp); 
}

MODULE_LICENSE("GPL");   
module_init(init);
module_exit(cleanup); 

但是,似乎并非所有内核例程都可以通过这种方式进行跟踪。我试图将处理程序附加到 system_call 以通过以下更改在任何系统调用执行中调用它们:

kp.addr = (kprobe_opcode_t *)kallsyms_lookup_name("system_call"); 

并且没有插入探针。 dmesg 显示 register_kprobe 返回 -22,即 -EINVAL。为什么这个函数无法追踪?是否可以在调度任何系统调用之前附加 kprobe 处理程序?

$ uname -r
3.8.0-29-generic

最佳答案

system_call 受 kprobes 保护,无法探测 system_call 函数。 我认为在调用任何实际系统调用之前我们没有任何您可以获得的有用信息。例如,如果您看到函数 system_call:

    RING0_INT_FRAME                 # can't unwind into user space anyway
    ASM_CLAC
    pushl_cfi %eax                  # save orig_eax
    SAVE_ALL
    GET_THREAD_INFO(%ebp)
                                    # system call tracing in operation / emulation
    testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags(%ebp)
    jnz syscall_trace_entry
    cmpl $(NR_syscalls), %eax
    jae syscall_badsys
    syscall_call:
    call *sys_call_table(,%eax,4)

在调用实际系统调用之前有一些指令。是的,我不确定您是否需要这些说明中的任何信息。

关于linux - 未为 system_call 函数插入内核探测器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20044682/

相关文章:

linux - 命令 'sudo gearmand -d' 和 'sudo service gearman-job-server start' 有什么区别?

Linux 内核在堆溢出或堆栈溢出时的行为

linux-kernel - ebpf:拦截函数调用

c++ - 确定 uint32_t 的字节顺序

linux - AWS ElasticBeanstalk 上的 Docker : "host.docker.internal: host-gateway": Connection refused. Iptables 问题?

c - 使用具有不同路径名的 Makefile 构建 Linux 内核模块

c - __udivdi3 undefined — 如何找到使用它的代码?

linux - register_kprobe 为涉及 rip 的指令返回 EINVAL (-22) 错误

linux - jprobe do_execve 不适用于内核 4.1

c++ - Makefile 中的隐式规则