Skip to content

Use debug "configurations" instead or "recipes" #1033

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Nov 3, 2020
Merged
90 changes: 79 additions & 11 deletions cli/debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,23 @@ package debug

import (
"context"
"fmt"
"os"
"os/signal"
"sort"

"github.com/arduino/arduino-cli/cli/errorcodes"
"github.com/arduino/arduino-cli/cli/feedback"
"github.com/arduino/arduino-cli/cli/instance"
"github.com/arduino/arduino-cli/commands/debug"
rpc "github.com/arduino/arduino-cli/rpc/commands"
dbg "github.com/arduino/arduino-cli/rpc/debug"
"github.com/arduino/arduino-cli/table"
"github.com/arduino/go-paths-helper"
"github.com/arduino/go-properties-orderedmap"
"github.com/fatih/color"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"google.golang.org/grpc/status"
)

var (
Expand All @@ -38,6 +43,8 @@ var (
verify bool
interpreter string
importDir string
printInfo bool
programmer string
)

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

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

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

// Intercept SIGINT and forward them to debug process
ctrlc := make(chan os.Signal, 1)
signal.Notify(ctrlc, os.Interrupt)

if _, err := debug.Debug(context.Background(), &dbg.DebugConfigReq{
Instance: &rpc.Instance{Id: instance.GetId()},
debugConfigRequested := &dbg.DebugConfigReq{
Instance: instance,
Fqbn: fqbn,
SketchPath: sketchPath.String(),
Port: port,
Interpreter: interpreter,
ImportDir: importDir,
}, os.Stdin, os.Stdout, ctrlc); err != nil {
feedback.Errorf("Error during Debug: %v", err)
os.Exit(errorcodes.ErrGeneric)
Programmer: programmer,
}

if printInfo {

if res, err := debug.GetDebugConfig(context.Background(), debugConfigRequested); err != nil {
if status, ok := status.FromError(err); ok {
feedback.Errorf("Error getting Debug info: %v", status.Message())
errorcodes.ExitWithGrpcStatus(status)
}
feedback.Errorf("Error getting Debug info: %v", err)
os.Exit(errorcodes.ErrGeneric)
} else {
feedback.PrintResult(&debugInfoResult{res})
}

} else {

// Intercept SIGINT and forward them to debug process
ctrlc := make(chan os.Signal, 1)
signal.Notify(ctrlc, os.Interrupt)

if _, err := debug.Debug(context.Background(), debugConfigRequested, os.Stdin, os.Stdout, ctrlc); err != nil {
feedback.Errorf("Error during Debug: %v", err)
os.Exit(errorcodes.ErrGeneric)
}

}
}

Expand All @@ -103,3 +132,42 @@ func initSketchPath(sketchPath *paths.Path) *paths.Path {
logrus.Infof("Reading sketch from dir: %s", wd)
return wd
}

type debugInfoResult struct {
info *dbg.GetDebugConfigResp
}

func (r *debugInfoResult) Data() interface{} {
return r.info
}

func (r *debugInfoResult) String() string {
t := table.New()
green := color.New(color.FgHiGreen)
dimGreen := color.New(color.FgGreen)
t.AddRow("Executable to debug", table.NewCell(r.info.GetExecutable(), green))
t.AddRow("Toolchain type", table.NewCell(r.info.GetToolchain(), green))
t.AddRow("Toolchain path", table.NewCell(r.info.GetToolchainPath(), dimGreen))
t.AddRow("Toolchain prefix", table.NewCell(r.info.GetToolchainPrefix(), dimGreen))
if len(r.info.GetToolchainConfiguration()) > 0 {
conf := properties.NewFromHashmap(r.info.GetToolchainConfiguration())
keys := conf.Keys()
sort.Strings(keys)
t.AddRow("Toolchain custom configurations")
for _, k := range keys {
t.AddRow(table.NewCell(" - "+k, dimGreen), table.NewCell(conf.Get(k), dimGreen))
}
}
t.AddRow("GDB Server type", table.NewCell(r.info.GetServer(), green))
t.AddRow("GDB Server path", table.NewCell(r.info.GetServerPath(), dimGreen))
if len(r.info.GetServerConfiguration()) > 0 {
conf := properties.NewFromHashmap(r.info.GetServerConfiguration())
keys := conf.Keys()
sort.Strings(keys)
t.AddRow(fmt.Sprintf("%s custom configurations", r.info.GetServer()))
for _, k := range keys {
t.AddRow(table.NewCell(" - "+k, dimGreen), table.NewCell(conf.Get(k), dimGreen))
}
}
return t.Render()
}
18 changes: 18 additions & 0 deletions cli/errorcodes/errorcodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@

package errorcodes

import (
"os"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

// Error codes to be used for os.Exit().
const (
_ = iota // 0 is not a valid exit error code
Expand All @@ -29,3 +36,14 @@ const (
ErrCoreConfig
ErrBadArgument
)

// ExitWithGrpcStatus will terminate the current process by returing the correct
// error code closest matching the gRPC status.
func ExitWithGrpcStatus(s *status.Status) {
switch s.Code() {
case codes.Unimplemented:
os.Exit(ErrBadArgument)
default:
os.Exit(ErrGeneric)
}
}
7 changes: 7 additions & 0 deletions commands/daemon/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
package daemon

import (
"context"
"os"

"github.com/arduino/arduino-cli/arduino/utils"
cmd "github.com/arduino/arduino-cli/commands/debug"
"github.com/arduino/arduino-cli/rpc/debug"
dbg "github.com/arduino/arduino-cli/rpc/debug"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -64,3 +66,8 @@ func (s *DebugService) Debug(stream dbg.Debug_DebugServer) error {
}
return stream.Send(resp)
}

// GetDebugConfig return metadata about a debug session
func (s *DebugService) GetDebugConfig(ctx context.Context, req *debug.DebugConfigReq) (*debug.GetDebugConfigResp, error) {
return cmd.GetDebugConfig(ctx, req)
}
Loading