master
c 505 lines 13.2 KB
Raw
1 /*
2 * replay.c
3 *
4 * Copyright (c) 2010-2015 Institute for System Programming
5 * of the Russian Academy of Sciences.
6 *
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.
9 *
10 */
11
12 #include "qemu/osdep.h"
13 #include "qapi/error.h"
14 #include "exec/icount.h"
15 #include "system/replay.h"
16 #include "system/runstate.h"
17 #include "replay-internal.h"
18 #include "qemu/main-loop.h"
19 #include "qemu/option.h"
20 #include "system/cpus.h"
21 #include "qemu/error-report.h"
22
23 /* Current version of the replay mechanism.
24 Increase it when file format changes. */
25 #define REPLAY_VERSION 0xe0200e
26 /* Size of replay log header */
27 #define HEADER_SIZE (sizeof(uint32_t) + sizeof(uint64_t))
28
29 ReplayMode replay_mode = REPLAY_MODE_NONE;
30 char *replay_snapshot;
31
32 /* Name of replay file */
33 static char *replay_filename;
34 ReplayState replay_state;
35 static GSList *replay_blockers;
36
37 /* Replay breakpoints */
38 uint64_t replay_break_icount = -1ULL;
39 QEMUTimer *replay_break_timer;
40
41 /* Pretty print event names */
42
43 static const char *replay_async_event_name(ReplayAsyncEventKind event)
44 {
45 switch (event) {
46 #define ASYNC_EVENT(_x) case REPLAY_ASYNC_EVENT_ ## _x: return "ASYNC_EVENT_"#_x
47 ASYNC_EVENT(BH);
48 ASYNC_EVENT(BH_ONESHOT);
49 ASYNC_EVENT(INPUT);
50 ASYNC_EVENT(INPUT_SYNC);
51 ASYNC_EVENT(CHAR_READ);
52 ASYNC_EVENT(BLOCK);
53 ASYNC_EVENT(NET);
54 #undef ASYNC_EVENT
55 default:
56 g_assert_not_reached();
57 }
58 }
59
60 static const char *replay_clock_event_name(ReplayClockKind clock)
61 {
62 switch (clock) {
63 #define CLOCK_EVENT(_x) case REPLAY_CLOCK_ ## _x: return "CLOCK_" #_x
64 CLOCK_EVENT(HOST);
65 CLOCK_EVENT(VIRTUAL_RT);
66 #undef CLOCK_EVENT
67 default:
68 g_assert_not_reached();
69 }
70 }
71
72 /* Pretty print shutdown event names */
73 static const char *replay_shutdown_event_name(ShutdownCause cause)
74 {
75 switch (cause) {
76 #define SHUTDOWN_EVENT(_x) case SHUTDOWN_CAUSE_ ## _x: return "SHUTDOWN_CAUSE_" #_x
77 SHUTDOWN_EVENT(NONE);
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);
88 #undef SHUTDOWN_EVENT
89 default:
90 g_assert_not_reached();
91 }
92 }
93
94 static const char *replay_checkpoint_event_name(enum ReplayCheckpoint checkpoint)
95 {
96 switch (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
108 default:
109 g_assert_not_reached();
110 }
111 }
112
113 static const char *replay_event_name(enum ReplayEvents event)
114 {
115 /* First deal with the simple ones */
116 switch (event) {
117 #define EVENT(_x) case EVENT_ ## _x: return "EVENT_"#_x
118 EVENT(INSTRUCTION);
119 EVENT(INTERRUPT);
120 EVENT(EXCEPTION);
121 EVENT(CHAR_WRITE);
122 EVENT(CHAR_READ_ALL);
123 EVENT(AUDIO_OUT);
124 EVENT(AUDIO_IN);
125 EVENT(RANDOM);
126 #undef EVENT
127 default:
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);
136 }
137 }
138
139 g_assert_not_reached();
140 }
141
142 bool replay_next_event_is(int event)
143 {
144 bool res = false;
145
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;
150 }
151
152 while (true) {
153 unsigned int data_kind = replay_state.data_kind;
154 if (event == data_kind) {
155 res = true;
156 }
157 switch (data_kind) {
158 case EVENT_SHUTDOWN ... EVENT_SHUTDOWN_LAST:
159 replay_finish_event();
160 qemu_system_shutdown_request(data_kind - EVENT_SHUTDOWN);
161 break;
162 default:
163 /* clock, time_t, checkpoint and other events */
164 return res;
165 }
166 }
167 return res;
168 }
169
170 uint64_t replay_get_current_icount(void)
171 {
172 return icount_get_raw();
173 }
174
175 int replay_get_instructions(void)
176 {
177 int res = 0;
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;
186 }
187 }
188 }
189 return res;
190 }
191
192 void replay_account_executed_instructions(void)
193 {
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());
198 }
199 }
200 }
201
202 bool replay_exception(void)
203 {
204
205 if (replay_mode == REPLAY_MODE_RECORD) {
206 g_assert(replay_mutex_locked());
207 replay_save_instructions();
208 replay_put_event(EVENT_EXCEPTION);
209 return true;
210 } else if (replay_mode == REPLAY_MODE_PLAY) {
211 g_assert(replay_mutex_locked());
212 bool res = replay_has_exception();
213 if (res) {
214 replay_finish_event();
215 }
216 return res;
217 }
218
219 return true;
220 }
221
222 bool replay_has_exception(void)
223 {
224 bool res = false;
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);
229 }
230
231 return res;
232 }
233
234 bool replay_interrupt(void)
235 {
236 if (replay_mode == REPLAY_MODE_RECORD) {
237 g_assert(replay_mutex_locked());
238 replay_save_instructions();
239 replay_put_event(EVENT_INTERRUPT);
240 return true;
241 } else if (replay_mode == REPLAY_MODE_PLAY) {
242 g_assert(replay_mutex_locked());
243 bool res = replay_has_interrupt();
244 if (res) {
245 replay_finish_event();
246 }
247 return res;
248 }
249
250 return true;
251 }
252
253 bool replay_has_interrupt(void)
254 {
255 bool res = false;
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);
260 }
261 return res;
262 }
263
264 void replay_shutdown_request(ShutdownCause cause)
265 {
266 replay_save_instructions();
267
268 if (replay_mode == REPLAY_MODE_RECORD) {
269 g_assert(replay_mutex_locked());
270 replay_put_event(EVENT_SHUTDOWN + cause);
271 }
272 }
273
274 bool replay_checkpoint(ReplayCheckpoint checkpoint)
275 {
276 assert(EVENT_CHECKPOINT + checkpoint <= EVENT_CHECKPOINT_LAST);
277
278 replay_save_instructions();
279
280 if (replay_mode == REPLAY_MODE_PLAY) {
281 g_assert(replay_mutex_locked());
282 if (replay_next_event_is(EVENT_CHECKPOINT + checkpoint)) {
283 replay_finish_event();
284 } else {
285 return false;
286 }
287 } else if (replay_mode == REPLAY_MODE_RECORD) {
288 g_assert(replay_mutex_locked());
289 replay_put_event(EVENT_CHECKPOINT + checkpoint);
290 }
291 return true;
292 }
293
294 void replay_async_events(void)
295 {
296 static bool processing = false;
297 /*
298 * If we are already processing the events, recursion may occur
299 * in case of incorrect implementation when HW event modifies timers.
300 * Timer modification may invoke the icount warp, event processing,
301 * and cause the recursion.
302 */
303 g_assert(!processing);
304 processing = true;
305
306 replay_save_instructions();
307
308 if (replay_mode == REPLAY_MODE_PLAY) {
309 g_assert(replay_mutex_locked());
310 replay_read_events();
311 } else if (replay_mode == REPLAY_MODE_RECORD) {
312 g_assert(replay_mutex_locked());
313 replay_save_events();
314 }
315 processing = false;
316 }
317
318 bool replay_has_event(void)
319 {
320 bool res = false;
321 if (replay_mode == REPLAY_MODE_PLAY) {
322 g_assert(replay_mutex_locked());
323 replay_account_executed_instructions();
324 res = EVENT_CHECKPOINT <= replay_state.data_kind
325 && replay_state.data_kind <= EVENT_CHECKPOINT_LAST;
326 res = res || (EVENT_ASYNC <= replay_state.data_kind
327 && replay_state.data_kind <= EVENT_ASYNC_LAST);
328 }
329 return res;
330 }
331
332 G_NORETURN void replay_sync_error(const char *error)
333 {
334 error_report("%s (insn total %"PRId64"/%d left, event %d is %s)", error,
335 replay_state.current_icount, replay_state.instruction_count,
336 replay_state.current_event,
337 replay_event_name(replay_state.data_kind));
338 abort();
339 }
340
341 static void replay_enable(const char *fname, int mode)
342 {
343 const char *fmode = NULL;
344 assert(!replay_file);
345
346 switch (mode) {
347 case REPLAY_MODE_RECORD:
348 fmode = "wb";
349 break;
350 case REPLAY_MODE_PLAY:
351 fmode = "rb";
352 break;
353 default:
354 fprintf(stderr, "Replay: internal error: invalid replay mode\n");
355 exit(1);
356 }
357
358 atexit(replay_finish);
359
360 replay_file = fopen(fname, fmode);
361 if (replay_file == NULL) {
362 fprintf(stderr, "Replay: open %s: %s\n", fname, strerror(errno));
363 exit(1);
364 }
365
366 replay_filename = g_strdup(fname);
367 replay_mode = mode;
368 replay_mutex_init();
369
370 replay_state.data_kind = -1;
371 replay_state.instruction_count = 0;
372 replay_state.current_icount = 0;
373 replay_state.current_event = 0;
374 replay_state.has_unread_data = 0;
375
376 /* skip file header for RECORD and check it for PLAY */
377 if (replay_mode == REPLAY_MODE_RECORD) {
378 fseek(replay_file, HEADER_SIZE, SEEK_SET);
379 } else if (replay_mode == REPLAY_MODE_PLAY) {
380 unsigned int version = replay_get_dword();
381 if (version != REPLAY_VERSION) {
382 fprintf(stderr, "Replay: invalid input log file version\n");
383 exit(1);
384 }
385 /* go to the beginning */
386 fseek(replay_file, HEADER_SIZE, SEEK_SET);
387 replay_fetch_data_kind();
388 }
389
390 runstate_replay_enable();
391
392 replay_init_events();
393 }
394
395 void replay_configure(QemuOpts *opts)
396 {
397 const char *fname;
398 const char *rr;
399 ReplayMode mode = REPLAY_MODE_NONE;
400 Location loc;
401
402 if (!opts) {
403 return;
404 }
405
406 loc_push_none(&loc);
407 qemu_opts_loc_restore(opts);
408
409 rr = qemu_opt_get(opts, "rr");
410 if (!rr) {
411 /* Just enabling icount */
412 goto out;
413 } else if (!strcmp(rr, "record")) {
414 mode = REPLAY_MODE_RECORD;
415 } else if (!strcmp(rr, "replay")) {
416 mode = REPLAY_MODE_PLAY;
417 } else {
418 error_report("Invalid icount rr option: %s", rr);
419 exit(1);
420 }
421
422 fname = qemu_opt_get(opts, "rrfile");
423 if (!fname) {
424 error_report("File name not specified for replay");
425 exit(1);
426 }
427
428 replay_snapshot = g_strdup(qemu_opt_get(opts, "rrsnapshot"));
429 replay_vmstate_register();
430 replay_enable(fname, mode);
431
432 out:
433 loc_pop(&loc);
434 }
435
436 void replay_start(void)
437 {
438 if (replay_mode == REPLAY_MODE_NONE) {
439 return;
440 }
441
442 if (replay_blockers) {
443 error_reportf_err(replay_blockers->data, "Record/replay: ");
444 exit(1);
445 }
446 if (!icount_enabled()) {
447 error_report("Please enable icount to use record/replay");
448 exit(1);
449 }
450
451 /* Timer for snapshotting will be set up here. */
452
453 replay_enable_events();
454 }
455
456 void replay_finish(void)
457 {
458 if (replay_mode == REPLAY_MODE_NONE) {
459 return;
460 }
461
462 replay_save_instructions();
463
464 /* finalize the file */
465 if (replay_file) {
466 if (replay_mode == REPLAY_MODE_RECORD) {
467 /*
468 * Can't do it in the signal handler, therefore
469 * add shutdown event here for the case of Ctrl-C.
470 */
471 replay_shutdown_request(SHUTDOWN_CAUSE_HOST_SIGNAL);
472 /* write end event */
473 replay_put_event(EVENT_END);
474
475 /* write header */
476 fseek(replay_file, 0, SEEK_SET);
477 replay_put_dword(REPLAY_VERSION);
478 }
479
480 fclose(replay_file);
481 replay_file = NULL;
482 }
483 g_free(replay_filename);
484 replay_filename = NULL;
485
486 g_free(replay_snapshot);
487 replay_snapshot = NULL;
488
489 replay_finish_events();
490 replay_mode = REPLAY_MODE_NONE;
491 }
492
493 void replay_add_blocker(const char *feature)
494 {
495 Error *reason = NULL;
496
497 error_setg(&reason, "Record/replay is not supported with %s",
498 feature);
499 replay_blockers = g_slist_prepend(replay_blockers, reason);
500 }
501
502 const char *replay_get_filename(void)
503 {
504 return replay_filename;
505 }