forked from microsoft/pxt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebugger.ts
More file actions
520 lines (445 loc) · 19 KB
/
debugger.ts
File metadata and controls
520 lines (445 loc) · 19 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
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
/// <reference path="./debugProtocol.ts" />
/// <reference path="./utils.ts" />
namespace pxsim {
export function getWarningMessage(msg: string) {
let r: DebuggerWarningMessage = {
type: "debugger",
subtype: "warning",
breakpointIds: [],
message: msg
}
let s = runtime.currFrame
while (s != null) {
r.breakpointIds.push(s.lastBrkId)
s = s.parent
}
return r
}
export class BreakpointMap {
public fileMap: { [index: string]: [number, DebugProtocol.Breakpoint][] } = {};
public idMap: { [index: number]: DebugProtocol.Breakpoint } = {};
constructor(breakpoints: [number, DebugProtocol.Breakpoint][]) {
breakpoints.forEach(tuple => {
const [id, bp] = tuple;
if (!this.fileMap[bp.source.path]) {
this.fileMap[bp.source.path] = [];
}
this.fileMap[bp.source.path].push(tuple);
this.idMap[id] = bp;
});
for (const file in this.fileMap) {
const bps = this.fileMap[file];
// Sort the breakpoints to make finding the closest breakpoint to a
// given line easier later. Order first by start line and then from
// worst to best choice for each line.
this.fileMap[file] = bps.sort(([, a], [, b]) => {
if (a.line === b.line) {
if (b.endLine === a.endLine) {
return a.column - b.column;
}
// We want the closest breakpoint, so give preference to breakpoints
// that span fewer lines (i.e. breakpoints that are "tighter" around
// the line being searched for)
return b.endLine - a.endLine;
}
return a.line - b.line;
});
}
}
public getById(id: number): DebugProtocol.Breakpoint {
return this.idMap[id];
}
public verifyBreakpoint(path: string, breakpoint: DebugProtocol.SourceBreakpoint): [number, DebugProtocol.Breakpoint] {
const breakpoints = this.fileMap[path];
let best: [number, DebugProtocol.Breakpoint];
if (breakpoints) {
// Breakpoints are pre-sorted for each file. The last matching breakpoint
// in the list should be the best match
for (const [id, bp] of breakpoints) {
if (bp.line <= breakpoint.line && bp.endLine >= breakpoint.line) {
best = [id, bp];
}
}
}
if (best) {
best[1].verified = true;
return best;
}
return [-1, { verified: false }];
}
}
export function dumpHeap(v: any, heap: Map<any>): Variables {
function valToJSON(v: any) {
switch (typeof v) {
case "string":
case "number":
case "boolean":
return v;
case "function":
return {
text: "(function)",
type: "function"
}
case "undefined":
return null;
case "object":
if (!v) return null;
if (v instanceof RefObject) {
heap[(v as RefObject).id] = v;
let preview = RefObject.toDebugString(v);
let type = preview.startsWith('[') ? "array" : preview;
return {
id: (v as RefObject).id,
preview: preview,
hasFields: (v as any).fields !== null || preview.startsWith('['),
type: type,
}
}
if (v._width && v._height) {
return {
text: v._width + 'x' + v._height,
type: "image",
}
}
return {
text: "(object)",
type: "object",
}
default:
throw new Error();
}
}
function frameVars(frame: any) {
const r: Variables = {}
for (let k of Object.keys(frame)) {
// skip members starting with __
if (!/^__/.test(k) && /___\d+$/.test(k)) {
r[k.replace(/___\d+$/, '')] = valToJSON(frame[k])
}
}
if (frame.fields) {
// Fields of an object.
for (let k of Object.keys(frame.fields)) {
r[k.replace(/___\d+$/, '')] = valToJSON(frame.fields[k])
}
} else if (frame.data) {
// This is an Array.
(frame.data as any[]).forEach((element, index) => {
r[index] = valToJSON(element);
});
}
return r
}
return frameVars(v);
}
export function getBreakpointMsg(s: pxsim.StackFrame, brkId: number): { msg: DebuggerBreakpointMessage, heap: Map<any> } {
const heap: pxsim.Map<any> = {};
const msg: DebuggerBreakpointMessage = {
type: "debugger",
subtype: "breakpoint",
breakpointId: brkId,
globals: dumpHeap(runtime.globals, heap),
stackframes: [],
}
while (s != null) {
let info = s.fn ? (s.fn as any).info : null
if (info)
msg.stackframes.push({
locals: dumpHeap(s, heap),
funcInfo: info,
breakpointId: s.lastBrkId
})
s = s.parent
}
return { msg, heap };
}
export interface SimLaunchArgs extends DebugProtocol.LaunchRequestArguments {
/* Root directory of the project workspace being debugged */
projectDir: string;
}
export class SimDebugSession extends protocol.DebugSession {
// We only have one thread
// TODO: We could theoretically visualize the individual fibers
private static THREAD_ID = 1;
private driver: SimulatorDriver;
private lastBreak: DebuggerBreakpointMessage;
private state: StoppedState;
private projectDir: string;
private breakpoints: BreakpointMap;
constructor(container: HTMLElement) {
super();
let options: pxsim.SimulatorDriverOptions = {
onDebuggerBreakpoint: b => this.onDebuggerBreakpoint(b),
onDebuggerWarning: w => this.onDebuggerWarning(w),
onDebuggerResume: () => this.onDebuggerResume(),
onStateChanged: s => this.onStateChanged(s)
};
this.driver = new pxsim.SimulatorDriver(container, options);
}
public runCode(js: string, parts: string[], fnArgs: Map<string>, breakpoints: BreakpointMap, board: pxsim.BoardDefinition) {
this.breakpoints = breakpoints;
if (this.projectDir) {
this.fixBreakpoints();
}
this.sendEvent(new protocol.InitializedEvent());
this.driver.run(js, {
parts,
fnArgs,
boardDefinition: board
});
}
public stopSimulator(unload = false) {
this.driver.stop(unload);
}
protected initializeRequest(response: DebugProtocol.InitializeResponse, args: DebugProtocol.InitializeRequestArguments): void {
response.body.supportsConditionalBreakpoints = false;
response.body.supportsHitConditionalBreakpoints = false;
response.body.supportsFunctionBreakpoints = false;
response.body.supportsEvaluateForHovers = false;
response.body.supportsStepBack = false;
response.body.supportsSetVariable = false;
response.body.supportsRestartFrame = false;
response.body.supportsStepInTargetsRequest = false;
response.body.supportsGotoTargetsRequest = false;
response.body.supportsCompletionsRequest = false;
// This default debug adapter implements the 'configurationDone' request.
response.body.supportsConfigurationDoneRequest = true;
this.sendResponse(response);
}
protected disconnectRequest(response: DebugProtocol.DisconnectResponse, args: DebugProtocol.DisconnectArguments): void {
this.sendResponse(response);
this.shutdown();
}
protected launchRequest(response: DebugProtocol.LaunchResponse, args: SimLaunchArgs): void {
if (!this.projectDir) {
this.projectDir = util.normalizePath(args.projectDir);
if (this.breakpoints) {
this.fixBreakpoints();
}
}
this.sendResponse(response);
}
protected setBreakPointsRequest(response: DebugProtocol.SetBreakpointsResponse, args: DebugProtocol.SetBreakpointsArguments): void {
response.body = { breakpoints: [] };
const ids: number[] = [];
args.breakpoints.forEach(requestedBp => {
if (this.breakpoints) {
const [id, bp] = this.breakpoints.verifyBreakpoint(util.relativePath(this.projectDir, args.source.path), requestedBp);
response.body.breakpoints.push(bp);
if (bp.verified) {
ids.push(id);
}
}
else {
response.body.breakpoints.push({ verified: false });
}
});
this.driver.setBreakpoints(ids);
this.sendResponse(response);
}
protected continueRequest(response: DebugProtocol.ContinueResponse, args: DebugProtocol.ContinueArguments): void {
this.driver.resume(SimulatorDebuggerCommand.Resume);
this.sendResponse(response);
}
protected nextRequest(response: DebugProtocol.NextResponse, args: DebugProtocol.NextArguments): void {
this.driver.resume(SimulatorDebuggerCommand.StepOver);
this.sendResponse(response);
}
protected stepInRequest(response: DebugProtocol.StepInResponse, args: DebugProtocol.StepInArguments): void {
this.driver.resume(SimulatorDebuggerCommand.StepInto);
this.sendResponse(response);
}
protected stepOutRequest(response: DebugProtocol.StepOutResponse, args: DebugProtocol.StepOutArguments): void {
this.driver.resume(SimulatorDebuggerCommand.StepOut);
this.sendResponse(response);
}
protected pauseRequest(response: DebugProtocol.PauseResponse, args: DebugProtocol.PauseArguments): void {
this.driver.resume(SimulatorDebuggerCommand.Pause);
this.sendResponse(response);
}
protected threadsRequest(response: DebugProtocol.ThreadsResponse): void {
response.body = { threads: [{ id: SimDebugSession.THREAD_ID, name: "main" }] }
this.sendResponse(response);
}
protected stackTraceRequest(response: DebugProtocol.StackTraceResponse, args: DebugProtocol.StackTraceArguments): void {
if (this.lastBreak) {
const frames = this.state.getFrames();
response.body = { stackFrames: frames };
}
this.sendResponse(response);
}
protected scopesRequest(response: DebugProtocol.ScopesResponse, args: DebugProtocol.ScopesArguments): void {
if (this.state) {
response.body = { scopes: this.state.getScopes(args.frameId) }
}
this.sendResponse(response);
}
protected variablesRequest(response: DebugProtocol.VariablesResponse, args: DebugProtocol.VariablesArguments): void {
if (this.state) {
response.body = { variables: this.state.getVariables(args.variablesReference) };
}
this.sendResponse(response);
}
private onDebuggerBreakpoint(breakMsg: DebuggerBreakpointMessage) {
this.lastBreak = breakMsg;
this.state = new StoppedState(this.lastBreak, this.breakpoints, this.projectDir);
if (breakMsg.exceptionMessage) {
const message = breakMsg.exceptionMessage.replace(/___\d+/g, '');
this.sendEvent(new protocol.StoppedEvent("exception", SimDebugSession.THREAD_ID, message));
}
else {
this.sendEvent(new protocol.StoppedEvent("breakpoint", SimDebugSession.THREAD_ID));
}
}
private onDebuggerWarning(warnMsg: DebuggerWarningMessage) {
}
private onDebuggerResume() {
this.sendEvent(new protocol.ContinuedEvent(SimDebugSession.THREAD_ID, true));
}
private onStateChanged(state: SimulatorState) {
switch (state) {
case SimulatorState.Paused:
// Sending a stopped event here would be redundant
break;
case SimulatorState.Running:
this.sendEvent(new protocol.ContinuedEvent(SimDebugSession.THREAD_ID, true))
break;
case SimulatorState.Stopped:
this.sendEvent(new protocol.TerminatedEvent())
break;
case SimulatorState.Unloaded:
default:
}
}
private fixBreakpoints() {
// Fix breakpoint locations from the debugger's format to the client's
for (const bpId in this.breakpoints.idMap) {
const bp = this.breakpoints.idMap[bpId];
bp.source.path = util.pathJoin(this.projectDir, bp.source.path);
bp.line = this.convertDebuggerLineToClient(bp.line);
bp.endLine = this.convertDebuggerLineToClient(bp.endLine);
bp.column = this.convertDebuggerColumnToClient(bp.column);
bp.endColumn = this.convertDebuggerColumnToClient(bp.endColumn);
}
}
}
interface SimFrame {
locals: Variables;
breakpointId: number;
// pxtc.FunctionLocationInfo
// FIXME: Make this dependency explicit
funcInfo: {
functionName: string;
fileName: string;
start: number;
length: number;
line: number;
column: number;
endLine?: number;
endColumn?: number;
};
}
/**
* Maintains the state at the current breakpoint and handles lazy
* queries for stack frames, scopes, variables, etc. The protocol
* expects requests to be made in the order:
* Frames -> Scopes -> Variables
*/
class StoppedState {
private _currentId: number = 1;
private _frames: { [index: number]: SimFrame } = {};
private _vars: { [index: number]: util.Lazy<protocol.Variable[]> } = {};
private _globalScope: DebugProtocol.Scope;
constructor(private _message: DebuggerBreakpointMessage, private _map: BreakpointMap, private _dir: string) {
const globalId = this.nextId();
this._vars[globalId] = this.getVariableValues(this._message.globals);
this._globalScope = {
name: "Globals",
variablesReference: globalId,
expensive: false
};
}
/**
* Get stack frames for current breakpoint.
*/
getFrames(): DebugProtocol.StackFrame[] {
return this._message.stackframes.map((s: SimFrame, i: number) => {
const bp = this._map.getById(s.breakpointId);
if (bp) {
this._frames[s.breakpointId] = s;
return {
id: s.breakpointId,
name: s.funcInfo ? s.funcInfo.functionName : (i === 0 ? "main" : "anonymous"),
line: bp.line,
column: bp.column,
endLine: bp.endLine,
endColumn: bp.endLine,
source: bp.source
};
}
return undefined;
}).filter(b => !!b);
}
/**
* Returns scopes visible to the given stack frame.
*
* TODO: Currently, we only support locals and globals (no closures)
*/
getScopes(frameId: number): DebugProtocol.Scope[] {
const frame = this._frames[frameId];
if (frame) {
const localId = this.nextId();
this._vars[localId] = this.getVariableValues(frame.locals);
return [{
name: "Locals",
variablesReference: localId,
expensive: false
}, this._globalScope];
}
return [this._globalScope];
}
/**
* Returns variable information (and object properties)
*/
getVariables(variablesReference: number): DebugProtocol.Variable[] {
const lz = this._vars[variablesReference];
return (lz && lz.value) || [];
}
private getVariableValues(v: Variables): util.Lazy<DebugProtocol.Variable[]> {
return new util.Lazy(() => {
const result: DebugProtocol.Variable[] = [];
for (const name in v) {
const value = v[name];
let vString: string;
let variablesReference = 0;
if (value === null) {
vString = "null";
}
else if (value === undefined) {
vString = "undefined"
}
else if (typeof value === "object") {
vString = "(object)";
variablesReference = this.nextId();
// Variables should be requested lazily, so reference loops aren't an issue
this._vars[variablesReference] = this.getVariableValues(value);
}
else {
vString = value.toString();
}
// Remove the metadata from the name
const displayName = name.substr(0, name.lastIndexOf("___"));
result.push({
name: displayName,
value: vString,
variablesReference
});
}
return result;
});
}
private nextId(): number {
return this._currentId++;
}
}
}