SWAT Blog

Linux kernel development and debugging using Eclipse CDT

February 11th, 2011 by Atul Sharma

This blog entry is inspired by Takis blog . This entry can be seen as the revised version of Takis blog entry with consideration to latest Eclipse and Linux Kernel versions.

Prerequisites

The steps below would work on Debian, Ubuntu and Mint Linux. I have used Linux Mint.

You need latest version of Eclipse IDE for C/C++ Developers (I have used Helios). You can download from http://www.eclipse.org/downloads/

For rest of this guide you’d need several packages that you can install using following command


apt-get install git-core kernel-package fakeroot build-essential libncurses5 libncurses5-dev qemu kvm crash kexec-tools makedumpfile kernel-wedge libelf-dev asciidoc binutils-dev debootstrap genext2fs e2fsprogs

STEP 1: Get the Kernel source using git.

Run following command to clone the kernel source into your home folder:

git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git ~/linux-2.6

STEP 2: Create a build directory

Create build directory in /mnt directory as its the best place to keep any mountable file system.

mkdir -p /mnt/build/linux-2.6

STEP 3: Configure kernel

First run make oldconfig, which will read the defaults from an existing .config and rewrite necessary links and files.

cd ~/linux-2.6
yes "" | make oldconfig

Now make changes to default configure to enable debugging options using menuconfig. You can also use xconfig if you’re using X.

make menuconfig O=/mnt/build/linux-2.6

Select “Kernel hacking” menu


Enable “Compile the kernel with debug info”


Enable “Compile the kernel with frame pointers”

STEP 4: Disable automatic build in Eclipse

Open Eclipse with CDT plugin

Kernel code is huge and we wouldn’t want to enable automatic building after every change.  Go to “Window->Preferences” menu, Select “General->Workspace” and deselect “Build automatically” checkbox

STEP 5: Add kernel project in Eclipse

Create a new project. Go to  File->New->Project then select C Project.

Click Next

Enter project name – I have used linux2.6

Uncheck “Use Default Location” and select the linux source directory (~/linux2.6 in my case)

Select “Empty Project” under “Makefile project” as Project Type

Select Linux GCC as Toolchain

Click Next

Keep default values in Select Configurations window

Click Finish

Now eclipse will import the kernel source into the project and index it. This will take a while. Once complete all progress bars will disappear and the UI will look similar to below:

STEP 6: Configure build properties in Eclipse

Right click on Project and click on “Properties” from context menu.

Select “C/C++ Build” in properties window.

Uncheck “Use default build command”

Set build command as – make  O=/mnt/build/linux-2.6

Set build directory as – /mnt/build/linux-2.6

Now the project is ready to build. Go to the menu-bar select “Project->Build all”.

If all goes well then after some time the Linux kernel build will be completed.

STEP 7: Configure debug settings in Eclipse

Right click on Project. Click on “Debug As ..” >> “Debug Configurations”

Double Click on “C/C++ Attach to Application”

Select vmlinux binary (in my case /mnt/build/linux-2.6/vmlinux) in C/C++ Application field on Main tab

Click on Debugger tab

Select GDB Server in Debugger option under main tab

Click on Connection tab

Select TCP in Connection “Type” field

Enter localhost in “Host name or IP address” field

Enter 1234 in “Port Number” field

STEP 8: Prepare a root filesystem for testing kernel

You’d need a root filesystem in order to boot and test the kernel. I prefer not using filesystem of my host machine. Here is a recipe to build a basic 8GB root file system


cd /mnt
sudo debootstrap sid sid

echo -e "rootnroot" | chroot sid passwd
echo -e "auto loniface lo inet loopbacknauto eth0niface eth0 inet dhcp" > sid/etc/network/interfaces
ln -sf vimrc sid/etc/vimrc.tiny
rm -f sid/etc/udev/rules.d/70-persistent-net.rules
echo atul > sid/etc/hostname
echo cgroup /mnt/cgroup cgroup defaults >> sid/etc/fstab
mkdir -p sid/mnt/cgroup

BLOCKS=$(((1024*$(du -m -s sid | awk '{print $1}')*12)/10))
genext2fs -z -d sid -b $BLOCKS -i 1024 sid.ext3
resize2fs sid.ext3 8G
tune2fs -j -c 0 -i 0 sid.ext3
rm -rf sid

Notice I have setup root password as “root”

STEP 9: Run QEMU to launch the kernel build

Use following command to startup QEMU


qemu -no-kvm -s -S  -kernel /mnt/build/linux-2.6/arch/x86/boot/bzImage -hda /mnt/sid.ext3 -append "root=/dev/sda"

In case you’re using 64 bit host the use following command


qemu-system-x86_64 -no-kvm -s -S  -kernel /mnt/build/linux-2.6/arch/x86/boot/bzImage -hda /mnt/sid.ext3 -append "root=/dev/sda"

At this point you’ll see a blank QEMU terminal window. The “-s” option is a shorthand for -gdb tcp::1234, i.e. open a gdbserver on TCP port
1234. The “-S” option stops the CPU to start at startup. Now QEMU is waiting for kernel to start in debug mode
STEP 10: Start Kernel in Debug mode
From the menu bar in Eclipse click on debug icon
Then click on Linux2.6 Default debug configuration to run from the drop down list.
Launching might take a while, you can see the progress a the bottom right progress bar

Once the application is ready to launch it will prompt you to open the debugging perspective. Click on Yes.

Now the Eclipse perspective will change form development to Debug

Click “Run > Step over” or press the “F6″ key to execute the kernel code line by line and examine what’s happening.
Or click “Run > Resume” or press the “F8″ key to execute the kernel code without breaks.

You can find several articles on CDT at http://www.eclipse.org/cdt/documentation.php

UPDATED:
Here is the updated URL to Taki’s blog – http://issaris.org/blog/2007/12/14/debugging-the-linux-kernel-using-eclipsecdt-and-qemu/

Tags: , , , , , ,

16 Responses to “Linux kernel development and debugging using Eclipse CDT”

  1. amirali says:

    Hi,

    According to the tutorial, I tried following command to run qemu:
    sudo qemu -s -S -hda linux-0.2.img -append “root=/dev/sda” -kernel
    /mnt/build/linux-2.6/arch/x86/boot/bzImage

    As I used my modified kernel code, I encountered with kernel oops; So
    I tried to find the related code line which causes problem, using
    breakpoints.
    Now my problem is, when I set breakpoints before start debugging in
    eclipse, it seems that it ignores breakpoints and kernel goes to oops
    condition again.

    Also I tried combination of gdb and qemu using following commands:
    #gdb
    (gdb) symbol-file vmlinux.o
    (gdb) target remote localhost:1234
    (gdb) break scsi_lib.c 1420
    (gdb) c

    I tried above commands to check if stops at breakpoint or not. I put a
    printk after breakpoint line. It printed it and it means that it has
    ignored breakpoint.
    Would you guide me what is wrong with what I have done?

    Thanks in advance,

  2. Husain says:

    Hi,

    I am running into a different issue.

    when i try to build i am getting the following errors

    make: *** /root/linux-2.6: Permission denied. Stop.
    make: *** [all] Error 2

    I am sure it something very simple, can you please let me know,

    also if you have any experiance with cross plateform development or point to a web site would be great.

    Thansk

    Husain

    • Atul says:

      Husain:
      looks like you’re logged in as root – make sure /root/linux-2.6 directory exists and has the kernel source and you’ve rw permissions.
      When you say cross platform – what is your target architecture?

      Good Luck.
      Atul

  3. Husain says:

    Hi Atul,

    My target architecture will be arm a8, Marvel Shieva or Omap Setara SOC.

    regarding my previous issue, I have checked that the directory exists as well as the /mnt/build/linux-2.6 also exists. one of the first things i did was to change mode to 777 just to be safe.

    Thanks for the help, I will keep plugging away.

    Husain

  4. Atul Sharma says:

    Adding another interesting link that I found while following the trackbacks.
    http://www.on5di.com/~noel/eclipse/kernelmodules.html

  5. Atul Sharma says:

    Adding another link, reference to a script that adds symbols is useful http://simonkagstrom.livejournal.com/31079.html

  6. joel says:

    Hi atul,

    First of all I want to thank you for this blog, it has been of immense help to me. I ran into an issue while following your above steps, everything went fine, except for the step before qemu.. in my workplace I dont have permission to create a root filesystem using debootstrap, so can you give a qemu command which uses the host root filesystem instead? Thanks a lot for your help in advance :)

    • Atul Sharma says:

      Hi Joel,

      Easiest would be to get a pre-built filesystem image – there are several available at http://fs.devloop.org.uk/.
      Or you can copy your hosts file system to a directory that you can then mount using command like cp -R /* /mnt/rootfs . I wouldn’t recommend mounting your hosts filesystem directly with a debug copy of kernel.

      Atul

  7. joel says:

    Thanks atul,

    I seem to have another problem..
    I enabled super user using sudo su, and proceeded with ur method using debootstrap itself.

    I dont have bzImage, instead in the path I gave zImage in the qemu command “qemu -no-kvm -s -S -kernel /mnt/build/linux-3.1_kernel/arch/arm/boot/zImage -hda /mnt/sid.ext3 -append “root=/dev/sda” ” I got the following error message

    pci_add_option_rom: failed to find romfile “pxe-rtl8139.bin”
    ctsuser@unameit-desktop:/mnt/build/linux-3.1_kernel/arch/arm/boot$ qemu -no-kvm -s -S -kernel /mnt/build/linux-3.1_kernel/arch/arm/boot/zImage -hda /mnt/sid.ext3 -append “root=/dev/sda”
    pci_add_option_rom: failed to find romfile “pxe-rtl8139.bin”

    The qemu window opens, then displays stop and is not responding.

    Please help

    • Atul Sharma says:

      Hi Joel,

      Try to install kvm-pxe package using apt-get or yum based on distribution you’re using.
      If you still see the error – please send me the complete stack trace.

      Atul

  8. joel says:

    Hi Atul,

    Thanks for your help, I was able to install kvm-pxe and the error left.

    This is the qemu command I gave

    root@unameit-desktop:/home/ctsuser# qemu -no-kvm -s -S -kernel /mnt/build/linux-3.1_kernel/arch/arm/boot/zImage -hda /mnt/sid.ext3 -append “root=/dev/sda”

    After successfully starting the qemu, the qemu stops and waits.

    Then I click on debug in eclipse

    In debug perspective a particular address location is shown and the respective disassembly for the instruction in that address location is displayed.

    Problem:
    -Unable to locate the source code for the disassembly
    -also Unable to perform step over, when I click resume for the gdb. it continues and the gdb stops

    Thanks a lot for your help :)

  9. joel says:

    Hi atul,

    I am having problem with the qemu, I gave this command qemu -no-kvm -s -kernel /mnt/build/linux-2.6.32_kernel/arch/arm/boot/zImage -hda /mnt/sid.ext3 -append “root=/dev/sda”

    the qemu is supposed to boot the kernel image but,
    the qemu is not booting the kernel, instead an array of colours and unknown symbols are generated and the qemu window hangs up,

    Please help, is it a problem with my zImage or is the problem elsewhere??

    I have also tried with other images such as uImage,Image file, all of which experiencing the same problem

  10. joel says:

    Hi Atul,

    I am not able to perform build in my eclipse

    When I perform the build I am getting this error

    /bin/sh: cannot create scripts/kconfig/.conf.cmd: Permission denied
    make[3]: *** [scripts/kconfig/conf] Error 2
    make[2]: *** [silentoldconfig] Error 2
    make[1]: *** No rule to make target `include/config/auto.conf’, needed by `include/config/kernel.release’. Stop.
    make: *** [sub-make] Error 2

    **** Build Finished ****

    Please help

  11. Rudra says:

    Hi Atul,
    Thanks for the post.
    Even i’m facing the same problem as Joel. Unable to build in my eclipse.

    **** Build of configuration Default for project LinuxKernel ****

    make O=/mnt/build/linux-3.6.1 all
    GEN /mnt/build/linux-3.1.6/Makefile
    scripts/kconfig/conf –silentoldconfig Kconfig

    *** Error during update of the configuration.

    make[4]: *** [silentoldconfig] Error 1
    make[3]: *** [silentoldconfig] Error 2
    make[2]: *** No rule to make target `include/config/auto.conf’, needed by `include/config/kernel.release’. Stop.
    make[1]: *** [sub-make] Error 2
    make: *** [all] Error 2

    **** Build Finished ****

    Please Help.

Leave a Reply for joel

  • © 2004-2015 Special Work & Technology Limited