2016-10-21 06:37:29 +00:00
|
|
|
// Copyright 2016 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2016-10-28 07:21:25 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
// If true, prints all messages sent and received by inspector.
|
|
|
|
const printProtocolMessages = false;
|
|
|
|
|
|
|
|
// The active wrapper instance.
|
|
|
|
let activeWrapper = undefined;
|
|
|
|
|
|
|
|
// Receiver function called by inspector, delegating to active wrapper.
|
|
|
|
function receive(message) {
|
|
|
|
activeWrapper.receiveMessage(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
class DebugWrapper {
|
|
|
|
constructor() {
|
|
|
|
// Message dictionary storing {id, message} pairs.
|
2016-11-07 12:24:40 +00:00
|
|
|
this.receivedMessages = new Map();
|
2016-10-28 07:21:25 +00:00
|
|
|
|
|
|
|
// Each message dispatched by the Debug wrapper is assigned a unique number
|
|
|
|
// using nextMessageId.
|
|
|
|
this.nextMessageId = 0;
|
|
|
|
|
|
|
|
// The listener method called on certain events.
|
2016-10-28 08:18:07 +00:00
|
|
|
this.listener = undefined;
|
|
|
|
|
|
|
|
// Debug events which can occur in the V8 JavaScript engine.
|
2016-11-07 12:24:40 +00:00
|
|
|
this.DebugEvent = { Break: 1,
|
|
|
|
Exception: 2,
|
2016-12-02 09:01:37 +00:00
|
|
|
AfterCompile: 3,
|
|
|
|
CompileError: 4,
|
2017-02-16 11:48:22 +00:00
|
|
|
OOM: 5,
|
2016-10-28 08:18:07 +00:00
|
|
|
};
|
2016-10-28 07:21:25 +00:00
|
|
|
|
2016-11-07 12:24:40 +00:00
|
|
|
// The different types of steps.
|
|
|
|
this.StepAction = { StepOut: 0,
|
|
|
|
StepNext: 1,
|
|
|
|
StepIn: 2,
|
|
|
|
};
|
|
|
|
|
|
|
|
// A copy of the scope types from runtime-debug.cc.
|
|
|
|
// NOTE: these constants should be backward-compatible, so
|
|
|
|
// add new ones to the end of this list.
|
|
|
|
this.ScopeType = { Global: 0,
|
|
|
|
Local: 1,
|
|
|
|
With: 2,
|
|
|
|
Closure: 3,
|
|
|
|
Catch: 4,
|
|
|
|
Block: 5,
|
|
|
|
Script: 6,
|
|
|
|
Eval: 7,
|
|
|
|
Module: 8
|
|
|
|
};
|
|
|
|
|
2016-11-08 13:53:55 +00:00
|
|
|
// Types of exceptions that can be broken upon.
|
|
|
|
this.ExceptionBreak = { Caught : 0,
|
|
|
|
Uncaught: 1 };
|
|
|
|
|
2016-11-07 12:24:40 +00:00
|
|
|
// Store the current script id so we can skip corresponding break events.
|
|
|
|
this.thisScriptId = %FunctionGetScriptId(receive);
|
|
|
|
|
2016-11-17 09:33:52 +00:00
|
|
|
// Stores all set breakpoints.
|
|
|
|
this.breakpoints = new Set();
|
|
|
|
|
2016-10-28 07:21:25 +00:00
|
|
|
// Register as the active wrapper.
|
|
|
|
assertTrue(activeWrapper === undefined);
|
|
|
|
activeWrapper = this;
|
|
|
|
}
|
|
|
|
|
2016-10-28 08:18:07 +00:00
|
|
|
enable() { this.sendMessageForMethodChecked("Debugger.enable"); }
|
|
|
|
disable() { this.sendMessageForMethodChecked("Debugger.disable"); }
|
|
|
|
|
|
|
|
setListener(listener) { this.listener = listener; }
|
|
|
|
|
|
|
|
stepOver() { this.sendMessageForMethodChecked("Debugger.stepOver"); }
|
|
|
|
stepInto() { this.sendMessageForMethodChecked("Debugger.stepInto"); }
|
|
|
|
stepOut() { this.sendMessageForMethodChecked("Debugger.stepOut"); }
|
|
|
|
|
2016-11-08 13:53:55 +00:00
|
|
|
setBreakOnException() {
|
|
|
|
this.sendMessageForMethodChecked(
|
|
|
|
"Debugger.setPauseOnExceptions", { state : "all" });
|
|
|
|
}
|
|
|
|
|
|
|
|
clearBreakOnException() {
|
|
|
|
const newState = this.isBreakOnUncaughtException() ? "uncaught" : "none";
|
|
|
|
this.sendMessageForMethodChecked(
|
|
|
|
"Debugger.setPauseOnExceptions", { state : newState });
|
|
|
|
}
|
|
|
|
|
|
|
|
isBreakOnException() {
|
|
|
|
return !!%IsBreakOnException(this.ExceptionBreak.Caught);
|
|
|
|
};
|
|
|
|
|
|
|
|
setBreakOnUncaughtException() {
|
|
|
|
const newState = this.isBreakOnException() ? "all" : "uncaught";
|
|
|
|
this.sendMessageForMethodChecked(
|
|
|
|
"Debugger.setPauseOnExceptions", { state : newState });
|
|
|
|
}
|
|
|
|
|
|
|
|
clearBreakOnUncaughtException() {
|
|
|
|
const newState = this.isBreakOnException() ? "all" : "none";
|
|
|
|
this.sendMessageForMethodChecked(
|
|
|
|
"Debugger.setPauseOnExceptions", { state : newState });
|
|
|
|
}
|
|
|
|
|
|
|
|
isBreakOnUncaughtException() {
|
|
|
|
return !!%IsBreakOnException(this.ExceptionBreak.Uncaught);
|
|
|
|
};
|
|
|
|
|
|
|
|
clearStepping() { %ClearStepping(); };
|
|
|
|
|
2016-10-28 08:18:07 +00:00
|
|
|
// Returns the resulting breakpoint id.
|
|
|
|
setBreakPoint(func, opt_line, opt_column, opt_condition) {
|
|
|
|
assertTrue(%IsFunction(func));
|
|
|
|
assertFalse(%FunctionIsAPIFunction(func));
|
|
|
|
|
|
|
|
const scriptid = %FunctionGetScriptId(func);
|
|
|
|
assertTrue(scriptid != -1);
|
|
|
|
|
|
|
|
const offset = %FunctionGetScriptSourcePosition(func);
|
|
|
|
const loc =
|
|
|
|
%ScriptLocationFromLine2(scriptid, opt_line, opt_column, offset);
|
2016-11-17 09:33:52 +00:00
|
|
|
return this.setBreakPointAtLocation(scriptid, loc, opt_condition);
|
2016-11-17 08:43:17 +00:00
|
|
|
}
|
|
|
|
|
2016-11-21 09:28:49 +00:00
|
|
|
clearBreakPoint(breakpoint) {
|
|
|
|
assertTrue(this.breakpoints.has(breakpoint));
|
|
|
|
const breakid = breakpoint.id;
|
2016-10-28 08:18:07 +00:00
|
|
|
const {msgid, msg} = this.createMessage(
|
|
|
|
"Debugger.removeBreakpoint", { breakpointId : breakid });
|
2016-10-28 07:21:25 +00:00
|
|
|
this.sendMessage(msg);
|
2016-11-07 12:24:40 +00:00
|
|
|
this.takeReplyChecked(msgid);
|
2016-11-17 09:33:52 +00:00
|
|
|
this.breakpoints.delete(breakid);
|
|
|
|
}
|
|
|
|
|
|
|
|
clearAllBreakPoints() {
|
2016-11-21 09:28:49 +00:00
|
|
|
for (let breakpoint of this.breakpoints) {
|
|
|
|
this.clearBreakPoint(breakpoint);
|
2016-11-17 09:33:52 +00:00
|
|
|
}
|
|
|
|
this.breakpoints.clear();
|
2016-10-28 07:21:25 +00:00
|
|
|
}
|
|
|
|
|
2017-06-20 14:32:07 +00:00
|
|
|
showBreakPoints(f) {
|
2016-11-16 08:34:01 +00:00
|
|
|
if (!%IsFunction(f)) throw new Error("Not passed a Function");
|
|
|
|
|
|
|
|
const source = %FunctionGetSourceCode(f);
|
|
|
|
const offset = %FunctionGetScriptSourcePosition(f);
|
2017-06-20 14:32:07 +00:00
|
|
|
const locations = %GetBreakLocations(f);
|
2016-11-16 08:34:01 +00:00
|
|
|
|
|
|
|
if (!locations) return source;
|
|
|
|
|
|
|
|
locations.sort(function(x, y) { return x - y; });
|
|
|
|
|
|
|
|
let result = "";
|
|
|
|
let prev_pos = 0;
|
|
|
|
let pos;
|
|
|
|
|
|
|
|
for (var i = 0; i < locations.length; i++) {
|
|
|
|
pos = locations[i] - offset;
|
|
|
|
result += source.slice(prev_pos, pos);
|
|
|
|
result += "[B" + i + "]";
|
|
|
|
prev_pos = pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = source.length;
|
|
|
|
result += source.substring(prev_pos, pos);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
debuggerFlags() {
|
|
|
|
return { breakPointsActive :
|
|
|
|
{ setValue : (enabled) => this.setBreakPointsActive(enabled) }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-07-05 20:34:14 +00:00
|
|
|
// Returns the script source. The return value is the script source for the
|
|
|
|
// script in which the function is defined.
|
|
|
|
scriptSource(func) {
|
|
|
|
return %FunctionGetScriptSource(func);
|
2016-11-17 09:33:52 +00:00
|
|
|
};
|
|
|
|
|
2016-11-16 08:34:01 +00:00
|
|
|
setBreakPointsActive(enabled) {
|
2016-10-28 08:18:07 +00:00
|
|
|
const {msgid, msg} = this.createMessage(
|
2016-11-16 08:34:01 +00:00
|
|
|
"Debugger.setBreakpointsActive", { active : enabled });
|
2016-10-28 08:18:07 +00:00
|
|
|
this.sendMessage(msg);
|
2016-11-16 08:34:01 +00:00
|
|
|
this.takeReplyChecked(msgid);
|
2016-10-28 07:21:25 +00:00
|
|
|
}
|
|
|
|
|
2016-11-22 13:31:51 +00:00
|
|
|
generatorScopeCount(gen) {
|
|
|
|
return %GetGeneratorScopeCount(gen);
|
|
|
|
}
|
|
|
|
|
|
|
|
generatorScope(gen, index) {
|
|
|
|
// These indexes correspond definitions in debug-scopes.h.
|
|
|
|
const kScopeDetailsTypeIndex = 0;
|
|
|
|
const kScopeDetailsObjectIndex = 1;
|
|
|
|
|
|
|
|
const details = %GetGeneratorScopeDetails(gen, index);
|
|
|
|
|
|
|
|
function scopeObjectProperties() {
|
|
|
|
const obj = details[kScopeDetailsObjectIndex];
|
|
|
|
return Object.keys(obj).map((k, v) => v);
|
|
|
|
}
|
|
|
|
|
|
|
|
function setScopeVariableValue(name, value) {
|
2018-06-03 05:09:41 +00:00
|
|
|
const res = %SetGeneratorScopeVariableValue(gen, index, name, value);
|
2018-05-02 13:57:26 +00:00
|
|
|
if (!res) throw new Error("Failed to set variable '" + name + "' value");
|
2016-11-22 13:31:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const scopeObject =
|
|
|
|
{ value : () => details[kScopeDetailsObjectIndex],
|
|
|
|
property : (prop) => details[kScopeDetailsObjectIndex][prop],
|
|
|
|
properties : scopeObjectProperties,
|
|
|
|
propertyNames : () => Object.keys(details[kScopeDetailsObjectIndex])
|
|
|
|
.map((key, _) => key),
|
|
|
|
};
|
|
|
|
return { scopeType : () => details[kScopeDetailsTypeIndex],
|
|
|
|
scopeIndex : () => index,
|
|
|
|
scopeObject : () => scopeObject,
|
|
|
|
setVariableValue : setScopeVariableValue,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
generatorScopes(gen) {
|
|
|
|
const count = %GetGeneratorScopeCount(gen);
|
|
|
|
const scopes = [];
|
|
|
|
for (let i = 0; i < count; i++) {
|
|
|
|
scopes.push(this.generatorScope(gen, i));
|
|
|
|
}
|
|
|
|
return scopes;
|
|
|
|
}
|
|
|
|
|
2016-10-28 07:21:25 +00:00
|
|
|
// --- Internal methods. -----------------------------------------------------
|
|
|
|
|
|
|
|
getNextMessageId() {
|
|
|
|
return this.nextMessageId++;
|
|
|
|
}
|
|
|
|
|
|
|
|
createMessage(method, params) {
|
|
|
|
const id = this.getNextMessageId();
|
|
|
|
const msg = JSON.stringify({
|
|
|
|
id: id,
|
|
|
|
method: method,
|
|
|
|
params: params,
|
|
|
|
});
|
2016-11-07 12:24:40 +00:00
|
|
|
return { msgid : id, msg: msg };
|
2016-10-28 07:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
receiveMessage(message) {
|
|
|
|
const parsedMessage = JSON.parse(message);
|
2016-11-21 15:18:54 +00:00
|
|
|
if (printProtocolMessages) {
|
|
|
|
print(JSON.stringify(parsedMessage, undefined, 1));
|
|
|
|
}
|
2016-10-28 07:21:25 +00:00
|
|
|
if (parsedMessage.id !== undefined) {
|
2016-11-07 12:24:40 +00:00
|
|
|
this.receivedMessages.set(parsedMessage.id, parsedMessage);
|
2016-10-28 07:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.dispatchMessage(parsedMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
sendMessage(message) {
|
|
|
|
if (printProtocolMessages) print(message);
|
|
|
|
send(message);
|
|
|
|
}
|
|
|
|
|
2016-11-08 13:53:55 +00:00
|
|
|
sendMessageForMethodChecked(method, params) {
|
|
|
|
const {msgid, msg} = this.createMessage(method, params);
|
2016-10-28 08:18:07 +00:00
|
|
|
this.sendMessage(msg);
|
2016-11-07 12:24:40 +00:00
|
|
|
this.takeReplyChecked(msgid);
|
|
|
|
}
|
|
|
|
|
|
|
|
takeReplyChecked(msgid) {
|
|
|
|
const reply = this.receivedMessages.get(msgid);
|
|
|
|
assertTrue(reply !== undefined);
|
|
|
|
this.receivedMessages.delete(msgid);
|
|
|
|
return reply;
|
|
|
|
}
|
|
|
|
|
2016-11-17 09:33:52 +00:00
|
|
|
setBreakPointAtLocation(scriptid, loc, opt_condition) {
|
|
|
|
const params = { location :
|
|
|
|
{ scriptId : scriptid.toString(),
|
|
|
|
lineNumber : loc.line,
|
|
|
|
columnNumber : loc.column,
|
|
|
|
},
|
|
|
|
condition : opt_condition,
|
|
|
|
};
|
|
|
|
|
|
|
|
const {msgid, msg} = this.createMessage("Debugger.setBreakpoint", params);
|
|
|
|
this.sendMessage(msg);
|
|
|
|
|
|
|
|
const reply = this.takeReplyChecked(msgid);
|
2016-11-21 09:28:49 +00:00
|
|
|
const result = reply.result;
|
|
|
|
assertTrue(result !== undefined);
|
|
|
|
const breakid = result.breakpointId;
|
2016-11-17 09:33:52 +00:00
|
|
|
assertTrue(breakid !== undefined);
|
|
|
|
|
2018-07-05 20:34:14 +00:00
|
|
|
const breakpoint = { id : result.breakpointId }
|
2016-11-21 09:28:49 +00:00
|
|
|
|
|
|
|
this.breakpoints.add(breakpoint);
|
|
|
|
return breakpoint;
|
2016-11-17 09:33:52 +00:00
|
|
|
}
|
|
|
|
|
2016-11-07 12:24:40 +00:00
|
|
|
execStatePrepareStep(action) {
|
|
|
|
switch(action) {
|
|
|
|
case this.StepAction.StepOut: this.stepOut(); break;
|
|
|
|
case this.StepAction.StepNext: this.stepOver(); break;
|
|
|
|
case this.StepAction.StepIn: this.stepInto(); break;
|
|
|
|
default: %AbortJS("Unsupported StepAction"); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-08 14:53:48 +00:00
|
|
|
execStateScopeType(type) {
|
|
|
|
switch (type) {
|
|
|
|
case "global": return this.ScopeType.Global;
|
|
|
|
case "local": return this.ScopeType.Local;
|
|
|
|
case "with": return this.ScopeType.With;
|
|
|
|
case "closure": return this.ScopeType.Closure;
|
|
|
|
case "catch": return this.ScopeType.Catch;
|
|
|
|
case "block": return this.ScopeType.Block;
|
|
|
|
case "script": return this.ScopeType.Script;
|
2016-11-23 07:30:01 +00:00
|
|
|
case "eval": return this.ScopeType.Eval;
|
2016-12-16 14:28:56 +00:00
|
|
|
case "module": return this.ScopeType.Module;
|
2016-11-08 14:53:48 +00:00
|
|
|
default: %AbortJS("Unexpected scope type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-16 08:34:01 +00:00
|
|
|
execStateScopeObjectProperty(serialized_scope, prop) {
|
|
|
|
let found = null;
|
|
|
|
for (let i = 0; i < serialized_scope.length; i++) {
|
|
|
|
const elem = serialized_scope[i];
|
|
|
|
if (elem.name == prop) {
|
|
|
|
found = elem;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-23 07:30:01 +00:00
|
|
|
if (found == null) return { isUndefined : () => true };
|
2016-11-16 08:34:01 +00:00
|
|
|
|
|
|
|
const val = { value : () => found.value.value };
|
2016-12-14 07:20:13 +00:00
|
|
|
// Not undefined in the sense that we did find a property, even though
|
|
|
|
// the value can be 'undefined'.
|
2016-11-16 08:34:01 +00:00
|
|
|
return { value : () => val,
|
2016-12-14 07:20:13 +00:00
|
|
|
isUndefined : () => false,
|
2016-11-16 08:34:01 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-11-08 14:53:48 +00:00
|
|
|
// Returns an array of property descriptors of the scope object.
|
|
|
|
// This is in contrast to the original API, which simply passed object
|
|
|
|
// mirrors.
|
|
|
|
execStateScopeObject(obj) {
|
2016-11-11 12:07:41 +00:00
|
|
|
const serialized_scope = this.getProperties(obj.objectId);
|
2016-11-16 08:34:01 +00:00
|
|
|
const scope = this.propertiesToObject(serialized_scope);
|
|
|
|
return { value : () => scope,
|
|
|
|
property : (prop) =>
|
2016-12-14 07:20:13 +00:00
|
|
|
this.execStateScopeObjectProperty(serialized_scope, prop),
|
|
|
|
properties : () => serialized_scope.map(elem => elem.value),
|
|
|
|
propertyNames : () => serialized_scope.map(elem => elem.name)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-11-16 08:34:01 +00:00
|
|
|
setVariableValue(frame, scope_index, name, value) {
|
|
|
|
const frameid = frame.callFrameId;
|
|
|
|
const {msgid, msg} = this.createMessage(
|
|
|
|
"Debugger.setVariableValue",
|
|
|
|
{ callFrameId : frameid,
|
|
|
|
scopeNumber : scope_index,
|
|
|
|
variableName : name,
|
|
|
|
newValue : { value : value }
|
|
|
|
});
|
|
|
|
this.sendMessage(msg);
|
2016-11-21 09:28:49 +00:00
|
|
|
const reply = this.takeReplyChecked(msgid);
|
|
|
|
if (reply.error) {
|
2018-05-02 13:57:26 +00:00
|
|
|
throw new Error("Failed to set variable '" + name + "' value");
|
2016-11-21 09:28:49 +00:00
|
|
|
}
|
2016-11-16 08:34:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
execStateScope(frame, scope_index) {
|
|
|
|
const scope = frame.scopeChain[scope_index];
|
|
|
|
return { scopeType : () => this.execStateScopeType(scope.type),
|
2016-12-14 07:20:13 +00:00
|
|
|
scopeIndex : () => scope_index,
|
|
|
|
frameIndex : () => frame.callFrameId,
|
2016-11-16 08:34:01 +00:00
|
|
|
scopeObject : () => this.execStateScopeObject(scope.object),
|
|
|
|
setVariableValue :
|
|
|
|
(name, value) => this.setVariableValue(frame, scope_index,
|
2016-12-14 07:20:13 +00:00
|
|
|
name, value),
|
2016-11-16 08:34:01 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Takes a list of properties as produced by getProperties and turns them
|
|
|
|
// into an object.
|
|
|
|
propertiesToObject(props) {
|
|
|
|
const obj = {}
|
|
|
|
props.forEach((elem) => {
|
2016-11-11 12:07:41 +00:00
|
|
|
const key = elem.name;
|
|
|
|
|
|
|
|
let value;
|
|
|
|
if (elem.value) {
|
|
|
|
// Some properties (e.g. with getters/setters) don't have a value.
|
|
|
|
switch (elem.value.type) {
|
|
|
|
case "undefined": value = undefined; break;
|
|
|
|
default: value = elem.value.value; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-16 08:34:01 +00:00
|
|
|
obj[key] = value;
|
2016-11-11 12:07:41 +00:00
|
|
|
})
|
|
|
|
|
2016-11-16 08:34:01 +00:00
|
|
|
return obj;
|
2016-11-07 12:24:40 +00:00
|
|
|
}
|
|
|
|
|
2016-11-11 12:07:41 +00:00
|
|
|
getProperties(objectId) {
|
|
|
|
const {msgid, msg} = this.createMessage(
|
2016-11-21 09:28:49 +00:00
|
|
|
"Runtime.getProperties", { objectId : objectId, ownProperties: true });
|
2016-11-11 12:07:41 +00:00
|
|
|
this.sendMessage(msg);
|
|
|
|
const reply = this.takeReplyChecked(msgid);
|
|
|
|
return reply.result.result;
|
|
|
|
}
|
|
|
|
|
|
|
|
getLocalScopeDetails(frame) {
|
|
|
|
const scopes = frame.scopeChain;
|
|
|
|
for (let i = 0; i < scopes.length; i++) {
|
|
|
|
const scope = scopes[i]
|
|
|
|
if (scope.type == "local") {
|
|
|
|
return this.getProperties(scope.object.objectId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
execStateFrameLocalCount(frame) {
|
|
|
|
const scope_details = this.getLocalScopeDetails(frame);
|
|
|
|
return scope_details ? scope_details.length : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
execStateFrameLocalName(frame, index) {
|
|
|
|
const scope_details = this.getLocalScopeDetails(frame);
|
|
|
|
if (index < 0 || index >= scope_details.length) return undefined;
|
|
|
|
return scope_details[index].name;
|
|
|
|
}
|
|
|
|
|
|
|
|
execStateFrameLocalValue(frame, index) {
|
|
|
|
const scope_details = this.getLocalScopeDetails(frame);
|
|
|
|
if (index < 0 || index >= scope_details.length) return undefined;
|
|
|
|
|
|
|
|
const local = scope_details[index];
|
|
|
|
|
|
|
|
let localValue;
|
|
|
|
switch (local.value.type) {
|
|
|
|
case "undefined": localValue = undefined; break;
|
|
|
|
default: localValue = local.value.value; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return { value : () => localValue };
|
|
|
|
}
|
|
|
|
|
2016-11-22 12:43:41 +00:00
|
|
|
reconstructValue(objectId) {
|
|
|
|
const {msgid, msg} = this.createMessage(
|
|
|
|
"Runtime.getProperties", { objectId : objectId, ownProperties: true });
|
|
|
|
this.sendMessage(msg);
|
|
|
|
const reply = this.takeReplyChecked(msgid);
|
|
|
|
return Object(reply.result.internalProperties[0].value.value);
|
|
|
|
}
|
|
|
|
|
2016-11-16 08:34:01 +00:00
|
|
|
reconstructRemoteObject(obj) {
|
|
|
|
let value = obj.value;
|
2016-11-21 09:28:49 +00:00
|
|
|
let isUndefined = false;
|
|
|
|
|
|
|
|
switch (obj.type) {
|
|
|
|
case "object": {
|
|
|
|
switch (obj.subtype) {
|
|
|
|
case "error": {
|
|
|
|
const desc = obj.description;
|
|
|
|
switch (obj.className) {
|
|
|
|
case "EvalError": throw new EvalError(desc);
|
|
|
|
case "RangeError": throw new RangeError(desc);
|
|
|
|
case "ReferenceError": throw new ReferenceError(desc);
|
|
|
|
case "SyntaxError": throw new SyntaxError(desc);
|
|
|
|
case "TypeError": throw new TypeError(desc);
|
|
|
|
case "URIError": throw new URIError(desc);
|
|
|
|
default: throw new Error(desc);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "array": {
|
|
|
|
const array = [];
|
|
|
|
const props = this.propertiesToObject(
|
|
|
|
this.getProperties(obj.objectId));
|
|
|
|
for (let i = 0; i < props.length; i++) {
|
|
|
|
array[i] = props[i];
|
|
|
|
}
|
|
|
|
value = array;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "null": {
|
|
|
|
value = null;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
2016-11-22 12:43:41 +00:00
|
|
|
switch (obj.className) {
|
|
|
|
case "global":
|
|
|
|
value = Function('return this')();
|
|
|
|
break;
|
|
|
|
case "Number":
|
|
|
|
case "String":
|
|
|
|
case "Boolean":
|
|
|
|
value = this.reconstructValue(obj.objectId);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
value = this.propertiesToObject(
|
|
|
|
this.getProperties(obj.objectId));
|
|
|
|
break;
|
|
|
|
}
|
2016-11-21 09:28:49 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-11-16 08:34:01 +00:00
|
|
|
}
|
2016-11-21 09:28:49 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "undefined": {
|
|
|
|
value = undefined;
|
|
|
|
isUndefined = true;
|
|
|
|
break;
|
|
|
|
}
|
2017-01-16 06:57:54 +00:00
|
|
|
case "number": {
|
2018-05-31 18:51:49 +00:00
|
|
|
if (obj.description === "NaN") {
|
|
|
|
value = NaN;
|
2017-01-16 06:57:54 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-03-15 14:14:53 +00:00
|
|
|
case "bigint": {
|
|
|
|
assertEquals("n", obj.unserializableValue.charAt(
|
|
|
|
obj.unserializableValue.length - 1));
|
|
|
|
value = eval(obj.unserializableValue);
|
|
|
|
break;
|
|
|
|
}
|
2016-11-21 09:28:49 +00:00
|
|
|
case "string":
|
|
|
|
case "boolean": {
|
|
|
|
break;
|
|
|
|
}
|
2019-06-19 12:01:34 +00:00
|
|
|
case "function": {
|
|
|
|
value = obj.description;
|
|
|
|
}
|
2016-11-21 09:28:49 +00:00
|
|
|
default: {
|
|
|
|
break;
|
2016-11-16 08:34:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return { value : () => value,
|
2016-11-28 11:38:02 +00:00
|
|
|
isUndefined : () => isUndefined,
|
|
|
|
type : () => obj.type,
|
|
|
|
className : () => obj.className
|
2016-11-16 08:34:01 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-02-10 17:06:22 +00:00
|
|
|
evaluateOnCallFrame(frame, expr, throw_on_side_effect = false) {
|
2016-11-11 12:07:41 +00:00
|
|
|
const frameid = frame.callFrameId;
|
|
|
|
const {msgid, msg} = this.createMessage(
|
|
|
|
"Debugger.evaluateOnCallFrame",
|
|
|
|
{ callFrameId : frameid,
|
2017-02-10 17:06:22 +00:00
|
|
|
expression : expr,
|
|
|
|
throwOnSideEffect : throw_on_side_effect,
|
2016-11-11 12:07:41 +00:00
|
|
|
});
|
|
|
|
this.sendMessage(msg);
|
|
|
|
const reply = this.takeReplyChecked(msgid);
|
|
|
|
|
|
|
|
const result = reply.result.result;
|
2016-11-16 08:34:01 +00:00
|
|
|
return this.reconstructRemoteObject(result);
|
2016-11-11 12:07:41 +00:00
|
|
|
}
|
|
|
|
|
2016-12-14 07:20:13 +00:00
|
|
|
frameReceiver(frame) {
|
|
|
|
return this.reconstructRemoteObject(frame.this);
|
|
|
|
}
|
|
|
|
|
|
|
|
frameReturnValue(frame) {
|
|
|
|
return this.reconstructRemoteObject(frame.returnValue);
|
|
|
|
}
|
|
|
|
|
2016-11-17 08:43:17 +00:00
|
|
|
execStateFrameRestart(frame) {
|
|
|
|
const frameid = frame.callFrameId;
|
|
|
|
const {msgid, msg} = this.createMessage(
|
|
|
|
"Debugger.restartFrame", { callFrameId : frameid });
|
|
|
|
this.sendMessage(msg);
|
|
|
|
this.takeReplyChecked(msgid);
|
|
|
|
}
|
|
|
|
|
2016-11-07 12:24:40 +00:00
|
|
|
execStateFrame(frame) {
|
|
|
|
const scriptid = parseInt(frame.location.scriptId);
|
|
|
|
const line = frame.location.lineNumber;
|
|
|
|
const column = frame.location.columnNumber;
|
|
|
|
const loc = %ScriptLocationFromLine2(scriptid, line, column, 0);
|
|
|
|
const func = { name : () => frame.functionName };
|
2016-11-21 15:18:54 +00:00
|
|
|
const index = JSON.parse(frame.callFrameId).ordinal;
|
2016-11-16 08:34:01 +00:00
|
|
|
|
|
|
|
function allScopes() {
|
|
|
|
const scopes = [];
|
|
|
|
for (let i = 0; i < frame.scopeChain.length; i++) {
|
|
|
|
scopes.push(this.execStateScope(frame, i));
|
|
|
|
}
|
|
|
|
return scopes;
|
2016-12-14 07:20:13 +00:00
|
|
|
}
|
2016-11-16 08:34:01 +00:00
|
|
|
|
2016-11-22 13:58:50 +00:00
|
|
|
return { sourceColumn : () => column,
|
|
|
|
sourceLine : () => line + 1,
|
2016-11-16 08:34:01 +00:00
|
|
|
sourceLineText : () => loc.sourceText,
|
2016-12-14 07:20:13 +00:00
|
|
|
sourcePosition : () => loc.position,
|
2017-02-10 17:06:22 +00:00
|
|
|
evaluate : (expr, throw_on_side_effect) =>
|
|
|
|
this.evaluateOnCallFrame(frame, expr, throw_on_side_effect),
|
2016-11-07 12:24:40 +00:00
|
|
|
functionName : () => frame.functionName,
|
|
|
|
func : () => func,
|
2016-11-21 15:18:54 +00:00
|
|
|
index : () => index,
|
2016-11-11 12:07:41 +00:00
|
|
|
localCount : () => this.execStateFrameLocalCount(frame),
|
|
|
|
localName : (ix) => this.execStateFrameLocalName(frame, ix),
|
|
|
|
localValue: (ix) => this.execStateFrameLocalValue(frame, ix),
|
2016-12-14 07:20:13 +00:00
|
|
|
receiver : () => this.frameReceiver(frame),
|
2016-11-17 08:43:17 +00:00
|
|
|
restart : () => this.execStateFrameRestart(frame),
|
2016-12-14 07:20:13 +00:00
|
|
|
returnValue : () => this.frameReturnValue(frame),
|
2016-11-07 12:24:40 +00:00
|
|
|
scopeCount : () => frame.scopeChain.length,
|
2016-11-16 08:34:01 +00:00
|
|
|
scope : (index) => this.execStateScope(frame, index),
|
|
|
|
allScopes : allScopes.bind(this)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-04-05 02:01:34 +00:00
|
|
|
evaluateGlobal(expr, throw_on_side_effect) {
|
2016-12-14 07:20:13 +00:00
|
|
|
const {msgid, msg} = this.createMessage(
|
2018-04-05 02:01:34 +00:00
|
|
|
"Runtime.evaluate", { expression : expr, throwOnSideEffect: throw_on_side_effect });
|
2016-12-14 07:20:13 +00:00
|
|
|
this.sendMessage(msg);
|
|
|
|
const reply = this.takeReplyChecked(msgid);
|
|
|
|
|
|
|
|
const result = reply.result.result;
|
|
|
|
return this.reconstructRemoteObject(result);
|
|
|
|
}
|
|
|
|
|
2016-11-16 08:34:01 +00:00
|
|
|
eventDataException(params) {
|
|
|
|
switch (params.data.type) {
|
|
|
|
case "string": {
|
|
|
|
return params.data.value;
|
|
|
|
}
|
|
|
|
case "object": {
|
|
|
|
const props = this.getProperties(params.data.objectId);
|
|
|
|
return this.propertiesToObject(props);
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
eventDataScriptSource(id) {
|
|
|
|
const {msgid, msg} = this.createMessage(
|
2016-11-21 09:28:49 +00:00
|
|
|
"Debugger.getScriptSource", { scriptId : String(id) });
|
2016-11-16 08:34:01 +00:00
|
|
|
this.sendMessage(msg);
|
|
|
|
const reply = this.takeReplyChecked(msgid);
|
|
|
|
return reply.result.scriptSource;
|
|
|
|
}
|
|
|
|
|
|
|
|
eventDataScriptSetSource(id, src) {
|
|
|
|
const {msgid, msg} = this.createMessage(
|
|
|
|
"Debugger.setScriptSource", { scriptId : id, scriptSource : src });
|
|
|
|
this.sendMessage(msg);
|
|
|
|
this.takeReplyChecked(msgid);
|
|
|
|
}
|
|
|
|
|
|
|
|
eventDataScript(params) {
|
2016-11-21 09:28:49 +00:00
|
|
|
const id = parseInt(params.scriptId);
|
2016-11-16 08:34:01 +00:00
|
|
|
const name = params.url ? params.url : undefined;
|
|
|
|
|
|
|
|
return { id : () => id,
|
|
|
|
name : () => name,
|
2016-11-22 13:58:50 +00:00
|
|
|
source : () => this.eventDataScriptSource(params.scriptId),
|
2016-11-16 08:34:01 +00:00
|
|
|
setSource : (src) => this.eventDataScriptSetSource(id, src)
|
2016-11-07 12:24:40 +00:00
|
|
|
};
|
2016-10-28 08:18:07 +00:00
|
|
|
}
|
|
|
|
|
2016-10-28 07:21:25 +00:00
|
|
|
// --- Message handlers. -----------------------------------------------------
|
|
|
|
|
|
|
|
dispatchMessage(message) {
|
|
|
|
const method = message.method;
|
2016-10-28 08:18:07 +00:00
|
|
|
if (method == "Debugger.paused") {
|
|
|
|
this.handleDebuggerPaused(message);
|
|
|
|
} else if (method == "Debugger.scriptParsed") {
|
2016-10-28 07:21:25 +00:00
|
|
|
this.handleDebuggerScriptParsed(message);
|
2016-11-16 08:34:01 +00:00
|
|
|
} else if (method == "Debugger.scriptFailedToParse") {
|
|
|
|
this.handleDebuggerScriptFailedToParse(message);
|
2016-10-28 07:21:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-28 08:18:07 +00:00
|
|
|
handleDebuggerPaused(message) {
|
|
|
|
const params = message.params;
|
|
|
|
|
2016-11-08 14:53:48 +00:00
|
|
|
var debugEvent;
|
|
|
|
switch (params.reason) {
|
|
|
|
case "exception":
|
|
|
|
case "promiseRejection":
|
|
|
|
debugEvent = this.DebugEvent.Exception;
|
|
|
|
break;
|
2017-02-16 11:48:22 +00:00
|
|
|
case "OOM":
|
|
|
|
debugEvent = this.DebugEvent.OOM;
|
|
|
|
break;
|
|
|
|
case "other":
|
2016-11-08 14:53:48 +00:00
|
|
|
debugEvent = this.DebugEvent.Break;
|
|
|
|
break;
|
2017-02-16 11:48:22 +00:00
|
|
|
case "ambiguous":
|
|
|
|
case "XHR":
|
|
|
|
case "DOM":
|
|
|
|
case "EventListener":
|
|
|
|
case "assert":
|
|
|
|
case "debugCommand":
|
|
|
|
assertUnreachable();
|
|
|
|
default:
|
|
|
|
assertUnreachable();
|
2016-11-08 14:53:48 +00:00
|
|
|
}
|
|
|
|
|
2016-12-19 14:07:55 +00:00
|
|
|
if (!params.callFrames[0]) return;
|
|
|
|
|
2016-11-07 12:24:40 +00:00
|
|
|
// Skip break events in this file.
|
|
|
|
if (params.callFrames[0].location.scriptId == this.thisScriptId) return;
|
|
|
|
|
2016-10-28 08:18:07 +00:00
|
|
|
// TODO(jgruber): Arguments as needed.
|
2016-11-07 12:24:40 +00:00
|
|
|
let execState = { frames : params.callFrames,
|
|
|
|
prepareStep : this.execStatePrepareStep.bind(this),
|
2016-12-14 07:20:13 +00:00
|
|
|
evaluateGlobal :
|
2018-04-05 02:01:34 +00:00
|
|
|
(expr) => this.evaluateGlobal(expr),
|
2016-11-07 12:24:40 +00:00
|
|
|
frame : (index) => this.execStateFrame(
|
|
|
|
index ? params.callFrames[index]
|
|
|
|
: params.callFrames[0]),
|
|
|
|
frameCount : () => params.callFrames.length
|
|
|
|
};
|
2016-11-10 09:05:07 +00:00
|
|
|
|
2016-11-07 12:24:40 +00:00
|
|
|
let eventData = this.execStateFrame(params.callFrames[0]);
|
2016-11-10 09:05:07 +00:00
|
|
|
if (debugEvent == this.DebugEvent.Exception) {
|
|
|
|
eventData.uncaught = () => params.data.uncaught;
|
2016-11-16 08:34:01 +00:00
|
|
|
eventData.exception = () => this.eventDataException(params);
|
2016-11-10 09:05:07 +00:00
|
|
|
}
|
|
|
|
|
2016-11-08 14:53:48 +00:00
|
|
|
this.invokeListener(debugEvent, execState, eventData);
|
2016-10-28 08:18:07 +00:00
|
|
|
}
|
|
|
|
|
2016-10-28 07:21:25 +00:00
|
|
|
handleDebuggerScriptParsed(message) {
|
|
|
|
const params = message.params;
|
2016-11-07 12:24:40 +00:00
|
|
|
let eventData = { scriptId : params.scriptId,
|
2016-11-16 08:34:01 +00:00
|
|
|
script : () => this.eventDataScript(params),
|
2016-11-07 12:24:40 +00:00
|
|
|
eventType : this.DebugEvent.AfterCompile
|
2016-10-28 07:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(jgruber): Arguments as needed. Still completely missing exec_state,
|
|
|
|
// and eventData used to contain the script mirror instead of its id.
|
2016-10-28 08:18:07 +00:00
|
|
|
this.invokeListener(this.DebugEvent.AfterCompile, undefined, eventData,
|
|
|
|
undefined);
|
|
|
|
}
|
|
|
|
|
2016-11-16 08:34:01 +00:00
|
|
|
handleDebuggerScriptFailedToParse(message) {
|
|
|
|
const params = message.params;
|
|
|
|
let eventData = { scriptId : params.scriptId,
|
|
|
|
script : () => this.eventDataScript(params),
|
|
|
|
eventType : this.DebugEvent.CompileError
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(jgruber): Arguments as needed. Still completely missing exec_state,
|
|
|
|
// and eventData used to contain the script mirror instead of its id.
|
|
|
|
this.invokeListener(this.DebugEvent.CompileError, undefined, eventData,
|
|
|
|
undefined);
|
|
|
|
}
|
|
|
|
|
2016-10-28 08:18:07 +00:00
|
|
|
invokeListener(event, exec_state, event_data, data) {
|
|
|
|
if (this.listener) {
|
|
|
|
this.listener(event, exec_state, event_data, data);
|
|
|
|
}
|
2016-10-28 07:21:25 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-07 12:24:40 +00:00
|
|
|
|
|
|
|
// Simulate the debug object generated by --expose-debug-as debug.
|
|
|
|
var debug = { instance : undefined };
|
2016-11-08 14:53:48 +00:00
|
|
|
|
2016-11-07 12:24:40 +00:00
|
|
|
Object.defineProperty(debug, 'Debug', { get: function() {
|
|
|
|
if (!debug.instance) {
|
|
|
|
debug.instance = new DebugWrapper();
|
|
|
|
debug.instance.enable();
|
|
|
|
}
|
|
|
|
return debug.instance;
|
|
|
|
}});
|
2016-11-08 14:53:48 +00:00
|
|
|
|
|
|
|
Object.defineProperty(debug, 'ScopeType', { get: function() {
|
|
|
|
const instance = debug.Debug;
|
|
|
|
return instance.ScopeType;
|
|
|
|
}});
|