Add v8_inspector support by ofrobots · Pull Request #6792 · nodejs/node (original) (raw)
For background see: #2546 (comment)
This PR brings in v8_inspector support to Node.js (currently behind a compile time flag.) This means Node.js can now be debugged and profiled via the Chrome Debugging Protocol, which enables inspection via Chrome DevTools and other tools & IDEs. So far, v8_inspector includes support for the following protocol domains: Debugger, Profiler, Runtime, HeapProfiler
In the Chrome DevTools client, the following is now available:
- Complete breakpoint debugging, stepping w/ blackboxing
- Source maps for transpiled code
- LiveEdit: JavaScript hot-swap evaluation w/ V8
- Console evaluation with ES6 feature/object support and custom object formatting
- Sampling JavaScript profiler w/ flamechart
- Heap snapshot inspection, heap allocation timeline, allocation profiling
- Asynchronous stacks for native promises
There is more functionality that could be implemented, but we've reached a good point to get input from the community on the feature set and on the integration with Node.js core.
This PR has two logical sets of changes:
Bring in v8_inspector as a dependency
v8_inspector
used to be part of Blink. We have removed almost all of the dependencies on Blink and are working on getting this code merged directly into V8 – the biggest piece remaining is integrating the DevTools tests into the V8 test infrastructure. However, this work will likely complete in V8 5.3 / 5.4 timeframe. If we wait for this upstream change, it would mean the improved debugging experience would not be available to Node.js v6.x LTS users.
In the meantime, we propose to bring the v8_inspector dependency directly into Node.js. This dependency can be dropped once v8_inspector merges upstream into V8 (in Node.js v7.x timeframe).
Node.js support for the v8_inspector protocol
The support for talking to v8_inspector protocol is primarily in node_inspector.cc
. We also needed support for a subset of the websockets protocol to be able to talk to DevTools. We have implemented this based on the websockets implementation from Chromium. We are calling it the 'inspector socket protocol' as it is not a complete websockets implementation, although it could be extended. This protocol is not available to userspace JavaScript code, and is accessible only to internal C++ code in core.
The implementation is behind a compile time flag controlled by the --with-inspector
configure flag.
Usage
$ make -j8
$ ./node --inspect benchmark/http_simple.js
Debugger listening on port 5858. To start debugging, open following URL in Chrome:
chrome-devtools://devtools/remote/serve_file/@4604d24a75168768584760ba56d175507941852f/inspector.html?experiments=true&v8only=true&ws=localhost:5858/node
You can attach Chrome DevTools by opening the printed url in a Chrome browser.
By default, the debugging port is 5858. You can optionally specify an alternate port via the --inspect=9229
flag. If you want the debugger to pause on application start, you can additional pass the --debug-brk
flag. For example:
$ ./node --inspect=9229 --debug-brk benchmark/http_simple.js
Debugger listening on port 9229. To start debugging, open following URL in Chrome:
chrome-devtools://devtools/remote/serve_file/@4604d24a75168768584760ba56d175507941852f/inspector.html?experiments=true&v8only=true&ws=localhost:9229/node
Getting this merged into Node.js would allow people to test the DevTools integration and allow proper debugging to be well-tested and stable well in time for the release of the 6.x LTS. It would also enable third-party debug tool writers to start building other debugging and profiling tools that work with Node.js.
Thanks for the review.
/cc @repenaxa, @eugeneo, @paulirish