mirror of
https://github.com/roytam1/palemoon27.git
synced 2026-05-26 14:18:48 +00:00
1e24b22ef4
- Bug 1150366 - Remove duplicate d3.js. r=jsantell (71f991da6) - Bug 1175352 - Refactor LoadInfo arguments to be more self contained (r=bent) (3f7d08164) - Bug 1181533 - Add nsCRTGlue.h and prtime.h includes to nsDownloadManager.cpp to fix --disable-safe-browsing build bustage. r=froydnj (af338edf1) - Bug 905127 - Part 1 - Make some functions from nsNetUtil not inline. r=jduell Bug 905127 - Part 2 - remove unnecessary nsNetUtil.h includes r=jduell Bug 905127 - Make some functions from nsNetUtil not inline. r=jduell (bc47d5b3a) - Bug 1117650 - Part 1: Move all CORS tests into dom/security/test. r=sicking (6f727387c) - Bug 1117650 - Part 2: Move all MixedContent tests into dom/security/test. r=tanvi (73f273829) - Bug 1004703 - ignore 'unsafe-inline' if nonce- or hash-source specifed - tests (r=sstamm) (ebf411159) - Bug 921493 - CSP: test whitelisting of scheme-relative sources (r=dveditz) (f0618b189) - Bug 1139569 - Optimize update function for element editors in markup view;r=mratcliffe (c60d38dca) - Partil of Bug 1139644 - Make markupview use sdk timers instead of individual timers for each window;r=pbrosset (8bc1a4d66) - Bug 1146568 - Avoid unsafe CPOW usage warnings in browser_markupview_tag_edit_08.js; r=bgrins Making use of the devtools test frame-script to set the test node's attributes instead of using a CPOW. (61d7eed64) - Bug 1146568 - Avoid unsafe CPOW usage in markupview tests, in assertAttributes helper; r=bgrins (efc492d5d) - Bug 858038 - Allow moving elements in the markupview by drag/drop; r=pbrosset (51d79c15c) - Bug 1139644 - Flash only relevant attributes in markup view when changed;r=pbrosset (29807fa46) - Bug 1147128 - Make sure attribute shows up in markup view after removing and setting to the previous value;r=mratcliffe (1947f7417) - Bug 1147325 - Clear box model timer on markup view destroy. r=pbrosset Prevents 'this._inspector.toolbox is null' spam in mochitest-dt (1d6808b4a) - Bug 921102 - 1 - Linkify URIs in the inspector; r=tromey, miker This first part adds a parser for node attributes which, given some node information and an attribute name, generates a small AST-like array of objects that tells which parts of the attribute (if any) are links, and what they link to. Using this, the markup-view generates the right HTML structure to display these parts as links. This part 1 doesn't yet allow users to follow these links. (c500e270e) - Bug 1139667 - CSP tests for fetch(). r=ckirschb (df030457f) - Bug 1086999 - CSP: Asterisk (*) wildcard should not allow blob:, data:, or filesystem: when matching source expressions - tests (r=sstamm) (dee0f2239) - Bug 1117650 - Part 3: Move all CSP tests into dom/security/test. r=sstamm (29c95475c) - Bug 1146566 - 1 - Use devtools common frame-script in markupview tests and add helper; r=bgrins (b10e42468) - Bug 1146566 - 2 - Avoid using CPOWs in test browser_markupview_tag_edit_03.js; r=bgrins This change updates the browser_markupview_tag_edit_03.js test by making use of the getDomElementInfo message listener to retrieve information about the tested nodes without having to go through CPOWs. (07f0ee3ff) - Bug 1036324 - Adds option to walker.parents() to not traverse DocShellTreeItems of different types (24123a5d2) - Bug 921102 - 2 - Markup-view tests for attribute links; r=miker (f6da81799) - Bug 901250 - Add scroll into view menu item for the inspector. r=bgrins (bfc7518b1) - Bug 994055 - Part 1: Add a toggle sidebar panel button to the inspector. r=miker This adds a new icon, next to the searchbox (like in the debugger) to expand or collapse the sidebar panel in the inspector. The state is *not* persisted yet when the toolbox is closed. The button is hidden when the toolbox host switches to 'side'. (87730e20a) - Bug 994055 - Part 2: Add tests for the inspector sidebar toggle button. r=miker (2872c3fbd) - Bug 921102 - 3 - Open/copy markup-view attribute links; r=bgrins This part adds contextual menu items that become enabled when the user right clicks on an attribute that has a link. Depending on the nature of the link, a new tab will be opened or a node selected. The user can also choose to copy the link in the clipboard. (9b93485f6) - Bug 921102 - 4 - Tests for the open/copy links on markup-view attributes; r=bgrins (861ba51f6)
594 lines
17 KiB
C++
594 lines
17 KiB
C++
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
|
|
/* 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 "HeapSnapshot.h"
|
|
|
|
#include <google/protobuf/io/coded_stream.h>
|
|
#include <google/protobuf/io/gzip_stream.h>
|
|
#include <google/protobuf/io/zero_copy_stream_impl_lite.h>
|
|
|
|
#include "js/Debug.h"
|
|
#include "js/TypeDecls.h"
|
|
#include "js/UbiNodeTraverse.h"
|
|
#include "mozilla/Attributes.h"
|
|
#include "mozilla/devtools/AutoMemMap.h"
|
|
#include "mozilla/devtools/CoreDump.pb.h"
|
|
#include "mozilla/devtools/DeserializedNode.h"
|
|
#include "mozilla/devtools/ZeroCopyNSIOutputStream.h"
|
|
#include "mozilla/dom/ChromeUtils.h"
|
|
#include "mozilla/dom/HeapSnapshotBinding.h"
|
|
#include "mozilla/UniquePtr.h"
|
|
|
|
#include "jsapi.h"
|
|
#include "nsCycleCollectionParticipant.h"
|
|
#include "nsCRTGlue.h"
|
|
#include "nsIOutputStream.h"
|
|
#include "nsISupportsImpl.h"
|
|
#include "nsNetUtil.h"
|
|
#include "nsIFile.h"
|
|
#include "prerror.h"
|
|
#include "prio.h"
|
|
#include "prtypes.h"
|
|
|
|
namespace mozilla {
|
|
namespace devtools {
|
|
|
|
using namespace JS;
|
|
using namespace dom;
|
|
|
|
using ::google::protobuf::io::ArrayInputStream;
|
|
using ::google::protobuf::io::CodedInputStream;
|
|
using ::google::protobuf::io::GzipInputStream;
|
|
using ::google::protobuf::io::ZeroCopyInputStream;
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(HeapSnapshot)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(HeapSnapshot)
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(HeapSnapshot)
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(HeapSnapshot)
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(HeapSnapshot)
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(HeapSnapshot)
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(HeapSnapshot)
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
NS_INTERFACE_MAP_END
|
|
|
|
/* virtual */ JSObject*
|
|
HeapSnapshot::WrapObject(JSContext* aCx, HandleObject aGivenProto)
|
|
{
|
|
return HeapSnapshotBinding::Wrap(aCx, this, aGivenProto);
|
|
}
|
|
|
|
/* static */ already_AddRefed<HeapSnapshot>
|
|
HeapSnapshot::Create(JSContext* cx,
|
|
GlobalObject& global,
|
|
const uint8_t* buffer,
|
|
uint32_t size,
|
|
ErrorResult& rv)
|
|
{
|
|
nsRefPtr<HeapSnapshot> snapshot = new HeapSnapshot(cx, global.GetAsSupports());
|
|
if (!snapshot->init(buffer, size)) {
|
|
rv.Throw(NS_ERROR_UNEXPECTED);
|
|
return nullptr;
|
|
}
|
|
return snapshot.forget();
|
|
}
|
|
|
|
template<typename MessageType>
|
|
static bool
|
|
parseMessage(ZeroCopyInputStream& stream, MessageType& message)
|
|
{
|
|
// We need to create a new `CodedInputStream` for each message so that the
|
|
// 64MB limit is applied per-message rather than to the whole stream.
|
|
CodedInputStream codedStream(&stream);
|
|
|
|
// Because protobuf messages aren't self-delimiting, we serialize each message
|
|
// preceeded by its size in bytes. When deserializing, we read this size and
|
|
// then limit reading from the stream to the given byte size. If we didn't,
|
|
// then the first message would consume the entire stream.
|
|
|
|
uint32_t size = 0;
|
|
if (NS_WARN_IF(!codedStream.ReadVarint32(&size)))
|
|
return false;
|
|
|
|
auto limit = codedStream.PushLimit(size);
|
|
if (NS_WARN_IF(!message.ParseFromCodedStream(&codedStream)) ||
|
|
NS_WARN_IF(!codedStream.ConsumedEntireMessage()))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
codedStream.PopLimit(limit);
|
|
return true;
|
|
}
|
|
|
|
bool
|
|
HeapSnapshot::saveNode(const protobuf::Node& node)
|
|
{
|
|
UniquePtr<DeserializedNode> dn(DeserializedNode::Create(node, *this));
|
|
if (!dn)
|
|
return false;
|
|
return nodes.put(dn->id, Move(dn));
|
|
}
|
|
|
|
static inline bool
|
|
StreamHasData(GzipInputStream& stream)
|
|
{
|
|
// Test for the end of the stream. The protobuf library gives no way to tell
|
|
// the difference between an underlying read error and the stream being
|
|
// done. All we can do is attempt to read data and extrapolate guestimations
|
|
// from the result of that operation.
|
|
|
|
const void* buf;
|
|
int size;
|
|
bool more = stream.Next(&buf, &size);
|
|
if (!more)
|
|
// Could not read any more data. We are optimistic and assume the stream is
|
|
// just exhausted and there is not an underlying IO error, since this
|
|
// function is only called at message boundaries.
|
|
return false;
|
|
|
|
// There is more data still available in the stream. Return the data we read
|
|
// to the stream and let the parser get at it.
|
|
stream.BackUp(size);
|
|
return true;
|
|
}
|
|
|
|
bool
|
|
HeapSnapshot::init(const uint8_t* buffer, uint32_t size)
|
|
{
|
|
if (!nodes.init() || !strings.init())
|
|
return false;
|
|
|
|
ArrayInputStream stream(buffer, size);
|
|
GzipInputStream gzipStream(&stream);
|
|
|
|
// First is the metadata.
|
|
|
|
protobuf::Metadata metadata;
|
|
if (!parseMessage(gzipStream, metadata))
|
|
return false;
|
|
if (metadata.has_timestamp())
|
|
timestamp.emplace(metadata.timestamp());
|
|
|
|
// Next is the root node.
|
|
|
|
protobuf::Node root;
|
|
if (!parseMessage(gzipStream, root))
|
|
return false;
|
|
|
|
// Although the id is optional in the protobuf format for future proofing, we
|
|
// can't currently do anything without it.
|
|
if (NS_WARN_IF(!root.has_id()))
|
|
return false;
|
|
rootId = root.id();
|
|
|
|
if (NS_WARN_IF(!saveNode(root)))
|
|
return false;
|
|
|
|
// Finally, the rest of the nodes in the core dump.
|
|
|
|
while (StreamHasData(gzipStream)) {
|
|
protobuf::Node node;
|
|
if (!parseMessage(gzipStream, node))
|
|
return false;
|
|
if (NS_WARN_IF(!saveNode(node)))
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
const char16_t*
|
|
HeapSnapshot::borrowUniqueString(const char16_t* duplicateString, size_t length)
|
|
{
|
|
MOZ_ASSERT(duplicateString);
|
|
UniqueStringHashPolicy::Lookup lookup(duplicateString, length);
|
|
auto ptr = strings.lookupForAdd(lookup);
|
|
|
|
if (!ptr) {
|
|
UniqueString owned(NS_strndup(duplicateString, length));
|
|
if (!owned || !strings.add(ptr, Move(owned)))
|
|
return nullptr;
|
|
}
|
|
|
|
MOZ_ASSERT(ptr->get() != duplicateString);
|
|
return ptr->get();
|
|
}
|
|
|
|
// If we are only taking a snapshot of the heap affected by the given set of
|
|
// globals, find the set of zones the globals are allocated within. Returns
|
|
// false on OOM failure.
|
|
static bool
|
|
PopulateZonesWithGlobals(ZoneSet& zones, AutoObjectVector& globals)
|
|
{
|
|
if (!zones.init())
|
|
return false;
|
|
|
|
unsigned length = globals.length();
|
|
for (unsigned i = 0; i < length; i++) {
|
|
if (!zones.put(GetObjectZone(globals[i])))
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
// Add the given set of globals as explicit roots in the given roots
|
|
// list. Returns false on OOM failure.
|
|
static bool
|
|
AddGlobalsAsRoots(AutoObjectVector& globals, ubi::RootList& roots)
|
|
{
|
|
unsigned length = globals.length();
|
|
for (unsigned i = 0; i < length; i++) {
|
|
if (!roots.addRoot(ubi::Node(globals[i].get()),
|
|
MOZ_UTF16("heap snapshot global")))
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
// Choose roots and limits for a traversal, given `boundaries`. Set `roots` to
|
|
// the set of nodes within the boundaries that are referred to by nodes
|
|
// outside. If `boundaries` does not include all JS zones, initialize `zones` to
|
|
// the set of included zones; otherwise, leave `zones` uninitialized. (You can
|
|
// use zones.initialized() to check.)
|
|
//
|
|
// If `boundaries` is incoherent, or we encounter an error while trying to
|
|
// handle it, or we run out of memory, set `rv` appropriately and return
|
|
// `false`.
|
|
static bool
|
|
EstablishBoundaries(JSContext* cx,
|
|
ErrorResult& rv,
|
|
const HeapSnapshotBoundaries& boundaries,
|
|
ubi::RootList& roots,
|
|
ZoneSet& zones)
|
|
{
|
|
MOZ_ASSERT(!roots.initialized());
|
|
MOZ_ASSERT(!zones.initialized());
|
|
|
|
bool foundBoundaryProperty = false;
|
|
|
|
if (boundaries.mRuntime.WasPassed()) {
|
|
foundBoundaryProperty = true;
|
|
|
|
if (!boundaries.mRuntime.Value()) {
|
|
rv.Throw(NS_ERROR_INVALID_ARG);
|
|
return false;
|
|
}
|
|
|
|
if (!roots.init()) {
|
|
rv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
if (boundaries.mDebugger.WasPassed()) {
|
|
if (foundBoundaryProperty) {
|
|
rv.Throw(NS_ERROR_INVALID_ARG);
|
|
return false;
|
|
}
|
|
foundBoundaryProperty = true;
|
|
|
|
JSObject* dbgObj = boundaries.mDebugger.Value();
|
|
if (!dbgObj || !dbg::IsDebugger(*dbgObj)) {
|
|
rv.Throw(NS_ERROR_INVALID_ARG);
|
|
return false;
|
|
}
|
|
|
|
AutoObjectVector globals(cx);
|
|
if (!dbg::GetDebuggeeGlobals(cx, *dbgObj, globals) ||
|
|
!PopulateZonesWithGlobals(zones, globals) ||
|
|
!roots.init(zones) ||
|
|
!AddGlobalsAsRoots(globals, roots))
|
|
{
|
|
rv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
if (boundaries.mGlobals.WasPassed()) {
|
|
if (foundBoundaryProperty) {
|
|
rv.Throw(NS_ERROR_INVALID_ARG);
|
|
return false;
|
|
}
|
|
foundBoundaryProperty = true;
|
|
|
|
uint32_t length = boundaries.mGlobals.Value().Length();
|
|
if (length == 0) {
|
|
rv.Throw(NS_ERROR_INVALID_ARG);
|
|
return false;
|
|
}
|
|
|
|
AutoObjectVector globals(cx);
|
|
for (uint32_t i = 0; i < length; i++) {
|
|
JSObject* global = boundaries.mGlobals.Value().ElementAt(i);
|
|
if (!JS_IsGlobalObject(global)) {
|
|
rv.Throw(NS_ERROR_INVALID_ARG);
|
|
return false;
|
|
}
|
|
if (!globals.append(global)) {
|
|
rv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
if (!PopulateZonesWithGlobals(zones, globals) ||
|
|
!roots.init(zones) ||
|
|
!AddGlobalsAsRoots(globals, roots))
|
|
{
|
|
rv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
if (!foundBoundaryProperty) {
|
|
rv.Throw(NS_ERROR_INVALID_ARG);
|
|
return false;
|
|
}
|
|
|
|
MOZ_ASSERT(roots.initialized());
|
|
MOZ_ASSERT_IF(boundaries.mDebugger.WasPassed(), zones.initialized());
|
|
MOZ_ASSERT_IF(boundaries.mGlobals.WasPassed(), zones.initialized());
|
|
return true;
|
|
}
|
|
|
|
|
|
// A `CoreDumpWriter` that serializes nodes to protobufs and writes them to the
|
|
// given `ZeroCopyOutputStream`.
|
|
class MOZ_STACK_CLASS StreamWriter : public CoreDumpWriter
|
|
{
|
|
JSContext* cx;
|
|
bool wantNames;
|
|
|
|
::google::protobuf::io::ZeroCopyOutputStream& stream;
|
|
|
|
bool writeMessage(const ::google::protobuf::MessageLite& message) {
|
|
// We have to create a new CodedOutputStream when writing each message so
|
|
// that the 64MB size limit used by Coded{Output,Input}Stream to prevent
|
|
// integer overflow is enforced per message rather than on the whole stream.
|
|
::google::protobuf::io::CodedOutputStream codedStream(&stream);
|
|
codedStream.WriteVarint32(message.ByteSize());
|
|
message.SerializeWithCachedSizes(&codedStream);
|
|
return !codedStream.HadError();
|
|
}
|
|
|
|
public:
|
|
StreamWriter(JSContext* cx,
|
|
::google::protobuf::io::ZeroCopyOutputStream& stream,
|
|
bool wantNames)
|
|
: cx(cx)
|
|
, wantNames(wantNames)
|
|
, stream(stream)
|
|
{ }
|
|
|
|
~StreamWriter() override { }
|
|
|
|
virtual bool writeMetadata(uint64_t timestamp) override {
|
|
protobuf::Metadata metadata;
|
|
metadata.set_timestamp(timestamp);
|
|
return writeMessage(metadata);
|
|
}
|
|
|
|
virtual bool writeNode(const JS::ubi::Node& ubiNode,
|
|
EdgePolicy includeEdges) override {
|
|
protobuf::Node protobufNode;
|
|
protobufNode.set_id(ubiNode.identifier());
|
|
|
|
const char16_t* typeName = ubiNode.typeName();
|
|
size_t length = NS_strlen(typeName) * sizeof(char16_t);
|
|
protobufNode.set_typename_(typeName, length);
|
|
|
|
JSRuntime* rt = JS_GetRuntime(cx);
|
|
mozilla::MallocSizeOf mallocSizeOf = dbg::GetDebuggerMallocSizeOf(rt);
|
|
MOZ_ASSERT(mallocSizeOf);
|
|
protobufNode.set_size(ubiNode.size(mallocSizeOf));
|
|
|
|
if (includeEdges) {
|
|
auto edges = ubiNode.edges(cx, wantNames);
|
|
if (NS_WARN_IF(!edges))
|
|
return false;
|
|
|
|
for ( ; !edges->empty(); edges->popFront()) {
|
|
const ubi::Edge& ubiEdge = edges->front();
|
|
|
|
protobuf::Edge* protobufEdge = protobufNode.add_edges();
|
|
if (NS_WARN_IF(!protobufEdge)) {
|
|
return false;
|
|
}
|
|
|
|
protobufEdge->set_referent(ubiEdge.referent.identifier());
|
|
|
|
if (wantNames && ubiEdge.name) {
|
|
size_t length = NS_strlen(ubiEdge.name) * sizeof(char16_t);
|
|
protobufEdge->set_name(ubiEdge.name, length);
|
|
}
|
|
}
|
|
}
|
|
|
|
return writeMessage(protobufNode);
|
|
}
|
|
};
|
|
|
|
// A JS::ubi::BreadthFirst handler that serializes a snapshot of the heap into a
|
|
// core dump.
|
|
class MOZ_STACK_CLASS HeapSnapshotHandler
|
|
{
|
|
CoreDumpWriter& writer;
|
|
JS::ZoneSet* zones;
|
|
|
|
public:
|
|
HeapSnapshotHandler(CoreDumpWriter& writer,
|
|
JS::ZoneSet* zones)
|
|
: writer(writer),
|
|
zones(zones)
|
|
{ }
|
|
|
|
// JS::ubi::BreadthFirst handler interface.
|
|
|
|
class NodeData { };
|
|
typedef JS::ubi::BreadthFirst<HeapSnapshotHandler> Traversal;
|
|
bool operator() (Traversal& traversal,
|
|
JS::ubi::Node origin,
|
|
const JS::ubi::Edge& edge,
|
|
NodeData*,
|
|
bool first)
|
|
{
|
|
// We're only interested in the first time we reach edge.referent, not in
|
|
// every edge arriving at that node. "But, don't we want to serialize every
|
|
// edge in the heap graph?" you ask. Don't worry! This edge is still
|
|
// serialized into the core dump. Serializing a node also serializes each of
|
|
// its edges, and if we are traversing a given edge, we must have already
|
|
// visited and serialized the origin node and its edges.
|
|
if (!first)
|
|
return true;
|
|
|
|
const JS::ubi::Node& referent = edge.referent;
|
|
|
|
if (!zones)
|
|
// We aren't targeting a particular set of zones, so serialize all the
|
|
// things!
|
|
return writer.writeNode(referent, CoreDumpWriter::INCLUDE_EDGES);
|
|
|
|
// We are targeting a particular set of zones. If this node is in our target
|
|
// set, serialize it and all of its edges. If this node is _not_ in our
|
|
// target set, we also serialize under the assumption that it is a shared
|
|
// resource being used by something in our target zones since we reached it
|
|
// by traversing the heap graph. However, we do not serialize its outgoing
|
|
// edges and we abandon further traversal from this node.
|
|
|
|
JS::Zone* zone = referent.zone();
|
|
|
|
if (zones->has(zone))
|
|
return writer.writeNode(referent, CoreDumpWriter::INCLUDE_EDGES);
|
|
|
|
traversal.abandonReferent();
|
|
return writer.writeNode(referent, CoreDumpWriter::EXCLUDE_EDGES);
|
|
}
|
|
};
|
|
|
|
|
|
bool
|
|
WriteHeapGraph(JSContext* cx,
|
|
const JS::ubi::Node& node,
|
|
CoreDumpWriter& writer,
|
|
bool wantNames,
|
|
JS::ZoneSet* zones,
|
|
JS::AutoCheckCannotGC& noGC)
|
|
{
|
|
// Serialize the starting node to the core dump.
|
|
|
|
if (NS_WARN_IF(!writer.writeNode(node, CoreDumpWriter::INCLUDE_EDGES))) {
|
|
return false;
|
|
}
|
|
|
|
// Walk the heap graph starting from the given node and serialize it into the
|
|
// core dump.
|
|
|
|
HeapSnapshotHandler handler(writer, zones);
|
|
HeapSnapshotHandler::Traversal traversal(cx, handler, noGC);
|
|
if (!traversal.init())
|
|
return false;
|
|
traversal.wantNames = wantNames;
|
|
|
|
return traversal.addStartVisited(node) &&
|
|
traversal.traverse();
|
|
}
|
|
|
|
} // namespace devtools
|
|
|
|
namespace dom {
|
|
|
|
using namespace JS;
|
|
using namespace devtools;
|
|
|
|
/* static */ void
|
|
ThreadSafeChromeUtils::SaveHeapSnapshot(GlobalObject& global,
|
|
JSContext* cx,
|
|
const nsAString& filePath,
|
|
const HeapSnapshotBoundaries& boundaries,
|
|
ErrorResult& rv)
|
|
{
|
|
bool wantNames = true;
|
|
ZoneSet zones;
|
|
Maybe<AutoCheckCannotGC> maybeNoGC;
|
|
ubi::RootList rootList(cx, maybeNoGC, wantNames);
|
|
if (!EstablishBoundaries(cx, rv, boundaries, rootList, zones))
|
|
return;
|
|
|
|
MOZ_ASSERT(maybeNoGC.isSome());
|
|
ubi::Node roots(&rootList);
|
|
|
|
nsCOMPtr<nsIFile> file;
|
|
rv = NS_NewLocalFile(filePath, false, getter_AddRefs(file));
|
|
if (NS_WARN_IF(rv.Failed()))
|
|
return;
|
|
|
|
nsCOMPtr<nsIOutputStream> outputStream;
|
|
rv = NS_NewLocalFileOutputStream(getter_AddRefs(outputStream), file,
|
|
PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE,
|
|
-1, 0);
|
|
if (NS_WARN_IF(rv.Failed()))
|
|
return;
|
|
|
|
ZeroCopyNSIOutputStream zeroCopyStream(outputStream);
|
|
::google::protobuf::io::GzipOutputStream gzipStream(&zeroCopyStream);
|
|
|
|
StreamWriter writer(cx, gzipStream, wantNames);
|
|
|
|
// Serialize the initial heap snapshot metadata to the core dump.
|
|
if (!writer.writeMetadata(PR_Now()) ||
|
|
// Serialize the heap graph to the core dump, starting from our list of
|
|
// roots.
|
|
!WriteHeapGraph(cx,
|
|
roots,
|
|
writer,
|
|
wantNames,
|
|
zones.initialized() ? &zones : nullptr,
|
|
maybeNoGC.ref()))
|
|
{
|
|
rv.Throw(zeroCopyStream.failed()
|
|
? zeroCopyStream.result()
|
|
: NS_ERROR_UNEXPECTED);
|
|
return;
|
|
}
|
|
}
|
|
|
|
/* static */ already_AddRefed<HeapSnapshot>
|
|
ThreadSafeChromeUtils::ReadHeapSnapshot(GlobalObject& global,
|
|
JSContext* cx,
|
|
const nsAString& filePath,
|
|
ErrorResult& rv)
|
|
{
|
|
UniquePtr<char[]> path(ToNewCString(filePath));
|
|
if (!path) {
|
|
rv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
return nullptr;
|
|
}
|
|
|
|
AutoMemMap mm;
|
|
rv = mm.init(path.get());
|
|
if (rv.Failed())
|
|
return nullptr;
|
|
|
|
return HeapSnapshot::Create(cx, global,
|
|
reinterpret_cast<const uint8_t*>(mm.address()),
|
|
mm.size(), rv);
|
|
}
|
|
|
|
} // namespace dom
|
|
} // namespace mozilla
|