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;
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
/* Pretty print event names */
43
static const char *replay_async_event_name(ReplayAsyncEventKind event)
46
#define ASYNC_EVENT(_x) case REPLAY_ASYNC_EVENT_ ## _x: return "ASYNC_EVENT_"#_x
48
ASYNC_EVENT(BH_ONESHOT);
50
ASYNC_EVENT(INPUT_SYNC);
51
ASYNC_EVENT(CHAR_READ);
56
g_assert_not_reached();
60
static const char *replay_clock_event_name(ReplayClockKind clock)
63
#define CLOCK_EVENT(_x) case REPLAY_CLOCK_ ## _x: return "CLOCK_" #_x
65
CLOCK_EVENT(VIRTUAL_RT);
68
g_assert_not_reached();
72
/* Pretty print shutdown event names */
73
static const char *replay_shutdown_event_name(ShutdownCause cause)
76
#define SHUTDOWN_EVENT(_x) case SHUTDOWN_CAUSE_ ## _x: return "SHUTDOWN_CAUSE_" #_x
78
SHUTDOWN_EVENT(HOST_ERROR);
79
SHUTDOWN_EVENT(HOST_QMP_QUIT);
80
SHUTDOWN_EVENT(HOST_QMP_SYSTEM_RESET);
81
SHUTDOWN_EVENT(HOST_SIGNAL);
82
SHUTDOWN_EVENT(HOST_UI);
83
SHUTDOWN_EVENT(GUEST_SHUTDOWN);
84
SHUTDOWN_EVENT(GUEST_RESET);
85
SHUTDOWN_EVENT(GUEST_PANIC);
86
SHUTDOWN_EVENT(SUBSYSTEM_RESET);
87
SHUTDOWN_EVENT(SNAPSHOT_LOAD);
90
g_assert_not_reached();
94
static const char *replay_checkpoint_event_name(enum ReplayCheckpoint checkpoint)
97
#define CHECKPOINT_EVENT(_x) case CHECKPOINT_ ## _x: return "CHECKPOINT_" #_x
98
CHECKPOINT_EVENT(CLOCK_WARP_START);
99
CHECKPOINT_EVENT(CLOCK_WARP_ACCOUNT);
100
CHECKPOINT_EVENT(RESET_REQUESTED);
101
CHECKPOINT_EVENT(SUSPEND_REQUESTED);
102
CHECKPOINT_EVENT(CLOCK_VIRTUAL);
103
CHECKPOINT_EVENT(CLOCK_HOST);
104
CHECKPOINT_EVENT(CLOCK_VIRTUAL_RT);
105
CHECKPOINT_EVENT(INIT);
106
CHECKPOINT_EVENT(RESET);
107
#undef CHECKPOINT_EVENT
109
g_assert_not_reached();
113
static const char *replay_event_name(enum ReplayEvents event)
115
/* First deal with the simple ones */
117
#define EVENT(_x) case EVENT_ ## _x: return "EVENT_"#_x
122
EVENT(CHAR_READ_ALL);
128
if (event >= EVENT_ASYNC && event <= EVENT_ASYNC_LAST) {
129
return replay_async_event_name(event - EVENT_ASYNC);
130
} else if (event >= EVENT_SHUTDOWN && event <= EVENT_SHUTDOWN_LAST) {
131
return replay_shutdown_event_name(event - EVENT_SHUTDOWN);
132
} else if (event >= EVENT_CLOCK && event <= EVENT_CLOCK_LAST) {
133
return replay_clock_event_name(event - EVENT_CLOCK);
134
} else if (event >= EVENT_CHECKPOINT && event <= EVENT_CHECKPOINT_LAST) {
135
return replay_checkpoint_event_name(event - EVENT_CHECKPOINT);
139
g_assert_not_reached();
142
bool replay_next_event_is(int event)
146
/* nothing to skip - not all instructions used */
147
if (replay_state.instruction_count != 0) {
148
assert(replay_state.data_kind == EVENT_INSTRUCTION);
149
return event == EVENT_INSTRUCTION;
153
unsigned int data_kind = replay_state.data_kind;
154
if (event == data_kind) {
158
case EVENT_SHUTDOWN ... EVENT_SHUTDOWN_LAST:
159
replay_finish_event();
160
qemu_system_shutdown_request(data_kind - EVENT_SHUTDOWN);
163
/* clock, time_t, checkpoint and other events */
170
uint64_t replay_get_current_icount(void)
172
return icount_get_raw();
175
int replay_get_instructions(void)
178
g_assert(replay_mutex_locked());
179
if (replay_next_event_is(EVENT_INSTRUCTION)) {
180
res = replay_state.instruction_count;
181
if (replay_break_icount != -1LL) {
182
uint64_t current = replay_get_current_icount();
183
assert(replay_break_icount >= current);
184
if (current + res > replay_break_icount) {
185
res = replay_break_icount - current;
192
void replay_account_executed_instructions(void)
194
if (replay_mode == REPLAY_MODE_PLAY) {
195
g_assert(replay_mutex_locked());
196
if (replay_state.instruction_count > 0) {
197
replay_advance_current_icount(replay_get_current_icount());
202
bool replay_exception(void)
205
if (replay_mode == REPLAY_MODE_RECORD) {
206
g_assert(replay_mutex_locked());
207
replay_save_instructions();
208
replay_put_event(EVENT_EXCEPTION);
210
} else if (replay_mode == REPLAY_MODE_PLAY) {
211
g_assert(replay_mutex_locked());
212
bool res = replay_has_exception();
214
replay_finish_event();
222
bool replay_has_exception(void)
225
if (replay_mode == REPLAY_MODE_PLAY) {
226
g_assert(replay_mutex_locked());
227
replay_account_executed_instructions();
228
res = replay_next_event_is(EVENT_EXCEPTION);
234
bool replay_interrupt(void)
236
if (replay_mode == REPLAY_MODE_RECORD) {
237
g_assert(replay_mutex_locked());
238
replay_save_instructions();
239
replay_put_event(EVENT_INTERRUPT);
241
} else if (replay_mode == REPLAY_MODE_PLAY) {
242
g_assert(replay_mutex_locked());
243
bool res = replay_has_interrupt();
245
replay_finish_event();
253
bool replay_has_interrupt(void)
256
if (replay_mode == REPLAY_MODE_PLAY) {
257
g_assert(replay_mutex_locked());
258
replay_account_executed_instructions();
259
res = replay_next_event_is(EVENT_INTERRUPT);
264
void replay_shutdown_request(ShutdownCause cause)
266
if (replay_mode == REPLAY_MODE_RECORD) {
267
g_assert(replay_mutex_locked());
268
replay_put_event(EVENT_SHUTDOWN + cause);
272
bool replay_checkpoint(ReplayCheckpoint checkpoint)
274
assert(EVENT_CHECKPOINT + checkpoint <= EVENT_CHECKPOINT_LAST);
276
replay_save_instructions();
278
if (replay_mode == REPLAY_MODE_PLAY) {
279
g_assert(replay_mutex_locked());
280
if (replay_next_event_is(EVENT_CHECKPOINT + checkpoint)) {
281
replay_finish_event();
285
} else if (replay_mode == REPLAY_MODE_RECORD) {
286
g_assert(replay_mutex_locked());
287
replay_put_event(EVENT_CHECKPOINT + checkpoint);
292
void replay_async_events(void)
294
static bool processing = false;
296
* If we are already processing the events, recursion may occur
297
* in case of incorrect implementation when HW event modifies timers.
298
* Timer modification may invoke the icount warp, event processing,
299
* and cause the recursion.
301
g_assert(!processing);
304
replay_save_instructions();
306
if (replay_mode == REPLAY_MODE_PLAY) {
307
g_assert(replay_mutex_locked());
308
replay_read_events();
309
} else if (replay_mode == REPLAY_MODE_RECORD) {
310
g_assert(replay_mutex_locked());
311
replay_save_events();
316
bool replay_has_event(void)
319
if (replay_mode == REPLAY_MODE_PLAY) {
320
g_assert(replay_mutex_locked());
321
replay_account_executed_instructions();
322
res = EVENT_CHECKPOINT <= replay_state.data_kind
323
&& replay_state.data_kind <= EVENT_CHECKPOINT_LAST;
324
res = res || (EVENT_ASYNC <= replay_state.data_kind
325
&& replay_state.data_kind <= EVENT_ASYNC_LAST);
330
G_NORETURN void replay_sync_error(const char *error)
332
error_report("%s (insn total %"PRId64"/%d left, event %d is %s)", error,
333
replay_state.current_icount, replay_state.instruction_count,
334
replay_state.current_event,
335
replay_event_name(replay_state.data_kind));
339
static void replay_enable(const char *fname, int mode)
341
const char *fmode = NULL;
342
assert(!replay_file);
345
case REPLAY_MODE_RECORD:
348
case REPLAY_MODE_PLAY:
352
fprintf(stderr, "Replay: internal error: invalid replay mode\n");
356
atexit(replay_finish);
358
replay_file = fopen(fname, fmode);
359
if (replay_file == NULL) {
360
fprintf(stderr, "Replay: open %s: %s\n", fname, strerror(errno));
364
replay_filename = g_strdup(fname);
368
replay_state.data_kind = -1;
369
replay_state.instruction_count = 0;
370
replay_state.current_icount = 0;
371
replay_state.current_event = 0;
372
replay_state.has_unread_data = 0;
374
/* skip file header for RECORD and check it for PLAY */
375
if (replay_mode == REPLAY_MODE_RECORD) {
376
fseek(replay_file, HEADER_SIZE, SEEK_SET);
377
} else if (replay_mode == REPLAY_MODE_PLAY) {
378
unsigned int version = replay_get_dword();
379
if (version != REPLAY_VERSION) {
380
fprintf(stderr, "Replay: invalid input log file version\n");
383
/* go to the beginning */
384
fseek(replay_file, HEADER_SIZE, SEEK_SET);
385
replay_fetch_data_kind();
388
runstate_replay_enable();
390
replay_init_events();
393
void replay_configure(QemuOpts *opts)
397
ReplayMode mode = REPLAY_MODE_NONE;
405
qemu_opts_loc_restore(opts);
407
rr = qemu_opt_get(opts, "rr");
409
/* Just enabling icount */
411
} else if (!strcmp(rr, "record")) {
412
mode = REPLAY_MODE_RECORD;
413
} else if (!strcmp(rr, "replay")) {
414
mode = REPLAY_MODE_PLAY;
416
error_report("Invalid icount rr option: %s", rr);
420
fname = qemu_opt_get(opts, "rrfile");
422
error_report("File name not specified for replay");
426
replay_snapshot = g_strdup(qemu_opt_get(opts, "rrsnapshot"));
427
replay_vmstate_register();
428
replay_enable(fname, mode);
434
void replay_start(void)
436
if (replay_mode == REPLAY_MODE_NONE) {
440
if (replay_blockers) {
441
error_reportf_err(replay_blockers->data, "Record/replay: ");
444
if (!icount_enabled()) {
445
error_report("Please enable icount to use record/replay");
449
/* Timer for snapshotting will be set up here. */
451
replay_enable_events();
454
void replay_finish(void)
456
if (replay_mode == REPLAY_MODE_NONE) {
460
replay_save_instructions();
462
/* finalize the file */
464
if (replay_mode == REPLAY_MODE_RECORD) {
466
* Can't do it in the signal handler, therefore
467
* add shutdown event here for the case of Ctrl-C.
469
replay_shutdown_request(SHUTDOWN_CAUSE_HOST_SIGNAL);
470
/* write end event */
471
replay_put_event(EVENT_END);
474
fseek(replay_file, 0, SEEK_SET);
475
replay_put_dword(REPLAY_VERSION);
481
g_free(replay_filename);
482
replay_filename = NULL;
484
g_free(replay_snapshot);
485
replay_snapshot = NULL;
487
replay_finish_events();
488
replay_mode = REPLAY_MODE_NONE;
491
void replay_add_blocker(const char *feature)
493
Error *reason = NULL;
495
error_setg(&reason, "Record/replay is not supported with %s",
497
replay_blockers = g_slist_prepend(replay_blockers, reason);
500
const char *replay_get_filename(void)
502
return replay_filename;