Search code examples
debugginggdbqemulibvirtvirsh

Debugging a custom OS with QEMU


I am trying to write a simple OS, I already wrote a bootloader but now I want to debug it, so I switched from using VirtualBox to QEMU because I saw it had better debugging. The problem is that after I added the -s parameter to QEMU command and successfully connected via GDB, it says that the symbol table isn't loaded and that I should use the "file" command. The only difference from what I did to what I saw people on the Internet do, is that they started GDB with gdb vmlinux, but I can't do that because I am not debugging a Linux kernel... so I figured that the issue is that I didn't start GDB with an executable, but using the "file" command on my OS image, and the compiled and linked .out file, tells me it's a "DOS/MBR boot sector", so I can't start GDB with either of them (I tried to do that anyways, but GDB failed).

Help would be appreciated.

EDIT: also, I did assemble the bootloader with the -g and --gstabs+ options.


Solution

  • gdb would like a file so that it can give you symbolic debugging information. For that you will need to give it a file in a format with debug info which corresponds to where your OS ends up in RAM. The "DOS/MBR boot sector" file is a disk image (the BIOS will load part of this into RAM for you, and it will then presumably finish loading code itself).

    But gdb will also entirely happily let you do assembly-level debugging; you can just ignore the warning about not having a symbol table, and use the single step instruction, disassemble-from-pc and similar commands:

    • "disas $pc,+32" disassembles 32 bytes from the current PC
    • the display command prints after execution stops, so "disp /3i $pc" will print the next 3 instructions every time gdb gets control
    • "stepi" and "nexti" do single-instruction step/next ("step" and "next" are source-line stepping and require debug info)