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