-
Notifications
You must be signed in to change notification settings - Fork 79
/
debugClient.ts
481 lines (399 loc) · 16.7 KB
/
debugClient.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import fs = require('fs');
import constants = require('constants');
import cp = require('child_process');
import assert = require('assert');
import net = require('net');
import {DebugProtocol} from '@vscode/debugprotocol';
import {ProtocolClient} from './protocolClient';
export interface ILocation {
path: string;
line: number;
column?: number;
verified?: boolean;
}
export interface IPartialLocation {
path?: string;
line?: number;
column?: number;
verified?: boolean;
}
export class DebugClient extends ProtocolClient {
private static CASE_INSENSITIVE_FILESYSTEM : boolean;
private _runtime: string;
private _executable: string;
private _adapterProcess: cp.ChildProcess;
private _spawnOptions: cp.SpawnOptions;
private _enableStderr: boolean;
private _debugType: string;
private _socket: net.Socket;
private _supportsConfigurationDoneRequest: boolean;
public defaultTimeout = 5000;
/**
* Creates a DebugClient object that provides a promise-based API to write
* debug adapter tests.
* A simple mocha example for setting and hitting a breakpoint in line 15 of a program 'test.js' looks like this:
*
* var dc;
* setup( () => {
* dc = new DebugClient('node', './out/node/nodeDebug.js', 'node');
* return dc.start();
* });
* teardown( () => dc.stop() );
*
* test('should stop on a breakpoint', () => {
* return dc.hitBreakpoint({ program: 'test.js' }, 'test.js', 15);
* });
*/
constructor(debugAdapterRuntime: string, debugAdapterExecutable: string, debugType: string, spawnOptions?: cp.SpawnOptions, enableStderr?: boolean) {
super();
this._runtime = debugAdapterRuntime;
this._executable = debugAdapterExecutable;
this._spawnOptions = spawnOptions;
this._enableStderr = enableStderr;
this._debugType = debugType;
this._supportsConfigurationDoneRequest = false;
if (DebugClient.CASE_INSENSITIVE_FILESYSTEM === undefined) {
try {
fs.accessSync(process.execPath.toLowerCase(), constants.F_OK);
fs.accessSync(process.execPath.toUpperCase(), constants.F_OK);
DebugClient.CASE_INSENSITIVE_FILESYSTEM = true;
} catch (err) {
DebugClient.CASE_INSENSITIVE_FILESYSTEM = false;
}
}
}
// ---- life cycle --------------------------------------------------------------------------------------------------------
/**
* Starts a new debug adapter and sets up communication via stdin/stdout.
* If a port number is specified the adapter is not launched but a connection to
* a debug adapter running in server mode is established. This is useful for debugging
* the adapter while running tests. For this reason all timeouts are disabled in server mode.
*/
public start(port?: number): Promise<void> {
return new Promise<void>((resolve, reject) => {
if (typeof port === 'number') {
this._socket = net.createConnection(port, '127.0.0.1', () => {
this.connect(this._socket, this._socket);
resolve();
});
} else {
this._adapterProcess = cp.spawn(this._runtime, [ this._executable ], this._spawnOptions);
const sanitize = (s: string) => s.toString().replace(/\r?\n$/mg, '');
this._adapterProcess.stderr.on('data', (data: string) => {
if (this._enableStderr) {
console.log(sanitize(data));
}
});
this._adapterProcess.on('error', (err) => {
console.log(err);
reject(err);
});
this._adapterProcess.on('exit', (code: number, signal: string) => {
if (code) {
// done(new Error('debug adapter exit code: ' + code));
}
});
this.connect(this._adapterProcess.stdout, this._adapterProcess.stdin);
resolve();
}
});
}
/**
* Shutdown the debuggee and the debug adapter (or disconnect if in server mode).
*/
public stop(): Promise<void> {
return this.disconnectRequest().then(() => {
this.stopAdapter();
}).catch(() => {
this.stopAdapter();
});
}
private stopAdapter() {
if (this._adapterProcess) {
this._adapterProcess.kill();
this._adapterProcess = null;
}
if (this._socket) {
this._socket.end();
this._socket = null;
}
}
// ---- protocol requests -------------------------------------------------------------------------------------------------
public initializeRequest(args?: DebugProtocol.InitializeRequestArguments): Promise<DebugProtocol.InitializeResponse> {
if (!args) {
args = {
adapterID: this._debugType,
linesStartAt1: true,
columnsStartAt1: true,
pathFormat: 'path'
};
}
return this.send('initialize', args);
}
public configurationDoneRequest(args?: DebugProtocol.ConfigurationDoneArguments): Promise<DebugProtocol.ConfigurationDoneResponse> {
return this.send('configurationDone', args);
}
public launchRequest(args: DebugProtocol.LaunchRequestArguments): Promise<DebugProtocol.LaunchResponse> {
return this.send('launch', args);
}
public attachRequest(args: DebugProtocol.AttachRequestArguments): Promise<DebugProtocol.AttachResponse> {
return this.send('attach', args);
}
public restartRequest(args: DebugProtocol.RestartArguments): Promise<DebugProtocol.RestartResponse> {
return this.send('restart', args);
}
public terminateRequest(args?: DebugProtocol.TerminateArguments): Promise<DebugProtocol.TerminateResponse> {
return this.send('terminate', args);
}
public disconnectRequest(args?: DebugProtocol.DisconnectArguments): Promise<DebugProtocol.DisconnectResponse> {
return this.send('disconnect', args);
}
public setBreakpointsRequest(args: DebugProtocol.SetBreakpointsArguments): Promise<DebugProtocol.SetBreakpointsResponse> {
return this.send('setBreakpoints', args);
}
public setFunctionBreakpointsRequest(args: DebugProtocol.SetFunctionBreakpointsArguments): Promise<DebugProtocol.SetFunctionBreakpointsResponse> {
return this.send('setFunctionBreakpoints', args);
}
public setExceptionBreakpointsRequest(args: DebugProtocol.SetExceptionBreakpointsArguments): Promise<DebugProtocol.SetExceptionBreakpointsResponse> {
return this.send('setExceptionBreakpoints', args);
}
public dataBreakpointInfoRequest(args: DebugProtocol.DataBreakpointInfoArguments): Promise<DebugProtocol.DataBreakpointInfoResponse> {
return this.send('dataBreakpointInfo', args);
}
public setDataBreakpointsRequest(args: DebugProtocol.SetDataBreakpointsArguments): Promise<DebugProtocol.SetDataBreakpointsResponse> {
return this.send('setDataBreakpoints', args);
}
public continueRequest(args: DebugProtocol.ContinueArguments): Promise<DebugProtocol.ContinueResponse> {
return this.send('continue', args);
}
public nextRequest(args: DebugProtocol.NextArguments): Promise<DebugProtocol.NextResponse> {
return this.send('next', args);
}
public stepInRequest(args: DebugProtocol.StepInArguments): Promise<DebugProtocol.StepInResponse> {
return this.send('stepIn', args);
}
public stepOutRequest(args: DebugProtocol.StepOutArguments): Promise<DebugProtocol.StepOutResponse> {
return this.send('stepOut', args);
}
public stepBackRequest(args: DebugProtocol.StepBackArguments): Promise<DebugProtocol.StepBackResponse> {
return this.send('stepBack', args);
}
public reverseContinueRequest(args: DebugProtocol.ReverseContinueArguments): Promise<DebugProtocol.ReverseContinueResponse> {
return this.send('reverseContinue', args);
}
public restartFrameRequest(args: DebugProtocol.RestartFrameArguments): Promise<DebugProtocol.RestartFrameResponse> {
return this.send('restartFrame', args);
}
public gotoRequest(args: DebugProtocol.GotoArguments): Promise<DebugProtocol.GotoResponse> {
return this.send('goto', args);
}
public pauseRequest(args: DebugProtocol.PauseArguments): Promise<DebugProtocol.PauseResponse> {
return this.send('pause', args);
}
public stackTraceRequest(args: DebugProtocol.StackTraceArguments): Promise<DebugProtocol.StackTraceResponse> {
return this.send('stackTrace', args);
}
public scopesRequest(args: DebugProtocol.ScopesArguments): Promise<DebugProtocol.ScopesResponse> {
return this.send('scopes', args);
}
public variablesRequest(args: DebugProtocol.VariablesArguments): Promise<DebugProtocol.VariablesResponse> {
return this.send('variables', args);
}
public setVariableRequest(args: DebugProtocol.SetVariableArguments): Promise<DebugProtocol.SetVariableResponse> {
return this.send('setVariable', args);
}
public sourceRequest(args: DebugProtocol.SourceArguments): Promise<DebugProtocol.SourceResponse> {
return this.send('source', args);
}
public threadsRequest(): Promise<DebugProtocol.ThreadsResponse> {
return this.send('threads');
}
public modulesRequest(args: DebugProtocol.ModulesArguments): Promise<DebugProtocol.ModulesResponse> {
return this.send('modules');
}
public evaluateRequest(args: DebugProtocol.EvaluateArguments): Promise<DebugProtocol.EvaluateResponse> {
return this.send('evaluate', args);
}
public disassembleRequest(args: DebugProtocol.DisassembleArguments): Promise<DebugProtocol.DisassembleResponse> {
return this.send('disassemble', args);
}
public stepInTargetsRequest(args: DebugProtocol.StepInTargetsArguments): Promise<DebugProtocol.StepInTargetsResponse> {
return this.send('stepInTargets', args);
}
public gotoTargetsRequest(args: DebugProtocol.GotoTargetsArguments): Promise<DebugProtocol.GotoTargetsResponse> {
return this.send('gotoTargets', args);
}
public completionsRequest(args: DebugProtocol.CompletionsArguments): Promise<DebugProtocol.CompletionsResponse> {
return this.send('completions', args);
}
public exceptionInfoRequest(args: DebugProtocol.ExceptionInfoArguments): Promise<DebugProtocol.ExceptionInfoResponse> {
return this.send('exceptionInfo', args);
}
public customRequest(command: string, args?: any): Promise<DebugProtocol.Response> {
return this.send(command, args);
}
// ---- convenience methods -----------------------------------------------------------------------------------------------
/*
* Returns a promise that will resolve if an event with a specific type was received within some specified time.
* The promise will be rejected if a timeout occurs.
*/
public waitForEvent(eventType: string, timeout?: number): Promise<DebugProtocol.Event> {
timeout = timeout || this.defaultTimeout;
let timeoutHandler: any;
return new Promise((resolve, reject) => {
this.once(eventType, event => {
clearTimeout(timeoutHandler);
resolve(event);
});
if (!this._socket) { // no timeouts if debugging the tests
timeoutHandler = setTimeout(() => {
reject(new Error(`no event '${eventType}' received after ${timeout} ms`));
}, timeout);
}
});
}
/*
* Returns a promise that will resolve if an 'initialized' event was received within some specified time
* and a subsequent 'configurationDone' request was successfully executed.
* The promise will be rejected if a timeout occurs or if the 'configurationDone' request fails.
*/
public configurationSequence(): Promise<any> {
return this.waitForEvent('initialized').then(event => {
return this.configurationDone();
});
}
/**
* Returns a promise that will resolve if a 'initialize' and a 'launch' request were successful.
*/
public launch(launchArgs: any): Promise<DebugProtocol.LaunchResponse> {
return this.initializeRequest().then(response => {
if (response.body && response.body.supportsConfigurationDoneRequest) {
this._supportsConfigurationDoneRequest = true;
}
return this.launchRequest(launchArgs);
});
}
private configurationDone() : Promise<DebugProtocol.Response> {
if (this._supportsConfigurationDoneRequest) {
return this.configurationDoneRequest();
} else {
// if debug adapter doesn't support the configurationDoneRequest we have to send the setExceptionBreakpointsRequest.
return this.setExceptionBreakpointsRequest({ filters: [ 'all' ] });
}
}
/*
* Returns a promise that will resolve if a 'stopped' event was received within some specified time
* and the event's reason and line number was asserted.
* The promise will be rejected if a timeout occurs, the assertions fail, or if the 'stackTrace' request fails.
*/
public assertStoppedLocation(reason: string, expected: { path?: string | RegExp, line?: number, column?: number } ) : Promise<DebugProtocol.StackTraceResponse> {
return this.waitForEvent('stopped').then(event => {
assert.equal(event.body.reason, reason);
return this.stackTraceRequest({
threadId: event.body.threadId
});
}).then(response => {
const frame = response.body.stackFrames[0];
if (typeof expected.path === 'string' || expected.path instanceof RegExp) {
this.assertPath(frame.source.path, expected.path, 'stopped location: path mismatch');
}
if (typeof expected.line === 'number') {
assert.equal(frame.line, expected.line, 'stopped location: line mismatch');
}
if (typeof expected.column === 'number') {
assert.equal(frame.column, expected.column, 'stopped location: column mismatch');
}
return response;
});
}
private assertPartialLocationsEqual(locA: IPartialLocation, locB: IPartialLocation): void {
if (locA.path) {
this.assertPath(locA.path, locB.path, 'breakpoint verification mismatch: path');
}
if (typeof locA.line === 'number') {
assert.equal(locA.line, locB.line, 'breakpoint verification mismatch: line');
}
if (typeof locB.column === 'number' && typeof locA.column === 'number') {
assert.equal(locA.column, locB.column, 'breakpoint verification mismatch: column');
}
}
/*
* Returns a promise that will resolve if enough output events with the given category have been received
* and the concatenated data match the expected data.
* The promise will be rejected as soon as the received data cannot match the expected data or if a timeout occurs.
*/
public assertOutput(category: string, expected: string, timeout?: number): Promise<DebugProtocol.Event> {
timeout = timeout || this.defaultTimeout;
return new Promise((resolve, reject) => {
let output = '';
let timeoutHandler: any;
this.on('output', event => {
const e = <DebugProtocol.OutputEvent> event;
if (e.body.category === category) {
output += e.body.output;
if (output.indexOf(expected) === 0) {
clearTimeout(timeoutHandler);
resolve(event);
} else if (expected.indexOf(output) !== 0) {
const sanitize = (s: string) => s.toString().replace(/\r/mg, '\\r').replace(/\n/mg, '\\n');
reject(new Error(`received data '${sanitize(output)}' is not a prefix of the expected data '${sanitize(expected)}'`));
}
}
});
if (!this._socket) { // no timeouts if debugging the tests
timeoutHandler = setTimeout(() => {
reject(new Error(`not enough output data received after ${timeout} ms`));
}, timeout);
}
});
}
public assertPath(path: string, expected: string | RegExp, message?: string) {
if (expected instanceof RegExp) {
assert.ok((<RegExp>expected).test(path), message);
} else {
if (DebugClient.CASE_INSENSITIVE_FILESYSTEM) {
if (typeof path === 'string') {
path = path.toLowerCase();
}
if (typeof expected === 'string') {
expected = (<string>expected).toLowerCase();
}
}
assert.equal(path, expected, message);
}
}
// ---- scenarios ---------------------------------------------------------------------------------------------------------
/**
* Returns a promise that will resolve if a configurable breakpoint has been hit within some time
* and the event's reason and line number was asserted.
* The promise will be rejected if a timeout occurs, the assertions fail, or if the requests fails.
*/
public hitBreakpoint(launchArgs: any, location: ILocation, expectedStopLocation?: IPartialLocation, expectedBPLocation?: IPartialLocation) : Promise<any> {
return Promise.all([
this.waitForEvent('initialized').then(event => {
return this.setBreakpointsRequest({
lines: [ location.line ],
breakpoints: [ { line: location.line, column: location.column } ],
source: { path: location.path }
});
}).then(response => {
const bp = response.body.breakpoints[0];
const verified = (typeof location.verified === 'boolean') ? location.verified : true;
assert.equal(bp.verified, verified, 'breakpoint verification mismatch: verified');
const actualLocation: ILocation = {
column: bp.column,
line: bp.line,
path: bp.source && bp.source.path
};
this.assertPartialLocationsEqual(actualLocation, expectedBPLocation || location);
return this.configurationDone();
}),
this.launch(launchArgs),
this.assertStoppedLocation('breakpoint', expectedStopLocation || location)
]);
}
}