Skip to content

Commit e4a031e

Browse files
author
rsora
committed
Implement debug command
1 parent 07aee7d commit e4a031e

File tree

4 files changed

+116
-11
lines changed

4 files changed

+116
-11
lines changed

cli/cli.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package cli
1717

1818
import (
1919
"fmt"
20+
"github.com/arduino/arduino-cli/cli/debug"
2021
"io/ioutil"
2122
"os"
2223
"path/filepath"
@@ -77,6 +78,7 @@ func createCliCommandTree(cmd *cobra.Command) {
7778
cmd.AddCommand(lib.NewCommand())
7879
cmd.AddCommand(sketch.NewCommand())
7980
cmd.AddCommand(upload.NewCommand())
81+
cmd.AddCommand(debug.NewCommand())
8082
cmd.AddCommand(version.NewCommand())
8183

8284
cmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Print the logs on the standard output.")

cli/debug/debug.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to license@arduino.cc.
15+
16+
package debug
17+
18+
import (
19+
"context"
20+
"os"
21+
22+
"github.com/arduino/arduino-cli/cli/errorcodes"
23+
"github.com/arduino/arduino-cli/cli/feedback"
24+
"github.com/arduino/arduino-cli/cli/instance"
25+
"github.com/arduino/arduino-cli/commands/debug"
26+
dbg "github.com/arduino/arduino-cli/rpc/debug"
27+
"github.com/arduino/go-paths-helper"
28+
"github.com/sirupsen/logrus"
29+
"github.com/spf13/cobra"
30+
)
31+
32+
var (
33+
fqbn string
34+
port string
35+
verbose bool
36+
verify bool
37+
importFile string
38+
)
39+
40+
// NewCommand created a new `upload` command
41+
func NewCommand() *cobra.Command {
42+
debugCommand := &cobra.Command{
43+
Use: "debug",
44+
Short: "Debug Arduino sketches.",
45+
Long: "Debug Arduino sketches.",
46+
Example: " " + os.Args[0] + " debug /home/user/Arduino/MySketch",
47+
Args: cobra.MaximumNArgs(1),
48+
Run: run,
49+
}
50+
51+
debugCommand.Flags().StringVarP(&fqbn, "fqbn", "b", "", "Fully Qualified Board Name, e.g.: arduino:avr:uno")
52+
debugCommand.Flags().StringVarP(&port, "port", "p", "", "Upload port, e.g.: COM10 or /dev/ttyACM0")
53+
debugCommand.Flags().StringVarP(&importFile, "input", "i", "", "Input file to be uploaded.")
54+
55+
debugCommand.MarkFlagRequired("port")
56+
57+
return debugCommand
58+
}
59+
60+
func run(command *cobra.Command, args []string) {
61+
instance, err := instance.CreateInstance()
62+
if err != nil {
63+
feedback.Errorf("Error during Debug: %v", err)
64+
os.Exit(errorcodes.ErrGeneric)
65+
}
66+
67+
var path *paths.Path
68+
if len(args) > 0 {
69+
path = paths.New(args[0])
70+
}
71+
sketchPath := initSketchPath(path)
72+
73+
if _, err := debug.Debug(context.Background(), &dbg.DebugConfigReq{
74+
Instance: &dbg.Instance{Id: instance.GetId()},
75+
Fqbn: fqbn,
76+
SketchPath: sketchPath.String(),
77+
Port: port,
78+
ImportFile: importFile,
79+
}, os.Stdin, os.Stdout); err != nil {
80+
feedback.Errorf("Error during Upload: %v", err)
81+
os.Exit(errorcodes.ErrGeneric)
82+
}
83+
}
84+
85+
// initSketchPath returns the current working directory
86+
func initSketchPath(sketchPath *paths.Path) *paths.Path {
87+
if sketchPath != nil {
88+
return sketchPath
89+
}
90+
91+
wd, err := paths.Getwd()
92+
if err != nil {
93+
feedback.Errorf("Couldn't get current working directory: %v", err)
94+
os.Exit(errorcodes.ErrGeneric)
95+
}
96+
logrus.Infof("Reading sketch from dir: %s", wd)
97+
return wd
98+
}

commands/daemon/debug.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ package daemon
1717

1818
import (
1919
"fmt"
20-
2120
cmd "github.com/arduino/arduino-cli/commands/debug"
21+
"io"
22+
2223
dbg "github.com/arduino/arduino-cli/rpc/debug"
2324
)
2425

@@ -42,8 +43,19 @@ func (s *DebugService) Debug(stream dbg.Debug_DebugServer) error {
4243
return fmt.Errorf("first message must contain debug request, not data")
4344
}
4445

46+
r, w := io.Pipe()
47+
go func() {
48+
for {
49+
if command, err := stream.Recv(); err != nil {
50+
return
51+
} else if _, err := w.Write(command.GetData()); err != nil {
52+
return
53+
}
54+
}
55+
}()
56+
4557
// launch debug recipe attaching stdin and out to grpc streaming
46-
resp, err := cmd.Debug(stream.Context(), req, stream, feedStream(func(data []byte) {
58+
resp, err := cmd.Debug(stream.Context(), req, r, feedStream(func(data []byte) {
4759
stream.Send(&dbg.DebugResp{Data: data})
4860
}))
4961
if err != nil {

commands/debug/debug.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import (
3636
)
3737

3838
// Debug FIXMEDOC
39-
func Debug(ctx context.Context, req *dbg.DebugConfigReq, inStream dbg.Debug_DebugServer, out io.Writer) (*dbg.DebugResp, error) {
39+
func Debug(ctx context.Context, req *dbg.DebugConfigReq, inStream io.Reader, out io.Writer) (*dbg.DebugResp, error) {
4040

4141
// TODO: make a generic function to extract sketch from request
4242
// and remove duplication in commands/compile.go
@@ -208,14 +208,7 @@ func Debug(ctx context.Context, req *dbg.DebugConfigReq, inStream dbg.Debug_Debu
208208

209209
// now we can read the other commands and re-route to the Debug Client...
210210
go func() {
211-
for {
212-
if command, err := inStream.Recv(); err != nil {
213-
break
214-
} else if _, err := in.Write(command.GetData()); err != nil {
215-
break
216-
}
217-
}
218-
211+
io.Copy(in, inStream)
219212
// In any case, try process termination after a second to avoid leaving
220213
// zombie process.
221214
time.Sleep(time.Second)

0 commit comments

Comments
 (0)