2016-12-13 02:08:31 +01:00
|
|
|
# Inspector
|
|
|
|
|
2017-11-04 09:08:46 +01:00
|
|
|
<!--introduced_in=v8.0.0-->
|
|
|
|
|
2016-12-13 02:08:31 +01:00
|
|
|
> Stability: 1 - Experimental
|
|
|
|
|
|
|
|
The `inspector` module provides an API for interacting with the V8 inspector.
|
|
|
|
|
|
|
|
It can be accessed using:
|
|
|
|
|
|
|
|
```js
|
|
|
|
const inspector = require('inspector');
|
|
|
|
```
|
|
|
|
|
2017-05-26 04:00:24 +02:00
|
|
|
## inspector.open([port[, host[, wait]]])
|
|
|
|
|
2018-04-02 03:44:32 +02:00
|
|
|
* `port` {number} Port to listen on for inspector connections. Optional.
|
|
|
|
**Default:** what was specified on the CLI.
|
|
|
|
* `host` {string} Host to listen on for inspector connections. Optional.
|
|
|
|
**Default:** what was specified on the CLI.
|
|
|
|
* `wait` {boolean} Block until a client has connected. Optional.
|
|
|
|
**Default:** `false`.
|
2017-05-26 04:00:24 +02:00
|
|
|
|
|
|
|
Activate inspector on host and port. Equivalent to `node
|
2017-12-31 10:45:43 +01:00
|
|
|
--inspect=[[host:]port]`, but can be done programmatically after node has
|
2017-05-26 04:00:24 +02:00
|
|
|
started.
|
|
|
|
|
|
|
|
If wait is `true`, will block until a client has connected to the inspect port
|
|
|
|
and flow control has been passed to the debugger client.
|
|
|
|
|
|
|
|
### inspector.close()
|
|
|
|
|
|
|
|
Deactivate the inspector. Blocks until there are no active connections.
|
|
|
|
|
|
|
|
### inspector.url()
|
|
|
|
|
2018-04-11 20:07:14 +02:00
|
|
|
* Returns: {string|undefined}
|
|
|
|
|
2017-05-26 04:00:24 +02:00
|
|
|
Return the URL of the active inspector, or `undefined` if there is none.
|
|
|
|
|
2016-12-13 02:08:31 +01:00
|
|
|
## Class: inspector.Session
|
|
|
|
|
|
|
|
The `inspector.Session` is used for dispatching messages to the V8 inspector
|
|
|
|
back-end and receiving message responses and notifications.
|
|
|
|
|
|
|
|
### Constructor: new inspector.Session()
|
|
|
|
<!-- YAML
|
2017-03-16 04:26:14 +01:00
|
|
|
added: v8.0.0
|
2016-12-13 02:08:31 +01:00
|
|
|
-->
|
|
|
|
|
|
|
|
Create a new instance of the `inspector.Session` class. The inspector session
|
|
|
|
needs to be connected through [`session.connect()`][] before the messages
|
|
|
|
can be dispatched to the inspector backend.
|
|
|
|
|
|
|
|
`inspector.Session` is an [`EventEmitter`][] with the following events:
|
|
|
|
|
|
|
|
### Event: 'inspectorNotification'
|
|
|
|
<!-- YAML
|
2017-03-16 04:26:14 +01:00
|
|
|
added: v8.0.0
|
2016-12-13 02:08:31 +01:00
|
|
|
-->
|
|
|
|
|
|
|
|
* {Object} The notification message object
|
|
|
|
|
|
|
|
Emitted when any notification from the V8 Inspector is received.
|
|
|
|
|
|
|
|
```js
|
|
|
|
session.on('inspectorNotification', (message) => console.log(message.method));
|
|
|
|
// Debugger.paused
|
|
|
|
// Debugger.resumed
|
|
|
|
```
|
|
|
|
|
|
|
|
It is also possible to subscribe only to notifications with specific method:
|
|
|
|
|
|
|
|
### Event: <inspector-protocol-method>
|
|
|
|
<!-- YAML
|
2017-03-16 04:26:14 +01:00
|
|
|
added: v8.0.0
|
2016-12-13 02:08:31 +01:00
|
|
|
-->
|
|
|
|
|
|
|
|
* {Object} The notification message object
|
|
|
|
|
|
|
|
Emitted when an inspector notification is received that has its method field set
|
|
|
|
to the `<inspector-protocol-method>` value.
|
|
|
|
|
2018-04-09 18:30:22 +02:00
|
|
|
The following snippet installs a listener on the [`'Debugger.paused'`][]
|
2016-12-13 02:08:31 +01:00
|
|
|
event, and prints the reason for program suspension whenever program
|
|
|
|
execution is suspended (through breakpoints, for example):
|
|
|
|
|
|
|
|
```js
|
2017-06-01 01:07:25 +02:00
|
|
|
session.on('Debugger.paused', ({ params }) => {
|
|
|
|
console.log(params.hitBreakpoints);
|
|
|
|
});
|
2017-10-18 04:26:34 +02:00
|
|
|
// [ '/the/file/that/has/the/breakpoint.js:11:0' ]
|
2016-12-13 02:08:31 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
### session.connect()
|
|
|
|
<!-- YAML
|
2017-03-16 04:26:14 +01:00
|
|
|
added: v8.0.0
|
2016-12-13 02:08:31 +01:00
|
|
|
-->
|
|
|
|
|
|
|
|
Connects a session to the inspector back-end. An exception will be thrown
|
|
|
|
if there is already a connected session established either through the API or by
|
|
|
|
a front-end connected to the Inspector WebSocket port.
|
|
|
|
|
|
|
|
### session.post(method[, params][, callback])
|
|
|
|
<!-- YAML
|
2017-03-16 04:26:14 +01:00
|
|
|
added: v8.0.0
|
2016-12-13 02:08:31 +01:00
|
|
|
-->
|
|
|
|
|
2018-04-07 14:23:10 +02:00
|
|
|
* `method` {string}
|
|
|
|
* `params` {Object}
|
|
|
|
* `callback` {Function}
|
2016-12-13 02:08:31 +01:00
|
|
|
|
|
|
|
Posts a message to the inspector back-end. `callback` will be notified when
|
|
|
|
a response is received. `callback` is a function that accepts two optional
|
|
|
|
arguments - error and message-specific result.
|
|
|
|
|
|
|
|
```js
|
2017-05-24 03:05:06 +02:00
|
|
|
session.post('Runtime.evaluate', { expression: '2 + 2' },
|
|
|
|
(error, { result }) => console.log(result));
|
2016-12-13 02:08:31 +01:00
|
|
|
// Output: { type: 'number', value: 4, description: '4' }
|
|
|
|
```
|
|
|
|
|
|
|
|
The latest version of the V8 inspector protocol is published on the
|
|
|
|
[Chrome DevTools Protocol Viewer][].
|
|
|
|
|
|
|
|
Node inspector supports all the Chrome DevTools Protocol domains declared
|
|
|
|
by V8. Chrome DevTools Protocol domain provides an interface for interacting
|
|
|
|
with one of the runtime agents used to inspect the application state and listen
|
|
|
|
to the run-time events.
|
|
|
|
|
|
|
|
### session.disconnect()
|
|
|
|
<!-- YAML
|
2017-03-16 04:26:14 +01:00
|
|
|
added: v8.0.0
|
2016-12-13 02:08:31 +01:00
|
|
|
-->
|
|
|
|
|
|
|
|
Immediately close the session. All pending message callbacks will be called
|
|
|
|
with an error. [`session.connect()`] will need to be called to be able to send
|
|
|
|
messages again. Reconnected session will lose all inspector state, such as
|
|
|
|
enabled agents or configured breakpoints.
|
|
|
|
|
2018-03-06 18:08:47 +01:00
|
|
|
## Example usage
|
|
|
|
|
|
|
|
### CPU Profiler
|
|
|
|
|
|
|
|
Apart from the debugger, various V8 Profilers are available through the DevTools
|
|
|
|
protocol. Here's a simple example showing how to use the [CPU profiler][]:
|
|
|
|
|
|
|
|
```js
|
|
|
|
const inspector = require('inspector');
|
2018-03-15 23:44:04 +01:00
|
|
|
const fs = require('fs');
|
2018-03-06 18:08:47 +01:00
|
|
|
const session = new inspector.Session();
|
|
|
|
session.connect();
|
|
|
|
|
|
|
|
session.post('Profiler.enable', () => {
|
|
|
|
session.post('Profiler.start', () => {
|
|
|
|
// invoke business logic under measurement here...
|
|
|
|
|
|
|
|
// some time later...
|
2018-03-15 23:44:04 +01:00
|
|
|
session.post('Profiler.stop', (err, { profile }) => {
|
2018-03-06 18:08:47 +01:00
|
|
|
// write profile to disk, upload, etc.
|
2018-03-15 23:44:04 +01:00
|
|
|
if (!err) {
|
|
|
|
fs.writeFileSync('./profile.cpuprofile', JSON.stringify(profile));
|
|
|
|
}
|
2018-03-06 18:08:47 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
2017-05-26 04:00:24 +02:00
|
|
|
|
2018-04-09 18:30:22 +02:00
|
|
|
[`'Debugger.paused'`]: https://chromedevtools.github.io/devtools-protocol/v8/Debugger#event-paused
|
2016-12-13 02:08:31 +01:00
|
|
|
[`EventEmitter`]: events.html#events_class_eventemitter
|
2018-04-09 18:30:22 +02:00
|
|
|
[`session.connect()`]: #inspector_session_connect
|
2016-12-13 02:08:31 +01:00
|
|
|
[Chrome DevTools Protocol Viewer]: https://chromedevtools.github.io/devtools-protocol/v8/
|
2018-03-06 18:08:47 +01:00
|
|
|
[CPU Profiler]: https://chromedevtools.github.io/devtools-protocol/v8/Profiler
|