Getting function arguments using kprobes

assembly, debugging, kernel, linux

Solution

There are at least two approaches.

Approach 1: Jprobes

Probably the most easy one: if Jprobes are suitable for your task, you could give them a try. They are the close relatives of kprobes (see their detailed description and links to the examples in the kernel docs).

Jprobes allow to call your function with the same signature as the probed one on entry to the latter. You get all of the arguments automatically this way.

Approach 2: Registers and Stack

Another approach could be to extend a bit what you already do and retrieve the arguments from the registers and the stack. From the output log in your question, I assume you are working on a 32 bit x86 system.

x86, 32 bit

As far as I have seen, there are two most common conventions on argument passing in the Linux kernel on x86 (the details are available in the manual by Agner Fog). Note that system calls follow other conventions (see the manual for details) but I assume you are interested in analysing an "ordinary" function rather than a system call.

Convention 1

For the functions marked with `asmlinkage` as well as for the functions with variable argument lists, all parameters are passed on stack. The return address of the function should be at the top of the stack on entry to the function, the first parameter being located right below it. The second parameter is below the first one, and so on.

As you have the saved value of `esp`, you can find what it points to. `*(esp+4)` should be the first argument, `*(esp+8)` - the second one, etc., if this convention is used.

Convention 2

It seems to be used for the most of the kernel functions including the one you mentioned in the question.

The kernel is compiled with `-mregparm=3` and therefore the first 3 arguments are passed in `eax`, `edx` and `ecx`, in that order, the rest go on stack. `*(esp+4)` should be the 4th argument, `*(esp+8)` - the 5th one, and so on.

x86, 64bit

It seems that things are a bit simpler on x86-64. Most of the kernel functions (including those with variable argument list) get the first 6 arguments in `rdi`, `rsi`, `rdx`, `rcx`, `r8`, `r9`, in that order, the remaining ones go on stack. `*(esp+8)` should be the 7th argument, `*(esp+16)` - the 8th and so on.

EDIT:

Note that on x86-32, the value of `esp` is not saved in `pt_regs` for kernel-mode traps (including the software breakpoints that KProbes rely upon). <asm/ptrace.h> provides `kernel_stack_pointer()` function to retrieve the correct value of `esp`, it works both on x86-32 and on x86-64. See the description of `kernel_stack_pointer()` in that header file for details.

In addition, `regs_get_kernel_stack_nth()` (also defined in that header) provides a convenient way to get the contents of the stack in the handler.

Problem

I have put a kprobe on a function and now I need to get values of its arguments in kprobe's prehandler function. Here is my function: ``` void foobar(int arg, int arg2, int arg3, int arg4, int arg5, int arg6, int arg7, int arg8) { printk("foobar called\n"); } ``` Putting kprobe on it and calling the function: ``` ... kp.addr = (kprobe_opcode_t *) foobar; register_kprobe(&kp); foobar(0xdead1, 0xdead2, 0xdead3, 0xdead4, 0xdead5, 0xdead6, 0xdead7, 0xdead8); ``` And finally prehandler function (taken from here): ``` static int inst_generic_make_request(struct kprobe *p, struct pt_regs *regs) { printk(KERN_INFO "eax: %08lx ebx: %08lx ecx: %08lx edx: %08lx\n", regs->ax, regs->bx, regs->cx, regs->dx); printk(KERN_INFO "esi: %08lx edi: %08lx ebp: %08lx esp: %08lx\n", regs->si, regs->di, regs->bp, regs->sp); regs++; //... } ``` The output from the prehandler function looks like this (I incremented `regs` pointer 3 times) ``` May 10 22:58:07 kernel: [ 402.640994] eax: 000dead1 ebx: f7d80086 ecx: 000dead3 edx: 000dead2 May 10 22:58:07 kernel: [ 402.640996] esi: 00000000 edi: b77c8040 ebp: 00000000 esp: f7d8006c May 10 22:58:07 kernel: [ 402.641006] eax: f7d8032c ebx: 000dead5 ecx: 000dead6 edx: 000dead7 May 10 22:58:07 kernel: [ 402.641007] esi: 000dead8 edi: f7d800e0 ebp: f7d80330 esp: 08049674 May 10 22:58:07 kernel: [ 402.641014] eax: 00000080 ebx: 0992b018 ecx: 0000108e edx: 0992b008 May 10 22:58:07 kernel: [ 402.641015] esi: 08049674 edi: b77c8040 ebp: bfe23fb8 esp: bfe23f50 ``` Now I can see arguments of `foobar` function in various registers (but where's `0xdead4`?), shouldn't they be in a stack? How can I access the stack from prehandler function? Or how can I get arguments of any function without knowing their types and count? I know that this might not be an easy task (and not even possible to get all values), but only approximately values should be enough. I'm calculating correlation between arguments of two functions and I really don't need exact values. Would it help if I had assembly code of caller function where the arguments are pushed onto the stack)?

Original source