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