Reputation: 1381
I am using Microsoft.ClearScript.V8 to embed a simple instance of a V8 Engine in a C# console application. I want to remote debug what is happening in the Google Chrome devtools but I fail.
Searching the interwebz, the only solution I can find is to install Eclipse and debug from there. This however is not desired. I want to understand what happens in the background and be able to reproduce the functionality.
I already know that the remote debugger talks through WebSockets but I don't know the implementation details. Can you guys perhaps point me in the right direction here?
My code:
int debugPort = 8888;
var ctx = new V8ScriptEngine(V8ScriptEngineFlags.EnableDebugging, debugPort);
ctx.AllowReflection = true;
/* This is not required but allows me to console log things. */
ctx.AddHostType("Console", typeof(Console));
When I attempt to connect using the V8 inspector (https://chrome.google.com/webstore/detail/nodejs-v8-inspector/lfnddfpljnhbneopljflpombpnkfhggl), I get the following error:
Could not launch debugger Unexpected token T in JSON at position 0
This makes complete sense because it expects a JSON formatted string as Node implements it that way.
When I open http://127.0.0.1:8888/ in my browser, I receive the following output:
Type: connect
V8-Version: 5.3.332.45
Protocol-Version: 1
Embedding-Host: V8Runtime
Content-Length: 0
How do I remote debug my non-Node application from the Google Devtools?
Upvotes: 4
Views: 2846
Reputation: 574
Reading https://stackoverflow.com/a/50695896/5288052 seems that local debug of JavaScript code with ClearScript and Visual Studio Code is a complex thing, but is not. Here follows instructions to do it:
setup Visual Studio Code editing the Launch configuration in settings.json as stated in "VII. Debugging with ClearScript and V8"
start the V8 engine with V8ScriptEngineFlags.EnableDebugging
+ V8ScriptEngineFlags.AwaitDebuggerAndPauseOnStart
option
run the C# code from Visual Studio or directly from the compiled executable; the C# code will pause waiting for the Visual Studio Code debug process to attach to it
start debug in Visual Studio Code (menu Debug | Start Debugging)
At this point VSCode connects and stops at the first JavaScript line executed and then stops at all "debugger;"
commands. The current script is loaded and debuggable, and all previously executed scripts are visible inside VSCode.
See also Issue 159 and Issue 24.
Upvotes: 2
Reputation: 886
This is the part of the answer (and not the answer).
Well so far I've have spent 2 days trying the same, and figured out that ClearScript doesn't use node for debuging javascript code =/=.
have managed to run node.exe and attach debugger: node process list output is (http://127.0.0.1:9229/json/list):
[ {
"description": "node.js instance",
"faviconUrl": "https://nodejs.org/static/favicon.ico",
"id": "574da142-2ee2-44da-a912-03f96868339c",
"title": "Administrator: IA-32 Visual Studio 2008 mode - node --inspect[6416]",
"type": "node",
"url": "file://"
} ]
But it doesn't look anything like ClearScript.V8 device (http://127.0.0.1:9222/):
Type: connect
V8-Version: 5.5.372.40
Protocol-Version: 1
Embedding-Host: V8Runtime
Content-Length: 0
Similar to yours.
Reading further V8 implements it's own debug protocol, which I didn't manage to find any definitive documents. All of internet has bits and pieces but nothing is complete. I've managed in Chrome Dev Tools to recognize Remote Device, but beyond that there is no option to Inspect Device, not sure why (maybe due fact we both are using older version of ClearScript 5.4.x). CDT > Remote Devices: Remote Target #127.0.0.1 / Target (and that's it, no button, no link, nothing can be done with it).
Last hope is to build my own inspector to some extent to see at least js stack traces if nothing (which was originally my goal).
--- Edit: to conclude and finish answer (for op, me, and others):
Found something that works correctly "Microsoft Code". It took little tweaking of configuration example, reading their helps, but made it work. Loading script sources, break points, console, everything just works (as advertised).
How to:
Download Microsoft Code (I've took zip package - no installation needed) https://code.visualstudio.com/
Run Microsoft Code, create workspace (launch.json file will be created there)
launch.json source code:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program (NodeJS)",
"program": "${workspaceRoot}/bin/Debug/",
"cwd": "${workspaceRoot}",
},
{
"type": "node",
"request": "attach",
"name": "Attach by Process ID",
"processId" :"${command:PickProcess}",
"cwd": "${workspaceRoot}",
},
// works with 5.5.x V8 Runtime
{
"type": "node",
"request": "attach",
"name": "Attach to CSV8:9222 (Inspector)",
"protocol": "inspector",
"address": "127.0.0.1",
"port": 9222,
},
// works with 5.4.x V8 Runtime
{
"type": "node",
"request": "attach",
"name": "Attach to CSV8:9222 (Legacy)",
"protocol": "legacy",
"address": "127.0.0.1",
"port": 9222,
},
// works with 5.4.x and 5.5.x V8 Runtime
{
"type": "node",
"request": "attach",
"name": "Attach to CSV8:9222 (Auto)",
"protocol": "auto",
"address": "127.0.0.1",
"port": 9222,
},
]
}
run your ClearScript application (make sure you have debugging enabled, and that debug port matches).
from Microsoft Code Debug toolbar select Attach to CSV8:9222 (Auto) and press run. In this moment MS Code will load all remote JS source code. and you can select source and put break points there. Debuging works just as advertised.
Hope this helps you too
Upvotes: 1
Reputation: 3548
You need at least ClearScript 5.5. Older versions don't support the V8 Inspector protocol. The latest version (5.5.2) works with chrome://inspect and fixes a few bugs.
Upvotes: 1