embark/lib/modules/debugger/index.js

293 lines
9.3 KiB
JavaScript
Raw Normal View History

var DebuggerManager = require('./debugger_manager.js');
class TransactionDebugger {
constructor(embark, _options) {
2018-10-29 13:15:48 +00:00
this.embark = embark;
2018-10-29 13:15:48 +00:00
this.debugger_manager = new DebuggerManager("http://localhost:8545");
embark.events.on('contracts:compile:solc', this.debugger_manager.setInputJson.bind(this.debugger_manager));
embark.events.on('contracts:compiled:solc', this.debugger_manager.setOutputJson.bind(this.debugger_manager));
2018-10-29 13:15:48 +00:00
this.tx_tracker = {};
this.last_tx = "";
2018-10-29 13:15:48 +00:00
this.isDebugging = false;
this.listenToEvents();
this.listenToCommands();
this.listentoAPI();
}
listenToEvents() {
2018-10-29 13:15:48 +00:00
const self = this;
this.embark.events.on('blockchain:tx', (tx) => {
this.embark.events.request("contracts:contract", tx.name, (contract) => {
2018-10-29 13:15:48 +00:00
self.tx_tracker[tx.transactionHash] = {tx: tx, contract: contract};
self.last_tx = tx.transactionHash;
if (tx.status !== '0x0') return;
self.embark.logger.info("Transaction failed");
self.debugger_manager.getLastLine(tx.transactionHash, contract.filename, (lines, line, known_vars) => {
lines.forEach((line) => {
2018-10-29 13:15:48 +00:00
self.embark.logger.error(line);
});
2018-10-29 14:33:12 +00:00
self.find_vars_in_line(tx.transactionHash, line, known_vars, (found_vars) => {
if (!found_vars) return;
self.embark.logger.info("vars:");
found_vars.forEach((variable) => {
self.embark.logger.info(`${variable.name}: ${variable.value}`);
});
});
2018-10-29 13:15:48 +00:00
});
});
});
}
2018-10-29 14:33:12 +00:00
find_vars_in_line(txHash, line, known_vars, cb) {
let found_vars = [];
this.getGlobals(txHash, (err, globals) => {
if (err) return cb([]);
for (let variable in globals) {
2018-10-29 13:15:48 +00:00
let value = globals[variable];
2018-10-29 14:33:12 +00:00
if (line.indexOf(variable) >= 0) {
found_vars.push({name: variable, value: value});
2018-10-29 14:33:12 +00:00
}
}
2018-10-29 14:33:12 +00:00
for (let variable in known_vars.locals) {
let value = known_vars.locals[variable];
2018-10-29 13:15:48 +00:00
let variable_name = variable.split(' ')[0];
2018-10-29 14:33:12 +00:00
if (line.indexOf(variable_name) >= 0) {
found_vars.push({name: variable, value: value});
2018-10-29 14:33:12 +00:00
}
}
2018-10-29 14:33:12 +00:00
for (let variable in known_vars.contract) {
let value = known_vars.contract[variable];
2018-10-29 13:15:48 +00:00
let variable_name = variable.split(' ')[0];
2018-10-29 14:33:12 +00:00
if (line.indexOf(variable_name) >= 0) {
found_vars.push({name: variable, value: value});
2018-10-29 14:33:12 +00:00
}
}
cb(found_vars);
2018-10-29 13:15:48 +00:00
});
}
listentoAPI() {
2018-10-29 13:15:48 +00:00
this.debuggerData = {};
this.apiDebugger = false;
this.embark.registerAPICall('post', '/embark-api/debugger/start', (req, res) => {
2018-10-29 13:15:48 +00:00
let txHash = req.body.params.txHash;
this.embark.events.request("contracts:contract:byTxHash", txHash, (err, contract) => {
if (err) {
this.embark.logger.error(err);
2018-10-29 13:15:48 +00:00
return res.send({error: err});
}
2018-10-29 13:15:48 +00:00
let filename = contract.filename;
this.apiDebugger = this.debugger_manager.createDebuggerSession(txHash, filename, () => {
2018-10-29 14:33:12 +00:00
this.getGlobals(txHash, (err, globals) => {
if (err) return res.send({ok: false});
this.debuggerData.globals = globals;
res.send({ok :true});
});
2018-10-29 13:15:48 +00:00
});
});
});
this.embark.registerAPICall('post', '/embark-api/debugger/JumpBack', (req, res) => {
2018-10-29 13:15:48 +00:00
this.apiDebugger.stepJumpNextBreakpoint();
res.send({ok :true});
});
this.embark.registerAPICall('post', '/embark-api/debugger/JumpForward', (req, res) => {
2018-10-29 13:15:48 +00:00
this.apiDebugger.stepJumpPreviousBreakpoint();
res.send({ok :true});
});
this.embark.registerAPICall('post', '/embark-api/debugger/StepOverForward', (req, res) => {
2018-10-29 13:15:48 +00:00
this.apiDebugger.stepOverForward(true);
res.send({ok :true});
});
this.embark.registerAPICall('post', '/embark-api/debugger/StepOverBackward', (req, res) => {
2018-10-29 13:15:48 +00:00
this.apiDebugger.stepOverBack(true);
res.send({ok :true});
});
this.embark.registerAPICall('post', '/embark-api/debugger/StepIntoForward', (req, res) => {
2018-10-29 13:15:48 +00:00
this.apiDebugger.stepIntoForward(true);
res.send({ok :true});
});
this.embark.registerAPICall('post', '/embark-api/debugger/StepIntoBackward', (req, res) => {
2018-10-29 13:15:48 +00:00
this.apiDebugger.stepIntoBack(true);
res.send({ok :true});
});
this.embark.registerAPICall('post', '/embark-api/debugger/breakpoint', (req, res) => {
2018-10-29 13:15:48 +00:00
console.dir("new breakpoint");
res.send({ok :true});
});
this.embark.registerAPICall('ws', '/embark-api/debugger', (ws, _req) => {
2018-10-29 13:15:48 +00:00
if (!this.apiDebugger) return;
this.apiDebugger.events.on("source", (lineColumnPos, rawLocation) => {
2018-10-29 13:15:48 +00:00
this.debuggerData.sources = {lineColumnPos, rawLocation};
ws.send(JSON.stringify(this.debuggerData), () => {});
});
2018-10-29 14:33:12 +00:00
this.apiDebugger.events.on("locals", (data) => {
this.debuggerData.locals = this.simplifyDebuggerVars(data);
ws.send(JSON.stringify(this.debuggerData), () => {});
});
this.apiDebugger.events.on("globals", (data) => {
2018-10-29 13:15:48 +00:00
this.debuggerData.contract = this.simplifyDebuggerVars(data);
ws.send(JSON.stringify(this.debuggerData), () => {});
});
});
}
simplifyDebuggerVars(data) {
let new_data = {};
for (let key in data) {
let field = data[key];
2018-10-29 13:15:48 +00:00
new_data[`${key} (${field.type})`] = field.value;
}
2018-10-29 13:15:48 +00:00
return new_data;
}
listenToCommands() {
2018-10-29 13:15:48 +00:00
const self = this;
this.cmdDebugger = false;
2018-10-29 14:33:12 +00:00
this.currentCmdTxHash = "";
this.embark.registerConsoleCommand((cmd, _options) => {
2018-10-29 13:15:48 +00:00
let cmdName = cmd.split(" ")[0];
let txHash = cmd.split(" ")[1];
return {
match: () => cmdName === 'debug',
2018-10-29 14:33:12 +00:00
process: (_cb) => {
if (txHash) {
this.embark.events.request("contracts:contract:byTxHash", txHash, (err, contract) => {
if (err) {
this.embark.logger.error(err);
return;
}
2018-10-29 13:15:48 +00:00
let filename = contract.filename;
2018-10-29 14:33:12 +00:00
self.currentCmdTxHash = txHash;
self.cmdDebugger = self.debugger_manager.createDebuggerSession(txHash, filename, () => {
self.cmdDebugger.getSource().forEach((line) => {
2018-10-29 13:15:48 +00:00
console.dir(line);
});
});
});
2018-10-29 13:15:48 +00:00
return;
}
2018-10-29 14:33:12 +00:00
self.currentCmdTxHash = self.last_tx;
2018-10-29 13:15:48 +00:00
let filename = self.tx_tracker[self.last_tx].contract.filename;
self.cmdDebugger = self.debugger_manager.createDebuggerSession(self.last_tx, filename, () => {
self.cmdDebugger.getSource().forEach((line) => {
2018-10-29 13:15:48 +00:00
console.dir(line);
});
});
}
};
2018-10-29 13:15:48 +00:00
});
this.embark.registerConsoleCommand((cmd, _options) => {
return {
match: () => (cmd === 'next' || cmd === 'n'),
2018-10-29 14:33:12 +00:00
process: (_cb) => {
if (!self.cmdDebugger.currentStep()) {
2018-10-29 13:15:48 +00:00
console.dir("end of execution reached");
return self.cmdDebugger.unload();
}
2018-10-29 13:15:48 +00:00
self.cmdDebugger.stepOverForward(true);
self.cmdDebugger.getSource().forEach((line) => {
2018-10-29 13:15:48 +00:00
console.dir(line);
});
}
};
2018-10-29 13:15:48 +00:00
});
this.embark.registerConsoleCommand((cmd, _options) => {
return {
match: () => (cmd === 'previous' || cmd === 'p'),
2018-10-29 14:33:12 +00:00
process: (_cb) => {
if (!self.cmdDebugger.currentStep()) {
2018-10-29 13:15:48 +00:00
console.dir("end of execution reached");
return self.cmdDebugger.unload();
}
2018-10-29 13:15:48 +00:00
self.cmdDebugger.stepOverBack(true);
self.cmdDebugger.getSource().forEach((line) => {
2018-10-29 13:15:48 +00:00
console.dir(line);
});
}
};
2018-10-29 13:15:48 +00:00
});
this.embark.registerConsoleCommand((cmd, _options) => {
return {
match: () => (cmd === 'var local' || cmd === 'v l' || cmd === 'vl'),
2018-10-29 14:33:12 +00:00
process: (_cb) => {
2018-10-29 13:15:48 +00:00
self.cmdDebugger.displayLocals();
}
};
2018-10-29 13:15:48 +00:00
});
this.embark.registerConsoleCommand((cmd, _options) => {
return {
match: () => (cmd === 'var global' || cmd === 'v g' || cmd === 'vg'),
2018-10-29 14:33:12 +00:00
process: (_cb) => {
2018-10-29 13:15:48 +00:00
self.cmdDebugger.displayGlobals();
}
};
2018-10-29 13:15:48 +00:00
});
this.embark.registerConsoleCommand((cmd, _options) => {
return {
match: () => (cmd === 'var all' || cmd === 'v a' || cmd === 'va'),
2018-10-29 14:33:12 +00:00
process: (_cb) => {
2018-10-29 14:33:12 +00:00
self.getGlobals((err, globals) => {
if (err) return self.embark.logger.error(err);
console.dir(globals);
});
}
};
2018-10-29 13:15:48 +00:00
});
}
2018-10-29 14:33:12 +00:00
getGlobals(txHash, cb) {
let globals = {};
this.embark.events.request("blockchain:getTransaction", txHash, (err, tx) => {
if (err) return cb(err);
this.embark.events.request("blockchain:block:byHash", tx.blockHash, (err, block) => {
if (err) return cb(err);
globals["block.blockHash"] = tx.blockHash;
globals["block.number"] = tx.blockNumber;
globals["block.coinbase"] = block.miner;
globals["block.difficulty"] = block.difficulty;
globals["block.gaslimit"] = block.gasLimit;
globals["block.timestamp"] = block.timestamp;
globals["msg.sender"] = tx.from;
globals["msg.gas"] = tx.gas;
globals["msg.gasPrice"] = tx.gasPrice;
globals["msg.value"] = tx.value;
globals["now"] = block.timestamp;
cb(null, globals);
});
});
}
}
2018-10-29 13:15:48 +00:00
module.exports = TransactionDebugger;