kvm-all: add trace events for kvm irqchip ops
[qemu/kevin.git] / replay / replay-internal.c
blob5835e8def3b48c353c17128ab5d2e445e30b34d9
1 /*
2 * replay-internal.c
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/osdep.h"
13 #include "qemu-common.h"
14 #include "sysemu/replay.h"
15 #include "replay-internal.h"
16 #include "qemu/error-report.h"
17 #include "sysemu/sysemu.h"
19 unsigned int replay_data_kind = -1;
20 static unsigned int replay_has_unread_data;
22 /* Mutex to protect reading and writing events to the log.
23 replay_data_kind and replay_has_unread_data are also protected
24 by this mutex.
25 It also protects replay events queue which stores events to be
26 written or read to the log. */
27 static QemuMutex lock;
29 /* File for replay writing */
30 FILE *replay_file;
32 void replay_put_byte(uint8_t byte)
34 if (replay_file) {
35 putc(byte, replay_file);
39 void replay_put_event(uint8_t event)
41 assert(event < EVENT_COUNT);
42 replay_put_byte(event);
46 void replay_put_word(uint16_t word)
48 replay_put_byte(word >> 8);
49 replay_put_byte(word);
52 void replay_put_dword(uint32_t dword)
54 replay_put_word(dword >> 16);
55 replay_put_word(dword);
58 void replay_put_qword(int64_t qword)
60 replay_put_dword(qword >> 32);
61 replay_put_dword(qword);
64 void replay_put_array(const uint8_t *buf, size_t size)
66 if (replay_file) {
67 replay_put_dword(size);
68 fwrite(buf, 1, size, replay_file);
72 uint8_t replay_get_byte(void)
74 uint8_t byte = 0;
75 if (replay_file) {
76 byte = getc(replay_file);
78 return byte;
81 uint16_t replay_get_word(void)
83 uint16_t word = 0;
84 if (replay_file) {
85 word = replay_get_byte();
86 word = (word << 8) + replay_get_byte();
89 return word;
92 uint32_t replay_get_dword(void)
94 uint32_t dword = 0;
95 if (replay_file) {
96 dword = replay_get_word();
97 dword = (dword << 16) + replay_get_word();
100 return dword;
103 int64_t replay_get_qword(void)
105 int64_t qword = 0;
106 if (replay_file) {
107 qword = replay_get_dword();
108 qword = (qword << 32) + replay_get_dword();
111 return qword;
114 void replay_get_array(uint8_t *buf, size_t *size)
116 if (replay_file) {
117 *size = replay_get_dword();
118 if (fread(buf, 1, *size, replay_file) != *size) {
119 error_report("replay read error");
124 void replay_get_array_alloc(uint8_t **buf, size_t *size)
126 if (replay_file) {
127 *size = replay_get_dword();
128 *buf = g_malloc(*size);
129 if (fread(*buf, 1, *size, replay_file) != *size) {
130 error_report("replay read error");
135 void replay_check_error(void)
137 if (replay_file) {
138 if (feof(replay_file)) {
139 error_report("replay file is over");
140 qemu_system_vmstop_request_prepare();
141 qemu_system_vmstop_request(RUN_STATE_PAUSED);
142 } else if (ferror(replay_file)) {
143 error_report("replay file is over or something goes wrong");
144 qemu_system_vmstop_request_prepare();
145 qemu_system_vmstop_request(RUN_STATE_INTERNAL_ERROR);
150 void replay_fetch_data_kind(void)
152 if (replay_file) {
153 if (!replay_has_unread_data) {
154 replay_data_kind = replay_get_byte();
155 if (replay_data_kind == EVENT_INSTRUCTION) {
156 replay_state.instructions_count = replay_get_dword();
158 replay_check_error();
159 replay_has_unread_data = 1;
160 if (replay_data_kind >= EVENT_COUNT) {
161 error_report("Replay: unknown event kind %d", replay_data_kind);
162 exit(1);
168 void replay_finish_event(void)
170 replay_has_unread_data = 0;
171 replay_fetch_data_kind();
174 void replay_mutex_init(void)
176 qemu_mutex_init(&lock);
179 void replay_mutex_destroy(void)
181 qemu_mutex_destroy(&lock);
184 void replay_mutex_lock(void)
186 qemu_mutex_lock(&lock);
189 void replay_mutex_unlock(void)
191 qemu_mutex_unlock(&lock);
194 /*! Saves cached instructions. */
195 void replay_save_instructions(void)
197 if (replay_file && replay_mode == REPLAY_MODE_RECORD) {
198 replay_mutex_lock();
199 int diff = (int)(replay_get_current_step() - replay_state.current_step);
200 if (diff > 0) {
201 replay_put_event(EVENT_INSTRUCTION);
202 replay_put_dword(diff);
203 replay_state.current_step += diff;
205 replay_mutex_unlock();