xref: /openbmc/qemu/docs/system/replay.rst (revision 85b597413d4370cb168f711192eaef2eb70535ac)
143185f7bSPavel Dovgalyuk.. _replay:
243185f7bSPavel Dovgalyuk
343185f7bSPavel Dovgalyuk..
443185f7bSPavel Dovgalyuk    Copyright (c) 2010-2022 Institute for System Programming
543185f7bSPavel Dovgalyuk                        of the Russian Academy of Sciences.
643185f7bSPavel Dovgalyuk
743185f7bSPavel Dovgalyuk    This work is licensed under the terms of the GNU GPL, version 2 or later.
843185f7bSPavel Dovgalyuk    See the COPYING file in the top-level directory.
943185f7bSPavel Dovgalyuk
1043185f7bSPavel DovgalyukRecord/replay
1143185f7bSPavel Dovgalyuk=============
1243185f7bSPavel Dovgalyuk
1343185f7bSPavel DovgalyukRecord/replay functions are used for the deterministic replay of qemu execution.
1443185f7bSPavel DovgalyukExecution recording writes a non-deterministic events log, which can be later
1543185f7bSPavel Dovgalyukused for replaying the execution anywhere and for unlimited number of times.
1643185f7bSPavel DovgalyukIt also supports checkpointing for faster rewind to the specific replay moment.
1743185f7bSPavel DovgalyukExecution replaying reads the log and replays all non-deterministic events
1843185f7bSPavel Dovgalyukincluding external input, hardware clocks, and interrupts.
1943185f7bSPavel Dovgalyuk
2043185f7bSPavel DovgalyukDeterministic replay has the following features:
2143185f7bSPavel Dovgalyuk
2243185f7bSPavel Dovgalyuk * Deterministically replays whole system execution and all contents of
2343185f7bSPavel Dovgalyuk   the memory, state of the hardware devices, clocks, and screen of the VM.
2443185f7bSPavel Dovgalyuk * Writes execution log into the file for later replaying for multiple times
2543185f7bSPavel Dovgalyuk   on different machines.
2643185f7bSPavel Dovgalyuk * Supports i386, x86_64, ARM, AArch64, Risc-V, MIPS, MIPS64, S390X, Alpha,
27*6c301485SPhilippe Mathieu-Daudé   PowerPC, PowerPC64, M68000, Microblaze, OpenRISC, SPARC,
2843185f7bSPavel Dovgalyuk   and Xtensa hardware platforms.
2943185f7bSPavel Dovgalyuk * Performs deterministic replay of all operations with keyboard and mouse
3043185f7bSPavel Dovgalyuk   input devices, serial ports, and network.
3143185f7bSPavel Dovgalyuk
3243185f7bSPavel DovgalyukUsage of the record/replay:
3343185f7bSPavel Dovgalyuk
3443185f7bSPavel Dovgalyuk * First, record the execution with the following command line:
3543185f7bSPavel Dovgalyuk
3643185f7bSPavel Dovgalyuk    .. parsed-literal::
3743185f7bSPavel Dovgalyuk        |qemu_system| \\
3843185f7bSPavel Dovgalyuk        -icount shift=auto,rr=record,rrfile=replay.bin \\
3943185f7bSPavel Dovgalyuk        -drive file=disk.qcow2,if=none,snapshot,id=img-direct \\
4043185f7bSPavel Dovgalyuk        -drive driver=blkreplay,if=none,image=img-direct,id=img-blkreplay \\
4143185f7bSPavel Dovgalyuk        -device ide-hd,drive=img-blkreplay \\
4243185f7bSPavel Dovgalyuk        -netdev user,id=net1 -device rtl8139,netdev=net1 \\
4343185f7bSPavel Dovgalyuk        -object filter-replay,id=replay,netdev=net1
4443185f7bSPavel Dovgalyuk
4543185f7bSPavel Dovgalyuk * After recording, you can replay it by using another command line:
4643185f7bSPavel Dovgalyuk
4743185f7bSPavel Dovgalyuk    .. parsed-literal::
4843185f7bSPavel Dovgalyuk        |qemu_system| \\
4943185f7bSPavel Dovgalyuk        -icount shift=auto,rr=replay,rrfile=replay.bin \\
5043185f7bSPavel Dovgalyuk        -drive file=disk.qcow2,if=none,snapshot,id=img-direct \\
5143185f7bSPavel Dovgalyuk        -drive driver=blkreplay,if=none,image=img-direct,id=img-blkreplay \\
5243185f7bSPavel Dovgalyuk        -device ide-hd,drive=img-blkreplay \\
5343185f7bSPavel Dovgalyuk        -netdev user,id=net1 -device rtl8139,netdev=net1 \\
5443185f7bSPavel Dovgalyuk        -object filter-replay,id=replay,netdev=net1
5543185f7bSPavel Dovgalyuk
5643185f7bSPavel Dovgalyuk   The only difference with recording is changing the rr option
5743185f7bSPavel Dovgalyuk   from record to replay.
5843185f7bSPavel Dovgalyuk * Block device images are not actually changed in the recording mode,
5943185f7bSPavel Dovgalyuk   because all of the changes are written to the temporary overlay file.
6043185f7bSPavel Dovgalyuk   This behavior is enabled by using blkreplay driver. It should be used
6143185f7bSPavel Dovgalyuk   for every enabled block device, as described in :ref:`block-label` section.
6243185f7bSPavel Dovgalyuk * ``-net none`` option should be specified when network is not used,
6343185f7bSPavel Dovgalyuk   because QEMU adds network card by default. When network is needed,
6443185f7bSPavel Dovgalyuk   it should be configured explicitly with replay filter, as described
6543185f7bSPavel Dovgalyuk   in :ref:`network-label` section.
6643185f7bSPavel Dovgalyuk * Interaction with audio devices and serial ports are recorded and replayed
6743185f7bSPavel Dovgalyuk   automatically when such devices are enabled.
6843185f7bSPavel Dovgalyuk
6943185f7bSPavel DovgalyukCore idea
7043185f7bSPavel Dovgalyuk---------
7143185f7bSPavel Dovgalyuk
7243185f7bSPavel DovgalyukRecord/replay system is based on saving and replaying non-deterministic
7343185f7bSPavel Dovgalyukevents (e.g. keyboard input) and simulating deterministic ones (e.g. reading
7443185f7bSPavel Dovgalyukfrom HDD or memory of the VM). Saving only non-deterministic events makes
7543185f7bSPavel Dovgalyuklog file smaller and simulation faster.
7643185f7bSPavel Dovgalyuk
7743185f7bSPavel DovgalyukThe following non-deterministic data from peripheral devices is saved into
7843185f7bSPavel Dovgalyukthe log: mouse and keyboard input, network packets, audio controller input,
7943185f7bSPavel Dovgalyukserial port input, and hardware clocks (they are non-deterministic
8043185f7bSPavel Dovgalyuktoo, because their values are taken from the host machine). Inputs from
8143185f7bSPavel Dovgalyuksimulated hardware, memory of VM, software interrupts, and execution of
8243185f7bSPavel Dovgalyukinstructions are not saved into the log, because they are deterministic and
8343185f7bSPavel Dovgalyukcan be replayed by simulating the behavior of virtual machine starting from
8443185f7bSPavel Dovgalyukinitial state.
8543185f7bSPavel Dovgalyuk
8643185f7bSPavel DovgalyukInstruction counting
8743185f7bSPavel Dovgalyuk--------------------
8843185f7bSPavel Dovgalyuk
8943185f7bSPavel DovgalyukQEMU should work in icount mode to use record/replay feature. icount was
9043185f7bSPavel Dovgalyukdesigned to allow deterministic execution in absence of external inputs
9143185f7bSPavel Dovgalyukof the virtual machine. Record/replay feature is enabled through ``-icount``
9243185f7bSPavel Dovgalyukcommand-line option, making possible deterministic execution of the machine,
9343185f7bSPavel Dovgalyukinteracting with user or network.
9443185f7bSPavel Dovgalyuk
9543185f7bSPavel Dovgalyuk.. _block-label:
9643185f7bSPavel Dovgalyuk
9743185f7bSPavel DovgalyukBlock devices
9843185f7bSPavel Dovgalyuk-------------
9943185f7bSPavel Dovgalyuk
10043185f7bSPavel DovgalyukBlock devices record/replay module intercepts calls of
10143185f7bSPavel Dovgalyukbdrv coroutine functions at the top of block drivers stack.
10243185f7bSPavel DovgalyukTo record and replay block operations the drive must be configured
10343185f7bSPavel Dovgalyukas following:
10443185f7bSPavel Dovgalyuk
10543185f7bSPavel Dovgalyuk.. parsed-literal::
10643185f7bSPavel Dovgalyuk    -drive file=disk.qcow2,if=none,snapshot,id=img-direct
10743185f7bSPavel Dovgalyuk    -drive driver=blkreplay,if=none,image=img-direct,id=img-blkreplay
10843185f7bSPavel Dovgalyuk    -device ide-hd,drive=img-blkreplay
10943185f7bSPavel Dovgalyuk
11043185f7bSPavel Dovgalyukblkreplay driver should be inserted between disk image and virtual driver
11143185f7bSPavel Dovgalyukcontroller. Therefore all disk requests may be recorded and replayed.
11243185f7bSPavel Dovgalyuk
11343185f7bSPavel Dovgalyuk.. _snapshotting-label:
11443185f7bSPavel Dovgalyuk
11543185f7bSPavel DovgalyukSnapshotting
11643185f7bSPavel Dovgalyuk------------
11743185f7bSPavel Dovgalyuk
11843185f7bSPavel DovgalyukNew VM snapshots may be created in replay mode. They can be used later
11943185f7bSPavel Dovgalyukto recover the desired VM state. All VM states created in replay mode
12043185f7bSPavel Dovgalyukare associated with the moment of time in the replay scenario.
12143185f7bSPavel DovgalyukAfter recovering the VM state replay will start from that position.
12243185f7bSPavel Dovgalyuk
12343185f7bSPavel DovgalyukDefault starting snapshot name may be specified with icount field
12443185f7bSPavel Dovgalyukrrsnapshot as follows:
12543185f7bSPavel Dovgalyuk
12643185f7bSPavel Dovgalyuk.. parsed-literal::
12743185f7bSPavel Dovgalyuk    -icount shift=auto,rr=record,rrfile=replay.bin,rrsnapshot=snapshot_name
12843185f7bSPavel Dovgalyuk
12943185f7bSPavel DovgalyukThis snapshot is created at start of recording and restored at start
13043185f7bSPavel Dovgalyukof replaying. It also can be loaded while replaying to roll back
13143185f7bSPavel Dovgalyukthe execution.
13243185f7bSPavel Dovgalyuk
13343185f7bSPavel Dovgalyuk``snapshot`` flag of the disk image must be removed to save the snapshots
13443185f7bSPavel Dovgalyukin the overlay (or original image) instead of using the temporary overlay.
13543185f7bSPavel Dovgalyuk
13643185f7bSPavel Dovgalyuk.. parsed-literal::
13743185f7bSPavel Dovgalyuk    -drive file=disk.ovl,if=none,id=img-direct
13843185f7bSPavel Dovgalyuk    -drive driver=blkreplay,if=none,image=img-direct,id=img-blkreplay
13943185f7bSPavel Dovgalyuk    -device ide-hd,drive=img-blkreplay
14043185f7bSPavel Dovgalyuk
14143185f7bSPavel DovgalyukUse QEMU monitor to create additional snapshots. ``savevm <name>`` command
14243185f7bSPavel Dovgalyukcreated the snapshot and ``loadvm <name>`` restores it. To prevent corruption
14343185f7bSPavel Dovgalyukof the original disk image, use overlay files linked to the original images.
14443185f7bSPavel DovgalyukTherefore all new snapshots (including the starting one) will be saved in
14543185f7bSPavel Dovgalyukoverlays and the original image remains unchanged.
14643185f7bSPavel Dovgalyuk
14743185f7bSPavel DovgalyukWhen you need to use snapshots with diskless virtual machine,
14843185f7bSPavel Dovgalyukit must be started with "orphan" qcow2 image. This image will be used
14943185f7bSPavel Dovgalyukfor storing VM snapshots. Here is the example of the command line for this:
15043185f7bSPavel Dovgalyuk
15143185f7bSPavel Dovgalyuk.. parsed-literal::
15243185f7bSPavel Dovgalyuk    |qemu_system| \\
15343185f7bSPavel Dovgalyuk      -icount shift=auto,rr=replay,rrfile=record.bin,rrsnapshot=init \\
15443185f7bSPavel Dovgalyuk      -net none -drive file=empty.qcow2,if=none,id=rr
15543185f7bSPavel Dovgalyuk
15643185f7bSPavel Dovgalyuk``empty.qcow2`` drive does not connected to any virtual block device and used
15743185f7bSPavel Dovgalyukfor VM snapshots only.
15843185f7bSPavel Dovgalyuk
15943185f7bSPavel Dovgalyuk.. _network-label:
16043185f7bSPavel Dovgalyuk
16143185f7bSPavel DovgalyukNetwork devices
16243185f7bSPavel Dovgalyuk---------------
16343185f7bSPavel Dovgalyuk
16443185f7bSPavel DovgalyukRecord and replay for network interactions is performed with the network filter.
16543185f7bSPavel DovgalyukEach backend must have its own instance of the replay filter as follows:
16643185f7bSPavel Dovgalyuk
16743185f7bSPavel Dovgalyuk.. parsed-literal::
16843185f7bSPavel Dovgalyuk    -netdev user,id=net1 -device rtl8139,netdev=net1
16943185f7bSPavel Dovgalyuk    -object filter-replay,id=replay,netdev=net1
17043185f7bSPavel Dovgalyuk
17143185f7bSPavel DovgalyukReplay network filter is used to record and replay network packets. While
17243185f7bSPavel Dovgalyukrecording the virtual machine this filter puts all packets coming from
17343185f7bSPavel Dovgalyukthe outer world into the log. In replay mode packets from the log are
17443185f7bSPavel Dovgalyukinjected into the network device. All interactions with network backend
17543185f7bSPavel Dovgalyukin replay mode are disabled.
17643185f7bSPavel Dovgalyuk
17743185f7bSPavel DovgalyukAudio devices
17843185f7bSPavel Dovgalyuk-------------
17943185f7bSPavel Dovgalyuk
18043185f7bSPavel DovgalyukAudio data is recorded and replay automatically. The command line for recording
18143185f7bSPavel Dovgalyukand replaying must contain identical specifications of audio hardware, e.g.:
18243185f7bSPavel Dovgalyuk
18343185f7bSPavel Dovgalyuk.. parsed-literal::
18434e81820SPaolo Bonzini    -audio pa,model=ac97
18543185f7bSPavel Dovgalyuk
18643185f7bSPavel DovgalyukSerial ports
18743185f7bSPavel Dovgalyuk------------
18843185f7bSPavel Dovgalyuk
18943185f7bSPavel DovgalyukSerial ports input is recorded and replay automatically. The command lines
19043185f7bSPavel Dovgalyukfor recording and replaying must contain identical number of ports in record
19143185f7bSPavel Dovgalyukand replay modes, but their backends may differ.
19243185f7bSPavel DovgalyukE.g., ``-serial stdio`` in record mode, and ``-serial null`` in replay mode.
19343185f7bSPavel Dovgalyuk
19443185f7bSPavel DovgalyukReverse debugging
19543185f7bSPavel Dovgalyuk-----------------
19643185f7bSPavel Dovgalyuk
19743185f7bSPavel DovgalyukReverse debugging allows "executing" the program in reverse direction.
19843185f7bSPavel DovgalyukGDB remote protocol supports "reverse step" and "reverse continue"
19943185f7bSPavel Dovgalyukcommands. The first one steps single instruction backwards in time,
20043185f7bSPavel Dovgalyukand the second one finds the last breakpoint in the past.
20143185f7bSPavel Dovgalyuk
20243185f7bSPavel DovgalyukRecorded executions may be used to enable reverse debugging. QEMU can't
20343185f7bSPavel Dovgalyukexecute the code in backwards direction, but can load a snapshot and
20443185f7bSPavel Dovgalyukreplay forward to find the desired position or breakpoint.
20543185f7bSPavel Dovgalyuk
20643185f7bSPavel DovgalyukThe following GDB commands are supported:
20743185f7bSPavel Dovgalyuk
20843185f7bSPavel Dovgalyuk - ``reverse-stepi`` (or ``rsi``) - step one instruction backwards
20943185f7bSPavel Dovgalyuk - ``reverse-continue`` (or ``rc``) - find last breakpoint in the past
21043185f7bSPavel Dovgalyuk
21143185f7bSPavel DovgalyukReverse step loads the nearest snapshot and replays the execution until
21243185f7bSPavel Dovgalyukthe required instruction is met.
21343185f7bSPavel Dovgalyuk
21443185f7bSPavel DovgalyukReverse continue may include several passes of examining the execution
21543185f7bSPavel Dovgalyukbetween the snapshots. Each of the passes include the following steps:
21643185f7bSPavel Dovgalyuk
21743185f7bSPavel Dovgalyuk #. loading the snapshot
21843185f7bSPavel Dovgalyuk #. replaying to examine the breakpoints
21943185f7bSPavel Dovgalyuk #. if breakpoint or watchpoint was met
22043185f7bSPavel Dovgalyuk
22143185f7bSPavel Dovgalyuk    * loading the snapshot again
22243185f7bSPavel Dovgalyuk    * replaying to the required breakpoint
22343185f7bSPavel Dovgalyuk
22443185f7bSPavel Dovgalyuk #. else
22543185f7bSPavel Dovgalyuk
22643185f7bSPavel Dovgalyuk    * proceeding to the p.1 with the earlier snapshot
22743185f7bSPavel Dovgalyuk
22843185f7bSPavel DovgalyukTherefore usage of the reverse debugging requires at least one snapshot
22943185f7bSPavel Dovgalyukcreated. This can be done by omitting ``snapshot`` option
23043185f7bSPavel Dovgalyukfor the block drives and adding ``rrsnapshot`` for both record and replay
23143185f7bSPavel Dovgalyukcommand lines.
23243185f7bSPavel DovgalyukSee the :ref:`snapshotting-label` section to learn more about running record/replay
23343185f7bSPavel Dovgalyukand creating the snapshot in these modes.
23443185f7bSPavel Dovgalyuk
23543185f7bSPavel DovgalyukWhen ``rrsnapshot`` is not used, then snapshot named ``start_debugging``
23643185f7bSPavel Dovgalyukcreated in temporary overlay. This allows using reverse debugging, but with
23743185f7bSPavel Dovgalyuktemporary snapshots (existing within the session).
238