| 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 dbg_DebugScript_h |
| 8 |
#define dbg_DebugScript_h |
| 9 |
|
| 10 |
#include <stddef.h> // for offsetof |
| 11 |
#include <stddef.h> // for size_t |
| 12 |
#include <stdint.h> // for uint32_t |
| 13 |
|
| 14 |
#include "jstypes.h" |
| 15 |
|
| 16 |
#include "gc/WeakMap.h" |
| 17 |
#include "vm/NativeObject.h" |
| 18 |
|
| 19 |
namespace JS { |
| 20 |
class JS_PUBLIC_API Realm; |
| 21 |
} |
| 22 |
|
| 23 |
namespace js { |
| 24 |
|
| 25 |
class JSBreakpointSite; |
| 26 |
class Debugger; |
| 27 |
class DebugScriptObject; |
| 28 |
|
| 29 |
// DebugScript manages the internal debugger state for a JSScript, which may be |
| 30 |
// associated with multiple Debuggers. |
| 31 |
class DebugScript { |
| 32 |
friend class DebugAPI; |
| 33 |
friend class DebugScriptObject; |
| 34 |
|
| 35 |
/* |
| 36 |
* If this is a generator script, this is the number of Debugger.Frames |
| 37 |
* referring to calls to this generator, whether live or suspended. Closed |
| 38 |
* generators do not contribute a count. |
| 39 |
* |
| 40 |
* When greater than zero, this script should be compiled with debug |
| 41 |
* instrumentation to call Debugger::onResumeFrame at each resumption site, so |
| 42 |
* that Debugger can reconnect any extant Debugger.Frames with the new |
| 43 |
* concrete frame. |
| 44 |
*/ |
| 45 |
uint32_t generatorObserverCount; |
| 46 |
|
| 47 |
/* |
| 48 |
* The number of Debugger.Frame objects that refer to frames running this |
| 49 |
* script and that have onStep handlers. When nonzero, the interpreter and JIT |
| 50 |
* must arrange to call Debugger::onSingleStep before each bytecode, or at |
| 51 |
* least at some useful granularity. |
| 52 |
*/ |
| 53 |
uint32_t stepperCount; |
| 54 |
|
| 55 |
/* |
| 56 |
* The size of the script as reported by BaseScript::length. This is the |
| 57 |
* length of the DebugScript::breakpoints array, below. |
| 58 |
*/ |
| 59 |
size_t codeLength; |
| 60 |
|
| 61 |
/* |
| 62 |
* Number of breakpoint sites at opcodes in the script. This is the number |
| 63 |
* of populated entries in DebugScript::breakpoints. |
| 64 |
*/ |
| 65 |
uint32_t numSites; |
| 66 |
|
| 67 |
/* |
| 68 |
* Breakpoints set in our script. For speed and simplicity, this array is |
| 69 |
* parallel to script->code(): the JSBreakpointSite for the opcode at |
| 70 |
* script->code()[offset] is debugScript->breakpoints[offset]. |
| 71 |
*/ |
| 72 |
JSBreakpointSite* breakpoints[1]; |
| 73 |
|
| 74 |
/* |
| 75 |
* True if this DebugScript carries any useful information. If false, it |
| 76 |
* should be removed from its JSScript. |
| 77 |
*/ |
| 78 |
bool needed() const { |
| 79 |
return generatorObserverCount > 0 || stepperCount > 0 || numSites > 0; |
| 80 |
} |
| 81 |
|
| 82 |
static size_t allocSize(size_t codeLength) { |
| 83 |
return offsetof(DebugScript, breakpoints) + |
| 84 |
codeLength * sizeof(JSBreakpointSite*); |
| 85 |
} |
| 86 |
|
| 87 |
void trace(JSTracer* trc); |
| 88 |
void delete_(JS::GCContext* gcx, DebugScriptObject* owner); |
| 89 |
|
| 90 |
static DebugScript* get(JSScript* script); |
| 91 |
static DebugScript* getOrCreate(JSContext* cx, HandleScript script); |
| 92 |
|
| 93 |
public: |
| 94 |
static JSBreakpointSite* getBreakpointSite(JSScript* script, jsbytecode* pc); |
| 95 |
static JSBreakpointSite* getOrCreateBreakpointSite(JSContext* cx, |
| 96 |
HandleScript script, |
| 97 |
jsbytecode* pc); |
| 98 |
static void destroyBreakpointSite(JS::GCContext* gcx, JSScript* script, |
| 99 |
jsbytecode* pc); |
| 100 |
|
| 101 |
static void clearBreakpointsIn(JS::GCContext* gcx, JSScript* script, |
| 102 |
Debugger* dbg, JSObject* handler); |
| 103 |
|
| 104 |
#ifdef DEBUG |
| 105 |
static uint32_t getStepperCount(JSScript* script); |
| 106 |
#endif |
| 107 |
|
| 108 |
/* |
| 109 |
* Increment or decrement the single-step count. If the count is non-zero |
| 110 |
* then the script is in single-step mode. |
| 111 |
* |
| 112 |
* Only incrementing is fallible, as it could allocate a DebugScript. |
| 113 |
*/ |
| 114 |
[[nodiscard]] static bool incrementStepperCount(JSContext* cx, |
| 115 |
HandleScript script); |
| 116 |
static void decrementStepperCount(JS::GCContext* gcx, JSScript* script); |
| 117 |
|
| 118 |
/* |
| 119 |
* Increment or decrement the generator observer count. If the count is |
| 120 |
* non-zero then the script reports resumptions to the debugger. |
| 121 |
* |
| 122 |
* Only incrementing is fallible, as it could allocate a DebugScript. |
| 123 |
*/ |
| 124 |
[[nodiscard]] static bool incrementGeneratorObserverCount( |
| 125 |
JSContext* cx, HandleScript script); |
| 126 |
static void decrementGeneratorObserverCount(JS::GCContext* gcx, |
| 127 |
JSScript* script); |
| 128 |
}; |
| 129 |
|
| 130 |
using UniqueDebugScript = js::UniquePtr<DebugScript, JS::FreePolicy>; |
| 131 |
|
| 132 |
// A JSObject that wraps a DebugScript, so we can use it as the value in a |
| 133 |
// WeakMap. This object owns the DebugScript and is responsible for deleting it. |
| 134 |
class DebugScriptObject : public NativeObject { |
| 135 |
public: |
| 136 |
static const JSClass class_; |
| 137 |
|
| 138 |
enum { ScriptSlot, SlotCount }; |
| 139 |
|
| 140 |
static DebugScriptObject* create(JSContext* cx, UniqueDebugScript debugScript, |
| 141 |
size_t nbytes); |
| 142 |
|
| 143 |
DebugScript* debugScript() const; |
| 144 |
|
| 145 |
private: |
| 146 |
static const JSClassOps classOps_; |
| 147 |
|
| 148 |
static void trace(JSTracer* trc, JSObject* obj); |
| 149 |
static void finalize(JS::GCContext* gcx, JSObject* obj); |
| 150 |
}; |
| 151 |
|
| 152 |
// A weak map from JSScripts to DebugScriptObjects. |
| 153 |
class DebugScriptMap : public WeakMap<JSScript*, DebugScriptObject*> { |
| 154 |
public: |
| 155 |
explicit DebugScriptMap(JSContext* cx) : WeakMap(cx) {} |
| 156 |
}; |
| 157 |
|
| 158 |
} /* namespace js */ |
| 159 |
|
| 160 |
#endif /* dbg_DebugScript_h */ |