summaryrefslogtreecommitdiffstats
path: root/js/src/debugger/DebugScript.cpp
blob: 610784c228a34ebfe189cc94c9cd1235407bf60d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: set ts=8 sts=2 et sw=2 tw=80:
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "debugger/DebugScript.h"

#include "mozilla/Assertions.h"  // for AssertionConditionType
#include "mozilla/HashTable.h"   // for HashMapEntry, HashTable<>::Ptr, HashMap
#include "mozilla/UniquePtr.h"   // for UniquePtr

#include <utility>  // for std::move

#include "debugger/DebugAPI.h"    // for DebugAPI
#include "debugger/Debugger.h"    // for JSBreakpointSite, Breakpoint
#include "gc/Cell.h"              // for TenuredCell
#include "gc/GCContext.h"         // for JS::GCContext
#include "gc/GCEnum.h"            // for MemoryUse, MemoryUse::BreakpointSite
#include "gc/Marking.h"           // for IsAboutToBeFinalized
#include "gc/Zone.h"              // for Zone
#include "gc/ZoneAllocator.h"     // for AddCellMemory
#include "jit/BaselineJIT.h"      // for BaselineScript
#include "vm/BytecodeIterator.h"  // for AllBytecodesIterable
#include "vm/JSContext.h"         // for JSContext
#include "vm/JSScript.h"          // for JSScript, DebugScriptMap
#include "vm/NativeObject.h"      // for NativeObject
#include "vm/Realm.h"             // for Realm, AutoRealm
#include "vm/Runtime.h"           // for ReportOutOfMemory
#include "vm/Stack.h"             // for ActivationIterator, Activation

#include "gc/GC-inl.h"                // for ZoneCellIter
#include "gc/GCContext-inl.h"         // for JS::GCContext::free_
#include "gc/Marking-inl.h"           // for CheckGCThingAfterMovingGC
#include "gc/WeakMap-inl.h"           // for WeakMap::remove
#include "vm/BytecodeIterator-inl.h"  // for AllBytecodesIterable
#include "vm/JSContext-inl.h"         // for JSContext::check
#include "vm/JSObject-inl.h"          // for NewObjectWithGivenProto
#include "vm/JSScript-inl.h"          // for JSScript::hasBaselineScript
#include "vm/Realm-inl.h"             // for AutoRealm::AutoRealm

namespace js {

const JSClass DebugScriptObject::class_ = {
    "DebugScriptObject",
    JSCLASS_HAS_RESERVED_SLOTS(SlotCount) | JSCLASS_BACKGROUND_FINALIZE,
    &classOps_, JS_NULL_CLASS_SPEC};

const JSClassOps DebugScriptObject::classOps_ = {
    nullptr,                      // addProperty
    nullptr,                      // delProperty
    nullptr,                      // enumerate
    nullptr,                      // newEnumerate
    nullptr,                      // resolve
    nullptr,                      // mayResolve
    DebugScriptObject::finalize,  // finalize
    nullptr,                      // call
    nullptr,                      // construct
    DebugScriptObject::trace,     // trace
};

/* static */
DebugScriptObject* DebugScriptObject::create(JSContext* cx,
                                             UniqueDebugScript debugScript,
                                             size_t nbytes) {
  auto* object = NewObjectWithGivenProto<DebugScriptObject>(cx, nullptr);
  if (!object) {
    return nullptr;
  }

  object->initReservedSlot(ScriptSlot, PrivateValue(debugScript.release()));
  AddCellMemory(object, nbytes, MemoryUse::ScriptDebugScript);

  return object;
}

DebugScript* DebugScriptObject::debugScript() const {
  return maybePtrFromReservedSlot<DebugScript>(ScriptSlot);
}

/* static */
void DebugScriptObject::trace(JSTracer* trc, JSObject* obj) {
  DebugScript* debugScript = obj->as<DebugScriptObject>().debugScript();
  if (debugScript) {
    debugScript->trace(trc);
  }
}

/* static */
void DebugScriptObject::finalize(JS::GCContext* gcx, JSObject* obj) {
  DebugScriptObject* object = &obj->as<DebugScriptObject>();
  DebugScript* debugScript = object->debugScript();
  if (debugScript) {
    debugScript->delete_(gcx, object);
  }
}

/* static */
DebugScript* DebugScript::get(JSScript* script) {
  MOZ_ASSERT(script->hasDebugScript());
  DebugScriptMap* map = script->zone()->debugScriptMap;
  MOZ_ASSERT(map);
  DebugScriptMap::Ptr p = map->lookupUnbarriered(script);
  MOZ_ASSERT(p);
  return p->value().get()->as<DebugScriptObject>().debugScript();
}

/* static */
DebugScript* DebugScript::getOrCreate(JSContext* cx, HandleScript script) {
  cx->check(script);

  if (script->hasDebugScript()) {
    return get(script);
  }

  size_t nbytes = allocSize(script->length());
  UniqueDebugScript debug(
      reinterpret_cast<DebugScript*>(cx->pod_calloc<uint8_t>(nbytes)));
  if (!debug) {
    return nullptr;
  }

  debug->codeLength = script->length();

  Rooted<DebugScriptObject*> object(
      cx, DebugScriptObject::create(cx, std::move(debug), nbytes));
  if (!object) {
    return nullptr;
  }

  /* Create zone's debugScriptMap if necessary. */
  Zone* zone = script->zone();
  MOZ_ASSERT(cx->zone() == zone);
  if (!zone->debugScriptMap) {
    DebugScriptMap* map = cx->new_<DebugScriptMap>(cx);
    if (!map) {
      return nullptr;
    }

    zone->debugScriptMap = map;
  }

  MOZ_ASSERT(script->hasBytecode());

  if (!zone->debugScriptMap->putNew(script.get(), object.get())) {
    ReportOutOfMemory(cx);
    return nullptr;
  }

  // It is safe to set this: we can't fail after this point.
  script->setHasDebugScript(true);

  /*
   * Ensure that any Interpret() instances running on this script have
   * interrupts enabled. The interrupts must stay enabled until the
   * debug state is destroyed.
   */
  for (ActivationIterator iter(cx); !iter.done(); ++iter) {
    if (iter->isInterpreter()) {
      iter->asInterpreter()->enableInterruptsIfRunning(script);
    }
  }

  return object->debugScript();
}

/* static */
JSBreakpointSite* DebugScript::getBreakpointSite(JSScript* script,
                                                 jsbytecode* pc) {
  uint32_t offset = script->pcToOffset(pc);
  return script->hasDebugScript() ? get(script)->breakpoints[offset] : nullptr;
}

/* static */
JSBreakpointSite* DebugScript::getOrCreateBreakpointSite(JSContext* cx,
                                                         HandleScript script,
                                                         jsbytecode* pc) {
  AutoRealm ar(cx, script);

  DebugScript* debug = getOrCreate(cx, script);
  if (!debug) {
    return nullptr;
  }

  JSBreakpointSite*& site = debug->breakpoints[script->pcToOffset(pc)];

  if (!site) {
    site = cx->new_<JSBreakpointSite>(script, pc);
    if (!site) {
      return nullptr;
    }
    debug->numSites++;
    AddCellMemory(script, sizeof(JSBreakpointSite), MemoryUse::BreakpointSite);

    if (script->hasBaselineScript()) {
      script->baselineScript()->toggleDebugTraps(script, pc);
    }
  }

  return site;
}

/* static */
void DebugScript::destroyBreakpointSite(JS::GCContext* gcx, JSScript* script,
                                        jsbytecode* pc) {
  DebugScript* debug = get(script);
  JSBreakpointSite*& site = debug->breakpoints[script->pcToOffset(pc)];
  MOZ_ASSERT(site);
  MOZ_ASSERT(site->isEmpty());

  site->delete_(gcx);
  site = nullptr;

  debug->numSites--;
  if (!debug->needed()) {
    DebugAPI::removeDebugScript(gcx, script);
  }

  if (script->hasBaselineScript()) {
    script->baselineScript()->toggleDebugTraps(script, pc);
  }
}

/* static */
void DebugScript::clearBreakpointsIn(JS::GCContext* gcx, JSScript* script,
                                     Debugger* dbg, JSObject* handler) {
  MOZ_ASSERT(script);
  // Breakpoints hold wrappers in the script's compartment for the handler. Make
  // sure we don't try to search for the unwrapped handler.
  MOZ_ASSERT_IF(handler, script->compartment() == handler->compartment());

  if (!script->hasDebugScript()) {
    return;
  }

  AllBytecodesIterable iter(script);
  for (BytecodeLocation loc : iter) {
    JSBreakpointSite* site = getBreakpointSite(script, loc.toRawBytecode());
    if (site) {
      Breakpoint* nextbp;
      for (Breakpoint* bp = site->firstBreakpoint(); bp; bp = nextbp) {
        nextbp = bp->nextInSite();
        if ((!dbg || bp->debugger == dbg) &&
            (!handler || bp->getHandler() == handler)) {
          bp->remove(gcx);
        }
      }
    }
  }
}

#ifdef DEBUG
/* static */
uint32_t DebugScript::getStepperCount(JSScript* script) {
  return script->hasDebugScript() ? get(script)->stepperCount : 0;
}
#endif  // DEBUG

/* static */
bool DebugScript::incrementStepperCount(JSContext* cx, HandleScript script) {
  cx->check(script);
  MOZ_ASSERT(cx->realm()->isDebuggee());

  AutoRealm ar(cx, script);

  DebugScript* debug = getOrCreate(cx, script);
  if (!debug) {
    return false;
  }

  debug->stepperCount++;

  if (debug->stepperCount == 1) {
    if (script->hasBaselineScript()) {
      script->baselineScript()->toggleDebugTraps(script, nullptr);
    }
  }

  return true;
}

/* static */
void DebugScript::decrementStepperCount(JS::GCContext* gcx, JSScript* script) {
  DebugScript* debug = get(script);
  MOZ_ASSERT(debug);
  MOZ_ASSERT(debug->stepperCount > 0);

  debug->stepperCount--;

  if (debug->stepperCount == 0) {
    if (script->hasBaselineScript()) {
      script->baselineScript()->toggleDebugTraps(script, nullptr);
    }

    if (!debug->needed()) {
      DebugAPI::removeDebugScript(gcx, script);
    }
  }
}

/* static */
bool DebugScript::incrementGeneratorObserverCount(JSContext* cx,
                                                  HandleScript script) {
  cx->check(script);
  MOZ_ASSERT(cx->realm()->isDebuggee());

  AutoRealm ar(cx, script);

  DebugScript* debug = getOrCreate(cx, script);
  if (!debug) {
    return false;
  }

  debug->generatorObserverCount++;

  // It is our caller's responsibility, before bumping the generator observer
  // count, to make sure that the baseline code includes the necessary
  // JSOp::AfterYield instrumentation by calling
  // {ensure,update}ExecutionObservabilityOfScript.
  MOZ_ASSERT_IF(script->hasBaselineScript(),
                script->baselineScript()->hasDebugInstrumentation());

  return true;
}

/* static */
void DebugScript::decrementGeneratorObserverCount(JS::GCContext* gcx,
                                                  JSScript* script) {
  DebugScript* debug = get(script);
  MOZ_ASSERT(debug);
  MOZ_ASSERT(debug->generatorObserverCount > 0);

  debug->generatorObserverCount--;

  if (!debug->needed()) {
    DebugAPI::removeDebugScript(gcx, script);
  }
}

void DebugScript::trace(JSTracer* trc) {
  for (size_t i = 0; i < codeLength; i++) {
    JSBreakpointSite* site = breakpoints[i];
    if (site) {
      site->trace(trc);
    }
  }
}

/* static */
void DebugAPI::removeDebugScript(JS::GCContext* gcx, JSScript* script) {
  if (script->hasDebugScript()) {
    if (IsAboutToBeFinalizedUnbarriered(script)) {
      // The script is dying and all breakpoint data will be cleaned up.
      return;
    }

    DebugScriptMap* map = script->zone()->debugScriptMap;
    MOZ_ASSERT(map);
    DebugScriptMap::Ptr p = map->lookupUnbarriered(script);
    MOZ_ASSERT(p);
    map->remove(p);
    script->setHasDebugScript(false);

    // The DebugScript will be destroyed at the next GC when its owning
    // DebugScriptObject dies.
  }
}

void DebugScript::delete_(JS::GCContext* gcx, DebugScriptObject* owner) {
  for (size_t i = 0; i < codeLength; i++) {
    JSBreakpointSite* site = breakpoints[i];
    if (site) {
      site->delete_(gcx);
    }
  }

  gcx->free_(owner, this, allocSize(codeLength), MemoryUse::ScriptDebugScript);
}

#ifdef JSGC_HASH_TABLE_CHECKS
/* static */
void DebugAPI::checkDebugScriptAfterMovingGC(DebugScript* ds) {
  for (uint32_t i = 0; i < ds->numSites; i++) {
    JSBreakpointSite* site = ds->breakpoints[i];
    if (site) {
      CheckGCThingAfterMovingGC(site->script.get());
    }
  }
}
#endif  // JSGC_HASH_TABLE_CHECKS

/* static */
bool DebugAPI::stepModeEnabledSlow(JSScript* script) {
  return DebugScript::get(script)->stepperCount > 0;
}

/* static */
bool DebugAPI::hasBreakpointsAtSlow(JSScript* script, jsbytecode* pc) {
  JSBreakpointSite* site = DebugScript::getBreakpointSite(script, pc);
  return !!site;
}

/* static */
void DebugAPI::traceDebugScriptMap(JSTracer* trc, DebugScriptMap* map) {
  map->trace(trc);
}

/* static */
void DebugAPI::deleteDebugScriptMap(DebugScriptMap* map) { js_delete(map); }

}  // namespace js