Sunday, December 12, 2010

Compiling Linux Kernel in eclipse, and debugging using QEMU

After I have started working with this eclipse IDE, I came across an article in a blog, and found it very interesting. The fact that compiling Linux kernel is fairly easy on the terminal does not need it to be compiled somewhere else, but then the fact that once configured, if you are using eclipse as an IDE for modifying and browsing the source code, it would be beneficial if you can directly build from there.

The procedure is very simple in fact !

Download the latest version of kernel from kernel.org website (ftp) and extract it to where ever you want to. The latest version now is (2.6.36.2), so download the linux-2.6.36.2.tar.gz file from ftp, and copy it to you home directory (in my case prasad)

tar xzvf linux-2.6.36.2.tar.gz

Now build the kernel, after configuring, and make sure it builds fine when built from the command prompt. Now its easy to integrate this environment into eclipse, this is almost same my previous post of building a make file based c project to build in eclipse. But it just looks complicated because its kernel.

Create a new make file based C project ans select the kernel source directory to be your project directory and select other tool chain option for tool chain. Click finish, I hope you have disabled indexing and auto build. 

Now, go to project properties and update the build directory path, and build command as needed, if you are building kernel in the same directory as the source code, you don't have to change anything here, but in case out put directory is different then just update the command as make O=/build/linux-2.6-xxx and build directory to /build/linux-2.6-xxx.

If you were building for a different target other than x86, update the Makefile of linux kernel with the CROSS_COMPILE and ARCH, or you can as well give them in the make command also.

Now click build all, you can see that eclipse IDE runs the commands set and outputs of the compilations are seen on eclipse console. if there is any error, you can just double click and the corresponding source code is highlighted (this is the best part of using an IDE)

Now you are ready to connect to QEMU for debugging the kernel, locate you bzImage/zImage, (kernel binary), run the kernel in QEMU, refer to qemu manual for how to do that (!?)

for your reference, in case of x86 kernel to be run on the QEMU type in this command (I assume qemu is compiled and installed)

qemu -hda /dev/zero -kernel ~/linux-2.6.32.2/arch/i386/boot/bzImage -s -S

-s -S options make the qemu wait on the TCP connection at port 1234 for the gdb debugger to connect. 

Now in the eclipse IDE, go to Run->Debug Configurations, double click on C/C++ application, a new configuration will be created. Browse the project, and in C/C++ application field browse the vmlinux for your kernel, and set the path for it. In the debugger tab, select the gdb-server, and set the stop point to be start_kernel. In the connections tab, set it to TCP and 1234 port number, and you are ready for debugging. 

The whole thing is explained in detail in the blog post here, Thanks for the wonderful information, it was really helpful for me. I myself debugged kernel using this approach in eclipse, if anybody faced problem let me know, I am glad to help you out.

1 comment: