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