Skip to main content

Module ch01_getting_started

Module ch01_getting_started 

Source
Expand description

§Getting Started

Let’s start tracing! There’s a little to set up first. In this chapter we check that your machine can run KVM guests, add kvmtrace to a project, and build the QEMU interposer.

§What you need

kvmtrace drives the Linux KVM hypervisor directly, so you need:

  • Linux on x86-64, with KVM enabled: /dev/kvm must exist and you must be able to open it for reading and writing. Being in the kvm group is the usual way.
  • Linux 5.15 or newer on the host, for KVM_GUESTDBG_BLOCKIRQ. Older kernels work with interrupt blocking switched off, with the caveats of chapter 10.
  • Rust 1.85 or newer (the crate uses the 2024 edition).

Intel and AMD hosts both work. Nothing here needs Intel PT or other hardware tracing; the method is plain single-stepping, which every x86 CPU with virtualization supports.

Let’s check /dev/kvm:

$ ls -l /dev/kvm
crw-rw-rw- 1 root kvm 10, 232 Dec  3 09:12 /dev/kvm
$ python3 -c 'open("/dev/kvm","r+b"); print("ok")'
ok

If the second command fails with Permission denied, add yourself to the group that owns /dev/kvm and log in again.

§Adding the crate

Add kvmtrace to your Cargo.toml, next to the two crates every Rust KVM program uses:

[dependencies]
kvmtrace = { git = "https://github.com/sahastasai/kvmtrace-cli" }
kvm-ioctls = "0.25"
kvm-bindings = "0.14"

The repository is a Cargo workspace with three members:

CrateWhat it is
kvmtracethe library this book documents
kvmtrace-preloadlibkvmtrace_preload.so, which traces an unmodified QEMU
kvmtrace-clithe kvmtrace command: dump, stats, merge, macsim, compare

§Building everything

$ git clone https://github.com/sahastasai/kvmtrace-cli kvmtrace
$ cd kvmtrace
$ cargo build --release
$ ls target/release/{kvmtrace,libkvmtrace_preload.so}
target/release/kvmtrace  target/release/libkvmtrace_preload.so

And run the tests. They boot tiny virtual machines, so they need /dev/kvm too (without it they pass trivially and say nothing):

$ cargo test -p kvmtrace
...
test every_handler_is_traced ... ok
test serialized_global_order_is_the_execution_order ... ok
test instruction_after_mov_ss_is_reconstructed ... ok
...

That’s it. In the next chapter we write a program that traces a guest.