2010-11-07 03:36:45 +03:00
|
|
|
var Net = require('net'),
|
|
|
|
EventEmitter = require('events').EventEmitter,
|
2010-10-26 06:41:25 +04:00
|
|
|
Buffer = require('buffer').Buffer,
|
2014-03-14 11:32:36 +04:00
|
|
|
debugProtocol = require('debug')('node-inspector:protocol:v8-debug'),
|
2010-11-07 03:36:45 +03:00
|
|
|
callbackHandler = require('./callback').create();
|
2010-07-23 08:53:36 +04:00
|
|
|
|
|
|
|
function makeMessage() {
|
|
|
|
return {
|
|
|
|
headersDone: false,
|
|
|
|
headers: null,
|
|
|
|
contentLength: 0
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////
|
|
|
|
// exports
|
|
|
|
|
2010-12-08 09:41:06 +03:00
|
|
|
exports.attachDebugger = function(port) {
|
2010-07-31 05:26:14 +04:00
|
|
|
var connected = false,
|
2010-11-07 03:36:45 +03:00
|
|
|
debugBuffer = '',
|
2010-07-31 05:26:14 +04:00
|
|
|
msg = false,
|
2010-11-07 03:36:45 +03:00
|
|
|
conn = Net.createConnection(port),
|
2010-07-31 05:26:14 +04:00
|
|
|
debugr,
|
|
|
|
offset,
|
2013-05-28 15:35:38 +04:00
|
|
|
contentLengthMatch,
|
|
|
|
lastError;
|
2010-09-14 06:57:52 +04:00
|
|
|
conn.setEncoding('utf8');
|
2010-07-31 05:26:14 +04:00
|
|
|
|
2010-07-23 08:53:36 +04:00
|
|
|
function parse() {
|
2010-11-07 03:36:45 +03:00
|
|
|
var b, obj;
|
2010-07-23 08:53:36 +04:00
|
|
|
if (msg && msg.headersDone) {
|
|
|
|
//parse body
|
2010-11-07 03:36:45 +03:00
|
|
|
if (Buffer.byteLength(debugBuffer) >= msg.contentLength) {
|
|
|
|
b = new Buffer(debugBuffer);
|
2010-09-14 06:57:52 +04:00
|
|
|
msg.body = b.toString('utf8', 0, msg.contentLength);
|
2010-11-07 03:36:45 +03:00
|
|
|
debugBuffer = b.toString('utf8', msg.contentLength, b.length);
|
2010-07-23 08:53:36 +04:00
|
|
|
if (msg.body.length > 0) {
|
2010-11-07 03:36:45 +03:00
|
|
|
obj = JSON.parse(msg.body);
|
2013-05-20 10:56:51 +04:00
|
|
|
if (typeof obj.running === 'boolean') {
|
|
|
|
debugr.isRunning = obj.running;
|
|
|
|
}
|
2010-10-26 06:41:25 +04:00
|
|
|
if (obj.type === 'response' && obj.request_seq > 0) {
|
2013-11-13 12:06:01 +04:00
|
|
|
debugProtocol('response: ' + msg.body);
|
2010-11-07 03:36:45 +03:00
|
|
|
callbackHandler.processResponse(obj.request_seq, [obj]);
|
2010-10-26 06:41:25 +04:00
|
|
|
}
|
2010-10-28 09:17:06 +04:00
|
|
|
else if (obj.type === 'event') {
|
2013-11-13 12:06:01 +04:00
|
|
|
debugProtocol('event: ' + msg.body);
|
2014-03-13 08:51:22 +04:00
|
|
|
if (['break', 'exception'].indexOf(obj.event) > -1) {
|
|
|
|
debugr.isRunning = false;
|
|
|
|
}
|
2010-10-28 09:17:06 +04:00
|
|
|
debugr.emit(obj.event, obj);
|
2010-10-26 06:41:25 +04:00
|
|
|
}
|
2013-11-13 12:06:01 +04:00
|
|
|
else {
|
|
|
|
debugProtocol('unknown: ' + msg.body);
|
|
|
|
}
|
2010-07-23 08:53:36 +04:00
|
|
|
}
|
|
|
|
msg = false;
|
|
|
|
parse();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2010-07-31 05:26:14 +04:00
|
|
|
if (!msg) {
|
|
|
|
msg = makeMessage();
|
|
|
|
}
|
2010-07-23 08:53:36 +04:00
|
|
|
|
2010-11-07 03:36:45 +03:00
|
|
|
offset = debugBuffer.indexOf('\r\n\r\n');
|
2010-07-23 08:53:36 +04:00
|
|
|
if (offset > 0) {
|
|
|
|
msg.headersDone = true;
|
2010-11-07 03:36:45 +03:00
|
|
|
msg.headers = debugBuffer.substr(0, offset + 4);
|
|
|
|
contentLengthMatch = /Content-Length: (\d+)/.exec(msg.headers);
|
|
|
|
if (contentLengthMatch[1]) {
|
|
|
|
msg.contentLength = parseInt(contentLengthMatch[1], 10);
|
2010-07-23 08:53:36 +04:00
|
|
|
}
|
|
|
|
else {
|
2010-09-11 03:59:18 +04:00
|
|
|
console.warn('no Content-Length');
|
2010-07-23 08:53:36 +04:00
|
|
|
}
|
2010-11-07 03:36:45 +03:00
|
|
|
debugBuffer = debugBuffer.slice(offset + 4);
|
2010-07-23 08:53:36 +04:00
|
|
|
parse();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-07 03:36:45 +03:00
|
|
|
debugr = Object.create(EventEmitter.prototype, {
|
2013-05-20 10:56:51 +04:00
|
|
|
isRunning: { writable: true, value: true },
|
|
|
|
|
2010-10-26 06:41:25 +04:00
|
|
|
send: {
|
2010-12-08 09:41:06 +03:00
|
|
|
value: function(data)
|
2010-07-23 08:53:36 +04:00
|
|
|
{
|
2013-11-13 12:06:01 +04:00
|
|
|
debugProtocol('request: ' + data);
|
2010-07-23 08:53:36 +04:00
|
|
|
if (connected) {
|
2010-11-07 03:36:45 +03:00
|
|
|
conn.write('Content-Length: ' + data.length + '\r\n\r\n' + data);
|
2010-07-23 08:53:36 +04:00
|
|
|
}
|
2010-07-31 05:26:14 +04:00
|
|
|
}
|
|
|
|
},
|
2013-05-22 13:08:27 +04:00
|
|
|
|
2013-05-29 13:50:54 +04:00
|
|
|
request: {
|
2010-12-08 09:41:06 +03:00
|
|
|
value: function(command, params, callback) {
|
2010-10-26 06:41:25 +04:00
|
|
|
var msg = {
|
2014-03-14 11:32:36 +04:00
|
|
|
seq: 0,
|
|
|
|
type: 'request',
|
|
|
|
command: command
|
|
|
|
};
|
2010-11-07 03:36:45 +03:00
|
|
|
if (typeof callback == 'function') {
|
|
|
|
msg.seq = callbackHandler.wrap(callback);
|
|
|
|
}
|
2010-10-26 06:41:25 +04:00
|
|
|
if (params) {
|
|
|
|
Object.keys(params).forEach(function(key) {
|
|
|
|
msg[key] = params[key];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
this.send(JSON.stringify(msg));
|
|
|
|
}
|
|
|
|
},
|
2013-05-14 22:32:10 +04:00
|
|
|
|
2010-07-23 08:53:36 +04:00
|
|
|
close: {
|
2010-12-08 09:41:06 +03:00
|
|
|
value: function()
|
2010-07-23 08:53:36 +04:00
|
|
|
{
|
|
|
|
conn.end();
|
2010-07-31 05:26:14 +04:00
|
|
|
}
|
2010-09-11 03:59:18 +04:00
|
|
|
},
|
|
|
|
connected: {
|
|
|
|
get: function() { return connected; }
|
2010-07-31 05:26:14 +04:00
|
|
|
}
|
|
|
|
});
|
2010-07-23 08:53:36 +04:00
|
|
|
|
2010-12-08 09:41:06 +03:00
|
|
|
conn.on('connect', function() {
|
2010-07-23 08:53:36 +04:00
|
|
|
connected = true;
|
|
|
|
debugr.emit('connect');
|
|
|
|
});
|
|
|
|
|
2010-12-08 09:41:06 +03:00
|
|
|
conn.on('data', function(data) {
|
2010-11-07 03:36:45 +03:00
|
|
|
debugBuffer += data;
|
2010-07-23 08:53:36 +04:00
|
|
|
parse();
|
|
|
|
});
|
2010-12-08 09:41:06 +03:00
|
|
|
|
2010-09-10 09:09:21 +04:00
|
|
|
conn.on('error', function(e) {
|
2013-05-28 15:35:38 +04:00
|
|
|
if (e.code == 'ECONNREFUSED') {
|
|
|
|
e.helpString = 'Is node running with --debug port ' + port + '?';
|
|
|
|
} else if (e.code == 'ECONNRESET') {
|
|
|
|
e.helpString = 'Check there is no other debugger client attached to port ' + port + '.';
|
|
|
|
}
|
|
|
|
|
|
|
|
lastError = e.toString();
|
|
|
|
if (e.helpString) {
|
|
|
|
lastError += '. ' + e.helpString;
|
|
|
|
}
|
|
|
|
|
2010-09-10 09:09:21 +04:00
|
|
|
debugr.emit('error', e);
|
|
|
|
});
|
2010-07-23 08:53:36 +04:00
|
|
|
|
2010-12-08 09:41:06 +03:00
|
|
|
conn.on('end', function() {
|
2010-07-23 08:53:36 +04:00
|
|
|
debugr.close();
|
|
|
|
});
|
2010-12-08 09:41:06 +03:00
|
|
|
|
|
|
|
conn.on('close', function() {
|
2010-07-23 08:53:36 +04:00
|
|
|
connected = false;
|
2013-05-28 15:35:38 +04:00
|
|
|
debugr.emit('close', lastError || 'Debugged process exited.');
|
2010-07-23 08:53:36 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
return debugr;
|
|
|
|
};
|
|
|
|
|