eclipse 阅读linux 内核

作者: bugouyonggan
发布时间:2015-07-29 11:43:22

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

1 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:

STEP 2: Create a build directory

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

1 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.

1 cd ~/linux-2.6
2 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.

1 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

01 cd /mnt
02 sudo debootstrap sid sid
03  
04 echo -e "rootnroot" | chroot sid passwd
05 echo -e "auto loniface lo inet loopbacknauto eth0niface eth0 inet dhcp" > sid/etc/network/interfaces
06 ln -sf vimrc sid/etc/vimrc.tiny
07 rm -f sid/etc/udev/rules.d/70-persistent-net.rules
08 echo atul > sid/etc/hostname
09 echo cgroup /mnt/cgroup cgroup defaults >> sid/etc/fstab
10 mkdir -p sid/mnt/cgroup
11  
12 BLOCKS=$(((1024*$(du -m -s sid | awk '{print $1}')*12)/10))
13 genext2fs -z -d sid -b $BLOCKS -i 1024 sid.ext3
14 resize2fs sid.ext3 8G
15 tune2fs -j -c 0 -i 0 sid.ext3
16 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

1 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

1 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/








http://code.google.com/p/myboards/wiki/CompilingLinuxKernelUsingEclipse

2) Setting the host:

This is next stepping that after getting the Board. We need to setup a host that may suitable for your work go smoothly.
For this we suggest and the recommended is to install Ubuntu 10.04 LTS on your PC. For this go to http://www.ubuntu.com 
and download the ISO, burn it on a CD and Install it on your PC.

After Successful installation you need to setup some packages for your development. The installation of this Packages
we have an automated script for you which can be downloaded from 

来源:http://blog.csdn.net/bugouyonggan/article/details/21454309

推荐: