e220669cb32ac385df084d982196a083270b09d2
[WebKit-https.git] / Source / WebCore / inspector / InspectorHeapProfilerAgent.cpp
1 /*
2  * Copyright (C) 2013 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32 #include "InspectorHeapProfilerAgent.h"
33
34 #if ENABLE(JAVASCRIPT_DEBUGGER) && ENABLE(INSPECTOR)
35
36 #include "CommandLineAPIHost.h"
37 #include "InstrumentingAgents.h"
38 #include "PageInjectedScriptManager.h"
39 #include "ScriptProfiler.h"
40 #include <inspector/InjectedScript.h>
41
42 using namespace Inspector;
43
44 namespace WebCore {
45
46 static const char* const UserInitiatedProfileNameHeap = "org.webkit.profiles.user-initiated";
47
48 InspectorHeapProfilerAgent::InspectorHeapProfilerAgent(InstrumentingAgents* instrumentingAgents, PageInjectedScriptManager* injectedScriptManager)
49     : InspectorAgentBase(ASCIILiteral("HeapProfiler"), instrumentingAgents)
50     , m_injectedScriptManager(injectedScriptManager)
51     , m_nextUserInitiatedHeapSnapshotNumber(1)
52     , m_profileHeadersRequested(false)
53 {
54     m_instrumentingAgents->setInspectorHeapProfilerAgent(this);
55 }
56
57 InspectorHeapProfilerAgent::~InspectorHeapProfilerAgent()
58 {
59     m_instrumentingAgents->setInspectorHeapProfilerAgent(0);
60 }
61
62 void InspectorHeapProfilerAgent::resetState()
63 {
64     m_snapshots.clear();
65     m_nextUserInitiatedHeapSnapshotNumber = 1;
66     resetFrontendProfiles();
67
68     if (CommandLineAPIHost* commandLineAPIHost = m_injectedScriptManager->commandLineAPIHost())
69         commandLineAPIHost->clearInspectedObjects();
70 }
71
72 void InspectorHeapProfilerAgent::resetFrontendProfiles()
73 {
74     if (!m_frontendDispatcher)
75         return;
76     if (!m_profileHeadersRequested)
77         return;
78     if (m_snapshots.isEmpty())
79         m_frontendDispatcher->resetProfiles();
80 }
81
82 void InspectorHeapProfilerAgent::didCreateFrontendAndBackend(Inspector::InspectorFrontendChannel* frontendChannel, InspectorBackendDispatcher* backendDispatcher)
83 {
84     m_frontendDispatcher = std::make_unique<InspectorHeapProfilerFrontendDispatcher>(frontendChannel);
85     m_backendDispatcher = InspectorHeapProfilerBackendDispatcher::create(backendDispatcher, this);
86 }
87
88 void InspectorHeapProfilerAgent::willDestroyFrontendAndBackend()
89 {
90     m_frontendDispatcher = nullptr;
91     m_backendDispatcher.clear();
92
93     m_profileHeadersRequested = false;
94 }
95
96 void InspectorHeapProfilerAgent::collectGarbage(WebCore::ErrorString*)
97 {
98     ScriptProfiler::collectGarbage();
99 }
100
101 PassRefPtr<Inspector::TypeBuilder::HeapProfiler::ProfileHeader> InspectorHeapProfilerAgent::createSnapshotHeader(const ScriptHeapSnapshot& snapshot)
102 {
103     RefPtr<Inspector::TypeBuilder::HeapProfiler::ProfileHeader> header = Inspector::TypeBuilder::HeapProfiler::ProfileHeader::create()
104         .setUid(snapshot.uid())
105         .setTitle(snapshot.title());
106     header->setMaxJSObjectId(snapshot.maxSnapshotJSObjectId());
107     return header.release();
108 }
109
110 void InspectorHeapProfilerAgent::hasHeapProfiler(ErrorString*, bool* result)
111 {
112     *result = ScriptProfiler::hasHeapProfiler();
113 }
114
115 void InspectorHeapProfilerAgent::getProfileHeaders(ErrorString*, RefPtr<Inspector::TypeBuilder::Array<Inspector::TypeBuilder::HeapProfiler::ProfileHeader>>& headers)
116 {
117     m_profileHeadersRequested = true;
118     headers = Inspector::TypeBuilder::Array<Inspector::TypeBuilder::HeapProfiler::ProfileHeader>::create();
119
120     IdToHeapSnapshotMap::iterator snapshotsEnd = m_snapshots.end();
121     for (IdToHeapSnapshotMap::iterator it = m_snapshots.begin(); it != snapshotsEnd; ++it)
122         headers->addItem(createSnapshotHeader(*it->value));
123 }
124
125 void InspectorHeapProfilerAgent::getHeapSnapshot(ErrorString* errorString, int rawUid)
126 {
127     class OutputStream : public ScriptHeapSnapshot::OutputStream {
128     public:
129         OutputStream(InspectorHeapProfilerFrontendDispatcher* frontend, unsigned uid)
130             : m_frontendDispatcher(frontend), m_uid(uid) { }
131         void Write(const String& chunk) { m_frontendDispatcher->addHeapSnapshotChunk(m_uid, chunk); }
132         void Close() { m_frontendDispatcher->finishHeapSnapshot(m_uid); }
133     private:
134         InspectorHeapProfilerFrontendDispatcher* m_frontendDispatcher;
135         int m_uid;
136     };
137
138     unsigned uid = static_cast<unsigned>(rawUid);
139     IdToHeapSnapshotMap::iterator it = m_snapshots.find(uid);
140     if (it == m_snapshots.end()) {
141         *errorString = "Profile wasn't found";
142         return;
143     }
144     RefPtr<ScriptHeapSnapshot> snapshot = it->value;
145     if (m_frontendDispatcher) {
146         OutputStream stream(m_frontendDispatcher.get(), uid);
147         snapshot->writeJSON(&stream);
148     }
149 }
150
151 void InspectorHeapProfilerAgent::removeProfile(ErrorString*, int rawUid)
152 {
153     unsigned uid = static_cast<unsigned>(rawUid);
154     if (m_snapshots.contains(uid))
155         m_snapshots.remove(uid);
156 }
157
158 void InspectorHeapProfilerAgent::takeHeapSnapshot(ErrorString*, const bool* reportProgress)
159 {
160     class HeapSnapshotProgress: public ScriptProfiler::HeapSnapshotProgress {
161     public:
162         explicit HeapSnapshotProgress(InspectorHeapProfilerFrontendDispatcher* frontend)
163             : m_frontendDispatcher(frontend) { }
164         void Start(int totalWork)
165         {
166             m_totalWork = totalWork;
167         }
168         void Worked(int workDone)
169         {
170             if (m_frontendDispatcher)
171                 m_frontendDispatcher->reportHeapSnapshotProgress(workDone, m_totalWork);
172         }
173         void Done() { }
174         bool isCanceled() { return false; }
175     private:
176         InspectorHeapProfilerFrontendDispatcher* m_frontendDispatcher;
177         int m_totalWork;
178     };
179
180     String title = makeString(UserInitiatedProfileNameHeap, '.', String::number(m_nextUserInitiatedHeapSnapshotNumber));
181     ++m_nextUserInitiatedHeapSnapshotNumber;
182
183     HeapSnapshotProgress progress(reportProgress && *reportProgress ? m_frontendDispatcher.get() : nullptr);
184     RefPtr<ScriptHeapSnapshot> snapshot = ScriptProfiler::takeHeapSnapshot(title, &progress);
185     if (snapshot) {
186         m_snapshots.add(snapshot->uid(), snapshot);
187         if (m_frontendDispatcher)
188             m_frontendDispatcher->addProfileHeader(createSnapshotHeader(*snapshot));
189     }
190 }
191
192 void InspectorHeapProfilerAgent::getObjectByHeapObjectId(ErrorString* error, const String& heapSnapshotObjectId, const String* objectGroup, RefPtr<Inspector::TypeBuilder::Runtime::RemoteObject>& result)
193 {
194     bool ok;
195     unsigned id = heapSnapshotObjectId.toUInt(&ok);
196     if (!ok) {
197         *error = "Invalid heap snapshot object id";
198         return;
199     }
200     Deprecated::ScriptObject heapObject = ScriptProfiler::objectByHeapObjectId(id);
201     if (heapObject.hasNoValue()) {
202         *error = "Object is not available";
203         return;
204     }
205     InjectedScript injectedScript = m_injectedScriptManager->injectedScriptFor(heapObject.scriptState());
206     if (injectedScript.hasNoValue()) {
207         *error = "Object is not available. Inspected context is gone";
208         return;
209     }
210     result = injectedScript.wrapObject(heapObject, objectGroup ? *objectGroup : "");
211     if (!result)
212         *error = "Failed to wrap object";
213 }
214
215 void InspectorHeapProfilerAgent::getHeapObjectId(ErrorString* errorString, const String& objectId, String* heapSnapshotObjectId)
216 {
217     InjectedScript injectedScript = m_injectedScriptManager->injectedScriptForObjectId(objectId);
218     if (injectedScript.hasNoValue()) {
219         *errorString = "Inspected context has gone";
220         return;
221     }
222     Deprecated::ScriptValue value = injectedScript.findObjectById(objectId);
223     if (value.hasNoValue() || value.isUndefined()) {
224         *errorString = "Object with given id not found";
225         return;
226     }
227     unsigned id = ScriptProfiler::getHeapObjectId(value);
228     *heapSnapshotObjectId = String::number(id);
229 }
230
231 } // namespace WebCore
232
233 #endif // ENABLE(JAVASCRIPT_DEBUGGER) && ENABLE(INSPECTOR)