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