2012-02-07 09:22:30 -08:00
|
|
|
/* -*- Mode: javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ft=javascript ts=2 et sw=2 tw=80: */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2012-02-07 09:22:30 -08:00
|
|
|
|
|
|
|
"use strict";
|
2012-11-02 18:03:13 -07:00
|
|
|
Components.utils.import("resource://gre/modules/NetUtil.jsm");
|
2012-02-07 09:22:30 -08:00
|
|
|
|
2013-10-06 11:45:07 -07:00
|
|
|
let wantLogging = Services.prefs.getBoolPref("devtools.debugger.log");
|
|
|
|
|
2012-02-07 09:22:30 -08:00
|
|
|
/**
|
|
|
|
* An adapter that handles data transfers between the debugger client and
|
|
|
|
* server. It can work with both nsIPipe and nsIServerSocket transports so
|
2012-04-05 12:37:21 -07:00
|
|
|
* long as the properly created input and output streams are specified.
|
2013-07-12 11:08:28 -07:00
|
|
|
* (However, for intra-process connections, LocalDebuggerTransport, below,
|
|
|
|
* is more efficient than using an nsIPipe pair with DebuggerTransport.)
|
2012-02-07 09:22:30 -08:00
|
|
|
*
|
|
|
|
* @param aInput nsIInputStream
|
|
|
|
* The input stream.
|
2012-11-13 13:28:22 -08:00
|
|
|
* @param aOutput nsIAsyncOutputStream
|
2012-02-07 09:22:30 -08:00
|
|
|
* The output stream.
|
2012-04-05 12:37:21 -07:00
|
|
|
*
|
|
|
|
* Given a DebuggerTransport instance dt:
|
|
|
|
* 1) Set dt.hooks to a packet handler object (described below).
|
|
|
|
* 2) Call dt.ready() to begin watching for input packets.
|
2013-07-12 11:08:28 -07:00
|
|
|
* 3) Call dt.send() to send packets as you please, and handle incoming
|
|
|
|
* packets passed to hook.onPacket.
|
2012-04-05 12:37:21 -07:00
|
|
|
* 4) Call dt.close() to close the connection, and disengage from the event
|
|
|
|
* loop.
|
|
|
|
*
|
2013-07-12 11:08:28 -07:00
|
|
|
* A packet handler is an object with two methods:
|
2012-04-05 12:37:21 -07:00
|
|
|
*
|
|
|
|
* - onPacket(packet) - called when we have received a complete packet.
|
|
|
|
* |Packet| is the parsed form of the packet --- a JavaScript value, not
|
|
|
|
* a JSON-syntax string.
|
|
|
|
*
|
|
|
|
* - onClosed(status) - called when the connection is closed. |Status| is
|
|
|
|
* an nsresult, of the sort passed to nsIRequestObserver.
|
2013-07-12 11:08:28 -07:00
|
|
|
*
|
2012-04-05 12:37:21 -07:00
|
|
|
* Data is transferred as a JSON packet serialized into a string, with the
|
|
|
|
* string length prepended to the packet, followed by a colon
|
|
|
|
* ([length]:[packet]). The contents of the JSON packet are specified in
|
|
|
|
* the Remote Debugging Protocol specification.
|
2012-02-07 09:22:30 -08:00
|
|
|
*/
|
2012-10-31 09:13:28 -07:00
|
|
|
this.DebuggerTransport = function DebuggerTransport(aInput, aOutput)
|
2012-02-07 09:22:30 -08:00
|
|
|
{
|
|
|
|
this._input = aInput;
|
|
|
|
this._output = aOutput;
|
2012-02-14 11:45:35 -08:00
|
|
|
|
|
|
|
this._converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"]
|
|
|
|
.createInstance(Ci.nsIScriptableUnicodeConverter);
|
|
|
|
this._converter.charset = "UTF-8";
|
|
|
|
|
2012-02-07 09:22:30 -08:00
|
|
|
this._outgoing = "";
|
|
|
|
this._incoming = "";
|
2012-04-05 12:37:21 -07:00
|
|
|
|
|
|
|
this.hooks = null;
|
2012-02-07 09:22:30 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
DebuggerTransport.prototype = {
|
|
|
|
/**
|
2012-04-05 12:37:21 -07:00
|
|
|
* Transmit a packet.
|
2013-10-01 14:25:19 -07:00
|
|
|
*
|
2012-04-05 12:37:21 -07:00
|
|
|
* This method returns immediately, without waiting for the entire
|
|
|
|
* packet to be transmitted, registering event handlers as needed to
|
|
|
|
* transmit the entire packet. Packets are transmitted in the order
|
|
|
|
* they are passed to this method.
|
2012-02-07 09:22:30 -08:00
|
|
|
*/
|
|
|
|
send: function DT_send(aPacket) {
|
2013-10-22 18:41:57 -07:00
|
|
|
let data = JSON.stringify(aPacket);
|
2012-02-14 11:45:35 -08:00
|
|
|
data = this._converter.ConvertFromUnicode(data);
|
2012-02-07 09:22:30 -08:00
|
|
|
data = data.length + ':' + data;
|
|
|
|
this._outgoing += data;
|
|
|
|
this._flushOutgoing();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Close the transport.
|
|
|
|
*/
|
|
|
|
close: function DT_close() {
|
|
|
|
this._input.close();
|
|
|
|
this._output.close();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Flush the outgoing stream.
|
|
|
|
*/
|
|
|
|
_flushOutgoing: function DT_flushOutgoing() {
|
|
|
|
if (this._outgoing.length > 0) {
|
|
|
|
var threadManager = Cc["@mozilla.org/thread-manager;1"].getService();
|
|
|
|
this._output.asyncWait(this, 0, 0, threadManager.currentThread);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-04-30 15:00:29 -07:00
|
|
|
onOutputStreamReady:
|
|
|
|
makeInfallible(function DT_onOutputStreamReady(aStream) {
|
2013-06-19 17:57:35 -07:00
|
|
|
let written = 0;
|
|
|
|
try {
|
|
|
|
written = aStream.write(this._outgoing, this._outgoing.length);
|
|
|
|
} catch(e if e.result == Components.results.NS_BASE_STREAM_CLOSED) {
|
|
|
|
dumpn("Connection closed.");
|
|
|
|
this.close();
|
|
|
|
return;
|
|
|
|
}
|
2012-02-07 09:22:30 -08:00
|
|
|
this._outgoing = this._outgoing.slice(written);
|
|
|
|
this._flushOutgoing();
|
2013-04-30 15:00:29 -07:00
|
|
|
}, "DebuggerTransport.prototype.onOutputStreamReady"),
|
2012-02-07 09:22:30 -08:00
|
|
|
|
|
|
|
/**
|
2012-04-05 12:37:21 -07:00
|
|
|
* Initialize the input stream for reading. Once this method has been
|
|
|
|
* called, we watch for packets on the input stream, and pass them to
|
|
|
|
* this.hook.onPacket.
|
2012-02-07 09:22:30 -08:00
|
|
|
*/
|
|
|
|
ready: function DT_ready() {
|
|
|
|
let pump = Cc["@mozilla.org/network/input-stream-pump;1"]
|
|
|
|
.createInstance(Ci.nsIInputStreamPump);
|
|
|
|
pump.init(this._input, -1, -1, 0, 0, false);
|
|
|
|
pump.asyncRead(this, null);
|
|
|
|
},
|
|
|
|
|
|
|
|
// nsIStreamListener
|
2013-04-30 15:00:29 -07:00
|
|
|
onStartRequest:
|
|
|
|
makeInfallible(function DT_onStartRequest(aRequest, aContext) {},
|
|
|
|
"DebuggerTransport.prototype.onStartRequest"),
|
2012-02-07 09:22:30 -08:00
|
|
|
|
2013-04-30 15:00:29 -07:00
|
|
|
onStopRequest:
|
|
|
|
makeInfallible(function DT_onStopRequest(aRequest, aContext, aStatus) {
|
2013-08-07 14:54:31 -07:00
|
|
|
this.close();
|
|
|
|
if (this.hooks) {
|
|
|
|
this.hooks.onClosed(aStatus);
|
|
|
|
this.hooks = null;
|
|
|
|
}
|
2013-04-30 15:00:29 -07:00
|
|
|
}, "DebuggerTransport.prototype.onStopRequest"),
|
2012-02-07 09:22:30 -08:00
|
|
|
|
2013-04-30 15:00:29 -07:00
|
|
|
onDataAvailable:
|
|
|
|
makeInfallible(function DT_onDataAvailable(aRequest, aContext,
|
|
|
|
aStream, aOffset, aCount) {
|
|
|
|
this._incoming += NetUtil.readInputStreamToString(aStream,
|
|
|
|
aStream.available());
|
|
|
|
while (this._processIncoming()) {};
|
|
|
|
}, "DebuggerTransport.prototype.onDataAvailable"),
|
2012-02-07 09:22:30 -08:00
|
|
|
|
|
|
|
/**
|
2012-01-23 00:29:15 -08:00
|
|
|
* Process incoming packets. Returns true if a packet has been received, either
|
2012-02-07 09:22:30 -08:00
|
|
|
* if it was properly parsed or not. Returns false if the incoming stream does
|
|
|
|
* not contain a full packet yet. After a proper packet is parsed, the dispatch
|
|
|
|
* handler DebuggerTransport.hooks.onPacket is called with the packet as a
|
|
|
|
* parameter.
|
|
|
|
*/
|
2012-01-23 00:29:15 -08:00
|
|
|
_processIncoming: function DT__processIncoming() {
|
2012-02-07 09:22:30 -08:00
|
|
|
// Well this is ugly.
|
|
|
|
let sep = this._incoming.indexOf(':');
|
|
|
|
if (sep < 0) {
|
2013-08-16 05:48:22 -07:00
|
|
|
// Incoming packet length is too big anyway - drop the connection.
|
|
|
|
if (this._incoming.length > 20) {
|
|
|
|
this.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let count = this._incoming.substring(0, sep);
|
|
|
|
// Check for a positive number with no garbage afterwards.
|
|
|
|
if (!/^[0-9]+$/.exec(count)) {
|
|
|
|
this.close();
|
2012-02-07 09:22:30 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-16 05:48:22 -07:00
|
|
|
count = +count;
|
2012-02-07 09:22:30 -08:00
|
|
|
if (this._incoming.length - (sep + 1) < count) {
|
|
|
|
// Don't have a complete request yet.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have a complete request, pluck it out of the data and parse it.
|
|
|
|
this._incoming = this._incoming.substring(sep + 1);
|
|
|
|
let packet = this._incoming.substring(0, count);
|
|
|
|
this._incoming = this._incoming.substring(count);
|
|
|
|
|
|
|
|
try {
|
2012-02-14 11:45:35 -08:00
|
|
|
packet = this._converter.ConvertToUnicode(packet);
|
2012-02-07 09:22:30 -08:00
|
|
|
var parsed = JSON.parse(packet);
|
|
|
|
} catch(e) {
|
2013-01-15 06:02:25 -08:00
|
|
|
let msg = "Error parsing incoming packet: " + packet + " (" + e + " - " + e.stack + ")";
|
|
|
|
if (Cu.reportError) {
|
|
|
|
Cu.reportError(msg);
|
|
|
|
}
|
|
|
|
dump(msg + "\n");
|
2012-02-07 09:22:30 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-10-22 18:41:57 -07:00
|
|
|
if (wantLogging) {
|
|
|
|
dumpn("Got: " + JSON.stringify(parsed, null, 2));
|
|
|
|
}
|
2013-04-30 15:00:29 -07:00
|
|
|
let self = this;
|
|
|
|
Services.tm.currentThread.dispatch(makeInfallible(function() {
|
2013-08-28 07:19:34 -07:00
|
|
|
// Ensure the hooks are still around by the time this runs (they will go
|
|
|
|
// away when the transport is closed).
|
|
|
|
if (self.hooks) {
|
|
|
|
self.hooks.onPacket(parsed);
|
|
|
|
}
|
2013-04-30 15:00:29 -07:00
|
|
|
}, "DebuggerTransport instance's this.hooks.onPacket"), 0);
|
2012-02-07 09:22:30 -08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2012-09-22 08:44:00 -07:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An adapter that handles data transfers between the debugger client and
|
|
|
|
* server when they both run in the same process. It presents the same API as
|
|
|
|
* DebuggerTransport, but instead of transmitting serialized messages across a
|
|
|
|
* connection it merely calls the packet dispatcher of the other side.
|
|
|
|
*
|
|
|
|
* @param aOther LocalDebuggerTransport
|
|
|
|
* The other endpoint for this debugger connection.
|
|
|
|
*
|
|
|
|
* @see DebuggerTransport
|
|
|
|
*/
|
2013-04-09 03:47:00 -07:00
|
|
|
this.LocalDebuggerTransport = function LocalDebuggerTransport(aOther)
|
2012-09-22 08:44:00 -07:00
|
|
|
{
|
|
|
|
this.other = aOther;
|
|
|
|
this.hooks = null;
|
2013-04-29 16:16:14 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* A packet number, shared between this and this.other. This isn't used
|
|
|
|
* by the protocol at all, but it makes the packet traces a lot easier to
|
|
|
|
* follow.
|
|
|
|
*/
|
|
|
|
this._serial = this.other ? this.other._serial : { count: 0 };
|
2012-09-22 08:44:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
LocalDebuggerTransport.prototype = {
|
|
|
|
/**
|
|
|
|
* Transmit a message by directly calling the onPacket handler of the other
|
|
|
|
* endpoint.
|
|
|
|
*/
|
|
|
|
send: function LDT_send(aPacket) {
|
2013-04-30 15:00:29 -07:00
|
|
|
let serial = this._serial.count++;
|
|
|
|
if (wantLogging) {
|
2013-07-24 08:27:26 -07:00
|
|
|
/* Check 'from' first, as 'echo' packets have both. */
|
|
|
|
if (aPacket.from) {
|
2013-04-30 15:00:29 -07:00
|
|
|
dumpn("Packet " + serial + " sent from " + uneval(aPacket.from));
|
2013-07-24 08:27:26 -07:00
|
|
|
} else if (aPacket.to) {
|
|
|
|
dumpn("Packet " + serial + " sent to " + uneval(aPacket.to));
|
2013-01-15 06:02:25 -08:00
|
|
|
}
|
2012-09-22 08:44:00 -07:00
|
|
|
}
|
2013-04-30 15:00:29 -07:00
|
|
|
this._deepFreeze(aPacket);
|
|
|
|
let other = this.other;
|
2013-06-10 21:16:47 -07:00
|
|
|
if (other) {
|
|
|
|
Services.tm.currentThread.dispatch(makeInfallible(function() {
|
|
|
|
// Avoid the cost of JSON.stringify() when logging is disabled.
|
|
|
|
if (wantLogging) {
|
|
|
|
dumpn("Received packet " + serial + ": " + JSON.stringify(aPacket, null, 2));
|
|
|
|
}
|
|
|
|
if (other.hooks) {
|
|
|
|
other.hooks.onPacket(aPacket);
|
|
|
|
}
|
|
|
|
}, "LocalDebuggerTransport instance's this.other.hooks.onPacket"), 0);
|
|
|
|
}
|
2012-09-22 08:44:00 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Close the transport.
|
|
|
|
*/
|
|
|
|
close: function LDT_close() {
|
|
|
|
if (this.other) {
|
|
|
|
// Remove the reference to the other endpoint before calling close(), to
|
|
|
|
// avoid infinite recursion.
|
|
|
|
let other = this.other;
|
2013-10-27 11:03:20 -07:00
|
|
|
this.other = null;
|
2012-09-22 08:44:00 -07:00
|
|
|
other.close();
|
|
|
|
}
|
2013-05-30 09:42:00 -07:00
|
|
|
if (this.hooks) {
|
2013-08-12 12:50:09 -07:00
|
|
|
try {
|
|
|
|
this.hooks.onClosed();
|
|
|
|
} catch(ex) {
|
|
|
|
Components.utils.reportError(ex);
|
|
|
|
}
|
2013-05-30 09:42:00 -07:00
|
|
|
this.hooks = null;
|
|
|
|
}
|
2012-09-22 08:44:00 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An empty method for emulating the DebuggerTransport API.
|
|
|
|
*/
|
|
|
|
ready: function LDT_ready() {},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function that makes an object fully immutable.
|
|
|
|
*/
|
|
|
|
_deepFreeze: function LDT_deepFreeze(aObject) {
|
|
|
|
Object.freeze(aObject);
|
|
|
|
for (let prop in aObject) {
|
|
|
|
// Freeze the properties that are objects, not on the prototype, and not
|
|
|
|
// already frozen. Note that this might leave an unfrozen reference
|
|
|
|
// somewhere in the object if there is an already frozen object containing
|
|
|
|
// an unfrozen object.
|
|
|
|
if (aObject.hasOwnProperty(prop) && typeof aObject === "object" &&
|
|
|
|
!Object.isFrozen(aObject)) {
|
|
|
|
this._deepFreeze(o[prop]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-04-30 15:00:29 -07:00
|
|
|
};
|
2013-07-12 11:08:28 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A transport for the debugging protocol that uses nsIMessageSenders to
|
|
|
|
* exchange packets with servers running in child processes.
|
|
|
|
*
|
|
|
|
* In the parent process, |aSender| should be the nsIMessageSender for the
|
|
|
|
* child process. In a child process, |aSender| should be the child process
|
|
|
|
* message manager, which sends packets to the parent.
|
|
|
|
*
|
|
|
|
* aPrefix is a string included in the message names, to distinguish
|
|
|
|
* multiple servers running in the same child process.
|
|
|
|
*
|
|
|
|
* This transport exchanges messages named 'debug:<prefix>:packet', where
|
|
|
|
* <prefix> is |aPrefix|, whose data is the protocol packet.
|
|
|
|
*/
|
|
|
|
function ChildDebuggerTransport(aSender, aPrefix) {
|
|
|
|
this._sender = aSender.QueryInterface(Components.interfaces.nsIMessageSender);
|
|
|
|
this._messageName = "debug:" + aPrefix + ":packet";
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* To avoid confusion, we use 'message' to mean something that
|
|
|
|
* nsIMessageSender conveys, and 'packet' to mean a remote debugging
|
|
|
|
* protocol packet.
|
|
|
|
*/
|
|
|
|
ChildDebuggerTransport.prototype = {
|
|
|
|
constructor: ChildDebuggerTransport,
|
|
|
|
|
|
|
|
hooks: null,
|
|
|
|
|
|
|
|
ready: function () {
|
|
|
|
this._sender.addMessageListener(this._messageName, this);
|
|
|
|
},
|
|
|
|
|
|
|
|
close: function () {
|
|
|
|
this._sender.removeMessageListener(this._messageName, this);
|
|
|
|
this.hooks.onClosed();
|
|
|
|
},
|
|
|
|
|
|
|
|
receiveMessage: function ({data}) {
|
|
|
|
this.hooks.onPacket(data);
|
|
|
|
},
|
|
|
|
|
|
|
|
send: function (packet) {
|
|
|
|
this._sender.sendAsyncMessage(this._messageName, packet);
|
|
|
|
}
|
|
|
|
};
|