Skip to content

Commit

Permalink
feat: add option to print MircomState
Browse files Browse the repository at this point in the history
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 <sudanl@amazon.com>
  • Loading branch information
Sudan Landge committed Oct 28, 2023
1 parent 6adf354 commit 998846a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 17 deletions.
21 changes: 21 additions & 0 deletions src/snapshot-editor/src/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Check warning on line 39 in src/snapshot-editor/src/info.rs

View check run for this annotation

Codecov / codecov/patch

src/snapshot-editor/src/info.rs#L39

Added line #L39 was not covered by tests
},
}

pub fn info_vmstate_command(command: InfoVmStateSubCommand) -> Result<(), InfoVmStateError> {
Expand All @@ -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)?,

Check warning on line 50 in src/snapshot-editor/src/info.rs

View check run for this annotation

Codecov / codecov/patch

src/snapshot-editor/src/info.rs#L50

Added line #L50 was not covered by tests
}
Ok(())
}
Expand Down Expand Up @@ -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)

Check warning on line 102 in src/snapshot-editor/src/info.rs

View check run for this annotation

Codecov / codecov/patch

src/snapshot-editor/src/info.rs#L98-L102

Added lines #L98 - L102 were not covered by tests
{
Some((key, _)) => {
println!("v{key}");
Ok(())

Check warning on line 106 in src/snapshot-editor/src/info.rs

View check run for this annotation

Codecov / codecov/patch

src/snapshot-editor/src/info.rs#L104-L106

Added lines #L104 - L106 were not covered by tests
}
None => Err(InfoVmStateError::InvalidVersion(version)),

Check warning on line 108 in src/snapshot-editor/src/info.rs

View check run for this annotation

Codecov / codecov/patch

src/snapshot-editor/src/info.rs#L108

Added line #L108 was not covered by tests
}
}

Check warning on line 110 in src/snapshot-editor/src/info.rs

View check run for this annotation

Codecov / codecov/patch

src/snapshot-editor/src/info.rs#L110

Added line #L110 was not covered by tests
33 changes: 16 additions & 17 deletions src/vmm/src/vstate/vcpu/x86_64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -574,28 +574,27 @@ pub struct VcpuState {
pub tsc_khz: Option<u32>,
}

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<kvm_bindings::kvm_msrs> = 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()
}

Check warning on line 598 in src/vmm/src/vstate/vcpu/x86_64.rs

View check run for this annotation

Codecov / codecov/patch

src/vmm/src/vstate/vcpu/x86_64.rs#L578-L598

Added lines #L578 - L598 were not covered by tests
}

Expand Down

0 comments on commit 998846a

Please sign in to comment.