blob: e847b58a8c91d3eae51df24d3911eb6d7f22f19f [file] [log] [blame]
bellard9dc39cb2004-03-14 21:38:27 +00001/*
2 * QEMU monitor
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard9dc39cb2004-03-14 21:38:27 +00004 * Copyright (c) 2003-2004 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard9dc39cb2004-03-14 21:38:27 +00006 * 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 */
Markus Armbrustere688df62018-02-01 12:18:31 +010024
Peter Maydelld38ea872016-01-29 17:50:05 +000025#include "qemu/osdep.h"
Kevin Wolf5bce3082019-06-13 17:33:59 +020026#include "monitor-internal.h"
Paolo Bonzini33c11872016-03-15 16:58:45 +010027#include "cpu.h"
Paolo Bonzinib4a42f82013-02-04 11:37:52 +010028#include "monitor/qdev.h"
pbrook87ecb682007-11-17 17:14:51 +000029#include "hw/usb.h"
Michael S. Tsirkina2cb15b2012-12-12 14:24:50 +020030#include "hw/pci/pci.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010031#include "sysemu/watchdog.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +020032#include "hw/loader.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010033#include "exec/gdbstub.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020034#include "net/net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000035#include "net/slirp.h"
Gerd Hoffmann75721502010-10-07 12:22:54 +020036#include "ui/qemu-spice.h"
Marc-André Lureau213dcb02016-12-12 20:22:24 +030037#include "qemu/config-file.h"
Markus Armbruster856dfd82019-05-23 16:35:06 +020038#include "qemu/ctype.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010039#include "ui/console.h"
Gerd Hoffmannc751a742013-12-04 15:02:28 +010040#include "ui/input.h"
pbrook87ecb682007-11-17 17:14:51 +000041#include "audio/audio.h"
Paolo Bonzini76cad712012-10-24 11:12:21 +020042#include "disas/disas.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010043#include "sysemu/balloon.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010044#include "qemu/timer.h"
Vincent Palatinb3946622017-01-10 11:59:55 +010045#include "sysemu/hw_accel.h"
Markus Armbruster54d31232019-08-12 07:23:59 +020046#include "sysemu/runstate.h"
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +000047#include "authz/list.h"
48#include "qapi/util.h"
Markus Armbruster55225c82019-06-19 22:10:44 +020049#include "sysemu/blockdev.h"
50#include "sysemu/sysemu.h"
Markus Armbruster14a48c12019-05-23 16:35:05 +020051#include "sysemu/tcg.h"
Paolo Bonzinibdee56f2013-04-02 18:28:41 +020052#include "sysemu/tpm.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010053#include "qapi/qmp/qdict.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010054#include "qapi/qmp/qerror.h"
Markus Armbrusterfc81fa12018-02-01 12:18:40 +010055#include "qapi/qmp/qstring.h"
Markus Armbrustera9c94272016-06-22 19:11:19 +020056#include "qom/object_interfaces.h"
Lluís31965ae2011-08-31 20:31:24 +020057#include "trace/control.h"
Pavel Butsykinbf957282015-09-10 18:38:59 +030058#include "monitor/hmp-target.h"
Markus Armbruster275307a2019-06-19 22:10:36 +020059#include "monitor/hmp.h"
Lluís6d8a7642011-08-31 20:30:43 +020060#ifdef CONFIG_TRACE_SIMPLE
Lluís31965ae2011-08-31 20:31:24 +020061#include "trace/simple.h"
Prerna Saxena22890ab2010-06-24 17:04:53 +053062#endif
Paolo Bonzini022c62c2012-12-17 18:19:49 +010063#include "exec/memory.h"
Paolo Bonzini63c91552016-03-15 13:18:37 +010064#include "exec/exec-all.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010065#include "qemu/option.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010066#include "qemu/thread.h"
Hani Benhabilesb21631f2014-05-27 23:39:37 +010067#include "block/qapi.h"
Maxim Levitskya2dde2f2020-03-08 11:24:32 +020068#include "block/block-hmp-cmds.h"
Markus Armbruster00ca24f2019-11-20 19:25:48 +010069#include "qapi/qapi-commands-char.h"
Kevin Wolffa4dcf52020-01-29 11:22:37 +010070#include "qapi/qapi-commands-control.h"
Markus Armbruster00ca24f2019-11-20 19:25:48 +010071#include "qapi/qapi-commands-migration.h"
72#include "qapi/qapi-commands-misc.h"
73#include "qapi/qapi-commands-qom.h"
74#include "qapi/qapi-commands-trace.h"
Markus Armbruster00ca24f2019-11-20 19:25:48 +010075#include "qapi/qapi-init-commands.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010076#include "qapi/error.h"
Wenchao Xia43a14cf2014-06-18 08:43:31 +020077#include "qapi/qmp-event.h"
Paolo Bonzinid2528bd2017-03-03 12:01:16 +010078#include "sysemu/cpus.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020079#include "qemu/cutils.h"
Emilio G. Cota72fd2ef2018-10-10 10:48:53 -040080#include "tcg/tcg.h"
ths6a5bd302007-12-03 17:05:38 +000081
Jason J. Hernea4538a52015-06-26 14:07:21 -040082#if defined(TARGET_S390X)
83#include "hw/s390x/storage-keys.h"
Claudio Imbrendaf860d492016-08-15 18:44:04 +020084#include "hw/s390x/storage-attributes.h"
Jason J. Hernea4538a52015-06-26 14:07:21 -040085#endif
86
Mark McLoughlinf07918f2009-07-22 09:11:40 +010087/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -050088typedef struct mon_fd_t mon_fd_t;
89struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +010090 char *name;
91 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -050092 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +010093};
94
Corey Bryantba1c0482012-08-14 16:43:43 -040095/* file descriptor associated with a file descriptor set */
96typedef struct MonFdsetFd MonFdsetFd;
97struct MonFdsetFd {
98 int fd;
99 bool removed;
100 char *opaque;
101 QLIST_ENTRY(MonFdsetFd) next;
102};
103
104/* file descriptor set containing fds passed via SCM_RIGHTS */
105typedef struct MonFdset MonFdset;
106struct MonFdset {
107 int64_t id;
108 QLIST_HEAD(, MonFdsetFd) fds;
Corey Bryantadb696f2012-08-14 16:43:47 -0400109 QLIST_HEAD(, MonFdsetFd) dup_fds;
Corey Bryantba1c0482012-08-14 16:43:43 -0400110 QLIST_ENTRY(MonFdset) next;
111};
112
Peter Xu47451462018-06-08 11:55:11 +0800113/* Protects mon_fdsets */
114static QemuMutex mon_fdsets_lock;
Paolo Bonzinib58deb32018-12-06 11:58:10 +0100115static QLIST_HEAD(, MonFdset) mon_fdsets;
Peter Xu47451462018-06-08 11:55:11 +0800116
Kevin Wolfa0cd5e12019-06-13 17:33:56 +0200117static HMPCommand hmp_info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000118
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200119char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
120 int64_t cpu_index, Error **errp)
Luiz Capitulino0268d972010-10-22 10:08:02 -0200121{
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200122 char *output = NULL;
Kevin Wolfb6c7c2e2019-06-13 17:33:52 +0200123 Monitor *old_mon;
Kevin Wolf5f9dba12019-06-13 17:33:54 +0200124 MonitorHMP hmp = {};
Luiz Capitulino0268d972010-10-22 10:08:02 -0200125
Kevin Wolf92082412019-06-13 17:34:03 +0200126 monitor_data_init(&hmp.common, false, true, false);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200127
128 old_mon = cur_mon;
Kevin Wolf5f9dba12019-06-13 17:33:54 +0200129 cur_mon = &hmp.common;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200130
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200131 if (has_cpu_index) {
132 int ret = monitor_set_cpu(cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200133 if (ret < 0) {
134 cur_mon = old_mon;
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100135 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
136 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200137 goto out;
138 }
139 }
140
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100141 handle_hmp_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200142 cur_mon = old_mon;
143
Kevin Wolf5f9dba12019-06-13 17:33:54 +0200144 qemu_mutex_lock(&hmp.common.mon_lock);
145 if (qstring_get_length(hmp.common.outbuf) > 0) {
146 output = g_strdup(qstring_get_str(hmp.common.outbuf));
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200147 } else {
148 output = g_strdup("");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200149 }
Kevin Wolf5f9dba12019-06-13 17:33:54 +0200150 qemu_mutex_unlock(&hmp.common.mon_lock);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200151
152out:
Kevin Wolf5f9dba12019-06-13 17:33:54 +0200153 monitor_data_destroy(&hmp.common);
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200154 return output;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200155}
156
Kevin Wolfed7bda52019-06-13 17:34:01 +0200157/**
158 * Is @name in the '|' separated list of names @list?
159 */
160int hmp_compare_cmd(const char *name, const char *list)
bellard9dc39cb2004-03-14 21:38:27 +0000161{
162 const char *p, *pstart;
163 int len;
164 len = strlen(name);
165 p = list;
Kevin Wolfed7bda52019-06-13 17:34:01 +0200166 for (;;) {
bellard9dc39cb2004-03-14 21:38:27 +0000167 pstart = p;
Keno Fischer5c99fa32018-06-29 12:32:10 +0200168 p = qemu_strchrnul(p, '|');
Kevin Wolfed7bda52019-06-13 17:34:01 +0200169 if ((p - pstart) == len && !memcmp(pstart, name, len)) {
bellard9dc39cb2004-03-14 21:38:27 +0000170 return 1;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800171 }
172 if (*p == '\0') {
173 break;
174 }
Kevin Wolfed7bda52019-06-13 17:34:01 +0200175 p++;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800176 }
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800177 return 0;
bellard9dc39cb2004-03-14 21:38:27 +0000178}
179
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300180static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300181{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300182 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300183}
184
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100185static void hmp_trace_event(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530186{
187 const char *tp_name = qdict_get_str(qdict, "name");
188 bool new_state = qdict_get_bool(qdict, "option");
Lluís Vilanova77e2b172016-07-11 12:53:57 +0200189 bool has_vcpu = qdict_haskey(qdict, "vcpu");
190 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
Lluís Vilanova14101d02014-08-25 13:20:03 +0200191 Error *local_err = NULL;
Blue Swirlf871d682010-10-13 19:14:29 +0000192
Lluís Vilanova77e2b172016-07-11 12:53:57 +0200193 if (vcpu < 0) {
194 monitor_printf(mon, "argument vcpu must be positive");
195 return;
196 }
197
198 qmp_trace_event_set_state(tp_name, new_state, true, true, has_vcpu, vcpu, &local_err);
Lluís Vilanova14101d02014-08-25 13:20:03 +0200199 if (local_err) {
Markus Armbruster091e38b2015-02-10 15:15:43 +0100200 error_report_err(local_err);
Blue Swirlf871d682010-10-13 19:14:29 +0000201 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530202}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100203
Michael Rothc45a8162011-10-02 08:44:37 -0500204#ifdef CONFIG_TRACE_SIMPLE
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100205static void hmp_trace_file(Monitor *mon, const QDict *qdict)
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100206{
207 const char *op = qdict_get_try_str(qdict, "op");
208 const char *arg = qdict_get_try_str(qdict, "arg");
209
210 if (!op) {
Markus Armbrusterba4912c2019-04-17 21:17:50 +0200211 st_print_trace_file_status();
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100212 } else if (!strcmp(op, "on")) {
213 st_set_trace_file_enabled(true);
214 } else if (!strcmp(op, "off")) {
215 st_set_trace_file_enabled(false);
216 } else if (!strcmp(op, "flush")) {
217 st_flush_trace_buffer();
218 } else if (!strcmp(op, "set")) {
219 if (arg) {
220 st_set_trace_file(arg);
221 }
222 } else {
223 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
224 help_cmd(mon, "trace-file");
225 }
226}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530227#endif
228
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100229static void hmp_info_help(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000230{
Luiz Capitulino13c74252009-10-07 13:41:55 -0300231 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000232}
233
Peter Xu6adf08d2018-03-09 16:59:50 +0800234static void monitor_init_qmp_commands(void)
Marc-André Lureauedcfaef2016-09-12 13:19:00 +0400235{
Markus Armbruster635db182017-03-03 13:32:27 +0100236 /*
237 * Two command lists:
238 * - qmp_commands contains all QMP commands
239 * - qmp_cap_negotiation_commands contains just
240 * "qmp_capabilities", to enforce capability negotiation
241 */
242
Markus Armbruster1527bad2017-03-03 13:32:25 +0100243 qmp_init_marshal(&qmp_commands);
Markus Armbruster05875682017-03-03 13:32:24 +0100244
Markus Armbruster1527bad2017-03-03 13:32:25 +0100245 qmp_register_command(&qmp_commands, "query-qmp-schema",
Igor Mammedovd6fe3d02018-05-11 18:51:43 +0200246 qmp_query_qmp_schema, QCO_ALLOW_PRECONFIG);
Markus Armbruster1527bad2017-03-03 13:32:25 +0100247 qmp_register_command(&qmp_commands, "device_add", qmp_device_add,
Marc-André Lureauedcfaef2016-09-12 13:19:00 +0400248 QCO_NO_OPTIONS);
Kevin Wolf5f07c4d2020-02-24 15:29:55 +0100249 qmp_register_command(&qmp_commands, "object-add", qmp_object_add,
250 QCO_NO_OPTIONS);
Marc-André Lureau5032a162016-09-12 13:19:02 +0400251
Markus Armbruster635db182017-03-03 13:32:27 +0100252 QTAILQ_INIT(&qmp_cap_negotiation_commands);
253 qmp_register_command(&qmp_cap_negotiation_commands, "qmp_capabilities",
Igor Mammedovd6fe3d02018-05-11 18:51:43 +0200254 qmp_marshal_qmp_capabilities, QCO_ALLOW_PRECONFIG);
Markus Armbruster635db182017-03-03 13:32:27 +0100255}
256
Peter Xud9f25282018-06-08 11:55:07 +0800257/* Set the current CPU defined by the user. Callers must hold BQL. */
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300258int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000259{
Andreas Färber55e5c282012-12-17 06:18:02 +0100260 CPUState *cpu;
bellard6a00d602005-11-21 23:25:50 +0000261
Andreas Färber1c8bb3c2013-02-15 17:01:09 +0100262 cpu = qemu_get_cpu(cpu_index);
263 if (cpu == NULL) {
264 return -1;
bellard6a00d602005-11-21 23:25:50 +0000265 }
Greg Kurz751f8cf2017-10-17 10:16:22 +0200266 g_free(cur_mon->mon_cpu_path);
267 cur_mon->mon_cpu_path = object_get_canonical_path(OBJECT(cpu));
Andreas Färber1c8bb3c2013-02-15 17:01:09 +0100268 return 0;
bellard6a00d602005-11-21 23:25:50 +0000269}
270
Peter Xud9f25282018-06-08 11:55:07 +0800271/* Callers must hold BQL. */
Viktor Mihajlovski137b5cb2018-02-16 17:08:41 +0100272static CPUState *mon_get_cpu_sync(bool synchronize)
bellard6a00d602005-11-21 23:25:50 +0000273{
Marc-André Lureaua85d0bf2019-10-04 15:18:20 +0400274 CPUState *cpu = NULL;
Greg Kurz751f8cf2017-10-17 10:16:22 +0200275
276 if (cur_mon->mon_cpu_path) {
277 cpu = (CPUState *) object_resolve_path_type(cur_mon->mon_cpu_path,
278 TYPE_CPU, NULL);
279 if (!cpu) {
280 g_free(cur_mon->mon_cpu_path);
281 cur_mon->mon_cpu_path = NULL;
282 }
283 }
284 if (!cur_mon->mon_cpu_path) {
Thomas Huth854e67f2017-01-13 13:12:35 +0100285 if (!first_cpu) {
286 return NULL;
287 }
David Gibson27a83f82016-09-21 15:29:26 +1000288 monitor_set_cpu(first_cpu->cpu_index);
Greg Kurz751f8cf2017-10-17 10:16:22 +0200289 cpu = first_cpu;
bellard6a00d602005-11-21 23:25:50 +0000290 }
Marc-André Lureaua85d0bf2019-10-04 15:18:20 +0400291 assert(cpu != NULL);
Viktor Mihajlovski137b5cb2018-02-16 17:08:41 +0100292 if (synchronize) {
293 cpu_synchronize_state(cpu);
294 }
Greg Kurz751f8cf2017-10-17 10:16:22 +0200295 return cpu;
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700296}
297
Viktor Mihajlovski137b5cb2018-02-16 17:08:41 +0100298CPUState *mon_get_cpu(void)
299{
300 return mon_get_cpu_sync(true);
301}
302
Pavel Butsykinbf957282015-09-10 18:38:59 +0300303CPUArchState *mon_get_cpu_env(void)
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700304{
Thomas Huth854e67f2017-01-13 13:12:35 +0100305 CPUState *cs = mon_get_cpu();
306
307 return cs ? cs->env_ptr : NULL;
bellard6a00d602005-11-21 23:25:50 +0000308}
309
Luiz Capitulino99b77962011-10-24 10:53:44 -0200310int monitor_get_cpu_index(void)
311{
Viktor Mihajlovski137b5cb2018-02-16 17:08:41 +0100312 CPUState *cs = mon_get_cpu_sync(false);
Thomas Huth854e67f2017-01-13 13:12:35 +0100313
314 return cs ? cs->cpu_index : UNASSIGNED_CPU_INDEX;
Luiz Capitulino99b77962011-10-24 10:53:44 -0200315}
316
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100317static void hmp_info_registers(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000318{
Suraj Jitindar Singh18f08282017-06-08 15:41:16 +1000319 bool all_cpus = qdict_get_try_bool(qdict, "cpustate_all", false);
320 CPUState *cs;
Thomas Huth854e67f2017-01-13 13:12:35 +0100321
Suraj Jitindar Singh18f08282017-06-08 15:41:16 +1000322 if (all_cpus) {
323 CPU_FOREACH(cs) {
324 monitor_printf(mon, "\nCPU#%d\n", cs->cpu_index);
Markus Armbruster90c84c52019-04-17 21:18:02 +0200325 cpu_dump_state(cs, NULL, CPU_DUMP_FPU);
Suraj Jitindar Singh18f08282017-06-08 15:41:16 +1000326 }
327 } else {
328 cs = mon_get_cpu();
329
330 if (!cs) {
331 monitor_printf(mon, "No CPU available\n");
332 return;
333 }
334
Markus Armbruster90c84c52019-04-17 21:18:02 +0200335 cpu_dump_state(cs, NULL, CPU_DUMP_FPU);
Thomas Huth854e67f2017-01-13 13:12:35 +0100336 }
bellard9307c4c2004-04-04 12:57:25 +0000337}
338
Paolo Bonzinif0d14a92012-09-17 13:42:41 +0200339#ifdef CONFIG_TCG
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100340static void hmp_info_jit(Monitor *mon, const QDict *qdict)
bellarde3db7222005-01-26 22:00:47 +0000341{
Thomas Huthb7da97e2017-04-26 06:11:47 +0200342 if (!tcg_enabled()) {
343 error_report("JIT information is only available with accel=tcg");
344 return;
345 }
346
Markus Armbruster3de2faa2019-04-17 21:17:52 +0200347 dump_exec_info();
Markus Armbruster76c86612019-04-17 21:17:53 +0200348 dump_drift_info();
bellarde3db7222005-01-26 22:00:47 +0000349}
350
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100351static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
Max Filippov246ae242014-11-02 11:04:18 +0300352{
Markus Armbrusterd4c51a02019-04-17 21:17:51 +0200353 dump_opcount_info();
Max Filippov246ae242014-11-02 11:04:18 +0300354}
Paolo Bonzinif0d14a92012-09-17 13:42:41 +0200355#endif
Max Filippov246ae242014-11-02 11:04:18 +0300356
Emilio G. Cota97bfafe2017-08-08 13:54:42 -0400357static void hmp_info_sync_profile(Monitor *mon, const QDict *qdict)
358{
359 int64_t max = qdict_get_try_int(qdict, "max", 10);
360 bool mean = qdict_get_try_bool(qdict, "mean", false);
361 bool coalesce = !qdict_get_try_bool(qdict, "no_coalesce", false);
362 enum QSPSortBy sort_by;
363
364 sort_by = mean ? QSP_SORT_BY_AVG_WAIT_TIME : QSP_SORT_BY_TOTAL_WAIT_TIME;
Markus Armbrusterac7ff4c2019-04-17 21:17:54 +0200365 qsp_report(max, sort_by, coalesce);
Emilio G. Cota97bfafe2017-08-08 13:54:42 -0400366}
367
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100368static void hmp_info_history(Monitor *mon, const QDict *qdict)
bellardaa455482004-04-04 13:07:25 +0000369{
Kevin Wolf5f9dba12019-06-13 17:33:54 +0200370 MonitorHMP *hmp_mon = container_of(mon, MonitorHMP, common);
bellardaa455482004-04-04 13:07:25 +0000371 int i;
bellard7e2515e2004-08-01 21:52:19 +0000372 const char *str;
ths3b46e622007-09-17 08:09:54 +0000373
Kevin Wolf5f9dba12019-06-13 17:33:54 +0200374 if (!hmp_mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000375 return;
Kevin Wolf5f9dba12019-06-13 17:33:54 +0200376 }
bellard7e2515e2004-08-01 21:52:19 +0000377 i = 0;
378 for(;;) {
Kevin Wolf5f9dba12019-06-13 17:33:54 +0200379 str = readline_get_history(hmp_mon->rs, i);
380 if (!str) {
bellard7e2515e2004-08-01 21:52:19 +0000381 break;
Kevin Wolf5f9dba12019-06-13 17:33:54 +0200382 }
aliguori376253e2009-03-05 23:01:23 +0000383 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000384 i++;
bellardaa455482004-04-04 13:07:25 +0000385 }
386}
387
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100388static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
j_mayer76a66252007-03-07 08:32:30 +0000389{
Thomas Huth854e67f2017-01-13 13:12:35 +0100390 CPUState *cs = mon_get_cpu();
391
392 if (!cs) {
393 monitor_printf(mon, "No CPU available\n");
394 return;
395 }
Markus Armbruster11cb6c12019-04-17 21:18:00 +0200396 cpu_dump_statistics(cs, 0);
j_mayer76a66252007-03-07 08:32:30 +0000397}
j_mayer76a66252007-03-07 08:32:30 +0000398
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100399static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530400{
Lluís Vilanovabd712112016-07-11 12:53:51 +0200401 const char *name = qdict_get_try_str(qdict, "name");
Lluís Vilanova77e2b172016-07-11 12:53:57 +0200402 bool has_vcpu = qdict_haskey(qdict, "vcpu");
403 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
Lluís Vilanovabd712112016-07-11 12:53:51 +0200404 TraceEventInfoList *events;
Lluís Vilanova14101d02014-08-25 13:20:03 +0200405 TraceEventInfoList *elem;
Lluís Vilanovabd712112016-07-11 12:53:51 +0200406 Error *local_err = NULL;
407
408 if (name == NULL) {
409 name = "*";
410 }
Lluís Vilanova77e2b172016-07-11 12:53:57 +0200411 if (vcpu < 0) {
412 monitor_printf(mon, "argument vcpu must be positive");
413 return;
414 }
Lluís Vilanovabd712112016-07-11 12:53:51 +0200415
Lluís Vilanova77e2b172016-07-11 12:53:57 +0200416 events = qmp_trace_event_get_state(name, has_vcpu, vcpu, &local_err);
Lluís Vilanovabd712112016-07-11 12:53:51 +0200417 if (local_err) {
418 error_report_err(local_err);
419 return;
420 }
Lluís Vilanova14101d02014-08-25 13:20:03 +0200421
422 for (elem = events; elem != NULL; elem = elem->next) {
423 monitor_printf(mon, "%s : state %u\n",
424 elem->value->name,
425 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
426 }
427 qapi_free_TraceEventInfoList(events);
Prerna Saxena22890ab2010-06-24 17:04:53 +0530428}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530429
Markus Armbrusterb8a185b2015-03-05 17:29:02 +0100430void qmp_client_migrate_info(const char *protocol, const char *hostname,
431 bool has_port, int64_t port,
432 bool has_tls_port, int64_t tls_port,
433 bool has_cert_subject, const char *cert_subject,
434 Error **errp)
Gerd Hoffmanne866e232010-04-23 13:28:21 +0200435{
Gerd Hoffmanne866e232010-04-23 13:28:21 +0200436 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb8a185b2015-03-05 17:29:02 +0100437 if (!qemu_using_spice(errp)) {
438 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +0200439 }
440
Markus Armbrusterb8a185b2015-03-05 17:29:02 +0100441 if (!has_port && !has_tls_port) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100442 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
Markus Armbrusterb8a185b2015-03-05 17:29:02 +0100443 return;
Yonit Halperin6ec5dae2012-03-18 09:42:39 +0200444 }
445
Markus Armbrusterb8a185b2015-03-05 17:29:02 +0100446 if (qemu_spice_migrate_info(hostname,
447 has_port ? port : -1,
448 has_tls_port ? tls_port : -1,
449 cert_subject)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100450 error_setg(errp, QERR_UNDEFINED_ERROR);
Markus Armbrusterb8a185b2015-03-05 17:29:02 +0100451 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +0200452 }
Markus Armbrusterb8a185b2015-03-05 17:29:02 +0100453 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +0200454 }
455
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100456 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
Gerd Hoffmanne866e232010-04-23 13:28:21 +0200457}
458
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100459static void hmp_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +0000460{
Markus Armbrusterdaa76aa2016-06-15 19:27:16 +0200461 Error *err = NULL;
462
463 qemu_set_log_filename(qdict_get_str(qdict, "filename"), &err);
464 if (err) {
465 error_report_err(err);
466 }
pbrooke735b912007-06-30 13:53:24 +0000467}
468
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100469static void hmp_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +0000470{
471 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300472 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +0000473
bellard9307c4c2004-04-04 12:57:25 +0000474 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +0000475 mask = 0;
476 } else {
Peter Maydell4fde1eb2013-02-11 16:41:22 +0000477 mask = qemu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +0000478 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +0000479 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +0000480 return;
481 }
482 }
Peter Maydell24537a02013-02-11 16:41:23 +0000483 qemu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +0000484}
485
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100486static void hmp_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +0000487{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300488 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +0000489 if (!option || !strcmp(option, "on")) {
490 singlestep = 1;
491 } else if (!strcmp(option, "off")) {
492 singlestep = 0;
493 } else {
494 monitor_printf(mon, "unexpected option %s\n", option);
495 }
496}
497
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100498static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +0000499{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300500 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +0000501 if (!device)
502 device = "tcp::" DEFAULT_GDBSTUB_PORT;
503 if (gdbserver_start(device) < 0) {
504 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
505 device);
506 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +0000507 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +0000508 } else {
aliguori59030a82009-04-05 18:43:41 +0000509 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
510 device);
bellard8a7ddc32004-03-31 19:00:16 +0000511 }
512}
513
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100514static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100515{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300516 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100517 if (select_watchdog_action(action) == -1) {
518 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
519 }
520}
521
aliguori376253e2009-03-05 23:01:23 +0000522static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +0000523{
aliguori376253e2009-03-05 23:01:23 +0000524 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000525 switch(c) {
526 case '\'':
aliguori376253e2009-03-05 23:01:23 +0000527 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +0000528 break;
529 case '\\':
aliguori376253e2009-03-05 23:01:23 +0000530 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +0000531 break;
532 case '\n':
aliguori376253e2009-03-05 23:01:23 +0000533 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +0000534 break;
535 case '\r':
aliguori376253e2009-03-05 23:01:23 +0000536 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +0000537 break;
538 default:
539 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +0000540 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +0000541 } else {
aliguori376253e2009-03-05 23:01:23 +0000542 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +0000543 }
544 break;
545 }
aliguori376253e2009-03-05 23:01:23 +0000546 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000547}
548
aliguori376253e2009-03-05 23:01:23 +0000549static void memory_dump(Monitor *mon, int count, int format, int wsize,
Avi Kivitya8170e52012-10-23 12:30:10 +0200550 hwaddr addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +0000551{
Blue Swirl23842aa2010-01-12 20:27:43 +0000552 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +0000553 uint8_t buf[16];
554 uint64_t v;
Thomas Huth854e67f2017-01-13 13:12:35 +0100555 CPUState *cs = mon_get_cpu();
556
557 if (!cs && (format == 'i' || !is_physical)) {
558 monitor_printf(mon, "Can not dump without CPU\n");
559 return;
560 }
bellard9307c4c2004-04-04 12:57:25 +0000561
562 if (format == 'i') {
Richard Henderson1d484742017-09-14 08:38:35 -0700563 monitor_disas(mon, cs, addr, count, is_physical);
bellard9307c4c2004-04-04 12:57:25 +0000564 return;
565 }
566
567 len = wsize * count;
568 if (wsize == 1)
569 line_size = 8;
570 else
571 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +0000572 max_digits = 0;
573
574 switch(format) {
575 case 'o':
Marc-André Lureau69db8df2017-06-22 13:04:16 +0200576 max_digits = DIV_ROUND_UP(wsize * 8, 3);
bellard9307c4c2004-04-04 12:57:25 +0000577 break;
578 default:
579 case 'x':
580 max_digits = (wsize * 8) / 4;
581 break;
582 case 'u':
583 case 'd':
Marc-André Lureau69db8df2017-06-22 13:04:16 +0200584 max_digits = DIV_ROUND_UP(wsize * 8 * 10, 33);
bellard9307c4c2004-04-04 12:57:25 +0000585 break;
586 case 'c':
587 wsize = 1;
588 break;
589 }
590
591 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +0000592 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +0000593 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +0000594 else
aliguori376253e2009-03-05 23:01:23 +0000595 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +0000596 l = len;
597 if (l > line_size)
598 l = line_size;
599 if (is_physical) {
Peter Maydell6f89ae52018-12-14 13:30:49 +0000600 AddressSpace *as = cs ? cs->as : &address_space_memory;
601 MemTxResult r = address_space_read(as, addr,
602 MEMTXATTRS_UNSPECIFIED, buf, l);
603 if (r != MEMTX_OK) {
604 monitor_printf(mon, " Cannot access memory\n");
605 break;
606 }
bellard9307c4c2004-04-04 12:57:25 +0000607 } else {
Thomas Huth854e67f2017-01-13 13:12:35 +0100608 if (cpu_memory_rw_debug(cs, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +0000609 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +0000610 break;
611 }
bellard9307c4c2004-04-04 12:57:25 +0000612 }
ths5fafdf22007-09-16 21:08:06 +0000613 i = 0;
bellard9307c4c2004-04-04 12:57:25 +0000614 while (i < l) {
615 switch(wsize) {
616 default:
617 case 1:
Peter Maydell24e60302015-01-20 15:19:32 +0000618 v = ldub_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000619 break;
620 case 2:
Peter Maydell24e60302015-01-20 15:19:32 +0000621 v = lduw_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000622 break;
623 case 4:
Peter Maydell24e60302015-01-20 15:19:32 +0000624 v = (uint32_t)ldl_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000625 break;
626 case 8:
Peter Maydell24e60302015-01-20 15:19:32 +0000627 v = ldq_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000628 break;
629 }
aliguori376253e2009-03-05 23:01:23 +0000630 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +0000631 switch(format) {
632 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000633 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000634 break;
635 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000636 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000637 break;
638 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000639 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000640 break;
641 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000642 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000643 break;
644 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000645 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +0000646 break;
647 }
648 i += wsize;
649 }
aliguori376253e2009-03-05 23:01:23 +0000650 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000651 addr += l;
652 len -= l;
653 }
654}
655
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100656static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000657{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300658 int count = qdict_get_int(qdict, "count");
659 int format = qdict_get_int(qdict, "format");
660 int size = qdict_get_int(qdict, "size");
661 target_long addr = qdict_get_int(qdict, "addr");
662
aliguori376253e2009-03-05 23:01:23 +0000663 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +0000664}
665
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100666static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000667{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300668 int count = qdict_get_int(qdict, "count");
669 int format = qdict_get_int(qdict, "format");
670 int size = qdict_get_int(qdict, "size");
Avi Kivitya8170e52012-10-23 12:30:10 +0200671 hwaddr addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300672
aliguori376253e2009-03-05 23:01:23 +0000673 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +0000674}
675
Paolo Bonzinie9628442017-04-20 15:30:58 +0200676static void *gpa2hva(MemoryRegion **p_mr, hwaddr addr, Error **errp)
677{
678 MemoryRegionSection mrs = memory_region_find(get_system_memory(),
679 addr, 1);
680
681 if (!mrs.mr) {
682 error_setg(errp, "No memory is mapped at address 0x%" HWADDR_PRIx, addr);
683 return NULL;
684 }
685
686 if (!memory_region_is_ram(mrs.mr) && !memory_region_is_romd(mrs.mr)) {
687 error_setg(errp, "Memory at address 0x%" HWADDR_PRIx "is not RAM", addr);
688 memory_region_unref(mrs.mr);
689 return NULL;
690 }
691
692 *p_mr = mrs.mr;
693 return qemu_map_ram_ptr(mrs.mr->ram_block, mrs.offset_within_region);
694}
695
696static void hmp_gpa2hva(Monitor *mon, const QDict *qdict)
697{
698 hwaddr addr = qdict_get_int(qdict, "addr");
699 Error *local_err = NULL;
700 MemoryRegion *mr = NULL;
701 void *ptr;
702
703 ptr = gpa2hva(&mr, addr, &local_err);
704 if (local_err) {
705 error_report_err(local_err);
706 return;
707 }
708
709 monitor_printf(mon, "Host virtual address for 0x%" HWADDR_PRIx
710 " (%s) is %p\n",
711 addr, mr->name, ptr);
712
713 memory_region_unref(mr);
714}
715
Dr. David Alan Gilbert574d9692019-04-12 16:26:52 +0100716static void hmp_gva2gpa(Monitor *mon, const QDict *qdict)
717{
718 target_ulong addr = qdict_get_int(qdict, "addr");
719 MemTxAttrs attrs;
720 CPUState *cs = mon_get_cpu();
721 hwaddr gpa;
722
723 if (!cs) {
724 monitor_printf(mon, "No cpu\n");
725 return;
726 }
727
Eduardo Habkost9d3250d2019-05-10 15:56:20 -0300728 gpa = cpu_get_phys_page_attrs_debug(cs, addr & TARGET_PAGE_MASK, &attrs);
Dr. David Alan Gilbert574d9692019-04-12 16:26:52 +0100729 if (gpa == -1) {
730 monitor_printf(mon, "Unmapped\n");
731 } else {
732 monitor_printf(mon, "gpa: %#" HWADDR_PRIx "\n",
733 gpa + (addr & ~TARGET_PAGE_MASK));
734 }
735}
736
Paolo Bonzinie9628442017-04-20 15:30:58 +0200737#ifdef CONFIG_LINUX
738static uint64_t vtop(void *ptr, Error **errp)
739{
740 uint64_t pinfo;
741 uint64_t ret = -1;
742 uintptr_t addr = (uintptr_t) ptr;
Wei Yang038adc22019-10-13 10:11:45 +0800743 uintptr_t pagesize = qemu_real_host_page_size;
Paolo Bonzinie9628442017-04-20 15:30:58 +0200744 off_t offset = addr / pagesize * sizeof(pinfo);
745 int fd;
746
747 fd = open("/proc/self/pagemap", O_RDONLY);
748 if (fd == -1) {
749 error_setg_errno(errp, errno, "Cannot open /proc/self/pagemap");
750 return -1;
751 }
752
753 /* Force copy-on-write if necessary. */
754 atomic_add((uint8_t *)ptr, 0);
755
756 if (pread(fd, &pinfo, sizeof(pinfo), offset) != sizeof(pinfo)) {
757 error_setg_errno(errp, errno, "Cannot read pagemap");
758 goto out;
759 }
760 if ((pinfo & (1ull << 63)) == 0) {
761 error_setg(errp, "Page not present");
762 goto out;
763 }
764 ret = ((pinfo & 0x007fffffffffffffull) * pagesize) | (addr & (pagesize - 1));
765
766out:
767 close(fd);
768 return ret;
769}
770
771static void hmp_gpa2hpa(Monitor *mon, const QDict *qdict)
772{
773 hwaddr addr = qdict_get_int(qdict, "addr");
774 Error *local_err = NULL;
775 MemoryRegion *mr = NULL;
776 void *ptr;
777 uint64_t physaddr;
778
779 ptr = gpa2hva(&mr, addr, &local_err);
780 if (local_err) {
781 error_report_err(local_err);
782 return;
783 }
784
785 physaddr = vtop(ptr, &local_err);
786 if (local_err) {
787 error_report_err(local_err);
788 } else {
789 monitor_printf(mon, "Host physical address for 0x%" HWADDR_PRIx
790 " (%s) is 0x%" PRIx64 "\n",
791 addr, mr->name, (uint64_t) physaddr);
792 }
793
794 memory_region_unref(mr);
795}
796#endif
797
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300798static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000799{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300800 int format = qdict_get_int(qdict, "format");
Avi Kivitya8170e52012-10-23 12:30:10 +0200801 hwaddr val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300802
bellard9307c4c2004-04-04 12:57:25 +0000803 switch(format) {
804 case 'o':
Avi Kivitya8170e52012-10-23 12:30:10 +0200805 monitor_printf(mon, "%#" HWADDR_PRIo, val);
bellard9307c4c2004-04-04 12:57:25 +0000806 break;
807 case 'x':
Avi Kivitya8170e52012-10-23 12:30:10 +0200808 monitor_printf(mon, "%#" HWADDR_PRIx, val);
bellard9307c4c2004-04-04 12:57:25 +0000809 break;
810 case 'u':
Avi Kivitya8170e52012-10-23 12:30:10 +0200811 monitor_printf(mon, "%" HWADDR_PRIu, val);
bellard9307c4c2004-04-04 12:57:25 +0000812 break;
813 default:
814 case 'd':
Avi Kivitya8170e52012-10-23 12:30:10 +0200815 monitor_printf(mon, "%" HWADDR_PRId, val);
bellard9307c4c2004-04-04 12:57:25 +0000816 break;
817 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000818 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +0000819 break;
820 }
aliguori376253e2009-03-05 23:01:23 +0000821 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000822}
823
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100824static void hmp_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +0000825{
826 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +0000827 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300828 uint32_t start = qdict_get_int(qdict, "start");
829 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +0000830
831 sum = 0;
832 for(addr = start; addr < (start + size); addr++) {
Peter Maydell42874d32015-04-26 16:49:24 +0100833 uint8_t val = address_space_ldub(&address_space_memory, addr,
834 MEMTXATTRS_UNSPECIFIED, NULL);
bellarde4cf1ad2005-06-04 20:15:57 +0000835 /* BSD sum algorithm ('sum' Unix command) */
836 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +0200837 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +0000838 }
aliguori376253e2009-03-05 23:01:23 +0000839 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +0000840}
841
bellard13224a82006-07-14 22:03:35 +0000842static int mouse_button_state;
843
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100844static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +0000845{
Gerd Hoffmannc751a742013-12-04 15:02:28 +0100846 int dx, dy, dz, button;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300847 const char *dx_str = qdict_get_str(qdict, "dx_str");
848 const char *dy_str = qdict_get_str(qdict, "dy_str");
849 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
Gerd Hoffmannc751a742013-12-04 15:02:28 +0100850
bellard13224a82006-07-14 22:03:35 +0000851 dx = strtol(dx_str, NULL, 0);
852 dy = strtol(dy_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +0100853 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
854 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
855
856 if (dz_str) {
bellard13224a82006-07-14 22:03:35 +0000857 dz = strtol(dz_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +0100858 if (dz != 0) {
Gerd Hoffmannf22d0af2016-01-12 12:14:12 +0100859 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
Gerd Hoffmannc751a742013-12-04 15:02:28 +0100860 qemu_input_queue_btn(NULL, button, true);
861 qemu_input_event_sync();
862 qemu_input_queue_btn(NULL, button, false);
863 }
864 }
865 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +0000866}
867
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100868static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +0000869{
Eric Blake7fb1cf12015-11-18 01:52:57 -0700870 static uint32_t bmap[INPUT_BUTTON__MAX] = {
Gerd Hoffmannc751a742013-12-04 15:02:28 +0100871 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
872 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
873 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
874 };
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300875 int button_state = qdict_get_int(qdict, "button_state");
Gerd Hoffmannc751a742013-12-04 15:02:28 +0100876
877 if (mouse_button_state == button_state) {
878 return;
879 }
880 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
881 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +0000882 mouse_button_state = button_state;
bellard13224a82006-07-14 22:03:35 +0000883}
884
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100885static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +0000886{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -0300887 int size = qdict_get_int(qdict, "size");
888 int addr = qdict_get_int(qdict, "addr");
889 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +0000890 uint32_t val;
891 int suffix;
892
893 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -0300894 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +0000895 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +0000896 addr++;
897 }
898 addr &= 0xffff;
899
900 switch(size) {
901 default:
902 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +0000903 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +0000904 suffix = 'b';
905 break;
906 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +0000907 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +0000908 suffix = 'w';
909 break;
910 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +0000911 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +0000912 suffix = 'l';
913 break;
914 }
aliguori376253e2009-03-05 23:01:23 +0000915 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
916 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +0000917}
bellarda3a91a32004-06-04 11:06:21 +0000918
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100919static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +0200920{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300921 int size = qdict_get_int(qdict, "size");
922 int addr = qdict_get_int(qdict, "addr");
923 int val = qdict_get_int(qdict, "val");
924
Jan Kiszkaf1147842009-07-14 10:20:11 +0200925 addr &= IOPORTS_MASK;
926
927 switch (size) {
928 default:
929 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +0000930 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +0200931 break;
932 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +0000933 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +0200934 break;
935 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +0000936 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +0200937 break;
938 }
939}
940
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100941static void hmp_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +0000942{
Gongleif1839932014-12-03 18:20:58 +0000943 Error *local_err = NULL;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300944 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +0000945
Gongleif1839932014-12-03 18:20:58 +0000946 qemu_boot_set(bootdevice, &local_err);
947 if (local_err) {
Markus Armbruster193227f2015-12-18 16:35:06 +0100948 error_report_err(local_err);
aurel320ecdffb2008-05-04 20:11:34 +0000949 } else {
Gongleif1839932014-12-03 18:20:58 +0000950 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +0000951 }
952}
953
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100954static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
Blue Swirl314e2982011-09-11 20:22:05 +0000955{
Peter Xu57bb40c2017-01-16 16:40:05 +0800956 bool flatview = qdict_get_try_bool(qdict, "flatview", false);
Alexey Kardashevskiy5e8fd942017-09-21 18:51:06 +1000957 bool dispatch_tree = qdict_get_try_bool(qdict, "dispatch_tree", false);
Alexey Kardashevskiyfc051ae2018-06-04 13:25:11 +1000958 bool owner = qdict_get_try_bool(qdict, "owner", false);
Philippe Mathieu-Daudé2261d392020-05-29 14:53:25 +0200959 bool disabled = qdict_get_try_bool(qdict, "disabled", false);
Peter Xu57bb40c2017-01-16 16:40:05 +0800960
Philippe Mathieu-Daudé2261d392020-05-29 14:53:25 +0200961 mtree_info(flatview, dispatch_tree, owner, disabled);
Blue Swirl314e2982011-09-11 20:22:05 +0000962}
963
bellard5f1ce942006-02-08 22:40:15 +0000964#ifdef CONFIG_PROFILER
965
Aurelien Jarnoe9a66252009-09-30 14:09:52 +0200966int64_t dev_time;
967
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100968static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +0000969{
Emilio G. Cota72fd2ef2018-10-10 10:48:53 -0400970 static int64_t last_cpu_exec_time;
971 int64_t cpu_exec_time;
972 int64_t delta;
973
974 cpu_exec_time = tcg_cpu_exec_time();
975 delta = cpu_exec_time - last_cpu_exec_time;
976
aliguori376253e2009-03-05 23:01:23 +0000977 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Rutuja Shah73bcb242016-03-21 21:32:30 +0530978 dev_time, dev_time / (double)NANOSECONDS_PER_SECOND);
aliguori376253e2009-03-05 23:01:23 +0000979 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Emilio G. Cota72fd2ef2018-10-10 10:48:53 -0400980 delta, delta / (double)NANOSECONDS_PER_SECOND);
981 last_cpu_exec_time = cpu_exec_time;
bellard5f1ce942006-02-08 22:40:15 +0000982 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +0000983}
984#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100985static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +0000986{
aliguori376253e2009-03-05 23:01:23 +0000987 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +0000988}
989#endif
990
bellardec36b692006-07-16 18:57:03 +0000991/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +0000992static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +0000993
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100994static void hmp_info_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +0000995{
996 int i;
997 CaptureState *s;
998
999 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001000 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001001 s->ops.info (s->opaque);
1002 }
1003}
1004
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001005static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001006{
1007 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001008 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001009 CaptureState *s;
1010
1011 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1012 if (i == n) {
1013 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001014 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001015 g_free (s);
bellardec36b692006-07-16 18:57:03 +00001016 return;
1017 }
1018 }
1019}
1020
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001021static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001022{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001023 const char *path = qdict_get_str(qdict, "path");
Kővágó, Zoltánf0b9f362019-08-19 01:06:48 +02001024 int freq = qdict_get_try_int(qdict, "freq", 44100);
1025 int bits = qdict_get_try_int(qdict, "bits", 16);
1026 int nchannels = qdict_get_try_int(qdict, "nchannels", 2);
1027 const char *audiodev = qdict_get_str(qdict, "audiodev");
bellardec36b692006-07-16 18:57:03 +00001028 CaptureState *s;
Kővágó, Zoltánf0b9f362019-08-19 01:06:48 +02001029 AudioState *as = audio_state_by_name(audiodev);
1030
1031 if (!as) {
1032 monitor_printf(mon, "Audiodev '%s' not found\n", audiodev);
1033 return;
1034 }
bellardec36b692006-07-16 18:57:03 +00001035
Anthony Liguori7267c092011-08-20 22:09:37 -05001036 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001037
Kővágó, Zoltánf0b9f362019-08-19 01:06:48 +02001038 if (wav_start_capture(as, s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001039 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05001040 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001041 return;
bellardec36b692006-07-16 18:57:03 +00001042 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001043 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001044}
bellardec36b692006-07-16 18:57:03 +00001045
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00001046static QAuthZList *find_auth(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001047{
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00001048 Object *obj;
1049 Object *container;
aliguori76655d62009-03-06 20:27:37 +00001050
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00001051 container = object_get_objects_root();
1052 obj = object_resolve_path_component(container, name);
1053 if (!obj) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001054 monitor_printf(mon, "acl: unknown list '%s'\n", name);
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00001055 return NULL;
aliguori76655d62009-03-06 20:27:37 +00001056 }
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00001057
1058 return QAUTHZ_LIST(obj);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001059}
aliguori76655d62009-03-06 20:27:37 +00001060
Daniel P. Berrangé01438402019-02-27 14:57:55 +00001061static bool warn_acl;
1062static void hmp_warn_acl(void)
1063{
1064 if (warn_acl) {
1065 return;
1066 }
1067 error_report("The acl_show, acl_reset, acl_policy, acl_add, acl_remove "
1068 "commands are deprecated with no replacement. Authorization "
1069 "for VNC should be performed using the pluggable QAuthZ "
1070 "objects");
1071 warn_acl = true;
1072}
1073
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001074static void hmp_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001075{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001076 const char *aclname = qdict_get_str(qdict, "aclname");
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00001077 QAuthZList *auth = find_auth(mon, aclname);
1078 QAuthZListRuleList *rules;
1079 size_t i = 0;
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001080
Daniel P. Berrangé01438402019-02-27 14:57:55 +00001081 hmp_warn_acl();
1082
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00001083 if (!auth) {
1084 return;
1085 }
1086
1087 monitor_printf(mon, "policy: %s\n",
1088 QAuthZListPolicy_str(auth->policy));
1089
1090 rules = auth->rules;
1091 while (rules) {
1092 QAuthZListRule *rule = rules->value;
1093 i++;
1094 monitor_printf(mon, "%zu: %s %s\n", i,
1095 QAuthZListPolicy_str(rule->policy),
1096 rule->match);
1097 rules = rules->next;
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001098 }
1099}
1100
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001101static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001102{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001103 const char *aclname = qdict_get_str(qdict, "aclname");
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00001104 QAuthZList *auth = find_auth(mon, aclname);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001105
Daniel P. Berrangé01438402019-02-27 14:57:55 +00001106 hmp_warn_acl();
1107
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00001108 if (!auth) {
1109 return;
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001110 }
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00001111
1112 auth->policy = QAUTHZ_LIST_POLICY_DENY;
1113 qapi_free_QAuthZListRuleList(auth->rules);
1114 auth->rules = NULL;
1115 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001116}
aliguori76655d62009-03-06 20:27:37 +00001117
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001118static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001119{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001120 const char *aclname = qdict_get_str(qdict, "aclname");
1121 const char *policy = qdict_get_str(qdict, "policy");
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00001122 QAuthZList *auth = find_auth(mon, aclname);
1123 int val;
1124 Error *err = NULL;
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001125
Daniel P. Berrangé01438402019-02-27 14:57:55 +00001126 hmp_warn_acl();
1127
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00001128 if (!auth) {
1129 return;
1130 }
1131
1132 val = qapi_enum_parse(&QAuthZListPolicy_lookup,
1133 policy,
1134 QAUTHZ_LIST_POLICY_DENY,
1135 &err);
1136 if (err) {
1137 error_free(err);
1138 monitor_printf(mon, "acl: unknown policy '%s', "
1139 "expected 'deny' or 'allow'\n", policy);
1140 } else {
1141 auth->policy = val;
1142 if (auth->policy == QAUTHZ_LIST_POLICY_ALLOW) {
aliguori28a76be2009-03-06 20:27:40 +00001143 monitor_printf(mon, "acl: policy set to 'allow'\n");
aliguori28a76be2009-03-06 20:27:40 +00001144 } else {
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00001145 monitor_printf(mon, "acl: policy set to 'deny'\n");
aliguori28a76be2009-03-06 20:27:40 +00001146 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001147 }
1148}
aliguori76655d62009-03-06 20:27:37 +00001149
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00001150static QAuthZListFormat hmp_acl_get_format(const char *match)
1151{
1152 if (strchr(match, '*')) {
1153 return QAUTHZ_LIST_FORMAT_GLOB;
1154 } else {
1155 return QAUTHZ_LIST_FORMAT_EXACT;
1156 }
1157}
1158
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001159static void hmp_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001160{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001161 const char *aclname = qdict_get_str(qdict, "aclname");
1162 const char *match = qdict_get_str(qdict, "match");
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00001163 const char *policystr = qdict_get_str(qdict, "policy");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001164 int has_index = qdict_haskey(qdict, "index");
1165 int index = qdict_get_try_int(qdict, "index", -1);
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00001166 QAuthZList *auth = find_auth(mon, aclname);
1167 Error *err = NULL;
1168 QAuthZListPolicy policy;
1169 QAuthZListFormat format;
1170 size_t i = 0;
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001171
Daniel P. Berrangé01438402019-02-27 14:57:55 +00001172 hmp_warn_acl();
1173
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00001174 if (!auth) {
1175 return;
1176 }
1177
1178 policy = qapi_enum_parse(&QAuthZListPolicy_lookup,
1179 policystr,
1180 QAUTHZ_LIST_POLICY_DENY,
1181 &err);
1182 if (err) {
1183 error_free(err);
1184 monitor_printf(mon, "acl: unknown policy '%s', "
1185 "expected 'deny' or 'allow'\n", policystr);
1186 return;
1187 }
1188
1189 format = hmp_acl_get_format(match);
1190
1191 if (has_index && index == 0) {
1192 monitor_printf(mon, "acl: unable to add acl entry\n");
1193 return;
1194 }
1195
1196 if (has_index) {
1197 i = qauthz_list_insert_rule(auth, match, policy,
1198 format, index - 1, &err);
1199 } else {
1200 i = qauthz_list_append_rule(auth, match, policy,
1201 format, &err);
1202 }
1203 if (err) {
1204 monitor_printf(mon, "acl: unable to add rule: %s",
1205 error_get_pretty(err));
1206 error_free(err);
1207 } else {
1208 monitor_printf(mon, "acl: added rule at position %zu\n", i + 1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001209 }
1210}
aliguori76655d62009-03-06 20:27:37 +00001211
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001212static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001213{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001214 const char *aclname = qdict_get_str(qdict, "aclname");
1215 const char *match = qdict_get_str(qdict, "match");
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00001216 QAuthZList *auth = find_auth(mon, aclname);
1217 ssize_t i = 0;
aliguori76655d62009-03-06 20:27:37 +00001218
Daniel P. Berrangé01438402019-02-27 14:57:55 +00001219 hmp_warn_acl();
1220
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00001221 if (!auth) {
1222 return;
1223 }
1224
1225 i = qauthz_list_delete_rule(auth, match);
1226 if (i >= 0) {
1227 monitor_printf(mon, "acl: removed rule at position %zu\n", i + 1);
1228 } else {
1229 monitor_printf(mon, "acl: no matching acl entry\n");
aliguori76655d62009-03-06 20:27:37 +00001230 }
1231}
1232
Corey Bryant208c9d12012-06-22 14:36:09 -04001233void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001234{
Anthony Liguoric227f092009-10-01 16:12:16 -05001235 mon_fd_t *monfd;
Peter Xu9409fc02018-06-08 11:55:06 +08001236 int fd, tmp_fd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001237
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03001238 fd = qemu_chr_fe_get_msgfd(&cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001239 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001240 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryant208c9d12012-06-22 14:36:09 -04001241 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001242 }
1243
1244 if (qemu_isdigit(fdname[0])) {
Stefan Hajnoczi0b9f0e22014-04-24 13:58:18 +02001245 close(fd);
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001246 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
1247 "a name not starting with a digit");
Corey Bryant208c9d12012-06-22 14:36:09 -04001248 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001249 }
1250
Peter Xu9409fc02018-06-08 11:55:06 +08001251 qemu_mutex_lock(&cur_mon->mon_lock);
Corey Bryant208c9d12012-06-22 14:36:09 -04001252 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001253 if (strcmp(monfd->name, fdname) != 0) {
1254 continue;
1255 }
1256
Peter Xu9409fc02018-06-08 11:55:06 +08001257 tmp_fd = monfd->fd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001258 monfd->fd = fd;
Peter Xu9409fc02018-06-08 11:55:06 +08001259 qemu_mutex_unlock(&cur_mon->mon_lock);
Markus Armbruster774a6b62018-07-03 10:53:57 +02001260 /* Make sure close() is outside critical section */
Peter Xu9409fc02018-06-08 11:55:06 +08001261 close(tmp_fd);
Corey Bryant208c9d12012-06-22 14:36:09 -04001262 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001263 }
1264
Anthony Liguori7267c092011-08-20 22:09:37 -05001265 monfd = g_malloc0(sizeof(mon_fd_t));
1266 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001267 monfd->fd = fd;
1268
Corey Bryant208c9d12012-06-22 14:36:09 -04001269 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Peter Xu9409fc02018-06-08 11:55:06 +08001270 qemu_mutex_unlock(&cur_mon->mon_lock);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001271}
1272
Corey Bryant208c9d12012-06-22 14:36:09 -04001273void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001274{
Anthony Liguoric227f092009-10-01 16:12:16 -05001275 mon_fd_t *monfd;
Peter Xu9409fc02018-06-08 11:55:06 +08001276 int tmp_fd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001277
Peter Xu9409fc02018-06-08 11:55:06 +08001278 qemu_mutex_lock(&cur_mon->mon_lock);
Corey Bryant208c9d12012-06-22 14:36:09 -04001279 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001280 if (strcmp(monfd->name, fdname) != 0) {
1281 continue;
1282 }
1283
Blue Swirl72cf2d42009-09-12 07:36:22 +00001284 QLIST_REMOVE(monfd, next);
Peter Xu9409fc02018-06-08 11:55:06 +08001285 tmp_fd = monfd->fd;
Anthony Liguori7267c092011-08-20 22:09:37 -05001286 g_free(monfd->name);
1287 g_free(monfd);
Peter Xu9409fc02018-06-08 11:55:06 +08001288 qemu_mutex_unlock(&cur_mon->mon_lock);
Markus Armbruster774a6b62018-07-03 10:53:57 +02001289 /* Make sure close() is outside critical section */
Peter Xu9409fc02018-06-08 11:55:06 +08001290 close(tmp_fd);
Corey Bryant208c9d12012-06-22 14:36:09 -04001291 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001292 }
1293
Peter Xu9409fc02018-06-08 11:55:06 +08001294 qemu_mutex_unlock(&cur_mon->mon_lock);
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001295 error_setg(errp, QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001296}
1297
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001298int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
Mark McLoughlin7768e042009-07-22 09:11:41 +01001299{
Anthony Liguoric227f092009-10-01 16:12:16 -05001300 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01001301
Peter Xu9409fc02018-06-08 11:55:06 +08001302 qemu_mutex_lock(&mon->mon_lock);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001303 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01001304 int fd;
1305
1306 if (strcmp(monfd->name, fdname) != 0) {
1307 continue;
1308 }
1309
1310 fd = monfd->fd;
1311
1312 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001313 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05001314 g_free(monfd->name);
1315 g_free(monfd);
Peter Xu9409fc02018-06-08 11:55:06 +08001316 qemu_mutex_unlock(&mon->mon_lock);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001317
1318 return fd;
1319 }
1320
Peter Xu9409fc02018-06-08 11:55:06 +08001321 qemu_mutex_unlock(&mon->mon_lock);
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001322 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001323 return -1;
1324}
1325
Corey Bryantba1c0482012-08-14 16:43:43 -04001326static void monitor_fdset_cleanup(MonFdset *mon_fdset)
1327{
1328 MonFdsetFd *mon_fdset_fd;
1329 MonFdsetFd *mon_fdset_fd_next;
1330
1331 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantebe52b52012-10-18 15:19:33 -04001332 if ((mon_fdset_fd->removed ||
1333 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
1334 runstate_is_running()) {
Corey Bryantba1c0482012-08-14 16:43:43 -04001335 close(mon_fdset_fd->fd);
1336 g_free(mon_fdset_fd->opaque);
1337 QLIST_REMOVE(mon_fdset_fd, next);
1338 g_free(mon_fdset_fd);
1339 }
1340 }
1341
Corey Bryantadb696f2012-08-14 16:43:47 -04001342 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04001343 QLIST_REMOVE(mon_fdset, next);
1344 g_free(mon_fdset);
1345 }
1346}
1347
Kevin Wolf7e3c0de2019-06-13 17:34:00 +02001348void monitor_fdsets_cleanup(void)
Corey Bryantefb87c12012-08-14 16:43:48 -04001349{
1350 MonFdset *mon_fdset;
1351 MonFdset *mon_fdset_next;
1352
Peter Xu47451462018-06-08 11:55:11 +08001353 qemu_mutex_lock(&mon_fdsets_lock);
Corey Bryantefb87c12012-08-14 16:43:48 -04001354 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
1355 monitor_fdset_cleanup(mon_fdset);
1356 }
Peter Xu47451462018-06-08 11:55:11 +08001357 qemu_mutex_unlock(&mon_fdsets_lock);
Corey Bryantefb87c12012-08-14 16:43:48 -04001358}
1359
Corey Bryantba1c0482012-08-14 16:43:43 -04001360AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
1361 const char *opaque, Error **errp)
1362{
1363 int fd;
1364 Monitor *mon = cur_mon;
Corey Bryantba1c0482012-08-14 16:43:43 -04001365 AddfdInfo *fdinfo;
1366
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03001367 fd = qemu_chr_fe_get_msgfd(&mon->chr);
Corey Bryantba1c0482012-08-14 16:43:43 -04001368 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001369 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryantba1c0482012-08-14 16:43:43 -04001370 goto error;
1371 }
1372
Corey Bryante446f702012-10-18 15:19:32 -04001373 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
1374 has_opaque, opaque, errp);
1375 if (fdinfo) {
1376 return fdinfo;
Corey Bryant9ac54af2012-10-18 15:19:31 -04001377 }
1378
Corey Bryantba1c0482012-08-14 16:43:43 -04001379error:
1380 if (fd != -1) {
1381 close(fd);
1382 }
1383 return NULL;
1384}
1385
1386void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
1387{
1388 MonFdset *mon_fdset;
1389 MonFdsetFd *mon_fdset_fd;
1390 char fd_str[60];
1391
Peter Xu47451462018-06-08 11:55:11 +08001392 qemu_mutex_lock(&mon_fdsets_lock);
Corey Bryantba1c0482012-08-14 16:43:43 -04001393 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1394 if (mon_fdset->id != fdset_id) {
1395 continue;
1396 }
1397 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1398 if (has_fd) {
1399 if (mon_fdset_fd->fd != fd) {
1400 continue;
1401 }
1402 mon_fdset_fd->removed = true;
1403 break;
1404 } else {
1405 mon_fdset_fd->removed = true;
1406 }
1407 }
1408 if (has_fd && !mon_fdset_fd) {
1409 goto error;
1410 }
1411 monitor_fdset_cleanup(mon_fdset);
Peter Xu47451462018-06-08 11:55:11 +08001412 qemu_mutex_unlock(&mon_fdsets_lock);
Corey Bryantba1c0482012-08-14 16:43:43 -04001413 return;
1414 }
1415
1416error:
Peter Xu47451462018-06-08 11:55:11 +08001417 qemu_mutex_unlock(&mon_fdsets_lock);
Corey Bryantba1c0482012-08-14 16:43:43 -04001418 if (has_fd) {
1419 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
1420 fdset_id, fd);
1421 } else {
1422 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
1423 }
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001424 error_setg(errp, QERR_FD_NOT_FOUND, fd_str);
Corey Bryantba1c0482012-08-14 16:43:43 -04001425}
1426
1427FdsetInfoList *qmp_query_fdsets(Error **errp)
1428{
1429 MonFdset *mon_fdset;
1430 MonFdsetFd *mon_fdset_fd;
1431 FdsetInfoList *fdset_list = NULL;
1432
Peter Xu47451462018-06-08 11:55:11 +08001433 qemu_mutex_lock(&mon_fdsets_lock);
Corey Bryantba1c0482012-08-14 16:43:43 -04001434 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1435 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
1436 FdsetFdInfoList *fdsetfd_list = NULL;
1437
1438 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
1439 fdset_info->value->fdset_id = mon_fdset->id;
1440
1441 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1442 FdsetFdInfoList *fdsetfd_info;
1443
1444 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
1445 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
1446 fdsetfd_info->value->fd = mon_fdset_fd->fd;
1447 if (mon_fdset_fd->opaque) {
1448 fdsetfd_info->value->has_opaque = true;
1449 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
1450 } else {
1451 fdsetfd_info->value->has_opaque = false;
1452 }
1453
1454 fdsetfd_info->next = fdsetfd_list;
1455 fdsetfd_list = fdsetfd_info;
1456 }
1457
1458 fdset_info->value->fds = fdsetfd_list;
1459
1460 fdset_info->next = fdset_list;
1461 fdset_list = fdset_info;
1462 }
Peter Xu47451462018-06-08 11:55:11 +08001463 qemu_mutex_unlock(&mon_fdsets_lock);
Corey Bryantba1c0482012-08-14 16:43:43 -04001464
1465 return fdset_list;
1466}
1467
Corey Bryante446f702012-10-18 15:19:32 -04001468AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
1469 bool has_opaque, const char *opaque,
1470 Error **errp)
1471{
1472 MonFdset *mon_fdset = NULL;
1473 MonFdsetFd *mon_fdset_fd;
1474 AddfdInfo *fdinfo;
1475
Daniel Brodsky6e8a3552020-04-03 21:21:08 -07001476 QEMU_LOCK_GUARD(&mon_fdsets_lock);
Corey Bryante446f702012-10-18 15:19:32 -04001477 if (has_fdset_id) {
1478 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1479 /* Break if match found or match impossible due to ordering by ID */
1480 if (fdset_id <= mon_fdset->id) {
1481 if (fdset_id < mon_fdset->id) {
1482 mon_fdset = NULL;
1483 }
1484 break;
1485 }
1486 }
1487 }
1488
1489 if (mon_fdset == NULL) {
1490 int64_t fdset_id_prev = -1;
1491 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
1492
1493 if (has_fdset_id) {
1494 if (fdset_id < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001495 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
1496 "a non-negative value");
Corey Bryante446f702012-10-18 15:19:32 -04001497 return NULL;
1498 }
1499 /* Use specified fdset ID */
1500 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1501 mon_fdset_cur = mon_fdset;
1502 if (fdset_id < mon_fdset_cur->id) {
1503 break;
1504 }
1505 }
1506 } else {
1507 /* Use first available fdset ID */
1508 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1509 mon_fdset_cur = mon_fdset;
1510 if (fdset_id_prev == mon_fdset_cur->id - 1) {
1511 fdset_id_prev = mon_fdset_cur->id;
1512 continue;
1513 }
1514 break;
1515 }
1516 }
1517
1518 mon_fdset = g_malloc0(sizeof(*mon_fdset));
1519 if (has_fdset_id) {
1520 mon_fdset->id = fdset_id;
1521 } else {
1522 mon_fdset->id = fdset_id_prev + 1;
1523 }
1524
1525 /* The fdset list is ordered by fdset ID */
1526 if (!mon_fdset_cur) {
1527 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
1528 } else if (mon_fdset->id < mon_fdset_cur->id) {
1529 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
1530 } else {
1531 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
1532 }
1533 }
1534
1535 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
1536 mon_fdset_fd->fd = fd;
1537 mon_fdset_fd->removed = false;
1538 if (has_opaque) {
1539 mon_fdset_fd->opaque = g_strdup(opaque);
1540 }
1541 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
1542
1543 fdinfo = g_malloc0(sizeof(*fdinfo));
1544 fdinfo->fdset_id = mon_fdset->id;
1545 fdinfo->fd = mon_fdset_fd->fd;
1546
1547 return fdinfo;
1548}
1549
Corey Bryantadb696f2012-08-14 16:43:47 -04001550int monitor_fdset_get_fd(int64_t fdset_id, int flags)
1551{
Peter Xu47451462018-06-08 11:55:11 +08001552#ifdef _WIN32
1553 return -ENOENT;
1554#else
Corey Bryantadb696f2012-08-14 16:43:47 -04001555 MonFdset *mon_fdset;
1556 MonFdsetFd *mon_fdset_fd;
1557 int mon_fd_flags;
Peter Xu47451462018-06-08 11:55:11 +08001558 int ret;
Corey Bryantadb696f2012-08-14 16:43:47 -04001559
Peter Xu47451462018-06-08 11:55:11 +08001560 qemu_mutex_lock(&mon_fdsets_lock);
Corey Bryantadb696f2012-08-14 16:43:47 -04001561 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1562 if (mon_fdset->id != fdset_id) {
1563 continue;
1564 }
1565 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1566 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
1567 if (mon_fd_flags == -1) {
Peter Xu47451462018-06-08 11:55:11 +08001568 ret = -errno;
1569 goto out;
Corey Bryantadb696f2012-08-14 16:43:47 -04001570 }
1571
1572 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
Peter Xu47451462018-06-08 11:55:11 +08001573 ret = mon_fdset_fd->fd;
1574 goto out;
Corey Bryantadb696f2012-08-14 16:43:47 -04001575 }
1576 }
Peter Xu47451462018-06-08 11:55:11 +08001577 ret = -EACCES;
1578 goto out;
Corey Bryantadb696f2012-08-14 16:43:47 -04001579 }
Peter Xu47451462018-06-08 11:55:11 +08001580 ret = -ENOENT;
Corey Bryantadb696f2012-08-14 16:43:47 -04001581
Peter Xu47451462018-06-08 11:55:11 +08001582out:
1583 qemu_mutex_unlock(&mon_fdsets_lock);
1584 return ret;
1585#endif
Corey Bryantadb696f2012-08-14 16:43:47 -04001586}
1587
1588int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
1589{
1590 MonFdset *mon_fdset;
1591 MonFdsetFd *mon_fdset_fd_dup;
1592
Peter Xu47451462018-06-08 11:55:11 +08001593 qemu_mutex_lock(&mon_fdsets_lock);
Corey Bryantadb696f2012-08-14 16:43:47 -04001594 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1595 if (mon_fdset->id != fdset_id) {
1596 continue;
1597 }
1598 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
1599 if (mon_fdset_fd_dup->fd == dup_fd) {
Peter Xu47451462018-06-08 11:55:11 +08001600 goto err;
Corey Bryantadb696f2012-08-14 16:43:47 -04001601 }
1602 }
1603 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
1604 mon_fdset_fd_dup->fd = dup_fd;
1605 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
Peter Xu47451462018-06-08 11:55:11 +08001606 qemu_mutex_unlock(&mon_fdsets_lock);
Corey Bryantadb696f2012-08-14 16:43:47 -04001607 return 0;
1608 }
Peter Xu47451462018-06-08 11:55:11 +08001609
1610err:
1611 qemu_mutex_unlock(&mon_fdsets_lock);
Corey Bryantadb696f2012-08-14 16:43:47 -04001612 return -1;
1613}
1614
Yury Kotov854f63d2019-05-23 12:44:33 +03001615static int64_t monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
Corey Bryantadb696f2012-08-14 16:43:47 -04001616{
1617 MonFdset *mon_fdset;
1618 MonFdsetFd *mon_fdset_fd_dup;
1619
Peter Xu47451462018-06-08 11:55:11 +08001620 qemu_mutex_lock(&mon_fdsets_lock);
Corey Bryantadb696f2012-08-14 16:43:47 -04001621 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1622 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
1623 if (mon_fdset_fd_dup->fd == dup_fd) {
1624 if (remove) {
1625 QLIST_REMOVE(mon_fdset_fd_dup, next);
Chen Quna6616142020-01-15 15:20:16 +08001626 g_free(mon_fdset_fd_dup);
Corey Bryantadb696f2012-08-14 16:43:47 -04001627 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
1628 monitor_fdset_cleanup(mon_fdset);
1629 }
Peter Xu47451462018-06-08 11:55:11 +08001630 goto err;
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02001631 } else {
Peter Xu47451462018-06-08 11:55:11 +08001632 qemu_mutex_unlock(&mon_fdsets_lock);
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02001633 return mon_fdset->id;
Corey Bryantadb696f2012-08-14 16:43:47 -04001634 }
Corey Bryantadb696f2012-08-14 16:43:47 -04001635 }
1636 }
1637 }
Peter Xu47451462018-06-08 11:55:11 +08001638
1639err:
1640 qemu_mutex_unlock(&mon_fdsets_lock);
Corey Bryantadb696f2012-08-14 16:43:47 -04001641 return -1;
1642}
1643
Yury Kotov854f63d2019-05-23 12:44:33 +03001644int64_t monitor_fdset_dup_fd_find(int dup_fd)
Corey Bryantadb696f2012-08-14 16:43:47 -04001645{
1646 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
1647}
1648
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02001649void monitor_fdset_dup_fd_remove(int dup_fd)
Corey Bryantadb696f2012-08-14 16:43:47 -04001650{
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02001651 monitor_fdset_dup_fd_find_remove(dup_fd, true);
Corey Bryantadb696f2012-08-14 16:43:47 -04001652}
1653
Markus Armbruster1677f4c2015-02-09 14:03:19 +01001654int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
Laszlo Ersek59063662014-04-10 10:24:31 +02001655{
1656 int fd;
1657 Error *local_err = NULL;
1658
1659 if (!qemu_isdigit(fdname[0]) && mon) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001660 fd = monitor_get_fd(mon, fdname, &local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00001661 } else {
1662 fd = qemu_parse_fd(fdname);
Laszlo Ersek59063662014-04-10 10:24:31 +02001663 if (fd == -1) {
1664 error_setg(&local_err, "Invalid file descriptor number '%s'",
1665 fdname);
1666 }
1667 }
1668 if (local_err) {
1669 error_propagate(errp, local_err);
1670 assert(fd == -1);
1671 } else {
1672 assert(fd != -1);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00001673 }
1674
1675 return fd;
1676}
1677
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03001678/* Please update hmp-commands.hx when adding or changing commands */
Kevin Wolfa0cd5e12019-06-13 17:33:56 +02001679static HMPCommand hmp_info_cmds[] = {
Pavel Butsykinda76ee72015-09-10 18:38:58 +03001680#include "hmp-commands-info.h"
1681 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00001682};
1683
Kevin Wolfa0cd5e12019-06-13 17:33:56 +02001684/* hmp_cmds and hmp_info_cmds would be sorted at runtime */
Kevin Wolfed7bda52019-06-13 17:34:01 +02001685HMPCommand hmp_cmds[] = {
Wenchao Xiaa13ced52013-01-14 14:06:28 +08001686#include "hmp-commands.h"
1687 { NULL, NULL, },
1688};
1689
Kevin Wolfed7bda52019-06-13 17:34:01 +02001690/*
1691 * Set @pval to the value in the register identified by @name.
1692 * return 0 if OK, -1 if not found
1693 */
1694int get_monitor_def(int64_t *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00001695{
Pavel Butsykinbf957282015-09-10 18:38:59 +03001696 const MonitorDef *md = target_monitor_defs();
Thomas Huth854e67f2017-01-13 13:12:35 +01001697 CPUState *cs = mon_get_cpu();
bellard92a31b12005-02-10 22:00:52 +00001698 void *ptr;
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11001699 uint64_t tmp = 0;
1700 int ret;
bellard92a31b12005-02-10 22:00:52 +00001701
Thomas Huth854e67f2017-01-13 13:12:35 +01001702 if (cs == NULL || md == NULL) {
Pavel Butsykinbf957282015-09-10 18:38:59 +03001703 return -1;
1704 }
1705
1706 for(; md->name != NULL; md++) {
Kevin Wolfed7bda52019-06-13 17:34:01 +02001707 if (hmp_compare_cmd(name, md->name)) {
bellard9307c4c2004-04-04 12:57:25 +00001708 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00001709 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00001710 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001711 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00001712 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00001713 switch(md->type) {
1714 case MD_I32:
1715 *pval = *(int32_t *)ptr;
1716 break;
1717 case MD_TLONG:
1718 *pval = *(target_long *)ptr;
1719 break;
1720 default:
1721 *pval = 0;
1722 break;
1723 }
bellard9307c4c2004-04-04 12:57:25 +00001724 }
1725 return 0;
1726 }
1727 }
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11001728
Thomas Huth854e67f2017-01-13 13:12:35 +01001729 ret = target_get_monitor_def(cs, name, &tmp);
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11001730 if (!ret) {
1731 *pval = (target_long) tmp;
1732 }
1733
1734 return ret;
bellard9307c4c2004-04-04 12:57:25 +00001735}
1736
Hani Benhabiles40d19392014-05-07 23:41:30 +01001737static void add_completion_option(ReadLineState *rs, const char *str,
1738 const char *option)
1739{
1740 if (!str || !option) {
1741 return;
1742 }
1743 if (!strncmp(option, str, strlen(str))) {
1744 readline_add_completion(rs, option);
1745 }
1746}
1747
Hani Benhabiles13e315d2014-05-07 23:41:29 +01001748void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
1749{
1750 size_t len;
1751 ChardevBackendInfoList *list, *start;
1752
1753 if (nb_args != 2) {
1754 return;
1755 }
1756 len = strlen(str);
1757 readline_set_completion_index(rs, len);
1758
1759 start = list = qmp_query_chardev_backends(NULL);
1760 while (list) {
1761 const char *chr_name = list->value->name;
1762
1763 if (!strncmp(chr_name, str, len)) {
1764 readline_add_completion(rs, chr_name);
1765 }
1766 list = list->next;
1767 }
1768 qapi_free_ChardevBackendInfoList(start);
1769}
1770
Hani Benhabilesb162b492014-05-07 23:41:31 +01001771void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
1772{
1773 size_t len;
1774 int i;
1775
1776 if (nb_args != 2) {
1777 return;
1778 }
1779 len = strlen(str);
1780 readline_set_completion_index(rs, len);
Markus Armbruster1c236ba2017-08-24 10:46:06 +02001781 for (i = 0; i < NET_CLIENT_DRIVER__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02001782 add_completion_option(rs, str, NetClientDriver_str(i));
Hani Benhabilesb162b492014-05-07 23:41:31 +01001783 }
1784}
1785
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01001786void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles992d3e62014-02-06 23:30:11 +01001787{
1788 GSList *list, *elt;
1789 size_t len;
1790
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01001791 if (nb_args != 2) {
1792 return;
1793 }
1794
Hani Benhabiles992d3e62014-02-06 23:30:11 +01001795 len = strlen(str);
1796 readline_set_completion_index(rs, len);
1797 list = elt = object_class_get_list(TYPE_DEVICE, false);
1798 while (elt) {
1799 const char *name;
1800 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
1801 TYPE_DEVICE);
1802 name = object_class_get_name(OBJECT_CLASS(dc));
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01001803
Eduardo Habkoste90f2a82017-05-03 17:35:44 -03001804 if (dc->user_creatable
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01001805 && !strncmp(name, str, len)) {
Hani Benhabiles992d3e62014-02-06 23:30:11 +01001806 readline_add_completion(rs, name);
1807 }
1808 elt = elt->next;
1809 }
1810 g_slist_free(list);
1811}
1812
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01001813void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles1094fd32014-02-06 23:30:13 +01001814{
1815 GSList *list, *elt;
1816 size_t len;
1817
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01001818 if (nb_args != 2) {
1819 return;
1820 }
1821
Hani Benhabiles1094fd32014-02-06 23:30:13 +01001822 len = strlen(str);
1823 readline_set_completion_index(rs, len);
1824 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
1825 while (elt) {
1826 const char *name;
1827
1828 name = object_class_get_name(OBJECT_CLASS(elt->data));
1829 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
1830 readline_add_completion(rs, name);
1831 }
1832 elt = elt->next;
1833 }
1834 g_slist_free(list);
1835}
1836
Marc-André Lureau91590152020-01-10 19:30:17 +04001837static int qdev_add_hotpluggable_device(Object *obj, void *opaque)
1838{
1839 GSList **list = opaque;
Philippe Mathieu-Daudé688ffbb2020-05-12 09:00:19 +02001840 DeviceState *dev = (DeviceState *)object_dynamic_cast(obj, TYPE_DEVICE);
Marc-André Lureau91590152020-01-10 19:30:17 +04001841
1842 if (dev == NULL) {
1843 return 0;
1844 }
1845
1846 if (dev->realized && object_property_get_bool(obj, "hotpluggable", NULL)) {
1847 *list = g_slist_append(*list, dev);
1848 }
1849
1850 return 0;
1851}
1852
1853static GSList *qdev_build_hotpluggable_device_list(Object *peripheral)
1854{
1855 GSList *list = NULL;
1856
1857 object_child_foreach(peripheral, qdev_add_hotpluggable_device, &list);
1858
1859 return list;
1860}
1861
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08001862static void peripheral_device_del_completion(ReadLineState *rs,
1863 const char *str, size_t len)
1864{
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02001865 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
1866 GSList *list, *item;
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08001867
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02001868 list = qdev_build_hotpluggable_device_list(peripheral);
1869 if (!list) {
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08001870 return;
1871 }
1872
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08001873 for (item = list; item; item = g_slist_next(item)) {
1874 DeviceState *dev = item->data;
1875
1876 if (dev->id && !strncmp(str, dev->id, len)) {
1877 readline_add_completion(rs, dev->id);
1878 }
1879 }
1880
1881 g_slist_free(list);
1882}
1883
Hani Benhabiles6297d9a2014-05-07 23:41:28 +01001884void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
1885{
1886 size_t len;
1887 ChardevInfoList *list, *start;
1888
1889 if (nb_args != 2) {
1890 return;
1891 }
1892 len = strlen(str);
1893 readline_set_completion_index(rs, len);
1894
1895 start = list = qmp_query_chardev(NULL);
1896 while (list) {
1897 ChardevInfo *chr = list->value;
1898
1899 if (!strncmp(chr->label, str, len)) {
1900 readline_add_completion(rs, chr->label);
1901 }
1902 list = list->next;
1903 }
1904 qapi_free_ChardevInfoList(start);
1905}
1906
Hani Benhabiles8e597772014-05-27 23:39:30 +01001907static void ringbuf_completion(ReadLineState *rs, const char *str)
1908{
1909 size_t len;
1910 ChardevInfoList *list, *start;
1911
1912 len = strlen(str);
1913 readline_set_completion_index(rs, len);
1914
1915 start = list = qmp_query_chardev(NULL);
1916 while (list) {
1917 ChardevInfo *chr_info = list->value;
1918
1919 if (!strncmp(chr_info->label, str, len)) {
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03001920 Chardev *chr = qemu_chr_find(chr_info->label);
Marc-André Lureau777357d2016-12-07 18:39:10 +03001921 if (chr && CHARDEV_IS_RINGBUF(chr)) {
Hani Benhabiles8e597772014-05-27 23:39:30 +01001922 readline_add_completion(rs, chr_info->label);
1923 }
1924 }
1925 list = list->next;
1926 }
1927 qapi_free_ChardevInfoList(start);
1928}
1929
Hani Benhabiles8e597772014-05-27 23:39:30 +01001930void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
1931{
1932 if (nb_args != 2) {
1933 return;
1934 }
1935 ringbuf_completion(rs, str);
1936}
1937
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01001938void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
1939{
1940 size_t len;
1941
1942 if (nb_args != 2) {
1943 return;
1944 }
1945
1946 len = strlen(str);
1947 readline_set_completion_index(rs, len);
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08001948 peripheral_device_del_completion(rs, str, len);
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01001949}
1950
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01001951void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabilesb48fa072014-02-06 23:30:12 +01001952{
1953 ObjectPropertyInfoList *list, *start;
1954 size_t len;
1955
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01001956 if (nb_args != 2) {
1957 return;
1958 }
Hani Benhabilesb48fa072014-02-06 23:30:12 +01001959 len = strlen(str);
1960 readline_set_completion_index(rs, len);
1961
1962 start = list = qmp_qom_list("/objects", NULL);
1963 while (list) {
1964 ObjectPropertyInfo *info = list->value;
1965
1966 if (!strncmp(info->type, "child<", 5)
1967 && !strncmp(info->name, str, len)) {
1968 readline_add_completion(rs, info->name);
1969 }
1970 list = list->next;
1971 }
1972 qapi_free_ObjectPropertyInfoList(start);
1973}
1974
Hani Benhabiles29136cd2014-05-07 23:41:27 +01001975void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
1976{
1977 int i;
1978 char *sep;
1979 size_t len;
1980
1981 if (nb_args != 2) {
1982 return;
1983 }
1984 sep = strrchr(str, '-');
1985 if (sep) {
1986 str = sep + 1;
1987 }
1988 len = strlen(str);
1989 readline_set_completion_index(rs, len);
Eric Blake7fb1cf12015-11-18 01:52:57 -07001990 for (i = 0; i < Q_KEY_CODE__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02001991 if (!strncmp(str, QKeyCode_str(i), len)) {
1992 readline_add_completion(rs, QKeyCode_str(i));
Hani Benhabiles29136cd2014-05-07 23:41:27 +01001993 }
1994 }
1995}
1996
Hani Benhabiles40d19392014-05-07 23:41:30 +01001997void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
1998{
1999 size_t len;
2000
2001 len = strlen(str);
2002 readline_set_completion_index(rs, len);
2003 if (nb_args == 2) {
Jason Wangeaed4832015-04-23 14:21:38 +08002004 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles40d19392014-05-07 23:41:30 +01002005 int count, i;
2006 count = qemu_find_net_clients_except(NULL, ncs,
Eric Blakef394b2e2016-07-13 21:50:23 -06002007 NET_CLIENT_DRIVER_NONE,
Jason Wangeaed4832015-04-23 14:21:38 +08002008 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08002009 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles40d19392014-05-07 23:41:30 +01002010 const char *name = ncs[i]->name;
2011 if (!strncmp(str, name, len)) {
2012 readline_add_completion(rs, name);
2013 }
2014 }
2015 } else if (nb_args == 3) {
2016 add_completion_option(rs, str, "on");
2017 add_completion_option(rs, str, "off");
2018 }
2019}
2020
Hani Benhabiles11b389f2014-05-07 23:41:32 +01002021void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
2022{
2023 int len, count, i;
Jason Wangeaed4832015-04-23 14:21:38 +08002024 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles11b389f2014-05-07 23:41:32 +01002025
2026 if (nb_args != 2) {
2027 return;
2028 }
2029
2030 len = strlen(str);
2031 readline_set_completion_index(rs, len);
Eric Blakef394b2e2016-07-13 21:50:23 -06002032 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_DRIVER_NIC,
Jason Wangeaed4832015-04-23 14:21:38 +08002033 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08002034 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles11b389f2014-05-07 23:41:32 +01002035 const char *name = ncs[i]->name;
2036 if (strncmp(str, name, len)) {
2037 continue;
2038 }
Eric Blake08712fc2020-03-17 15:17:11 -05002039 if (ncs[i]->is_netdev) {
Hani Benhabiles11b389f2014-05-07 23:41:32 +01002040 readline_add_completion(rs, name);
2041 }
2042 }
2043}
2044
Lluís Vilanovabd712112016-07-11 12:53:51 +02002045void info_trace_events_completion(ReadLineState *rs, int nb_args, const char *str)
2046{
2047 size_t len;
2048
2049 len = strlen(str);
2050 readline_set_completion_index(rs, len);
2051 if (nb_args == 2) {
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01002052 TraceEventIter iter;
2053 TraceEvent *ev;
2054 char *pattern = g_strdup_printf("%s*", str);
2055 trace_event_iter_init(&iter, pattern);
2056 while ((ev = trace_event_iter_next(&iter)) != NULL) {
2057 readline_add_completion(rs, trace_event_get_name(ev));
Lluís Vilanovabd712112016-07-11 12:53:51 +02002058 }
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01002059 g_free(pattern);
Lluís Vilanovabd712112016-07-11 12:53:51 +02002060 }
2061}
2062
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01002063void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
2064{
2065 size_t len;
2066
2067 len = strlen(str);
2068 readline_set_completion_index(rs, len);
2069 if (nb_args == 2) {
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01002070 TraceEventIter iter;
2071 TraceEvent *ev;
2072 char *pattern = g_strdup_printf("%s*", str);
2073 trace_event_iter_init(&iter, pattern);
2074 while ((ev = trace_event_iter_next(&iter)) != NULL) {
2075 readline_add_completion(rs, trace_event_get_name(ev));
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01002076 }
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01002077 g_free(pattern);
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01002078 } else if (nb_args == 3) {
2079 add_completion_option(rs, str, "on");
2080 add_completion_option(rs, str, "off");
2081 }
2082}
2083
Hani Benhabilesd0ece342014-05-27 23:39:31 +01002084void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
2085{
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01002086 int i;
2087
Hani Benhabilesd0ece342014-05-27 23:39:31 +01002088 if (nb_args != 2) {
2089 return;
2090 }
2091 readline_set_completion_index(rs, strlen(str));
Michal Privoznik14d53b42017-09-07 10:05:24 +02002092 for (i = 0; i < WATCHDOG_ACTION__MAX; i++) {
2093 add_completion_option(rs, str, WatchdogAction_str(i));
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01002094 }
Hani Benhabilesd0ece342014-05-27 23:39:31 +01002095}
2096
Hani Benhabilesc68a0402014-05-27 23:39:32 +01002097void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
2098 const char *str)
2099{
2100 size_t len;
2101
2102 len = strlen(str);
2103 readline_set_completion_index(rs, len);
2104 if (nb_args == 2) {
2105 int i;
Eric Blake7fb1cf12015-11-18 01:52:57 -07002106 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02002107 const char *name = MigrationCapability_str(i);
Hani Benhabilesc68a0402014-05-27 23:39:32 +01002108 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
Liang Li50e9a622015-03-23 16:32:29 +08002118void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
2119 const char *str)
2120{
2121 size_t len;
2122
2123 len = strlen(str);
2124 readline_set_completion_index(rs, len);
2125 if (nb_args == 2) {
2126 int i;
Eric Blake7fb1cf12015-11-18 01:52:57 -07002127 for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02002128 const char *name = MigrationParameter_str(i);
Liang Li50e9a622015-03-23 16:32:29 +08002129 if (!strncmp(str, name, len)) {
2130 readline_add_completion(rs, name);
2131 }
2132 }
2133 }
2134}
2135
Hani Benhabilesb21631f2014-05-27 23:39:37 +01002136static void vm_completion(ReadLineState *rs, const char *str)
2137{
2138 size_t len;
Kevin Wolf7c8eece2016-03-22 18:58:50 +01002139 BlockDriverState *bs;
Kevin Wolf88be7b42016-05-20 18:49:07 +02002140 BdrvNextIterator it;
Hani Benhabilesb21631f2014-05-27 23:39:37 +01002141
2142 len = strlen(str);
2143 readline_set_completion_index(rs, len);
Kevin Wolf7c8eece2016-03-22 18:58:50 +01002144
Kevin Wolf88be7b42016-05-20 18:49:07 +02002145 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Hani Benhabilesb21631f2014-05-27 23:39:37 +01002146 SnapshotInfoList *snapshots, *snapshot;
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03002147 AioContext *ctx = bdrv_get_aio_context(bs);
2148 bool ok = false;
Hani Benhabilesb21631f2014-05-27 23:39:37 +01002149
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03002150 aio_context_acquire(ctx);
2151 if (bdrv_can_snapshot(bs)) {
2152 ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0;
2153 }
2154 aio_context_release(ctx);
2155 if (!ok) {
Hani Benhabilesb21631f2014-05-27 23:39:37 +01002156 continue;
2157 }
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03002158
Hani Benhabilesb21631f2014-05-27 23:39:37 +01002159 snapshot = snapshots;
2160 while (snapshot) {
2161 char *completion = snapshot->value->name;
2162 if (!strncmp(str, completion, len)) {
2163 readline_add_completion(rs, completion);
2164 }
2165 completion = snapshot->value->id;
2166 if (!strncmp(str, completion, len)) {
2167 readline_add_completion(rs, completion);
2168 }
2169 snapshot = snapshot->next;
2170 }
2171 qapi_free_SnapshotInfoList(snapshots);
2172 }
2173
2174}
2175
2176void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
2177{
2178 if (nb_args == 2) {
2179 vm_completion(rs, str);
2180 }
2181}
2182
2183void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
2184{
2185 if (nb_args == 2) {
2186 vm_completion(rs, str);
2187 }
2188}
2189
Wayne Xia816f8922011-10-12 11:32:41 +08002190static int
2191compare_mon_cmd(const void *a, const void *b)
2192{
Kevin Wolfa0cd5e12019-06-13 17:33:56 +02002193 return strcmp(((const HMPCommand *)a)->name,
2194 ((const HMPCommand *)b)->name);
Wayne Xia816f8922011-10-12 11:32:41 +08002195}
2196
2197static void sortcmdlist(void)
2198{
Kevin Wolfa0cd5e12019-06-13 17:33:56 +02002199 qsort(hmp_cmds, ARRAY_SIZE(hmp_cmds) - 1,
2200 sizeof(*hmp_cmds),
2201 compare_mon_cmd);
2202 qsort(hmp_info_cmds, ARRAY_SIZE(hmp_info_cmds) - 1,
2203 sizeof(*hmp_info_cmds),
2204 compare_mon_cmd);
Wayne Xia816f8922011-10-12 11:32:41 +08002205}
2206
Peter Xu6adf08d2018-03-09 16:59:50 +08002207void monitor_init_globals(void)
2208{
Kevin Wolf1d95db72019-06-13 17:34:02 +02002209 monitor_init_globals_core();
Peter Xu6adf08d2018-03-09 16:59:50 +08002210 monitor_init_qmp_commands();
Peter Xu6adf08d2018-03-09 16:59:50 +08002211 sortcmdlist();
Peter Xu47451462018-06-08 11:55:11 +08002212 qemu_mutex_init(&mon_fdsets_lock);
Peter Xu6adf08d2018-03-09 16:59:50 +08002213}