-
-
Notifications
You must be signed in to change notification settings - Fork 601
Add MVP of virtual machine tracer #4931
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nekevss
wants to merge
1
commit into
boa-dev:main
Choose a base branch
from
nekevss:rework-vm-tracing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| use std::time::Duration; | ||
|
|
||
| /// The call frame name | ||
| /// | ||
| /// This will have the name of the call frame provided or `Global` it's | ||
| /// the global call frame. | ||
| #[derive(Debug, Clone)] | ||
| pub enum CallFrameName { | ||
| Global, | ||
| Name(String), | ||
| } | ||
|
|
||
| /// A message that is emitted at the beginning of execution | ||
| #[derive(Debug, Clone)] | ||
| pub struct ExecutionStartMessage { | ||
| pub call_frame_name: CallFrameName, | ||
| } | ||
|
|
||
| /// A message that emits details about a call frame | ||
| #[derive(Debug, Clone)] | ||
| pub struct CallFrameMessage { | ||
| pub bytecode: String, | ||
| } | ||
|
|
||
| /// A message that emits instruction execution details about a call frame | ||
| #[derive(Debug, Clone)] | ||
| pub struct OpcodeExecutionMessage { | ||
| pub opcode: &'static str, | ||
| pub duration: Duration, | ||
| pub operands: String, | ||
| pub stack: String, | ||
| } | ||
|
|
||
| /// The various events that are emitted from Boa's virtual machine. | ||
| #[derive(Debug, Clone)] | ||
| pub enum VirtualMachineEvent { | ||
| /// This event is the first event triggered. | ||
| /// | ||
| /// It emits information about the call frame. | ||
| CallFrameTrace(CallFrameMessage), | ||
| /// This event is triggered when the execution of a call frame is starting. | ||
| ExecutionStart(ExecutionStartMessage), | ||
| /// This event is triggered when executing an operation. | ||
| /// | ||
| /// It provides information about the opcode execution | ||
| ExecutionTrace(OpcodeExecutionMessage), | ||
| /// This event is triggered when a opcode that calls is reached. | ||
| /// | ||
| /// It signals that we about about to switch call frames. | ||
| ExecutionCallEvent, | ||
| } | ||
|
|
||
| /// A trait to define a tracer that plugs into Boa's `Vm` | ||
| pub trait VirtualMachineTracer: std::fmt::Debug { | ||
| /// Emits `VirtualMachineEvent`s from the virtual machine during execution | ||
| fn emit_event(&self, _event: VirtualMachineEvent) {} | ||
| } | ||
|
|
||
| /// A default empty virtual machine tracer that drops events submitted to it. | ||
| #[derive(Debug, Clone, Copy)] | ||
| pub struct EmptyTracer; | ||
|
|
||
| impl VirtualMachineTracer for EmptyTracer {} | ||
|
|
||
| /// `StdoutTracer` is a `VirtualMachineTracer` implementation that prints the events | ||
| /// to stdout in a specific format. | ||
| #[derive(Debug, Clone, Copy)] | ||
| pub struct StdoutTracer; | ||
|
|
||
| impl StdoutTracer { | ||
| const COLUMN_WIDTH: usize = 26; | ||
| const TIME_COLUMN_WIDTH: usize = Self::COLUMN_WIDTH / 2; | ||
| const OPCODE_COLUMN_WIDTH: usize = Self::COLUMN_WIDTH; | ||
| const OPERAND_COLUMN_WIDTH: usize = Self::COLUMN_WIDTH; | ||
| const NUMBER_OF_COLUMNS: usize = 4; | ||
| } | ||
|
|
||
| #[allow(clippy::print_stdout)] | ||
| impl VirtualMachineTracer for StdoutTracer { | ||
| fn emit_event(&self, event: VirtualMachineEvent) { | ||
| match event { | ||
| VirtualMachineEvent::ExecutionStart(start_message) => { | ||
| let msg = match start_message.call_frame_name { | ||
| CallFrameName::Global => " VM Start ".to_string(), | ||
| CallFrameName::Name(name) => { | ||
| format!(" Call Frame -- {name} ") | ||
| } | ||
| }; | ||
|
|
||
| println!( | ||
| "{msg:-^width$}", | ||
| width = Self::COLUMN_WIDTH * Self::NUMBER_OF_COLUMNS - 10 | ||
| ); | ||
| println!( | ||
| "{:<TIME_COLUMN_WIDTH$} {:<OPCODE_COLUMN_WIDTH$} {:<OPERAND_COLUMN_WIDTH$} Stack\n", | ||
| "Time", | ||
| "Opcode", | ||
| "Operands", | ||
| TIME_COLUMN_WIDTH = Self::TIME_COLUMN_WIDTH, | ||
| OPCODE_COLUMN_WIDTH = Self::OPCODE_COLUMN_WIDTH, | ||
| OPERAND_COLUMN_WIDTH = Self::OPERAND_COLUMN_WIDTH, | ||
| ); | ||
| } | ||
| VirtualMachineEvent::ExecutionCallEvent => println!(), | ||
| VirtualMachineEvent::CallFrameTrace(call_frame_message) => { | ||
| println!("{}", call_frame_message.bytecode); | ||
| } | ||
| VirtualMachineEvent::ExecutionTrace(execution_message) => { | ||
| let OpcodeExecutionMessage { | ||
| opcode, | ||
| duration, | ||
| operands, | ||
| stack, | ||
| } = execution_message; | ||
|
|
||
| println!( | ||
| "{:<TIME_COLUMN_WIDTH$} {opcode:<OPCODE_COLUMN_WIDTH$} {operands:<OPERAND_COLUMN_WIDTH$} {stack}", | ||
| format!("{}μs", duration.as_micros()), | ||
| TIME_COLUMN_WIDTH = Self::TIME_COLUMN_WIDTH, | ||
| OPCODE_COLUMN_WIDTH = Self::OPCODE_COLUMN_WIDTH, | ||
| OPERAND_COLUMN_WIDTH = Self::OPERAND_COLUMN_WIDTH, | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be better to granularize this, because some tracers would need to have the individual operands and stack values instead of a big string; I'm thinking of like a web-based tracer that displays the stack in a more dynamic way, for example.