Debian Codegraph @

js/src/doc/Debugger/Tutorial-Debugger-Statement.md

raw
1 Tutorial: Evaluate an Expression When a debugger; Statement Is Executed
2 =======================================================================
3
4 **NOTE: This tutorial no longer works in current versions of Firefox.**
5 Instead, please try the updated and expanded [breakpoint tutorial][tut breakpoint].
6
7 This page shows how you can try out the [`Debugger` API][debugger] yourself
8 using Firefox's Scratchpad. We use the API to evaluate an expression in the web
9 page whenever it executes a JavaScript `debugger;` statement.
10
11 1. Visit the URL `about:config`, and set the `devtools.chrome.enabled`
12 preference to `true`:
13
14 ![Setting the 'devtools.chrome.enabled' preference][img-chrome-pref]
15
16 2. Save the following HTML text to a file, and visit the file in your
17 browser:
18
19 ```html
20 <div onclick="var x = 'snoo'; debugger;">Click me!</div>
21 ```
22
23 3. Open a developer Scratchpad (Menu button > Developer > Scratchpad), and
24 select "Browser" from the "Environment" menu. (This menu will not be
25 present unless you have changed the preference as explained above.)
26
27 ![Selecting the 'browser' context in the Scratchpad][img-scratchpad-browser]
28
29 4. Enter the following code in the Scratchpad:
30
31 ```js
32 // This simply defines 'Debugger' in this Scratchpad;
33 // it doesn't actually start debugging anything.
34 const { addDebuggerToGlobal } = ChromeUtils.importESModule(
35 "resource://gre/modules/jsdebugger.sys.mjs"
36 );
37 addDebuggerToGlobal(window);
38
39 // Create a 'Debugger' instance.
40 var dbg = new Debugger;
41
42 // Get the current tab's content window, and make it a debuggee.
43 var w = gBrowser.selectedBrowser.contentWindow.wrappedJSObject;
44 dbg.addDebuggee(w);
45
46 // When the debuggee executes a 'debugger' statement, evaluate
47 // the expression 'x' in that stack frame, and show its value.
48 dbg.onDebuggerStatement = function (frame) {
49 alert('hit debugger statement; x = ' + frame.eval('x').return);
50 }
51 ```
52
53 5. In the Scratchpad, ensure that no text is selected, and press the "Run"
54 button.
55
56 6. Now, click on the text that says "Click me!" in the web page. This runs
57 the `div` element's `onclick` handler. When control reaches the
58 `debugger;` statement, `Debugger` calls your callback function, passing
59 a `Debugger.Frame` instance. Your callback function evaluates the
60 expression `x` in the given stack frame, and displays the alert:
61
62 ![The Debugger callback displaying an alert][img-example-alert]
63
64 7. Press "Run" in the Scratchpad again. Now, clicking on the "Click me!"
65 text causes *two* alerts to show---one for each `Debugger`
66 instance.
67
68 Multiple `Debugger` instances can observe the same debuggee. Re-running
69 the code in the Scratchpad created a fresh `Debugger` instance, added
70 the same web page as its debuggee, and then registered a fresh
71 `debugger;` statement handler with the new instance. When you clicked
72 on the `div` element, both of them ran. This shows how any number of
73 `Debugger`-based tools can observe a single web page
74 simultaneously---although, since the order in which their handlers
75 run is not specified, such tools should probably only observe, and not
76 influence, the debuggee's behavior.
77
78 8. Close the web page and the Scratchpad.
79
80 Since both the Scratchpad's global object and the debuggee window are
81 now gone, the `Debugger` instances will be garbage collected, since
82 they can no longer have any visible effect on Firefox's behavior. The
83 `Debugger` API tries to interact with garbage collection as
84 transparently as possible; for example, if both a `Debugger.Object`
85 instance and its referent are not reachable, they will both be
86 collected, even while the `Debugger` instance to which the shadow
87 belonged continues to exist.
88
89 [tut breakpoint]: Tutorial-Breakpoint.md
90 [debugger]: Debugger-API.md
91
92 [img-chrome-pref]: enable-chrome-devtools.png
93 [img-scratchpad-browser]: scratchpad-browser-environment.png
94 [img-example-alert]: debugger-alert.png