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