Search code examples
c#.netv8remote-debuggingclearscript

Interacting with the Chrome V8 debugger protocol through .NET (ClearScript)


I'm maintaining an enterprise C# application that provides end-user extensibility through user-defined JavaScript functions (with a select few C# types/proxies exposed) (think financial algorithms).

We evaluate those functions using ClearScript (prevously we were using another library called Noesis however we've moved away from that).

I'd like to offer a way for end-users to perform very basic debugging of the scripts (through a web interface). At a minimum - inspect variables and step over/into functions.

I've successfully connected to the ClearScript engine w/ Chrome when running the app locally and have been able to step through the scripts - however I don't think the same would make sense for end users whilst running in production for various reasons:

  • Might be too tricky for them to set up
  • Possible difficulties with port allocation
  • Probable firewall issues

What I'd really like is a SignalR interface to the V8 debugger - the web-app would interact with the SignalR, and SignalR would interact with the V8 debugger.

  • Has anyone done something along these lines before?
  • Are there any relevant .NET libraries I should be looking at?
  • Am I right in my thoughts that this seems like a fairly big piece of work?
  • Any other advice?

Solution

  • It's a web-socket based protocol - though you must make a request via HTTP to retrieve the tabId first (request to /json/list)

    https://chromedevtools.github.io/devtools-protocol/1-2/Debugger describes the protocol - there are different revisions targetting different versions of Chrome but 1.2 (stable) seems to work w/ the version of ClearScript I used.

    As well as request-response (command) type interactions on the websocket, V8 will also publish events - e.g. Debugger.scriptParsed & Debugger.paused on the same socket.

    Some of the key commands: Runtime.enable Debugger.enable Runtime.runIfWaitingForDebugger Debugger.getScriptSource Debugger.resume Debugger.evaluateOnCallFrame Runtime.getProperties

    I've written a small demo project https://github.com/flakey-bit/ClearScriptDebugging/ that shows a target process being debugged by another process.