main: move some duplicate code to a single place
This makes fixes like https://github.com/tinygo-org/tinygo/pull/915 easier to apply. And in general, avoiding duplication is a good thing.
Этот коммит содержится в:
родитель
6a50f25a48
коммит
63cfb09e9e
1 изменённых файлов: 18 добавлений и 53 удалений
71
main.go
71
main.go
|
@ -313,6 +313,7 @@ func FlashGDB(pkgName string, ocdOutput bool, options *compileopts.Options) erro
|
||||||
|
|
||||||
// Run the GDB server, if necessary.
|
// Run the GDB server, if necessary.
|
||||||
var gdbCommands []string
|
var gdbCommands []string
|
||||||
|
var daemon *exec.Cmd
|
||||||
switch gdbInterface {
|
switch gdbInterface {
|
||||||
case "native":
|
case "native":
|
||||||
// Run GDB directly.
|
// Run GDB directly.
|
||||||
|
@ -324,7 +325,7 @@ func FlashGDB(pkgName string, ocdOutput bool, options *compileopts.Options) erro
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
daemon := exec.Command("openocd", args...)
|
daemon = exec.Command("openocd", args...)
|
||||||
if ocdOutput {
|
if ocdOutput {
|
||||||
// Make it clear which output is from the daemon.
|
// Make it clear which output is from the daemon.
|
||||||
w := &ColorWriter{
|
w := &ColorWriter{
|
||||||
|
@ -335,24 +336,11 @@ func FlashGDB(pkgName string, ocdOutput bool, options *compileopts.Options) erro
|
||||||
daemon.Stdout = w
|
daemon.Stdout = w
|
||||||
daemon.Stderr = w
|
daemon.Stderr = w
|
||||||
}
|
}
|
||||||
// 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.
|
|
||||||
err = daemon.Start()
|
|
||||||
if err != nil {
|
|
||||||
return &commandError{"failed to run", daemon.Path, err}
|
|
||||||
}
|
|
||||||
defer func() {
|
|
||||||
daemon.Process.Signal(os.Interrupt)
|
|
||||||
// Maybe we should send a .Kill() after x seconds?
|
|
||||||
daemon.Wait()
|
|
||||||
}()
|
|
||||||
case "jlink":
|
case "jlink":
|
||||||
gdbCommands = append(gdbCommands, "target remote :2331", "load", "monitor reset halt")
|
gdbCommands = append(gdbCommands, "target remote :2331", "load", "monitor reset halt")
|
||||||
|
|
||||||
// We need a separate debugging daemon for on-chip debugging.
|
// We need a separate debugging daemon for on-chip debugging.
|
||||||
daemon := exec.Command("JLinkGDBServer", "-device", config.Target.JLinkDevice)
|
daemon = exec.Command("JLinkGDBServer", "-device", config.Target.JLinkDevice)
|
||||||
if ocdOutput {
|
if ocdOutput {
|
||||||
// Make it clear which output is from the daemon.
|
// Make it clear which output is from the daemon.
|
||||||
w := &ColorWriter{
|
w := &ColorWriter{
|
||||||
|
@ -363,28 +351,29 @@ func FlashGDB(pkgName string, ocdOutput bool, options *compileopts.Options) erro
|
||||||
daemon.Stdout = w
|
daemon.Stdout = w
|
||||||
daemon.Stderr = w
|
daemon.Stderr = w
|
||||||
}
|
}
|
||||||
// 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.
|
|
||||||
err = daemon.Start()
|
|
||||||
if err != nil {
|
|
||||||
return &commandError{"failed to run", daemon.Path, err}
|
|
||||||
}
|
|
||||||
defer func() {
|
|
||||||
daemon.Process.Signal(os.Interrupt)
|
|
||||||
// Maybe we should send a .Kill() after x seconds?
|
|
||||||
daemon.Wait()
|
|
||||||
}()
|
|
||||||
case "qemu":
|
case "qemu":
|
||||||
gdbCommands = append(gdbCommands, "target remote :1234")
|
gdbCommands = append(gdbCommands, "target remote :1234")
|
||||||
|
|
||||||
// Run in an emulator.
|
// Run in an emulator.
|
||||||
args := append(config.Target.Emulator[1:], tmppath, "-s", "-S")
|
args := append(config.Target.Emulator[1:], tmppath, "-s", "-S")
|
||||||
daemon := exec.Command(config.Target.Emulator[0], args...)
|
daemon = exec.Command(config.Target.Emulator[0], args...)
|
||||||
daemon.Stdout = os.Stdout
|
daemon.Stdout = os.Stdout
|
||||||
daemon.Stderr = os.Stderr
|
daemon.Stderr = os.Stderr
|
||||||
|
case "mgba":
|
||||||
|
gdbCommands = append(gdbCommands, "target remote :2345")
|
||||||
|
|
||||||
|
// Run in an emulator.
|
||||||
|
args := append(config.Target.Emulator[1:], tmppath, "-g")
|
||||||
|
daemon = exec.Command(config.Target.Emulator[0], args...)
|
||||||
|
daemon.Stdout = os.Stdout
|
||||||
|
daemon.Stderr = os.Stderr
|
||||||
|
case "msd":
|
||||||
|
return errors.New("gdb is not supported for drag-and-drop programmable devices")
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("gdb is not supported with interface %#v", gdbInterface)
|
||||||
|
}
|
||||||
|
|
||||||
|
if daemon != nil {
|
||||||
// Make sure the daemon doesn't receive Ctrl-C that is intended for
|
// Make sure the daemon doesn't receive Ctrl-C that is intended for
|
||||||
// GDB (to break the currently executing program).
|
// GDB (to break the currently executing program).
|
||||||
setCommandAsDaemon(daemon)
|
setCommandAsDaemon(daemon)
|
||||||
|
@ -399,30 +388,6 @@ func FlashGDB(pkgName string, ocdOutput bool, options *compileopts.Options) erro
|
||||||
// Maybe we should send a .Kill() after x seconds?
|
// Maybe we should send a .Kill() after x seconds?
|
||||||
daemon.Wait()
|
daemon.Wait()
|
||||||
}()
|
}()
|
||||||
case "mgba":
|
|
||||||
gdbCommands = append(gdbCommands, "target remote :2345")
|
|
||||||
|
|
||||||
// Run in an emulator.
|
|
||||||
args := append(config.Target.Emulator[1:], tmppath, "-g")
|
|
||||||
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() {
|
|
||||||
daemon.Process.Signal(os.Interrupt)
|
|
||||||
// Maybe we should send a .Kill() after x seconds?
|
|
||||||
daemon.Wait()
|
|
||||||
}()
|
|
||||||
case "msd":
|
|
||||||
return errors.New("gdb is not supported for drag-and-drop programmable devices")
|
|
||||||
default:
|
|
||||||
return fmt.Errorf("gdb is not supported with interface %#v", gdbInterface)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ignore Ctrl-C, it must be passed on to GDB.
|
// Ignore Ctrl-C, it must be passed on to GDB.
|
||||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче