Debashish
Debashish

Reputation: 1185

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 !

Upvotes: 3

Views: 1496

Answers (1)

Alex Hoppus
Alex Hoppus

Reputation: 3935

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.

Upvotes: 2

Related Questions