2014-06-25 09:12:07 +04:00
|
|
|
/* -*- indent-tabs-mode: nil; js-indent-level: 4 -*-
|
2011-05-11 23:47:19 +04:00
|
|
|
* vim: set ts=8 sw=4 et tw=78:
|
|
|
|
*
|
|
|
|
* jorendb - A toy command-line debugger for shell-js programs.
|
|
|
|
*
|
2012-05-21 15:12:37 +04:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
2013-06-04 23:14:17 +04:00
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
2011-05-11 23:47:19 +04:00
|
|
|
|
|
|
|
/*
|
2013-06-04 23:14:17 +04:00
|
|
|
* jorendb is a simple command-line debugger for shell-js programs. It is
|
|
|
|
* intended as a demo of the Debugger object (as there are no shell js programs
|
|
|
|
* to speak of).
|
2011-05-11 23:47:19 +04:00
|
|
|
*
|
|
|
|
* To run it: $JS -d path/to/this/file/jorendb.js
|
|
|
|
* To run some JS code under it, try:
|
|
|
|
* (jorendb) print load("my-script-to-debug.js")
|
|
|
|
* Execution will stop at debugger statements and you'll get a jorendb prompt.
|
|
|
|
*/
|
|
|
|
|
2013-06-04 23:14:17 +04:00
|
|
|
// Debugger state.
|
|
|
|
var focusedFrame = null;
|
|
|
|
var topFrame = null;
|
|
|
|
var debuggeeValues = {};
|
|
|
|
var nextDebuggeeValueIndex = 1;
|
|
|
|
var lastExc = null;
|
2016-03-25 20:52:43 +03:00
|
|
|
var todo = [];
|
|
|
|
var activeTask;
|
|
|
|
var options = { 'pretty': true,
|
2017-03-27 01:43:23 +03:00
|
|
|
'emacs': !!os.getenv('INSIDE_EMACS') };
|
2016-03-25 20:52:43 +03:00
|
|
|
var rerun = true;
|
2011-05-11 23:47:19 +04:00
|
|
|
|
2013-06-04 23:14:17 +04:00
|
|
|
// Cleanup functions to run when we next re-enter the repl.
|
|
|
|
var replCleanups = [];
|
2011-05-11 23:47:19 +04:00
|
|
|
|
2016-03-25 20:52:43 +03:00
|
|
|
// Redirect debugger printing functions to go to the original output
|
|
|
|
// destination, unaffected by any redirects done by the debugged script.
|
|
|
|
var initialOut = os.file.redirect();
|
|
|
|
var initialErr = os.file.redirectErr();
|
|
|
|
|
|
|
|
function wrap(global, name) {
|
|
|
|
var orig = global[name];
|
|
|
|
global[name] = function(...args) {
|
|
|
|
|
|
|
|
var oldOut = os.file.redirect(initialOut);
|
|
|
|
var oldErr = os.file.redirectErr(initialErr);
|
|
|
|
try {
|
|
|
|
return orig.apply(global, args);
|
|
|
|
} finally {
|
|
|
|
os.file.redirect(oldOut);
|
|
|
|
os.file.redirectErr(oldErr);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
wrap(this, 'print');
|
|
|
|
wrap(this, 'printErr');
|
|
|
|
wrap(this, 'putstr');
|
|
|
|
|
2013-06-04 23:14:17 +04:00
|
|
|
// Convert a debuggee value v to a string.
|
|
|
|
function dvToString(v) {
|
|
|
|
return (typeof v !== 'object' || v === null) ? uneval(v) : "[object " + v.class + "]";
|
|
|
|
}
|
2011-12-10 02:12:36 +04:00
|
|
|
|
2016-03-25 20:52:43 +03:00
|
|
|
function summaryObject(dv) {
|
|
|
|
var obj = {};
|
|
|
|
for (var name of dv.getOwnPropertyNames()) {
|
|
|
|
var v = dv.getOwnPropertyDescriptor(name).value;
|
|
|
|
if (v instanceof Debugger.Object) {
|
|
|
|
v = "(...)";
|
|
|
|
}
|
|
|
|
obj[name] = v;
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
function debuggeeValueToString(dv, style) {
|
2013-06-04 23:14:17 +04:00
|
|
|
var dvrepr = dvToString(dv);
|
2016-03-25 20:52:43 +03:00
|
|
|
if (!style.pretty || (typeof dv !== 'object'))
|
|
|
|
return [dvrepr, undefined];
|
|
|
|
|
|
|
|
if (dv.class == "Error") {
|
|
|
|
let errval = debuggeeGlobalWrapper.executeInGlobalWithBindings("$" + i + ".toString()", debuggeeValues);
|
|
|
|
return [dvrepr, errval.return];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (style.brief)
|
|
|
|
return [dvrepr, JSON.stringify(summaryObject(dv), null, 4)];
|
|
|
|
|
|
|
|
let str = debuggeeGlobalWrapper.executeInGlobalWithBindings("JSON.stringify(v, null, 4)", {v: dv});
|
|
|
|
if ('throw' in str) {
|
|
|
|
if (style.noerror)
|
|
|
|
return [dvrepr, undefined];
|
|
|
|
|
|
|
|
let substyle = {};
|
|
|
|
Object.assign(substyle, style);
|
|
|
|
substyle.noerror = true;
|
|
|
|
return [dvrepr, debuggeeValueToString(str.throw, substyle)];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [dvrepr, str.return];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Problem! Used to do [object Object] followed by details. Now just details?
|
|
|
|
|
|
|
|
function showDebuggeeValue(dv, style={pretty: options.pretty}) {
|
2013-06-04 23:14:17 +04:00
|
|
|
var i = nextDebuggeeValueIndex++;
|
|
|
|
debuggeeValues["$" + i] = dv;
|
2016-03-25 20:52:43 +03:00
|
|
|
let [brief, full] = debuggeeValueToString(dv, style);
|
|
|
|
print("$" + i + " = " + brief);
|
|
|
|
if (full !== undefined)
|
|
|
|
print(full);
|
2013-06-04 23:14:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Object.defineProperty(Debugger.Frame.prototype, "num", {
|
|
|
|
configurable: true,
|
|
|
|
enumerable: false,
|
|
|
|
get: function () {
|
|
|
|
var i = 0;
|
|
|
|
for (var f = topFrame; f && f !== this; f = f.older)
|
|
|
|
i++;
|
|
|
|
return f === null ? undefined : i;
|
2011-12-10 02:12:36 +04:00
|
|
|
}
|
2013-06-04 23:14:17 +04:00
|
|
|
});
|
2011-12-10 02:12:36 +04:00
|
|
|
|
2013-06-04 23:14:17 +04:00
|
|
|
Debugger.Frame.prototype.frameDescription = function frameDescription() {
|
|
|
|
if (this.type == "call")
|
|
|
|
return ((this.callee.name || '<anonymous>') +
|
|
|
|
"(" + this.arguments.map(dvToString).join(", ") + ")");
|
|
|
|
else
|
|
|
|
return this.type + " code";
|
|
|
|
}
|
2011-05-11 23:47:19 +04:00
|
|
|
|
2013-06-04 23:14:17 +04:00
|
|
|
Debugger.Frame.prototype.positionDescription = function positionDescription() {
|
|
|
|
if (this.script) {
|
2015-10-22 10:35:00 +03:00
|
|
|
var line = this.script.getOffsetLocation(this.offset).lineNumber;
|
2013-06-04 23:14:17 +04:00
|
|
|
if (this.script.url)
|
|
|
|
return this.script.url + ":" + line;
|
|
|
|
return "line " + line;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2011-05-11 23:47:19 +04:00
|
|
|
|
2016-03-25 20:52:43 +03:00
|
|
|
Debugger.Frame.prototype.location = function () {
|
|
|
|
if (this.script) {
|
|
|
|
var { lineNumber, columnNumber, isEntryPoint } = this.script.getOffsetLocation(this.offset);
|
|
|
|
if (this.script.url)
|
|
|
|
return this.script.url + ":" + lineNumber;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-06-04 23:14:17 +04:00
|
|
|
Debugger.Frame.prototype.fullDescription = function fullDescription() {
|
|
|
|
var fr = this.frameDescription();
|
|
|
|
var pos = this.positionDescription();
|
|
|
|
if (pos)
|
|
|
|
return fr + ", " + pos;
|
|
|
|
return fr;
|
|
|
|
}
|
2011-05-11 23:47:19 +04:00
|
|
|
|
2013-06-04 23:14:17 +04:00
|
|
|
Object.defineProperty(Debugger.Frame.prototype, "line", {
|
|
|
|
configurable: true,
|
|
|
|
enumerable: false,
|
|
|
|
get: function() {
|
|
|
|
if (this.script)
|
2015-10-22 10:35:00 +03:00
|
|
|
return this.script.getOffsetLocation(this.offset).lineNumber;
|
2013-06-04 23:14:17 +04:00
|
|
|
else
|
|
|
|
return null;
|
2011-05-11 23:47:19 +04:00
|
|
|
}
|
2013-06-04 23:14:17 +04:00
|
|
|
});
|
2011-05-11 23:47:19 +04:00
|
|
|
|
2013-06-04 23:14:17 +04:00
|
|
|
function callDescription(f) {
|
|
|
|
return ((f.callee.name || '<anonymous>') +
|
|
|
|
"(" + f.arguments.map(dvToString).join(", ") + ")");
|
|
|
|
}
|
2011-05-11 23:47:19 +04:00
|
|
|
|
2013-06-04 23:14:17 +04:00
|
|
|
function showFrame(f, n) {
|
|
|
|
if (f === undefined || f === null) {
|
|
|
|
f = focusedFrame;
|
|
|
|
if (f === null) {
|
|
|
|
print("No stack.");
|
|
|
|
return;
|
2011-05-11 23:47:19 +04:00
|
|
|
}
|
2013-06-04 23:14:17 +04:00
|
|
|
}
|
|
|
|
if (n === undefined) {
|
|
|
|
n = f.num;
|
|
|
|
if (n === undefined)
|
|
|
|
throw new Error("Internal error: frame not on stack");
|
|
|
|
}
|
2011-05-11 23:47:19 +04:00
|
|
|
|
2013-06-04 23:14:17 +04:00
|
|
|
print('#' + n + " " + f.fullDescription());
|
|
|
|
}
|
2011-05-11 23:47:19 +04:00
|
|
|
|
2013-06-04 23:14:17 +04:00
|
|
|
function saveExcursion(fn) {
|
|
|
|
var tf = topFrame, ff = focusedFrame;
|
|
|
|
try {
|
|
|
|
return fn();
|
|
|
|
} finally {
|
|
|
|
topFrame = tf;
|
|
|
|
focusedFrame = ff;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-25 20:52:43 +03:00
|
|
|
function parseArgs(str) {
|
|
|
|
return str.split(" ");
|
|
|
|
}
|
|
|
|
|
|
|
|
function describedRv(r, desc) {
|
|
|
|
desc = "[" + desc + "] ";
|
|
|
|
if (r === undefined) {
|
|
|
|
print(desc + "Returning undefined");
|
|
|
|
} else if (r === null) {
|
|
|
|
print(desc + "Returning null");
|
|
|
|
} else if (r.length === undefined) {
|
|
|
|
print(desc + "Returning object " + JSON.stringify(r));
|
|
|
|
} else {
|
|
|
|
print(desc + "Returning length-" + r.length + " list");
|
|
|
|
if (r.length > 0) {
|
|
|
|
print(" " + r[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rerun the program (reloading it from the file)
|
|
|
|
function runCommand(args) {
|
|
|
|
print("Restarting program");
|
|
|
|
if (args)
|
|
|
|
activeTask.scriptArgs = parseArgs(args);
|
|
|
|
rerun = true;
|
|
|
|
for (var f = topFrame; f; f = f.older) {
|
|
|
|
if (f.older) {
|
2016-04-18 23:21:33 +03:00
|
|
|
f.onPop = () => null;
|
2016-03-25 20:52:43 +03:00
|
|
|
} else {
|
2016-04-18 23:21:33 +03:00
|
|
|
f.onPop = () => ({ 'return': 0 });
|
2016-03-25 20:52:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
//return describedRv([{ 'return': 0 }], "runCommand");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-06-04 23:14:17 +04:00
|
|
|
// Evaluate an expression in the Debugger global
|
|
|
|
function evalCommand(expr) {
|
|
|
|
eval(expr);
|
|
|
|
}
|
|
|
|
|
|
|
|
function quitCommand() {
|
|
|
|
dbg.enabled = false;
|
|
|
|
quit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
function backtraceCommand() {
|
|
|
|
if (topFrame === null)
|
|
|
|
print("No stack.");
|
|
|
|
for (var i = 0, f = topFrame; f; i++, f = f.older)
|
|
|
|
showFrame(f, i);
|
|
|
|
}
|
|
|
|
|
2016-03-25 20:52:43 +03:00
|
|
|
function setCommand(rest) {
|
|
|
|
var space = rest.indexOf(' ');
|
|
|
|
if (space == -1) {
|
|
|
|
print("Invalid set <option> <value> command");
|
|
|
|
} else {
|
|
|
|
var name = rest.substr(0, space);
|
|
|
|
var value = rest.substr(space + 1);
|
|
|
|
|
|
|
|
if (name == 'args') {
|
|
|
|
activeTask.scriptArgs = parseArgs(value);
|
|
|
|
} else {
|
|
|
|
var yes = ["1", "yes", "true", "on"];
|
|
|
|
var no = ["0", "no", "false", "off"];
|
|
|
|
|
2018-02-01 22:45:22 +03:00
|
|
|
if (yes.includes(value))
|
2016-03-25 20:52:43 +03:00
|
|
|
options[name] = true;
|
2018-02-01 22:45:22 +03:00
|
|
|
else if (no.includes(value))
|
2016-03-25 20:52:43 +03:00
|
|
|
options[name] = false;
|
|
|
|
else
|
|
|
|
options[name] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function split_print_options(s, style) {
|
|
|
|
var m = /^\/(\w+)/.exec(s);
|
|
|
|
if (!m)
|
|
|
|
return [ s, style ];
|
2018-02-01 22:45:22 +03:00
|
|
|
if (m[1].includes("p"))
|
2016-03-25 20:52:43 +03:00
|
|
|
style.pretty = true;
|
2018-02-01 22:45:22 +03:00
|
|
|
if (m[1].includes("b"))
|
2016-03-25 20:52:43 +03:00
|
|
|
style.brief = true;
|
|
|
|
return [ s.substr(m[0].length).trimLeft(), style ];
|
|
|
|
}
|
|
|
|
|
|
|
|
function doPrint(expr, style) {
|
2013-06-04 23:14:17 +04:00
|
|
|
// This is the real deal.
|
|
|
|
var cv = saveExcursion(
|
|
|
|
() => focusedFrame == null
|
2016-03-25 20:52:43 +03:00
|
|
|
? debuggeeGlobalWrapper.executeInGlobalWithBindings(expr, debuggeeValues)
|
|
|
|
: focusedFrame.evalWithBindings(expr, debuggeeValues));
|
2013-06-04 23:14:17 +04:00
|
|
|
if (cv === null) {
|
|
|
|
if (!dbg.enabled)
|
|
|
|
return [cv];
|
|
|
|
print("Debuggee died.");
|
|
|
|
} else if ('return' in cv) {
|
|
|
|
if (!dbg.enabled)
|
2011-05-11 23:47:19 +04:00
|
|
|
return [undefined];
|
2016-03-25 20:52:43 +03:00
|
|
|
showDebuggeeValue(cv.return, style);
|
2013-06-04 23:14:17 +04:00
|
|
|
} else {
|
|
|
|
if (!dbg.enabled)
|
|
|
|
return [cv];
|
|
|
|
print("Exception caught. (To rethrow it, type 'throw'.)");
|
|
|
|
lastExc = cv.throw;
|
2016-03-25 20:52:43 +03:00
|
|
|
showDebuggeeValue(lastExc, style);
|
2013-06-04 23:14:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-25 20:52:43 +03:00
|
|
|
function printCommand(rest) {
|
|
|
|
var [expr, style] = split_print_options(rest, {pretty: options.pretty});
|
|
|
|
return doPrint(expr, style);
|
|
|
|
}
|
|
|
|
|
|
|
|
function keysCommand(rest) { return doPrint("Object.keys(" + rest + ")"); }
|
|
|
|
|
2013-06-04 23:14:17 +04:00
|
|
|
function detachCommand() {
|
|
|
|
dbg.enabled = false;
|
|
|
|
return [undefined];
|
|
|
|
}
|
|
|
|
|
2016-03-25 20:52:43 +03:00
|
|
|
function continueCommand(rest) {
|
2013-06-04 23:14:17 +04:00
|
|
|
if (focusedFrame === null) {
|
|
|
|
print("No stack.");
|
|
|
|
return;
|
|
|
|
}
|
2016-03-25 20:52:43 +03:00
|
|
|
|
|
|
|
var match = rest.match(/^(\d+)$/);
|
|
|
|
if (match) {
|
|
|
|
return doStepOrNext({upto:true, stopLine:match[1]});
|
|
|
|
}
|
|
|
|
|
2013-06-04 23:14:17 +04:00
|
|
|
return [undefined];
|
|
|
|
}
|
|
|
|
|
|
|
|
function throwCommand(rest) {
|
|
|
|
var v;
|
|
|
|
if (focusedFrame !== topFrame) {
|
|
|
|
print("To throw, you must select the newest frame (use 'frame 0').");
|
|
|
|
return;
|
|
|
|
} else if (focusedFrame === null) {
|
|
|
|
print("No stack.");
|
|
|
|
return;
|
|
|
|
} else if (rest === '') {
|
|
|
|
return [{throw: lastExc}];
|
|
|
|
} else {
|
|
|
|
var cv = saveExcursion(function () { return focusedFrame.eval(rest); });
|
|
|
|
if (cv === null) {
|
|
|
|
if (!dbg.enabled)
|
|
|
|
return [cv];
|
|
|
|
print("Debuggee died while determining what to throw. Stopped.");
|
|
|
|
} else if ('return' in cv) {
|
|
|
|
return [{throw: cv.return}];
|
|
|
|
} else {
|
|
|
|
if (!dbg.enabled)
|
|
|
|
return [cv];
|
|
|
|
print("Exception determining what to throw. Stopped.");
|
|
|
|
showDebuggeeValue(cv.throw);
|
2011-05-11 23:47:19 +04:00
|
|
|
}
|
2013-06-04 23:14:17 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2011-05-11 23:47:19 +04:00
|
|
|
|
2013-06-04 23:14:17 +04:00
|
|
|
function frameCommand(rest) {
|
|
|
|
var n, f;
|
|
|
|
if (rest.match(/[0-9]+/)) {
|
|
|
|
n = +rest;
|
|
|
|
f = topFrame;
|
|
|
|
if (f === null) {
|
|
|
|
print("No stack.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (var i = 0; i < n && f; i++) {
|
|
|
|
if (!f.older) {
|
|
|
|
print("There is no frame " + rest + ".");
|
2011-05-11 23:47:19 +04:00
|
|
|
return;
|
|
|
|
}
|
2013-06-04 23:14:17 +04:00
|
|
|
f.older.younger = f;
|
|
|
|
f = f.older;
|
2011-05-11 23:47:19 +04:00
|
|
|
}
|
2013-06-04 23:14:17 +04:00
|
|
|
focusedFrame = f;
|
2016-03-25 20:52:43 +03:00
|
|
|
updateLocation(focusedFrame);
|
2013-06-04 23:14:17 +04:00
|
|
|
showFrame(f, n);
|
2016-03-25 20:52:43 +03:00
|
|
|
} else if (rest === '') {
|
|
|
|
if (topFrame === null) {
|
2013-06-04 23:14:17 +04:00
|
|
|
print("No stack.");
|
2016-03-25 20:52:43 +03:00
|
|
|
} else {
|
|
|
|
updateLocation(focusedFrame);
|
2013-06-04 23:14:17 +04:00
|
|
|
showFrame();
|
2016-03-25 20:52:43 +03:00
|
|
|
}
|
2013-06-04 23:14:17 +04:00
|
|
|
} else {
|
|
|
|
print("do what now?");
|
|
|
|
}
|
|
|
|
}
|
2011-05-11 23:47:19 +04:00
|
|
|
|
2013-06-04 23:14:17 +04:00
|
|
|
function upCommand() {
|
|
|
|
if (focusedFrame === null)
|
|
|
|
print("No stack.");
|
|
|
|
else if (focusedFrame.older === null)
|
|
|
|
print("Initial frame selected; you cannot go up.");
|
|
|
|
else {
|
|
|
|
focusedFrame.older.younger = focusedFrame;
|
|
|
|
focusedFrame = focusedFrame.older;
|
2016-03-25 20:52:43 +03:00
|
|
|
updateLocation(focusedFrame);
|
2013-06-04 23:14:17 +04:00
|
|
|
showFrame();
|
|
|
|
}
|
|
|
|
}
|
2011-05-11 23:47:19 +04:00
|
|
|
|
2013-06-04 23:14:17 +04:00
|
|
|
function downCommand() {
|
|
|
|
if (focusedFrame === null)
|
|
|
|
print("No stack.");
|
|
|
|
else if (!focusedFrame.younger)
|
|
|
|
print("Youngest frame selected; you cannot go down.");
|
|
|
|
else {
|
|
|
|
focusedFrame = focusedFrame.younger;
|
2016-03-25 20:52:43 +03:00
|
|
|
updateLocation(focusedFrame);
|
2013-06-04 23:14:17 +04:00
|
|
|
showFrame();
|
|
|
|
}
|
|
|
|
}
|
2011-05-11 23:47:19 +04:00
|
|
|
|
2013-06-04 23:14:17 +04:00
|
|
|
function forcereturnCommand(rest) {
|
|
|
|
var v;
|
|
|
|
var f = focusedFrame;
|
|
|
|
if (f !== topFrame) {
|
|
|
|
print("To forcereturn, you must select the newest frame (use 'frame 0').");
|
|
|
|
} else if (f === null) {
|
|
|
|
print("Nothing on the stack.");
|
|
|
|
} else if (rest === '') {
|
|
|
|
return [{return: undefined}];
|
|
|
|
} else {
|
|
|
|
var cv = saveExcursion(function () { return f.eval(rest); });
|
|
|
|
if (cv === null) {
|
|
|
|
if (!dbg.enabled)
|
|
|
|
return [cv];
|
|
|
|
print("Debuggee died while determining what to forcereturn. Stopped.");
|
|
|
|
} else if ('return' in cv) {
|
|
|
|
return [{return: cv.return}];
|
|
|
|
} else {
|
|
|
|
if (!dbg.enabled)
|
|
|
|
return [cv];
|
|
|
|
print("Error determining what to forcereturn. Stopped.");
|
|
|
|
showDebuggeeValue(cv.throw);
|
2011-05-11 23:47:19 +04:00
|
|
|
}
|
2013-06-04 23:14:17 +04:00
|
|
|
}
|
|
|
|
}
|
2011-05-11 23:47:19 +04:00
|
|
|
|
2013-06-04 23:14:17 +04:00
|
|
|
function printPop(f, c) {
|
|
|
|
var fdesc = f.fullDescription();
|
|
|
|
if (c.return) {
|
|
|
|
print("frame returning (still selected): " + fdesc);
|
2016-03-25 20:52:43 +03:00
|
|
|
showDebuggeeValue(c.return, {brief: true});
|
2013-06-04 23:14:17 +04:00
|
|
|
} else if (c.throw) {
|
|
|
|
print("frame threw exception: " + fdesc);
|
|
|
|
showDebuggeeValue(c.throw);
|
|
|
|
print("(To rethrow it, type 'throw'.)");
|
|
|
|
lastExc = c.throw;
|
|
|
|
} else {
|
|
|
|
print("frame was terminated: " + fdesc);
|
|
|
|
}
|
|
|
|
}
|
2011-05-11 23:47:19 +04:00
|
|
|
|
2013-06-04 23:14:17 +04:00
|
|
|
// Set |prop| on |obj| to |value|, but then restore its current value
|
|
|
|
// when we next enter the repl.
|
|
|
|
function setUntilRepl(obj, prop, value) {
|
|
|
|
var saved = obj[prop];
|
|
|
|
obj[prop] = value;
|
|
|
|
replCleanups.push(function () { obj[prop] = saved; });
|
|
|
|
}
|
2011-05-11 23:47:19 +04:00
|
|
|
|
2016-03-25 20:52:43 +03:00
|
|
|
function updateLocation(frame) {
|
|
|
|
if (options.emacs) {
|
|
|
|
var loc = frame.location();
|
|
|
|
if (loc)
|
|
|
|
print("\032\032" + loc + ":1");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-04 23:14:17 +04:00
|
|
|
function doStepOrNext(kind) {
|
|
|
|
var startFrame = topFrame;
|
|
|
|
var startLine = startFrame.line;
|
2016-03-25 20:52:43 +03:00
|
|
|
// print("stepping in: " + startFrame.fullDescription());
|
|
|
|
// print("starting line: " + uneval(startLine));
|
2013-06-04 23:14:17 +04:00
|
|
|
|
|
|
|
function stepPopped(completion) {
|
|
|
|
// Note that we're popping this frame; we need to watch for
|
|
|
|
// subsequent step events on its caller.
|
|
|
|
this.reportedPop = true;
|
|
|
|
printPop(this, completion);
|
|
|
|
topFrame = focusedFrame = this;
|
2016-03-25 20:52:43 +03:00
|
|
|
if (kind.finish) {
|
|
|
|
// We want to continue, but this frame is going to be invalid as
|
|
|
|
// soon as this function returns, which will make the replCleanups
|
|
|
|
// assert when it tries to access the dead frame's 'onPop'
|
|
|
|
// property. So clear it out now while the frame is still valid,
|
|
|
|
// and trade it for an 'onStep' callback on the frame we're popping to.
|
|
|
|
preReplCleanups();
|
|
|
|
setUntilRepl(this.older, 'onStep', stepStepped);
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
updateLocation(this);
|
2013-06-04 23:14:17 +04:00
|
|
|
return repl();
|
|
|
|
}
|
2011-05-11 23:47:19 +04:00
|
|
|
|
2013-06-04 23:14:17 +04:00
|
|
|
function stepEntered(newFrame) {
|
|
|
|
print("entered frame: " + newFrame.fullDescription());
|
2016-03-25 20:52:43 +03:00
|
|
|
updateLocation(newFrame);
|
2013-06-04 23:14:17 +04:00
|
|
|
topFrame = focusedFrame = newFrame;
|
|
|
|
return repl();
|
|
|
|
}
|
|
|
|
|
|
|
|
function stepStepped() {
|
2016-03-25 20:52:43 +03:00
|
|
|
// print("stepStepped: " + this.fullDescription());
|
|
|
|
updateLocation(this);
|
|
|
|
var stop = false;
|
|
|
|
|
|
|
|
if (kind.finish) {
|
|
|
|
// 'finish' set a one-time onStep for stopping at the frame it
|
|
|
|
// wants to return to
|
|
|
|
stop = true;
|
|
|
|
} else if (kind.upto) {
|
|
|
|
// running until a given line is reached
|
|
|
|
if (this.line == kind.stopLine)
|
|
|
|
stop = true;
|
|
|
|
} else {
|
|
|
|
// regular step; stop whenever the line number changes
|
|
|
|
if ((this.line != startLine) || (this != startFrame))
|
|
|
|
stop = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stop) {
|
2013-06-04 23:14:17 +04:00
|
|
|
topFrame = focusedFrame = this;
|
|
|
|
if (focusedFrame != startFrame)
|
|
|
|
print(focusedFrame.fullDescription());
|
|
|
|
return repl();
|
2011-05-11 23:47:19 +04:00
|
|
|
}
|
|
|
|
|
2013-06-04 23:14:17 +04:00
|
|
|
// Otherwise, let execution continue.
|
|
|
|
return undefined;
|
|
|
|
}
|
2011-05-11 23:47:19 +04:00
|
|
|
|
2013-06-04 23:14:17 +04:00
|
|
|
if (kind.step)
|
|
|
|
setUntilRepl(dbg, 'onEnterFrame', stepEntered);
|
2011-05-11 23:47:19 +04:00
|
|
|
|
2013-06-04 23:14:17 +04:00
|
|
|
// If we're stepping after an onPop, watch for steps and pops in the
|
|
|
|
// next-older frame; this one is done.
|
|
|
|
var stepFrame = startFrame.reportedPop ? startFrame.older : startFrame;
|
|
|
|
if (!stepFrame || !stepFrame.script)
|
|
|
|
stepFrame = null;
|
|
|
|
if (stepFrame) {
|
2016-03-25 20:52:43 +03:00
|
|
|
if (!kind.finish)
|
|
|
|
setUntilRepl(stepFrame, 'onStep', stepStepped);
|
2013-06-04 23:14:17 +04:00
|
|
|
setUntilRepl(stepFrame, 'onPop', stepPopped);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let the program continue!
|
|
|
|
return [undefined];
|
|
|
|
}
|
|
|
|
|
|
|
|
function stepCommand() { return doStepOrNext({step:true}); }
|
|
|
|
function nextCommand() { return doStepOrNext({next:true}); }
|
2016-03-25 20:52:43 +03:00
|
|
|
function finishCommand() { return doStepOrNext({finish:true}); }
|
|
|
|
|
|
|
|
// FIXME: DOES NOT WORK YET
|
|
|
|
function breakpointCommand(where) {
|
|
|
|
print("Sorry, breakpoints don't work yet.");
|
|
|
|
var script = focusedFrame.script;
|
|
|
|
var offsets = script.getLineOffsets(Number(where));
|
|
|
|
if (offsets.length == 0) {
|
|
|
|
print("Unable to break at line " + where);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (var offset of offsets) {
|
|
|
|
script.setBreakpoint(offset, { hit: handleBreakpoint });
|
|
|
|
}
|
|
|
|
print("Set breakpoint in " + script.url + ":" + script.startLine + " at line " + where + ", " + offsets.length);
|
|
|
|
}
|
2013-06-04 23:14:17 +04:00
|
|
|
|
|
|
|
// Build the table of commands.
|
|
|
|
var commands = {};
|
|
|
|
var commandArray = [
|
|
|
|
backtraceCommand, "bt", "where",
|
2016-03-25 20:52:43 +03:00
|
|
|
breakpointCommand, "b", "break",
|
2013-06-04 23:14:17 +04:00
|
|
|
continueCommand, "c",
|
|
|
|
detachCommand,
|
|
|
|
downCommand, "d",
|
2016-03-25 20:52:43 +03:00
|
|
|
evalCommand, "!",
|
2013-06-04 23:14:17 +04:00
|
|
|
forcereturnCommand,
|
|
|
|
frameCommand, "f",
|
2016-03-25 20:52:43 +03:00
|
|
|
finishCommand, "fin",
|
2013-06-04 23:14:17 +04:00
|
|
|
nextCommand, "n",
|
|
|
|
printCommand, "p",
|
2016-03-25 20:52:43 +03:00
|
|
|
keysCommand, "k",
|
2013-06-04 23:14:17 +04:00
|
|
|
quitCommand, "q",
|
2016-03-25 20:52:43 +03:00
|
|
|
runCommand, "run",
|
2013-06-04 23:14:17 +04:00
|
|
|
stepCommand, "s",
|
2016-03-25 20:52:43 +03:00
|
|
|
setCommand,
|
2013-06-04 23:14:17 +04:00
|
|
|
throwCommand, "t",
|
|
|
|
upCommand, "u",
|
|
|
|
helpCommand, "h",
|
2016-03-25 20:52:43 +03:00
|
|
|
];
|
|
|
|
var currentCmd = null;
|
2013-06-04 23:14:17 +04:00
|
|
|
for (var i = 0; i < commandArray.length; i++) {
|
|
|
|
var cmd = commandArray[i];
|
|
|
|
if (typeof cmd === "string")
|
2016-03-25 20:52:43 +03:00
|
|
|
commands[cmd] = currentCmd;
|
2013-06-04 23:14:17 +04:00
|
|
|
else
|
2016-03-25 20:52:43 +03:00
|
|
|
currentCmd = commands[cmd.name.replace(/Command$/, '')] = cmd;
|
2013-06-04 23:14:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
function helpCommand(rest) {
|
|
|
|
print("Available commands:");
|
|
|
|
var printcmd = function(group) {
|
|
|
|
print(" " + group.join(", "));
|
|
|
|
}
|
2011-05-11 23:47:19 +04:00
|
|
|
|
2013-06-04 23:14:17 +04:00
|
|
|
var group = [];
|
|
|
|
for (var cmd of commandArray) {
|
|
|
|
if (typeof cmd === "string") {
|
|
|
|
group.push(cmd);
|
|
|
|
} else {
|
|
|
|
if (group.length) printcmd(group);
|
|
|
|
group = [ cmd.name.replace(/Command$/, '') ];
|
2011-05-11 23:47:19 +04:00
|
|
|
}
|
2013-06-04 23:14:17 +04:00
|
|
|
}
|
|
|
|
printcmd(group);
|
|
|
|
}
|
2011-05-11 23:47:19 +04:00
|
|
|
|
2013-06-04 23:14:17 +04:00
|
|
|
// Break cmd into two parts: its first word and everything else. If it begins
|
2016-03-25 20:52:43 +03:00
|
|
|
// with punctuation, treat that as a separate word. The first word is
|
|
|
|
// terminated with whitespace or the '/' character. So:
|
|
|
|
//
|
|
|
|
// print x => ['print', 'x']
|
|
|
|
// print => ['print', '']
|
|
|
|
// !print x => ['!', 'print x']
|
|
|
|
// ?!wtf!? => ['?', '!wtf!?']
|
|
|
|
// print/b x => ['print', '/b x']
|
|
|
|
//
|
2013-06-04 23:14:17 +04:00
|
|
|
function breakcmd(cmd) {
|
|
|
|
cmd = cmd.trimLeft();
|
2018-02-01 22:45:22 +03:00
|
|
|
if ("!@#$%^&*_+=/?.,<>:;'\"".includes(cmd.substr(0, 1)))
|
2013-06-04 23:14:17 +04:00
|
|
|
return [cmd.substr(0, 1), cmd.substr(1).trimLeft()];
|
2016-03-25 20:52:43 +03:00
|
|
|
var m = /\s+|(?=\/)/.exec(cmd);
|
2013-06-04 23:14:17 +04:00
|
|
|
if (m === null)
|
|
|
|
return [cmd, ''];
|
2016-03-25 20:52:43 +03:00
|
|
|
return [cmd.slice(0, m.index), cmd.slice(m.index + m[0].length)];
|
2013-06-04 23:14:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
function runcmd(cmd) {
|
|
|
|
var pieces = breakcmd(cmd);
|
|
|
|
if (pieces[0] === "")
|
|
|
|
return undefined;
|
|
|
|
|
|
|
|
var first = pieces[0], rest = pieces[1];
|
|
|
|
if (!commands.hasOwnProperty(first)) {
|
|
|
|
print("unrecognized command '" + first + "'");
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
var cmd = commands[first];
|
|
|
|
if (cmd.length === 0 && rest !== '') {
|
|
|
|
print("this command cannot take an argument");
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
return cmd(rest);
|
|
|
|
}
|
|
|
|
|
2016-03-25 20:52:43 +03:00
|
|
|
function preReplCleanups() {
|
2013-06-04 23:14:17 +04:00
|
|
|
while (replCleanups.length > 0)
|
|
|
|
replCleanups.pop()();
|
2016-03-25 20:52:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var prevcmd = undefined;
|
|
|
|
function repl() {
|
|
|
|
preReplCleanups();
|
2013-06-04 23:14:17 +04:00
|
|
|
|
|
|
|
var cmd;
|
|
|
|
for (;;) {
|
|
|
|
putstr("\n" + prompt);
|
|
|
|
cmd = readline();
|
|
|
|
if (cmd === null)
|
|
|
|
return null;
|
2016-03-25 20:52:43 +03:00
|
|
|
else if (cmd === "")
|
|
|
|
cmd = prevcmd;
|
2013-06-04 23:14:17 +04:00
|
|
|
|
|
|
|
try {
|
2016-03-25 20:52:43 +03:00
|
|
|
prevcmd = cmd;
|
2013-06-04 23:14:17 +04:00
|
|
|
var result = runcmd(cmd);
|
|
|
|
if (result === undefined)
|
2016-03-25 20:52:43 +03:00
|
|
|
; // do nothing, return to prompt
|
2013-06-04 23:14:17 +04:00
|
|
|
else if (Array.isArray(result))
|
|
|
|
return result[0];
|
2016-03-25 20:52:43 +03:00
|
|
|
else if (result === null)
|
|
|
|
return null;
|
2013-06-04 23:14:17 +04:00
|
|
|
else
|
2016-03-25 20:52:43 +03:00
|
|
|
throw new Error("Internal error: result of runcmd wasn't array or undefined: " + result);
|
2013-06-04 23:14:17 +04:00
|
|
|
} catch (exc) {
|
|
|
|
print("*** Internal error: exception in the debugger code.");
|
|
|
|
print(" " + exc);
|
|
|
|
print(exc.stack);
|
2011-05-11 23:47:19 +04:00
|
|
|
}
|
2013-06-04 23:14:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var dbg = new Debugger();
|
|
|
|
dbg.onDebuggerStatement = function (frame) {
|
|
|
|
return saveExcursion(function () {
|
|
|
|
topFrame = focusedFrame = frame;
|
|
|
|
print("'debugger' statement hit.");
|
|
|
|
showFrame();
|
2016-03-25 20:52:43 +03:00
|
|
|
updateLocation(focusedFrame);
|
|
|
|
backtrace();
|
|
|
|
return describedRv(repl(), "debugger.saveExc");
|
2013-06-04 23:14:17 +04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
dbg.onThrow = function (frame, exc) {
|
|
|
|
return saveExcursion(function () {
|
|
|
|
topFrame = focusedFrame = frame;
|
|
|
|
print("Unwinding due to exception. (Type 'c' to continue unwinding.)");
|
|
|
|
showFrame();
|
|
|
|
print("Exception value is:");
|
|
|
|
showDebuggeeValue(exc);
|
|
|
|
return repl();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-03-25 20:52:43 +03:00
|
|
|
function handleBreakpoint (frame) {
|
|
|
|
print("Breakpoint hit!");
|
|
|
|
return saveExcursion(() => {
|
|
|
|
topFrame = focusedFrame = frame;
|
|
|
|
print("breakpoint hit.");
|
|
|
|
showFrame();
|
|
|
|
updateLocation(focusedFrame);
|
|
|
|
return repl();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-06-04 23:14:17 +04:00
|
|
|
// The depth of jorendb nesting.
|
|
|
|
var jorendbDepth;
|
|
|
|
if (typeof jorendbDepth == 'undefined') jorendbDepth = 0;
|
|
|
|
|
|
|
|
var debuggeeGlobal = newGlobal("new-compartment");
|
|
|
|
debuggeeGlobal.jorendbDepth = jorendbDepth + 1;
|
|
|
|
var debuggeeGlobalWrapper = dbg.addDebuggee(debuggeeGlobal);
|
|
|
|
|
|
|
|
print("jorendb version -0.0");
|
|
|
|
prompt = '(' + Array(jorendbDepth+1).join('meta-') + 'jorendb) ';
|
|
|
|
|
2016-03-25 20:52:43 +03:00
|
|
|
var args = scriptArgs.slice(0);
|
|
|
|
print("INITIAL ARGS: " + args);
|
|
|
|
|
|
|
|
// Find the script to run and its arguments. The script may have been given as
|
|
|
|
// a plain script name, in which case all remaining arguments belong to the
|
|
|
|
// script. Or there may have been any number of arguments to the JS shell,
|
|
|
|
// followed by -f scriptName, followed by additional arguments to the JS shell,
|
|
|
|
// followed by the script arguments. There may be multiple -e or -f options in
|
|
|
|
// the JS shell arguments, and we want to treat each one as a debuggable
|
|
|
|
// script.
|
|
|
|
//
|
|
|
|
// The difficulty is that the JS shell has a mixture of
|
|
|
|
//
|
|
|
|
// --boolean
|
|
|
|
//
|
|
|
|
// and
|
|
|
|
//
|
|
|
|
// --value VAL
|
|
|
|
//
|
|
|
|
// parameters, and there's no way to know whether --option takes an argument or
|
|
|
|
// not. We will assume that VAL will never end in .js, or rather that the first
|
|
|
|
// argument that does not start with "-" but does end in ".js" is the name of
|
|
|
|
// the script.
|
|
|
|
//
|
|
|
|
// If you need to pass other options and not have them given to the script,
|
|
|
|
// pass them before the -f jorendb.js argument. Thus, the safe ways to pass
|
|
|
|
// arguments are:
|
|
|
|
//
|
|
|
|
// js [JS shell options] -f jorendb.js (-e SCRIPT | -f FILE)+ -- [script args]
|
|
|
|
// js [JS shell options] -f jorendb.js (-e SCRIPT | -f FILE)* script.js [script args]
|
|
|
|
//
|
|
|
|
// Additionally, if you want to run a script that is *NOT* debugged, put it in
|
|
|
|
// as part of the leading [JS shell options].
|
|
|
|
|
|
|
|
|
|
|
|
// Compute actualScriptArgs by finding the script to be run and grabbing every
|
|
|
|
// non-script argument. The script may be given by -f scriptname or just plain
|
|
|
|
// scriptname. In the latter case, it will be in the global variable
|
|
|
|
// 'scriptPath' (and NOT in scriptArgs.)
|
|
|
|
var actualScriptArgs = [];
|
|
|
|
var scriptSeen;
|
|
|
|
|
|
|
|
if (scriptPath !== undefined) {
|
|
|
|
todo.push({
|
|
|
|
'action': 'load',
|
|
|
|
'script': scriptPath,
|
|
|
|
});
|
|
|
|
scriptSeen = true;
|
|
|
|
}
|
|
|
|
|
2013-06-04 23:14:17 +04:00
|
|
|
while(args.length > 0) {
|
|
|
|
var arg = args.shift();
|
2016-03-25 20:52:43 +03:00
|
|
|
print("arg: " + arg);
|
|
|
|
if (arg == '-e') {
|
|
|
|
print(" eval");
|
|
|
|
todo.push({
|
|
|
|
'action': 'eval',
|
|
|
|
'code': args.shift()
|
|
|
|
});
|
|
|
|
} else if (arg == '-f') {
|
|
|
|
var script = args.shift();
|
|
|
|
print(" load -f " + script);
|
|
|
|
scriptSeen = true;
|
|
|
|
todo.push({
|
|
|
|
'action': 'load',
|
|
|
|
'script': script,
|
|
|
|
});
|
|
|
|
} else if (arg.indexOf("-") == 0) {
|
|
|
|
if (arg == '--') {
|
|
|
|
print(" pass remaining args to script");
|
|
|
|
actualScriptArgs.push(...args);
|
|
|
|
break;
|
|
|
|
} else if ((args.length > 0) && (args[0].indexOf(".js") + 3 == args[0].length)) {
|
|
|
|
// Ends with .js, assume we are looking at --boolean script.js
|
|
|
|
print(" load script.js after --boolean");
|
|
|
|
todo.push({
|
|
|
|
'action': 'load',
|
|
|
|
'script': args.shift(),
|
|
|
|
});
|
|
|
|
scriptSeen = true;
|
|
|
|
} else {
|
|
|
|
// Does not end with .js, assume we are looking at JS shell arg
|
|
|
|
// --value VAL
|
|
|
|
print(" ignore");
|
|
|
|
args.shift();
|
|
|
|
}
|
2013-06-04 23:14:17 +04:00
|
|
|
} else {
|
2016-03-25 20:52:43 +03:00
|
|
|
if (!scriptSeen) {
|
|
|
|
print(" load general");
|
2016-04-18 23:21:33 +03:00
|
|
|
actualScriptArgs.push(...args);
|
2016-03-25 20:52:43 +03:00
|
|
|
todo.push({
|
|
|
|
'action': 'load',
|
|
|
|
'script': arg,
|
|
|
|
});
|
2016-04-18 23:21:33 +03:00
|
|
|
break;
|
2016-03-25 20:52:43 +03:00
|
|
|
} else {
|
|
|
|
print(" arg " + arg);
|
|
|
|
actualScriptArgs.push(arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
print("jorendb: scriptPath = " + scriptPath);
|
|
|
|
print("jorendb: scriptArgs = " + scriptArgs);
|
|
|
|
print("jorendb: actualScriptArgs = " + actualScriptArgs);
|
|
|
|
|
|
|
|
for (var task of todo) {
|
|
|
|
task['scriptArgs'] = actualScriptArgs;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If nothing to run, just drop into a repl
|
|
|
|
if (todo.length == 0) {
|
|
|
|
todo.push({ 'action': 'repl' });
|
|
|
|
}
|
|
|
|
|
|
|
|
while (rerun) {
|
|
|
|
print("Top of run loop");
|
|
|
|
rerun = false;
|
|
|
|
for (var task of todo) {
|
|
|
|
activeTask = task;
|
|
|
|
if (task.action == 'eval') {
|
|
|
|
debuggeeGlobal.eval(task.code);
|
|
|
|
} else if (task.action == 'load') {
|
|
|
|
debuggeeGlobal['scriptArgs'] = task.scriptArgs;
|
|
|
|
debuggeeGlobal['scriptPath'] = task.script;
|
|
|
|
print("Loading JavaScript file " + task.script);
|
|
|
|
debuggeeGlobal.evaluate(read(task.script), { 'fileName': task.script, 'lineNumber': 1 });
|
|
|
|
} else if (task.action == 'repl') {
|
|
|
|
repl();
|
|
|
|
}
|
|
|
|
if (rerun)
|
|
|
|
break;
|
2013-06-04 23:14:17 +04:00
|
|
|
}
|
|
|
|
}
|
2011-05-11 23:47:19 +04:00
|
|
|
|
2016-03-25 20:52:43 +03:00
|
|
|
quit(0);
|