""" GDB Python script for debugging raisernics under QEMU. Usage: 1. Terminal 1: ./scripts/run.sh --debug (GDB stub on :1234) Add PROFILE=debug for line-level symbols (slower guest). 2. Terminal 2: gdb -x scripts/debug-kernel.py """ import gdb import os # Find the kernel binary (debug profile preferred, release as fallback) script_dir = os.path.dirname(os.path.abspath(__file__)) project_root = os.path.dirname(script_dir) candidates = [ os.path.join(project_root, "target/x86_64-unknown-uefi/debug/rsdos.efi"), os.path.join(project_root, "target/x86_64-unknown-uefi/release/rsdos.efi"), ] binary = next((c for c in candidates if os.path.exists(c)), candidates[0]) # Connect to QEMU's GDB stub gdb.execute("target remote :1234") # Load the binary (add symbol table) if os.path.exists(binary): gdb.execute(f"symbol-file {binary}") print(f"Loaded symbols from {binary}") else: print(f"WARNING: {binary} not found. Build with debug symbols first.") gdb.execute("set breakpoint pending on") # Helper commands class DumpBootServices(gdb.Command): """Dump EFI boot services signature and key pointers""" def __init__(self): super().__init__("dump-bs", gdb.COMMAND_USER) def invoke(self, argument, from_tty): del argument, from_tty gdb.execute("printf \"SystemTable, BootServices, and key function pointers\\n\"") gdb.execute("info functions efi_main") class DumpGop(gdb.Command): """Dump GOP framebuffer info""" def __init__(self): super().__init__("dump-gop", gdb.COMMAND_USER) def invoke(self, argument, from_tty): del argument, from_tty gdb.execute("printf \"Check the gop::Framebuffer static\\n\"") DumpBootServices() DumpGop() print("") print("raisernics GDB session ready.") print(" target remote :1234 (connected)") print(" Commands: continue, stepi, bt, info registers, etc.") print(" Custom: dump-bs, dump-gop")