xref: /qemu/docs/system/replay.rst (revision 43185f7bd4cbe730e7d5a6abcefbf83a5c98f3db)
1*43185f7bSPavel Dovgalyuk.. _replay:
2*43185f7bSPavel Dovgalyuk
3*43185f7bSPavel Dovgalyuk..
4*43185f7bSPavel Dovgalyuk    Copyright (c) 2010-2022 Institute for System Programming
5*43185f7bSPavel Dovgalyuk                        of the Russian Academy of Sciences.
6*43185f7bSPavel Dovgalyuk
7*43185f7bSPavel Dovgalyuk    This work is licensed under the terms of the GNU GPL, version 2 or later.
8*43185f7bSPavel Dovgalyuk    See the COPYING file in the top-level directory.
9*43185f7bSPavel Dovgalyuk
10*43185f7bSPavel DovgalyukRecord/replay
11*43185f7bSPavel Dovgalyuk=============
12*43185f7bSPavel Dovgalyuk
13*43185f7bSPavel DovgalyukRecord/replay functions are used for the deterministic replay of qemu execution.
14*43185f7bSPavel DovgalyukExecution recording writes a non-deterministic events log, which can be later
15*43185f7bSPavel Dovgalyukused for replaying the execution anywhere and for unlimited number of times.
16*43185f7bSPavel DovgalyukIt also supports checkpointing for faster rewind to the specific replay moment.
17*43185f7bSPavel DovgalyukExecution replaying reads the log and replays all non-deterministic events
18*43185f7bSPavel Dovgalyukincluding external input, hardware clocks, and interrupts.
19*43185f7bSPavel Dovgalyuk
20*43185f7bSPavel DovgalyukDeterministic replay has the following features:
21*43185f7bSPavel Dovgalyuk
22*43185f7bSPavel Dovgalyuk * Deterministically replays whole system execution and all contents of
23*43185f7bSPavel Dovgalyuk   the memory, state of the hardware devices, clocks, and screen of the VM.
24*43185f7bSPavel Dovgalyuk * Writes execution log into the file for later replaying for multiple times
25*43185f7bSPavel Dovgalyuk   on different machines.
26*43185f7bSPavel Dovgalyuk * Supports i386, x86_64, ARM, AArch64, Risc-V, MIPS, MIPS64, S390X, Alpha,
27*43185f7bSPavel Dovgalyuk   PowerPC, PowerPC64, M68000, Microblaze, OpenRISC, Nios II, SPARC,
28*43185f7bSPavel Dovgalyuk   and Xtensa hardware platforms.
29*43185f7bSPavel Dovgalyuk * Performs deterministic replay of all operations with keyboard and mouse
30*43185f7bSPavel Dovgalyuk   input devices, serial ports, and network.
31*43185f7bSPavel Dovgalyuk
32*43185f7bSPavel DovgalyukUsage of the record/replay:
33*43185f7bSPavel Dovgalyuk
34*43185f7bSPavel Dovgalyuk * First, record the execution with the following command line:
35*43185f7bSPavel Dovgalyuk
36*43185f7bSPavel Dovgalyuk    .. parsed-literal::
37*43185f7bSPavel Dovgalyuk        |qemu_system| \\
38*43185f7bSPavel Dovgalyuk        -icount shift=auto,rr=record,rrfile=replay.bin \\
39*43185f7bSPavel Dovgalyuk        -drive file=disk.qcow2,if=none,snapshot,id=img-direct \\
40*43185f7bSPavel Dovgalyuk        -drive driver=blkreplay,if=none,image=img-direct,id=img-blkreplay \\
41*43185f7bSPavel Dovgalyuk        -device ide-hd,drive=img-blkreplay \\
42*43185f7bSPavel Dovgalyuk        -netdev user,id=net1 -device rtl8139,netdev=net1 \\
43*43185f7bSPavel Dovgalyuk        -object filter-replay,id=replay,netdev=net1
44*43185f7bSPavel Dovgalyuk
45*43185f7bSPavel Dovgalyuk * After recording, you can replay it by using another command line:
46*43185f7bSPavel Dovgalyuk
47*43185f7bSPavel Dovgalyuk    .. parsed-literal::
48*43185f7bSPavel Dovgalyuk        |qemu_system| \\
49*43185f7bSPavel Dovgalyuk        -icount shift=auto,rr=replay,rrfile=replay.bin \\
50*43185f7bSPavel Dovgalyuk        -drive file=disk.qcow2,if=none,snapshot,id=img-direct \\
51*43185f7bSPavel Dovgalyuk        -drive driver=blkreplay,if=none,image=img-direct,id=img-blkreplay \\
52*43185f7bSPavel Dovgalyuk        -device ide-hd,drive=img-blkreplay \\
53*43185f7bSPavel Dovgalyuk        -netdev user,id=net1 -device rtl8139,netdev=net1 \\
54*43185f7bSPavel Dovgalyuk        -object filter-replay,id=replay,netdev=net1
55*43185f7bSPavel Dovgalyuk
56*43185f7bSPavel Dovgalyuk   The only difference with recording is changing the rr option
57*43185f7bSPavel Dovgalyuk   from record to replay.
58*43185f7bSPavel Dovgalyuk * Block device images are not actually changed in the recording mode,
59*43185f7bSPavel Dovgalyuk   because all of the changes are written to the temporary overlay file.
60*43185f7bSPavel Dovgalyuk   This behavior is enabled by using blkreplay driver. It should be used
61*43185f7bSPavel Dovgalyuk   for every enabled block device, as described in :ref:`block-label` section.
62*43185f7bSPavel Dovgalyuk * ``-net none`` option should be specified when network is not used,
63*43185f7bSPavel Dovgalyuk   because QEMU adds network card by default. When network is needed,
64*43185f7bSPavel Dovgalyuk   it should be configured explicitly with replay filter, as described
65*43185f7bSPavel Dovgalyuk   in :ref:`network-label` section.
66*43185f7bSPavel Dovgalyuk * Interaction with audio devices and serial ports are recorded and replayed
67*43185f7bSPavel Dovgalyuk   automatically when such devices are enabled.
68*43185f7bSPavel Dovgalyuk
69*43185f7bSPavel DovgalyukCore idea
70*43185f7bSPavel Dovgalyuk---------
71*43185f7bSPavel Dovgalyuk
72*43185f7bSPavel DovgalyukRecord/replay system is based on saving and replaying non-deterministic
73*43185f7bSPavel Dovgalyukevents (e.g. keyboard input) and simulating deterministic ones (e.g. reading
74*43185f7bSPavel Dovgalyukfrom HDD or memory of the VM). Saving only non-deterministic events makes
75*43185f7bSPavel Dovgalyuklog file smaller and simulation faster.
76*43185f7bSPavel Dovgalyuk
77*43185f7bSPavel DovgalyukThe following non-deterministic data from peripheral devices is saved into
78*43185f7bSPavel Dovgalyukthe log: mouse and keyboard input, network packets, audio controller input,
79*43185f7bSPavel Dovgalyukserial port input, and hardware clocks (they are non-deterministic
80*43185f7bSPavel Dovgalyuktoo, because their values are taken from the host machine). Inputs from
81*43185f7bSPavel Dovgalyuksimulated hardware, memory of VM, software interrupts, and execution of
82*43185f7bSPavel Dovgalyukinstructions are not saved into the log, because they are deterministic and
83*43185f7bSPavel Dovgalyukcan be replayed by simulating the behavior of virtual machine starting from
84*43185f7bSPavel Dovgalyukinitial state.
85*43185f7bSPavel Dovgalyuk
86*43185f7bSPavel DovgalyukInstruction counting
87*43185f7bSPavel Dovgalyuk--------------------
88*43185f7bSPavel Dovgalyuk
89*43185f7bSPavel DovgalyukQEMU should work in icount mode to use record/replay feature. icount was
90*43185f7bSPavel Dovgalyukdesigned to allow deterministic execution in absence of external inputs
91*43185f7bSPavel Dovgalyukof the virtual machine. Record/replay feature is enabled through ``-icount``
92*43185f7bSPavel Dovgalyukcommand-line option, making possible deterministic execution of the machine,
93*43185f7bSPavel Dovgalyukinteracting with user or network.
94*43185f7bSPavel Dovgalyuk
95*43185f7bSPavel Dovgalyuk.. _block-label:
96*43185f7bSPavel Dovgalyuk
97*43185f7bSPavel DovgalyukBlock devices
98*43185f7bSPavel Dovgalyuk-------------
99*43185f7bSPavel Dovgalyuk
100*43185f7bSPavel DovgalyukBlock devices record/replay module intercepts calls of
101*43185f7bSPavel Dovgalyukbdrv coroutine functions at the top of block drivers stack.
102*43185f7bSPavel DovgalyukTo record and replay block operations the drive must be configured
103*43185f7bSPavel Dovgalyukas following:
104*43185f7bSPavel Dovgalyuk
105*43185f7bSPavel Dovgalyuk.. parsed-literal::
106*43185f7bSPavel Dovgalyuk    -drive file=disk.qcow2,if=none,snapshot,id=img-direct
107*43185f7bSPavel Dovgalyuk    -drive driver=blkreplay,if=none,image=img-direct,id=img-blkreplay
108*43185f7bSPavel Dovgalyuk    -device ide-hd,drive=img-blkreplay
109*43185f7bSPavel Dovgalyuk
110*43185f7bSPavel Dovgalyukblkreplay driver should be inserted between disk image and virtual driver
111*43185f7bSPavel Dovgalyukcontroller. Therefore all disk requests may be recorded and replayed.
112*43185f7bSPavel Dovgalyuk
113*43185f7bSPavel Dovgalyuk.. _snapshotting-label:
114*43185f7bSPavel Dovgalyuk
115*43185f7bSPavel DovgalyukSnapshotting
116*43185f7bSPavel Dovgalyuk------------
117*43185f7bSPavel Dovgalyuk
118*43185f7bSPavel DovgalyukNew VM snapshots may be created in replay mode. They can be used later
119*43185f7bSPavel Dovgalyukto recover the desired VM state. All VM states created in replay mode
120*43185f7bSPavel Dovgalyukare associated with the moment of time in the replay scenario.
121*43185f7bSPavel DovgalyukAfter recovering the VM state replay will start from that position.
122*43185f7bSPavel Dovgalyuk
123*43185f7bSPavel DovgalyukDefault starting snapshot name may be specified with icount field
124*43185f7bSPavel Dovgalyukrrsnapshot as follows:
125*43185f7bSPavel Dovgalyuk
126*43185f7bSPavel Dovgalyuk.. parsed-literal::
127*43185f7bSPavel Dovgalyuk    -icount shift=auto,rr=record,rrfile=replay.bin,rrsnapshot=snapshot_name
128*43185f7bSPavel Dovgalyuk
129*43185f7bSPavel DovgalyukThis snapshot is created at start of recording and restored at start
130*43185f7bSPavel Dovgalyukof replaying. It also can be loaded while replaying to roll back
131*43185f7bSPavel Dovgalyukthe execution.
132*43185f7bSPavel Dovgalyuk
133*43185f7bSPavel Dovgalyuk``snapshot`` flag of the disk image must be removed to save the snapshots
134*43185f7bSPavel Dovgalyukin the overlay (or original image) instead of using the temporary overlay.
135*43185f7bSPavel Dovgalyuk
136*43185f7bSPavel Dovgalyuk.. parsed-literal::
137*43185f7bSPavel Dovgalyuk    -drive file=disk.ovl,if=none,id=img-direct
138*43185f7bSPavel Dovgalyuk    -drive driver=blkreplay,if=none,image=img-direct,id=img-blkreplay
139*43185f7bSPavel Dovgalyuk    -device ide-hd,drive=img-blkreplay
140*43185f7bSPavel Dovgalyuk
141*43185f7bSPavel DovgalyukUse QEMU monitor to create additional snapshots. ``savevm <name>`` command
142*43185f7bSPavel Dovgalyukcreated the snapshot and ``loadvm <name>`` restores it. To prevent corruption
143*43185f7bSPavel Dovgalyukof the original disk image, use overlay files linked to the original images.
144*43185f7bSPavel DovgalyukTherefore all new snapshots (including the starting one) will be saved in
145*43185f7bSPavel Dovgalyukoverlays and the original image remains unchanged.
146*43185f7bSPavel Dovgalyuk
147*43185f7bSPavel DovgalyukWhen you need to use snapshots with diskless virtual machine,
148*43185f7bSPavel Dovgalyukit must be started with "orphan" qcow2 image. This image will be used
149*43185f7bSPavel Dovgalyukfor storing VM snapshots. Here is the example of the command line for this:
150*43185f7bSPavel Dovgalyuk
151*43185f7bSPavel Dovgalyuk.. parsed-literal::
152*43185f7bSPavel Dovgalyuk    |qemu_system| \\
153*43185f7bSPavel Dovgalyuk      -icount shift=auto,rr=replay,rrfile=record.bin,rrsnapshot=init \\
154*43185f7bSPavel Dovgalyuk      -net none -drive file=empty.qcow2,if=none,id=rr
155*43185f7bSPavel Dovgalyuk
156*43185f7bSPavel Dovgalyuk``empty.qcow2`` drive does not connected to any virtual block device and used
157*43185f7bSPavel Dovgalyukfor VM snapshots only.
158*43185f7bSPavel Dovgalyuk
159*43185f7bSPavel Dovgalyuk.. _network-label:
160*43185f7bSPavel Dovgalyuk
161*43185f7bSPavel DovgalyukNetwork devices
162*43185f7bSPavel Dovgalyuk---------------
163*43185f7bSPavel Dovgalyuk
164*43185f7bSPavel DovgalyukRecord and replay for network interactions is performed with the network filter.
165*43185f7bSPavel DovgalyukEach backend must have its own instance of the replay filter as follows:
166*43185f7bSPavel Dovgalyuk
167*43185f7bSPavel Dovgalyuk.. parsed-literal::
168*43185f7bSPavel Dovgalyuk    -netdev user,id=net1 -device rtl8139,netdev=net1
169*43185f7bSPavel Dovgalyuk    -object filter-replay,id=replay,netdev=net1
170*43185f7bSPavel Dovgalyuk
171*43185f7bSPavel DovgalyukReplay network filter is used to record and replay network packets. While
172*43185f7bSPavel Dovgalyukrecording the virtual machine this filter puts all packets coming from
173*43185f7bSPavel Dovgalyukthe outer world into the log. In replay mode packets from the log are
174*43185f7bSPavel Dovgalyukinjected into the network device. All interactions with network backend
175*43185f7bSPavel Dovgalyukin replay mode are disabled.
176*43185f7bSPavel Dovgalyuk
177*43185f7bSPavel DovgalyukAudio devices
178*43185f7bSPavel Dovgalyuk-------------
179*43185f7bSPavel Dovgalyuk
180*43185f7bSPavel DovgalyukAudio data is recorded and replay automatically. The command line for recording
181*43185f7bSPavel Dovgalyukand replaying must contain identical specifications of audio hardware, e.g.:
182*43185f7bSPavel Dovgalyuk
183*43185f7bSPavel Dovgalyuk.. parsed-literal::
184*43185f7bSPavel Dovgalyuk    -soundhw ac97
185*43185f7bSPavel Dovgalyuk
186*43185f7bSPavel DovgalyukSerial ports
187*43185f7bSPavel Dovgalyuk------------
188*43185f7bSPavel Dovgalyuk
189*43185f7bSPavel DovgalyukSerial ports input is recorded and replay automatically. The command lines
190*43185f7bSPavel Dovgalyukfor recording and replaying must contain identical number of ports in record
191*43185f7bSPavel Dovgalyukand replay modes, but their backends may differ.
192*43185f7bSPavel DovgalyukE.g., ``-serial stdio`` in record mode, and ``-serial null`` in replay mode.
193*43185f7bSPavel Dovgalyuk
194*43185f7bSPavel DovgalyukReverse debugging
195*43185f7bSPavel Dovgalyuk-----------------
196*43185f7bSPavel Dovgalyuk
197*43185f7bSPavel DovgalyukReverse debugging allows "executing" the program in reverse direction.
198*43185f7bSPavel DovgalyukGDB remote protocol supports "reverse step" and "reverse continue"
199*43185f7bSPavel Dovgalyukcommands. The first one steps single instruction backwards in time,
200*43185f7bSPavel Dovgalyukand the second one finds the last breakpoint in the past.
201*43185f7bSPavel Dovgalyuk
202*43185f7bSPavel DovgalyukRecorded executions may be used to enable reverse debugging. QEMU can't
203*43185f7bSPavel Dovgalyukexecute the code in backwards direction, but can load a snapshot and
204*43185f7bSPavel Dovgalyukreplay forward to find the desired position or breakpoint.
205*43185f7bSPavel Dovgalyuk
206*43185f7bSPavel DovgalyukThe following GDB commands are supported:
207*43185f7bSPavel Dovgalyuk
208*43185f7bSPavel Dovgalyuk - ``reverse-stepi`` (or ``rsi``) - step one instruction backwards
209*43185f7bSPavel Dovgalyuk - ``reverse-continue`` (or ``rc``) - find last breakpoint in the past
210*43185f7bSPavel Dovgalyuk
211*43185f7bSPavel DovgalyukReverse step loads the nearest snapshot and replays the execution until
212*43185f7bSPavel Dovgalyukthe required instruction is met.
213*43185f7bSPavel Dovgalyuk
214*43185f7bSPavel DovgalyukReverse continue may include several passes of examining the execution
215*43185f7bSPavel Dovgalyukbetween the snapshots. Each of the passes include the following steps:
216*43185f7bSPavel Dovgalyuk
217*43185f7bSPavel Dovgalyuk #. loading the snapshot
218*43185f7bSPavel Dovgalyuk #. replaying to examine the breakpoints
219*43185f7bSPavel Dovgalyuk #. if breakpoint or watchpoint was met
220*43185f7bSPavel Dovgalyuk
221*43185f7bSPavel Dovgalyuk    * loading the snapshot again
222*43185f7bSPavel Dovgalyuk    * replaying to the required breakpoint
223*43185f7bSPavel Dovgalyuk
224*43185f7bSPavel Dovgalyuk #. else
225*43185f7bSPavel Dovgalyuk
226*43185f7bSPavel Dovgalyuk    * proceeding to the p.1 with the earlier snapshot
227*43185f7bSPavel Dovgalyuk
228*43185f7bSPavel DovgalyukTherefore usage of the reverse debugging requires at least one snapshot
229*43185f7bSPavel Dovgalyukcreated. This can be done by omitting ``snapshot`` option
230*43185f7bSPavel Dovgalyukfor the block drives and adding ``rrsnapshot`` for both record and replay
231*43185f7bSPavel Dovgalyukcommand lines.
232*43185f7bSPavel DovgalyukSee the :ref:`snapshotting-label` section to learn more about running record/replay
233*43185f7bSPavel Dovgalyukand creating the snapshot in these modes.
234*43185f7bSPavel Dovgalyuk
235*43185f7bSPavel DovgalyukWhen ``rrsnapshot`` is not used, then snapshot named ``start_debugging``
236*43185f7bSPavel Dovgalyukcreated in temporary overlay. This allows using reverse debugging, but with
237*43185f7bSPavel Dovgalyuktemporary snapshots (existing within the session).
238