forked from arduino/arduino-cli
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug_info.go
170 lines (153 loc) · 6.5 KB
/
debug_info.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// This file is part of arduino-cli.
//
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to license@arduino.cc.
package debug
import (
"context"
"fmt"
"strings"
"github.com/arduino/arduino-cli/arduino/cores"
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
"github.com/arduino/arduino-cli/arduino/sketches"
"github.com/arduino/arduino-cli/commands"
"github.com/arduino/arduino-cli/rpc/cc/arduino/cli/debug/v1"
"github.com/arduino/go-paths-helper"
"github.com/arduino/go-properties-orderedmap"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// GetDebugConfig returns metadata to start debugging with the specified board
funcGetDebugConfig(ctx context.Context, req*debug.DebugConfigRequest) (*debug.GetDebugConfigResponse, error) {
pm:=commands.GetPackageManager(req.GetInstance().GetId())
returngetDebugProperties(req, pm)
}
funcgetDebugProperties(req*debug.DebugConfigRequest, pm*packagemanager.PackageManager) (*debug.GetDebugConfigResponse, error) {
// TODO: make a generic function to extract sketch from request
// and remove duplication in commands/compile.go
ifreq.GetSketchPath() =="" {
returnnil, fmt.Errorf("missing sketchPath")
}
sketchPath:=paths.New(req.GetSketchPath())
sketch, err:=sketches.NewSketchFromPath(sketchPath)
iferr!=nil {
returnnil, errors.Wrap(err, "opening sketch")
}
// XXX Remove this code duplication!!
fqbnIn:=req.GetFqbn()
iffqbnIn==""&&sketch!=nil&&sketch.Metadata!=nil {
fqbnIn=sketch.Metadata.CPU.Fqbn
}
iffqbnIn=="" {
returnnil, fmt.Errorf("no Fully Qualified Board Name provided")
}
fqbn, err:=cores.ParseFQBN(fqbnIn)
iferr!=nil {
returnnil, errors.Wrap(err, "error parsing FQBN")
}
// Find target board and board properties
_, platformRelease, board, boardProperties, referencedPlatformRelease, err:=pm.ResolveFQBN(fqbn)
iferr!=nil {
returnnil, errors.Wrap(err, "error resolving FQBN")
}
// Build configuration for debug
toolProperties:=properties.NewMap()
ifreferencedPlatformRelease!=nil {
toolProperties.Merge(referencedPlatformRelease.Properties)
}
toolProperties.Merge(platformRelease.Properties)
toolProperties.Merge(platformRelease.RuntimeProperties())
toolProperties.Merge(boardProperties)
// HOTFIX: Remove me when the `arduino:samd` core is updated
// (remember to remove it also in arduino/board/details.go)
if!toolProperties.ContainsKey("debug.executable") {
ifplatformRelease.String() =="arduino:samd@1.8.9"||platformRelease.String() =="arduino:samd@1.8.8" {
toolProperties.Set("debug.executable", "{build.path}/{build.project_name}.elf")
toolProperties.Set("debug.toolchain", "gcc")
toolProperties.Set("debug.toolchain.path", "{runtime.tools.arm-none-eabi-gcc-7-2017q4.path}/bin/")
toolProperties.Set("debug.toolchain.prefix", "arm-none-eabi-")
toolProperties.Set("debug.server", "openocd")
toolProperties.Set("debug.server.openocd.path", "{runtime.tools.openocd-0.10.0-arduino7.path}/bin/openocd")
toolProperties.Set("debug.server.openocd.scripts_dir", "{runtime.tools.openocd-0.10.0-arduino7.path}/share/openocd/scripts/")
toolProperties.Set("debug.server.openocd.script", "{runtime.platform.path}/variants/{build.variant}/{build.openocdscript}")
}
}
for_, tool:=rangepm.GetAllInstalledToolsReleases() {
toolProperties.Merge(tool.RuntimeProperties())
}
ifrequiredTools, err:=pm.FindToolsRequiredForBoard(board); err==nil {
for_, requiredTool:=rangerequiredTools {
logrus.WithField("tool", requiredTool).Info("Tool required for debug")
toolProperties.Merge(requiredTool.RuntimeProperties())
}
}
ifreq.GetProgrammer() !="" {
ifp, ok:=platformRelease.Programmers[req.GetProgrammer()]; ok {
toolProperties.Merge(p.Properties)
} elseifrefP, ok:=referencedPlatformRelease.Programmers[req.GetProgrammer()]; ok {
toolProperties.Merge(refP.Properties)
} else {
returnnil, fmt.Errorf("programmer '%s' not found", req.GetProgrammer())
}
}
varimportPath*paths.Path
ifimportDir:=req.GetImportDir(); importDir!="" {
importPath=paths.New(importDir)
} else {
// TODO: Create a function to obtain importPath from sketch
importPath, err=sketch.BuildPath()
iferr!=nil {
returnnil, fmt.Errorf("can't find build path for sketch: %v", err)
}
}
if!importPath.Exist() {
returnnil, fmt.Errorf("compiled sketch not found in %s", importPath)
}
if!importPath.IsDir() {
returnnil, fmt.Errorf("expected compiled sketch in directory %s, but is a file instead", importPath)
}
toolProperties.SetPath("build.path", importPath)
toolProperties.Set("build.project_name", sketch.Name+".ino")
// Set debug port property
port:=req.GetPort()
ifport!="" {
toolProperties.Set("debug.port", port)
ifstrings.HasPrefix(port, "/dev/") {
toolProperties.Set("debug.port.file", port[5:])
} else {
toolProperties.Set("debug.port.file", port)
}
}
// Extract and expand all debugging properties
debugProperties:=properties.NewMap()
fork, v:=rangetoolProperties.SubTree("debug").AsMap() {
debugProperties.Set(k, toolProperties.ExpandPropsInString(v))
}
if!debugProperties.ContainsKey("executable") {
returnnil, status.Error(codes.Unimplemented, fmt.Sprintf("debugging not supported for board %s", req.GetFqbn()))
}
server:=debugProperties.Get("server")
toolchain:=debugProperties.Get("toolchain")
return&debug.GetDebugConfigResponse{
Executable: debugProperties.Get("executable"),
Server: server,
ServerPath: debugProperties.Get("server."+server+".path"),
ServerConfiguration: debugProperties.SubTree("server."+server).AsMap(),
Toolchain: toolchain,
ToolchainPath: debugProperties.Get("toolchain.path"),
ToolchainPrefix: debugProperties.Get("toolchain.prefix"),
ToolchainConfiguration: debugProperties.SubTree("toolchain."+toolchain).AsMap(),
}, nil
}