| 1 |
# The `Debugger` Interface |
| 2 |
|
| 3 |
Mozilla's JavaScript engine, SpiderMonkey, provides a debugging interface |
| 4 |
named `Debugger` which lets JavaScript code observe and manipulate the |
| 5 |
execution of other JavaScript code. Both Firefox's built-in developer tools |
| 6 |
and the Firebug add-on use `Debugger` to implement their JavaScript |
| 7 |
debuggers. However, `Debugger` is quite general, and can be used to |
| 8 |
implement other kinds of tools like tracers, coverage analysis, |
| 9 |
patch-and-continue, and so on. |
| 10 |
|
| 11 |
`Debugger` has three essential qualities: |
| 12 |
|
| 13 |
- It is a *source level* interface: it operates in terms of the JavaScript |
| 14 |
language, not machine language. It operates on JavaScript objects, stack |
| 15 |
frames, environments, and code, and presents a consistent interface |
| 16 |
regardless of whether the debuggee is interpreted, compiled, or |
| 17 |
optimized. If you have a strong command of the JavaScript language, you |
| 18 |
should have all the background you need to use `Debugger` successfully, |
| 19 |
even if you have never looked into the language's implementation. |
| 20 |
|
| 21 |
- It is for use *by JavaScript code*. JavaScript is both the debuggee |
| 22 |
language and the tool implementation language, so the qualities that make |
| 23 |
JavaScript effective on the web can be brought to bear in crafting tools |
| 24 |
for developers. As is expected of JavaScript APIs, `Debugger` is a |
| 25 |
*sound* interface: using (or even misusing) `Debugger` should never cause |
| 26 |
Gecko to crash. Errors throw proper JavaScript exceptions. |
| 27 |
|
| 28 |
- It is an *intra-thread* debugging API. Both the debuggee and the code |
| 29 |
using `Debugger` to observe it must run in the same thread. Cross-thread, |
| 30 |
cross-process, and cross-device tools must use `Debugger` to observe the |
| 31 |
debuggee from within the same thread, and then handle any needed |
| 32 |
communication themselves. (Firefox's builtin tools have a |
| 33 |
[protocol][protocol] defined for this purpose.) |
| 34 |
|
| 35 |
In Gecko, the `Debugger` API is available to chrome code only. By design, |
| 36 |
it ought not to introduce security holes, so in principle it could be made |
| 37 |
available to content as well; but it is hard to justify the security risks |
| 38 |
of the additional attack surface. |
| 39 |
|
| 40 |
The `Debugger` API cannot currently observe self-hosted JavaScript. This is not |
| 41 |
inherent in the API's design, but simply that the self-hosting infrastructure |
| 42 |
isn't prepared for the kind of invasions the `Debugger` API can perform. |
| 43 |
|
| 44 |
|
| 45 |
## Debugger Instances and Shadow Objects |
| 46 |
|
| 47 |
`Debugger` reflects every aspect of the debuggee's state as a JavaScript |
| 48 |
value---not just actual JavaScript values like objects and primitives, |
| 49 |
but also stack frames, environments, scripts, and compilation units, which |
| 50 |
are not normally accessible as objects in their own right. |
| 51 |
|
| 52 |
Here is a JavaScript program in the process of running a timer callback function: |
| 53 |
|
| 54 |
![A running JavaScript program and its Debugger shadows][img-shadows] |
| 55 |
|
| 56 |
This diagram shows the various types of shadow objects that make up the |
| 57 |
Debugger API (which all follow some [general conventions][conventions]): |
| 58 |
|
| 59 |
- A [`Debugger.Object`][object] represents a debuggee object, offering a |
| 60 |
reflection-oriented API that protects the debugger from accidentally |
| 61 |
invoking getters, setters, proxy traps, and so on. |
| 62 |
|
| 63 |
- A [`Debugger.Script`][script] represents a block of JavaScript |
| 64 |
code---either a function body or a top-level script. Given a |
| 65 |
`Debugger.Script`, one can set breakpoints, translate between source |
| 66 |
positions and bytecode offsets (a deviation from the "source level" |
| 67 |
design principle), and find other static characteristics of the code. |
| 68 |
|
| 69 |
- A [`Debugger.Frame`][frame] represents a running stack frame. You can use |
| 70 |
these to walk the stack and find each frame's script and environment. You |
| 71 |
can also set `onStep` and `onPop` handlers on frames. |
| 72 |
|
| 73 |
- A [`Debugger.Environment`][environment] represents an environment, |
| 74 |
associating variable names with storage locations. Environments may |
| 75 |
belong to a running stack frame, captured by a function closure, or |
| 76 |
reflect some global object's properties as variables. |
| 77 |
|
| 78 |
The [`Debugger`][debugger-object] instance itself is not really a shadow of |
| 79 |
anything in the debuggee; rather, it maintains the set of global objects |
| 80 |
which are to be considered debuggees. A `Debugger` observes only execution |
| 81 |
taking place in the scope of these global objects. You can set functions to |
| 82 |
be called when new stack frames are pushed; when new code is loaded; and so |
| 83 |
on. |
| 84 |
|
| 85 |
Omitted from this picture are [`Debugger.Source`][source] instances, which |
| 86 |
represent JavaScript compilation units. A `Debugger.Source` can furnish a |
| 87 |
full copy of its source code, and explain how the code entered the system, |
| 88 |
whether via a call to `eval`, a `<script>` element, or otherwise. A |
| 89 |
`Debugger.Script` points to the `Debugger.Source` from which it is derived. |
| 90 |
|
| 91 |
Also omitted is the `Debugger`'s [`Debugger.Memory`][memory] instance, which |
| 92 |
holds methods and accessors for observing the debuggee's memory use. |
| 93 |
|
| 94 |
All these types follow some [general conventions][conventions], which you |
| 95 |
should look through before drilling down into any particular type's |
| 96 |
specification. |
| 97 |
|
| 98 |
All shadow objects are unique per `Debugger` and per referent. For a given |
| 99 |
`Debugger`, there is exactly one `Debugger.Object` that refers to a |
| 100 |
particular debuggee object; exactly one `Debugger.Frame` for a particular |
| 101 |
stack frame; and so on. Thus, a tool can store metadata about a shadow's |
| 102 |
referent as a property on the shadow itself, and count on finding that |
| 103 |
metadata again if it comes across the same referent. And since shadows are |
| 104 |
per-`Debugger`, tools can do so without worrying about interfering with |
| 105 |
other tools that use their own `Debugger` instances. |
| 106 |
|
| 107 |
|
| 108 |
## Examples |
| 109 |
|
| 110 |
Here are some things you can try out yourself that show off some of `Debugger`'s |
| 111 |
features: |
| 112 |
|
| 113 |
- [Setting a breakpoint][tut breakpoint] in a page, running a handler function |
| 114 |
when it is hit that evaluates an expression in the page's context. |
| 115 |
|
| 116 |
- [Showing how many objects different call paths allocate.][tut alloc log] |
| 117 |
|
| 118 |
[object]: Debugger.Object.md |
| 119 |
[protocol]: https://wiki.mozilla.org/Remote_Debugging_Protocol |
| 120 |
[img-shadows]: shadows.svg |
| 121 |
[script]: Debugger.Script.md |
| 122 |
[frame]: Debugger.Frame.md |
| 123 |
[environment]: Debugger.Environment.md |
| 124 |
[debugger-object]: Debugger.md |
| 125 |
[source]: Debugger.Source.md |
| 126 |
[memory]: Debugger.Memory.md |
| 127 |
[conventions]: Conventions.md |
| 128 |
[tut breakpoint]: Tutorial-Breakpoint.md |
| 129 |
[tut alloc log]: Tutorial-Alloc-Log-Tree.md |