1/*
2 * Copyright (C) 2014, 2015 Apple 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
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "JSGlobalObjectInspectorController.h"
28
29#include "CatchScope.h"
30#include "Completion.h"
31#include "ConsoleMessage.h"
32#include "ErrorHandlingScope.h"
33#include "Exception.h"
34#include "InjectedScriptHost.h"
35#include "InjectedScriptManager.h"
36#include "InspectorAgent.h"
37#include "InspectorBackendDispatcher.h"
38#include "InspectorConsoleAgent.h"
39#include "InspectorFrontendChannel.h"
40#include "InspectorFrontendRouter.h"
41#include "InspectorHeapAgent.h"
42#include "InspectorScriptProfilerAgent.h"
43#include "JSCInlines.h"
44#include "JSGlobalObject.h"
45#include "JSGlobalObjectAuditAgent.h"
46#include "JSGlobalObjectConsoleClient.h"
47#include "JSGlobalObjectDebuggerAgent.h"
48#include "JSGlobalObjectRuntimeAgent.h"
49#include "ScriptArguments.h"
50#include "ScriptCallStack.h"
51#include "ScriptCallStackFactory.h"
52#include <wtf/StackTrace.h>
53#include <wtf/Stopwatch.h>
54
55#if ENABLE(REMOTE_INSPECTOR)
56#include "JSGlobalObjectDebuggable.h"
57#include "RemoteInspector.h"
58#endif
59
60using namespace JSC;
61
62namespace Inspector {
63
64JSGlobalObjectInspectorController::JSGlobalObjectInspectorController(JSGlobalObject& globalObject)
65 : m_globalObject(globalObject)
66 , m_injectedScriptManager(std::make_unique<InjectedScriptManager>(*this, InjectedScriptHost::create()))
67 , m_executionStopwatch(Stopwatch::create())
68 , m_scriptDebugServer(globalObject)
69 , m_frontendRouter(FrontendRouter::create())
70 , m_backendDispatcher(BackendDispatcher::create(m_frontendRouter.copyRef()))
71{
72 auto context = jsAgentContext();
73
74 auto inspectorAgent = std::make_unique<InspectorAgent>(context);
75 auto consoleAgent = std::make_unique<InspectorConsoleAgent>(context);
76
77 m_inspectorAgent = inspectorAgent.get();
78 m_consoleAgent = consoleAgent.get();
79 m_consoleClient = std::make_unique<JSGlobalObjectConsoleClient>(m_consoleAgent);
80
81 m_agents.append(WTFMove(inspectorAgent));
82 m_agents.append(WTFMove(consoleAgent));
83
84 m_executionStopwatch->start();
85}
86
87JSGlobalObjectInspectorController::~JSGlobalObjectInspectorController()
88{
89#if ENABLE(INSPECTOR_ALTERNATE_DISPATCHERS)
90 if (m_augmentingClient)
91 m_augmentingClient->inspectorControllerDestroyed();
92#endif
93}
94
95void JSGlobalObjectInspectorController::globalObjectDestroyed()
96{
97 ASSERT(!m_frontendRouter->hasFrontends());
98
99 m_injectedScriptManager->disconnect();
100
101 m_agents.discardValues();
102}
103
104void JSGlobalObjectInspectorController::connectFrontend(FrontendChannel& frontendChannel, bool isAutomaticInspection, bool immediatelyPause)
105{
106 m_isAutomaticInspection = isAutomaticInspection;
107 m_pauseAfterInitialization = immediatelyPause;
108
109 createLazyAgents();
110
111 bool connectedFirstFrontend = !m_frontendRouter->hasFrontends();
112 m_frontendRouter->connectFrontend(frontendChannel);
113
114 if (!connectedFirstFrontend)
115 return;
116
117 // Keep the JSGlobalObject and VM alive while we are debugging it.
118 m_strongVM = &m_globalObject.vm();
119 m_strongGlobalObject.set(m_globalObject.vm(), &m_globalObject);
120
121 // FIXME: change this to notify agents which frontend has connected (by id).
122 m_agents.didCreateFrontendAndBackend(nullptr, nullptr);
123
124#if ENABLE(INSPECTOR_ALTERNATE_DISPATCHERS)
125 m_inspectorAgent->activateExtraDomains(m_agents.extraDomains());
126
127 if (m_augmentingClient)
128 m_augmentingClient->inspectorConnected();
129#endif
130}
131
132void JSGlobalObjectInspectorController::disconnectFrontend(FrontendChannel& frontendChannel)
133{
134 // FIXME: change this to notify agents which frontend has disconnected (by id).
135 m_agents.willDestroyFrontendAndBackend(DisconnectReason::InspectorDestroyed);
136
137 m_frontendRouter->disconnectFrontend(frontendChannel);
138
139 m_isAutomaticInspection = false;
140 m_pauseAfterInitialization = false;
141
142 bool disconnectedLastFrontend = !m_frontendRouter->hasFrontends();
143 if (!disconnectedLastFrontend)
144 return;
145
146#if ENABLE(INSPECTOR_ALTERNATE_DISPATCHERS)
147 if (m_augmentingClient)
148 m_augmentingClient->inspectorDisconnected();
149#endif
150
151 // Remove our JSGlobalObject and VM references, we are done debugging it.
152 m_strongGlobalObject.clear();
153 m_strongVM = nullptr;
154}
155
156void JSGlobalObjectInspectorController::dispatchMessageFromFrontend(const String& message)
157{
158 m_backendDispatcher->dispatch(message);
159}
160
161void JSGlobalObjectInspectorController::appendAPIBacktrace(ScriptCallStack& callStack)
162{
163 static const int framesToShow = 31;
164 static const int framesToSkip = 3; // WTFGetBacktrace, appendAPIBacktrace, reportAPIException.
165
166 void* samples[framesToShow + framesToSkip];
167 int frames = framesToShow + framesToSkip;
168 WTFGetBacktrace(samples, &frames);
169
170 void** stack = samples + framesToSkip;
171 int size = frames - framesToSkip;
172 for (int i = 0; i < size; ++i) {
173 auto demangled = StackTrace::demangle(stack[i]);
174 if (demangled)
175 callStack.append(ScriptCallFrame(demangled->demangledName() ? demangled->demangledName() : demangled->mangledName(), "[native code]"_s, noSourceID, 0, 0));
176 else
177 callStack.append(ScriptCallFrame("?"_s, "[native code]"_s, noSourceID, 0, 0));
178 }
179}
180
181void JSGlobalObjectInspectorController::reportAPIException(ExecState* exec, Exception* exception)
182{
183 VM& vm = exec->vm();
184 if (isTerminatedExecutionException(vm, exception))
185 return;
186
187 auto scope = DECLARE_CATCH_SCOPE(vm);
188 ErrorHandlingScope errorScope(vm);
189
190 Ref<ScriptCallStack> callStack = createScriptCallStackFromException(exec, exception);
191 if (includesNativeCallStackWhenReportingExceptions())
192 appendAPIBacktrace(callStack.get());
193
194 // FIXME: <http://webkit.org/b/115087> Web Inspector: Should not evaluate JavaScript handling exceptions
195 // If this is a custom exception object, call toString on it to try and get a nice string representation for the exception.
196 String errorMessage = exception->value().toWTFString(exec);
197 scope.clearException();
198
199 if (JSGlobalObjectConsoleClient::logToSystemConsole()) {
200 if (callStack->size()) {
201 const ScriptCallFrame& callFrame = callStack->at(0);
202 ConsoleClient::printConsoleMessage(MessageSource::JS, MessageType::Log, MessageLevel::Error, errorMessage, callFrame.sourceURL(), callFrame.lineNumber(), callFrame.columnNumber());
203 } else
204 ConsoleClient::printConsoleMessage(MessageSource::JS, MessageType::Log, MessageLevel::Error, errorMessage, String(), 0, 0);
205 }
206
207 m_consoleAgent->addMessageToConsole(std::make_unique<ConsoleMessage>(MessageSource::JS, MessageType::Log, MessageLevel::Error, errorMessage, WTFMove(callStack)));
208}
209
210ConsoleClient* JSGlobalObjectInspectorController::consoleClient() const
211{
212 return m_consoleClient.get();
213}
214
215bool JSGlobalObjectInspectorController::developerExtrasEnabled() const
216{
217#if ENABLE(REMOTE_INSPECTOR)
218 if (!RemoteInspector::singleton().enabled())
219 return false;
220
221 if (!m_globalObject.inspectorDebuggable().remoteDebuggingAllowed())
222 return false;
223#endif
224
225 return true;
226}
227
228InspectorFunctionCallHandler JSGlobalObjectInspectorController::functionCallHandler() const
229{
230 return JSC::call;
231}
232
233InspectorEvaluateHandler JSGlobalObjectInspectorController::evaluateHandler() const
234{
235 return JSC::evaluate;
236}
237
238void JSGlobalObjectInspectorController::frontendInitialized()
239{
240 if (m_pauseAfterInitialization) {
241 m_pauseAfterInitialization = false;
242
243 ASSERT(m_debuggerAgent);
244 ErrorString ignored;
245 m_debuggerAgent->enable(ignored);
246 m_debuggerAgent->pause(ignored);
247 }
248
249#if ENABLE(REMOTE_INSPECTOR)
250 if (m_isAutomaticInspection)
251 m_globalObject.inspectorDebuggable().unpauseForInitializedInspector();
252#endif
253}
254
255Ref<Stopwatch> JSGlobalObjectInspectorController::executionStopwatch()
256{
257 return m_executionStopwatch.copyRef();
258}
259
260JSGlobalObjectScriptDebugServer& JSGlobalObjectInspectorController::scriptDebugServer()
261{
262 return m_scriptDebugServer;
263}
264
265VM& JSGlobalObjectInspectorController::vm()
266{
267 return m_globalObject.vm();
268}
269
270#if ENABLE(INSPECTOR_ALTERNATE_DISPATCHERS)
271void JSGlobalObjectInspectorController::appendExtraAgent(std::unique_ptr<InspectorAgentBase> agent)
272{
273 String domainName = agent->domainName();
274
275 // FIXME: change this to notify agents which frontend has connected (by id).
276 agent->didCreateFrontendAndBackend(nullptr, nullptr);
277
278 m_agents.appendExtraAgent(WTFMove(agent));
279
280 m_inspectorAgent->activateExtraDomain(domainName);
281}
282#endif
283
284JSAgentContext JSGlobalObjectInspectorController::jsAgentContext()
285{
286 AgentContext baseContext = {
287 *this,
288 *m_injectedScriptManager,
289 m_frontendRouter.get(),
290 m_backendDispatcher.get()
291 };
292
293 JSAgentContext context = {
294 baseContext,
295 m_globalObject
296 };
297
298 return context;
299}
300
301void JSGlobalObjectInspectorController::createLazyAgents()
302{
303 if (m_didCreateLazyAgents)
304 return;
305
306 m_didCreateLazyAgents = true;
307
308 auto context = jsAgentContext();
309
310 m_agents.append(std::make_unique<JSGlobalObjectRuntimeAgent>(context));
311
312 auto debuggerAgent = std::make_unique<JSGlobalObjectDebuggerAgent>(context, m_consoleAgent);
313 m_debuggerAgent = debuggerAgent.get();
314 m_consoleClient->setInspectorDebuggerAgent(m_debuggerAgent);
315 m_agents.append(WTFMove(debuggerAgent));
316
317 auto scriptProfilerAgentPtr = std::make_unique<InspectorScriptProfilerAgent>(context);
318 m_consoleClient->setInspectorScriptProfilerAgent(scriptProfilerAgentPtr.get());
319 m_agents.append(WTFMove(scriptProfilerAgentPtr));
320
321 auto heapAgent = std::make_unique<InspectorHeapAgent>(context);
322 if (m_consoleAgent)
323 m_consoleAgent->setInspectorHeapAgent(heapAgent.get());
324 m_agents.append(WTFMove(heapAgent));
325
326 m_agents.append(std::make_unique<JSGlobalObjectAuditAgent>(context));
327}
328
329} // namespace Inspector
330