From bfbe3a93205d54e16379c94186247bbf157306df Mon Sep 17 00:00:00 2001 From: Sudan Landge Date: Fri, 29 Sep 2023 15:53:34 +0000 Subject: [PATCH] feat: add option to print MircomState Add subcommand in InfoVmStateSubCommand to print the VmState. The output of this command can be used to diff between 2 different snapshot files. Signed-off-by: Sudan Landge --- src/snapshot-editor/src/info.rs | 21 ++++++++++++++++++++ src/vmm/src/vstate/vcpu/x86_64.rs | 33 +++++++++++++++---------------- 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/src/snapshot-editor/src/info.rs b/src/snapshot-editor/src/info.rs index a39adbc335e..3aab96ebf17 100644 --- a/src/snapshot-editor/src/info.rs +++ b/src/snapshot-editor/src/info.rs @@ -32,6 +32,12 @@ pub enum InfoVmStateSubCommand { #[arg(short, long)] vmstate_path: PathBuf, }, + /// Print readable MicroVM state. + VmState { + /// Path to the vmstate file. + #[arg(short, long)] + vmstate_path: PathBuf, + }, } pub fn info_vmstate_command(command: InfoVmStateSubCommand) -> Result<(), InfoVmStateError> { @@ -41,6 +47,7 @@ pub fn info_vmstate_command(command: InfoVmStateSubCommand) -> Result<(), InfoVm InfoVmStateSubCommand::VcpuStates { vmstate_path } => { info(&vmstate_path, info_vcpu_states)? } + InfoVmStateSubCommand::VmState { vmstate_path } => info(&vmstate_path, info_vmstate)?, } Ok(()) } @@ -87,3 +94,17 @@ fn info_vcpu_states(state: &MicrovmState, _: u16) -> Result<(), InfoVmStateError } Ok(()) } + +fn info_vmstate(vmstate: &MicrovmState, version: u16) -> Result<(), InfoVmStateError> { + println!("{vmstate:#?}"); + match FC_VERSION_TO_SNAP_VERSION + .iter() + .find(|(_, &v)| v == version) + { + Some((key, _)) => { + println!("v{key}"); + Ok(()) + } + None => Err(InfoVmStateError::InvalidVersion(version)), + } +} diff --git a/src/vmm/src/vstate/vcpu/x86_64.rs b/src/vmm/src/vstate/vcpu/x86_64.rs index 1cca3c44110..a9a30cce757 100644 --- a/src/vmm/src/vstate/vcpu/x86_64.rs +++ b/src/vmm/src/vstate/vcpu/x86_64.rs @@ -5,8 +5,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the THIRD-PARTY file. -use std::fmt::Debug; use std::collections::{HashMap, HashSet}; +use std::fmt::Debug; use kvm_bindings::{ kvm_debugregs, kvm_lapic_state, kvm_mp_state, kvm_regs, kvm_sregs, kvm_vcpu_events, kvm_xcrs, @@ -573,28 +573,27 @@ pub struct VcpuState { pub tsc_khz: Option, } -impl Debug for VcpuState{ +impl Debug for VcpuState { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let mut debug_kvm_regs: Vec = Vec::new(); - for kvm_msrs in self.saved_msrs.iter() - { + for kvm_msrs in self.saved_msrs.iter() { debug_kvm_regs = kvm_msrs.clone().into_raw(); debug_kvm_regs.sort_by_key(|msr| (msr.nmsrs, msr.pad)); } f.debug_struct("VcpuState") - .field("cpuid", &self.cpuid) - .field("msrs", &self.msrs) - .field("saved_msrs", &debug_kvm_regs) - .field("debug_regs", &self.debug_regs) - .field("lapic", &self.lapic) - .field("mp_state", &self.mp_state) - .field("regs", &self.regs) - .field("sregs", &self.sregs) - .field("vcpu_events", &self.vcpu_events) - .field("xcrs", &self.xcrs) - .field("xsave", &self.xsave) - .field("tsc_khz", &self.tsc_khz) - .finish() + .field("cpuid", &self.cpuid) + .field("msrs", &self.msrs) + .field("saved_msrs", &debug_kvm_regs) + .field("debug_regs", &self.debug_regs) + .field("lapic", &self.lapic) + .field("mp_state", &self.mp_state) + .field("regs", &self.regs) + .field("sregs", &self.sregs) + .field("vcpu_events", &self.vcpu_events) + .field("xcrs", &self.xcrs) + .field("xsave", &self.xsave) + .field("tsc_khz", &self.tsc_khz) + .finish() } }