V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
v8-inspector-impl.cc
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(
86  v8::Local<v8::UnboundScript> unboundScript;
87  if (!v8::debug::CompileInspectorScript(m_isolate, source)
88  .ToLocal(&unboundScript))
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  std::vector<int> contextIdsToClear;
251  forEachContext(contextGroupId,
252  [&contextIdsToClear](InspectedContext* context) {
253  contextIdsToClear.push_back(context->contextId());
254  });
255  m_debugger->wasmTranslation()->Clear(m_isolate, contextIdsToClear);
256  forEachSession(contextGroupId,
257  [](V8InspectorSessionImpl* session) { session->reset(); });
258  m_contexts.erase(contextGroupId);
259 }
260 
261 void V8InspectorImpl::idleStarted() { m_isolate->SetIdle(true); }
262 
263 void V8InspectorImpl::idleFinished() { m_isolate->SetIdle(false); }
264 
265 unsigned V8InspectorImpl::exceptionThrown(
266  v8::Local<v8::Context> context, const StringView& message,
267  v8::Local<v8::Value> exception, const StringView& detailedMessage,
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));
282  return exceptionId;
283 }
284 
285 void V8InspectorImpl::exceptionRevoked(v8::Local<v8::Context> context,
286  unsigned exceptionId,
287  const StringView& message) {
288  int groupId = contextGroupId(context);
289  if (!groupId) return;
290 
291  std::unique_ptr<V8ConsoleMessage> consoleMessage =
292  V8ConsoleMessage::createForRevokedException(
293  m_client->currentTimeMS(), toString16(message), exceptionId);
294  ensureConsoleMessageStorage(groupId)->addMessage(std::move(consoleMessage));
295 }
296 
297 std::unique_ptr<V8StackTrace> V8InspectorImpl::captureStackTrace(
298  bool fullStack) {
299  return m_debugger->captureStackTrace(fullStack);
300 }
301 
302 V8StackTraceId V8InspectorImpl::storeCurrentStackTrace(
303  const StringView& description) {
304  return m_debugger->storeCurrentStackTrace(description);
305 }
306 
307 void V8InspectorImpl::externalAsyncTaskStarted(const V8StackTraceId& parent) {
308  m_debugger->externalAsyncTaskStarted(parent);
309 }
310 
311 void V8InspectorImpl::externalAsyncTaskFinished(const V8StackTraceId& parent) {
312  m_debugger->externalAsyncTaskFinished(parent);
313 }
314 
315 void V8InspectorImpl::asyncTaskScheduled(const StringView& taskName, void* task,
316  bool recurring) {
317  if (!task) return;
318  m_debugger->asyncTaskScheduled(taskName, task, recurring);
319 }
320 
321 void V8InspectorImpl::asyncTaskCanceled(void* task) {
322  if (!task) return;
323  m_debugger->asyncTaskCanceled(task);
324 }
325 
326 void V8InspectorImpl::asyncTaskStarted(void* task) {
327  if (!task) return;
328  m_debugger->asyncTaskStarted(task);
329 }
330 
331 void V8InspectorImpl::asyncTaskFinished(void* task) {
332  if (!task) return;
333  m_debugger->asyncTaskFinished(task);
334 }
335 
336 void V8InspectorImpl::allAsyncTasksCanceled() {
337  m_debugger->allAsyncTasksCanceled();
338 }
339 
340 v8::Local<v8::Context> V8InspectorImpl::regexContext() {
341  if (m_regexContext.IsEmpty())
342  m_regexContext.Reset(m_isolate, v8::Context::New(m_isolate));
343  return m_regexContext.Get(m_isolate);
344 }
345 
346 void V8InspectorImpl::discardInspectedContext(int contextGroupId,
347  int contextId) {
348  if (!getContext(contextGroupId, contextId)) return;
349  m_contexts[contextGroupId]->erase(contextId);
350  if (m_contexts[contextGroupId]->empty()) m_contexts.erase(contextGroupId);
351 }
352 
353 V8InspectorSessionImpl* V8InspectorImpl::sessionById(int contextGroupId,
354  int sessionId) {
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;
359 }
360 
361 V8Console* V8InspectorImpl::console() {
362  if (!m_console) m_console.reset(new V8Console(this));
363  return m_console.get();
364 }
365 
366 void V8InspectorImpl::forEachContext(
367  int contextGroupId,
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);
374 
375  // Retrieve by ids each time since |callback| may destroy some contexts.
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());
381  }
382 }
383 
384 void V8InspectorImpl::forEachSession(
385  int contextGroupId,
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);
392 
393  // Retrieve by ids each time since |callback| may destroy some contexts.
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);
399  }
400 }
401 
402 V8InspectorImpl::EvaluateScope::EvaluateScope(v8::Isolate* isolate)
403  : m_isolate(isolate), m_safeForTerminationScope(isolate) {}
404 
406  v8::base::Mutex m_mutex;
407  bool m_canceled = false;
408 };
409 
410 V8InspectorImpl::EvaluateScope::~EvaluateScope() {
411  if (m_cancelToken) {
412  v8::base::MutexGuard lock(&m_cancelToken->m_mutex);
413  m_cancelToken->m_canceled = true;
414  m_isolate->CancelTerminateExecution();
415  }
416 }
417 
419  public:
420  TerminateTask(v8::Isolate* isolate, std::shared_ptr<CancelToken> token)
421  : m_isolate(isolate), m_token(std::move(token)) {}
422 
423  void Run() override {
424  // CancelToken contains m_canceled bool which may be changed from main
425  // thread, so lock mutex first.
426  v8::base::MutexGuard lock(&m_token->m_mutex);
427  if (m_token->m_canceled) return;
428  m_isolate->TerminateExecution();
429  }
430 
431  private:
432  v8::Isolate* m_isolate;
433  std::shared_ptr<CancelToken> m_token;
434 };
435 
436 protocol::Response V8InspectorImpl::EvaluateScope::setTimeout(double timeout) {
437  if (m_isolate->IsExecutionTerminating()) {
438  return protocol::Response::Error("Execution was terminated");
439  }
440  m_cancelToken.reset(new CancelToken());
441  v8::debug::GetCurrentPlatform()->CallDelayedOnWorkerThread(
442  v8::base::make_unique<TerminateTask>(m_isolate, m_cancelToken), timeout);
443  return protocol::Response::OK();
444 }
445 
446 } // namespace v8_inspector
static V8_WARN_UNUSED_RESULT MaybeLocal< Script > Compile(Local< Context > context, Source *source, CompileOptions options=kNoCompileOptions, NoCacheReason no_cache_reason=kNoCacheNoReason)
Definition: api.cc:2378
Local< Script > BindToCurrentContext()
Definition: api.cc:2030
Definition: v8.h:56
V8_INLINE bool IsEmpty() const
Definition: v8.h:195
V8_INLINE T FromJust() const
Definition: v8.h:8683
virtual void CallDelayedOnWorkerThread(std::unique_ptr< Task > task, double delay_in_seconds)=0
Definition: libplatform.h:13
V8_INLINE void Reset()
Definition: v8.h:9469