Skip to content

Commit 1d49b6a

Browse files
authored
Use debug "configurations" instead or "recipes" (#1033)
* Added 'debug --info/-I' flag to get debug metadata * Debugger: do not use anymore pre-made recipes * Added nicer color ouput for 'debug --info' * Removed deprecated field * Merged together DebugConfigReq protoc type It makes no sense to have two separate types to create a debug session or to create a debug configuration. * Debug: added programmer selection flag * debug: return error if debugging is not supported by the platform * debug: openocd server now require full path to openocd executable * debug: fixed test runner * debugger: Fixed command line tests * debugger: hotfix for samd platforms 1.8.8 and 1.8.9
1 parent 8cc7127 commit 1d49b6a

File tree

10 files changed

+629
-194
lines changed

10 files changed

+629
-194
lines changed

Diff for: cli/debug/debug.go

+79-11
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,23 @@ package debug
1717

1818
import (
1919
"context"
20+
"fmt"
2021
"os"
2122
"os/signal"
23+
"sort"
2224

2325
"github.com/arduino/arduino-cli/cli/errorcodes"
2426
"github.com/arduino/arduino-cli/cli/feedback"
2527
"github.com/arduino/arduino-cli/cli/instance"
2628
"github.com/arduino/arduino-cli/commands/debug"
27-
rpc "github.com/arduino/arduino-cli/rpc/commands"
2829
dbg "github.com/arduino/arduino-cli/rpc/debug"
30+
"github.com/arduino/arduino-cli/table"
2931
"github.com/arduino/go-paths-helper"
32+
"github.com/arduino/go-properties-orderedmap"
33+
"github.com/fatih/color"
3034
"github.com/sirupsen/logrus"
3135
"github.com/spf13/cobra"
36+
"google.golang.org/grpc/status"
3237
)
3338

3439
var (
@@ -38,6 +43,8 @@ var (
3843
verify bool
3944
interpreter string
4045
importDir string
46+
printInfo bool
47+
programmer string
4148
)
4249

4350
// NewCommand created a new `upload` command
@@ -46,15 +53,17 @@ func NewCommand() *cobra.Command {
4653
Use: "debug",
4754
Short: "Debug Arduino sketches.",
4855
Long: "Debug Arduino sketches. (this command opens an interactive gdb session)",
49-
Example: " " + os.Args[0] + " debug -b arduino:samd:mkr1000 /home/user/Arduino/MySketch",
56+
Example: " " + os.Args[0] + " debug -b arduino:samd:mkr1000 -P atmel_ice /home/user/Arduino/MySketch",
5057
Args: cobra.MaximumNArgs(1),
5158
Run: run,
5259
}
5360

5461
debugCommand.Flags().StringVarP(&fqbn, "fqbn", "b", "", "Fully Qualified Board Name, e.g.: arduino:avr:uno")
5562
debugCommand.Flags().StringVarP(&port, "port", "p", "", "Debug port, e.g.: COM10 or /dev/ttyACM0")
63+
debugCommand.Flags().StringVarP(&programmer, "programmer", "P", "", "Programmer to use for debugging")
5664
debugCommand.Flags().StringVar(&interpreter, "interpreter", "console", "Debug interpreter e.g.: console, mi, mi1, mi2, mi3")
5765
debugCommand.Flags().StringVarP(&importDir, "input-dir", "", "", "Directory containing binaries for debug.")
66+
debugCommand.Flags().BoolVarP(&printInfo, "info", "I", false, "Show metadata about the debug session instead of starting the debugger.")
5867

5968
return debugCommand
6069
}
@@ -72,20 +81,40 @@ func run(command *cobra.Command, args []string) {
7281
}
7382
sketchPath := initSketchPath(path)
7483

75-
// Intercept SIGINT and forward them to debug process
76-
ctrlc := make(chan os.Signal, 1)
77-
signal.Notify(ctrlc, os.Interrupt)
78-
79-
if _, err := debug.Debug(context.Background(), &dbg.DebugConfigReq{
80-
Instance: &rpc.Instance{Id: instance.GetId()},
84+
debugConfigRequested := &dbg.DebugConfigReq{
85+
Instance: instance,
8186
Fqbn: fqbn,
8287
SketchPath: sketchPath.String(),
8388
Port: port,
8489
Interpreter: interpreter,
8590
ImportDir: importDir,
86-
}, os.Stdin, os.Stdout, ctrlc); err != nil {
87-
feedback.Errorf("Error during Debug: %v", err)
88-
os.Exit(errorcodes.ErrGeneric)
91+
Programmer: programmer,
92+
}
93+
94+
if printInfo {
95+
96+
if res, err := debug.GetDebugConfig(context.Background(), debugConfigRequested); err != nil {
97+
if status, ok := status.FromError(err); ok {
98+
feedback.Errorf("Error getting Debug info: %v", status.Message())
99+
errorcodes.ExitWithGrpcStatus(status)
100+
}
101+
feedback.Errorf("Error getting Debug info: %v", err)
102+
os.Exit(errorcodes.ErrGeneric)
103+
} else {
104+
feedback.PrintResult(&debugInfoResult{res})
105+
}
106+
107+
} else {
108+
109+
// Intercept SIGINT and forward them to debug process
110+
ctrlc := make(chan os.Signal, 1)
111+
signal.Notify(ctrlc, os.Interrupt)
112+
113+
if _, err := debug.Debug(context.Background(), debugConfigRequested, os.Stdin, os.Stdout, ctrlc); err != nil {
114+
feedback.Errorf("Error during Debug: %v", err)
115+
os.Exit(errorcodes.ErrGeneric)
116+
}
117+
89118
}
90119
}
91120

@@ -103,3 +132,42 @@ func initSketchPath(sketchPath *paths.Path) *paths.Path {
103132
logrus.Infof("Reading sketch from dir: %s", wd)
104133
return wd
105134
}
135+
136+
type debugInfoResult struct {
137+
info *dbg.GetDebugConfigResp
138+
}
139+
140+
func (r *debugInfoResult) Data() interface{} {
141+
return r.info
142+
}
143+
144+
func (r *debugInfoResult) String() string {
145+
t := table.New()
146+
green := color.New(color.FgHiGreen)
147+
dimGreen := color.New(color.FgGreen)
148+
t.AddRow("Executable to debug", table.NewCell(r.info.GetExecutable(), green))
149+
t.AddRow("Toolchain type", table.NewCell(r.info.GetToolchain(), green))
150+
t.AddRow("Toolchain path", table.NewCell(r.info.GetToolchainPath(), dimGreen))
151+
t.AddRow("Toolchain prefix", table.NewCell(r.info.GetToolchainPrefix(), dimGreen))
152+
if len(r.info.GetToolchainConfiguration()) > 0 {
153+
conf := properties.NewFromHashmap(r.info.GetToolchainConfiguration())
154+
keys := conf.Keys()
155+
sort.Strings(keys)
156+
t.AddRow("Toolchain custom configurations")
157+
for _, k := range keys {
158+
t.AddRow(table.NewCell(" - "+k, dimGreen), table.NewCell(conf.Get(k), dimGreen))
159+
}
160+
}
161+
t.AddRow("GDB Server type", table.NewCell(r.info.GetServer(), green))
162+
t.AddRow("GDB Server path", table.NewCell(r.info.GetServerPath(), dimGreen))
163+
if len(r.info.GetServerConfiguration()) > 0 {
164+
conf := properties.NewFromHashmap(r.info.GetServerConfiguration())
165+
keys := conf.Keys()
166+
sort.Strings(keys)
167+
t.AddRow(fmt.Sprintf("%s custom configurations", r.info.GetServer()))
168+
for _, k := range keys {
169+
t.AddRow(table.NewCell(" - "+k, dimGreen), table.NewCell(conf.Get(k), dimGreen))
170+
}
171+
}
172+
return t.Render()
173+
}

Diff for: cli/errorcodes/errorcodes.go

+18
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@
1515

1616
package errorcodes
1717

18+
import (
19+
"os"
20+
21+
"google.golang.org/grpc/codes"
22+
"google.golang.org/grpc/status"
23+
)
24+
1825
// Error codes to be used for os.Exit().
1926
const (
2027
_ = iota // 0 is not a valid exit error code
@@ -29,3 +36,14 @@ const (
2936
ErrCoreConfig
3037
ErrBadArgument
3138
)
39+
40+
// ExitWithGrpcStatus will terminate the current process by returing the correct
41+
// error code closest matching the gRPC status.
42+
func ExitWithGrpcStatus(s *status.Status) {
43+
switch s.Code() {
44+
case codes.Unimplemented:
45+
os.Exit(ErrBadArgument)
46+
default:
47+
os.Exit(ErrGeneric)
48+
}
49+
}

Diff for: commands/daemon/debug.go

+7
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616
package daemon
1717

1818
import (
19+
"context"
1920
"os"
2021

2122
"github.com/arduino/arduino-cli/arduino/utils"
2223
cmd "github.com/arduino/arduino-cli/commands/debug"
24+
"github.com/arduino/arduino-cli/rpc/debug"
2325
dbg "github.com/arduino/arduino-cli/rpc/debug"
2426
"github.com/pkg/errors"
2527
)
@@ -64,3 +66,8 @@ func (s *DebugService) Debug(stream dbg.Debug_DebugServer) error {
6466
}
6567
return stream.Send(resp)
6668
}
69+
70+
// GetDebugConfig return metadata about a debug session
71+
func (s *DebugService) GetDebugConfig(ctx context.Context, req *debug.DebugConfigReq) (*debug.GetDebugConfigResp, error) {
72+
return cmd.GetDebugConfig(ctx, req)
73+
}

0 commit comments

Comments
 (0)