| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#ifndef debugger_Debugger_h |
| 8 |
#define debugger_Debugger_h |
| 9 |
|
| 10 |
#include "mozilla/Assertions.h" // for MOZ_ASSERT_HELPER1 |
| 11 |
#include "mozilla/Attributes.h" // for MOZ_RAII |
| 12 |
#include "mozilla/DoublyLinkedList.h" // for DoublyLinkedListElement |
| 13 |
#include "mozilla/HashTable.h" // for HashSet, DefaultHasher (ptr only) |
| 14 |
#include "mozilla/LinkedList.h" // for LinkedList (ptr only) |
| 15 |
#include "mozilla/Maybe.h" // for Maybe, Nothing |
| 16 |
#include "mozilla/Range.h" // for Range |
| 17 |
#include "mozilla/Result.h" // for Result |
| 18 |
#include "mozilla/TimeStamp.h" // for TimeStamp |
| 19 |
#include "mozilla/Variant.h" // for Variant |
| 20 |
|
| 21 |
#include <stddef.h> // for size_t |
| 22 |
#include <stdint.h> // for uint32_t, uint64_t, uintptr_t |
| 23 |
#include <utility> // for std::move |
| 24 |
|
| 25 |
#include "jstypes.h" // for JS_GC_ZEAL |
| 26 |
#include "NamespaceImports.h" // for Value, HandleObject |
| 27 |
|
| 28 |
#include "debugger/DebugAPI.h" // for DebugAPI |
| 29 |
#include "debugger/Object.h" // for DebuggerObject |
| 30 |
#include "ds/TraceableFifo.h" // for TraceableFifo |
| 31 |
#include "gc/Barrier.h" // |
| 32 |
#include "gc/Tracer.h" // for TraceNullableEdge, TraceEdge |
| 33 |
#include "gc/WeakMap.h" // for WeakMap |
| 34 |
#include "gc/ZoneAllocator.h" // for ZoneAllocPolicy |
| 35 |
#include "js/Debug.h" // JS_DefineDebuggerObject |
| 36 |
#include "js/GCAPI.h" // for GarbageCollectionEvent |
| 37 |
#include "js/GCVariant.h" // for GCVariant |
| 38 |
#include "js/Proxy.h" // for PropertyDescriptor |
| 39 |
#include "js/RootingAPI.h" // for Handle |
| 40 |
#include "js/TracingAPI.h" // for TraceRoot |
| 41 |
#include "js/Wrapper.h" // for UncheckedUnwrap |
| 42 |
#include "proxy/DeadObjectProxy.h" // for IsDeadProxyObject |
| 43 |
#include "vm/GeneratorObject.h" // for AbstractGeneratorObject |
| 44 |
#include "vm/GlobalObject.h" // for GlobalObject |
| 45 |
#include "vm/JSContext.h" // for JSContext |
| 46 |
#include "vm/JSObject.h" // for JSObject |
| 47 |
#include "vm/JSScript.h" // for JSScript, ScriptSourceObject |
| 48 |
#include "vm/NativeObject.h" // for NativeObject |
| 49 |
#include "vm/Runtime.h" // for JSRuntime |
| 50 |
#include "vm/SavedFrame.h" // for SavedFrame |
| 51 |
#include "vm/Stack.h" // for AbstractFramePtr, FrameIter |
| 52 |
#include "vm/StringType.h" // for JSAtom |
| 53 |
#include "wasm/WasmJS.h" // for WasmInstanceObject |
| 54 |
|
| 55 |
class JS_PUBLIC_API JSFunction; |
| 56 |
|
| 57 |
namespace JS { |
| 58 |
class JS_PUBLIC_API AutoStableStringChars; |
| 59 |
class JS_PUBLIC_API Compartment; |
| 60 |
class JS_PUBLIC_API Realm; |
| 61 |
class JS_PUBLIC_API Zone; |
| 62 |
} /* namespace JS */ |
| 63 |
|
| 64 |
namespace js { |
| 65 |
class AutoRealm; |
| 66 |
class CrossCompartmentKey; |
| 67 |
class Debugger; |
| 68 |
class DebuggerEnvironment; |
| 69 |
class PromiseObject; |
| 70 |
namespace gc { |
| 71 |
struct Cell; |
| 72 |
} /* namespace gc */ |
| 73 |
namespace wasm { |
| 74 |
class Instance; |
| 75 |
} /* namespace wasm */ |
| 76 |
} /* namespace js */ |
| 77 |
|
| 78 |
/* |
| 79 |
* Windows 3.x used a cooperative multitasking model, with a Yield macro that |
| 80 |
* let you relinquish control to other cooperative threads. Microsoft replaced |
| 81 |
* it with an empty macro long ago. We should be free to use it in our code. |
| 82 |
*/ |
| 83 |
#undef Yield |
| 84 |
|
| 85 |
namespace js { |
| 86 |
|
| 87 |
class Breakpoint; |
| 88 |
class DebuggerFrame; |
| 89 |
class DebuggerScript; |
| 90 |
class DebuggerSource; |
| 91 |
class DebuggerMemory; |
| 92 |
class ScriptedOnStepHandler; |
| 93 |
class ScriptedOnPopHandler; |
| 94 |
class DebuggerDebuggeeLink; |
| 95 |
|
| 96 |
/** |
| 97 |
* Tells how the JS engine should resume debuggee execution after firing a |
| 98 |
* debugger hook. Most debugger hooks get to choose how the debuggee proceeds; |
| 99 |
* see js/src/doc/Debugger/Conventions.md under "Resumption Values". |
| 100 |
* |
| 101 |
* Debugger::processHandlerResult() translates between JavaScript values and |
| 102 |
* this enum. |
| 103 |
*/ |
| 104 |
enum class ResumeMode { |
| 105 |
/** |
| 106 |
* The debuggee should continue unchanged. |
| 107 |
* |
| 108 |
* This corresponds to a resumption value of `undefined`. |
| 109 |
*/ |
| 110 |
Continue, |
| 111 |
|
| 112 |
/** |
| 113 |
* Throw an exception in the debuggee. |
| 114 |
* |
| 115 |
* This corresponds to a resumption value of `{throw: <value>}`. |
| 116 |
*/ |
| 117 |
Throw, |
| 118 |
|
| 119 |
/** |
| 120 |
* Terminate the debuggee, as if it had been cancelled via the "slow |
| 121 |
* script" ribbon. |
| 122 |
* |
| 123 |
* This corresponds to a resumption value of `null`. |
| 124 |
*/ |
| 125 |
Terminate, |
| 126 |
|
| 127 |
/** |
| 128 |
* Force the debuggee to return from the current frame. |
| 129 |
* |
| 130 |
* This corresponds to a resumption value of `{return: <value>}`. |
| 131 |
*/ |
| 132 |
Return, |
| 133 |
}; |
| 134 |
|
| 135 |
/** |
| 136 |
* A completion value, describing how some sort of JavaScript evaluation |
| 137 |
* completed. This is used to tell an onPop handler what's going on with the |
| 138 |
* frame, and to report the outcome of call, apply, setProperty, and getProperty |
| 139 |
* operations. |
| 140 |
* |
| 141 |
* Local variables of type Completion should be held in Rooted locations, |
| 142 |
* and passed using Handle and MutableHandle. |
| 143 |
*/ |
| 144 |
class Completion { |
| 145 |
public: |
| 146 |
struct Return { |
| 147 |
explicit Return(const Value& value) : value(value) {} |
| 148 |
Value value; |
| 149 |
|
| 150 |
void trace(JSTracer* trc) { |
| 151 |
JS::TraceRoot(trc, &value, "js::Completion::Return::value"); |
| 152 |
} |
| 153 |
}; |
| 154 |
|
| 155 |
struct Throw { |
| 156 |
Throw(const Value& exception, SavedFrame* stack) |
| 157 |
: exception(exception), stack(stack) {} |
| 158 |
Value exception; |
| 159 |
SavedFrame* stack; |
| 160 |
|
| 161 |
void trace(JSTracer* trc) { |
| 162 |
JS::TraceRoot(trc, &exception, "js::Completion::Throw::exception"); |
| 163 |
JS::TraceRoot(trc, &stack, "js::Completion::Throw::stack"); |
| 164 |
} |
| 165 |
}; |
| 166 |
|
| 167 |
struct Terminate { |
| 168 |
void trace(JSTracer* trc) {} |
| 169 |
}; |
| 170 |
|
| 171 |
struct InitialYield { |
| 172 |
explicit InitialYield(AbstractGeneratorObject* generatorObject) |
| 173 |
: generatorObject(generatorObject) {} |
| 174 |
AbstractGeneratorObject* generatorObject; |
| 175 |
|
| 176 |
void trace(JSTracer* trc) { |
| 177 |
JS::TraceRoot(trc, &generatorObject, |
| 178 |
"js::Completion::InitialYield::generatorObject"); |
| 179 |
} |
| 180 |
}; |
| 181 |
|
| 182 |
struct Yield { |
| 183 |
Yield(AbstractGeneratorObject* generatorObject, const Value& iteratorResult) |
| 184 |
: generatorObject(generatorObject), iteratorResult(iteratorResult) {} |
| 185 |
AbstractGeneratorObject* generatorObject; |
| 186 |
Value iteratorResult; |
| 187 |
|
| 188 |
void trace(JSTracer* trc) { |
| 189 |
JS::TraceRoot(trc, &generatorObject, |
| 190 |
"js::Completion::Yield::generatorObject"); |
| 191 |
JS::TraceRoot(trc, &iteratorResult, |
| 192 |
"js::Completion::Yield::iteratorResult"); |
| 193 |
} |
| 194 |
}; |
| 195 |
|
| 196 |
struct Await { |
| 197 |
Await(AbstractGeneratorObject* generatorObject, const Value& awaitee) |
| 198 |
: generatorObject(generatorObject), awaitee(awaitee) {} |
| 199 |
AbstractGeneratorObject* generatorObject; |
| 200 |
Value awaitee; |
| 201 |
|
| 202 |
void trace(JSTracer* trc) { |
| 203 |
JS::TraceRoot(trc, &generatorObject, |
| 204 |
"js::Completion::Await::generatorObject"); |
| 205 |
JS::TraceRoot(trc, &awaitee, "js::Completion::Await::awaitee"); |
| 206 |
} |
| 207 |
}; |
| 208 |
|
| 209 |
// The JS::Result macros want to assign to an existing variable, so having a |
| 210 |
// default constructor is handy. |
| 211 |
Completion() : variant(Terminate()) {} |
| 212 |
|
| 213 |
// Construct a completion from a specific variant. |
| 214 |
// |
| 215 |
// Unfortunately, using a template here would prevent the implicit definitions |
| 216 |
// of the copy and move constructor and assignment operators, which is icky. |
| 217 |
explicit Completion(Return&& variant) |
| 218 |
: variant(std::forward<Return>(variant)) {} |
| 219 |
explicit Completion(Throw&& variant) |
| 220 |
: variant(std::forward<Throw>(variant)) {} |
| 221 |
explicit Completion(Terminate&& variant) |
| 222 |
: variant(std::forward<Terminate>(variant)) {} |
| 223 |
explicit Completion(InitialYield&& variant) |
| 224 |
: variant(std::forward<InitialYield>(variant)) {} |
| 225 |
explicit Completion(Yield&& variant) |
| 226 |
: variant(std::forward<Yield>(variant)) {} |
| 227 |
explicit Completion(Await&& variant) |
| 228 |
: variant(std::forward<Await>(variant)) {} |
| 229 |
|
| 230 |
// Capture a JavaScript operation result as a Completion value. This clears |
| 231 |
// any exception and stack from cx, taking ownership of them itself. |
| 232 |
static Completion fromJSResult(JSContext* cx, bool ok, const Value& rv); |
| 233 |
|
| 234 |
// Construct a completion given an AbstractFramePtr that is being popped. This |
| 235 |
// clears any exception and stack from cx, taking ownership of them itself. |
| 236 |
static Completion fromJSFramePop(JSContext* cx, AbstractFramePtr frame, |
| 237 |
const jsbytecode* pc, bool ok); |
| 238 |
|
| 239 |
template <typename V> |
| 240 |
bool is() const { |
| 241 |
return variant.template is<V>(); |
| 242 |
} |
| 243 |
|
| 244 |
template <typename V> |
| 245 |
V& as() { |
| 246 |
return variant.template as<V>(); |
| 247 |
} |
| 248 |
|
| 249 |
template <typename V> |
| 250 |
const V& as() const { |
| 251 |
return variant.template as<V>(); |
| 252 |
} |
| 253 |
|
| 254 |
void trace(JSTracer* trc); |
| 255 |
|
| 256 |
/* True if this completion is a suspension of a generator or async call. */ |
| 257 |
bool suspending() const { |
| 258 |
return variant.is<InitialYield>() || variant.is<Yield>() || |
| 259 |
variant.is<Await>(); |
| 260 |
} |
| 261 |
|
| 262 |
/* Set `result` to a Debugger API completion value describing this completion. |
| 263 |
*/ |
| 264 |
bool buildCompletionValue(JSContext* cx, Debugger* dbg, |
| 265 |
MutableHandleValue result) const; |
| 266 |
|
| 267 |
/* |
| 268 |
* Set `resumeMode`, `value`, and `exnStack` to values describing this |
| 269 |
* completion. |
| 270 |
*/ |
| 271 |
void toResumeMode(ResumeMode& resumeMode, MutableHandleValue value, |
| 272 |
MutableHandle<SavedFrame*> exnStack) const; |
| 273 |
/* |
| 274 |
* Given a `ResumeMode` and value (typically derived from a resumption value |
| 275 |
* returned by a Debugger hook), update this completion as requested. |
| 276 |
*/ |
| 277 |
void updateFromHookResult(ResumeMode resumeMode, HandleValue value); |
| 278 |
|
| 279 |
private: |
| 280 |
using Variant = |
| 281 |
mozilla::Variant<Return, Throw, Terminate, InitialYield, Yield, Await>; |
| 282 |
struct BuildValueMatcher; |
| 283 |
struct ToResumeModeMatcher; |
| 284 |
|
| 285 |
Variant variant; |
| 286 |
}; |
| 287 |
|
| 288 |
using WeakGlobalObjectSet = |
| 289 |
HashSet<WeakHeapPtr<GlobalObject*>, |
| 290 |
StableCellHasher<WeakHeapPtr<GlobalObject*>>, ZoneAllocPolicy>; |
| 291 |
|
| 292 |
#ifdef DEBUG |
| 293 |
extern void CheckDebuggeeThing(BaseScript* script, bool invisibleOk); |
| 294 |
|
| 295 |
extern void CheckDebuggeeThing(JSObject* obj, bool invisibleOk); |
| 296 |
#endif |
| 297 |
|
| 298 |
/* |
| 299 |
* [SMDOC] Cross-compartment weakmap entries for Debugger API objects |
| 300 |
* |
| 301 |
* The Debugger API creates objects like Debugger.Object, Debugger.Script, |
| 302 |
* Debugger.Environment, etc. to refer to things in the debuggee. Each Debugger |
| 303 |
* gets at most one Debugger.Mumble for each referent: Debugger.Mumbles are |
| 304 |
* unique per referent per Debugger. This is accomplished by storing the |
| 305 |
* debugger objects in a DebuggerWeakMap, using the debuggee thing as the key. |
| 306 |
* |
| 307 |
* Since a Debugger and its debuggee must be in different compartments, a |
| 308 |
* Debugger.Mumble's pointer to its referent is a cross-compartment edge, from |
| 309 |
* the debugger's compartment into the debuggee compartment. Like any other sort |
| 310 |
* of cross-compartment edge, the GC needs to be able to find all of these edges |
| 311 |
* readily. The GC therefore consults the debugger's weakmap tables as |
| 312 |
* necessary. This allows the garbage collector to easily find edges between |
| 313 |
* debuggee object compartments and debugger compartments when calculating the |
| 314 |
* zone sweep groups. |
| 315 |
* |
| 316 |
* The current implementation results in all debuggee object compartments being |
| 317 |
* swept in the same group as the debugger. This is a conservative approach, and |
| 318 |
* compartments may be unnecessarily grouped. However this results in a simpler |
| 319 |
* and faster implementation. |
| 320 |
*/ |
| 321 |
|
| 322 |
/* |
| 323 |
* A weakmap from GC thing keys to JSObject values that supports the keys being |
| 324 |
* in different compartments to the values. All values must be in the same |
| 325 |
* compartment. |
| 326 |
* |
| 327 |
* If InvisibleKeysOk is true, then the map can have keys in invisible-to- |
| 328 |
* debugger compartments. If it is false, we assert that such entries are never |
| 329 |
* created. |
| 330 |
* |
| 331 |
* Note that keys in these weakmaps can be in any compartment, debuggee or not, |
| 332 |
* because they are not deleted when a compartment is no longer a debuggee: the |
| 333 |
* values need to maintain object identity across add/remove/add |
| 334 |
* transitions. (Frames are an exception to the rule. Existing Debugger.Frame |
| 335 |
* objects are killed if their realm is removed as a debugger; if the realm |
| 336 |
* beacomes a debuggee again later, new Frame objects are created.) |
| 337 |
*/ |
| 338 |
template <class Referent, class Wrapper, bool InvisibleKeysOk = false> |
| 339 |
class DebuggerWeakMap : private WeakMap<Referent*, Wrapper*> { |
| 340 |
private: |
| 341 |
using Key = Referent*; |
| 342 |
using Value = Wrapper*; |
| 343 |
|
| 344 |
JS::Compartment* compartment; |
| 345 |
|
| 346 |
public: |
| 347 |
using Base = WeakMap<Key, Value>; |
| 348 |
using ReferentType = Referent; |
| 349 |
using WrapperType = Wrapper; |
| 350 |
|
| 351 |
explicit DebuggerWeakMap(JSContext* cx) |
| 352 |
: Base(cx), compartment(cx->compartment()) {} |
| 353 |
|
| 354 |
public: |
| 355 |
// Expose those parts of HashMap public interface that are used by Debugger |
| 356 |
// methods. |
| 357 |
|
| 358 |
using Entry = typename Base::Entry; |
| 359 |
using Ptr = typename Base::Ptr; |
| 360 |
using AddPtr = typename Base::AddPtr; |
| 361 |
using Range = typename Base::Range; |
| 362 |
using Lookup = typename Base::Lookup; |
| 363 |
|
| 364 |
// Expose WeakMap public interface. |
| 365 |
|
| 366 |
using Base::all; |
| 367 |
using Base::has; |
| 368 |
using Base::lookup; |
| 369 |
using Base::lookupForAdd; |
| 370 |
using Base::lookupUnbarriered; |
| 371 |
using Base::remove; |
| 372 |
using Base::trace; |
| 373 |
using Base::zone; |
| 374 |
#ifdef DEBUG |
| 375 |
using Base::hasEntry; |
| 376 |
#endif |
| 377 |
|
| 378 |
class Enum : public Base::Enum { |
| 379 |
public: |
| 380 |
explicit Enum(DebuggerWeakMap& map) : Base::Enum(map) {} |
| 381 |
}; |
| 382 |
|
| 383 |
template <typename KeyInput, typename ValueInput> |
| 384 |
bool relookupOrAdd(AddPtr& p, const KeyInput& k, const ValueInput& v) { |
| 385 |
MOZ_ASSERT(v->compartment() == this->compartment); |
| 386 |
#ifdef DEBUG |
| 387 |
CheckDebuggeeThing(k, InvisibleKeysOk); |
| 388 |
#endif |
| 389 |
MOZ_ASSERT(!Base::has(k)); |
| 390 |
bool ok = Base::relookupOrAdd(p, k, v); |
| 391 |
return ok; |
| 392 |
} |
| 393 |
|
| 394 |
public: |
| 395 |
void traceCrossCompartmentEdges(JSTracer* tracer) { |
| 396 |
for (Enum e(*this); !e.empty(); e.popFront()) { |
| 397 |
TraceEdge(tracer, &e.front().mutableKey(), "Debugger WeakMap key"); |
| 398 |
e.front().value()->trace(tracer); |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
bool findSweepGroupEdges(JS::Zone* atomsZone) override; |
| 403 |
|
| 404 |
private: |
| 405 |
#ifdef JS_GC_ZEAL |
| 406 |
// Let the weak map marking verifier know that this map can |
| 407 |
// contain keys in other zones. |
| 408 |
virtual bool allowKeysInOtherZones() const override { return true; } |
| 409 |
#endif |
| 410 |
}; |
| 411 |
|
| 412 |
class LeaveDebuggeeNoExecute; |
| 413 |
|
| 414 |
class MOZ_RAII EvalOptions { |
| 415 |
public: |
| 416 |
enum class EnvKind { |
| 417 |
Frame, |
| 418 |
FrameWithExtraBindings, |
| 419 |
Global, |
| 420 |
GlobalWithExtraOuterBindings, |
| 421 |
GlobalWithExtraInnerBindings, |
| 422 |
}; |
| 423 |
|
| 424 |
private: |
| 425 |
JS::UniqueChars filename_; |
| 426 |
unsigned lineno_ = 1; |
| 427 |
bool hideFromDebugger_ = false; |
| 428 |
EnvKind kind_; |
| 429 |
|
| 430 |
public: |
| 431 |
explicit EvalOptions(EnvKind kind) : kind_(kind) {}; |
| 432 |
~EvalOptions() = default; |
| 433 |
const char* filename() const { return filename_.get(); } |
| 434 |
unsigned lineno() const { return lineno_; } |
| 435 |
bool hideFromDebugger() const { return hideFromDebugger_; } |
| 436 |
EnvKind kind() const { return kind_; } |
| 437 |
void setUseInnerBindings() { |
| 438 |
MOZ_ASSERT(kind_ == EvalOptions::EnvKind::GlobalWithExtraOuterBindings); |
| 439 |
kind_ = EvalOptions::EnvKind::GlobalWithExtraInnerBindings; |
| 440 |
} |
| 441 |
[[nodiscard]] bool setFilename(JSContext* cx, const char* filename); |
| 442 |
void setLineno(unsigned lineno) { lineno_ = lineno; } |
| 443 |
void setHideFromDebugger(bool hide) { hideFromDebugger_ = hide; } |
| 444 |
}; |
| 445 |
|
| 446 |
/* |
| 447 |
* Env is the type of what ECMA-262 calls "lexical environments" (the records |
| 448 |
* that represent scopes and bindings). See vm/EnvironmentObject.h. |
| 449 |
* |
| 450 |
* This is JSObject rather than js::EnvironmentObject because GlobalObject and |
| 451 |
* some proxies, despite not being in the EnvironmentObject class hierarchy, |
| 452 |
* can be in environment chains. |
| 453 |
*/ |
| 454 |
using Env = JSObject; |
| 455 |
|
| 456 |
// The referent of a Debugger.Script. |
| 457 |
// |
| 458 |
// - For most scripts, we point at their BaseScript. |
| 459 |
// |
| 460 |
// - For Web Assembly instances for which we are presenting a script-like |
| 461 |
// interface, we point at their WasmInstanceObject. |
| 462 |
// |
| 463 |
// The DebuggerScript object itself simply stores a Cell* in its private |
| 464 |
// pointer, but when we're working with that pointer in C++ code, we'd rather |
| 465 |
// not pass around a Cell* and be constantly asserting that, yes, this really |
| 466 |
// does point to something okay. Instead, we immediately build an instance of |
| 467 |
// this type from the Cell* and use that instead, so we can benefit from |
| 468 |
// Variant's static checks. |
| 469 |
using DebuggerScriptReferent = |
| 470 |
mozilla::Variant<BaseScript*, WasmInstanceObject*>; |
| 471 |
|
| 472 |
// The referent of a Debugger.Source. |
| 473 |
// |
| 474 |
// - For most sources, this is a ScriptSourceObject. |
| 475 |
// |
| 476 |
// - For Web Assembly instances for which we are presenting a source-like |
| 477 |
// interface, we point at their WasmInstanceObject. |
| 478 |
// |
| 479 |
// The DebuggerSource object actually simply stores a Cell* in its private |
| 480 |
// pointer. See the comments for DebuggerScriptReferent for the rationale for |
| 481 |
// this type. |
| 482 |
using DebuggerSourceReferent = |
| 483 |
mozilla::Variant<ScriptSourceObject*, WasmInstanceObject*>; |
| 484 |
|
| 485 |
template <typename HookIsEnabledFun /* bool (Debugger*) */> |
| 486 |
class MOZ_RAII DebuggerList { |
| 487 |
private: |
| 488 |
// Note: In the general case, 'debuggers' contains references to objects in |
| 489 |
// different compartments--every compartment *except* the debugger's. |
| 490 |
RootedValueVector debuggers; |
| 491 |
HookIsEnabledFun hookIsEnabled; |
| 492 |
|
| 493 |
public: |
| 494 |
/** |
| 495 |
* The hook function will be called during `init()` to build the list of |
| 496 |
* active debuggers, and again during dispatch to validate that the hook is |
| 497 |
* still active for the given debugger. |
| 498 |
*/ |
| 499 |
DebuggerList(JSContext* cx, HookIsEnabledFun hookIsEnabled) |
| 500 |
: debuggers(cx), hookIsEnabled(hookIsEnabled) {} |
| 501 |
|
| 502 |
[[nodiscard]] bool init(JSContext* cx); |
| 503 |
|
| 504 |
bool empty() { return debuggers.empty(); } |
| 505 |
|
| 506 |
template <typename FireHookFun /* ResumeMode (Debugger*) */> |
| 507 |
bool dispatchHook(JSContext* cx, FireHookFun fireHook); |
| 508 |
|
| 509 |
template <typename FireHookFun /* void (Debugger*) */> |
| 510 |
void dispatchQuietHook(JSContext* cx, FireHookFun fireHook); |
| 511 |
|
| 512 |
template <typename FireHookFun /* bool (Debugger*, ResumeMode&, MutableHandleValue) */> |
| 513 |
[[nodiscard]] bool dispatchResumptionHook(JSContext* cx, |
| 514 |
AbstractFramePtr frame, |
| 515 |
FireHookFun fireHook); |
| 516 |
}; |
| 517 |
|
| 518 |
// The Debugger.prototype object. |
| 519 |
class DebuggerPrototypeObject : public NativeObject { |
| 520 |
public: |
| 521 |
static const JSClass class_; |
| 522 |
}; |
| 523 |
|
| 524 |
class DebuggerInstanceObject : public NativeObject { |
| 525 |
private: |
| 526 |
static const JSClassOps classOps_; |
| 527 |
|
| 528 |
public: |
| 529 |
static const JSClass class_; |
| 530 |
}; |
| 531 |
|
| 532 |
class Debugger : private mozilla::LinkedListElement<Debugger> { |
| 533 |
friend class DebugAPI; |
| 534 |
friend class Breakpoint; |
| 535 |
friend class DebuggerFrame; |
| 536 |
friend class DebuggerMemory; |
| 537 |
friend class DebuggerInstanceObject; |
| 538 |
|
| 539 |
template <typename> |
| 540 |
friend class DebuggerList; |
| 541 |
friend struct JSRuntime::GlobalObjectWatchersLinkAccess<Debugger>; |
| 542 |
friend struct JSRuntime::GarbageCollectionWatchersLinkAccess<Debugger>; |
| 543 |
friend class SavedStacks; |
| 544 |
friend class ScriptedOnStepHandler; |
| 545 |
friend class ScriptedOnPopHandler; |
| 546 |
friend class mozilla::LinkedListElement<Debugger>; |
| 547 |
friend class mozilla::LinkedList<Debugger>; |
| 548 |
friend bool(::JS_DefineDebuggerObject)(JSContext* cx, JS::HandleObject obj); |
| 549 |
friend bool(::JS::dbg::IsDebugger)(JSObject&); |
| 550 |
friend bool(::JS::dbg::GetDebuggeeGlobals)(JSContext*, JSObject&, |
| 551 |
MutableHandleObjectVector); |
| 552 |
friend bool JS::dbg::FireOnGarbageCollectionHookRequired(JSContext* cx); |
| 553 |
friend bool JS::dbg::FireOnGarbageCollectionHook( |
| 554 |
JSContext* cx, JS::dbg::GarbageCollectionEvent::Ptr&& data); |
| 555 |
|
| 556 |
public: |
| 557 |
enum Hook { |
| 558 |
OnDebuggerStatement, |
| 559 |
OnExceptionUnwind, |
| 560 |
OnNewScript, |
| 561 |
OnEnterFrame, |
| 562 |
OnNativeCall, |
| 563 |
OnNewGlobalObject, |
| 564 |
OnNewPromise, |
| 565 |
OnPromiseSettled, |
| 566 |
OnGarbageCollection, |
| 567 |
HookCount |
| 568 |
}; |
| 569 |
enum { |
| 570 |
JSSLOT_DEBUG_PROTO_START, |
| 571 |
JSSLOT_DEBUG_FRAME_PROTO = JSSLOT_DEBUG_PROTO_START, |
| 572 |
JSSLOT_DEBUG_ENV_PROTO, |
| 573 |
JSSLOT_DEBUG_OBJECT_PROTO, |
| 574 |
JSSLOT_DEBUG_SCRIPT_PROTO, |
| 575 |
JSSLOT_DEBUG_SOURCE_PROTO, |
| 576 |
JSSLOT_DEBUG_MEMORY_PROTO, |
| 577 |
JSSLOT_DEBUG_PROTO_STOP, |
| 578 |
JSSLOT_DEBUG_DEBUGGER = JSSLOT_DEBUG_PROTO_STOP, |
| 579 |
JSSLOT_DEBUG_HOOK_START, |
| 580 |
JSSLOT_DEBUG_HOOK_STOP = JSSLOT_DEBUG_HOOK_START + HookCount, |
| 581 |
JSSLOT_DEBUG_MEMORY_INSTANCE = JSSLOT_DEBUG_HOOK_STOP, |
| 582 |
JSSLOT_DEBUG_DEBUGGEE_LINK, |
| 583 |
JSSLOT_DEBUG_COUNT |
| 584 |
}; |
| 585 |
|
| 586 |
// Bring DebugAPI::IsObserving into the Debugger namespace. |
| 587 |
using IsObserving = DebugAPI::IsObserving; |
| 588 |
static const IsObserving Observing = DebugAPI::Observing; |
| 589 |
static const IsObserving NotObserving = DebugAPI::NotObserving; |
| 590 |
|
| 591 |
// Return true if the given realm is a debuggee of this debugger, |
| 592 |
// false otherwise. |
| 593 |
bool isDebuggeeUnbarriered(const Realm* realm) const; |
| 594 |
|
| 595 |
// Return true if this Debugger observed a debuggee that participated in the |
| 596 |
// GC identified by the given GC number. Return false otherwise. |
| 597 |
// May return false negatives if we have hit OOM. |
| 598 |
bool observedGC(uint64_t majorGCNumber) const { |
| 599 |
return observedGCs.has(majorGCNumber); |
| 600 |
} |
| 601 |
|
| 602 |
// Notify this Debugger that one or more of its debuggees is participating |
| 603 |
// in the GC identified by the given GC number. |
| 604 |
bool debuggeeIsBeingCollected(uint64_t majorGCNumber) { |
| 605 |
return observedGCs.put(majorGCNumber); |
| 606 |
} |
| 607 |
|
| 608 |
static SavedFrame* getObjectAllocationSite(JSObject& obj); |
| 609 |
|
| 610 |
struct AllocationsLogEntry { |
| 611 |
AllocationsLogEntry(HandleObject frame, mozilla::TimeStamp when, |
| 612 |
const char* className, size_t size, bool inNursery) |
| 613 |
: frame(frame), |
| 614 |
when(when), |
| 615 |
className(className), |
| 616 |
size(size), |
| 617 |
inNursery(inNursery) { |
| 618 |
MOZ_ASSERT_IF(frame, UncheckedUnwrap(frame)->is<SavedFrame>() || |
| 619 |
IsDeadProxyObject(frame)); |
| 620 |
} |
| 621 |
|
| 622 |
HeapPtr<JSObject*> frame; |
| 623 |
mozilla::TimeStamp when; |
| 624 |
const char* className; |
| 625 |
size_t size; |
| 626 |
bool inNursery; |
| 627 |
|
| 628 |
void trace(JSTracer* trc) { |
| 629 |
TraceNullableEdge(trc, &frame, "Debugger::AllocationsLogEntry::frame"); |
| 630 |
} |
| 631 |
}; |
| 632 |
|
| 633 |
private: |
| 634 |
HeapPtr<NativeObject*> object; /* The Debugger object. Strong reference. */ |
| 635 |
WeakGlobalObjectSet |
| 636 |
debuggees; /* Debuggee globals. Cross-compartment weak references. */ |
| 637 |
JS::ZoneSet debuggeeZones; /* Set of zones that we have debuggees in. */ |
| 638 |
HeapPtr<JSObject*> uncaughtExceptionHook; /* Strong reference. */ |
| 639 |
bool allowUnobservedAsmJS; |
| 640 |
bool allowUnobservedWasm; |
| 641 |
|
| 642 |
// When this flag is true, this debugger should be the only one to have its |
| 643 |
// hooks called when it evaluates via Frame.evalWithBindings, |
| 644 |
// Object.executeInGlobalWithBindings or Object.call. |
| 645 |
bool exclusiveDebuggerOnEval; |
| 646 |
|
| 647 |
// When this flag is true, the onNativeCall hook is called with additional |
| 648 |
// arguments which are the native function call arguments and well as a |
| 649 |
// reference to the object on which the function call (if any). |
| 650 |
bool inspectNativeCallArguments; |
| 651 |
|
| 652 |
// Whether to enable code coverage on the Debuggee. |
| 653 |
bool collectCoverageInfo; |
| 654 |
|
| 655 |
// Whether to ask avoid side-effects in the native code. |
| 656 |
// See JS::dbg::ShouldAvoidSideEffects. |
| 657 |
bool shouldAvoidSideEffects; |
| 658 |
|
| 659 |
template <typename T> |
| 660 |
struct DebuggerLinkAccess { |
| 661 |
static mozilla::DoublyLinkedListElement<T>& Get(T* aThis) { |
| 662 |
return aThis->debuggerLink; |
| 663 |
} |
| 664 |
}; |
| 665 |
|
| 666 |
// List of all js::Breakpoints in this debugger. |
| 667 |
using BreakpointList = |
| 668 |
mozilla::DoublyLinkedList<js::Breakpoint, |
| 669 |
DebuggerLinkAccess<js::Breakpoint>>; |
| 670 |
BreakpointList breakpoints; |
| 671 |
|
| 672 |
// The set of GC numbers for which one or more of this Debugger's observed |
| 673 |
// debuggees participated in. |
| 674 |
using GCNumberSet = |
| 675 |
HashSet<uint64_t, DefaultHasher<uint64_t>, ZoneAllocPolicy>; |
| 676 |
GCNumberSet observedGCs; |
| 677 |
|
| 678 |
using AllocationsLog = js::TraceableFifo<AllocationsLogEntry>; |
| 679 |
|
| 680 |
AllocationsLog allocationsLog; |
| 681 |
bool trackingAllocationSites; |
| 682 |
double allocationSamplingProbability; |
| 683 |
size_t maxAllocationsLogLength; |
| 684 |
bool allocationsLogOverflowed; |
| 685 |
|
| 686 |
static const size_t DEFAULT_MAX_LOG_LENGTH = 5000; |
| 687 |
|
| 688 |
[[nodiscard]] bool appendAllocationSite(JSContext* cx, HandleObject obj, |
| 689 |
Handle<SavedFrame*> frame, |
| 690 |
mozilla::TimeStamp when); |
| 691 |
|
| 692 |
/* |
| 693 |
* Recompute the set of debuggee zones based on the set of debuggee globals. |
| 694 |
*/ |
| 695 |
void recomputeDebuggeeZoneSet(); |
| 696 |
|
| 697 |
/* |
| 698 |
* Return true if there is an existing object metadata callback for the |
| 699 |
* given global's compartment that will prevent our instrumentation of |
| 700 |
* allocations. |
| 701 |
*/ |
| 702 |
static bool cannotTrackAllocations(const GlobalObject& global); |
| 703 |
|
| 704 |
/* |
| 705 |
* Add allocations tracking for objects allocated within the given |
| 706 |
* debuggee's compartment. The given debuggee global must be observed by at |
| 707 |
* least one Debugger that is tracking allocations. |
| 708 |
*/ |
| 709 |
[[nodiscard]] static bool addAllocationsTracking( |
| 710 |
JSContext* cx, Handle<GlobalObject*> debuggee); |
| 711 |
|
| 712 |
/* |
| 713 |
* Remove allocations tracking for objects allocated within the given |
| 714 |
* global's compartment. This is a no-op if there are still Debuggers |
| 715 |
* observing this global and who are tracking allocations. |
| 716 |
*/ |
| 717 |
static void removeAllocationsTracking(GlobalObject& global); |
| 718 |
|
| 719 |
/* |
| 720 |
* Add or remove allocations tracking for all debuggees. |
| 721 |
*/ |
| 722 |
[[nodiscard]] bool addAllocationsTrackingForAllDebuggees(JSContext* cx); |
| 723 |
void removeAllocationsTrackingForAllDebuggees(); |
| 724 |
|
| 725 |
/* |
| 726 |
* If this Debugger has a onNewGlobalObject handler, then |
| 727 |
* this link is inserted into the list headed by |
| 728 |
* JSRuntime::onNewGlobalObjectWatchers. |
| 729 |
*/ |
| 730 |
mozilla::DoublyLinkedListElement<Debugger> onNewGlobalObjectWatchersLink; |
| 731 |
|
| 732 |
/* |
| 733 |
* If this Debugger has a onGarbageCollection handler, then |
| 734 |
* this link is inserted into the list headed by |
| 735 |
* JSRuntime::onGarbageCollectionWatchers. |
| 736 |
*/ |
| 737 |
mozilla::DoublyLinkedListElement<Debugger> onGarbageCollectionWatchersLink; |
| 738 |
|
| 739 |
/* |
| 740 |
* Map from stack frames that are currently on the stack to Debugger.Frame |
| 741 |
* instances. |
| 742 |
* |
| 743 |
* The keys are always live stack frames. We drop them from this map as |
| 744 |
* soon as they leave the stack (see slowPathOnLeaveFrame) and in |
| 745 |
* removeDebuggee. |
| 746 |
* |
| 747 |
* Wasm JS PI allows suspending/resuming a portion of the stack, only |
| 748 |
* frame pointers and activations are changed. The stack frames are still |
| 749 |
* live, and shall be present in the frames map if DebuggerFrame is created. |
| 750 |
* |
| 751 |
* We don't trace the keys of this map (the frames are on the stack and |
| 752 |
* thus necessarily live), but we do trace the values. It's like a WeakMap |
| 753 |
* that way, but since stack frames are not gc-things, the implementation |
| 754 |
* has to be different. |
| 755 |
*/ |
| 756 |
using FrameMap = HashMap<AbstractFramePtr, HeapPtr<DebuggerFrame*>, |
| 757 |
DefaultHasher<AbstractFramePtr>, ZoneAllocPolicy>; |
| 758 |
FrameMap frames; |
| 759 |
|
| 760 |
/* |
| 761 |
* Map from generator objects to their Debugger.Frame instances. |
| 762 |
* |
| 763 |
* When a Debugger.Frame is created for a generator frame, it is added to |
| 764 |
* this map and remains there for the lifetime of the generator, whether |
| 765 |
* that frame is on the stack at the moment or not. This is in addition to |
| 766 |
* the entry in `frames` that exists as long as the generator frame is on |
| 767 |
* the stack. |
| 768 |
* |
| 769 |
* We need to keep the Debugger.Frame object alive to deliver it to the |
| 770 |
* onEnterFrame handler on resume, and to retain onStep and onPop hooks. |
| 771 |
* |
| 772 |
* An entry is present in this table when: |
| 773 |
* - both the debuggee generator object and the Debugger.Frame object exists |
| 774 |
* - the debuggee generator object belongs to a realm that is a debuggee of |
| 775 |
* the Debugger.Frame's owner. |
| 776 |
* |
| 777 |
* regardless of whether the frame is currently suspended. (This list is |
| 778 |
* meant to explain why we update the table in the particular places where |
| 779 |
* we do so.) |
| 780 |
* |
| 781 |
* An entry in this table exists if and only if the Debugger.Frame's |
| 782 |
* GENERATOR_INFO_SLOT is set. |
| 783 |
*/ |
| 784 |
using GeneratorWeakMap = |
| 785 |
DebuggerWeakMap<AbstractGeneratorObject, DebuggerFrame>; |
| 786 |
GeneratorWeakMap generatorFrames; |
| 787 |
|
| 788 |
// An ephemeral map from BaseScript* to Debugger.Script instances. |
| 789 |
using ScriptWeakMap = DebuggerWeakMap<BaseScript, DebuggerScript>; |
| 790 |
ScriptWeakMap scripts; |
| 791 |
|
| 792 |
using BaseScriptVector = JS::GCVector<BaseScript*>; |
| 793 |
|
| 794 |
// The map from debuggee source script objects to their Debugger.Source |
| 795 |
// instances. |
| 796 |
using SourceWeakMap = |
| 797 |
DebuggerWeakMap<ScriptSourceObject, DebuggerSource, true>; |
| 798 |
SourceWeakMap sources; |
| 799 |
|
| 800 |
// The map from debuggee objects to their Debugger.Object instances. |
| 801 |
using ObjectWeakMap = DebuggerWeakMap<JSObject, DebuggerObject>; |
| 802 |
ObjectWeakMap objects; |
| 803 |
|
| 804 |
// The map from debuggee Envs to Debugger.Environment instances. |
| 805 |
using EnvironmentWeakMap = DebuggerWeakMap<JSObject, DebuggerEnvironment>; |
| 806 |
EnvironmentWeakMap environments; |
| 807 |
|
| 808 |
// The map from WasmInstanceObjects to synthesized Debugger.Script |
| 809 |
// instances. |
| 810 |
using WasmInstanceScriptWeakMap = |
| 811 |
DebuggerWeakMap<WasmInstanceObject, DebuggerScript>; |
| 812 |
WasmInstanceScriptWeakMap wasmInstanceScripts; |
| 813 |
|
| 814 |
// The map from WasmInstanceObjects to synthesized Debugger.Source |
| 815 |
// instances. |
| 816 |
using WasmInstanceSourceWeakMap = |
| 817 |
DebuggerWeakMap<WasmInstanceObject, DebuggerSource>; |
| 818 |
WasmInstanceSourceWeakMap wasmInstanceSources; |
| 819 |
|
| 820 |
class QueryBase; |
| 821 |
class ScriptQuery; |
| 822 |
class SourceQuery; |
| 823 |
class ObjectQuery; |
| 824 |
|
| 825 |
enum class FromSweep { No, Yes }; |
| 826 |
|
| 827 |
[[nodiscard]] bool addDebuggeeGlobal(JSContext* cx, |
| 828 |
Handle<GlobalObject*> obj); |
| 829 |
void removeDebuggeeGlobal(JS::GCContext* gcx, GlobalObject* global, |
| 830 |
WeakGlobalObjectSet::Enum* debugEnum, |
| 831 |
FromSweep fromSweep); |
| 832 |
|
| 833 |
/* |
| 834 |
* Handle the result of a hook that is expected to return a resumption |
| 835 |
* value <https://wiki.mozilla.org/Debugger#Resumption_Values>. This is |
| 836 |
* called when we return from a debugging hook to debuggee code. |
| 837 |
* |
| 838 |
* If `success` is false, the hook failed. If an exception is pending in |
| 839 |
* ar.context(), attempt to handle it via the uncaught exception hook, |
| 840 |
* otherwise report it to the AutoRealm's global. |
| 841 |
* |
| 842 |
* If `success` is true, there must be no exception pending in ar.context(). |
| 843 |
* `rv` may be: |
| 844 |
* |
| 845 |
* undefined - Set `resultMode` to `ResumeMode::Continue` to continue |
| 846 |
* execution normally. |
| 847 |
* |
| 848 |
* {return: value} or {throw: value} - Call unwrapDebuggeeValue to |
| 849 |
* unwrap `value`. Store the result in `vp` and set `resultMode` to |
| 850 |
* `ResumeMode::Return` or `ResumeMode::Throw`. The interpreter |
| 851 |
* will force the current frame to return or throw an exception. |
| 852 |
* |
| 853 |
* null - Set `resultMode` to `ResumeMode::Terminate` to terminate the |
| 854 |
* debuggee with an uncatchable error. |
| 855 |
* |
| 856 |
* anything else - Make a new TypeError the pending exception and |
| 857 |
* attempt to handle it with the uncaught exception handler. |
| 858 |
*/ |
| 859 |
[[nodiscard]] bool processHandlerResult( |
| 860 |
JSContext* cx, bool success, HandleValue rv, AbstractFramePtr frame, |
| 861 |
jsbytecode* pc, ResumeMode& resultMode, MutableHandleValue vp); |
| 862 |
|
| 863 |
[[nodiscard]] bool processParsedHandlerResult( |
| 864 |
JSContext* cx, AbstractFramePtr frame, const jsbytecode* pc, bool success, |
| 865 |
ResumeMode resumeMode, HandleValue value, ResumeMode& resultMode, |
| 866 |
MutableHandleValue vp); |
| 867 |
|
| 868 |
/** |
| 869 |
* Given a resumption return value from a hook, parse and validate it based |
| 870 |
* on the given frame, and split the result into a ResumeMode and Value. |
| 871 |
*/ |
| 872 |
[[nodiscard]] bool prepareResumption(JSContext* cx, AbstractFramePtr frame, |
| 873 |
const jsbytecode* pc, |
| 874 |
ResumeMode& resumeMode, |
| 875 |
MutableHandleValue vp); |
| 876 |
|
| 877 |
/** |
| 878 |
* If there is a pending exception and a handler, call the handler with the |
| 879 |
* exception so that it can attempt to resolve the error. |
| 880 |
*/ |
| 881 |
[[nodiscard]] bool callUncaughtExceptionHandler(JSContext* cx, |
| 882 |
MutableHandleValue vp); |
| 883 |
|
| 884 |
/** |
| 885 |
* If the context has a pending exception, report it to the current global. |
| 886 |
*/ |
| 887 |
void reportUncaughtException(JSContext* cx); |
| 888 |
|
| 889 |
/* |
| 890 |
* Call the uncaught exception handler if there is one, returning true |
| 891 |
* if it handled the error, or false otherwise. |
| 892 |
*/ |
| 893 |
[[nodiscard]] bool handleUncaughtException(JSContext* cx); |
| 894 |
|
| 895 |
GlobalObject* unwrapDebuggeeArgument(JSContext* cx, const Value& v); |
| 896 |
|
| 897 |
static void traceObject(JSTracer* trc, JSObject* obj); |
| 898 |
|
| 899 |
void trace(JSTracer* trc); |
| 900 |
|
| 901 |
void traceForMovingGC(JSTracer* trc); |
| 902 |
void traceCrossCompartmentEdges(JSTracer* tracer); |
| 903 |
|
| 904 |
private: |
| 905 |
template <typename F> |
| 906 |
void forEachWeakMap(const F& f); |
| 907 |
|
| 908 |
[[nodiscard]] static bool getHookImpl(JSContext* cx, const CallArgs& args, |
| 909 |
Debugger& dbg, Hook which); |
| 910 |
[[nodiscard]] static bool setHookImpl(JSContext* cx, const CallArgs& args, |
| 911 |
Debugger& dbg, Hook which); |
| 912 |
|
| 913 |
[[nodiscard]] static bool getGarbageCollectionHook(JSContext* cx, |
| 914 |
const CallArgs& args, |
| 915 |
Debugger& dbg); |
| 916 |
[[nodiscard]] static bool setGarbageCollectionHook(JSContext* cx, |
| 917 |
const CallArgs& args, |
| 918 |
Debugger& dbg); |
| 919 |
|
| 920 |
static bool isCompilableUnit(JSContext* cx, unsigned argc, Value* vp); |
| 921 |
static bool recordReplayProcessKind(JSContext* cx, unsigned argc, Value* vp); |
| 922 |
static bool construct(JSContext* cx, unsigned argc, Value* vp); |
| 923 |
|
| 924 |
struct CallData; |
| 925 |
|
| 926 |
static const JSPropertySpec properties[]; |
| 927 |
static const JSFunctionSpec methods[]; |
| 928 |
static const JSPropertySpec static_properties[]; |
| 929 |
static const JSFunctionSpec static_methods[]; |
| 930 |
|
| 931 |
/** |
| 932 |
* Suspend the DebuggerFrame, clearing on-stack data but leaving it linked |
| 933 |
* with the AbstractGeneratorObject so it can be re-used later. |
| 934 |
*/ |
| 935 |
static void suspendGeneratorDebuggerFrames(JSContext* cx, |
| 936 |
AbstractFramePtr frame); |
| 937 |
|
| 938 |
/** |
| 939 |
* Terminate the DebuggerFrame, clearing all data associated with the frame |
| 940 |
* so that it cannot be used to introspect stack frame data. |
| 941 |
*/ |
| 942 |
static void terminateDebuggerFrames(JSContext* cx, AbstractFramePtr frame); |
| 943 |
|
| 944 |
/** |
| 945 |
* Terminate a given DebuggerFrame, removing all internal state and all |
| 946 |
* references to the frame from the Debugger itself. If the frame is being |
| 947 |
* terminated while 'frames' or 'generatorFrames' are being iterated, pass a |
| 948 |
* pointer to the iteration Enum to remove the entry and ensure that iteration |
| 949 |
* behaves properly. |
| 950 |
* |
| 951 |
* The AbstractFramePtr may be omited in a call so long as it is either |
| 952 |
* called again later with the correct 'frame', or the frame itself has never |
| 953 |
* had on-stack data or a 'frames' entry and has never had an onStep handler. |
| 954 |
*/ |
| 955 |
static void terminateDebuggerFrame( |
| 956 |
JS::GCContext* gcx, Debugger* dbg, DebuggerFrame* dbgFrame, |
| 957 |
AbstractFramePtr frame, FrameMap::Enum* maybeFramesEnum = nullptr, |
| 958 |
GeneratorWeakMap::Enum* maybeGeneratorFramesEnum = nullptr); |
| 959 |
|
| 960 |
static bool updateExecutionObservabilityOfFrames( |
| 961 |
JSContext* cx, const DebugAPI::ExecutionObservableSet& obs, |
| 962 |
IsObserving observing); |
| 963 |
static bool updateExecutionObservabilityOfScripts( |
| 964 |
JSContext* cx, const DebugAPI::ExecutionObservableSet& obs, |
| 965 |
IsObserving observing); |
| 966 |
static bool updateExecutionObservability( |
| 967 |
JSContext* cx, DebugAPI::ExecutionObservableSet& obs, |
| 968 |
IsObserving observing); |
| 969 |
|
| 970 |
template <typename FrameFn /* void (Debugger*, DebuggerFrame*) */> |
| 971 |
static void forEachOnStackDebuggerFrame(AbstractFramePtr frame, |
| 972 |
const JS::AutoRequireNoGC& nogc, |
| 973 |
FrameFn fn); |
| 974 |
template <typename FrameFn /* void (Debugger*, DebuggerFrame*) */> |
| 975 |
static void forEachOnStackOrSuspendedDebuggerFrame( |
| 976 |
JSContext* cx, AbstractFramePtr frame, const JS::AutoRequireNoGC& nogc, |
| 977 |
FrameFn fn); |
| 978 |
|
| 979 |
/* |
| 980 |
* Return a vector containing all Debugger.Frame instances referring to |
| 981 |
* |frame|. |global| is |frame|'s global object; if nullptr or omitted, we |
| 982 |
* compute it ourselves from |frame|. |
| 983 |
*/ |
| 984 |
using DebuggerFrameVector = GCVector<DebuggerFrame*, 0, SystemAllocPolicy>; |
| 985 |
[[nodiscard]] static bool getDebuggerFrames( |
| 986 |
AbstractFramePtr frame, MutableHandle<DebuggerFrameVector> frames); |
| 987 |
|
| 988 |
public: |
| 989 |
// Public for DebuggerScript::setBreakpoint. |
| 990 |
[[nodiscard]] static bool ensureExecutionObservabilityOfScript( |
| 991 |
JSContext* cx, JSScript* script); |
| 992 |
|
| 993 |
// Whether the Debugger instance needs to observe all non-AOT JS |
| 994 |
// execution of its debugees. |
| 995 |
IsObserving observesAllExecution() const; |
| 996 |
|
| 997 |
// Whether the Debugger instance needs to observe AOT-compiled asm.js |
| 998 |
// execution of its debuggees. |
| 999 |
IsObserving observesAsmJS() const; |
| 1000 |
|
| 1001 |
// Whether the Debugger instance needs to observe compiled Wasm |
| 1002 |
// execution of its debuggees. |
| 1003 |
IsObserving observesWasm() const; |
| 1004 |
|
| 1005 |
// Whether the Debugger instance needs to observe coverage of any JavaScript |
| 1006 |
// execution. |
| 1007 |
IsObserving observesCoverage() const; |
| 1008 |
|
| 1009 |
// Whether the Debugger instance needs to observe native call invocations. |
| 1010 |
IsObserving observesNativeCalls() const; |
| 1011 |
|
| 1012 |
bool isExclusiveDebuggerOnEval() const; |
| 1013 |
|
| 1014 |
private: |
| 1015 |
[[nodiscard]] static bool ensureExecutionObservabilityOfFrame( |
| 1016 |
JSContext* cx, AbstractFramePtr frame); |
| 1017 |
[[nodiscard]] static bool ensureExecutionObservabilityOfRealm( |
| 1018 |
JSContext* cx, JS::Realm* realm); |
| 1019 |
|
| 1020 |
static bool hookObservesAllExecution(Hook which); |
| 1021 |
|
| 1022 |
[[nodiscard]] bool updateObservesAllExecutionOnDebuggees( |
| 1023 |
JSContext* cx, IsObserving observing); |
| 1024 |
[[nodiscard]] bool updateObservesCoverageOnDebuggees(JSContext* cx, |
| 1025 |
IsObserving observing); |
| 1026 |
void updateObservesAsmJSOnDebuggees(IsObserving observing); |
| 1027 |
void updateObservesWasmOnDebuggees(IsObserving observing); |
| 1028 |
void updateObservesNativeCallOnDebuggees(IsObserving observing); |
| 1029 |
|
| 1030 |
JSObject* getHook(Hook hook) const; |
| 1031 |
bool hasAnyLiveHooks() const; |
| 1032 |
inline bool isHookCallAllowed(JSContext* cx) const; |
| 1033 |
|
| 1034 |
static void slowPathPromiseHook(JSContext* cx, Hook hook, |
| 1035 |
Handle<PromiseObject*> promise); |
| 1036 |
|
| 1037 |
template <typename HookIsEnabledFun /* bool (Debugger*) */, |
| 1038 |
typename FireHookFun /* void (Debugger*) */> |
| 1039 |
static void dispatchQuietHook(JSContext* cx, HookIsEnabledFun hookIsEnabled, |
| 1040 |
FireHookFun fireHook); |
| 1041 |
template < |
| 1042 |
typename HookIsEnabledFun /* bool (Debugger*) */, typename FireHookFun /* bool (Debugger*, ResumeMode&, MutableHandleValue) */> |
| 1043 |
[[nodiscard]] static bool dispatchResumptionHook( |
| 1044 |
JSContext* cx, AbstractFramePtr frame, HookIsEnabledFun hookIsEnabled, |
| 1045 |
FireHookFun fireHook); |
| 1046 |
|
| 1047 |
template <typename RunImpl /* bool () */> |
| 1048 |
[[nodiscard]] bool enterDebuggerHook(JSContext* cx, RunImpl runImpl) { |
| 1049 |
if (!isHookCallAllowed(cx)) { |
| 1050 |
return true; |
| 1051 |
} |
| 1052 |
|
| 1053 |
AutoRealm ar(cx, object); |
| 1054 |
|
| 1055 |
if (!runImpl()) { |
| 1056 |
// We do not want errors within one hook to effect errors in other hooks, |
| 1057 |
// so the only errors that we allow to propagate out of a debugger hook |
| 1058 |
// are OOM errors and general terminations. |
| 1059 |
if (!cx->isExceptionPending() || cx->isThrowingOutOfMemory()) { |
| 1060 |
return false; |
| 1061 |
} |
| 1062 |
|
| 1063 |
reportUncaughtException(cx); |
| 1064 |
} |
| 1065 |
MOZ_ASSERT(!cx->isExceptionPending()); |
| 1066 |
return true; |
| 1067 |
} |
| 1068 |
|
| 1069 |
[[nodiscard]] bool fireDebuggerStatement(JSContext* cx, |
| 1070 |
ResumeMode& resumeMode, |
| 1071 |
MutableHandleValue vp); |
| 1072 |
[[nodiscard]] bool fireExceptionUnwind(JSContext* cx, HandleValue exc, |
| 1073 |
ResumeMode& resumeMode, |
| 1074 |
MutableHandleValue vp); |
| 1075 |
[[nodiscard]] bool fireEnterFrame(JSContext* cx, ResumeMode& resumeMode, |
| 1076 |
MutableHandleValue vp); |
| 1077 |
[[nodiscard]] bool fireNativeCall(JSContext* cx, const CallArgs& args, |
| 1078 |
CallReason reason, ResumeMode& resumeMode, |
| 1079 |
MutableHandleValue vp); |
| 1080 |
[[nodiscard]] bool fireNewGlobalObject(JSContext* cx, |
| 1081 |
Handle<GlobalObject*> global); |
| 1082 |
[[nodiscard]] bool firePromiseHook(JSContext* cx, Hook hook, |
| 1083 |
HandleObject promise); |
| 1084 |
|
| 1085 |
DebuggerScript* newVariantWrapper(JSContext* cx, |
| 1086 |
Handle<DebuggerScriptReferent> referent) { |
| 1087 |
return newDebuggerScript(cx, referent); |
| 1088 |
} |
| 1089 |
DebuggerSource* newVariantWrapper(JSContext* cx, |
| 1090 |
Handle<DebuggerSourceReferent> referent) { |
| 1091 |
return newDebuggerSource(cx, referent); |
| 1092 |
} |
| 1093 |
|
| 1094 |
/* |
| 1095 |
* Helper function to help wrap Debugger objects whose referents may be |
| 1096 |
* variants. Currently Debugger.Script and Debugger.Source referents may |
| 1097 |
* be variants. |
| 1098 |
* |
| 1099 |
* Prefer using wrapScript, wrapWasmScript, wrapSource, and wrapWasmSource |
| 1100 |
* whenever possible. |
| 1101 |
*/ |
| 1102 |
template <typename ReferentType, typename Map> |
| 1103 |
typename Map::WrapperType* wrapVariantReferent( |
| 1104 |
JSContext* cx, Map& map, |
| 1105 |
Handle<typename Map::WrapperType::ReferentVariant> referent); |
| 1106 |
DebuggerScript* wrapVariantReferent(JSContext* cx, |
| 1107 |
Handle<DebuggerScriptReferent> referent); |
| 1108 |
DebuggerSource* wrapVariantReferent(JSContext* cx, |
| 1109 |
Handle<DebuggerSourceReferent> referent); |
| 1110 |
|
| 1111 |
/* |
| 1112 |
* Allocate and initialize a Debugger.Script instance whose referent is |
| 1113 |
* |referent|. |
| 1114 |
*/ |
| 1115 |
DebuggerScript* newDebuggerScript(JSContext* cx, |
| 1116 |
Handle<DebuggerScriptReferent> referent); |
| 1117 |
|
| 1118 |
/* |
| 1119 |
* Allocate and initialize a Debugger.Source instance whose referent is |
| 1120 |
* |referent|. |
| 1121 |
*/ |
| 1122 |
DebuggerSource* newDebuggerSource(JSContext* cx, |
| 1123 |
Handle<DebuggerSourceReferent> referent); |
| 1124 |
|
| 1125 |
/* |
| 1126 |
* Receive a "new script" event from the engine. A new script was compiled |
| 1127 |
* or deserialized. |
| 1128 |
*/ |
| 1129 |
[[nodiscard]] bool fireNewScript( |
| 1130 |
JSContext* cx, Handle<DebuggerScriptReferent> scriptReferent); |
| 1131 |
|
| 1132 |
/* |
| 1133 |
* Receive a "garbage collection" event from the engine. A GC cycle with the |
| 1134 |
* given data was recently completed. |
| 1135 |
*/ |
| 1136 |
[[nodiscard]] bool fireOnGarbageCollectionHook( |
| 1137 |
JSContext* cx, const JS::dbg::GarbageCollectionEvent::Ptr& gcData); |
| 1138 |
|
| 1139 |
inline Breakpoint* firstBreakpoint() const; |
| 1140 |
|
| 1141 |
[[nodiscard]] static bool replaceFrameGuts(JSContext* cx, |
| 1142 |
AbstractFramePtr from, |
| 1143 |
AbstractFramePtr to, |
| 1144 |
ScriptFrameIter& iter); |
| 1145 |
|
| 1146 |
public: |
| 1147 |
Debugger(JSContext* cx, NativeObject* dbg); |
| 1148 |
~Debugger(); |
| 1149 |
|
| 1150 |
inline const js::HeapPtr<NativeObject*>& toJSObject() const; |
| 1151 |
inline js::HeapPtr<NativeObject*>& toJSObjectRef(); |
| 1152 |
static inline Debugger* fromJSObject(const JSObject* obj); |
| 1153 |
|
| 1154 |
#ifdef DEBUG |
| 1155 |
static bool isChildJSObject(JSObject* obj); |
| 1156 |
#endif |
| 1157 |
|
| 1158 |
Zone* zone() const { return toJSObject()->zone(); } |
| 1159 |
|
| 1160 |
bool hasMemory() const; |
| 1161 |
DebuggerMemory& memory() const; |
| 1162 |
|
| 1163 |
WeakGlobalObjectSet::Range allDebuggees() const { return debuggees.all(); } |
| 1164 |
|
| 1165 |
#ifdef DEBUG |
| 1166 |
static bool isDebuggerCrossCompartmentEdge(JSObject* obj, |
| 1167 |
const js::gc::Cell* cell); |
| 1168 |
#endif |
| 1169 |
|
| 1170 |
static bool hasLiveHook(GlobalObject* global, Hook which); |
| 1171 |
|
| 1172 |
/*** Functions for use by Debugger.cpp. *********************************/ |
| 1173 |
|
| 1174 |
inline bool observesEnterFrame() const; |
| 1175 |
inline bool observesNewScript() const; |
| 1176 |
inline bool observesNewGlobalObject() const; |
| 1177 |
inline bool observesGlobal(GlobalObject* global) const; |
| 1178 |
bool observesFrame(AbstractFramePtr frame) const; |
| 1179 |
bool observesFrame(const FrameIter& iter) const; |
| 1180 |
bool observesScript(JSScript* script) const; |
| 1181 |
bool observesWasm(wasm::Instance* instance) const; |
| 1182 |
|
| 1183 |
/* |
| 1184 |
* If env is nullptr, call vp->setNull() and return true. Otherwise, find |
| 1185 |
* or create a Debugger.Environment object for the given Env. On success, |
| 1186 |
* store the Environment object in *vp and return true. |
| 1187 |
*/ |
| 1188 |
[[nodiscard]] bool wrapEnvironment(JSContext* cx, Handle<Env*> env, |
| 1189 |
MutableHandleValue vp); |
| 1190 |
[[nodiscard]] bool wrapEnvironment( |
| 1191 |
JSContext* cx, Handle<Env*> env, |
| 1192 |
MutableHandle<DebuggerEnvironment*> result); |
| 1193 |
|
| 1194 |
/* |
| 1195 |
* Like cx->compartment()->wrap(cx, vp), but for the debugger realm. |
| 1196 |
* |
| 1197 |
* Preconditions: *vp is a value from a debuggee realm; cx is in the |
| 1198 |
* debugger's compartment. |
| 1199 |
* |
| 1200 |
* If *vp is an object, this produces a (new or existing) Debugger.Object |
| 1201 |
* wrapper for it. Otherwise this is the same as Compartment::wrap. |
| 1202 |
* |
| 1203 |
* If *vp is a magic JS_OPTIMIZED_OUT value, this produces a plain object |
| 1204 |
* of the form { optimizedOut: true }. |
| 1205 |
* |
| 1206 |
* If *vp is a magic JS_MISSING_ARGUMENTS value signifying missing |
| 1207 |
* arguments, this produces a plain object of the form { missingArguments: |
| 1208 |
* true }. |
| 1209 |
* |
| 1210 |
* If *vp is a magic JS_UNINITIALIZED_LEXICAL value signifying an |
| 1211 |
* unaccessible uninitialized binding, this produces a plain object of the |
| 1212 |
* form { uninitialized: true }. |
| 1213 |
*/ |
| 1214 |
[[nodiscard]] bool wrapDebuggeeValue(JSContext* cx, MutableHandleValue vp); |
| 1215 |
[[nodiscard]] bool wrapDebuggeeObject(JSContext* cx, HandleObject obj, |
| 1216 |
MutableHandle<DebuggerObject*> result); |
| 1217 |
[[nodiscard]] bool wrapNullableDebuggeeObject( |
| 1218 |
JSContext* cx, HandleObject obj, MutableHandle<DebuggerObject*> result); |
| 1219 |
|
| 1220 |
/* |
| 1221 |
* Unwrap a Debug.Object, without rewrapping it for any particular debuggee |
| 1222 |
* compartment. |
| 1223 |
* |
| 1224 |
* Preconditions: cx is in the debugger compartment. *vp is a value in that |
| 1225 |
* compartment. (*vp should be a "debuggee value", meaning it is the |
| 1226 |
* debugger's reflection of a value in the debuggee.) |
| 1227 |
* |
| 1228 |
* If *vp is a Debugger.Object, store the referent in *vp. Otherwise, if *vp |
| 1229 |
* is an object, throw a TypeError, because it is not a debuggee |
| 1230 |
* value. Otherwise *vp is a primitive, so leave it alone. |
| 1231 |
* |
| 1232 |
* When passing values from the debuggee to the debugger: |
| 1233 |
* enter debugger compartment; |
| 1234 |
* call wrapDebuggeeValue; // compartment- and debugger-wrapping |
| 1235 |
* |
| 1236 |
* When passing values from the debugger to the debuggee: |
| 1237 |
* call unwrapDebuggeeValue; // debugger-unwrapping |
| 1238 |
* enter debuggee realm; |
| 1239 |
* call cx->compartment()->wrap; // compartment-rewrapping |
| 1240 |
* |
| 1241 |
* (Extreme nerd sidebar: Unwrapping happens in two steps because there are |
| 1242 |
* two different kinds of symmetry at work: regardless of which direction |
| 1243 |
* we're going, we want any exceptions to be created and thrown in the |
| 1244 |
* debugger compartment--mirror symmetry. But compartment wrapping always |
| 1245 |
* happens in the target compartment--rotational symmetry.) |
| 1246 |
*/ |
| 1247 |
[[nodiscard]] bool unwrapDebuggeeValue(JSContext* cx, MutableHandleValue vp); |
| 1248 |
[[nodiscard]] bool unwrapDebuggeeObject(JSContext* cx, |
| 1249 |
MutableHandleObject obj); |
| 1250 |
[[nodiscard]] bool unwrapPropertyDescriptor( |
| 1251 |
JSContext* cx, HandleObject obj, MutableHandle<PropertyDescriptor> desc); |
| 1252 |
|
| 1253 |
/* |
| 1254 |
* Store the Debugger.Frame object for iter in *vp/result. |
| 1255 |
* |
| 1256 |
* If this Debugger does not already have a Frame object for the frame |
| 1257 |
* `iter` points to, a new Frame object is created, and `iter`'s private |
| 1258 |
* data is copied into it. |
| 1259 |
*/ |
| 1260 |
[[nodiscard]] bool getFrame(JSContext* cx, const FrameIter& iter, |
| 1261 |
MutableHandleValue vp); |
| 1262 |
[[nodiscard]] bool getFrame(JSContext* cx, |
| 1263 |
MutableHandle<DebuggerFrame*> result); |
| 1264 |
[[nodiscard]] bool getFrame(JSContext* cx, const FrameIter& iter, |
| 1265 |
MutableHandle<DebuggerFrame*> result); |
| 1266 |
[[nodiscard]] bool getFrame(JSContext* cx, |
| 1267 |
Handle<AbstractGeneratorObject*> genObj, |
| 1268 |
MutableHandle<DebuggerFrame*> result); |
| 1269 |
|
| 1270 |
/* |
| 1271 |
* Return the Debugger.Script object for |script|, or create a new one if |
| 1272 |
* needed. The context |cx| must be in the debugger realm; |script| must be |
| 1273 |
* a script in a debuggee realm. |
| 1274 |
*/ |
| 1275 |
DebuggerScript* wrapScript(JSContext* cx, Handle<BaseScript*> script); |
| 1276 |
|
| 1277 |
/* |
| 1278 |
* Return the Debugger.Script object for |wasmInstance| (the toplevel |
| 1279 |
* script), synthesizing a new one if needed. The context |cx| must be in |
| 1280 |
* the debugger compartment; |wasmInstance| must be a WasmInstanceObject in |
| 1281 |
* the debuggee realm. |
| 1282 |
*/ |
| 1283 |
DebuggerScript* wrapWasmScript(JSContext* cx, |
| 1284 |
Handle<WasmInstanceObject*> wasmInstance); |
| 1285 |
|
| 1286 |
/* |
| 1287 |
* Return the Debugger.Source object for |source|, or create a new one if |
| 1288 |
* needed. The context |cx| must be in the debugger compartment; |source| |
| 1289 |
* must be a script source object in a debuggee realm. |
| 1290 |
*/ |
| 1291 |
DebuggerSource* wrapSource(JSContext* cx, |
| 1292 |
js::Handle<ScriptSourceObject*> source); |
| 1293 |
|
| 1294 |
/* |
| 1295 |
* Return the Debugger.Source object for |wasmInstance| (the entire module), |
| 1296 |
* synthesizing a new one if needed. The context |cx| must be in the |
| 1297 |
* debugger compartment; |wasmInstance| must be a WasmInstanceObject in the |
| 1298 |
* debuggee realm. |
| 1299 |
*/ |
| 1300 |
DebuggerSource* wrapWasmSource(JSContext* cx, |
| 1301 |
Handle<WasmInstanceObject*> wasmInstance); |
| 1302 |
|
| 1303 |
DebuggerDebuggeeLink* getDebuggeeLink(); |
| 1304 |
|
| 1305 |
private: |
| 1306 |
Debugger(const Debugger&) = delete; |
| 1307 |
Debugger& operator=(const Debugger&) = delete; |
| 1308 |
}; |
| 1309 |
|
| 1310 |
// Specialize InternalBarrierMethods so we can have WeakHeapPtr<Debugger*>. |
| 1311 |
template <> |
| 1312 |
struct InternalBarrierMethods<Debugger*> { |
| 1313 |
static bool isMarkable(Debugger* dbg) { return dbg->toJSObject(); } |
| 1314 |
|
| 1315 |
static void postBarrier(Debugger** vp, Debugger* prev, Debugger* next) {} |
| 1316 |
|
| 1317 |
static void readBarrier(Debugger* dbg) { |
| 1318 |
InternalBarrierMethods<JSObject*>::readBarrier(dbg->toJSObject()); |
| 1319 |
} |
| 1320 |
|
| 1321 |
#ifdef DEBUG |
| 1322 |
static void assertThingIsNotGray(Debugger* dbg) {} |
| 1323 |
#endif |
| 1324 |
}; |
| 1325 |
|
| 1326 |
/** |
| 1327 |
* This class exists for one specific reason. If a given Debugger object is in |
| 1328 |
* a state where: |
| 1329 |
* |
| 1330 |
* a) nothing in the system has a reference to the object |
| 1331 |
* b) the debugger is currently attached to a live debuggee |
| 1332 |
* c) the debugger has hooks like 'onEnterFrame' |
| 1333 |
* |
| 1334 |
* then we don't want the GC to delete the Debugger, because the system could |
| 1335 |
* still call the hooks. This means we need to ensure that, whenever the global |
| 1336 |
* gets marked, the Debugger will get marked as well. Critically, we _only_ |
| 1337 |
* want that to happen if the debugger has hooks. If it doesn't, then GCing |
| 1338 |
* the debugger is the right think to do. |
| 1339 |
* |
| 1340 |
* Note that there are _other_ cases where the debugger may be held live, but |
| 1341 |
* those are not addressed by this case. |
| 1342 |
* |
| 1343 |
* To accomplish this, we use a bit of roundabout link approach. Both the |
| 1344 |
* Debugger and the debuggees can reach the link object: |
| 1345 |
* |
| 1346 |
* Debugger -> DebuggerDebuggeeLink <- CCW <- Debuggee Global #1 |
| 1347 |
* | | ^ ^---<- CCW <- Debuggee Global #2 |
| 1348 |
* \--<<-optional-<<--/ \------<- CCW <- Debuggee Global #3 |
| 1349 |
* |
| 1350 |
* and critically, the Debugger is able to conditionally add or remove the link |
| 1351 |
* going from the DebuggerDebuggeeLink _back_ to the Debugger. When this link |
| 1352 |
* exists, the GC can trace all the way from the global to the Debugger, |
| 1353 |
* meaning that any Debugger with this link will be kept alive as long as any |
| 1354 |
* of its debuggees are alive. |
| 1355 |
*/ |
| 1356 |
class DebuggerDebuggeeLink : public NativeObject { |
| 1357 |
private: |
| 1358 |
enum { |
| 1359 |
DEBUGGER_LINK_SLOT, |
| 1360 |
RESERVED_SLOTS, |
| 1361 |
}; |
| 1362 |
|
| 1363 |
public: |
| 1364 |
static const JSClass class_; |
| 1365 |
|
| 1366 |
void setLinkSlot(Debugger& dbg); |
| 1367 |
void clearLinkSlot(); |
| 1368 |
}; |
| 1369 |
|
| 1370 |
/* |
| 1371 |
* A Handler represents a Debugger API reflection object's handler function, |
| 1372 |
* like a Debugger.Frame's onStep handler. These handler functions are called by |
| 1373 |
* the Debugger API to notify the user of certain events. For each event type, |
| 1374 |
* we define a separate subclass of Handler. |
| 1375 |
* |
| 1376 |
* When a reflection object accepts a Handler, it calls its 'hold' method; and |
| 1377 |
* if the Handler is replaced by another, or the reflection object is finalized, |
| 1378 |
* the reflection object calls the Handler's 'drop' method. The reflection |
| 1379 |
* object does not otherwise manage the Handler's lifetime, say, by calling its |
| 1380 |
* destructor or freeing its memory. A simple Handler implementation might have |
| 1381 |
* an empty 'hold' method, and have its 'drop' method delete the Handler. A more |
| 1382 |
* complex Handler might process many kinds of events, and thus inherit from |
| 1383 |
* many Handler subclasses and be held by many reflection objects |
| 1384 |
* simultaneously; a handler like this could use 'hold' and 'drop' to manage a |
| 1385 |
* reference count. |
| 1386 |
* |
| 1387 |
* To support SpiderMonkey's memory use tracking, 'hold' and 'drop' also require |
| 1388 |
* a pointer to the owning reflection object, so that the Holder implementation |
| 1389 |
* can properly report changes in ownership to functions using the |
| 1390 |
* js::gc::MemoryUse categories. |
| 1391 |
*/ |
| 1392 |
struct Handler { |
| 1393 |
virtual ~Handler() = default; |
| 1394 |
|
| 1395 |
/* |
| 1396 |
* If this Handler is a reference to a callable JSObject, return that |
| 1397 |
* JSObject. Otherwise, this method returns nullptr. |
| 1398 |
* |
| 1399 |
* The JavaScript getters for handler properties on reflection objects use |
| 1400 |
* this method to obtain the callable the handler represents. When a Handler's |
| 1401 |
* 'object' method returns nullptr, that handler is simply not visible to |
| 1402 |
* JavaScript. |
| 1403 |
*/ |
| 1404 |
virtual JSObject* object() const = 0; |
| 1405 |
|
| 1406 |
/* Report that this Handler is now held by owner. See comment above. */ |
| 1407 |
virtual void hold(JSObject* owner) = 0; |
| 1408 |
|
| 1409 |
/* Report that this Handler is no longer held by owner. See comment above. */ |
| 1410 |
virtual void drop(JS::GCContext* gcx, JSObject* owner) = 0; |
| 1411 |
|
| 1412 |
/* |
| 1413 |
* Trace the reference to the handler. This method will be called by the |
| 1414 |
* reflection object holding this Handler whenever the former is traced. |
| 1415 |
*/ |
| 1416 |
virtual void trace(JSTracer* tracer) = 0; |
| 1417 |
|
| 1418 |
/* Allocation size in bytes for memory accounting purposes. */ |
| 1419 |
virtual size_t allocSize() const = 0; |
| 1420 |
}; |
| 1421 |
|
| 1422 |
class JSBreakpointSite; |
| 1423 |
class WasmBreakpointSite; |
| 1424 |
|
| 1425 |
/** |
| 1426 |
* Breakpoint GC rules: |
| 1427 |
* |
| 1428 |
* BreakpointSites and Breakpoints are owned by the code in which they are set. |
| 1429 |
* Tracing a JSScript or WasmInstance traces all BreakpointSites set in it, |
| 1430 |
* which traces all Breakpoints; and if the code is garbage collected, the |
| 1431 |
* BreakpointSite and the Breakpoints set at it are freed as well. Doing so is |
| 1432 |
* not observable to JS, since the handlers would never fire, and there is no |
| 1433 |
* way to enumerate all breakpoints without specifying a specific script, in |
| 1434 |
* which case it must not have been GC'd. |
| 1435 |
* |
| 1436 |
* Although BreakpointSites and Breakpoints are not GC things, they should be |
| 1437 |
* treated as belonging to the code's compartment. This means that the |
| 1438 |
* BreakpointSite concrete subclasses' pointers to the code are not |
| 1439 |
* cross-compartment references, but a Breakpoint's pointers to its handler and |
| 1440 |
* owning Debugger are cross-compartment references, and go through |
| 1441 |
* cross-compartment wrappers. |
| 1442 |
*/ |
| 1443 |
|
| 1444 |
/** |
| 1445 |
* A location in a JSScript or WasmInstance at which we have breakpoints. A |
| 1446 |
* BreakpointSite owns a linked list of all the breakpoints set at its location. |
| 1447 |
* In general, this list contains breakpoints set by multiple Debuggers in |
| 1448 |
* various compartments. |
| 1449 |
* |
| 1450 |
* BreakpointSites are created only as needed, for locations at which |
| 1451 |
* breakpoints are currently set. When the last breakpoint is removed from a |
| 1452 |
* location, the BreakpointSite is removed as well. |
| 1453 |
* |
| 1454 |
* This is an abstract base class, with subclasses specialized for the different |
| 1455 |
* sorts of code a breakpoint might be set in. JSBreakpointSite manages sites in |
| 1456 |
* JSScripts, and WasmBreakpointSite manages sites in WasmInstances. |
| 1457 |
*/ |
| 1458 |
class BreakpointSite { |
| 1459 |
friend class DebugAPI; |
| 1460 |
friend class Breakpoint; |
| 1461 |
friend class Debugger; |
| 1462 |
|
| 1463 |
private: |
| 1464 |
template <typename T> |
| 1465 |
struct SiteLinkAccess { |
| 1466 |
static mozilla::DoublyLinkedListElement<T>& Get(T* aThis) { |
| 1467 |
return aThis->siteLink; |
| 1468 |
} |
| 1469 |
}; |
| 1470 |
|
| 1471 |
// List of all js::Breakpoints at this instruction. |
| 1472 |
using BreakpointList = |
| 1473 |
mozilla::DoublyLinkedList<js::Breakpoint, SiteLinkAccess<js::Breakpoint>>; |
| 1474 |
BreakpointList breakpoints; |
| 1475 |
|
| 1476 |
protected: |
| 1477 |
BreakpointSite() = default; |
| 1478 |
virtual ~BreakpointSite() = default; |
| 1479 |
void finalize(JS::GCContext* gcx); |
| 1480 |
virtual gc::Cell* owningCell() = 0; |
| 1481 |
|
| 1482 |
public: |
| 1483 |
Breakpoint* firstBreakpoint() const; |
| 1484 |
bool hasBreakpoint(Breakpoint* bp); |
| 1485 |
|
| 1486 |
bool isEmpty() const; |
| 1487 |
virtual void trace(JSTracer* trc); |
| 1488 |
virtual void remove(JS::GCContext* gcx) = 0; |
| 1489 |
void destroyIfEmpty(JS::GCContext* gcx) { |
| 1490 |
if (isEmpty()) { |
| 1491 |
remove(gcx); |
| 1492 |
} |
| 1493 |
} |
| 1494 |
virtual Realm* realm() const = 0; |
| 1495 |
}; |
| 1496 |
|
| 1497 |
/* |
| 1498 |
* A breakpoint set at a given BreakpointSite, indicating the owning debugger |
| 1499 |
* and the handler object. A Breakpoint is a member of two linked lists: its |
| 1500 |
* owning debugger's list and its site's list. |
| 1501 |
*/ |
| 1502 |
class Breakpoint { |
| 1503 |
friend class DebugAPI; |
| 1504 |
friend class Debugger; |
| 1505 |
friend class BreakpointSite; |
| 1506 |
|
| 1507 |
public: |
| 1508 |
/* Our owning debugger. */ |
| 1509 |
Debugger* const debugger; |
| 1510 |
|
| 1511 |
/** |
| 1512 |
* A cross-compartment wrapper for our owning debugger's object, a CCW in the |
| 1513 |
* code's compartment to the Debugger object in its own compartment. Holding |
| 1514 |
* this lets the GC know about the effective cross-compartment reference from |
| 1515 |
* the code to the debugger; see "Breakpoint GC Rules", above. |
| 1516 |
* |
| 1517 |
* This is almost redundant with the `debugger` field, except that we need |
| 1518 |
* access to our owning `Debugger` regardless of the relative privilege levels |
| 1519 |
* of debugger and debuggee, regardless of whether we're in the midst of a GC, |
| 1520 |
* and so on - unwrapping is just too entangled. |
| 1521 |
*/ |
| 1522 |
const HeapPtr<JSObject*> wrappedDebugger; |
| 1523 |
|
| 1524 |
/* The site at which we're inserted. */ |
| 1525 |
BreakpointSite* const site; |
| 1526 |
|
| 1527 |
private: |
| 1528 |
/** |
| 1529 |
* The breakpoint handler object, via a cross-compartment wrapper in the |
| 1530 |
* code's compartment. |
| 1531 |
* |
| 1532 |
* Although eventually we would like this to be a `js::Handler` instance, for |
| 1533 |
* now it is just cross-compartment wrapper for the JS object supplied to |
| 1534 |
* `setBreakpoint`, hopefully with a callable `hit` property. |
| 1535 |
*/ |
| 1536 |
const HeapPtr<JSObject*> handler; |
| 1537 |
|
| 1538 |
/** |
| 1539 |
* Link elements for each list this breakpoint can be in. |
| 1540 |
*/ |
| 1541 |
mozilla::DoublyLinkedListElement<Breakpoint> debuggerLink; |
| 1542 |
mozilla::DoublyLinkedListElement<Breakpoint> siteLink; |
| 1543 |
|
| 1544 |
void trace(JSTracer* trc); |
| 1545 |
|
| 1546 |
public: |
| 1547 |
Breakpoint(Debugger* debugger, HandleObject wrappedDebugger, |
| 1548 |
BreakpointSite* site, HandleObject handler); |
| 1549 |
|
| 1550 |
enum MayDestroySite { False, True }; |
| 1551 |
|
| 1552 |
/** |
| 1553 |
* Unlink this breakpoint from its Debugger's and and BreakpointSite's lists, |
| 1554 |
* and free its memory. |
| 1555 |
* |
| 1556 |
* This is the low-level primitive shared by breakpoint removal and script |
| 1557 |
* finalization code. It is only concerned with cleaning up this Breakpoint; |
| 1558 |
* it does not check for now-empty BreakpointSites, unneeded DebugScripts, or |
| 1559 |
* the like. |
| 1560 |
*/ |
| 1561 |
void delete_(JS::GCContext* gcx); |
| 1562 |
|
| 1563 |
/** |
| 1564 |
* Remove this breakpoint. Unlink it from its Debugger's and BreakpointSite's |
| 1565 |
* lists, and if the BreakpointSite is now empty, clean that up and update JIT |
| 1566 |
* code as necessary. |
| 1567 |
*/ |
| 1568 |
void remove(JS::GCContext* gcx); |
| 1569 |
|
| 1570 |
Breakpoint* nextInDebugger(); |
| 1571 |
Breakpoint* nextInSite(); |
| 1572 |
JSObject* getHandler() const { return handler; } |
| 1573 |
}; |
| 1574 |
|
| 1575 |
class JSBreakpointSite : public BreakpointSite { |
| 1576 |
public: |
| 1577 |
const HeapPtr<JSScript*> script; |
| 1578 |
jsbytecode* const pc; |
| 1579 |
|
| 1580 |
public: |
| 1581 |
JSBreakpointSite(JSScript* script, jsbytecode* pc); |
| 1582 |
|
| 1583 |
void trace(JSTracer* trc) override; |
| 1584 |
void delete_(JS::GCContext* gcx); |
| 1585 |
void remove(JS::GCContext* gcx) override; |
| 1586 |
Realm* realm() const override; |
| 1587 |
|
| 1588 |
private: |
| 1589 |
gc::Cell* owningCell() override; |
| 1590 |
}; |
| 1591 |
|
| 1592 |
class WasmBreakpointSite : public BreakpointSite { |
| 1593 |
public: |
| 1594 |
const HeapPtr<WasmInstanceObject*> instanceObject; |
| 1595 |
uint32_t offset; |
| 1596 |
|
| 1597 |
public: |
| 1598 |
WasmBreakpointSite(WasmInstanceObject* instanceObject, uint32_t offset); |
| 1599 |
|
| 1600 |
void trace(JSTracer* trc) override; |
| 1601 |
void delete_(JS::GCContext* gcx); |
| 1602 |
void remove(JS::GCContext* gcx) override; |
| 1603 |
Realm* realm() const override; |
| 1604 |
|
| 1605 |
private: |
| 1606 |
gc::Cell* owningCell() override; |
| 1607 |
}; |
| 1608 |
|
| 1609 |
Breakpoint* Debugger::firstBreakpoint() const { |
| 1610 |
if (breakpoints.isEmpty()) { |
| 1611 |
return nullptr; |
| 1612 |
} |
| 1613 |
return &(*breakpoints.begin()); |
| 1614 |
} |
| 1615 |
|
| 1616 |
const js::HeapPtr<NativeObject*>& Debugger::toJSObject() const { |
| 1617 |
MOZ_ASSERT(object); |
| 1618 |
return object; |
| 1619 |
} |
| 1620 |
|
| 1621 |
js::HeapPtr<NativeObject*>& Debugger::toJSObjectRef() { |
| 1622 |
MOZ_ASSERT(object); |
| 1623 |
return object; |
| 1624 |
} |
| 1625 |
|
| 1626 |
bool Debugger::observesEnterFrame() const { return getHook(OnEnterFrame); } |
| 1627 |
|
| 1628 |
bool Debugger::observesNewScript() const { return getHook(OnNewScript); } |
| 1629 |
|
| 1630 |
bool Debugger::observesNewGlobalObject() const { |
| 1631 |
return getHook(OnNewGlobalObject); |
| 1632 |
} |
| 1633 |
|
| 1634 |
bool Debugger::observesGlobal(GlobalObject* global) const { |
| 1635 |
WeakHeapPtr<GlobalObject*> debuggee(global); |
| 1636 |
return debuggees.has(debuggee); |
| 1637 |
} |
| 1638 |
|
| 1639 |
[[nodiscard]] bool ReportObjectRequired(JSContext* cx); |
| 1640 |
|
| 1641 |
JSObject* IdVectorToArray(JSContext* cx, HandleIdVector ids); |
| 1642 |
bool IsInterpretedNonSelfHostedFunction(JSFunction* fun); |
| 1643 |
JSScript* GetOrCreateFunctionScript(JSContext* cx, HandleFunction fun); |
| 1644 |
ArrayObject* GetFunctionParameterNamesArray(JSContext* cx, HandleFunction fun); |
| 1645 |
bool ValueToIdentifier(JSContext* cx, HandleValue v, MutableHandleId id); |
| 1646 |
bool ValueToStableChars(JSContext* cx, const char* fnname, HandleValue value, |
| 1647 |
JS::AutoStableStringChars& stableChars); |
| 1648 |
bool ParseEvalOptions(JSContext* cx, HandleValue value, EvalOptions& options); |
| 1649 |
|
| 1650 |
Result<Completion> DebuggerGenericEval( |
| 1651 |
JSContext* cx, const mozilla::Range<const char16_t> chars, |
| 1652 |
HandleObject bindings, const EvalOptions& options, Debugger* dbg, |
| 1653 |
HandleObject envArg, FrameIter* iter); |
| 1654 |
|
| 1655 |
bool ParseResumptionValue(JSContext* cx, HandleValue rval, |
| 1656 |
ResumeMode& resumeMode, MutableHandleValue vp); |
| 1657 |
|
| 1658 |
#define JS_DEBUG_PSG(Name, Getter) \ |
| 1659 |
JS_PSG(Name, CallData::ToNative<&CallData::Getter>, 0) |
| 1660 |
|
| 1661 |
#define JS_DEBUG_PSGS(Name, Getter, Setter) \ |
| 1662 |
JS_PSGS(Name, CallData::ToNative<&CallData::Getter>, \ |
| 1663 |
CallData::ToNative<&CallData::Setter>, 0) |
| 1664 |
|
| 1665 |
#define JS_DEBUG_FN(Name, Method, NumArgs) \ |
| 1666 |
JS_FN(Name, CallData::ToNative<&CallData::Method>, NumArgs, 0) |
| 1667 |
|
| 1668 |
} /* namespace js */ |
| 1669 |
|
| 1670 |
#endif /* debugger_Debugger_h */ |