2013-09-02 22:01:18 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
* vim: set ts=8 sts=4 et sw=4 tw=99:
|
|
|
|
* 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/. */
|
|
|
|
|
2014-04-11 15:19:05 -07:00
|
|
|
#ifndef js_TracingAPI_h
|
|
|
|
#define js_TracingAPI_h
|
2013-09-02 22:01:18 -07:00
|
|
|
|
2014-11-07 02:56:00 -08:00
|
|
|
#include "jsalloc.h"
|
2014-12-01 14:49:21 -08:00
|
|
|
#include "jspubtd.h"
|
2013-09-02 22:01:18 -07:00
|
|
|
|
2014-11-07 02:56:00 -08:00
|
|
|
#include "js/HashTable.h"
|
|
|
|
|
2014-04-11 15:19:05 -07:00
|
|
|
class JS_PUBLIC_API(JSTracer);
|
2013-09-02 22:01:18 -07:00
|
|
|
|
|
|
|
namespace JS {
|
2015-02-25 13:11:37 -08:00
|
|
|
class JS_PUBLIC_API(CallbackTracer);
|
2013-09-02 22:01:18 -07:00
|
|
|
template <typename T> class Heap;
|
|
|
|
template <typename T> class TenuredHeap;
|
|
|
|
}
|
|
|
|
|
2014-12-01 14:49:07 -08:00
|
|
|
// When tracing a thing, the GC needs to know about the layout of the object it
|
|
|
|
// is looking at. There are a fixed number of different layouts that the GC
|
|
|
|
// knows about. The "trace kind" is a static map which tells which layout a GC
|
|
|
|
// thing has.
|
|
|
|
//
|
|
|
|
// Although this map is public, the details are completely hidden. Not all of
|
|
|
|
// the matching C++ types are exposed, and those that are, are opaque.
|
|
|
|
//
|
|
|
|
// See Value::gcKind() and JSTraceCallback in Tracer.h for more details.
|
|
|
|
enum JSGCTraceKind
|
|
|
|
{
|
|
|
|
// These trace kinds have a publicly exposed, although opaque, C++ type.
|
|
|
|
// Note: The order here is determined by our Value packing. Other users
|
|
|
|
// should sort alphabetically, for consistency.
|
|
|
|
JSTRACE_OBJECT = 0x00,
|
|
|
|
JSTRACE_STRING = 0x01,
|
|
|
|
JSTRACE_SYMBOL = 0x02,
|
|
|
|
JSTRACE_SCRIPT = 0x03,
|
|
|
|
|
|
|
|
// Shape details are exposed through JS_TraceShapeCycleCollectorChildren.
|
|
|
|
JSTRACE_SHAPE = 0x04,
|
|
|
|
|
|
|
|
// The kind associated with a nullptr.
|
|
|
|
JSTRACE_NULL = 0x06,
|
|
|
|
|
|
|
|
// A kind that indicates the real kind should be looked up in the arena.
|
|
|
|
JSTRACE_OUTOFLINE = 0x07,
|
|
|
|
|
|
|
|
// The following kinds do not have an exposed C++ idiom.
|
|
|
|
JSTRACE_BASE_SHAPE = 0x0F,
|
|
|
|
JSTRACE_JITCODE = 0x1F,
|
|
|
|
JSTRACE_LAZY_SCRIPT = 0x2F,
|
2015-02-02 10:20:38 -08:00
|
|
|
JSTRACE_OBJECT_GROUP = 0x3F,
|
2014-12-01 14:49:07 -08:00
|
|
|
|
2015-02-02 10:20:38 -08:00
|
|
|
JSTRACE_LAST = JSTRACE_OBJECT_GROUP
|
2014-12-01 14:49:07 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
// Returns a static string equivalent of |kind|.
|
2015-03-28 15:22:11 -07:00
|
|
|
JS_FRIEND_API(const char*)
|
2014-12-01 14:49:07 -08:00
|
|
|
GCTraceKindToAscii(JSGCTraceKind kind);
|
|
|
|
}
|
|
|
|
|
2013-09-02 22:01:18 -07:00
|
|
|
// Tracer callback, called for each traceable thing directly referenced by a
|
|
|
|
// particular object or runtime structure. It is the callback responsibility
|
|
|
|
// to ensure the traversal of the full object graph via calling eventually
|
|
|
|
// JS_TraceChildren on the passed thing. In this case the callback must be
|
|
|
|
// prepared to deal with cycles in the traversal graph.
|
|
|
|
//
|
|
|
|
// kind argument is one of JSTRACE_OBJECT, JSTRACE_STRING or a tag denoting
|
|
|
|
// internal implementation-specific traversal kind. In the latter case the only
|
|
|
|
// operations on thing that the callback can do is to call JS_TraceChildren or
|
|
|
|
// JS_GetTraceThingInfo.
|
|
|
|
//
|
|
|
|
// If eagerlyTraceWeakMaps is true, when we trace a WeakMap visit all
|
|
|
|
// of its mappings. This should be used in cases where the tracer
|
|
|
|
// wants to use the existing liveness of entries.
|
|
|
|
typedef void
|
2015-03-28 15:22:11 -07:00
|
|
|
(* JSTraceCallback)(JS::CallbackTracer* trc, void** thingp, JSGCTraceKind kind);
|
2013-09-02 22:01:18 -07:00
|
|
|
|
|
|
|
// Callback that JSTraceOp implementation can provide to return a string
|
|
|
|
// describing the reference traced with JS_CallTracer.
|
|
|
|
typedef void
|
2015-03-28 15:22:11 -07:00
|
|
|
(* JSTraceNamePrinter)(JSTracer* trc, char* buf, size_t bufsize);
|
2013-09-02 22:01:18 -07:00
|
|
|
|
|
|
|
enum WeakMapTraceKind {
|
|
|
|
DoNotTraceWeakMaps = 0,
|
|
|
|
TraceWeakMapValues = 1,
|
|
|
|
TraceWeakMapKeysValues = 2
|
|
|
|
};
|
|
|
|
|
2014-04-11 15:19:05 -07:00
|
|
|
class JS_PUBLIC_API(JSTracer)
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// Set debugging information about a reference to a traceable thing to prepare
|
|
|
|
// for the following call to JS_CallTracer.
|
|
|
|
//
|
|
|
|
// When printer is null, arg must be const char * or char * C string naming
|
|
|
|
// the reference and index must be either (size_t)-1 indicating that the name
|
|
|
|
// alone describes the reference or it must be an index into some array vector
|
|
|
|
// that stores the reference.
|
|
|
|
//
|
|
|
|
// When printer callback is not null, the arg and index arguments are
|
|
|
|
// available to the callback as debugPrintArg_ and debugPrintIndex_ fields
|
|
|
|
// of JSTracer.
|
|
|
|
//
|
|
|
|
// The storage for name or callback's arguments needs to live only until
|
|
|
|
// the following call to JS_CallTracer returns.
|
2015-03-28 15:22:11 -07:00
|
|
|
void setTracingDetails(JSTraceNamePrinter printer, const void* arg, size_t index) {
|
2014-04-11 15:19:05 -07:00
|
|
|
debugPrinter_ = printer;
|
|
|
|
debugPrintArg_ = arg;
|
|
|
|
debugPrintIndex_ = index;
|
|
|
|
}
|
|
|
|
|
2015-03-28 15:22:11 -07:00
|
|
|
void setTracingIndex(const char* name, size_t index) {
|
|
|
|
setTracingDetails(nullptr, (void*)name, index);
|
2014-04-11 15:19:05 -07:00
|
|
|
}
|
|
|
|
|
2015-03-28 15:22:11 -07:00
|
|
|
void setTracingName(const char* name) {
|
|
|
|
setTracingDetails(nullptr, (void*)name, size_t(-1));
|
2014-04-11 15:19:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the currently set tracing details.
|
|
|
|
void clearTracingDetails() {
|
|
|
|
debugPrinter_ = nullptr;
|
|
|
|
debugPrintArg_ = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return true if tracing details are currently set.
|
|
|
|
bool hasTracingDetails() const;
|
|
|
|
|
|
|
|
// Get the string set with the most recent call to setTracingName or return
|
|
|
|
// fallback if a name printer function has been installed.
|
2015-03-28 15:22:11 -07:00
|
|
|
const char* tracingName(const char* fallback) const;
|
2014-04-11 15:19:05 -07:00
|
|
|
|
|
|
|
// Build a description of this edge in the heap graph. This call may invoke
|
|
|
|
// the debug printer, which may inspect arbitrary areas of the heap.
|
2015-03-28 15:22:11 -07:00
|
|
|
const char* getTracingEdgeName(char* buffer, size_t bufferSize);
|
2014-04-11 15:19:05 -07:00
|
|
|
|
|
|
|
// Access the currently active tracing details.
|
|
|
|
JSTraceNamePrinter debugPrinter() const;
|
2015-03-28 15:22:11 -07:00
|
|
|
const void* debugPrintArg() const;
|
2014-04-11 15:19:05 -07:00
|
|
|
size_t debugPrintIndex() const;
|
|
|
|
|
|
|
|
// Return the runtime set on the tracer.
|
2015-03-28 15:22:11 -07:00
|
|
|
JSRuntime* runtime() const { return runtime_; }
|
2014-04-11 15:19:05 -07:00
|
|
|
|
|
|
|
// Return the weak map tracing behavior set on this tracer.
|
|
|
|
WeakMapTraceKind eagerlyTraceWeakMaps() const { return eagerlyTraceWeakMaps_; }
|
|
|
|
|
2013-09-02 22:01:18 -07:00
|
|
|
#ifdef JS_GC_ZEAL
|
2014-04-11 15:19:05 -07:00
|
|
|
// Sets the "real" location for a marked reference, when passing the address
|
|
|
|
// directly is not feasable. This address is used for matching against the
|
|
|
|
// store buffer when verifying the correctness of the entrees there.
|
|
|
|
//
|
|
|
|
// This is currently complicated by our need to nest calls for Values
|
|
|
|
// stored as keys in hash tables.
|
2015-03-28 15:22:11 -07:00
|
|
|
void setTracingLocation(void* location);
|
2014-04-11 15:19:05 -07:00
|
|
|
void unsetTracingLocation();
|
2015-03-28 15:22:11 -07:00
|
|
|
void** tracingLocation(void** thingp);
|
2013-09-02 22:01:18 -07:00
|
|
|
#else
|
2015-03-28 15:22:11 -07:00
|
|
|
void setTracingLocation(void* location) {}
|
2014-04-11 15:19:05 -07:00
|
|
|
void unsetTracingLocation() {}
|
2015-03-28 15:22:11 -07:00
|
|
|
void** tracingLocation(void** thingp) { return nullptr; }
|
2013-09-02 22:01:18 -07:00
|
|
|
#endif
|
|
|
|
|
2015-02-25 13:11:37 -08:00
|
|
|
// An intermediate state on the road from C to C++ style dispatch.
|
|
|
|
enum TracerKindTag {
|
|
|
|
MarkingTracer,
|
|
|
|
CallbackTracer
|
|
|
|
};
|
|
|
|
bool isMarkingTracer() const { return tag == MarkingTracer; }
|
|
|
|
bool isCallbackTracer() const { return tag == CallbackTracer; }
|
2015-03-28 15:22:11 -07:00
|
|
|
inline JS::CallbackTracer* asCallbackTracer();
|
2015-02-25 13:11:37 -08:00
|
|
|
|
|
|
|
protected:
|
2015-03-28 15:22:11 -07:00
|
|
|
JSTracer(JSRuntime* rt, TracerKindTag tag,
|
2015-02-25 13:11:37 -08:00
|
|
|
WeakMapTraceKind weakTraceKind = TraceWeakMapValues);
|
2013-09-02 22:01:18 -07:00
|
|
|
|
2014-04-11 15:19:05 -07:00
|
|
|
private:
|
2015-03-28 15:22:11 -07:00
|
|
|
JSRuntime* runtime_;
|
2015-02-25 13:11:37 -08:00
|
|
|
TracerKindTag tag;
|
2014-04-11 15:19:05 -07:00
|
|
|
JSTraceNamePrinter debugPrinter_;
|
2015-03-28 15:22:11 -07:00
|
|
|
const void* debugPrintArg_;
|
2014-04-11 15:19:05 -07:00
|
|
|
size_t debugPrintIndex_;
|
|
|
|
WeakMapTraceKind eagerlyTraceWeakMaps_;
|
|
|
|
#ifdef JS_GC_ZEAL
|
2015-03-28 15:22:11 -07:00
|
|
|
void* realLocation_;
|
2014-04-11 15:19:05 -07:00
|
|
|
#endif
|
|
|
|
};
|
2013-09-02 22:01:18 -07:00
|
|
|
|
2015-02-25 13:11:37 -08:00
|
|
|
namespace JS {
|
|
|
|
|
|
|
|
class JS_PUBLIC_API(CallbackTracer) : public JSTracer
|
|
|
|
{
|
|
|
|
public:
|
2015-03-28 15:22:11 -07:00
|
|
|
CallbackTracer(JSRuntime* rt, JSTraceCallback traceCallback,
|
2015-02-25 13:11:37 -08:00
|
|
|
WeakMapTraceKind weakTraceKind = TraceWeakMapValues)
|
|
|
|
: JSTracer(rt, JSTracer::CallbackTracer, weakTraceKind), callback(traceCallback)
|
|
|
|
{}
|
|
|
|
|
|
|
|
// Update the trace callback.
|
|
|
|
void setTraceCallback(JSTraceCallback traceCallback);
|
|
|
|
|
|
|
|
// Test if the given callback is the same as our callback.
|
|
|
|
bool hasCallback(JSTraceCallback maybeCallback) const {
|
|
|
|
return maybeCallback == callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call the callback.
|
2015-03-28 15:22:11 -07:00
|
|
|
void invoke(void** thing, JSGCTraceKind kind) {
|
2015-02-25 13:11:37 -08:00
|
|
|
callback(this, thing, kind);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Exposed publicly for several callers that need to check if the tracer
|
|
|
|
// calling them is of the right type.
|
|
|
|
JSTraceCallback callback;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace JS
|
|
|
|
|
2015-03-28 15:22:11 -07:00
|
|
|
JS::CallbackTracer*
|
2015-02-25 13:11:37 -08:00
|
|
|
JSTracer::asCallbackTracer()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(isCallbackTracer());
|
2015-03-28 15:22:11 -07:00
|
|
|
return static_cast<JS::CallbackTracer*>(this);
|
2015-02-25 13:11:37 -08:00
|
|
|
}
|
|
|
|
|
2013-09-02 22:01:18 -07:00
|
|
|
// The JS_Call*Tracer family of functions traces the given GC thing reference.
|
|
|
|
// This performs the tracing action configured on the given JSTracer:
|
|
|
|
// typically calling the JSTracer::callback or marking the thing as live.
|
|
|
|
//
|
|
|
|
// The argument to JS_Call*Tracer is an in-out param: when the function
|
|
|
|
// returns, the garbage collector might have moved the GC thing. In this case,
|
|
|
|
// the reference passed to JS_Call*Tracer will be updated to the object's new
|
|
|
|
// location. Callers of this method are responsible for updating any state
|
|
|
|
// that is dependent on the object's address. For example, if the object's
|
|
|
|
// address is used as a key in a hashtable, then the object must be removed
|
|
|
|
// and re-inserted with the correct hash.
|
|
|
|
//
|
|
|
|
extern JS_PUBLIC_API(void)
|
2015-03-28 15:22:11 -07:00
|
|
|
JS_CallValueTracer(JSTracer* trc, JS::Heap<JS::Value>* valuep, const char* name);
|
2013-09-02 22:01:18 -07:00
|
|
|
|
|
|
|
extern JS_PUBLIC_API(void)
|
2015-03-28 15:22:11 -07:00
|
|
|
JS_CallIdTracer(JSTracer* trc, JS::Heap<jsid>* idp, const char* name);
|
2013-09-02 22:01:18 -07:00
|
|
|
|
|
|
|
extern JS_PUBLIC_API(void)
|
2015-03-28 15:22:11 -07:00
|
|
|
JS_CallObjectTracer(JSTracer* trc, JS::Heap<JSObject*>* objp, const char* name);
|
2013-09-02 22:01:18 -07:00
|
|
|
|
|
|
|
extern JS_PUBLIC_API(void)
|
2015-03-28 15:22:11 -07:00
|
|
|
JS_CallStringTracer(JSTracer* trc, JS::Heap<JSString*>* strp, const char* name);
|
2013-09-02 22:01:18 -07:00
|
|
|
|
|
|
|
extern JS_PUBLIC_API(void)
|
2015-03-28 15:22:11 -07:00
|
|
|
JS_CallScriptTracer(JSTracer* trc, JS::Heap<JSScript*>* scriptp, const char* name);
|
2013-09-02 22:01:18 -07:00
|
|
|
|
|
|
|
extern JS_PUBLIC_API(void)
|
2015-03-28 15:22:11 -07:00
|
|
|
JS_CallFunctionTracer(JSTracer* trc, JS::Heap<JSFunction*>* funp, const char* name);
|
2013-09-02 22:01:18 -07:00
|
|
|
|
2014-08-13 02:05:22 -07:00
|
|
|
// The following JS_CallUnbarriered*Tracer functions should only be called where
|
|
|
|
// you know for sure that a heap post barrier is not required. Use with extreme
|
|
|
|
// caution!
|
2013-09-02 22:01:18 -07:00
|
|
|
extern JS_PUBLIC_API(void)
|
2015-03-28 15:22:11 -07:00
|
|
|
JS_CallUnbarrieredValueTracer(JSTracer* trc, JS::Value* valuep, const char* name);
|
2013-09-02 22:01:18 -07:00
|
|
|
|
|
|
|
extern JS_PUBLIC_API(void)
|
2015-03-28 15:22:11 -07:00
|
|
|
JS_CallUnbarrieredIdTracer(JSTracer* trc, jsid* idp, const char* name);
|
2013-09-02 22:01:18 -07:00
|
|
|
|
|
|
|
extern JS_PUBLIC_API(void)
|
2015-03-28 15:22:11 -07:00
|
|
|
JS_CallUnbarrieredObjectTracer(JSTracer* trc, JSObject** objp, const char* name);
|
2013-09-02 22:01:18 -07:00
|
|
|
|
|
|
|
extern JS_PUBLIC_API(void)
|
2015-03-28 15:22:11 -07:00
|
|
|
JS_CallUnbarrieredStringTracer(JSTracer* trc, JSString** strp, const char* name);
|
2013-09-02 22:01:18 -07:00
|
|
|
|
2014-01-07 16:53:17 -08:00
|
|
|
extern JS_PUBLIC_API(void)
|
2015-03-28 15:22:11 -07:00
|
|
|
JS_CallUnbarrieredScriptTracer(JSTracer* trc, JSScript** scriptp, const char* name);
|
2014-01-07 16:53:17 -08:00
|
|
|
|
2013-09-02 22:01:18 -07:00
|
|
|
template <typename HashSetEnum>
|
|
|
|
inline void
|
2015-03-28 15:22:11 -07:00
|
|
|
JS_CallHashSetObjectTracer(JSTracer* trc, HashSetEnum& e, JSObject* const& key, const char* name)
|
2013-09-02 22:01:18 -07:00
|
|
|
{
|
2015-03-28 15:22:11 -07:00
|
|
|
JSObject* updated = key;
|
|
|
|
trc->setTracingLocation(reinterpret_cast<void*>(&const_cast<JSObject*&>(key)));
|
2014-08-13 02:05:22 -07:00
|
|
|
JS_CallUnbarrieredObjectTracer(trc, &updated, name);
|
2013-09-02 22:01:18 -07:00
|
|
|
if (updated != key)
|
2014-11-14 01:22:26 -08:00
|
|
|
e.rekeyFront(updated);
|
2013-09-02 22:01:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Trace an object that is known to always be tenured. No post barriers are
|
|
|
|
// required in this case.
|
|
|
|
extern JS_PUBLIC_API(void)
|
2015-03-28 15:22:11 -07:00
|
|
|
JS_CallTenuredObjectTracer(JSTracer* trc, JS::TenuredHeap<JSObject*>* objp, const char* name);
|
2013-09-02 22:01:18 -07:00
|
|
|
|
|
|
|
extern JS_PUBLIC_API(void)
|
2015-03-28 15:22:11 -07:00
|
|
|
JS_TraceChildren(JSTracer* trc, void* thing, JSGCTraceKind kind);
|
2013-09-02 22:01:18 -07:00
|
|
|
|
|
|
|
extern JS_PUBLIC_API(void)
|
2015-03-28 15:22:11 -07:00
|
|
|
JS_TraceRuntime(JSTracer* trc);
|
2013-09-02 22:01:18 -07:00
|
|
|
|
2014-11-07 02:56:00 -08:00
|
|
|
namespace JS {
|
2015-03-28 15:22:11 -07:00
|
|
|
typedef js::HashSet<Zone*, js::DefaultHasher<Zone*>, js::SystemAllocPolicy> ZoneSet;
|
2014-11-07 02:56:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Trace every value within |zones| that is wrapped by a cross-compartment
|
|
|
|
// wrapper from a zone that is not an element of |zones|.
|
|
|
|
extern JS_PUBLIC_API(void)
|
2015-03-28 15:22:11 -07:00
|
|
|
JS_TraceIncomingCCWs(JSTracer* trc, const JS::ZoneSet& zones);
|
2014-11-07 02:56:00 -08:00
|
|
|
|
2013-09-02 22:01:18 -07:00
|
|
|
extern JS_PUBLIC_API(void)
|
2015-03-28 15:22:11 -07:00
|
|
|
JS_GetTraceThingInfo(char* buf, size_t bufsize, JSTracer* trc,
|
|
|
|
void* thing, JSGCTraceKind kind, bool includeDetails);
|
2013-09-02 22:01:18 -07:00
|
|
|
|
2014-04-11 15:19:05 -07:00
|
|
|
#endif /* js_TracingAPI_h */
|