编译一个可以用Qemu进行Debug的Linux Kernel:

1. Download the Kernel Source Code

2. Unpacket it with:

xz -d ***.tar.xz
tar -xvf  ***.tar

Here ‘-d’and ‘-x’ means decompress, 'v' means printing the detail info on screen.

3. Generate the default configuration file .config with:

Make defconfig

The .comfig is a hidden file, you can see it with

ls -a

4. Since I want to debug the kernel, the debug info should be allowed before compiling. There are two way to modify this option.

   One is using the configuration menu. To launch it with command

Make menuconfig

   we need to have package ncurses-devel. But this package cannot be install with only apt-get. After     searching online, I found a solution:

apt-get install libncurses*

   So the configuration menu can be launch now and we can go to option 'kernel hacking' and select the     option 'compile kernel with debug info'.

   Another way is to open the .config file using vim and search for CONFIG_DEBUG_INFO and set it to 'y'.

   After set this option, there will be a new .config file and the orginal one will be renamed as .config.old

5. Compile the Kernel now with make or make bzImage.

   But I had a problem in this step since the Ubuntu I used is installed with Wubi and I didn't assign too much space for it, so there is error reported with No Enough Space.

   After reinstalling a ubuntu with 30GB, I compile the kernel successfully and get the result:

Kernel: arch/x86/boot/bzImage is ready  (#1)

Besides, if I want to complie a kernel of 32 bits, I can use the command below to generate the configuration file:

make ARCH=i386 defconfig

Also, you need to enable the EXT2, EXT3 file system in the configuration file, otherwise the qemu will stop at some call trace place.

6. Now we can boot the kernel with qemu and debug it.

   To just run the newly compiled kernel we can use the command:

qemu-system-i386 -kernel bzImage

   Here since I have created the soft-link and my kernel is 64bits, I can use  

qemu64 -kernel bzImage

   To debug the kernel step by step rather than just running it, the command is:

qemu-system-i386 -s -S -kernel "path to bzImage"

   Here the "-s" means to ask the qemu to stop as soon as it starts. "-S" means to inform qemu to wait for HSBC connection on port 1234 kernel.

7. Actually I still need a linux img file though I could understand well the reason. The command I use is:

qemu -S -kernel bzImage -hda linux-0.2.img -append "root=/dev/hda"

8. After the kernel is boot, it is stopped. Now use ctrl+alt+2 to change to qemu console and use "gdbserver" command

9. Launch another terminal and use the command:

gdb vmlinux

now you can set a breakpoint:

b start_kernel

and you can use continue command "c"

你可能感兴趣的:(debug,ubuntu,kernel,qemu)