Debugger Client API: Difference between revisions

From MozillaWiki
Jump to navigation Jump to search
(New socket API)
(Moved to the tree)
 
Line 1: Line 1:
Besides the "low-level", direct [https://developer.mozilla.org/en-US/docs/Tools/Debugger-API JS Debugger API], there is a debugger client module that allows applications to be written that control the debugger using the [https://wiki.mozilla.org/Remote_Debugging_Protocol Remote Debugging Protocol].
This doc was moved to http://searchfox.org/mozilla-central/source/devtools/docs/backend/client-api.md
 
== Starting the Debugger<br>  ==
 
In order to communicate with the Debugger, a client and a server instance must be created and a protocol connection must be established. The connection can be either over a TCP socket or an nsIPipe. The startDebugger function displayed below establishes an nsIPipe-backed connection:<br>
<pre>function startDebugger()
{
  // Start the server.
  if (!DebuggerServer.initialized) {
    DebuggerServer.init();
    DebuggerServer.addBrowserActors();
  }
  // Listen to an nsIPipe
  let transport = DebuggerServer.connectPipe();
 
  // Start the client.
  client = new DebuggerClient(transport);
  // Attach listeners for client events.
  client.addListener("tabNavigated", onTab);
  client.addListener("newScript", onScript);
  client.connect(function(aType, aTraits) {
    // Now the client is conected to the server.
    debugTab();
  });
}
</pre>
If a TCP socket is required, the function should be split in two parts, a server-side and a client-side, like this:<br>
<pre>function startDebuggerServer()
{
  // Start the server.
  if (!DebuggerServer.initialized) {
    DebuggerServer.init();
    DebuggerServer.addBrowserActors();
  }
  // For an nsIServerSocket we do this:
  DebuggerServer.openListener(2929); // A connection on port 2929.
}
 
let startDebuggerClient = Task.async(function*() {
  let transport = yield DebuggerClient.socketConnect({ host: "localhost", port: 2929 });
 
  // Start the client.
  client = new DebuggerClient(transport);
  // Attach listeners for client events.
  client.addListener("tabNavigated", onTab);
  client.addListener("newScript", onScript);
  client.connect(function(aType, aTraits) {
    // Now the client is conected to the server.
    debugTab();
  });
});
</pre>
 
== Shutting down the Debugger<br>  ==
 
When the application is finished, it has to notify the client to shut down the protocol connection. This makes sure that memory leaks are avoided and the server is terminated in an orderly fashion. Shutting down is as simple as it gets:
<pre>function shutdownDebugger()
{
  client.close();
}</pre>
== Debugging a browser tab<br>  ==
 
Debugging a browser tab requires enumerating the available tabs, attaching to the current one, and then attaching to its thread. Threads in the debugger client API represent contexts.<br>
<pre>function debugTab()
{
  // Get the list of tabs to find the one to attach to.
  client.listTabs(function(aResponse) {
    // Find the active tab.
    let tab = aResponse.tabs[aResponse.selected];
    // Attach to the tab.
    client.attachTab(tab.actor, function(aResponse, aTabClient) {
      if (!aTabClient) return;
      // Attach to the thread (context).
      client.attachThread(aResponse.threadActor, function(aResponse, aThreadClient) {
        if (!aThreadClient) return;
        threadClient = aThreadClient;
        // Attach listeners for thread events.
        threadClient.addListener("paused", onPause);
        threadClient.addListener("resumed", fooListener);
        threadClient.addListener("detached", fooListener);
        threadClient.addListener("framesadded", onFrames);
        threadClient.addListener("framescleared", fooListener);
        threadClient.addListener("scriptsadded", onScripts);
        threadClient.addListener("scriptscleared", fooListener);
        // Resume the thread.
        threadClient.resume();
        // Debugger is now ready and debuggee is running.
      });
    });
  });
}
</pre>
The debugger client will send event notifications for a number of events the application may be interested in. These events include state changes in the debugger, like pausing and resuming, stack frames or source scripts being ready for retrieval, etc.
 
== Handling location changes  ==
 
When teh user navigates away from a page, a tabNavigated event will be fired. The proper way to handle this event is to detach from the previous thread and tab and attach to the new ones:<br>
<pre>function onTab()
{
  // Detach from the previous thread.
  client.activeThread.detach(function() {
    // Detach from the previous tab.
    client.activeTab.detach(function() {
      // Start debugging the new tab.
      debugTab();
    });
  });
}</pre>
== Handling pause events<br>  ==
 
When the debugger enters a paused state, it's a good opportunity for the application to request a list of stack frames and source scripts:
<pre>function onPause()
{
  // Get the top 20 frames in the server's frame stack cache.
  client.activeThread.fillFrames(20);
  // Get the scripts loaded in the server's source script cache.
  client.activeThread.fillScripts();
}</pre>
== Handling new stack frames<br>  ==
 
When a framesadded event is fired, the application can examine the list of stack frames in the server:<br>
<pre>function onFrames()
{
  // Get the list of frames in the server.
  for each (let frame in client.activeThread.cachedFrames) {
    // frame is a Debugger.Frame grip.
    dump("frame: " + frame.toSource() + "\n");
    inspectFrame(frame);
  }
}</pre>
== Handling new scripts<br>  ==
 
When a scriptsadded event is fired, the application can examine the list of available source scripts in the server:<br>
<pre>function onScripts()
{
  // Get the list of scripts in the server.
  for each (let script in client.activeThread.cachedScripts) {
    // script is a Debugger.Script grip.
    dump("script: " + script.toSource() + "\n");
  }
}
 
</pre>
== Inspecting a stack frame<br>  ==
 
When enumerating the list of stack frames, the application can request more information about the frame, such as the function call parameter names and values, inspect the prototype chain, and more:<br>
<pre>/**
* Helper function to inspect the provided frame.
*/
function inspectFrame(aFrame)
{
  // Get the "this" object.
  if (aFrame["this"]) {
    getObjectProperties(aFrame["this"]);
  }
  // Add "arguments".
  if (aFrame.arguments &amp;&amp; aFrame.arguments.length &gt; 0) {
    // frame.arguments is a regular Array.
    dump("frame.arguments: " + aFrame.arguments.toSource() + "\n");
    // Add variables for every argument.
    let objClient = client.activeThread.pauseGrip(aFrame.callee);
    objClient.getSignature(function(aResponse) {
      for (let i = 0; i &lt; aResponse.parameters.length; i++) {
        let name = aResponse.parameters[i];
        let value = aFrame.arguments[i];
        if (typeof value == "object" &amp;&amp; value.type == "object") {
          getObjectProperties(value);
        }
      }
    });
  }
}
/**
* Helper function that retrieves the specified object's properties.
*/
function getObjectProperties(aObject)
{
  let thisClient = client.activeThread.pauseGrip(aObject);
  thisClient.getPrototypeAndProperties(function(aResponse) {
    // Get prototype as a protocol-specified grip.
    if (aResponse.prototype.type&nbsp;!= "null") {
      dump("__proto__: " + aResponse.prototype.toSource() + "\n");
    }
    // Get the rest of the object's own properties as protocol-specified grips.
    for each (let prop in Object.keys(aResponse.ownProperties)) {
      dump(prop + ": " + aResponse.ownProperties[prop].toSource() + "\n");
    }
  });
}
</pre>
 
= Example application<br>  =
 
The source code for a complete application program can be seen below:<br>
<pre>/*
* Debugger API demo.
* Try it in Scratchpad with Environment -&gt; Browser, using
* http://htmlpad.org/debugger/ as the current page.
*/
Components.utils.import("resource://gre/modules/devtools/dbg-server.jsm");
Components.utils.import("resource://gre/modules/devtools/dbg-client.jsm");
 
let client;
let threadClient;
 
function startDebugger()
{
  // Start the server.
  if (!DebuggerServer.initialized) {
    DebuggerServer.init();
    DebuggerServer.addBrowserActors();
  }
  // Listen to an nsIPipe
  let transport = DebuggerServer.connectPipe();
  // For an nsIServerSocket we do this:
  // DebuggerServer.openListener(aPort);
  // ...and this at the client:
  // let transport = debuggerSocketConnect(aHost, aPort);
 
  // Start the client.
  client = new DebuggerClient(transport);
  // Attach listeners for client events.
  client.addListener("tabNavigated", onTab);
  client.addListener("newScript", fooListener);
  client.connect(function(aType, aTraits) {
    // Now the client is conected to the server.
    debugTab();
  });
}
 
function shutdownDebugger()
{
  client.close();
}
 
/**
* Start debugging the current tab.
*/
function debugTab()
{
  // Get the list of tabs to find the one to attach to.
  client.listTabs(function(aResponse) {
    // Find the active tab.
    let tab = aResponse.tabs[aResponse.selected];
    // Attach to the tab.
    client.attachTab(tab.actor, function(aResponse, aTabClient) {
      if (!aTabClient) return;
      // Attach to the thread (context).
      client.attachThread(aResponse.threadActor, function(aResponse, aThreadClient) {
        if (!aThreadClient) return;
        threadClient = aThreadClient;
        // Attach listeners for thread events.
        threadClient.addListener("paused", onPause);
        threadClient.addListener("resumed", fooListener);
        threadClient.addListener("detached", fooListener);
        threadClient.addListener("framesadded", onFrames);
        threadClient.addListener("framescleared", fooListener);
        threadClient.addListener("scriptsadded", onScripts);
        threadClient.addListener("scriptscleared", fooListener);
        // Resume the thread.
        threadClient.resume();
        // Debugger is now ready and debuggee is running.
      });
    });
  });
}
 
/**
* Handler for location changes.
*/
function onTab()
{
  // Detach from the previous thread.
  client.activeThread.detach(function() {
    // Detach from the previous tab.
    client.activeTab.detach(function() {
      // Start debugging the new tab.
      debugTab();
    });
  });
}
 
/**
* Handler for entering pause state.
*/
function onPause()
{
  // Get the top 20 frames in the server's frame stack cache.
  client.activeThread.fillFrames(20);
  // Get the scripts loaded in the server's source script cache.
  client.activeThread.fillScripts();
}
 
/**
* Handler for framesadded events.
*/
function onFrames()
{
  // Get the list of frames in the server.
  for each (let frame in client.activeThread.cachedFrames) {
    // frame is a Debugger.Frame grip.
    dump("frame: " + frame.toSource() + "\n");
    inspectFrame(frame);
  }
}
 
/**
* Handler for scriptsadded events.
*/
function onScripts()
{
  // Get the list of scripts in the server.
  for each (let script in client.activeThread.cachedScripts) {
    // script is a Debugger.Script grip.
    dump("script: " + script.toSource() + "\n");
  }
 
  // Resume execution, since this is the last thing going on in the paused
  // state and there is no UI in this program. Wait a bit so that object
  // inspection has a chance to finish.
  setTimeout(function() {
    threadClient.resume();
  }, 1000);
}
 
/**
* Helper function to inspect the provided frame.
*/
function inspectFrame(aFrame)
{
  // Get the "this" object.
  if (aFrame["this"]) {
    getObjectProperties(aFrame["this"]);
  }
  // Add "arguments".
  if (aFrame.arguments &amp;&amp; aFrame.arguments.length &gt; 0) {
    // frame.arguments is a regular Array.
    dump("frame.arguments: " + aFrame.arguments.toSource() + "\n");
    // Add variables for every argument.
    let objClient = client.activeThread.pauseGrip(aFrame.callee);
    objClient.getSignature(function(aResponse) {
      for (let i = 0; i &lt; aResponse.parameters.length; i++) {
        let name = aResponse.parameters[i];
        let value = aFrame.arguments[i];
        if (typeof value == "object" &amp;&amp; value.type == "object") {
          getObjectProperties(value);
        }
      }
    });
  }
}
 
/**
* Helper function that retrieves the specified object's properties.
*/
function getObjectProperties(aObject)
{
  let thisClient = client.activeThread.pauseGrip(aObject);
  thisClient.getPrototypeAndProperties(function(aResponse) {
    // Get prototype as a protocol-specified grip.
    if (aResponse.prototype.type&nbsp;!= "null") {
      dump("__proto__: " + aResponse.prototype.toSource() + "\n");
    }
    // Get the rest of the object's own properties as protocol-specified grips.
    for each (let prop in Object.keys(aResponse.ownProperties)) {
      dump(prop + ": " + aResponse.ownProperties[prop].toSource() + "\n");
    }
  });
}
 
/**
* Generic event listener.
*/
function fooListener(aEvent)
{
  dump(aEvent + "\n");
}
 
// Run the program.
startDebugger();
// Execute the following line to stop the program.
//shutdownDebugger();
 
</pre>
You can try out this program using the [https://developer.mozilla.org/en/Tools/Scratchpad Scratchpad]. Just remember to set Browser as your Environment.<br>

Latest revision as of 16:34, 3 May 2017