Debugging over the V8 Inspector API

Jakob Gruber edited this page May 10, 2017 · 5 revisions

Introduction

V8 provides extensive debugging functionality to both users and embedders. Users will usually interact with the V8 debugger through the Chrome Devtools interface. Embedders (including Devtools) need to rely directly on the Inspector Protocol.

This page is intended to give embedders the basic tools they need to implement debugging support in V8.

Connecting to Inspector

V8's command-line debug shell d8 includes a simple inspector integration through the InspectorFrontend and InspectorClient. The client sets up a communication channel for messages sent from the embedder to V8 in

  static void SendInspectorMessage(
      const v8::FunctionCallbackInfo<v8::Value>& args) {
    // [...] Create a StringView that Inspector can understand.
    session->dispatchProtocolMessage(message_view);
  }

while the frontend establishes a channel for messages sent from V8 to the embedder by implementing sendResponse and sendNotification, which then forward to:

  void Send(const v8_inspector::StringView& string) {
    // [...] String transformations.
    // Grab the global property called 'receive' from the current context.
    Local<String> callback_name =
        v8::String::NewFromUtf8(isolate_, "receive", v8::NewStringType::kNormal)
            .ToLocalChecked();
    Local<Context> context = context_.Get(isolate_);
    Local<Value> callback =
        context->Global()->Get(context, callback_name).ToLocalChecked();
    // And call it to pass the message on to JS.
    if (callback->IsFunction()) {
      // [...]
      MaybeLocal<Value> result = Local<Function>::Cast(callback)->Call(
          context, Undefined(isolate_), 1, args);
    }
  }

Using the Inspector Protocol

Continuing with our example, d8 forwards inspector messages to JavaScript. The following code implements a basic, but fully functional interaction with inspector through d8:

// inspector-demo.js
// Receiver function called by d8.
function receive(message) {
  print(message)
}

const msg = JSON.stringify({
      id: 0,
      method: "Debugger.enable",
    });

// Call the function provided by d8.
send(msg);

// Run this file by executing 'd8 --enable-inspector inspector-demo.js'.

Further Documentation

A more fleshed-out example of Inspector API usage is available at test-api.js, which implements a simple debugging API for use by V8's test suite.

V8 also contains an alternative Inspector integration at inspector-test.cc.

The Chrome Devtools wiki provides full documentation of all available functions.

Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.