Pavel Dovgalyuk | 306e196 | 2016-09-26 11:08:10 +0300 | [diff] [blame] | 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" |
Pavel Dovgalyuk | 306e196 | 2016-09-26 11:08:10 +0300 | [diff] [blame] | 14 | #include "sysemu/replay.h" |
| 15 | #include "replay-internal.h" |
Pavel Dovgalyuk | 306e196 | 2016-09-26 11:08:10 +0300 | [diff] [blame] | 16 | #include "monitor/monitor.h" |
| 17 | #include "qapi/qmp/qstring.h" |
| 18 | #include "qemu/error-report.h" |
| 19 | #include "migration/vmstate.h" |
Juan Quintela | 5e22479 | 2017-04-20 14:25:55 +0200 | [diff] [blame] | 20 | #include "migration/snapshot.h" |
Pavel Dovgalyuk | 306e196 | 2016-09-26 11:08:10 +0300 | [diff] [blame] | 21 | |
Dr. David Alan Gilbert | 44b1ff3 | 2017-09-25 12:29:12 +0100 | [diff] [blame] | 22 | static int replay_pre_save(void *opaque) |
Pavel Dovgalyuk | 306e196 | 2016-09-26 11:08:10 +0300 | [diff] [blame] | 23 | { |
| 24 | ReplayState *state = opaque; |
| 25 | state->file_offset = ftell(replay_file); |
Dr. David Alan Gilbert | 44b1ff3 | 2017-09-25 12:29:12 +0100 | [diff] [blame] | 26 | |
| 27 | return 0; |
Pavel Dovgalyuk | 306e196 | 2016-09-26 11:08:10 +0300 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | static int replay_post_load(void *opaque, int version_id) |
| 31 | { |
| 32 | ReplayState *state = opaque; |
Pavel Dovgalyuk | bb3d770 | 2018-09-12 11:19:39 +0300 | [diff] [blame] | 33 | if (replay_mode == REPLAY_MODE_PLAY) { |
| 34 | fseek(replay_file, state->file_offset, SEEK_SET); |
Pavel Dovgalyuk | bb3d770 | 2018-09-12 11:19:39 +0300 | [diff] [blame] | 35 | /* 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 Dovgalyuk | 13f2671 | 2019-07-25 11:44:43 +0300 | [diff] [blame] | 41 | state->instruction_count = 0; |
Pavel Dovgalyuk | bb3d770 | 2018-09-12 11:19:39 +0300 | [diff] [blame] | 42 | state->block_request_id = 0; |
| 43 | } |
Pavel Dovgalyuk | 306e196 | 2016-09-26 11:08:10 +0300 | [diff] [blame] | 44 | |
| 45 | return 0; |
| 46 | } |
| 47 | |
| 48 | static const VMStateDescription vmstate_replay = { |
| 49 | .name = "replay", |
Dr. David Alan Gilbert | a02fe2c | 2019-07-24 12:58:22 +0100 | [diff] [blame] | 50 | .version_id = 2, |
| 51 | .minimum_version_id = 2, |
Pavel Dovgalyuk | 306e196 | 2016-09-26 11:08:10 +0300 | [diff] [blame] | 52 | .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 Dovgalyuk | 13f2671 | 2019-07-25 11:44:43 +0300 | [diff] [blame] | 56 | VMSTATE_UINT64(current_icount, ReplayState), |
| 57 | VMSTATE_INT32(instruction_count, ReplayState), |
Pavel Dovgalyuk | 306e196 | 2016-09-26 11:08:10 +0300 | [diff] [blame] | 58 | VMSTATE_UINT32(data_kind, ReplayState), |
| 59 | VMSTATE_UINT32(has_unread_data, ReplayState), |
| 60 | VMSTATE_UINT64(file_offset, ReplayState), |
Pavel Dovgalyuk | 6d0ceb8 | 2016-09-26 11:08:16 +0300 | [diff] [blame] | 61 | VMSTATE_UINT64(block_request_id, ReplayState), |
Pavel Dovgalyuk | 0b30dc0 | 2018-02-27 12:53:22 +0300 | [diff] [blame] | 62 | VMSTATE_INT32(read_event_kind, ReplayState), |
| 63 | VMSTATE_UINT64(read_event_id, ReplayState), |
| 64 | VMSTATE_INT32(read_event_checkpoint, ReplayState), |
Pavel Dovgalyuk | 306e196 | 2016-09-26 11:08:10 +0300 | [diff] [blame] | 65 | VMSTATE_END_OF_LIST() |
| 66 | }, |
| 67 | }; |
| 68 | |
| 69 | void replay_vmstate_register(void) |
| 70 | { |
| 71 | vmstate_register(NULL, 0, &vmstate_replay, &replay_state); |
| 72 | } |
Pavel Dovgalyuk | 9c2037d | 2017-01-24 10:17:47 +0300 | [diff] [blame] | 73 | |
| 74 | void replay_vmstate_init(void) |
| 75 | { |
Juan Quintela | 927d663 | 2017-04-18 18:12:35 +0200 | [diff] [blame] | 76 | Error *err = NULL; |
| 77 | |
Pavel Dovgalyuk | 9c2037d | 2017-01-24 10:17:47 +0300 | [diff] [blame] | 78 | if (replay_snapshot) { |
| 79 | if (replay_mode == REPLAY_MODE_RECORD) { |
Juan Quintela | 5e22479 | 2017-04-20 14:25:55 +0200 | [diff] [blame] | 80 | if (save_snapshot(replay_snapshot, &err) != 0) { |
Juan Quintela | 927d663 | 2017-04-18 18:12:35 +0200 | [diff] [blame] | 81 | error_report_err(err); |
Pavel Dovgalyuk | 9c2037d | 2017-01-24 10:17:47 +0300 | [diff] [blame] | 82 | error_report("Could not create snapshot for icount record"); |
| 83 | exit(1); |
| 84 | } |
| 85 | } else if (replay_mode == REPLAY_MODE_PLAY) { |
Juan Quintela | 5e22479 | 2017-04-20 14:25:55 +0200 | [diff] [blame] | 86 | if (load_snapshot(replay_snapshot, &err) != 0) { |
Juan Quintela | 927d663 | 2017-04-18 18:12:35 +0200 | [diff] [blame] | 87 | error_report_err(err); |
Pavel Dovgalyuk | 9c2037d | 2017-01-24 10:17:47 +0300 | [diff] [blame] | 88 | error_report("Could not load snapshot for icount replay"); |
| 89 | exit(1); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | } |
Pavel Dovgalyuk | 377b21c | 2018-02-27 12:52:14 +0300 | [diff] [blame] | 94 | |
| 95 | bool replay_can_snapshot(void) |
| 96 | { |
| 97 | return replay_mode == REPLAY_MODE_NONE |
| 98 | || !replay_has_events(); |
| 99 | } |