From 57d0dbb1c62401559533ced7c1c686da5125df75 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sat, 13 Aug 2022 21:29:30 +0300 Subject: feat(tvix/eval): implement optional runtime tracing This adds a `disassembler` feature to the crate configuration that traces the operations executed and the state of the stack at runtime. This can be enabled by compiling with `--feature disassembler`. This will also gain a more sensible layout of code slices eventually. Change-Id: I34c15e1cd346ecc4362b5afba6bf82dd49359d20 Reviewed-on: https://cl.tvl.fyi/c/depot/+/6193 Tested-by: BuildkiteCI Reviewed-by: sterni --- tvix/eval/src/disassembler.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 tvix/eval/src/disassembler.rs (limited to 'tvix/eval/src/disassembler.rs') diff --git a/tvix/eval/src/disassembler.rs b/tvix/eval/src/disassembler.rs new file mode 100644 index 000000000000..98a6dac9afc4 --- /dev/null +++ b/tvix/eval/src/disassembler.rs @@ -0,0 +1,37 @@ +//! Implements methods for disassembling and printing a representation +//! of compiled code, as well as tracing the runtime stack during +//! execution. +use std::io::{Stderr, Write}; +use tabwriter::TabWriter; + +use crate::opcode::OpCode; +use crate::value::Value; + +/// Helper struct to trace runtime values and automatically flush the +/// output after the value is dropped (i.e. in both success and +/// failure exits from the VM). +pub struct Tracer(TabWriter); + +impl Tracer { + pub fn new() -> Self { + let mut tw = TabWriter::new(std::io::stderr()); + write!(&mut tw, "=== runtime trace ===\n").ok(); + Tracer(tw) + } + + pub fn trace(&mut self, op: &OpCode, ip: usize, stack: &[Value]) { + write!(&mut self.0, "{:04} {:?}\t[ ", ip, op).ok(); + + for val in stack { + write!(&mut self.0, "{} ", val).ok(); + } + + write!(&mut self.0, "]\n").ok(); + } +} + +impl Drop for Tracer { + fn drop(&mut self) { + self.0.flush().ok(); + } +} -- cgit 1.4.1