Personal tools

Debugger Client API

From MozillaWiki

Jump to: navigation, search

Besides the "low-level", direct JS Debugger API, there is a debugger client module that allows applications to be written that control the debugger using the Remote Debugging Protocol.

Contents

Starting the Debugger

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:

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();
  });
}

If a TCP socket is required, the function should be split in two parts, a server-side and a client-side, like this:

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.
}

function startDebuggerClient()
{
  let transport = debuggerSocketConnect("localhost", 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();
  });
}

Shutting down the Debugger

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:

function shutdownDebugger()
{
  client.close();
}

Debugging a browser tab

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.

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.
      });
    });
  });
}

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:

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();
    });
  });
}

Handling pause events

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:

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();
}

Handling new stack frames

When a framesadded event is fired, the application can examine the list of stack frames in the server:

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);
  }
}

Handling new scripts

When a scriptsadded event is fired, the application can examine the list of available source scripts in the server:

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");
  }
}

Inspecting a stack frame

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:

/**
 * 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 && aFrame.arguments.length > 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 < aResponse.parameters.length; i++) {
        let name = aResponse.parameters[i];
        let value = aFrame.arguments[i];
        if (typeof value == "object" && 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 != "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");
    }
  });
}

Example application

The source code for a complete application program can be seen below:

/*
 * Debugger API demo.
 * Try it in Scratchpad with Environment -> 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 && aFrame.arguments.length > 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 < aResponse.parameters.length; i++) {
        let name = aResponse.parameters[i];
        let value = aFrame.arguments[i];
        if (typeof value == "object" && 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 != "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();

You can try out this program using the Scratchpad. Just remember to set Browser as your Environment.