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" |
Philippe Mathieu-Daudé | 32cad1f | 2024-12-03 15:20:13 +0100 | [diff] [blame] | 14 | #include "system/cpu-timers.h" |
| 15 | #include "system/replay.h" |
| 16 | #include "system/runstate.h" |
Pavel Dovgalyuk | 26bc60a | 2015-09-17 19:23:54 +0300 | [diff] [blame] | 17 | #include "replay-internal.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" |
Philippe Mathieu-Daudé | 32cad1f | 2024-12-03 15:20:13 +0100 | [diff] [blame] | 20 | #include "system/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 | 3e21408 | 2022-05-27 13:46:23 +0300 | [diff] [blame] | 25 | #define REPLAY_VERSION 0xe0200c |
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 | |
Pavel Dovgalyuk | e751067 | 2020-10-03 20:13:26 +0300 | [diff] [blame] | 37 | /* Replay breakpoints */ |
| 38 | uint64_t replay_break_icount = -1ULL; |
| 39 | QEMUTimer *replay_break_timer; |
| 40 | |
Alex Bennée | dcda732 | 2023-12-11 09:13:38 +0000 | [diff] [blame] | 41 | /* Pretty print event names */ |
| 42 | |
| 43 | static const char *replay_async_event_name(ReplayAsyncEventKind event) |
| 44 | { |
| 45 | switch (event) { |
| 46 | #define ASYNC_EVENT(_x) case REPLAY_ASYNC_EVENT_ ## _x: return "ASYNC_EVENT_"#_x |
| 47 | ASYNC_EVENT(BH); |
| 48 | ASYNC_EVENT(BH_ONESHOT); |
| 49 | ASYNC_EVENT(INPUT); |
| 50 | ASYNC_EVENT(INPUT_SYNC); |
| 51 | ASYNC_EVENT(CHAR_READ); |
| 52 | ASYNC_EVENT(BLOCK); |
| 53 | ASYNC_EVENT(NET); |
| 54 | #undef ASYNC_EVENT |
| 55 | default: |
| 56 | g_assert_not_reached(); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | static const char *replay_clock_event_name(ReplayClockKind clock) |
| 61 | { |
| 62 | switch (clock) { |
| 63 | #define CLOCK_EVENT(_x) case REPLAY_CLOCK_ ## _x: return "CLOCK_" #_x |
| 64 | CLOCK_EVENT(HOST); |
| 65 | CLOCK_EVENT(VIRTUAL_RT); |
| 66 | #undef CLOCK_EVENT |
| 67 | default: |
| 68 | g_assert_not_reached(); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /* Pretty print shutdown event names */ |
| 73 | static const char *replay_shutdown_event_name(ShutdownCause cause) |
| 74 | { |
| 75 | switch (cause) { |
| 76 | #define SHUTDOWN_EVENT(_x) case SHUTDOWN_CAUSE_ ## _x: return "SHUTDOWN_CAUSE_" #_x |
| 77 | SHUTDOWN_EVENT(NONE); |
| 78 | SHUTDOWN_EVENT(HOST_ERROR); |
| 79 | SHUTDOWN_EVENT(HOST_QMP_QUIT); |
| 80 | SHUTDOWN_EVENT(HOST_QMP_SYSTEM_RESET); |
| 81 | SHUTDOWN_EVENT(HOST_SIGNAL); |
| 82 | SHUTDOWN_EVENT(HOST_UI); |
| 83 | SHUTDOWN_EVENT(GUEST_SHUTDOWN); |
| 84 | SHUTDOWN_EVENT(GUEST_RESET); |
| 85 | SHUTDOWN_EVENT(GUEST_PANIC); |
| 86 | SHUTDOWN_EVENT(SUBSYSTEM_RESET); |
| 87 | SHUTDOWN_EVENT(SNAPSHOT_LOAD); |
| 88 | #undef SHUTDOWN_EVENT |
| 89 | default: |
| 90 | g_assert_not_reached(); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | static const char *replay_checkpoint_event_name(enum ReplayCheckpoint checkpoint) |
| 95 | { |
| 96 | switch (checkpoint) { |
| 97 | #define CHECKPOINT_EVENT(_x) case CHECKPOINT_ ## _x: return "CHECKPOINT_" #_x |
| 98 | CHECKPOINT_EVENT(CLOCK_WARP_START); |
| 99 | CHECKPOINT_EVENT(CLOCK_WARP_ACCOUNT); |
| 100 | CHECKPOINT_EVENT(RESET_REQUESTED); |
| 101 | CHECKPOINT_EVENT(SUSPEND_REQUESTED); |
| 102 | CHECKPOINT_EVENT(CLOCK_VIRTUAL); |
| 103 | CHECKPOINT_EVENT(CLOCK_HOST); |
| 104 | CHECKPOINT_EVENT(CLOCK_VIRTUAL_RT); |
| 105 | CHECKPOINT_EVENT(INIT); |
| 106 | CHECKPOINT_EVENT(RESET); |
| 107 | #undef CHECKPOINT_EVENT |
| 108 | default: |
| 109 | g_assert_not_reached(); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | static const char *replay_event_name(enum ReplayEvents event) |
| 114 | { |
| 115 | /* First deal with the simple ones */ |
| 116 | switch (event) { |
| 117 | #define EVENT(_x) case EVENT_ ## _x: return "EVENT_"#_x |
| 118 | EVENT(INSTRUCTION); |
| 119 | EVENT(INTERRUPT); |
| 120 | EVENT(EXCEPTION); |
| 121 | EVENT(CHAR_WRITE); |
| 122 | EVENT(CHAR_READ_ALL); |
| 123 | EVENT(AUDIO_OUT); |
| 124 | EVENT(AUDIO_IN); |
| 125 | EVENT(RANDOM); |
| 126 | #undef EVENT |
| 127 | default: |
| 128 | if (event >= EVENT_ASYNC && event <= EVENT_ASYNC_LAST) { |
| 129 | return replay_async_event_name(event - EVENT_ASYNC); |
| 130 | } else if (event >= EVENT_SHUTDOWN && event <= EVENT_SHUTDOWN_LAST) { |
| 131 | return replay_shutdown_event_name(event - EVENT_SHUTDOWN); |
| 132 | } else if (event >= EVENT_CLOCK && event <= EVENT_CLOCK_LAST) { |
| 133 | return replay_clock_event_name(event - EVENT_CLOCK); |
| 134 | } else if (event >= EVENT_CHECKPOINT && event <= EVENT_CHECKPOINT_LAST) { |
| 135 | return replay_checkpoint_event_name(event - EVENT_CHECKPOINT); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | g_assert_not_reached(); |
| 140 | } |
| 141 | |
Pavel Dovgalyuk | 26bc60a | 2015-09-17 19:23:54 +0300 | [diff] [blame] | 142 | bool replay_next_event_is(int event) |
| 143 | { |
| 144 | bool res = false; |
| 145 | |
| 146 | /* nothing to skip - not all instructions used */ |
Pavel Dovgalyuk | 13f2671 | 2019-07-25 11:44:43 +0300 | [diff] [blame] | 147 | if (replay_state.instruction_count != 0) { |
Pavel Dovgalyuk | f186d64 | 2016-09-26 11:08:04 +0300 | [diff] [blame] | 148 | assert(replay_state.data_kind == EVENT_INSTRUCTION); |
Pavel Dovgalyuk | 26bc60a | 2015-09-17 19:23:54 +0300 | [diff] [blame] | 149 | return event == EVENT_INSTRUCTION; |
| 150 | } |
| 151 | |
| 152 | while (true) { |
Pavel Dovgalyuk | e957ad8 | 2019-07-25 11:44:32 +0300 | [diff] [blame] | 153 | unsigned int data_kind = replay_state.data_kind; |
| 154 | if (event == data_kind) { |
Pavel Dovgalyuk | 26bc60a | 2015-09-17 19:23:54 +0300 | [diff] [blame] | 155 | res = true; |
| 156 | } |
Pavel Dovgalyuk | e957ad8 | 2019-07-25 11:44:32 +0300 | [diff] [blame] | 157 | switch (data_kind) { |
Eric Blake | 802f045 | 2017-05-15 16:41:12 -0500 | [diff] [blame] | 158 | case EVENT_SHUTDOWN ... EVENT_SHUTDOWN_LAST: |
Pavel Dovgalyuk | b60c48a | 2015-09-17 19:24:33 +0300 | [diff] [blame] | 159 | replay_finish_event(); |
Pavel Dovgalyuk | e957ad8 | 2019-07-25 11:44:32 +0300 | [diff] [blame] | 160 | qemu_system_shutdown_request(data_kind - EVENT_SHUTDOWN); |
Pavel Dovgalyuk | b60c48a | 2015-09-17 19:24:33 +0300 | [diff] [blame] | 161 | break; |
Pavel Dovgalyuk | 26bc60a | 2015-09-17 19:23:54 +0300 | [diff] [blame] | 162 | default: |
| 163 | /* clock, time_t, checkpoint and other events */ |
| 164 | return res; |
| 165 | } |
| 166 | } |
| 167 | return res; |
| 168 | } |
| 169 | |
Pavel Dovgalyuk | 13f2671 | 2019-07-25 11:44:43 +0300 | [diff] [blame] | 170 | uint64_t replay_get_current_icount(void) |
Pavel Dovgalyuk | 26bc60a | 2015-09-17 19:23:54 +0300 | [diff] [blame] | 171 | { |
Claudio Fontana | 8191d36 | 2020-08-31 16:18:34 +0200 | [diff] [blame] | 172 | return icount_get_raw(); |
Pavel Dovgalyuk | 26bc60a | 2015-09-17 19:23:54 +0300 | [diff] [blame] | 173 | } |
Pavel Dovgalyuk | 8b42704 | 2015-09-17 19:24:05 +0300 | [diff] [blame] | 174 | |
| 175 | int replay_get_instructions(void) |
| 176 | { |
| 177 | int res = 0; |
Jamie Iles | 83ecdb1 | 2023-04-27 03:09:25 +0100 | [diff] [blame] | 178 | g_assert(replay_mutex_locked()); |
Pavel Dovgalyuk | 8b42704 | 2015-09-17 19:24:05 +0300 | [diff] [blame] | 179 | if (replay_next_event_is(EVENT_INSTRUCTION)) { |
Pavel Dovgalyuk | 13f2671 | 2019-07-25 11:44:43 +0300 | [diff] [blame] | 180 | res = replay_state.instruction_count; |
Pavel Dovgalyuk | e751067 | 2020-10-03 20:13:26 +0300 | [diff] [blame] | 181 | if (replay_break_icount != -1LL) { |
| 182 | uint64_t current = replay_get_current_icount(); |
| 183 | assert(replay_break_icount >= current); |
| 184 | if (current + res > replay_break_icount) { |
| 185 | res = replay_break_icount - current; |
| 186 | } |
| 187 | } |
Pavel Dovgalyuk | 8b42704 | 2015-09-17 19:24:05 +0300 | [diff] [blame] | 188 | } |
Pavel Dovgalyuk | 8b42704 | 2015-09-17 19:24:05 +0300 | [diff] [blame] | 189 | return res; |
| 190 | } |
| 191 | |
| 192 | void replay_account_executed_instructions(void) |
| 193 | { |
| 194 | if (replay_mode == REPLAY_MODE_PLAY) { |
Alex Bennée | d759c95 | 2018-02-27 12:52:48 +0300 | [diff] [blame] | 195 | g_assert(replay_mutex_locked()); |
Pavel Dovgalyuk | 13f2671 | 2019-07-25 11:44:43 +0300 | [diff] [blame] | 196 | if (replay_state.instruction_count > 0) { |
Pavel Dovgalyuk | 366a85e | 2021-02-16 15:51:44 +0300 | [diff] [blame] | 197 | replay_advance_current_icount(replay_get_current_icount()); |
Pavel Dovgalyuk | 8b42704 | 2015-09-17 19:24:05 +0300 | [diff] [blame] | 198 | } |
Pavel Dovgalyuk | 8b42704 | 2015-09-17 19:24:05 +0300 | [diff] [blame] | 199 | } |
| 200 | } |
Pavel Dovgalyuk | 6f06096 | 2015-09-17 19:24:16 +0300 | [diff] [blame] | 201 | |
| 202 | bool replay_exception(void) |
| 203 | { |
Alex Bennée | d759c95 | 2018-02-27 12:52:48 +0300 | [diff] [blame] | 204 | |
Pavel Dovgalyuk | 6f06096 | 2015-09-17 19:24:16 +0300 | [diff] [blame] | 205 | if (replay_mode == REPLAY_MODE_RECORD) { |
Alex Bennée | d759c95 | 2018-02-27 12:52:48 +0300 | [diff] [blame] | 206 | g_assert(replay_mutex_locked()); |
Pavel Dovgalyuk | 6f06096 | 2015-09-17 19:24:16 +0300 | [diff] [blame] | 207 | replay_save_instructions(); |
Pavel Dovgalyuk | 6f06096 | 2015-09-17 19:24:16 +0300 | [diff] [blame] | 208 | replay_put_event(EVENT_EXCEPTION); |
Pavel Dovgalyuk | 6f06096 | 2015-09-17 19:24:16 +0300 | [diff] [blame] | 209 | return true; |
| 210 | } else if (replay_mode == REPLAY_MODE_PLAY) { |
Alex Bennée | d759c95 | 2018-02-27 12:52:48 +0300 | [diff] [blame] | 211 | g_assert(replay_mutex_locked()); |
Pavel Dovgalyuk | 6f06096 | 2015-09-17 19:24:16 +0300 | [diff] [blame] | 212 | bool res = replay_has_exception(); |
| 213 | if (res) { |
Pavel Dovgalyuk | 6f06096 | 2015-09-17 19:24:16 +0300 | [diff] [blame] | 214 | replay_finish_event(); |
Pavel Dovgalyuk | 6f06096 | 2015-09-17 19:24:16 +0300 | [diff] [blame] | 215 | } |
| 216 | return res; |
| 217 | } |
| 218 | |
| 219 | return true; |
| 220 | } |
| 221 | |
| 222 | bool replay_has_exception(void) |
| 223 | { |
| 224 | bool res = false; |
| 225 | if (replay_mode == REPLAY_MODE_PLAY) { |
Alex Bennée | d759c95 | 2018-02-27 12:52:48 +0300 | [diff] [blame] | 226 | g_assert(replay_mutex_locked()); |
Pavel Dovgalyuk | 6f06096 | 2015-09-17 19:24:16 +0300 | [diff] [blame] | 227 | replay_account_executed_instructions(); |
Pavel Dovgalyuk | 6f06096 | 2015-09-17 19:24:16 +0300 | [diff] [blame] | 228 | res = replay_next_event_is(EVENT_EXCEPTION); |
Pavel Dovgalyuk | 6f06096 | 2015-09-17 19:24:16 +0300 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | return res; |
| 232 | } |
| 233 | |
| 234 | bool replay_interrupt(void) |
| 235 | { |
| 236 | if (replay_mode == REPLAY_MODE_RECORD) { |
Alex Bennée | d759c95 | 2018-02-27 12:52:48 +0300 | [diff] [blame] | 237 | g_assert(replay_mutex_locked()); |
Pavel Dovgalyuk | 6f06096 | 2015-09-17 19:24:16 +0300 | [diff] [blame] | 238 | replay_save_instructions(); |
Pavel Dovgalyuk | 6f06096 | 2015-09-17 19:24:16 +0300 | [diff] [blame] | 239 | replay_put_event(EVENT_INTERRUPT); |
Pavel Dovgalyuk | 6f06096 | 2015-09-17 19:24:16 +0300 | [diff] [blame] | 240 | return true; |
| 241 | } else if (replay_mode == REPLAY_MODE_PLAY) { |
Alex Bennée | d759c95 | 2018-02-27 12:52:48 +0300 | [diff] [blame] | 242 | g_assert(replay_mutex_locked()); |
Pavel Dovgalyuk | 6f06096 | 2015-09-17 19:24:16 +0300 | [diff] [blame] | 243 | bool res = replay_has_interrupt(); |
| 244 | if (res) { |
Pavel Dovgalyuk | 6f06096 | 2015-09-17 19:24:16 +0300 | [diff] [blame] | 245 | replay_finish_event(); |
Pavel Dovgalyuk | 6f06096 | 2015-09-17 19:24:16 +0300 | [diff] [blame] | 246 | } |
| 247 | return res; |
| 248 | } |
| 249 | |
| 250 | return true; |
| 251 | } |
| 252 | |
| 253 | bool replay_has_interrupt(void) |
| 254 | { |
| 255 | bool res = false; |
| 256 | if (replay_mode == REPLAY_MODE_PLAY) { |
Alex Bennée | d759c95 | 2018-02-27 12:52:48 +0300 | [diff] [blame] | 257 | g_assert(replay_mutex_locked()); |
Pavel Dovgalyuk | 6f06096 | 2015-09-17 19:24:16 +0300 | [diff] [blame] | 258 | replay_account_executed_instructions(); |
Pavel Dovgalyuk | 6f06096 | 2015-09-17 19:24:16 +0300 | [diff] [blame] | 259 | res = replay_next_event_is(EVENT_INTERRUPT); |
Pavel Dovgalyuk | 6f06096 | 2015-09-17 19:24:16 +0300 | [diff] [blame] | 260 | } |
| 261 | return res; |
| 262 | } |
Pavel Dovgalyuk | b60c48a | 2015-09-17 19:24:33 +0300 | [diff] [blame] | 263 | |
Eric Blake | 802f045 | 2017-05-15 16:41:12 -0500 | [diff] [blame] | 264 | void replay_shutdown_request(ShutdownCause cause) |
Pavel Dovgalyuk | b60c48a | 2015-09-17 19:24:33 +0300 | [diff] [blame] | 265 | { |
| 266 | if (replay_mode == REPLAY_MODE_RECORD) { |
Alex Bennée | d759c95 | 2018-02-27 12:52:48 +0300 | [diff] [blame] | 267 | g_assert(replay_mutex_locked()); |
Eric Blake | 802f045 | 2017-05-15 16:41:12 -0500 | [diff] [blame] | 268 | replay_put_event(EVENT_SHUTDOWN + cause); |
Pavel Dovgalyuk | b60c48a | 2015-09-17 19:24:33 +0300 | [diff] [blame] | 269 | } |
| 270 | } |
Pavel Dovgalyuk | 8bd7f71 | 2015-09-17 19:24:44 +0300 | [diff] [blame] | 271 | |
| 272 | bool replay_checkpoint(ReplayCheckpoint checkpoint) |
| 273 | { |
Pavel Dovgalyuk | 8bd7f71 | 2015-09-17 19:24:44 +0300 | [diff] [blame] | 274 | assert(EVENT_CHECKPOINT + checkpoint <= EVENT_CHECKPOINT_LAST); |
Pavel Dovgalyuk | 8bd7f71 | 2015-09-17 19:24:44 +0300 | [diff] [blame] | 275 | |
Pavel Dovgalyuk | 66eb782 | 2018-02-27 12:53:05 +0300 | [diff] [blame] | 276 | replay_save_instructions(); |
Pavel Dovgalyuk | 8bd7f71 | 2015-09-17 19:24:44 +0300 | [diff] [blame] | 277 | |
| 278 | if (replay_mode == REPLAY_MODE_PLAY) { |
Alex Bennée | d759c95 | 2018-02-27 12:52:48 +0300 | [diff] [blame] | 279 | g_assert(replay_mutex_locked()); |
Pavel Dovgalyuk | 8bd7f71 | 2015-09-17 19:24:44 +0300 | [diff] [blame] | 280 | if (replay_next_event_is(EVENT_CHECKPOINT + checkpoint)) { |
| 281 | replay_finish_event(); |
Pavel Dovgalyuk | 60618e2 | 2022-05-27 13:46:18 +0300 | [diff] [blame] | 282 | } else { |
| 283 | return false; |
Pavel Dovgalyuk | 8bd7f71 | 2015-09-17 19:24:44 +0300 | [diff] [blame] | 284 | } |
Pavel Dovgalyuk | 8bd7f71 | 2015-09-17 19:24:44 +0300 | [diff] [blame] | 285 | } else if (replay_mode == REPLAY_MODE_RECORD) { |
Alex Bennée | d759c95 | 2018-02-27 12:52:48 +0300 | [diff] [blame] | 286 | g_assert(replay_mutex_locked()); |
Pavel Dovgalyuk | 8bd7f71 | 2015-09-17 19:24:44 +0300 | [diff] [blame] | 287 | replay_put_event(EVENT_CHECKPOINT + checkpoint); |
Pavel Dovgalyuk | 8bd7f71 | 2015-09-17 19:24:44 +0300 | [diff] [blame] | 288 | } |
Pavel Dovgalyuk | 60618e2 | 2022-05-27 13:46:18 +0300 | [diff] [blame] | 289 | return true; |
Pavel Dovgalyuk | 8bd7f71 | 2015-09-17 19:24:44 +0300 | [diff] [blame] | 290 | } |
Pavel Dovgalyuk | 7615936 | 2015-09-17 19:25:07 +0300 | [diff] [blame] | 291 | |
Pavel Dovgalyuk | 60618e2 | 2022-05-27 13:46:18 +0300 | [diff] [blame] | 292 | void replay_async_events(void) |
| 293 | { |
| 294 | static bool processing = false; |
| 295 | /* |
| 296 | * If we are already processing the events, recursion may occur |
| 297 | * in case of incorrect implementation when HW event modifies timers. |
| 298 | * Timer modification may invoke the icount warp, event processing, |
| 299 | * and cause the recursion. |
| 300 | */ |
| 301 | g_assert(!processing); |
| 302 | processing = true; |
| 303 | |
| 304 | replay_save_instructions(); |
| 305 | |
| 306 | if (replay_mode == REPLAY_MODE_PLAY) { |
| 307 | g_assert(replay_mutex_locked()); |
| 308 | replay_read_events(); |
| 309 | } else if (replay_mode == REPLAY_MODE_RECORD) { |
| 310 | g_assert(replay_mutex_locked()); |
| 311 | replay_save_events(); |
| 312 | } |
| 313 | processing = false; |
| 314 | } |
| 315 | |
| 316 | bool replay_has_event(void) |
Pavel Dovgalyuk | 0c08185 | 2018-09-12 11:19:45 +0300 | [diff] [blame] | 317 | { |
| 318 | bool res = false; |
| 319 | if (replay_mode == REPLAY_MODE_PLAY) { |
| 320 | g_assert(replay_mutex_locked()); |
| 321 | replay_account_executed_instructions(); |
| 322 | res = EVENT_CHECKPOINT <= replay_state.data_kind |
| 323 | && replay_state.data_kind <= EVENT_CHECKPOINT_LAST; |
Pavel Dovgalyuk | 3e21408 | 2022-05-27 13:46:23 +0300 | [diff] [blame] | 324 | res = res || (EVENT_ASYNC <= replay_state.data_kind |
| 325 | && replay_state.data_kind <= EVENT_ASYNC_LAST); |
Pavel Dovgalyuk | 0c08185 | 2018-09-12 11:19:45 +0300 | [diff] [blame] | 326 | } |
| 327 | return res; |
| 328 | } |
| 329 | |
Alex Bennée | dcda732 | 2023-12-11 09:13:38 +0000 | [diff] [blame] | 330 | G_NORETURN void replay_sync_error(const char *error) |
| 331 | { |
| 332 | error_report("%s (insn total %"PRId64"/%d left, event %d is %s)", error, |
| 333 | replay_state.current_icount, replay_state.instruction_count, |
| 334 | replay_state.current_event, |
| 335 | replay_event_name(replay_state.data_kind)); |
| 336 | abort(); |
| 337 | } |
| 338 | |
Pavel Dovgalyuk | 7615936 | 2015-09-17 19:25:07 +0300 | [diff] [blame] | 339 | static void replay_enable(const char *fname, int mode) |
| 340 | { |
| 341 | const char *fmode = NULL; |
| 342 | assert(!replay_file); |
| 343 | |
| 344 | switch (mode) { |
| 345 | case REPLAY_MODE_RECORD: |
| 346 | fmode = "wb"; |
| 347 | break; |
| 348 | case REPLAY_MODE_PLAY: |
| 349 | fmode = "rb"; |
| 350 | break; |
| 351 | default: |
| 352 | fprintf(stderr, "Replay: internal error: invalid replay mode\n"); |
| 353 | exit(1); |
| 354 | } |
| 355 | |
| 356 | atexit(replay_finish); |
| 357 | |
Pavel Dovgalyuk | 7615936 | 2015-09-17 19:25:07 +0300 | [diff] [blame] | 358 | replay_file = fopen(fname, fmode); |
| 359 | if (replay_file == NULL) { |
| 360 | fprintf(stderr, "Replay: open %s: %s\n", fname, strerror(errno)); |
| 361 | exit(1); |
| 362 | } |
| 363 | |
| 364 | replay_filename = g_strdup(fname); |
Pavel Dovgalyuk | 7615936 | 2015-09-17 19:25:07 +0300 | [diff] [blame] | 365 | replay_mode = mode; |
Alex Bennée | d759c95 | 2018-02-27 12:52:48 +0300 | [diff] [blame] | 366 | replay_mutex_init(); |
| 367 | |
Pavel Dovgalyuk | f186d64 | 2016-09-26 11:08:04 +0300 | [diff] [blame] | 368 | replay_state.data_kind = -1; |
Pavel Dovgalyuk | 13f2671 | 2019-07-25 11:44:43 +0300 | [diff] [blame] | 369 | replay_state.instruction_count = 0; |
| 370 | replay_state.current_icount = 0; |
Alex Bennée | dcda732 | 2023-12-11 09:13:38 +0000 | [diff] [blame] | 371 | replay_state.current_event = 0; |
Pavel Dovgalyuk | f186d64 | 2016-09-26 11:08:04 +0300 | [diff] [blame] | 372 | replay_state.has_unread_data = 0; |
Pavel Dovgalyuk | 7615936 | 2015-09-17 19:25:07 +0300 | [diff] [blame] | 373 | |
| 374 | /* skip file header for RECORD and check it for PLAY */ |
| 375 | if (replay_mode == REPLAY_MODE_RECORD) { |
| 376 | fseek(replay_file, HEADER_SIZE, SEEK_SET); |
| 377 | } else if (replay_mode == REPLAY_MODE_PLAY) { |
| 378 | unsigned int version = replay_get_dword(); |
| 379 | if (version != REPLAY_VERSION) { |
| 380 | fprintf(stderr, "Replay: invalid input log file version\n"); |
| 381 | exit(1); |
| 382 | } |
| 383 | /* go to the beginning */ |
| 384 | fseek(replay_file, HEADER_SIZE, SEEK_SET); |
| 385 | replay_fetch_data_kind(); |
| 386 | } |
| 387 | |
Nicholas Piggin | 9dbab31 | 2024-08-13 21:23:20 +0100 | [diff] [blame] | 388 | runstate_replay_enable(); |
| 389 | |
Pavel Dovgalyuk | 7615936 | 2015-09-17 19:25:07 +0300 | [diff] [blame] | 390 | replay_init_events(); |
| 391 | } |
| 392 | |
| 393 | void replay_configure(QemuOpts *opts) |
| 394 | { |
| 395 | const char *fname; |
| 396 | const char *rr; |
| 397 | ReplayMode mode = REPLAY_MODE_NONE; |
Eduardo Habkost | 890ad55 | 2016-02-12 17:02:26 -0200 | [diff] [blame] | 398 | Location loc; |
| 399 | |
| 400 | if (!opts) { |
| 401 | return; |
| 402 | } |
| 403 | |
| 404 | loc_push_none(&loc); |
| 405 | qemu_opts_loc_restore(opts); |
Pavel Dovgalyuk | 7615936 | 2015-09-17 19:25:07 +0300 | [diff] [blame] | 406 | |
| 407 | rr = qemu_opt_get(opts, "rr"); |
| 408 | if (!rr) { |
| 409 | /* Just enabling icount */ |
Markus Armbruster | d9d3aae | 2016-04-27 16:29:08 +0200 | [diff] [blame] | 410 | goto out; |
Pavel Dovgalyuk | 7615936 | 2015-09-17 19:25:07 +0300 | [diff] [blame] | 411 | } else if (!strcmp(rr, "record")) { |
| 412 | mode = REPLAY_MODE_RECORD; |
| 413 | } else if (!strcmp(rr, "replay")) { |
| 414 | mode = REPLAY_MODE_PLAY; |
| 415 | } else { |
| 416 | error_report("Invalid icount rr option: %s", rr); |
| 417 | exit(1); |
| 418 | } |
| 419 | |
| 420 | fname = qemu_opt_get(opts, "rrfile"); |
| 421 | if (!fname) { |
| 422 | error_report("File name not specified for replay"); |
| 423 | exit(1); |
| 424 | } |
| 425 | |
Pavel Dovgalyuk | 9c2037d | 2017-01-24 10:17:47 +0300 | [diff] [blame] | 426 | replay_snapshot = g_strdup(qemu_opt_get(opts, "rrsnapshot")); |
Pavel Dovgalyuk | 306e196 | 2016-09-26 11:08:10 +0300 | [diff] [blame] | 427 | replay_vmstate_register(); |
Pavel Dovgalyuk | 7615936 | 2015-09-17 19:25:07 +0300 | [diff] [blame] | 428 | replay_enable(fname, mode); |
Eduardo Habkost | 890ad55 | 2016-02-12 17:02:26 -0200 | [diff] [blame] | 429 | |
Markus Armbruster | d9d3aae | 2016-04-27 16:29:08 +0200 | [diff] [blame] | 430 | out: |
Eduardo Habkost | 890ad55 | 2016-02-12 17:02:26 -0200 | [diff] [blame] | 431 | loc_pop(&loc); |
Pavel Dovgalyuk | 7615936 | 2015-09-17 19:25:07 +0300 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | void replay_start(void) |
| 435 | { |
| 436 | if (replay_mode == REPLAY_MODE_NONE) { |
| 437 | return; |
| 438 | } |
| 439 | |
Pavel Dovgalyuk | 0194749 | 2015-09-17 19:25:13 +0300 | [diff] [blame] | 440 | if (replay_blockers) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 441 | error_reportf_err(replay_blockers->data, "Record/replay: "); |
Pavel Dovgalyuk | 0194749 | 2015-09-17 19:25:13 +0300 | [diff] [blame] | 442 | exit(1); |
| 443 | } |
Claudio Fontana | 740b175 | 2020-08-19 13:17:19 +0200 | [diff] [blame] | 444 | if (!icount_enabled()) { |
Pavel Dovgalyuk | 4c27b85 | 2015-09-17 19:25:18 +0300 | [diff] [blame] | 445 | error_report("Please enable icount to use record/replay"); |
| 446 | exit(1); |
| 447 | } |
Pavel Dovgalyuk | 0194749 | 2015-09-17 19:25:13 +0300 | [diff] [blame] | 448 | |
Pavel Dovgalyuk | 7615936 | 2015-09-17 19:25:07 +0300 | [diff] [blame] | 449 | /* Timer for snapshotting will be set up here. */ |
| 450 | |
| 451 | replay_enable_events(); |
| 452 | } |
| 453 | |
| 454 | void replay_finish(void) |
| 455 | { |
| 456 | if (replay_mode == REPLAY_MODE_NONE) { |
| 457 | return; |
| 458 | } |
| 459 | |
| 460 | replay_save_instructions(); |
| 461 | |
| 462 | /* finalize the file */ |
| 463 | if (replay_file) { |
| 464 | if (replay_mode == REPLAY_MODE_RECORD) { |
Pavel Dovgalyuk | ed5d7ff | 2020-05-22 09:45:54 +0300 | [diff] [blame] | 465 | /* |
| 466 | * Can't do it in the signal handler, therefore |
| 467 | * add shutdown event here for the case of Ctrl-C. |
| 468 | */ |
| 469 | replay_shutdown_request(SHUTDOWN_CAUSE_HOST_SIGNAL); |
Pavel Dovgalyuk | 7615936 | 2015-09-17 19:25:07 +0300 | [diff] [blame] | 470 | /* write end event */ |
| 471 | replay_put_event(EVENT_END); |
| 472 | |
| 473 | /* write header */ |
| 474 | fseek(replay_file, 0, SEEK_SET); |
| 475 | replay_put_dword(REPLAY_VERSION); |
| 476 | } |
| 477 | |
| 478 | fclose(replay_file); |
| 479 | replay_file = NULL; |
| 480 | } |
Markus Armbruster | 76eb88b | 2022-09-23 11:04:28 +0200 | [diff] [blame] | 481 | g_free(replay_filename); |
| 482 | replay_filename = NULL; |
Pavel Dovgalyuk | 7615936 | 2015-09-17 19:25:07 +0300 | [diff] [blame] | 483 | |
Pavel Dovgalyuk | 9c2037d | 2017-01-24 10:17:47 +0300 | [diff] [blame] | 484 | g_free(replay_snapshot); |
| 485 | replay_snapshot = NULL; |
| 486 | |
Pavel Dovgalyuk | 7615936 | 2015-09-17 19:25:07 +0300 | [diff] [blame] | 487 | replay_finish_events(); |
Pavel Dovgalyuk | c4b8ffc | 2022-05-27 13:46:07 +0300 | [diff] [blame] | 488 | replay_mode = REPLAY_MODE_NONE; |
Pavel Dovgalyuk | 7615936 | 2015-09-17 19:25:07 +0300 | [diff] [blame] | 489 | } |
Pavel Dovgalyuk | 0194749 | 2015-09-17 19:25:13 +0300 | [diff] [blame] | 490 | |
Markus Armbruster | 0ec8384 | 2023-02-07 08:51:12 +0100 | [diff] [blame] | 491 | void replay_add_blocker(const char *feature) |
Pavel Dovgalyuk | 0194749 | 2015-09-17 19:25:13 +0300 | [diff] [blame] | 492 | { |
Markus Armbruster | 0ec8384 | 2023-02-07 08:51:12 +0100 | [diff] [blame] | 493 | Error *reason = NULL; |
| 494 | |
Markus Armbruster | 7653b1e | 2024-03-01 13:06:41 +0100 | [diff] [blame] | 495 | error_setg(&reason, "Record/replay is not supported with %s", |
Markus Armbruster | 0ec8384 | 2023-02-07 08:51:12 +0100 | [diff] [blame] | 496 | feature); |
Pavel Dovgalyuk | 0194749 | 2015-09-17 19:25:13 +0300 | [diff] [blame] | 497 | replay_blockers = g_slist_prepend(replay_blockers, reason); |
| 498 | } |
Pavel Dovgalyuk | 56db119 | 2020-10-03 20:12:57 +0300 | [diff] [blame] | 499 | |
| 500 | const char *replay_get_filename(void) |
| 501 | { |
| 502 | return replay_filename; |
| 503 | } |