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