This repository was archived by the owner on Oct 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 236
/
Copy pathconfigurationProvider.ts
206 lines (179 loc) · 7.86 KB
/
configurationProvider.ts
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
import * as path from "path";
import * as vscode from "vscode";
import { ArduinoApp } from "../arduino/arduino";
import ArduinoActivator from "../arduinoActivator";
import ArduinoContext from "../arduinoContext";
import { VscodeSettings } from "../arduino/vscodeSettings";
import * as platform from "../common/platform";
import * as util from "../common/util";
import { ArduinoWorkspace } from "../common/workspace";
import { DeviceContext } from "../deviceContext";
import * as Logger from "../logger/logger";
export class ArduinoDebugConfigurationProvider implements vscode.DebugConfigurationProvider {
constructor() { }
public provideDebugConfigurations(folder: vscode.WorkspaceFolder | undefined, token?: vscode.CancellationToken):
vscode.ProviderResult<vscode.DebugConfiguration[]> {
return [
this.getDefaultDebugSettings(folder),
];
}
// Try to add all missing attributes to the debug configuration being launched.
public resolveDebugConfiguration(folder: vscode.WorkspaceFolder | undefined, config: vscode.DebugConfiguration, token?: vscode.CancellationToken):
vscode.ProviderResult<vscode.DebugConfiguration> {
if (!config || !config.request) {
config = this.getDefaultDebugSettings(folder);
}
return this.resolveDebugConfigurationAsync(config);
}
private getDefaultDebugSettings(folder: vscode.WorkspaceFolder | undefined) {
return {
name: "Arduino",
type: "arduino",
request: "launch",
program: "${file}",
cwd: "${workspaceFolder}",
MIMode: "gdb",
targetArchitecture: "arm",
miDebuggerPath: "",
debugServerPath: "",
debugServerArgs: "",
customLaunchSetupCommands: [
{
text: "target remote localhost:3333",
},
{
text: "file \"${file}\"",
},
{
text: "load",
},
{
text: "monitor reset halt",
},
{
text: "monitor reset init",
},
],
stopAtEntry: true,
serverStarted: "Info\\ :\\ [\\w\\d\\.]*:\\ hardware",
launchCompleteCommand: "exec-continue",
filterStderr: true,
args: [],
};
}
private async resolveDebugConfigurationAsync(config: vscode.DebugConfiguration) {
if (!ArduinoContext.initialized) {
await ArduinoActivator.activate();
}
if (VscodeSettings.getInstance().logLevel === "verbose" && !config.logging) {
config = {
...config, logging: {
engineLogging: true,
},
};
}
if (!ArduinoContext.boardManager.currentBoard) {
vscode.window.showErrorMessage("Please select a board.");
return undefined;
}
if (!this.resolveOpenOcd(config)) {
return undefined;
}
if (!await this.resolveOpenOcdOptions(config)) {
return undefined;
}
if (!this.resolveDebuggerPath(config)) {
return undefined;
}
if (!await this.resolveProgramPath(config)) {
return undefined;
}
// Use the C++ debugger MIEngine as the real internal debugger
config.type = "cppdbg";
const dc = DeviceContext.getInstance();
Logger.traceUserData("start-cppdbg", { board: dc.board });
return config;
}
private async resolveProgramPath(config) {
const dc = DeviceContext.getInstance();
if (!config.program || config.program === "${file}") {
// make a unique temp folder because keeping same temp folder will corrupt the build when board is changed
const outputFolder = path.join(dc.output || `.build`, ArduinoContext.boardManager.currentBoard.board);
util.mkdirRecursivelySync(path.join(ArduinoWorkspace.rootPath, outputFolder));
if (!dc.sketch || !util.fileExistsSync(path.join(ArduinoWorkspace.rootPath, dc.sketch))) {
await dc.resolveMainSketch();
}
if (!dc.sketch) {
vscode.window.showErrorMessage("No sketch file was found. Please specify the sketch in the arduino.json file");
return false;
}
if (!util.fileExistsSync(path.join(ArduinoWorkspace.rootPath, dc.sketch))) {
vscode.window.showErrorMessage(`Cannot find ${dc.sketch}, Please specify the sketch in the arduino.json file`);
return false;
}
config.program = path.join(ArduinoWorkspace.rootPath, outputFolder, `${path.basename(dc.sketch)}.elf`);
// always compile elf to make sure debug the right elf
if (!await ArduinoContext.arduinoApp.verify(outputFolder)) {
vscode.window.showErrorMessage("Failure to verify the program, please check output for details.");
return false;
}
config.program = config.program.replace(/\\/g, "/");
config.customLaunchSetupCommands.forEach((obj) => {
if (obj.text && obj.text.indexOf("${file}") > 0) {
obj.text = obj.text.replace(/\$\{file\}/, config.program);
}
});
}
if (!util.fileExistsSync(config.program)) {
vscode.window.showErrorMessage("Cannot find the elf file.");
return false;
}
return true;
}
private resolveDebuggerPath(config) {
if (!config.miDebuggerPath) {
config.miDebuggerPath = platform.findFile(platform.getExecutableFileName("arm-none-eabi-gdb"),
path.join(ArduinoContext.arduinoApp.settings.packagePath, "packages", ArduinoContext.boardManager.currentBoard.getPackageName()));
}
if (!util.fileExistsSync(config.miDebuggerPath)) {
config.miDebuggerPath = ArduinoContext.debuggerManager.miDebuggerPath;
}
if (!util.fileExistsSync(config.miDebuggerPath)) {
vscode.window.showErrorMessage("Cannot find the debugger path.");
return false;
}
return true;
}
private resolveOpenOcd(config) {
if (!config.debugServerPath) {
config.debugServerPath = platform.findFile(platform.getExecutableFileName("openocd"),
path.join(ArduinoContext.arduinoApp.settings.packagePath, "packages",
ArduinoContext.boardManager.currentBoard.getPackageName()));
}
if (!util.fileExistsSync(config.debugServerPath)) {
config.debugServerPath = ArduinoContext.debuggerManager.debugServerPath;
}
if (!util.fileExistsSync(config.debugServerPath)) {
vscode.window.showErrorMessage("Cannot find the OpenOCD from the launch.json debugServerPath property." +
"Please input the right path of OpenOCD");
return false;
}
return true;
}
private async resolveOpenOcdOptions(config) {
if (config.debugServerPath && !config.debugServerArgs) {
try {
config.debugServerArgs = await ArduinoContext.debuggerManager.resolveOpenOcdOptions(config);
if (!config.debugServerArgs) {
return false;
}
} catch (error) {
vscode.window.showErrorMessage(error.message);
return false;
}
}
return true;
}
}