Commit 3866faa9 authored by Iuri Matias's avatar Iuri Matias Committed by aniket-engg

refactor getStackAt

parent f788b8d2
...@@ -90,7 +90,14 @@ Ethdebugger.prototype.extractLocalsAt = function (step, callback) { ...@@ -90,7 +90,14 @@ Ethdebugger.prototype.extractLocalsAt = function (step, callback) {
Ethdebugger.prototype.decodeLocalsAt = function (step, sourceLocation, callback) { Ethdebugger.prototype.decodeLocalsAt = function (step, sourceLocation, callback) {
const self = this const self = this
this.traceManager.waterfall([ this.traceManager.waterfall([
this.traceManager.getStackAt, function getStackAt (stepIndex, callback) {
try {
const result = self.traceManager.getStackAt(stepIndex)
callback(null, result)
} catch (error) {
callback(error)
}
},
this.traceManager.getMemoryAt, this.traceManager.getMemoryAt,
function getCurrentCalledAddressAt (stepIndex, next) { function getCurrentCalledAddressAt (stepIndex, next) {
try { try {
......
...@@ -87,14 +87,14 @@ class VmDebuggerLogic { ...@@ -87,14 +87,14 @@ class VmDebuggerLogic {
this.event.trigger('traceManagerCallStackUpdate', [{}]) this.event.trigger('traceManagerCallStackUpdate', [{}])
} }
this._traceManager.getStackAt(index, (error, callstack) => { try {
if (error) { const callstack = this._traceManager.getStackAt(index)
// console.log(error) if (this.stepManager.currentStepIndex === index) {
this.event.trigger('traceManagerStackUpdate', [{}])
} else if (this.stepManager.currentStepIndex === index) {
this.event.trigger('traceManagerStackUpdate', [callstack]) this.event.trigger('traceManagerStackUpdate', [callstack])
} }
}) } catch (error) {
this.event.trigger('traceManagerStackUpdate', [{}])
}
try { try {
const address = this._traceManager.getCurrentCalledAddressAt(index) const address = this._traceManager.getCurrentCalledAddressAt(index)
......
...@@ -30,9 +30,17 @@ class DebuggerSolidityLocals { ...@@ -30,9 +30,17 @@ class DebuggerSolidityLocals {
} }
decode (sourceLocation) { decode (sourceLocation) {
const self = this
this.event.trigger('solidityLocalsMessage', ['']) this.event.trigger('solidityLocalsMessage', [''])
this.traceManager.waterfall([ this.traceManager.waterfall([
this.traceManager.getStackAt, function getStackAt (stepIndex, callback) {
try {
const result = self.traceManager.getStackAt(stepIndex)
callback(null, result)
} catch (error) {
callback(error)
}
},
this.traceManager.getMemoryAt, this.traceManager.getMemoryAt,
function getCurrentCalledAddressAt (stepIndex, next) { function getCurrentCalledAddressAt (stepIndex, next) {
try { try {
......
...@@ -224,26 +224,26 @@ function includeVariableDeclaration (tree, step, sourceLocation, scopeId, newLoc ...@@ -224,26 +224,26 @@ function includeVariableDeclaration (tree, step, sourceLocation, scopeId, newLoc
// we check if the current vm trace step target a new ast node of type VariableDeclaration // we check if the current vm trace step target a new ast node of type VariableDeclaration
// that way we know that there is a new local variable from here. // that way we know that there is a new local variable from here.
if (variableDeclaration && !tree.scopes[scopeId].locals[variableDeclaration.attributes.name]) { if (variableDeclaration && !tree.scopes[scopeId].locals[variableDeclaration.attributes.name]) {
tree.traceManager.getStackAt(step, (error, stack) => { try {
const stack = tree.traceManager.getStackAt(step)
// the stack length at this point is where the value of the new local variable will be stored. // the stack length at this point is where the value of the new local variable will be stored.
// so, either this is the direct value, or the offset in memory. That depends on the type. // so, either this is the direct value, or the offset in memory. That depends on the type.
if (!error) { tree.solidityProxy.contractNameAt(step, (error, contractName) => { // cached
tree.solidityProxy.contractNameAt(step, (error, contractName) => { // cached if (!error && variableDeclaration.attributes.name !== '') {
if (!error && variableDeclaration.attributes.name !== '') { var states = tree.solidityProxy.extractStatesDefinitions()
var states = tree.solidityProxy.extractStatesDefinitions() var location = typesUtil.extractLocationFromAstVariable(variableDeclaration)
var location = typesUtil.extractLocationFromAstVariable(variableDeclaration) location = location === 'default' ? 'storage' : location
location = location === 'default' ? 'storage' : location
// we push the new local variable in our tree // we push the new local variable in our tree
tree.scopes[scopeId].locals[variableDeclaration.attributes.name] = { tree.scopes[scopeId].locals[variableDeclaration.attributes.name] = {
name: variableDeclaration.attributes.name, name: variableDeclaration.attributes.name,
type: decodeInfo.parseType(variableDeclaration.attributes.type, states, contractName, location), type: decodeInfo.parseType(variableDeclaration.attributes.type, states, contractName, location),
stackDepth: stack.length, stackDepth: stack.length,
sourceLocation: sourceLocation sourceLocation: sourceLocation
}
} }
}) }
} })
}) } catch (error) {
}
} }
// we check here if we are at the beginning inside a new function. // we check here if we are at the beginning inside a new function.
// if that is the case, we have to add to locals tree the inputs and output params // if that is the case, we have to add to locals tree the inputs and output params
...@@ -253,34 +253,36 @@ function includeVariableDeclaration (tree, step, sourceLocation, scopeId, newLoc ...@@ -253,34 +253,36 @@ function includeVariableDeclaration (tree, step, sourceLocation, scopeId, newLoc
const functionDefinitionAndInputs = {functionDefinition, inputs: []} const functionDefinitionAndInputs = {functionDefinition, inputs: []}
// means: the previous location was a function definition && JUMPDEST // means: the previous location was a function definition && JUMPDEST
// => we are at the beginning of the function and input/output are setup // => we are at the beginning of the function and input/output are setup
tree.solidityProxy.contractNameAt(step, (error, contractName) => { // cached tree.solidityProxy.contractNameAt(step, (error, contractName) => { // cached
if (!error) { if (!error) {
tree.traceManager.getStackAt(step, (error, stack) => { try {
if (!error) { const stack = tree.traceManager.getStackAt(step)
var states = tree.solidityProxy.extractStatesDefinitions() var states = tree.solidityProxy.extractStatesDefinitions()
if (functionDefinition.children && functionDefinition.children.length) { if (functionDefinition.children && functionDefinition.children.length) {
let inputs let inputs
let outputs let outputs
for (const element of functionDefinition.children) { for (const element of functionDefinition.children) {
if (element.name === 'ParameterList') { if (element.name === 'ParameterList') {
if (!inputs) inputs = element if (!inputs) inputs = element
else { else {
outputs = element outputs = element
break break
}
} }
} }
// input params
if (inputs) {
functionDefinitionAndInputs.inputs = addParams(inputs, tree, scopeId, states, contractName, previousSourceLocation, stack.length, inputs.children.length, -1)
}
// output params
if (outputs) addParams(outputs, tree, scopeId, states, contractName, previousSourceLocation, stack.length, 0, 1)
} }
// input params
if (inputs) {
functionDefinitionAndInputs.inputs = addParams(inputs, tree, scopeId, states, contractName, previousSourceLocation, stack.length, inputs.children.length, -1)
}
// output params
if (outputs) addParams(outputs, tree, scopeId, states, contractName, previousSourceLocation, stack.length, 0, 1)
} }
}) } catch (error) {
}
} }
}) })
tree.functionDefinitionsByScope[scopeId] = functionDefinitionAndInputs tree.functionDefinitionsByScope[scopeId] = functionDefinitionAndInputs
} }
} }
......
...@@ -193,23 +193,27 @@ function testDebugging (debugManager) { ...@@ -193,23 +193,27 @@ function testDebugging (debugManager) {
// stack // stack
tape('traceManager.getStackAt 4', (t) => { tape('traceManager.getStackAt 4', (t) => {
t.plan(1) t.plan(1)
debugManager.traceManager.getStackAt(4, (error, callstack) => { try {
if (error) return t.end(error) const callstack = debugManager.traceManager.getStackAt(4)
t.equal(JSON.stringify(callstack), JSON.stringify([ '0x0000000000000000000000000000000000000000000000000000000000000000' ])) t.equal(JSON.stringify(callstack), JSON.stringify(['0x0000000000000000000000000000000000000000000000000000000000000000']))
}) } catch (error) {
return t.end(error)
}
}) })
tape('traceManager.getStackAt 41', (t) => { tape('traceManager.getStackAt 41', (t) => {
t.plan(1) t.plan(1)
debugManager.traceManager.getStackAt(41, (error, callstack) => { try {
if (error) return t.end(error) const callstack = debugManager.traceManager.getStackAt(41)
t.equal(JSON.stringify(callstack), JSON.stringify([ t.equal(JSON.stringify(callstack), JSON.stringify([
'0x0000000000000000000000000000000000000000000000000000000000000080', '0x0000000000000000000000000000000000000000000000000000000000000080',
'0x0000000000000000000000000000000000000000000000000000000000000020', '0x0000000000000000000000000000000000000000000000000000000000000020',
'0x0000000000000000000000000000000000000000000000000000000000000080', '0x0000000000000000000000000000000000000000000000000000000000000080',
'0x00000000000000000000000000000000000000000000000000000000000000e0', '0x00000000000000000000000000000000000000000000000000000000000000e0',
'0x00000000000000000000000000000000000000000000000000000000000000e0'])) '0x00000000000000000000000000000000000000000000000000000000000000e0']))
}) } catch (error) {
return t.end(error)
}
}) })
// storage // storage
......
...@@ -7,7 +7,14 @@ var localDecoder = require('../../../src/solidity-decoder/localDecoder') ...@@ -7,7 +7,14 @@ var localDecoder = require('../../../src/solidity-decoder/localDecoder')
function decodeLocal (st, index, traceManager, callTree, verifier) { function decodeLocal (st, index, traceManager, callTree, verifier) {
try { try {
traceManager.waterfall([ traceManager.waterfall([
traceManager.getStackAt, function getStackAt (stepIndex, callback) {
try {
const result = traceManager.getStackAt(stepIndex)
callback(null, result)
} catch (error) {
callback(error)
}
},
traceManager.getMemoryAt], traceManager.getMemoryAt],
index, index,
function (error, result) { function (error, result) {
......
...@@ -124,19 +124,14 @@ TraceManager.prototype.getCallStackAt = function (stepIndex) { ...@@ -124,19 +124,14 @@ TraceManager.prototype.getCallStackAt = function (stepIndex) {
return call.callStack return call.callStack
} }
TraceManager.prototype.getStackAt = function (stepIndex, callback) { TraceManager.prototype.getStackAt = function (stepIndex) {
try { this.checkRequestedStep(stepIndex)
this.checkRequestedStep(stepIndex)
} catch (check) {
return callback(check, null)
}
let stack
if (this.trace[stepIndex] && this.trace[stepIndex].stack) { // there's always a stack if (this.trace[stepIndex] && this.trace[stepIndex].stack) { // there's always a stack
stack = this.trace[stepIndex].stack.slice(0) let stack = this.trace[stepIndex].stack.slice(0)
stack.reverse() stack.reverse()
callback(null, stack) return stack
} else { } else {
callback('no stack found', null) throw new Error('no stack found')
} }
} }
......
...@@ -96,24 +96,22 @@ tape('TraceManager', function (t) { ...@@ -96,24 +96,22 @@ tape('TraceManager', function (t) {
t.test('TraceManager.getStackAt', function (st) { t.test('TraceManager.getStackAt', function (st) {
st.plan(3) st.plan(3)
traceManager.getStackAt(0, function (error, result) { try {
const result = traceManager.getStackAt(0)
console.log(result) console.log(result)
if (error) { st.ok(result.length === 0)
st.fail(error) } catch (error) {
} else { st.fail(error)
st.ok(result.length === 0) }
}
})
traceManager.getStackAt(28, function (error, result) { try {
const result = traceManager.getStackAt(28)
console.log(result) console.log(result)
if (error) { st.ok(result.length === 4)
st.fail(error) st.ok(result[3] === '0x60fe47b1')
} else { } catch (error) {
st.ok(result.length === 4) st.fail(error)
st.ok(result[3] === '0x60fe47b1') }
}
})
}) })
t.test('TraceManager.getLastCallChangeSince', function (st) { t.test('TraceManager.getLastCallChangeSince', function (st) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment