forked from microsoft/vscode-node-debug
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebugSession.ts
More file actions
474 lines (383 loc) · 13.8 KB
/
Copy pathdebugSession.ts
File metadata and controls
474 lines (383 loc) · 13.8 KB
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import {V8Protocol, Response, Event} from './v8Protocol';
import * as Net from 'net';
import * as Path from 'path';
import * as Url from 'url';
export class Source implements DebugProtocol.Source {
name: string;
path: string;
sourceReference: number;
public constructor(name: string, path: string, id: number = 0) {
this.name = name;
this.path = path;
this.sourceReference = id;
}
}
export class Scope implements DebugProtocol.Scope {
name: string;
variablesReference: number;
expensive: boolean;
public constructor(name: string, reference: number, expensive: boolean = false) {
this.name = name;
this.variablesReference = reference;
this.expensive = expensive;
}
}
export class StackFrame implements DebugProtocol.StackFrame {
id: number;
source: Source;
line: number;
column: number;
name: string;
public constructor(i: number, nm: string, src: Source, ln: number, col: number) {
this.id = i;
this.source = src;
this.line = ln;
this.column = col;
this.name = nm;
}
}
export class Thread implements DebugProtocol.Thread {
id: number;
name: string;
public constructor(id: number, name: string) {
this.id = id;
if (name) {
this.name = name;
} else {
this.name = "Thread #" + id;
}
}
}
export class Variable implements DebugProtocol.Variable {
name: string;
value: string;
variablesReference: number;
public constructor(name: string, value: string, ref: number = 0) {
this.name = name;
this.value = value;
this.variablesReference = ref;
}
}
export class Breakpoint implements DebugProtocol.Breakpoint {
verified: boolean;
line: number;
public constructor(verified: boolean, line: number) {
this.verified = verified;
this.line = line;
}
}
export class StoppedEvent extends Event implements DebugProtocol.StoppedEvent {
body: {
reason: string;
threadId: number;
};
public constructor(reason: string, threadId: number, exception_text: string = null) {
super('stopped');
this.body = {
reason: reason,
threadId: threadId
};
if (exception_text) {
(<any>this).body.text = exception_text;
}
}
}
export class InitializedEvent extends Event implements DebugProtocol.InitializedEvent {
public constructor() {
super('initialized');
}
}
export class TerminatedEvent extends Event implements DebugProtocol.TerminatedEvent {
public constructor() {
super('terminated');
}
}
export class OutputEvent extends Event implements DebugProtocol.OutputEvent {
body: {
category: string,
output: string
};
public constructor(output: string, category: string = 'console') {
super('output');
this.body = {
category: category,
output: output
};
}
}
export enum ErrorDestination {
User = 1,
Telemetry = 2
};
export class DebugSession extends V8Protocol {
private _debuggerLinesStartAt1: boolean;
private _debuggerColumnsStartAt1: boolean;
private _debuggerPathsAreURIs: boolean;
private _clientLinesStartAt1: boolean;
private _clientColumnsStartAt1: boolean;
private _clientPathsAreURIs: boolean;
private _isServer: boolean;
public constructor(debuggerLinesAndColumnsStartAt1: boolean, isServer: boolean = false) {
super();
this._debuggerLinesStartAt1 = debuggerLinesAndColumnsStartAt1;
this._debuggerColumnsStartAt1 = debuggerLinesAndColumnsStartAt1;
this._debuggerPathsAreURIs = false;
this._clientLinesStartAt1 = true;
this._clientColumnsStartAt1 = true;
this._clientPathsAreURIs = false;
this._isServer = isServer;
this.on('close', () => {
this.shutdown();
});
this.on('error', (error) => {
this.shutdown();
});
}
/**
* A virtual constructor...
*/
public static run(debugSession: typeof DebugSession) {
// parse arguments
let port = 0;
const args = process.argv.slice(2);
args.forEach(function (val, index, array) {
const portMatch = /^--server=(\d{4,5})$/.exec(val);
if (portMatch) {
port = parseInt(portMatch[1], 10);
}
});
if (port > 0) {
// start as a server
console.error(`waiting for v8 protocol on port ${port}`);
Net.createServer((socket) => {
console.error('>> accepted connection from client');
socket.on('end', () => {
console.error('>> client connection closed\n');
});
new debugSession(false, true).start(socket, socket);
}).listen(port);
} else {
// start a session
console.error("waiting for v8 protocol on stdin/stdout");
let session = new debugSession(false);
process.on('SIGTERM', () => {
session.shutdown();
});
session.start(process.stdin, process.stdout);
}
}
public shutdown(): void {
if (this._isServer) {
console.error('process.exit ignored in server mode');
} else {
// wait a bit before shutting down
setTimeout(() => {
process.exit(0);
}, 100);
}
}
protected sendErrorResponse(response: DebugProtocol.Response, code: number, format: string, args?: any, dest: ErrorDestination = ErrorDestination.User): void {
const message = DebugSession.formatPII(format, true, args);
response.success = false;
response.message = `${response.command}: ${message}`;
if (!response.body) {
response.body = {};
}
const msg = <DebugProtocol.Message> {
id: code,
format: format
};
if (args) {
msg.variables = args;
}
if (dest & ErrorDestination.User) {
msg.showUser = true;
}
if (dest & ErrorDestination.Telemetry) {
msg.sendTelemetry = true;
}
response.body.error = msg;
this.sendResponse(response);
}
protected dispatchRequest(request: DebugProtocol.Request): void {
const response = new Response(request);
try {
if (request.command === 'initialize') {
var args = <DebugProtocol.InitializeRequestArguments> request.arguments;
if (typeof args.linesStartAt1 === 'boolean') {
this._clientLinesStartAt1 = args.linesStartAt1;
}
if (typeof args.columnsStartAt1 === 'boolean') {
this._clientColumnsStartAt1 = args.columnsStartAt1;
}
if (args.pathFormat !== 'path') {
this.sendErrorResponse(response, 2018, "debug adapter only supports native paths", null, ErrorDestination.Telemetry);
} else {
this.initializeRequest(<DebugProtocol.InitializeResponse> response, args);
}
} else if (request.command === 'launch') {
this.launchRequest(<DebugProtocol.LaunchResponse> response, request.arguments);
} else if (request.command === 'attach') {
this.attachRequest(<DebugProtocol.AttachResponse> response, request.arguments);
} else if (request.command === 'disconnect') {
this.disconnectRequest(<DebugProtocol.DisconnectResponse> response, request.arguments);
} else if (request.command === 'setBreakpoints') {
this.setBreakPointsRequest(<DebugProtocol.SetBreakpointsResponse> response, request.arguments);
} else if (request.command === 'setExceptionBreakpoints') {
this.setExceptionBreakPointsRequest(<DebugProtocol.SetExceptionBreakpointsResponse> response, request.arguments);
} else if (request.command === 'continue') {
this.continueRequest(<DebugProtocol.ContinueResponse> response, request.arguments);
} else if (request.command === 'next') {
this.nextRequest(<DebugProtocol.NextResponse> response, request.arguments);
} else if (request.command === 'stepIn') {
this.stepInRequest(<DebugProtocol.StepInResponse> response, request.arguments);
} else if (request.command === 'stepOut') {
this.stepOutRequest(<DebugProtocol.StepOutResponse> response, request.arguments);
} else if (request.command === 'pause') {
this.pauseRequest(<DebugProtocol.PauseResponse> response, request.arguments);
} else if (request.command === 'stackTrace') {
this.stackTraceRequest(<DebugProtocol.StackTraceResponse> response, request.arguments);
} else if (request.command === 'scopes') {
this.scopesRequest(<DebugProtocol.ScopesResponse> response, request.arguments);
} else if (request.command === 'variables') {
this.variablesRequest(<DebugProtocol.VariablesResponse> response, request.arguments);
} else if (request.command === 'source') {
this.sourceRequest(<DebugProtocol.SourceResponse> response, request.arguments);
} else if (request.command === 'threads') {
this.threadsRequest(<DebugProtocol.ThreadsResponse> response);
} else if (request.command === 'evaluate') {
this.evaluateRequest(<DebugProtocol.EvaluateResponse> response, request.arguments);
} else {
this.sendErrorResponse(response, 1014, "unrecognized request", null, ErrorDestination.Telemetry);
}
} catch (e) {
this.sendErrorResponse(response, 1104, "exception while processing request (exception: {_exception})", { _exception: e.message }, ErrorDestination.Telemetry);
}
}
protected initializeRequest(response: DebugProtocol.InitializeResponse, args: DebugProtocol.InitializeRequestArguments): void {
this.sendResponse(response);
}
protected disconnectRequest(response: DebugProtocol.DisconnectResponse, args: DebugProtocol.DisconnectArguments): void {
this.sendResponse(response);
this.shutdown();
}
protected launchRequest(response: DebugProtocol.LaunchResponse, args: DebugProtocol.LaunchRequestArguments): void {
this.sendResponse(response);
}
protected attachRequest(response: DebugProtocol.AttachResponse, args: DebugProtocol.AttachRequestArguments): void {
this.sendResponse(response);
}
protected setBreakPointsRequest(response: DebugProtocol.SetBreakpointsResponse, args: DebugProtocol.SetBreakpointsArguments): void {
this.sendResponse(response);
}
protected setExceptionBreakPointsRequest(response: DebugProtocol.SetExceptionBreakpointsResponse, args: DebugProtocol.SetExceptionBreakpointsArguments): void {
this.sendResponse(response);
}
protected continueRequest(response: DebugProtocol.ContinueResponse, args: DebugProtocol.ContinueArguments) : void {
this.sendResponse(response);
}
protected nextRequest(response: DebugProtocol.NextResponse, args: DebugProtocol.NextArguments) : void {
this.sendResponse(response);
}
protected stepInRequest(response: DebugProtocol.StepInResponse, args: DebugProtocol.StepInArguments) : void {
this.sendResponse(response);
}
protected stepOutRequest(response: DebugProtocol.StepOutResponse, args: DebugProtocol.StepOutArguments) : void {
this.sendResponse(response);
}
protected pauseRequest(response: DebugProtocol.PauseResponse, args: DebugProtocol.PauseArguments) : void {
this.sendResponse(response);
}
protected sourceRequest(response: DebugProtocol.SourceResponse, args: DebugProtocol.SourceArguments) : void {
this.sendResponse(response);
}
protected threadsRequest(response: DebugProtocol.ThreadsResponse): void {
this.sendResponse(response);
}
protected stackTraceRequest(response: DebugProtocol.StackTraceResponse, args: DebugProtocol.StackTraceArguments): void {
this.sendResponse(response);
}
protected scopesRequest(response: DebugProtocol.ScopesResponse, args: DebugProtocol.ScopesArguments): void {
this.sendResponse(response);
}
protected variablesRequest(response: DebugProtocol.VariablesResponse, args: DebugProtocol.VariablesArguments): void {
this.sendResponse(response);
}
protected evaluateRequest(response: DebugProtocol.EvaluateResponse, args: DebugProtocol.EvaluateArguments): void {
this.sendResponse(response);
}
//---- protected -------------------------------------------------------------------------------------------------
protected convertClientLineToDebugger(line: number): number {
if (this._debuggerLinesStartAt1) {
return this._clientLinesStartAt1 ? line : line + 1;
}
return this._clientLinesStartAt1 ? line - 1 : line;
}
protected convertDebuggerLineToClient(line: number): number {
if (this._debuggerLinesStartAt1) {
return this._clientLinesStartAt1 ? line : line - 1;
}
return this._clientLinesStartAt1 ? line + 1 : line;
}
protected convertClientColumnToDebugger(column: number): number {
if (this._debuggerColumnsStartAt1) {
return this._clientColumnsStartAt1 ? column : column + 1;
}
return this._clientColumnsStartAt1 ? column - 1 : column;
}
protected convertDebuggerColumnToClient(column: number): number {
if (this._debuggerColumnsStartAt1) {
return this._clientColumnsStartAt1 ? column : column + 1;
}
return this._clientColumnsStartAt1 ? column - 1 : column;
}
protected convertClientPathToDebugger(clientPath: string): string {
if (this._clientPathsAreURIs != this._debuggerPathsAreURIs) {
if (this._clientPathsAreURIs) {
return DebugSession.uri2path(clientPath);
} else {
return DebugSession.path2uri(clientPath);
}
}
return clientPath;
}
protected convertDebuggerPathToClient(debuggerPath: string): string {
if (this._debuggerPathsAreURIs != this._clientPathsAreURIs) {
if (this._debuggerPathsAreURIs) {
return DebugSession.uri2path(debuggerPath);
} else {
return DebugSession.path2uri(debuggerPath);
}
}
return debuggerPath;
}
//---- private -------------------------------------------------------------------------------
private static path2uri(str: string): string {
var pathName = str.replace(/\\/g, '/');
if (pathName[0] !== '/') {
pathName = '/' + pathName;
}
return encodeURI('file://' + pathName);
}
private static uri2path(url: string): string {
return Url.parse(url).pathname;
}
private static _formatPIIRegexp = /{([^}]+)}/g;
/*
* If argument starts with '_' it is OK to send its value to telemetry.
*/
private static formatPII(format:string, excludePII: boolean, args: {[key: string]: string}): string {
return format.replace(DebugSession._formatPIIRegexp, function(match, paramName) {
if (excludePII && paramName.length > 0 && paramName[0] !== '_') {
return match;
}
return args[paramName] && args.hasOwnProperty(paramName) ?
args[paramName] :
match;
})
}
}