From d91bde3504f77910e0978a0ec24fc96b163ad5ca Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Wed, 10 Jun 2015 19:13:03 +0900 Subject: [PATCH] More ethvm CLI options. --- ethvm/main.cpp | 109 +++++++++++++++++++++++++++++--------- libethereum/Executive.cpp | 27 ++++++---- libethereum/Executive.h | 5 +- 3 files changed, 104 insertions(+), 37 deletions(-) diff --git a/ethvm/main.cpp b/ethvm/main.cpp index e00f03430..ca77d8963 100644 --- a/ethvm/main.cpp +++ b/ethvm/main.cpp @@ -35,28 +35,51 @@ using namespace eth; void help() { cout - << "Usage ethvm " << endl - << "Options:" << endl - ; + << "Usage ethvm [trace|stats|output] (|--)" << endl + << "Transaction options:" << endl + << " --value Transaction should transfer the wei (default: 0)." << endl + << " --gas Transaction should be given gas (default: block gas limit)." << endl + << " --gas-price Transaction's gas price' should be (default: 0)." << endl + << " --sender Transaction sender should be (default: 0000...0069)." << endl + << " --origin Transaction origin should be (default: 0000...0069)." << endl + << "Options for trace:" << endl + << " --flat Minimal whitespace in the JSON." << endl + << " --mnemonics Show instruction mnemonics in the trace (non-standard)." << endl + << endl + << "General options:" << endl + << " -V,--version Show the version and exit." << endl + << " -h,--help Show this help message and exit." << endl; exit(0); } void version() { - cout << "evm version " << dev::Version << endl; + cout << "ethvm version " << dev::Version << endl; + cout << "By Gav Wood, 2015." << endl; + cout << "Build: " << DEV_QUOTED(ETH_BUILD_PLATFORM) << "/" << DEV_QUOTED(ETH_BUILD_TYPE) << endl; exit(0); } +enum class Mode +{ + Trace, + Statistics, + OutputOnly +}; + int main(int argc, char** argv) { string incoming = "--"; + Mode mode = Mode::Statistics; State state; Address sender = Address(69); Address origin = Address(69); u256 value = 0; u256 gas = state.gasLimitRemaining(); u256 gasPrice = 0; + bool styledJson = true; + StandardTrace st; for (int i = 1; i < argc; ++i) { @@ -65,6 +88,30 @@ int main(int argc, char** argv) help(); else if (arg == "-V" || arg == "--version") version(); + else if (arg == "--mnemonics") + st.setShowMnemonics(); + else if (arg == "--flat") + styledJson = false; + else if (arg == "--value" && i + 1 < argc) + value = u256(argv[++i]); + else if (arg == "--sender" && i + 1 < argc) + sender = Address(argv[++i]); + else if (arg == "--origin" && i + 1 < argc) + origin = Address(argv[++i]); + else if (arg == "--gas" && i + 1 < argc) + gas = u256(argv[++i]); + else if (arg == "--gas-price" && i + 1 < argc) + gasPrice = u256(argv[++i]); + else if (arg == "--value" && i + 1 < argc) + value = u256(argv[++i]); + else if (arg == "--value" && i + 1 < argc) + value = u256(argv[++i]); + else if (arg == "stats") + mode = Mode::Statistics; + else if (arg == "output") + mode = Mode::OutputOnly; + else if (arg == "trace") + mode = Mode::Trace; else incoming = arg; } @@ -89,12 +136,17 @@ int main(int argc, char** argv) unordered_map> counts; unsigned total = 0; bigint memTotal; - auto onOp = [&](uint64_t, Instruction inst, bigint m, bigint gasCost, bigint, VM*, ExtVMFace const*) { - counts[(byte)inst].first++; - counts[(byte)inst].second += gasCost; - total++; - if (m > 0) - memTotal = m; + auto onOp = [&](uint64_t step, Instruction inst, bigint m, bigint gasCost, bigint gas, VM* vm, ExtVMFace const* extVM) { + if (mode == Mode::Statistics) + { + counts[(byte)inst].first++; + counts[(byte)inst].second += gasCost; + total++; + if (m > 0) + memTotal = m; + } + else if (mode == Mode::Trace) + st(step, inst, m, gasCost, gas, vm, extVM); }; executive.initialize(t); @@ -104,24 +156,31 @@ int main(int argc, char** argv) double execTime = timer.elapsed(); executive.finalize(); bytes output = std::move(res.output); - LogEntries logs = executive.logs(); - cout << "Gas used: " << res.gasUsed << " (+" << t.gasRequired() << " for transaction, -" << res.gasRefunded << " refunded)" << endl; - cout << "Output: " << toHex(output) << endl; - cout << logs.size() << " logs" << (logs.empty() ? "." : ":") << endl; - for (LogEntry const& l: logs) + if (mode == Mode::Statistics) { - cout << " " << l.address.hex() << ": " << toHex(t.data()) << endl; - for (h256 const& t: l.topics) - cout << " " << t.hex() << endl; - } + cout << "Gas used: " << res.gasUsed << " (+" << t.gasRequired() << " for transaction, -" << res.gasRefunded << " refunded)" << endl; + cout << "Output: " << toHex(output) << endl; + LogEntries logs = executive.logs(); + cout << logs.size() << " logs" << (logs.empty() ? "." : ":") << endl; + for (LogEntry const& l: logs) + { + cout << " " << l.address.hex() << ": " << toHex(t.data()) << endl; + for (h256 const& t: l.topics) + cout << " " << t.hex() << endl; + } - cout << total << " operations in " << execTime << " seconds." << endl; - cout << "Maximum memory usage: " << memTotal * 32 << " bytes" << endl; - cout << "Expensive operations:" << endl; - for (auto const& c: {Instruction::SSTORE, Instruction::SLOAD, Instruction::CALL, Instruction::CREATE, Instruction::CALLCODE, Instruction::MSTORE8, Instruction::MSTORE, Instruction::MLOAD, Instruction::SHA3}) - if (!!counts[(byte)c].first) - cout << " " << instructionInfo(c).name << " x " << counts[(byte)c].first << " (" << counts[(byte)c].second << " gas)" << endl; + cout << total << " operations in " << execTime << " seconds." << endl; + cout << "Maximum memory usage: " << memTotal * 32 << " bytes" << endl; + cout << "Expensive operations:" << endl; + for (auto const& c: {Instruction::SSTORE, Instruction::SLOAD, Instruction::CALL, Instruction::CREATE, Instruction::CALLCODE, Instruction::MSTORE8, Instruction::MSTORE, Instruction::MLOAD, Instruction::SHA3}) + if (!!counts[(byte)c].first) + cout << " " << instructionInfo(c).name << " x " << counts[(byte)c].first << " (" << counts[(byte)c].second << " gas)" << endl; + } + else if (mode == Mode::Trace) + cout << st.json(styledJson); + else if (mode == Mode::OutputOnly) + cout << toHex(output); return 0; } diff --git a/libethereum/Executive.cpp b/libethereum/Executive.cpp index 4e741795c..c8f922561 100644 --- a/libethereum/Executive.cpp +++ b/libethereum/Executive.cpp @@ -46,6 +46,8 @@ bool changesMemory(Instruction _inst) return _inst == Instruction::MSTORE || _inst == Instruction::MSTORE8 || + _inst == Instruction::MLOAD || + _inst == Instruction::CREATE || _inst == Instruction::CALL || _inst == Instruction::CALLCODE || _inst == Instruction::SHA3 || @@ -71,6 +73,7 @@ void StandardTrace::operator()(uint64_t _steps, Instruction inst, bigint newMemS stack.append(toHex(toCompactBigEndian(i), 1)); r["stack"] = stack; + bool returned = false; bool newContext = false; Instruction lastInst = Instruction::STOP; @@ -84,6 +87,7 @@ void StandardTrace::operator()(uint64_t _steps, Instruction inst, bigint newMemS else if (m_lastInst.size() == ext.depth + 2) { // returned from old context + returned = true; m_lastInst.pop_back(); lastInst = m_lastInst.back(); } @@ -91,6 +95,7 @@ void StandardTrace::operator()(uint64_t _steps, Instruction inst, bigint newMemS { // continuing in previous context lastInst = m_lastInst.back(); + m_lastInst.back() = inst; } else { @@ -100,12 +105,7 @@ void StandardTrace::operator()(uint64_t _steps, Instruction inst, bigint newMemS } if (changesMemory(lastInst) || newContext) - { - Json::Value mem(Json::arrayValue); - for (auto const& i: vm.memory()) - mem.append(toHex(toCompactBigEndian(i), 1)); - r["memory"] = mem; - } + r["memory"] = toHex(vm.memory()); if (changesStorage(lastInst) || newContext) { @@ -115,21 +115,26 @@ void StandardTrace::operator()(uint64_t _steps, Instruction inst, bigint newMemS r["storage"] = storage; } - r["depth"] = ext.depth; - r["address"] = ext.myAddress.hex(); + if (returned) + r["depth"] = ext.depth; + if (newContext) + r["address"] = ext.myAddress.hex(); r["steps"] = (unsigned)_steps; r["inst"] = (unsigned)inst; + if (m_showMnemonics) + r["instname"] = instructionInfo(inst).name; r["pc"] = toString(vm.curPC()); r["gas"] = toString(gas); r["gascost"] = toString(gasCost); - r["memexpand"] = toString(newMemSize); + if (!!newMemSize) + r["memexpand"] = toString(newMemSize); m_trace->append(r); } -string StandardTrace::json() const +string StandardTrace::json(bool _styled) const { - return Json::FastWriter().write(*m_trace); + return _styled ? Json::StyledWriter().write(*m_trace) : Json::FastWriter().write(*m_trace); } Executive::Executive(State& _s, BlockChain const& _bc, unsigned _level): diff --git a/libethereum/Executive.h b/libethereum/Executive.h index e2c910cd0..aee0880ad 100644 --- a/libethereum/Executive.h +++ b/libethereum/Executive.h @@ -49,9 +49,12 @@ public: StandardTrace(); void operator()(uint64_t _steps, Instruction _inst, bigint _newMemSize, bigint _gasCost, bigint _gas, VM* _vm, ExtVMFace const* _extVM); - std::string json() const; + void setShowMnemonics() { m_showMnemonics = true; } + + std::string json(bool _styled = false) const; private: + bool m_showMnemonics = false; std::vector m_lastInst; std::shared_ptr m_trace; };