/* This file is part of cpp-ethereum. cpp-ethereum is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. cpp-ethereum is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with cpp-ethereum. If not, see . */ /** @file AssemblyDebuggerCtrl.h * @author Yann yann@ethdev.com * @date 2014 * display opcode debugging. */ #include #include #include #include #include #include #include "libethereum/Transaction.h" #include "AssemblyDebuggerModel.h" #include "AssemblyDebuggerCtrl.h" #include "KeyEventManager.h" #include "AppContext.h" #include "DebuggingStateWrapper.h" #include "TransactionListModel.h" #include "QContractDefinition.h" #include "QVariableDeclaration.h" #include "ContractCallDataEncoder.h" using namespace dev::eth; using namespace dev::mix; AssemblyDebuggerCtrl::AssemblyDebuggerCtrl(QTextDocument* _doc): Extension(ExtensionDisplayBehavior::ModalDialog) { qRegisterMetaType("QVariableDefinition*"); qRegisterMetaType("QVariableDefinitionList*"); qRegisterMetaType>("QList"); qRegisterMetaType("QVariableDeclaration*"); qRegisterMetaType(); qRegisterMetaType(); connect(this, SIGNAL(dataAvailable(bool, DebuggingStatusResult, QList, QList, AssemblyDebuggerData)), this, SLOT(updateGUI(bool, DebuggingStatusResult, QList, QList, AssemblyDebuggerData)), Qt::QueuedConnection); m_modelDebugger = std::unique_ptr(new AssemblyDebuggerModel); m_compilation = std::unique_ptr(new ConstantCompilationModel); m_doc = _doc; } QString AssemblyDebuggerCtrl::contentUrl() const { return QStringLiteral("qrc:/qml/Debugger.qml"); } QString AssemblyDebuggerCtrl::title() const { return QApplication::tr("debugger"); } void AssemblyDebuggerCtrl::start() const { //start to listen on F5 m_ctx->getKeyEventManager()->registerEvent(this, SLOT(keyPressed(int))); } void AssemblyDebuggerCtrl::keyPressed(int _key) { if (_key == Qt::Key_F5) { QtConcurrent::run([this]() { deployContract(m_doc->toPlainText()); }); } } void AssemblyDebuggerCtrl::callContract(dev::mix::TransactionSettings _tr) { CompilerResult compilerRes = m_compilation.get()->compile(m_doc->toPlainText()); if (!compilerRes.success) { AppContext::getInstance()->displayMessageDialog("debugger","compilation failed"); return; } ContractCallDataEncoder c; std::shared_ptr contractDef = QContractDefinition::Contract(m_doc->toPlainText()); QFunctionDefinition* f = nullptr; for (int k = 0; k < contractDef.get()->functions().size(); k++) { if (contractDef.get()->functions().at(k)->name() == _tr.functionId) { f = (QFunctionDefinition*)contractDef->functions().at(k); } } if (!f) { AppContext::getInstance()->displayMessageDialog("debugger","contract code changed. redeploy contract"); return; } c.encode(f->index()); for (int k = 0; k < f->parameters().size(); k++) { QVariableDeclaration* var = (QVariableDeclaration*)f->parameters().at(k); c.encode(var, _tr.parameterValues[var->name()]); } DebuggingContent debuggingContent = m_modelDebugger->getContractCallDebugStates(m_previousDebugResult.contractAddress, c.encodedData(), _tr); debuggingContent.returnParameters = c.decode(f->returnParameters(), debuggingContent.returnValue); finalizeExecution(debuggingContent); } void AssemblyDebuggerCtrl::deployContract(QString _source) { CompilerResult compilerRes = m_compilation.get()->compile(_source); if (!compilerRes.success) { emit dataAvailable(false, DebuggingStatusResult::Compilationfailed); return; } m_previousDebugResult = m_modelDebugger->getContractInitiationDebugStates(compilerRes.bytes); finalizeExecution(m_previousDebugResult); } void AssemblyDebuggerCtrl::finalizeExecution(DebuggingContent _debuggingContent) { //we need to wrap states in a QObject before sending to QML. QList wStates; for(int i = 0; i < _debuggingContent.machineStates.size(); i++) { DebuggingStateWrapper* s = new DebuggingStateWrapper(_debuggingContent.executionCode, _debuggingContent.executionData.toBytes(), this); s->setState(_debuggingContent.machineStates.at(i)); wStates.append(s); } AssemblyDebuggerData code = DebuggingStateWrapper::getHumanReadableCode(_debuggingContent.executionCode, this); emit dataAvailable(true, DebuggingStatusResult::Ok, _debuggingContent.returnParameters, wStates, code); } void AssemblyDebuggerCtrl::updateGUI(bool _success, DebuggingStatusResult _reason, QList _returnParam, QList _wStates, AssemblyDebuggerData _code) { Q_UNUSED(_reason); if (_success) { m_appEngine->rootContext()->setContextProperty("debugStates", QVariant::fromValue(_wStates)); m_appEngine->rootContext()->setContextProperty("humanReadableExecutionCode", QVariant::fromValue(std::get<0>(_code))); m_appEngine->rootContext()->setContextProperty("bytesCodeMapping", QVariant::fromValue(std::get<1>(_code))); m_appEngine->rootContext()->setContextProperty("contractCallReturnParameters", QVariant::fromValue(new QVariableDefinitionList(_returnParam))); this->addContentOn(this); } else m_ctx->displayMessageDialog(QApplication::tr("debugger"), QApplication::tr("compilation failed")); } void AssemblyDebuggerCtrl::runTransaction(TransactionSettings _tr) { QtConcurrent::run([this, _tr]() { callContract(_tr); }); }