trace.rs

Overview

This file provides tracing and debugging utilities for the execution of a virtual machine engine, specifically designed to handle detailed runtime information during the execution of commands. It includes functionality to convert internal engine trace information into a serializable data structure and to output formatted trace logs based on configurable trace bits set on the engine. The file focuses on capturing and formatting information about command execution steps, gas usage, stack state, control states, and exceptions encountered during execution.


Components

Conversion Implementation: From<&EngineTraceInfo<'_>> for EngineTraceInfoData

This impl block implements a conversion from a borrowed reference of EngineTraceInfo to an owned EngineTraceInfoData struct. This conversion extracts and formats relevant fields from the raw EngineTraceInfo for easier inspection or serialization.

Details:

Extracted Fields:

Usage Example:

let trace_info_data: EngineTraceInfoData = (&engine_trace_info).into();

Function: simple_trace_callback

A callback function designed for logging trace information from the virtual machine engine during execution. It selectively logs messages depending on the trace info type and the trace bits enabled in the engine.

Parameters:

Behavior:

Trace Bits Checked:

Usage Example:

engine.set_trace_callback(simple_trace_callback);

Function: dump_stack_result

Converts the current stack state into a concise string representation for logging or debugging purposes.

Parameters:

Return:

Details:

Implementation Notes:

Usage Example:

let stack_str = dump_stack_result(&engine.current_stack());
println!("Stack: {}", stack_str);

Interaction with Other Components


Implementation Details and Algorithms


Diagram: Functional Flow of Trace Logging

flowchart TD
Engine -->|executes commands| EngineTraceInfo
EngineTraceInfo -->|converted to| EngineTraceInfoData
Engine -->|provides trace bits| simple_trace_callback
EngineTraceInfo -->|passes info| simple_trace_callback
simple_trace_callback -->|logs command string| Tracing[tracing::info! / trace!]
simple_trace_callback -->|calls| dump_stack_result
dump_stack_result -->|returns formatted stack string| simple_trace_callback
simple_trace_callback -->|logs stack info| Tracing
simple_trace_callback -->|logs control states| Tracing
simple_trace_callback -->|logs gas usage| Tracing

The diagram shows how the Engine produces EngineTraceInfo during execution, which is converted and passed to the simple_trace_callback. The callback checks engine trace bits to decide which aspects (command, stack, controls, gas) to log, using dump_stack_result to format the stack.


References