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