]> git.proxmox.com Git - mirror_qemu.git/blame - monitor/misc.c
exec/exec-all: Move 'qemu/log.h' include in units requiring it
[mirror_qemu.git] / monitor / misc.c
CommitLineData
9dc39cba
FB
1/*
2 * QEMU monitor
5fafdf24 3 *
9dc39cba 4 * Copyright (c) 2003-2004 Fabrice Bellard
5fafdf24 5 *
9dc39cba
FB
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
e688df6b 24
d38ea87a 25#include "qemu/osdep.h"
5bce308a 26#include "monitor-internal.h"
b4a42f81 27#include "monitor/qdev.h"
87ecb68b 28#include "hw/usb.h"
a2cb15b0 29#include "hw/pci/pci.h"
0d09e41a 30#include "sysemu/watchdog.h"
45a50b16 31#include "hw/loader.h"
022c62cb 32#include "exec/gdbstub.h"
1422e32d 33#include "net/net.h"
68ac40d2 34#include "net/slirp.h"
7572150c 35#include "ui/qemu-spice.h"
213dcb06 36#include "qemu/config-file.h"
856dfd8a 37#include "qemu/ctype.h"
28ecbaee 38#include "ui/console.h"
c751a74a 39#include "ui/input.h"
87ecb68b 40#include "audio/audio.h"
76cad711 41#include "disas/disas.h"
9c17d615 42#include "sysemu/balloon.h"
1de7afc9 43#include "qemu/timer.h"
cd617484 44#include "qemu/log.h"
b3946626 45#include "sysemu/hw_accel.h"
54d31236 46#include "sysemu/runstate.h"
b76806d4
DB
47#include "authz/list.h"
48#include "qapi/util.h"
55225c85
MA
49#include "sysemu/blockdev.h"
50#include "sysemu/sysemu.h"
14a48c1d 51#include "sysemu/tcg.h"
bdee56f5 52#include "sysemu/tpm.h"
452fcdbc 53#include "qapi/qmp/qdict.h"
cc7a8ea7 54#include "qapi/qmp/qerror.h"
fc81fa1e 55#include "qapi/qmp/qstring.h"
a9c94277 56#include "qom/object_interfaces.h"
31965ae2 57#include "trace/control.h"
bf957284 58#include "monitor/hmp-target.h"
275307aa 59#include "monitor/hmp.h"
6d8a764e 60#ifdef CONFIG_TRACE_SIMPLE
31965ae2 61#include "trace/simple.h"
22890ab5 62#endif
022c62cb 63#include "exec/memory.h"
63c91552 64#include "exec/exec-all.h"
922a01a0 65#include "qemu/option.h"
1de7afc9 66#include "qemu/thread.h"
b21631f3 67#include "block/qapi.h"
a2dde2f2 68#include "block/block-hmp-cmds.h"
00ca24ff 69#include "qapi/qapi-commands-char.h"
fa4dcf57 70#include "qapi/qapi-commands-control.h"
00ca24ff
MA
71#include "qapi/qapi-commands-migration.h"
72#include "qapi/qapi-commands-misc.h"
73#include "qapi/qapi-commands-qom.h"
22afb46e 74#include "qapi/qapi-commands-run-state.h"
00ca24ff 75#include "qapi/qapi-commands-trace.h"
dd98234c 76#include "qapi/qapi-commands-machine.h"
00ca24ff 77#include "qapi/qapi-init-commands.h"
e688df6b 78#include "qapi/error.h"
43a14cfc 79#include "qapi/qmp-event.h"
d2528bdc 80#include "sysemu/cpus.h"
f348b6d1 81#include "qemu/cutils.h"
6a5bd307 82
a4538a5c
JH
83#if defined(TARGET_S390X)
84#include "hw/s390x/storage-keys.h"
f860d497 85#include "hw/s390x/storage-attributes.h"
a4538a5c
JH
86#endif
87
f07918fd 88/* file descriptors passed via SCM_RIGHTS */
c227f099
AL
89typedef struct mon_fd_t mon_fd_t;
90struct mon_fd_t {
f07918fd
MM
91 char *name;
92 int fd;
c227f099 93 QLIST_ENTRY(mon_fd_t) next;
f07918fd
MM
94};
95
ba1c048a
CB
96/* file descriptor associated with a file descriptor set */
97typedef struct MonFdsetFd MonFdsetFd;
98struct MonFdsetFd {
99 int fd;
100 bool removed;
101 char *opaque;
102 QLIST_ENTRY(MonFdsetFd) next;
103};
104
105/* file descriptor set containing fds passed via SCM_RIGHTS */
106typedef struct MonFdset MonFdset;
107struct MonFdset {
108 int64_t id;
109 QLIST_HEAD(, MonFdsetFd) fds;
adb696f3 110 QLIST_HEAD(, MonFdsetFd) dup_fds;
ba1c048a
CB
111 QLIST_ENTRY(MonFdset) next;
112};
113
47451466
PX
114/* Protects mon_fdsets */
115static QemuMutex mon_fdsets_lock;
b58deb34 116static QLIST_HEAD(, MonFdset) mon_fdsets;
47451466 117
a0cd5e1c 118static HMPCommand hmp_info_cmds[];
9dc39cba 119
d51a67b4
LC
120char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
121 int64_t cpu_index, Error **errp)
0268d97c 122{
d51a67b4 123 char *output = NULL;
5f9dba16 124 MonitorHMP hmp = {};
0268d97c 125
92082416 126 monitor_data_init(&hmp.common, false, true, false);
0268d97c 127
d51a67b4 128 if (has_cpu_index) {
dcba65f8 129 int ret = monitor_set_cpu(&hmp.common, cpu_index);
0268d97c 130 if (ret < 0) {
c6bd8c70
MA
131 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
132 "a CPU number");
0268d97c
LC
133 goto out;
134 }
135 }
136
7ef6cf63 137 handle_hmp_command(&hmp, command_line);
0268d97c 138
0210c3b3 139 WITH_QEMU_LOCK_GUARD(&hmp.common.mon_lock) {
20076f4a 140 output = g_strdup(hmp.common.outbuf->str);
0268d97c
LC
141 }
142
143out:
5f9dba16 144 monitor_data_destroy(&hmp.common);
d51a67b4 145 return output;
0268d97c
LC
146}
147
ed7bda5d
KW
148/**
149 * Is @name in the '|' separated list of names @list?
150 */
151int hmp_compare_cmd(const char *name, const char *list)
9dc39cba
FB
152{
153 const char *p, *pstart;
154 int len;
155 len = strlen(name);
156 p = list;
ed7bda5d 157 for (;;) {
9dc39cba 158 pstart = p;
5c99fa37 159 p = qemu_strchrnul(p, '|');
ed7bda5d 160 if ((p - pstart) == len && !memcmp(pstart, name, len)) {
9dc39cba 161 return 1;
f5438c05
WX
162 }
163 if (*p == '\0') {
164 break;
165 }
ed7bda5d 166 p++;
f5438c05 167 }
dcc70cdf 168 return 0;
9dc39cba
FB
169}
170
d54908a5 171static void do_help_cmd(Monitor *mon, const QDict *qdict)
38183186 172{
d54908a5 173 help_cmd(mon, qdict_get_try_str(qdict, "name"));
38183186
LC
174}
175
3e5a50d6 176static void hmp_trace_event(Monitor *mon, const QDict *qdict)
22890ab5
PS
177{
178 const char *tp_name = qdict_get_str(qdict, "name");
179 bool new_state = qdict_get_bool(qdict, "option");
77e2b172
LV
180 bool has_vcpu = qdict_haskey(qdict, "vcpu");
181 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
14101d02 182 Error *local_err = NULL;
f871d689 183
77e2b172
LV
184 if (vcpu < 0) {
185 monitor_printf(mon, "argument vcpu must be positive");
186 return;
187 }
188
189 qmp_trace_event_set_state(tp_name, new_state, true, true, has_vcpu, vcpu, &local_err);
14101d02 190 if (local_err) {
091e38b7 191 error_report_err(local_err);
f871d689 192 }
22890ab5 193}
c5ceb523 194
c45a8168 195#ifdef CONFIG_TRACE_SIMPLE
3e5a50d6 196static void hmp_trace_file(Monitor *mon, const QDict *qdict)
c5ceb523
SH
197{
198 const char *op = qdict_get_try_str(qdict, "op");
199 const char *arg = qdict_get_try_str(qdict, "arg");
200
201 if (!op) {
ba4912cb 202 st_print_trace_file_status();
c5ceb523
SH
203 } else if (!strcmp(op, "on")) {
204 st_set_trace_file_enabled(true);
205 } else if (!strcmp(op, "off")) {
206 st_set_trace_file_enabled(false);
207 } else if (!strcmp(op, "flush")) {
208 st_flush_trace_buffer();
209 } else if (!strcmp(op, "set")) {
210 if (arg) {
211 st_set_trace_file(arg);
212 }
213 } else {
214 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
215 help_cmd(mon, "trace-file");
216 }
217}
22890ab5
PS
218#endif
219
3e5a50d6 220static void hmp_info_help(Monitor *mon, const QDict *qdict)
9dc39cba 221{
13c7425e 222 help_cmd(mon, "info");
9dc39cba
FB
223}
224
6adf08dd 225static void monitor_init_qmp_commands(void)
edcfaefe 226{
635db18f
MA
227 /*
228 * Two command lists:
229 * - qmp_commands contains all QMP commands
230 * - qmp_cap_negotiation_commands contains just
231 * "qmp_capabilities", to enforce capability negotiation
232 */
233
1527badb 234 qmp_init_marshal(&qmp_commands);
05875687 235
6604e475
MA
236 qmp_register_command(&qmp_commands, "device_add",
237 qmp_device_add, 0, 0);
5032a16d 238
635db18f
MA
239 QTAILQ_INIT(&qmp_cap_negotiation_commands);
240 qmp_register_command(&qmp_cap_negotiation_commands, "qmp_capabilities",
6604e475
MA
241 qmp_marshal_qmp_capabilities,
242 QCO_ALLOW_PRECONFIG, 0);
635db18f
MA
243}
244
d9f25280 245/* Set the current CPU defined by the user. Callers must hold BQL. */
dcba65f8 246int monitor_set_cpu(Monitor *mon, int cpu_index)
6a00d601 247{
55e5c285 248 CPUState *cpu;
6a00d601 249
1c8bb3cc
AF
250 cpu = qemu_get_cpu(cpu_index);
251 if (cpu == NULL) {
252 return -1;
6a00d601 253 }
dcba65f8
KW
254 g_free(mon->mon_cpu_path);
255 mon->mon_cpu_path = object_get_canonical_path(OBJECT(cpu));
1c8bb3cc 256 return 0;
6a00d601
FB
257}
258
d9f25280 259/* Callers must hold BQL. */
87e6f4a4 260static CPUState *mon_get_cpu_sync(Monitor *mon, bool synchronize)
6a00d601 261{
a85d0bf3 262 CPUState *cpu = NULL;
751f8cfe 263
87e6f4a4
KW
264 if (mon->mon_cpu_path) {
265 cpu = (CPUState *) object_resolve_path_type(mon->mon_cpu_path,
751f8cfe
GK
266 TYPE_CPU, NULL);
267 if (!cpu) {
87e6f4a4
KW
268 g_free(mon->mon_cpu_path);
269 mon->mon_cpu_path = NULL;
751f8cfe
GK
270 }
271 }
87e6f4a4 272 if (!mon->mon_cpu_path) {
854e67fe
TH
273 if (!first_cpu) {
274 return NULL;
275 }
87e6f4a4 276 monitor_set_cpu(mon, first_cpu->cpu_index);
751f8cfe 277 cpu = first_cpu;
6a00d601 278 }
a85d0bf3 279 assert(cpu != NULL);
137b5cb6
VM
280 if (synchronize) {
281 cpu_synchronize_state(cpu);
282 }
751f8cfe 283 return cpu;
5bcda5f7
PC
284}
285
2fc5d01b 286CPUState *mon_get_cpu(Monitor *mon)
137b5cb6 287{
2fc5d01b 288 return mon_get_cpu_sync(mon, true);
137b5cb6
VM
289}
290
e7cff9c6 291CPUArchState *mon_get_cpu_env(Monitor *mon)
5bcda5f7 292{
e7cff9c6 293 CPUState *cs = mon_get_cpu(mon);
854e67fe
TH
294
295 return cs ? cs->env_ptr : NULL;
6a00d601
FB
296}
297
87e6f4a4 298int monitor_get_cpu_index(Monitor *mon)
99b7796f 299{
87e6f4a4 300 CPUState *cs = mon_get_cpu_sync(mon, false);
854e67fe
TH
301
302 return cs ? cs->cpu_index : UNASSIGNED_CPU_INDEX;
99b7796f
LC
303}
304
1ce6be24 305static void hmp_info_registers(Monitor *mon, const QDict *qdict)
9307c4c1 306{
18f08282
SJS
307 bool all_cpus = qdict_get_try_bool(qdict, "cpustate_all", false);
308 CPUState *cs;
854e67fe 309
18f08282
SJS
310 if (all_cpus) {
311 CPU_FOREACH(cs) {
312 monitor_printf(mon, "\nCPU#%d\n", cs->cpu_index);
90c84c56 313 cpu_dump_state(cs, NULL, CPU_DUMP_FPU);
18f08282
SJS
314 }
315 } else {
2fc5d01b 316 cs = mon_get_cpu(mon);
18f08282
SJS
317
318 if (!cs) {
319 monitor_printf(mon, "No CPU available\n");
320 return;
321 }
322
90c84c56 323 cpu_dump_state(cs, NULL, CPU_DUMP_FPU);
854e67fe 324 }
9307c4c1
FB
325}
326
97bfafe2
EC
327static void hmp_info_sync_profile(Monitor *mon, const QDict *qdict)
328{
329 int64_t max = qdict_get_try_int(qdict, "max", 10);
330 bool mean = qdict_get_try_bool(qdict, "mean", false);
331 bool coalesce = !qdict_get_try_bool(qdict, "no_coalesce", false);
332 enum QSPSortBy sort_by;
333
334 sort_by = mean ? QSP_SORT_BY_AVG_WAIT_TIME : QSP_SORT_BY_TOTAL_WAIT_TIME;
ac7ff4cf 335 qsp_report(max, sort_by, coalesce);
97bfafe2
EC
336}
337
1ce6be24 338static void hmp_info_history(Monitor *mon, const QDict *qdict)
aa455485 339{
5f9dba16 340 MonitorHMP *hmp_mon = container_of(mon, MonitorHMP, common);
aa455485 341 int i;
7e2515e8 342 const char *str;
3b46e624 343
5f9dba16 344 if (!hmp_mon->rs) {
cde76ee1 345 return;
5f9dba16 346 }
7e2515e8
FB
347 i = 0;
348 for(;;) {
5f9dba16
KW
349 str = readline_get_history(hmp_mon->rs, i);
350 if (!str) {
7e2515e8 351 break;
5f9dba16 352 }
376253ec 353 monitor_printf(mon, "%d: '%s'\n", i, str);
8e3a9fd2 354 i++;
aa455485
FB
355 }
356}
357
1ce6be24 358static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
22890ab5 359{
bd71211d 360 const char *name = qdict_get_try_str(qdict, "name");
77e2b172
LV
361 bool has_vcpu = qdict_haskey(qdict, "vcpu");
362 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
bd71211d 363 TraceEventInfoList *events;
14101d02 364 TraceEventInfoList *elem;
bd71211d
LV
365 Error *local_err = NULL;
366
367 if (name == NULL) {
368 name = "*";
369 }
77e2b172
LV
370 if (vcpu < 0) {
371 monitor_printf(mon, "argument vcpu must be positive");
372 return;
373 }
bd71211d 374
77e2b172 375 events = qmp_trace_event_get_state(name, has_vcpu, vcpu, &local_err);
bd71211d
LV
376 if (local_err) {
377 error_report_err(local_err);
378 return;
379 }
14101d02
LV
380
381 for (elem = events; elem != NULL; elem = elem->next) {
382 monitor_printf(mon, "%s : state %u\n",
383 elem->value->name,
384 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
385 }
386 qapi_free_TraceEventInfoList(events);
22890ab5 387}
22890ab5 388
b8a185bc
MA
389void qmp_client_migrate_info(const char *protocol, const char *hostname,
390 bool has_port, int64_t port,
391 bool has_tls_port, int64_t tls_port,
392 bool has_cert_subject, const char *cert_subject,
393 Error **errp)
e866e239 394{
e866e239 395 if (strcmp(protocol, "spice") == 0) {
b8a185bc
MA
396 if (!qemu_using_spice(errp)) {
397 return;
e866e239
GH
398 }
399
b8a185bc 400 if (!has_port && !has_tls_port) {
c6bd8c70 401 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
b8a185bc 402 return;
6ec5dae5
YH
403 }
404
7477477c 405 if (qemu_spice.migrate_info(hostname,
b8a185bc
MA
406 has_port ? port : -1,
407 has_tls_port ? tls_port : -1,
408 cert_subject)) {
9e1b9c6c 409 error_setg(errp, "Could not set up display for migration");
b8a185bc 410 return;
e866e239 411 }
b8a185bc 412 return;
e866e239
GH
413 }
414
99750d82 415 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "'spice'");
e866e239
GH
416}
417
3e5a50d6 418static void hmp_logfile(Monitor *mon, const QDict *qdict)
e735b91c 419{
daa76aa4
MA
420 Error *err = NULL;
421
422 qemu_set_log_filename(qdict_get_str(qdict, "filename"), &err);
423 if (err) {
424 error_report_err(err);
425 }
e735b91c
PB
426}
427
3e5a50d6 428static void hmp_log(Monitor *mon, const QDict *qdict)
f193c797
FB
429{
430 int mask;
d54908a5 431 const char *items = qdict_get_str(qdict, "items");
3b46e624 432
9307c4c1 433 if (!strcmp(items, "none")) {
f193c797
FB
434 mask = 0;
435 } else {
4fde1eba 436 mask = qemu_str_to_log_mask(items);
f193c797 437 if (!mask) {
376253ec 438 help_cmd(mon, "log");
f193c797
FB
439 return;
440 }
441 }
24537a01 442 qemu_set_log(mask);
f193c797
FB
443}
444
3e5a50d6 445static void hmp_singlestep(Monitor *mon, const QDict *qdict)
1b530a6d 446{
d54908a5 447 const char *option = qdict_get_try_str(qdict, "option");
1b530a6d
AJ
448 if (!option || !strcmp(option, "on")) {
449 singlestep = 1;
450 } else if (!strcmp(option, "off")) {
451 singlestep = 0;
452 } else {
453 monitor_printf(mon, "unexpected option %s\n", option);
454 }
455}
456
3e5a50d6 457static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
59030a8c 458{
d54908a5 459 const char *device = qdict_get_try_str(qdict, "device");
89854b95 460 if (!device) {
59030a8c 461 device = "tcp::" DEFAULT_GDBSTUB_PORT;
89854b95
YA
462 }
463
59030a8c
AL
464 if (gdbserver_start(device) < 0) {
465 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
466 device);
467 } else if (strcmp(device, "none") == 0) {
36556b20 468 monitor_printf(mon, "Disabled gdbserver\n");
8a7ddc38 469 } else {
59030a8c
AL
470 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
471 device);
8a7ddc38
FB
472 }
473}
474
3e5a50d6 475static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
9dd986cc 476{
22afb46e
PB
477 Error *err = NULL;
478 WatchdogAction action;
479 char *qapi_value;
480
481 qapi_value = g_ascii_strdown(qdict_get_str(qdict, "action"), -1);
482 action = qapi_enum_parse(&WatchdogAction_lookup, qapi_value, -1, &err);
483 g_free(qapi_value);
484 if (err) {
485 hmp_handle_error(mon, err);
486 return;
9dd986cc 487 }
22afb46e 488 qmp_watchdog_set_action(action, &error_abort);
9dd986cc
RJ
489}
490
376253ec 491static void monitor_printc(Monitor *mon, int c)
9307c4c1 492{
376253ec 493 monitor_printf(mon, "'");
9307c4c1
FB
494 switch(c) {
495 case '\'':
376253ec 496 monitor_printf(mon, "\\'");
9307c4c1
FB
497 break;
498 case '\\':
376253ec 499 monitor_printf(mon, "\\\\");
9307c4c1
FB
500 break;
501 case '\n':
376253ec 502 monitor_printf(mon, "\\n");
9307c4c1
FB
503 break;
504 case '\r':
376253ec 505 monitor_printf(mon, "\\r");
9307c4c1
FB
506 break;
507 default:
508 if (c >= 32 && c <= 126) {
376253ec 509 monitor_printf(mon, "%c", c);
9307c4c1 510 } else {
376253ec 511 monitor_printf(mon, "\\x%02x", c);
9307c4c1
FB
512 }
513 break;
514 }
376253ec 515 monitor_printf(mon, "'");
9307c4c1
FB
516}
517
376253ec 518static void memory_dump(Monitor *mon, int count, int format, int wsize,
a8170e5e 519 hwaddr addr, int is_physical)
9307c4c1 520{
23842aab 521 int l, line_size, i, max_digits, len;
9307c4c1
FB
522 uint8_t buf[16];
523 uint64_t v;
2fc5d01b 524 CPUState *cs = mon_get_cpu(mon);
854e67fe
TH
525
526 if (!cs && (format == 'i' || !is_physical)) {
527 monitor_printf(mon, "Can not dump without CPU\n");
528 return;
529 }
9307c4c1
FB
530
531 if (format == 'i') {
1d48474d 532 monitor_disas(mon, cs, addr, count, is_physical);
9307c4c1
FB
533 return;
534 }
535
536 len = wsize * count;
89854b95 537 if (wsize == 1) {
9307c4c1 538 line_size = 8;
89854b95 539 } else {
9307c4c1 540 line_size = 16;
89854b95 541 }
9307c4c1
FB
542 max_digits = 0;
543
544 switch(format) {
545 case 'o':
69db8dfc 546 max_digits = DIV_ROUND_UP(wsize * 8, 3);
9307c4c1
FB
547 break;
548 default:
549 case 'x':
550 max_digits = (wsize * 8) / 4;
551 break;
552 case 'u':
553 case 'd':
69db8dfc 554 max_digits = DIV_ROUND_UP(wsize * 8 * 10, 33);
9307c4c1
FB
555 break;
556 case 'c':
557 wsize = 1;
558 break;
559 }
560
561 while (len > 0) {
89854b95 562 if (is_physical) {
376253ec 563 monitor_printf(mon, TARGET_FMT_plx ":", addr);
89854b95 564 } else {
376253ec 565 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
89854b95 566 }
9307c4c1
FB
567 l = len;
568 if (l > line_size)
569 l = line_size;
570 if (is_physical) {
6f89ae58
PM
571 AddressSpace *as = cs ? cs->as : &address_space_memory;
572 MemTxResult r = address_space_read(as, addr,
573 MEMTXATTRS_UNSPECIFIED, buf, l);
574 if (r != MEMTX_OK) {
575 monitor_printf(mon, " Cannot access memory\n");
576 break;
577 }
9307c4c1 578 } else {
854e67fe 579 if (cpu_memory_rw_debug(cs, addr, buf, l, 0) < 0) {
376253ec 580 monitor_printf(mon, " Cannot access memory\n");
c8f79b67
AL
581 break;
582 }
9307c4c1 583 }
5fafdf24 584 i = 0;
9307c4c1
FB
585 while (i < l) {
586 switch(wsize) {
587 default:
588 case 1:
24e60305 589 v = ldub_p(buf + i);
9307c4c1
FB
590 break;
591 case 2:
24e60305 592 v = lduw_p(buf + i);
9307c4c1
FB
593 break;
594 case 4:
24e60305 595 v = (uint32_t)ldl_p(buf + i);
9307c4c1
FB
596 break;
597 case 8:
24e60305 598 v = ldq_p(buf + i);
9307c4c1
FB
599 break;
600 }
376253ec 601 monitor_printf(mon, " ");
9307c4c1
FB
602 switch(format) {
603 case 'o':
376253ec 604 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
9307c4c1
FB
605 break;
606 case 'x':
376253ec 607 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
9307c4c1
FB
608 break;
609 case 'u':
376253ec 610 monitor_printf(mon, "%*" PRIu64, max_digits, v);
9307c4c1
FB
611 break;
612 case 'd':
376253ec 613 monitor_printf(mon, "%*" PRId64, max_digits, v);
9307c4c1
FB
614 break;
615 case 'c':
376253ec 616 monitor_printc(mon, v);
9307c4c1
FB
617 break;
618 }
619 i += wsize;
620 }
376253ec 621 monitor_printf(mon, "\n");
9307c4c1
FB
622 addr += l;
623 len -= l;
624 }
625}
626
3e5a50d6 627static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
9307c4c1 628{
1bd1442e
LC
629 int count = qdict_get_int(qdict, "count");
630 int format = qdict_get_int(qdict, "format");
631 int size = qdict_get_int(qdict, "size");
632 target_long addr = qdict_get_int(qdict, "addr");
633
376253ec 634 memory_dump(mon, count, format, size, addr, 0);
9307c4c1
FB
635}
636
3e5a50d6 637static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
9307c4c1 638{
1bd1442e
LC
639 int count = qdict_get_int(qdict, "count");
640 int format = qdict_get_int(qdict, "format");
641 int size = qdict_get_int(qdict, "size");
a8170e5e 642 hwaddr addr = qdict_get_int(qdict, "addr");
1bd1442e 643
376253ec 644 memory_dump(mon, count, format, size, addr, 1);
9307c4c1
FB
645}
646
c7f7e697 647void *gpa2hva(MemoryRegion **p_mr, hwaddr addr, uint64_t size, Error **errp)
e9628441 648{
c7f7e697 649 Int128 gpa_region_size;
e9628441 650 MemoryRegionSection mrs = memory_region_find(get_system_memory(),
c7f7e697 651 addr, size);
e9628441
PB
652
653 if (!mrs.mr) {
654 error_setg(errp, "No memory is mapped at address 0x%" HWADDR_PRIx, addr);
655 return NULL;
656 }
657
658 if (!memory_region_is_ram(mrs.mr) && !memory_region_is_romd(mrs.mr)) {
659 error_setg(errp, "Memory at address 0x%" HWADDR_PRIx "is not RAM", addr);
660 memory_region_unref(mrs.mr);
661 return NULL;
662 }
663
c7f7e697
TFF
664 gpa_region_size = int128_make64(size);
665 if (int128_lt(mrs.size, gpa_region_size)) {
666 error_setg(errp, "Size of memory region at 0x%" HWADDR_PRIx
667 " exceeded.", addr);
668 memory_region_unref(mrs.mr);
669 return NULL;
670 }
671
e9628441
PB
672 *p_mr = mrs.mr;
673 return qemu_map_ram_ptr(mrs.mr->ram_block, mrs.offset_within_region);
674}
675
676static void hmp_gpa2hva(Monitor *mon, const QDict *qdict)
677{
678 hwaddr addr = qdict_get_int(qdict, "addr");
679 Error *local_err = NULL;
680 MemoryRegion *mr = NULL;
681 void *ptr;
682
c7f7e697 683 ptr = gpa2hva(&mr, addr, 1, &local_err);
e9628441
PB
684 if (local_err) {
685 error_report_err(local_err);
686 return;
687 }
688
689 monitor_printf(mon, "Host virtual address for 0x%" HWADDR_PRIx
690 " (%s) is %p\n",
691 addr, mr->name, ptr);
692
693 memory_region_unref(mr);
694}
695
574d9693
DDAG
696static void hmp_gva2gpa(Monitor *mon, const QDict *qdict)
697{
698 target_ulong addr = qdict_get_int(qdict, "addr");
699 MemTxAttrs attrs;
2fc5d01b 700 CPUState *cs = mon_get_cpu(mon);
574d9693
DDAG
701 hwaddr gpa;
702
703 if (!cs) {
704 monitor_printf(mon, "No cpu\n");
705 return;
706 }
707
9d3250d5 708 gpa = cpu_get_phys_page_attrs_debug(cs, addr & TARGET_PAGE_MASK, &attrs);
574d9693
DDAG
709 if (gpa == -1) {
710 monitor_printf(mon, "Unmapped\n");
711 } else {
712 monitor_printf(mon, "gpa: %#" HWADDR_PRIx "\n",
713 gpa + (addr & ~TARGET_PAGE_MASK));
714 }
715}
716
e9628441
PB
717#ifdef CONFIG_LINUX
718static uint64_t vtop(void *ptr, Error **errp)
719{
720 uint64_t pinfo;
721 uint64_t ret = -1;
722 uintptr_t addr = (uintptr_t) ptr;
038adc2f 723 uintptr_t pagesize = qemu_real_host_page_size;
e9628441
PB
724 off_t offset = addr / pagesize * sizeof(pinfo);
725 int fd;
726
727 fd = open("/proc/self/pagemap", O_RDONLY);
728 if (fd == -1) {
729 error_setg_errno(errp, errno, "Cannot open /proc/self/pagemap");
730 return -1;
731 }
732
733 /* Force copy-on-write if necessary. */
d73415a3 734 qatomic_add((uint8_t *)ptr, 0);
e9628441
PB
735
736 if (pread(fd, &pinfo, sizeof(pinfo), offset) != sizeof(pinfo)) {
737 error_setg_errno(errp, errno, "Cannot read pagemap");
738 goto out;
739 }
740 if ((pinfo & (1ull << 63)) == 0) {
741 error_setg(errp, "Page not present");
742 goto out;
743 }
744 ret = ((pinfo & 0x007fffffffffffffull) * pagesize) | (addr & (pagesize - 1));
745
746out:
747 close(fd);
748 return ret;
749}
750
751static void hmp_gpa2hpa(Monitor *mon, const QDict *qdict)
752{
753 hwaddr addr = qdict_get_int(qdict, "addr");
754 Error *local_err = NULL;
755 MemoryRegion *mr = NULL;
756 void *ptr;
757 uint64_t physaddr;
758
c7f7e697 759 ptr = gpa2hva(&mr, addr, 1, &local_err);
e9628441
PB
760 if (local_err) {
761 error_report_err(local_err);
762 return;
763 }
764
765 physaddr = vtop(ptr, &local_err);
766 if (local_err) {
767 error_report_err(local_err);
768 } else {
769 monitor_printf(mon, "Host physical address for 0x%" HWADDR_PRIx
770 " (%s) is 0x%" PRIx64 "\n",
771 addr, mr->name, (uint64_t) physaddr);
772 }
773
774 memory_region_unref(mr);
775}
776#endif
777
1bd1442e 778static void do_print(Monitor *mon, const QDict *qdict)
9307c4c1 779{
1bd1442e 780 int format = qdict_get_int(qdict, "format");
a8170e5e 781 hwaddr val = qdict_get_int(qdict, "val");
1bd1442e 782
9307c4c1
FB
783 switch(format) {
784 case 'o':
a8170e5e 785 monitor_printf(mon, "%#" HWADDR_PRIo, val);
9307c4c1
FB
786 break;
787 case 'x':
a8170e5e 788 monitor_printf(mon, "%#" HWADDR_PRIx, val);
9307c4c1
FB
789 break;
790 case 'u':
a8170e5e 791 monitor_printf(mon, "%" HWADDR_PRIu, val);
9307c4c1
FB
792 break;
793 default:
794 case 'd':
a8170e5e 795 monitor_printf(mon, "%" HWADDR_PRId, val);
9307c4c1
FB
796 break;
797 case 'c':
376253ec 798 monitor_printc(mon, val);
9307c4c1
FB
799 break;
800 }
376253ec 801 monitor_printf(mon, "\n");
9307c4c1
FB
802}
803
3e5a50d6 804static void hmp_sum(Monitor *mon, const QDict *qdict)
e4cf1adc
FB
805{
806 uint32_t addr;
e4cf1adc 807 uint16_t sum;
f18c16de
LC
808 uint32_t start = qdict_get_int(qdict, "start");
809 uint32_t size = qdict_get_int(qdict, "size");
e4cf1adc
FB
810
811 sum = 0;
812 for(addr = start; addr < (start + size); addr++) {
42874d3a
PM
813 uint8_t val = address_space_ldub(&address_space_memory, addr,
814 MEMTXATTRS_UNSPECIFIED, NULL);
e4cf1adc
FB
815 /* BSD sum algorithm ('sum' Unix command) */
816 sum = (sum >> 1) | (sum << 15);
54f7b4a3 817 sum += val;
e4cf1adc 818 }
376253ec 819 monitor_printf(mon, "%05d\n", sum);
e4cf1adc
FB
820}
821
13224a87
FB
822static int mouse_button_state;
823
3e5a50d6 824static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
13224a87 825{
c751a74a 826 int dx, dy, dz, button;
1d4daa91
LC
827 const char *dx_str = qdict_get_str(qdict, "dx_str");
828 const char *dy_str = qdict_get_str(qdict, "dy_str");
829 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
c751a74a 830
13224a87
FB
831 dx = strtol(dx_str, NULL, 0);
832 dy = strtol(dy_str, NULL, 0);
c751a74a
GH
833 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
834 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
835
836 if (dz_str) {
13224a87 837 dz = strtol(dz_str, NULL, 0);
c751a74a 838 if (dz != 0) {
f22d0af0 839 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
c751a74a
GH
840 qemu_input_queue_btn(NULL, button, true);
841 qemu_input_event_sync();
842 qemu_input_queue_btn(NULL, button, false);
843 }
844 }
845 qemu_input_event_sync();
13224a87
FB
846}
847
3e5a50d6 848static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
13224a87 849{
7fb1cf16 850 static uint32_t bmap[INPUT_BUTTON__MAX] = {
c751a74a
GH
851 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
852 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
853 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
854 };
d54908a5 855 int button_state = qdict_get_int(qdict, "button_state");
c751a74a
GH
856
857 if (mouse_button_state == button_state) {
858 return;
859 }
860 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
861 qemu_input_event_sync();
13224a87 862 mouse_button_state = button_state;
13224a87
FB
863}
864
3e5a50d6 865static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
3440557b 866{
aa93e39c
LC
867 int size = qdict_get_int(qdict, "size");
868 int addr = qdict_get_int(qdict, "addr");
869 int has_index = qdict_haskey(qdict, "index");
3440557b
FB
870 uint32_t val;
871 int suffix;
872
873 if (has_index) {
aa93e39c 874 int index = qdict_get_int(qdict, "index");
afcea8cb 875 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
3440557b
FB
876 addr++;
877 }
878 addr &= 0xffff;
879
880 switch(size) {
881 default:
882 case 1:
afcea8cb 883 val = cpu_inb(addr);
3440557b
FB
884 suffix = 'b';
885 break;
886 case 2:
afcea8cb 887 val = cpu_inw(addr);
3440557b
FB
888 suffix = 'w';
889 break;
890 case 4:
afcea8cb 891 val = cpu_inl(addr);
3440557b
FB
892 suffix = 'l';
893 break;
894 }
33b1fa94 895 monitor_printf(mon, "port%c[0x%04x] = 0x%0*x\n",
376253ec 896 suffix, addr, size * 2, val);
3440557b 897}
a3a91a35 898
3e5a50d6 899static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
f114784f 900{
1bd1442e
LC
901 int size = qdict_get_int(qdict, "size");
902 int addr = qdict_get_int(qdict, "addr");
903 int val = qdict_get_int(qdict, "val");
904
f114784f
JK
905 addr &= IOPORTS_MASK;
906
907 switch (size) {
908 default:
909 case 1:
afcea8cb 910 cpu_outb(addr, val);
f114784f
JK
911 break;
912 case 2:
afcea8cb 913 cpu_outw(addr, val);
f114784f
JK
914 break;
915 case 4:
afcea8cb 916 cpu_outl(addr, val);
f114784f
JK
917 break;
918 }
919}
920
3e5a50d6 921static void hmp_boot_set(Monitor *mon, const QDict *qdict)
0ecdffbb 922{
f1839938 923 Error *local_err = NULL;
d54908a5 924 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
0ecdffbb 925
f1839938
GA
926 qemu_boot_set(bootdevice, &local_err);
927 if (local_err) {
193227f9 928 error_report_err(local_err);
0ecdffbb 929 } else {
f1839938 930 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
0ecdffbb
AJ
931 }
932}
933
1ce6be24 934static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
314e2987 935{
57bb40c9 936 bool flatview = qdict_get_try_bool(qdict, "flatview", false);
5e8fd947 937 bool dispatch_tree = qdict_get_try_bool(qdict, "dispatch_tree", false);
fc051ae6 938 bool owner = qdict_get_try_bool(qdict, "owner", false);
2261d393 939 bool disabled = qdict_get_try_bool(qdict, "disabled", false);
57bb40c9 940
2261d393 941 mtree_info(flatview, dispatch_tree, owner, disabled);
314e2987
BS
942}
943
ec36b695 944/* Capture support */
72cf2d4f 945static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
ec36b695 946
1ce6be24 947static void hmp_info_capture(Monitor *mon, const QDict *qdict)
ec36b695
FB
948{
949 int i;
950 CaptureState *s;
951
952 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
376253ec 953 monitor_printf(mon, "[%d]: ", i);
ec36b695
FB
954 s->ops.info (s->opaque);
955 }
956}
957
3e5a50d6 958static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
ec36b695
FB
959{
960 int i;
d54908a5 961 int n = qdict_get_int(qdict, "n");
ec36b695
FB
962 CaptureState *s;
963
964 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
965 if (i == n) {
966 s->ops.destroy (s->opaque);
72cf2d4f 967 QLIST_REMOVE (s, entries);
7267c094 968 g_free (s);
ec36b695
FB
969 return;
970 }
971 }
972}
973
3e5a50d6 974static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
c1925484
LC
975{
976 const char *path = qdict_get_str(qdict, "path");
f0b9f36d
KZ
977 int freq = qdict_get_try_int(qdict, "freq", 44100);
978 int bits = qdict_get_try_int(qdict, "bits", 16);
979 int nchannels = qdict_get_try_int(qdict, "nchannels", 2);
980 const char *audiodev = qdict_get_str(qdict, "audiodev");
ec36b695 981 CaptureState *s;
f0b9f36d 982 AudioState *as = audio_state_by_name(audiodev);
ec36b695 983
f0b9f36d
KZ
984 if (!as) {
985 monitor_printf(mon, "Audiodev '%s' not found\n", audiodev);
986 return;
987 }
ec36b695 988
f0b9f36d 989 s = g_malloc0 (sizeof (*s));
ec36b695 990
f0b9f36d 991 if (wav_start_capture(as, s, path, freq, bits, nchannels)) {
d00b2618 992 monitor_printf(mon, "Failed to add wave capture\n");
7267c094 993 g_free (s);
d00b2618 994 return;
ec36b695 995 }
72cf2d4f 996 QLIST_INSERT_HEAD (&capture_head, s, entries);
ec36b695 997}
ec36b695 998
208c9d1b 999void qmp_getfd(const char *fdname, Error **errp)
f07918fd 1000{
947e4744 1001 Monitor *cur_mon = monitor_cur();
c227f099 1002 mon_fd_t *monfd;
9409fc05 1003 int fd, tmp_fd;
f07918fd 1004
5345fdb4 1005 fd = qemu_chr_fe_get_msgfd(&cur_mon->chr);
f07918fd 1006 if (fd == -1) {
f820af87 1007 error_setg(errp, "No file descriptor supplied via SCM_RIGHTS");
208c9d1b 1008 return;
f07918fd
MM
1009 }
1010
1011 if (qemu_isdigit(fdname[0])) {
0b9f0e2f 1012 close(fd);
c6bd8c70
MA
1013 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
1014 "a name not starting with a digit");
208c9d1b 1015 return;
f07918fd
MM
1016 }
1017
0210c3b3 1018 QEMU_LOCK_GUARD(&cur_mon->mon_lock);
208c9d1b 1019 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
f07918fd
MM
1020 if (strcmp(monfd->name, fdname) != 0) {
1021 continue;
1022 }
1023
9409fc05 1024 tmp_fd = monfd->fd;
f07918fd 1025 monfd->fd = fd;
774a6b67 1026 /* Make sure close() is outside critical section */
9409fc05 1027 close(tmp_fd);
208c9d1b 1028 return;
f07918fd
MM
1029 }
1030
7267c094
AL
1031 monfd = g_malloc0(sizeof(mon_fd_t));
1032 monfd->name = g_strdup(fdname);
f07918fd
MM
1033 monfd->fd = fd;
1034
208c9d1b 1035 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
f07918fd
MM
1036}
1037
208c9d1b 1038void qmp_closefd(const char *fdname, Error **errp)
f07918fd 1039{
947e4744 1040 Monitor *cur_mon = monitor_cur();
c227f099 1041 mon_fd_t *monfd;
9409fc05 1042 int tmp_fd;
f07918fd 1043
9409fc05 1044 qemu_mutex_lock(&cur_mon->mon_lock);
208c9d1b 1045 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
f07918fd
MM
1046 if (strcmp(monfd->name, fdname) != 0) {
1047 continue;
1048 }
1049
72cf2d4f 1050 QLIST_REMOVE(monfd, next);
9409fc05 1051 tmp_fd = monfd->fd;
7267c094
AL
1052 g_free(monfd->name);
1053 g_free(monfd);
9409fc05 1054 qemu_mutex_unlock(&cur_mon->mon_lock);
774a6b67 1055 /* Make sure close() is outside critical section */
9409fc05 1056 close(tmp_fd);
208c9d1b 1057 return;
f07918fd
MM
1058 }
1059
9409fc05 1060 qemu_mutex_unlock(&cur_mon->mon_lock);
f820af87 1061 error_setg(errp, "File descriptor named '%s' not found", fdname);
f07918fd
MM
1062}
1063
a9940fc4 1064int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
7768e04c 1065{
c227f099 1066 mon_fd_t *monfd;
7768e04c 1067
0210c3b3 1068 QEMU_LOCK_GUARD(&mon->mon_lock);
72cf2d4f 1069 QLIST_FOREACH(monfd, &mon->fds, next) {
7768e04c
MM
1070 int fd;
1071
1072 if (strcmp(monfd->name, fdname) != 0) {
1073 continue;
1074 }
1075
1076 fd = monfd->fd;
1077
1078 /* caller takes ownership of fd */
72cf2d4f 1079 QLIST_REMOVE(monfd, next);
7267c094
AL
1080 g_free(monfd->name);
1081 g_free(monfd);
7768e04c
MM
1082
1083 return fd;
1084 }
1085
a9940fc4 1086 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
7768e04c
MM
1087 return -1;
1088}
1089
ba1c048a
CB
1090static void monitor_fdset_cleanup(MonFdset *mon_fdset)
1091{
1092 MonFdsetFd *mon_fdset_fd;
1093 MonFdsetFd *mon_fdset_fd_next;
1094
1095 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
ebe52b59
CB
1096 if ((mon_fdset_fd->removed ||
1097 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
1098 runstate_is_running()) {
ba1c048a
CB
1099 close(mon_fdset_fd->fd);
1100 g_free(mon_fdset_fd->opaque);
1101 QLIST_REMOVE(mon_fdset_fd, next);
1102 g_free(mon_fdset_fd);
1103 }
1104 }
1105
adb696f3 1106 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
ba1c048a
CB
1107 QLIST_REMOVE(mon_fdset, next);
1108 g_free(mon_fdset);
1109 }
1110}
1111
7e3c0dea 1112void monitor_fdsets_cleanup(void)
efb87c16
CB
1113{
1114 MonFdset *mon_fdset;
1115 MonFdset *mon_fdset_next;
1116
0210c3b3 1117 QEMU_LOCK_GUARD(&mon_fdsets_lock);
efb87c16
CB
1118 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
1119 monitor_fdset_cleanup(mon_fdset);
1120 }
1121}
1122
ba1c048a
CB
1123AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
1124 const char *opaque, Error **errp)
1125{
1126 int fd;
947e4744 1127 Monitor *mon = monitor_cur();
ba1c048a
CB
1128 AddfdInfo *fdinfo;
1129
5345fdb4 1130 fd = qemu_chr_fe_get_msgfd(&mon->chr);
ba1c048a 1131 if (fd == -1) {
f820af87 1132 error_setg(errp, "No file descriptor supplied via SCM_RIGHTS");
ba1c048a
CB
1133 goto error;
1134 }
1135
e446f70d
CB
1136 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
1137 has_opaque, opaque, errp);
1138 if (fdinfo) {
1139 return fdinfo;
ba1c048a 1140 }
ba1c048a
CB
1141
1142error:
1143 if (fd != -1) {
1144 close(fd);
1145 }
1146 return NULL;
1147}
1148
1149void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
1150{
1151 MonFdset *mon_fdset;
1152 MonFdsetFd *mon_fdset_fd;
1153 char fd_str[60];
1154
0210c3b3 1155 QEMU_LOCK_GUARD(&mon_fdsets_lock);
ba1c048a
CB
1156 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1157 if (mon_fdset->id != fdset_id) {
1158 continue;
1159 }
1160 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1161 if (has_fd) {
1162 if (mon_fdset_fd->fd != fd) {
1163 continue;
1164 }
1165 mon_fdset_fd->removed = true;
1166 break;
1167 } else {
1168 mon_fdset_fd->removed = true;
1169 }
1170 }
1171 if (has_fd && !mon_fdset_fd) {
1172 goto error;
1173 }
1174 monitor_fdset_cleanup(mon_fdset);
1175 return;
1176 }
1177
1178error:
1179 if (has_fd) {
1180 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
1181 fdset_id, fd);
1182 } else {
1183 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
1184 }
f820af87 1185 error_setg(errp, "File descriptor named '%s' not found", fd_str);
ba1c048a
CB
1186}
1187
1188FdsetInfoList *qmp_query_fdsets(Error **errp)
1189{
1190 MonFdset *mon_fdset;
1191 MonFdsetFd *mon_fdset_fd;
1192 FdsetInfoList *fdset_list = NULL;
1193
0210c3b3 1194 QEMU_LOCK_GUARD(&mon_fdsets_lock);
ba1c048a 1195 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
54aa3de7 1196 FdsetInfo *fdset_info = g_malloc0(sizeof(*fdset_info));
ba1c048a 1197
54aa3de7 1198 fdset_info->fdset_id = mon_fdset->id;
ba1c048a
CB
1199
1200 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
54aa3de7 1201 FdsetFdInfo *fdsetfd_info;
ba1c048a
CB
1202
1203 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
54aa3de7 1204 fdsetfd_info->fd = mon_fdset_fd->fd;
ba1c048a 1205 if (mon_fdset_fd->opaque) {
54aa3de7
EB
1206 fdsetfd_info->has_opaque = true;
1207 fdsetfd_info->opaque = g_strdup(mon_fdset_fd->opaque);
ba1c048a 1208 } else {
54aa3de7 1209 fdsetfd_info->has_opaque = false;
ba1c048a
CB
1210 }
1211
54aa3de7 1212 QAPI_LIST_PREPEND(fdset_info->fds, fdsetfd_info);
ba1c048a
CB
1213 }
1214
54aa3de7 1215 QAPI_LIST_PREPEND(fdset_list, fdset_info);
ba1c048a
CB
1216 }
1217
1218 return fdset_list;
1219}
1220
e446f70d
CB
1221AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
1222 bool has_opaque, const char *opaque,
1223 Error **errp)
1224{
1225 MonFdset *mon_fdset = NULL;
1226 MonFdsetFd *mon_fdset_fd;
1227 AddfdInfo *fdinfo;
1228
6e8a355d 1229 QEMU_LOCK_GUARD(&mon_fdsets_lock);
e446f70d
CB
1230 if (has_fdset_id) {
1231 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1232 /* Break if match found or match impossible due to ordering by ID */
1233 if (fdset_id <= mon_fdset->id) {
1234 if (fdset_id < mon_fdset->id) {
1235 mon_fdset = NULL;
1236 }
1237 break;
1238 }
1239 }
1240 }
1241
1242 if (mon_fdset == NULL) {
1243 int64_t fdset_id_prev = -1;
1244 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
1245
1246 if (has_fdset_id) {
1247 if (fdset_id < 0) {
c6bd8c70
MA
1248 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
1249 "a non-negative value");
e446f70d
CB
1250 return NULL;
1251 }
1252 /* Use specified fdset ID */
1253 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1254 mon_fdset_cur = mon_fdset;
1255 if (fdset_id < mon_fdset_cur->id) {
1256 break;
1257 }
1258 }
1259 } else {
1260 /* Use first available fdset ID */
1261 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1262 mon_fdset_cur = mon_fdset;
1263 if (fdset_id_prev == mon_fdset_cur->id - 1) {
1264 fdset_id_prev = mon_fdset_cur->id;
1265 continue;
1266 }
1267 break;
1268 }
1269 }
1270
1271 mon_fdset = g_malloc0(sizeof(*mon_fdset));
1272 if (has_fdset_id) {
1273 mon_fdset->id = fdset_id;
1274 } else {
1275 mon_fdset->id = fdset_id_prev + 1;
1276 }
1277
1278 /* The fdset list is ordered by fdset ID */
1279 if (!mon_fdset_cur) {
1280 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
1281 } else if (mon_fdset->id < mon_fdset_cur->id) {
1282 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
1283 } else {
1284 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
1285 }
1286 }
1287
1288 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
1289 mon_fdset_fd->fd = fd;
1290 mon_fdset_fd->removed = false;
1291 if (has_opaque) {
1292 mon_fdset_fd->opaque = g_strdup(opaque);
1293 }
1294 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
1295
1296 fdinfo = g_malloc0(sizeof(*fdinfo));
1297 fdinfo->fdset_id = mon_fdset->id;
1298 fdinfo->fd = mon_fdset_fd->fd;
1299
1300 return fdinfo;
1301}
1302
60efffa4 1303int monitor_fdset_dup_fd_add(int64_t fdset_id, int flags)
adb696f3 1304{
47451466
PX
1305#ifdef _WIN32
1306 return -ENOENT;
1307#else
adb696f3 1308 MonFdset *mon_fdset;
adb696f3 1309
0210c3b3 1310 QEMU_LOCK_GUARD(&mon_fdsets_lock);
adb696f3 1311 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
60efffa4
DB
1312 MonFdsetFd *mon_fdset_fd;
1313 MonFdsetFd *mon_fdset_fd_dup;
1314 int fd = -1;
1315 int dup_fd;
1316 int mon_fd_flags;
1317
adb696f3
CB
1318 if (mon_fdset->id != fdset_id) {
1319 continue;
1320 }
60efffa4 1321
adb696f3
CB
1322 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1323 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
1324 if (mon_fd_flags == -1) {
60efffa4 1325 return -1;
adb696f3
CB
1326 }
1327
1328 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
60efffa4
DB
1329 fd = mon_fdset_fd->fd;
1330 break;
adb696f3
CB
1331 }
1332 }
adb696f3 1333
60efffa4 1334 if (fd == -1) {
60efffa4
DB
1335 errno = EACCES;
1336 return -1;
adb696f3 1337 }
60efffa4
DB
1338
1339 dup_fd = qemu_dup_flags(fd, flags);
1340 if (dup_fd == -1) {
60efffa4 1341 return -1;
adb696f3 1342 }
60efffa4 1343
adb696f3
CB
1344 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
1345 mon_fdset_fd_dup->fd = dup_fd;
1346 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
60efffa4 1347 return dup_fd;
adb696f3 1348 }
47451466 1349
60efffa4 1350 errno = ENOENT;
adb696f3 1351 return -1;
60efffa4 1352#endif
adb696f3
CB
1353}
1354
854f63d4 1355static int64_t monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
adb696f3
CB
1356{
1357 MonFdset *mon_fdset;
1358 MonFdsetFd *mon_fdset_fd_dup;
1359
0210c3b3 1360 QEMU_LOCK_GUARD(&mon_fdsets_lock);
adb696f3
CB
1361 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1362 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
1363 if (mon_fdset_fd_dup->fd == dup_fd) {
1364 if (remove) {
1365 QLIST_REMOVE(mon_fdset_fd_dup, next);
a661614d 1366 g_free(mon_fdset_fd_dup);
adb696f3
CB
1367 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
1368 monitor_fdset_cleanup(mon_fdset);
1369 }
0210c3b3 1370 return -1;
b3dd1b8c
MT
1371 } else {
1372 return mon_fdset->id;
adb696f3 1373 }
adb696f3
CB
1374 }
1375 }
1376 }
47451466 1377
adb696f3
CB
1378 return -1;
1379}
1380
854f63d4 1381int64_t monitor_fdset_dup_fd_find(int dup_fd)
adb696f3
CB
1382{
1383 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
1384}
1385
b3dd1b8c 1386void monitor_fdset_dup_fd_remove(int dup_fd)
adb696f3 1387{
b3dd1b8c 1388 monitor_fdset_dup_fd_find_remove(dup_fd, true);
adb696f3
CB
1389}
1390
1677f4c6 1391int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
5906366e
LE
1392{
1393 int fd;
1394 Error *local_err = NULL;
a96ed02f 1395
5906366e 1396 if (!qemu_isdigit(fdname[0]) && mon) {
a9940fc4 1397 fd = monitor_get_fd(mon, fdname, &local_err);
5906366e
LE
1398 } else {
1399 fd = qemu_parse_fd(fdname);
a96ed02f 1400 if (fd == -1) {
5906366e
LE
1401 error_setg(&local_err, "Invalid file descriptor number '%s'",
1402 fdname);
a96ed02f 1403 }
5906366e
LE
1404 }
1405 if (local_err) {
1406 error_propagate(errp, local_err);
1407 assert(fd == -1);
a96ed02f 1408 } else {
5906366e 1409 assert(fd != -1);
a96ed02f
NB
1410 }
1411
1412 return fd;
1413}
1414
acd0a093 1415/* Please update hmp-commands.hx when adding or changing commands */
a0cd5e1c 1416static HMPCommand hmp_info_cmds[] = {
da76ee76
PB
1417#include "hmp-commands-info.h"
1418 { NULL, NULL, },
9dc39cba
FB
1419};
1420
a0cd5e1c 1421/* hmp_cmds and hmp_info_cmds would be sorted at runtime */
ed7bda5d 1422HMPCommand hmp_cmds[] = {
a13ced59
WX
1423#include "hmp-commands.h"
1424 { NULL, NULL, },
1425};
1426
ed7bda5d
KW
1427/*
1428 * Set @pval to the value in the register identified by @name.
1429 * return 0 if OK, -1 if not found
1430 */
2fc5d01b 1431int get_monitor_def(Monitor *mon, int64_t *pval, const char *name)
9307c4c1 1432{
bf957284 1433 const MonitorDef *md = target_monitor_defs();
2fc5d01b 1434 CPUState *cs = mon_get_cpu(mon);
92a31b1f 1435 void *ptr;
0a9516c2
AK
1436 uint64_t tmp = 0;
1437 int ret;
92a31b1f 1438
854e67fe 1439 if (cs == NULL || md == NULL) {
bf957284
PB
1440 return -1;
1441 }
1442
1443 for(; md->name != NULL; md++) {
ed7bda5d 1444 if (hmp_compare_cmd(name, md->name)) {
9307c4c1 1445 if (md->get_value) {
43cf067f 1446 *pval = md->get_value(mon, md, md->offset);
9307c4c1 1447 } else {
e7cff9c6 1448 CPUArchState *env = mon_get_cpu_env(mon);
6a00d601 1449 ptr = (uint8_t *)env + md->offset;
92a31b1f
FB
1450 switch(md->type) {
1451 case MD_I32:
1452 *pval = *(int32_t *)ptr;
1453 break;
1454 case MD_TLONG:
1455 *pval = *(target_long *)ptr;
1456 break;
1457 default:
1458 *pval = 0;
1459 break;
1460 }
9307c4c1
FB
1461 }
1462 return 0;
1463 }
1464 }
0a9516c2 1465
854e67fe 1466 ret = target_get_monitor_def(cs, name, &tmp);
0a9516c2
AK
1467 if (!ret) {
1468 *pval = (target_long) tmp;
1469 }
1470
1471 return ret;
9307c4c1
FB
1472}
1473
40d19394
HB
1474static void add_completion_option(ReadLineState *rs, const char *str,
1475 const char *option)
1476{
1477 if (!str || !option) {
1478 return;
1479 }
1480 if (!strncmp(option, str, strlen(str))) {
1481 readline_add_completion(rs, option);
1482 }
1483}
1484
13e315da
HB
1485void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
1486{
1487 size_t len;
1488 ChardevBackendInfoList *list, *start;
1489
1490 if (nb_args != 2) {
1491 return;
1492 }
1493 len = strlen(str);
1494 readline_set_completion_index(rs, len);
1495
1496 start = list = qmp_query_chardev_backends(NULL);
1497 while (list) {
1498 const char *chr_name = list->value->name;
1499
1500 if (!strncmp(chr_name, str, len)) {
1501 readline_add_completion(rs, chr_name);
1502 }
1503 list = list->next;
1504 }
1505 qapi_free_ChardevBackendInfoList(start);
1506}
1507
b162b49a
HB
1508void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
1509{
1510 size_t len;
1511 int i;
1512
1513 if (nb_args != 2) {
1514 return;
1515 }
1516 len = strlen(str);
1517 readline_set_completion_index(rs, len);
1c236ba5 1518 for (i = 0; i < NET_CLIENT_DRIVER__MAX; i++) {
977c736f 1519 add_completion_option(rs, str, NetClientDriver_str(i));
b162b49a
HB
1520 }
1521}
1522
2da1b3ab 1523void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
992d3e64
HB
1524{
1525 GSList *list, *elt;
1526 size_t len;
1527
2da1b3ab
HB
1528 if (nb_args != 2) {
1529 return;
1530 }
1531
992d3e64
HB
1532 len = strlen(str);
1533 readline_set_completion_index(rs, len);
1534 list = elt = object_class_get_list(TYPE_DEVICE, false);
1535 while (elt) {
1536 const char *name;
1537 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
1538 TYPE_DEVICE);
1539 name = object_class_get_name(OBJECT_CLASS(dc));
2da1b3ab 1540
e90f2a8c 1541 if (dc->user_creatable
2da1b3ab 1542 && !strncmp(name, str, len)) {
992d3e64
HB
1543 readline_add_completion(rs, name);
1544 }
1545 elt = elt->next;
1546 }
1547 g_slist_free(list);
1548}
1549
bfa40f77 1550void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
1094fd3a
HB
1551{
1552 GSList *list, *elt;
1553 size_t len;
1554
bfa40f77
HB
1555 if (nb_args != 2) {
1556 return;
1557 }
1558
1094fd3a
HB
1559 len = strlen(str);
1560 readline_set_completion_index(rs, len);
1561 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
1562 while (elt) {
1563 const char *name;
1564
1565 name = object_class_get_name(OBJECT_CLASS(elt->data));
1566 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
1567 readline_add_completion(rs, name);
1568 }
1569 elt = elt->next;
1570 }
1571 g_slist_free(list);
1572}
1573
91590159
MAL
1574static int qdev_add_hotpluggable_device(Object *obj, void *opaque)
1575{
1576 GSList **list = opaque;
688ffbb4 1577 DeviceState *dev = (DeviceState *)object_dynamic_cast(obj, TYPE_DEVICE);
91590159
MAL
1578
1579 if (dev == NULL) {
1580 return 0;
1581 }
1582
1583 if (dev->realized && object_property_get_bool(obj, "hotpluggable", NULL)) {
1584 *list = g_slist_append(*list, dev);
1585 }
1586
1587 return 0;
1588}
1589
1590static GSList *qdev_build_hotpluggable_device_list(Object *peripheral)
1591{
1592 GSList *list = NULL;
1593
1594 object_child_foreach(peripheral, qdev_add_hotpluggable_device, &list);
1595
1596 return list;
1597}
1598
6a1fa9f5
ZG
1599static void peripheral_device_del_completion(ReadLineState *rs,
1600 const char *str, size_t len)
1601{
4cae4d5a
MA
1602 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
1603 GSList *list, *item;
6a1fa9f5 1604
4cae4d5a
MA
1605 list = qdev_build_hotpluggable_device_list(peripheral);
1606 if (!list) {
6a1fa9f5
ZG
1607 return;
1608 }
1609
6a1fa9f5
ZG
1610 for (item = list; item; item = g_slist_next(item)) {
1611 DeviceState *dev = item->data;
1612
1613 if (dev->id && !strncmp(str, dev->id, len)) {
1614 readline_add_completion(rs, dev->id);
1615 }
1616 }
1617
1618 g_slist_free(list);
1619}
1620
6297d9a2
HB
1621void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
1622{
1623 size_t len;
1624 ChardevInfoList *list, *start;
1625
1626 if (nb_args != 2) {
1627 return;
1628 }
1629 len = strlen(str);
1630 readline_set_completion_index(rs, len);
1631
1632 start = list = qmp_query_chardev(NULL);
1633 while (list) {
1634 ChardevInfo *chr = list->value;
1635
1636 if (!strncmp(chr->label, str, len)) {
1637 readline_add_completion(rs, chr->label);
1638 }
1639 list = list->next;
1640 }
1641 qapi_free_ChardevInfoList(start);
1642}
1643
8e597779
HB
1644static void ringbuf_completion(ReadLineState *rs, const char *str)
1645{
1646 size_t len;
1647 ChardevInfoList *list, *start;
1648
1649 len = strlen(str);
1650 readline_set_completion_index(rs, len);
1651
1652 start = list = qmp_query_chardev(NULL);
1653 while (list) {
1654 ChardevInfo *chr_info = list->value;
1655
1656 if (!strncmp(chr_info->label, str, len)) {
0ec7b3e7 1657 Chardev *chr = qemu_chr_find(chr_info->label);
777357d7 1658 if (chr && CHARDEV_IS_RINGBUF(chr)) {
8e597779
HB
1659 readline_add_completion(rs, chr_info->label);
1660 }
1661 }
1662 list = list->next;
1663 }
1664 qapi_free_ChardevInfoList(start);
1665}
1666
8e597779
HB
1667void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
1668{
1669 if (nb_args != 2) {
1670 return;
1671 }
1672 ringbuf_completion(rs, str);
1673}
1674
2da1b3ab
HB
1675void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
1676{
1677 size_t len;
1678
1679 if (nb_args != 2) {
1680 return;
1681 }
1682
1683 len = strlen(str);
1684 readline_set_completion_index(rs, len);
6a1fa9f5 1685 peripheral_device_del_completion(rs, str, len);
2da1b3ab
HB
1686}
1687
bfa40f77 1688void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
b48fa074
HB
1689{
1690 ObjectPropertyInfoList *list, *start;
1691 size_t len;
1692
bfa40f77
HB
1693 if (nb_args != 2) {
1694 return;
1695 }
b48fa074
HB
1696 len = strlen(str);
1697 readline_set_completion_index(rs, len);
1698
1699 start = list = qmp_qom_list("/objects", NULL);
1700 while (list) {
1701 ObjectPropertyInfo *info = list->value;
1702
1703 if (!strncmp(info->type, "child<", 5)
1704 && !strncmp(info->name, str, len)) {
1705 readline_add_completion(rs, info->name);
1706 }
1707 list = list->next;
1708 }
1709 qapi_free_ObjectPropertyInfoList(start);
1710}
1711
29136cd8
HB
1712void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
1713{
1714 int i;
1715 char *sep;
1716 size_t len;
1717
1718 if (nb_args != 2) {
1719 return;
1720 }
1721 sep = strrchr(str, '-');
1722 if (sep) {
1723 str = sep + 1;
1724 }
1725 len = strlen(str);
1726 readline_set_completion_index(rs, len);
7fb1cf16 1727 for (i = 0; i < Q_KEY_CODE__MAX; i++) {
977c736f
MA
1728 if (!strncmp(str, QKeyCode_str(i), len)) {
1729 readline_add_completion(rs, QKeyCode_str(i));
29136cd8
HB
1730 }
1731 }
1732}
1733
40d19394
HB
1734void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
1735{
1736 size_t len;
1737
1738 len = strlen(str);
1739 readline_set_completion_index(rs, len);
1740 if (nb_args == 2) {
eaed483c 1741 NetClientState *ncs[MAX_QUEUE_NUM];
40d19394
HB
1742 int count, i;
1743 count = qemu_find_net_clients_except(NULL, ncs,
f394b2e2 1744 NET_CLIENT_DRIVER_NONE,
eaed483c 1745 MAX_QUEUE_NUM);
bcfa4d60 1746 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
40d19394
HB
1747 const char *name = ncs[i]->name;
1748 if (!strncmp(str, name, len)) {
1749 readline_add_completion(rs, name);
1750 }
1751 }
1752 } else if (nb_args == 3) {
1753 add_completion_option(rs, str, "on");
1754 add_completion_option(rs, str, "off");
1755 }
1756}
1757
11b389f2
HB
1758void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
1759{
1760 int len, count, i;
eaed483c 1761 NetClientState *ncs[MAX_QUEUE_NUM];
11b389f2
HB
1762
1763 if (nb_args != 2) {
1764 return;
1765 }
1766
1767 len = strlen(str);
1768 readline_set_completion_index(rs, len);
f394b2e2 1769 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_DRIVER_NIC,
eaed483c 1770 MAX_QUEUE_NUM);
bcfa4d60 1771 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
11b389f2
HB
1772 const char *name = ncs[i]->name;
1773 if (strncmp(str, name, len)) {
1774 continue;
1775 }
08712fcb 1776 if (ncs[i]->is_netdev) {
11b389f2
HB
1777 readline_add_completion(rs, name);
1778 }
1779 }
1780}
1781
bd71211d
LV
1782void info_trace_events_completion(ReadLineState *rs, int nb_args, const char *str)
1783{
1784 size_t len;
1785
1786 len = strlen(str);
1787 readline_set_completion_index(rs, len);
1788 if (nb_args == 2) {
0d4e995c
DB
1789 TraceEventIter iter;
1790 TraceEvent *ev;
1791 char *pattern = g_strdup_printf("%s*", str);
117856c3 1792 trace_event_iter_init_pattern(&iter, pattern);
0d4e995c
DB
1793 while ((ev = trace_event_iter_next(&iter)) != NULL) {
1794 readline_add_completion(rs, trace_event_get_name(ev));
bd71211d 1795 }
0d4e995c 1796 g_free(pattern);
bd71211d
LV
1797 }
1798}
1799
987bd270
DDAG
1800void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
1801{
1802 size_t len;
1803
1804 len = strlen(str);
1805 readline_set_completion_index(rs, len);
1806 if (nb_args == 2) {
0d4e995c
DB
1807 TraceEventIter iter;
1808 TraceEvent *ev;
1809 char *pattern = g_strdup_printf("%s*", str);
117856c3 1810 trace_event_iter_init_pattern(&iter, pattern);
0d4e995c
DB
1811 while ((ev = trace_event_iter_next(&iter)) != NULL) {
1812 readline_add_completion(rs, trace_event_get_name(ev));
1813 }
1814 g_free(pattern);
987bd270
DDAG
1815 } else if (nb_args == 3) {
1816 add_completion_option(rs, str, "on");
1817 add_completion_option(rs, str, "off");
1818 }
1819}
1820
d0ece345
HB
1821void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
1822{
4bb08af3
HB
1823 int i;
1824
d0ece345
HB
1825 if (nb_args != 2) {
1826 return;
1827 }
1828 readline_set_completion_index(rs, strlen(str));
14d53b4f
MP
1829 for (i = 0; i < WATCHDOG_ACTION__MAX; i++) {
1830 add_completion_option(rs, str, WatchdogAction_str(i));
4bb08af3 1831 }
d0ece345
HB
1832}
1833
c68a0409
HB
1834void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
1835 const char *str)
1836{
1837 size_t len;
1838
1839 len = strlen(str);
1840 readline_set_completion_index(rs, len);
1841 if (nb_args == 2) {
1842 int i;
7fb1cf16 1843 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
977c736f 1844 const char *name = MigrationCapability_str(i);
c68a0409
HB
1845 if (!strncmp(str, name, len)) {
1846 readline_add_completion(rs, name);
1847 }
1848 }
1849 } else if (nb_args == 3) {
1850 add_completion_option(rs, str, "on");
1851 add_completion_option(rs, str, "off");
1852 }
1853}
1854
50e9a629
LL
1855void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
1856 const char *str)
1857{
1858 size_t len;
1859
1860 len = strlen(str);
1861 readline_set_completion_index(rs, len);
1862 if (nb_args == 2) {
1863 int i;
7fb1cf16 1864 for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
977c736f 1865 const char *name = MigrationParameter_str(i);
50e9a629
LL
1866 if (!strncmp(str, name, len)) {
1867 readline_add_completion(rs, name);
1868 }
1869 }
1870 }
1871}
1872
b21631f3
HB
1873static void vm_completion(ReadLineState *rs, const char *str)
1874{
1875 size_t len;
7c8eece4 1876 BlockDriverState *bs;
88be7b4b 1877 BdrvNextIterator it;
b21631f3
HB
1878
1879 len = strlen(str);
1880 readline_set_completion_index(rs, len);
7c8eece4 1881
88be7b4b 1882 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
b21631f3 1883 SnapshotInfoList *snapshots, *snapshot;
6bf1faa8
DL
1884 AioContext *ctx = bdrv_get_aio_context(bs);
1885 bool ok = false;
b21631f3 1886
6bf1faa8
DL
1887 aio_context_acquire(ctx);
1888 if (bdrv_can_snapshot(bs)) {
1889 ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0;
b21631f3 1890 }
6bf1faa8
DL
1891 aio_context_release(ctx);
1892 if (!ok) {
b21631f3
HB
1893 continue;
1894 }
6bf1faa8 1895
b21631f3
HB
1896 snapshot = snapshots;
1897 while (snapshot) {
1898 char *completion = snapshot->value->name;
1899 if (!strncmp(str, completion, len)) {
1900 readline_add_completion(rs, completion);
1901 }
1902 completion = snapshot->value->id;
1903 if (!strncmp(str, completion, len)) {
1904 readline_add_completion(rs, completion);
1905 }
1906 snapshot = snapshot->next;
1907 }
1908 qapi_free_SnapshotInfoList(snapshots);
1909 }
1910
1911}
1912
1913void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
1914{
1915 if (nb_args == 2) {
1916 vm_completion(rs, str);
1917 }
1918}
1919
1920void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
1921{
1922 if (nb_args == 2) {
1923 vm_completion(rs, str);
1924 }
1925}
1926
816f8925
WX
1927static int
1928compare_mon_cmd(const void *a, const void *b)
1929{
a0cd5e1c
KW
1930 return strcmp(((const HMPCommand *)a)->name,
1931 ((const HMPCommand *)b)->name);
816f8925
WX
1932}
1933
1934static void sortcmdlist(void)
1935{
a0cd5e1c
KW
1936 qsort(hmp_cmds, ARRAY_SIZE(hmp_cmds) - 1,
1937 sizeof(*hmp_cmds),
1938 compare_mon_cmd);
1939 qsort(hmp_info_cmds, ARRAY_SIZE(hmp_info_cmds) - 1,
1940 sizeof(*hmp_info_cmds),
1941 compare_mon_cmd);
816f8925
WX
1942}
1943
f0e48cbd
GH
1944void monitor_register_hmp(const char *name, bool info,
1945 void (*cmd)(Monitor *mon, const QDict *qdict))
1946{
1947 HMPCommand *table = info ? hmp_info_cmds : hmp_cmds;
1948
1949 while (table->name != NULL) {
1950 if (strcmp(table->name, name) == 0) {
f9429c67 1951 g_assert(table->cmd == NULL && table->cmd_info_hrt == NULL);
f0e48cbd
GH
1952 table->cmd = cmd;
1953 return;
1954 }
1955 table++;
1956 }
1957 g_assert_not_reached();
1958}
1959
f9429c67
DB
1960void monitor_register_hmp_info_hrt(const char *name,
1961 HumanReadableText *(*handler)(Error **errp))
1962{
1963 HMPCommand *table = hmp_info_cmds;
1964
1965 while (table->name != NULL) {
1966 if (strcmp(table->name, name) == 0) {
1967 g_assert(table->cmd == NULL && table->cmd_info_hrt == NULL);
1968 table->cmd_info_hrt = handler;
1969 return;
1970 }
1971 table++;
1972 }
1973 g_assert_not_reached();
1974}
1975
6adf08dd
PX
1976void monitor_init_globals(void)
1977{
1d95db74 1978 monitor_init_globals_core();
6adf08dd 1979 monitor_init_qmp_commands();
6adf08dd 1980 sortcmdlist();
47451466 1981 qemu_mutex_init(&mon_fdsets_lock);
aa455485 1982}