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/cpu-timers.h"
15 #include "sysemu/replay.h"
16 #include "sysemu/runstate.h"
17 #include "replay-internal.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 0xe0200c
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 /* Replay breakpoints */
38 uint64_t replay_break_icount
= -1ULL;
39 QEMUTimer
*replay_break_timer
;
41 bool replay_next_event_is(int event
)
45 /* nothing to skip - not all instructions used */
46 if (replay_state
.instruction_count
!= 0) {
47 assert(replay_state
.data_kind
== EVENT_INSTRUCTION
);
48 return event
== EVENT_INSTRUCTION
;
52 unsigned int data_kind
= replay_state
.data_kind
;
53 if (event
== data_kind
) {
57 case EVENT_SHUTDOWN
... EVENT_SHUTDOWN_LAST
:
58 replay_finish_event();
59 qemu_system_shutdown_request(data_kind
- EVENT_SHUTDOWN
);
62 /* clock, time_t, checkpoint and other events */
69 uint64_t replay_get_current_icount(void)
71 return icount_get_raw();
74 int replay_get_instructions(void)
77 g_assert(replay_mutex_locked());
78 if (replay_next_event_is(EVENT_INSTRUCTION
)) {
79 res
= replay_state
.instruction_count
;
80 if (replay_break_icount
!= -1LL) {
81 uint64_t current
= replay_get_current_icount();
82 assert(replay_break_icount
>= current
);
83 if (current
+ res
> replay_break_icount
) {
84 res
= replay_break_icount
- current
;
91 void replay_account_executed_instructions(void)
93 if (replay_mode
== REPLAY_MODE_PLAY
) {
94 g_assert(replay_mutex_locked());
95 if (replay_state
.instruction_count
> 0) {
96 replay_advance_current_icount(replay_get_current_icount());
101 bool replay_exception(void)
104 if (replay_mode
== REPLAY_MODE_RECORD
) {
105 g_assert(replay_mutex_locked());
106 replay_save_instructions();
107 replay_put_event(EVENT_EXCEPTION
);
109 } else if (replay_mode
== REPLAY_MODE_PLAY
) {
110 g_assert(replay_mutex_locked());
111 bool res
= replay_has_exception();
113 replay_finish_event();
121 bool replay_has_exception(void)
124 if (replay_mode
== REPLAY_MODE_PLAY
) {
125 g_assert(replay_mutex_locked());
126 replay_account_executed_instructions();
127 res
= replay_next_event_is(EVENT_EXCEPTION
);
133 bool replay_interrupt(void)
135 if (replay_mode
== REPLAY_MODE_RECORD
) {
136 g_assert(replay_mutex_locked());
137 replay_save_instructions();
138 replay_put_event(EVENT_INTERRUPT
);
140 } else if (replay_mode
== REPLAY_MODE_PLAY
) {
141 g_assert(replay_mutex_locked());
142 bool res
= replay_has_interrupt();
144 replay_finish_event();
152 bool replay_has_interrupt(void)
155 if (replay_mode
== REPLAY_MODE_PLAY
) {
156 g_assert(replay_mutex_locked());
157 replay_account_executed_instructions();
158 res
= replay_next_event_is(EVENT_INTERRUPT
);
163 void replay_shutdown_request(ShutdownCause cause
)
165 if (replay_mode
== REPLAY_MODE_RECORD
) {
166 g_assert(replay_mutex_locked());
167 replay_put_event(EVENT_SHUTDOWN
+ cause
);
171 bool replay_checkpoint(ReplayCheckpoint checkpoint
)
173 assert(EVENT_CHECKPOINT
+ checkpoint
<= EVENT_CHECKPOINT_LAST
);
175 replay_save_instructions();
177 if (replay_mode
== REPLAY_MODE_PLAY
) {
178 g_assert(replay_mutex_locked());
179 if (replay_next_event_is(EVENT_CHECKPOINT
+ checkpoint
)) {
180 replay_finish_event();
184 } else if (replay_mode
== REPLAY_MODE_RECORD
) {
185 g_assert(replay_mutex_locked());
186 replay_put_event(EVENT_CHECKPOINT
+ checkpoint
);
191 void replay_async_events(void)
193 static bool processing
= false;
195 * If we are already processing the events, recursion may occur
196 * in case of incorrect implementation when HW event modifies timers.
197 * Timer modification may invoke the icount warp, event processing,
198 * and cause the recursion.
200 g_assert(!processing
);
203 replay_save_instructions();
205 if (replay_mode
== REPLAY_MODE_PLAY
) {
206 g_assert(replay_mutex_locked());
207 replay_read_events();
208 } else if (replay_mode
== REPLAY_MODE_RECORD
) {
209 g_assert(replay_mutex_locked());
210 replay_save_events();
215 bool replay_has_event(void)
218 if (replay_mode
== REPLAY_MODE_PLAY
) {
219 g_assert(replay_mutex_locked());
220 replay_account_executed_instructions();
221 res
= EVENT_CHECKPOINT
<= replay_state
.data_kind
222 && replay_state
.data_kind
<= EVENT_CHECKPOINT_LAST
;
223 res
= res
|| (EVENT_ASYNC
<= replay_state
.data_kind
224 && replay_state
.data_kind
<= EVENT_ASYNC_LAST
);
229 static void replay_enable(const char *fname
, int mode
)
231 const char *fmode
= NULL
;
232 assert(!replay_file
);
235 case REPLAY_MODE_RECORD
:
238 case REPLAY_MODE_PLAY
:
242 fprintf(stderr
, "Replay: internal error: invalid replay mode\n");
246 atexit(replay_finish
);
248 replay_file
= fopen(fname
, fmode
);
249 if (replay_file
== NULL
) {
250 fprintf(stderr
, "Replay: open %s: %s\n", fname
, strerror(errno
));
254 replay_filename
= g_strdup(fname
);
258 replay_state
.data_kind
= -1;
259 replay_state
.instruction_count
= 0;
260 replay_state
.current_icount
= 0;
261 replay_state
.has_unread_data
= 0;
263 /* skip file header for RECORD and check it for PLAY */
264 if (replay_mode
== REPLAY_MODE_RECORD
) {
265 fseek(replay_file
, HEADER_SIZE
, SEEK_SET
);
266 } else if (replay_mode
== REPLAY_MODE_PLAY
) {
267 unsigned int version
= replay_get_dword();
268 if (version
!= REPLAY_VERSION
) {
269 fprintf(stderr
, "Replay: invalid input log file version\n");
272 /* go to the beginning */
273 fseek(replay_file
, HEADER_SIZE
, SEEK_SET
);
274 replay_fetch_data_kind();
277 replay_init_events();
280 void replay_configure(QemuOpts
*opts
)
284 ReplayMode mode
= REPLAY_MODE_NONE
;
292 qemu_opts_loc_restore(opts
);
294 rr
= qemu_opt_get(opts
, "rr");
296 /* Just enabling icount */
298 } else if (!strcmp(rr
, "record")) {
299 mode
= REPLAY_MODE_RECORD
;
300 } else if (!strcmp(rr
, "replay")) {
301 mode
= REPLAY_MODE_PLAY
;
303 error_report("Invalid icount rr option: %s", rr
);
307 fname
= qemu_opt_get(opts
, "rrfile");
309 error_report("File name not specified for replay");
313 replay_snapshot
= g_strdup(qemu_opt_get(opts
, "rrsnapshot"));
314 replay_vmstate_register();
315 replay_enable(fname
, mode
);
321 void replay_start(void)
323 if (replay_mode
== REPLAY_MODE_NONE
) {
327 if (replay_blockers
) {
328 error_reportf_err(replay_blockers
->data
, "Record/replay: ");
331 if (!icount_enabled()) {
332 error_report("Please enable icount to use record/replay");
336 /* Timer for snapshotting will be set up here. */
338 replay_enable_events();
341 void replay_finish(void)
343 if (replay_mode
== REPLAY_MODE_NONE
) {
347 replay_save_instructions();
349 /* finalize the file */
351 if (replay_mode
== REPLAY_MODE_RECORD
) {
353 * Can't do it in the signal handler, therefore
354 * add shutdown event here for the case of Ctrl-C.
356 replay_shutdown_request(SHUTDOWN_CAUSE_HOST_SIGNAL
);
357 /* write end event */
358 replay_put_event(EVENT_END
);
361 fseek(replay_file
, 0, SEEK_SET
);
362 replay_put_dword(REPLAY_VERSION
);
368 g_free(replay_filename
);
369 replay_filename
= NULL
;
371 g_free(replay_snapshot
);
372 replay_snapshot
= NULL
;
374 replay_finish_events();
375 replay_mode
= REPLAY_MODE_NONE
;
378 void replay_add_blocker(const char *feature
)
380 Error
*reason
= NULL
;
382 error_setg(&reason
, "Record/replay feature is not supported for '%s'",
384 replay_blockers
= g_slist_prepend(replay_blockers
, reason
);
387 const char *replay_get_filename(void)
389 return replay_filename
;