]> git.proxmox.com Git - mirror_qemu.git/blame - replay/replay.c
Merge tag 'pull-aspeed-20240201' of https://github.com/legoater/qemu into staging
[mirror_qemu.git] / replay / replay.c
CommitLineData
d73abd6d
PD
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
d38ea87a 12#include "qemu/osdep.h"
da34e65c 13#include "qapi/error.h"
740b1759 14#include "sysemu/cpu-timers.h"
d73abd6d 15#include "sysemu/replay.h"
54d31236 16#include "sysemu/runstate.h"
26bc60ac 17#include "replay-internal.h"
8b427044 18#include "qemu/main-loop.h"
922a01a0 19#include "qemu/option.h"
d2528bdc 20#include "sysemu/cpus.h"
7615936e
PD
21#include "qemu/error-report.h"
22
23/* Current version of the replay mechanism.
24 Increase it when file format changes. */
3e21408b 25#define REPLAY_VERSION 0xe0200c
7615936e
PD
26/* Size of replay log header */
27#define HEADER_SIZE (sizeof(uint32_t) + sizeof(uint64_t))
d73abd6d
PD
28
29ReplayMode replay_mode = REPLAY_MODE_NONE;
9c2037d0 30char *replay_snapshot;
26bc60ac 31
7615936e
PD
32/* Name of replay file */
33static char *replay_filename;
26bc60ac 34ReplayState replay_state;
0194749a 35static GSList *replay_blockers;
26bc60ac 36
e7510671
PD
37/* Replay breakpoints */
38uint64_t replay_break_icount = -1ULL;
39QEMUTimer *replay_break_timer;
40
dcda7321
AB
41/* Pretty print event names */
42
43static 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
60static 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 */
73static 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
94static 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
113static 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
26bc60ac
PD
142bool replay_next_event_is(int event)
143{
144 bool res = false;
145
146 /* nothing to skip - not all instructions used */
13f26713 147 if (replay_state.instruction_count != 0) {
f186d64d 148 assert(replay_state.data_kind == EVENT_INSTRUCTION);
26bc60ac
PD
149 return event == EVENT_INSTRUCTION;
150 }
151
152 while (true) {
e957ad8a
PD
153 unsigned int data_kind = replay_state.data_kind;
154 if (event == data_kind) {
26bc60ac
PD
155 res = true;
156 }
e957ad8a 157 switch (data_kind) {
802f045a 158 case EVENT_SHUTDOWN ... EVENT_SHUTDOWN_LAST:
b60c48a7 159 replay_finish_event();
e957ad8a 160 qemu_system_shutdown_request(data_kind - EVENT_SHUTDOWN);
b60c48a7 161 break;
26bc60ac
PD
162 default:
163 /* clock, time_t, checkpoint and other events */
164 return res;
165 }
166 }
167 return res;
168}
169
13f26713 170uint64_t replay_get_current_icount(void)
26bc60ac 171{
8191d368 172 return icount_get_raw();
26bc60ac 173}
8b427044
PD
174
175int replay_get_instructions(void)
176{
177 int res = 0;
83ecdb18 178 g_assert(replay_mutex_locked());
8b427044 179 if (replay_next_event_is(EVENT_INSTRUCTION)) {
13f26713 180 res = replay_state.instruction_count;
e7510671
PD
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 }
8b427044 188 }
8b427044
PD
189 return res;
190}
191
192void replay_account_executed_instructions(void)
193{
194 if (replay_mode == REPLAY_MODE_PLAY) {
d759c951 195 g_assert(replay_mutex_locked());
13f26713 196 if (replay_state.instruction_count > 0) {
366a85e4 197 replay_advance_current_icount(replay_get_current_icount());
8b427044 198 }
8b427044
PD
199 }
200}
6f060969
PD
201
202bool replay_exception(void)
203{
d759c951 204
6f060969 205 if (replay_mode == REPLAY_MODE_RECORD) {
d759c951 206 g_assert(replay_mutex_locked());
6f060969 207 replay_save_instructions();
6f060969 208 replay_put_event(EVENT_EXCEPTION);
6f060969
PD
209 return true;
210 } else if (replay_mode == REPLAY_MODE_PLAY) {
d759c951 211 g_assert(replay_mutex_locked());
6f060969
PD
212 bool res = replay_has_exception();
213 if (res) {
6f060969 214 replay_finish_event();
6f060969
PD
215 }
216 return res;
217 }
218
219 return true;
220}
221
222bool replay_has_exception(void)
223{
224 bool res = false;
225 if (replay_mode == REPLAY_MODE_PLAY) {
d759c951 226 g_assert(replay_mutex_locked());
6f060969 227 replay_account_executed_instructions();
6f060969 228 res = replay_next_event_is(EVENT_EXCEPTION);
6f060969
PD
229 }
230
231 return res;
232}
233
234bool replay_interrupt(void)
235{
236 if (replay_mode == REPLAY_MODE_RECORD) {
d759c951 237 g_assert(replay_mutex_locked());
6f060969 238 replay_save_instructions();
6f060969 239 replay_put_event(EVENT_INTERRUPT);
6f060969
PD
240 return true;
241 } else if (replay_mode == REPLAY_MODE_PLAY) {
d759c951 242 g_assert(replay_mutex_locked());
6f060969
PD
243 bool res = replay_has_interrupt();
244 if (res) {
6f060969 245 replay_finish_event();
6f060969
PD
246 }
247 return res;
248 }
249
250 return true;
251}
252
253bool replay_has_interrupt(void)
254{
255 bool res = false;
256 if (replay_mode == REPLAY_MODE_PLAY) {
d759c951 257 g_assert(replay_mutex_locked());
6f060969 258 replay_account_executed_instructions();
6f060969 259 res = replay_next_event_is(EVENT_INTERRUPT);
6f060969
PD
260 }
261 return res;
262}
b60c48a7 263
802f045a 264void replay_shutdown_request(ShutdownCause cause)
b60c48a7
PD
265{
266 if (replay_mode == REPLAY_MODE_RECORD) {
d759c951 267 g_assert(replay_mutex_locked());
802f045a 268 replay_put_event(EVENT_SHUTDOWN + cause);
b60c48a7
PD
269 }
270}
8bd7f71d
PD
271
272bool replay_checkpoint(ReplayCheckpoint checkpoint)
273{
8bd7f71d 274 assert(EVENT_CHECKPOINT + checkpoint <= EVENT_CHECKPOINT_LAST);
8bd7f71d 275
66eb7825 276 replay_save_instructions();
8bd7f71d
PD
277
278 if (replay_mode == REPLAY_MODE_PLAY) {
d759c951 279 g_assert(replay_mutex_locked());
8bd7f71d
PD
280 if (replay_next_event_is(EVENT_CHECKPOINT + checkpoint)) {
281 replay_finish_event();
60618e2d
PD
282 } else {
283 return false;
8bd7f71d 284 }
8bd7f71d 285 } else if (replay_mode == REPLAY_MODE_RECORD) {
d759c951 286 g_assert(replay_mutex_locked());
8bd7f71d 287 replay_put_event(EVENT_CHECKPOINT + checkpoint);
8bd7f71d 288 }
60618e2d
PD
289 return true;
290}
291
292void 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;
8bd7f71d 314}
7615936e 315
60618e2d 316bool replay_has_event(void)
0c08185f
PD
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;
3e21408b
PD
324 res = res || (EVENT_ASYNC <= replay_state.data_kind
325 && replay_state.data_kind <= EVENT_ASYNC_LAST);
0c08185f
PD
326 }
327 return res;
328}
329
dcda7321
AB
330G_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
7615936e
PD
339static 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
7615936e
PD
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);
7615936e 365 replay_mode = mode;
d759c951
AB
366 replay_mutex_init();
367
f186d64d 368 replay_state.data_kind = -1;
13f26713
PD
369 replay_state.instruction_count = 0;
370 replay_state.current_icount = 0;
dcda7321 371 replay_state.current_event = 0;
f186d64d 372 replay_state.has_unread_data = 0;
7615936e
PD
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
388 replay_init_events();
389}
390
391void replay_configure(QemuOpts *opts)
392{
393 const char *fname;
394 const char *rr;
395 ReplayMode mode = REPLAY_MODE_NONE;
890ad550
EH
396 Location loc;
397
398 if (!opts) {
399 return;
400 }
401
402 loc_push_none(&loc);
403 qemu_opts_loc_restore(opts);
7615936e
PD
404
405 rr = qemu_opt_get(opts, "rr");
406 if (!rr) {
407 /* Just enabling icount */
d9d3aaea 408 goto out;
7615936e
PD
409 } else if (!strcmp(rr, "record")) {
410 mode = REPLAY_MODE_RECORD;
411 } else if (!strcmp(rr, "replay")) {
412 mode = REPLAY_MODE_PLAY;
413 } else {
414 error_report("Invalid icount rr option: %s", rr);
415 exit(1);
416 }
417
418 fname = qemu_opt_get(opts, "rrfile");
419 if (!fname) {
420 error_report("File name not specified for replay");
421 exit(1);
422 }
423
9c2037d0 424 replay_snapshot = g_strdup(qemu_opt_get(opts, "rrsnapshot"));
306e196f 425 replay_vmstate_register();
7615936e 426 replay_enable(fname, mode);
890ad550 427
d9d3aaea 428out:
890ad550 429 loc_pop(&loc);
7615936e
PD
430}
431
432void replay_start(void)
433{
434 if (replay_mode == REPLAY_MODE_NONE) {
435 return;
436 }
437
0194749a 438 if (replay_blockers) {
c29b77f9 439 error_reportf_err(replay_blockers->data, "Record/replay: ");
0194749a
PD
440 exit(1);
441 }
740b1759 442 if (!icount_enabled()) {
4c27b859
PD
443 error_report("Please enable icount to use record/replay");
444 exit(1);
445 }
0194749a 446
7615936e
PD
447 /* Timer for snapshotting will be set up here. */
448
449 replay_enable_events();
450}
451
1f881ea4
AB
452/*
453 * For none/record the answer is yes.
454 */
455bool replay_can_wait(void)
456{
457 if (replay_mode == REPLAY_MODE_PLAY) {
458 /*
459 * For playback we shouldn't ever be at a point we wait. If
460 * the instruction count has reached zero and we have an
461 * unconsumed event we should go around again and consume it.
462 */
463 if (replay_state.instruction_count == 0 && replay_state.has_unread_data) {
464 return false;
465 } else {
466 replay_sync_error("Playback shouldn't have to iowait");
467 }
468 }
469 return true;
470}
471
472
7615936e
PD
473void replay_finish(void)
474{
475 if (replay_mode == REPLAY_MODE_NONE) {
476 return;
477 }
478
479 replay_save_instructions();
480
481 /* finalize the file */
482 if (replay_file) {
483 if (replay_mode == REPLAY_MODE_RECORD) {
ed5d7ff3
PD
484 /*
485 * Can't do it in the signal handler, therefore
486 * add shutdown event here for the case of Ctrl-C.
487 */
488 replay_shutdown_request(SHUTDOWN_CAUSE_HOST_SIGNAL);
7615936e
PD
489 /* write end event */
490 replay_put_event(EVENT_END);
491
492 /* write header */
493 fseek(replay_file, 0, SEEK_SET);
494 replay_put_dword(REPLAY_VERSION);
495 }
496
497 fclose(replay_file);
498 replay_file = NULL;
499 }
76eb88b1
MA
500 g_free(replay_filename);
501 replay_filename = NULL;
7615936e 502
9c2037d0
PD
503 g_free(replay_snapshot);
504 replay_snapshot = NULL;
505
7615936e 506 replay_finish_events();
c4b8ffcb 507 replay_mode = REPLAY_MODE_NONE;
7615936e 508}
0194749a 509
0ec8384f 510void replay_add_blocker(const char *feature)
0194749a 511{
0ec8384f
MA
512 Error *reason = NULL;
513
514 error_setg(&reason, "Record/replay feature is not supported for '%s'",
515 feature);
0194749a
PD
516 replay_blockers = g_slist_prepend(replay_blockers, reason);
517}
56db1198
PD
518
519const char *replay_get_filename(void)
520{
521 return replay_filename;
522}