Skip to content

Commit d41da43

Browse files
authored
Added debug check command to check if a combination of board/programmer supports debugging. (#2443)
* Moved rcp message to proper position * Added gRPC command to check for debugger support * Made debug flags var non-global * Implementation of 'debug check' command * Implementation of cli command 'debug check' * added integration test * Renamed field for clarity * Added minimum debug_fqbn computation in 'debug check' command
1 parent 5d3f7c5 commit d41da43

File tree

15 files changed

+916
-368
lines changed

15 files changed

+916
-368
lines changed

Diff for: arduino/cores/fqbn.go

+10
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ type FQBN struct {
3030
Configs *properties.Map
3131
}
3232

33+
// MustParseFQBN extract an FQBN object from the input string
34+
// or panics if the input is not a valid FQBN.
35+
func MustParseFQBN(fqbnIn string) *FQBN {
36+
res, err := ParseFQBN(fqbnIn)
37+
if err != nil {
38+
panic(err)
39+
}
40+
return res
41+
}
42+
3343
// ParseFQBN extract an FQBN object from the input string
3444
func ParseFQBN(fqbnIn string) (*FQBN, error) {
3545
// Split fqbn

Diff for: commands/daemon/debug.go

+6
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,9 @@ func (s *ArduinoCoreServerImpl) GetDebugConfig(ctx context.Context, req *rpc.Get
6666
res, err := cmd.GetDebugConfig(ctx, req)
6767
return res, convertErrorToRPCStatus(err)
6868
}
69+
70+
// IsDebugSupported checks if debugging is supported for a given configuration
71+
func (s *ArduinoCoreServerImpl) IsDebugSupported(ctx context.Context, req *rpc.IsDebugSupportedRequest) (*rpc.IsDebugSupportedResponse, error) {
72+
res, err := cmd.IsDebugSupported(ctx, req)
73+
return res, convertErrorToRPCStatus(err)
74+
}

Diff for: commands/debug/debug.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func Debug(ctx context.Context, req *rpc.GetDebugConfigRequest, inStream io.Read
118118

119119
// getCommandLine compose a debug command represented by a core recipe
120120
func getCommandLine(req *rpc.GetDebugConfigRequest, pme *packagemanager.Explorer) ([]string, error) {
121-
debugInfo, err := getDebugProperties(req, pme)
121+
debugInfo, err := getDebugProperties(req, pme, false)
122122
if err != nil {
123123
return nil, err
124124
}

Diff for: commands/debug/debug_info.go

+80-18
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ package debug
1818
import (
1919
"context"
2020
"encoding/json"
21+
"errors"
22+
"reflect"
2123
"slices"
2224
"strconv"
2325
"strings"
@@ -41,25 +43,83 @@ func GetDebugConfig(ctx context.Context, req *rpc.GetDebugConfigRequest) (*rpc.G
4143
return nil, &arduino.InvalidInstanceError{}
4244
}
4345
defer release()
44-
return getDebugProperties(req, pme)
46+
return getDebugProperties(req, pme, false)
4547
}
4648

47-
func getDebugProperties(req *rpc.GetDebugConfigRequest, pme *packagemanager.Explorer) (*rpc.GetDebugConfigResponse, error) {
48-
// TODO: make a generic function to extract sketch from request
49-
// and remove duplication in commands/compile.go
50-
if req.GetSketchPath() == "" {
51-
return nil, &arduino.MissingSketchPathError{}
49+
// IsDebugSupported checks if the given board/programmer configuration supports debugging.
50+
func IsDebugSupported(ctx context.Context, req *rpc.IsDebugSupportedRequest) (*rpc.IsDebugSupportedResponse, error) {
51+
pme, release := instances.GetPackageManagerExplorer(req.GetInstance())
52+
if pme == nil {
53+
return nil, &arduino.InvalidInstanceError{}
54+
}
55+
defer release()
56+
configRequest := &rpc.GetDebugConfigRequest{
57+
Instance: req.GetInstance(),
58+
Fqbn: req.GetFqbn(),
59+
SketchPath: "",
60+
Port: req.GetPort(),
61+
Interpreter: req.GetInterpreter(),
62+
ImportDir: "",
63+
Programmer: req.GetProgrammer(),
64+
}
65+
expectedOutput, err := getDebugProperties(configRequest, pme, true)
66+
var x *arduino.FailedDebugError
67+
if errors.As(err, &x) {
68+
return &rpc.IsDebugSupportedResponse{DebuggingSupported: false}, nil
5269
}
53-
sketchPath := paths.New(req.GetSketchPath())
54-
sk, err := sketch.New(sketchPath)
5570
if err != nil {
56-
return nil, &arduino.CantOpenSketchError{Cause: err}
71+
return nil, err
72+
}
73+
74+
// Compute the minimum FQBN required to get the same debug configuration.
75+
// (i.e. the FQBN cleaned up of the options that do not affect the debugger configuration)
76+
minimumFQBN := cores.MustParseFQBN(req.GetFqbn())
77+
for _, config := range minimumFQBN.Configs.Keys() {
78+
checkFQBN := minimumFQBN.Clone()
79+
checkFQBN.Configs.Remove(config)
80+
configRequest.Fqbn = checkFQBN.String()
81+
checkOutput, err := getDebugProperties(configRequest, pme, true)
82+
if err == nil && reflect.DeepEqual(expectedOutput, checkOutput) {
83+
minimumFQBN.Configs.Remove(config)
84+
}
85+
}
86+
return &rpc.IsDebugSupportedResponse{
87+
DebuggingSupported: true,
88+
DebugFqbn: minimumFQBN.String(),
89+
}, nil
90+
}
91+
92+
func getDebugProperties(req *rpc.GetDebugConfigRequest, pme *packagemanager.Explorer, skipSketchChecks bool) (*rpc.GetDebugConfigResponse, error) {
93+
var (
94+
sketchName string
95+
sketchDefaultFQBN string
96+
sketchDefaultBuildPath *paths.Path
97+
)
98+
if !skipSketchChecks {
99+
// TODO: make a generic function to extract sketch from request
100+
// and remove duplication in commands/compile.go
101+
if req.GetSketchPath() == "" {
102+
return nil, &arduino.MissingSketchPathError{}
103+
}
104+
sketchPath := paths.New(req.GetSketchPath())
105+
sk, err := sketch.New(sketchPath)
106+
if err != nil {
107+
return nil, &arduino.CantOpenSketchError{Cause: err}
108+
}
109+
sketchName = sk.Name
110+
sketchDefaultFQBN = sk.GetDefaultFQBN()
111+
sketchDefaultBuildPath = sk.DefaultBuildPath()
112+
} else {
113+
// Use placeholder sketch data
114+
sketchName = "Sketch"
115+
sketchDefaultFQBN = ""
116+
sketchDefaultBuildPath = paths.New("SketchBuildPath")
57117
}
58118

59119
// XXX Remove this code duplication!!
60120
fqbnIn := req.GetFqbn()
61-
if fqbnIn == "" && sk != nil {
62-
fqbnIn = sk.GetDefaultFQBN()
121+
if fqbnIn == "" {
122+
fqbnIn = sketchDefaultFQBN
63123
}
64124
if fqbnIn == "" {
65125
return nil, &arduino.MissingFQBNError{}
@@ -109,16 +169,18 @@ func getDebugProperties(req *rpc.GetDebugConfigRequest, pme *packagemanager.Expl
109169
if importDir := req.GetImportDir(); importDir != "" {
110170
importPath = paths.New(importDir)
111171
} else {
112-
importPath = sk.DefaultBuildPath()
113-
}
114-
if !importPath.Exist() {
115-
return nil, &arduino.NotFoundError{Message: tr("Compiled sketch not found in %s", importPath)}
172+
importPath = sketchDefaultBuildPath
116173
}
117-
if !importPath.IsDir() {
118-
return nil, &arduino.NotFoundError{Message: tr("Expected compiled sketch in directory %s, but is a file instead", importPath)}
174+
if !skipSketchChecks {
175+
if !importPath.Exist() {
176+
return nil, &arduino.NotFoundError{Message: tr("Compiled sketch not found in %s", importPath)}
177+
}
178+
if !importPath.IsDir() {
179+
return nil, &arduino.NotFoundError{Message: tr("Expected compiled sketch in directory %s, but is a file instead", importPath)}
180+
}
119181
}
120182
toolProperties.SetPath("build.path", importPath)
121-
toolProperties.Set("build.project_name", sk.Name+".ino")
183+
toolProperties.Set("build.project_name", sketchName+".ino")
122184

123185
// Set debug port property
124186
port := req.GetPort()

Diff for: internal/cli/debug/debug.go

+17-12
Original file line numberDiff line numberDiff line change
@@ -36,27 +36,31 @@ import (
3636
"github.com/spf13/cobra"
3737
)
3838

39-
var (
40-
fqbnArg arguments.Fqbn
41-
portArgs arguments.Port
42-
interpreter string
43-
importDir string
44-
printInfo bool
45-
programmer arguments.Programmer
46-
tr = i18n.Tr
47-
)
39+
var tr = i18n.Tr
4840

4941
// NewCommand created a new `upload` command
5042
func NewCommand() *cobra.Command {
43+
var (
44+
fqbnArg arguments.Fqbn
45+
portArgs arguments.Port
46+
interpreter string
47+
importDir string
48+
printInfo bool
49+
programmer arguments.Programmer
50+
)
51+
5152
debugCommand := &cobra.Command{
5253
Use: "debug",
5354
Short: tr("Debug Arduino sketches."),
5455
Long: tr("Debug Arduino sketches. (this command opens an interactive gdb session)"),
5556
Example: " " + os.Args[0] + " debug -b arduino:samd:mkr1000 -P atmel_ice /home/user/Arduino/MySketch",
5657
Args: cobra.MaximumNArgs(1),
57-
Run: runDebugCommand,
58+
Run: func(cmd *cobra.Command, args []string) {
59+
runDebugCommand(args, &portArgs, &fqbnArg, interpreter, importDir, &programmer, printInfo)
60+
},
5861
}
5962

63+
debugCommand.AddCommand(newDebugCheckCommand())
6064
fqbnArg.AddToCommand(debugCommand)
6165
portArgs.AddToCommand(debugCommand)
6266
programmer.AddToCommand(debugCommand)
@@ -67,7 +71,8 @@ func NewCommand() *cobra.Command {
6771
return debugCommand
6872
}
6973

70-
func runDebugCommand(command *cobra.Command, args []string) {
74+
func runDebugCommand(args []string, portArgs *arguments.Port, fqbnArg *arguments.Fqbn,
75+
interpreter string, importDir string, programmer *arguments.Programmer, printInfo bool) {
7176
instance := instance.CreateAndInit()
7277
logrus.Info("Executing `arduino-cli debug`")
7378

@@ -81,7 +86,7 @@ func runDebugCommand(command *cobra.Command, args []string) {
8186
if err != nil {
8287
feedback.FatalError(err, feedback.ErrGeneric)
8388
}
84-
fqbn, port := arguments.CalculateFQBNAndPort(&portArgs, &fqbnArg, instance, sk.GetDefaultFqbn(), sk.GetDefaultPort(), sk.GetDefaultProtocol())
89+
fqbn, port := arguments.CalculateFQBNAndPort(portArgs, fqbnArg, instance, sk.GetDefaultFqbn(), sk.GetDefaultPort(), sk.GetDefaultProtocol())
8590
debugConfigRequested := &rpc.GetDebugConfigRequest{
8691
Instance: instance,
8792
Fqbn: fqbn,

Diff for: internal/cli/debug/debug_check.go

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2023 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 [email protected].
15+
16+
package debug
17+
18+
import (
19+
"context"
20+
"os"
21+
22+
"github.com/arduino/arduino-cli/commands/debug"
23+
"github.com/arduino/arduino-cli/internal/cli/arguments"
24+
"github.com/arduino/arduino-cli/internal/cli/feedback"
25+
"github.com/arduino/arduino-cli/internal/cli/feedback/result"
26+
"github.com/arduino/arduino-cli/internal/cli/instance"
27+
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
28+
"github.com/sirupsen/logrus"
29+
"github.com/spf13/cobra"
30+
)
31+
32+
func newDebugCheckCommand() *cobra.Command {
33+
var (
34+
fqbnArg arguments.Fqbn
35+
portArgs arguments.Port
36+
interpreter string
37+
programmer arguments.Programmer
38+
)
39+
debugCheckCommand := &cobra.Command{
40+
Use: "check",
41+
Short: tr("Check if the given board/programmer combination supports debugging."),
42+
Example: " " + os.Args[0] + " debug check -b arduino:samd:mkr1000 -P atmel_ice",
43+
Run: func(cmd *cobra.Command, args []string) {
44+
runDebugCheckCommand(&portArgs, &fqbnArg, interpreter, &programmer)
45+
},
46+
}
47+
fqbnArg.AddToCommand(debugCheckCommand)
48+
portArgs.AddToCommand(debugCheckCommand)
49+
programmer.AddToCommand(debugCheckCommand)
50+
debugCheckCommand.Flags().StringVar(&interpreter, "interpreter", "console", tr("Debug interpreter e.g.: %s", "console, mi, mi1, mi2, mi3"))
51+
return debugCheckCommand
52+
}
53+
54+
func runDebugCheckCommand(portArgs *arguments.Port, fqbnArg *arguments.Fqbn, interpreter string, programmerArg *arguments.Programmer) {
55+
instance := instance.CreateAndInit()
56+
logrus.Info("Executing `arduino-cli debug`")
57+
58+
port, err := portArgs.GetPort(instance, "", "")
59+
if err != nil {
60+
feedback.FatalError(err, feedback.ErrBadArgument)
61+
}
62+
fqbn := fqbnArg.String()
63+
resp, err := debug.IsDebugSupported(context.Background(), &rpc.IsDebugSupportedRequest{
64+
Instance: instance,
65+
Fqbn: fqbn,
66+
Port: port,
67+
Interpreter: interpreter,
68+
Programmer: programmerArg.String(instance, fqbn),
69+
})
70+
if err != nil {
71+
feedback.FatalError(err, feedback.ErrGeneric)
72+
}
73+
feedback.PrintResult(&debugCheckResult{result.NewIsDebugSupportedResponse(resp)})
74+
}
75+
76+
type debugCheckResult struct {
77+
Result *result.IsDebugSupportedResponse
78+
}
79+
80+
func (d *debugCheckResult) Data() interface{} {
81+
return d.Result
82+
}
83+
84+
func (d *debugCheckResult) String() string {
85+
if d.Result.DebuggingSupported {
86+
return tr("The given board/programmer configuration supports debugging.")
87+
}
88+
return tr("The given board/programmer configuration does NOT support debugging.")
89+
}

Diff for: internal/cli/feedback/result/rpc.go

+12
Original file line numberDiff line numberDiff line change
@@ -1057,3 +1057,15 @@ func NewCompileDiagnosticNote(cdn *rpc.CompileDiagnosticNote) *CompileDiagnostic
10571057
Column: cdn.GetColumn(),
10581058
}
10591059
}
1060+
1061+
type IsDebugSupportedResponse struct {
1062+
DebuggingSupported bool `json:"debugging_supported"`
1063+
DebugFQBN string `json:"debug_fqbn,omitempty"`
1064+
}
1065+
1066+
func NewIsDebugSupportedResponse(resp *rpc.IsDebugSupportedResponse) *IsDebugSupportedResponse {
1067+
return &IsDebugSupportedResponse{
1068+
DebuggingSupported: resp.GetDebuggingSupported(),
1069+
DebugFQBN: resp.GetDebugFqbn(),
1070+
}
1071+
}

Diff for: internal/cli/feedback/result/rpc_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,10 @@ func TestAllFieldAreMapped(t *testing.T) {
217217
compileDiagnosticNoteRpc := &rpc.CompileDiagnosticNote{}
218218
compileDiagnosticNoteResult := result.NewCompileDiagnosticNote(compileDiagnosticNoteRpc)
219219
mustContainsAllPropertyOfRpcStruct(t, compileDiagnosticNoteRpc, compileDiagnosticNoteResult)
220+
221+
isDebugSupportedResponseRpc := &rpc.IsDebugSupportedResponse{}
222+
isDebugSupportedResponseResult := result.NewIsDebugSupportedResponse(isDebugSupportedResponseRpc)
223+
mustContainsAllPropertyOfRpcStruct(t, isDebugSupportedResponseRpc, isDebugSupportedResponseResult)
220224
}
221225

222226
func TestEnumsMapsEveryRpcCounterpart(t *testing.T) {

0 commit comments

Comments
 (0)