Debugger
The Debug object provides functions for debugging code running in a separate compartment. You can provide functions for SpiderMonkey to call when events like steps, calls, and breakpoint hits occur in the debuggee, examine the debuggee's stack frames, and inspect and manipulate the debuggee's objects.
Debug object event hook functions run in the same thread as the debuggee, on the same stack: when the event occurs, the debuggee pauses while your hook functions run, and resumes (unless you say otherwise) when your functions return.
The debugger and debuggee must be in separate compartments. Your hook functions run in the debugger's compartment. SpiderMonkey mediates their access to the debuggee's objects, and prevents the debuggee from accessing the debugger's objects at all.
The Debug object provides objects representing the debuggee's stack frames, scripts, and other internal interpreter structures, for your hook functions to examine and manipulate.
Debugger access to debuggee values
The Debug object follows certain conventions to help debuggers safely inspect and modify the debuggee's objects and values. Primitive values are passed freely between debugger and debuggee; copying or wrapping is handled transparently, as appropriate. Objects (including host objects like DOM nodes) received from the debuggee are fronted in the debugger by Debug.Object instances (described in detail below), which provide reflection-oriented methods for inspecting the referent object's properties and other characteristics. Of the debugger's objects, only Debug.Object instances may be passed to the debuggee: when this occurs, the debuggee receives the Debug.Object's referent, not the Debug.Object instance itself.
In the descriptions below, the term "debuggee value" means either a primitive value or a Debug.Object instance; it is a value that might be received from the debuggee, or that could be passed to the debuggee.
Beginning to Debug
To begin debugging another compartment's code, you create a Debug object for the debuggee compartment, and install your hook functions.
- Debug(object)
- Create a debugger object debugging |object|'s compartment. |Object| is typically a global object, but can be any JavaScript object from the debuggee's compartment.
The |object| must be in a different compartment than the calling code, and debugger/debuggee compartments may not form a cycle. |object|'s compartment must not be in use by another thread.
- D.setHooks(hooks)
- Use the functions in |hooks| to handle events occurring in D's debuggee. |Hooks| should be an object; each property should be named after a debugging event, and its value should be a function SpiderMonkey should call when the named event occurs. See below for descriptions of specific debugging hooks.
This removes all previously registered hooks; after the call, only the hooks mentioned in |hooks| are in force.
Hook function calls are cross-compartment, same-thread calls. Hook functions run in the thread in which the event occurred, not in the thread that registered the hooks. (It is your responsibility to ensure that two threads don't try to run in the same compartment). Hook functions run in the compartment to which they belong, not in the debuggee's compartment.
- D.getHooks()
- Return an object holding all the event hooks currently in force. The returned object is suitable for use with D.setHooks.
Debugging hooks
For each debugging hook, we give the name of the hook and the arguments passed to its handler function, and describe the circumstances under which SpiderMonkey calls it.
- interrupt(frame)
- A bytecode instruction is about to execute in the stack frame represented by frame, a Debug.Frame instance. Naturally, frame is the youngest debuggee frame.
This hook function's return value determines how execution should continue:
- If it returns true, execution continues normally.
- If it returns an object of the form { throw: value }, then value is thrown as an exception from the current bytecode instruction. value must be a debuggee value.