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