An interesting feature got added to node.js 0.12 (also available in io.js):
vm.runInDebugContext(code)
The documentation says "The primary use case is to get access to the V8 debug object", using the following snippet:
var vm = require("vm")
var Debug = vm.runInDebugContext("Debug")
Debug.setListener(_=>_)
Debug.scripts().forEach(function(script) {
console.log(script.name)
})
This code snippet should list all the "scripts" V8 has loaded. Try it, there are TONS of them.
The debug context gets autocleared after the end of the vm.runInDebugContext() line. To make things work, we need to keep the context active somehow; in this case, by doing a Debug.setListener()
.
So, what else can you do with this thing. That's what I'm going to find out.
The place to figure this is out in the REPL, and also by checking out the V8 source for this stuff:
Note that a lot of the text here comes directly from debug-debugger.js
.
Note that I'm going to be skipping two parts of the Debug object for now:
TestApi
LiveEdit
An object with the following properties, whose values are number constants:
Break
Exception
NewFunction
BeforeCompile
AfterCompile
CompileError
PromiseEvent
AsyncTaskEvent
BreakForCommand
An object with the following properties, whose values are number constants:
Caught
Uncaught
An object with the following properties, whose values are number constants:
StepOut
StepNext
StepIn
StepMin
StepInMin
StepFrame
An object with the following properties, whose values are number constants:
Native
Extension
Normal
An object with the following properties, whose values are number constants:
Host
Eval
JSON
An object with the following properties, whose values are number constants:
ScriptId
ScriptName
ScriptRegExp
An object with the following properties, whose values are number constants:
Statement
BreakPosition
Sets an event listening for debug Events.
see: https://github.com/iojs/io.js/blob/v1.x/deps/v8/test/mjsunit/debug-event-listener.js
listener
should be a function with following signature:
function(event, exec_state, event_data, data)
event
will be one of theDebugEvent
propertiesexec_state
will be anExecState
objectevent_data
is event-specific datadata
is the object passed tosetListener()
'sopt_data
parameter
Returns a Script
object.
If the parameter is a function, the return value is the script in which the function is defined.
If the parameter is a string, the return value is the script for which the script name has that string value.
If it is a regexp and there is a unique script whose name matches we return that, otherwise undefined.
Returns the script source.
If the parameter is a function the return value is the script source for the script in which the function is defined.
If the parameter is a string the return value is the script for which the script name has that string value.
Returns the source of a function.
Returns the disassembly of a function.
Returns the disassembly of a constructor.
Returns an array of Script
objects
Returns an object with the following properties:
breakPointsActive
breakOnCaughtException
breakOnUncaughtException
Each of these properties is an object with a getValue()
and setValue()
method, which get and set the boolean value of the property.
A Script
object has the following properties:
id
- Numbername
- Stringsource
- Stringtype
- value ofDebug.ScriptType
compilation_type
- value ofDebug.ScriptCompilationType
column_offset
- Numberline_offset
- Numberline_ends
- array of Numbercontext_data
- ???eval_from_script
- ???eval_from_script_position
- ???eval_from_function_name
- ???source_url
- ???source_mapping_url
- ???