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