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 0xe02004
25 /* Size of replay log header */
26 #define HEADER_SIZE (sizeof(uint32_t) + sizeof(uint64_t))
28 ReplayMode replay_mode
= REPLAY_MODE_NONE
;
30 /* Name of replay file */
31 static char *replay_filename
;
32 ReplayState replay_state
;
33 static GSList
*replay_blockers
;
35 bool replay_next_event_is(int event
)
39 /* nothing to skip - not all instructions used */
40 if (replay_state
.instructions_count
!= 0) {
41 assert(replay_data_kind
== EVENT_INSTRUCTION
);
42 return event
== EVENT_INSTRUCTION
;
46 if (event
== replay_data_kind
) {
49 switch (replay_data_kind
) {
51 replay_finish_event();
52 qemu_system_shutdown_request();
55 /* clock, time_t, checkpoint and other events */
62 uint64_t replay_get_current_step(void)
64 return cpu_get_icount_raw();
67 int replay_get_instructions(void)
71 if (replay_next_event_is(EVENT_INSTRUCTION
)) {
72 res
= replay_state
.instructions_count
;
74 replay_mutex_unlock();
78 void replay_account_executed_instructions(void)
80 if (replay_mode
== REPLAY_MODE_PLAY
) {
82 if (replay_state
.instructions_count
> 0) {
83 int count
= (int)(replay_get_current_step()
84 - replay_state
.current_step
);
85 replay_state
.instructions_count
-= count
;
86 replay_state
.current_step
+= count
;
87 if (replay_state
.instructions_count
== 0) {
88 assert(replay_data_kind
== EVENT_INSTRUCTION
);
89 replay_finish_event();
90 /* Wake up iothread. This is required because
91 timers will not expire until clock counters
92 will be read from the log. */
96 replay_mutex_unlock();
100 bool replay_exception(void)
102 if (replay_mode
== REPLAY_MODE_RECORD
) {
103 replay_save_instructions();
105 replay_put_event(EVENT_EXCEPTION
);
106 replay_mutex_unlock();
108 } else if (replay_mode
== REPLAY_MODE_PLAY
) {
109 bool res
= replay_has_exception();
112 replay_finish_event();
113 replay_mutex_unlock();
121 bool replay_has_exception(void)
124 if (replay_mode
== REPLAY_MODE_PLAY
) {
125 replay_account_executed_instructions();
127 res
= replay_next_event_is(EVENT_EXCEPTION
);
128 replay_mutex_unlock();
134 bool replay_interrupt(void)
136 if (replay_mode
== REPLAY_MODE_RECORD
) {
137 replay_save_instructions();
139 replay_put_event(EVENT_INTERRUPT
);
140 replay_mutex_unlock();
142 } else if (replay_mode
== REPLAY_MODE_PLAY
) {
143 bool res
= replay_has_interrupt();
146 replay_finish_event();
147 replay_mutex_unlock();
155 bool replay_has_interrupt(void)
158 if (replay_mode
== REPLAY_MODE_PLAY
) {
159 replay_account_executed_instructions();
161 res
= replay_next_event_is(EVENT_INTERRUPT
);
162 replay_mutex_unlock();
167 void replay_shutdown_request(void)
169 if (replay_mode
== REPLAY_MODE_RECORD
) {
171 replay_put_event(EVENT_SHUTDOWN
);
172 replay_mutex_unlock();
176 bool replay_checkpoint(ReplayCheckpoint checkpoint
)
179 assert(EVENT_CHECKPOINT
+ checkpoint
<= EVENT_CHECKPOINT_LAST
);
180 replay_save_instructions();
188 if (replay_mode
== REPLAY_MODE_PLAY
) {
189 if (replay_next_event_is(EVENT_CHECKPOINT
+ checkpoint
)) {
190 replay_finish_event();
191 } else if (replay_data_kind
!= EVENT_ASYNC
) {
195 replay_read_events(checkpoint
);
196 /* replay_read_events may leave some unread events.
197 Return false if not all of the events associated with
198 checkpoint were processed */
199 res
= replay_data_kind
!= EVENT_ASYNC
;
200 } else if (replay_mode
== REPLAY_MODE_RECORD
) {
201 replay_put_event(EVENT_CHECKPOINT
+ checkpoint
);
202 replay_save_events(checkpoint
);
206 replay_mutex_unlock();
210 static void replay_enable(const char *fname
, int mode
)
212 const char *fmode
= NULL
;
213 assert(!replay_file
);
216 case REPLAY_MODE_RECORD
:
219 case REPLAY_MODE_PLAY
:
223 fprintf(stderr
, "Replay: internal error: invalid replay mode\n");
227 atexit(replay_finish
);
231 replay_file
= fopen(fname
, fmode
);
232 if (replay_file
== NULL
) {
233 fprintf(stderr
, "Replay: open %s: %s\n", fname
, strerror(errno
));
237 replay_filename
= g_strdup(fname
);
240 replay_data_kind
= -1;
241 replay_state
.instructions_count
= 0;
242 replay_state
.current_step
= 0;
244 /* skip file header for RECORD and check it for PLAY */
245 if (replay_mode
== REPLAY_MODE_RECORD
) {
246 fseek(replay_file
, HEADER_SIZE
, SEEK_SET
);
247 } else if (replay_mode
== REPLAY_MODE_PLAY
) {
248 unsigned int version
= replay_get_dword();
249 if (version
!= REPLAY_VERSION
) {
250 fprintf(stderr
, "Replay: invalid input log file version\n");
253 /* go to the beginning */
254 fseek(replay_file
, HEADER_SIZE
, SEEK_SET
);
255 replay_fetch_data_kind();
258 replay_init_events();
261 void replay_configure(QemuOpts
*opts
)
265 ReplayMode mode
= REPLAY_MODE_NONE
;
273 qemu_opts_loc_restore(opts
);
275 rr
= qemu_opt_get(opts
, "rr");
277 /* Just enabling icount */
279 } else if (!strcmp(rr
, "record")) {
280 mode
= REPLAY_MODE_RECORD
;
281 } else if (!strcmp(rr
, "replay")) {
282 mode
= REPLAY_MODE_PLAY
;
284 error_report("Invalid icount rr option: %s", rr
);
288 fname
= qemu_opt_get(opts
, "rrfile");
290 error_report("File name not specified for replay");
294 replay_enable(fname
, mode
);
300 void replay_start(void)
302 if (replay_mode
== REPLAY_MODE_NONE
) {
306 if (replay_blockers
) {
307 error_reportf_err(replay_blockers
->data
, "Record/replay: ");
311 error_report("Please enable icount to use record/replay");
315 /* Timer for snapshotting will be set up here. */
317 replay_enable_events();
320 void replay_finish(void)
322 if (replay_mode
== REPLAY_MODE_NONE
) {
326 replay_save_instructions();
328 /* finalize the file */
330 if (replay_mode
== REPLAY_MODE_RECORD
) {
331 /* write end event */
332 replay_put_event(EVENT_END
);
335 fseek(replay_file
, 0, SEEK_SET
);
336 replay_put_dword(REPLAY_VERSION
);
342 if (replay_filename
) {
343 g_free(replay_filename
);
344 replay_filename
= NULL
;
347 replay_finish_events();
348 replay_mutex_destroy();
351 void replay_add_blocker(Error
*reason
)
353 replay_blockers
= g_slist_prepend(replay_blockers
, reason
);