replay.rst 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. .. _replay:
  2. ..
  3. Copyright (c) 2010-2022 Institute for System Programming
  4. of the Russian Academy of Sciences.
  5. This work is licensed under the terms of the GNU GPL, version 2 or later.
  6. See the COPYING file in the top-level directory.
  7. Record/replay
  8. =============
  9. Record/replay functions are used for the deterministic replay of qemu execution.
  10. Execution recording writes a non-deterministic events log, which can be later
  11. used for replaying the execution anywhere and for unlimited number of times.
  12. It also supports checkpointing for faster rewind to the specific replay moment.
  13. Execution replaying reads the log and replays all non-deterministic events
  14. including external input, hardware clocks, and interrupts.
  15. Deterministic replay has the following features:
  16. * Deterministically replays whole system execution and all contents of
  17. the memory, state of the hardware devices, clocks, and screen of the VM.
  18. * Writes execution log into the file for later replaying for multiple times
  19. on different machines.
  20. * Supports i386, x86_64, ARM, AArch64, Risc-V, MIPS, MIPS64, S390X, Alpha,
  21. PowerPC, PowerPC64, M68000, Microblaze, OpenRISC, SPARC,
  22. and Xtensa hardware platforms.
  23. * Performs deterministic replay of all operations with keyboard and mouse
  24. input devices, serial ports, and network.
  25. Usage of the record/replay:
  26. * First, record the execution with the following command line:
  27. .. parsed-literal::
  28. |qemu_system| \\
  29. -icount shift=auto,rr=record,rrfile=replay.bin \\
  30. -drive file=disk.qcow2,if=none,snapshot,id=img-direct \\
  31. -drive driver=blkreplay,if=none,image=img-direct,id=img-blkreplay \\
  32. -device ide-hd,drive=img-blkreplay \\
  33. -netdev user,id=net1 -device rtl8139,netdev=net1 \\
  34. -object filter-replay,id=replay,netdev=net1
  35. * After recording, you can replay it by using another command line:
  36. .. parsed-literal::
  37. |qemu_system| \\
  38. -icount shift=auto,rr=replay,rrfile=replay.bin \\
  39. -drive file=disk.qcow2,if=none,snapshot,id=img-direct \\
  40. -drive driver=blkreplay,if=none,image=img-direct,id=img-blkreplay \\
  41. -device ide-hd,drive=img-blkreplay \\
  42. -netdev user,id=net1 -device rtl8139,netdev=net1 \\
  43. -object filter-replay,id=replay,netdev=net1
  44. The only difference with recording is changing the rr option
  45. from record to replay.
  46. * Block device images are not actually changed in the recording mode,
  47. because all of the changes are written to the temporary overlay file.
  48. This behavior is enabled by using blkreplay driver. It should be used
  49. for every enabled block device, as described in :ref:`block-label` section.
  50. * ``-net none`` option should be specified when network is not used,
  51. because QEMU adds network card by default. When network is needed,
  52. it should be configured explicitly with replay filter, as described
  53. in :ref:`network-label` section.
  54. * Interaction with audio devices and serial ports are recorded and replayed
  55. automatically when such devices are enabled.
  56. Core idea
  57. ---------
  58. Record/replay system is based on saving and replaying non-deterministic
  59. events (e.g. keyboard input) and simulating deterministic ones (e.g. reading
  60. from HDD or memory of the VM). Saving only non-deterministic events makes
  61. log file smaller and simulation faster.
  62. The following non-deterministic data from peripheral devices is saved into
  63. the log: mouse and keyboard input, network packets, audio controller input,
  64. serial port input, and hardware clocks (they are non-deterministic
  65. too, because their values are taken from the host machine). Inputs from
  66. simulated hardware, memory of VM, software interrupts, and execution of
  67. instructions are not saved into the log, because they are deterministic and
  68. can be replayed by simulating the behavior of virtual machine starting from
  69. initial state.
  70. Instruction counting
  71. --------------------
  72. QEMU should work in icount mode to use record/replay feature. icount was
  73. designed to allow deterministic execution in absence of external inputs
  74. of the virtual machine. Record/replay feature is enabled through ``-icount``
  75. command-line option, making possible deterministic execution of the machine,
  76. interacting with user or network.
  77. .. _block-label:
  78. Block devices
  79. -------------
  80. Block devices record/replay module intercepts calls of
  81. bdrv coroutine functions at the top of block drivers stack.
  82. To record and replay block operations the drive must be configured
  83. as following:
  84. .. parsed-literal::
  85. -drive file=disk.qcow2,if=none,snapshot,id=img-direct
  86. -drive driver=blkreplay,if=none,image=img-direct,id=img-blkreplay
  87. -device ide-hd,drive=img-blkreplay
  88. blkreplay driver should be inserted between disk image and virtual driver
  89. controller. Therefore all disk requests may be recorded and replayed.
  90. .. _snapshotting-label:
  91. Snapshotting
  92. ------------
  93. New VM snapshots may be created in replay mode. They can be used later
  94. to recover the desired VM state. All VM states created in replay mode
  95. are associated with the moment of time in the replay scenario.
  96. After recovering the VM state replay will start from that position.
  97. Default starting snapshot name may be specified with icount field
  98. rrsnapshot as follows:
  99. .. parsed-literal::
  100. -icount shift=auto,rr=record,rrfile=replay.bin,rrsnapshot=snapshot_name
  101. This snapshot is created at start of recording and restored at start
  102. of replaying. It also can be loaded while replaying to roll back
  103. the execution.
  104. ``snapshot`` flag of the disk image must be removed to save the snapshots
  105. in the overlay (or original image) instead of using the temporary overlay.
  106. .. parsed-literal::
  107. -drive file=disk.ovl,if=none,id=img-direct
  108. -drive driver=blkreplay,if=none,image=img-direct,id=img-blkreplay
  109. -device ide-hd,drive=img-blkreplay
  110. Use QEMU monitor to create additional snapshots. ``savevm <name>`` command
  111. created the snapshot and ``loadvm <name>`` restores it. To prevent corruption
  112. of the original disk image, use overlay files linked to the original images.
  113. Therefore all new snapshots (including the starting one) will be saved in
  114. overlays and the original image remains unchanged.
  115. When you need to use snapshots with diskless virtual machine,
  116. it must be started with "orphan" qcow2 image. This image will be used
  117. for storing VM snapshots. Here is the example of the command line for this:
  118. .. parsed-literal::
  119. |qemu_system| \\
  120. -icount shift=auto,rr=replay,rrfile=record.bin,rrsnapshot=init \\
  121. -net none -drive file=empty.qcow2,if=none,id=rr
  122. ``empty.qcow2`` drive does not connected to any virtual block device and used
  123. for VM snapshots only.
  124. .. _network-label:
  125. Network devices
  126. ---------------
  127. Record and replay for network interactions is performed with the network filter.
  128. Each backend must have its own instance of the replay filter as follows:
  129. .. parsed-literal::
  130. -netdev user,id=net1 -device rtl8139,netdev=net1
  131. -object filter-replay,id=replay,netdev=net1
  132. Replay network filter is used to record and replay network packets. While
  133. recording the virtual machine this filter puts all packets coming from
  134. the outer world into the log. In replay mode packets from the log are
  135. injected into the network device. All interactions with network backend
  136. in replay mode are disabled.
  137. Audio devices
  138. -------------
  139. Audio data is recorded and replay automatically. The command line for recording
  140. and replaying must contain identical specifications of audio hardware, e.g.:
  141. .. parsed-literal::
  142. -audio pa,model=ac97
  143. Serial ports
  144. ------------
  145. Serial ports input is recorded and replay automatically. The command lines
  146. for recording and replaying must contain identical number of ports in record
  147. and replay modes, but their backends may differ.
  148. E.g., ``-serial stdio`` in record mode, and ``-serial null`` in replay mode.
  149. Reverse debugging
  150. -----------------
  151. Reverse debugging allows "executing" the program in reverse direction.
  152. GDB remote protocol supports "reverse step" and "reverse continue"
  153. commands. The first one steps single instruction backwards in time,
  154. and the second one finds the last breakpoint in the past.
  155. Recorded executions may be used to enable reverse debugging. QEMU can't
  156. execute the code in backwards direction, but can load a snapshot and
  157. replay forward to find the desired position or breakpoint.
  158. The following GDB commands are supported:
  159. - ``reverse-stepi`` (or ``rsi``) - step one instruction backwards
  160. - ``reverse-continue`` (or ``rc``) - find last breakpoint in the past
  161. Reverse step loads the nearest snapshot and replays the execution until
  162. the required instruction is met.
  163. Reverse continue may include several passes of examining the execution
  164. between the snapshots. Each of the passes include the following steps:
  165. #. loading the snapshot
  166. #. replaying to examine the breakpoints
  167. #. if breakpoint or watchpoint was met
  168. * loading the snapshot again
  169. * replaying to the required breakpoint
  170. #. else
  171. * proceeding to the p.1 with the earlier snapshot
  172. Therefore usage of the reverse debugging requires at least one snapshot
  173. created. This can be done by omitting ``snapshot`` option
  174. for the block drives and adding ``rrsnapshot`` for both record and replay
  175. command lines.
  176. See the :ref:`snapshotting-label` section to learn more about running record/replay
  177. and creating the snapshot in these modes.
  178. When ``rrsnapshot`` is not used, then snapshot named ``start_debugging``
  179. created in temporary overlay. This allows using reverse debugging, but with
  180. temporary snapshots (existing within the session).