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/timer.h"
17 #include "qemu/main-loop.h"
18 #include "sysemu/sysemu.h"
19 #include "qemu/error-report.h"
21 /* Current version of the replay mechanism.
22 Increase it when file format changes. */
23 #define REPLAY_VERSION 0xe02002
24 /* Size of replay log header */
25 #define HEADER_SIZE (sizeof(uint32_t) + sizeof(uint64_t))
27 ReplayMode replay_mode
= REPLAY_MODE_NONE
;
29 /* Name of replay file */
30 static char *replay_filename
;
31 ReplayState replay_state
;
32 static GSList
*replay_blockers
;
34 bool replay_next_event_is(int event
)
38 /* nothing to skip - not all instructions used */
39 if (replay_state
.instructions_count
!= 0) {
40 assert(replay_data_kind
== EVENT_INSTRUCTION
);
41 return event
== EVENT_INSTRUCTION
;
45 if (event
== replay_data_kind
) {
48 switch (replay_data_kind
) {
50 replay_finish_event();
51 qemu_system_shutdown_request();
54 /* clock, time_t, checkpoint and other events */
61 uint64_t replay_get_current_step(void)
63 return cpu_get_icount_raw();
66 int replay_get_instructions(void)
70 if (replay_next_event_is(EVENT_INSTRUCTION
)) {
71 res
= replay_state
.instructions_count
;
73 replay_mutex_unlock();
77 void replay_account_executed_instructions(void)
79 if (replay_mode
== REPLAY_MODE_PLAY
) {
81 if (replay_state
.instructions_count
> 0) {
82 int count
= (int)(replay_get_current_step()
83 - replay_state
.current_step
);
84 replay_state
.instructions_count
-= count
;
85 replay_state
.current_step
+= count
;
86 if (replay_state
.instructions_count
== 0) {
87 assert(replay_data_kind
== EVENT_INSTRUCTION
);
88 replay_finish_event();
89 /* Wake up iothread. This is required because
90 timers will not expire until clock counters
91 will be read from the log. */
95 replay_mutex_unlock();
99 bool replay_exception(void)
101 if (replay_mode
== REPLAY_MODE_RECORD
) {
102 replay_save_instructions();
104 replay_put_event(EVENT_EXCEPTION
);
105 replay_mutex_unlock();
107 } else if (replay_mode
== REPLAY_MODE_PLAY
) {
108 bool res
= replay_has_exception();
111 replay_finish_event();
112 replay_mutex_unlock();
120 bool replay_has_exception(void)
123 if (replay_mode
== REPLAY_MODE_PLAY
) {
124 replay_account_executed_instructions();
126 res
= replay_next_event_is(EVENT_EXCEPTION
);
127 replay_mutex_unlock();
133 bool replay_interrupt(void)
135 if (replay_mode
== REPLAY_MODE_RECORD
) {
136 replay_save_instructions();
138 replay_put_event(EVENT_INTERRUPT
);
139 replay_mutex_unlock();
141 } else if (replay_mode
== REPLAY_MODE_PLAY
) {
142 bool res
= replay_has_interrupt();
145 replay_finish_event();
146 replay_mutex_unlock();
154 bool replay_has_interrupt(void)
157 if (replay_mode
== REPLAY_MODE_PLAY
) {
158 replay_account_executed_instructions();
160 res
= replay_next_event_is(EVENT_INTERRUPT
);
161 replay_mutex_unlock();
166 void replay_shutdown_request(void)
168 if (replay_mode
== REPLAY_MODE_RECORD
) {
170 replay_put_event(EVENT_SHUTDOWN
);
171 replay_mutex_unlock();
175 bool replay_checkpoint(ReplayCheckpoint checkpoint
)
178 assert(EVENT_CHECKPOINT
+ checkpoint
<= EVENT_CHECKPOINT_LAST
);
179 replay_save_instructions();
187 if (replay_mode
== REPLAY_MODE_PLAY
) {
188 if (replay_next_event_is(EVENT_CHECKPOINT
+ checkpoint
)) {
189 replay_finish_event();
190 } else if (replay_data_kind
!= EVENT_ASYNC
) {
194 replay_read_events(checkpoint
);
195 /* replay_read_events may leave some unread events.
196 Return false if not all of the events associated with
197 checkpoint were processed */
198 res
= replay_data_kind
!= EVENT_ASYNC
;
199 } else if (replay_mode
== REPLAY_MODE_RECORD
) {
200 replay_put_event(EVENT_CHECKPOINT
+ checkpoint
);
201 replay_save_events(checkpoint
);
205 replay_mutex_unlock();
209 static void replay_enable(const char *fname
, int mode
)
211 const char *fmode
= NULL
;
212 assert(!replay_file
);
215 case REPLAY_MODE_RECORD
:
218 case REPLAY_MODE_PLAY
:
222 fprintf(stderr
, "Replay: internal error: invalid replay mode\n");
226 atexit(replay_finish
);
230 replay_file
= fopen(fname
, fmode
);
231 if (replay_file
== NULL
) {
232 fprintf(stderr
, "Replay: open %s: %s\n", fname
, strerror(errno
));
236 replay_filename
= g_strdup(fname
);
239 replay_data_kind
= -1;
240 replay_state
.instructions_count
= 0;
241 replay_state
.current_step
= 0;
243 /* skip file header for RECORD and check it for PLAY */
244 if (replay_mode
== REPLAY_MODE_RECORD
) {
245 fseek(replay_file
, HEADER_SIZE
, SEEK_SET
);
246 } else if (replay_mode
== REPLAY_MODE_PLAY
) {
247 unsigned int version
= replay_get_dword();
248 if (version
!= REPLAY_VERSION
) {
249 fprintf(stderr
, "Replay: invalid input log file version\n");
252 /* go to the beginning */
253 fseek(replay_file
, HEADER_SIZE
, SEEK_SET
);
254 replay_fetch_data_kind();
257 replay_init_events();
260 void replay_configure(QemuOpts
*opts
)
264 ReplayMode mode
= REPLAY_MODE_NONE
;
266 rr
= qemu_opt_get(opts
, "rr");
268 /* Just enabling icount */
270 } else if (!strcmp(rr
, "record")) {
271 mode
= REPLAY_MODE_RECORD
;
272 } else if (!strcmp(rr
, "replay")) {
273 mode
= REPLAY_MODE_PLAY
;
275 error_report("Invalid icount rr option: %s", rr
);
279 fname
= qemu_opt_get(opts
, "rrfile");
281 error_report("File name not specified for replay");
285 replay_enable(fname
, mode
);
288 void replay_start(void)
290 if (replay_mode
== REPLAY_MODE_NONE
) {
294 if (replay_blockers
) {
295 error_reportf_err(replay_blockers
->data
, "Record/replay: ");
299 error_report("Please enable icount to use record/replay");
303 /* Timer for snapshotting will be set up here. */
305 replay_enable_events();
308 void replay_finish(void)
310 if (replay_mode
== REPLAY_MODE_NONE
) {
314 replay_save_instructions();
316 /* finalize the file */
318 if (replay_mode
== REPLAY_MODE_RECORD
) {
319 /* write end event */
320 replay_put_event(EVENT_END
);
323 fseek(replay_file
, 0, SEEK_SET
);
324 replay_put_dword(REPLAY_VERSION
);
330 if (replay_filename
) {
331 g_free(replay_filename
);
332 replay_filename
= NULL
;
335 replay_finish_events();
336 replay_mutex_destroy();
339 void replay_add_blocker(Error
*reason
)
341 replay_blockers
= g_slist_prepend(replay_blockers
, reason
);