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 "sysemu/runstate.h"
16 #include "replay-internal.h"
17 #include "qemu/timer.h"
18 #include "qemu/main-loop.h"
19 #include "qemu/option.h"
20 #include "sysemu/cpus.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 0xe0200a
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
.instruction_count
!= 0) {
43 assert(replay_state
.data_kind
== EVENT_INSTRUCTION
);
44 return event
== EVENT_INSTRUCTION
;
48 unsigned int data_kind
= replay_state
.data_kind
;
49 if (event
== data_kind
) {
53 case EVENT_SHUTDOWN
... EVENT_SHUTDOWN_LAST
:
54 replay_finish_event();
55 qemu_system_shutdown_request(data_kind
- EVENT_SHUTDOWN
);
58 /* clock, time_t, checkpoint and other events */
65 uint64_t replay_get_current_icount(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
.instruction_count
;
77 replay_mutex_unlock();
81 void replay_account_executed_instructions(void)
83 if (replay_mode
== REPLAY_MODE_PLAY
) {
84 g_assert(replay_mutex_locked());
85 if (replay_state
.instruction_count
> 0) {
86 int count
= (int)(replay_get_current_icount()
87 - replay_state
.current_icount
);
89 /* Time can only go forward */
92 replay_state
.instruction_count
-= count
;
93 replay_state
.current_icount
+= count
;
94 if (replay_state
.instruction_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. */
106 bool replay_exception(void)
109 if (replay_mode
== REPLAY_MODE_RECORD
) {
110 g_assert(replay_mutex_locked());
111 replay_save_instructions();
112 replay_put_event(EVENT_EXCEPTION
);
114 } else if (replay_mode
== REPLAY_MODE_PLAY
) {
115 g_assert(replay_mutex_locked());
116 bool res
= replay_has_exception();
118 replay_finish_event();
126 bool replay_has_exception(void)
129 if (replay_mode
== REPLAY_MODE_PLAY
) {
130 g_assert(replay_mutex_locked());
131 replay_account_executed_instructions();
132 res
= replay_next_event_is(EVENT_EXCEPTION
);
138 bool replay_interrupt(void)
140 if (replay_mode
== REPLAY_MODE_RECORD
) {
141 g_assert(replay_mutex_locked());
142 replay_save_instructions();
143 replay_put_event(EVENT_INTERRUPT
);
145 } else if (replay_mode
== REPLAY_MODE_PLAY
) {
146 g_assert(replay_mutex_locked());
147 bool res
= replay_has_interrupt();
149 replay_finish_event();
157 bool replay_has_interrupt(void)
160 if (replay_mode
== REPLAY_MODE_PLAY
) {
161 g_assert(replay_mutex_locked());
162 replay_account_executed_instructions();
163 res
= replay_next_event_is(EVENT_INTERRUPT
);
168 void replay_shutdown_request(ShutdownCause cause
)
170 if (replay_mode
== REPLAY_MODE_RECORD
) {
171 g_assert(replay_mutex_locked());
172 replay_put_event(EVENT_SHUTDOWN
+ cause
);
176 bool replay_checkpoint(ReplayCheckpoint checkpoint
)
179 static bool in_checkpoint
;
180 assert(EVENT_CHECKPOINT
+ checkpoint
<= EVENT_CHECKPOINT_LAST
);
187 /* If we are already in checkpoint, then there is no need
188 for additional synchronization.
189 Recursion occurs when HW event modifies timers.
190 Timer modification may invoke the checkpoint and
191 proceed to recursion. */
194 in_checkpoint
= true;
196 replay_save_instructions();
198 if (replay_mode
== REPLAY_MODE_PLAY
) {
199 g_assert(replay_mutex_locked());
200 if (replay_next_event_is(EVENT_CHECKPOINT
+ checkpoint
)) {
201 replay_finish_event();
202 } else if (replay_state
.data_kind
!= EVENT_ASYNC
) {
206 replay_read_events(checkpoint
);
207 /* replay_read_events may leave some unread events.
208 Return false if not all of the events associated with
209 checkpoint were processed */
210 res
= replay_state
.data_kind
!= EVENT_ASYNC
;
211 } else if (replay_mode
== REPLAY_MODE_RECORD
) {
212 g_assert(replay_mutex_locked());
213 replay_put_event(EVENT_CHECKPOINT
+ checkpoint
);
214 /* This checkpoint belongs to several threads.
215 Processing events from different threads is
217 if (checkpoint
!= CHECKPOINT_CLOCK_WARP_START
218 /* FIXME: this is temporary fix, other checkpoints
219 may also be invoked from the different threads someday.
220 Asynchronous event processing should be refactored
221 to create additional replay event kind which is
222 nailed to the one of the threads and which processes
224 && checkpoint
!= CHECKPOINT_CLOCK_VIRTUAL
) {
225 replay_save_events(checkpoint
);
230 in_checkpoint
= false;
234 bool replay_has_checkpoint(void)
237 if (replay_mode
== REPLAY_MODE_PLAY
) {
238 g_assert(replay_mutex_locked());
239 replay_account_executed_instructions();
240 res
= EVENT_CHECKPOINT
<= replay_state
.data_kind
241 && replay_state
.data_kind
<= EVENT_CHECKPOINT_LAST
;
246 static void replay_enable(const char *fname
, int mode
)
248 const char *fmode
= NULL
;
249 assert(!replay_file
);
252 case REPLAY_MODE_RECORD
:
255 case REPLAY_MODE_PLAY
:
259 fprintf(stderr
, "Replay: internal error: invalid replay mode\n");
263 atexit(replay_finish
);
265 replay_file
= fopen(fname
, fmode
);
266 if (replay_file
== NULL
) {
267 fprintf(stderr
, "Replay: open %s: %s\n", fname
, strerror(errno
));
271 replay_filename
= g_strdup(fname
);
275 replay_state
.data_kind
= -1;
276 replay_state
.instruction_count
= 0;
277 replay_state
.current_icount
= 0;
278 replay_state
.has_unread_data
= 0;
280 /* skip file header for RECORD and check it for PLAY */
281 if (replay_mode
== REPLAY_MODE_RECORD
) {
282 fseek(replay_file
, HEADER_SIZE
, SEEK_SET
);
283 } else if (replay_mode
== REPLAY_MODE_PLAY
) {
284 unsigned int version
= replay_get_dword();
285 if (version
!= REPLAY_VERSION
) {
286 fprintf(stderr
, "Replay: invalid input log file version\n");
289 /* go to the beginning */
290 fseek(replay_file
, HEADER_SIZE
, SEEK_SET
);
291 replay_fetch_data_kind();
294 replay_init_events();
297 void replay_configure(QemuOpts
*opts
)
301 ReplayMode mode
= REPLAY_MODE_NONE
;
309 qemu_opts_loc_restore(opts
);
311 rr
= qemu_opt_get(opts
, "rr");
313 /* Just enabling icount */
315 } else if (!strcmp(rr
, "record")) {
316 mode
= REPLAY_MODE_RECORD
;
317 } else if (!strcmp(rr
, "replay")) {
318 mode
= REPLAY_MODE_PLAY
;
320 error_report("Invalid icount rr option: %s", rr
);
324 fname
= qemu_opt_get(opts
, "rrfile");
326 error_report("File name not specified for replay");
330 replay_snapshot
= g_strdup(qemu_opt_get(opts
, "rrsnapshot"));
331 replay_vmstate_register();
332 replay_enable(fname
, mode
);
338 void replay_start(void)
340 if (replay_mode
== REPLAY_MODE_NONE
) {
344 if (replay_blockers
) {
345 error_reportf_err(replay_blockers
->data
, "Record/replay: ");
349 error_report("Please enable icount to use record/replay");
353 /* Timer for snapshotting will be set up here. */
355 replay_enable_events();
358 void replay_finish(void)
360 if (replay_mode
== REPLAY_MODE_NONE
) {
364 replay_save_instructions();
366 /* finalize the file */
368 if (replay_mode
== REPLAY_MODE_RECORD
) {
370 * Can't do it in the signal handler, therefore
371 * add shutdown event here for the case of Ctrl-C.
373 replay_shutdown_request(SHUTDOWN_CAUSE_HOST_SIGNAL
);
374 /* write end event */
375 replay_put_event(EVENT_END
);
378 fseek(replay_file
, 0, SEEK_SET
);
379 replay_put_dword(REPLAY_VERSION
);
385 if (replay_filename
) {
386 g_free(replay_filename
);
387 replay_filename
= NULL
;
390 g_free(replay_snapshot
);
391 replay_snapshot
= NULL
;
393 replay_mode
= REPLAY_MODE_NONE
;
395 replay_finish_events();
398 void replay_add_blocker(Error
*reason
)
400 replay_blockers
= g_slist_prepend(replay_blockers
, reason
);