xref: /qemu/replay/replay-snapshot.c (revision 416e34bd)
1 /*
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"
14 #include "sysemu/replay.h"
15 #include "replay-internal.h"
16 #include "sysemu/sysemu.h"
17 #include "monitor/monitor.h"
18 #include "qapi/qmp/qstring.h"
19 #include "qemu/error-report.h"
20 #include "migration/vmstate.h"
21 #include "migration/snapshot.h"
22 
23 static int replay_pre_save(void *opaque)
24 {
25     ReplayState *state = opaque;
26     state->file_offset = ftell(replay_file);
27     state->host_clock_last = qemu_clock_get_last(QEMU_CLOCK_HOST);
28 
29     return 0;
30 }
31 
32 static int replay_post_load(void *opaque, int version_id)
33 {
34     ReplayState *state = opaque;
35     if (replay_mode == REPLAY_MODE_PLAY) {
36         fseek(replay_file, state->file_offset, SEEK_SET);
37         qemu_clock_set_last(QEMU_CLOCK_HOST, state->host_clock_last);
38         /* If this was a vmstate, saved in recording mode,
39            we need to initialize replay data fields. */
40         replay_fetch_data_kind();
41     } else if (replay_mode == REPLAY_MODE_RECORD) {
42         /* This is only useful for loading the initial state.
43            Therefore reset all the counters. */
44         state->instructions_count = 0;
45         state->block_request_id = 0;
46     }
47 
48     return 0;
49 }
50 
51 static const VMStateDescription vmstate_replay = {
52     .name = "replay",
53     .version_id = 1,
54     .minimum_version_id = 1,
55     .pre_save = replay_pre_save,
56     .post_load = replay_post_load,
57     .fields = (VMStateField[]) {
58         VMSTATE_INT64_ARRAY(cached_clock, ReplayState, REPLAY_CLOCK_COUNT),
59         VMSTATE_UINT64(current_step, ReplayState),
60         VMSTATE_INT32(instructions_count, ReplayState),
61         VMSTATE_UINT32(data_kind, ReplayState),
62         VMSTATE_UINT32(has_unread_data, ReplayState),
63         VMSTATE_UINT64(file_offset, ReplayState),
64         VMSTATE_UINT64(block_request_id, ReplayState),
65         VMSTATE_UINT64(host_clock_last, ReplayState),
66         VMSTATE_INT32(read_event_kind, ReplayState),
67         VMSTATE_UINT64(read_event_id, ReplayState),
68         VMSTATE_INT32(read_event_checkpoint, ReplayState),
69         VMSTATE_END_OF_LIST()
70     },
71 };
72 
73 void replay_vmstate_register(void)
74 {
75     vmstate_register(NULL, 0, &vmstate_replay, &replay_state);
76 }
77 
78 void replay_vmstate_init(void)
79 {
80     Error *err = NULL;
81 
82     if (replay_snapshot) {
83         if (replay_mode == REPLAY_MODE_RECORD) {
84             if (save_snapshot(replay_snapshot, &err) != 0) {
85                 error_report_err(err);
86                 error_report("Could not create snapshot for icount record");
87                 exit(1);
88             }
89         } else if (replay_mode == REPLAY_MODE_PLAY) {
90             if (load_snapshot(replay_snapshot, &err) != 0) {
91                 error_report_err(err);
92                 error_report("Could not load snapshot for icount replay");
93                 exit(1);
94             }
95         }
96     }
97 }
98 
99 bool replay_can_snapshot(void)
100 {
101     return replay_mode == REPLAY_MODE_NONE
102         || !replay_has_events();
103 }
104