From 7bdd4a11866d16dd6626f4cbb5ce4c3616485db5 Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Fri, 29 Nov 2019 22:39:25 +0100 Subject: [PATCH] main: add support for QEMU in the gdb subcommand This allows debugging QEMU-only targets with GDB, which can be super useful. --- main.go | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 7cb1c241..01962735 100644 --- a/main.go +++ b/main.go @@ -268,12 +268,13 @@ func FlashGDB(pkgName, port string, ocdOutput bool, options *compileopts.Options gdbInterface, openocdInterface := config.Programmer() switch gdbInterface { case "msd", "command", "": - if gdbInterface == "" { - gdbInterface = "command" - } if openocdInterface != "" && config.Target.OpenOCDTarget != "" { gdbInterface = "openocd" } + if len(config.Target.Emulator) != 0 { + // Assume QEMU as an emulator. + gdbInterface = "qemu" + } } // Run the GDB server, if necessary. @@ -303,6 +304,26 @@ func FlashGDB(pkgName, port string, ocdOutput bool, options *compileopts.Options // Make sure the daemon doesn't receive Ctrl-C that is intended for // GDB (to break the currently executing program). setCommandAsDaemon(daemon) + // Start now, and kill it on exit. + daemon.Start() + defer func() { + daemon.Process.Signal(os.Interrupt) + // Maybe we should send a .Kill() after x seconds? + daemon.Wait() + }() + case "qemu": + gdbCommands = append(gdbCommands, "target remote :1234") + + // Run in an emulator. + args := append(config.Target.Emulator[1:], tmppath, "-s", "-S") + daemon := exec.Command(config.Target.Emulator[0], args...) + daemon.Stdout = os.Stdout + daemon.Stderr = os.Stderr + + // Make sure the daemon doesn't receive Ctrl-C that is intended for + // GDB (to break the currently executing program). + setCommandAsDaemon(daemon) + // Start now, and kill it on exit. daemon.Start() defer func() {