DbgController.js 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. var TRACE_NONE = 0x0000;
  6. var TRACE_COMMANDS = 0x001;
  7. var TRACE_DIAG_OUTPUT = 0x002;
  8. var TRACE_INTERNAL_FUNCTIONS = 0x004;
  9. var TRACE_DEBUG_EVENTS = 0x008;
  10. var TRACE_ALL = TRACE_COMMANDS | TRACE_DIAG_OUTPUT | TRACE_INTERNAL_FUNCTIONS | TRACE_DEBUG_EVENTS;
  11. // Have all JsDiag* functions installed on it by Debugger.cpp
  12. var hostDebugObject = {};
  13. var controllerObj = (function () {
  14. var _commandList = [];
  15. var _commandCompletions = [];
  16. var _wasResumed = false;
  17. var _currentStackFrameIndex = 0;
  18. var _trace = TRACE_NONE;
  19. var _eventLog = [];
  20. var _baseline = undefined;
  21. var _exceptionCommands = undefined;
  22. var _onasyncbreakCommands = undefined;
  23. var _inspectMaxStringLength = 16;
  24. function internalPrint(str) {
  25. WScript.Echo(str);
  26. }
  27. function isTracing(traceFlag) {
  28. return _trace & traceFlag;
  29. }
  30. function internalTrace(traceFlag, ...varArgs) {
  31. if (isTracing(traceFlag)) {
  32. var str = "";
  33. varArgs.map(function (element) {
  34. str += (typeof element != "string") ? JSON.stringify(element, undefined, " ") : element;
  35. });
  36. internalPrint(str);
  37. }
  38. }
  39. function printError(str) {
  40. internalPrint("Error: " + str);
  41. }
  42. function callHostFunction(fn) {
  43. var result = fn.apply(undefined, [].slice.call(arguments, 1));
  44. var obj = {};
  45. obj[fn.name] = result;
  46. internalTrace(TRACE_DIAG_OUTPUT, obj);
  47. return result;
  48. }
  49. filterLog = (function () {
  50. var parentFilter = { "this": 1, "locals": 1, "globals": 1 };
  51. var filter = {};
  52. // Discard all known globals to reduce baseline noise.
  53. [
  54. "#__proto__",
  55. "Array",
  56. "ArrayBuffer",
  57. "Atomics",
  58. "Boolean",
  59. "chWriteTraceEvent",
  60. "CollectGarbage",
  61. "console",
  62. "DataView",
  63. "Date",
  64. "decodeURI",
  65. "decodeURIComponent",
  66. "encodeURI",
  67. "encodeURIComponent",
  68. "Error",
  69. "escape",
  70. "eval",
  71. "EvalError",
  72. "Float32Array",
  73. "Float64Array",
  74. "Function",
  75. "Infinity",
  76. "Int16Array",
  77. "Int32Array",
  78. "Int8Array",
  79. "Intl",
  80. "isFinite",
  81. "isNaN",
  82. "JSON",
  83. "Map",
  84. "Math",
  85. "NaN",
  86. "Number",
  87. "Object",
  88. "parseFloat",
  89. "parseInt",
  90. "print",
  91. "Promise",
  92. "Proxy",
  93. "RangeError",
  94. "read",
  95. "readbuffer",
  96. "ReferenceError",
  97. "Reflect",
  98. "RegExp",
  99. "Set",
  100. "SharedArrayBuffer",
  101. "String",
  102. "Symbol",
  103. "SyntaxError",
  104. "TypeError",
  105. "Uint16Array",
  106. "Uint32Array",
  107. "Uint8Array",
  108. "Uint8ClampedArray",
  109. "undefined",
  110. "unescape",
  111. "URIError",
  112. "WeakMap",
  113. "WeakSet",
  114. "WebAssembly",
  115. "WScript",
  116. ].forEach(function (name) {
  117. filter[name] = 1;
  118. });
  119. function filterInternal(parentName, obj, depth) {
  120. for (var p in obj) {
  121. if (parentFilter[parentName] == 1 && filter[p] == 1) {
  122. delete obj[p];
  123. } else if (typeof obj[p] == "object") {
  124. filterInternal(p.trim(), obj[p], depth + 1);
  125. }
  126. }
  127. }
  128. return function (obj) {
  129. try {
  130. filterInternal("this"/*filter root*/, obj, 0);
  131. } catch (ex) {
  132. printError("exception during filter: " + ex);
  133. }
  134. };
  135. })();
  136. function recordEvent(json) {
  137. filterLog(json);
  138. _eventLog.push(json);
  139. }
  140. // remove path from file name and just have the filename with extension
  141. function filterFileName(fileName) {
  142. try {
  143. var index = fileName.lastIndexOf("\\");
  144. if (index >= 0) {
  145. return fileName.substring(index + 1);
  146. }
  147. } catch (ex) { }
  148. return "";
  149. }
  150. var bpManager = (function () {
  151. var _bpMap = [];
  152. var _locBpId = -1;
  153. function getBpFromName(name) {
  154. for (var i in _bpMap) {
  155. if (_bpMap[i].name === name) {
  156. return _bpMap[i];
  157. }
  158. }
  159. printError("Breakpoint named '" + name + "' was not found");
  160. }
  161. function internalSetBp(name, scriptId, line, column, execStr) {
  162. var bpObject = callHostFunction(hostDebugObject.JsDiagSetBreakpoint, scriptId, line, column);
  163. return {
  164. id: bpObject.breakpointId,
  165. scriptId: scriptId,
  166. name: name,
  167. line: bpObject.line,
  168. column: bpObject.column,
  169. execStr: execStr,
  170. enabled: true
  171. };
  172. }
  173. function addBpObject(bpObj) {
  174. internalTrace(TRACE_INTERNAL_FUNCTIONS, "addBpObject: ", bpObj);
  175. _bpMap[bpObj.id] = bpObj;
  176. }
  177. return {
  178. setBreakpoint: function (name, scriptId, line, column, execStr) {
  179. var bpObj = internalSetBp(name, scriptId, line, column, execStr);
  180. addBpObject(bpObj);
  181. },
  182. setLocationBreakpoint: function (name, scriptId, line, column, execStr) {
  183. var bpObj = {
  184. id: _locBpId--,
  185. scriptId: scriptId,
  186. name: name,
  187. line: line,
  188. column: column,
  189. execStr: execStr,
  190. enabled: false
  191. }
  192. addBpObject(bpObj);
  193. },
  194. enableBreakpoint: function (name) {
  195. var bpObj = getBpFromName(name);
  196. if (bpObj) {
  197. delete _bpMap[bpObj.id];
  198. internalTrace(TRACE_INTERNAL_FUNCTIONS, "enableBreakpoint: ", name, " bpObj: ", bpObj);
  199. bpObj = internalSetBp(bpObj.name, bpObj.scriptId, bpObj.line, bpObj.column, bpObj.execStr);
  200. addBpObject(bpObj);
  201. }
  202. },
  203. deleteBreakpoint: function (name) {
  204. var bpObj = getBpFromName(name);
  205. if (bpObj && bpObj.enabled) {
  206. internalTrace(TRACE_INTERNAL_FUNCTIONS, "deleteBreakpoint: ", name, " bpObj: ", bpObj);
  207. callHostFunction(hostDebugObject.JsDiagRemoveBreakpoint, bpObj.id);
  208. delete _bpMap[bpObj.id];
  209. }
  210. },
  211. disableBreakpoint: function (name) {
  212. var bpObj = getBpFromName(name);
  213. if (bpObj && bpObj.enabled) {
  214. internalTrace(TRACE_INTERNAL_FUNCTIONS, "disableBreakpoint: ", name, " bpObj: ", bpObj);
  215. callHostFunction(hostDebugObject.JsDiagRemoveBreakpoint, bpObj.id);
  216. _bpMap[bpObj.id].enabled = false;
  217. }
  218. },
  219. getExecStr: function (id) {
  220. for (var i in _bpMap) {
  221. if (_bpMap[i].id === id) {
  222. return _bpMap[i].execStr;
  223. }
  224. }
  225. printError("Breakpoint '" + id + "' was not found");
  226. },
  227. setExecStr: function (id, newExecStr) {
  228. for (var i in _bpMap) {
  229. if (_bpMap[i].id === id) {
  230. _bpMap[i].execStr = newExecStr;
  231. }
  232. }
  233. },
  234. clearAllBreakpoints: function () {
  235. _bpMap = [];
  236. _locBpId = -1;
  237. }
  238. }
  239. })();
  240. function addSourceFile(text, srcId) {
  241. try {
  242. // Split the text into lines. Note this doesn't take into account block comments, but that's probably okay.
  243. var lines = text.split(/\n/);
  244. // /**bp <-- a breakpoint
  245. // /**loc <-- a named source location used for enabling bp at later stage
  246. // /**exception <-- set exception handling, catch all or only uncaught exception
  247. // /**onasyncbreak <-- set what happens on async break
  248. var bpStartToken = "/**";
  249. var bpStartStrings = ["bp", "loc", "exception", "onasyncbreak"];
  250. var bpEnd = "**/";
  251. // Iterate through each source line, setting any breakpoints.
  252. for (var i = 0; i < lines.length; ++i) {
  253. var line = lines[i];
  254. for (var startString in bpStartStrings) {
  255. var bpStart = bpStartToken + bpStartStrings[startString];
  256. var isLocationBreakpoint = (bpStart.indexOf("loc") != -1);
  257. var isExceptionBreakpoint = (bpStart.indexOf("exception") != -1);
  258. var isOnAsyncBreak = (bpStart.indexOf("onasyncbreak") != -1);
  259. var startIdx = -1;
  260. while ((startIdx = line.indexOf(bpStart, startIdx + 1)) != -1) {
  261. var endIdx;
  262. var currLine = i;
  263. var bpLine = i;
  264. var currBpLineString = "";
  265. // Gather up any lines within the breakpoint comment.
  266. do {
  267. var currentStartIdx = 0;
  268. if (currLine == i) {
  269. currentStartIdx = startIdx;
  270. }
  271. currBpLineString += lines[currLine++];
  272. endIdx = currBpLineString.indexOf(bpEnd, currentStartIdx);
  273. } while (endIdx == -1 && currLine < lines.length && lines[currLine].indexOf(bpStartToken) == -1);
  274. // Move the line cursor forward, allowing the current line to be re-checked
  275. i = currLine - 1;
  276. // Do some checking
  277. if (endIdx == -1) {
  278. printError("Unterminated breakpoint expression");
  279. return;
  280. }
  281. var bpStrStartIdx = startIdx + bpStart.length;
  282. var bpStr = currBpLineString.substring(bpStrStartIdx, endIdx);
  283. var bpFullStr = currBpLineString.substring(startIdx, endIdx);
  284. // Quick check to make sure the breakpoint is not within a
  285. // quoted string (such as an eval). If it is within an eval, the
  286. // eval will cause a separate call to have its breakpoints parsed.
  287. // This check can be defeated, but it should cover the useful scenarios.
  288. var quoteCount = 0;
  289. var escapeCount = 0;
  290. for (var j = 0; j < bpStrStartIdx; ++j) {
  291. switch (currBpLineString[j]) {
  292. case '\\':
  293. escapeCount++;
  294. continue;
  295. case '"':
  296. case '\'':
  297. if (escapeCount % 2 == 0)
  298. quoteCount++;
  299. /* fall through */
  300. default:
  301. escapeCount = 0;
  302. }
  303. }
  304. if (quoteCount % 2 == 1) {
  305. // The breakpoint was in a quoted string.
  306. continue;
  307. }
  308. // Only support strings like:
  309. // /**bp**/
  310. // /**bp(name)**/
  311. // /**bp(columnoffset)**/ takes an integer
  312. // /**bp:locals();stack()**/
  313. // /**bp(name):locals();stack()**/
  314. // /**loc(name)**/
  315. // /**loc(name):locals();stack()**/
  316. //
  317. // Parse the breakpoint name (if it exists)
  318. var bpName = undefined;
  319. var bpColumnOffset = 0;
  320. var bpExecStr = undefined;
  321. var parseIdx = 0;
  322. if (bpStr[parseIdx] == '(') {
  323. // The name and offset is overloaded with the same parameter.
  324. // if that is int (determined by parseInt), then it is column offset otherwise left as name.
  325. bpName = bpStr.match(/\(([\w,]+?)\)/)[1];
  326. parseIdx = bpName.length + 2;
  327. bpColumnOffset = parseInt(bpName);
  328. if (isNaN(bpColumnOffset)) {
  329. bpColumnOffset = 0;
  330. } else {
  331. bpName = undefined;
  332. if (bpColumnOffset > line.length) {
  333. bpColumnOffset = line.length - 1;
  334. } else if (bpColumnOffset < 0) {
  335. bpColumnOffset = 0;
  336. }
  337. }
  338. } else if (isLocationBreakpoint) {
  339. printError("'loc' sites require a label, for example /**loc(myFunc)**/");
  340. return;
  341. }
  342. // Process the exception label:
  343. // exception(none)
  344. // exception(uncaught)
  345. // exception(all)
  346. if (isExceptionBreakpoint) {
  347. var exceptionAttributes = -1;
  348. if (bpName !== undefined) {
  349. if (bpName == "none") {
  350. exceptionAttributes = 0; // JsDiagBreakOnExceptionAttributeNone
  351. } else if (bpName == "uncaught") {
  352. exceptionAttributes = 0x2; // JsDiagBreakOnExceptionAttributeFirstChance
  353. } else if (bpName == "all") {
  354. exceptionAttributes = 0x1 | 0x2; // JsDiagBreakOnExceptionAttributeUncaught | JsDiagBreakOnExceptionAttributeFirstChance
  355. }
  356. } else {
  357. // throw "No exception type specified";
  358. }
  359. callHostFunction(hostDebugObject.JsDiagSetBreakOnException, exceptionAttributes);
  360. }
  361. // Parse the breakpoint execution string
  362. if (bpStr[parseIdx] == ':') {
  363. bpExecStr = bpStr.substring(parseIdx + 1);
  364. } else if (parseIdx != bpStr.length) {
  365. printError("Invalid breakpoint string: " + bpStr);
  366. return;
  367. }
  368. // Insert the breakpoint.
  369. if (isExceptionBreakpoint) {
  370. if (_exceptionCommands != undefined) {
  371. printError("More than one 'exception' annotation found");
  372. return;
  373. }
  374. _exceptionCommands = bpExecStr;
  375. }
  376. if (isOnAsyncBreak) {
  377. if (_onasyncbreakCommands != undefined) {
  378. printError("More than one 'onasyncbreak' annotation found");
  379. return;
  380. }
  381. _onasyncbreakCommands = bpExecStr;
  382. }
  383. if (!isExceptionBreakpoint && !isOnAsyncBreak) {
  384. if (!isLocationBreakpoint) {
  385. bpManager.setBreakpoint(bpName, srcId, bpLine, bpColumnOffset, bpExecStr);
  386. } else {
  387. bpManager.setLocationBreakpoint(bpName, srcId, bpLine, bpColumnOffset, bpExecStr);
  388. }
  389. }
  390. }
  391. }
  392. }
  393. } catch (ex) {
  394. printError(ex);
  395. }
  396. }
  397. function handleBreakpoint(id) {
  398. internalTrace(TRACE_INTERNAL_FUNCTIONS, "handleBreakpoint id: ", id)
  399. _wasResumed = false;
  400. if (id != -1) {
  401. try {
  402. var execStr = "";
  403. if (id === "exception") {
  404. execStr = _exceptionCommands;
  405. if (execStr && execStr.toString().search("removeExpr()") != -1) {
  406. _exceptionCommands = undefined;
  407. }
  408. } else if (id === "asyncbreak") {
  409. execStr = _onasyncbreakCommands;
  410. if (execStr && execStr.toString().search("removeExpr()") != -1) {
  411. _onasyncbreakCommands = undefined;
  412. }
  413. } else if (id === "debuggerStatement") {
  414. execStr = "dumpBreak();locals();stack();"
  415. } else {
  416. // Retrieve this breakpoint's execution string
  417. execStr = bpManager.getExecStr(id);
  418. if (execStr.toString().search("removeExpr()") != -1) {
  419. // A hack to remove entire expression, so that it will not run again.
  420. bpManager.setExecStr(id, null);
  421. }
  422. }
  423. internalTrace(TRACE_INTERNAL_FUNCTIONS, "handleBreakpoint execStr: ", execStr)
  424. if (execStr != null) {
  425. eval(execStr);
  426. }
  427. } catch (ex) {
  428. printError(ex);
  429. }
  430. }
  431. internalTrace(TRACE_INTERNAL_FUNCTIONS, "_commandList length: ", _commandList.length, " _wasResumed: ", _wasResumed);
  432. // Continue processing the command list.
  433. while (_commandList.length > 0 && !_wasResumed) {
  434. var cmd = _commandList.shift();
  435. internalTrace(TRACE_INTERNAL_FUNCTIONS, "cmd: ", cmd);
  436. var completion = cmd.fn.apply(this, cmd.args);
  437. if (typeof completion === "function") {
  438. _commandCompletions.push(completion);
  439. }
  440. }
  441. while (_commandCompletions.length > 0) {
  442. var completion = _commandCompletions.shift();
  443. completion();
  444. }
  445. if (!_wasResumed) {
  446. _currentStackFrameIndex = 0;
  447. _wasResumed = true;
  448. }
  449. }
  450. function GetObjectDisplay(obj) {
  451. var objectDisplay = ("className" in obj) ? obj["className"] : obj["type"];
  452. var value = ("value" in obj) ? obj["value"] : obj["display"];
  453. if (value && value.length > _inspectMaxStringLength) {
  454. objectDisplay += " <large string>";
  455. } else {
  456. objectDisplay += " " + value;
  457. }
  458. return objectDisplay;
  459. }
  460. var stringToArrayBuffer = function stringToArrayBuffer(str) {
  461. var arr = [];
  462. for (var i = 0, len = str.length; i < len; i++) {
  463. arr[i] = str.charCodeAt(i) & 0xFF;
  464. }
  465. return new Uint8Array(arr).buffer;
  466. }
  467. function GetChild(obj, level) {
  468. function GetChildrens(obj, level) {
  469. var retArray = {};
  470. for (var i = 0; i < obj.length; ++i) {
  471. var propName = (obj[i].name == "__proto__") ? "#__proto__" : obj[i].name;
  472. retArray[propName] = GetChild(obj[i], level);
  473. }
  474. return retArray;
  475. }
  476. var retValue = {};
  477. if ("handle" in obj) {
  478. if (level >= 0) {
  479. var childProps = callHostFunction(hostDebugObject.JsDiagGetProperties, obj["handle"], 0, 1000);
  480. var properties = childProps["properties"];
  481. var debuggerOnlyProperties = childProps["debuggerOnlyProperties"];
  482. Array.prototype.push.apply(properties, debuggerOnlyProperties);
  483. if (properties.length > 0) {
  484. retValue = GetChildrens(properties, level - 1);
  485. } else {
  486. retValue = GetObjectDisplay(obj);
  487. }
  488. } else {
  489. retValue = GetObjectDisplay(obj);
  490. }
  491. delete obj["handle"];
  492. }
  493. if ("propertyAttributes" in obj) {
  494. delete obj["propertyAttributes"];
  495. }
  496. return retValue;
  497. }
  498. function compareWithBaseline() {
  499. function compareObjects(a, b, obj_namespace) {
  500. var objectsEqual = true;
  501. // This is a basic object comparison function, primarily to be used for JSON data.
  502. // It doesn't handle cyclical objects.
  503. function fail(step, message) {
  504. if (message == undefined) {
  505. message = "diff baselines for details";
  506. }
  507. printError("Step " + step + "; on: " + obj_namespace + ": " + message);
  508. objectsEqual = false;
  509. }
  510. function failNonObj(step, a, b, message) {
  511. if (message == undefined) {
  512. message = "";
  513. }
  514. printError("Step " + step + "; Local Diff on: " + obj_namespace + ": " + message);
  515. printError("Value 1:" + JSON.stringify(a));
  516. printError("Value 2:" + JSON.stringify(b));
  517. print("");
  518. objectsEqual = false;
  519. }
  520. // (1) Check strict equality.
  521. if (a === b)
  522. return true;
  523. // (2) non-Objects must have passed the strict equality comparison in (1)
  524. if ((typeof a != "object") || (typeof b != "object")) {
  525. failNonObj(2, a, b);
  526. return false;
  527. }
  528. // (3) check all properties
  529. for (var p in a) {
  530. // (4) check the property
  531. if (a[p] === b[p])
  532. continue;
  533. // (5) non-Objects must have passed the strict equality comparison in (4)
  534. if (typeof (a[p]) != "object") {
  535. failNonObj(5, a[p], b[p], "Property " + p);
  536. continue;
  537. }
  538. // (6) recursively check objects or arrays
  539. if (!compareObjects(a[p], b[p], obj_namespace + "." + p)) {
  540. // Don't need to report error message as it'll be reported inside nested call
  541. objectsEqual = false;
  542. continue;
  543. }
  544. }
  545. // (7) check any properties not in the previous enumeration
  546. var hasOwnProperty = Object.prototype.hasOwnProperty;
  547. for (var p in b) {
  548. if (hasOwnProperty.call(b, p) && !hasOwnProperty.call(a, p)) {
  549. fail(7, "Property missing: " + p + ", value: " + JSON.stringify(b[p]));
  550. continue;
  551. }
  552. }
  553. return objectsEqual;
  554. }
  555. var PASSED = 0;
  556. var FAILED = 1;
  557. if (_baseline == undefined) {
  558. return PASSED;
  559. }
  560. try {
  561. if (compareObjects(_baseline, _eventLog, "baseline")) {
  562. return PASSED;
  563. }
  564. }
  565. catch (ex) {
  566. printError("EXCEPTION: " + ex);
  567. }
  568. printError("TEST FAILED");
  569. return FAILED;
  570. }
  571. return {
  572. pushCommand: function pushCommand(fn, args) {
  573. _commandList.push({
  574. fn: fn,
  575. args: args
  576. });
  577. },
  578. debuggerCommands: {
  579. log: function (str) {
  580. internalPrint("LOG: " + str);
  581. },
  582. logJson: function (str) {
  583. recordEvent({ log: str });
  584. },
  585. resume: function (kind) {
  586. if (_wasResumed) {
  587. printError("Breakpoint resumed twice");
  588. } else {
  589. var stepType = -1;
  590. if (kind == "step_into") {
  591. stepType = 0;
  592. } else if (kind == "step_out") {
  593. stepType = 1;
  594. } else if (kind == "step_over") {
  595. stepType = 2;
  596. } else if (kind == "step_document") {
  597. stepType = 0;
  598. }
  599. if (stepType != -1) {
  600. callHostFunction(hostDebugObject.JsDiagSetStepType, stepType);
  601. } else if (kind != "continue") {
  602. throw new Error("Unhandled step type - " + kind);
  603. }
  604. _wasResumed = true;
  605. _currentStackFrameIndex = 0;
  606. }
  607. },
  608. locals: function (expandLevel, flags) {
  609. if (expandLevel == undefined || expandLevel < 0) {
  610. expandLevel = 0;
  611. }
  612. var stackProperties = callHostFunction(hostDebugObject.JsDiagGetStackProperties, _currentStackFrameIndex);
  613. if (expandLevel >= 0) {
  614. var localsJSON = {};
  615. ["thisObject", "exception", "arguments", "returnValue"].forEach(function (name) {
  616. if (name in stackProperties) {
  617. var stackObject = stackProperties[name];
  618. localsJSON[stackObject.name] = GetChild(stackObject, expandLevel - 1);
  619. }
  620. });
  621. ["functionCallsReturn", "locals"].forEach(function (name) {
  622. if (name in stackProperties) {
  623. var stackObject = stackProperties[name];
  624. if (stackObject.length > 0) {
  625. localsJSON[name] = {};
  626. for (var i = 0; i < stackObject.length; ++i) {
  627. localsJSON[name][stackObject[i].name] = GetChild(stackObject[i], expandLevel - 1);
  628. }
  629. }
  630. }
  631. });
  632. if ("scopes" in stackProperties) {
  633. var scopesArray = stackProperties["scopes"];
  634. for (var i = 0; i < scopesArray.length; ++i) {
  635. localsJSON["scopes" + i] = GetChild(scopesArray[i], expandLevel - 1);
  636. }
  637. }
  638. if ("globals" in stackProperties && expandLevel > 0) {
  639. localsJSON["globals"] = GetChild(stackProperties["globals"], expandLevel - 1);
  640. }
  641. recordEvent(localsJSON);
  642. }
  643. },
  644. stack: function (flags) {
  645. if (flags === undefined) {
  646. flags = 0;
  647. }
  648. var stackTrace = callHostFunction(hostDebugObject.JsDiagGetStackTrace);
  649. var stackTraceArray = [];
  650. for (var i = 0; i < stackTrace.length; ++i) {
  651. var stack = {};
  652. stack["line"] = stackTrace[i].line;
  653. stack["column"] = stackTrace[i].column;
  654. stack["sourceText"] = stackTrace[i].sourceText;
  655. var functionObject = callHostFunction(hostDebugObject.JsDiagGetObjectFromHandle, stackTrace[i].functionHandle);
  656. stack["function"] = functionObject.name;
  657. stackTraceArray.push(stack);
  658. }
  659. recordEvent({
  660. 'callStack': stackTraceArray
  661. });
  662. },
  663. evaluate: function (expression, expandLevel, flags) {
  664. if (expression != undefined) {
  665. if (typeof expandLevel != "number" || expandLevel <= 0) {
  666. expandLevel = 0;
  667. }
  668. if (WScript && typeof expression == 'string' && WScript.forceDebugArrayBuffer)
  669. expression = stringToArrayBuffer(expression);
  670. var evalResult = callHostFunction(hostDebugObject.JsDiagEvaluate, _currentStackFrameIndex, expression);
  671. var evaluateOutput = {};
  672. evaluateOutput[evalResult.name] = GetChild(evalResult, expandLevel - 1);
  673. recordEvent({
  674. 'evaluate': evaluateOutput
  675. });
  676. }
  677. },
  678. enableBp: function (name) {
  679. bpManager.enableBreakpoint(name);
  680. },
  681. disableBp: function (name) {
  682. bpManager.disableBreakpoint(name);
  683. },
  684. deleteBp: function (name) {
  685. bpManager.deleteBreakpoint(name);
  686. },
  687. setFrame: function (depth) {
  688. var stackTrace = callHostFunction(hostDebugObject.JsDiagGetStackTrace);
  689. for (var i = 0; i < stackTrace.length; ++i) {
  690. if (stackTrace[i].index == depth) {
  691. _currentStackFrameIndex = depth;
  692. break;
  693. }
  694. }
  695. },
  696. dumpBreak: function () {
  697. var breakpoints = callHostFunction(hostDebugObject.JsDiagGetBreakpoints);
  698. recordEvent({
  699. 'breakpoints': breakpoints
  700. });
  701. },
  702. dumpSourceList: function () {
  703. var sources = callHostFunction(hostDebugObject.JsDiagGetScripts);
  704. sources.forEach(function (source) {
  705. if ("fileName" in source) {
  706. source["fileName"] = filterFileName(source["fileName"]);
  707. }
  708. });
  709. recordEvent({
  710. 'sources': sources
  711. });
  712. },
  713. dumpFunctionProperties: function (frameIdOrArrayOfIds = [0], expandLevel = 0) {
  714. if (typeof frameIdOrArrayOfIds != "number" && !(frameIdOrArrayOfIds instanceof Array)) {
  715. frameIdOrArrayOfIds = [0];
  716. }
  717. if (typeof expandLevel != "number" || expandLevel < 0) {
  718. expandLevel = 0;
  719. }
  720. let stackTrace = callHostFunction(hostDebugObject.JsDiagGetStackTrace);
  721. let functionHandles = [];
  722. let requestedFrameIndexes = [];
  723. if (typeof frameIdOrArrayOfIds === "number") {
  724. requestedFrameIndexes.push(frameIdOrArrayOfIds);
  725. } else if (frameIdOrArrayOfIds instanceof Array) {
  726. frameIdOrArrayOfIds.forEach((s) => {
  727. if (typeof s === "number") {
  728. requestedFrameIndexes.push(s);
  729. }
  730. });
  731. }
  732. if (requestedFrameIndexes.length == 0) {
  733. requestedFrameIndexes.push(0);
  734. }
  735. stackTrace.forEach((stackFrame) => {
  736. let stackFrameIndex = stackFrame.index;
  737. if (requestedFrameIndexes.includes(stackFrameIndex) && !functionHandles.includes(stackFrame.functionHandle)) {
  738. functionHandles.push(stackFrame.functionHandle);
  739. }
  740. });
  741. let functionProperties = [];
  742. functionHandles.forEach((handle) => {
  743. functionProperties.push(GetChild({ handle: handle }, expandLevel));
  744. });
  745. recordEvent({
  746. 'functionProperties': functionProperties
  747. });
  748. },
  749. trace: function (traceFlag) {
  750. _trace |= traceFlag;
  751. }
  752. },
  753. setBaseline: function (baseline) {
  754. try {
  755. _baseline = JSON.parse(baseline);
  756. } catch (ex) {
  757. printError("Invalid JSON passed to setBaseline: " + ex);
  758. internalPrint(baseline);
  759. }
  760. },
  761. dumpFunctionPosition: function (functionPosition) {
  762. if (!functionPosition) {
  763. functionPosition = {};
  764. }
  765. else {
  766. functionPosition["fileName"] = filterFileName(functionPosition["fileName"]);
  767. }
  768. recordEvent({
  769. 'functionPosition': functionPosition
  770. });
  771. },
  772. setInspectMaxStringLength: function (value) {
  773. _inspectMaxStringLength = value;
  774. },
  775. getOutputJson: function () {
  776. return JSON.stringify(_eventLog, undefined, " ");
  777. },
  778. verify: function () {
  779. return compareWithBaseline();
  780. },
  781. handleDebugEvent: function (debugEvent, eventData) {
  782. function debugEventToString(debugEvent) {
  783. switch (debugEvent) {
  784. case 0:
  785. return "JsDiagDebugEventSourceCompile";
  786. case 1:
  787. return "JsDiagDebugEventCompileError";
  788. case 2:
  789. return "JsDiagDebugEventBreakpoint";
  790. case 3:
  791. return "JsDiagDebugEventStepComplete";
  792. case 4:
  793. return "JsDiagDebugEventDebuggerStatement";
  794. case 5:
  795. return "JsDiagDebugEventAsyncBreak";
  796. case 6:
  797. return "JsDiagDebugEventRuntimeException";
  798. default:
  799. return "UnKnown";
  800. }
  801. }
  802. internalTrace(TRACE_DEBUG_EVENTS, {
  803. 'debugEvent': debugEventToString(debugEvent),
  804. 'eventData': eventData
  805. });
  806. switch (debugEvent) {
  807. case 0:
  808. /*JsDiagDebugEventSourceCompile*/
  809. var source = callHostFunction(hostDebugObject.JsDiagGetSource, eventData.scriptId);
  810. addSourceFile(source.source, source.scriptId);
  811. break;
  812. case 1:
  813. /*JsDiagDebugEventCompileError*/
  814. var stackTrace = callHostFunction(hostDebugObject.JsDiagGetScripts);
  815. break;
  816. case 2:
  817. /*JsDiagDebugEventBreakpoint*/
  818. case 3:
  819. /*JsDiagDebugEventStepComplete*/
  820. handleBreakpoint(("breakpointId" in eventData) ? eventData.breakpointId : -1);
  821. break;
  822. case 4:
  823. /*JsDiagDebugEventDebuggerStatement*/
  824. handleBreakpoint("debuggerStatement");
  825. break;
  826. case 5:
  827. /*JsDiagDebugEventAsyncBreak*/
  828. handleBreakpoint("asyncbreak");
  829. break;
  830. case 6:
  831. /*JsDiagDebugEventRuntimeException*/
  832. handleBreakpoint("exception");
  833. break;
  834. default:
  835. throw "Unhandled JsDiagDebugEvent value " + debugEvent;
  836. break;
  837. }
  838. },
  839. handleSourceRunDown: function (sources) {
  840. bpManager.clearAllBreakpoints();
  841. for (var len = 0; len < sources.length; ++len) {
  842. var source = callHostFunction(hostDebugObject.JsDiagGetSource, sources[len].scriptId);
  843. addSourceFile(source.source, source.scriptId);
  844. };
  845. },
  846. }
  847. })();
  848. // Commands for use from the breakpoint execution string in test files
  849. function log(str) {
  850. // Prints given string as 'LOG: <given string>' on console
  851. controllerObj.pushCommand(controllerObj.debuggerCommands.log, arguments);
  852. }
  853. function logJson(str) {
  854. // Prints given string on console
  855. controllerObj.pushCommand(controllerObj.debuggerCommands.logJson, arguments);
  856. }
  857. function resume(kind) {
  858. // Resume exceution after a break, kinds - step_into, step_out, step_over
  859. controllerObj.pushCommand(controllerObj.debuggerCommands.resume, arguments);
  860. }
  861. function locals(expandLevel, flags) {
  862. // Dumps locals tree, expand till expandLevel with given flags
  863. controllerObj.pushCommand(controllerObj.debuggerCommands.locals, arguments);
  864. }
  865. function stack() {
  866. controllerObj.pushCommand(controllerObj.debuggerCommands.stack, arguments);
  867. }
  868. function removeExpr(bpId) {
  869. // A workaround to remove the current expression
  870. }
  871. function evaluate(expression, expandLevel, flags) {
  872. controllerObj.pushCommand(controllerObj.debuggerCommands.evaluate, arguments);
  873. }
  874. function enableBp(name) {
  875. controllerObj.pushCommand(controllerObj.debuggerCommands.enableBp, arguments);
  876. }
  877. function disableBp(name) {
  878. controllerObj.pushCommand(controllerObj.debuggerCommands.disableBp, arguments);
  879. }
  880. function deleteBp(name) {
  881. controllerObj.pushCommand(controllerObj.debuggerCommands.deleteBp, arguments);
  882. }
  883. function setFrame(name) {
  884. controllerObj.pushCommand(controllerObj.debuggerCommands.setFrame, arguments);
  885. }
  886. function dumpBreak() {
  887. controllerObj.pushCommand(controllerObj.debuggerCommands.dumpBreak, arguments);
  888. }
  889. function dumpSourceList() {
  890. controllerObj.pushCommand(controllerObj.debuggerCommands.dumpSourceList, arguments);
  891. }
  892. function dumpFunctionProperties() {
  893. controllerObj.pushCommand(controllerObj.debuggerCommands.dumpFunctionProperties, arguments);
  894. }
  895. // Start internal tracing. E.g.: /**bp:trace(TRACE_COMMANDS)**/
  896. function trace() {
  897. controllerObj.pushCommand(controllerObj.debuggerCommands.trace, arguments);
  898. }
  899. // Non Supported - TO BE REMOVED
  900. function setExceptionResume() { }
  901. function setnext() { }
  902. function evaluateAsync() { }
  903. function trackProjectionCall() { }
  904. function mbp() { }
  905. function deleteMbp() { }
  906. // APIs exposed to Debugger.cpp
  907. function GetOutputJson() {
  908. return controllerObj.getOutputJson.apply(controllerObj, arguments);
  909. }
  910. function Verify() {
  911. return controllerObj.verify.apply(controllerObj, arguments);
  912. }
  913. function SetBaseline() {
  914. return controllerObj.setBaseline.apply(controllerObj, arguments);
  915. }
  916. function SetInspectMaxStringLength() {
  917. return controllerObj.setInspectMaxStringLength.apply(controllerObj, arguments);
  918. }
  919. function DumpFunctionPosition() {
  920. return controllerObj.dumpFunctionPosition.apply(controllerObj, arguments);
  921. }
  922. // Called from Debugger.cpp to handle JsDiagDebugEvent
  923. function HandleDebugEvent() {
  924. return controllerObj.handleDebugEvent.apply(controllerObj, arguments);
  925. }
  926. // Called from Debugger.cpp when debugger is attached using WScript.Attach
  927. function HandleSourceRunDown() {
  928. return controllerObj.handleSourceRunDown.apply(controllerObj, arguments);
  929. }