diff --git a/src/helpers/util.js b/src/helpers/util.js index 261037eeff..2a84846cd4 100644 --- a/src/helpers/util.js +++ b/src/helpers/util.js @@ -85,7 +85,21 @@ module.exports = { return index >= 0 ? array[index] : null }, - findCall: findCall + findCall: findCall, + buildCallsPath: buildCallsPath +} + +/** + * Find calls path from @args rootCall which leads to @args index (recursive) + * + * @param {Int} index - index of the vmtrace + * @param {Object} rootCall - call tree, built by the trace analyser + * @return {Array} - return the calls path to @args index + */ +function buildCallsPath (index, rootCall) { + var ret = [] + findCallInternal(index, rootCall, ret) + return ret } /** @@ -96,12 +110,18 @@ module.exports = { * @return {Object} - return the call which include the @args index */ function findCall (index, rootCall) { + var ret = buildCallsPath(index, rootCall) + return ret[ret.length - 1] +} + +function findCallInternal (index, rootCall, callsPath) { var calls = Object.keys(rootCall.calls) var ret = rootCall + callsPath.push(rootCall) for (var k in calls) { var subCall = rootCall.calls[calls[k]] if (index >= subCall.start && index <= subCall.return) { - ret = findCall(index, subCall) + findCallInternal(index, subCall, callsPath) break } } diff --git a/src/trace/traceManager.js b/src/trace/traceManager.js index 412c520f3f..a8f323827b 100644 --- a/src/trace/traceManager.js +++ b/src/trace/traceManager.js @@ -114,6 +114,16 @@ TraceManager.prototype.getCallDataAt = function (stepIndex, callback) { callback(null, [this.traceCache.callsData[callDataChange]]) } +TraceManager.prototype.buildCallsPath = function (stepIndex, callback) { + var check = this.checkRequestedStep(stepIndex) + if (check) { + return callback(check, null) + } + var callsPath = util.buildCallsPath(stepIndex, this.traceCache.callsTree.call) + if (callsPath === null) return callback('no call path built', null) + callback(null, callsPath) +} + TraceManager.prototype.getCallStackAt = function (stepIndex, callback) { var check = this.checkRequestedStep(stepIndex) if (check) {