Skip to content

Debugging

Douglas Thain edited this page Dec 20, 2018 · 5 revisions

This is just one method for debugging using GDB connected to QEMU as a remote debugger.

To complete these instructions, you need:

  • qemu
  • The i686-elf version of gdb built using the cross compiler script.

First, create a .gdbinit file that tells GDB to connect to QEMU on port 1234 and load symbols from the kernel, and from any one user program that you want to debug simultaneously:

Here is an example:

target remote localhost:1234
add-symbol-file kernel/kernel.elf 0x10000
add-symbol-file user/test.exe 0x80000000

Then start QEMU in the background with the -s and -S options:

qemu-system-i386 -cdrom basekernel.iso -s -S &

You should get a qemu instance with nothing happening, because it is waiting for GDB to connect. So run gdb:

i686-elf-gdb

You should get something like this:

    0x0000fff0 in ?? ()
    add symbol table from file "kernel.elf" at
    	.text_addr = 0x10000
    add symbol table from file "test.elf" at
    	.text_addr = 0x80000000
    (gdb)

You should then be able to reference a symbol and set breakpoints like a normal debugger, i.e.:

    (gdb) break kernel_main
    Breakpoint 1 at 0x10634: file main.c, line 40.
    (gdb) continue
    Continuing.
 
    Breakpoint 1, kernel_main () at main.c:40
    40	{
    (gdb) next
    41		struct graphics *g = graphics_create_root();
    (gdb) next

This is made possible by two changes: first, the Makefile compiles sources as ELF files with symbols before using objcopy to convert the executable to flat binary; second, there is a .gdbinit file that establishes the connection to qemu and loads the symbol-table on startup.

Clone this wiki locally