blob: e26fa4c892e843fd1dba03ecf92b8b736aa3cdd3 [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",
Dr. David Alan Gilberta02fe2c2019-07-24 12:58:22 +010050 .version_id = 2,
51 .minimum_version_id = 2,
Pavel Dovgalyuk306e1962016-09-26 11:08:10 +030052 .pre_save = replay_pre_save,
53 .post_load = replay_post_load,
54 .fields = (VMStateField[]) {
55 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),
Pavel Dovgalyuk306e1962016-09-26 11:08:10 +030058 VMSTATE_UINT32(data_kind, ReplayState),
59 VMSTATE_UINT32(has_unread_data, ReplayState),
60 VMSTATE_UINT64(file_offset, ReplayState),
Pavel Dovgalyuk6d0ceb82016-09-26 11:08:16 +030061 VMSTATE_UINT64(block_request_id, ReplayState),
Pavel Dovgalyuk0b30dc02018-02-27 12:53:22 +030062 VMSTATE_INT32(read_event_kind, ReplayState),
63 VMSTATE_UINT64(read_event_id, ReplayState),
64 VMSTATE_INT32(read_event_checkpoint, ReplayState),
Pavel Dovgalyuk306e1962016-09-26 11:08:10 +030065 VMSTATE_END_OF_LIST()
66 },
67};
68
69void replay_vmstate_register(void)
70{
71 vmstate_register(NULL, 0, &vmstate_replay, &replay_state);
72}
Pavel Dovgalyuk9c2037d2017-01-24 10:17:47 +030073
74void replay_vmstate_init(void)
75{
Juan Quintela927d6632017-04-18 18:12:35 +020076 Error *err = NULL;
77
Pavel Dovgalyuk9c2037d2017-01-24 10:17:47 +030078 if (replay_snapshot) {
79 if (replay_mode == REPLAY_MODE_RECORD) {
Juan Quintela5e224792017-04-20 14:25:55 +020080 if (save_snapshot(replay_snapshot, &err) != 0) {
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) {
Juan Quintela5e224792017-04-20 14:25:55 +020086 if (load_snapshot(replay_snapshot, &err) != 0) {
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}