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