19 Feb 2009

Trying to use QEMU and GDB to debug GRUB 2

I made some modifications to GRUB 2 that seems to cause crashing somewhat early in boot process. So I wondered how to debug this. One obivious solution was to see what is happening on the system. So I planned to try out QEMU's GDB support.

First I was running QEMU from Ubuntu 7.10 to try it out, but it crashed the whole QEMU when my problemous code was executed so it wasn't so nice :). Next I went to get newest QEMU from their SVN, compiled it and tested it out. This newest version doesn't crash anymore so I continued my trek.

As there didn't seem to be any good front ends matching my requirements for this case I turned to last resort; gdb text console itself.

I have never liked the gdb text console interface so it took a bit time to remember (and google) how it works.

To start QEMU I used following command:

qemu -cdrom grub2.iso -s -S


And then in GDB:

target remote localhost:1234

; as we start in 16bit real mode
set arch i8086

; set breakpoint in entry point, at begining of GRUB2's CD-ROM boot sector code.
break *0x7c00

Then I just entered 'c' to start execution until it hit the break point.

After that it said:

Program received signal SIGTRAP, Trace/breakpoint trap.
0x00000000 in ?? ()

This message was a bit confusing and QEMU manual provide a bit advice in here.

To see where I really was I had to use following command:

x/10i $cs*16+$eip

And there was the boot sector code.

Now in order to step one instruction forward there is stepi command in gdb. However it seems that I first have to delete previous breakpoint:

; to see list of breakpoints
info b

; and to delete one I just set
delete 1

After this stepi worked fine.

Now I just need to figure out a bit more user friendlier way to debug :|

3 comments:

Unknown said...

could you please define more precisely the patch grub2-gdb and grub2 version -or svn number-

I am unable from googling to get the right patch_gdb_grub2 that applies

I wish to do both debugging grub with qemu-gdb as well as with serial-gdb-stub
thank you

Unknown said...

Did you do anything special to get the remote target to work correctly? I get the dreaded "Remote 'g' packet reply is too long: ..." message even when using a 32-bit gdb.

daChaac said...

I used QEMU and GDB from svn. It seems that they need to be recent enough.