Skip to content

Commit

Permalink
tracing: Get names from instr::traits[]
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast committed Nov 29, 2022
1 parent 12009ab commit 8a25cfd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 18 deletions.
24 changes: 8 additions & 16 deletions lib/evmone/tracing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ namespace evmone
{
namespace
{
std::string get_name(const char* const* names, uint8_t opcode)
std::string get_name(uint8_t opcode)
{
const auto name = names[opcode];
// TODO: Create constexpr tables of names (maybe even per revision).
const auto name = instr::traits[opcode].name;
return (name != nullptr) ? name : "0x" + evmc::hex(opcode);
}

Expand All @@ -25,21 +26,18 @@ class HistogramTracer : public Tracer
{
const int32_t depth;
const uint8_t* const code;
const char* const* const opcode_names;
uint32_t counts[256]{};

Context(int32_t _depth, const uint8_t* _code, const char* const* _opcode_names) noexcept
: depth{_depth}, code{_code}, opcode_names{_opcode_names}
{}
Context(int32_t _depth, const uint8_t* _code) noexcept : depth{_depth}, code{_code} {}
};

std::stack<Context> m_contexts;
std::ostream& m_out;

void on_execution_start(
evmc_revision rev, const evmc_message& msg, bytes_view code) noexcept override
evmc_revision /*rev*/, const evmc_message& msg, bytes_view code) noexcept override
{
m_contexts.emplace(msg.depth, code.data(), evmc_get_instruction_names_table(rev));
m_contexts.emplace(msg.depth, code.data());
}

void on_instruction_start(uint32_t pc, const intx::uint256* /*stack_top*/, int /*stack_height*/,
Expand All @@ -52,13 +50,12 @@ class HistogramTracer : public Tracer
void on_execution_end(const evmc_result& /*result*/) noexcept override
{
const auto& ctx = m_contexts.top();
const auto names = ctx.opcode_names;

m_out << "--- # HISTOGRAM depth=" << ctx.depth << "\nopcode,count\n";
for (size_t i = 0; i < std::size(ctx.counts); ++i)
{
if (ctx.counts[i] != 0)
m_out << get_name(names, static_cast<uint8_t>(i)) << ',' << ctx.counts[i] << '\n';
m_out << get_name(static_cast<uint8_t>(i)) << ',' << ctx.counts[i] << '\n';
}

m_contexts.pop();
Expand All @@ -80,7 +77,6 @@ class InstructionTracer : public Tracer
};

std::stack<Context> m_contexts;
const char* const* m_opcode_names = nullptr;
std::ostream& m_out; ///< Output stream.

void output_stack(const intx::uint256* stack_top, int stack_height)
Expand All @@ -100,8 +96,6 @@ class InstructionTracer : public Tracer
void on_execution_start(
evmc_revision rev, const evmc_message& msg, bytes_view code) noexcept override
{
if (m_contexts.empty())
m_opcode_names = evmc_get_instruction_names_table(rev);
m_contexts.emplace(code.data(), msg.gas);

m_out << "{";
Expand All @@ -120,7 +114,7 @@ class InstructionTracer : public Tracer
m_out << "{";
m_out << R"("pc":)" << pc;
m_out << R"(,"op":)" << int{opcode};
m_out << R"(,"opName":")" << get_name(m_opcode_names, opcode) << '"';
m_out << R"(,"opName":")" << get_name(opcode) << '"';
m_out << R"(,"gas":)" << state.gas_left;
output_stack(stack_top, stack_height);

Expand All @@ -147,8 +141,6 @@ class InstructionTracer : public Tracer
m_out << "}\n";

m_contexts.pop();
if (m_contexts.empty())
m_opcode_names = nullptr;
}

public:
Expand Down
4 changes: 2 additions & 2 deletions test/unittests/tracing_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <evmc/evmc.hpp>
#include <evmc/mocked_host.hpp>
#include <evmone/evmone.h>
#include <evmone/instructions_traits.hpp>
#include <evmone/tracing.hpp>
#include <evmone/vm.hpp>
#include <gmock/gmock.h>
Expand Down Expand Up @@ -58,8 +59,7 @@ class tracing : public Test
int /*stack_height*/, const evmone::ExecutionState& /*state*/) noexcept override
{
const auto opcode = m_code[pc];
m_trace << m_name << pc << ":"
<< evmc_get_instruction_names_table(EVMC_MAX_REVISION)[opcode] << " ";
m_trace << m_name << pc << ":" << evmone::instr::traits[opcode].name << " ";
}

public:
Expand Down

0 comments on commit 8a25cfd

Please sign in to comment.