diff --git a/lib/_debugger.js b/lib/_debugger.js index 0232f312baa..72af3a5cbc4 100644 --- a/lib/_debugger.js +++ b/lib/_debugger.js @@ -187,7 +187,30 @@ Client.prototype.reqBacktrace = function(cb) { }; -var helpMessage = "Commands: backtrace, version, eval, help, quit"; +// Returns an array of objects like this: +// +// { handle: 11, +// type: 'script', +// name: 'node.js', +// id: 14, +// lineOffset: 0, +// columnOffset: 0, +// lineCount: 562, +// sourceStart: '(function(process) {\n\n ', +// sourceLength: 15939, +// scriptType: 2, +// compilationType: 0, +// context: { ref: 10 }, +// text: 'node.js (lines: 562)' } +// +Client.prototype.reqScripts = function(cb) { + this.req({ command: 'scripts' } , function (res) { + if (cb) cb(res.body); + }); +}; + + +var helpMessage = "Commands: scripts, backtrace, version, eval, help, quit"; function startInterface() { @@ -226,6 +249,13 @@ function startInterface() { i.prompt(); }); + } else if (/^scripts/.test(cmd)) { + c.reqScripts(function (res) { + var text = res.map(function (x) { return x.text; }); + console.log(text.join('\n')); + i.prompt(); + }); + } else if (/^eval/.test(cmd)) { c.reqEval(cmd.slice(5), function (res) { console.log(res); diff --git a/test/simple/test-debugger-client.js b/test/simple/test-debugger-client.js index 5239430d40f..434a05ae66e 100644 --- a/test/simple/test-debugger-client.js +++ b/test/simple/test-debugger-client.js @@ -18,17 +18,59 @@ p.execute("Type: connect\r\n" + "Content-Length: 0\r\n\r\n"); assert.equal(1, resCount); +var expectedConnections = 0; +var tests = []; +function addTest (cb) { + expectedConnections++; + tests.push(cb); +} + +addTest(function (client, done) { + console.error("requesting version"); + client.reqVersion(function (v) { + console.log("version: %s", v); + assert.equal(process.versions.v8, v); + done(); + }); +}); + +addTest(function (client, done) { + console.error("requesting scripts"); + client.reqScripts(function (s) { + console.error("got %d scripts", s.length); + var foundMainScript = false; + for (var i = 0; i < s.length; i++) { + if (s[i].name === 'node.js') { + foundMainScript = true; + break; + } + } + assert.ok(foundMainScript); + done(); + }); +}); + +addTest(function (client, done) { + console.error("eval 2+2"); + client.reqEval("2+2", function (res) { + console.error(res); + assert.equal('4', res.text); + assert.equal(4, res.value); + done(); + }); +}); + var connectCount = 0; -function test(cb) { +function doTest(cb, done) { var nodeProcess = spawn(process.execPath, ['-e', 'setInterval(function () { console.log("blah"); }, 1000);']); nodeProcess.stdout.once('data', function () { - console.log("new node process: %d", nodeProcess.pid); + console.log(">>> new node process: %d", nodeProcess.pid); process.kill(nodeProcess.pid, "SIGUSR1"); - console.log("signaling it with SIGUSR1"); + console.log(">>> signaling it with SIGUSR1"); }); var didTryConnect = false; @@ -39,27 +81,34 @@ function test(cb) { // Wait for some data before trying to connect var c = new debug.Client(); - process.stdout.write("connecting..."); + process.stdout.write(">>> connecting..."); c.connect(debug.port, function () { connectCount++; console.log("connected!"); - cb(c, nodeProcess); + cb(c, function () { + console.error(">>> killing node process %d\n\n", nodeProcess.pid); + nodeProcess.kill(); + done(); + }); }); } }); } -test(function (client, nodeProcess) { - client.reqVersion(function (v) { - console.log("version: %s", v); - assert.equal(process.versions.v8, v); - nodeProcess.kill(); +function run () { + var t = tests[0]; + if (!t) return; + + doTest(t, function () { + tests.shift(); + run(); }); -}); +} +run(); process.on('exit', function() { - assert.equal(1, connectCount); + assert.equal(expectedConnections, connectCount); });