|
| 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 [email protected]. |
| 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. (this command opens an interactive gdb session)", |
| 46 | + Example: " " + os.Args[0] + " debug -b arduino:samd:mkr1000 /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 for debug.") |
| 54 | + |
| 55 | + return debugCommand |
| 56 | +} |
| 57 | + |
| 58 | +func run(command *cobra.Command, args []string) { |
| 59 | + instance, err := instance.CreateInstance() |
| 60 | + if err != nil { |
| 61 | + feedback.Errorf("Error during Debug: %v", err) |
| 62 | + os.Exit(errorcodes.ErrGeneric) |
| 63 | + } |
| 64 | + |
| 65 | + var path *paths.Path |
| 66 | + if len(args) > 0 { |
| 67 | + path = paths.New(args[0]) |
| 68 | + } |
| 69 | + sketchPath := initSketchPath(path) |
| 70 | + |
| 71 | + if _, err := debug.Debug(context.Background(), &dbg.DebugConfigReq{ |
| 72 | + Instance: &dbg.Instance{Id: instance.GetId()}, |
| 73 | + Fqbn: fqbn, |
| 74 | + SketchPath: sketchPath.String(), |
| 75 | + Port: port, |
| 76 | + ImportFile: importFile, |
| 77 | + }, os.Stdin, os.Stdout); err != nil { |
| 78 | + feedback.Errorf("Error during Debug: %v", err) |
| 79 | + os.Exit(errorcodes.ErrGeneric) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +// initSketchPath returns the current working directory |
| 84 | +func initSketchPath(sketchPath *paths.Path) *paths.Path { |
| 85 | + if sketchPath != nil { |
| 86 | + return sketchPath |
| 87 | + } |
| 88 | + |
| 89 | + wd, err := paths.Getwd() |
| 90 | + if err != nil { |
| 91 | + feedback.Errorf("Couldn't get current working directory: %v", err) |
| 92 | + os.Exit(errorcodes.ErrGeneric) |
| 93 | + } |
| 94 | + logrus.Infof("Reading sketch from dir: %s", wd) |
| 95 | + return wd |
| 96 | +} |
0 commit comments