-
-
Notifications
You must be signed in to change notification settings - Fork 434
/
Copy pathdebug.ts
455 lines (433 loc) · 13.5 KB
/
debug.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
import { Emitter, Event } from '@theia/core/lib/common/event';
import { MenuModelRegistry } from '@theia/core/lib/common/menu/menu-model-registry';
import { nls } from '@theia/core/lib/common/nls';
import { MaybePromise } from '@theia/core/lib/common/types';
import { inject, injectable } from '@theia/core/shared/inversify';
import { noBoardSelected } from '../../common/nls';
import {
BoardDetails,
BoardIdentifier,
BoardsService,
CheckDebugEnabledParams,
ExecutableService,
SketchRef,
isBoardIdentifierChangeEvent,
isCompileSummary,
} from '../../common/protocol';
import { BoardsDataStore } from '../boards/boards-data-store';
import { BoardsServiceProvider } from '../boards/boards-service-provider';
import { HostedPluginSupport } from '../hosted/hosted-plugin-support';
import { ArduinoMenus } from '../menu/arduino-menus';
import { NotificationCenter } from '../notification-center';
import { CurrentSketch } from '../sketches-service-client-impl';
import { ArduinoToolbar } from '../toolbar/arduino-toolbar';
import {
Command,
CommandRegistry,
SketchContribution,
TabBarToolbarRegistry,
URI,
} from './contribution';
const COMPILE_FOR_DEBUG_KEY = 'arduino-compile-for-debug';
interface StartDebugParams {
/**
* Absolute filesystem path to the Arduino CLI executable.
*/
readonly cliPath: string;
/**
* The the board to debug.
*/
readonly board: Readonly<{ fqbn: string; name?: string }>;
/**
* Absolute filesystem path of the sketch to debug.
*/
readonly sketchPath: string;
/**
* Location where the `launch.json` will be created on the fly before starting every debug session.
* If not defined, it falls back to `sketchPath/.vscode/launch.json`.
*/
readonly launchConfigsDirPath?: string;
/**
* Absolute path to the `arduino-cli.yaml` file. If not specified, it falls back to `~/.arduinoIDE/arduino-cli.yaml`.
*/
readonly cliConfigPath?: string;
/**
* Programmer for the debugging.
*/
readonly programmer?: string;
/**
* Custom progress title to use when getting the debug information from the CLI.
*/
readonly title?: string;
}
type StartDebugResult = boolean;
export const DebugDisabledStatusMessageSource = Symbol(
'DebugDisabledStatusMessageSource'
);
export interface DebugDisabledStatusMessageSource {
/**
* `undefined` if debugging is enabled (for the currently selected board + programmer + config options).
* Otherwise, it's the human readable message why it's disabled.
*/
get message(): string | undefined;
/**
* Emits an event when {@link message} changes.
*/
get onDidChangeMessage(): Event<string | undefined>;
}
@injectable()
export class Debug
extends SketchContribution
implements DebugDisabledStatusMessageSource
{
@inject(HostedPluginSupport)
private readonly hostedPluginSupport: HostedPluginSupport;
@inject(NotificationCenter)
private readonly notificationCenter: NotificationCenter;
@inject(ExecutableService)
private readonly executableService: ExecutableService;
@inject(BoardsService)
private readonly boardService: BoardsService;
@inject(BoardsServiceProvider)
private readonly boardsServiceProvider: BoardsServiceProvider;
@inject(BoardsDataStore)
private readonly boardsDataStore: BoardsDataStore;
/**
* If `undefined`, debugging is enabled. Otherwise, the human-readable reason why it's disabled.
*/
private _message?: string = noBoardSelected; // Initial pessimism.
private readonly didChangeMessageEmitter = new Emitter<string | undefined>();
readonly onDidChangeMessage = this.didChangeMessageEmitter.event;
get message(): string | undefined {
return this._message;
}
private set message(message: string | undefined) {
this._message = message;
this.didChangeMessageEmitter.fire(this._message);
}
private readonly debugToolbarItem = {
id: Debug.Commands.START_DEBUGGING.id,
command: Debug.Commands.START_DEBUGGING.id,
tooltip: `${
this.message
? nls.localize(
'arduino/debug/debugWithMessage',
'Debug - {0}',
this.message
)
: Debug.Commands.START_DEBUGGING.label
}`,
priority: 3,
onDidChange: this.onDidChangeMessage as Event<void>,
};
override onStart(): void {
this.onDidChangeMessage(
() =>
(this.debugToolbarItem.tooltip = `${
this.message
? nls.localize(
'arduino/debug/debugWithMessage',
'Debug - {0}',
this.message
)
: Debug.Commands.START_DEBUGGING.label
}`)
);
this.boardsServiceProvider.onBoardsConfigDidChange((event) => {
if (isBoardIdentifierChangeEvent(event)) {
this.updateMessage();
}
});
this.notificationCenter.onPlatformDidInstall(() => this.updateMessage());
this.notificationCenter.onPlatformDidUninstall(() => this.updateMessage());
this.boardsDataStore.onDidChange((event) => {
const selectedFqbn =
this.boardsServiceProvider.boardsConfig.selectedBoard?.fqbn;
if (event.changes.find((change) => change.fqbn === selectedFqbn)) {
this.updateMessage();
}
});
this.commandService.onDidExecuteCommand((event) => {
const { commandId, args } = event;
if (
commandId === 'arduino.languageserver.notifyBuildDidComplete' &&
isCompileSummary(args[0])
) {
this.updateMessage();
}
});
}
override onReady(): void {
this.boardsServiceProvider.ready.then(() => this.updateMessage());
}
override registerCommands(registry: CommandRegistry): void {
registry.registerCommand(Debug.Commands.START_DEBUGGING, {
execute: () => this.startDebug(),
isVisible: (widget) =>
ArduinoToolbar.is(widget) && widget.side === 'left',
isEnabled: () => !this.message,
});
registry.registerCommand(Debug.Commands.TOGGLE_OPTIMIZE_FOR_DEBUG, {
execute: () => this.toggleCompileForDebug(),
isToggled: () => this.compileForDebug,
});
registry.registerCommand(Debug.Commands.IS_OPTIMIZE_FOR_DEBUG, {
execute: () => this.compileForDebug,
});
}
override registerToolbarItems(registry: TabBarToolbarRegistry): void {
registry.registerItem(this.debugToolbarItem);
}
override registerMenus(registry: MenuModelRegistry): void {
registry.registerMenuAction(ArduinoMenus.SKETCH__MAIN_GROUP, {
commandId: Debug.Commands.TOGGLE_OPTIMIZE_FOR_DEBUG.id,
label: Debug.Commands.TOGGLE_OPTIMIZE_FOR_DEBUG.label,
order: '5',
});
}
private async updateMessage(): Promise<void> {
try {
await this.isDebugEnabled();
this.message = undefined;
} catch (err) {
let message = String(err);
if (err instanceof Error) {
message = err.message;
}
this.message = message;
}
}
private async isDebugEnabled(
board: BoardIdentifier | undefined = this.boardsServiceProvider.boardsConfig
.selectedBoard
): Promise<string> {
const debugFqbn = await isDebugEnabled(
board,
(fqbn) => this.boardService.getBoardDetails({ fqbn }),
(fqbn) => this.boardsDataStore.getData(fqbn),
(fqbn) => this.boardsDataStore.appendConfigToFqbn(fqbn),
(params) => this.boardService.checkDebugEnabled(params)
);
return debugFqbn;
}
private async startDebug(
board: BoardIdentifier | undefined = this.boardsServiceProvider.boardsConfig
.selectedBoard,
sketch:
| CurrentSketch
| undefined = this.sketchServiceClient.tryGetCurrentSketch()
): Promise<StartDebugResult> {
if (!CurrentSketch.isValid(sketch)) {
return false;
}
const params = await this.createStartDebugParams(board);
if (!params) {
return false;
}
await this.hostedPluginSupport.didStart;
try {
const result = await this.debug(params);
return Boolean(result);
} catch (err) {
if (await this.isSketchNotVerifiedError(err, sketch)) {
const yes = nls.localize('vscode/extensionsUtils/yes', 'Yes');
const answer = await this.messageService.error(
sketchIsNotCompiled(sketch.name),
yes
);
if (answer === yes) {
this.commandService.executeCommand('arduino-verify-sketch');
}
} else {
this.messageService.error(
err instanceof Error ? err.message : String(err)
);
}
}
return false;
}
private async debug(
params: StartDebugParams
): Promise<StartDebugResult | undefined> {
return this.commandService.executeCommand<StartDebugResult>(
'arduino.debug.start',
params
);
}
get compileForDebug(): boolean {
const value = window.localStorage.getItem(COMPILE_FOR_DEBUG_KEY);
return value === 'true';
}
private toggleCompileForDebug(): void {
const oldState = this.compileForDebug;
const newState = !oldState;
window.localStorage.setItem(COMPILE_FOR_DEBUG_KEY, String(newState));
this.menuManager.update();
}
private async isSketchNotVerifiedError(
err: unknown,
sketch: SketchRef
): Promise<boolean> {
if (err instanceof Error) {
try {
const tempBuildPaths = await this.sketchesService.tempBuildPath(sketch);
return tempBuildPaths.some((tempBuildPath) =>
err.message.includes(tempBuildPath)
);
} catch {
return false;
}
}
return false;
}
private async createStartDebugParams(
board: BoardIdentifier | undefined
): Promise<StartDebugParams | undefined> {
if (!board || !board.fqbn) {
return undefined;
}
let debugFqbn: string | undefined = undefined;
try {
debugFqbn = await this.isDebugEnabled(board);
} catch {}
if (!debugFqbn) {
return undefined;
}
const [sketch, executables, boardsData] = await Promise.all([
this.sketchServiceClient.currentSketch(),
this.executableService.list(),
this.boardsDataStore.getData(board.fqbn),
]);
if (!CurrentSketch.isValid(sketch)) {
return undefined;
}
const ideTempFolderUri = await this.sketchesService.getIdeTempFolderUri(
sketch
);
const [cliPath, sketchPath, launchConfigsDirPath] = await Promise.all([
this.fileService.fsPath(new URI(executables.cliUri)),
this.fileService.fsPath(new URI(sketch.uri)),
this.fileService.fsPath(new URI(ideTempFolderUri)),
]);
return {
board: { fqbn: debugFqbn, name: board.name },
cliPath,
sketchPath,
launchConfigsDirPath,
programmer: boardsData.selectedProgrammer?.id,
title: nls.localize(
'arduino/debug/getDebugInfo',
'Getting debug info...'
),
};
}
}
export namespace Debug {
export namespace Commands {
export const START_DEBUGGING = Command.toLocalizedCommand(
{
id: 'arduino-start-debug',
label: 'Start Debugging',
category: 'Arduino',
},
'vscode/debug.contribution/startDebuggingHelp'
);
export const TOGGLE_OPTIMIZE_FOR_DEBUG = Command.toLocalizedCommand(
{
id: 'arduino-toggle-optimize-for-debug',
label: 'Optimize for Debugging',
category: 'Arduino',
},
'arduino/debug/optimizeForDebugging'
);
export const IS_OPTIMIZE_FOR_DEBUG: Command = {
id: 'arduino-is-optimize-for-debug',
};
}
}
/**
* Resolves with the FQBN to use for the `debug --info --programmer p --fqbn $FQBN` command. Otherwise, rejects.
*
* (non-API)
*/
export async function isDebugEnabled(
board: BoardIdentifier | undefined,
getDetails: (fqbn: string) => MaybePromise<BoardDetails | undefined>,
getData: (fqbn: string) => MaybePromise<BoardsDataStore.Data>,
appendConfigToFqbn: (fqbn: string) => MaybePromise<string | undefined>,
checkDebugEnabled: (params: CheckDebugEnabledParams) => MaybePromise<string>
): Promise<string> {
if (!board) {
throw new Error(noBoardSelected);
}
const { fqbn } = board;
if (!fqbn) {
throw new Error(noPlatformInstalledFor(board.name));
}
const [details, data, fqbnWithConfig] = await Promise.all([
getDetails(fqbn),
getData(fqbn),
appendConfigToFqbn(fqbn),
]);
if (!details) {
throw new Error(noPlatformInstalledFor(board.name));
}
if (!fqbnWithConfig) {
throw new Error(
`Failed to append boards config to the FQBN. Original FQBN was: ${fqbn}`
);
}
if (!data.selectedProgrammer) {
throw new Error(noProgrammerSelectedFor(board.name));
}
const params = {
fqbn: fqbnWithConfig,
programmer: data.selectedProgrammer.id,
};
try {
const debugFqbn = await checkDebugEnabled(params);
return debugFqbn;
} catch (err) {
throw new Error(debuggingNotSupported(board.name));
}
}
/**
* (non-API)
*/
export function sketchIsNotCompiled(sketchName: string): string {
return nls.localize(
'arduino/debug/sketchIsNotCompiled',
"Sketch '{0}' must be verified before starting a debug session. Please verify the sketch and start debugging again. Do you want to verify the sketch now?",
sketchName
);
}
/**
* (non-API)
*/
export function noPlatformInstalledFor(boardName: string): string {
return nls.localize(
'arduino/debug/noPlatformInstalledFor',
"Platform is not installed for '{0}'",
boardName
);
}
/**
* (non-API)
*/
export function debuggingNotSupported(boardName: string): string {
return nls.localize(
'arduino/debug/debuggingNotSupported',
"Debugging is not supported by '{0}'",
boardName
);
}
/**
* (non-API)
*/
export function noProgrammerSelectedFor(boardName: string): string {
return nls.localize(
'arduino/debug/noProgrammerSelectedFor',
"No programmer selected for '{0}'",
boardName
);
}