blob: ccb4d89dda748b99107c8730b5e855e2371ff6af [file] [log] [blame]
Pavel Dovgalyuk306e1962016-09-26 11:08:10 +03001/*
2 * replay-snapshot.c
3 *
4 * Copyright (c) 2010-2016 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
12#include "qemu/osdep.h"
13#include "qapi/error.h"
Pavel Dovgalyuk306e1962016-09-26 11:08:10 +030014#include "sysemu/replay.h"
15#include "replay-internal.h"
Pavel Dovgalyuk306e1962016-09-26 11:08:10 +030016#include "monitor/monitor.h"
17#include "qapi/qmp/qstring.h"
18#include "qemu/error-report.h"
19#include "migration/vmstate.h"
Juan Quintela5e224792017-04-20 14:25:55 +020020#include "migration/snapshot.h"
Pavel Dovgalyuk306e1962016-09-26 11:08:10 +030021
Dr. David Alan Gilbert44b1ff32017-09-25 12:29:12 +010022static int replay_pre_save(void *opaque)
Pavel Dovgalyuk306e1962016-09-26 11:08:10 +030023{
24 ReplayState *state = opaque;
25 state->file_offset = ftell(replay_file);
Dr. David Alan Gilbert44b1ff32017-09-25 12:29:12 +010026
27 return 0;
Pavel Dovgalyuk306e1962016-09-26 11:08:10 +030028}
29
30static int replay_post_load(void *opaque, int version_id)
31{
32 ReplayState *state = opaque;
Pavel Dovgalyukbb3d7702018-09-12 11:19:39 +030033 if (replay_mode == REPLAY_MODE_PLAY) {
34 fseek(replay_file, state->file_offset, SEEK_SET);
Pavel Dovgalyukbb3d7702018-09-12 11:19:39 +030035 /* If this was a vmstate, saved in recording mode,
36 we need to initialize replay data fields. */
37 replay_fetch_data_kind();
38 } else if (replay_mode == REPLAY_MODE_RECORD) {
39 /* This is only useful for loading the initial state.
40 Therefore reset all the counters. */
Pavel Dovgalyuk13f26712019-07-25 11:44:43 +030041 state->instruction_count = 0;
Pavel Dovgalyukbb3d7702018-09-12 11:19:39 +030042 state->block_request_id = 0;
43 }
Pavel Dovgalyuk306e1962016-09-26 11:08:10 +030044
45 return 0;
46}
47
48static const VMStateDescription vmstate_replay = {
49 .name = "replay",
Alex Bennée2b7a58b2023-12-11 09:13:37 +000050 .version_id = 3,
51 .minimum_version_id = 3,
Pavel Dovgalyuk306e1962016-09-26 11:08:10 +030052 .pre_save = replay_pre_save,
53 .post_load = replay_post_load,
Richard Henderson440b2172023-12-21 14:16:49 +110054 .fields = (const VMStateField[]) {
Pavel Dovgalyuk306e1962016-09-26 11:08:10 +030055 VMSTATE_INT64_ARRAY(cached_clock, ReplayState, REPLAY_CLOCK_COUNT),
Pavel Dovgalyuk13f26712019-07-25 11:44:43 +030056 VMSTATE_UINT64(current_icount, ReplayState),
57 VMSTATE_INT32(instruction_count, ReplayState),
Alex Bennéedcda7322023-12-11 09:13:38 +000058 VMSTATE_UINT32(current_event, ReplayState),
Pavel Dovgalyuk306e1962016-09-26 11:08:10 +030059 VMSTATE_UINT32(data_kind, ReplayState),
Alex Bennée2b7a58b2023-12-11 09:13:37 +000060 VMSTATE_BOOL(has_unread_data, ReplayState),
Pavel Dovgalyuk306e1962016-09-26 11:08:10 +030061 VMSTATE_UINT64(file_offset, ReplayState),
Pavel Dovgalyuk6d0ceb82016-09-26 11:08:16 +030062 VMSTATE_UINT64(block_request_id, ReplayState),
Pavel Dovgalyuk0b30dc02018-02-27 12:53:22 +030063 VMSTATE_UINT64(read_event_id, ReplayState),
Pavel Dovgalyuk306e1962016-09-26 11:08:10 +030064 VMSTATE_END_OF_LIST()
65 },
66};
67
68void replay_vmstate_register(void)
69{
70 vmstate_register(NULL, 0, &vmstate_replay, &replay_state);
71}
Pavel Dovgalyuk9c2037d2017-01-24 10:17:47 +030072
73void replay_vmstate_init(void)
74{
Juan Quintela927d6632017-04-18 18:12:35 +020075 Error *err = NULL;
76
Pavel Dovgalyuk9c2037d2017-01-24 10:17:47 +030077 if (replay_snapshot) {
78 if (replay_mode == REPLAY_MODE_RECORD) {
Daniel P. Berrangéf1a9fcd2021-02-04 12:48:30 +000079 if (!save_snapshot(replay_snapshot,
80 true, NULL, false, NULL, &err)) {
Juan Quintela927d6632017-04-18 18:12:35 +020081 error_report_err(err);
Pavel Dovgalyuk9c2037d2017-01-24 10:17:47 +030082 error_report("Could not create snapshot for icount record");
83 exit(1);
84 }
85 } else if (replay_mode == REPLAY_MODE_PLAY) {
Daniel P. Berrangéf1a9fcd2021-02-04 12:48:30 +000086 if (!load_snapshot(replay_snapshot, NULL, false, NULL, &err)) {
Juan Quintela927d6632017-04-18 18:12:35 +020087 error_report_err(err);
Pavel Dovgalyuk9c2037d2017-01-24 10:17:47 +030088 error_report("Could not load snapshot for icount replay");
89 exit(1);
90 }
91 }
92 }
93}
Pavel Dovgalyuk377b21c2018-02-27 12:52:14 +030094
95bool replay_can_snapshot(void)
96{
97 return replay_mode == REPLAY_MODE_NONE
98 || !replay_has_events();
99}