@@ -17,18 +17,23 @@ package debug
1717
1818import (
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
3439var (
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+ }
0 commit comments