| 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/DebuggerMemory.h" |
| 8 |
|
| 9 |
#include "mozilla/Maybe.h" |
| 10 |
#include "mozilla/Vector.h" |
| 11 |
|
| 12 |
#include <stdlib.h> |
| 13 |
#include <utility> |
| 14 |
|
| 15 |
#include "jsapi.h" |
| 16 |
|
| 17 |
#include "builtin/MapObject.h" |
| 18 |
#include "debugger/Debugger.h" |
| 19 |
#include "gc/Marking.h" |
| 20 |
#include "js/AllocPolicy.h" |
| 21 |
#include "js/Debug.h" |
| 22 |
#include "js/friend/ErrorMessages.h" // js::GetErrorMessage, JSMSG_* |
| 23 |
#include "js/PropertySpec.h" |
| 24 |
#include "js/TracingAPI.h" |
| 25 |
#include "js/UbiNode.h" |
| 26 |
#include "js/UbiNodeCensus.h" |
| 27 |
#include "js/Utility.h" |
| 28 |
#include "vm/GlobalObject.h" |
| 29 |
#include "vm/JSContext.h" |
| 30 |
#include "vm/PlainObject.h" // js::PlainObject |
| 31 |
#include "vm/Realm.h" |
| 32 |
#include "vm/SavedStacks.h" |
| 33 |
|
| 34 |
#include "debugger/Debugger-inl.h" |
| 35 |
#include "gc/StableCellHasher-inl.h" |
| 36 |
#include "vm/NativeObject-inl.h" |
| 37 |
|
| 38 |
using namespace js; |
| 39 |
|
| 40 |
/* static */ |
| 41 |
DebuggerMemory* DebuggerMemory::create(JSContext* cx, Debugger* dbg) { |
| 42 |
Value memoryProtoValue = |
| 43 |
dbg->object->getReservedSlot(Debugger::JSSLOT_DEBUG_MEMORY_PROTO); |
| 44 |
RootedObject memoryProto(cx, &memoryProtoValue.toObject()); |
| 45 |
Rooted<DebuggerMemory*> memory( |
| 46 |
cx, NewObjectWithGivenProto<DebuggerMemory>(cx, memoryProto)); |
| 47 |
if (!memory) { |
| 48 |
return nullptr; |
| 49 |
} |
| 50 |
|
| 51 |
dbg->object->setReservedSlot(Debugger::JSSLOT_DEBUG_MEMORY_INSTANCE, |
| 52 |
ObjectValue(*memory)); |
| 53 |
memory->setReservedSlot(JSSLOT_DEBUGGER, ObjectValue(*dbg->object)); |
| 54 |
|
| 55 |
return memory; |
| 56 |
} |
| 57 |
|
| 58 |
Debugger* DebuggerMemory::getDebugger() { |
| 59 |
const Value& dbgVal = getReservedSlot(JSSLOT_DEBUGGER); |
| 60 |
return Debugger::fromJSObject(&dbgVal.toObject()); |
| 61 |
} |
| 62 |
|
| 63 |
/* static */ |
| 64 |
bool DebuggerMemory::construct(JSContext* cx, unsigned argc, Value* vp) { |
| 65 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_NO_CONSTRUCTOR, |
| 66 |
"Debugger.Source"); |
| 67 |
return false; |
| 68 |
} |
| 69 |
|
| 70 |
/* static */ const JSClass DebuggerMemory::class_ = { |
| 71 |
"Memory", |
| 72 |
JSCLASS_HAS_RESERVED_SLOTS(JSSLOT_COUNT), |
| 73 |
}; |
| 74 |
|
| 75 |
/* static */ |
| 76 |
DebuggerMemory* DebuggerMemory::checkThis(JSContext* cx, CallArgs& args) { |
| 77 |
const Value& thisValue = args.thisv(); |
| 78 |
|
| 79 |
if (!thisValue.isObject()) { |
| 80 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 81 |
JSMSG_OBJECT_REQUIRED, |
| 82 |
InformalValueTypeName(thisValue)); |
| 83 |
return nullptr; |
| 84 |
} |
| 85 |
|
| 86 |
JSObject& thisObject = thisValue.toObject(); |
| 87 |
if (!thisObject.is<DebuggerMemory>()) { |
| 88 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 89 |
JSMSG_INCOMPATIBLE_PROTO, class_.name, "method", |
| 90 |
thisObject.getClass()->name); |
| 91 |
return nullptr; |
| 92 |
} |
| 93 |
|
| 94 |
return &thisObject.as<DebuggerMemory>(); |
| 95 |
} |
| 96 |
|
| 97 |
struct MOZ_STACK_CLASS DebuggerMemory::CallData { |
| 98 |
JSContext* cx; |
| 99 |
const CallArgs& args; |
| 100 |
|
| 101 |
Handle<DebuggerMemory*> memory; |
| 102 |
|
| 103 |
CallData(JSContext* cx, const CallArgs& args, Handle<DebuggerMemory*> memory) |
| 104 |
: cx(cx), args(args), memory(memory) {} |
| 105 |
|
| 106 |
// Accessor properties of Debugger.Memory.prototype. |
| 107 |
|
| 108 |
bool setTrackingAllocationSites(); |
| 109 |
bool getTrackingAllocationSites(); |
| 110 |
bool setMaxAllocationsLogLength(); |
| 111 |
bool getMaxAllocationsLogLength(); |
| 112 |
bool setAllocationSamplingProbability(); |
| 113 |
bool getAllocationSamplingProbability(); |
| 114 |
bool getAllocationsLogOverflowed(); |
| 115 |
bool getOnGarbageCollection(); |
| 116 |
bool setOnGarbageCollection(); |
| 117 |
|
| 118 |
// Function properties of Debugger.Memory.prototype. |
| 119 |
|
| 120 |
bool takeCensus(); |
| 121 |
bool drainAllocationsLog(); |
| 122 |
|
| 123 |
using Method = bool (CallData::*)(); |
| 124 |
|
| 125 |
template <Method MyMethod> |
| 126 |
static bool ToNative(JSContext* cx, unsigned argc, Value* vp); |
| 127 |
}; |
| 128 |
|
| 129 |
template <DebuggerMemory::CallData::Method MyMethod> |
| 130 |
/* static */ |
| 131 |
bool DebuggerMemory::CallData::ToNative(JSContext* cx, unsigned argc, |
| 132 |
Value* vp) { |
| 133 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 134 |
|
| 135 |
Rooted<DebuggerMemory*> memory(cx, DebuggerMemory::checkThis(cx, args)); |
| 136 |
if (!memory) { |
| 137 |
return false; |
| 138 |
} |
| 139 |
|
| 140 |
CallData data(cx, args, memory); |
| 141 |
return (data.*MyMethod)(); |
| 142 |
} |
| 143 |
|
| 144 |
static bool undefined(const CallArgs& args) { |
| 145 |
args.rval().setUndefined(); |
| 146 |
return true; |
| 147 |
} |
| 148 |
|
| 149 |
bool DebuggerMemory::CallData::setTrackingAllocationSites() { |
| 150 |
if (!args.requireAtLeast(cx, "(set trackingAllocationSites)", 1)) { |
| 151 |
return false; |
| 152 |
} |
| 153 |
|
| 154 |
Debugger* dbg = memory->getDebugger(); |
| 155 |
bool enabling = ToBoolean(args[0]); |
| 156 |
|
| 157 |
if (enabling == dbg->trackingAllocationSites) { |
| 158 |
return undefined(args); |
| 159 |
} |
| 160 |
|
| 161 |
dbg->trackingAllocationSites = enabling; |
| 162 |
|
| 163 |
if (enabling) { |
| 164 |
if (!dbg->addAllocationsTrackingForAllDebuggees(cx)) { |
| 165 |
dbg->trackingAllocationSites = false; |
| 166 |
return false; |
| 167 |
} |
| 168 |
} else { |
| 169 |
dbg->removeAllocationsTrackingForAllDebuggees(); |
| 170 |
} |
| 171 |
|
| 172 |
return undefined(args); |
| 173 |
} |
| 174 |
|
| 175 |
bool DebuggerMemory::CallData::getTrackingAllocationSites() { |
| 176 |
args.rval().setBoolean(memory->getDebugger()->trackingAllocationSites); |
| 177 |
return true; |
| 178 |
} |
| 179 |
|
| 180 |
bool DebuggerMemory::CallData::drainAllocationsLog() { |
| 181 |
Debugger* dbg = memory->getDebugger(); |
| 182 |
|
| 183 |
if (!dbg->trackingAllocationSites) { |
| 184 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 185 |
JSMSG_NOT_TRACKING_ALLOCATIONS, |
| 186 |
"drainAllocationsLog"); |
| 187 |
return false; |
| 188 |
} |
| 189 |
|
| 190 |
size_t length = dbg->allocationsLog.length(); |
| 191 |
|
| 192 |
Rooted<ArrayObject*> result(cx, NewDenseFullyAllocatedArray(cx, length)); |
| 193 |
if (!result) { |
| 194 |
return false; |
| 195 |
} |
| 196 |
result->ensureDenseInitializedLength(0, length); |
| 197 |
|
| 198 |
for (size_t i = 0; i < length; i++) { |
| 199 |
Rooted<PlainObject*> obj(cx, NewPlainObject(cx)); |
| 200 |
if (!obj) { |
| 201 |
return false; |
| 202 |
} |
| 203 |
|
| 204 |
// Don't pop the AllocationsLogEntry yet. The queue's links are followed |
| 205 |
// by the GC to find the AllocationsLogEntry, but are not barriered, so |
| 206 |
// we must edit them with great care. Use the queue entry in place, and |
| 207 |
// then pop and delete together. |
| 208 |
Debugger::AllocationsLogEntry& entry = dbg->allocationsLog.front(); |
| 209 |
|
| 210 |
RootedValue frame(cx, ObjectOrNullValue(entry.frame)); |
| 211 |
if (!DefineDataProperty(cx, obj, cx->names().frame, frame)) { |
| 212 |
return false; |
| 213 |
} |
| 214 |
|
| 215 |
double when = |
| 216 |
(entry.when - mozilla::TimeStamp::ProcessCreation()).ToMilliseconds(); |
| 217 |
RootedValue timestampValue(cx, NumberValue(when)); |
| 218 |
if (!DefineDataProperty(cx, obj, cx->names().timestamp, timestampValue)) { |
| 219 |
return false; |
| 220 |
} |
| 221 |
|
| 222 |
RootedString className( |
| 223 |
cx, Atomize(cx, entry.className, strlen(entry.className))); |
| 224 |
if (!className) { |
| 225 |
return false; |
| 226 |
} |
| 227 |
RootedValue classNameValue(cx, StringValue(className)); |
| 228 |
if (!DefineDataProperty(cx, obj, cx->names().class_, classNameValue)) { |
| 229 |
return false; |
| 230 |
} |
| 231 |
|
| 232 |
RootedValue size(cx, NumberValue(entry.size)); |
| 233 |
if (!DefineDataProperty(cx, obj, cx->names().size, size)) { |
| 234 |
return false; |
| 235 |
} |
| 236 |
|
| 237 |
RootedValue inNursery(cx, BooleanValue(entry.inNursery)); |
| 238 |
if (!DefineDataProperty(cx, obj, cx->names().inNursery, inNursery)) { |
| 239 |
return false; |
| 240 |
} |
| 241 |
|
| 242 |
result->setDenseElement(i, ObjectValue(*obj)); |
| 243 |
|
| 244 |
// Pop the front queue entry, and delete it immediately, so that the GC |
| 245 |
// sees the AllocationsLogEntry's HeapPtr barriers run atomically with |
| 246 |
// the change to the graph (the queue link). |
| 247 |
dbg->allocationsLog.popFront(); |
| 248 |
} |
| 249 |
|
| 250 |
dbg->allocationsLogOverflowed = false; |
| 251 |
args.rval().setObject(*result); |
| 252 |
return true; |
| 253 |
} |
| 254 |
|
| 255 |
bool DebuggerMemory::CallData::getMaxAllocationsLogLength() { |
| 256 |
args.rval().setInt32(memory->getDebugger()->maxAllocationsLogLength); |
| 257 |
return true; |
| 258 |
} |
| 259 |
|
| 260 |
bool DebuggerMemory::CallData::setMaxAllocationsLogLength() { |
| 261 |
if (!args.requireAtLeast(cx, "(set maxAllocationsLogLength)", 1)) { |
| 262 |
return false; |
| 263 |
} |
| 264 |
|
| 265 |
int32_t max; |
| 266 |
if (!ToInt32(cx, args[0], &max)) { |
| 267 |
return false; |
| 268 |
} |
| 269 |
|
| 270 |
if (max < 1) { |
| 271 |
JS_ReportErrorNumberASCII( |
| 272 |
cx, GetErrorMessage, nullptr, JSMSG_UNEXPECTED_TYPE, |
| 273 |
"(set maxAllocationsLogLength)'s parameter", "not a positive integer"); |
| 274 |
return false; |
| 275 |
} |
| 276 |
|
| 277 |
Debugger* dbg = memory->getDebugger(); |
| 278 |
dbg->maxAllocationsLogLength = max; |
| 279 |
|
| 280 |
while (dbg->allocationsLog.length() > dbg->maxAllocationsLogLength) { |
| 281 |
dbg->allocationsLog.popFront(); |
| 282 |
} |
| 283 |
|
| 284 |
args.rval().setUndefined(); |
| 285 |
return true; |
| 286 |
} |
| 287 |
|
| 288 |
bool DebuggerMemory::CallData::getAllocationSamplingProbability() { |
| 289 |
args.rval().setDouble(memory->getDebugger()->allocationSamplingProbability); |
| 290 |
return true; |
| 291 |
} |
| 292 |
|
| 293 |
bool DebuggerMemory::CallData::setAllocationSamplingProbability() { |
| 294 |
if (!args.requireAtLeast(cx, "(set allocationSamplingProbability)", 1)) { |
| 295 |
return false; |
| 296 |
} |
| 297 |
|
| 298 |
double probability; |
| 299 |
if (!ToNumber(cx, args[0], &probability)) { |
| 300 |
return false; |
| 301 |
} |
| 302 |
|
| 303 |
// Careful! This must also reject NaN. |
| 304 |
if (!(0.0 <= probability && probability <= 1.0)) { |
| 305 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 306 |
JSMSG_UNEXPECTED_TYPE, |
| 307 |
"(set allocationSamplingProbability)'s parameter", |
| 308 |
"not a number between 0 and 1"); |
| 309 |
return false; |
| 310 |
} |
| 311 |
|
| 312 |
Debugger* dbg = memory->getDebugger(); |
| 313 |
if (dbg->allocationSamplingProbability != probability) { |
| 314 |
dbg->allocationSamplingProbability = probability; |
| 315 |
|
| 316 |
// If this is a change any debuggees would observe, have all debuggee |
| 317 |
// realms recompute their sampling probabilities. |
| 318 |
if (dbg->trackingAllocationSites) { |
| 319 |
for (auto r = dbg->debuggees.all(); !r.empty(); r.popFront()) { |
| 320 |
r.front()->realm()->chooseAllocationSamplingProbability(); |
| 321 |
} |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
args.rval().setUndefined(); |
| 326 |
return true; |
| 327 |
} |
| 328 |
|
| 329 |
bool DebuggerMemory::CallData::getAllocationsLogOverflowed() { |
| 330 |
args.rval().setBoolean(memory->getDebugger()->allocationsLogOverflowed); |
| 331 |
return true; |
| 332 |
} |
| 333 |
|
| 334 |
bool DebuggerMemory::CallData::getOnGarbageCollection() { |
| 335 |
return Debugger::getGarbageCollectionHook(cx, args, *memory->getDebugger()); |
| 336 |
} |
| 337 |
|
| 338 |
bool DebuggerMemory::CallData::setOnGarbageCollection() { |
| 339 |
return Debugger::setGarbageCollectionHook(cx, args, *memory->getDebugger()); |
| 340 |
} |
| 341 |
|
| 342 |
/* Debugger.Memory.prototype.takeCensus */ |
| 343 |
|
| 344 |
JS_PUBLIC_API void JS::dbg::SetDebuggerMallocSizeOf( |
| 345 |
JSContext* cx, mozilla::MallocSizeOf mallocSizeOf) { |
| 346 |
cx->runtime()->debuggerMallocSizeOf = mallocSizeOf; |
| 347 |
} |
| 348 |
|
| 349 |
JS_PUBLIC_API mozilla::MallocSizeOf JS::dbg::GetDebuggerMallocSizeOf( |
| 350 |
JSContext* cx) { |
| 351 |
return cx->runtime()->debuggerMallocSizeOf; |
| 352 |
} |
| 353 |
|
| 354 |
using JS::ubi::Census; |
| 355 |
using JS::ubi::CountBasePtr; |
| 356 |
using JS::ubi::CountTypePtr; |
| 357 |
|
| 358 |
// The takeCensus function works in three phases: |
| 359 |
// |
| 360 |
// 1) We examine the 'breakdown' property of our 'options' argument, and |
| 361 |
// use that to build a CountType tree. |
| 362 |
// |
| 363 |
// 2) We create a count node for the root of our CountType tree, and then walk |
| 364 |
// the heap, counting each node we find, expanding our tree of counts as we |
| 365 |
// go. |
| 366 |
// |
| 367 |
// 3) We walk the tree of counts and produce JavaScript objects reporting the |
| 368 |
// accumulated results. |
| 369 |
bool DebuggerMemory::CallData::takeCensus() { |
| 370 |
Census census(cx); |
| 371 |
CountTypePtr rootType; |
| 372 |
|
| 373 |
RootedObject options(cx); |
| 374 |
if (args.get(0).isObject()) { |
| 375 |
options = &args[0].toObject(); |
| 376 |
} |
| 377 |
|
| 378 |
if (!JS::ubi::ParseCensusOptions(cx, census, options, rootType)) { |
| 379 |
return false; |
| 380 |
} |
| 381 |
|
| 382 |
JS::ubi::RootedCount rootCount(cx, rootType->makeCount()); |
| 383 |
if (!rootCount) { |
| 384 |
ReportOutOfMemory(cx); |
| 385 |
return false; |
| 386 |
} |
| 387 |
JS::ubi::CensusHandler handler(census, rootCount, |
| 388 |
cx->runtime()->debuggerMallocSizeOf); |
| 389 |
|
| 390 |
Debugger* dbg = memory->getDebugger(); |
| 391 |
RootedObject dbgObj(cx, dbg->object); |
| 392 |
|
| 393 |
// Populate our target set of debuggee zones. |
| 394 |
for (WeakGlobalObjectSet::Range r = dbg->allDebuggees(); !r.empty(); |
| 395 |
r.popFront()) { |
| 396 |
if (!census.targetZones.put(r.front()->zone())) { |
| 397 |
ReportOutOfMemory(cx); |
| 398 |
return false; |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
{ |
| 403 |
JS::ubi::RootList rootList(cx); |
| 404 |
auto [ok, nogc] = rootList.init(dbgObj); |
| 405 |
if (!ok) { |
| 406 |
ReportOutOfMemory(cx); |
| 407 |
return false; |
| 408 |
} |
| 409 |
|
| 410 |
JS::ubi::CensusTraversal traversal(cx, handler, nogc); |
| 411 |
traversal.wantNames = false; |
| 412 |
|
| 413 |
if (!traversal.addStart(JS::ubi::Node(&rootList)) || |
| 414 |
!traversal.traverse()) { |
| 415 |
ReportOutOfMemory(cx); |
| 416 |
return false; |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
return handler.report(cx, args.rval()); |
| 421 |
} |
| 422 |
|
| 423 |
/* Debugger.Memory property and method tables. */ |
| 424 |
|
| 425 |
/* static */ const JSPropertySpec DebuggerMemory::properties[] = { |
| 426 |
JS_DEBUG_PSGS("trackingAllocationSites", getTrackingAllocationSites, |
| 427 |
setTrackingAllocationSites), |
| 428 |
JS_DEBUG_PSGS("maxAllocationsLogLength", getMaxAllocationsLogLength, |
| 429 |
setMaxAllocationsLogLength), |
| 430 |
JS_DEBUG_PSGS("allocationSamplingProbability", |
| 431 |
getAllocationSamplingProbability, |
| 432 |
setAllocationSamplingProbability), |
| 433 |
JS_DEBUG_PSG("allocationsLogOverflowed", getAllocationsLogOverflowed), |
| 434 |
JS_DEBUG_PSGS("onGarbageCollection", getOnGarbageCollection, |
| 435 |
setOnGarbageCollection), |
| 436 |
JS_PS_END, |
| 437 |
}; |
| 438 |
|
| 439 |
/* static */ const JSFunctionSpec DebuggerMemory::methods[] = { |
| 440 |
JS_DEBUG_FN("drainAllocationsLog", drainAllocationsLog, 0), |
| 441 |
JS_DEBUG_FN("takeCensus", takeCensus, 0), |
| 442 |
JS_FS_END, |
| 443 |
}; |