tests/acceptance: Test U-Boot/Linux from Armbian 20.08 on Orange Pi PC
[qemu/ar7.git] / replay / replay-debugging.c
blob5ec574724a2505d44d14714e9b40136c9e5bec09
1 /*
2 * replay-debugging.c
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;
26 static int64_t replay_last_breakpoint;
27 static int64_t replay_last_snapshot;
29 bool replay_running_debug(void)
31 return replay_is_debugging;
34 void hmp_info_replay(Monitor *mon, const QDict *qdict)
36 if (replay_mode == REPLAY_MODE_NONE) {
37 monitor_printf(mon, "Record/replay is not active\n");
38 } else {
39 monitor_printf(mon,
40 "%s execution '%s': instruction count = %"PRId64"\n",
41 replay_mode == REPLAY_MODE_RECORD ? "Recording" : "Replaying",
42 replay_get_filename(), replay_get_current_icount());
46 ReplayInfo *qmp_query_replay(Error **errp)
48 ReplayInfo *retval = g_new0(ReplayInfo, 1);
50 retval->mode = replay_mode;
51 if (replay_get_filename()) {
52 retval->filename = g_strdup(replay_get_filename());
53 retval->has_filename = true;
55 retval->icount = replay_get_current_icount();
56 return retval;
59 static void replay_break(uint64_t icount, QEMUTimerCB callback, void *opaque)
61 assert(replay_mode == REPLAY_MODE_PLAY);
62 assert(replay_mutex_locked());
63 assert(replay_break_icount >= replay_get_current_icount());
64 assert(callback);
66 replay_break_icount = icount;
68 if (replay_break_timer) {
69 timer_del(replay_break_timer);
71 replay_break_timer = timer_new_ns(QEMU_CLOCK_REALTIME,
72 callback, opaque);
75 static void replay_delete_break(void)
77 assert(replay_mode == REPLAY_MODE_PLAY);
78 assert(replay_mutex_locked());
80 if (replay_break_timer) {
81 timer_free(replay_break_timer);
82 replay_break_timer = NULL;
84 replay_break_icount = -1ULL;
87 static void replay_stop_vm(void *opaque)
89 vm_stop(RUN_STATE_PAUSED);
90 replay_delete_break();
93 void qmp_replay_break(int64_t icount, Error **errp)
95 if (replay_mode == REPLAY_MODE_PLAY) {
96 if (icount >= replay_get_current_icount()) {
97 replay_break(icount, replay_stop_vm, NULL);
98 } else {
99 error_setg(errp,
100 "cannot set breakpoint at the instruction in the past");
102 } else {
103 error_setg(errp, "setting the breakpoint is allowed only in play mode");
107 void hmp_replay_break(Monitor *mon, const QDict *qdict)
109 int64_t icount = qdict_get_try_int(qdict, "icount", -1LL);
110 Error *err = NULL;
112 qmp_replay_break(icount, &err);
113 if (err) {
114 error_report_err(err);
115 return;
119 void qmp_replay_delete_break(Error **errp)
121 if (replay_mode == REPLAY_MODE_PLAY) {
122 replay_delete_break();
123 } else {
124 error_setg(errp, "replay breakpoints are allowed only in play mode");
128 void hmp_replay_delete_break(Monitor *mon, const QDict *qdict)
130 Error *err = NULL;
132 qmp_replay_delete_break(&err);
133 if (err) {
134 error_report_err(err);
135 return;
139 static char *replay_find_nearest_snapshot(int64_t icount,
140 int64_t *snapshot_icount)
142 BlockDriverState *bs;
143 QEMUSnapshotInfo *sn_tab;
144 QEMUSnapshotInfo *nearest = NULL;
145 char *ret = NULL;
146 int nb_sns, i;
147 AioContext *aio_context;
149 *snapshot_icount = -1;
151 bs = bdrv_all_find_vmstate_bs();
152 if (!bs) {
153 goto fail;
155 aio_context = bdrv_get_aio_context(bs);
157 aio_context_acquire(aio_context);
158 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
159 aio_context_release(aio_context);
161 for (i = 0; i < nb_sns; i++) {
162 if (bdrv_all_find_snapshot(sn_tab[i].name, &bs) == 0) {
163 if (sn_tab[i].icount != -1ULL
164 && sn_tab[i].icount <= icount
165 && (!nearest || nearest->icount < sn_tab[i].icount)) {
166 nearest = &sn_tab[i];
170 if (nearest) {
171 ret = g_strdup(nearest->name);
172 *snapshot_icount = nearest->icount;
174 g_free(sn_tab);
176 fail:
177 return ret;
180 static void replay_seek(int64_t icount, QEMUTimerCB callback, Error **errp)
182 char *snapshot = NULL;
183 int64_t snapshot_icount;
185 if (replay_mode != REPLAY_MODE_PLAY) {
186 error_setg(errp, "replay must be enabled to seek");
187 return;
190 snapshot = replay_find_nearest_snapshot(icount, &snapshot_icount);
191 if (snapshot) {
192 if (icount < replay_get_current_icount()
193 || replay_get_current_icount() < snapshot_icount) {
194 vm_stop(RUN_STATE_RESTORE_VM);
195 load_snapshot(snapshot, errp);
197 g_free(snapshot);
199 if (replay_get_current_icount() <= icount) {
200 replay_break(icount, callback, NULL);
201 vm_start();
202 } else {
203 error_setg(errp, "cannot seek to the specified instruction count");
207 void qmp_replay_seek(int64_t icount, Error **errp)
209 replay_seek(icount, replay_stop_vm, errp);
212 void hmp_replay_seek(Monitor *mon, const QDict *qdict)
214 int64_t icount = qdict_get_try_int(qdict, "icount", -1LL);
215 Error *err = NULL;
217 qmp_replay_seek(icount, &err);
218 if (err) {
219 error_report_err(err);
220 return;
224 static void replay_stop_vm_debug(void *opaque)
226 replay_is_debugging = false;
227 vm_stop(RUN_STATE_DEBUG);
228 replay_delete_break();
231 bool replay_reverse_step(void)
233 Error *err = NULL;
235 assert(replay_mode == REPLAY_MODE_PLAY);
237 if (replay_get_current_icount() != 0) {
238 replay_seek(replay_get_current_icount() - 1,
239 replay_stop_vm_debug, &err);
240 if (err) {
241 error_free(err);
242 return false;
244 replay_is_debugging = true;
245 return true;
248 return false;
251 static void replay_continue_end(void)
253 replay_is_debugging = false;
254 vm_stop(RUN_STATE_DEBUG);
255 replay_delete_break();
258 static void replay_continue_stop(void *opaque)
260 Error *err = NULL;
261 if (replay_last_breakpoint != -1LL) {
262 replay_seek(replay_last_breakpoint, replay_stop_vm_debug, &err);
263 if (err) {
264 error_free(err);
265 replay_continue_end();
267 return;
270 * No breakpoints since the last snapshot.
271 * Find previous snapshot and try again.
273 if (replay_last_snapshot != 0) {
274 replay_seek(replay_last_snapshot - 1, replay_continue_stop, &err);
275 if (err) {
276 error_free(err);
277 replay_continue_end();
279 replay_last_snapshot = replay_get_current_icount();
280 } else {
281 /* Seek to the very first step */
282 replay_seek(0, replay_stop_vm_debug, &err);
283 if (err) {
284 error_free(err);
285 replay_continue_end();
290 bool replay_reverse_continue(void)
292 Error *err = NULL;
294 assert(replay_mode == REPLAY_MODE_PLAY);
296 if (replay_get_current_icount() != 0) {
297 replay_seek(replay_get_current_icount() - 1,
298 replay_continue_stop, &err);
299 if (err) {
300 error_free(err);
301 return false;
303 replay_last_breakpoint = -1LL;
304 replay_is_debugging = true;
305 replay_last_snapshot = replay_get_current_icount();
306 return true;
309 return false;
312 void replay_breakpoint(void)
314 assert(replay_mode == REPLAY_MODE_PLAY);
315 replay_last_breakpoint = replay_get_current_icount();
318 void replay_gdb_attached(void)
321 * Create VM snapshot on temporary overlay to allow reverse
322 * debugging even if snapshots were not enabled.
324 if (replay_mode == REPLAY_MODE_PLAY
325 && !replay_snapshot) {
326 if (save_snapshot("start_debugging", NULL) != 0) {
327 /* Can't create the snapshot. Continue conventional debugging. */