2015年12月10日星期四

Debug kernel with Qemu and GDB

The first step: download linux kernel and complie bzImage file.

      make menuconfig
      make bzImage -jN        N=cores numbers
then we get vmlinux under the linux-kernel directory, and under arch/x86/boot/ directory, there will be a bzImage file.

The second step: install qemu and gdb.
      sudo apt-get install qemu
Note: gdb can not be installed by apt-get install directly, because there is a bug when using  gdb remote connect to gdbserver in qemu:
      Remote g packet reply is too long.
To resolve this problem, we should modify the gdb source code. Download the gdb-7.10.tar.xz from http://ftp.gnu.org/gnu/gdb/, then modify the gdb/remote.c like this:

 6565   //if (buf_len > 2 * rsa->sizeof_g_packet)
 6566   //  error (_("Remote 'g' packet reply is too long: %s"), rs->buf);
 6567
 6568   if (buf_len > 2 * rsa->sizeof_g_packet)
 6569     {
 6570       rsa->sizeof_g_packet = buf_len;
 6571
 6572       for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
 6573     {
 6574       if (rsa->regs[i].pnum == -1)
 6575         continue;
 6576
 6577       if (rsa->regs[i].offset >= rsa->sizeof_g_packet)
 6578         rsa->regs[i].in_g_packet = 0;
 6579       else
 6580         rsa->regs[i].in_g_packet = 1;
 6581     }           
 6582    }
 After changing the source code, install gbd:
       ./configure
       make
       sudo make install
makeinfo problem can be solved: sudo apt-get install texinfo

If gdb installed successfully, check the gdb version:
       gdb -v


Then the last step, we can debug kernel with qemu and gdb.
invoke qemu:
       qemu-system-x86_64 -kernel arch/x86/boot/bzImage -initrd /boot/initrd.img-2.6.32.68 -append "root=/dev/sda" -s -S
intialize gdb:
       gdb vmlinux
Connect gdb to qemu gdbserver port:
(gdb) target remote localhost:1234
(gdb) breakpoint where_you_want_to_break
(gdb) c
.....

The last thing, when compile the kernel, we should open the CONFIG_DEBUG_INFO option by selecting kernel hacking ----> Compile-time checks and compiler options  --->  [*] Compile the kernel with debug info.



Enjoy!









没有评论:

发表评论