]> git.proxmox.com Git - mirror_qemu.git/blame - monitor/misc.c
util/log: Pass Error pointer to qemu_set_log
[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
e2c7c6a4 422 if (!qemu_set_log_filename(qdict_get_str(qdict, "filename"), &err)) {
daa76aa4
MA
423 error_report_err(err);
424 }
e735b91c
PB
425}
426
3e5a50d6 427static void hmp_log(Monitor *mon, const QDict *qdict)
f193c797
FB
428{
429 int mask;
d54908a5 430 const char *items = qdict_get_str(qdict, "items");
c5955f4f 431 Error *err = NULL;
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 }
c5955f4f
RH
442
443 if (!qemu_set_log(mask, &err)) {
444 error_report_err(err);
445 }
f193c797
FB
446}
447
3e5a50d6 448static void hmp_singlestep(Monitor *mon, const QDict *qdict)
1b530a6d 449{
d54908a5 450 const char *option = qdict_get_try_str(qdict, "option");
1b530a6d
AJ
451 if (!option || !strcmp(option, "on")) {
452 singlestep = 1;
453 } else if (!strcmp(option, "off")) {
454 singlestep = 0;
455 } else {
456 monitor_printf(mon, "unexpected option %s\n", option);
457 }
458}
459
3e5a50d6 460static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
59030a8c 461{
d54908a5 462 const char *device = qdict_get_try_str(qdict, "device");
89854b95 463 if (!device) {
59030a8c 464 device = "tcp::" DEFAULT_GDBSTUB_PORT;
89854b95
YA
465 }
466
59030a8c
AL
467 if (gdbserver_start(device) < 0) {
468 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
469 device);
470 } else if (strcmp(device, "none") == 0) {
36556b20 471 monitor_printf(mon, "Disabled gdbserver\n");
8a7ddc38 472 } else {
59030a8c
AL
473 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
474 device);
8a7ddc38
FB
475 }
476}
477
3e5a50d6 478static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
9dd986cc 479{
22afb46e
PB
480 Error *err = NULL;
481 WatchdogAction action;
482 char *qapi_value;
483
484 qapi_value = g_ascii_strdown(qdict_get_str(qdict, "action"), -1);
485 action = qapi_enum_parse(&WatchdogAction_lookup, qapi_value, -1, &err);
486 g_free(qapi_value);
487 if (err) {
488 hmp_handle_error(mon, err);
489 return;
9dd986cc 490 }
22afb46e 491 qmp_watchdog_set_action(action, &error_abort);
9dd986cc
RJ
492}
493
376253ec 494static void monitor_printc(Monitor *mon, int c)
9307c4c1 495{
376253ec 496 monitor_printf(mon, "'");
9307c4c1
FB
497 switch(c) {
498 case '\'':
376253ec 499 monitor_printf(mon, "\\'");
9307c4c1
FB
500 break;
501 case '\\':
376253ec 502 monitor_printf(mon, "\\\\");
9307c4c1
FB
503 break;
504 case '\n':
376253ec 505 monitor_printf(mon, "\\n");
9307c4c1
FB
506 break;
507 case '\r':
376253ec 508 monitor_printf(mon, "\\r");
9307c4c1
FB
509 break;
510 default:
511 if (c >= 32 && c <= 126) {
376253ec 512 monitor_printf(mon, "%c", c);
9307c4c1 513 } else {
376253ec 514 monitor_printf(mon, "\\x%02x", c);
9307c4c1
FB
515 }
516 break;
517 }
376253ec 518 monitor_printf(mon, "'");
9307c4c1
FB
519}
520
376253ec 521static void memory_dump(Monitor *mon, int count, int format, int wsize,
a8170e5e 522 hwaddr addr, int is_physical)
9307c4c1 523{
23842aab 524 int l, line_size, i, max_digits, len;
9307c4c1
FB
525 uint8_t buf[16];
526 uint64_t v;
2fc5d01b 527 CPUState *cs = mon_get_cpu(mon);
854e67fe
TH
528
529 if (!cs && (format == 'i' || !is_physical)) {
530 monitor_printf(mon, "Can not dump without CPU\n");
531 return;
532 }
9307c4c1
FB
533
534 if (format == 'i') {
1d48474d 535 monitor_disas(mon, cs, addr, count, is_physical);
9307c4c1
FB
536 return;
537 }
538
539 len = wsize * count;
89854b95 540 if (wsize == 1) {
9307c4c1 541 line_size = 8;
89854b95 542 } else {
9307c4c1 543 line_size = 16;
89854b95 544 }
9307c4c1
FB
545 max_digits = 0;
546
547 switch(format) {
548 case 'o':
69db8dfc 549 max_digits = DIV_ROUND_UP(wsize * 8, 3);
9307c4c1
FB
550 break;
551 default:
552 case 'x':
553 max_digits = (wsize * 8) / 4;
554 break;
555 case 'u':
556 case 'd':
69db8dfc 557 max_digits = DIV_ROUND_UP(wsize * 8 * 10, 33);
9307c4c1
FB
558 break;
559 case 'c':
560 wsize = 1;
561 break;
562 }
563
564 while (len > 0) {
89854b95 565 if (is_physical) {
376253ec 566 monitor_printf(mon, TARGET_FMT_plx ":", addr);
89854b95 567 } else {
376253ec 568 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
89854b95 569 }
9307c4c1
FB
570 l = len;
571 if (l > line_size)
572 l = line_size;
573 if (is_physical) {
6f89ae58
PM
574 AddressSpace *as = cs ? cs->as : &address_space_memory;
575 MemTxResult r = address_space_read(as, addr,
576 MEMTXATTRS_UNSPECIFIED, buf, l);
577 if (r != MEMTX_OK) {
578 monitor_printf(mon, " Cannot access memory\n");
579 break;
580 }
9307c4c1 581 } else {
854e67fe 582 if (cpu_memory_rw_debug(cs, addr, buf, l, 0) < 0) {
376253ec 583 monitor_printf(mon, " Cannot access memory\n");
c8f79b67
AL
584 break;
585 }
9307c4c1 586 }
5fafdf24 587 i = 0;
9307c4c1
FB
588 while (i < l) {
589 switch(wsize) {
590 default:
591 case 1:
24e60305 592 v = ldub_p(buf + i);
9307c4c1
FB
593 break;
594 case 2:
24e60305 595 v = lduw_p(buf + i);
9307c4c1
FB
596 break;
597 case 4:
24e60305 598 v = (uint32_t)ldl_p(buf + i);
9307c4c1
FB
599 break;
600 case 8:
24e60305 601 v = ldq_p(buf + i);
9307c4c1
FB
602 break;
603 }
376253ec 604 monitor_printf(mon, " ");
9307c4c1
FB
605 switch(format) {
606 case 'o':
376253ec 607 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
9307c4c1
FB
608 break;
609 case 'x':
376253ec 610 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
9307c4c1
FB
611 break;
612 case 'u':
376253ec 613 monitor_printf(mon, "%*" PRIu64, max_digits, v);
9307c4c1
FB
614 break;
615 case 'd':
376253ec 616 monitor_printf(mon, "%*" PRId64, max_digits, v);
9307c4c1
FB
617 break;
618 case 'c':
376253ec 619 monitor_printc(mon, v);
9307c4c1
FB
620 break;
621 }
622 i += wsize;
623 }
376253ec 624 monitor_printf(mon, "\n");
9307c4c1
FB
625 addr += l;
626 len -= l;
627 }
628}
629
3e5a50d6 630static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
9307c4c1 631{
1bd1442e
LC
632 int count = qdict_get_int(qdict, "count");
633 int format = qdict_get_int(qdict, "format");
634 int size = qdict_get_int(qdict, "size");
635 target_long addr = qdict_get_int(qdict, "addr");
636
376253ec 637 memory_dump(mon, count, format, size, addr, 0);
9307c4c1
FB
638}
639
3e5a50d6 640static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
9307c4c1 641{
1bd1442e
LC
642 int count = qdict_get_int(qdict, "count");
643 int format = qdict_get_int(qdict, "format");
644 int size = qdict_get_int(qdict, "size");
a8170e5e 645 hwaddr addr = qdict_get_int(qdict, "addr");
1bd1442e 646
376253ec 647 memory_dump(mon, count, format, size, addr, 1);
9307c4c1
FB
648}
649
c7f7e697 650void *gpa2hva(MemoryRegion **p_mr, hwaddr addr, uint64_t size, Error **errp)
e9628441 651{
c7f7e697 652 Int128 gpa_region_size;
e9628441 653 MemoryRegionSection mrs = memory_region_find(get_system_memory(),
c7f7e697 654 addr, size);
e9628441
PB
655
656 if (!mrs.mr) {
657 error_setg(errp, "No memory is mapped at address 0x%" HWADDR_PRIx, addr);
658 return NULL;
659 }
660
661 if (!memory_region_is_ram(mrs.mr) && !memory_region_is_romd(mrs.mr)) {
662 error_setg(errp, "Memory at address 0x%" HWADDR_PRIx "is not RAM", addr);
663 memory_region_unref(mrs.mr);
664 return NULL;
665 }
666
c7f7e697
TFF
667 gpa_region_size = int128_make64(size);
668 if (int128_lt(mrs.size, gpa_region_size)) {
669 error_setg(errp, "Size of memory region at 0x%" HWADDR_PRIx
670 " exceeded.", addr);
671 memory_region_unref(mrs.mr);
672 return NULL;
673 }
674
e9628441
PB
675 *p_mr = mrs.mr;
676 return qemu_map_ram_ptr(mrs.mr->ram_block, mrs.offset_within_region);
677}
678
679static void hmp_gpa2hva(Monitor *mon, const QDict *qdict)
680{
681 hwaddr addr = qdict_get_int(qdict, "addr");
682 Error *local_err = NULL;
683 MemoryRegion *mr = NULL;
684 void *ptr;
685
c7f7e697 686 ptr = gpa2hva(&mr, addr, 1, &local_err);
e9628441
PB
687 if (local_err) {
688 error_report_err(local_err);
689 return;
690 }
691
692 monitor_printf(mon, "Host virtual address for 0x%" HWADDR_PRIx
693 " (%s) is %p\n",
694 addr, mr->name, ptr);
695
696 memory_region_unref(mr);
697}
698
574d9693
DDAG
699static void hmp_gva2gpa(Monitor *mon, const QDict *qdict)
700{
701 target_ulong addr = qdict_get_int(qdict, "addr");
702 MemTxAttrs attrs;
2fc5d01b 703 CPUState *cs = mon_get_cpu(mon);
574d9693
DDAG
704 hwaddr gpa;
705
706 if (!cs) {
707 monitor_printf(mon, "No cpu\n");
708 return;
709 }
710
9d3250d5 711 gpa = cpu_get_phys_page_attrs_debug(cs, addr & TARGET_PAGE_MASK, &attrs);
574d9693
DDAG
712 if (gpa == -1) {
713 monitor_printf(mon, "Unmapped\n");
714 } else {
715 monitor_printf(mon, "gpa: %#" HWADDR_PRIx "\n",
716 gpa + (addr & ~TARGET_PAGE_MASK));
717 }
718}
719
e9628441
PB
720#ifdef CONFIG_LINUX
721static uint64_t vtop(void *ptr, Error **errp)
722{
723 uint64_t pinfo;
724 uint64_t ret = -1;
725 uintptr_t addr = (uintptr_t) ptr;
8e3b0cbb 726 uintptr_t pagesize = qemu_real_host_page_size();
e9628441
PB
727 off_t offset = addr / pagesize * sizeof(pinfo);
728 int fd;
729
730 fd = open("/proc/self/pagemap", O_RDONLY);
731 if (fd == -1) {
732 error_setg_errno(errp, errno, "Cannot open /proc/self/pagemap");
733 return -1;
734 }
735
736 /* Force copy-on-write if necessary. */
d73415a3 737 qatomic_add((uint8_t *)ptr, 0);
e9628441
PB
738
739 if (pread(fd, &pinfo, sizeof(pinfo), offset) != sizeof(pinfo)) {
740 error_setg_errno(errp, errno, "Cannot read pagemap");
741 goto out;
742 }
743 if ((pinfo & (1ull << 63)) == 0) {
744 error_setg(errp, "Page not present");
745 goto out;
746 }
747 ret = ((pinfo & 0x007fffffffffffffull) * pagesize) | (addr & (pagesize - 1));
748
749out:
750 close(fd);
751 return ret;
752}
753
754static void hmp_gpa2hpa(Monitor *mon, const QDict *qdict)
755{
756 hwaddr addr = qdict_get_int(qdict, "addr");
757 Error *local_err = NULL;
758 MemoryRegion *mr = NULL;
759 void *ptr;
760 uint64_t physaddr;
761
c7f7e697 762 ptr = gpa2hva(&mr, addr, 1, &local_err);
e9628441
PB
763 if (local_err) {
764 error_report_err(local_err);
765 return;
766 }
767
768 physaddr = vtop(ptr, &local_err);
769 if (local_err) {
770 error_report_err(local_err);
771 } else {
772 monitor_printf(mon, "Host physical address for 0x%" HWADDR_PRIx
773 " (%s) is 0x%" PRIx64 "\n",
774 addr, mr->name, (uint64_t) physaddr);
775 }
776
777 memory_region_unref(mr);
778}
779#endif
780
1bd1442e 781static void do_print(Monitor *mon, const QDict *qdict)
9307c4c1 782{
1bd1442e 783 int format = qdict_get_int(qdict, "format");
a8170e5e 784 hwaddr val = qdict_get_int(qdict, "val");
1bd1442e 785
9307c4c1
FB
786 switch(format) {
787 case 'o':
a8170e5e 788 monitor_printf(mon, "%#" HWADDR_PRIo, val);
9307c4c1
FB
789 break;
790 case 'x':
a8170e5e 791 monitor_printf(mon, "%#" HWADDR_PRIx, val);
9307c4c1
FB
792 break;
793 case 'u':
a8170e5e 794 monitor_printf(mon, "%" HWADDR_PRIu, val);
9307c4c1
FB
795 break;
796 default:
797 case 'd':
a8170e5e 798 monitor_printf(mon, "%" HWADDR_PRId, val);
9307c4c1
FB
799 break;
800 case 'c':
376253ec 801 monitor_printc(mon, val);
9307c4c1
FB
802 break;
803 }
376253ec 804 monitor_printf(mon, "\n");
9307c4c1
FB
805}
806
3e5a50d6 807static void hmp_sum(Monitor *mon, const QDict *qdict)
e4cf1adc
FB
808{
809 uint32_t addr;
e4cf1adc 810 uint16_t sum;
f18c16de
LC
811 uint32_t start = qdict_get_int(qdict, "start");
812 uint32_t size = qdict_get_int(qdict, "size");
e4cf1adc
FB
813
814 sum = 0;
815 for(addr = start; addr < (start + size); addr++) {
42874d3a
PM
816 uint8_t val = address_space_ldub(&address_space_memory, addr,
817 MEMTXATTRS_UNSPECIFIED, NULL);
e4cf1adc
FB
818 /* BSD sum algorithm ('sum' Unix command) */
819 sum = (sum >> 1) | (sum << 15);
54f7b4a3 820 sum += val;
e4cf1adc 821 }
376253ec 822 monitor_printf(mon, "%05d\n", sum);
e4cf1adc
FB
823}
824
13224a87
FB
825static int mouse_button_state;
826
3e5a50d6 827static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
13224a87 828{
c751a74a 829 int dx, dy, dz, button;
1d4daa91
LC
830 const char *dx_str = qdict_get_str(qdict, "dx_str");
831 const char *dy_str = qdict_get_str(qdict, "dy_str");
832 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
c751a74a 833
13224a87
FB
834 dx = strtol(dx_str, NULL, 0);
835 dy = strtol(dy_str, NULL, 0);
c751a74a
GH
836 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
837 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
838
839 if (dz_str) {
13224a87 840 dz = strtol(dz_str, NULL, 0);
c751a74a 841 if (dz != 0) {
f22d0af0 842 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
c751a74a
GH
843 qemu_input_queue_btn(NULL, button, true);
844 qemu_input_event_sync();
845 qemu_input_queue_btn(NULL, button, false);
846 }
847 }
848 qemu_input_event_sync();
13224a87
FB
849}
850
3e5a50d6 851static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
13224a87 852{
7fb1cf16 853 static uint32_t bmap[INPUT_BUTTON__MAX] = {
c751a74a
GH
854 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
855 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
856 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
857 };
d54908a5 858 int button_state = qdict_get_int(qdict, "button_state");
c751a74a
GH
859
860 if (mouse_button_state == button_state) {
861 return;
862 }
863 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
864 qemu_input_event_sync();
13224a87 865 mouse_button_state = button_state;
13224a87
FB
866}
867
3e5a50d6 868static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
3440557b 869{
aa93e39c
LC
870 int size = qdict_get_int(qdict, "size");
871 int addr = qdict_get_int(qdict, "addr");
872 int has_index = qdict_haskey(qdict, "index");
3440557b
FB
873 uint32_t val;
874 int suffix;
875
876 if (has_index) {
aa93e39c 877 int index = qdict_get_int(qdict, "index");
afcea8cb 878 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
3440557b
FB
879 addr++;
880 }
881 addr &= 0xffff;
882
883 switch(size) {
884 default:
885 case 1:
afcea8cb 886 val = cpu_inb(addr);
3440557b
FB
887 suffix = 'b';
888 break;
889 case 2:
afcea8cb 890 val = cpu_inw(addr);
3440557b
FB
891 suffix = 'w';
892 break;
893 case 4:
afcea8cb 894 val = cpu_inl(addr);
3440557b
FB
895 suffix = 'l';
896 break;
897 }
33b1fa94 898 monitor_printf(mon, "port%c[0x%04x] = 0x%0*x\n",
376253ec 899 suffix, addr, size * 2, val);
3440557b 900}
a3a91a35 901
3e5a50d6 902static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
f114784f 903{
1bd1442e
LC
904 int size = qdict_get_int(qdict, "size");
905 int addr = qdict_get_int(qdict, "addr");
906 int val = qdict_get_int(qdict, "val");
907
f114784f
JK
908 addr &= IOPORTS_MASK;
909
910 switch (size) {
911 default:
912 case 1:
afcea8cb 913 cpu_outb(addr, val);
f114784f
JK
914 break;
915 case 2:
afcea8cb 916 cpu_outw(addr, val);
f114784f
JK
917 break;
918 case 4:
afcea8cb 919 cpu_outl(addr, val);
f114784f
JK
920 break;
921 }
922}
923
3e5a50d6 924static void hmp_boot_set(Monitor *mon, const QDict *qdict)
0ecdffbb 925{
f1839938 926 Error *local_err = NULL;
d54908a5 927 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
0ecdffbb 928
f1839938
GA
929 qemu_boot_set(bootdevice, &local_err);
930 if (local_err) {
193227f9 931 error_report_err(local_err);
0ecdffbb 932 } else {
f1839938 933 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
0ecdffbb
AJ
934 }
935}
936
1ce6be24 937static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
314e2987 938{
57bb40c9 939 bool flatview = qdict_get_try_bool(qdict, "flatview", false);
5e8fd947 940 bool dispatch_tree = qdict_get_try_bool(qdict, "dispatch_tree", false);
fc051ae6 941 bool owner = qdict_get_try_bool(qdict, "owner", false);
2261d393 942 bool disabled = qdict_get_try_bool(qdict, "disabled", false);
57bb40c9 943
2261d393 944 mtree_info(flatview, dispatch_tree, owner, disabled);
314e2987
BS
945}
946
ec36b695 947/* Capture support */
72cf2d4f 948static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
ec36b695 949
1ce6be24 950static void hmp_info_capture(Monitor *mon, const QDict *qdict)
ec36b695
FB
951{
952 int i;
953 CaptureState *s;
954
955 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
376253ec 956 monitor_printf(mon, "[%d]: ", i);
ec36b695
FB
957 s->ops.info (s->opaque);
958 }
959}
960
3e5a50d6 961static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
ec36b695
FB
962{
963 int i;
d54908a5 964 int n = qdict_get_int(qdict, "n");
ec36b695
FB
965 CaptureState *s;
966
967 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
968 if (i == n) {
969 s->ops.destroy (s->opaque);
72cf2d4f 970 QLIST_REMOVE (s, entries);
7267c094 971 g_free (s);
ec36b695
FB
972 return;
973 }
974 }
975}
976
3e5a50d6 977static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
c1925484
LC
978{
979 const char *path = qdict_get_str(qdict, "path");
f0b9f36d
KZ
980 int freq = qdict_get_try_int(qdict, "freq", 44100);
981 int bits = qdict_get_try_int(qdict, "bits", 16);
982 int nchannels = qdict_get_try_int(qdict, "nchannels", 2);
983 const char *audiodev = qdict_get_str(qdict, "audiodev");
ec36b695 984 CaptureState *s;
f0b9f36d 985 AudioState *as = audio_state_by_name(audiodev);
ec36b695 986
f0b9f36d
KZ
987 if (!as) {
988 monitor_printf(mon, "Audiodev '%s' not found\n", audiodev);
989 return;
990 }
ec36b695 991
f0b9f36d 992 s = g_malloc0 (sizeof (*s));
ec36b695 993
f0b9f36d 994 if (wav_start_capture(as, s, path, freq, bits, nchannels)) {
d00b2618 995 monitor_printf(mon, "Failed to add wave capture\n");
7267c094 996 g_free (s);
d00b2618 997 return;
ec36b695 998 }
72cf2d4f 999 QLIST_INSERT_HEAD (&capture_head, s, entries);
ec36b695 1000}
ec36b695 1001
208c9d1b 1002void qmp_getfd(const char *fdname, Error **errp)
f07918fd 1003{
947e4744 1004 Monitor *cur_mon = monitor_cur();
c227f099 1005 mon_fd_t *monfd;
9409fc05 1006 int fd, tmp_fd;
f07918fd 1007
5345fdb4 1008 fd = qemu_chr_fe_get_msgfd(&cur_mon->chr);
f07918fd 1009 if (fd == -1) {
f820af87 1010 error_setg(errp, "No file descriptor supplied via SCM_RIGHTS");
208c9d1b 1011 return;
f07918fd
MM
1012 }
1013
1014 if (qemu_isdigit(fdname[0])) {
0b9f0e2f 1015 close(fd);
c6bd8c70
MA
1016 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
1017 "a name not starting with a digit");
208c9d1b 1018 return;
f07918fd
MM
1019 }
1020
0210c3b3 1021 QEMU_LOCK_GUARD(&cur_mon->mon_lock);
208c9d1b 1022 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
f07918fd
MM
1023 if (strcmp(monfd->name, fdname) != 0) {
1024 continue;
1025 }
1026
9409fc05 1027 tmp_fd = monfd->fd;
f07918fd 1028 monfd->fd = fd;
774a6b67 1029 /* Make sure close() is outside critical section */
9409fc05 1030 close(tmp_fd);
208c9d1b 1031 return;
f07918fd
MM
1032 }
1033
b21e2380 1034 monfd = g_new0(mon_fd_t, 1);
7267c094 1035 monfd->name = g_strdup(fdname);
f07918fd
MM
1036 monfd->fd = fd;
1037
208c9d1b 1038 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
f07918fd
MM
1039}
1040
208c9d1b 1041void qmp_closefd(const char *fdname, Error **errp)
f07918fd 1042{
947e4744 1043 Monitor *cur_mon = monitor_cur();
c227f099 1044 mon_fd_t *monfd;
9409fc05 1045 int tmp_fd;
f07918fd 1046
9409fc05 1047 qemu_mutex_lock(&cur_mon->mon_lock);
208c9d1b 1048 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
f07918fd
MM
1049 if (strcmp(monfd->name, fdname) != 0) {
1050 continue;
1051 }
1052
72cf2d4f 1053 QLIST_REMOVE(monfd, next);
9409fc05 1054 tmp_fd = monfd->fd;
7267c094
AL
1055 g_free(monfd->name);
1056 g_free(monfd);
9409fc05 1057 qemu_mutex_unlock(&cur_mon->mon_lock);
774a6b67 1058 /* Make sure close() is outside critical section */
9409fc05 1059 close(tmp_fd);
208c9d1b 1060 return;
f07918fd
MM
1061 }
1062
9409fc05 1063 qemu_mutex_unlock(&cur_mon->mon_lock);
f820af87 1064 error_setg(errp, "File descriptor named '%s' not found", fdname);
f07918fd
MM
1065}
1066
a9940fc4 1067int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
7768e04c 1068{
c227f099 1069 mon_fd_t *monfd;
7768e04c 1070
0210c3b3 1071 QEMU_LOCK_GUARD(&mon->mon_lock);
72cf2d4f 1072 QLIST_FOREACH(monfd, &mon->fds, next) {
7768e04c
MM
1073 int fd;
1074
1075 if (strcmp(monfd->name, fdname) != 0) {
1076 continue;
1077 }
1078
1079 fd = monfd->fd;
1080
1081 /* caller takes ownership of fd */
72cf2d4f 1082 QLIST_REMOVE(monfd, next);
7267c094
AL
1083 g_free(monfd->name);
1084 g_free(monfd);
7768e04c
MM
1085
1086 return fd;
1087 }
1088
a9940fc4 1089 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
7768e04c
MM
1090 return -1;
1091}
1092
ba1c048a
CB
1093static void monitor_fdset_cleanup(MonFdset *mon_fdset)
1094{
1095 MonFdsetFd *mon_fdset_fd;
1096 MonFdsetFd *mon_fdset_fd_next;
1097
1098 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
ebe52b59
CB
1099 if ((mon_fdset_fd->removed ||
1100 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
1101 runstate_is_running()) {
ba1c048a
CB
1102 close(mon_fdset_fd->fd);
1103 g_free(mon_fdset_fd->opaque);
1104 QLIST_REMOVE(mon_fdset_fd, next);
1105 g_free(mon_fdset_fd);
1106 }
1107 }
1108
adb696f3 1109 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
ba1c048a
CB
1110 QLIST_REMOVE(mon_fdset, next);
1111 g_free(mon_fdset);
1112 }
1113}
1114
7e3c0dea 1115void monitor_fdsets_cleanup(void)
efb87c16
CB
1116{
1117 MonFdset *mon_fdset;
1118 MonFdset *mon_fdset_next;
1119
0210c3b3 1120 QEMU_LOCK_GUARD(&mon_fdsets_lock);
efb87c16
CB
1121 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
1122 monitor_fdset_cleanup(mon_fdset);
1123 }
1124}
1125
ba1c048a
CB
1126AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
1127 const char *opaque, Error **errp)
1128{
1129 int fd;
947e4744 1130 Monitor *mon = monitor_cur();
ba1c048a
CB
1131 AddfdInfo *fdinfo;
1132
5345fdb4 1133 fd = qemu_chr_fe_get_msgfd(&mon->chr);
ba1c048a 1134 if (fd == -1) {
f820af87 1135 error_setg(errp, "No file descriptor supplied via SCM_RIGHTS");
ba1c048a
CB
1136 goto error;
1137 }
1138
e446f70d
CB
1139 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
1140 has_opaque, opaque, errp);
1141 if (fdinfo) {
1142 return fdinfo;
ba1c048a 1143 }
ba1c048a
CB
1144
1145error:
1146 if (fd != -1) {
1147 close(fd);
1148 }
1149 return NULL;
1150}
1151
1152void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
1153{
1154 MonFdset *mon_fdset;
1155 MonFdsetFd *mon_fdset_fd;
1156 char fd_str[60];
1157
0210c3b3 1158 QEMU_LOCK_GUARD(&mon_fdsets_lock);
ba1c048a
CB
1159 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1160 if (mon_fdset->id != fdset_id) {
1161 continue;
1162 }
1163 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1164 if (has_fd) {
1165 if (mon_fdset_fd->fd != fd) {
1166 continue;
1167 }
1168 mon_fdset_fd->removed = true;
1169 break;
1170 } else {
1171 mon_fdset_fd->removed = true;
1172 }
1173 }
1174 if (has_fd && !mon_fdset_fd) {
1175 goto error;
1176 }
1177 monitor_fdset_cleanup(mon_fdset);
1178 return;
1179 }
1180
1181error:
1182 if (has_fd) {
1183 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
1184 fdset_id, fd);
1185 } else {
1186 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
1187 }
f820af87 1188 error_setg(errp, "File descriptor named '%s' not found", fd_str);
ba1c048a
CB
1189}
1190
1191FdsetInfoList *qmp_query_fdsets(Error **errp)
1192{
1193 MonFdset *mon_fdset;
1194 MonFdsetFd *mon_fdset_fd;
1195 FdsetInfoList *fdset_list = NULL;
1196
0210c3b3 1197 QEMU_LOCK_GUARD(&mon_fdsets_lock);
ba1c048a 1198 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
54aa3de7 1199 FdsetInfo *fdset_info = g_malloc0(sizeof(*fdset_info));
ba1c048a 1200
54aa3de7 1201 fdset_info->fdset_id = mon_fdset->id;
ba1c048a
CB
1202
1203 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
54aa3de7 1204 FdsetFdInfo *fdsetfd_info;
ba1c048a
CB
1205
1206 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
54aa3de7 1207 fdsetfd_info->fd = mon_fdset_fd->fd;
ba1c048a 1208 if (mon_fdset_fd->opaque) {
54aa3de7
EB
1209 fdsetfd_info->has_opaque = true;
1210 fdsetfd_info->opaque = g_strdup(mon_fdset_fd->opaque);
ba1c048a 1211 } else {
54aa3de7 1212 fdsetfd_info->has_opaque = false;
ba1c048a
CB
1213 }
1214
54aa3de7 1215 QAPI_LIST_PREPEND(fdset_info->fds, fdsetfd_info);
ba1c048a
CB
1216 }
1217
54aa3de7 1218 QAPI_LIST_PREPEND(fdset_list, fdset_info);
ba1c048a
CB
1219 }
1220
1221 return fdset_list;
1222}
1223
e446f70d
CB
1224AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
1225 bool has_opaque, const char *opaque,
1226 Error **errp)
1227{
1228 MonFdset *mon_fdset = NULL;
1229 MonFdsetFd *mon_fdset_fd;
1230 AddfdInfo *fdinfo;
1231
6e8a355d 1232 QEMU_LOCK_GUARD(&mon_fdsets_lock);
e446f70d
CB
1233 if (has_fdset_id) {
1234 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1235 /* Break if match found or match impossible due to ordering by ID */
1236 if (fdset_id <= mon_fdset->id) {
1237 if (fdset_id < mon_fdset->id) {
1238 mon_fdset = NULL;
1239 }
1240 break;
1241 }
1242 }
1243 }
1244
1245 if (mon_fdset == NULL) {
1246 int64_t fdset_id_prev = -1;
1247 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
1248
1249 if (has_fdset_id) {
1250 if (fdset_id < 0) {
c6bd8c70
MA
1251 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
1252 "a non-negative value");
e446f70d
CB
1253 return NULL;
1254 }
1255 /* Use specified fdset ID */
1256 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1257 mon_fdset_cur = mon_fdset;
1258 if (fdset_id < mon_fdset_cur->id) {
1259 break;
1260 }
1261 }
1262 } else {
1263 /* Use first available fdset ID */
1264 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1265 mon_fdset_cur = mon_fdset;
1266 if (fdset_id_prev == mon_fdset_cur->id - 1) {
1267 fdset_id_prev = mon_fdset_cur->id;
1268 continue;
1269 }
1270 break;
1271 }
1272 }
1273
1274 mon_fdset = g_malloc0(sizeof(*mon_fdset));
1275 if (has_fdset_id) {
1276 mon_fdset->id = fdset_id;
1277 } else {
1278 mon_fdset->id = fdset_id_prev + 1;
1279 }
1280
1281 /* The fdset list is ordered by fdset ID */
1282 if (!mon_fdset_cur) {
1283 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
1284 } else if (mon_fdset->id < mon_fdset_cur->id) {
1285 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
1286 } else {
1287 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
1288 }
1289 }
1290
1291 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
1292 mon_fdset_fd->fd = fd;
1293 mon_fdset_fd->removed = false;
1294 if (has_opaque) {
1295 mon_fdset_fd->opaque = g_strdup(opaque);
1296 }
1297 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
1298
1299 fdinfo = g_malloc0(sizeof(*fdinfo));
1300 fdinfo->fdset_id = mon_fdset->id;
1301 fdinfo->fd = mon_fdset_fd->fd;
1302
1303 return fdinfo;
1304}
1305
60efffa4 1306int monitor_fdset_dup_fd_add(int64_t fdset_id, int flags)
adb696f3 1307{
47451466
PX
1308#ifdef _WIN32
1309 return -ENOENT;
1310#else
adb696f3 1311 MonFdset *mon_fdset;
adb696f3 1312
0210c3b3 1313 QEMU_LOCK_GUARD(&mon_fdsets_lock);
adb696f3 1314 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
60efffa4
DB
1315 MonFdsetFd *mon_fdset_fd;
1316 MonFdsetFd *mon_fdset_fd_dup;
1317 int fd = -1;
1318 int dup_fd;
1319 int mon_fd_flags;
1320
adb696f3
CB
1321 if (mon_fdset->id != fdset_id) {
1322 continue;
1323 }
60efffa4 1324
adb696f3
CB
1325 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1326 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
1327 if (mon_fd_flags == -1) {
60efffa4 1328 return -1;
adb696f3
CB
1329 }
1330
1331 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
60efffa4
DB
1332 fd = mon_fdset_fd->fd;
1333 break;
adb696f3
CB
1334 }
1335 }
adb696f3 1336
60efffa4 1337 if (fd == -1) {
60efffa4
DB
1338 errno = EACCES;
1339 return -1;
adb696f3 1340 }
60efffa4
DB
1341
1342 dup_fd = qemu_dup_flags(fd, flags);
1343 if (dup_fd == -1) {
60efffa4 1344 return -1;
adb696f3 1345 }
60efffa4 1346
adb696f3
CB
1347 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
1348 mon_fdset_fd_dup->fd = dup_fd;
1349 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
60efffa4 1350 return dup_fd;
adb696f3 1351 }
47451466 1352
60efffa4 1353 errno = ENOENT;
adb696f3 1354 return -1;
60efffa4 1355#endif
adb696f3
CB
1356}
1357
854f63d4 1358static int64_t monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
adb696f3
CB
1359{
1360 MonFdset *mon_fdset;
1361 MonFdsetFd *mon_fdset_fd_dup;
1362
0210c3b3 1363 QEMU_LOCK_GUARD(&mon_fdsets_lock);
adb696f3
CB
1364 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1365 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
1366 if (mon_fdset_fd_dup->fd == dup_fd) {
1367 if (remove) {
1368 QLIST_REMOVE(mon_fdset_fd_dup, next);
a661614d 1369 g_free(mon_fdset_fd_dup);
adb696f3
CB
1370 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
1371 monitor_fdset_cleanup(mon_fdset);
1372 }
0210c3b3 1373 return -1;
b3dd1b8c
MT
1374 } else {
1375 return mon_fdset->id;
adb696f3 1376 }
adb696f3
CB
1377 }
1378 }
1379 }
47451466 1380
adb696f3
CB
1381 return -1;
1382}
1383
854f63d4 1384int64_t monitor_fdset_dup_fd_find(int dup_fd)
adb696f3
CB
1385{
1386 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
1387}
1388
b3dd1b8c 1389void monitor_fdset_dup_fd_remove(int dup_fd)
adb696f3 1390{
b3dd1b8c 1391 monitor_fdset_dup_fd_find_remove(dup_fd, true);
adb696f3
CB
1392}
1393
1677f4c6 1394int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
5906366e
LE
1395{
1396 int fd;
1397 Error *local_err = NULL;
a96ed02f 1398
5906366e 1399 if (!qemu_isdigit(fdname[0]) && mon) {
a9940fc4 1400 fd = monitor_get_fd(mon, fdname, &local_err);
5906366e
LE
1401 } else {
1402 fd = qemu_parse_fd(fdname);
a96ed02f 1403 if (fd == -1) {
5906366e
LE
1404 error_setg(&local_err, "Invalid file descriptor number '%s'",
1405 fdname);
a96ed02f 1406 }
5906366e
LE
1407 }
1408 if (local_err) {
1409 error_propagate(errp, local_err);
1410 assert(fd == -1);
a96ed02f 1411 } else {
5906366e 1412 assert(fd != -1);
a96ed02f
NB
1413 }
1414
1415 return fd;
1416}
1417
acd0a093 1418/* Please update hmp-commands.hx when adding or changing commands */
a0cd5e1c 1419static HMPCommand hmp_info_cmds[] = {
da76ee76
PB
1420#include "hmp-commands-info.h"
1421 { NULL, NULL, },
9dc39cba
FB
1422};
1423
a0cd5e1c 1424/* hmp_cmds and hmp_info_cmds would be sorted at runtime */
ed7bda5d 1425HMPCommand hmp_cmds[] = {
a13ced59
WX
1426#include "hmp-commands.h"
1427 { NULL, NULL, },
1428};
1429
ed7bda5d
KW
1430/*
1431 * Set @pval to the value in the register identified by @name.
1432 * return 0 if OK, -1 if not found
1433 */
2fc5d01b 1434int get_monitor_def(Monitor *mon, int64_t *pval, const char *name)
9307c4c1 1435{
bf957284 1436 const MonitorDef *md = target_monitor_defs();
2fc5d01b 1437 CPUState *cs = mon_get_cpu(mon);
92a31b1f 1438 void *ptr;
0a9516c2
AK
1439 uint64_t tmp = 0;
1440 int ret;
92a31b1f 1441
854e67fe 1442 if (cs == NULL || md == NULL) {
bf957284
PB
1443 return -1;
1444 }
1445
1446 for(; md->name != NULL; md++) {
ed7bda5d 1447 if (hmp_compare_cmd(name, md->name)) {
9307c4c1 1448 if (md->get_value) {
43cf067f 1449 *pval = md->get_value(mon, md, md->offset);
9307c4c1 1450 } else {
e7cff9c6 1451 CPUArchState *env = mon_get_cpu_env(mon);
6a00d601 1452 ptr = (uint8_t *)env + md->offset;
92a31b1f
FB
1453 switch(md->type) {
1454 case MD_I32:
1455 *pval = *(int32_t *)ptr;
1456 break;
1457 case MD_TLONG:
1458 *pval = *(target_long *)ptr;
1459 break;
1460 default:
1461 *pval = 0;
1462 break;
1463 }
9307c4c1
FB
1464 }
1465 return 0;
1466 }
1467 }
0a9516c2 1468
854e67fe 1469 ret = target_get_monitor_def(cs, name, &tmp);
0a9516c2
AK
1470 if (!ret) {
1471 *pval = (target_long) tmp;
1472 }
1473
1474 return ret;
9307c4c1
FB
1475}
1476
40d19394
HB
1477static void add_completion_option(ReadLineState *rs, const char *str,
1478 const char *option)
1479{
1480 if (!str || !option) {
1481 return;
1482 }
1483 if (!strncmp(option, str, strlen(str))) {
1484 readline_add_completion(rs, option);
1485 }
1486}
1487
13e315da
HB
1488void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
1489{
1490 size_t len;
1491 ChardevBackendInfoList *list, *start;
1492
1493 if (nb_args != 2) {
1494 return;
1495 }
1496 len = strlen(str);
1497 readline_set_completion_index(rs, len);
1498
1499 start = list = qmp_query_chardev_backends(NULL);
1500 while (list) {
1501 const char *chr_name = list->value->name;
1502
1503 if (!strncmp(chr_name, str, len)) {
1504 readline_add_completion(rs, chr_name);
1505 }
1506 list = list->next;
1507 }
1508 qapi_free_ChardevBackendInfoList(start);
1509}
1510
b162b49a
HB
1511void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
1512{
1513 size_t len;
1514 int i;
1515
1516 if (nb_args != 2) {
1517 return;
1518 }
1519 len = strlen(str);
1520 readline_set_completion_index(rs, len);
1c236ba5 1521 for (i = 0; i < NET_CLIENT_DRIVER__MAX; i++) {
977c736f 1522 add_completion_option(rs, str, NetClientDriver_str(i));
b162b49a
HB
1523 }
1524}
1525
2da1b3ab 1526void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
992d3e64
HB
1527{
1528 GSList *list, *elt;
1529 size_t len;
1530
2da1b3ab
HB
1531 if (nb_args != 2) {
1532 return;
1533 }
1534
992d3e64
HB
1535 len = strlen(str);
1536 readline_set_completion_index(rs, len);
1537 list = elt = object_class_get_list(TYPE_DEVICE, false);
1538 while (elt) {
1539 const char *name;
1540 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
1541 TYPE_DEVICE);
1542 name = object_class_get_name(OBJECT_CLASS(dc));
2da1b3ab 1543
e90f2a8c 1544 if (dc->user_creatable
2da1b3ab 1545 && !strncmp(name, str, len)) {
992d3e64
HB
1546 readline_add_completion(rs, name);
1547 }
1548 elt = elt->next;
1549 }
1550 g_slist_free(list);
1551}
1552
bfa40f77 1553void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
1094fd3a
HB
1554{
1555 GSList *list, *elt;
1556 size_t len;
1557
bfa40f77
HB
1558 if (nb_args != 2) {
1559 return;
1560 }
1561
1094fd3a
HB
1562 len = strlen(str);
1563 readline_set_completion_index(rs, len);
1564 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
1565 while (elt) {
1566 const char *name;
1567
1568 name = object_class_get_name(OBJECT_CLASS(elt->data));
1569 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
1570 readline_add_completion(rs, name);
1571 }
1572 elt = elt->next;
1573 }
1574 g_slist_free(list);
1575}
1576
91590159
MAL
1577static int qdev_add_hotpluggable_device(Object *obj, void *opaque)
1578{
1579 GSList **list = opaque;
688ffbb4 1580 DeviceState *dev = (DeviceState *)object_dynamic_cast(obj, TYPE_DEVICE);
91590159
MAL
1581
1582 if (dev == NULL) {
1583 return 0;
1584 }
1585
1586 if (dev->realized && object_property_get_bool(obj, "hotpluggable", NULL)) {
1587 *list = g_slist_append(*list, dev);
1588 }
1589
1590 return 0;
1591}
1592
1593static GSList *qdev_build_hotpluggable_device_list(Object *peripheral)
1594{
1595 GSList *list = NULL;
1596
1597 object_child_foreach(peripheral, qdev_add_hotpluggable_device, &list);
1598
1599 return list;
1600}
1601
6a1fa9f5
ZG
1602static void peripheral_device_del_completion(ReadLineState *rs,
1603 const char *str, size_t len)
1604{
4cae4d5a
MA
1605 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
1606 GSList *list, *item;
6a1fa9f5 1607
4cae4d5a
MA
1608 list = qdev_build_hotpluggable_device_list(peripheral);
1609 if (!list) {
6a1fa9f5
ZG
1610 return;
1611 }
1612
6a1fa9f5
ZG
1613 for (item = list; item; item = g_slist_next(item)) {
1614 DeviceState *dev = item->data;
1615
1616 if (dev->id && !strncmp(str, dev->id, len)) {
1617 readline_add_completion(rs, dev->id);
1618 }
1619 }
1620
1621 g_slist_free(list);
1622}
1623
6297d9a2
HB
1624void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
1625{
1626 size_t len;
1627 ChardevInfoList *list, *start;
1628
1629 if (nb_args != 2) {
1630 return;
1631 }
1632 len = strlen(str);
1633 readline_set_completion_index(rs, len);
1634
1635 start = list = qmp_query_chardev(NULL);
1636 while (list) {
1637 ChardevInfo *chr = list->value;
1638
1639 if (!strncmp(chr->label, str, len)) {
1640 readline_add_completion(rs, chr->label);
1641 }
1642 list = list->next;
1643 }
1644 qapi_free_ChardevInfoList(start);
1645}
1646
8e597779
HB
1647static void ringbuf_completion(ReadLineState *rs, const char *str)
1648{
1649 size_t len;
1650 ChardevInfoList *list, *start;
1651
1652 len = strlen(str);
1653 readline_set_completion_index(rs, len);
1654
1655 start = list = qmp_query_chardev(NULL);
1656 while (list) {
1657 ChardevInfo *chr_info = list->value;
1658
1659 if (!strncmp(chr_info->label, str, len)) {
0ec7b3e7 1660 Chardev *chr = qemu_chr_find(chr_info->label);
777357d7 1661 if (chr && CHARDEV_IS_RINGBUF(chr)) {
8e597779
HB
1662 readline_add_completion(rs, chr_info->label);
1663 }
1664 }
1665 list = list->next;
1666 }
1667 qapi_free_ChardevInfoList(start);
1668}
1669
8e597779
HB
1670void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
1671{
1672 if (nb_args != 2) {
1673 return;
1674 }
1675 ringbuf_completion(rs, str);
1676}
1677
2da1b3ab
HB
1678void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
1679{
1680 size_t len;
1681
1682 if (nb_args != 2) {
1683 return;
1684 }
1685
1686 len = strlen(str);
1687 readline_set_completion_index(rs, len);
6a1fa9f5 1688 peripheral_device_del_completion(rs, str, len);
2da1b3ab
HB
1689}
1690
bfa40f77 1691void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
b48fa074
HB
1692{
1693 ObjectPropertyInfoList *list, *start;
1694 size_t len;
1695
bfa40f77
HB
1696 if (nb_args != 2) {
1697 return;
1698 }
b48fa074
HB
1699 len = strlen(str);
1700 readline_set_completion_index(rs, len);
1701
1702 start = list = qmp_qom_list("/objects", NULL);
1703 while (list) {
1704 ObjectPropertyInfo *info = list->value;
1705
1706 if (!strncmp(info->type, "child<", 5)
1707 && !strncmp(info->name, str, len)) {
1708 readline_add_completion(rs, info->name);
1709 }
1710 list = list->next;
1711 }
1712 qapi_free_ObjectPropertyInfoList(start);
1713}
1714
29136cd8
HB
1715void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
1716{
1717 int i;
1718 char *sep;
1719 size_t len;
1720
1721 if (nb_args != 2) {
1722 return;
1723 }
1724 sep = strrchr(str, '-');
1725 if (sep) {
1726 str = sep + 1;
1727 }
1728 len = strlen(str);
1729 readline_set_completion_index(rs, len);
7fb1cf16 1730 for (i = 0; i < Q_KEY_CODE__MAX; i++) {
977c736f
MA
1731 if (!strncmp(str, QKeyCode_str(i), len)) {
1732 readline_add_completion(rs, QKeyCode_str(i));
29136cd8
HB
1733 }
1734 }
1735}
1736
40d19394
HB
1737void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
1738{
1739 size_t len;
1740
1741 len = strlen(str);
1742 readline_set_completion_index(rs, len);
1743 if (nb_args == 2) {
eaed483c 1744 NetClientState *ncs[MAX_QUEUE_NUM];
40d19394
HB
1745 int count, i;
1746 count = qemu_find_net_clients_except(NULL, ncs,
f394b2e2 1747 NET_CLIENT_DRIVER_NONE,
eaed483c 1748 MAX_QUEUE_NUM);
bcfa4d60 1749 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
40d19394
HB
1750 const char *name = ncs[i]->name;
1751 if (!strncmp(str, name, len)) {
1752 readline_add_completion(rs, name);
1753 }
1754 }
1755 } else if (nb_args == 3) {
1756 add_completion_option(rs, str, "on");
1757 add_completion_option(rs, str, "off");
1758 }
1759}
1760
11b389f2
HB
1761void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
1762{
1763 int len, count, i;
eaed483c 1764 NetClientState *ncs[MAX_QUEUE_NUM];
11b389f2
HB
1765
1766 if (nb_args != 2) {
1767 return;
1768 }
1769
1770 len = strlen(str);
1771 readline_set_completion_index(rs, len);
f394b2e2 1772 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_DRIVER_NIC,
eaed483c 1773 MAX_QUEUE_NUM);
bcfa4d60 1774 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
11b389f2
HB
1775 const char *name = ncs[i]->name;
1776 if (strncmp(str, name, len)) {
1777 continue;
1778 }
08712fcb 1779 if (ncs[i]->is_netdev) {
11b389f2
HB
1780 readline_add_completion(rs, name);
1781 }
1782 }
1783}
1784
bd71211d
LV
1785void info_trace_events_completion(ReadLineState *rs, int nb_args, const char *str)
1786{
1787 size_t len;
1788
1789 len = strlen(str);
1790 readline_set_completion_index(rs, len);
1791 if (nb_args == 2) {
0d4e995c
DB
1792 TraceEventIter iter;
1793 TraceEvent *ev;
1794 char *pattern = g_strdup_printf("%s*", str);
117856c3 1795 trace_event_iter_init_pattern(&iter, pattern);
0d4e995c
DB
1796 while ((ev = trace_event_iter_next(&iter)) != NULL) {
1797 readline_add_completion(rs, trace_event_get_name(ev));
bd71211d 1798 }
0d4e995c 1799 g_free(pattern);
bd71211d
LV
1800 }
1801}
1802
987bd270
DDAG
1803void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
1804{
1805 size_t len;
1806
1807 len = strlen(str);
1808 readline_set_completion_index(rs, len);
1809 if (nb_args == 2) {
0d4e995c
DB
1810 TraceEventIter iter;
1811 TraceEvent *ev;
1812 char *pattern = g_strdup_printf("%s*", str);
117856c3 1813 trace_event_iter_init_pattern(&iter, pattern);
0d4e995c
DB
1814 while ((ev = trace_event_iter_next(&iter)) != NULL) {
1815 readline_add_completion(rs, trace_event_get_name(ev));
1816 }
1817 g_free(pattern);
987bd270
DDAG
1818 } else if (nb_args == 3) {
1819 add_completion_option(rs, str, "on");
1820 add_completion_option(rs, str, "off");
1821 }
1822}
1823
d0ece345
HB
1824void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
1825{
4bb08af3
HB
1826 int i;
1827
d0ece345
HB
1828 if (nb_args != 2) {
1829 return;
1830 }
1831 readline_set_completion_index(rs, strlen(str));
14d53b4f
MP
1832 for (i = 0; i < WATCHDOG_ACTION__MAX; i++) {
1833 add_completion_option(rs, str, WatchdogAction_str(i));
4bb08af3 1834 }
d0ece345
HB
1835}
1836
c68a0409
HB
1837void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
1838 const char *str)
1839{
1840 size_t len;
1841
1842 len = strlen(str);
1843 readline_set_completion_index(rs, len);
1844 if (nb_args == 2) {
1845 int i;
7fb1cf16 1846 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
977c736f 1847 const char *name = MigrationCapability_str(i);
c68a0409
HB
1848 if (!strncmp(str, name, len)) {
1849 readline_add_completion(rs, name);
1850 }
1851 }
1852 } else if (nb_args == 3) {
1853 add_completion_option(rs, str, "on");
1854 add_completion_option(rs, str, "off");
1855 }
1856}
1857
50e9a629
LL
1858void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
1859 const char *str)
1860{
1861 size_t len;
1862
1863 len = strlen(str);
1864 readline_set_completion_index(rs, len);
1865 if (nb_args == 2) {
1866 int i;
7fb1cf16 1867 for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
977c736f 1868 const char *name = MigrationParameter_str(i);
50e9a629
LL
1869 if (!strncmp(str, name, len)) {
1870 readline_add_completion(rs, name);
1871 }
1872 }
1873 }
1874}
1875
b21631f3
HB
1876static void vm_completion(ReadLineState *rs, const char *str)
1877{
1878 size_t len;
7c8eece4 1879 BlockDriverState *bs;
88be7b4b 1880 BdrvNextIterator it;
b21631f3
HB
1881
1882 len = strlen(str);
1883 readline_set_completion_index(rs, len);
7c8eece4 1884
88be7b4b 1885 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
b21631f3 1886 SnapshotInfoList *snapshots, *snapshot;
6bf1faa8
DL
1887 AioContext *ctx = bdrv_get_aio_context(bs);
1888 bool ok = false;
b21631f3 1889
6bf1faa8
DL
1890 aio_context_acquire(ctx);
1891 if (bdrv_can_snapshot(bs)) {
1892 ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0;
b21631f3 1893 }
6bf1faa8
DL
1894 aio_context_release(ctx);
1895 if (!ok) {
b21631f3
HB
1896 continue;
1897 }
6bf1faa8 1898
b21631f3
HB
1899 snapshot = snapshots;
1900 while (snapshot) {
1901 char *completion = snapshot->value->name;
1902 if (!strncmp(str, completion, len)) {
1903 readline_add_completion(rs, completion);
1904 }
1905 completion = snapshot->value->id;
1906 if (!strncmp(str, completion, len)) {
1907 readline_add_completion(rs, completion);
1908 }
1909 snapshot = snapshot->next;
1910 }
1911 qapi_free_SnapshotInfoList(snapshots);
1912 }
1913
1914}
1915
1916void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
1917{
1918 if (nb_args == 2) {
1919 vm_completion(rs, str);
1920 }
1921}
1922
1923void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
1924{
1925 if (nb_args == 2) {
1926 vm_completion(rs, str);
1927 }
1928}
1929
816f8925
WX
1930static int
1931compare_mon_cmd(const void *a, const void *b)
1932{
a0cd5e1c
KW
1933 return strcmp(((const HMPCommand *)a)->name,
1934 ((const HMPCommand *)b)->name);
816f8925
WX
1935}
1936
1937static void sortcmdlist(void)
1938{
a0cd5e1c
KW
1939 qsort(hmp_cmds, ARRAY_SIZE(hmp_cmds) - 1,
1940 sizeof(*hmp_cmds),
1941 compare_mon_cmd);
1942 qsort(hmp_info_cmds, ARRAY_SIZE(hmp_info_cmds) - 1,
1943 sizeof(*hmp_info_cmds),
1944 compare_mon_cmd);
816f8925
WX
1945}
1946
f0e48cbd
GH
1947void monitor_register_hmp(const char *name, bool info,
1948 void (*cmd)(Monitor *mon, const QDict *qdict))
1949{
1950 HMPCommand *table = info ? hmp_info_cmds : hmp_cmds;
1951
1952 while (table->name != NULL) {
1953 if (strcmp(table->name, name) == 0) {
f9429c67 1954 g_assert(table->cmd == NULL && table->cmd_info_hrt == NULL);
f0e48cbd
GH
1955 table->cmd = cmd;
1956 return;
1957 }
1958 table++;
1959 }
1960 g_assert_not_reached();
1961}
1962
f9429c67
DB
1963void monitor_register_hmp_info_hrt(const char *name,
1964 HumanReadableText *(*handler)(Error **errp))
1965{
1966 HMPCommand *table = hmp_info_cmds;
1967
1968 while (table->name != NULL) {
1969 if (strcmp(table->name, name) == 0) {
1970 g_assert(table->cmd == NULL && table->cmd_info_hrt == NULL);
1971 table->cmd_info_hrt = handler;
1972 return;
1973 }
1974 table++;
1975 }
1976 g_assert_not_reached();
1977}
1978
6adf08dd
PX
1979void monitor_init_globals(void)
1980{
1d95db74 1981 monitor_init_globals_core();
6adf08dd 1982 monitor_init_qmp_commands();
6adf08dd 1983 sortcmdlist();
47451466 1984 qemu_mutex_init(&mon_fdsets_lock);
aa455485 1985}