2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
33 var DebuggerScript = {};
35 DebuggerScript.PauseOnExceptionsState = {
36 DontPauseOnExceptions: 0,
37 PauseOnAllExceptions: 1,
38 PauseOnUncaughtExceptions: 2
41 DebuggerScript.ScopeInfoDetails = {
47 DebuggerScript._pauseOnExceptionsState = DebuggerScript.PauseOnExceptionsState.DontPauseOnExceptions;
48 Debug.clearBreakOnException();
49 Debug.clearBreakOnUncaughtException();
51 DebuggerScript.getAfterCompileScript = function(eventData)
53 return DebuggerScript._formatScript(eventData.script_.script_);
56 DebuggerScript.getWorkerScripts = function()
59 var scripts = Debug.scripts();
60 for (var i = 0; i < scripts.length; ++i) {
61 var script = scripts[i];
62 // Workers don't share same V8 heap now so there is no need to complicate stuff with
63 // the context id like we do to discriminate between scripts from different pages.
64 // However we need to filter out v8 native scripts.
65 if (script.context_data && script.context_data === "worker")
66 result.push(DebuggerScript._formatScript(script));
71 DebuggerScript.getFunctionScopes = function(fun)
73 var mirror = MakeMirror(fun);
74 var count = mirror.scopeCount();
78 for (var i = 0; i < count; i++) {
79 var scopeDetails = mirror.scope(i).details();
81 type: scopeDetails.type(),
82 object: DebuggerScript._buildScopeObject(scopeDetails.type(), scopeDetails.object())
88 DebuggerScript.getInternalProperties = function(value)
90 var properties = ObjectMirror.GetInternalProperties(value);
92 for (var i = 0; i < properties.length; i++) {
93 var mirror = properties[i];
96 value: mirror.value().value()
102 DebuggerScript.setFunctionVariableValue = function(functionValue, scopeIndex, variableName, newValue)
104 var mirror = MakeMirror(functionValue);
105 if (!mirror.isFunction())
106 throw new Error("Function value has incorrect type");
107 return DebuggerScript._setScopeVariableValue(mirror, scopeIndex, variableName, newValue);
110 DebuggerScript._setScopeVariableValue = function(scopeHolder, scopeIndex, variableName, newValue)
112 var scopeMirror = scopeHolder.scope(scopeIndex);
114 throw new Error("Incorrect scope index");
115 scopeMirror.setVariableValue(variableName, newValue);
119 DebuggerScript.getScripts = function(contextData)
125 var comma = contextData.indexOf(",");
128 // Context data is a string in the following format:
129 // ("page"|"injected")","<page id>
130 var idSuffix = contextData.substring(comma); // including the comma
132 var scripts = Debug.scripts();
133 for (var i = 0; i < scripts.length; ++i) {
134 var script = scripts[i];
135 if (script.context_data && script.context_data.lastIndexOf(idSuffix) != -1)
136 result.push(DebuggerScript._formatScript(script));
141 DebuggerScript._formatScript = function(script)
143 var lineEnds = script.line_ends;
144 var lineCount = lineEnds.length;
145 var endLine = script.line_offset + lineCount - 1;
147 // V8 will not count last line if script source ends with \n.
148 if (script.source[script.source.length - 1] === '\n') {
153 endColumn = script.source.length + script.column_offset;
155 endColumn = script.source.length - (lineEnds[lineCount - 2] + 1);
160 name: script.nameOrSourceURL(),
161 source: script.source,
162 startLine: script.line_offset,
163 startColumn: script.column_offset,
165 endColumn: endColumn,
166 isContentScript: !!script.context_data && script.context_data.indexOf("injected") == 0
170 DebuggerScript.setBreakpoint = function(execState, info)
172 var positionAlignment = info.interstatementLocation ? Debug.BreakPositionAlignment.BreakPosition : Debug.BreakPositionAlignment.Statement;
173 var breakId = Debug.setScriptBreakPointById(info.sourceID, info.lineNumber, info.columnNumber, info.condition, undefined, positionAlignment);
175 var locations = Debug.findBreakPointActualLocations(breakId);
176 if (!locations.length)
178 info.lineNumber = locations[0].line;
179 info.columnNumber = locations[0].column;
180 return breakId.toString();
183 DebuggerScript.removeBreakpoint = function(execState, info)
185 Debug.findBreakPoint(info.breakpointId, true);
188 DebuggerScript.pauseOnExceptionsState = function()
190 return DebuggerScript._pauseOnExceptionsState;
193 DebuggerScript.setPauseOnExceptionsState = function(newState)
195 DebuggerScript._pauseOnExceptionsState = newState;
197 if (DebuggerScript.PauseOnExceptionsState.PauseOnAllExceptions === newState)
198 Debug.setBreakOnException();
200 Debug.clearBreakOnException();
202 if (DebuggerScript.PauseOnExceptionsState.PauseOnUncaughtExceptions === newState)
203 Debug.setBreakOnUncaughtException();
205 Debug.clearBreakOnUncaughtException();
208 DebuggerScript.frameCount = function(execState)
210 return execState.frameCount();
213 DebuggerScript.currentCallFrame = function(execState, data)
215 var maximumLimit = data >> 2;
216 var scopeDetailsLevel = data & 3;
218 var frameCount = execState.frameCount();
219 if (maximumLimit && maximumLimit < frameCount)
220 frameCount = maximumLimit;
221 var topFrame = undefined;
222 for (var i = frameCount - 1; i >= 0; i--) {
223 var frameMirror = execState.frame(i);
224 topFrame = DebuggerScript._frameMirrorToJSCallFrame(frameMirror, topFrame, scopeDetailsLevel);
229 DebuggerScript.stepIntoStatement = function(execState)
231 execState.prepareStep(Debug.StepAction.StepIn, 1);
234 DebuggerScript.stepOverStatement = function(execState, callFrame)
236 execState.prepareStep(Debug.StepAction.StepNext, 1);
239 DebuggerScript.stepOutOfFunction = function(execState, callFrame)
241 execState.prepareStep(Debug.StepAction.StepOut, 1);
244 // Returns array in form:
245 // [ 0, <v8_result_report> ] in case of success
246 // or [ 1, <general_error_message>, <compiler_message>, <line_number>, <column_number> ] in case of compile error, numbers are 1-based.
247 // or throws exception with message.
248 DebuggerScript.liveEditScriptSource = function(scriptId, newSource, preview)
250 var scripts = Debug.scripts();
251 var scriptToEdit = null;
252 for (var i = 0; i < scripts.length; i++) {
253 if (scripts[i].id == scriptId) {
254 scriptToEdit = scripts[i];
259 throw("Script not found");
263 var result = Debug.LiveEdit.SetScriptSource(scriptToEdit, newSource, preview, changeLog);
266 if (e instanceof Debug.LiveEdit.Failure && "details" in e) {
267 var details = e.details;
268 if (details.type === "liveedit_compile_error") {
269 var startPosition = details.position.start;
270 return [1, String(e), String(details.syntaxErrorMessage), Number(startPosition.line), Number(startPosition.column)];
277 DebuggerScript.clearBreakpoints = function(execState, info)
279 Debug.clearAllBreakPoints();
282 DebuggerScript.setBreakpointsActivated = function(execState, info)
284 Debug.debuggerFlags().breakPointsActive.setValue(info.enabled);
287 DebuggerScript.getScriptSource = function(eventData)
289 return eventData.script().source();
292 DebuggerScript.setScriptSource = function(eventData, source)
294 if (eventData.script().data() === "injected-script")
296 eventData.script().setSource(source);
299 DebuggerScript.getScriptName = function(eventData)
301 return eventData.script().script_.nameOrSourceURL();
304 DebuggerScript.getBreakpointNumbers = function(eventData)
306 var breakpoints = eventData.breakPointsHit();
311 for (var i = 0; i < breakpoints.length; i++) {
312 var breakpoint = breakpoints[i];
313 var scriptBreakPoint = breakpoint.script_break_point();
314 numbers.push(scriptBreakPoint ? scriptBreakPoint.number() : breakpoint.number());
319 DebuggerScript.isEvalCompilation = function(eventData)
321 var script = eventData.script();
322 return (script.compilationType() === Debug.ScriptCompilationType.Eval);
325 // NOTE: This function is performance critical, as it can be run on every
326 // statement that generates an async event (like addEventListener) to support
327 // asynchronous call stacks. Thus, when possible, initialize the data lazily.
328 DebuggerScript._frameMirrorToJSCallFrame = function(frameMirror, callerFrame, scopeDetailsLevel)
330 // Stuff that can not be initialized lazily (i.e. valid while paused with a valid break_id).
331 // The frameMirror and scopeMirror can be accessed only while paused on the debugger.
332 var frameDetails = frameMirror.details();
334 var funcObject = frameDetails.func();
335 var sourcePosition = frameDetails.sourcePosition();
336 var thisObject = frameDetails.receiver();
338 var isAtReturn = !!frameDetails.isAtReturn();
339 var returnValue = isAtReturn ? frameDetails.returnValue() : undefined;
341 var scopeMirrors = (scopeDetailsLevel === DebuggerScript.ScopeInfoDetails.NoScopes ? [] : frameMirror.allScopes(scopeDetailsLevel === DebuggerScript.ScopeInfoDetails.FastAsyncScopes));
342 var scopeTypes = new Array(scopeMirrors.length);
343 var scopeObjects = new Array(scopeMirrors.length);
344 for (var i = 0; i < scopeMirrors.length; ++i) {
345 var scopeDetails = scopeMirrors[i].details();
346 scopeTypes[i] = scopeDetails.type();
347 scopeObjects[i] = scopeDetails.object();
350 // Calculated lazily.
355 function lazyScopeChain()
359 for (var i = 0; i < scopeObjects.length; ++i)
360 scopeChain.push(DebuggerScript._buildScopeObject(scopeTypes[i], scopeObjects[i]));
361 scopeObjects = null; // Free for GC.
366 function ensureFuncMirror()
369 funcMirror = MakeMirror(funcObject);
370 if (!funcMirror.isFunction())
371 funcMirror = new UnresolvedFunctionMirror(funcObject);
376 function ensureLocation()
379 var script = ensureFuncMirror().script();
381 location = script.locationFromPosition(sourcePosition, true);
383 location = { line: 0, column: 0 };
390 return ensureLocation().line;
395 return ensureLocation().column;
400 var script = ensureFuncMirror().script();
401 return script && script.id();
404 function functionName()
406 var func = ensureFuncMirror();
407 if (!func.resolved())
410 var valueMirror = func.property("displayName").value();
411 if (valueMirror && valueMirror.isString())
412 displayName = valueMirror.value();
413 return displayName || func.name() || func.inferredName();
416 function evaluate(expression)
418 return frameMirror.evaluate(expression, false).value();
423 return Debug.LiveEdit.RestartFrame(frameMirror);
426 function setVariableValue(scopeNumber, variableName, newValue)
428 return DebuggerScript._setScopeVariableValue(frameMirror, scopeNumber, variableName, newValue);
431 function stepInPositions()
433 var stepInPositionsV8 = frameMirror.stepInPositions();
434 var stepInPositionsProtocol;
435 if (stepInPositionsV8) {
436 stepInPositionsProtocol = [];
437 var script = ensureFuncMirror().script();
439 var scriptId = String(script.id());
440 for (var i = 0; i < stepInPositionsV8.length; i++) {
443 lineNumber: stepInPositionsV8[i].position.line,
444 columnNumber: stepInPositionsV8[i].position.column
446 stepInPositionsProtocol.push(item);
450 return JSON.stringify(stepInPositionsProtocol);
454 "sourceID": sourceID,
457 "functionName": functionName,
458 "thisObject": thisObject,
459 "scopeChain": lazyScopeChain,
460 "scopeType": scopeTypes,
461 "evaluate": evaluate,
462 "caller": callerFrame,
464 "setVariableValue": setVariableValue,
465 "stepInPositions": stepInPositions,
466 "isAtReturn": isAtReturn,
467 "returnValue": returnValue
471 DebuggerScript._buildScopeObject = function(scopeType, scopeObject)
475 case ScopeType.Local:
476 case ScopeType.Closure:
477 case ScopeType.Catch:
478 // For transient objects we create a "persistent" copy that contains
479 // the same properties.
480 // Reset scope object prototype to null so that the proto properties
481 // don't appear in the local scope section.
482 result = { __proto__: null };
483 var properties = MakeMirror(scopeObject, true /* transient */).properties();
484 for (var j = 0; j < properties.length; j++) {
485 var name = properties[j].name();
486 if (name.charAt(0) === ".")
487 continue; // Skip internal variables like ".arguments"
488 result[name] = properties[j].value_;
491 case ScopeType.Global:
493 result = scopeObject;
495 case ScopeType.Block:
496 // Unsupported yet. Mustn't be reachable.
502 // We never resolve Mirror by its handle so to avoid memory leaks caused by Mirrors in the cache we disable it.
503 ToggleMirrorCache(false);
505 return DebuggerScript;