-
Notifications
You must be signed in to change notification settings - Fork 6
/
term-debugger.mjs
565 lines (488 loc) · 16.2 KB
/
term-debugger.mjs
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
/*
TODO:
* skip to the end
* have to press i twice to step in?
* switch to left and right arrow for step in and out
* allow collapsing of data structures
* hover over source to see value
* zoom-in/zoom-out effect when going into or coming out of frame
* current line where... query
* last-mutation-of query
* make use of references when rendering heap objects when it makes sense. Show anchoring ids
* allow arrow buttons + current mouse position for scrolling as well
* click to select a stack frame and jump out to that frame
* ability to change layout
* ability to hide a pane (heap pane for when you are not using heap objects for example)
* re-layout when window resize occurs
* help menu
* back button
* color coding of heap ids/objects
* current line query (done)
* show current snapshot number and other info in status bar
* indicator on the side to show whether you are before or after the line (done)
* bug in heap string rendering (two_sum.py) (done)
* bug shape_calculator.py floats showing as {} (done)
* support multiple files (done)
* bug when circular reference (circular-ref.play) (done)
* play lang: when exception, record it in DB and show it in debugger (done)
* option to choose between stack + heap or rich stack (done)
* use i and k for control for qwerty users (done)
* stack parameter rendering (done)
* styled function signatures in stack frame (done)
* make a short alias for node src/term-debugger/term-debugger.mjs (done)
* inlined object display on the stack similar to chrome (done)
* bring back current line highlight (styled string) (done)
* horizontal scroll (done)
* ENTER to re-center code pane to current line (done)
* scrolling code stroll (done)
* scrolling for stack pane (done)
* scrolling for heap pane (done)
* separate files (done)
* heap pane (done)
* step over (done)
* step back over (done)
* step out (done)
* step back out (done)
* stack frame (done)
*/
import path from "path";
import fetch from "node-fetch";
import fs from "fs";
import { CodePane } from "./code-pane.mjs";
import { HeapPane } from "./heap-pane.mjs";
import { StackPane } from "./stack-pane.mjs";
import { RichStackPane } from "./rich-stack-pane.mjs";
import { DataCache } from "./data-cache.mjs";
import {
clearScreen,
renderText,
printAt,
setCursorVisible,
setMouseButtonTracking
} from "./term-utils.mjs";
import { HistoryServer } from "./spawn-history-server.mjs";
import simpleSleep from "simple-sleep";
import StyledString from "styled_string";
import { inspect } from "util";
import exitHook from "exit-hook";
function TermDebugger() {
const self = {
get apiUrl() { return url },
get snapshot() { return snapshot },
get cache() { return cache },
get log() { return log },
fastForward,
rewind,
start,
cleanUp
};
const log = fs.createWriteStream("term-debug.log");
let url;
let historyServer;
const argument = process.argv[2];
const mode = process.argv[3];
let cache;
let snapshot;
let screen;
let windowWidth, windowHeight;
async function start() {
if (!argument) {
console.log("Please provide either a URL or a file.");
exit();
}
if (argument.startsWith("http://")) {
url = argument;
} else {
url = "http://localhost:1337";
// start the history server
log.write(`Starting history API server\n`);
historyServer = HistoryServer(argument, 1337);
await historyServer.start();
}
cache = DataCache(self);
snapshot = null;
process.stdin.setRawMode(true);
process.stdin.on('data', onDataReceived);
clearScreen();
setCursorVisible(false);
setMouseButtonTracking(true);
[windowWidth, windowHeight] = process.stdout.getWindowSize();
if (mode === "low") {
screen = LowLevelScreen(self);
} else {
screen = HighLevelScreen(self);
}
await fetchFirstStep();
screen.updateDisplay();
updateStatusBar();
}
function onDataReceived(data) {
log.write("Data: ");
for (let i = 0; i < data.length; i++) {
log.write(data[i] + " ");
}
log.write("\n");
log.write("String: " + JSON.stringify(String(data)) + "\n");
if (String(data) === 'q') {
clearScreen();
exit();
}
if (isDownArrow(data)) {
stepOver();
} else if (isUpArrow(data)) {
stepOverBackward();
} else if (isStepIntoKey(data)) {
stepForward();
} else if (isStepOutKey(data)) {
stepOut();
} else if (String(data) == "r" || String(data) == "p") {
stepBackward();
} else if (String(data) == "e") {
gotoEnd();
} else if (String(data) == "b") {
gotoBeginning();
} else if (isMouseClick(data)) {
const x = data[4] - 32;
const y = data[5] - 32;
if (screen.isPointInCodePane(x, y)) {
const lineNo = screen.codePane.getLineNoForY(y);
fastForward(lineNo);
}
} else if (isCtrlMouseClick(data) || isRightMouseClick(data)) {
const x = data[4] - 32;
const y = data[5] - 32;
if (screen.isPointInCodePane(x, y)) {
const lineNo = screen.codePane.getLineNoForY(y);
rewind(lineNo);
}
} else if (isWheelUpEvent(data)) {
scrollUp(data);
} else if (isWheelDownEvent(data)) {
scrollDown(data);
} else if (isWheelUpAltEvent(data)) {
scrollLeft(data);
} else if (isWheelDownAltEvent(data)) {
scrollRight(data);
} else if (isEnter(data)) {
codePane.showCurrentLine();
}
}
function drawDivider(leftOffset) {
for (let i = 0; i < windowHeight; i++) {
printAt(leftOffset, i + topOffset, "┃");
}
}
function logSnapshot(snapshot) {
// log.write(`Snapshot ${snapshot.id}: ${JSON.stringify(snapshot, null, " ")}\n`);
log.write(`Snapshot ${snapshot && snapshot.id}, ${snapshot && snapshot.state}\n`);
}
async function fetchFirstStep() {
let start = new Date();
const response = await fetch(`${url}/api/SnapshotWithError`);
const result = await response.json();
let end = new Date();
log.write(`${`${url}/api/SnapshotWithError`} took ${end - start}ms.\n`);
if (result) {
snapshot = result;
logSnapshot(snapshot);
await cache.update(snapshot);
} else {
let start = new Date();
const response = await fetch(`${url}/api/SnapshotExpanded?id=1`);
snapshot = await response.json();
let end = new Date();
log.write(`${`${url}/api/SnapshotExpanded?id=1`} took ${end - start}ms.\n`);
logSnapshot(snapshot);
await cache.update(snapshot);
}
}
async function gotoBeginning() {
await stepWithFetch(`${url}/api/SnapshotExpanded?id=1`);
}
async function gotoEnd() {
await stepWithFetch(`${url}/api/GotoEnd`);
}
async function stepForward() {
await stepWithFetch(`${url}/api/SnapshotExpanded?id=${snapshot.id + 1}`);
}
async function stepBackward() {
await stepWithFetch(`${url}/api/SnapshotExpanded?id=${snapshot.id - 1}`);
}
async function stepOver() {
await stepWithFetch(`${url}/api/StepOver?id=${snapshot.id}`);
}
async function stepOverBackward() {
await stepWithFetch(`${url}/api/StepOverBackward?id=${snapshot.id}`);
}
async function stepOut() {
await stepWithFetch(`${url}/api/StepOut?id=${snapshot.id}`);
}
async function stepOutBackward() {
await stepWithFetch(`${url}/api/StepOutBackward?id=${snapshot.id}`);
}
async function fastForward(lineNo) {
const funCall = cache.getFunCall(snapshot.fun_call_id);
const fun = cache.getFun(funCall.fun_id);
const codeFileId = fun.code_file_id;
await stepWithFetch(`${url}/api/FastForward?from=${snapshot.id}&code_file_id=${codeFileId}&line_no=${lineNo}`);
}
async function rewind(lineNo) {
const funCall = cache.getFunCall(snapshot.fun_call_id);
const fun = cache.getFun(funCall.fun_id);
const codeFileId = fun.code_file_id;
await stepWithFetch(`${url}/api/Rewind?from=${snapshot.id}&code_file_id=${codeFileId}&line_no=${lineNo}`);
}
async function stepWithFetch(url) {
let start = new Date();
const response = await fetch(url);
screen.unsetStep();
const result = await response.json();
let end = new Date();
log.write(`${url} took ${end - start}ms.\n`);
logSnapshot(result);
if (result) {
snapshot = result;
await cache.update(snapshot);
updateStatusBar();
}
screen.updateDisplay();
}
function updateStatusBar() {
let funCall = cache.getFunCall(snapshot.fun_call_id);
let fun = cache.getFun(funCall.fun_id);
let message = `Step ${snapshot.id} ${fun.name}() line ${snapshot.line_no}`;
let color;
if (snapshot.error) {
message += `, Error: ${snapshot.error.message || snapshot.error.type}`;
color = "red";
} else {
color = "blue";
}
const leftPadding = Math.max(0, Math.floor((windowWidth - message.length) / 2));
const banner = (Array(leftPadding + 1).join(" ") + message +
Array(Math.max(0, windowWidth - leftPadding - message.length + 1)).join(" "))
.substring(0, windowWidth);
printAt(1, windowHeight,
StyledString(banner, { foreground: color, background: "white" }).toString());
}
function scrollUp(data) {
const x = data[4] - 32;
const targetPane = pickTargetPane(x);
targetPane.textPane.scrollUp();
}
function scrollDown(data) {
const x = data[4] - 32;
const targetPane = pickTargetPane(x);
targetPane.textPane.scrollDown();
}
function scrollLeft(data) {
const x = data[4] - 32;
const targetPane = pickTargetPane(x);
targetPane.textPane.scrollLeft();
}
function scrollRight(data) {
const x = data[4] - 32;
const targetPane = pickTargetPane(x);
targetPane.textPane.scrollRight();
}
function pickTargetPane(x) {
return screen.pickTargetPane(x);
}
function cleanUp() {
process.stdin.setRawMode(false);
setCursorVisible(true);
setMouseButtonTracking(false);
if (historyServer) {
historyServer.stop();
}
}
function exit() {
cleanUp();
process.exit(0)
}
return self;
}
function HighLevelScreen(db) {
const self = {
initialDisplay,
updateDisplay,
pickTargetPane,
unsetStep,
isPointInCodePane,
get codePane() { return codePane }
};
const [windowWidth, windowHeight] = process.stdout.getWindowSize();
const singlePaneWidth = Math.floor((windowWidth - 2) / 2);
const dividerColumn1 = singlePaneWidth + 1;
const codePane = CodePane(db, {
top: 1,
left: 1,
width: dividerColumn1 - 1,
height: windowHeight - 1
});
const stackPane = RichStackPane(db, {
top: 1,
left: dividerColumn1 + 1,
width: windowWidth - singlePaneWidth - 1,
height: windowHeight - 1
});
drawDivider(dividerColumn1);
function drawDivider(leftOffset) {
for (let i = 0; i < windowHeight - 1; i++) {
printAt(leftOffset, i + 1, "┃");
}
}
function unsetStep() {
codePane.unsetStep();
}
function initialDisplay() {
codePane.initialDisplay();
stackPane.updateDisplay();
}
function updateDisplay() {
stackPane.updateDisplay();
codePane.updateDisplay();
}
function pickTargetPane(x) {
if (x < dividerColumn1) {
return codePane;
} else {
return stackPane;
}
}
function isPointInCodePane(x, y) {
return x < dividerColumn1;
}
return self;
}
function LowLevelScreen(db) {
const self = {
initialDisplay,
updateDisplay,
pickTargetPane,
unsetStep,
isPointInCodePane,
get codePane() { return codePane }
};
const [windowWidth, windowHeight] = process.stdout.getWindowSize();
const singlePaneWidth = Math.floor((windowWidth - 2) / 3);
const dividerColumn1 = singlePaneWidth + 1;
const dividerColumn2 = dividerColumn1 + singlePaneWidth + 1;
const codePane = CodePane(db, {
top: 1,
left: 1,
width: dividerColumn1 - 1,
height: windowHeight - 1
});
const stackPane = StackPane(db, {
top: 1,
left: dividerColumn1 + 1,
width: singlePaneWidth,
height: windowHeight - 1
});
const heapPane = HeapPane(db, {
top: 1,
left: dividerColumn2 + 1,
width: windowWidth - 2 * singlePaneWidth - 2,
height: windowHeight - 1
});
drawDivider(dividerColumn1);
drawDivider(dividerColumn2);
function drawDivider(leftOffset) {
for (let i = 0; i < windowHeight - 1; i++) {
printAt(leftOffset, i + 1, "┃");
}
}
function unsetStep() {
codePane.unsetStep();
}
function initialDisplay() {
codePane.initialDisplay();
stackPane.updateDisplay();
heapPane.updateDisplay();
}
function updateDisplay() {
stackPane.updateDisplay();
heapPane.updateDisplay();
codePane.updateDisplay();
}
function isPointInCodePane(x, y) {
return x < dividerColumn1;
}
function pickTargetPane(x) {
if (x < dividerColumn1) {
return codePane;
} else if (x < dividerColumn2) {
return stackPane;
} else {
return heapPane;
}
}
return self;
}
const db = TermDebugger();
exitHook(db.cleanUp);
db.start().catch((e) => {
console.log(e.stack)
});
function isStepIntoKey(data) {
return String(data) === "i";
}
function isStepOutKey(data) {
return String(data) === "o";
}
function isLeftArrow(data) {
return data.length === 3 &&
data[0] === 27 &&
data[1] === 91 &&
data[2] === 68;
}
function isRightArrow(data) {
return data.length === 3 &&
data[0] === 27 &&
data[1] === 91 &&
data[2] === 67;
}
function isUpArrow(data) {
return data.length === 3 &&
data[0] === 27 &&
data[1] === 91 &&
data[2] === 65;
}
function isDownArrow(data) {
return data.length === 3 &&
data[0] === 27 &&
data[1] === 91 &&
data[2] === 66;
}
function isEnter(data) {
return data.length === 1 && data[0] === 13;
}
function isWheelUpEvent(data) {
return (data.length === 6 || data.length === 12) &&
data[0] === 27 && data[1] === 91 && data[2] === 77 && data[3] === 97;
}
function isWheelDownEvent(data) {
return (data.length === 6 || data.length === 12) &&
data[0] === 27 && data[1] === 91 && data[2] === 77 && data[3] === 96;
}
function isWheelUpAltEvent(data) {
return (data.length === 6 || data.length === 12) &&
data[0] === 27 && data[1] === 91 && data[2] === 77 && data[3] === 105;
}
function isWheelDownAltEvent(data) {
return (data.length === 6 || data.length === 12) &&
data[0] === 27 && data[1] === 91 && data[2] === 77 && data[3] === 104;
}
function isMouseClick(data) {
return (data.length === 6) &&
data[0] === 27 && data[1] === 91 && data[2] === 77 && data[3] === 35;
}
function isRightMouseClick(data) {
return (data.length === 6) &&
data[0] === 27 && data[1] === 91 && data[2] === 77 && data[3] === 34;
}
function isCtrlMouseClick(data) {
return (data.length === 6) &&
data[0] === 27 && data[1] === 91 && data[2] === 77 && data[3] === 51;
}