Debian Codegraph @

js/src/debugger/DebugScript.cpp

raw
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 #include "debugger/DebugScript.h"
8
9 #include "mozilla/Assertions.h" // for AssertionConditionType
10 #include "mozilla/HashTable.h" // for HashMapEntry, HashTable<>::Ptr, HashMap
11 #include "mozilla/UniquePtr.h" // for UniquePtr
12
13 #include <utility> // for std::move
14
15 #include "debugger/DebugAPI.h" // for DebugAPI
16 #include "debugger/Debugger.h" // for JSBreakpointSite, Breakpoint
17 #include "gc/Cell.h" // for TenuredCell
18 #include "gc/GCContext.h" // for JS::GCContext
19 #include "gc/GCEnum.h" // for MemoryUse, MemoryUse::BreakpointSite
20 #include "gc/Marking.h" // for IsAboutToBeFinalized
21 #include "gc/Zone.h" // for Zone
22 #include "gc/ZoneAllocator.h" // for AddCellMemory
23 #include "jit/BaselineJIT.h" // for BaselineScript
24 #include "vm/BytecodeIterator.h" // for AllBytecodesIterable
25 #include "vm/JSContext.h" // for JSContext
26 #include "vm/JSScript.h" // for JSScript, DebugScriptMap
27 #include "vm/NativeObject.h" // for NativeObject
28 #include "vm/Realm.h" // for Realm, AutoRealm
29 #include "vm/Runtime.h" // for ReportOutOfMemory
30 #include "vm/Stack.h" // for ActivationIterator, Activation
31
32 #include "gc/GC-inl.h" // for ZoneCellIter
33 #include "gc/GCContext-inl.h" // for JS::GCContext::free_
34 #include "gc/Marking-inl.h" // for CheckGCThingAfterMovingGC
35 #include "gc/WeakMap-inl.h" // for WeakMap::remove
36 #include "vm/BytecodeIterator-inl.h" // for AllBytecodesIterable
37 #include "vm/JSContext-inl.h" // for JSContext::check
38 #include "vm/JSObject-inl.h" // for NewObjectWithGivenProto
39 #include "vm/JSScript-inl.h" // for JSScript::hasBaselineScript
40 #include "vm/Realm-inl.h" // for AutoRealm::AutoRealm
41
42 namespace js {
43
44 const JSClass DebugScriptObject::class_ = {
45 "DebugScriptObject",
46 JSCLASS_HAS_RESERVED_SLOTS(SlotCount) | JSCLASS_BACKGROUND_FINALIZE,
47 &classOps_,
48 JS_NULL_CLASS_SPEC,
49 };
50
51 const JSClassOps DebugScriptObject::classOps_ = {
52 nullptr, // addProperty
53 nullptr, // delProperty
54 nullptr, // enumerate
55 nullptr, // newEnumerate
56 nullptr, // resolve
57 nullptr, // mayResolve
58 DebugScriptObject::finalize, // finalize
59 nullptr, // call
60 nullptr, // construct
61 DebugScriptObject::trace, // trace
62 };
63
64 /* static */
65 DebugScriptObject* DebugScriptObject::create(JSContext* cx,
66 UniqueDebugScript debugScript,
67 size_t nbytes) {
68 auto* object = NewObjectWithGivenProto<DebugScriptObject>(cx, nullptr);
69 if (!object) {
70 return nullptr;
71 }
72
73 object->initReservedSlot(ScriptSlot, PrivateValue(debugScript.release()));
74 AddCellMemory(object, nbytes, MemoryUse::ScriptDebugScript);
75
76 return object;
77 }
78
79 DebugScript* DebugScriptObject::debugScript() const {
80 return maybePtrFromReservedSlot<DebugScript>(ScriptSlot);
81 }
82
83 /* static */
84 void DebugScriptObject::trace(JSTracer* trc, JSObject* obj) {
85 DebugScript* debugScript = obj->as<DebugScriptObject>().debugScript();
86 if (debugScript) {
87 debugScript->trace(trc);
88 }
89 }
90
91 /* static */
92 void DebugScriptObject::finalize(JS::GCContext* gcx, JSObject* obj) {
93 DebugScriptObject* object = &obj->as<DebugScriptObject>();
94 DebugScript* debugScript = object->debugScript();
95 if (debugScript) {
96 debugScript->delete_(gcx, object);
97 }
98 }
99
100 /* static */
101 DebugScript* DebugScript::get(JSScript* script) {
102 MOZ_ASSERT(!IsAboutToBeFinalizedUnbarriered(script));
103 MOZ_ASSERT(script->hasDebugScript());
104 DebugScriptMap* map = script->zone()->debugScriptMap;
105 MOZ_ASSERT(map);
106 DebugScriptMap::Ptr p = map->lookupUnbarriered(script);
107 MOZ_ASSERT(p);
108 return p->value().get()->as<DebugScriptObject>().debugScript();
109 }
110
111 /* static */
112 DebugScript* DebugScript::getOrCreate(JSContext* cx, HandleScript script) {
113 cx->check(script);
114
115 if (script->hasDebugScript()) {
116 return get(script);
117 }
118
119 size_t nbytes = allocSize(script->length());
120 UniqueDebugScript debug(
121 reinterpret_cast<DebugScript*>(cx->pod_calloc<uint8_t>(nbytes)));
122 if (!debug) {
123 return nullptr;
124 }
125
126 debug->codeLength = script->length();
127
128 Rooted<DebugScriptObject*> object(
129 cx, DebugScriptObject::create(cx, std::move(debug), nbytes));
130 if (!object) {
131 return nullptr;
132 }
133
134 /* Create zone's debugScriptMap if necessary. */
135 Zone* zone = script->zone();
136 MOZ_ASSERT(cx->zone() == zone);
137 if (!zone->debugScriptMap) {
138 DebugScriptMap* map = cx->new_<DebugScriptMap>(cx);
139 if (!map) {
140 return nullptr;
141 }
142
143 zone->debugScriptMap = map;
144 }
145
146 MOZ_ASSERT(script->hasBytecode());
147
148 if (!zone->debugScriptMap->putNew(script.get(), object.get())) {
149 ReportOutOfMemory(cx);
150 return nullptr;
151 }
152
153 // It is safe to set this: we can't fail after this point.
154 script->setHasDebugScript(true);
155
156 /*
157 * Ensure that any Interpret() instances running on this script have
158 * interrupts enabled. The interrupts must stay enabled until the
159 * debug state is destroyed.
160 */
161 for (ActivationIterator iter(cx); !iter.done(); ++iter) {
162 if (iter->isInterpreter()) {
163 iter->asInterpreter()->enableInterruptsIfRunning(script);
164 }
165 }
166
167 return object->debugScript();
168 }
169
170 /* static */
171 JSBreakpointSite* DebugScript::getBreakpointSite(JSScript* script,
172 jsbytecode* pc) {
173 uint32_t offset = script->pcToOffset(pc);
174 return script->hasDebugScript() ? get(script)->breakpoints[offset] : nullptr;
175 }
176
177 /* static */
178 JSBreakpointSite* DebugScript::getOrCreateBreakpointSite(JSContext* cx,
179 HandleScript script,
180 jsbytecode* pc) {
181 AutoRealm ar(cx, script);
182
183 DebugScript* debug = getOrCreate(cx, script);
184 if (!debug) {
185 return nullptr;
186 }
187
188 JSBreakpointSite*& site = debug->breakpoints[script->pcToOffset(pc)];
189
190 if (!site) {
191 site = cx->new_<JSBreakpointSite>(script, pc);
192 if (!site) {
193 return nullptr;
194 }
195 debug->numSites++;
196 AddCellMemory(script, sizeof(JSBreakpointSite), MemoryUse::BreakpointSite);
197
198 if (script->hasBaselineScript()) {
199 script->baselineScript()->toggleDebugTraps(script, pc);
200 }
201 }
202
203 return site;
204 }
205
206 /* static */
207 void DebugScript::destroyBreakpointSite(JS::GCContext* gcx, JSScript* script,
208 jsbytecode* pc) {
209 if (IsAboutToBeFinalizedUnbarriered(script)) {
210 return;
211 }
212
213 DebugScript* debug = get(script);
214
215 JSBreakpointSite*& site = debug->breakpoints[script->pcToOffset(pc)];
216 MOZ_ASSERT(site);
217 MOZ_ASSERT(site->isEmpty());
218
219 site->delete_(gcx);
220 site = nullptr;
221
222 debug->numSites--;
223 if (!debug->needed()) {
224 DebugAPI::removeDebugScript(gcx, script);
225 }
226
227 if (script->hasBaselineScript()) {
228 script->baselineScript()->toggleDebugTraps(script, pc);
229 }
230 }
231
232 /* static */
233 void DebugScript::clearBreakpointsIn(JS::GCContext* gcx, JSScript* script,
234 Debugger* dbg, JSObject* handler) {
235 MOZ_ASSERT(script);
236 // Breakpoints hold wrappers in the script's compartment for the handler. Make
237 // sure we don't try to search for the unwrapped handler.
238 MOZ_ASSERT_IF(handler, script->compartment() == handler->compartment());
239
240 if (!script->hasDebugScript()) {
241 return;
242 }
243
244 AllBytecodesIterable iter(script);
245 for (BytecodeLocation loc : iter) {
246 JSBreakpointSite* site = getBreakpointSite(script, loc.toRawBytecode());
247 if (site) {
248 Breakpoint* nextbp;
249 for (Breakpoint* bp = site->firstBreakpoint(); bp; bp = nextbp) {
250 nextbp = bp->nextInSite();
251 if ((!dbg || bp->debugger == dbg) &&
252 (!handler || bp->getHandler() == handler)) {
253 bp->remove(gcx);
254 }
255 }
256 }
257 }
258 }
259
260 #ifdef DEBUG
261 /* static */
262 uint32_t DebugScript::getStepperCount(JSScript* script) {
263 return script->hasDebugScript() ? get(script)->stepperCount : 0;
264 }
265 #endif // DEBUG
266
267 /* static */
268 bool DebugScript::incrementStepperCount(JSContext* cx, HandleScript script) {
269 cx->check(script);
270 MOZ_ASSERT(cx->realm()->isDebuggee());
271
272 AutoRealm ar(cx, script);
273
274 DebugScript* debug = getOrCreate(cx, script);
275 if (!debug) {
276 return false;
277 }
278
279 debug->stepperCount++;
280
281 if (debug->stepperCount == 1) {
282 if (script->hasBaselineScript()) {
283 script->baselineScript()->toggleDebugTraps(script, nullptr);
284 }
285 }
286
287 return true;
288 }
289
290 /* static */
291 void DebugScript::decrementStepperCount(JS::GCContext* gcx, JSScript* script) {
292 if (IsAboutToBeFinalizedUnbarriered(script)) {
293 return;
294 }
295
296 DebugScript* debug = get(script);
297 MOZ_ASSERT(debug);
298 MOZ_ASSERT(debug->stepperCount > 0);
299
300 debug->stepperCount--;
301
302 if (debug->stepperCount == 0) {
303 if (script->hasBaselineScript()) {
304 script->baselineScript()->toggleDebugTraps(script, nullptr);
305 }
306
307 if (!debug->needed()) {
308 DebugAPI::removeDebugScript(gcx, script);
309 }
310 }
311 }
312
313 /* static */
314 bool DebugScript::incrementGeneratorObserverCount(JSContext* cx,
315 HandleScript script) {
316 cx->check(script);
317 MOZ_ASSERT(cx->realm()->isDebuggee());
318
319 AutoRealm ar(cx, script);
320
321 DebugScript* debug = getOrCreate(cx, script);
322 if (!debug) {
323 return false;
324 }
325
326 debug->generatorObserverCount++;
327
328 // It is our caller's responsibility, before bumping the generator observer
329 // count, to make sure that the baseline code includes the necessary
330 // JSOp::AfterYield instrumentation by calling
331 // {ensure,update}ExecutionObservabilityOfScript.
332 MOZ_ASSERT_IF(script->hasBaselineScript(),
333 script->baselineScript()->hasDebugInstrumentation());
334
335 return true;
336 }
337
338 /* static */
339 void DebugScript::decrementGeneratorObserverCount(JS::GCContext* gcx,
340 JSScript* script) {
341 if (IsAboutToBeFinalizedUnbarriered(script)) {
342 return;
343 }
344
345 DebugScript* debug = get(script);
346 MOZ_ASSERT(debug);
347 MOZ_ASSERT(debug->generatorObserverCount > 0);
348
349 debug->generatorObserverCount--;
350
351 if (!debug->needed()) {
352 DebugAPI::removeDebugScript(gcx, script);
353 }
354 }
355
356 void DebugScript::trace(JSTracer* trc) {
357 for (size_t i = 0; i < codeLength; i++) {
358 JSBreakpointSite* site = breakpoints[i];
359 if (site) {
360 site->trace(trc);
361 }
362 }
363 }
364
365 /* static */
366 void DebugAPI::removeDebugScript(JS::GCContext* gcx, JSScript* script) {
367 if (script->hasDebugScript()) {
368 if (IsAboutToBeFinalizedUnbarriered(script)) {
369 // The script is dying and all breakpoint data will be cleaned up.
370 return;
371 }
372
373 DebugScriptMap* map = script->zone()->debugScriptMap;
374 MOZ_ASSERT(map);
375 DebugScriptMap::Ptr p = map->lookupUnbarriered(script);
376 MOZ_ASSERT(p);
377 map->remove(p);
378 script->setHasDebugScript(false);
379
380 // The DebugScript will be destroyed at the next GC when its owning
381 // DebugScriptObject dies.
382 }
383 }
384
385 void DebugScript::delete_(JS::GCContext* gcx, DebugScriptObject* owner) {
386 for (size_t i = 0; i < codeLength; i++) {
387 JSBreakpointSite* site = breakpoints[i];
388 if (site) {
389 site->delete_(gcx);
390 }
391 }
392
393 gcx->free_(owner, this, allocSize(codeLength), MemoryUse::ScriptDebugScript);
394 }
395
396 #ifdef JSGC_HASH_TABLE_CHECKS
397 /* static */
398 void DebugAPI::checkDebugScriptAfterMovingGC(DebugScript* ds) {
399 for (uint32_t i = 0; i < ds->numSites; i++) {
400 JSBreakpointSite* site = ds->breakpoints[i];
401 if (site) {
402 CheckGCThingAfterMovingGC(site->script.get());
403 }
404 }
405 }
406 #endif // JSGC_HASH_TABLE_CHECKS
407
408 /* static */
409 bool DebugAPI::stepModeEnabledSlow(JSScript* script) {
410 if (IsAboutToBeFinalizedUnbarriered(script)) {
411 return false;
412 }
413
414 return DebugScript::get(script)->stepperCount > 0;
415 }
416
417 /* static */
418 bool DebugAPI::hasBreakpointsAtSlow(JSScript* script, jsbytecode* pc) {
419 JSBreakpointSite* site = DebugScript::getBreakpointSite(script, pc);
420 return !!site;
421 }
422
423 /* static */
424 void DebugAPI::traceDebugScriptMap(JSTracer* trc, DebugScriptMap* map) {
425 map->trace(trc);
426 }
427
428 /* static */
429 void DebugAPI::deleteDebugScriptMap(DebugScriptMap* map) { js_delete(map); }
430
431 } // namespace js