Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(79)

Side by Side Diff: third_party/WebKit/Source/platform/v8_inspector/V8DebuggerImpl.cpp

Issue 2199943004: [DevTools] Rename V8Debugger to V8Inspector. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "platform/v8_inspector/V8DebuggerImpl.h"
32
33 #include "platform/inspector_protocol/Values.h"
34 #include "platform/v8_inspector/Atomics.h"
35 #include "platform/v8_inspector/DebuggerScript.h"
36 #include "platform/v8_inspector/InspectedContext.h"
37 #include "platform/v8_inspector/ScriptBreakpoint.h"
38 #include "platform/v8_inspector/V8Compat.h"
39 #include "platform/v8_inspector/V8ConsoleAgentImpl.h"
40 #include "platform/v8_inspector/V8ConsoleMessage.h"
41 #include "platform/v8_inspector/V8DebuggerAgentImpl.h"
42 #include "platform/v8_inspector/V8InjectedScriptHost.h"
43 #include "platform/v8_inspector/V8InspectorSessionImpl.h"
44 #include "platform/v8_inspector/V8InternalValueType.h"
45 #include "platform/v8_inspector/V8RuntimeAgentImpl.h"
46 #include "platform/v8_inspector/V8StackTraceImpl.h"
47 #include "platform/v8_inspector/V8StringUtil.h"
48 #include "platform/v8_inspector/public/V8DebuggerClient.h"
49 #include <v8-profiler.h>
50
51 namespace blink {
52
53 namespace {
54 const char stepIntoV8MethodName[] = "stepIntoStatement";
55 const char stepOutV8MethodName[] = "stepOutOfFunction";
56 volatile int s_lastContextId = 0;
57 static const char v8AsyncTaskEventEnqueue[] = "enqueue";
58 static const char v8AsyncTaskEventWillHandle[] = "willHandle";
59 static const char v8AsyncTaskEventDidHandle[] = "didHandle";
60
61 inline v8::Local<v8::Boolean> v8Boolean(bool value, v8::Isolate* isolate)
62 {
63 return value ? v8::True(isolate) : v8::False(isolate);
64 }
65
66 }
67
68 static bool inLiveEditScope = false;
69
70 v8::MaybeLocal<v8::Value> V8DebuggerImpl::callDebuggerMethod(const char* functio nName, int argc, v8::Local<v8::Value> argv[])
71 {
72 v8::MicrotasksScope microtasks(m_isolate, v8::MicrotasksScope::kDoNotRunMicr otasks);
73 v8::Local<v8::Object> debuggerScript = m_debuggerScript.Get(m_isolate);
74 v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast(debuggerScr ipt->Get(v8InternalizedString(functionName)));
75 DCHECK(m_isolate->InContext());
76 return function->Call(m_isolate->GetCurrentContext(), debuggerScript, argc, argv);
77 }
78
79 std::unique_ptr<V8Debugger> V8Debugger::create(v8::Isolate* isolate, V8DebuggerC lient* client)
80 {
81 return wrapUnique(new V8DebuggerImpl(isolate, client));
82 }
83
84 V8DebuggerImpl::V8DebuggerImpl(v8::Isolate* isolate, V8DebuggerClient* client)
85 : m_isolate(isolate)
86 , m_client(client)
87 , m_capturingStackTracesCount(0)
88 , m_lastExceptionId(0)
89 , m_enabledAgentsCount(0)
90 , m_breakpointsActivated(true)
91 , m_runningNestedMessageLoop(false)
92 , m_maxAsyncCallStackDepth(0)
93 {
94 }
95
96 V8DebuggerImpl::~V8DebuggerImpl()
97 {
98 }
99
100 void V8DebuggerImpl::enable()
101 {
102 DCHECK(!enabled());
103 v8::HandleScope scope(m_isolate);
104 v8::Debug::SetDebugEventListener(m_isolate, &V8DebuggerImpl::v8DebugEventCal lback, v8::External::New(m_isolate, this));
105 m_debuggerContext.Reset(m_isolate, v8::Debug::GetDebugContext(m_isolate));
106 compileDebuggerScript();
107 }
108
109 void V8DebuggerImpl::disable()
110 {
111 DCHECK(enabled());
112 clearBreakpoints();
113 m_debuggerScript.Reset();
114 m_debuggerContext.Reset();
115 allAsyncTasksCanceled();
116 v8::Debug::SetDebugEventListener(m_isolate, nullptr);
117 }
118
119 bool V8DebuggerImpl::enabled() const
120 {
121 return !m_debuggerScript.IsEmpty();
122 }
123
124 // static
125 int V8DebuggerImpl::contextId(v8::Local<v8::Context> context)
126 {
127 v8::Local<v8::Value> data = context->GetEmbedderData(static_cast<int>(v8::Co ntext::kDebugIdIndex));
128 if (data.IsEmpty() || !data->IsString())
129 return 0;
130 String16 dataString = toProtocolString(data.As<v8::String>());
131 if (dataString.isEmpty())
132 return 0;
133 size_t commaPos = dataString.find(",");
134 if (commaPos == kNotFound)
135 return 0;
136 size_t commaPos2 = dataString.find(",", commaPos + 1);
137 if (commaPos2 == kNotFound)
138 return 0;
139 return dataString.substring(commaPos + 1, commaPos2 - commaPos - 1).toInt();
140 }
141
142 // static
143 int V8DebuggerImpl::getGroupId(v8::Local<v8::Context> context)
144 {
145 v8::Local<v8::Value> data = context->GetEmbedderData(static_cast<int>(v8::Co ntext::kDebugIdIndex));
146 if (data.IsEmpty() || !data->IsString())
147 return 0;
148 String16 dataString = toProtocolString(data.As<v8::String>());
149 if (dataString.isEmpty())
150 return 0;
151 size_t commaPos = dataString.find(",");
152 if (commaPos == kNotFound)
153 return 0;
154 return dataString.substring(0, commaPos).toInt();
155 }
156
157 void V8DebuggerImpl::debuggerAgentEnabled()
158 {
159 if (!m_enabledAgentsCount++)
160 enable();
161 }
162
163 void V8DebuggerImpl::debuggerAgentDisabled()
164 {
165 if (!--m_enabledAgentsCount)
166 disable();
167 }
168
169 V8DebuggerAgentImpl* V8DebuggerImpl::findEnabledDebuggerAgent(int contextGroupId )
170 {
171 if (!contextGroupId)
172 return nullptr;
173 SessionMap::iterator it = m_sessions.find(contextGroupId);
174 if (it == m_sessions.end())
175 return nullptr;
176 V8DebuggerAgentImpl* agent = it->second->debuggerAgent();
177 if (!agent->enabled())
178 return nullptr;
179 return agent;
180 }
181
182 V8DebuggerAgentImpl* V8DebuggerImpl::findEnabledDebuggerAgent(v8::Local<v8::Cont ext> context)
183 {
184 return findEnabledDebuggerAgent(getGroupId(context));
185 }
186
187 void V8DebuggerImpl::getCompiledScripts(int contextGroupId, std::vector<std::uni que_ptr<V8DebuggerScript>>& result)
188 {
189 v8::HandleScope scope(m_isolate);
190 v8::MicrotasksScope microtasks(m_isolate, v8::MicrotasksScope::kDoNotRunMicr otasks);
191 v8::Local<v8::Object> debuggerScript = m_debuggerScript.Get(m_isolate);
192 DCHECK(!debuggerScript->IsUndefined());
193 v8::Local<v8::Function> getScriptsFunction = v8::Local<v8::Function>::Cast(d ebuggerScript->Get(v8InternalizedString("getScripts")));
194 v8::Local<v8::Value> argv[] = { v8::Integer::New(m_isolate, contextGroupId) };
195 v8::Local<v8::Value> value;
196 if (!getScriptsFunction->Call(debuggerContext(), debuggerScript, PROTOCOL_AR RAY_LENGTH(argv), argv).ToLocal(&value))
197 return;
198 DCHECK(value->IsArray());
199 v8::Local<v8::Array> scriptsArray = v8::Local<v8::Array>::Cast(value);
200 result.reserve(scriptsArray->Length());
201 for (unsigned i = 0; i < scriptsArray->Length(); ++i) {
202 v8::Local<v8::Object> scriptObject = v8::Local<v8::Object>::Cast(scripts Array->Get(v8::Integer::New(m_isolate, i)));
203 result.push_back(wrapUnique(new V8DebuggerScript(m_isolate, scriptObject , inLiveEditScope)));
204 }
205 }
206
207 String16 V8DebuggerImpl::setBreakpoint(const String16& sourceID, const ScriptBre akpoint& scriptBreakpoint, int* actualLineNumber, int* actualColumnNumber, bool interstatementLocation)
208 {
209 v8::HandleScope scope(m_isolate);
210 v8::Context::Scope contextScope(debuggerContext());
211
212 v8::Local<v8::Object> info = v8::Object::New(m_isolate);
213 info->Set(v8InternalizedString("sourceID"), toV8String(m_isolate, sourceID)) ;
214 info->Set(v8InternalizedString("lineNumber"), v8::Integer::New(m_isolate, sc riptBreakpoint.lineNumber));
215 info->Set(v8InternalizedString("columnNumber"), v8::Integer::New(m_isolate, scriptBreakpoint.columnNumber));
216 info->Set(v8InternalizedString("interstatementLocation"), v8Boolean(intersta tementLocation, m_isolate));
217 info->Set(v8InternalizedString("condition"), toV8String(m_isolate, scriptBre akpoint.condition));
218
219 v8::Local<v8::Function> setBreakpointFunction = v8::Local<v8::Function>::Cas t(m_debuggerScript.Get(m_isolate)->Get(v8InternalizedString("setBreakpoint")));
220 v8::Local<v8::Value> breakpointId = v8::Debug::Call(debuggerContext(), setBr eakpointFunction, info).ToLocalChecked();
221 if (!breakpointId->IsString())
222 return "";
223 *actualLineNumber = info->Get(v8InternalizedString("lineNumber"))->Int32Valu e();
224 *actualColumnNumber = info->Get(v8InternalizedString("columnNumber"))->Int32 Value();
225 return toProtocolString(breakpointId.As<v8::String>());
226 }
227
228 void V8DebuggerImpl::removeBreakpoint(const String16& breakpointId)
229 {
230 v8::HandleScope scope(m_isolate);
231 v8::Context::Scope contextScope(debuggerContext());
232
233 v8::Local<v8::Object> info = v8::Object::New(m_isolate);
234 info->Set(v8InternalizedString("breakpointId"), toV8String(m_isolate, breakp ointId));
235
236 v8::Local<v8::Function> removeBreakpointFunction = v8::Local<v8::Function>:: Cast(m_debuggerScript.Get(m_isolate)->Get(v8InternalizedString("removeBreakpoint ")));
237 v8::Debug::Call(debuggerContext(), removeBreakpointFunction, info).ToLocalCh ecked();
238 }
239
240 void V8DebuggerImpl::clearBreakpoints()
241 {
242 v8::HandleScope scope(m_isolate);
243 v8::Context::Scope contextScope(debuggerContext());
244
245 v8::Local<v8::Function> clearBreakpoints = v8::Local<v8::Function>::Cast(m_d ebuggerScript.Get(m_isolate)->Get(v8InternalizedString("clearBreakpoints")));
246 v8::Debug::Call(debuggerContext(), clearBreakpoints).ToLocalChecked();
247 }
248
249 void V8DebuggerImpl::setBreakpointsActivated(bool activated)
250 {
251 if (!enabled()) {
252 NOTREACHED();
253 return;
254 }
255 v8::HandleScope scope(m_isolate);
256 v8::Context::Scope contextScope(debuggerContext());
257
258 v8::Local<v8::Object> info = v8::Object::New(m_isolate);
259 info->Set(v8InternalizedString("enabled"), v8::Boolean::New(m_isolate, activ ated));
260 v8::Local<v8::Function> setBreakpointsActivated = v8::Local<v8::Function>::C ast(m_debuggerScript.Get(m_isolate)->Get(v8InternalizedString("setBreakpointsAct ivated")));
261 v8::Debug::Call(debuggerContext(), setBreakpointsActivated, info).ToLocalChe cked();
262
263 m_breakpointsActivated = activated;
264 }
265
266 V8DebuggerImpl::PauseOnExceptionsState V8DebuggerImpl::getPauseOnExceptionsState ()
267 {
268 DCHECK(enabled());
269 v8::HandleScope scope(m_isolate);
270 v8::Context::Scope contextScope(debuggerContext());
271
272 v8::Local<v8::Value> argv[] = { v8::Undefined(m_isolate) };
273 v8::Local<v8::Value> result = callDebuggerMethod("pauseOnExceptionsState", 0 , argv).ToLocalChecked();
274 return static_cast<V8DebuggerImpl::PauseOnExceptionsState>(result->Int32Valu e());
275 }
276
277 void V8DebuggerImpl::setPauseOnExceptionsState(PauseOnExceptionsState pauseOnExc eptionsState)
278 {
279 DCHECK(enabled());
280 v8::HandleScope scope(m_isolate);
281 v8::Context::Scope contextScope(debuggerContext());
282
283 v8::Local<v8::Value> argv[] = { v8::Int32::New(m_isolate, pauseOnExceptionsS tate) };
284 callDebuggerMethod("setPauseOnExceptionsState", 1, argv);
285 }
286
287 void V8DebuggerImpl::setPauseOnNextStatement(bool pause)
288 {
289 if (m_runningNestedMessageLoop)
290 return;
291 if (pause)
292 v8::Debug::DebugBreak(m_isolate);
293 else
294 v8::Debug::CancelDebugBreak(m_isolate);
295 }
296
297 bool V8DebuggerImpl::canBreakProgram()
298 {
299 if (!m_breakpointsActivated)
300 return false;
301 return m_isolate->InContext();
302 }
303
304 void V8DebuggerImpl::breakProgram()
305 {
306 if (isPaused()) {
307 DCHECK(!m_runningNestedMessageLoop);
308 v8::Local<v8::Value> exception;
309 v8::Local<v8::Array> hitBreakpoints;
310 handleProgramBreak(m_pausedContext, m_executionState, exception, hitBrea kpoints);
311 return;
312 }
313
314 if (!canBreakProgram())
315 return;
316
317 v8::HandleScope scope(m_isolate);
318 v8::Local<v8::Function> breakFunction;
319 if (!v8::Function::New(m_isolate->GetCurrentContext(), &V8DebuggerImpl::brea kProgramCallback, v8::External::New(m_isolate, this), 0, v8::ConstructorBehavior ::kThrow).ToLocal(&breakFunction))
320 return;
321 v8::Debug::Call(debuggerContext(), breakFunction).ToLocalChecked();
322 }
323
324 void V8DebuggerImpl::continueProgram()
325 {
326 if (isPaused())
327 m_client->quitMessageLoopOnPause();
328 m_pausedContext.Clear();
329 m_executionState.Clear();
330 }
331
332 void V8DebuggerImpl::stepIntoStatement()
333 {
334 DCHECK(isPaused());
335 DCHECK(!m_executionState.IsEmpty());
336 v8::HandleScope handleScope(m_isolate);
337 v8::Local<v8::Value> argv[] = { m_executionState };
338 callDebuggerMethod(stepIntoV8MethodName, 1, argv);
339 continueProgram();
340 }
341
342 void V8DebuggerImpl::stepOverStatement()
343 {
344 DCHECK(isPaused());
345 DCHECK(!m_executionState.IsEmpty());
346 v8::HandleScope handleScope(m_isolate);
347 v8::Local<v8::Value> argv[] = { m_executionState };
348 callDebuggerMethod("stepOverStatement", 1, argv);
349 continueProgram();
350 }
351
352 void V8DebuggerImpl::stepOutOfFunction()
353 {
354 DCHECK(isPaused());
355 DCHECK(!m_executionState.IsEmpty());
356 v8::HandleScope handleScope(m_isolate);
357 v8::Local<v8::Value> argv[] = { m_executionState };
358 callDebuggerMethod(stepOutV8MethodName, 1, argv);
359 continueProgram();
360 }
361
362 void V8DebuggerImpl::clearStepping()
363 {
364 DCHECK(enabled());
365 v8::HandleScope scope(m_isolate);
366 v8::Context::Scope contextScope(debuggerContext());
367
368 v8::Local<v8::Value> argv[] = { v8::Undefined(m_isolate) };
369 callDebuggerMethod("clearStepping", 0, argv);
370 }
371
372 bool V8DebuggerImpl::setScriptSource(const String16& sourceID, v8::Local<v8::Str ing> newSource, bool preview, ErrorString* error, Maybe<protocol::Runtime::Excep tionDetails>* exceptionDetails, JavaScriptCallFrames* newCallFrames, Maybe<bool> * stackChanged)
373 {
374 class EnableLiveEditScope {
375 public:
376 explicit EnableLiveEditScope(v8::Isolate* isolate) : m_isolate(isolate)
377 {
378 v8::Debug::SetLiveEditEnabled(m_isolate, true);
379 inLiveEditScope = true;
380 }
381 ~EnableLiveEditScope()
382 {
383 v8::Debug::SetLiveEditEnabled(m_isolate, false);
384 inLiveEditScope = false;
385 }
386 private:
387 v8::Isolate* m_isolate;
388 };
389
390 DCHECK(enabled());
391 v8::HandleScope scope(m_isolate);
392
393 std::unique_ptr<v8::Context::Scope> contextScope;
394 if (!isPaused())
395 contextScope = wrapUnique(new v8::Context::Scope(debuggerContext()));
396
397 v8::Local<v8::Value> argv[] = { toV8String(m_isolate, sourceID), newSource, v8Boolean(preview, m_isolate) };
398
399 v8::Local<v8::Value> v8result;
400 {
401 EnableLiveEditScope enableLiveEditScope(m_isolate);
402 v8::TryCatch tryCatch(m_isolate);
403 tryCatch.SetVerbose(false);
404 v8::MaybeLocal<v8::Value> maybeResult = callDebuggerMethod("liveEditScri ptSource", 3, argv);
405 if (tryCatch.HasCaught()) {
406 v8::Local<v8::Message> message = tryCatch.Message();
407 if (!message.IsEmpty())
408 *error = toProtocolStringWithTypeCheck(message->Get());
409 else
410 *error = "Unknown error.";
411 return false;
412 }
413 v8result = maybeResult.ToLocalChecked();
414 }
415 DCHECK(!v8result.IsEmpty());
416 v8::Local<v8::Object> resultTuple = v8result->ToObject(m_isolate);
417 int code = static_cast<int>(resultTuple->Get(0)->ToInteger(m_isolate)->Value ());
418 switch (code) {
419 case 0:
420 {
421 *stackChanged = resultTuple->Get(1)->BooleanValue();
422 // Call stack may have changed after if the edited function was on t he stack.
423 if (!preview && isPaused()) {
424 JavaScriptCallFrames frames = currentCallFrames();
425 newCallFrames->swap(frames);
426 }
427 return true;
428 }
429 // Compile error.
430 case 1:
431 {
432 *exceptionDetails = protocol::Runtime::ExceptionDetails::create()
433 .setText(toProtocolStringWithTypeCheck(resultTuple->Get(2)))
434 .setScriptId(String16("0"))
435 .setLineNumber(resultTuple->Get(3)->ToInteger(m_isolate)->Value( ) - 1)
436 .setColumnNumber(resultTuple->Get(4)->ToInteger(m_isolate)->Valu e() - 1).build();
437 return false;
438 }
439 }
440 *error = "Unknown error.";
441 return false;
442 }
443
444 JavaScriptCallFrames V8DebuggerImpl::currentCallFrames(int limit)
445 {
446 if (!m_isolate->InContext())
447 return JavaScriptCallFrames();
448 v8::Local<v8::Value> currentCallFramesV8;
449 if (m_executionState.IsEmpty()) {
450 v8::Local<v8::Function> currentCallFramesFunction = v8::Local<v8::Functi on>::Cast(m_debuggerScript.Get(m_isolate)->Get(v8InternalizedString("currentCall Frames")));
451 currentCallFramesV8 = v8::Debug::Call(debuggerContext(), currentCallFram esFunction, v8::Integer::New(m_isolate, limit)).ToLocalChecked();
452 } else {
453 v8::Local<v8::Value> argv[] = { m_executionState, v8::Integer::New(m_iso late, limit) };
454 currentCallFramesV8 = callDebuggerMethod("currentCallFrames", PROTOCOL_A RRAY_LENGTH(argv), argv).ToLocalChecked();
455 }
456 DCHECK(!currentCallFramesV8.IsEmpty());
457 if (!currentCallFramesV8->IsArray())
458 return JavaScriptCallFrames();
459 v8::Local<v8::Array> callFramesArray = currentCallFramesV8.As<v8::Array>();
460 JavaScriptCallFrames callFrames;
461 for (size_t i = 0; i < callFramesArray->Length(); ++i) {
462 v8::Local<v8::Value> callFrameValue;
463 if (!callFramesArray->Get(debuggerContext(), i).ToLocal(&callFrameValue) )
464 return JavaScriptCallFrames();
465 if (!callFrameValue->IsObject())
466 return JavaScriptCallFrames();
467 v8::Local<v8::Object> callFrameObject = callFrameValue.As<v8::Object>();
468 callFrames.push_back(JavaScriptCallFrame::create(debuggerContext(), v8:: Local<v8::Object>::Cast(callFrameObject)));
469 }
470 return callFrames;
471 }
472
473 static V8DebuggerImpl* toV8DebuggerImpl(v8::Local<v8::Value> data)
474 {
475 void* p = v8::Local<v8::External>::Cast(data)->Value();
476 return static_cast<V8DebuggerImpl*>(p);
477 }
478
479 void V8DebuggerImpl::breakProgramCallback(const v8::FunctionCallbackInfo<v8::Val ue>& info)
480 {
481 DCHECK_EQ(info.Length(), 2);
482 V8DebuggerImpl* thisPtr = toV8DebuggerImpl(info.Data());
483 if (!thisPtr->enabled())
484 return;
485 v8::Local<v8::Context> pausedContext = thisPtr->m_isolate->GetCurrentContext ();
486 v8::Local<v8::Value> exception;
487 v8::Local<v8::Array> hitBreakpoints;
488 thisPtr->handleProgramBreak(pausedContext, v8::Local<v8::Object>::Cast(info[ 0]), exception, hitBreakpoints);
489 }
490
491 void V8DebuggerImpl::handleProgramBreak(v8::Local<v8::Context> pausedContext, v8 ::Local<v8::Object> executionState, v8::Local<v8::Value> exception, v8::Local<v8 ::Array> hitBreakpointNumbers, bool isPromiseRejection)
492 {
493 // Don't allow nested breaks.
494 if (m_runningNestedMessageLoop)
495 return;
496
497 V8DebuggerAgentImpl* agent = findEnabledDebuggerAgent(pausedContext);
498 if (!agent)
499 return;
500
501 std::vector<String16> breakpointIds;
502 if (!hitBreakpointNumbers.IsEmpty()) {
503 breakpointIds.reserve(hitBreakpointNumbers->Length());
504 for (size_t i = 0; i < hitBreakpointNumbers->Length(); i++) {
505 v8::Local<v8::Value> hitBreakpointNumber = hitBreakpointNumbers->Get (i);
506 DCHECK(!hitBreakpointNumber.IsEmpty() && hitBreakpointNumber->IsInt3 2());
507 breakpointIds.push_back(String16::fromInteger(hitBreakpointNumber->I nt32Value()));
508 }
509 }
510
511 m_pausedContext = pausedContext;
512 m_executionState = executionState;
513 V8DebuggerAgentImpl::SkipPauseRequest result = agent->didPause(pausedContext , exception, breakpointIds, isPromiseRejection);
514 if (result == V8DebuggerAgentImpl::RequestNoSkip) {
515 m_runningNestedMessageLoop = true;
516 int groupId = getGroupId(pausedContext);
517 DCHECK(groupId);
518 m_client->runMessageLoopOnPause(groupId);
519 // The agent may have been removed in the nested loop.
520 agent = findEnabledDebuggerAgent(pausedContext);
521 if (agent)
522 agent->didContinue();
523 m_runningNestedMessageLoop = false;
524 }
525 m_pausedContext.Clear();
526 m_executionState.Clear();
527
528 if (result == V8DebuggerAgentImpl::RequestStepFrame) {
529 v8::Local<v8::Value> argv[] = { executionState };
530 callDebuggerMethod("stepFrameStatement", 1, argv);
531 } else if (result == V8DebuggerAgentImpl::RequestStepInto) {
532 v8::Local<v8::Value> argv[] = { executionState };
533 callDebuggerMethod(stepIntoV8MethodName, 1, argv);
534 } else if (result == V8DebuggerAgentImpl::RequestStepOut) {
535 v8::Local<v8::Value> argv[] = { executionState };
536 callDebuggerMethod(stepOutV8MethodName, 1, argv);
537 }
538 }
539
540 void V8DebuggerImpl::v8DebugEventCallback(const v8::Debug::EventDetails& eventDe tails)
541 {
542 V8DebuggerImpl* thisPtr = toV8DebuggerImpl(eventDetails.GetCallbackData());
543 thisPtr->handleV8DebugEvent(eventDetails);
544 }
545
546 v8::Local<v8::Value> V8DebuggerImpl::callInternalGetterFunction(v8::Local<v8::Ob ject> object, const char* functionName)
547 {
548 v8::MicrotasksScope microtasks(m_isolate, v8::MicrotasksScope::kDoNotRunMicr otasks);
549 v8::Local<v8::Value> getterValue = object->Get(v8InternalizedString(function Name));
550 DCHECK(!getterValue.IsEmpty() && getterValue->IsFunction());
551 return v8::Local<v8::Function>::Cast(getterValue)->Call(m_isolate->GetCurren tContext(), object, 0, 0).ToLocalChecked();
552 }
553
554 void V8DebuggerImpl::handleV8DebugEvent(const v8::Debug::EventDetails& eventDeta ils)
555 {
556 if (!enabled())
557 return;
558 v8::DebugEvent event = eventDetails.GetEvent();
559 if (event != v8::AsyncTaskEvent && event != v8::Break && event != v8::Except ion && event != v8::AfterCompile && event != v8::BeforeCompile && event != v8::C ompileError)
560 return;
561
562 v8::Local<v8::Context> eventContext = eventDetails.GetEventContext();
563 DCHECK(!eventContext.IsEmpty());
564
565 if (event == v8::AsyncTaskEvent) {
566 v8::HandleScope scope(m_isolate);
567 handleV8AsyncTaskEvent(eventContext, eventDetails.GetExecutionState(), e ventDetails.GetEventData());
568 return;
569 }
570
571 V8DebuggerAgentImpl* agent = findEnabledDebuggerAgent(eventContext);
572 if (agent) {
573 v8::HandleScope scope(m_isolate);
574 if (event == v8::AfterCompile || event == v8::CompileError) {
575 v8::Context::Scope contextScope(debuggerContext());
576 v8::Local<v8::Value> argv[] = { eventDetails.GetEventData() };
577 v8::Local<v8::Value> value = callDebuggerMethod("getAfterCompileScri pt", 1, argv).ToLocalChecked();
578 DCHECK(value->IsObject());
579 v8::Local<v8::Object> scriptObject = v8::Local<v8::Object>::Cast(val ue);
580 agent->didParseSource(wrapUnique(new V8DebuggerScript(m_isolate, scr iptObject, inLiveEditScope)), event == v8::AfterCompile);
581 } else if (event == v8::Exception) {
582 v8::Local<v8::Object> eventData = eventDetails.GetEventData();
583 v8::Local<v8::Value> exception = callInternalGetterFunction(eventDat a, "exception");
584 v8::Local<v8::Value> promise = callInternalGetterFunction(eventData, "promise");
585 bool isPromiseRejection = !promise.IsEmpty() && promise->IsObject();
586 handleProgramBreak(eventContext, eventDetails.GetExecutionState(), e xception, v8::Local<v8::Array>(), isPromiseRejection);
587 } else if (event == v8::Break) {
588 v8::Local<v8::Value> argv[] = { eventDetails.GetEventData() };
589 v8::Local<v8::Value> hitBreakpoints = callDebuggerMethod("getBreakpo intNumbers", 1, argv).ToLocalChecked();
590 DCHECK(hitBreakpoints->IsArray());
591 handleProgramBreak(eventContext, eventDetails.GetExecutionState(), v 8::Local<v8::Value>(), hitBreakpoints.As<v8::Array>());
592 }
593 }
594 }
595
596 void V8DebuggerImpl::handleV8AsyncTaskEvent(v8::Local<v8::Context> context, v8:: Local<v8::Object> executionState, v8::Local<v8::Object> eventData)
597 {
598 if (!m_maxAsyncCallStackDepth)
599 return;
600
601 String16 type = toProtocolStringWithTypeCheck(callInternalGetterFunction(eve ntData, "type"));
602 String16 name = toProtocolStringWithTypeCheck(callInternalGetterFunction(eve ntData, "name"));
603 int id = callInternalGetterFunction(eventData, "id")->ToInteger(m_isolate)-> Value();
604 // The scopes for the ids are defined by the eventData.name namespaces. Ther e are currently two namespaces: "Object." and "Promise.".
605 void* ptr = reinterpret_cast<void*>(id * 4 + (name[0] == 'P' ? 2 : 0) + 1);
606 if (type == v8AsyncTaskEventEnqueue)
607 asyncTaskScheduled(name, ptr, false);
608 else if (type == v8AsyncTaskEventWillHandle)
609 asyncTaskStarted(ptr);
610 else if (type == v8AsyncTaskEventDidHandle)
611 asyncTaskFinished(ptr);
612 else
613 NOTREACHED();
614 }
615
616 V8StackTraceImpl* V8DebuggerImpl::currentAsyncCallChain()
617 {
618 if (!m_currentStacks.size())
619 return nullptr;
620 return m_currentStacks.back().get();
621 }
622
623 void V8DebuggerImpl::compileDebuggerScript()
624 {
625 if (!m_debuggerScript.IsEmpty()) {
626 NOTREACHED();
627 return;
628 }
629
630 v8::HandleScope scope(m_isolate);
631 v8::Context::Scope contextScope(debuggerContext());
632
633 v8::Local<v8::String> scriptValue = v8::String::NewFromUtf8(m_isolate, Debug gerScript_js, v8::NewStringType::kInternalized, sizeof(DebuggerScript_js)).ToLoc alChecked();
634 v8::Local<v8::Value> value;
635 if (!compileAndRunInternalScript(debuggerContext(), scriptValue).ToLocal(&va lue)) {
636 NOTREACHED();
637 return;
638 }
639 DCHECK(value->IsObject());
640 m_debuggerScript.Reset(m_isolate, value.As<v8::Object>());
641 }
642
643 v8::Local<v8::Context> V8DebuggerImpl::debuggerContext() const
644 {
645 DCHECK(!m_debuggerContext.IsEmpty());
646 return m_debuggerContext.Get(m_isolate);
647 }
648
649 v8::Local<v8::String> V8DebuggerImpl::v8InternalizedString(const char* str) cons t
650 {
651 return v8::String::NewFromUtf8(m_isolate, str, v8::NewStringType::kInternali zed).ToLocalChecked();
652 }
653
654 v8::MaybeLocal<v8::Value> V8DebuggerImpl::functionScopes(v8::Local<v8::Function> function)
655 {
656 if (!enabled()) {
657 NOTREACHED();
658 return v8::Local<v8::Value>::New(m_isolate, v8::Undefined(m_isolate));
659 }
660 v8::Local<v8::Value> argv[] = { function };
661 v8::Local<v8::Value> scopesValue;
662 if (!callDebuggerMethod("getFunctionScopes", 1, argv).ToLocal(&scopesValue) || !scopesValue->IsArray())
663 return v8::MaybeLocal<v8::Value>();
664 v8::Local<v8::Array> scopes = scopesValue.As<v8::Array>();
665 v8::Local<v8::Context> context = m_debuggerContext.Get(m_isolate);
666 if (!markAsInternal(context, scopes, V8InternalValueType::kScopeList))
667 return v8::MaybeLocal<v8::Value>();
668 if (!markArrayEntriesAsInternal(context, scopes, V8InternalValueType::kScope ))
669 return v8::MaybeLocal<v8::Value>();
670 if (!scopes->SetPrototype(context, v8::Null(m_isolate)).FromMaybe(false))
671 return v8::Undefined(m_isolate);
672 return scopes;
673 }
674
675 v8::MaybeLocal<v8::Array> V8DebuggerImpl::internalProperties(v8::Local<v8::Conte xt> context, v8::Local<v8::Value> value)
676 {
677 v8::Local<v8::Array> properties;
678 if (!v8::Debug::GetInternalProperties(m_isolate, value).ToLocal(&properties) )
679 return v8::MaybeLocal<v8::Array>();
680 if (value->IsFunction()) {
681 v8::Local<v8::Function> function = value.As<v8::Function>();
682 v8::Local<v8::Value> location = functionLocation(context, function);
683 if (location->IsObject()) {
684 properties->Set(properties->Length(), v8InternalizedString("[[Functi onLocation]]"));
685 properties->Set(properties->Length(), location);
686 }
687 if (function->IsGeneratorFunction()) {
688 properties->Set(properties->Length(), v8InternalizedString("[[IsGene rator]]"));
689 properties->Set(properties->Length(), v8::True(m_isolate));
690 }
691 }
692 if (!enabled())
693 return properties;
694 if (value->IsMap() || value->IsWeakMap() || value->IsSet() || value->IsWeakS et() || value->IsSetIterator() || value->IsMapIterator()) {
695 v8::Local<v8::Value> entries = collectionEntries(context, v8::Local<v8:: Object>::Cast(value));
696 if (entries->IsArray()) {
697 properties->Set(properties->Length(), v8InternalizedString("[[Entrie s]]"));
698 properties->Set(properties->Length(), entries);
699 }
700 }
701 if (value->IsGeneratorObject()) {
702 v8::Local<v8::Value> location = generatorObjectLocation(v8::Local<v8::Ob ject>::Cast(value));
703 if (location->IsObject()) {
704 properties->Set(properties->Length(), v8InternalizedString("[[Genera torLocation]]"));
705 properties->Set(properties->Length(), location);
706 }
707 }
708 if (value->IsFunction()) {
709 v8::Local<v8::Function> function = value.As<v8::Function>();
710 v8::Local<v8::Value> boundFunction = function->GetBoundFunction();
711 v8::Local<v8::Value> scopes;
712 if (boundFunction->IsUndefined() && functionScopes(function).ToLocal(&sc opes)) {
713 properties->Set(properties->Length(), v8InternalizedString("[[Scopes ]]"));
714 properties->Set(properties->Length(), scopes);
715 }
716 }
717 return properties;
718 }
719
720 v8::Local<v8::Value> V8DebuggerImpl::collectionEntries(v8::Local<v8::Context> co ntext, v8::Local<v8::Object> object)
721 {
722 if (!enabled()) {
723 NOTREACHED();
724 return v8::Undefined(m_isolate);
725 }
726 v8::Local<v8::Value> argv[] = { object };
727 v8::Local<v8::Value> entriesValue = callDebuggerMethod("getCollectionEntries ", 1, argv).ToLocalChecked();
728 if (!entriesValue->IsArray())
729 return v8::Undefined(m_isolate);
730 v8::Local<v8::Array> entries = entriesValue.As<v8::Array>();
731 if (!markArrayEntriesAsInternal(context, entries, V8InternalValueType::kEntr y))
732 return v8::Undefined(m_isolate);
733 if (!entries->SetPrototype(context, v8::Null(m_isolate)).FromMaybe(false))
734 return v8::Undefined(m_isolate);
735 return entries;
736 }
737
738 v8::Local<v8::Value> V8DebuggerImpl::generatorObjectLocation(v8::Local<v8::Objec t> object)
739 {
740 if (!enabled()) {
741 NOTREACHED();
742 return v8::Null(m_isolate);
743 }
744 v8::Local<v8::Value> argv[] = { object };
745 v8::Local<v8::Value> location = callDebuggerMethod("getGeneratorObjectLocati on", 1, argv).ToLocalChecked();
746 if (!location->IsObject())
747 return v8::Null(m_isolate);
748 v8::Local<v8::Context> context = m_debuggerContext.Get(m_isolate);
749 if (!markAsInternal(context, v8::Local<v8::Object>::Cast(location), V8Intern alValueType::kLocation))
750 return v8::Null(m_isolate);
751 return location;
752 }
753
754 v8::Local<v8::Value> V8DebuggerImpl::functionLocation(v8::Local<v8::Context> con text, v8::Local<v8::Function> function)
755 {
756 int scriptId = function->ScriptId();
757 if (scriptId == v8::UnboundScript::kNoScriptId)
758 return v8::Null(m_isolate);
759 int lineNumber = function->GetScriptLineNumber();
760 int columnNumber = function->GetScriptColumnNumber();
761 if (lineNumber == v8::Function::kLineOffsetNotFound || columnNumber == v8::F unction::kLineOffsetNotFound)
762 return v8::Null(m_isolate);
763 v8::Local<v8::Object> location = v8::Object::New(m_isolate);
764 if (!location->Set(context, v8InternalizedString("scriptId"), toV8String(m_i solate, String16::fromInteger(scriptId))).FromMaybe(false))
765 return v8::Null(m_isolate);
766 if (!location->Set(context, v8InternalizedString("lineNumber"), v8::Integer: :New(m_isolate, lineNumber)).FromMaybe(false))
767 return v8::Null(m_isolate);
768 if (!location->Set(context, v8InternalizedString("columnNumber"), v8::Intege r::New(m_isolate, columnNumber)).FromMaybe(false))
769 return v8::Null(m_isolate);
770 if (!markAsInternal(context, location, V8InternalValueType::kLocation))
771 return v8::Null(m_isolate);
772 return location;
773 }
774
775 bool V8DebuggerImpl::isPaused()
776 {
777 return !m_pausedContext.IsEmpty();
778 }
779
780 v8::MaybeLocal<v8::Value> V8DebuggerImpl::runCompiledScript(v8::Local<v8::Contex t> context, v8::Local<v8::Script> script)
781 {
782 // TODO(dgozman): get rid of this check.
783 if (!m_client->isExecutionAllowed())
784 return v8::MaybeLocal<v8::Value>();
785
786 v8::MicrotasksScope microtasksScope(m_isolate, v8::MicrotasksScope::kRunMicr otasks);
787 int groupId = getGroupId(context);
788 if (V8DebuggerAgentImpl* agent = findEnabledDebuggerAgent(groupId))
789 agent->willExecuteScript(script->GetUnboundScript()->GetId());
790 v8::MaybeLocal<v8::Value> result = script->Run(context);
791 // Get agent from the map again, since it could have detached during script execution.
792 if (V8DebuggerAgentImpl* agent = findEnabledDebuggerAgent(groupId))
793 agent->didExecuteScript();
794 return result;
795 }
796
797 v8::MaybeLocal<v8::Value> V8DebuggerImpl::callFunction(v8::Local<v8::Function> f unction, v8::Local<v8::Context> context, v8::Local<v8::Value> receiver, int argc , v8::Local<v8::Value> info[])
798 {
799 // TODO(dgozman): get rid of this check.
800 if (!m_client->isExecutionAllowed())
801 return v8::MaybeLocal<v8::Value>();
802
803 v8::MicrotasksScope microtasksScope(m_isolate, v8::MicrotasksScope::kRunMicr otasks);
804 int groupId = getGroupId(context);
805 if (V8DebuggerAgentImpl* agent = findEnabledDebuggerAgent(groupId))
806 agent->willExecuteScript(function->ScriptId());
807 v8::MaybeLocal<v8::Value> result = function->Call(context, receiver, argc, i nfo);
808 // Get agent from the map again, since it could have detached during script execution.
809 if (V8DebuggerAgentImpl* agent = findEnabledDebuggerAgent(groupId))
810 agent->didExecuteScript();
811 return result;
812 }
813
814 v8::MaybeLocal<v8::Value> V8DebuggerImpl::compileAndRunInternalScript(v8::Local< v8::Context> context, v8::Local<v8::String> source)
815 {
816 v8::Local<v8::Script> script = compileScript(context, source, String(), true );
817 if (script.IsEmpty())
818 return v8::MaybeLocal<v8::Value>();
819 v8::MicrotasksScope microtasksScope(m_isolate, v8::MicrotasksScope::kDoNotRu nMicrotasks);
820 return script->Run(context);
821 }
822
823 v8::Local<v8::Script> V8DebuggerImpl::compileScript(v8::Local<v8::Context> conte xt, v8::Local<v8::String> code, const String16& fileName, bool markAsInternal)
824 {
825 v8::ScriptOrigin origin(
826 toV8String(m_isolate, fileName),
827 v8::Integer::New(m_isolate, 0),
828 v8::Integer::New(m_isolate, 0),
829 v8::False(m_isolate), // sharable
830 v8::Local<v8::Integer>(),
831 v8::Boolean::New(m_isolate, markAsInternal), // internal
832 toV8String(m_isolate, String16()), // sourceMap
833 v8::True(m_isolate)); // opaqueresource
834 v8::ScriptCompiler::Source source(code, origin);
835 v8::Local<v8::Script> script;
836 if (!v8::ScriptCompiler::Compile(context, &source, v8::ScriptCompiler::kNoCo mpileOptions).ToLocal(&script))
837 return v8::Local<v8::Script>();
838 return script;
839 }
840
841 void V8DebuggerImpl::enableStackCapturingIfNeeded()
842 {
843 if (!m_capturingStackTracesCount)
844 V8StackTraceImpl::setCaptureStackTraceForUncaughtExceptions(m_isolate, t rue);
845 ++m_capturingStackTracesCount;
846 }
847
848 void V8DebuggerImpl::disableStackCapturingIfNeeded()
849 {
850 if (!(--m_capturingStackTracesCount))
851 V8StackTraceImpl::setCaptureStackTraceForUncaughtExceptions(m_isolate, f alse);
852 }
853
854 V8ConsoleMessageStorage* V8DebuggerImpl::ensureConsoleMessageStorage(int context GroupId)
855 {
856 ConsoleStorageMap::iterator storageIt = m_consoleStorageMap.find(contextGrou pId);
857 if (storageIt == m_consoleStorageMap.end())
858 storageIt = m_consoleStorageMap.insert(std::make_pair(contextGroupId, wr apUnique(new V8ConsoleMessageStorage(this, contextGroupId)))).first;
859 return storageIt->second.get();
860 }
861
862 std::unique_ptr<V8StackTrace> V8DebuggerImpl::createStackTrace(v8::Local<v8::Sta ckTrace> stackTrace)
863 {
864 return createStackTraceImpl(stackTrace);
865 }
866
867 std::unique_ptr<V8StackTraceImpl> V8DebuggerImpl::createStackTraceImpl(v8::Local <v8::StackTrace> stackTrace)
868 {
869 int contextGroupId = m_isolate->InContext() ? getGroupId(m_isolate->GetCurre ntContext()) : 0;
870 return V8StackTraceImpl::create(this, contextGroupId, stackTrace, V8StackTra ceImpl::maxCallStackSizeToCapture);
871 }
872
873 std::unique_ptr<V8InspectorSession> V8DebuggerImpl::connect(int contextGroupId, protocol::FrontendChannel* channel, V8InspectorSessionClient* client, const Stri ng16* state)
874 {
875 DCHECK(m_sessions.find(contextGroupId) == m_sessions.cend());
876 std::unique_ptr<V8InspectorSessionImpl> session =
877 V8InspectorSessionImpl::create(this, contextGroupId, channel, client, st ate);
878 m_sessions[contextGroupId] = session.get();
879 return std::move(session);
880 }
881
882 void V8DebuggerImpl::disconnect(V8InspectorSessionImpl* session)
883 {
884 DCHECK(m_sessions.find(session->contextGroupId()) != m_sessions.end());
885 m_sessions.erase(session->contextGroupId());
886 }
887
888 InspectedContext* V8DebuggerImpl::getContext(int groupId, int contextId) const
889 {
890 ContextsByGroupMap::const_iterator contextGroupIt = m_contexts.find(groupId) ;
891 if (contextGroupIt == m_contexts.end())
892 return nullptr;
893
894 ContextByIdMap::iterator contextIt = contextGroupIt->second->find(contextId) ;
895 if (contextIt == contextGroupIt->second->end())
896 return nullptr;
897
898 return contextIt->second.get();
899 }
900
901 void V8DebuggerImpl::contextCreated(const V8ContextInfo& info)
902 {
903 DCHECK(info.context->GetIsolate() == m_isolate);
904 // TODO(dgozman): make s_lastContextId non-static.
905 int contextId = atomicIncrement(&s_lastContextId);
906 String16 debugData = String16::fromInteger(info.contextGroupId) + "," + Stri ng16::fromInteger(contextId) + "," + (info.isDefault ? "default" : "nondefault") ;
907 v8::HandleScope scope(m_isolate);
908 v8::Context::Scope contextScope(info.context);
909 info.context->SetEmbedderData(static_cast<int>(v8::Context::kDebugIdIndex), toV8String(m_isolate, debugData));
910
911 ContextsByGroupMap::iterator contextIt = m_contexts.find(info.contextGroupId );
912 if (contextIt == m_contexts.end())
913 contextIt = m_contexts.insert(std::make_pair(info.contextGroupId, wrapUn ique(new ContextByIdMap()))).first;
914
915 const auto& contextById = contextIt->second;
916
917 DCHECK(contextById->find(contextId) == contextById->cend());
918 InspectedContext* context = new InspectedContext(this, info, contextId);
919 (*contextById)[contextId] = wrapUnique(context);
920 SessionMap::iterator sessionIt = m_sessions.find(info.contextGroupId);
921 if (sessionIt != m_sessions.end())
922 sessionIt->second->runtimeAgent()->reportExecutionContextCreated(context );
923 }
924
925 void V8DebuggerImpl::contextDestroyed(v8::Local<v8::Context> context)
926 {
927 int contextId = V8DebuggerImpl::contextId(context);
928 int contextGroupId = getGroupId(context);
929
930 ConsoleStorageMap::iterator storageIt = m_consoleStorageMap.find(contextGrou pId);
931 if (storageIt != m_consoleStorageMap.end())
932 storageIt->second->contextDestroyed(contextId);
933
934 InspectedContext* inspectedContext = getContext(contextGroupId, contextId);
935 if (!inspectedContext)
936 return;
937
938 SessionMap::iterator iter = m_sessions.find(contextGroupId);
939 if (iter != m_sessions.end())
940 iter->second->runtimeAgent()->reportExecutionContextDestroyed(inspectedC ontext);
941 discardInspectedContext(contextGroupId, contextId);
942 }
943
944 void V8DebuggerImpl::resetContextGroup(int contextGroupId)
945 {
946 m_consoleStorageMap.erase(contextGroupId);
947 SessionMap::iterator session = m_sessions.find(contextGroupId);
948 if (session != m_sessions.end())
949 session->second->reset();
950 m_contexts.erase(contextGroupId);
951 }
952
953 void V8DebuggerImpl::setAsyncCallStackDepth(V8DebuggerAgentImpl* agent, int dept h)
954 {
955 if (depth <= 0)
956 m_maxAsyncCallStackDepthMap.erase(agent);
957 else
958 m_maxAsyncCallStackDepthMap[agent] = depth;
959
960 int maxAsyncCallStackDepth = 0;
961 for (const auto& pair : m_maxAsyncCallStackDepthMap) {
962 if (pair.second > maxAsyncCallStackDepth)
963 maxAsyncCallStackDepth = pair.second;
964 }
965
966 if (m_maxAsyncCallStackDepth == maxAsyncCallStackDepth)
967 return;
968 m_maxAsyncCallStackDepth = maxAsyncCallStackDepth;
969 if (!maxAsyncCallStackDepth)
970 allAsyncTasksCanceled();
971 }
972
973 void V8DebuggerImpl::asyncTaskScheduled(const String16& taskName, void* task, bo ol recurring)
974 {
975 if (!m_maxAsyncCallStackDepth)
976 return;
977 v8::HandleScope scope(m_isolate);
978 int contextGroupId = m_isolate->InContext() ? getGroupId(m_isolate->GetCurre ntContext()) : 0;
979 std::unique_ptr<V8StackTraceImpl> chain = V8StackTraceImpl::capture(this, co ntextGroupId, V8StackTraceImpl::maxCallStackSizeToCapture, taskName);
980 if (chain) {
981 m_asyncTaskStacks[task] = std::move(chain);
982 if (recurring)
983 m_recurringTasks.insert(task);
984 }
985 }
986
987 void V8DebuggerImpl::asyncTaskCanceled(void* task)
988 {
989 if (!m_maxAsyncCallStackDepth)
990 return;
991 m_asyncTaskStacks.erase(task);
992 m_recurringTasks.erase(task);
993 }
994
995 void V8DebuggerImpl::asyncTaskStarted(void* task)
996 {
997 if (!m_maxAsyncCallStackDepth)
998 return;
999 m_currentTasks.push_back(task);
1000 AsyncTaskToStackTrace::iterator stackIt = m_asyncTaskStacks.find(task);
1001 // Needs to support following order of events:
1002 // - asyncTaskScheduled
1003 // <-- attached here -->
1004 // - asyncTaskStarted
1005 // - asyncTaskCanceled <-- canceled before finished
1006 // <-- async stack requested here -->
1007 // - asyncTaskFinished
1008 std::unique_ptr<V8StackTraceImpl> stack;
1009 if (stackIt != m_asyncTaskStacks.end() && stackIt->second)
1010 stack = stackIt->second->cloneImpl();
1011 m_currentStacks.push_back(std::move(stack));
1012 }
1013
1014 void V8DebuggerImpl::asyncTaskFinished(void* task)
1015 {
1016 if (!m_maxAsyncCallStackDepth)
1017 return;
1018 // We could start instrumenting half way and the stack is empty.
1019 if (!m_currentStacks.size())
1020 return;
1021
1022 DCHECK(m_currentTasks.back() == task);
1023 m_currentTasks.pop_back();
1024
1025 m_currentStacks.pop_back();
1026 if (m_recurringTasks.find(task) == m_recurringTasks.end())
1027 m_asyncTaskStacks.erase(task);
1028 }
1029
1030 void V8DebuggerImpl::allAsyncTasksCanceled()
1031 {
1032 m_asyncTaskStacks.clear();
1033 m_recurringTasks.clear();
1034 m_currentStacks.clear();
1035 m_currentTasks.clear();
1036 }
1037
1038 void V8DebuggerImpl::willExecuteScript(v8::Local<v8::Context> context, int scrip tId)
1039 {
1040 if (V8DebuggerAgentImpl* agent = findEnabledDebuggerAgent(context))
1041 agent->willExecuteScript(scriptId);
1042 }
1043
1044 void V8DebuggerImpl::didExecuteScript(v8::Local<v8::Context> context)
1045 {
1046 if (V8DebuggerAgentImpl* agent = findEnabledDebuggerAgent(context))
1047 agent->didExecuteScript();
1048 }
1049
1050 void V8DebuggerImpl::idleStarted()
1051 {
1052 m_isolate->GetCpuProfiler()->SetIdle(true);
1053 }
1054
1055 void V8DebuggerImpl::idleFinished()
1056 {
1057 m_isolate->GetCpuProfiler()->SetIdle(false);
1058 }
1059
1060 unsigned V8DebuggerImpl::exceptionThrown(v8::Local<v8::Context> context, const S tring16& message, v8::Local<v8::Value> exception, const String16& detailedMessag e, const String16& url, unsigned lineNumber, unsigned columnNumber, std::unique_ ptr<V8StackTrace> stackTrace, int scriptId)
1061 {
1062 int contextGroupId = getGroupId(context);
1063 if (!contextGroupId)
1064 return 0;
1065 std::unique_ptr<V8StackTraceImpl> stackTraceImpl = wrapUnique(static_cast<V8 StackTraceImpl*>(stackTrace.release()));
1066 unsigned exceptionId = ++m_lastExceptionId;
1067 std::unique_ptr<V8ConsoleMessage> consoleMessage = V8ConsoleMessage::createF orException(m_client->currentTimeMS(), detailedMessage, url, lineNumber, columnN umber, std::move(stackTraceImpl), scriptId, m_isolate, message, contextId(contex t), exception, exceptionId);
1068 ensureConsoleMessageStorage(contextGroupId)->addMessage(std::move(consoleMes sage));
1069 return exceptionId;
1070 }
1071
1072 void V8DebuggerImpl::exceptionRevoked(v8::Local<v8::Context> context, unsigned e xceptionId, const String16& message)
1073 {
1074 int contextGroupId = getGroupId(context);
1075 if (!contextGroupId)
1076 return;
1077
1078 std::unique_ptr<V8ConsoleMessage> consoleMessage = V8ConsoleMessage::createF orRevokedException(m_client->currentTimeMS(), message, exceptionId);
1079 ensureConsoleMessageStorage(contextGroupId)->addMessage(std::move(consoleMes sage));
1080 }
1081
1082 std::unique_ptr<V8StackTrace> V8DebuggerImpl::captureStackTrace(bool fullStack)
1083 {
1084 return captureStackTraceImpl(fullStack);
1085 }
1086
1087 std::unique_ptr<V8StackTraceImpl> V8DebuggerImpl::captureStackTraceImpl(bool ful lStack)
1088 {
1089 if (!m_isolate->InContext())
1090 return nullptr;
1091
1092 v8::HandleScope handles(m_isolate);
1093 int contextGroupId = getGroupId(m_isolate->GetCurrentContext());
1094 if (!contextGroupId)
1095 return nullptr;
1096
1097 size_t stackSize = fullStack ? V8StackTraceImpl::maxCallStackSizeToCapture : 1;
1098 SessionMap::iterator sessionIt = m_sessions.find(contextGroupId);
1099 if (sessionIt != m_sessions.end() && sessionIt->second->runtimeAgent()->enab led())
1100 stackSize = V8StackTraceImpl::maxCallStackSizeToCapture;
1101
1102 return V8StackTraceImpl::capture(this, contextGroupId, stackSize);
1103 }
1104
1105 v8::Local<v8::Context> V8DebuggerImpl::regexContext()
1106 {
1107 if (m_regexContext.IsEmpty())
1108 m_regexContext.Reset(m_isolate, v8::Context::New(m_isolate));
1109 return m_regexContext.Get(m_isolate);
1110 }
1111
1112 void V8DebuggerImpl::discardInspectedContext(int contextGroupId, int contextId)
1113 {
1114 if (!getContext(contextGroupId, contextId))
1115 return;
1116 m_contexts[contextGroupId]->erase(contextId);
1117 if (m_contexts[contextGroupId]->empty())
1118 m_contexts.erase(contextGroupId);
1119 }
1120
1121 const V8DebuggerImpl::ContextByIdMap* V8DebuggerImpl::contextGroup(int contextGr oupId)
1122 {
1123 ContextsByGroupMap::iterator iter = m_contexts.find(contextGroupId);
1124 return iter == m_contexts.end() ? nullptr : iter->second.get();
1125 }
1126
1127 V8InspectorSessionImpl* V8DebuggerImpl::sessionForContextGroup(int contextGroupI d)
1128 {
1129 if (!contextGroupId)
1130 return nullptr;
1131 SessionMap::iterator iter = m_sessions.find(contextGroupId);
1132 return iter == m_sessions.end() ? nullptr : iter->second;
1133 }
1134
1135 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698