How to debug the Linux kernel with GDB and QEMU?

E-Kami picture E-Kami · Jul 10, 2012 · Viewed 50.4k times · Source

I'm new to kernel development and I would like to know how to run/debug the linux kernel using QEMU and gdb. I'm actually reading Robert Love's book but unfortunately it doesn't help the reader on how to install proper tools to run or debug the kernel... So what I did was to follow this tutorial http://opensourceforu.efytimes.com/2011/02/kernel-development-debugging-using-eclipse/. I'm using eclipse as an IDE to develop on the kernel but I wanted first to get it work under QEMU/gdb. So what I did so far was:

1) To compile the kernel with:

make defconfig (then setting the CONFIG_DEBUG_INFO=y in the .config)
make -j4

2) Once the compilation is over I run Qemu using:

qemu-system-x86_64 -s -S /dev/zero -kernel /arch/x86/boot/bzImage

which launch the kernel in "stopped" state

3) Thus I have to use gdb, I try the following command:

gdb ./vmlinux

which run it correctly but... Now I don't know what to do... I know that I have to use remote debugging on the port 1234 (default port used by Qemu), using the vmlinux as the symbol table file for debugging.

So my question is: What should I do to run the kernel on Qemu, attach my debugger to it and thus, get them work together to make my life easier with kernel development.

Answer

BjoernD picture BjoernD · Jul 10, 2012

I'd try:

(gdb) target remote localhost:1234
(gdb) continue

Using the '-s' option makes qemu listen on port tcp::1234, which you can connect to as localhost:1234 if you are on the same machine. Qemu's '-S' option makes Qemu stop execution until you give the continue command.

Best thing would probably be to have a look at a decent GDB tutorial to get along with what you are doing. This one looks quite nice.