master
c 338 lines 8.26 KB
Raw
1 /*
2 * replay-debugging.c
3 *
4 * Copyright (c) 2010-2020 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 "system/replay.h"
15 #include "system/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 "qobject/qdict.h"
21 #include "qemu/timer.h"
22 #include "block/snapshot.h"
23 #include "migration/snapshot.h"
24
25 static bool replay_is_debugging;
26 static int64_t replay_last_breakpoint;
27 static int64_t replay_last_snapshot;
28
29 bool replay_running_debug(void)
30 {
31 return replay_is_debugging;
32 }
33
34 #ifdef CONFIG_HMP
35 void hmp_info_replay(MonitorHMP *hmp, const QDict *qdict)
36 {
37 if (replay_mode == REPLAY_MODE_NONE) {
38 monitor_hmp_printf(hmp, "Record/replay is not active\n");
39 } else {
40 monitor_hmp_printf(hmp,
41 "%s execution '%s': instruction count = %"PRId64"\n",
42 replay_mode == REPLAY_MODE_RECORD ? "Recording" : "Replaying",
43 replay_get_filename(), replay_get_current_icount());
44 }
45 }
46 #endif
47
48 ReplayInfo *qmp_query_replay(Error **errp)
49 {
50 ReplayInfo *retval = g_new0(ReplayInfo, 1);
51
52 retval->mode = replay_mode;
53 if (replay_get_filename()) {
54 retval->filename = g_strdup(replay_get_filename());
55 }
56 retval->icount = replay_get_current_icount();
57 return retval;
58 }
59
60 static void replay_break(uint64_t icount, QEMUTimerCB callback, void *opaque)
61 {
62 assert(replay_mode == REPLAY_MODE_PLAY);
63 assert(replay_mutex_locked());
64 assert(replay_break_icount >= replay_get_current_icount());
65 assert(callback);
66
67 replay_break_icount = icount;
68
69 if (replay_break_timer) {
70 timer_del(replay_break_timer);
71 }
72 replay_break_timer = timer_new_ns(QEMU_CLOCK_REALTIME,
73 callback, opaque);
74 }
75
76 static void replay_delete_break(void)
77 {
78 assert(replay_mode == REPLAY_MODE_PLAY);
79 assert(replay_mutex_locked());
80
81 if (replay_break_timer) {
82 timer_free(replay_break_timer);
83 replay_break_timer = NULL;
84 }
85 replay_break_icount = -1ULL;
86 }
87
88 static void replay_stop_vm(void *opaque)
89 {
90 vm_stop(RUN_STATE_PAUSED);
91 replay_delete_break();
92 }
93
94 void qmp_replay_break(int64_t icount, Error **errp)
95 {
96 if (replay_mode == REPLAY_MODE_PLAY) {
97 if (icount >= replay_get_current_icount()) {
98 replay_break(icount, replay_stop_vm, NULL);
99 } else {
100 error_setg(errp,
101 "cannot set breakpoint at the instruction in the past");
102 }
103 } else {
104 error_setg(errp, "setting the breakpoint is allowed only in play mode");
105 }
106 }
107
108 #ifdef CONFIG_HMP
109 void hmp_replay_break(MonitorHMP *hmp, const QDict *qdict)
110 {
111 int64_t icount = qdict_get_try_int(qdict, "icount", -1LL);
112 Error *err = NULL;
113
114 qmp_replay_break(icount, &err);
115 if (err) {
116 error_report_err(err);
117 return;
118 }
119 }
120 #endif
121
122 void qmp_replay_delete_break(Error **errp)
123 {
124 if (replay_mode == REPLAY_MODE_PLAY) {
125 replay_delete_break();
126 } else {
127 error_setg(errp, "replay breakpoints are allowed only in play mode");
128 }
129 }
130
131 #ifdef CONFIG_HMP
132 void hmp_replay_delete_break(MonitorHMP *hmp, const QDict *qdict)
133 {
134 Error *err = NULL;
135
136 qmp_replay_delete_break(&err);
137 if (err) {
138 error_report_err(err);
139 return;
140 }
141 }
142 #endif
143
144 static char *replay_find_nearest_snapshot(int64_t icount,
145 int64_t *snapshot_icount)
146 {
147 BlockDriverState *bs;
148 g_autofree QEMUSnapshotInfo *sn_tab = NULL;
149 QEMUSnapshotInfo *nearest = NULL;
150 int rv;
151 int nb_sns, i;
152
153 *snapshot_icount = -1;
154
155 bs = bdrv_all_find_vmstate_bs(NULL, false, NULL, NULL);
156 if (!bs) {
157 return NULL;
158 }
159
160 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
161 if (nb_sns < 0) {
162 return NULL;
163 }
164
165 for (i = 0; i < nb_sns; i++) {
166 rv = bdrv_all_has_snapshot(sn_tab[i].name, false, NULL, NULL);
167 if (rv < 0) {
168 return NULL;
169 }
170 if (rv == 1) {
171 if (sn_tab[i].icount != -1ULL
172 && sn_tab[i].icount <= icount
173 && (!nearest || nearest->icount < sn_tab[i].icount)) {
174 nearest = &sn_tab[i];
175 }
176 }
177 }
178 if (!nearest) {
179 return NULL;
180 }
181
182 *snapshot_icount = nearest->icount;
183 return g_strdup(nearest->name);
184 }
185
186 static void replay_seek(int64_t icount, QEMUTimerCB callback, Error **errp)
187 {
188 char *snapshot = NULL;
189 int64_t snapshot_icount;
190
191 if (replay_mode != REPLAY_MODE_PLAY) {
192 error_setg(errp, "replay must be enabled to seek");
193 return;
194 }
195
196 snapshot = replay_find_nearest_snapshot(icount, &snapshot_icount);
197 if (snapshot) {
198 if (icount < replay_get_current_icount()
199 || replay_get_current_icount() < snapshot_icount) {
200 vm_stop(RUN_STATE_RESTORE_VM);
201 load_snapshot(snapshot, NULL, false, NULL, errp);
202 }
203 g_free(snapshot);
204 }
205 if (replay_get_current_icount() <= icount) {
206 replay_break(icount, callback, NULL);
207 vm_start();
208 } else {
209 error_setg(errp, "cannot seek to the specified instruction count");
210 }
211 }
212
213 void qmp_replay_seek(int64_t icount, Error **errp)
214 {
215 replay_seek(icount, replay_stop_vm, errp);
216 }
217
218 #ifdef CONFIG_HMP
219 void hmp_replay_seek(MonitorHMP *hmp, const QDict *qdict)
220 {
221 int64_t icount = qdict_get_try_int(qdict, "icount", -1LL);
222 Error *err = NULL;
223
224 qmp_replay_seek(icount, &err);
225 if (err) {
226 error_report_err(err);
227 return;
228 }
229 }
230 #endif
231
232 static void replay_stop_vm_debug(void *opaque)
233 {
234 replay_is_debugging = false;
235 vm_stop(RUN_STATE_DEBUG);
236 replay_delete_break();
237 }
238
239 bool replay_reverse_step(void)
240 {
241 Error *err = NULL;
242
243 assert(replay_mode == REPLAY_MODE_PLAY);
244
245 if (replay_get_current_icount() != 0) {
246 replay_seek(replay_get_current_icount() - 1,
247 replay_stop_vm_debug, &err);
248 if (err) {
249 error_free(err);
250 return false;
251 }
252 replay_is_debugging = true;
253 return true;
254 }
255
256 return false;
257 }
258
259 static void replay_continue_end(void)
260 {
261 replay_is_debugging = false;
262 vm_stop(RUN_STATE_DEBUG);
263 replay_delete_break();
264 }
265
266 static void replay_continue_stop(void *opaque)
267 {
268 Error *err = NULL;
269 if (replay_last_breakpoint != -1LL) {
270 replay_seek(replay_last_breakpoint, replay_stop_vm_debug, &err);
271 if (err) {
272 error_free(err);
273 replay_continue_end();
274 }
275 return;
276 }
277 /*
278 * No breakpoints since the last snapshot.
279 * Find previous snapshot and try again.
280 */
281 if (replay_last_snapshot != 0) {
282 replay_seek(replay_last_snapshot - 1, replay_continue_stop, &err);
283 if (err) {
284 error_free(err);
285 replay_continue_end();
286 }
287 replay_last_snapshot = replay_get_current_icount();
288 } else {
289 /* Seek to the very first step */
290 replay_seek(0, replay_stop_vm_debug, &err);
291 if (err) {
292 error_free(err);
293 replay_continue_end();
294 }
295 }
296 }
297
298 bool replay_reverse_continue(void)
299 {
300 Error *err = NULL;
301
302 assert(replay_mode == REPLAY_MODE_PLAY);
303
304 if (replay_get_current_icount() != 0) {
305 replay_seek(replay_get_current_icount() - 1,
306 replay_continue_stop, &err);
307 if (err) {
308 error_free(err);
309 return false;
310 }
311 replay_last_breakpoint = -1LL;
312 replay_is_debugging = true;
313 replay_last_snapshot = replay_get_current_icount();
314 return true;
315 }
316
317 return false;
318 }
319
320 void replay_breakpoint(void)
321 {
322 assert(replay_mode == REPLAY_MODE_PLAY);
323 replay_last_breakpoint = replay_get_current_icount();
324 }
325
326 void replay_gdb_attached(void)
327 {
328 /*
329 * Create VM snapshot on temporary overlay to allow reverse
330 * debugging even if snapshots were not enabled.
331 */
332 if (replay_mode == REPLAY_MODE_PLAY
333 && !replay_snapshot) {
334 if (!save_snapshot("start_debugging", true, NULL, false, NULL, NULL)) {
335 /* Can't create the snapshot. Continue conventional debugging. */
336 }
337 }
338 }