Abstract
Continue to learn about Linux.
Today we are going to build a Linux kernel debugging environment.
Environment: ubuntu13.04 gcc4.7.3
Install qemu
Qemu Introduction
Qemu is a set of simulation processors compiled by Fabrice bellard that use the GPL license to distribute source code. It is widely used on the GNU/Linux platform. Bochs and pearpc are similar, but they do not have many of their features, such as high speed and cross-platform features. With the open-source accelerator kqmeu, qemu can simulate speeds close to real computers.
The simplest method is to install Ubuntu Software Center:
If the source cannot be found, update the source.
For other Linux distributions, you can install the tool from the source code-http://wiki.qemu.org/main_page.
Compile the Linux Kernel
A previous article introduced kernel compilation. For details, refer to-Linux operating system analysis (4)-update the kernel and add system calls.
Here we need to recompile and add debugging information.
First download the latest kernel version
Https://www.kernel.org/pub/linux/kernel/v3.x/
Download linux-3.9.tar.gz
Decompress the package and enter the folder. Run the following command:
make menuconfig
In the kernel hacking project, select compile the kernel with debug info and compile the kernel with frame pointers. These projects allow you to add debugging information during compilation, similar to the-G option we usually use.
Run the following command:
make -j2 bzImage
To compile the kernel, I use a dual-core CPU, so it is-J2, quad-core-J4.
Debugging
Go to http://wiki.qemu.org/testingto download linux-0.2.img.bz2, and then drop it to the source code directory.
Terminal execution:
qemu -S -kernel arch/i386/boot/bzImage -hda linux-0.2.img -append "root=dev/hda"
#-The kernel is used to specify the kernel. Note that arch/x86/bzimage is the kernel without debugging information, and vmlinux is the kernel with debugging information.
#-Hda specifies the IDE Hard Disk
#-Append cmdline use 'cmdline' as kernel command line
Then a small black window pops up, And qemu is up.
You can switch between CTRL + ALT + 1 and CTRL + ALT + 2. The former is the screen output, and the latter is the qemu console.
Black screen after running, we want to switch to the console, click the window with the mouse, and then CTRL + ALT + 2,
Run the following command in the qemu command line:
gdbserver tcp::1234
Start gdbserver and listen on TCP port 1234.-s indicates that the CPU is frozen at the beginning until the remote GDB enters the corresponding control command.
Start a new terminal and use GDB to debug vmlinux.
gdb vmlinux
target remote localhost:1234
Set the breakpoint and run it.
Software development skills
Now that qemu has installed and runs the customer's operating system, you can start to develop software. Both the original compiler under the simulator and the cross compiler on the host are available. For large development projects that require repeated software compilation and testing in the customer's operating system, cross-compiler may be faster, but the installation configuration is beyond the scope of this article. The compiler running in the simulator is suitable for small projects or projects that do not frequently compile software.
For software, a computer is like a real example of a target platform and can be used for the most practical purposes. Some features, such as byte sequence, CPU consistency, and other basic hardware features, run normally on a real computer. I have used a real PowerPC Apple iMac and qemu-based PowerPC system to develop and test the PowerPC version of the Global unique identifier (guid) Partition Table (GPT) fdisk program, the program needs to know the byte sequence of the CPU. In my opinion, the qemu-based system is similar to the real iMac system, except that the qemu system is slow.
This shows that there are some differences in depth. For example, the qemu analog hard drive returns the "qemu harddisk" mode string, which is different from the real hard drive. In general, qemu's virtual hard disks are outdated. The hard disks are either PATA or small computer system interface (SCSI) devices. Based on the specific platform, the video hardware is outdated. You can find detailed information about the virtual hardware in the qemu document. Note that the details of each version are different.
In Linux, qemu sessions consume a small amount of CPU time, except when the customer's operating system is working. However, the qemu session occupies the ram capacity specified for it. Therefore, you need to allocate a large amount of memory to the host system, especially when you plan to run more than one or two qemu sessions at the same time.
Refer to the use of qemu for kernel source code-http://blog.csdn.net/gdt_a20/article/details/7231652use qemu for cross-platform development-http://www.ibm.com/developerworks/cn/linux/l-qemu-development/