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