-
Notifications
You must be signed in to change notification settings - Fork 29.9k
/
rawDebugSession.ts
780 lines (663 loc) · 28 KB
/
rawDebugSession.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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as nls from 'vs/nls';
import { Event, Emitter } from 'vs/base/common/event';
import * as objects from 'vs/base/common/objects';
import { toAction } from 'vs/base/common/actions';
import * as errors from 'vs/base/common/errors';
import { createErrorWithActions } from 'vs/base/common/errorMessage';
import { formatPII, isUri } from 'vs/workbench/contrib/debug/common/debugUtils';
import { IDebugAdapter, IConfig, AdapterEndEvent, IDebugger } from 'vs/workbench/contrib/debug/common/debug';
import { IExtensionHostDebugService, IOpenExtensionWindowResult } from 'vs/platform/debug/common/extensionHostDebug';
import { URI } from 'vs/base/common/uri';
import { IOpenerService } from 'vs/platform/opener/common/opener';
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
import { CancellationToken } from 'vs/base/common/cancellation';
import { INotificationService, Severity } from 'vs/platform/notification/common/notification';
import { IDialogService } from 'vs/platform/dialogs/common/dialogs';
import { Schemas } from 'vs/base/common/network';
/**
* This interface represents a single command line argument split into a "prefix" and a "path" half.
* The optional "prefix" contains arbitrary text and the optional "path" contains a file system path.
* Concatenating both results in the original command line argument.
*/
interface ILaunchVSCodeArgument {
prefix?: string;
path?: string;
}
interface ILaunchVSCodeArguments {
args: ILaunchVSCodeArgument[];
debugRenderer?: boolean;
env?: { [key: string]: string | null };
}
/**
* Encapsulates the DebugAdapter lifecycle and some idiosyncrasies of the Debug Adapter Protocol.
*/
export class RawDebugSession implements IDisposable {
private allThreadsContinued = true;
private _readyForBreakpoints = false;
private _capabilities: DebugProtocol.Capabilities;
// shutdown
private debugAdapterStopped = false;
private inShutdown = false;
private terminated = false;
private firedAdapterExitEvent = false;
// telemetry
private startTime = 0;
private didReceiveStoppedEvent = false;
// DAP events
private readonly _onDidInitialize = new Emitter<DebugProtocol.InitializedEvent>();
private readonly _onDidStop = new Emitter<DebugProtocol.StoppedEvent>();
private readonly _onDidContinued = new Emitter<DebugProtocol.ContinuedEvent>();
private readonly _onDidTerminateDebugee = new Emitter<DebugProtocol.TerminatedEvent>();
private readonly _onDidExitDebugee = new Emitter<DebugProtocol.ExitedEvent>();
private readonly _onDidThread = new Emitter<DebugProtocol.ThreadEvent>();
private readonly _onDidOutput = new Emitter<DebugProtocol.OutputEvent>();
private readonly _onDidBreakpoint = new Emitter<DebugProtocol.BreakpointEvent>();
private readonly _onDidLoadedSource = new Emitter<DebugProtocol.LoadedSourceEvent>();
private readonly _onDidProgressStart = new Emitter<DebugProtocol.ProgressStartEvent>();
private readonly _onDidProgressUpdate = new Emitter<DebugProtocol.ProgressUpdateEvent>();
private readonly _onDidProgressEnd = new Emitter<DebugProtocol.ProgressEndEvent>();
private readonly _onDidInvalidated = new Emitter<DebugProtocol.InvalidatedEvent>();
private readonly _onDidInvalidateMemory = new Emitter<DebugProtocol.MemoryEvent>();
private readonly _onDidCustomEvent = new Emitter<DebugProtocol.Event>();
private readonly _onDidEvent = new Emitter<DebugProtocol.Event>();
// DA events
private readonly _onDidExitAdapter = new Emitter<AdapterEndEvent>();
private debugAdapter: IDebugAdapter | null;
private toDispose: IDisposable[] = [];
constructor(
debugAdapter: IDebugAdapter,
public readonly dbgr: IDebugger,
private readonly sessionId: string,
@IExtensionHostDebugService private readonly extensionHostDebugService: IExtensionHostDebugService,
@IOpenerService private readonly openerService: IOpenerService,
@INotificationService private readonly notificationService: INotificationService,
@IDialogService private readonly dialogSerivce: IDialogService,
) {
this.debugAdapter = debugAdapter;
this._capabilities = Object.create(null);
this.toDispose.push(this.debugAdapter.onError(err => {
this.shutdown(err);
}));
this.toDispose.push(this.debugAdapter.onExit(code => {
if (code !== 0) {
this.shutdown(new Error(`exit code: ${code}`));
} else {
// normal exit
this.shutdown();
}
}));
this.debugAdapter.onEvent(event => {
switch (event.event) {
case 'initialized':
this._readyForBreakpoints = true;
this._onDidInitialize.fire(event);
break;
case 'loadedSource':
this._onDidLoadedSource.fire(<DebugProtocol.LoadedSourceEvent>event);
break;
case 'capabilities':
if (event.body) {
const capabilities = (<DebugProtocol.CapabilitiesEvent>event).body.capabilities;
this.mergeCapabilities(capabilities);
}
break;
case 'stopped':
this.didReceiveStoppedEvent = true; // telemetry: remember that debugger stopped successfully
this._onDidStop.fire(<DebugProtocol.StoppedEvent>event);
break;
case 'continued':
this.allThreadsContinued = (<DebugProtocol.ContinuedEvent>event).body.allThreadsContinued === false ? false : true;
this._onDidContinued.fire(<DebugProtocol.ContinuedEvent>event);
break;
case 'thread':
this._onDidThread.fire(<DebugProtocol.ThreadEvent>event);
break;
case 'output':
this._onDidOutput.fire(<DebugProtocol.OutputEvent>event);
break;
case 'breakpoint':
this._onDidBreakpoint.fire(<DebugProtocol.BreakpointEvent>event);
break;
case 'terminated':
this._onDidTerminateDebugee.fire(<DebugProtocol.TerminatedEvent>event);
break;
case 'exit':
this._onDidExitDebugee.fire(<DebugProtocol.ExitedEvent>event);
break;
case 'progressStart':
this._onDidProgressStart.fire(event as DebugProtocol.ProgressStartEvent);
break;
case 'progressUpdate':
this._onDidProgressUpdate.fire(event as DebugProtocol.ProgressUpdateEvent);
break;
case 'progressEnd':
this._onDidProgressEnd.fire(event as DebugProtocol.ProgressEndEvent);
break;
case 'invalidated':
this._onDidInvalidated.fire(event as DebugProtocol.InvalidatedEvent);
break;
case 'memory':
this._onDidInvalidateMemory.fire(event as DebugProtocol.MemoryEvent);
break;
case 'process':
break;
case 'module':
break;
default:
this._onDidCustomEvent.fire(event);
break;
}
this._onDidEvent.fire(event);
});
this.debugAdapter.onRequest(request => this.dispatchRequest(request, dbgr));
}
get onDidExitAdapter(): Event<AdapterEndEvent> {
return this._onDidExitAdapter.event;
}
get capabilities(): DebugProtocol.Capabilities {
return this._capabilities;
}
/**
* DA is ready to accepts setBreakpoint requests.
* Becomes true after "initialized" events has been received.
*/
get readyForBreakpoints(): boolean {
return this._readyForBreakpoints;
}
//---- DAP events
get onDidInitialize(): Event<DebugProtocol.InitializedEvent> {
return this._onDidInitialize.event;
}
get onDidStop(): Event<DebugProtocol.StoppedEvent> {
return this._onDidStop.event;
}
get onDidContinued(): Event<DebugProtocol.ContinuedEvent> {
return this._onDidContinued.event;
}
get onDidTerminateDebugee(): Event<DebugProtocol.TerminatedEvent> {
return this._onDidTerminateDebugee.event;
}
get onDidExitDebugee(): Event<DebugProtocol.ExitedEvent> {
return this._onDidExitDebugee.event;
}
get onDidThread(): Event<DebugProtocol.ThreadEvent> {
return this._onDidThread.event;
}
get onDidOutput(): Event<DebugProtocol.OutputEvent> {
return this._onDidOutput.event;
}
get onDidBreakpoint(): Event<DebugProtocol.BreakpointEvent> {
return this._onDidBreakpoint.event;
}
get onDidLoadedSource(): Event<DebugProtocol.LoadedSourceEvent> {
return this._onDidLoadedSource.event;
}
get onDidCustomEvent(): Event<DebugProtocol.Event> {
return this._onDidCustomEvent.event;
}
get onDidProgressStart(): Event<DebugProtocol.ProgressStartEvent> {
return this._onDidProgressStart.event;
}
get onDidProgressUpdate(): Event<DebugProtocol.ProgressUpdateEvent> {
return this._onDidProgressUpdate.event;
}
get onDidProgressEnd(): Event<DebugProtocol.ProgressEndEvent> {
return this._onDidProgressEnd.event;
}
get onDidInvalidated(): Event<DebugProtocol.InvalidatedEvent> {
return this._onDidInvalidated.event;
}
get onDidInvalidateMemory(): Event<DebugProtocol.MemoryEvent> {
return this._onDidInvalidateMemory.event;
}
get onDidEvent(): Event<DebugProtocol.Event> {
return this._onDidEvent.event;
}
//---- DebugAdapter lifecycle
/**
* Starts the underlying debug adapter and tracks the session time for telemetry.
*/
async start(): Promise<void> {
if (!this.debugAdapter) {
return Promise.reject(new Error(nls.localize('noDebugAdapterStart', "No debug adapter, can not start debug session.")));
}
await this.debugAdapter.startSession();
this.startTime = new Date().getTime();
}
/**
* Send client capabilities to the debug adapter and receive DA capabilities in return.
*/
async initialize(args: DebugProtocol.InitializeRequestArguments): Promise<DebugProtocol.InitializeResponse | undefined> {
const response = await this.send('initialize', args, undefined, undefined, false);
if (response) {
this.mergeCapabilities(response.body);
}
return response;
}
/**
* Terminate the debuggee and shutdown the adapter
*/
disconnect(args: DebugProtocol.DisconnectArguments): Promise<any> {
const terminateDebuggee = this.capabilities.supportTerminateDebuggee ? args.terminateDebuggee : undefined;
const suspendDebuggee = this.capabilities.supportTerminateDebuggee && this.capabilities.supportSuspendDebuggee ? args.suspendDebuggee : undefined;
return this.shutdown(undefined, args.restart, terminateDebuggee, suspendDebuggee);
}
//---- DAP requests
async launchOrAttach(config: IConfig): Promise<DebugProtocol.Response | undefined> {
const response = await this.send(config.request, config, undefined, undefined, false);
if (response) {
this.mergeCapabilities(response.body);
}
return response;
}
/**
* Try killing the debuggee softly...
*/
terminate(restart = false): Promise<DebugProtocol.TerminateResponse | undefined> {
if (this.capabilities.supportsTerminateRequest) {
if (!this.terminated) {
this.terminated = true;
return this.send('terminate', { restart }, undefined, 2000);
}
return this.disconnect({ terminateDebuggee: true, restart });
}
return Promise.reject(new Error('terminated not supported'));
}
restart(args: DebugProtocol.RestartArguments): Promise<DebugProtocol.RestartResponse | undefined> {
if (this.capabilities.supportsRestartRequest) {
return this.send('restart', args);
}
return Promise.reject(new Error('restart not supported'));
}
async next(args: DebugProtocol.NextArguments): Promise<DebugProtocol.NextResponse | undefined> {
const response = await this.send('next', args);
this.fireSimulatedContinuedEvent(args.threadId);
return response;
}
async stepIn(args: DebugProtocol.StepInArguments): Promise<DebugProtocol.StepInResponse | undefined> {
const response = await this.send('stepIn', args);
this.fireSimulatedContinuedEvent(args.threadId);
return response;
}
async stepOut(args: DebugProtocol.StepOutArguments): Promise<DebugProtocol.StepOutResponse | undefined> {
const response = await this.send('stepOut', args);
this.fireSimulatedContinuedEvent(args.threadId);
return response;
}
async continue(args: DebugProtocol.ContinueArguments): Promise<DebugProtocol.ContinueResponse | undefined> {
const response = await this.send<DebugProtocol.ContinueResponse>('continue', args);
if (response && response.body && response.body.allThreadsContinued !== undefined) {
this.allThreadsContinued = response.body.allThreadsContinued;
}
this.fireSimulatedContinuedEvent(args.threadId, this.allThreadsContinued);
return response;
}
pause(args: DebugProtocol.PauseArguments): Promise<DebugProtocol.PauseResponse | undefined> {
return this.send('pause', args);
}
terminateThreads(args: DebugProtocol.TerminateThreadsArguments): Promise<DebugProtocol.TerminateThreadsResponse | undefined> {
if (this.capabilities.supportsTerminateThreadsRequest) {
return this.send('terminateThreads', args);
}
return Promise.reject(new Error('terminateThreads not supported'));
}
setVariable(args: DebugProtocol.SetVariableArguments): Promise<DebugProtocol.SetVariableResponse | undefined> {
if (this.capabilities.supportsSetVariable) {
return this.send<DebugProtocol.SetVariableResponse>('setVariable', args);
}
return Promise.reject(new Error('setVariable not supported'));
}
setExpression(args: DebugProtocol.SetExpressionArguments): Promise<DebugProtocol.SetExpressionResponse | undefined> {
if (this.capabilities.supportsSetExpression) {
return this.send<DebugProtocol.SetExpressionResponse>('setExpression', args);
}
return Promise.reject(new Error('setExpression not supported'));
}
async restartFrame(args: DebugProtocol.RestartFrameArguments, threadId: number): Promise<DebugProtocol.RestartFrameResponse | undefined> {
if (this.capabilities.supportsRestartFrame) {
const response = await this.send('restartFrame', args);
this.fireSimulatedContinuedEvent(threadId);
return response;
}
return Promise.reject(new Error('restartFrame not supported'));
}
stepInTargets(args: DebugProtocol.StepInTargetsArguments): Promise<DebugProtocol.StepInTargetsResponse | undefined> {
if (this.capabilities.supportsStepInTargetsRequest) {
return this.send('stepInTargets', args);
}
return Promise.reject(new Error('stepInTargets not supported'));
}
completions(args: DebugProtocol.CompletionsArguments, token: CancellationToken): Promise<DebugProtocol.CompletionsResponse | undefined> {
if (this.capabilities.supportsCompletionsRequest) {
return this.send<DebugProtocol.CompletionsResponse>('completions', args, token);
}
return Promise.reject(new Error('completions not supported'));
}
setBreakpoints(args: DebugProtocol.SetBreakpointsArguments): Promise<DebugProtocol.SetBreakpointsResponse | undefined> {
return this.send<DebugProtocol.SetBreakpointsResponse>('setBreakpoints', args);
}
setFunctionBreakpoints(args: DebugProtocol.SetFunctionBreakpointsArguments): Promise<DebugProtocol.SetFunctionBreakpointsResponse | undefined> {
if (this.capabilities.supportsFunctionBreakpoints) {
return this.send<DebugProtocol.SetFunctionBreakpointsResponse>('setFunctionBreakpoints', args);
}
return Promise.reject(new Error('setFunctionBreakpoints not supported'));
}
dataBreakpointInfo(args: DebugProtocol.DataBreakpointInfoArguments): Promise<DebugProtocol.DataBreakpointInfoResponse | undefined> {
if (this.capabilities.supportsDataBreakpoints) {
return this.send<DebugProtocol.DataBreakpointInfoResponse>('dataBreakpointInfo', args);
}
return Promise.reject(new Error('dataBreakpointInfo not supported'));
}
setDataBreakpoints(args: DebugProtocol.SetDataBreakpointsArguments): Promise<DebugProtocol.SetDataBreakpointsResponse | undefined> {
if (this.capabilities.supportsDataBreakpoints) {
return this.send<DebugProtocol.SetDataBreakpointsResponse>('setDataBreakpoints', args);
}
return Promise.reject(new Error('setDataBreakpoints not supported'));
}
setExceptionBreakpoints(args: DebugProtocol.SetExceptionBreakpointsArguments): Promise<DebugProtocol.SetExceptionBreakpointsResponse | undefined> {
return this.send<DebugProtocol.SetExceptionBreakpointsResponse>('setExceptionBreakpoints', args);
}
breakpointLocations(args: DebugProtocol.BreakpointLocationsArguments): Promise<DebugProtocol.BreakpointLocationsResponse | undefined> {
if (this.capabilities.supportsBreakpointLocationsRequest) {
return this.send('breakpointLocations', args);
}
return Promise.reject(new Error('breakpointLocations is not supported'));
}
configurationDone(): Promise<DebugProtocol.ConfigurationDoneResponse | undefined> {
if (this.capabilities.supportsConfigurationDoneRequest) {
return this.send('configurationDone', null);
}
return Promise.reject(new Error('configurationDone not supported'));
}
stackTrace(args: DebugProtocol.StackTraceArguments, token: CancellationToken): Promise<DebugProtocol.StackTraceResponse | undefined> {
return this.send<DebugProtocol.StackTraceResponse>('stackTrace', args, token);
}
exceptionInfo(args: DebugProtocol.ExceptionInfoArguments): Promise<DebugProtocol.ExceptionInfoResponse | undefined> {
if (this.capabilities.supportsExceptionInfoRequest) {
return this.send<DebugProtocol.ExceptionInfoResponse>('exceptionInfo', args);
}
return Promise.reject(new Error('exceptionInfo not supported'));
}
scopes(args: DebugProtocol.ScopesArguments, token: CancellationToken): Promise<DebugProtocol.ScopesResponse | undefined> {
return this.send<DebugProtocol.ScopesResponse>('scopes', args, token);
}
variables(args: DebugProtocol.VariablesArguments, token?: CancellationToken): Promise<DebugProtocol.VariablesResponse | undefined> {
return this.send<DebugProtocol.VariablesResponse>('variables', args, token);
}
source(args: DebugProtocol.SourceArguments): Promise<DebugProtocol.SourceResponse | undefined> {
return this.send<DebugProtocol.SourceResponse>('source', args);
}
loadedSources(args: DebugProtocol.LoadedSourcesArguments): Promise<DebugProtocol.LoadedSourcesResponse | undefined> {
if (this.capabilities.supportsLoadedSourcesRequest) {
return this.send<DebugProtocol.LoadedSourcesResponse>('loadedSources', args);
}
return Promise.reject(new Error('loadedSources not supported'));
}
threads(): Promise<DebugProtocol.ThreadsResponse | undefined> {
return this.send<DebugProtocol.ThreadsResponse>('threads', null);
}
evaluate(args: DebugProtocol.EvaluateArguments): Promise<DebugProtocol.EvaluateResponse | undefined> {
return this.send<DebugProtocol.EvaluateResponse>('evaluate', args);
}
async stepBack(args: DebugProtocol.StepBackArguments): Promise<DebugProtocol.StepBackResponse | undefined> {
if (this.capabilities.supportsStepBack) {
const response = await this.send('stepBack', args);
this.fireSimulatedContinuedEvent(args.threadId);
return response;
}
return Promise.reject(new Error('stepBack not supported'));
}
async reverseContinue(args: DebugProtocol.ReverseContinueArguments): Promise<DebugProtocol.ReverseContinueResponse | undefined> {
if (this.capabilities.supportsStepBack) {
const response = await this.send('reverseContinue', args);
this.fireSimulatedContinuedEvent(args.threadId);
return response;
}
return Promise.reject(new Error('reverseContinue not supported'));
}
gotoTargets(args: DebugProtocol.GotoTargetsArguments): Promise<DebugProtocol.GotoTargetsResponse | undefined> {
if (this.capabilities.supportsGotoTargetsRequest) {
return this.send('gotoTargets', args);
}
return Promise.reject(new Error('gotoTargets is not supported'));
}
async goto(args: DebugProtocol.GotoArguments): Promise<DebugProtocol.GotoResponse | undefined> {
if (this.capabilities.supportsGotoTargetsRequest) {
const response = await this.send('goto', args);
this.fireSimulatedContinuedEvent(args.threadId);
return response;
}
return Promise.reject(new Error('goto is not supported'));
}
async setInstructionBreakpoints(args: DebugProtocol.SetInstructionBreakpointsArguments): Promise<DebugProtocol.SetInstructionBreakpointsResponse | undefined> {
if (this.capabilities.supportsInstructionBreakpoints) {
return await this.send('setInstructionBreakpoints', args);
}
return Promise.reject(new Error('setInstructionBreakpoints is not supported'));
}
async disassemble(args: DebugProtocol.DisassembleArguments): Promise<DebugProtocol.DisassembleResponse | undefined> {
if (this.capabilities.supportsDisassembleRequest) {
return await this.send('disassemble', args);
}
return Promise.reject(new Error('disassemble is not supported'));
}
async readMemory(args: DebugProtocol.ReadMemoryArguments): Promise<DebugProtocol.ReadMemoryResponse | undefined> {
if (this.capabilities.supportsReadMemoryRequest) {
return await this.send('readMemory', args);
}
return Promise.reject(new Error('readMemory is not supported'));
}
async writeMemory(args: DebugProtocol.WriteMemoryArguments): Promise<DebugProtocol.WriteMemoryResponse | undefined> {
if (this.capabilities.supportsWriteMemoryRequest) {
return await this.send('writeMemory', args);
}
return Promise.reject(new Error('writeMemory is not supported'));
}
cancel(args: DebugProtocol.CancelArguments): Promise<DebugProtocol.CancelResponse | undefined> {
return this.send('cancel', args);
}
custom(request: string, args: any): Promise<DebugProtocol.Response | undefined> {
return this.send(request, args);
}
//---- private
private async shutdown(error?: Error, restart = false, terminateDebuggee: boolean | undefined = undefined, suspendDebuggee: boolean | undefined = undefined): Promise<any> {
if (!this.inShutdown) {
this.inShutdown = true;
if (this.debugAdapter) {
try {
const args: DebugProtocol.DisconnectArguments = { restart };
if (typeof terminateDebuggee === 'boolean') {
args.terminateDebuggee = terminateDebuggee;
}
if (typeof suspendDebuggee === 'boolean') {
args.suspendDebuggee = suspendDebuggee;
}
this.send('disconnect', args, undefined, 2000);
} catch (e) {
// Catch the potential 'disconnect' error - no need to show it to the user since the adapter is shutting down
} finally {
this.stopAdapter(error);
}
} else {
return this.stopAdapter(error);
}
}
}
private async stopAdapter(error?: Error): Promise<any> {
try {
if (this.debugAdapter) {
const da = this.debugAdapter;
this.debugAdapter = null;
await da.stopSession();
this.debugAdapterStopped = true;
}
} finally {
this.fireAdapterExitEvent(error);
}
}
private fireAdapterExitEvent(error?: Error): void {
if (!this.firedAdapterExitEvent) {
this.firedAdapterExitEvent = true;
const e: AdapterEndEvent = {
emittedStopped: this.didReceiveStoppedEvent,
sessionLengthInSeconds: (new Date().getTime() - this.startTime) / 1000
};
if (error && !this.debugAdapterStopped) {
e.error = error;
}
this._onDidExitAdapter.fire(e);
}
}
private async dispatchRequest(request: DebugProtocol.Request, dbgr: IDebugger): Promise<void> {
const response: DebugProtocol.Response = {
type: 'response',
seq: 0,
command: request.command,
request_seq: request.seq,
success: true
};
const safeSendResponse = (response: DebugProtocol.Response) => this.debugAdapter && this.debugAdapter.sendResponse(response);
switch (request.command) {
case 'launchVSCode':
try {
let result = await this.launchVsCode(<ILaunchVSCodeArguments>request.arguments);
if (!result.success) {
const showResult = await this.dialogSerivce.show(Severity.Warning, nls.localize('canNotStart', "The debugger needs to open a new tab or window for the debuggee but the browser prevented this. You must give permission to continue."),
[nls.localize('continue', "Continue"), nls.localize('cancel', "Cancel")], { cancelId: 1 });
if (showResult.choice === 0) {
result = await this.launchVsCode(<ILaunchVSCodeArguments>request.arguments);
} else {
response.success = false;
safeSendResponse(response);
await this.shutdown();
}
}
response.body = {
rendererDebugPort: result.rendererDebugPort,
};
safeSendResponse(response);
} catch (err) {
response.success = false;
response.message = err.message;
safeSendResponse(response);
}
break;
case 'runInTerminal':
try {
const shellProcessId = await dbgr.runInTerminal(request.arguments as DebugProtocol.RunInTerminalRequestArguments, this.sessionId);
const resp = response as DebugProtocol.RunInTerminalResponse;
resp.body = {};
if (typeof shellProcessId === 'number') {
resp.body.shellProcessId = shellProcessId;
}
safeSendResponse(resp);
} catch (err) {
response.success = false;
response.message = err.message;
safeSendResponse(response);
}
break;
default:
response.success = false;
response.message = `unknown request '${request.command}'`;
safeSendResponse(response);
break;
}
}
private launchVsCode(vscodeArgs: ILaunchVSCodeArguments): Promise<IOpenExtensionWindowResult> {
const args: string[] = [];
for (const arg of vscodeArgs.args) {
const a2 = (arg.prefix || '') + (arg.path || '');
const match = /^--(.+)=(.+)$/.exec(a2);
if (match && match.length === 3) {
const key = match[1];
let value = match[2];
if ((key === 'file-uri' || key === 'folder-uri') && !isUri(arg.path)) {
value = isUri(value) ? value : URI.file(value).toString();
}
args.push(`--${key}=${value}`);
} else {
args.push(a2);
}
}
if (vscodeArgs.env) {
args.push(`--extensionEnvironment=${JSON.stringify(vscodeArgs.env)}`);
}
return this.extensionHostDebugService.openExtensionDevelopmentHostWindow(args, !!vscodeArgs.debugRenderer);
}
private send<R extends DebugProtocol.Response>(command: string, args: any, token?: CancellationToken, timeout?: number, showErrors = true): Promise<R | undefined> {
return new Promise<DebugProtocol.Response | undefined>((completeDispatch, errorDispatch) => {
if (!this.debugAdapter) {
if (this.inShutdown) {
// We are in shutdown silently complete
completeDispatch(undefined);
} else {
errorDispatch(new Error(nls.localize('noDebugAdapter', "No debugger available found. Can not send '{0}'.", command)));
}
return;
}
let cancelationListener: IDisposable;
const requestId = this.debugAdapter.sendRequest(command, args, (response: DebugProtocol.Response) => {
cancelationListener?.dispose();
if (response.success) {
completeDispatch(response);
} else {
errorDispatch(response);
}
}, timeout);
if (token) {
cancelationListener = token.onCancellationRequested(() => {
cancelationListener.dispose();
if (this.capabilities.supportsCancelRequest) {
this.cancel({ requestId });
}
});
}
}).then(undefined, err => Promise.reject(this.handleErrorResponse(err, showErrors)));
}
private handleErrorResponse(errorResponse: DebugProtocol.Response, showErrors: boolean): Error {
if (errorResponse.command === 'canceled' && errorResponse.message === 'canceled') {
return errors.canceled();
}
const error: DebugProtocol.Message | undefined = errorResponse?.body?.error;
const errorMessage = errorResponse?.message || '';
const userMessage = error ? formatPII(error.format, false, error.variables) : errorMessage;
const url = error?.url;
if (error && url) {
const label = error.urlLabel ? error.urlLabel : nls.localize('moreInfo', "More Info");
const uri = URI.parse(url);
// Use a suffixed id if uri invokes a command, so default 'Open launch.json' command is suppressed on dialog
const actionId = uri.scheme === Schemas.command ? 'debug.moreInfo.command' : 'debug.moreInfo';
return createErrorWithActions(userMessage, [toAction({ id: actionId, label, run: () => this.openerService.open(uri, { allowCommands: true }) })]);
}
if (showErrors && error && error.format && error.showUser) {
this.notificationService.error(userMessage);
}
const result = new Error(userMessage);
(<any>result).showUser = error?.showUser;
return result;
}
private mergeCapabilities(capabilities: DebugProtocol.Capabilities | undefined): void {
if (capabilities) {
this._capabilities = objects.mixin(this._capabilities, capabilities);
}
}
private fireSimulatedContinuedEvent(threadId: number, allThreadsContinued = false): void {
this._onDidContinued.fire({
type: 'event',
event: 'continued',
body: {
threadId,
allThreadsContinued
},
seq: undefined!
});
}
dispose(): void {
dispose(this.toDispose);
}
}