You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
90 lines
3.0 KiB
90 lines
3.0 KiB
8 years ago
|
'use strict'
|
||
7 years ago
|
var EventManager = require('./eventManager')
|
||
|
var helper = require('./helpers/traceHelper')
|
||
|
var SourceMappingDecoder = require('./sourceMappingDecoder')
|
||
|
var util = require('./util')
|
||
8 years ago
|
|
||
|
/**
|
||
|
* Process the source code location for the current executing bytecode
|
||
|
*/
|
||
|
function SourceLocationTracker (_codeManager) {
|
||
|
this.codeManager = _codeManager
|
||
8 years ago
|
this.event = new EventManager()
|
||
8 years ago
|
this.sourceMappingDecoder = new SourceMappingDecoder()
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Return the source location associated with the given @arg index
|
||
|
*
|
||
|
* @param {String} address - contract address from which the source location is retrieved
|
||
|
* @param {Int} index - index in the instruction list from where the source location is retrieved
|
||
|
* @param {Object} contractDetails - AST of compiled contracts
|
||
|
* @param {Function} cb - callback function
|
||
|
*/
|
||
8 years ago
|
SourceLocationTracker.prototype.getSourceLocationFromInstructionIndex = function (address, index, contracts, cb) {
|
||
8 years ago
|
var self = this
|
||
8 years ago
|
extractSourceMap(this.codeManager, address, contracts, function (error, sourceMap) {
|
||
|
if (error) {
|
||
8 years ago
|
cb(error)
|
||
8 years ago
|
} else {
|
||
|
cb(null, self.sourceMappingDecoder.atIndex(index, sourceMap))
|
||
8 years ago
|
}
|
||
|
})
|
||
|
}
|
||
|
|
||
8 years ago
|
/**
|
||
|
* Return the source location associated with the given @arg pc
|
||
|
*
|
||
|
* @param {String} address - contract address from which the source location is retrieved
|
||
|
* @param {Int} vmtraceStepIndex - index of the current code in the vmtrace
|
||
|
* @param {Object} contractDetails - AST of compiled contracts
|
||
|
* @param {Function} cb - callback function
|
||
|
*/
|
||
8 years ago
|
SourceLocationTracker.prototype.getSourceLocationFromVMTraceIndex = function (address, vmtraceStepIndex, contracts, cb) {
|
||
8 years ago
|
var self = this
|
||
8 years ago
|
extractSourceMap(this.codeManager, address, contracts, function (error, sourceMap) {
|
||
8 years ago
|
if (!error) {
|
||
8 years ago
|
self.codeManager.getInstructionIndex(address, vmtraceStepIndex, function (error, index) {
|
||
|
if (error) {
|
||
|
cb(error)
|
||
|
} else {
|
||
|
cb(null, self.sourceMappingDecoder.atIndex(index, sourceMap))
|
||
|
}
|
||
|
})
|
||
8 years ago
|
} else {
|
||
|
cb(error)
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
|
||
8 years ago
|
function getSourceMap (address, code, contracts) {
|
||
8 years ago
|
var isCreation = helper.isContractCreation(address)
|
||
7 years ago
|
var bytes
|
||
|
for (var file in contracts) {
|
||
|
for (var contract in contracts[file]) {
|
||
|
bytes = isCreation ? contracts[file][contract].evm.bytecode.object : contracts[file][contract].evm.deployedBytecode.object
|
||
|
if (util.compareByteCode(code, '0x' + bytes)) {
|
||
|
return isCreation ? contracts[file][contract].evm.bytecode.sourceMap : contracts[file][contract].evm.deployedBytecode.sourceMap
|
||
|
}
|
||
8 years ago
|
}
|
||
|
}
|
||
|
return null
|
||
|
}
|
||
|
|
||
8 years ago
|
function extractSourceMap (codeManager, address, contracts, cb) {
|
||
|
codeManager.getCode(address, function (error, result) {
|
||
|
if (!error) {
|
||
|
var sourceMap = getSourceMap(address, result.bytecode, contracts)
|
||
|
if (sourceMap) {
|
||
|
cb(null, sourceMap)
|
||
|
} else {
|
||
7 years ago
|
cb('no sourcemap associated with the code ' + address)
|
||
8 years ago
|
}
|
||
|
} else {
|
||
|
cb(error)
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
|
||
8 years ago
|
module.exports = SourceLocationTracker
|