4 * Copyright (c) 2010-2020 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 "monitor/hmp.h"
18 #include "monitor/monitor.h"
19 #include "qapi/qapi-commands-replay.h"
20 #include "qapi/qmp/qdict.h"
21 #include "qemu/timer.h"
22 #include "block/snapshot.h"
23 #include "migration/snapshot.h"
25 static bool replay_is_debugging
;
27 bool replay_running_debug(void)
29 return replay_is_debugging
;
32 void hmp_info_replay(Monitor
*mon
, const QDict
*qdict
)
34 if (replay_mode
== REPLAY_MODE_NONE
) {
35 monitor_printf(mon
, "Record/replay is not active\n");
38 "%s execution '%s': instruction count = %"PRId64
"\n",
39 replay_mode
== REPLAY_MODE_RECORD
? "Recording" : "Replaying",
40 replay_get_filename(), replay_get_current_icount());
44 ReplayInfo
*qmp_query_replay(Error
**errp
)
46 ReplayInfo
*retval
= g_new0(ReplayInfo
, 1);
48 retval
->mode
= replay_mode
;
49 if (replay_get_filename()) {
50 retval
->filename
= g_strdup(replay_get_filename());
51 retval
->has_filename
= true;
53 retval
->icount
= replay_get_current_icount();
57 static void replay_break(uint64_t icount
, QEMUTimerCB callback
, void *opaque
)
59 assert(replay_mode
== REPLAY_MODE_PLAY
);
60 assert(replay_mutex_locked());
61 assert(replay_break_icount
>= replay_get_current_icount());
64 replay_break_icount
= icount
;
66 if (replay_break_timer
) {
67 timer_del(replay_break_timer
);
69 replay_break_timer
= timer_new_ns(QEMU_CLOCK_REALTIME
,
73 static void replay_delete_break(void)
75 assert(replay_mode
== REPLAY_MODE_PLAY
);
76 assert(replay_mutex_locked());
78 if (replay_break_timer
) {
79 timer_del(replay_break_timer
);
80 timer_free(replay_break_timer
);
81 replay_break_timer
= NULL
;
83 replay_break_icount
= -1ULL;
86 static void replay_stop_vm(void *opaque
)
88 vm_stop(RUN_STATE_PAUSED
);
89 replay_delete_break();
92 void qmp_replay_break(int64_t icount
, Error
**errp
)
94 if (replay_mode
== REPLAY_MODE_PLAY
) {
95 if (icount
>= replay_get_current_icount()) {
96 replay_break(icount
, replay_stop_vm
, NULL
);
99 "cannot set breakpoint at the instruction in the past");
102 error_setg(errp
, "setting the breakpoint is allowed only in play mode");
106 void hmp_replay_break(Monitor
*mon
, const QDict
*qdict
)
108 int64_t icount
= qdict_get_try_int(qdict
, "icount", -1LL);
111 qmp_replay_break(icount
, &err
);
113 error_report_err(err
);
118 void qmp_replay_delete_break(Error
**errp
)
120 if (replay_mode
== REPLAY_MODE_PLAY
) {
121 replay_delete_break();
123 error_setg(errp
, "replay breakpoints are allowed only in play mode");
127 void hmp_replay_delete_break(Monitor
*mon
, const QDict
*qdict
)
131 qmp_replay_delete_break(&err
);
133 error_report_err(err
);
138 static char *replay_find_nearest_snapshot(int64_t icount
,
139 int64_t *snapshot_icount
)
141 BlockDriverState
*bs
;
142 QEMUSnapshotInfo
*sn_tab
;
143 QEMUSnapshotInfo
*nearest
= NULL
;
146 AioContext
*aio_context
;
148 *snapshot_icount
= -1;
150 bs
= bdrv_all_find_vmstate_bs();
154 aio_context
= bdrv_get_aio_context(bs
);
156 aio_context_acquire(aio_context
);
157 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
158 aio_context_release(aio_context
);
160 for (i
= 0; i
< nb_sns
; i
++) {
161 if (bdrv_all_find_snapshot(sn_tab
[i
].name
, &bs
) == 0) {
162 if (sn_tab
[i
].icount
!= -1ULL
163 && sn_tab
[i
].icount
<= icount
164 && (!nearest
|| nearest
->icount
< sn_tab
[i
].icount
)) {
165 nearest
= &sn_tab
[i
];
170 ret
= g_strdup(nearest
->name
);
171 *snapshot_icount
= nearest
->icount
;
179 static void replay_seek(int64_t icount
, QEMUTimerCB callback
, Error
**errp
)
181 char *snapshot
= NULL
;
182 int64_t snapshot_icount
;
184 if (replay_mode
!= REPLAY_MODE_PLAY
) {
185 error_setg(errp
, "replay must be enabled to seek");
189 snapshot
= replay_find_nearest_snapshot(icount
, &snapshot_icount
);
191 if (icount
< replay_get_current_icount()
192 || replay_get_current_icount() < snapshot_icount
) {
193 vm_stop(RUN_STATE_RESTORE_VM
);
194 load_snapshot(snapshot
, errp
);
198 if (replay_get_current_icount() <= icount
) {
199 replay_break(icount
, callback
, NULL
);
202 error_setg(errp
, "cannot seek to the specified instruction count");
206 void qmp_replay_seek(int64_t icount
, Error
**errp
)
208 replay_seek(icount
, replay_stop_vm
, errp
);
211 void hmp_replay_seek(Monitor
*mon
, const QDict
*qdict
)
213 int64_t icount
= qdict_get_try_int(qdict
, "icount", -1LL);
216 qmp_replay_seek(icount
, &err
);
218 error_report_err(err
);
223 static void replay_stop_vm_debug(void *opaque
)
225 replay_is_debugging
= false;
226 vm_stop(RUN_STATE_DEBUG
);
227 replay_delete_break();
230 bool replay_reverse_step(void)
234 assert(replay_mode
== REPLAY_MODE_PLAY
);
236 if (replay_get_current_icount() != 0) {
237 replay_seek(replay_get_current_icount() - 1,
238 replay_stop_vm_debug
, &err
);
243 replay_is_debugging
= true;