Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trace from user space code to kernel space

I recently set up my system for kernel debug using qemu+gdb. At present, I can set breakpoints at, for example, __do_page_fault() and trace the call via gdb (with win command). Now I want the following task: A simple C program having a "hello world" printfstatement. Trace the call sequence starting from the userspace down to the write() system call ( or anything in the kernel space that is invoked during the execution of that particular userspace program). I want to learn how userspace program traps into system call w.r.t Linux kernel specifically.

Now my doubt is where to set the breakpoint? We have kernel code as well as the C code of the program. How to go about this situation ? Please give us an explanation with example.

Thank You !

like image 865
Debashish Avatar asked Aug 31 '26 04:08

Debashish


1 Answers

The most easiest way in my opinion is to separate this into two pieces.

  1. Place breakpoint in guest kernel using host gdb.
  2. Place breakpoint in user code before trap instruction, using in-guest target gdb, when hit - print stack using target (in-qemu) gdb. You will get user space stack trace.
  3. Continue execution in guest gdb
  4. In-kernel breakpoint (we have set it at stage 1) will be hit in host gdb. Print kernel stack trace.

P.S. If your kernel will continuously hit breakpoint (f.e. write syscall is definitely used widely), you can use a conditional breakpoint to hit a breakpoint only with a certain parameters passed.

like image 100
Alex Hoppus Avatar answered Sep 03 '26 23:09

Alex Hoppus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!