OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2010-2011 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 "src/inspector/V8InspectorImpl.h" |
| 32 |
| 33 #include <v8-profiler.h> |
| 34 #include "src/inspector/InspectedContext.h" |
| 35 #include "src/inspector/StringUtil.h" |
| 36 #include "src/inspector/V8Compat.h" |
| 37 #include "src/inspector/V8ConsoleAgentImpl.h" |
| 38 #include "src/inspector/V8ConsoleMessage.h" |
| 39 #include "src/inspector/V8Debugger.h" |
| 40 #include "src/inspector/V8DebuggerAgentImpl.h" |
| 41 #include "src/inspector/V8InspectorSessionImpl.h" |
| 42 #include "src/inspector/V8RuntimeAgentImpl.h" |
| 43 #include "src/inspector/V8StackTraceImpl.h" |
| 44 #include "src/inspector/protocol/Protocol.h" |
| 45 #include "src/inspector/public/V8InspectorClient.h" |
| 46 |
| 47 namespace v8_inspector { |
| 48 |
| 49 std::unique_ptr<V8Inspector> V8Inspector::create(v8::Isolate* isolate, |
| 50 V8InspectorClient* client) { |
| 51 return wrapUnique(new V8InspectorImpl(isolate, client)); |
| 52 } |
| 53 |
| 54 V8InspectorImpl::V8InspectorImpl(v8::Isolate* isolate, |
| 55 V8InspectorClient* client) |
| 56 : m_isolate(isolate), |
| 57 m_client(client), |
| 58 m_debugger(new V8Debugger(isolate, this)), |
| 59 m_capturingStackTracesCount(0), |
| 60 m_lastExceptionId(0) {} |
| 61 |
| 62 V8InspectorImpl::~V8InspectorImpl() {} |
| 63 |
| 64 V8DebuggerAgentImpl* V8InspectorImpl::enabledDebuggerAgentForGroup( |
| 65 int contextGroupId) { |
| 66 V8InspectorSessionImpl* session = sessionForContextGroup(contextGroupId); |
| 67 V8DebuggerAgentImpl* agent = session ? session->debuggerAgent() : nullptr; |
| 68 return agent && agent->enabled() ? agent : nullptr; |
| 69 } |
| 70 |
| 71 V8RuntimeAgentImpl* V8InspectorImpl::enabledRuntimeAgentForGroup( |
| 72 int contextGroupId) { |
| 73 V8InspectorSessionImpl* session = sessionForContextGroup(contextGroupId); |
| 74 V8RuntimeAgentImpl* agent = session ? session->runtimeAgent() : nullptr; |
| 75 return agent && agent->enabled() ? agent : nullptr; |
| 76 } |
| 77 |
| 78 v8::MaybeLocal<v8::Value> V8InspectorImpl::runCompiledScript( |
| 79 v8::Local<v8::Context> context, v8::Local<v8::Script> script) { |
| 80 v8::MicrotasksScope microtasksScope(m_isolate, |
| 81 v8::MicrotasksScope::kRunMicrotasks); |
| 82 int groupId = V8Debugger::getGroupId(context); |
| 83 if (V8DebuggerAgentImpl* agent = enabledDebuggerAgentForGroup(groupId)) |
| 84 agent->willExecuteScript(script->GetUnboundScript()->GetId()); |
| 85 v8::MaybeLocal<v8::Value> result = script->Run(context); |
| 86 // Get agent from the map again, since it could have detached during script |
| 87 // execution. |
| 88 if (V8DebuggerAgentImpl* agent = enabledDebuggerAgentForGroup(groupId)) |
| 89 agent->didExecuteScript(); |
| 90 return result; |
| 91 } |
| 92 |
| 93 v8::MaybeLocal<v8::Value> V8InspectorImpl::callFunction( |
| 94 v8::Local<v8::Function> function, v8::Local<v8::Context> context, |
| 95 v8::Local<v8::Value> receiver, int argc, v8::Local<v8::Value> info[]) { |
| 96 v8::MicrotasksScope microtasksScope(m_isolate, |
| 97 v8::MicrotasksScope::kRunMicrotasks); |
| 98 int groupId = V8Debugger::getGroupId(context); |
| 99 if (V8DebuggerAgentImpl* agent = enabledDebuggerAgentForGroup(groupId)) |
| 100 agent->willExecuteScript(function->ScriptId()); |
| 101 v8::MaybeLocal<v8::Value> result = |
| 102 function->Call(context, receiver, argc, info); |
| 103 // Get agent from the map again, since it could have detached during script |
| 104 // execution. |
| 105 if (V8DebuggerAgentImpl* agent = enabledDebuggerAgentForGroup(groupId)) |
| 106 agent->didExecuteScript(); |
| 107 return result; |
| 108 } |
| 109 |
| 110 v8::MaybeLocal<v8::Value> V8InspectorImpl::compileAndRunInternalScript( |
| 111 v8::Local<v8::Context> context, v8::Local<v8::String> source) { |
| 112 v8::Local<v8::Script> script = |
| 113 compileScript(context, source, String16(), true); |
| 114 if (script.IsEmpty()) return v8::MaybeLocal<v8::Value>(); |
| 115 v8::MicrotasksScope microtasksScope(m_isolate, |
| 116 v8::MicrotasksScope::kDoNotRunMicrotasks); |
| 117 return script->Run(context); |
| 118 } |
| 119 |
| 120 v8::Local<v8::Script> V8InspectorImpl::compileScript( |
| 121 v8::Local<v8::Context> context, v8::Local<v8::String> code, |
| 122 const String16& fileName, bool markAsInternal) { |
| 123 v8::ScriptOrigin origin( |
| 124 toV8String(m_isolate, fileName), v8::Integer::New(m_isolate, 0), |
| 125 v8::Integer::New(m_isolate, 0), |
| 126 v8::False(m_isolate), // sharable |
| 127 v8::Local<v8::Integer>(), |
| 128 v8::Boolean::New(m_isolate, markAsInternal), // internal |
| 129 toV8String(m_isolate, String16()), // sourceMap |
| 130 v8::True(m_isolate)); // opaqueresource |
| 131 v8::ScriptCompiler::Source source(code, origin); |
| 132 v8::Local<v8::Script> script; |
| 133 if (!v8::ScriptCompiler::Compile(context, &source, |
| 134 v8::ScriptCompiler::kNoCompileOptions) |
| 135 .ToLocal(&script)) |
| 136 return v8::Local<v8::Script>(); |
| 137 return script; |
| 138 } |
| 139 |
| 140 void V8InspectorImpl::enableStackCapturingIfNeeded() { |
| 141 if (!m_capturingStackTracesCount) |
| 142 V8StackTraceImpl::setCaptureStackTraceForUncaughtExceptions(m_isolate, |
| 143 true); |
| 144 ++m_capturingStackTracesCount; |
| 145 } |
| 146 |
| 147 void V8InspectorImpl::disableStackCapturingIfNeeded() { |
| 148 if (!(--m_capturingStackTracesCount)) |
| 149 V8StackTraceImpl::setCaptureStackTraceForUncaughtExceptions(m_isolate, |
| 150 false); |
| 151 } |
| 152 |
| 153 void V8InspectorImpl::muteExceptions(int contextGroupId) { |
| 154 m_muteExceptionsMap[contextGroupId]++; |
| 155 } |
| 156 |
| 157 void V8InspectorImpl::unmuteExceptions(int contextGroupId) { |
| 158 m_muteExceptionsMap[contextGroupId]--; |
| 159 } |
| 160 |
| 161 V8ConsoleMessageStorage* V8InspectorImpl::ensureConsoleMessageStorage( |
| 162 int contextGroupId) { |
| 163 ConsoleStorageMap::iterator storageIt = |
| 164 m_consoleStorageMap.find(contextGroupId); |
| 165 if (storageIt == m_consoleStorageMap.end()) |
| 166 storageIt = |
| 167 m_consoleStorageMap |
| 168 .insert(std::make_pair( |
| 169 contextGroupId, |
| 170 wrapUnique(new V8ConsoleMessageStorage(this, contextGroupId)))) |
| 171 .first; |
| 172 return storageIt->second.get(); |
| 173 } |
| 174 |
| 175 std::unique_ptr<V8StackTrace> V8InspectorImpl::createStackTrace( |
| 176 v8::Local<v8::StackTrace> stackTrace) { |
| 177 return m_debugger->createStackTrace(stackTrace); |
| 178 } |
| 179 |
| 180 std::unique_ptr<V8InspectorSession> V8InspectorImpl::connect( |
| 181 int contextGroupId, V8Inspector::Channel* channel, |
| 182 const StringView& state) { |
| 183 DCHECK(m_sessions.find(contextGroupId) == m_sessions.cend()); |
| 184 std::unique_ptr<V8InspectorSessionImpl> session = |
| 185 V8InspectorSessionImpl::create(this, contextGroupId, channel, state); |
| 186 m_sessions[contextGroupId] = session.get(); |
| 187 return std::move(session); |
| 188 } |
| 189 |
| 190 void V8InspectorImpl::disconnect(V8InspectorSessionImpl* session) { |
| 191 DCHECK(m_sessions.find(session->contextGroupId()) != m_sessions.end()); |
| 192 m_sessions.erase(session->contextGroupId()); |
| 193 } |
| 194 |
| 195 InspectedContext* V8InspectorImpl::getContext(int groupId, |
| 196 int contextId) const { |
| 197 if (!groupId || !contextId) return nullptr; |
| 198 |
| 199 ContextsByGroupMap::const_iterator contextGroupIt = m_contexts.find(groupId); |
| 200 if (contextGroupIt == m_contexts.end()) return nullptr; |
| 201 |
| 202 ContextByIdMap::iterator contextIt = contextGroupIt->second->find(contextId); |
| 203 if (contextIt == contextGroupIt->second->end()) return nullptr; |
| 204 |
| 205 return contextIt->second.get(); |
| 206 } |
| 207 |
| 208 void V8InspectorImpl::contextCreated(const V8ContextInfo& info) { |
| 209 int contextId = m_debugger->markContext(info); |
| 210 |
| 211 ContextsByGroupMap::iterator contextIt = m_contexts.find(info.contextGroupId); |
| 212 if (contextIt == m_contexts.end()) |
| 213 contextIt = m_contexts |
| 214 .insert(std::make_pair(info.contextGroupId, |
| 215 wrapUnique(new ContextByIdMap()))) |
| 216 .first; |
| 217 |
| 218 const auto& contextById = contextIt->second; |
| 219 |
| 220 DCHECK(contextById->find(contextId) == contextById->cend()); |
| 221 InspectedContext* context = new InspectedContext(this, info, contextId); |
| 222 (*contextById)[contextId] = wrapUnique(context); |
| 223 SessionMap::iterator sessionIt = m_sessions.find(info.contextGroupId); |
| 224 if (sessionIt != m_sessions.end()) |
| 225 sessionIt->second->runtimeAgent()->reportExecutionContextCreated(context); |
| 226 } |
| 227 |
| 228 void V8InspectorImpl::contextDestroyed(v8::Local<v8::Context> context) { |
| 229 int contextId = V8Debugger::contextId(context); |
| 230 int contextGroupId = V8Debugger::getGroupId(context); |
| 231 |
| 232 ConsoleStorageMap::iterator storageIt = |
| 233 m_consoleStorageMap.find(contextGroupId); |
| 234 if (storageIt != m_consoleStorageMap.end()) |
| 235 storageIt->second->contextDestroyed(contextId); |
| 236 |
| 237 InspectedContext* inspectedContext = getContext(contextGroupId, contextId); |
| 238 if (!inspectedContext) return; |
| 239 |
| 240 SessionMap::iterator iter = m_sessions.find(contextGroupId); |
| 241 if (iter != m_sessions.end()) |
| 242 iter->second->runtimeAgent()->reportExecutionContextDestroyed( |
| 243 inspectedContext); |
| 244 discardInspectedContext(contextGroupId, contextId); |
| 245 } |
| 246 |
| 247 void V8InspectorImpl::resetContextGroup(int contextGroupId) { |
| 248 m_consoleStorageMap.erase(contextGroupId); |
| 249 m_muteExceptionsMap.erase(contextGroupId); |
| 250 SessionMap::iterator session = m_sessions.find(contextGroupId); |
| 251 if (session != m_sessions.end()) session->second->reset(); |
| 252 m_contexts.erase(contextGroupId); |
| 253 } |
| 254 |
| 255 void V8InspectorImpl::willExecuteScript(v8::Local<v8::Context> context, |
| 256 int scriptId) { |
| 257 if (V8DebuggerAgentImpl* agent = |
| 258 enabledDebuggerAgentForGroup(V8Debugger::getGroupId(context))) |
| 259 agent->willExecuteScript(scriptId); |
| 260 } |
| 261 |
| 262 void V8InspectorImpl::didExecuteScript(v8::Local<v8::Context> context) { |
| 263 if (V8DebuggerAgentImpl* agent = |
| 264 enabledDebuggerAgentForGroup(V8Debugger::getGroupId(context))) |
| 265 agent->didExecuteScript(); |
| 266 } |
| 267 |
| 268 void V8InspectorImpl::idleStarted() { |
| 269 m_isolate->GetCpuProfiler()->SetIdle(true); |
| 270 } |
| 271 |
| 272 void V8InspectorImpl::idleFinished() { |
| 273 m_isolate->GetCpuProfiler()->SetIdle(false); |
| 274 } |
| 275 |
| 276 unsigned V8InspectorImpl::exceptionThrown( |
| 277 v8::Local<v8::Context> context, const StringView& message, |
| 278 v8::Local<v8::Value> exception, const StringView& detailedMessage, |
| 279 const StringView& url, unsigned lineNumber, unsigned columnNumber, |
| 280 std::unique_ptr<V8StackTrace> stackTrace, int scriptId) { |
| 281 int contextGroupId = V8Debugger::getGroupId(context); |
| 282 if (!contextGroupId || m_muteExceptionsMap[contextGroupId]) return 0; |
| 283 std::unique_ptr<V8StackTraceImpl> stackTraceImpl = |
| 284 wrapUnique(static_cast<V8StackTraceImpl*>(stackTrace.release())); |
| 285 unsigned exceptionId = nextExceptionId(); |
| 286 std::unique_ptr<V8ConsoleMessage> consoleMessage = |
| 287 V8ConsoleMessage::createForException( |
| 288 m_client->currentTimeMS(), toString16(detailedMessage), |
| 289 toString16(url), lineNumber, columnNumber, std::move(stackTraceImpl), |
| 290 scriptId, m_isolate, toString16(message), |
| 291 V8Debugger::contextId(context), exception, exceptionId); |
| 292 ensureConsoleMessageStorage(contextGroupId) |
| 293 ->addMessage(std::move(consoleMessage)); |
| 294 return exceptionId; |
| 295 } |
| 296 |
| 297 void V8InspectorImpl::exceptionRevoked(v8::Local<v8::Context> context, |
| 298 unsigned exceptionId, |
| 299 const StringView& message) { |
| 300 int contextGroupId = V8Debugger::getGroupId(context); |
| 301 if (!contextGroupId) return; |
| 302 |
| 303 std::unique_ptr<V8ConsoleMessage> consoleMessage = |
| 304 V8ConsoleMessage::createForRevokedException( |
| 305 m_client->currentTimeMS(), toString16(message), exceptionId); |
| 306 ensureConsoleMessageStorage(contextGroupId) |
| 307 ->addMessage(std::move(consoleMessage)); |
| 308 } |
| 309 |
| 310 std::unique_ptr<V8StackTrace> V8InspectorImpl::captureStackTrace( |
| 311 bool fullStack) { |
| 312 return m_debugger->captureStackTrace(fullStack); |
| 313 } |
| 314 |
| 315 void V8InspectorImpl::asyncTaskScheduled(const StringView& taskName, void* task, |
| 316 bool recurring) { |
| 317 m_debugger->asyncTaskScheduled(taskName, task, recurring); |
| 318 } |
| 319 |
| 320 void V8InspectorImpl::asyncTaskCanceled(void* task) { |
| 321 m_debugger->asyncTaskCanceled(task); |
| 322 } |
| 323 |
| 324 void V8InspectorImpl::asyncTaskStarted(void* task) { |
| 325 m_debugger->asyncTaskStarted(task); |
| 326 } |
| 327 |
| 328 void V8InspectorImpl::asyncTaskFinished(void* task) { |
| 329 m_debugger->asyncTaskFinished(task); |
| 330 } |
| 331 |
| 332 void V8InspectorImpl::allAsyncTasksCanceled() { |
| 333 m_debugger->allAsyncTasksCanceled(); |
| 334 } |
| 335 |
| 336 v8::Local<v8::Context> V8InspectorImpl::regexContext() { |
| 337 if (m_regexContext.IsEmpty()) |
| 338 m_regexContext.Reset(m_isolate, v8::Context::New(m_isolate)); |
| 339 return m_regexContext.Get(m_isolate); |
| 340 } |
| 341 |
| 342 void V8InspectorImpl::discardInspectedContext(int contextGroupId, |
| 343 int contextId) { |
| 344 if (!getContext(contextGroupId, contextId)) return; |
| 345 m_contexts[contextGroupId]->erase(contextId); |
| 346 if (m_contexts[contextGroupId]->empty()) m_contexts.erase(contextGroupId); |
| 347 } |
| 348 |
| 349 const V8InspectorImpl::ContextByIdMap* V8InspectorImpl::contextGroup( |
| 350 int contextGroupId) { |
| 351 ContextsByGroupMap::iterator iter = m_contexts.find(contextGroupId); |
| 352 return iter == m_contexts.end() ? nullptr : iter->second.get(); |
| 353 } |
| 354 |
| 355 V8InspectorSessionImpl* V8InspectorImpl::sessionForContextGroup( |
| 356 int contextGroupId) { |
| 357 if (!contextGroupId) return nullptr; |
| 358 SessionMap::iterator iter = m_sessions.find(contextGroupId); |
| 359 return iter == m_sessions.end() ? nullptr : iter->second; |
| 360 } |
| 361 |
| 362 } // namespace v8_inspector |
OLD | NEW |