| 1 |
Tutorial: Set a breakpoint using `Debugger` |
| 2 |
=========================================== |
| 3 |
|
| 4 |
This page shows how you can try out the [`Debugger` API][debugger] yourself |
| 5 |
using Firefox's Scratchpad. We use `Debugger` to set a breakpoint in a function, |
| 6 |
and then evaluate an expression whenever it is hit. |
| 7 |
|
| 8 |
This tutorial was tested against Firefox 58 Beta and Nightly. It does not work in Firefox 57. |
| 9 |
|
| 10 |
1. Since the `Debugger` API is only available to privileged JavaScript code, |
| 11 |
you'll need to use the Browser Content Toolbox to try it out. To do this, |
| 12 |
open the Firefox developer tools, click on the options gear at the upper |
| 13 |
right of the toolbox, and make sure that both “Enable browser chrome and |
| 14 |
add-on debugging toolboxes” and “Enable remote debugging” are checked. These |
| 15 |
are located at the bottom right of the options panel; you may need to scroll |
| 16 |
to see them. Once they're checked, you can close the developer tools. |
| 17 |
|
| 18 |
|
| 19 |
2. Save the following text to an HTML file: |
| 20 |
|
| 21 |
```html |
| 22 |
<div onclick="report('the best div');">Click me!</div> |
| 23 |
<div onclick="report('another great div');">Or me!</div> |
| 24 |
<script> |
| 25 |
function report(what) { |
| 26 |
console.log('clicked: ' + what); |
| 27 |
} |
| 28 |
</script> |
| 29 |
``` |
| 30 |
|
| 31 |
3. Visit the HTML file in your browser, and open the Browser Content Toolbox by |
| 32 |
opening the Firefox menu, choosing “Browser Tools”, and then “Browser |
| 33 |
Content Toolbox”. If that item doesn't appear in the “Browser Tools” menu, |
| 34 |
make sure you checked both boxes to enable the Browser Content Toolbox as |
| 35 |
explained in Step 1. |
| 36 |
|
| 37 |
4. Our example code is long enough that the best way to run it is to use the |
| 38 |
Scratchpad panel, which is not enabled by default. To enable it, click on |
| 39 |
the options gear at the upper right of the Browser Content Toolbox, and make |
| 40 |
sure the “Scratchpad” box in the “Default Developer Tools” section the left |
| 41 |
is checked. The Scratchpad panel should appear at the top of the Toolbox |
| 42 |
alongside the Console, Debugger, and Memory panels. |
| 43 |
|
| 44 |
5. Click on the Scratchpad panel and enter the following code: |
| 45 |
|
| 46 |
```js |
| 47 |
const { addDebuggerToGlobal } = ChromeUtils.importESModule( |
| 48 |
"resource://gre/modules/jsdebugger.sys.mjs" |
| 49 |
); |
| 50 |
|
| 51 |
// This simply defines 'Debugger' in this Scratchpad; |
| 52 |
// it doesn't actually start debugging anything. |
| 53 |
addDebuggerToGlobal(globalThis); |
| 54 |
|
| 55 |
// Create a 'Debugger' instance. |
| 56 |
var dbg = new Debugger; |
| 57 |
|
| 58 |
// Make the tab's top window a debuggee, and get a |
| 59 |
// Debugger.Object referring to the window. |
| 60 |
var windowDO = dbg.addDebuggee(tabs[0].content); |
| 61 |
|
| 62 |
// Get a Debugger.Object referring to the window's `report` |
| 63 |
// function. |
| 64 |
var reportDO = windowDO.getOwnPropertyDescriptor('report').value; |
| 65 |
|
| 66 |
// Set a breakpoint at the entry point of `report`. |
| 67 |
reportDO.script.setBreakpoint(0, { |
| 68 |
hit: function (frame) { |
| 69 |
console.log('hit breakpoint in ' + frame.callee.name); |
| 70 |
console.log('what = ' + frame.eval('what').return); |
| 71 |
} |
| 72 |
}); |
| 73 |
|
| 74 |
console.log('Finished setting breakpoint!'); |
| 75 |
``` |
| 76 |
|
| 77 |
6. In the Scratchpad, ensure that no text is selected, and press the "Run" |
| 78 |
button. |
| 79 |
|
| 80 |
Now, click on the text that says "Click me!" in the web page. |
| 81 |
This runs the `div` element's `onclick` handler. |
| 82 |
When control reaches the start of the `report` function, |
| 83 |
`Debugger` calls the breakpoint handler's `hit` method, |
| 84 |
passing a `Debugger.Frame` instance. |
| 85 |
The `hit` method logs the breakpoint hit to the browser content toolbox's console. |
| 86 |
Then it evaluates the expression `what` in the given stack frame, and logs its result. |
| 87 |
The toolbox's console now looks like this: |
| 88 |
|
| 89 |
![The breakpoint handler's console output][img-example-console] |
| 90 |
<!-- HTML comment prevents caption, which MDN styles poorly --> |
| 91 |
|
| 92 |
You can also click on the text that says “Or me!”, to see `report` called from a |
| 93 |
different handler. |
| 94 |
|
| 95 |
If `Debugger` is unable to find the `report` function, or the console output |
| 96 |
does not appear, evaluate the expression `tabs[0].content.document.location` |
| 97 |
in the console to make sure that `tabs[0]` indeed refers to the HTML file you |
| 98 |
visited. If you have more than one tab visiting a `file:` URL, they all share |
| 99 |
a single content process, so you may need to use a different element of the |
| 100 |
array as the debuggee. |
| 101 |
|
| 102 |
7. Press "Run" in the Scratchpad again. Now, clicking on "Click me!" causes the |
| 103 |
breakpoint hit to be logged twice---one for each `Debugger` instance. |
| 104 |
|
| 105 |
Multiple `Debugger` instances can observe the same debuggee. Re-running the code |
| 106 |
in the Scratchpad creates a fresh `Debugger` instance, adds the same web page as |
| 107 |
its debuggee, and then sets a new breakpoint. When you click on the `div` |
| 108 |
element, both `Debugger`s' breakpoints are hit, and both handlers run. |
| 109 |
|
| 110 |
This shows how any number of `Debugger`-based tools can observe a single web |
| 111 |
page simultaneously. In fact, you can use the Browser Content Toolbox's Debugger |
| 112 |
panel to set its own breakpoint in `report`, and it will trigger along with the |
| 113 |
first two. Keep in mind, however, that when multiple Debuggers share a debuggee, |
| 114 |
the order in which their handlers run is not specified. If more than one tool |
| 115 |
tries to influence the debuggee's behavior, their combined behavior could be |
| 116 |
unpredictable. |
| 117 |
|
| 118 |
8. Close the web page and the Browser Content Toolbox. |
| 119 |
|
| 120 |
Since both the Scratchpad's global object and the debuggee window are |
| 121 |
now gone, the `Debugger` instances will be garbage collected, since |
| 122 |
they can no longer have any visible effect on Firefox's behavior. The |
| 123 |
`Debugger` API tries to interact with garbage collection as |
| 124 |
transparently as possible; for example, if both a `Debugger.Object` |
| 125 |
instance and its referent are not reachable, they will both be |
| 126 |
collected, even while the `Debugger` instance to which the shadow |
| 127 |
belonged continues to exist. |
| 128 |
|
| 129 |
[debugger]: Debugger-API.md |
| 130 |
[img-example-console]: console.png |