gpt4 book ai didi

c - 在 kretprobe 的 entry_handler 中获取被探测函数的参数

转载 作者:行者123 更新时间:2023-12-01 07:14:47 25 4
gpt4 key购买 nike

我正在尝试使用 kretprobe void *__kmalloc(size_t size, gfp_t flags); 拦截 kmalloc我可以使用 kretprobe 结构的 handler 成员找出 kmalloc 的返回值。

static struct kretprobe kmalloc_probe = {
.handler = kmalloc_ret_handler,
.entry_handler = kmalloc_entry_handler,
.data_size = sizeof(struct kmalloc_read_args),
.maxactive = 20,
};

但我需要一种方法来找到在 entry_handler 中调用该函数的参数。这是我的 entry_handler 函数:

static int kmalloc_entry_handler(struct kretprobe_instance *ri, struct pt_regs *regs)

我尝试搜索 regs 结构参数的所有寄存器,但没有成功。我使用的架构是 i686。我知道 jprobes 更适合解决此类问题,但我需要仅使用 kretprobes 来解决它。能否请您提示我如何使用寄存器或堆栈来查找函数调用参数?

指向 pt_regs 结构的链接:http://lxr.free-electrons.com/source/arch/x86/include/asm/ptrace.h#L11

最佳答案

在 x86 内核中传递参数的约定在 asm/calling.h 中的注释中进行了描述。 .

在 32 位 x86 系统上,Linux 内核中的函数的第一个参数(除了系统调用和其他一些东西)通常按顺序传入 %eax、%edx、%ecx。这是因为源代码是使用默认设置的“-mregparm=3”GCC 选项编译的。至少从内核 2.6.32 开始就是这种情况,或者可能更早。

其余参数在堆栈上传递。

如果函数有一个可变参数列表(如 sprintf()),所有参数都在堆栈上传递,据我所知。

因此,在您的情况下,size 应该在 %eax 和 flags 中 - 在函数入口处的 %edx 中。如果这些寄存器没有被 kretprobe 以某种方式破坏,您应该能够在 pt_regs 中找到它们。

在 64 位 x86 系统上,约定更简单,更符合 x86-64 ABI。内核函数的第一个参数(同样,系统调用和一些特殊函数除外)依次传入 %rdi、%rsi、%rdx、%rcx、%r8、%r9,其余的在堆栈中。

关于c - 在 kretprobe 的 entry_handler 中获取被探测函数的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22686393/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com