Remote debugging protocol

Under the hood, Chrome Developer Tools is a web application written in HTML, JavaScript and CSS. It has a special binding available at JavaScript runtime that allows interacting with chrome pages and instrumenting them. Interaction protocol consists of commands that are sent to the page and events that the page is generating. Although Chrome Developer Tools is the primary client of this protocol, including for remote debugging, there are ways for third parties to use it and start instrumenting browser pages explicitly. We will describe the ways it could be done below.

Protocol

Interaction protocol consists of JSON commands that are sent to the page and events that the page is generating. We define this protocol in Blink ("upstream") so that any Blink-based browser supported it.

Stable

Debugger protocol version 1.1 is the most recent stable release of the protocol.

As of Google Chrome 31, we commit to supporting v1.1. All subsequent 1.* versions of the protocol are going to be backwards compatible with 1.1. Our protocol backwards compatibility commitment is:

  • No commands or events are removed from the protocol.
  • No required parameters are added to the commands.
  • No required parameters are removed from command responses or events.

Previous versions: Protocol v1.0 was shipped and supported as of Chrome 18. Protocol v0.1 was shipped and supported as of Chrome 16.

Alpha

The tip-of-tree protocol is volatile and may break at any time. However it captures the full capabilities of the Protocol, whereas the stable release is a subset. There is no backwards compatibility support guaranteed for the capabilities it introduces. You can use it with Google Canary builds at your own risk.

The tip-of-tree protocol is more readable in the debugger protocol viewer.

Sniffing the protocol

You can inspect how the Chrome DevTools uses the protocol. Especially handy when looking up newer features. First, run Chrome with the debugging port open:

/Applications/Google\ Chrome\ Canary.app/Contents/MacOS/Google\ Chrome\ Canary --remote-debugging-port=9222 http://localhost:9222 http://chromium.org
Then, select the Chromium Projects item in the Inspectable Pages list. Now that DevTools is up and fullscreen, open DevTools to inspect it. Cmd-R in the new inspector to make the first restart. Now head to Network Panel, filter by Websocket, select the connection and click the Frames tab. Now you can easily see the frames of WebSocket activity as you use the first instance of the DevTools.

Sniffing the debugger protocol

Debugging over the wire

Today Developer Tools front-end can attach to a remotely running Chrome instance for debugging. For this scenario to work, you should start your host Chrome instance with the remote-debugging-port command line switch:

    chrome.exe --remote-debugging-port=9222

Then you can start a client Chrome instance, using a separate user profile:

    chrome.exe --user-data-dir=<some directory>

And now you can navigate to the given port from your client and attach to any of the discovered tabs for debugging: http://localhost:9222

You will find Developer Tools interface identical to the embedded one and here is why:

  • When you navigate your client browser to the remote's Chrome port, Developer Tools front-end is being served from the host Chrome as a Web Application from the Web Server.
  • It fetches HTML, JavaScript and CSS files over HTTP
  • Once loaded, Developer Tools establishes a Web Socket connection to its host and starts interchanging JSON messages with it.

In this scenario, you can substitute Developer Tools front-end with your own implementation. Instead of navigating to the HTML page at http://localhost:9222, your application can discover available pages by requesting:

http://localhost:9222/json

and getting a JSON object with information about inspectable pages along with the WebSocket addresses that you could use in order to start instrumenting them.

Remote debugging is especially useful when debugging remote instances of the browser or attaching to the embedded devices. Blink port owners are responsible for exposing debugging connections to the external users.

Debugging Protocol Clients

Many applications and libraries already use the protocol. Some to collect performance data, others to breakpoint debug from another editor. There are libraries to access the raw protocol from Node.js and Python.

Many of these clients are showcased here: Showcased Debugging Protocol Clients.

Using debugger extension API

To allow third parties to interact with the protocol, we introduced chrome.debugger extension API that exposes this JSON message transport interface. As a result, you can not only attach to the remotely running Chrome instance, but also instrument it from its own extension.

Chrome Debugger Extension API provides a higher level API where command domain, name and body are provided explicitly in the sendCommand call. This API hides request ids and handles binding of the request with its response, hence allowing sendCommand to report result in the callback function call. One can also use this API in combination with the other Extension APIs.

If you are developing a Web-based IDE, you should implement an extension that exposes debugging capabilities to your page and your IDE will be able to open pages with the target application, set breakpoints there, evaluate expressions in console, live edit JavaScript and CSS, display live DOM, network interaction and any other aspect that Developer Tools is instrumenting today.

Opening embedded Developer Tools will terminate the remote connection and thus detach the extension.

Simultaneous protocol clients

We currently do not support multiple clients connected to the protocol simultaneously. That includes the DevTools opening while another client is connected. On the bug tracker, crbug.com/129539 follows the issue; you can star it for email updates.

Upon disconnnection, the outgoing client will receive a detached event. For example: {"method":"Inspector.detached","params":{"reason":"replaced_with_devtools"}}. View the enum of possible reasons. (For reference: the original patch). After disconnection, some apps have chosen to pause their state and offer a reconnect button.