Debian Codegraph @

js/src/doc/Debugger/Debugger.Frame.md

raw
1 # Debugger.Frame
2
3 A `Debugger.Frame` instance represents a [visible stack frame][vf]. Given a
4 `Debugger.Frame` instance, you can find the script the frame is executing,
5 walk the stack to older frames, find the lexical environment in which the
6 execution is taking place, and so on.
7
8 For a given [`Debugger`][debugger-object] instance, SpiderMonkey creates
9 only one `Debugger.Frame` instance for a given visible frame. Every handler
10 method called while the debuggee is running in a given frame is given the
11 same frame object. Similarly, walking the stack back to a previously
12 accessed frame yields the same frame object as before. Debugger code can
13 add its own properties to a frame object and expect to find them later, use
14 `==` to decide whether two expressions refer to the same frame, and so on.
15
16 (If more than one [`Debugger`][debugger-object] instance is debugging the
17 same code, each [`Debugger`][debugger-object] gets a separate
18 `Debugger.Frame` instance for a given frame. This allows the code using
19 each [`Debugger`][debugger-object] instance to place whatever properties it
20 likes on its `Debugger.Frame` instances, without worrying about interfering
21 with other debuggers.)
22
23 When the debuggee pops a stack frame (say, because a function call has
24 returned or an exception has been thrown from it), the `Debugger.Frame`
25 instance referring to that frame becomes inactive: its `onStack` property
26 becomes `false`, and accessing its many properties or calling its methods
27 throws an exception. Note that frames only become inactive at times that
28 are predictable for the debugger: when the debuggee runs, or when the
29 debugger removes frames from the stack itself.
30
31
32 ## Visible Frames
33
34 When inspecting the call stack, [`Debugger`][debugger-object] does not
35 reveal all the frames that are actually present on the stack: while it does
36 reveal all frames running debuggee code, it omits frames running the
37 debugger's own code, and omits most frames running non-debuggee code. We
38 call those stack frames a [`Debugger`][debugger-object] does reveal
39 <i>visible frames</i>.
40
41 A frame is a visible frame if any of the following are true:
42
43 * it is running [debuggee code][dbg code];
44
45 * its immediate caller is a frame running debuggee code; or
46
47 * it is a [`"debugger"` frame][inv fr],
48 representing the continuation of debuggee code invoked by the debugger.
49
50 The "immediate caller" rule means that, when debuggee code calls a
51 non-debuggee function, it looks like a call to a primitive: you see a frame
52 for the non-debuggee function that was accessible to the debuggee, but any
53 further calls that function makes are treated as internal details, and
54 omitted from the stack trace. If the non-debuggee function eventually calls
55 back into debuggee code, then those frames are visible.
56
57 (Note that the debuggee is not considered an "immediate caller" of handler
58 methods it triggers. Even though the debuggee and debugger share the same
59 JavaScript stack, frames pushed for SpiderMonkey's calls to handler methods
60 to report events in the debuggee are never considered visible frames.)
61
62
63 ## Invocation Functions and "debugger" Frames
64
65 An <i>invocation function</i> is any function in this interface that allows
66 the debugger to invoke code in the debuggee:
67 `Debugger.Object.prototype.call`, `Debugger.Frame.prototype.eval`, and so
68 on.
69
70 While invocation functions differ in the code to be run and how to pass
71 values to it, they all follow this general procedure:
72
73 1. Let <i>older</i> be the youngest visible frame on the stack, or `null`
74 if there is no such frame. (This is never one of the the debugger's own
75 frames; those never appear as `Debugger.Frame` instances.)
76
77 2. Push a `"debugger"` frame on the stack, with <i>older</i> as its
78 `older` property.
79
80 3. Invoke the debuggee code as appropriate for the given invocation
81 function, with the `"debugger"` frame as its continuation. For example,
82 `Debugger.Frame.prototype.eval` pushes an `"eval"` frame for code it
83 runs, whereas `Debugger.Object.prototype.call` pushes a `"call"` frame.
84
85 4. When the debuggee code completes, whether by returning, throwing an
86 exception or being terminated, pop the `"debugger"` frame, and return an
87 appropriate [completion value][cv] from the invocation function to the
88 debugger.
89
90 When a debugger calls an invocation function to run debuggee code, that
91 code's continuation is the debugger, not the next debuggee code frame.
92 Pushing a `"debugger"` frame makes this continuation explicit, and makes it
93 easier to find the extent of the stack created for the invocation.
94
95
96 ## Suspended Frames
97
98 Some frames can be *suspended*.
99
100 When a generator `yield`s a value, or when an async function `await`s a
101 value, the current frame is suspended and removed from the stack, and
102 other JS code has a chance to run. Later (if the `await`ed promise
103 becomes resolved, for example), SpiderMonkey will *resume* the frame. It
104 will be put back onto the stack, and execution will continue where it
105 left off. Only generator and async function call frames can be suspended
106 and resumed.
107
108 Currently, a frame's `onStack` property is `false` while it's suspended
109 ([bug 1448880](https://bugzilla.mozilla.org/show_bug.cgi?id=1448880)).
110
111 SpiderMonkey uses the same `Debugger.Frame` object each time a generator
112 or async function call is put back onto the stack. This means that the
113 `onStep` handler can be used to step over `yield` and `await`.
114
115 The `frame.onPop` handler is called each time a frame is suspended, and
116 the `Debugger.onEnterFrame` handler is called each time a frame is
117 resumed. (This means these events can fire multiple times for the same
118 `Frame` object, which is odd, but accurately conveys what's happening.)
119
120 The [completion value][cv] passed to the `frame.onPop` handler for a suspension
121 contains additional properties to clarify what's going on. See the documentation
122 for completion values for details.
123
124
125 ## Stepping Into Generators: The "Initial Yield"
126
127 When a debuggee generator is called, something weird happens. The
128 `.onEnterFrame` hook fires, as though we're stepping into the generator.
129 But the code inside the generator doesn't run. Instead it immediately
130 returns. Then we sometimes get *another* `.onEnterFrame` event for the
131 same generator. What's going on?
132
133 To explain this, we first have to describe how generator calls work,
134 according to the ECMAScript language specification. Note that except for
135 step 3, it's exactly like a regular function call.
136
137 1. An "execution context" (what we call a `Frame`) is pushed to the stack.
138 2. An environment is created (for arguments and local variables).
139 Argument-default-value-expressions, if any, are evaluated.
140 3. A generator object is created, initially suspended at the start of the generator body.
141 4. The stack frame is popped, and the generator object is returned to the caller.
142
143 The JavaScript engine actually carries out these steps, in this order.
144 So when a debuggee generator is called, here's what you'll observe:
145
146 1. The `debugger.onEnterFrame` hook fires.
147 2. The debugger can step through the argument-default-value code, if any.
148 3. The body of the generator does not run yet. Instead, a generator object
149 is created and suspended (which does not fire any debugger events).
150 4. The `frame.onPop` hook fires, with a completion value of
151 `{return:` *(the new generator object)* `}`.
152
153 In SpiderMonkey, this process of suspending and returning a new
154 generator object is called the "initial yield".
155
156 If the caller then uses the generator's `.next()` method, which may or
157 may not happen right away depending on the debuggee code, the suspended
158 generator will be resumed, firing `.onEnterFrame` again.
159
160 ## Accessor Properties of the Debugger.Frame Prototype Object
161
162 A `Debugger.Frame` instance inherits the following accessor properties from
163 its prototype:
164
165 ### `type`
166
167 A string describing what sort of frame this is:
168
169 * `"call"`: a frame running a function call. (We may not be able to obtain
170 frames for calls to host functions.)
171
172 * `"eval"`: a frame running code passed to `eval`.
173
174 * `"global"`: a frame running global code (JavaScript that is neither of
175 the above).
176
177 * `"module"`: a frame running code at the top level of a module.
178
179 * `"wasmcall"`: a frame running a WebAssembly function call.
180
181 * `"debugger"`: a frame for a call to user code invoked by the debugger
182 (see the `eval` method below).
183
184 Accessing this property will throw if `.terminated == true`.
185
186 ### `implementation`
187 A string describing which tier of the JavaScript engine this frame is
188 executing in:
189
190 * `"interpreter"`: a frame running in the interpreter.
191
192 * `"baseline"`: a frame running in the unoptimizing, baseline JIT.
193
194 * `"ion"`: a frame running in the optimizing JIT.
195
196 * `"wasm"`: a frame running in WebAssembly baseline JIT.
197
198 Accessing this property will throw if `.onStack == false`.
199
200 ### `this`
201 The value of `this` for this frame (a debuggee value). For a `wasmcall`
202 frame, this property throws a `TypeError`.
203
204 Accessing this property will throw if `.terminated == true`.
205
206 ### `older`
207 The `Debugger.Frame` for the next-older visible frame, in which control will
208 resume when this frame completes. If there is no older frame, this is `null`.
209 If there an explicitly inserted asynchronous stack trace above this frame,
210 this is `null`, since the explicit saved frame takes priority.
211 If this frame is a suspended generator or async call, this will also be `null`.
212
213 Accessing this property will throw if `.terminated == true`.
214
215 ### `olderSavedFrame`
216
217 If this frame has no `older` frame, this field may hold a [`SavedFrame`][saved-frame]
218 object representing the saved asynchronous stack that triggered execution of
219 this `Debugger.Frame` instance.
220
221 Accessing this property will throw if `.terminated == true`.
222
223 ### `onStack`
224 True if the frame this `Debugger.Frame` instance refers to is still on
225 the stack; false if it has completed execution or been popped in some other way.
226 Note that this property may be accessed regardless of what state the frame is
227 in, so it can be used to verify whether it is safe to access other properties
228 that require an on-stack frame.
229
230 ### `terminated`
231 True if the frame this `Debugger.Frame` instance refers to will never run
232 again; false if it is on-stack or is a suspended generator/async call that
233 may be resumed later. Note that this property may be accessed regardless of
234 what state the frame is in, so it can be used to verify whether it is safe to
235 access other properties that require a non-terminated frame.
236
237 ### `script`
238 The script being executed in this frame (a [`Debugger.Script`][script]
239 instance), or `null` on frames that do not represent calls to debuggee
240 code. On frames whose `callee` property is not null, this is equal to
241 `callee.script`.
242
243 Accessing this property will throw if `.terminated == true`.
244
245 ### `offset`
246 The offset of the bytecode instruction currently being executed in
247 `script`, or `undefined` if the frame's `script` property is `null`.
248 For a `wasmcall` frame, this property throws a `TypeError`.
249
250 If this is used on a suspended function frame, the offset will reference
251 the offset where the frame will be resumed.
252
253 Accessing this property will throw if `.terminated == true`.
254
255 ### `environment`
256 The lexical environment within which evaluation is taking place (a
257 [`Debugger.Environment`][environment] instance), or `null` on frames
258 that do not represent the evaluation of debuggee code, like calls
259 non-debuggee functions, host functions or `"debugger"` frames.
260
261 Accessing this property will throw if `.terminated == true`.
262
263 ### `callee`
264 The function whose application created this frame, as a debuggee value,
265 or `null` if this is not a `"call"` frame.
266
267 Accessing this property will throw if `.terminated == true`.
268
269 ### `constructing`
270 True if this frame is for a function called as a constructor, false
271 otherwise.
272
273 Accessing this property will throw if `.terminated == true`.
274
275 ### `arguments`
276 The arguments passed to the current frame, or `null` if this is not a
277 `"call"` frame. When non-`null`, this is an object, allocated in the
278 same global as the debugger, with `Array.prototype` on its prototype
279 chain, a non-writable `length` property, and properties whose names are
280 array indices. Each property is a read-only accessor property whose
281 getter returns the current value of the corresponding parameter. When
282 the referent frame is popped, the argument value's properties' getters
283 throw an error.
284
285 Accessing this property will throw if `.onStack == false`.
286
287 ### `asyncPromise`
288
289 If the frame is not an async (generator) function, this will be `undefined`.
290
291 For async functions, this will be a [`Debugger.Object`][object] whose referent
292 is the promise for async function call's return value. Note that this
293 property will be `null` if the value is accessed during `onEnterFrame`,
294 since the promise doesn't exist yet at that point.
295
296 For async generator functions, this will be a [`Debugger.Object`][object]
297 whose referent is the promise for the current iteration's "value"+"done" object,
298 which will be resolved when the generator next throws/yields/returns.
299 Note that this will be `null` if the value is accessed during the initial
300 generator `onEnterFrame`/`onPop` (before the first `.next` call),
301 since there is no promise yet at that point.
302
303 Accessing this property will throw if `.terminated == true`.
304
305
306 ## Handler Methods of Debugger.Frame Instances
307
308 Each `Debugger.Frame` instance inherits accessor properties holding handler
309 functions for SpiderMonkey to call when given events occur in the frame.
310
311 Calls to frames' handler methods are cross-compartment, intra-thread calls:
312 the call takes place in the thread to which the frame belongs, and runs in
313 the compartment to which the handler method belongs.
314
315 `Debugger.Frame` instances inherit the following handler method properties:
316
317 ### `onStep`
318 This property must be either `undefined` or a function. If it is a
319 function, SpiderMonkey calls it when execution in this frame makes a
320 small amount of progress, passing no arguments and providing this
321 `Debugger.Frame` instance as the `this`value. The function should
322 return a [resumption value][rv] specifying how the debuggee's execution
323 should proceed.
324
325 What constitutes "a small amount of progress" varies depending on the
326 implementation, but it is fine-grained enough to implement useful
327 "step" and "next" behavior.
328
329 If multiple [`Debugger`][debugger-object] instances each have
330 `Debugger.Frame` instances for a given stack frame with `onStep`
331 handlers set, their handlers are run in an unspecified order. If any
332 `onStep` handler forces the frame to return early (by returning a
333 resumption value other than `undefined`), any remaining debuggers'
334 `onStep` handlers do not run.
335
336 This property is ignored on frames that are not executing debuggee
337 code, like `"call"` frames for calls to host functions and `"debugger"`
338 frames.
339
340 Accessing and reassigning this property is allowed independent of whether
341 or not the frame is currently on-stack/suspended/terminated.
342
343 ### `onPop`
344 This property must be either `undefined` or a function. If it is a function,
345 SpiderMonkey calls it just before this frame is popped or suspended, passing
346 a [completion value][cv] indicating the reason, and providing this
347 `Debugger.Frame` instance as the `this` value. The function should return a
348 [resumption value][rv] indicating how execution should proceed. On newly
349 created frames, this property's value is `undefined`.
350
351 When this handler is called, this frame's current execution location, as
352 reflected in its `offset` and `environment` properties, is the operation
353 which caused it to be unwound. In frames returning or throwing an exception,
354 the location is often a return or a throw statement. In frames propagating
355 exceptions, the location is a call. In generator or async function frames,
356 the location may be a `yield` or `await` expression.
357
358 When an `onPop` call reports the completion of a construction call
359 (that is, a function called via the `new` operator), the completion
360 value passed to the handler describes the value returned by the
361 function body. If this value is not an object, it may be different from
362 the value produced by the `new` expression, which will be the value of
363 the frame's `this` property. (In ECMAScript terms, the `onPop` handler
364 receives the value returned by the `[[Call]]` method, not the value
365 returned by the `[[Construct]]` method.)
366
367 When a debugger handler function forces a frame to complete early, by
368 returning a `{ return:... }`, `{ throw:... }`, or `null` resumption
369 value, SpiderMonkey calls the frame's `onPop` handler, if any. The
370 completion value passed in this case reflects the resumption value that
371 caused the frame to complete.
372
373 When SpiderMonkey calls an `onPop` handler for a frame that is throwing
374 an exception or being terminated, and the handler returns `undefined`,
375 then SpiderMonkey proceeds with the exception or termination. That is,
376 an `undefined` resumption value leaves the frame's throwing and
377 termination process undisturbed.
378
379 If multiple [`Debugger`][debugger-object] instances each have
380 `Debugger.Frame` instances for a given stack frame with `onPop`
381 handlers set, their handlers are run in an unspecified order. The
382 resumption value each handler returns establishes the completion value
383 reported to the next handler.
384
385 The `onPop` handler is typically called only once for a given
386 `Debugger.Frame`, after which the frame becomes inactive. However, in the
387 case of [generators and async functions](#suspended-frames), `onPop` fires
388 each time the frame is suspended.
389
390 This handler is not called on `"debugger"` frames. It is also not called
391 when unwinding a frame due to an over-recursion or out-of-memory
392 exception.
393
394 Accessing and reassigning this property is allowed independent of whether
395 or not the frame is currently on-stack/suspended/terminated.
396
397
398 ## Function Properties of the Debugger.Frame Prototype Object
399
400 The functions described below may only be called with a `this` value
401 referring to a `Debugger.Frame` instance; they may not be used as
402 methods of other kinds of objects.
403
404 ### `eval(code, [options])`
405 Evaluate <i>code</i> in the execution context of this frame, and return
406 a [completion value][cv] describing how it completed. <i>Code</i> is a
407 string. If this frame's `environment` property is `null` or `type` property
408 is `wasmcall`, throw a `TypeError`. All extant handler methods, breakpoints,
409 and so on remain active during the call. This function follows the
410 [invocation function conventions][inv fr].
411
412 <i>Code</i> is interpreted as strict mode code when it contains a Use
413 Strict Directive, or the code executing in this frame is strict mode
414 code.
415
416 If <i>code</i> is not strict mode code, then variable declarations in
417 <i>code</i> affect the environment of this frame. (In the terms used by
418 the ECMAScript specification, the `VariableEnvironment` of the
419 execution context for the eval code is the `VariableEnvironment` of the
420 execution context that this frame represents.) If implementation
421 restrictions prevent SpiderMonkey from extending this frame's
422 environment as requested, this call throws an Error exception.
423
424 If given, <i>options</i> should be an object whose properties specify
425 details of how the evaluation should occur. The `eval` method
426 recognizes the following properties:
427
428 * `url`
429
430 The filename or URL to which we should attribute <i>code</i>. If this
431 property is omitted, the URL defaults to `"debugger eval code"`.
432
433 * `lineNumber`
434
435 The line number at which the evaluated code should be claimed to begin
436 within <i>url</i>.
437
438 Accessing this property will throw if `.onStack == false`.
439
440 ### `evalWithBindings(code, bindings, [options])`
441 Like `eval`, but evaluate <i>code</i> in the environment of this frame,
442 extended with bindings from the object <i>bindings</i>. For each own
443 enumerable property of <i>bindings</i> named <i>name</i> whose value is
444 <i>value</i>, include a variable in the environment in which
445 <i>code</i> is evaluated named <i>name</i>, whose value is
446 <i>value</i>. Each <i>value</i> must be a debuggee value. (This is not
447 like a `with` statement: <i>code</i> may access, assign to, and delete
448 the introduced bindings without having any effect on the
449 <i>bindings</i> object.)
450
451 This method allows debugger code to introduce temporary bindings that
452 are visible to the given debuggee code and which refer to debugger-held
453 debuggee values, and do so without mutating any existing debuggee
454 environment.
455
456 Note that, like `eval`, declarations in the <i>code</i> passed to
457 `evalWithBindings` affect the environment of this frame, even as that
458 environment is extended by bindings visible within <i>code</i>. (In the
459 terms used by the ECMAScript specification, the `VariableEnvironment`
460 of the execution context for the eval code is the `VariableEnvironment`
461 of the execution context that this frame represents, and the
462 <i>bindings</i> appear in a new declarative environment, which is the
463 eval code's `LexicalEnvironment`.) If implementation restrictions
464 prevent SpiderMonkey from extending this frame's environment as
465 requested, this call throws an `Error` exception.
466
467 The <i>options</i> argument is as for
468 [`Debugger.Frame.prototype.eval`][fr eval], described above.
469 Also like `eval`, if this frame's `environment` property is `null` or
470 `type` property is `wasmcall`, throw a `TypeError`.
471
472 Note: If this method is called on an object whose owner
473 [Debugger object][debugger-object] has an onNativeCall handler, only hooks
474 on objects associated with that debugger will be called during the evaluation.
475
476 Accessing this property will throw if `.onStack == false`.
477
478
479 [vf]: #visible-frames
480 [debugger-object]: Debugger.md
481 [object]: Debugger.Object.md
482 [dbg code]: Conventions.md#debuggee-code
483 [inv fr]: #invocation-functions-and-debugger-frames
484 [cv]: Conventions.md#completion-values
485 [script]: Debugger.Script.md
486 [environment]: Debugger.Environment.md
487 [rv]: Conventions.md#resumption-values
488 [fr eval]: #eval-code-options
489 [saved-frame]: ../SavedFrame/index