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 "sysemu/replay.h"
14 #include "replay-internal.h"
15 #include "qemu/timer.h"
16 #include "qemu/main-loop.h"
17 #include "sysemu/sysemu.h"
18 #include "qemu/error-report.h"
20 /* Current version of the replay mechanism.
21 Increase it when file format changes. */
22 #define REPLAY_VERSION 0xe02002
23 /* Size of replay log header */
24 #define HEADER_SIZE (sizeof(uint32_t) + sizeof(uint64_t))
26 ReplayMode replay_mode
= REPLAY_MODE_NONE
;
28 /* Name of replay file */
29 static char *replay_filename
;
30 ReplayState replay_state
;
31 static GSList
*replay_blockers
;
33 bool replay_next_event_is(int event
)
37 /* nothing to skip - not all instructions used */
38 if (replay_state
.instructions_count
!= 0) {
39 assert(replay_data_kind
== EVENT_INSTRUCTION
);
40 return event
== EVENT_INSTRUCTION
;
44 if (event
== replay_data_kind
) {
47 switch (replay_data_kind
) {
49 replay_finish_event();
50 qemu_system_shutdown_request();
53 /* clock, time_t, checkpoint and other events */
60 uint64_t replay_get_current_step(void)
62 return cpu_get_icount_raw();
65 int replay_get_instructions(void)
69 if (replay_next_event_is(EVENT_INSTRUCTION
)) {
70 res
= replay_state
.instructions_count
;
72 replay_mutex_unlock();
76 void replay_account_executed_instructions(void)
78 if (replay_mode
== REPLAY_MODE_PLAY
) {
80 if (replay_state
.instructions_count
> 0) {
81 int count
= (int)(replay_get_current_step()
82 - replay_state
.current_step
);
83 replay_state
.instructions_count
-= count
;
84 replay_state
.current_step
+= count
;
85 if (replay_state
.instructions_count
== 0) {
86 assert(replay_data_kind
== EVENT_INSTRUCTION
);
87 replay_finish_event();
88 /* Wake up iothread. This is required because
89 timers will not expire until clock counters
90 will be read from the log. */
94 replay_mutex_unlock();
98 bool replay_exception(void)
100 if (replay_mode
== REPLAY_MODE_RECORD
) {
101 replay_save_instructions();
103 replay_put_event(EVENT_EXCEPTION
);
104 replay_mutex_unlock();
106 } else if (replay_mode
== REPLAY_MODE_PLAY
) {
107 bool res
= replay_has_exception();
110 replay_finish_event();
111 replay_mutex_unlock();
119 bool replay_has_exception(void)
122 if (replay_mode
== REPLAY_MODE_PLAY
) {
123 replay_account_executed_instructions();
125 res
= replay_next_event_is(EVENT_EXCEPTION
);
126 replay_mutex_unlock();
132 bool replay_interrupt(void)
134 if (replay_mode
== REPLAY_MODE_RECORD
) {
135 replay_save_instructions();
137 replay_put_event(EVENT_INTERRUPT
);
138 replay_mutex_unlock();
140 } else if (replay_mode
== REPLAY_MODE_PLAY
) {
141 bool res
= replay_has_interrupt();
144 replay_finish_event();
145 replay_mutex_unlock();
153 bool replay_has_interrupt(void)
156 if (replay_mode
== REPLAY_MODE_PLAY
) {
157 replay_account_executed_instructions();
159 res
= replay_next_event_is(EVENT_INTERRUPT
);
160 replay_mutex_unlock();
165 void replay_shutdown_request(void)
167 if (replay_mode
== REPLAY_MODE_RECORD
) {
169 replay_put_event(EVENT_SHUTDOWN
);
170 replay_mutex_unlock();
174 bool replay_checkpoint(ReplayCheckpoint checkpoint
)
177 assert(EVENT_CHECKPOINT
+ checkpoint
<= EVENT_CHECKPOINT_LAST
);
178 replay_save_instructions();
186 if (replay_mode
== REPLAY_MODE_PLAY
) {
187 if (replay_next_event_is(EVENT_CHECKPOINT
+ checkpoint
)) {
188 replay_finish_event();
189 } else if (replay_data_kind
!= EVENT_ASYNC
) {
193 replay_read_events(checkpoint
);
194 /* replay_read_events may leave some unread events.
195 Return false if not all of the events associated with
196 checkpoint were processed */
197 res
= replay_data_kind
!= EVENT_ASYNC
;
198 } else if (replay_mode
== REPLAY_MODE_RECORD
) {
199 replay_put_event(EVENT_CHECKPOINT
+ checkpoint
);
200 replay_save_events(checkpoint
);
204 replay_mutex_unlock();
208 static void replay_enable(const char *fname
, int mode
)
210 const char *fmode
= NULL
;
211 assert(!replay_file
);
214 case REPLAY_MODE_RECORD
:
217 case REPLAY_MODE_PLAY
:
221 fprintf(stderr
, "Replay: internal error: invalid replay mode\n");
225 atexit(replay_finish
);
229 replay_file
= fopen(fname
, fmode
);
230 if (replay_file
== NULL
) {
231 fprintf(stderr
, "Replay: open %s: %s\n", fname
, strerror(errno
));
235 replay_filename
= g_strdup(fname
);
238 replay_data_kind
= -1;
239 replay_state
.instructions_count
= 0;
240 replay_state
.current_step
= 0;
242 /* skip file header for RECORD and check it for PLAY */
243 if (replay_mode
== REPLAY_MODE_RECORD
) {
244 fseek(replay_file
, HEADER_SIZE
, SEEK_SET
);
245 } else if (replay_mode
== REPLAY_MODE_PLAY
) {
246 unsigned int version
= replay_get_dword();
247 if (version
!= REPLAY_VERSION
) {
248 fprintf(stderr
, "Replay: invalid input log file version\n");
251 /* go to the beginning */
252 fseek(replay_file
, HEADER_SIZE
, SEEK_SET
);
253 replay_fetch_data_kind();
256 replay_init_events();
259 void replay_configure(QemuOpts
*opts
)
263 ReplayMode mode
= REPLAY_MODE_NONE
;
265 rr
= qemu_opt_get(opts
, "rr");
267 /* Just enabling icount */
269 } else if (!strcmp(rr
, "record")) {
270 mode
= REPLAY_MODE_RECORD
;
271 } else if (!strcmp(rr
, "replay")) {
272 mode
= REPLAY_MODE_PLAY
;
274 error_report("Invalid icount rr option: %s", rr
);
278 fname
= qemu_opt_get(opts
, "rrfile");
280 error_report("File name not specified for replay");
284 replay_enable(fname
, mode
);
287 void replay_start(void)
289 if (replay_mode
== REPLAY_MODE_NONE
) {
293 if (replay_blockers
) {
294 error_reportf_err(replay_blockers
->data
, "Record/replay: ");
298 error_report("Please enable icount to use record/replay");
302 /* Timer for snapshotting will be set up here. */
304 replay_enable_events();
307 void replay_finish(void)
309 if (replay_mode
== REPLAY_MODE_NONE
) {
313 replay_save_instructions();
315 /* finalize the file */
317 if (replay_mode
== REPLAY_MODE_RECORD
) {
318 /* write end event */
319 replay_put_event(EVENT_END
);
322 fseek(replay_file
, 0, SEEK_SET
);
323 replay_put_dword(REPLAY_VERSION
);
329 if (replay_filename
) {
330 g_free(replay_filename
);
331 replay_filename
= NULL
;
334 replay_finish_events();
335 replay_mutex_destroy();
338 void replay_add_blocker(Error
*reason
)
340 replay_blockers
= g_slist_prepend(replay_blockers
, reason
);