forked from ethereum/solidity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInspector.cpp
153 lines (124 loc) · 4.03 KB
/
Inspector.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
This file is part of solidity.
solidity 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.
solidity 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 solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
/**
* Yul interpreter.
*/
#include <test/tools/yulInterpreter/Inspector.h>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/algorithm/string.hpp>
using namespace solidity;
using namespace solidity::yul;
using namespace solidity::yul::test;
namespace
{
void printVariable(std::string_view const _name, u256 const& _value)
{
std::cout << "\t" << _name << " = " << _value.str();
if (_value != 0)
std::cout << " (" << toCompactHexWithPrefix(_value) << ")";
std::cout << std::endl;
}
}
void InspectedInterpreter::run(
std::shared_ptr<Inspector> _inspector,
InterpreterState& _state,
AST const& _ast,
bool _disableExternalCalls,
bool _disableMemoryTrace
)
{
Scope scope;
InspectedInterpreter{_inspector, _state, _ast.dialect(), scope, _disableExternalCalls, _disableMemoryTrace}(_ast.root());
}
Inspector::NodeAction Inspector::queryUser(langutil::DebugData const& _data, std::map<YulName, u256> const& _variables)
{
if (m_stepMode == NodeAction::RunNode)
{
// Output instructions that are being skipped/run
std::cout << "Running " << currentSource(_data) << std::endl;
return NodeAction::StepThroughNode;
}
std::string input;
while (true)
{
// Output sourcecode about to run.
std::cout << "> " << currentSource(_data) << std::endl;
// Ask user for action
std::cout << std::endl
<< "(s)tep/(n)ext/(i)nspect/(p)rint/all (v)ariables?"
<< std::endl
<< "# ";
std::cout.flush();
std::getline(std::cin, input);
boost::algorithm::trim(input);
// Imitate GDB and repeat last cmd for empty string input.
if (input == "")
input = m_lastInput;
else
m_lastInput = input;
if (input == "next" || input == "n")
return NodeAction::RunNode;
else if (input == "step" || input == "s")
return NodeAction::StepThroughNode;
else if (input == "inspect" || input == "i")
m_state.dumpTraceAndState(std::cout, false);
else if (input == "variables" || input == "v")
{
for (auto &&[yulStr, val]: _variables)
printVariable(yulStr.str(), val);
std::cout << std::endl;
}
else if (
boost::starts_with(input, "print") ||
boost::starts_with(input, "p")
)
{
size_t whitespacePos = input.find(' ');
if (whitespacePos == std::string::npos)
std::cout << "Error parsing command! Expected variable name." << std::endl;
std::string const varname = input.substr(whitespacePos + 1);
std::vector<std::string> candidates;
bool found = false;
for (auto &&[yulStr, val]: _variables)
if (yulStr.str() == varname)
{
printVariable(varname, val);
found = true;
break;
}
if (!found)
std::cout << varname << " not found." << std::endl;
}
}
}
std::string Inspector::currentSource(langutil::DebugData const& _data) const
{
return m_source.substr(
static_cast<size_t>(_data.nativeLocation.start),
static_cast<size_t>(_data.nativeLocation.end - _data.nativeLocation.start)
);
}
u256 InspectedInterpreter::evaluate(Expression const& _expression)
{
InspectedExpressionEvaluator ev(m_inspector, m_state, m_dialect, *m_scope, m_variables, m_disableExternalCalls, m_disableMemoryTrace);
ev.visit(_expression);
return ev.value();
}
std::vector<u256> InspectedInterpreter::evaluateMulti(Expression const& _expression)
{
InspectedExpressionEvaluator ev(m_inspector, m_state, m_dialect, *m_scope, m_variables, m_disableExternalCalls, m_disableMemoryTrace);
ev.visit(_expression);
return ev.values();
}