From 7123941df061f8e4d4a8aa27662b02fb04101ae9 Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Wed, 23 Sep 2020 19:14:21 +0200 Subject: [PATCH] main: add support for debugging qemu-user targets This commit allows debugging like the following: GOARCH=arm tinygo gdb ./testdata/alias.go This can be very useful to debug issues on a different instruction set architecture but still on a host system. I tested the following 7 configurations to make sure it works and I didn't break anything: GOOS=amd64 GOOS=386 GOOS=arm GOOS=arm64 tinygo gdb -target=hifive1-qemu tinygo gdb -target=cortex-m-qemu tinygo gdb -target=microbit --- compileopts/target.go | 22 ++++++++++------------ main.go | 14 +++++++++++++- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/compileopts/target.go b/compileopts/target.go index 89e40252..f95c4855 100644 --- a/compileopts/target.go +++ b/compileopts/target.go @@ -231,16 +231,15 @@ func defaultTarget(goos, goarch, triple string) (*TargetSpec, error) { // No target spec available. Use the default one, useful on most systems // with a regular OS. spec := TargetSpec{ - Triple: triple, - GOOS: goos, - GOARCH: goarch, - BuildTags: []string{goos, goarch}, - Compiler: "clang", - Linker: "cc", - CFlags: []string{"--target=" + triple}, - GDB: "gdb", - PortReset: "false", - FlashMethod: "native", + Triple: triple, + GOOS: goos, + GOARCH: goarch, + BuildTags: []string{goos, goarch}, + Compiler: "clang", + Linker: "cc", + CFlags: []string{"--target=" + triple}, + GDB: "gdb", + PortReset: "false", } if goos == "darwin" { spec.LDFlags = append(spec.LDFlags, "-Wl,-dead_strip") @@ -249,16 +248,15 @@ func defaultTarget(goos, goarch, triple string) (*TargetSpec, error) { } if goarch != runtime.GOARCH { // Some educated guesses as to how to invoke helper programs. + spec.GDB = "gdb-multiarch" if goarch == "arm" && goos == "linux" { spec.CFlags = append(spec.CFlags, "--sysroot=/usr/arm-linux-gnueabihf") spec.Linker = "arm-linux-gnueabihf-gcc" - spec.GDB = "arm-linux-gnueabihf-gdb" spec.Emulator = []string{"qemu-arm", "-L", "/usr/arm-linux-gnueabihf"} } if goarch == "arm64" && goos == "linux" { spec.CFlags = append(spec.CFlags, "--sysroot=/usr/aarch64-linux-gnu") spec.Linker = "aarch64-linux-gnu-gcc" - spec.GDB = "aarch64-linux-gnu-gdb" spec.Emulator = []string{"qemu-aarch64", "-L", "/usr/aarch64-linux-gnu"} } if goarch == "386" && runtime.GOARCH == "amd64" { diff --git a/main.go b/main.go index 66501965..993463cb 100644 --- a/main.go +++ b/main.go @@ -351,13 +351,17 @@ func FlashGDB(pkgName string, ocdOutput bool, options *compileopts.Options) erro // Assume QEMU as an emulator. if config.Target.Emulator[0] == "mgba" { gdbInterface = "mgba" - } else { + } else if strings.HasPrefix(config.Target.Emulator[0], "qemu-system-") { gdbInterface = "qemu" + } else { + gdbInterface = "qemu-user" } } else if openocdInterface != "" && config.Target.OpenOCDTarget != "" { gdbInterface = "openocd" } else if config.Target.JLinkDevice != "" { gdbInterface = "jlink" + } else { + gdbInterface = "native" } } @@ -409,6 +413,14 @@ func FlashGDB(pkgName string, ocdOutput bool, options *compileopts.Options) erro daemon = exec.Command(config.Target.Emulator[0], args...) daemon.Stdout = os.Stdout daemon.Stderr = os.Stderr + case "qemu-user": + gdbCommands = append(gdbCommands, "target remote :1234") + + // Run in an emulator. + args := append(config.Target.Emulator[1:], "-g", "1234", result.Binary) + daemon = exec.Command(config.Target.Emulator[0], args...) + daemon.Stdout = os.Stdout + daemon.Stderr = os.Stderr case "mgba": gdbCommands = append(gdbCommands, "target remote :2345")