Skip to content

Commit 7123941

Browse files
aykevldeadprogram
authored andcommitted
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
1 parent 05d2f2c commit 7123941

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

compileopts/target.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -231,16 +231,15 @@ func defaultTarget(goos, goarch, triple string) (*TargetSpec, error) {
231231
// No target spec available. Use the default one, useful on most systems
232232
// with a regular OS.
233233
spec := TargetSpec{
234-
Triple: triple,
235-
GOOS: goos,
236-
GOARCH: goarch,
237-
BuildTags: []string{goos, goarch},
238-
Compiler: "clang",
239-
Linker: "cc",
240-
CFlags: []string{"--target=" + triple},
241-
GDB: "gdb",
242-
PortReset: "false",
243-
FlashMethod: "native",
234+
Triple: triple,
235+
GOOS: goos,
236+
GOARCH: goarch,
237+
BuildTags: []string{goos, goarch},
238+
Compiler: "clang",
239+
Linker: "cc",
240+
CFlags: []string{"--target=" + triple},
241+
GDB: "gdb",
242+
PortReset: "false",
244243
}
245244
if goos == "darwin" {
246245
spec.LDFlags = append(spec.LDFlags, "-Wl,-dead_strip")
@@ -249,16 +248,15 @@ func defaultTarget(goos, goarch, triple string) (*TargetSpec, error) {
249248
}
250249
if goarch != runtime.GOARCH {
251250
// Some educated guesses as to how to invoke helper programs.
251+
spec.GDB = "gdb-multiarch"
252252
if goarch == "arm" && goos == "linux" {
253253
spec.CFlags = append(spec.CFlags, "--sysroot=/usr/arm-linux-gnueabihf")
254254
spec.Linker = "arm-linux-gnueabihf-gcc"
255-
spec.GDB = "arm-linux-gnueabihf-gdb"
256255
spec.Emulator = []string{"qemu-arm", "-L", "/usr/arm-linux-gnueabihf"}
257256
}
258257
if goarch == "arm64" && goos == "linux" {
259258
spec.CFlags = append(spec.CFlags, "--sysroot=/usr/aarch64-linux-gnu")
260259
spec.Linker = "aarch64-linux-gnu-gcc"
261-
spec.GDB = "aarch64-linux-gnu-gdb"
262260
spec.Emulator = []string{"qemu-aarch64", "-L", "/usr/aarch64-linux-gnu"}
263261
}
264262
if goarch == "386" && runtime.GOARCH == "amd64" {

main.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,13 +351,17 @@ func FlashGDB(pkgName string, ocdOutput bool, options *compileopts.Options) erro
351351
// Assume QEMU as an emulator.
352352
if config.Target.Emulator[0] == "mgba" {
353353
gdbInterface = "mgba"
354-
} else {
354+
} else if strings.HasPrefix(config.Target.Emulator[0], "qemu-system-") {
355355
gdbInterface = "qemu"
356+
} else {
357+
gdbInterface = "qemu-user"
356358
}
357359
} else if openocdInterface != "" && config.Target.OpenOCDTarget != "" {
358360
gdbInterface = "openocd"
359361
} else if config.Target.JLinkDevice != "" {
360362
gdbInterface = "jlink"
363+
} else {
364+
gdbInterface = "native"
361365
}
362366
}
363367

@@ -409,6 +413,14 @@ func FlashGDB(pkgName string, ocdOutput bool, options *compileopts.Options) erro
409413
daemon = exec.Command(config.Target.Emulator[0], args...)
410414
daemon.Stdout = os.Stdout
411415
daemon.Stderr = os.Stderr
416+
case "qemu-user":
417+
gdbCommands = append(gdbCommands, "target remote :1234")
418+
419+
// Run in an emulator.
420+
args := append(config.Target.Emulator[1:], "-g", "1234", result.Binary)
421+
daemon = exec.Command(config.Target.Emulator[0], args...)
422+
daemon.Stdout = os.Stdout
423+
daemon.Stderr = os.Stderr
412424
case "mgba":
413425
gdbCommands = append(gdbCommands, "target remote :2345")
414426

0 commit comments

Comments
 (0)