4 * Copyright (c) 2010-2015 Institute for System Programming
5 * of the Russian Academy of Sciences.
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.
12 #include "qemu-common.h"
13 #include "replay-internal.h"
14 #include "qemu/error-report.h"
15 #include "sysemu/sysemu.h"
17 unsigned int replay_data_kind = -1;
18 static unsigned int replay_has_unread_data;
20 /* Mutex to protect reading and writing events to the log.
21 replay_data_kind and replay_has_unread_data are also protected
23 It also protects replay events queue which stores events to be
24 written or read to the log. */
25 static QemuMutex lock;
27 /* File for replay writing */
30 void replay_put_byte(uint8_t byte)
33 putc(byte, replay_file);
37 void replay_put_event(uint8_t event)
39 replay_put_byte(event);
43 void replay_put_word(uint16_t word)
45 replay_put_byte(word >> 8);
46 replay_put_byte(word);
49 void replay_put_dword(uint32_t dword)
51 replay_put_word(dword >> 16);
52 replay_put_word(dword);
55 void replay_put_qword(int64_t qword)
57 replay_put_dword(qword >> 32);
58 replay_put_dword(qword);
61 void replay_put_array(const uint8_t *buf, size_t size)
64 replay_put_dword(size);
65 fwrite(buf, 1, size, replay_file);
69 uint8_t replay_get_byte(void)
73 byte = getc(replay_file);
78 uint16_t replay_get_word(void)
82 word = replay_get_byte();
83 word = (word << 8) + replay_get_byte();
89 uint32_t replay_get_dword(void)
93 dword = replay_get_word();
94 dword = (dword << 16) + replay_get_word();
100 int64_t replay_get_qword(void)
104 qword = replay_get_dword();
105 qword = (qword << 32) + replay_get_dword();
111 void replay_get_array(uint8_t *buf, size_t *size)
114 *size = replay_get_dword();
115 if (fread(buf, 1, *size, replay_file) != *size) {
116 error_report("replay read error");
121 void replay_get_array_alloc(uint8_t **buf, size_t *size)
124 *size = replay_get_dword();
125 *buf = g_malloc(*size);
126 if (fread(*buf, 1, *size, replay_file) != *size) {
127 error_report("replay read error");
132 void replay_check_error(void)
135 if (feof(replay_file)) {
136 error_report("replay file is over");
137 qemu_system_vmstop_request_prepare();
138 qemu_system_vmstop_request(RUN_STATE_PAUSED);
139 } else if (ferror(replay_file)) {
140 error_report("replay file is over or something goes wrong");
141 qemu_system_vmstop_request_prepare();
142 qemu_system_vmstop_request(RUN_STATE_INTERNAL_ERROR);
147 void replay_fetch_data_kind(void)
150 if (!replay_has_unread_data) {
151 replay_data_kind = replay_get_byte();
152 replay_check_error();
153 replay_has_unread_data = 1;
158 void replay_finish_event(void)
160 replay_has_unread_data = 0;
161 replay_fetch_data_kind();
164 void replay_mutex_init(void)
166 qemu_mutex_init(&lock);
169 void replay_mutex_destroy(void)
171 qemu_mutex_destroy(&lock);
174 void replay_mutex_lock(void)
176 qemu_mutex_lock(&lock);
179 void replay_mutex_unlock(void)
181 qemu_mutex_unlock(&lock);