-
Notifications
You must be signed in to change notification settings - Fork 6
/
clojureDebug.ts
664 lines (543 loc) · 20.4 KB
/
clojureDebug.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
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
"use strict";
///<reference path="node.d.ts"/>
import {DebugSession, InitializedEvent, TerminatedEvent, StoppedEvent, OutputEvent, Thread, StackFrame, Scope, Source, Handles} from 'vscode-debugadapter';
import {DebugProtocol} from 'vscode-debugprotocol';
import {readFileSync} from 'fs';
// import {blue} from 'chalk';
import {basename, dirname, join} from 'path';
import {spawn} from 'child_process';
import nrepl_client = require('nrepl-client');
let chalk = require("chalk");
// Constants to represent the various states of the debugger
class DebuggerState {
public static get PRE_LAUNCH(): string { return "PRE_LAUNCH";}
public static get REPL_STARTED(): string {return "REPL_STARTED";}
public static get DEBUGGER_ATTACHED(): string {return "DEBUGGER_ATTACHED";}
public static get REPL_READY(): string {return "REPL_READY";}
public static get LAUNCH_COMPLETE(): string {return "LAUNCH_COMPLETE";}
}
class DebuggerSubState {
public static get NOP(): string { return "NOP";}
public static get EVENT_IN_PROGRESS(): string { return "EVENT_IN_PROGRESS";}
}
/**
* This interface should always match the schema found in the clojure-debug extension manifest.
*/
export interface LaunchRequestArguments {
// Absolute path to the tool.jar file.
toolsJar: string;
// Port for nREPL
replPort: number;
// Port for JDWP connection
debugPort: number;
// Current working directory
cwd: string;
// The environment variables that should be set when running the target.
env: string[];
// Automatically stop target after launch. If not specified, target does not stop.
stopOnEntry?: boolean;
}
// utility funciton to
class ClojureDebugSession extends DebugSession {
// Get the full path to a source file. Input paths are of the form repl_test/core.clj.
// TODO Change this to search for the given debuggerPath under all the src directories.
// For now assume all source is under src.
protected convertDebuggerPathToClient(debuggerPath: string): string {
return join(this._cwd, "src", debuggerPath);
}
// just use the first thread as the default thread
private static THREAD_ID = 0;
// the current working directory
private _cwd: string;
// Clojure REPL process
private _cdtRepl: any;
private _primaryRepl: any;
private __currentLine: number;
private get _currentLine() : number {
return this.__currentLine;
}
private set _currentLine(line: number) {
this.__currentLine = line;
this.sendEvent(new OutputEvent(`line: ${line}\n`)); // print current line on debug console
}
private __threadIndex: number = 0;
private __threads: Thread[] = [];
// update the list of Threads with the given list of thread names
private updateThreads(thds: string[]) {
// add in new threads
for (var t of thds){
// TypeScript arrays don't have a `find` method
var index = -1;
for (var i=0; i < this.__threads.length; i++) {
const thread = this.__threads[i];
if (thread.name == t) {
index = i;
break;
}
}
if (index == -1) {
var newThread = new Thread(this.__threadIndex, t);
this.__threadIndex = this.__threadIndex + 1;
this.__threads.push(newThread);
}
}
// remove threads that aren't on the new list
this.__threads = this.__threads.filter((value: Thread) => {
if (thds.indexOf(value.name) != -1) {
return true;
} else {
return false;
}
});
}
// Returns the Thread with the given name.
private threadWithName(name: string): Thread {
// TypeScript arrays don't have a `find` method
var index = -1;
for (var i=0; i < this.__threads.length; i++) {
const thread = this.__threads[i];
if (thread.name == name) {
index = i;
break;
}
}
var rval = null;
if (index != -1) {
rval = this.__threads[index];
}
return rval;
}
// Returns the Thread with the given id
private threadWithID(id: number): Thread {
var rval: Thread = null;
for (var i = 0; i < this.__threads.length; i++) {
const t = this.__threads[i];
if (t.id == id) {
rval = t;
break;
}
}
return rval;
}
private _sourceFile: string;
private _sourceLines: string[];
private _breakPoints: any;
// private _variableHandles: Handles<string>;
private _variableHandles: Handles<any[]>;
private _connection: nrepl_client.Connection;
// Debugger state
private _debuggerState: DebuggerState;
// Debugger substate
private _debuggerSubState: DebuggerSubState;
// map of sessions ids to evalulation results
private _evalResults: any;
// list of stack frames for the current thread
private _frames: StackFrame[];
public constructor(_debuggerLinesStartAt1: boolean = true, isServer: boolean = false) {
// We always use debuggerLinesStartAt1 = true for Clojure
super(true, isServer);
this._sourceFile = null;
this._sourceLines = [];
this._currentLine = 0;
this._breakPoints = {};
this._variableHandles = new Handles<any[]>();
this._debuggerState = DebuggerState.PRE_LAUNCH;
this._debuggerSubState = DebuggerSubState.NOP;
this._evalResults = {};
this._frames = [];
}
// send data form the REPL's stdout to be displayed in the debugger
protected output(text, category){
var outputEvent = new OutputEvent(text, category);
this.sendEvent(outputEvent);
console.log(text);
}
protected pout(text){
this.output(chalk.magenta(text), "stdout");
}
protected perr(text){
this.output(text, "stderr");
}
protected initializeRequest(response: DebugProtocol.InitializeResponse, args: DebugProtocol.InitializeRequestArguments): void {
console.log("INITIALIZE REQUEST");
// announce that we are ready to accept breakpoints -> fire the initialized event to give UI a chance to set breakpoints
this.sendEvent(new InitializedEvent());
response.body.supportsConfigurationDoneRequest = true;``
// We want to have VS Code call evaulate when hovering over source (yet. if there is a way to expand to a full
// form then we will want to do this.)
response.body.supportsEvaluateForHovers = false;
// SOME DAY!!!
response.body.supportsFunctionBreakpoints = false;
this.sendResponse(response);
//super.initializeRequest(response, args);
}
// Handle events from the REPL (breakpoints, exceptions). We make a single request here to get an event,
// which effectivley creates a channel that the middleware can send event info back to us on.
// TODO - test to see if there is a timeout issue here since we will be listening on the socket waiting
// for a response until an event happens.
private handleEvent(err: any, result: any){
if (result != null) {
let event = result[0]["event"];
var eventMap = JSON.parse(event);
var threadName = eventMap["thread"];
let eventType = eventMap["event-type"];
const thread = this.threadWithName(threadName);
var threadId = -1;
if (thread == null) {
threadId = this.__threadIndex;
this.__threads.push(new Thread(threadId, threadName));
this.__threadIndex = this.__threadIndex + 1;
} else {
threadId = thread.id;
}
if (eventType == "breakpoint") {
var src = eventMap["src"];
var line = eventMap["line"];
this._currentLine = line;
console.log("Sending breakpoint event to debugger for thread " + threadId);
this.sendEvent(new StoppedEvent("breakpoint", threadId));
}
}
// start listening for events again
let debug = this;
this._connection.send({op: 'get-event'}, (err: any, result: any) => {
// TODO handle errors here
console.log("GOT EVENT:");
console.log(result);
debug.handleEvent(err, result);
});
}
// Handle output from the REPL after launch is complete
protected handleREPLOutput(output) {
if (this._debuggerSubState == DebuggerSubState.EVENT_IN_PROGRESS) {
this._debuggerSubState = DebuggerSubState.NOP;
var eventMap = JSON.parse(output);
var event = eventMap["event"];
var threadName = eventMap["thread"];
const thread = this.threadWithName(threadName);
var threadId = -1;
if (thread == null) {
threadId = this.__threadIndex;
this.__threads.push(new Thread(threadId, threadName));
this.__threadIndex = this.__threadIndex + 1;
}
if (event == "breakpoint") {
var src = eventMap["src"];
var line = eventMap["line"];
this._currentLine = line;
this.sendEvent(new StoppedEvent("breakpoint", threadId));
}
}
if (output.search(/CDB MIDDLEWARE EVENT/) != -1) {
this._debuggerSubState = DebuggerSubState.EVENT_IN_PROGRESS;
}
}
protected launchRequest(response: DebugProtocol.LaunchResponse, args: LaunchRequestArguments): void {
console.log("LAUNCH REQUEST");
//this._sourceFile = args.program;
//this._sourceLines = readFileSync(this._sourceFile).toString().split('\n');
//var cwd = dirname(args.program);
this._cwd = args.cwd;
var repl_port = 5555;
if (args.replPort) {
repl_port = args.replPort;
}
var debug_port = 8030;
if (args.debugPort) {
debug_port = args.debugPort;
}
var env = {"JVM_OPTS": "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=" + debug_port,
"CLOJURE_DEBUG_JDWP_PORT": "" + debug_port};
this._primaryRepl = spawn('/usr/local/bin/lein', ["with-profile", "+debug-repl", "repl", ":headless", ":port", "" + repl_port], {cwd: this._cwd, env: env});
this._debuggerState = DebuggerState.REPL_STARTED;
this._primaryRepl.stdout.on('data', (data) => {
var output = '' + data;
// TODO This should check the message instead of assuming the second
// message means the REPL is ready to receive connections.
if (this._debuggerState == DebuggerState.REPL_READY) {
this._debuggerState = DebuggerState.REPL_READY;
this._connection = nrepl_client.connect({port: repl_port, host: "127.0.0.1", verbose: false});
// tell the middleware to start the debugger
// this._connection.send({op: 'connect', port: debug_port}, (err: any, result: any) => {
// console.log(result);
// });
// start listening for events
this.handleEvent(null, null);
this._debuggerState = DebuggerState.LAUNCH_COMPLETE;
var debug = this;
this._connection.send({op: 'list-threads'}, (err: any, result: any) => {
console.log(result);
this.updateThreads(result[0]["threads"]);
console.log("Got threads");
});
if (args.stopOnEntry) {
this._currentLine = 1;
this.sendResponse(response);
// we stop on the first line - TODO need to figure out what thread this would be and if we even want to support this
this.sendEvent(new StoppedEvent("entry", ClojureDebugSession.THREAD_ID));
} else {
// we just start to run until we hit a breakpoint or an exception
this.continueRequest(response, { threadId: ClojureDebugSession.THREAD_ID });
}
}
if (this._debuggerState == DebuggerState.DEBUGGER_ATTACHED) {
this._debuggerState = DebuggerState.REPL_READY;
}
if (this._debuggerState == DebuggerState.REPL_STARTED) {
this._debuggerState = DebuggerState.DEBUGGER_ATTACHED;
}
this.handleREPLOutput(output);
this.pout(output);
});
this._primaryRepl.stderr.on('data', (data) => {
this.perr(data);
console.log(`stderr: ${data}`);
});
this._primaryRepl.on('close', (code) => {
if (code !== 0) {
console.log(`REPL process exited with code ${code}`);
}
console.log("REPL closed");
});
}
// TODO Fix the check for successful breakpoints and return the correct list
protected finishBreakPointsRequest(response: DebugProtocol.SetBreakpointsResponse, args: DebugProtocol.SetBreakpointsArguments, fileContents: Buffer, path: string): void {
var clientLines = args.lines;
// read file contents into array for direct access
var lines = fileContents.toString().split('\n');
var newPositions = [clientLines.length];
var breakpoints = [];
// verify breakpoint locations
// TODO fix this
for (var i = 0; i < clientLines.length; i++) {
var l = this.convertClientLineToDebugger(clientLines[i]);
this._connection.send({op: 'set-breakpoint', line: l, path: path}, (err: any, result: any) => {
console.log(result);
});
// this._connection.send({op: 'set-breakpoint', line: l, path: path}, function (err, result) {
// console.log(result);
// });
var verified = false;
if (l < lines.length) {
// if a line starts with '+' we don't allow to set a breakpoint but move the breakpoint down
if (lines[l].indexOf("+") == 0)
l++;
// if a line starts with '-' we don't allow to set a breakpoint but move the breakpoint up
if (lines[l].indexOf("-") == 0)
l--;
verified = true; // this breakpoint has been validated
}
newPositions[i] = l;
breakpoints.push({ verified: verified, line: this.convertDebuggerLineToClient(l)});
}
this._breakPoints[path] = newPositions;
// send back the actual breakpoints
response.body = {
breakpoints: breakpoints
};
this.sendResponse(response);
}
protected setBreakPointsRequest(response: DebugProtocol.SetBreakpointsResponse, args: DebugProtocol.SetBreakpointsArguments): void {
console.log("Set breakpoint requested");
var path = args.source.path;
var debug = this;
this._connection.send({op: 'clear-breakpoints', path: path}, (err: any, result: any) => {
// read file contents
var fileContents = readFileSync(path);
var regex = /\(ns\s+?(.*?)(\s|\))/;
var ns = regex.exec(fileContents.toString())[1];
// Load the associated namespace into the REPL.
// We must wait for the response before replying.
console.log("SESSIONS:");
console.log(this._connection.sessions);
// this._connection.send({op: 'require-namespace', namespace: ns}, (err: any, result: any) => {
debug._connection.eval("(require '" + ns + ")", (err: any, result: any) => {
// TODO handle errors here
debug.finishBreakPointsRequest(response, args, fileContents, path)
//console.log(result);
});
});
// TODO reject breakpoint requests outside of a namespace
}
protected threadsRequest(response: DebugProtocol.ThreadsResponse): void {
console.log("GETTING THREADS");
const debug = this;
this._connection.send({op: 'list-threads'}, (err: any, result: any) => {
console.log(result);
debug.updateThreads(result[0]["threads"]);
console.log("Sending threads to debugger:\n");
for (let i = 0; i < debug.__threads.length; i++) {
let th = debug.__threads[i];
console.log("id: " + th.id + " name: " + th.name);
}
response.body = {
threads: debug.__threads
};
debug.sendResponse(response);
});
}
protected stackTraceRequest(response: DebugProtocol.StackTraceResponse, args: DebugProtocol.StackTraceArguments): void {
console.log("STACK FRAME REQUEST");
const levels = args.levels;
const threadId = args.threadId;
console.log("LEVELS: " + levels);
console.log("THREAD_ID: " + threadId);
const th = this.threadWithID(threadId)
const debug = this;
this._connection.send({op: 'list-frames', "thread-name": th.name}, (err: any, result: any) => {
console.log(result);
var resFrames = result[0]["frames"];
console.log(resFrames);
const frames: StackFrame[] = resFrames.map((frame: any, index: number) : StackFrame => {
var f = new StackFrame(index, `${frame["srcName"]}(${index})`, new Source(frame["srcName"], debug.convertDebuggerPathToClient(frame["srcPath"])), debug.convertDebuggerLineToClient(frame["line"]), 0);
f["threadName"] = th.name;
return f;
});
debug._frames = frames;
response.body = {
stackFrames: frames
};
debug.sendResponse(response);
});
}
// TODO Write a function to take a complex variable and convert it to a nested structure (possibly with sub variable references)
protected scopesRequest(response: DebugProtocol.ScopesResponse, args: DebugProtocol.ScopesArguments): void {
console.log("SCOPES REQUEST");
const frameReference = args.frameId;
const frame = this._frames[frameReference];
const threadName = frame["threadName"];
const debug = this;
// get the variables for the given stack frame
this._connection.send({op: 'list-vars', 'thread-name': threadName, 'frame-index': frame.id}, (err: any, result: any) => {
console.log("GOT VARIABLES");
console.log(result);
var variables = result[0]["vars"];
var frameArgs = variables[0];
var frameLocals = variables[1];
var argScope = frameArgs.map((v: any) : any => {
let val = {name: v["name"], value: v["value"], variablesReference: 0};
return val;
});
var localScope = frameLocals.map((v: any) : any => {
let val = {name: v["name"], value: v["value"], variablesReference: 0};
return val;
});
const scopes = new Array<Scope>();
scopes.push(new Scope("Local", this._variableHandles.create(localScope), false));
scopes.push(new Scope("Argument", this._variableHandles.create(argScope), false));
// scopes.push(new Scope("Global", this._variableHandles.create("global_" + frameReference), true));
response.body = {
scopes: scopes
};
debug.sendResponse(response);
});
}
protected variablesRequest(response: DebugProtocol.VariablesResponse, args: DebugProtocol.VariablesArguments): void {
console.log("VARIABLES REQUEST");
var variables = [];
const id = this._variableHandles.get(args.variablesReference);
if (id != null) {
variables = id;
}
response.body = {
variables: variables
};
this.sendResponse(response);
}
protected continueRequest(response: DebugProtocol.ContinueResponse, args: DebugProtocol.ContinueArguments): void {
console.log("CONTINUE REQUEST");
const debug = this;
this._connection.send({op: 'continue'}, (err: any, result: any) => {
// TODO handle errors here
debug.sendResponse(response);
console.log(result);
});
// const lines = this._breakPoints[this._sourceFile];
// for (let ln = this._currentLine+1; ln < this._sourceLines.length; ln++) {
// // is breakpoint on this line?
// if (lines && lines.indexOf(ln) >= 0) {
// this._currentLine = ln;
// this.sendResponse(response);
// this.sendEvent(new StoppedEvent("breakpoint", ClojureDebugSession.THREAD_ID));
// return;
// }
// // if word 'exception' found in source -> throw exception
// if (this._sourceLines[ln].indexOf("exception") >= 0) {
// this._currentLine = ln;
// this.sendResponse(response);
// this.sendEvent(new StoppedEvent("exception", ClojureDebugSession.THREAD_ID));
// this.sendEvent(new OutputEvent(`exception in line: ${ln}\n`, 'stderr'));
// return;
// }
// }
// this.sendResponse(response);
// // no more lines: run to end
// this.sendEvent(new TerminatedEvent());
}
protected nextRequest(response: DebugProtocol.NextResponse, args: DebugProtocol.NextArguments): void {
console.log("CONTINUE REQUEST");
for (let ln = this._currentLine+1; ln < this._sourceLines.length; ln++) {
if (this._sourceLines[ln].trim().length > 0) { // find next non-empty line
this._currentLine = ln;
this.sendResponse(response);
this.sendEvent(new StoppedEvent("step", ClojureDebugSession.THREAD_ID));
return;
}
}
this.sendResponse(response);
// no more lines: run to end
this.sendEvent(new TerminatedEvent());
}
protected handleResult(response: DebugProtocol.EvaluateResponse, replResult: any): void {
// forward stdout form the REPL to the debugger
var out = replResult["out"];
if (out && out != ""){
this.pout(out);
}
// forwared stderr from the REPL to the debugger
var err = replResult["err"];
if (err && err != ""){
this.perr(err);
}
var session = replResult["session"];
var result = this._evalResults[session] || {};
if (replResult["status"] && replResult["status"][0] == "done") {
response.body = {
result: result["value"],
// TODO implement this for complex results
variablesReference: 0
}
var err = result["error"];
if (err && err != "") {
response.success = false;
response.message = err;
}
this.sendResponse(response);
this._evalResults[session] = null;
} else {
if (replResult["value"]) {
var value = result["value"] || "";
result["value"] = value + replResult["value"];
}
var ex = replResult["ex"];
if (ex && ex != "") {
var err = result["error"] || "";
result["error"] = err + "\n" + ex;
}
this._evalResults[session] = result;
}
}
protected evaluateRequest(response: DebugProtocol.EvaluateResponse, args: DebugProtocol.EvaluateArguments): void {
var expr = args.expression;
var self = this;
this._connection.eval(expr, (err: any, result: any) => {
for (var res of result){
this.handleResult(response, res);
}
});
}
}
DebugSession.run(ClojureDebugSession);