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/v8-inspector-impl.h" 32 33 #include <vector> 34 35 #include "src/base/platform/mutex.h" 36 #include "src/inspector/inspected-context.h" 37 #include "src/inspector/string-util.h" 38 #include "src/inspector/v8-console-agent-impl.h" 39 #include "src/inspector/v8-console-message.h" 40 #include "src/inspector/v8-console.h" 41 #include "src/inspector/v8-debugger-agent-impl.h" 42 #include "src/inspector/v8-debugger.h" 43 #include "src/inspector/v8-inspector-session-impl.h" 44 #include "src/inspector/v8-profiler-agent-impl.h" 45 #include "src/inspector/v8-runtime-agent-impl.h" 46 #include "src/inspector/v8-stack-trace-impl.h" 47 48 #include "include/v8-platform.h" 49 50 namespace v8_inspector { 51 52 std::unique_ptr<V8Inspector> V8Inspector::create(v8::Isolate* isolate, 53 V8InspectorClient* client) { 54 return std::unique_ptr<V8Inspector>(new V8InspectorImpl(isolate, client)); 55 } 56 57 V8InspectorImpl::V8InspectorImpl(v8::Isolate* isolate, 58 V8InspectorClient* client) 59 : m_isolate(isolate), 60 m_client(client), 61 m_debugger(new V8Debugger(isolate, this)), 62 m_capturingStackTracesCount(0), 63 m_lastExceptionId(0), 64 m_lastContextId(0), 65 m_isolateId(v8::debug::GetNextRandomInt64(m_isolate)) { 66 v8::debug::SetInspector(m_isolate, this); 67 v8::debug::SetConsoleDelegate(m_isolate, console()); 68 } 69 70 V8InspectorImpl::~V8InspectorImpl() { 71 v8::debug::SetInspector(m_isolate, nullptr); 72 v8::debug::SetConsoleDelegate(m_isolate, nullptr); 73 } 74 75 int V8InspectorImpl::contextGroupId(v8::Local<v8::Context> context) const { 76 return contextGroupId(InspectedContext::contextId(context)); 77 } 78 79 int V8InspectorImpl::contextGroupId(int contextId) const { 80 auto it = m_contextIdToGroupIdMap.find(contextId); 81 return it != m_contextIdToGroupIdMap.end() ? it->second : 0; 82 } 83 84 v8::MaybeLocal<v8::Value> V8InspectorImpl::compileAndRunInternalScript( 85 v8::Local<v8::Context> context, v8::Local<v8::String> source) { 86 v8::Local<v8::UnboundScript> unboundScript; 87 if (!v8::debug::CompileInspectorScript(m_isolate, source) 88 .ToLocal(&unboundScript)) 89 return v8::MaybeLocal<v8::Value>(); 90 v8::MicrotasksScope microtasksScope(m_isolate, 91 v8::MicrotasksScope::kDoNotRunMicrotasks); 92 v8::Context::Scope contextScope(context); 93 v8::Isolate::SafeForTerminationScope allowTermination(m_isolate); 94 return unboundScript->BindToCurrentContext()->Run(context); 95 } 96 97 v8::MaybeLocal<v8::Script> V8InspectorImpl::compileScript( 98 v8::Local<v8::Context> context, const String16& code, 99 const String16& fileName) { 100 v8::ScriptOrigin origin( 101 toV8String(m_isolate, fileName), v8::Integer::New(m_isolate, 0), 102 v8::Integer::New(m_isolate, 0), 103 v8::False(m_isolate), // sharable 104 v8::Local<v8::Integer>(), toV8String(m_isolate, String16()), // sourceMap 105 v8::True(m_isolate)); // opaqueresource 106 v8::ScriptCompiler::Source source(toV8String(m_isolate, code), origin); 107 return v8::ScriptCompiler::Compile(context, &source, 108 v8::ScriptCompiler::kNoCompileOptions); 109 } 110 111 void V8InspectorImpl::enableStackCapturingIfNeeded() { 112 if (!m_capturingStackTracesCount) 113 V8StackTraceImpl::setCaptureStackTraceForUncaughtExceptions(m_isolate, 114 true); 115 ++m_capturingStackTracesCount; 116 } 117 118 void V8InspectorImpl::disableStackCapturingIfNeeded() { 119 if (!(--m_capturingStackTracesCount)) 120 V8StackTraceImpl::setCaptureStackTraceForUncaughtExceptions(m_isolate, 121 false); 122 } 123 124 void V8InspectorImpl::muteExceptions(int contextGroupId) { 125 m_muteExceptionsMap[contextGroupId]++; 126 } 127 128 void V8InspectorImpl::unmuteExceptions(int contextGroupId) { 129 m_muteExceptionsMap[contextGroupId]--; 130 } 131 132 V8ConsoleMessageStorage* V8InspectorImpl::ensureConsoleMessageStorage( 133 int contextGroupId) { 134 ConsoleStorageMap::iterator storageIt = 135 m_consoleStorageMap.find(contextGroupId); 136 if (storageIt == m_consoleStorageMap.end()) 137 storageIt = m_consoleStorageMap 138 .insert(std::make_pair( 139 contextGroupId, 140 std::unique_ptr<V8ConsoleMessageStorage>( 141 new V8ConsoleMessageStorage(this, contextGroupId)))) 142 .first; 143 return storageIt->second.get(); 144 } 145 146 bool V8InspectorImpl::hasConsoleMessageStorage(int contextGroupId) { 147 ConsoleStorageMap::iterator storageIt = 148 m_consoleStorageMap.find(contextGroupId); 149 return storageIt != m_consoleStorageMap.end(); 150 } 151 152 std::unique_ptr<V8StackTrace> V8InspectorImpl::createStackTrace( 153 v8::Local<v8::StackTrace> stackTrace) { 154 return m_debugger->createStackTrace(stackTrace); 155 } 156 157 std::unique_ptr<V8InspectorSession> V8InspectorImpl::connect( 158 int contextGroupId, V8Inspector::Channel* channel, 159 const StringView& state) { 160 int sessionId = ++m_lastSessionId; 161 std::unique_ptr<V8InspectorSessionImpl> session = 162 V8InspectorSessionImpl::create(this, contextGroupId, sessionId, channel, 163 state); 164 m_sessions[contextGroupId][sessionId] = session.get(); 165 return std::move(session); 166 } 167 168 void V8InspectorImpl::disconnect(V8InspectorSessionImpl* session) { 169 auto& map = m_sessions[session->contextGroupId()]; 170 map.erase(session->sessionId()); 171 if (map.empty()) m_sessions.erase(session->contextGroupId()); 172 } 173 174 InspectedContext* V8InspectorImpl::getContext(int groupId, 175 int contextId) const { 176 if (!groupId || !contextId) return nullptr; 177 178 ContextsByGroupMap::const_iterator contextGroupIt = m_contexts.find(groupId); 179 if (contextGroupIt == m_contexts.end()) return nullptr; 180 181 ContextByIdMap::iterator contextIt = contextGroupIt->second->find(contextId); 182 if (contextIt == contextGroupIt->second->end()) return nullptr; 183 184 return contextIt->second.get(); 185 } 186 187 InspectedContext* V8InspectorImpl::getContext(int contextId) const { 188 return getContext(contextGroupId(contextId), contextId); 189 } 190 191 v8::MaybeLocal<v8::Context> V8InspectorImpl::contextById( 192 int groupId, v8::Maybe<int> contextId) { 193 if (contextId.IsNothing()) { 194 v8::Local<v8::Context> context = 195 client()->ensureDefaultContextInGroup(groupId); 196 return context.IsEmpty() ? v8::MaybeLocal<v8::Context>() : context; 197 } 198 InspectedContext* context = getContext(contextId.FromJust()); 199 return context ? context->context() : v8::MaybeLocal<v8::Context>(); 200 } 201 202 void V8InspectorImpl::contextCreated(const V8ContextInfo& info) { 203 int contextId = ++m_lastContextId; 204 InspectedContext* context = new InspectedContext(this, info, contextId); 205 m_contextIdToGroupIdMap[contextId] = info.contextGroupId; 206 207 ContextsByGroupMap::iterator contextIt = m_contexts.find(info.contextGroupId); 208 if (contextIt == m_contexts.end()) 209 contextIt = m_contexts 210 .insert(std::make_pair( 211 info.contextGroupId, 212 std::unique_ptr<ContextByIdMap>(new ContextByIdMap()))) 213 .first; 214 const auto& contextById = contextIt->second; 215 216 DCHECK(contextById->find(contextId) == contextById->cend()); 217 (*contextById)[contextId].reset(context); 218 forEachSession( 219 info.contextGroupId, [&context](V8InspectorSessionImpl* session) { 220 session->runtimeAgent()->addBindings(context); 221 session->runtimeAgent()->reportExecutionContextCreated(context); 222 }); 223 } 224 225 void V8InspectorImpl::contextDestroyed(v8::Local<v8::Context> context) { 226 int contextId = InspectedContext::contextId(context); 227 int groupId = contextGroupId(context); 228 contextCollected(groupId, contextId); 229 } 230 231 void V8InspectorImpl::contextCollected(int groupId, int contextId) { 232 m_contextIdToGroupIdMap.erase(contextId); 233 234 ConsoleStorageMap::iterator storageIt = m_consoleStorageMap.find(groupId); 235 if (storageIt != m_consoleStorageMap.end()) 236 storageIt->second->contextDestroyed(contextId); 237 238 InspectedContext* inspectedContext = getContext(groupId, contextId); 239 if (!inspectedContext) return; 240 241 forEachSession(groupId, [&inspectedContext](V8InspectorSessionImpl* session) { 242 session->runtimeAgent()->reportExecutionContextDestroyed(inspectedContext); 243 }); 244 discardInspectedContext(groupId, contextId); 245 } 246 247 void V8InspectorImpl::resetContextGroup(int contextGroupId) { 248 m_consoleStorageMap.erase(contextGroupId); 249 m_muteExceptionsMap.erase(contextGroupId); 250 forEachSession(contextGroupId, 251 [](V8InspectorSessionImpl* session) { session->reset(); }); 252 m_contexts.erase(contextGroupId); 253 m_debugger->wasmTranslation()->Clear(); 254 } 255 256 void V8InspectorImpl::idleStarted() { m_isolate->SetIdle(true); } 257 258 void V8InspectorImpl::idleFinished() { m_isolate->SetIdle(false); } 259 260 unsigned V8InspectorImpl::exceptionThrown( 261 v8::Local<v8::Context> context, const StringView& message, 262 v8::Local<v8::Value> exception, const StringView& detailedMessage, 263 const StringView& url, unsigned lineNumber, unsigned columnNumber, 264 std::unique_ptr<V8StackTrace> stackTrace, int scriptId) { 265 int groupId = contextGroupId(context); 266 if (!groupId || m_muteExceptionsMap[groupId]) return 0; 267 std::unique_ptr<V8StackTraceImpl> stackTraceImpl( 268 static_cast<V8StackTraceImpl*>(stackTrace.release())); 269 unsigned exceptionId = nextExceptionId(); 270 std::unique_ptr<V8ConsoleMessage> consoleMessage = 271 V8ConsoleMessage::createForException( 272 m_client->currentTimeMS(), toString16(detailedMessage), 273 toString16(url), lineNumber, columnNumber, std::move(stackTraceImpl), 274 scriptId, m_isolate, toString16(message), 275 InspectedContext::contextId(context), exception, exceptionId); 276 ensureConsoleMessageStorage(groupId)->addMessage(std::move(consoleMessage)); 277 return exceptionId; 278 } 279 280 void V8InspectorImpl::exceptionRevoked(v8::Local<v8::Context> context, 281 unsigned exceptionId, 282 const StringView& message) { 283 int groupId = contextGroupId(context); 284 if (!groupId) return; 285 286 std::unique_ptr<V8ConsoleMessage> consoleMessage = 287 V8ConsoleMessage::createForRevokedException( 288 m_client->currentTimeMS(), toString16(message), exceptionId); 289 ensureConsoleMessageStorage(groupId)->addMessage(std::move(consoleMessage)); 290 } 291 292 std::unique_ptr<V8StackTrace> V8InspectorImpl::captureStackTrace( 293 bool fullStack) { 294 return m_debugger->captureStackTrace(fullStack); 295 } 296 297 V8StackTraceId V8InspectorImpl::storeCurrentStackTrace( 298 const StringView& description) { 299 return m_debugger->storeCurrentStackTrace(description); 300 } 301 302 void V8InspectorImpl::externalAsyncTaskStarted(const V8StackTraceId& parent) { 303 m_debugger->externalAsyncTaskStarted(parent); 304 } 305 306 void V8InspectorImpl::externalAsyncTaskFinished(const V8StackTraceId& parent) { 307 m_debugger->externalAsyncTaskFinished(parent); 308 } 309 310 void V8InspectorImpl::asyncTaskScheduled(const StringView& taskName, void* task, 311 bool recurring) { 312 if (!task) return; 313 m_debugger->asyncTaskScheduled(taskName, task, recurring); 314 } 315 316 void V8InspectorImpl::asyncTaskCanceled(void* task) { 317 if (!task) return; 318 m_debugger->asyncTaskCanceled(task); 319 } 320 321 void V8InspectorImpl::asyncTaskStarted(void* task) { 322 if (!task) return; 323 m_debugger->asyncTaskStarted(task); 324 } 325 326 void V8InspectorImpl::asyncTaskFinished(void* task) { 327 if (!task) return; 328 m_debugger->asyncTaskFinished(task); 329 } 330 331 void V8InspectorImpl::allAsyncTasksCanceled() { 332 m_debugger->allAsyncTasksCanceled(); 333 } 334 335 v8::Local<v8::Context> V8InspectorImpl::regexContext() { 336 if (m_regexContext.IsEmpty()) 337 m_regexContext.Reset(m_isolate, v8::Context::New(m_isolate)); 338 return m_regexContext.Get(m_isolate); 339 } 340 341 void V8InspectorImpl::discardInspectedContext(int contextGroupId, 342 int contextId) { 343 if (!getContext(contextGroupId, contextId)) return; 344 m_contexts[contextGroupId]->erase(contextId); 345 if (m_contexts[contextGroupId]->empty()) m_contexts.erase(contextGroupId); 346 } 347 348 V8InspectorSessionImpl* V8InspectorImpl::sessionById(int contextGroupId, 349 int sessionId) { 350 auto it = m_sessions.find(contextGroupId); 351 if (it == m_sessions.end()) return nullptr; 352 auto it2 = it->second.find(sessionId); 353 return it2 == it->second.end() ? nullptr : it2->second; 354 } 355 356 V8Console* V8InspectorImpl::console() { 357 if (!m_console) m_console.reset(new V8Console(this)); 358 return m_console.get(); 359 } 360 361 void V8InspectorImpl::forEachContext( 362 int contextGroupId, std::function<void(InspectedContext*)> callback) { 363 auto it = m_contexts.find(contextGroupId); 364 if (it == m_contexts.end()) return; 365 std::vector<int> ids; 366 ids.reserve(it->second->size()); 367 for (auto& contextIt : *(it->second)) ids.push_back(contextIt.first); 368 369 // Retrieve by ids each time since |callback| may destroy some contexts. 370 for (auto& contextId : ids) { 371 it = m_contexts.find(contextGroupId); 372 if (it == m_contexts.end()) continue; 373 auto contextIt = it->second->find(contextId); 374 if (contextIt != it->second->end()) callback(contextIt->second.get()); 375 } 376 } 377 378 void V8InspectorImpl::forEachSession( 379 int contextGroupId, std::function<void(V8InspectorSessionImpl*)> callback) { 380 auto it = m_sessions.find(contextGroupId); 381 if (it == m_sessions.end()) return; 382 std::vector<int> ids; 383 ids.reserve(it->second.size()); 384 for (auto& sessionIt : it->second) ids.push_back(sessionIt.first); 385 386 // Retrieve by ids each time since |callback| may destroy some contexts. 387 for (auto& sessionId : ids) { 388 it = m_sessions.find(contextGroupId); 389 if (it == m_sessions.end()) continue; 390 auto sessionIt = it->second.find(sessionId); 391 if (sessionIt != it->second.end()) callback(sessionIt->second); 392 } 393 } 394 395 V8InspectorImpl::EvaluateScope::EvaluateScope(v8::Isolate* isolate) 396 : m_isolate(isolate), m_safeForTerminationScope(isolate) {} 397 398 struct V8InspectorImpl::EvaluateScope::CancelToken { 399 v8::base::Mutex m_mutex; 400 bool m_canceled = false; 401 }; 402 403 V8InspectorImpl::EvaluateScope::~EvaluateScope() { 404 if (m_cancelToken) { 405 v8::base::LockGuard<v8::base::Mutex> lock(&m_cancelToken->m_mutex); 406 m_cancelToken->m_canceled = true; 407 m_isolate->CancelTerminateExecution(); 408 } 409 } 410 411 class V8InspectorImpl::EvaluateScope::TerminateTask : public v8::Task { 412 public: 413 TerminateTask(v8::Isolate* isolate, std::shared_ptr<CancelToken> token) 414 : m_isolate(isolate), m_token(token) {} 415 416 void Run() { 417 // CancelToken contains m_canceled bool which may be changed from main 418 // thread, so lock mutex first. 419 v8::base::LockGuard<v8::base::Mutex> lock(&m_token->m_mutex); 420 if (m_token->m_canceled) return; 421 m_isolate->TerminateExecution(); 422 } 423 424 private: 425 v8::Isolate* m_isolate; 426 std::shared_ptr<CancelToken> m_token; 427 }; 428 429 protocol::Response V8InspectorImpl::EvaluateScope::setTimeout(double timeout) { 430 if (m_isolate->IsExecutionTerminating()) { 431 return protocol::Response::Error("Execution was terminated"); 432 } 433 m_cancelToken.reset(new CancelToken()); 434 v8::debug::GetCurrentPlatform()->CallDelayedOnWorkerThread( 435 v8::base::make_unique<TerminateTask>(m_isolate, m_cancelToken), timeout); 436 return protocol::Response::OK(); 437 } 438 439 } // namespace v8_inspector 440