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 "qapi/error.h"
14 #include "qemu-common.h"
15 #include "sysemu/replay.h"
16 #include "replay-internal.h"
17 #include "qemu/timer.h"
18 #include "qemu/main-loop.h"
19 #include "sysemu/sysemu.h"
20 #include "qemu/error-report.h"
22 /* Current version of the replay mechanism.
23 Increase it when file format changes. */
24 #define REPLAY_VERSION 0xe02005
25 /* Size of replay log header */
26 #define HEADER_SIZE (sizeof(uint32_t) + sizeof(uint64_t))
28 ReplayMode replay_mode
= REPLAY_MODE_NONE
;
29 char *replay_snapshot
;
31 /* Name of replay file */
32 static char *replay_filename
;
33 ReplayState replay_state
;
34 static GSList
*replay_blockers
;
36 bool replay_next_event_is(int event
)
40 /* nothing to skip - not all instructions used */
41 if (replay_state
.instructions_count
!= 0) {
42 assert(replay_state
.data_kind
== EVENT_INSTRUCTION
);
43 return event
== EVENT_INSTRUCTION
;
47 if (event
== replay_state
.data_kind
) {
50 switch (replay_state
.data_kind
) {
52 replay_finish_event();
53 qemu_system_shutdown_request();
56 /* clock, time_t, checkpoint and other events */
63 uint64_t replay_get_current_step(void)
65 return cpu_get_icount_raw();
68 int replay_get_instructions(void)
72 if (replay_next_event_is(EVENT_INSTRUCTION
)) {
73 res
= replay_state
.instructions_count
;
75 replay_mutex_unlock();
79 void replay_account_executed_instructions(void)
81 if (replay_mode
== REPLAY_MODE_PLAY
) {
83 if (replay_state
.instructions_count
> 0) {
84 int count
= (int)(replay_get_current_step()
85 - replay_state
.current_step
);
86 replay_state
.instructions_count
-= count
;
87 replay_state
.current_step
+= count
;
88 if (replay_state
.instructions_count
== 0) {
89 assert(replay_state
.data_kind
== EVENT_INSTRUCTION
);
90 replay_finish_event();
91 /* Wake up iothread. This is required because
92 timers will not expire until clock counters
93 will be read from the log. */
97 replay_mutex_unlock();
101 bool replay_exception(void)
103 if (replay_mode
== REPLAY_MODE_RECORD
) {
104 replay_save_instructions();
106 replay_put_event(EVENT_EXCEPTION
);
107 replay_mutex_unlock();
109 } else if (replay_mode
== REPLAY_MODE_PLAY
) {
110 bool res
= replay_has_exception();
113 replay_finish_event();
114 replay_mutex_unlock();
122 bool replay_has_exception(void)
125 if (replay_mode
== REPLAY_MODE_PLAY
) {
126 replay_account_executed_instructions();
128 res
= replay_next_event_is(EVENT_EXCEPTION
);
129 replay_mutex_unlock();
135 bool replay_interrupt(void)
137 if (replay_mode
== REPLAY_MODE_RECORD
) {
138 replay_save_instructions();
140 replay_put_event(EVENT_INTERRUPT
);
141 replay_mutex_unlock();
143 } else if (replay_mode
== REPLAY_MODE_PLAY
) {
144 bool res
= replay_has_interrupt();
147 replay_finish_event();
148 replay_mutex_unlock();
156 bool replay_has_interrupt(void)
159 if (replay_mode
== REPLAY_MODE_PLAY
) {
160 replay_account_executed_instructions();
162 res
= replay_next_event_is(EVENT_INTERRUPT
);
163 replay_mutex_unlock();
168 void replay_shutdown_request(void)
170 if (replay_mode
== REPLAY_MODE_RECORD
) {
172 replay_put_event(EVENT_SHUTDOWN
);
173 replay_mutex_unlock();
177 bool replay_checkpoint(ReplayCheckpoint checkpoint
)
180 assert(EVENT_CHECKPOINT
+ checkpoint
<= EVENT_CHECKPOINT_LAST
);
181 replay_save_instructions();
189 if (replay_mode
== REPLAY_MODE_PLAY
) {
190 if (replay_next_event_is(EVENT_CHECKPOINT
+ checkpoint
)) {
191 replay_finish_event();
192 } else if (replay_state
.data_kind
!= EVENT_ASYNC
) {
196 replay_read_events(checkpoint
);
197 /* replay_read_events may leave some unread events.
198 Return false if not all of the events associated with
199 checkpoint were processed */
200 res
= replay_state
.data_kind
!= EVENT_ASYNC
;
201 } else if (replay_mode
== REPLAY_MODE_RECORD
) {
202 replay_put_event(EVENT_CHECKPOINT
+ checkpoint
);
203 replay_save_events(checkpoint
);
207 replay_mutex_unlock();
211 static void replay_enable(const char *fname
, int mode
)
213 const char *fmode
= NULL
;
214 assert(!replay_file
);
217 case REPLAY_MODE_RECORD
:
220 case REPLAY_MODE_PLAY
:
224 fprintf(stderr
, "Replay: internal error: invalid replay mode\n");
228 atexit(replay_finish
);
232 replay_file
= fopen(fname
, fmode
);
233 if (replay_file
== NULL
) {
234 fprintf(stderr
, "Replay: open %s: %s\n", fname
, strerror(errno
));
238 replay_filename
= g_strdup(fname
);
241 replay_state
.data_kind
= -1;
242 replay_state
.instructions_count
= 0;
243 replay_state
.current_step
= 0;
244 replay_state
.has_unread_data
= 0;
246 /* skip file header for RECORD and check it for PLAY */
247 if (replay_mode
== REPLAY_MODE_RECORD
) {
248 fseek(replay_file
, HEADER_SIZE
, SEEK_SET
);
249 } else if (replay_mode
== REPLAY_MODE_PLAY
) {
250 unsigned int version
= replay_get_dword();
251 if (version
!= REPLAY_VERSION
) {
252 fprintf(stderr
, "Replay: invalid input log file version\n");
255 /* go to the beginning */
256 fseek(replay_file
, HEADER_SIZE
, SEEK_SET
);
257 replay_fetch_data_kind();
260 replay_init_events();
263 void replay_configure(QemuOpts
*opts
)
267 ReplayMode mode
= REPLAY_MODE_NONE
;
275 qemu_opts_loc_restore(opts
);
277 rr
= qemu_opt_get(opts
, "rr");
279 /* Just enabling icount */
281 } else if (!strcmp(rr
, "record")) {
282 mode
= REPLAY_MODE_RECORD
;
283 } else if (!strcmp(rr
, "replay")) {
284 mode
= REPLAY_MODE_PLAY
;
286 error_report("Invalid icount rr option: %s", rr
);
290 fname
= qemu_opt_get(opts
, "rrfile");
292 error_report("File name not specified for replay");
296 replay_snapshot
= g_strdup(qemu_opt_get(opts
, "rrsnapshot"));
297 replay_vmstate_register();
298 replay_enable(fname
, mode
);
304 void replay_start(void)
306 if (replay_mode
== REPLAY_MODE_NONE
) {
310 if (replay_blockers
) {
311 error_reportf_err(replay_blockers
->data
, "Record/replay: ");
315 error_report("Please enable icount to use record/replay");
319 /* Timer for snapshotting will be set up here. */
321 replay_enable_events();
324 void replay_finish(void)
326 if (replay_mode
== REPLAY_MODE_NONE
) {
330 replay_save_instructions();
332 /* finalize the file */
334 if (replay_mode
== REPLAY_MODE_RECORD
) {
335 /* write end event */
336 replay_put_event(EVENT_END
);
339 fseek(replay_file
, 0, SEEK_SET
);
340 replay_put_dword(REPLAY_VERSION
);
346 if (replay_filename
) {
347 g_free(replay_filename
);
348 replay_filename
= NULL
;
351 g_free(replay_snapshot
);
352 replay_snapshot
= NULL
;
354 replay_finish_events();
355 replay_mutex_destroy();
358 void replay_add_blocker(Error
*reason
)
360 replay_blockers
= g_slist_prepend(replay_blockers
, reason
);