var net = require('net'); var readline = require('readline'); var inherits = require('util').inherits; var spawn = require('child_process').spawn; _AN_Write_port('port', exports, false , 5858); exports.start = function (){ if (_AN_Read_length('length', process.argv) < 3) { console.error('Usage: node debug script.js'); process.exit(1); } var interface = new Interface(); process.on('uncaughtException', function (e){ console.error("There was an internal error in Node's debugger. " + 'Please report this bug.'); console.error(e.message); console.error(e.stack); if (interface.child) interface.child.kill(); process.exit(1); } ); } ; var args = process.argv.slice(2); args.unshift('--debug-brk'); function Protocol(){ this._newRes(); } _AN_Write_protocol('Protocol', exports, false , Protocol); Protocol.prototype._newRes = function (raw){ this.res = { raw: raw || '', headers: { } } ; this.state = 'headers'; this.reqSeq = 1; this.execute(''); } ; Protocol.prototype.execute = function (d){ var res = this.res; res.raw += d; switch (this.state){ case 'headers': var endHeaderIndex = res.raw.indexOf('\r\n\r\n'); if (endHeaderIndex < 0) break ; var lines = res.raw.slice(0, endHeaderIndex).split('\r\n'); for (var i = 0; i < _AN_Read_length('length', lines); i++ ){ var kv = lines[i].split(/: +/); res.headers[kv[0]] = kv[1]; } this.contentLength = + res.headers["Content-Length"] ; this.bodyStartIndex = endHeaderIndex + 4; this.state = 'body'; if (_AN_Read_length('length', res.raw) - this.bodyStartIndex < this.contentLength) break ; case 'body': if (_AN_Read_length('length', res.raw) - this.bodyStartIndex >= this.contentLength) { res.body = res.raw.slice(this.bodyStartIndex, this.bodyStartIndex + this.contentLength); res.body = _AN_Read_length('length', res.body)? JSON.parse(res.body): { } ; this.onResponse(res); this._newRes(res.raw.slice(this.bodyStartIndex + this.contentLength)); } break ; default : { throw new Error('Unknown state') break ; } } } ; Protocol.prototype.serialize = function (req){ req.type = 'request'; req.seq = this.reqSeq++ ; var json = JSON.stringify(req); return 'Content-Length: ' + _AN_Read_length('length', json) + '\r\n\r\n' + json; } ; var NO_FRAME = -1; function Client(){ net.Stream.call(this); var protocol = _AN_Write_protocol('protocol', this, false , new Protocol(this)); this._reqCallbacks = [] ; var socket = this; this.currentFrame = NO_FRAME; this.currentSourceLine = -1; this.currentSource = null ; this.handles = { } ; this.scripts = { } ; socket.setEncoding('utf8'); socket.on('data', function (d){ protocol.execute(d); } ); protocol.onResponse = this._onResponse.bind(this); } inherits(Client, net.Stream); exports.Client = Client; Client.prototype._addHandle = function (desc){ if (typeof desc != 'object' || typeof desc.handle != 'number') { return ; } this.handles[desc.handle] = desc; if (desc.type == 'script') { this._addScript(desc); } } ; var natives = process.binding('natives'); Client.prototype._addScript = function (desc){ this.scripts[desc.id] = desc; if (desc.name) { desc.isNative = (_AN_Call_replace('replace', desc.name, '.js', '') in natives) || desc.name == 'node.js'; } } ; Client.prototype._removeScript = function (desc){ this.scripts[desc.id] = undefined; } ; Client.prototype._onResponse = function (res){ for (var i = 0; i < _AN_Read_length('length', this._reqCallbacks); i++ ){ var cb = this._reqCallbacks[i]; if (this._reqCallbacks[i].request_seq == res.body.request_seq) break ; } var self = this; var handled = false ; if (res.headers.Type == 'connect') { self.reqScripts(); self.emit('ready'); handled = true ; } else if (res.body && res.body.event == 'break') { this.emit('break', res.body); handled = true ; } else if (res.body && res.body.event == 'afterCompile') { this._addHandle(res.body.body.script); handled = true ; } else if (res.body && res.body.event == 'scriptCollected') { this._removeScript(res.body.body.script); handled = true ; } if (cb) { this._reqCallbacks.splice(i, 1); handled = true ; cb(res.body); } if (!handled) this.emit('unhandledResponse', res.body); } ; Client.prototype.req = function (req, cb){ _AN_Call_write('write', this, _AN_Read_protocol('protocol', this).serialize(req)); cb.request_seq = req.seq; this._reqCallbacks.push(cb); } ; Client.prototype.reqVersion = function (cb){ this.req({ command: 'version'} , function (res){ if (cb) cb(res.body.V8Version, res.running); } ); } ; Client.prototype.reqLookup = function (refs, cb){ var self = this; var req = { command: 'lookup', arguments: { handles: refs} } ; this.req(req, function (res){ if (res.success) { for (var ref in res.body){ if (typeof res.body[ref] == 'object') { self._addHandle(res.body[ref]); } } } if (cb) cb(res); } ); } ; Client.prototype.reqEval = function (expression, cb){ var self = this; if (this.currentFrame == NO_FRAME) { this.reqFrameEval(expression, NO_FRAME, cb); return ; } this.reqBacktrace(function (bt){ if (!bt.frames) { cb({ } ); return ; } var frame = bt.frames[self.currentFrame]; var evalFrames = frame.scopes.map(function (s){ return bt.frames[s.index].index; } ); self._reqFramesEval(expression, evalFrames, cb); } ); } ; Client.prototype._reqFramesEval = function (expression, evalFrames, cb){ if (_AN_Read_length('length', evalFrames) == 0) { this.reqFrameEval(expression, NO_FRAME, cb); return ; } var self = this; var i = evalFrames.shift(); this.reqFrameEval(expression, i, function (res){ if (res.success) { if (cb) cb(res); } else { self._reqFramesEval(expression, evalFrames, cb); } } ); } ; Client.prototype.reqFrameEval = function (expression, frame, cb){ var self = this; var req = { command: 'evaluate', arguments: { expression: expression} } ; if (frame == NO_FRAME) { req.arguments.global = true ; } else { req.arguments.frame = frame; } this.req(req, function (res){ if (res.success) { self._addHandle(res.body); } if (cb) cb(res); } ); } ; Client.prototype.reqBacktrace = function (cb){ this.req({ command: 'backtrace'} , function (res){ if (cb) cb(res.body); } ); } ; Client.prototype.reqScripts = function (cb){ var self = this; this.req({ command: 'scripts'} , function (res){ for (var i = 0; i < _AN_Read_length('length', res.body); i++ ){ self._addHandle(res.body[i]); } if (cb) cb(); } ); } ; Client.prototype.reqContinue = function (cb){ this.req({ command: 'continue'} , function (res){ if (cb) cb(res); } ); } ; Client.prototype.listbreakpoints = function (cb){ this.req({ command: 'listbreakpoints'} , function (res){ if (cb) cb(res); } ); } ; Client.prototype.reqSource = function (from, to, cb){ var req = { command: 'source', fromLine: from, toLine: to} ; this.req(req, function (res){ if (cb) cb(res.body); } ); } ; Client.prototype.step = function (action, count, cb){ var req = { command: 'continue', arguments: { stepaction: action, stepcount: count} } ; this.req(req, function (res){ if (cb) cb(res); } ); } ; Client.prototype.mirrorObject = function (handle, cb){ var self = this; if (handle.type == 'object') { var propertyRefs = handle.properties.map(function (p){ return p.ref; } ); this.reqLookup(propertyRefs, function (res){ if (!res.success) { console.error('problem with reqLookup'); if (cb) cb(handle); return ; } var mirror; if (handle.className == 'Array') { mirror = [] ; } else { mirror = { } ; } for (var i = 0; i < _AN_Read_length('length', handle.properties); i++ ){ var value = res.body[handle.properties[i].ref]; var mirrorValue; if (value) { mirrorValue = value.value? value.value: value.text; } else { mirrorValue = '[?]'; } if (Array.isArray(mirror) && typeof handle.properties[i].name != 'number') { continue ; } mirror[handle.properties[i].name] = mirrorValue; } if (cb) cb(mirror); } ); } else if (handle.value) { process.nextTick(function (){ cb(handle.value); } ); } else { process.nextTick(function (){ cb(handle); } ); } } ; Client.prototype.fullTrace = function (cb){ var self = this; this.reqBacktrace(function (trace){ var refs = [] ; for (var i = 0; i < _AN_Read_length('length', trace.frames); i++ ){ var frame = trace.frames[i]; refs.push(frame.script.ref); refs.push(frame.func.ref); refs.push(frame.receiver.ref); } self.reqLookup(refs, function (res){ for (var i = 0; i < _AN_Read_length('length', trace.frames); i++ ){ var frame = trace.frames[i]; frame.script = res.body[frame.script.ref]; frame.func = res.body[frame.func.ref]; frame.receiver = res.body[frame.receiver.ref]; } if (cb) cb(trace); } ); } ); } ; var commands = ['backtrace', 'continue', 'help', 'info breakpoints', 'kill', 'list', 'next', 'print', 'quit', 'run', 'scripts', 'step', 'version'] ; var helpMessage = 'Commands: ' + commands.join(', '); function SourceUnderline(sourceText, position){ if (!sourceText) return ; var underline = ''; for (var i = 0; i < position; i++ ){ if (sourceText[i] == '\t') { underline += '\t'; } else { underline += ' '; } } underline += '^'; return sourceText + '\n' + underline; } function SourceInfo(body){ var result = ''; if (body.script) { if (body.script.name) { result += body.script.name; } else { result += '[unnamed]'; } } result += ':'; result += body.sourceLine + 1; return result; } function Interface(){ var self = this; var child; var client; function complete(line){ return self.complete(line); } var term = readline.createInterface(process.stdin, process.stdout, complete); this.term = term; process.on('exit', function (){ self.killChild(); } ); this.stdin = process.openStdin(); term.setPrompt('debug> '); term.prompt(); this.quitting = false ; process.on('SIGINT', function (){ self.handleSIGINT(); } ); term.on('SIGINT', function (){ self.handleSIGINT(); } ); term.on('attemptClose', function (){ self.tryQuit(); } ); term.on('line', function (cmd){ cmd = _AN_Call_replace('replace', _AN_Call_replace('replace', cmd, /^\s*/, ''), /\s*$/, ''); if (cmd.length) { self._lastCommand = cmd; self.handleCommand(cmd); } else { self.handleCommand(self._lastCommand); } } ); } Interface.prototype.complete = function (line){ var matches = [] ; line = _AN_Call_replace('replace', line, /^\s*/, ''); for (var i = 0; i < _AN_Read_length('length', commands); i++ ){ if (commands[i].indexOf(line) >= 0) { matches.push(commands[i]); } } return [matches, line] ; } ; Interface.prototype.handleSIGINT = function (){ if (this.paused) { this.child.kill('SIGINT'); } else { this.tryQuit(); } } ; Interface.prototype.quit = function (){ if (this.quitting) return ; this.quitting = true ; this.killChild(); this.term.close(); process.exit(0); } ; Interface.prototype.tryQuit = function (){ var self = this; if (self.child) { self.quitQuestion(function (yes){ if (yes) { self.quit(); } else { self.term.prompt(); } } ); } else { self.quit(); } } ; Interface.prototype.pause = function (){ this.paused = true ; this.stdin.pause(); this.term.pause(); } ; Interface.prototype.resume = function (){ if (!this.paused) return false ; this.paused = false ; this.stdin.resume(); this.term.resume(); this.term.prompt(); return true ; } ; Interface.prototype.handleBreak = function (r){ var result = ''; if (r.breakpoints) { result += 'breakpoint'; if (_AN_Read_length('length', r.breakpoints) > 1) { result += 's'; } result += ' #'; for (var i = 0; i < _AN_Read_length('length', r.breakpoints); i++ ){ if (i > 0) { result += ', #'; } result += r.breakpoints[i]; } } else { result += 'break'; } result += ' in '; result += r.invocationText; result += ', '; result += SourceInfo(r); result += '\n'; result += SourceUnderline(r.sourceLineText, r.sourceColumn); this.client.currentSourceLine = r.sourceLine; this.client.currentFrame = 0; this.client.currentScript = r.script.name; console.log(result); if (!this.resume()) this.term.prompt(); } ; function intChars(n){ if (n < 50) { return 2; } else if (n < 950) { return 3; } else if (n < 9950) { return 4; } else { return 5; } } function leftPad(n){ var s = n.toString(); var nchars = intChars(n); var nspaces = nchars - _AN_Read_length('length', s); for (var i = 0; i < nspaces; i++ ){ s = ' ' + s; } return s; } Interface.prototype.handleCommand = function (cmd){ var self = this; var client = this.client; var term = this.term; if (cmd == 'quit' || cmd == 'q' || cmd == 'exit') { self._lastCommand = null ; self.tryQuit(); } else if (/^r(un)?/.test(cmd)) { self._lastCommand = null ; if (self.child) { self.restartQuestion(function (yes){ if (!yes) { self._lastCommand = null ; term.prompt(); } else { console.log('restarting...'); self.killChild(); _AN_Call_settimeout('setTimeout', window, function (){ self.trySpawn(); } , 1000); } } ); } else { self.trySpawn(); } } else if (/^help/.test(cmd)) { console.log(helpMessage); term.prompt(); } else if ('version' == cmd) { if (!client) { self.printNotConnected(); return ; } client.reqVersion(function (v){ console.log(v); term.prompt(); } ); } else if (/info +breakpoints/.test(cmd)) { if (!client) { self.printNotConnected(); return ; } client.listbreakpoints(function (res){ console.log(res); term.prompt(); } ); } else if ('l' == cmd || 'list' == cmd) { if (!client) { self.printNotConnected(); return ; } var from = client.currentSourceLine - 5; var to = client.currentSourceLine + 5; client.reqSource(from, to, function (res){ var lines = res.source.split('\n'); for (var i = 0; i < _AN_Read_length('length', lines); i++ ){ var lineno = res.fromLine + i + 1; if (lineno < from || lineno > to) continue ; if (lineno == 1) { var wrapper = require('module').wrapper[0]; lines[i] = lines[i].slice(_AN_Read_length('length', wrapper)); } if (lineno == 1 + client.currentSourceLine) { var nchars = intChars(lineno); var pointer = ''; for (var j = 0; j < nchars - 1; j++ ){ pointer += '='; } pointer += '>'; console.log(pointer + ' ' + lines[i]); } else { console.log(leftPad(lineno) + ' ' + lines[i]); } } term.prompt(); } ); } else if (/^backtrace/.test(cmd) || /^bt/.test(cmd)) { if (!client) { self.printNotConnected(); return ; } client.fullTrace(function (bt){ if (bt.totalFrames == 0) { console.log('(empty stack)'); } else { var text = ''; var firstFrameNative = bt.frames[0].script.isNative; for (var i = 0; i < _AN_Read_length('length', bt.frames); i++ ){ var frame = bt.frames[i]; if (!firstFrameNative && frame.script.isNative) break ; text += '#' + i + ' '; if (frame.func.inferredName && _AN_Read_length('length', frame.func.inferredName) > 0) { text += frame.func.inferredName + ' '; } text += require('path').basename(frame.script.name) + ':'; text += (frame.line + 1) + ':' + (frame.column + 1); text += '\n'; } console.log(text); } term.prompt(); } ); } else if (cmd == 'scripts' || cmd == 'scripts full') { if (!client) { self.printNotConnected(); return ; } self.printScripts(cmd.indexOf('full') > 0); term.prompt(); } else if (/^c(ontinue)?/.test(cmd)) { if (!client) { self.printNotConnected(); return ; } self.pause(); client.reqContinue(function (){ self.resume(); } ); } else if (/^k(ill)?/.test(cmd)) { if (!client) { self.printNotConnected(); return ; } if (self.child) { self.killQuestion(function (yes){ if (yes) { self.killChild(); } else { self._lastCommand = null ; } } ); } else { self.term.prompt(); } } else if (/^next/.test(cmd) || /^n/.test(cmd)) { if (!client) { self.printNotConnected(); return ; } client.step('next', 1, function (res){ } ); } else if (/^step/.test(cmd) || /^s/.test(cmd)) { if (!client) { self.printNotConnected(); return ; } client.step('in', 1, function (res){ } ); } else if (/^print/.test(cmd) || /^p/.test(cmd)) { if (!client) { self.printNotConnected(); return ; } var i = cmd.indexOf(' '); if (i < 0) { console.log('print [expression]'); term.prompt(); } else { cmd = cmd.slice(i); client.reqEval(cmd, function (res){ if (!res.success) { console.log(res.message); term.prompt(); return ; } client.mirrorObject(res.body, function (mirror){ console.log(mirror); term.prompt(); } ); } ); } } else { if (!/^\s*$/.test(cmd)) { console.log('Unknown command "%s". Try "help"', cmd); } term.prompt(); } } ; Interface.prototype.yesNoQuestion = function (prompt, cb){ var self = this; self.resume(); this.term.question(prompt, function (answer){ if (/^y(es)?$/i.test(answer)) { cb(true ); } else if (/^n(o)?$/i.test(answer)) { cb(false ); } else { console.log('Please answer y or n.'); self.restartQuestion(cb); } } ); } ; Interface.prototype.restartQuestion = function (cb){ this.yesNoQuestion('The program being debugged has been started already.\n' + 'Start it from the beginning? (y or n) ', cb); } ; Interface.prototype.killQuestion = function (cb){ this.yesNoQuestion('Kill the program being debugged? (y or n) ', cb); } ; Interface.prototype.quitQuestion = function (cb){ this.yesNoQuestion('A debugging session is active. Quit anyway? (y or n) ', cb); } ; Interface.prototype.killChild = function (){ if (this.child) { this.child.kill(); this.child = null ; } if (this.client) { this.client.destroy(); this.client = null ; } this.resume(); } ; Interface.prototype.trySpawn = function (cb){ var self = this; this.killChild(); this.child = spawn(process.execPath, args, { customFds: [0, 1, 2] } ); this.pause(); _AN_Call_settimeout('setTimeout', window, function (){ _AN_Call_write('write', process.stdout, 'connecting...'); var client = self.client = new Client(); client.connect(_AN_Read_port('port', exports)); client.once('ready', function (){ _AN_Call_write('write', process.stdout, 'ok\r\n'); client.reqContinue(function (){ if (cb) cb(); } ); } ); client.on('close', function (){ console.log('\nprogram terminated'); self.client = null ; self.killChild(); if (!self.quitting) self.term.prompt(); } ); client.on('unhandledResponse', function (res){ console.log('\r\nunhandled res:'); console.log(res); self.term.prompt(); } ); client.on('break', function (res){ self.handleBreak(res.body); } ); } , 100); } ; Interface.prototype.printNotConnected = function (){ console.log("Program not running. Try 'run'."); this.term.prompt(); } ; Interface.prototype.printScripts = function (displayNatives){ var client = this.client; var text = ''; for (var id in client.scripts){ var script = client.scripts[id]; if (typeof script == 'object' && script.name) { if (displayNatives || script.name == client.currentScript || !script.isNative) { text += script.name == client.currentScript? '* ': ' '; text += require('path').basename(script.name) + '\n'; } } } _AN_Call_write('write', process.stdout, text); } ;