]>
Commit | Line | Data |
---|---|---|
81345200 A |
1 | /* |
2 | * Copyright (C) 2014 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 | #if ENABLE(INSPECTOR) | |
30 | ||
31 | #include "Completion.h" | |
32 | #include "ErrorHandlingScope.h" | |
33 | #include "InjectedScriptHost.h" | |
34 | #include "InjectedScriptManager.h" | |
35 | #include "InspectorAgent.h" | |
36 | #include "InspectorBackendDispatcher.h" | |
37 | #include "InspectorFrontendChannel.h" | |
38 | #include "JSConsoleClient.h" | |
39 | #include "JSGlobalObject.h" | |
40 | #include "JSGlobalObjectConsoleAgent.h" | |
41 | #include "JSGlobalObjectDebuggerAgent.h" | |
42 | #include "JSGlobalObjectProfilerAgent.h" | |
43 | #include "JSGlobalObjectRuntimeAgent.h" | |
44 | #include "ScriptArguments.h" | |
45 | #include "ScriptCallStack.h" | |
46 | #include "ScriptCallStackFactory.h" | |
47 | #include <cxxabi.h> | |
48 | #include <dlfcn.h> | |
49 | #include <execinfo.h> | |
50 | ||
40a37d08 A |
51 | #if ENABLE(REMOTE_INSPECTOR) |
52 | #include "JSGlobalObjectDebuggable.h" | |
53 | #include "RemoteInspector.h" | |
54 | #endif | |
55 | ||
81345200 A |
56 | using namespace JSC; |
57 | ||
58 | namespace Inspector { | |
59 | ||
60 | JSGlobalObjectInspectorController::JSGlobalObjectInspectorController(JSGlobalObject& globalObject) | |
61 | : m_globalObject(globalObject) | |
62 | , m_injectedScriptManager(std::make_unique<InjectedScriptManager>(*this, InjectedScriptHost::create())) | |
63 | , m_inspectorFrontendChannel(nullptr) | |
64 | , m_includeNativeCallStackWithExceptions(true) | |
65 | { | |
66 | auto runtimeAgent = std::make_unique<JSGlobalObjectRuntimeAgent>(m_injectedScriptManager.get(), m_globalObject); | |
67 | auto consoleAgent = std::make_unique<JSGlobalObjectConsoleAgent>(m_injectedScriptManager.get()); | |
68 | auto debuggerAgent = std::make_unique<JSGlobalObjectDebuggerAgent>(m_injectedScriptManager.get(), m_globalObject, consoleAgent.get()); | |
69 | auto profilerAgent = std::make_unique<JSGlobalObjectProfilerAgent>(m_globalObject); | |
70 | ||
71 | m_consoleAgent = consoleAgent.get(); | |
72 | m_consoleClient = std::make_unique<JSConsoleClient>(m_consoleAgent, profilerAgent.get()); | |
73 | ||
74 | runtimeAgent->setScriptDebugServer(&debuggerAgent->scriptDebugServer()); | |
75 | profilerAgent->setScriptDebugServer(&debuggerAgent->scriptDebugServer()); | |
76 | ||
77 | m_agents.append(std::make_unique<InspectorAgent>()); | |
78 | m_agents.append(WTF::move(runtimeAgent)); | |
79 | m_agents.append(WTF::move(consoleAgent)); | |
80 | m_agents.append(WTF::move(debuggerAgent)); | |
81 | } | |
82 | ||
83 | JSGlobalObjectInspectorController::~JSGlobalObjectInspectorController() | |
84 | { | |
85 | m_agents.discardAgents(); | |
86 | } | |
87 | ||
88 | void JSGlobalObjectInspectorController::globalObjectDestroyed() | |
89 | { | |
90 | disconnectFrontend(InspectorDisconnectReason::InspectedTargetDestroyed); | |
91 | ||
92 | m_injectedScriptManager->disconnect(); | |
93 | } | |
94 | ||
95 | void JSGlobalObjectInspectorController::connectFrontend(InspectorFrontendChannel* frontendChannel) | |
96 | { | |
97 | ASSERT(!m_inspectorFrontendChannel); | |
98 | ASSERT(!m_inspectorBackendDispatcher); | |
99 | ||
100 | m_inspectorFrontendChannel = frontendChannel; | |
101 | m_inspectorBackendDispatcher = InspectorBackendDispatcher::create(frontendChannel); | |
102 | ||
103 | m_agents.didCreateFrontendAndBackend(frontendChannel, m_inspectorBackendDispatcher.get()); | |
104 | } | |
105 | ||
106 | void JSGlobalObjectInspectorController::disconnectFrontend(InspectorDisconnectReason reason) | |
107 | { | |
108 | if (!m_inspectorFrontendChannel) | |
109 | return; | |
110 | ||
111 | m_agents.willDestroyFrontendAndBackend(reason); | |
112 | ||
113 | m_inspectorBackendDispatcher->clearFrontend(); | |
114 | m_inspectorBackendDispatcher.clear(); | |
115 | m_inspectorFrontendChannel = nullptr; | |
116 | } | |
117 | ||
118 | void JSGlobalObjectInspectorController::dispatchMessageFromFrontend(const String& message) | |
119 | { | |
120 | if (m_inspectorBackendDispatcher) | |
121 | m_inspectorBackendDispatcher->dispatch(message); | |
122 | } | |
123 | ||
124 | void JSGlobalObjectInspectorController::appendAPIBacktrace(ScriptCallStack* callStack) | |
125 | { | |
126 | static const int framesToShow = 31; | |
127 | static const int framesToSkip = 3; // WTFGetBacktrace, appendAPIBacktrace, reportAPIException. | |
128 | ||
129 | void* samples[framesToShow + framesToSkip]; | |
130 | int frames = framesToShow + framesToSkip; | |
131 | WTFGetBacktrace(samples, &frames); | |
132 | ||
133 | void** stack = samples + framesToSkip; | |
134 | int size = frames - framesToSkip; | |
135 | for (int i = 0; i < size; ++i) { | |
136 | const char* mangledName = nullptr; | |
137 | char* cxaDemangled = nullptr; | |
138 | Dl_info info; | |
139 | if (dladdr(stack[i], &info) && info.dli_sname) | |
140 | mangledName = info.dli_sname; | |
141 | if (mangledName) | |
142 | cxaDemangled = abi::__cxa_demangle(mangledName, nullptr, nullptr, nullptr); | |
143 | if (mangledName || cxaDemangled) | |
144 | callStack->append(ScriptCallFrame(cxaDemangled ? cxaDemangled : mangledName, ASCIILiteral("[native code]"), 0, 0)); | |
145 | else | |
146 | callStack->append(ScriptCallFrame(ASCIILiteral("?"), ASCIILiteral("[native code]"), 0, 0)); | |
147 | free(cxaDemangled); | |
148 | } | |
149 | } | |
150 | ||
151 | void JSGlobalObjectInspectorController::reportAPIException(ExecState* exec, JSValue exception) | |
152 | { | |
153 | if (isTerminatedExecutionException(exception)) | |
154 | return; | |
155 | ||
156 | ErrorHandlingScope errorScope(exec->vm()); | |
157 | ||
158 | RefPtr<ScriptCallStack> callStack = createScriptCallStackFromException(exec, exception, ScriptCallStack::maxCallStackSizeToCapture); | |
159 | if (includesNativeCallStackWhenReportingExceptions()) | |
160 | appendAPIBacktrace(callStack.get()); | |
161 | ||
162 | // FIXME: <http://webkit.org/b/115087> Web Inspector: Should not evaluate JavaScript handling exceptions | |
163 | // If this is a custom exception object, call toString on it to try and get a nice string representation for the exception. | |
164 | String errorMessage = exception.toString(exec)->value(exec); | |
165 | exec->clearException(); | |
166 | ||
167 | if (JSConsoleClient::logToSystemConsole()) { | |
168 | if (callStack->size()) { | |
169 | const ScriptCallFrame& callFrame = callStack->at(0); | |
170 | ConsoleClient::printConsoleMessage(MessageSource::JS, MessageType::Log, MessageLevel::Error, errorMessage, callFrame.sourceURL(), callFrame.lineNumber(), callFrame.columnNumber()); | |
171 | } else | |
172 | ConsoleClient::printConsoleMessage(MessageSource::JS, MessageType::Log, MessageLevel::Error, errorMessage, String(), 0, 0); | |
173 | } | |
174 | ||
175 | m_consoleAgent->addMessageToConsole(MessageSource::JS, MessageType::Log, MessageLevel::Error, errorMessage, callStack); | |
176 | } | |
177 | ||
178 | ConsoleClient* JSGlobalObjectInspectorController::consoleClient() const | |
179 | { | |
180 | return m_consoleClient.get(); | |
181 | } | |
182 | ||
40a37d08 A |
183 | bool JSGlobalObjectInspectorController::developerExtrasEnabled() const |
184 | { | |
185 | #if ENABLE(REMOTE_INSPECTOR) | |
186 | if (!RemoteInspector::shared().enabled()) | |
187 | return false; | |
188 | ||
189 | if (!m_globalObject.inspectorDebuggable().remoteDebuggingAllowed()) | |
190 | return false; | |
191 | #endif | |
192 | ||
193 | return true; | |
194 | } | |
195 | ||
81345200 A |
196 | InspectorFunctionCallHandler JSGlobalObjectInspectorController::functionCallHandler() const |
197 | { | |
198 | return JSC::call; | |
199 | } | |
200 | ||
201 | InspectorEvaluateHandler JSGlobalObjectInspectorController::evaluateHandler() const | |
202 | { | |
203 | return JSC::evaluate; | |
204 | } | |
205 | ||
206 | } // namespace Inspector | |
207 | ||
208 | #endif // ENABLE(INSPECTOR) |