Web Inspector: We should have a way to capture heap snapshots programatically.
authorcommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 29 Mar 2016 18:51:35 +0000 (18:51 +0000)
committercommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 29 Mar 2016 18:51:35 +0000 (18:51 +0000)
https://bugs.webkit.org/show_bug.cgi?id=154407
<rdar://problem/24726292>

Patch by Joseph Pecoraro <pecoraro@apple.com> on 2016-03-29
Reviewed by Timothy Hatcher.

Source/JavaScriptCore:

* inspector/protocol/Console.json:
Add a new Console.heapSnapshot event for when a heap snapshot is taken.

* runtime/ConsolePrototype.cpp:
(JSC::ConsolePrototype::finishCreation):
(JSC::consoleProtoFuncProfile):
(JSC::consoleProtoFuncProfileEnd):
(JSC::consoleProtoFuncTakeHeapSnapshot):
* runtime/ConsoleClient.h:
Add the console.takeHeapSnapshot method and dispatch to the ConsoleClient.

* inspector/JSGlobalObjectConsoleClient.cpp:
(Inspector::JSGlobalObjectConsoleClient::takeHeapSnapshot):
* inspector/JSGlobalObjectConsoleClient.h:
Have the InspectorConsoleAgent handle this.

* inspector/JSGlobalObjectInspectorController.cpp:
(Inspector::JSGlobalObjectInspectorController::JSGlobalObjectInspectorController):
* inspector/agents/InspectorConsoleAgent.cpp:
(Inspector::InspectorConsoleAgent::InspectorConsoleAgent):
(Inspector::InspectorConsoleAgent::takeHeapSnapshot):
* inspector/agents/InspectorConsoleAgent.h:
* inspector/agents/JSGlobalObjectConsoleAgent.cpp:
(Inspector::JSGlobalObjectConsoleAgent::JSGlobalObjectConsoleAgent):
* inspector/agents/JSGlobalObjectConsoleAgent.h:
Give the ConsoleAgent a HeapAgent pointer so that it can have the HeapAgent
perform the snapshot building work like it normally does.

Source/WebCore:

Test: inspector/console/heapSnapshot.html

* page/PageConsoleClient.cpp:
(WebCore::PageConsoleClient::takeHeapSnapshot):
* page/PageConsoleClient.h:
Pass through to Inspector Instrumentation.

* inspector/InspectorConsoleInstrumentation.h:
(WebCore::InspectorInstrumentation::takeHeapSnapshot):
* inspector/InspectorInstrumentation.cpp:
(WebCore::InspectorInstrumentation::takeHeapSnapshotImpl):
* inspector/InspectorInstrumentation.h:
Pass through to InspectorConsoleAgent.

* inspector/InspectorController.cpp:
(WebCore::InspectorController::InspectorController):
* inspector/PageConsoleAgent.cpp:
(WebCore::PageConsoleAgent::PageConsoleAgent):
* inspector/PageConsoleAgent.h:
* inspector/WebConsoleAgent.cpp:
(WebCore::WebConsoleAgent::WebConsoleAgent):
* inspector/WebConsoleAgent.h:
* workers/WorkerConsoleClient.cpp:
(WebCore::WorkerConsoleClient::takeHeapSnapshot):
* workers/WorkerConsoleClient.h:
Provide a HeapAgent to the ConsoleAgent.

Source/WebInspectorUI:

* Localizations/en.lproj/localizedStrings.js:
* UserInterface/Protocol/ConsoleObserver.js:
(WebInspector.ConsoleObserver.prototype.heapSnapshot):
(WebInspector.ConsoleObserver):
Create a HeapSnapshot with an optional title and add to the timeline.

(WebInspector.HeapAllocationsTimelineDataGridNode):
* UserInterface/Views/TimelineTabContentView.js:
(WebInspector.TimelineTabContentView.displayNameForRecord):
Share code for snapshot display names which may now include a title.

* UserInterface/Proxies/HeapSnapshotProxy.js:
(WebInspector.HeapSnapshotProxy):
(WebInspector.HeapSnapshotProxy.deserialize):
(WebInspector.HeapSnapshotProxy.prototype.get title):
* UserInterface/Views/HeapAllocationsTimelineDataGridNode.js:
* UserInterface/Workers/HeapSnapshot/HeapSnapshot.js:
(HeapSnapshot):
(HeapSnapshot.prototype.serialize):
* UserInterface/Workers/HeapSnapshot/HeapSnapshotWorker.js:
(HeapSnapshotWorker.prototype.createSnapshot):
Include an optional title in a HeapSnapshot.

LayoutTests:

* inspector/console/heapSnapshot-expected.txt: Added.
* inspector/console/heapSnapshot.html: Added.
Test that we get expected data and events after calling
console.takeHeapSnapshot when the inspector is open.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@198786 268f45cc-cd09-0410-ab3c-d52691b4dbfc

35 files changed:
LayoutTests/ChangeLog
LayoutTests/inspector/console/heapSnapshot-expected.txt [new file with mode: 0644]
LayoutTests/inspector/console/heapSnapshot.html [new file with mode: 0644]
Source/JavaScriptCore/ChangeLog
Source/JavaScriptCore/inspector/JSGlobalObjectConsoleClient.cpp
Source/JavaScriptCore/inspector/JSGlobalObjectConsoleClient.h
Source/JavaScriptCore/inspector/JSGlobalObjectInspectorController.cpp
Source/JavaScriptCore/inspector/agents/InspectorConsoleAgent.cpp
Source/JavaScriptCore/inspector/agents/InspectorConsoleAgent.h
Source/JavaScriptCore/inspector/agents/JSGlobalObjectConsoleAgent.cpp
Source/JavaScriptCore/inspector/agents/JSGlobalObjectConsoleAgent.h
Source/JavaScriptCore/inspector/protocol/Console.json
Source/JavaScriptCore/runtime/ConsoleClient.h
Source/JavaScriptCore/runtime/ConsolePrototype.cpp
Source/WebCore/ChangeLog
Source/WebCore/inspector/InspectorConsoleInstrumentation.h
Source/WebCore/inspector/InspectorController.cpp
Source/WebCore/inspector/InspectorInstrumentation.cpp
Source/WebCore/inspector/InspectorInstrumentation.h
Source/WebCore/inspector/PageConsoleAgent.cpp
Source/WebCore/inspector/PageConsoleAgent.h
Source/WebCore/inspector/WebConsoleAgent.cpp
Source/WebCore/inspector/WebConsoleAgent.h
Source/WebCore/page/PageConsoleClient.cpp
Source/WebCore/page/PageConsoleClient.h
Source/WebCore/workers/WorkerConsoleClient.cpp
Source/WebCore/workers/WorkerConsoleClient.h
Source/WebInspectorUI/ChangeLog
Source/WebInspectorUI/Localizations/en.lproj/localizedStrings.js
Source/WebInspectorUI/UserInterface/Protocol/ConsoleObserver.js
Source/WebInspectorUI/UserInterface/Proxies/HeapSnapshotProxy.js
Source/WebInspectorUI/UserInterface/Views/HeapAllocationsTimelineDataGridNode.js
Source/WebInspectorUI/UserInterface/Views/TimelineTabContentView.js
Source/WebInspectorUI/UserInterface/Workers/HeapSnapshot/HeapSnapshot.js
Source/WebInspectorUI/UserInterface/Workers/HeapSnapshot/HeapSnapshotWorker.js

index 3dd2951..6680177 100644 (file)
@@ -1,3 +1,16 @@
+2016-03-29  Joseph Pecoraro  <pecoraro@apple.com>
+
+        Web Inspector: We should have a way to capture heap snapshots programatically.
+        https://bugs.webkit.org/show_bug.cgi?id=154407
+        <rdar://problem/24726292>
+
+        Reviewed by Timothy Hatcher.
+
+        * inspector/console/heapSnapshot-expected.txt: Added.
+        * inspector/console/heapSnapshot.html: Added.
+        Test that we get expected data and events after calling
+        console.takeHeapSnapshot when the inspector is open.
+
 2016-03-29  Eric Carlson  <eric.carlson@apple.com>
 
         media/track/track-remove-track.html is flaky, crashing and failing
diff --git a/LayoutTests/inspector/console/heapSnapshot-expected.txt b/LayoutTests/inspector/console/heapSnapshot-expected.txt
new file mode 100644 (file)
index 0000000..02c2ecb
--- /dev/null
@@ -0,0 +1,16 @@
+Test for the Console.heapSnapshot event triggered by console.takeHeapSnapshot.
+
+
+== Running test suite: Console.heapSnapshot
+-- Running test case: TriggerHeapSnapshotNoTitle
+Console.heapSnapshot
+PASS: Should have a timestamp.
+PASS: Should have snapshotData.
+PASS: Should not have a title.
+
+-- Running test case: TriggerHeapSnapshotWithTitle
+Console.heapSnapshot
+PASS: Should have a timestamp.
+PASS: Should have snapshotData.
+PASS: Should have expected title.
+
diff --git a/LayoutTests/inspector/console/heapSnapshot.html b/LayoutTests/inspector/console/heapSnapshot.html
new file mode 100644 (file)
index 0000000..911c444
--- /dev/null
@@ -0,0 +1,55 @@
+<!DOCTYPE html>
+<html>
+<head>
+<script src="../../http/tests/inspector/resources/protocol-test.js"></script>
+<script>
+function triggerHeapSnapshotNoTitle() {
+    console.takeHeapSnapshot();
+}
+
+function triggerHeapSnapshotWithTitle() {
+    console.takeHeapSnapshot("Snapshot Title");
+}
+
+function test()
+{
+    let suite = ProtocolTest.createAsyncSuite("Console.heapSnapshot");
+
+    suite.addTestCase({
+        name: "TriggerHeapSnapshotNoTitle",
+        test: function(resolve, reject) {
+            InspectorProtocol.awaitEvent({event: "Console.heapSnapshot"}).then((messageObject) => {
+                ProtocolTest.log("Console.heapSnapshot");
+                ProtocolTest.expectThat(typeof messageObject.params.timestamp === "number", "Should have a timestamp.");
+                ProtocolTest.expectThat(typeof messageObject.params.snapshotData === "string", "Should have snapshotData.");
+                ProtocolTest.expectThat(!messageObject.params.title, "Should not have a title.");
+                resolve();
+            });
+
+            ProtocolTest.evaluateInPage("triggerHeapSnapshotNoTitle()");
+        }
+    });
+
+    suite.addTestCase({
+        name: "TriggerHeapSnapshotWithTitle",
+        test: function(resolve, reject) {
+            InspectorProtocol.awaitEvent({event: "Console.heapSnapshot"}).then((messageObject) => {
+                ProtocolTest.log("Console.heapSnapshot");
+                ProtocolTest.expectThat(typeof messageObject.params.timestamp === "number", "Should have a timestamp.");
+                ProtocolTest.expectThat(typeof messageObject.params.snapshotData === "string", "Should have snapshotData.");
+                ProtocolTest.expectThat(messageObject.params.title === "Snapshot Title", "Should have expected title.");
+                resolve();
+            });
+
+            ProtocolTest.evaluateInPage("triggerHeapSnapshotWithTitle()");
+        }
+    });
+
+    suite.runTestCasesAndFinish();
+}
+</script>
+</head>
+<body onload="runTest()">
+<p>Test for the Console.heapSnapshot event triggered by console.takeHeapSnapshot.</p>
+</body>
+</html>
index c832498..e38b292 100644 (file)
@@ -1,3 +1,39 @@
+2016-03-29  Joseph Pecoraro  <pecoraro@apple.com>
+
+        Web Inspector: We should have a way to capture heap snapshots programatically.
+        https://bugs.webkit.org/show_bug.cgi?id=154407
+        <rdar://problem/24726292>
+
+        Reviewed by Timothy Hatcher.
+
+        * inspector/protocol/Console.json:
+        Add a new Console.heapSnapshot event for when a heap snapshot is taken.
+
+        * runtime/ConsolePrototype.cpp:
+        (JSC::ConsolePrototype::finishCreation):
+        (JSC::consoleProtoFuncProfile):
+        (JSC::consoleProtoFuncProfileEnd):
+        (JSC::consoleProtoFuncTakeHeapSnapshot):
+        * runtime/ConsoleClient.h:
+        Add the console.takeHeapSnapshot method and dispatch to the ConsoleClient.
+
+        * inspector/JSGlobalObjectConsoleClient.cpp:
+        (Inspector::JSGlobalObjectConsoleClient::takeHeapSnapshot):
+        * inspector/JSGlobalObjectConsoleClient.h:
+        Have the InspectorConsoleAgent handle this.
+
+        * inspector/JSGlobalObjectInspectorController.cpp:
+        (Inspector::JSGlobalObjectInspectorController::JSGlobalObjectInspectorController):
+        * inspector/agents/InspectorConsoleAgent.cpp:
+        (Inspector::InspectorConsoleAgent::InspectorConsoleAgent):
+        (Inspector::InspectorConsoleAgent::takeHeapSnapshot):
+        * inspector/agents/InspectorConsoleAgent.h:
+        * inspector/agents/JSGlobalObjectConsoleAgent.cpp:
+        (Inspector::JSGlobalObjectConsoleAgent::JSGlobalObjectConsoleAgent):
+        * inspector/agents/JSGlobalObjectConsoleAgent.h:
+        Give the ConsoleAgent a HeapAgent pointer so that it can have the HeapAgent
+        perform the snapshot building work like it normally does.
+
 2016-03-29  Yusuke Suzuki  <utatane.tea@gmail.com>
 
         REGRESSION(r192914): 10% regression on Sunspider's date-format-tofte
index f4a155d..e36850c 100644 (file)
@@ -83,6 +83,11 @@ void JSGlobalObjectConsoleClient::profileEnd(JSC::ExecState*, const String&)
     // FIXME: support |console.profile| for JSContexts. <https://webkit.org/b/136466>
 }
 
+void JSGlobalObjectConsoleClient::takeHeapSnapshot(JSC::ExecState*, const String& title)
+{
+    m_consoleAgent->takeHeapSnapshot(title);
+}
+
 void JSGlobalObjectConsoleClient::time(ExecState*, const String& title)
 {
     m_consoleAgent->startTiming(title);
index 3220199..f3687f4 100644 (file)
@@ -46,6 +46,7 @@ protected:
     void count(JSC::ExecState*, RefPtr<ScriptArguments>&&) override;
     void profile(JSC::ExecState*, const String& title) override;
     void profileEnd(JSC::ExecState*, const String& title) override;
+    void takeHeapSnapshot(JSC::ExecState*, const String& title) override;
     void time(JSC::ExecState*, const String& title) override;
     void timeEnd(JSC::ExecState*, const String& title) override;
     void timeStamp(JSC::ExecState*, RefPtr<ScriptArguments>&&) override;
index fd7f869..f0ffc18 100644 (file)
@@ -85,7 +85,8 @@ JSGlobalObjectInspectorController::JSGlobalObjectInspectorController(JSGlobalObj
 
     auto inspectorAgent = std::make_unique<InspectorAgent>(context);
     auto runtimeAgent = std::make_unique<JSGlobalObjectRuntimeAgent>(context);
-    auto consoleAgent = std::make_unique<JSGlobalObjectConsoleAgent>(context);
+    auto heapAgent = std::make_unique<InspectorHeapAgent>(context);
+    auto consoleAgent = std::make_unique<JSGlobalObjectConsoleAgent>(context, heapAgent.get());
     auto debuggerAgent = std::make_unique<JSGlobalObjectDebuggerAgent>(context, consoleAgent.get());
 
     m_inspectorAgent = inspectorAgent.get();
@@ -97,7 +98,7 @@ JSGlobalObjectInspectorController::JSGlobalObjectInspectorController(JSGlobalObj
     m_agents.append(WTFMove(runtimeAgent));
     m_agents.append(WTFMove(consoleAgent));
     m_agents.append(WTFMove(debuggerAgent));
-    m_agents.append(std::make_unique<InspectorHeapAgent>(context));
+    m_agents.append(WTFMove(heapAgent));
     m_agents.append(std::make_unique<InspectorScriptProfilerAgent>(context));
 
     m_executionStopwatch->start();
index e518f7f..3116b06 100644 (file)
@@ -29,6 +29,7 @@
 #include "ConsoleMessage.h"
 #include "InjectedScriptManager.h"
 #include "InspectorFrontendRouter.h"
+#include "InspectorHeapAgent.h"
 #include "ScriptArguments.h"
 #include "ScriptCallFrame.h"
 #include "ScriptCallStack.h"
@@ -43,11 +44,12 @@ namespace Inspector {
 static const unsigned maximumConsoleMessages = 100;
 static const int expireConsoleMessagesStep = 10;
 
-InspectorConsoleAgent::InspectorConsoleAgent(AgentContext& context)
+InspectorConsoleAgent::InspectorConsoleAgent(AgentContext& context, InspectorHeapAgent* heapAgent)
     : InspectorAgentBase(ASCIILiteral("Console"))
     , m_injectedScriptManager(context.injectedScriptManager)
     , m_frontendDispatcher(std::make_unique<ConsoleFrontendDispatcher>(context.frontendRouter))
     , m_backendDispatcher(ConsoleBackendDispatcher::create(context.backendDispatcher, this))
+    , m_heapAgent(heapAgent)
 {
 }
 
@@ -153,6 +155,19 @@ void InspectorConsoleAgent::stopTiming(const String& title, PassRefPtr<ScriptCal
     addMessageToConsole(std::make_unique<ConsoleMessage>(MessageSource::ConsoleAPI, MessageType::Timing, MessageLevel::Debug, message, callStack));
 }
 
+void InspectorConsoleAgent::takeHeapSnapshot(const String& title)
+{
+    if (!m_injectedScriptManager.inspectorEnvironment().developerExtrasEnabled())
+        return;
+
+    ErrorString ignored;
+    double timestamp;
+    String snapshotData;
+    m_heapAgent->snapshot(ignored, &timestamp, &snapshotData);
+
+    m_frontendDispatcher->heapSnapshot(timestamp, snapshotData, title.isEmpty() ? nullptr : &title);
+}
+
 void InspectorConsoleAgent::count(JSC::ExecState* state, PassRefPtr<ScriptArguments> arguments)
 {
     RefPtr<ScriptCallStack> callStack(createScriptCallStackForConsole(state, ScriptCallStack::maxCallStackSizeToCapture));
index 873f6d3..9451ec4 100644 (file)
@@ -44,6 +44,7 @@ namespace Inspector {
 
 class ConsoleMessage;
 class InjectedScriptManager;
+class InspectorHeapAgent;
 class ScriptArguments;
 class ScriptCallStack;
 typedef String ErrorString;
@@ -52,7 +53,7 @@ class JS_EXPORT_PRIVATE InspectorConsoleAgent : public InspectorAgentBase, publi
     WTF_MAKE_NONCOPYABLE(InspectorConsoleAgent);
     WTF_MAKE_FAST_ALLOCATED;
 public:
-    InspectorConsoleAgent(AgentContext&);
+    InspectorConsoleAgent(AgentContext&, InspectorHeapAgent*);
     virtual ~InspectorConsoleAgent();
 
     void didCreateFrontendAndBackend(FrontendRouter*, BackendDispatcher*) override;
@@ -71,6 +72,7 @@ public:
 
     void startTiming(const String& title);
     void stopTiming(const String& title, PassRefPtr<ScriptCallStack>);
+    void takeHeapSnapshot(const String& title);
     void count(JSC::ExecState*, PassRefPtr<ScriptArguments>);
 
 protected:
@@ -79,6 +81,7 @@ protected:
     InjectedScriptManager& m_injectedScriptManager;
     std::unique_ptr<ConsoleFrontendDispatcher> m_frontendDispatcher;
     RefPtr<ConsoleBackendDispatcher> m_backendDispatcher;
+    InspectorHeapAgent* m_heapAgent;
 
     ConsoleMessage* m_previousMessage { nullptr };
     Vector<std::unique_ptr<ConsoleMessage>> m_consoleMessages;
index 0217dc6..401162b 100644 (file)
@@ -28,8 +28,8 @@
 
 namespace Inspector {
 
-JSGlobalObjectConsoleAgent::JSGlobalObjectConsoleAgent(AgentContext& context)
-    : InspectorConsoleAgent(context)
+JSGlobalObjectConsoleAgent::JSGlobalObjectConsoleAgent(AgentContext& context, InspectorHeapAgent* heapAgent)
+    : InspectorConsoleAgent(context, heapAgent)
 {
 }
 
index b1dae4c..c069c82 100644 (file)
@@ -35,7 +35,7 @@ class JSGlobalObjectConsoleAgent final : public InspectorConsoleAgent {
     WTF_MAKE_NONCOPYABLE(JSGlobalObjectConsoleAgent);
     WTF_MAKE_FAST_ALLOCATED;
 public:
-    JSGlobalObjectConsoleAgent(AgentContext&);
+    JSGlobalObjectConsoleAgent(AgentContext&, InspectorHeapAgent*);
     virtual ~JSGlobalObjectConsoleAgent() { }
 
     // FIXME: XHRs and Nodes only makes sense debugging a Web context. Can this be moved to a different agent?
index a255362..bec1dd5 100644 (file)
         {
             "name": "messagesCleared",
             "description": "Issued when console is cleared. This happens either upon <code>clearMessages</code> command or after page navigation."
+        },
+        {
+            "name": "heapSnapshot",
+            "description": "Issued from console.takeHeapSnapshot.",
+            "parameters": [
+                { "name": "timestamp", "type": "number" },
+                { "name": "snapshotData", "$ref": "Heap.HeapSnapshotData", "description": "Snapshot at the end of tracking." },
+                { "name": "title", "type": "string", "optional": true, "description": "Optional title provided to console.takeHeapSnapshot." }
+            ]
         }
     ]
 }
index a16b85b..a9faa56 100644 (file)
@@ -59,6 +59,7 @@ public:
     virtual void count(ExecState*, RefPtr<Inspector::ScriptArguments>&&) = 0;
     virtual void profile(ExecState*, const String& title) = 0;
     virtual void profileEnd(ExecState*, const String& title) = 0;
+    virtual void takeHeapSnapshot(ExecState*, const String& title) = 0;
     virtual void time(ExecState*, const String& title) = 0;
     virtual void timeEnd(ExecState*, const String& title) = 0;
     virtual void timeStamp(ExecState*, RefPtr<Inspector::ScriptArguments>&&) = 0;
index c8db068..f185fef 100644 (file)
@@ -52,6 +52,7 @@ static EncodedJSValue JSC_HOST_CALL consoleProtoFuncAssert(ExecState*);
 static EncodedJSValue JSC_HOST_CALL consoleProtoFuncCount(ExecState*);
 static EncodedJSValue JSC_HOST_CALL consoleProtoFuncProfile(ExecState*);
 static EncodedJSValue JSC_HOST_CALL consoleProtoFuncProfileEnd(ExecState*);
+static EncodedJSValue JSC_HOST_CALL consoleProtoFuncTakeHeapSnapshot(ExecState*);
 static EncodedJSValue JSC_HOST_CALL consoleProtoFuncTime(ExecState*);
 static EncodedJSValue JSC_HOST_CALL consoleProtoFuncTimeEnd(ExecState*);
 static EncodedJSValue JSC_HOST_CALL consoleProtoFuncTimeStamp(ExecState*);
@@ -86,6 +87,7 @@ void ConsolePrototype::finishCreation(VM& vm, JSGlobalObject* globalObject)
     JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("time", consoleProtoFuncTime, None, 0);
     JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("timeEnd", consoleProtoFuncTimeEnd, None, 0);
     JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("timeStamp", consoleProtoFuncTimeStamp, None, 0);
+    JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("takeHeapSnapshot", consoleProtoFuncTakeHeapSnapshot, None, 0);
     JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("group", consoleProtoFuncGroup, None, 0);
     JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("groupCollapsed", consoleProtoFuncGroupCollapsed, None, 0);
     JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("groupEnd", consoleProtoFuncGroupEnd, None, 0);
@@ -258,7 +260,7 @@ static EncodedJSValue JSC_HOST_CALL consoleProtoFuncProfile(ExecState* exec)
         return JSValue::encode(jsUndefined());
 
     size_t argsCount = exec->argumentCount();
-    if (argsCount <= 0) {
+    if (!argsCount) {
         client->profile(exec, String());
         return JSValue::encode(jsUndefined());
     }
@@ -282,7 +284,7 @@ static EncodedJSValue JSC_HOST_CALL consoleProtoFuncProfileEnd(ExecState* exec)
         return JSValue::encode(jsUndefined());
 
     size_t argsCount = exec->argumentCount();
-    if (argsCount <= 0) {
+    if (!argsCount) {
         client->profileEnd(exec, String());
         return JSValue::encode(jsUndefined());
     }
@@ -295,6 +297,30 @@ static EncodedJSValue JSC_HOST_CALL consoleProtoFuncProfileEnd(ExecState* exec)
     return JSValue::encode(jsUndefined());
 }
 
+static EncodedJSValue JSC_HOST_CALL consoleProtoFuncTakeHeapSnapshot(ExecState* exec)
+{
+    JSConsole* castedThis = jsDynamicCast<JSConsole*>(exec->thisValue());
+    if (!castedThis)
+        return throwVMTypeError(exec);
+    ASSERT_GC_OBJECT_INHERITS(castedThis, JSConsole::info());
+    ConsoleClient* client = castedThis->globalObject()->consoleClient();
+    if (!client)
+        return JSValue::encode(jsUndefined());
+
+    size_t argsCount = exec->argumentCount();
+    if (!argsCount) {
+        client->takeHeapSnapshot(exec, String());
+        return JSValue::encode(jsUndefined());
+    }
+
+    const String& title(valueToStringWithUndefinedOrNullCheck(exec, exec->argument(0)));
+    if (exec->hadException())
+        return JSValue::encode(jsUndefined());
+
+    client->takeHeapSnapshot(exec, title);
+    return JSValue::encode(jsUndefined());
+}
+
 static EncodedJSValue JSC_HOST_CALL consoleProtoFuncTime(ExecState* exec)
 {
     JSConsole* castedThis = jsDynamicCast<JSConsole*>(exec->thisValue());
index 9254883..651a7a3 100644 (file)
@@ -1,3 +1,38 @@
+2016-03-29  Joseph Pecoraro  <pecoraro@apple.com>
+
+        Web Inspector: We should have a way to capture heap snapshots programatically.
+        https://bugs.webkit.org/show_bug.cgi?id=154407
+        <rdar://problem/24726292>
+
+        Reviewed by Timothy Hatcher.
+
+        Test: inspector/console/heapSnapshot.html
+
+        * page/PageConsoleClient.cpp:
+        (WebCore::PageConsoleClient::takeHeapSnapshot):
+        * page/PageConsoleClient.h:
+        Pass through to Inspector Instrumentation.
+
+        * inspector/InspectorConsoleInstrumentation.h:
+        (WebCore::InspectorInstrumentation::takeHeapSnapshot):
+        * inspector/InspectorInstrumentation.cpp:
+        (WebCore::InspectorInstrumentation::takeHeapSnapshotImpl):
+        * inspector/InspectorInstrumentation.h:
+        Pass through to InspectorConsoleAgent.
+
+        * inspector/InspectorController.cpp:
+        (WebCore::InspectorController::InspectorController):
+        * inspector/PageConsoleAgent.cpp:
+        (WebCore::PageConsoleAgent::PageConsoleAgent):
+        * inspector/PageConsoleAgent.h:
+        * inspector/WebConsoleAgent.cpp:
+        (WebCore::WebConsoleAgent::WebConsoleAgent):
+        * inspector/WebConsoleAgent.h:
+        * workers/WorkerConsoleClient.cpp:
+        (WebCore::WorkerConsoleClient::takeHeapSnapshot):
+        * workers/WorkerConsoleClient.h:
+        Provide a HeapAgent to the ConsoleAgent.
+
 2016-03-29  Zalan Bujtas  <zalan@apple.com>
 
         REGRESSION (r196813): Missing plug-in placeholder is missing
index 6a05c2f..4849235 100644 (file)
@@ -54,6 +54,13 @@ inline void InspectorInstrumentation::consoleCount(Page& page, JSC::ExecState* s
     consoleCountImpl(instrumentingAgentsForPage(page), state, WTFMove(arguments));
 }
 
+inline void InspectorInstrumentation::takeHeapSnapshot(Frame& frame, const String& title)
+{
+    FAST_RETURN_IF_NO_FRONTENDS(void());
+    if (InstrumentingAgents* instrumentingAgents = instrumentingAgentsForFrame(frame))
+        takeHeapSnapshotImpl(*instrumentingAgents, title);
+}
+
 inline void InspectorInstrumentation::startConsoleTiming(Frame& frame, const String& title)
 {
     if (InstrumentingAgents* instrumentingAgents = instrumentingAgentsForFrame(frame))
index a8411ac..cb42ef2 100644 (file)
@@ -162,7 +162,11 @@ InspectorController::InspectorController(Page& page, InspectorClient* inspectorC
     m_timelineAgent = timelineAgentPtr.get();
     m_agents.append(WTFMove(timelineAgentPtr));
 
-    auto consoleAgentPtr = std::make_unique<PageConsoleAgent>(pageContext, m_domAgent);
+    auto heapAgentPtr = std::make_unique<InspectorHeapAgent>(pageContext);
+    InspectorHeapAgent* heapAgent = heapAgentPtr.get();
+    m_agents.append(WTFMove(heapAgentPtr));
+
+    auto consoleAgentPtr = std::make_unique<PageConsoleAgent>(pageContext, heapAgent, m_domAgent);
     WebConsoleAgent* consoleAgent = consoleAgentPtr.get();
     m_instrumentingAgents->setWebConsoleAgent(consoleAgentPtr.get());
     m_agents.append(WTFMove(consoleAgentPtr));
@@ -172,7 +176,6 @@ InspectorController::InspectorController(Page& page, InspectorClient* inspectorC
     m_agents.append(WTFMove(debuggerAgentPtr));
 
     m_agents.append(std::make_unique<InspectorDOMDebuggerAgent>(pageContext, m_domAgent, debuggerAgent));
-    m_agents.append(std::make_unique<InspectorHeapAgent>(pageContext));
     m_agents.append(std::make_unique<InspectorScriptProfilerAgent>(pageContext));
     m_agents.append(std::make_unique<InspectorApplicationCacheAgent>(pageContext, pageAgent));
     m_agents.append(std::make_unique<InspectorLayerTreeAgent>(pageContext));
index 0153946..7cbd595 100644 (file)
@@ -855,6 +855,12 @@ void InspectorInstrumentation::consoleCountImpl(InstrumentingAgents& instrumenti
         consoleAgent->count(state, arguments);
 }
 
+void InspectorInstrumentation::takeHeapSnapshotImpl(InstrumentingAgents& instrumentingAgents, const String& title)
+{
+    if (WebConsoleAgent* consoleAgent = instrumentingAgents.webConsoleAgent())
+        consoleAgent->takeHeapSnapshot(title);
+}
+
 void InspectorInstrumentation::startConsoleTimingImpl(InstrumentingAgents& instrumentingAgents, Frame& frame, const String& title)
 {
     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents.inspectorTimelineAgent())
index 93df184..dbafcdb 100644 (file)
@@ -209,6 +209,7 @@ public:
     static void addMessageToConsole(WorkerGlobalScope*, std::unique_ptr<Inspector::ConsoleMessage>);
 
     static void consoleCount(Page&, JSC::ExecState*, RefPtr<Inspector::ScriptArguments>&&);
+    static void takeHeapSnapshot(Frame&, const String& title);
     static void startConsoleTiming(Frame&, const String& title);
     static void stopConsoleTiming(Frame&, const String& title, RefPtr<Inspector::ScriptCallStack>&&);
     static void consoleTimeStamp(Frame&, RefPtr<Inspector::ScriptArguments>&&);
@@ -382,6 +383,7 @@ private:
     static void addMessageToConsoleImpl(InstrumentingAgents&, std::unique_ptr<Inspector::ConsoleMessage>);
 
     static void consoleCountImpl(InstrumentingAgents&, JSC::ExecState*, RefPtr<Inspector::ScriptArguments>&&);
+    static void takeHeapSnapshotImpl(InstrumentingAgents&, const String& title);
     static void startConsoleTimingImpl(InstrumentingAgents&, Frame&, const String& title);
     static void stopConsoleTimingImpl(InstrumentingAgents&, Frame&, const String& title, RefPtr<Inspector::ScriptCallStack>&&);
     static void consoleTimeStampImpl(InstrumentingAgents&, Frame&, RefPtr<Inspector::ScriptArguments>&&);
index 0149a0f..6dc90dc 100644 (file)
@@ -41,8 +41,8 @@ using namespace Inspector;
 
 namespace WebCore {
 
-PageConsoleAgent::PageConsoleAgent(WebAgentContext& context, InspectorDOMAgent* domAgent)
-    : WebConsoleAgent(context)
+PageConsoleAgent::PageConsoleAgent(WebAgentContext& context, InspectorHeapAgent* heapAgent, InspectorDOMAgent* domAgent)
+    : WebConsoleAgent(context, heapAgent)
     , m_inspectorDOMAgent(domAgent)
 {
 }
index 55c6efc..5edd49d 100644 (file)
@@ -43,7 +43,7 @@ class PageConsoleAgent final : public WebConsoleAgent {
     WTF_MAKE_NONCOPYABLE(PageConsoleAgent);
     WTF_MAKE_FAST_ALLOCATED;
 public:
-    PageConsoleAgent(WebAgentContext&, InspectorDOMAgent*);
+    PageConsoleAgent(WebAgentContext&, Inspector::InspectorHeapAgent*, InspectorDOMAgent*);
     virtual ~PageConsoleAgent() { }
 
 private:
index 33ec0b7..8905270 100644 (file)
@@ -41,8 +41,8 @@ using namespace Inspector;
 
 namespace WebCore {
 
-WebConsoleAgent::WebConsoleAgent(AgentContext& context)
-    : InspectorConsoleAgent(context)
+WebConsoleAgent::WebConsoleAgent(AgentContext& context, InspectorHeapAgent* heapAgent)
+    : InspectorConsoleAgent(context, heapAgent)
 {
 }
 
index d564ded..b0a6cf6 100644 (file)
@@ -39,7 +39,7 @@ class WebConsoleAgent : public Inspector::InspectorConsoleAgent {
     WTF_MAKE_NONCOPYABLE(WebConsoleAgent);
     WTF_MAKE_FAST_ALLOCATED;
 public:
-    WebConsoleAgent(Inspector::AgentContext&);
+    WebConsoleAgent(Inspector::AgentContext&, Inspector::InspectorHeapAgent*);
     virtual ~WebConsoleAgent() { }
 
     void setMonitoringXHREnabled(ErrorString&, bool enabled) final;
index 1164e00..5cfa97b 100644 (file)
@@ -195,6 +195,11 @@ void PageConsoleClient::profileEnd(JSC::ExecState* exec, const String& title)
         m_profiles.append(WTFMove(profile));
 }
 
+void PageConsoleClient::takeHeapSnapshot(JSC::ExecState*, const String& title)
+{
+    InspectorInstrumentation::takeHeapSnapshot(m_page.mainFrame(), title);
+}
+
 void PageConsoleClient::time(JSC::ExecState*, const String& title)
 {
     InspectorInstrumentation::startConsoleTiming(m_page.mainFrame(), title);
index 4681aa1..ce5c518 100644 (file)
@@ -69,6 +69,7 @@ protected:
     void count(JSC::ExecState*, RefPtr<Inspector::ScriptArguments>&&) override;
     void profile(JSC::ExecState*, const String& title) override;
     void profileEnd(JSC::ExecState*, const String& title) override;
+    void takeHeapSnapshot(JSC::ExecState*, const String& title) override;
     void time(JSC::ExecState*, const String& title) override;
     void timeEnd(JSC::ExecState*, const String& title) override;
     void timeStamp(JSC::ExecState*, RefPtr<Inspector::ScriptArguments>&&) override;
index f98a9bb..b3edd41 100644 (file)
@@ -56,6 +56,7 @@ void WorkerConsoleClient::profileEnd(JSC::ExecState*, const String&) { }
 
 // FIXME: <https://webkit.org/b/127634> Web Inspector: support debugging web workers
 void WorkerConsoleClient::count(JSC::ExecState*, RefPtr<ScriptArguments>&&) { }
+void WorkerConsoleClient::takeHeapSnapshot(JSC::ExecState*, const String&) { }
 void WorkerConsoleClient::time(JSC::ExecState*, const String&) { }
 void WorkerConsoleClient::timeEnd(JSC::ExecState*, const String&) { }
 void WorkerConsoleClient::timeStamp(JSC::ExecState*, RefPtr<ScriptArguments>&&) { }
index 8be450d..3611231 100644 (file)
@@ -47,6 +47,7 @@ protected:
     void count(JSC::ExecState*, RefPtr<Inspector::ScriptArguments>&&) override;
     void profile(JSC::ExecState*, const String& title) override;
     void profileEnd(JSC::ExecState*, const String& title) override;
+    void takeHeapSnapshot(JSC::ExecState*, const String& title) override;
     void time(JSC::ExecState*, const String& title) override;
     void timeEnd(JSC::ExecState*, const String& title) override;
     void timeStamp(JSC::ExecState*, RefPtr<Inspector::ScriptArguments>&&) override;
index 8409988..025e7ab 100644 (file)
@@ -1,3 +1,34 @@
+2016-03-29  Joseph Pecoraro  <pecoraro@apple.com>
+
+        Web Inspector: We should have a way to capture heap snapshots programatically.
+        https://bugs.webkit.org/show_bug.cgi?id=154407
+        <rdar://problem/24726292>
+
+        Reviewed by Timothy Hatcher.
+
+        * Localizations/en.lproj/localizedStrings.js:
+        * UserInterface/Protocol/ConsoleObserver.js:
+        (WebInspector.ConsoleObserver.prototype.heapSnapshot):
+        (WebInspector.ConsoleObserver):
+        Create a HeapSnapshot with an optional title and add to the timeline.
+
+        (WebInspector.HeapAllocationsTimelineDataGridNode):
+        * UserInterface/Views/TimelineTabContentView.js:
+        (WebInspector.TimelineTabContentView.displayNameForRecord):
+        Share code for snapshot display names which may now include a title.
+
+        * UserInterface/Proxies/HeapSnapshotProxy.js:
+        (WebInspector.HeapSnapshotProxy):
+        (WebInspector.HeapSnapshotProxy.deserialize):
+        (WebInspector.HeapSnapshotProxy.prototype.get title):
+        * UserInterface/Views/HeapAllocationsTimelineDataGridNode.js:
+        * UserInterface/Workers/HeapSnapshot/HeapSnapshot.js:
+        (HeapSnapshot):
+        (HeapSnapshot.prototype.serialize):
+        * UserInterface/Workers/HeapSnapshot/HeapSnapshotWorker.js:
+        (HeapSnapshotWorker.prototype.createSnapshot):
+        Include an optional title in a HeapSnapshot.
+
 2016-03-28  Joseph Pecoraro  <pecoraro@apple.com>
 
         Web Inspector: Ensure maximum accuracy while profiling
index 42b4b42..7d4e0eb 100644 (file)
Binary files a/Source/WebInspectorUI/Localizations/en.lproj/localizedStrings.js and b/Source/WebInspectorUI/Localizations/en.lproj/localizedStrings.js differ
index cfc2fc0..c42dc80 100644 (file)
@@ -52,4 +52,13 @@ WebInspector.ConsoleObserver = class ConsoleObserver
     {
         WebInspector.logManager.messagesCleared();
     }
+
+    heapSnapshot(timestamp, snapshotStringData, title)
+    {
+        let workerProxy = WebInspector.HeapSnapshotWorkerProxy.singleton();
+        workerProxy.createSnapshot(snapshotStringData, title || null, ({objectId, snapshot: serializedSnapshot}) => {
+            let snapshot = WebInspector.HeapSnapshotProxy.deserialize(objectId, serializedSnapshot);
+            WebInspector.timelineManager.heapSnapshotAdded(timestamp, snapshot);
+        });
+    }
 };
index 8ee6633..79f8313 100644 (file)
 
 WebInspector.HeapSnapshotProxy = class HeapSnapshotProxy extends WebInspector.Object
 {
-    constructor(snapshotObjectId, identifier, totalSize, totalObjectCount, categories)
+    constructor(snapshotObjectId, identifier, title, totalSize, totalObjectCount, categories)
     {
         super();
 
         this._proxyObjectId = snapshotObjectId;
 
         this._identifier = identifier;
+        this._title = title;
         this._totalSize = totalSize;
         this._totalObjectCount = totalObjectCount;
         this._categories = Map.fromObject(categories);
@@ -41,14 +42,15 @@ WebInspector.HeapSnapshotProxy = class HeapSnapshotProxy extends WebInspector.Ob
 
     static deserialize(objectId, serializedSnapshot)
     {
-        let {identifier, totalSize, totalObjectCount, categories} = serializedSnapshot;
-        return new WebInspector.HeapSnapshotProxy(objectId, identifier, totalSize, totalObjectCount, categories);
+        let {identifier, title, totalSize, totalObjectCount, categories} = serializedSnapshot;
+        return new WebInspector.HeapSnapshotProxy(objectId, identifier, title, totalSize, totalObjectCount, categories);
     }
 
     // Public
 
     get proxyObjectId() { return this._proxyObjectId; }
     get identifier() { return this._identifier; }
+    get title() { return this._title; }
     get totalSize() { return this._totalSize; }
     get totalObjectCount() { return this._totalObjectCount; }
     get categories() { return this._categories; }
index 6f37a22..95bfadb 100644 (file)
@@ -33,7 +33,7 @@ WebInspector.HeapAllocationsTimelineDataGridNode = class HeapAllocationsTimeline
         this._heapAllocationsView = heapAllocationsView;
 
         this._data = {
-            name: WebInspector.UIString("Snapshot %d").format(this._record.heapSnapshot.identifier),
+            name: WebInspector.TimelineTabContentView.displayNameForRecord(heapAllocationsTimelineRecord),
             timestamp: this._record.timestamp - zeroTime,
             size: this._record.heapSnapshot.totalSize,
         };
index 68cf173..decb5ff 100644 (file)
@@ -251,6 +251,8 @@ WebInspector.TimelineTabContentView = class TimelineTabContentView extends WebIn
         case WebInspector.TimelineRecord.Type.RenderingFrame:
             return WebInspector.UIString("Frame %d").format(timelineRecord.frameNumber);
         case WebInspector.TimelineRecord.Type.HeapAllocations:
+            if (timelineRecord.heapSnapshot.title)
+                return WebInspector.UIString("Snapshot %d \u2014 %s").format(timelineRecord.heapSnapshot.identifier, timelineRecord.heapSnapshot.title);
             return WebInspector.UIString("Snapshot %d").format(timelineRecord.heapSnapshot.identifier);
         case WebInspector.TimelineRecord.Type.Memory:
             // Not used. Fall through to error just in case.
index a03a429..309ec65 100644 (file)
@@ -72,10 +72,11 @@ let nextSnapshotIdentifier = 1;
 
 HeapSnapshot = class HeapSnapshot
 {
-    constructor(objectId, snapshotDataString)
+    constructor(objectId, snapshotDataString, title = null)
     {
         this._identifier = nextSnapshotIdentifier++;
         this._objectId = objectId;
+        this._title = title;
 
         let json = JSON.parse(snapshotDataString);
         snapshotDataString = null;
@@ -309,6 +310,7 @@ HeapSnapshot = class HeapSnapshot
     {
         return {
             identifier: this._identifier,
+            title: this._title,
             totalSize: this._totalSize,
             totalObjectCount: this._nodeCount - 1, // <root>.
             categories: this._categories,
index b51b56c..caab549 100644 (file)
@@ -39,10 +39,10 @@ HeapSnapshotWorker = class HeapSnapshotWorker
 
     // Actions
 
-    createSnapshot(snapshotString)
+    createSnapshot(snapshotString, title)
     {
         let objectId = this._nextObjectId++;
-        let snapshot = new HeapSnapshot(objectId, snapshotString);
+        let snapshot = new HeapSnapshot(objectId, snapshotString, title);
         this._objects.set(objectId, snapshot);
         return {objectId, snapshot: snapshot.serialize()};
     }