Pavel Dovgalyuk | d73abd6 | 2015-09-17 19:23:37 +0300 | [diff] [blame] | 1 | /* |
| 2 | * replay.c |
| 3 | * |
| 4 | * Copyright (c) 2010-2015 Institute for System Programming |
| 5 | * of the Russian Academy of Sciences. |
| 6 | * |
| 7 | * This work is licensed under the terms of the GNU GPL, version 2 or later. |
| 8 | * See the COPYING file in the top-level directory. |
| 9 | * |
| 10 | */ |
| 11 | |
Peter Maydell | d38ea87 | 2016-01-29 17:50:05 +0000 | [diff] [blame] | 12 | #include "qemu/osdep.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 13 | #include "qapi/error.h" |
Pavel Dovgalyuk | d73abd6 | 2015-09-17 19:23:37 +0300 | [diff] [blame] | 14 | #include "sysemu/replay.h" |
Markus Armbruster | 54d3123 | 2019-08-12 07:23:59 +0200 | [diff] [blame] | 15 | #include "sysemu/runstate.h" |
Pavel Dovgalyuk | 26bc60a | 2015-09-17 19:23:54 +0300 | [diff] [blame] | 16 | #include "replay-internal.h" |
| 17 | #include "qemu/timer.h" |
Pavel Dovgalyuk | 8b42704 | 2015-09-17 19:24:05 +0300 | [diff] [blame] | 18 | #include "qemu/main-loop.h" |
Markus Armbruster | 922a01a | 2018-02-01 12:18:46 +0100 | [diff] [blame] | 19 | #include "qemu/option.h" |
Paolo Bonzini | d2528bd | 2017-03-03 12:01:16 +0100 | [diff] [blame] | 20 | #include "sysemu/cpus.h" |
Pavel Dovgalyuk | 7615936 | 2015-09-17 19:25:07 +0300 | [diff] [blame] | 21 | #include "qemu/error-report.h" |
| 22 | |
| 23 | /* Current version of the replay mechanism. |
| 24 | Increase it when file format changes. */ |
Pavel Dovgalyuk | 677a3ba | 2020-05-22 09:35:27 +0300 | [diff] [blame] | 25 | #define REPLAY_VERSION 0xe0200a |
Pavel Dovgalyuk | 7615936 | 2015-09-17 19:25:07 +0300 | [diff] [blame] | 26 | /* Size of replay log header */ |
| 27 | #define HEADER_SIZE (sizeof(uint32_t) + sizeof(uint64_t)) |
Pavel Dovgalyuk | d73abd6 | 2015-09-17 19:23:37 +0300 | [diff] [blame] | 28 | |
| 29 | ReplayMode replay_mode = REPLAY_MODE_NONE; |
Pavel Dovgalyuk | 9c2037d | 2017-01-24 10:17:47 +0300 | [diff] [blame] | 30 | char *replay_snapshot; |
Pavel Dovgalyuk | 26bc60a | 2015-09-17 19:23:54 +0300 | [diff] [blame] | 31 | |
Pavel Dovgalyuk | 7615936 | 2015-09-17 19:25:07 +0300 | [diff] [blame] | 32 | /* Name of replay file */ |
| 33 | static char *replay_filename; |
Pavel Dovgalyuk | 26bc60a | 2015-09-17 19:23:54 +0300 | [diff] [blame] | 34 | ReplayState replay_state; |
Pavel Dovgalyuk | 0194749 | 2015-09-17 19:25:13 +0300 | [diff] [blame] | 35 | static GSList *replay_blockers; |
Pavel Dovgalyuk | 26bc60a | 2015-09-17 19:23:54 +0300 | [diff] [blame] | 36 | |
| 37 | bool replay_next_event_is(int event) |
| 38 | { |
| 39 | bool res = false; |
| 40 | |
| 41 | /* nothing to skip - not all instructions used */ |
Pavel Dovgalyuk | 13f2671 | 2019-07-25 11:44:43 +0300 | [diff] [blame] | 42 | if (replay_state.instruction_count != 0) { |
Pavel Dovgalyuk | f186d64 | 2016-09-26 11:08:04 +0300 | [diff] [blame] | 43 | assert(replay_state.data_kind == EVENT_INSTRUCTION); |
Pavel Dovgalyuk | 26bc60a | 2015-09-17 19:23:54 +0300 | [diff] [blame] | 44 | return event == EVENT_INSTRUCTION; |
| 45 | } |
| 46 | |
| 47 | while (true) { |
Pavel Dovgalyuk | e957ad8 | 2019-07-25 11:44:32 +0300 | [diff] [blame] | 48 | unsigned int data_kind = replay_state.data_kind; |
| 49 | if (event == data_kind) { |
Pavel Dovgalyuk | 26bc60a | 2015-09-17 19:23:54 +0300 | [diff] [blame] | 50 | res = true; |
| 51 | } |
Pavel Dovgalyuk | e957ad8 | 2019-07-25 11:44:32 +0300 | [diff] [blame] | 52 | switch (data_kind) { |
Eric Blake | 802f045 | 2017-05-15 16:41:12 -0500 | [diff] [blame] | 53 | case EVENT_SHUTDOWN ... EVENT_SHUTDOWN_LAST: |
Pavel Dovgalyuk | b60c48a | 2015-09-17 19:24:33 +0300 | [diff] [blame] | 54 | replay_finish_event(); |
Pavel Dovgalyuk | e957ad8 | 2019-07-25 11:44:32 +0300 | [diff] [blame] | 55 | qemu_system_shutdown_request(data_kind - EVENT_SHUTDOWN); |
Pavel Dovgalyuk | b60c48a | 2015-09-17 19:24:33 +0300 | [diff] [blame] | 56 | break; |
Pavel Dovgalyuk | 26bc60a | 2015-09-17 19:23:54 +0300 | [diff] [blame] | 57 | default: |
| 58 | /* clock, time_t, checkpoint and other events */ |
| 59 | return res; |
| 60 | } |
| 61 | } |
| 62 | return res; |
| 63 | } |
| 64 | |
Pavel Dovgalyuk | 13f2671 | 2019-07-25 11:44:43 +0300 | [diff] [blame] | 65 | uint64_t replay_get_current_icount(void) |
Pavel Dovgalyuk | 26bc60a | 2015-09-17 19:23:54 +0300 | [diff] [blame] | 66 | { |
| 67 | return cpu_get_icount_raw(); |
| 68 | } |
Pavel Dovgalyuk | 8b42704 | 2015-09-17 19:24:05 +0300 | [diff] [blame] | 69 | |
| 70 | int replay_get_instructions(void) |
| 71 | { |
| 72 | int res = 0; |
| 73 | replay_mutex_lock(); |
| 74 | if (replay_next_event_is(EVENT_INSTRUCTION)) { |
Pavel Dovgalyuk | 13f2671 | 2019-07-25 11:44:43 +0300 | [diff] [blame] | 75 | res = replay_state.instruction_count; |
Pavel Dovgalyuk | 8b42704 | 2015-09-17 19:24:05 +0300 | [diff] [blame] | 76 | } |
| 77 | replay_mutex_unlock(); |
| 78 | return res; |
| 79 | } |
| 80 | |
| 81 | void replay_account_executed_instructions(void) |
| 82 | { |
| 83 | if (replay_mode == REPLAY_MODE_PLAY) { |
Alex Bennée | d759c95 | 2018-02-27 12:52:48 +0300 | [diff] [blame] | 84 | g_assert(replay_mutex_locked()); |
Pavel Dovgalyuk | 13f2671 | 2019-07-25 11:44:43 +0300 | [diff] [blame] | 85 | if (replay_state.instruction_count > 0) { |
| 86 | int count = (int)(replay_get_current_icount() |
| 87 | - replay_state.current_icount); |
Alex Bennée | 982263c | 2017-04-05 11:05:28 +0100 | [diff] [blame] | 88 | |
| 89 | /* Time can only go forward */ |
| 90 | assert(count >= 0); |
| 91 | |
Pavel Dovgalyuk | 13f2671 | 2019-07-25 11:44:43 +0300 | [diff] [blame] | 92 | replay_state.instruction_count -= count; |
| 93 | replay_state.current_icount += count; |
| 94 | if (replay_state.instruction_count == 0) { |
Pavel Dovgalyuk | f186d64 | 2016-09-26 11:08:04 +0300 | [diff] [blame] | 95 | assert(replay_state.data_kind == EVENT_INSTRUCTION); |
Pavel Dovgalyuk | 8b42704 | 2015-09-17 19:24:05 +0300 | [diff] [blame] | 96 | replay_finish_event(); |
| 97 | /* Wake up iothread. This is required because |
| 98 | timers will not expire until clock counters |
| 99 | will be read from the log. */ |
| 100 | qemu_notify_event(); |
| 101 | } |
| 102 | } |
Pavel Dovgalyuk | 8b42704 | 2015-09-17 19:24:05 +0300 | [diff] [blame] | 103 | } |
| 104 | } |
Pavel Dovgalyuk | 6f06096 | 2015-09-17 19:24:16 +0300 | [diff] [blame] | 105 | |
| 106 | bool replay_exception(void) |
| 107 | { |
Alex Bennée | d759c95 | 2018-02-27 12:52:48 +0300 | [diff] [blame] | 108 | |
Pavel Dovgalyuk | 6f06096 | 2015-09-17 19:24:16 +0300 | [diff] [blame] | 109 | if (replay_mode == REPLAY_MODE_RECORD) { |
Alex Bennée | d759c95 | 2018-02-27 12:52:48 +0300 | [diff] [blame] | 110 | g_assert(replay_mutex_locked()); |
Pavel Dovgalyuk | 6f06096 | 2015-09-17 19:24:16 +0300 | [diff] [blame] | 111 | replay_save_instructions(); |
Pavel Dovgalyuk | 6f06096 | 2015-09-17 19:24:16 +0300 | [diff] [blame] | 112 | replay_put_event(EVENT_EXCEPTION); |
Pavel Dovgalyuk | 6f06096 | 2015-09-17 19:24:16 +0300 | [diff] [blame] | 113 | return true; |
| 114 | } else if (replay_mode == REPLAY_MODE_PLAY) { |
Alex Bennée | d759c95 | 2018-02-27 12:52:48 +0300 | [diff] [blame] | 115 | g_assert(replay_mutex_locked()); |
Pavel Dovgalyuk | 6f06096 | 2015-09-17 19:24:16 +0300 | [diff] [blame] | 116 | bool res = replay_has_exception(); |
| 117 | if (res) { |
Pavel Dovgalyuk | 6f06096 | 2015-09-17 19:24:16 +0300 | [diff] [blame] | 118 | replay_finish_event(); |
Pavel Dovgalyuk | 6f06096 | 2015-09-17 19:24:16 +0300 | [diff] [blame] | 119 | } |
| 120 | return res; |
| 121 | } |
| 122 | |
| 123 | return true; |
| 124 | } |
| 125 | |
| 126 | bool replay_has_exception(void) |
| 127 | { |
| 128 | bool res = false; |
| 129 | if (replay_mode == REPLAY_MODE_PLAY) { |
Alex Bennée | d759c95 | 2018-02-27 12:52:48 +0300 | [diff] [blame] | 130 | g_assert(replay_mutex_locked()); |
Pavel Dovgalyuk | 6f06096 | 2015-09-17 19:24:16 +0300 | [diff] [blame] | 131 | replay_account_executed_instructions(); |
Pavel Dovgalyuk | 6f06096 | 2015-09-17 19:24:16 +0300 | [diff] [blame] | 132 | res = replay_next_event_is(EVENT_EXCEPTION); |
Pavel Dovgalyuk | 6f06096 | 2015-09-17 19:24:16 +0300 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | return res; |
| 136 | } |
| 137 | |
| 138 | bool replay_interrupt(void) |
| 139 | { |
| 140 | if (replay_mode == REPLAY_MODE_RECORD) { |
Alex Bennée | d759c95 | 2018-02-27 12:52:48 +0300 | [diff] [blame] | 141 | g_assert(replay_mutex_locked()); |
Pavel Dovgalyuk | 6f06096 | 2015-09-17 19:24:16 +0300 | [diff] [blame] | 142 | replay_save_instructions(); |
Pavel Dovgalyuk | 6f06096 | 2015-09-17 19:24:16 +0300 | [diff] [blame] | 143 | replay_put_event(EVENT_INTERRUPT); |
Pavel Dovgalyuk | 6f06096 | 2015-09-17 19:24:16 +0300 | [diff] [blame] | 144 | return true; |
| 145 | } else if (replay_mode == REPLAY_MODE_PLAY) { |
Alex Bennée | d759c95 | 2018-02-27 12:52:48 +0300 | [diff] [blame] | 146 | g_assert(replay_mutex_locked()); |
Pavel Dovgalyuk | 6f06096 | 2015-09-17 19:24:16 +0300 | [diff] [blame] | 147 | bool res = replay_has_interrupt(); |
| 148 | if (res) { |
Pavel Dovgalyuk | 6f06096 | 2015-09-17 19:24:16 +0300 | [diff] [blame] | 149 | replay_finish_event(); |
Pavel Dovgalyuk | 6f06096 | 2015-09-17 19:24:16 +0300 | [diff] [blame] | 150 | } |
| 151 | return res; |
| 152 | } |
| 153 | |
| 154 | return true; |
| 155 | } |
| 156 | |
| 157 | bool replay_has_interrupt(void) |
| 158 | { |
| 159 | bool res = false; |
| 160 | if (replay_mode == REPLAY_MODE_PLAY) { |
Alex Bennée | d759c95 | 2018-02-27 12:52:48 +0300 | [diff] [blame] | 161 | g_assert(replay_mutex_locked()); |
Pavel Dovgalyuk | 6f06096 | 2015-09-17 19:24:16 +0300 | [diff] [blame] | 162 | replay_account_executed_instructions(); |
Pavel Dovgalyuk | 6f06096 | 2015-09-17 19:24:16 +0300 | [diff] [blame] | 163 | res = replay_next_event_is(EVENT_INTERRUPT); |
Pavel Dovgalyuk | 6f06096 | 2015-09-17 19:24:16 +0300 | [diff] [blame] | 164 | } |
| 165 | return res; |
| 166 | } |
Pavel Dovgalyuk | b60c48a | 2015-09-17 19:24:33 +0300 | [diff] [blame] | 167 | |
Eric Blake | 802f045 | 2017-05-15 16:41:12 -0500 | [diff] [blame] | 168 | void replay_shutdown_request(ShutdownCause cause) |
Pavel Dovgalyuk | b60c48a | 2015-09-17 19:24:33 +0300 | [diff] [blame] | 169 | { |
| 170 | if (replay_mode == REPLAY_MODE_RECORD) { |
Alex Bennée | d759c95 | 2018-02-27 12:52:48 +0300 | [diff] [blame] | 171 | g_assert(replay_mutex_locked()); |
Eric Blake | 802f045 | 2017-05-15 16:41:12 -0500 | [diff] [blame] | 172 | replay_put_event(EVENT_SHUTDOWN + cause); |
Pavel Dovgalyuk | b60c48a | 2015-09-17 19:24:33 +0300 | [diff] [blame] | 173 | } |
| 174 | } |
Pavel Dovgalyuk | 8bd7f71 | 2015-09-17 19:24:44 +0300 | [diff] [blame] | 175 | |
| 176 | bool replay_checkpoint(ReplayCheckpoint checkpoint) |
| 177 | { |
| 178 | bool res = false; |
Pavel Dovgalyuk | 66eb782 | 2018-02-27 12:53:05 +0300 | [diff] [blame] | 179 | static bool in_checkpoint; |
Pavel Dovgalyuk | 8bd7f71 | 2015-09-17 19:24:44 +0300 | [diff] [blame] | 180 | assert(EVENT_CHECKPOINT + checkpoint <= EVENT_CHECKPOINT_LAST); |
Pavel Dovgalyuk | 8bd7f71 | 2015-09-17 19:24:44 +0300 | [diff] [blame] | 181 | |
| 182 | if (!replay_file) { |
| 183 | return true; |
| 184 | } |
| 185 | |
Pavel Dovgalyuk | 66eb782 | 2018-02-27 12:53:05 +0300 | [diff] [blame] | 186 | if (in_checkpoint) { |
| 187 | /* If we are already in checkpoint, then there is no need |
| 188 | for additional synchronization. |
| 189 | Recursion occurs when HW event modifies timers. |
| 190 | Timer modification may invoke the checkpoint and |
| 191 | proceed to recursion. */ |
| 192 | return true; |
| 193 | } |
| 194 | in_checkpoint = true; |
| 195 | |
| 196 | replay_save_instructions(); |
Pavel Dovgalyuk | 8bd7f71 | 2015-09-17 19:24:44 +0300 | [diff] [blame] | 197 | |
| 198 | if (replay_mode == REPLAY_MODE_PLAY) { |
Alex Bennée | d759c95 | 2018-02-27 12:52:48 +0300 | [diff] [blame] | 199 | g_assert(replay_mutex_locked()); |
Pavel Dovgalyuk | 8bd7f71 | 2015-09-17 19:24:44 +0300 | [diff] [blame] | 200 | if (replay_next_event_is(EVENT_CHECKPOINT + checkpoint)) { |
| 201 | replay_finish_event(); |
Pavel Dovgalyuk | f186d64 | 2016-09-26 11:08:04 +0300 | [diff] [blame] | 202 | } else if (replay_state.data_kind != EVENT_ASYNC) { |
Pavel Dovgalyuk | 8bd7f71 | 2015-09-17 19:24:44 +0300 | [diff] [blame] | 203 | res = false; |
| 204 | goto out; |
| 205 | } |
| 206 | replay_read_events(checkpoint); |
| 207 | /* replay_read_events may leave some unread events. |
| 208 | Return false if not all of the events associated with |
| 209 | checkpoint were processed */ |
Pavel Dovgalyuk | f186d64 | 2016-09-26 11:08:04 +0300 | [diff] [blame] | 210 | res = replay_state.data_kind != EVENT_ASYNC; |
Pavel Dovgalyuk | 8bd7f71 | 2015-09-17 19:24:44 +0300 | [diff] [blame] | 211 | } else if (replay_mode == REPLAY_MODE_RECORD) { |
Alex Bennée | d759c95 | 2018-02-27 12:52:48 +0300 | [diff] [blame] | 212 | g_assert(replay_mutex_locked()); |
Pavel Dovgalyuk | 8bd7f71 | 2015-09-17 19:24:44 +0300 | [diff] [blame] | 213 | replay_put_event(EVENT_CHECKPOINT + checkpoint); |
Pavel Dovgalyuk | 89e46eb | 2018-02-27 12:53:16 +0300 | [diff] [blame] | 214 | /* This checkpoint belongs to several threads. |
| 215 | Processing events from different threads is |
| 216 | non-deterministic */ |
Pavel Dovgalyuk | ca9759c | 2018-10-18 09:33:45 +0300 | [diff] [blame] | 217 | if (checkpoint != CHECKPOINT_CLOCK_WARP_START |
| 218 | /* FIXME: this is temporary fix, other checkpoints |
| 219 | may also be invoked from the different threads someday. |
| 220 | Asynchronous event processing should be refactored |
| 221 | to create additional replay event kind which is |
| 222 | nailed to the one of the threads and which processes |
| 223 | the event queue. */ |
| 224 | && checkpoint != CHECKPOINT_CLOCK_VIRTUAL) { |
Pavel Dovgalyuk | 89e46eb | 2018-02-27 12:53:16 +0300 | [diff] [blame] | 225 | replay_save_events(checkpoint); |
| 226 | } |
Pavel Dovgalyuk | 8bd7f71 | 2015-09-17 19:24:44 +0300 | [diff] [blame] | 227 | res = true; |
| 228 | } |
| 229 | out: |
Pavel Dovgalyuk | 66eb782 | 2018-02-27 12:53:05 +0300 | [diff] [blame] | 230 | in_checkpoint = false; |
Pavel Dovgalyuk | 8bd7f71 | 2015-09-17 19:24:44 +0300 | [diff] [blame] | 231 | return res; |
| 232 | } |
Pavel Dovgalyuk | 7615936 | 2015-09-17 19:25:07 +0300 | [diff] [blame] | 233 | |
Pavel Dovgalyuk | 0c08185 | 2018-09-12 11:19:45 +0300 | [diff] [blame] | 234 | bool replay_has_checkpoint(void) |
| 235 | { |
| 236 | bool res = false; |
| 237 | if (replay_mode == REPLAY_MODE_PLAY) { |
| 238 | g_assert(replay_mutex_locked()); |
| 239 | replay_account_executed_instructions(); |
| 240 | res = EVENT_CHECKPOINT <= replay_state.data_kind |
| 241 | && replay_state.data_kind <= EVENT_CHECKPOINT_LAST; |
| 242 | } |
| 243 | return res; |
| 244 | } |
| 245 | |
Pavel Dovgalyuk | 7615936 | 2015-09-17 19:25:07 +0300 | [diff] [blame] | 246 | static void replay_enable(const char *fname, int mode) |
| 247 | { |
| 248 | const char *fmode = NULL; |
| 249 | assert(!replay_file); |
| 250 | |
| 251 | switch (mode) { |
| 252 | case REPLAY_MODE_RECORD: |
| 253 | fmode = "wb"; |
| 254 | break; |
| 255 | case REPLAY_MODE_PLAY: |
| 256 | fmode = "rb"; |
| 257 | break; |
| 258 | default: |
| 259 | fprintf(stderr, "Replay: internal error: invalid replay mode\n"); |
| 260 | exit(1); |
| 261 | } |
| 262 | |
| 263 | atexit(replay_finish); |
| 264 | |
Pavel Dovgalyuk | 7615936 | 2015-09-17 19:25:07 +0300 | [diff] [blame] | 265 | replay_file = fopen(fname, fmode); |
| 266 | if (replay_file == NULL) { |
| 267 | fprintf(stderr, "Replay: open %s: %s\n", fname, strerror(errno)); |
| 268 | exit(1); |
| 269 | } |
| 270 | |
| 271 | replay_filename = g_strdup(fname); |
Pavel Dovgalyuk | 7615936 | 2015-09-17 19:25:07 +0300 | [diff] [blame] | 272 | replay_mode = mode; |
Alex Bennée | d759c95 | 2018-02-27 12:52:48 +0300 | [diff] [blame] | 273 | replay_mutex_init(); |
| 274 | |
Pavel Dovgalyuk | f186d64 | 2016-09-26 11:08:04 +0300 | [diff] [blame] | 275 | replay_state.data_kind = -1; |
Pavel Dovgalyuk | 13f2671 | 2019-07-25 11:44:43 +0300 | [diff] [blame] | 276 | replay_state.instruction_count = 0; |
| 277 | replay_state.current_icount = 0; |
Pavel Dovgalyuk | f186d64 | 2016-09-26 11:08:04 +0300 | [diff] [blame] | 278 | replay_state.has_unread_data = 0; |
Pavel Dovgalyuk | 7615936 | 2015-09-17 19:25:07 +0300 | [diff] [blame] | 279 | |
| 280 | /* skip file header for RECORD and check it for PLAY */ |
| 281 | if (replay_mode == REPLAY_MODE_RECORD) { |
| 282 | fseek(replay_file, HEADER_SIZE, SEEK_SET); |
| 283 | } else if (replay_mode == REPLAY_MODE_PLAY) { |
| 284 | unsigned int version = replay_get_dword(); |
| 285 | if (version != REPLAY_VERSION) { |
| 286 | fprintf(stderr, "Replay: invalid input log file version\n"); |
| 287 | exit(1); |
| 288 | } |
| 289 | /* go to the beginning */ |
| 290 | fseek(replay_file, HEADER_SIZE, SEEK_SET); |
| 291 | replay_fetch_data_kind(); |
| 292 | } |
| 293 | |
| 294 | replay_init_events(); |
| 295 | } |
| 296 | |
| 297 | void replay_configure(QemuOpts *opts) |
| 298 | { |
| 299 | const char *fname; |
| 300 | const char *rr; |
| 301 | ReplayMode mode = REPLAY_MODE_NONE; |
Eduardo Habkost | 890ad55 | 2016-02-12 17:02:26 -0200 | [diff] [blame] | 302 | Location loc; |
| 303 | |
| 304 | if (!opts) { |
| 305 | return; |
| 306 | } |
| 307 | |
| 308 | loc_push_none(&loc); |
| 309 | qemu_opts_loc_restore(opts); |
Pavel Dovgalyuk | 7615936 | 2015-09-17 19:25:07 +0300 | [diff] [blame] | 310 | |
| 311 | rr = qemu_opt_get(opts, "rr"); |
| 312 | if (!rr) { |
| 313 | /* Just enabling icount */ |
Markus Armbruster | d9d3aae | 2016-04-27 16:29:08 +0200 | [diff] [blame] | 314 | goto out; |
Pavel Dovgalyuk | 7615936 | 2015-09-17 19:25:07 +0300 | [diff] [blame] | 315 | } else if (!strcmp(rr, "record")) { |
| 316 | mode = REPLAY_MODE_RECORD; |
| 317 | } else if (!strcmp(rr, "replay")) { |
| 318 | mode = REPLAY_MODE_PLAY; |
| 319 | } else { |
| 320 | error_report("Invalid icount rr option: %s", rr); |
| 321 | exit(1); |
| 322 | } |
| 323 | |
| 324 | fname = qemu_opt_get(opts, "rrfile"); |
| 325 | if (!fname) { |
| 326 | error_report("File name not specified for replay"); |
| 327 | exit(1); |
| 328 | } |
| 329 | |
Pavel Dovgalyuk | 9c2037d | 2017-01-24 10:17:47 +0300 | [diff] [blame] | 330 | replay_snapshot = g_strdup(qemu_opt_get(opts, "rrsnapshot")); |
Pavel Dovgalyuk | 306e196 | 2016-09-26 11:08:10 +0300 | [diff] [blame] | 331 | replay_vmstate_register(); |
Pavel Dovgalyuk | 7615936 | 2015-09-17 19:25:07 +0300 | [diff] [blame] | 332 | replay_enable(fname, mode); |
Eduardo Habkost | 890ad55 | 2016-02-12 17:02:26 -0200 | [diff] [blame] | 333 | |
Markus Armbruster | d9d3aae | 2016-04-27 16:29:08 +0200 | [diff] [blame] | 334 | out: |
Eduardo Habkost | 890ad55 | 2016-02-12 17:02:26 -0200 | [diff] [blame] | 335 | loc_pop(&loc); |
Pavel Dovgalyuk | 7615936 | 2015-09-17 19:25:07 +0300 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | void replay_start(void) |
| 339 | { |
| 340 | if (replay_mode == REPLAY_MODE_NONE) { |
| 341 | return; |
| 342 | } |
| 343 | |
Pavel Dovgalyuk | 0194749 | 2015-09-17 19:25:13 +0300 | [diff] [blame] | 344 | if (replay_blockers) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 345 | error_reportf_err(replay_blockers->data, "Record/replay: "); |
Pavel Dovgalyuk | 0194749 | 2015-09-17 19:25:13 +0300 | [diff] [blame] | 346 | exit(1); |
| 347 | } |
Pavel Dovgalyuk | 4c27b85 | 2015-09-17 19:25:18 +0300 | [diff] [blame] | 348 | if (!use_icount) { |
| 349 | error_report("Please enable icount to use record/replay"); |
| 350 | exit(1); |
| 351 | } |
Pavel Dovgalyuk | 0194749 | 2015-09-17 19:25:13 +0300 | [diff] [blame] | 352 | |
Pavel Dovgalyuk | 7615936 | 2015-09-17 19:25:07 +0300 | [diff] [blame] | 353 | /* Timer for snapshotting will be set up here. */ |
| 354 | |
| 355 | replay_enable_events(); |
| 356 | } |
| 357 | |
| 358 | void replay_finish(void) |
| 359 | { |
| 360 | if (replay_mode == REPLAY_MODE_NONE) { |
| 361 | return; |
| 362 | } |
| 363 | |
| 364 | replay_save_instructions(); |
| 365 | |
| 366 | /* finalize the file */ |
| 367 | if (replay_file) { |
| 368 | if (replay_mode == REPLAY_MODE_RECORD) { |
Pavel Dovgalyuk | ed5d7ff | 2020-05-22 09:45:54 +0300 | [diff] [blame] | 369 | /* |
| 370 | * Can't do it in the signal handler, therefore |
| 371 | * add shutdown event here for the case of Ctrl-C. |
| 372 | */ |
| 373 | replay_shutdown_request(SHUTDOWN_CAUSE_HOST_SIGNAL); |
Pavel Dovgalyuk | 7615936 | 2015-09-17 19:25:07 +0300 | [diff] [blame] | 374 | /* write end event */ |
| 375 | replay_put_event(EVENT_END); |
| 376 | |
| 377 | /* write header */ |
| 378 | fseek(replay_file, 0, SEEK_SET); |
| 379 | replay_put_dword(REPLAY_VERSION); |
| 380 | } |
| 381 | |
| 382 | fclose(replay_file); |
| 383 | replay_file = NULL; |
| 384 | } |
| 385 | if (replay_filename) { |
| 386 | g_free(replay_filename); |
| 387 | replay_filename = NULL; |
| 388 | } |
| 389 | |
Pavel Dovgalyuk | 9c2037d | 2017-01-24 10:17:47 +0300 | [diff] [blame] | 390 | g_free(replay_snapshot); |
| 391 | replay_snapshot = NULL; |
| 392 | |
Pavel Dovgalyuk | ae25dcc | 2019-09-17 14:58:13 +0300 | [diff] [blame] | 393 | replay_mode = REPLAY_MODE_NONE; |
| 394 | |
Pavel Dovgalyuk | 7615936 | 2015-09-17 19:25:07 +0300 | [diff] [blame] | 395 | replay_finish_events(); |
Pavel Dovgalyuk | 7615936 | 2015-09-17 19:25:07 +0300 | [diff] [blame] | 396 | } |
Pavel Dovgalyuk | 0194749 | 2015-09-17 19:25:13 +0300 | [diff] [blame] | 397 | |
| 398 | void replay_add_blocker(Error *reason) |
| 399 | { |
| 400 | replay_blockers = g_slist_prepend(replay_blockers, reason); |
| 401 | } |