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 "sysemu/replay.h"
15 #include "replay-internal.h"
16 #include "qemu/timer.h"
17 #include "qemu/main-loop.h"
18 #include "qemu/option.h"
19 #include "sysemu/cpus.h"
20 #include "sysemu/sysemu.h"
21 #include "qemu/error-report.h"
23 /* Current version of the replay mechanism.
24 Increase it when file format changes. */
25 #define REPLAY_VERSION 0xe02006
26 /* Size of replay log header */
27 #define HEADER_SIZE (sizeof(uint32_t) + sizeof(uint64_t))
29 ReplayMode replay_mode
= REPLAY_MODE_NONE
;
30 char *replay_snapshot
;
32 /* Name of replay file */
33 static char *replay_filename
;
34 ReplayState replay_state
;
35 static GSList
*replay_blockers
;
37 bool replay_next_event_is(int event
)
41 /* nothing to skip - not all instructions used */
42 if (replay_state
.instructions_count
!= 0) {
43 assert(replay_state
.data_kind
== EVENT_INSTRUCTION
);
44 return event
== EVENT_INSTRUCTION
;
48 if (event
== replay_state
.data_kind
) {
51 switch (replay_state
.data_kind
) {
52 case EVENT_SHUTDOWN
... EVENT_SHUTDOWN_LAST
:
53 replay_finish_event();
54 qemu_system_shutdown_request(replay_state
.data_kind
-
58 /* clock, time_t, checkpoint and other events */
65 uint64_t replay_get_current_step(void)
67 return cpu_get_icount_raw();
70 int replay_get_instructions(void)
74 if (replay_next_event_is(EVENT_INSTRUCTION
)) {
75 res
= replay_state
.instructions_count
;
77 replay_mutex_unlock();
81 void replay_account_executed_instructions(void)
83 if (replay_mode
== REPLAY_MODE_PLAY
) {
85 if (replay_state
.instructions_count
> 0) {
86 int count
= (int)(replay_get_current_step()
87 - replay_state
.current_step
);
89 /* Time can only go forward */
92 replay_state
.instructions_count
-= count
;
93 replay_state
.current_step
+= count
;
94 if (replay_state
.instructions_count
== 0) {
95 assert(replay_state
.data_kind
== EVENT_INSTRUCTION
);
96 replay_finish_event();
97 /* Wake up iothread. This is required because
98 timers will not expire until clock counters
99 will be read from the log. */
103 replay_mutex_unlock();
107 bool replay_exception(void)
109 if (replay_mode
== REPLAY_MODE_RECORD
) {
110 replay_save_instructions();
112 replay_put_event(EVENT_EXCEPTION
);
113 replay_mutex_unlock();
115 } else if (replay_mode
== REPLAY_MODE_PLAY
) {
116 bool res
= replay_has_exception();
119 replay_finish_event();
120 replay_mutex_unlock();
128 bool replay_has_exception(void)
131 if (replay_mode
== REPLAY_MODE_PLAY
) {
132 replay_account_executed_instructions();
134 res
= replay_next_event_is(EVENT_EXCEPTION
);
135 replay_mutex_unlock();
141 bool replay_interrupt(void)
143 if (replay_mode
== REPLAY_MODE_RECORD
) {
144 replay_save_instructions();
146 replay_put_event(EVENT_INTERRUPT
);
147 replay_mutex_unlock();
149 } else if (replay_mode
== REPLAY_MODE_PLAY
) {
150 bool res
= replay_has_interrupt();
153 replay_finish_event();
154 replay_mutex_unlock();
162 bool replay_has_interrupt(void)
165 if (replay_mode
== REPLAY_MODE_PLAY
) {
166 replay_account_executed_instructions();
168 res
= replay_next_event_is(EVENT_INTERRUPT
);
169 replay_mutex_unlock();
174 void replay_shutdown_request(ShutdownCause cause
)
176 if (replay_mode
== REPLAY_MODE_RECORD
) {
178 replay_put_event(EVENT_SHUTDOWN
+ cause
);
179 replay_mutex_unlock();
183 bool replay_checkpoint(ReplayCheckpoint checkpoint
)
186 assert(EVENT_CHECKPOINT
+ checkpoint
<= EVENT_CHECKPOINT_LAST
);
187 replay_save_instructions();
195 if (replay_mode
== REPLAY_MODE_PLAY
) {
196 if (replay_next_event_is(EVENT_CHECKPOINT
+ checkpoint
)) {
197 replay_finish_event();
198 } else if (replay_state
.data_kind
!= EVENT_ASYNC
) {
202 replay_read_events(checkpoint
);
203 /* replay_read_events may leave some unread events.
204 Return false if not all of the events associated with
205 checkpoint were processed */
206 res
= replay_state
.data_kind
!= EVENT_ASYNC
;
207 } else if (replay_mode
== REPLAY_MODE_RECORD
) {
208 replay_put_event(EVENT_CHECKPOINT
+ checkpoint
);
209 replay_save_events(checkpoint
);
213 replay_mutex_unlock();
217 static void replay_enable(const char *fname
, int mode
)
219 const char *fmode
= NULL
;
220 assert(!replay_file
);
223 case REPLAY_MODE_RECORD
:
226 case REPLAY_MODE_PLAY
:
230 fprintf(stderr
, "Replay: internal error: invalid replay mode\n");
234 atexit(replay_finish
);
238 replay_file
= fopen(fname
, fmode
);
239 if (replay_file
== NULL
) {
240 fprintf(stderr
, "Replay: open %s: %s\n", fname
, strerror(errno
));
244 replay_filename
= g_strdup(fname
);
247 replay_state
.data_kind
= -1;
248 replay_state
.instructions_count
= 0;
249 replay_state
.current_step
= 0;
250 replay_state
.has_unread_data
= 0;
252 /* skip file header for RECORD and check it for PLAY */
253 if (replay_mode
== REPLAY_MODE_RECORD
) {
254 fseek(replay_file
, HEADER_SIZE
, SEEK_SET
);
255 } else if (replay_mode
== REPLAY_MODE_PLAY
) {
256 unsigned int version
= replay_get_dword();
257 if (version
!= REPLAY_VERSION
) {
258 fprintf(stderr
, "Replay: invalid input log file version\n");
261 /* go to the beginning */
262 fseek(replay_file
, HEADER_SIZE
, SEEK_SET
);
263 replay_fetch_data_kind();
266 replay_init_events();
269 void replay_configure(QemuOpts
*opts
)
273 ReplayMode mode
= REPLAY_MODE_NONE
;
281 qemu_opts_loc_restore(opts
);
283 rr
= qemu_opt_get(opts
, "rr");
285 /* Just enabling icount */
287 } else if (!strcmp(rr
, "record")) {
288 mode
= REPLAY_MODE_RECORD
;
289 } else if (!strcmp(rr
, "replay")) {
290 mode
= REPLAY_MODE_PLAY
;
292 error_report("Invalid icount rr option: %s", rr
);
296 fname
= qemu_opt_get(opts
, "rrfile");
298 error_report("File name not specified for replay");
302 replay_snapshot
= g_strdup(qemu_opt_get(opts
, "rrsnapshot"));
303 replay_vmstate_register();
304 replay_enable(fname
, mode
);
310 void replay_start(void)
312 if (replay_mode
== REPLAY_MODE_NONE
) {
316 if (replay_blockers
) {
317 error_reportf_err(replay_blockers
->data
, "Record/replay: ");
321 error_report("Please enable icount to use record/replay");
325 /* Timer for snapshotting will be set up here. */
327 replay_enable_events();
330 void replay_finish(void)
332 if (replay_mode
== REPLAY_MODE_NONE
) {
336 replay_save_instructions();
338 /* finalize the file */
340 if (replay_mode
== REPLAY_MODE_RECORD
) {
341 /* write end event */
342 replay_put_event(EVENT_END
);
345 fseek(replay_file
, 0, SEEK_SET
);
346 replay_put_dword(REPLAY_VERSION
);
352 if (replay_filename
) {
353 g_free(replay_filename
);
354 replay_filename
= NULL
;
357 g_free(replay_snapshot
);
358 replay_snapshot
= NULL
;
360 replay_finish_events();
361 replay_mutex_destroy();
364 void replay_add_blocker(Error
*reason
)
366 replay_blockers
= g_slist_prepend(replay_blockers
, reason
);