Expand description
§Reading a Trace
A trace is only useful if you can read it. kvmtrace writes two kinds of
file, and this chapter shows how to read both: first with the command-line
tool, then from Rust.
§With the command-line tool
kvmtrace dump prints the extended trace with disassembly. For the
program of Listing 2-1:
$ kvmtrace dump trace-out
0 g0 cpu0 cpl0 cr3=0x1000 0x0000000000010000 mov ecx,3
1 g1 cpu0 cpl0 cr3=0x1000 0x0000000000010005 add [20000h],rcx RW8@0x20000(pa 0x20000)
2 g2 cpu0 cpl0 cr3=0x1000 0x000000000001000d dec rcx
3 g3 cpu0 cpl0 cr3=0x1000 0x0000000000010010 jne short 0000000000010005h
4 g4 cpu0 cpl0 cr3=0x1000 0x0000000000010005 add [20000h],rcx RW8@0x20000(pa 0x20000)
5 g5 cpu0 cpl0 cr3=0x1000 0x000000000001000d dec rcx
6 g6 cpu0 cpl0 cr3=0x1000 0x0000000000010010 jne short 0000000000010005h
7 g7 cpu0 cpl0 cr3=0x1000 0x0000000000010005 add [20000h],rcx RW8@0x20000(pa 0x20000)
8 g8 cpu0 cpl0 cr3=0x1000 0x000000000001000d dec rcx
9 g9 cpu0 cpl0 cr3=0x1000 0x0000000000010010 jne short 0000000000010005h
10 g10 cpu0 cpl0 cr3=0x1000 0x0000000000010012 hltEach line is one InstructionRecord:
- the per-vCPU sequence number (
0,1, …) and the global one (g0,g1, …): chapter 6 is about the difference; - the vCPU, the CPL, CR3, and RIP;
- the disassembled instruction (decoded from the recorded bytes);
- each data access: kind (
R,W,RW), size, linear address and, in parentheses, guest-physical address.
Instruction fetches are recorded too (kvmtrace dump leaves them out to
keep lines short); switch them off with
TraceOptions::instruction_fetches.
kvmtrace stats summarizes:
$ kvmtrace stats trace-out
vCPU 0: 11 instructions (cpl0 11, cpl3 0), 0 events
accesses: 3 loads, 3 stores, 11 fetches
events by vector: {}
address spaces (CR3): 1
MacSim records: 11(Each add [m], rcx is one read-modify-write access, counted once as a
load and once as a store.) kvmtrace macsim prints the MacSim records:
$ kvmtrace macsim trace-out --count 3
0 pc=0x10000 size=5 op=DATAXFER cf=0 ld=0 [0x0 0x0] rsz=0 st=0 [0x0] wsz=0 src=[] dst=[9] fp=0 wflg=0 taken=0 tgt=0x0
1 pc=0x10005 size=8 op=BINARY cf=0 ld=1 [0x20000 0x0] rsz=8 st=1 [0x20000] wsz=8 src=[9] dst=[25] fp=0 wflg=1 taken=0 tgt=0x0
2 pc=0x1000d size=3 op=BINARY cf=0 ld=0 [0x0 0x0] rsz=0 st=0 [0x0] wsz=0 src=[9] dst=[9, 25] fp=0 wflg=1 taken=0 tgt=0x0and kvmtrace merge interleaves several vCPUs in global order.
§From Rust
The extended format is read with KvtReader,
an iterator over TraceItems. Listing 3-1 adds up the
bytes each instruction wrote.
Listing 3-1: Counting bytes written, per instruction address
use std::collections::BTreeMap;
use kvmtrace::kvt::KvtReader;
use kvmtrace::{AccessKind, TraceItem};
fn main() -> kvmtrace::Result<()> {
let mut written: BTreeMap<u64, u64> = BTreeMap::new();
for item in KvtReader::open("trace-out/vcpu0.kvt".as_ref())? {
if let TraceItem::Instruction(r) = item? {
for a in r.data_accesses() {
if matches!(a.kind, AccessKind::Write | AccessKind::ReadWrite) {
*written.entry(r.rip).or_default() += a.size as u64;
}
}
}
}
for (rip, bytes) in written {
println!("{rip:#x}: {bytes} bytes");
}
Ok(())
}Two kinds of item come out of the reader:
TraceItem::Instruction: one instruction (or one iteration of aREPstring instruction, or one attempt that faulted; seeInstructionOutcome).TraceItem::Event: the CPU switched to a handler: an exception, an interrupt, or a software interrupt such asint 0x80. Events carry their own memory accesses (reading the IDT, pushing the interrupt frame) because a cache sees those too.
§Accesses, in detail
Every MemoryAccess says:
| Field | Meaning |
|---|---|
linear | the linear (virtual) address, after segmentation |
physical | the guest-physical address, from walking the guest’s page tables |
physical_end | for an access that crosses a page, the physical address of its last byte |
size | bytes |
kind | Read, Write, ReadWrite (one access that does both, like add [m], r), or Fetch |
class | Operand, Implicit (stack, string, descriptor), Prefetch, InstructionFetch, EventDescriptor, EventFrame |
outcome | Performed, Faulted (this access caused the fault), Aborted (the instruction faulted before reaching it), or Suppressed |
conditional | the access depends on a condition or mask (e.g. a masked gather lane) |
The outcome field is how the trace reports both attempted and
successfully executed accesses: a load that page-faults appears once with
Faulted, then the handler runs (traced), then the load appears again,
Performed, when the instruction is restarted.
§The MacSim files
The .raw files are what MacSim reads. They hold less: MacSim’s record
has room for two loads and one store per instruction, and no physical
addresses. Chapter 8 explains the
mapping field by field; to read them from Rust use
MacsimReader.