31 #include "src/inspector/v8-inspector-impl.h" 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" 48 #include "include/v8-platform.h" 52 std::unique_ptr<V8Inspector> V8Inspector::create(v8::Isolate* isolate,
53 V8InspectorClient* client) {
54 return std::unique_ptr<V8Inspector>(
new V8InspectorImpl(isolate, client));
57 V8InspectorImpl::V8InspectorImpl(v8::Isolate* isolate,
58 V8InspectorClient* client)
61 m_debugger(new V8Debugger(isolate, this)),
62 m_capturingStackTracesCount(0),
65 m_isolateId(
v8::debug::GetNextRandomInt64(m_isolate)) {
66 v8::debug::SetInspector(m_isolate,
this);
67 v8::debug::SetConsoleDelegate(m_isolate, console());
70 V8InspectorImpl::~V8InspectorImpl() {
71 v8::debug::SetInspector(m_isolate,
nullptr);
72 v8::debug::SetConsoleDelegate(m_isolate,
nullptr);
76 return contextGroupId(InspectedContext::contextId(context));
79 int V8InspectorImpl::contextGroupId(
int contextId)
const {
80 auto it = m_contextIdToGroupIdMap.find(contextId);
81 return it != m_contextIdToGroupIdMap.end() ? it->second : 0;
87 if (!v8::debug::CompileInspectorScript(m_isolate, source)
88 .ToLocal(&unboundScript))
90 v8::MicrotasksScope microtasksScope(m_isolate,
91 v8::MicrotasksScope::kDoNotRunMicrotasks);
99 const String16& fileName) {
101 toV8String(m_isolate, fileName), v8::Integer::New(m_isolate, 0),
102 v8::Integer::New(m_isolate, 0),
103 v8::False(m_isolate),
105 v8::True(m_isolate));
108 v8::ScriptCompiler::kNoCompileOptions);
111 void V8InspectorImpl::enableStackCapturingIfNeeded() {
112 if (!m_capturingStackTracesCount)
113 V8StackTraceImpl::setCaptureStackTraceForUncaughtExceptions(m_isolate,
115 ++m_capturingStackTracesCount;
118 void V8InspectorImpl::disableStackCapturingIfNeeded() {
119 if (!(--m_capturingStackTracesCount))
120 V8StackTraceImpl::setCaptureStackTraceForUncaughtExceptions(m_isolate,
124 void V8InspectorImpl::muteExceptions(
int contextGroupId) {
125 m_muteExceptionsMap[contextGroupId]++;
128 void V8InspectorImpl::unmuteExceptions(
int contextGroupId) {
129 m_muteExceptionsMap[contextGroupId]--;
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(
140 std::unique_ptr<V8ConsoleMessageStorage>(
141 new V8ConsoleMessageStorage(
this, contextGroupId))))
143 return storageIt->second.get();
146 bool V8InspectorImpl::hasConsoleMessageStorage(
int contextGroupId) {
147 ConsoleStorageMap::iterator storageIt =
148 m_consoleStorageMap.find(contextGroupId);
149 return storageIt != m_consoleStorageMap.end();
152 std::unique_ptr<V8StackTrace> V8InspectorImpl::createStackTrace(
154 return m_debugger->createStackTrace(stackTrace);
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,
164 m_sessions[contextGroupId][sessionId] = session.get();
165 return std::move(session);
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());
174 InspectedContext* V8InspectorImpl::getContext(
int groupId,
175 int contextId)
const {
176 if (!groupId || !contextId)
return nullptr;
178 ContextsByGroupMap::const_iterator contextGroupIt = m_contexts.find(groupId);
179 if (contextGroupIt == m_contexts.end())
return nullptr;
181 ContextByIdMap::iterator contextIt = contextGroupIt->second->find(contextId);
182 if (contextIt == contextGroupIt->second->end())
return nullptr;
184 return contextIt->second.get();
187 InspectedContext* V8InspectorImpl::getContext(
int contextId)
const {
188 return getContext(contextGroupId(contextId), contextId);
193 if (contextId.IsNothing()) {
195 client()->ensureDefaultContextInGroup(groupId);
198 InspectedContext* context = getContext(contextId.
FromJust());
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;
207 ContextsByGroupMap::iterator contextIt = m_contexts.find(info.contextGroupId);
208 if (contextIt == m_contexts.end())
209 contextIt = m_contexts
210 .insert(std::make_pair(
212 std::unique_ptr<ContextByIdMap>(
new ContextByIdMap())))
214 const auto& contextById = contextIt->second;
216 DCHECK(contextById->find(contextId) == contextById->cend());
217 (*contextById)[contextId].reset(context);
219 info.contextGroupId, [&context](V8InspectorSessionImpl* session) {
220 session->runtimeAgent()->addBindings(context);
221 session->runtimeAgent()->reportExecutionContextCreated(context);
226 int contextId = InspectedContext::contextId(context);
227 int groupId = contextGroupId(context);
228 contextCollected(groupId, contextId);
231 void V8InspectorImpl::contextCollected(
int groupId,
int contextId) {
232 m_contextIdToGroupIdMap.erase(contextId);
234 ConsoleStorageMap::iterator storageIt = m_consoleStorageMap.find(groupId);
235 if (storageIt != m_consoleStorageMap.end())
236 storageIt->second->contextDestroyed(contextId);
238 InspectedContext* inspectedContext = getContext(groupId, contextId);
239 if (!inspectedContext)
return;
241 forEachSession(groupId, [&inspectedContext](V8InspectorSessionImpl* session) {
242 session->runtimeAgent()->reportExecutionContextDestroyed(inspectedContext);
244 discardInspectedContext(groupId, contextId);
247 void V8InspectorImpl::resetContextGroup(
int contextGroupId) {
248 m_consoleStorageMap.erase(contextGroupId);
249 m_muteExceptionsMap.erase(contextGroupId);
250 std::vector<int> contextIdsToClear;
251 forEachContext(contextGroupId,
252 [&contextIdsToClear](InspectedContext* context) {
253 contextIdsToClear.push_back(context->contextId());
255 m_debugger->wasmTranslation()->Clear(m_isolate, contextIdsToClear);
256 forEachSession(contextGroupId,
257 [](V8InspectorSessionImpl* session) { session->reset(); });
258 m_contexts.erase(contextGroupId);
261 void V8InspectorImpl::idleStarted() { m_isolate->SetIdle(
true); }
263 void V8InspectorImpl::idleFinished() { m_isolate->SetIdle(
false); }
265 unsigned V8InspectorImpl::exceptionThrown(
268 const StringView& url,
unsigned lineNumber,
unsigned columnNumber,
269 std::unique_ptr<V8StackTrace> stackTrace,
int scriptId) {
270 int groupId = contextGroupId(context);
271 if (!groupId || m_muteExceptionsMap[groupId])
return 0;
272 std::unique_ptr<V8StackTraceImpl> stackTraceImpl(
273 static_cast<V8StackTraceImpl*>(stackTrace.release()));
274 unsigned exceptionId = nextExceptionId();
275 std::unique_ptr<V8ConsoleMessage> consoleMessage =
276 V8ConsoleMessage::createForException(
277 m_client->currentTimeMS(), toString16(detailedMessage),
278 toString16(url), lineNumber, columnNumber, std::move(stackTraceImpl),
279 scriptId, m_isolate, toString16(message),
280 InspectedContext::contextId(context), exception, exceptionId);
281 ensureConsoleMessageStorage(groupId)->addMessage(std::move(consoleMessage));
286 unsigned exceptionId,
287 const StringView& message) {
288 int groupId = contextGroupId(context);
289 if (!groupId)
return;
291 std::unique_ptr<V8ConsoleMessage> consoleMessage =
292 V8ConsoleMessage::createForRevokedException(
293 m_client->currentTimeMS(), toString16(message), exceptionId);
294 ensureConsoleMessageStorage(groupId)->addMessage(std::move(consoleMessage));
297 std::unique_ptr<V8StackTrace> V8InspectorImpl::captureStackTrace(
299 return m_debugger->captureStackTrace(fullStack);
302 V8StackTraceId V8InspectorImpl::storeCurrentStackTrace(
303 const StringView& description) {
304 return m_debugger->storeCurrentStackTrace(description);
307 void V8InspectorImpl::externalAsyncTaskStarted(
const V8StackTraceId& parent) {
308 m_debugger->externalAsyncTaskStarted(parent);
311 void V8InspectorImpl::externalAsyncTaskFinished(
const V8StackTraceId& parent) {
312 m_debugger->externalAsyncTaskFinished(parent);
315 void V8InspectorImpl::asyncTaskScheduled(
const StringView& taskName,
void* task,
318 m_debugger->asyncTaskScheduled(taskName, task, recurring);
321 void V8InspectorImpl::asyncTaskCanceled(
void* task) {
323 m_debugger->asyncTaskCanceled(task);
326 void V8InspectorImpl::asyncTaskStarted(
void* task) {
328 m_debugger->asyncTaskStarted(task);
331 void V8InspectorImpl::asyncTaskFinished(
void* task) {
333 m_debugger->asyncTaskFinished(task);
336 void V8InspectorImpl::allAsyncTasksCanceled() {
337 m_debugger->allAsyncTasksCanceled();
341 if (m_regexContext.IsEmpty())
342 m_regexContext.
Reset(m_isolate, v8::Context::New(m_isolate));
343 return m_regexContext.Get(m_isolate);
346 void V8InspectorImpl::discardInspectedContext(
int contextGroupId,
348 if (!getContext(contextGroupId, contextId))
return;
349 m_contexts[contextGroupId]->erase(contextId);
350 if (m_contexts[contextGroupId]->empty()) m_contexts.erase(contextGroupId);
353 V8InspectorSessionImpl* V8InspectorImpl::sessionById(
int contextGroupId,
355 auto it = m_sessions.find(contextGroupId);
356 if (it == m_sessions.end())
return nullptr;
357 auto it2 = it->second.find(sessionId);
358 return it2 == it->second.end() ? nullptr : it2->second;
361 V8Console* V8InspectorImpl::console() {
362 if (!m_console) m_console.reset(
new V8Console(
this));
363 return m_console.get();
366 void V8InspectorImpl::forEachContext(
368 const std::function<
void(InspectedContext*)>& callback) {
369 auto it = m_contexts.find(contextGroupId);
370 if (it == m_contexts.end())
return;
371 std::vector<int> ids;
372 ids.reserve(it->second->size());
373 for (
auto& contextIt : *(it->second)) ids.push_back(contextIt.first);
376 for (
auto& contextId : ids) {
377 it = m_contexts.find(contextGroupId);
378 if (it == m_contexts.end())
continue;
379 auto contextIt = it->second->find(contextId);
380 if (contextIt != it->second->end()) callback(contextIt->second.get());
384 void V8InspectorImpl::forEachSession(
386 const std::function<
void(V8InspectorSessionImpl*)>& callback) {
387 auto it = m_sessions.find(contextGroupId);
388 if (it == m_sessions.end())
return;
389 std::vector<int> ids;
390 ids.reserve(it->second.size());
391 for (
auto& sessionIt : it->second) ids.push_back(sessionIt.first);
394 for (
auto& sessionId : ids) {
395 it = m_sessions.find(contextGroupId);
396 if (it == m_sessions.end())
continue;
397 auto sessionIt = it->second.find(sessionId);
398 if (sessionIt != it->second.end()) callback(sessionIt->second);
402 V8InspectorImpl::EvaluateScope::EvaluateScope(v8::Isolate* isolate)
403 : m_isolate(isolate), m_safeForTerminationScope(isolate) {}
407 bool m_canceled =
false;
410 V8InspectorImpl::EvaluateScope::~EvaluateScope() {
412 v8::base::MutexGuard lock(&m_cancelToken->m_mutex);
413 m_cancelToken->m_canceled =
true;
414 m_isolate->CancelTerminateExecution();
420 TerminateTask(v8::Isolate* isolate, std::shared_ptr<CancelToken> token)
421 : m_isolate(isolate), m_token(std::move(token)) {}
423 void Run()
override {
426 v8::base::MutexGuard lock(&m_token->m_mutex);
427 if (m_token->m_canceled)
return;
428 m_isolate->TerminateExecution();
432 v8::Isolate* m_isolate;
433 std::shared_ptr<CancelToken> m_token;
436 protocol::Response V8InspectorImpl::EvaluateScope::setTimeout(
double timeout) {
437 if (m_isolate->IsExecutionTerminating()) {
438 return protocol::Response::Error(
"Execution was terminated");
440 m_cancelToken.reset(
new CancelToken());
442 v8::base::make_unique<TerminateTask>(m_isolate, m_cancelToken), timeout);
443 return protocol::Response::OK();
static V8_WARN_UNUSED_RESULT MaybeLocal< Script > Compile(Local< Context > context, Source *source, CompileOptions options=kNoCompileOptions, NoCacheReason no_cache_reason=kNoCacheNoReason)
Local< Script > BindToCurrentContext()
V8_INLINE bool IsEmpty() const
V8_INLINE T FromJust() const