blob: c918d6bd08ea047cd3d481d31b3827b71b24ecb2 [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 Wolf5f9dba12019-06-13 17:33:54 +0200123 MonitorHMP hmp = {};
Luiz Capitulino0268d972010-10-22 10:08:02 -0200124
Kevin Wolf92082412019-06-13 17:34:03 +0200125 monitor_data_init(&hmp.common, false, true, false);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200126
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200127 if (has_cpu_index) {
Kevin Wolfdcba65f2020-10-05 17:58:42 +0200128 int ret = monitor_set_cpu(&hmp.common, cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200129 if (ret < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100130 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
131 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200132 goto out;
133 }
134 }
135
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100136 handle_hmp_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200137
Dr. David Alan Gilbert0210c3b2020-09-22 10:57:41 +0100138 WITH_QEMU_LOCK_GUARD(&hmp.common.mon_lock) {
139 if (qstring_get_length(hmp.common.outbuf) > 0) {
140 output = g_strdup(qstring_get_str(hmp.common.outbuf));
141 } else {
142 output = g_strdup("");
143 }
Luiz Capitulino0268d972010-10-22 10:08:02 -0200144 }
145
146out:
Kevin Wolf5f9dba12019-06-13 17:33:54 +0200147 monitor_data_destroy(&hmp.common);
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200148 return output;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200149}
150
Kevin Wolfed7bda52019-06-13 17:34:01 +0200151/**
152 * Is @name in the '|' separated list of names @list?
153 */
154int hmp_compare_cmd(const char *name, const char *list)
bellard9dc39cb2004-03-14 21:38:27 +0000155{
156 const char *p, *pstart;
157 int len;
158 len = strlen(name);
159 p = list;
Kevin Wolfed7bda52019-06-13 17:34:01 +0200160 for (;;) {
bellard9dc39cb2004-03-14 21:38:27 +0000161 pstart = p;
Keno Fischer5c99fa32018-06-29 12:32:10 +0200162 p = qemu_strchrnul(p, '|');
Kevin Wolfed7bda52019-06-13 17:34:01 +0200163 if ((p - pstart) == len && !memcmp(pstart, name, len)) {
bellard9dc39cb2004-03-14 21:38:27 +0000164 return 1;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800165 }
166 if (*p == '\0') {
167 break;
168 }
Kevin Wolfed7bda52019-06-13 17:34:01 +0200169 p++;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800170 }
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800171 return 0;
bellard9dc39cb2004-03-14 21:38:27 +0000172}
173
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300174static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300175{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300176 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300177}
178
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100179static void hmp_trace_event(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530180{
181 const char *tp_name = qdict_get_str(qdict, "name");
182 bool new_state = qdict_get_bool(qdict, "option");
Lluís Vilanova77e2b172016-07-11 12:53:57 +0200183 bool has_vcpu = qdict_haskey(qdict, "vcpu");
184 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
Lluís Vilanova14101d02014-08-25 13:20:03 +0200185 Error *local_err = NULL;
Blue Swirlf871d682010-10-13 19:14:29 +0000186
Lluís Vilanova77e2b172016-07-11 12:53:57 +0200187 if (vcpu < 0) {
188 monitor_printf(mon, "argument vcpu must be positive");
189 return;
190 }
191
192 qmp_trace_event_set_state(tp_name, new_state, true, true, has_vcpu, vcpu, &local_err);
Lluís Vilanova14101d02014-08-25 13:20:03 +0200193 if (local_err) {
Markus Armbruster091e38b2015-02-10 15:15:43 +0100194 error_report_err(local_err);
Blue Swirlf871d682010-10-13 19:14:29 +0000195 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530196}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100197
Michael Rothc45a8162011-10-02 08:44:37 -0500198#ifdef CONFIG_TRACE_SIMPLE
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100199static void hmp_trace_file(Monitor *mon, const QDict *qdict)
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100200{
201 const char *op = qdict_get_try_str(qdict, "op");
202 const char *arg = qdict_get_try_str(qdict, "arg");
203
204 if (!op) {
Markus Armbrusterba4912c2019-04-17 21:17:50 +0200205 st_print_trace_file_status();
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100206 } else if (!strcmp(op, "on")) {
207 st_set_trace_file_enabled(true);
208 } else if (!strcmp(op, "off")) {
209 st_set_trace_file_enabled(false);
210 } else if (!strcmp(op, "flush")) {
211 st_flush_trace_buffer();
212 } else if (!strcmp(op, "set")) {
213 if (arg) {
214 st_set_trace_file(arg);
215 }
216 } else {
217 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
218 help_cmd(mon, "trace-file");
219 }
220}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530221#endif
222
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100223static void hmp_info_help(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000224{
Luiz Capitulino13c74252009-10-07 13:41:55 -0300225 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000226}
227
Peter Xu6adf08d2018-03-09 16:59:50 +0800228static void monitor_init_qmp_commands(void)
Marc-André Lureauedcfaef2016-09-12 13:19:00 +0400229{
Markus Armbruster635db182017-03-03 13:32:27 +0100230 /*
231 * Two command lists:
232 * - qmp_commands contains all QMP commands
233 * - qmp_cap_negotiation_commands contains just
234 * "qmp_capabilities", to enforce capability negotiation
235 */
236
Markus Armbruster1527bad2017-03-03 13:32:25 +0100237 qmp_init_marshal(&qmp_commands);
Markus Armbruster05875682017-03-03 13:32:24 +0100238
Markus Armbruster1527bad2017-03-03 13:32:25 +0100239 qmp_register_command(&qmp_commands, "query-qmp-schema",
Igor Mammedovd6fe3d02018-05-11 18:51:43 +0200240 qmp_query_qmp_schema, QCO_ALLOW_PRECONFIG);
Markus Armbruster1527bad2017-03-03 13:32:25 +0100241 qmp_register_command(&qmp_commands, "device_add", qmp_device_add,
Marc-André Lureauedcfaef2016-09-12 13:19:00 +0400242 QCO_NO_OPTIONS);
Kevin Wolf5f07c4d2020-02-24 15:29:55 +0100243 qmp_register_command(&qmp_commands, "object-add", qmp_object_add,
244 QCO_NO_OPTIONS);
Marc-André Lureau5032a162016-09-12 13:19:02 +0400245
Markus Armbruster635db182017-03-03 13:32:27 +0100246 QTAILQ_INIT(&qmp_cap_negotiation_commands);
247 qmp_register_command(&qmp_cap_negotiation_commands, "qmp_capabilities",
Igor Mammedovd6fe3d02018-05-11 18:51:43 +0200248 qmp_marshal_qmp_capabilities, QCO_ALLOW_PRECONFIG);
Markus Armbruster635db182017-03-03 13:32:27 +0100249}
250
Peter Xud9f25282018-06-08 11:55:07 +0800251/* Set the current CPU defined by the user. Callers must hold BQL. */
Kevin Wolfdcba65f2020-10-05 17:58:42 +0200252int monitor_set_cpu(Monitor *mon, int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000253{
Andreas Färber55e5c282012-12-17 06:18:02 +0100254 CPUState *cpu;
bellard6a00d602005-11-21 23:25:50 +0000255
Andreas Färber1c8bb3c2013-02-15 17:01:09 +0100256 cpu = qemu_get_cpu(cpu_index);
257 if (cpu == NULL) {
258 return -1;
bellard6a00d602005-11-21 23:25:50 +0000259 }
Kevin Wolfdcba65f2020-10-05 17:58:42 +0200260 g_free(mon->mon_cpu_path);
261 mon->mon_cpu_path = object_get_canonical_path(OBJECT(cpu));
Andreas Färber1c8bb3c2013-02-15 17:01:09 +0100262 return 0;
bellard6a00d602005-11-21 23:25:50 +0000263}
264
Peter Xud9f25282018-06-08 11:55:07 +0800265/* Callers must hold BQL. */
Kevin Wolf87e6f4a2020-10-05 17:58:43 +0200266static CPUState *mon_get_cpu_sync(Monitor *mon, bool synchronize)
bellard6a00d602005-11-21 23:25:50 +0000267{
Marc-André Lureaua85d0bf2019-10-04 15:18:20 +0400268 CPUState *cpu = NULL;
Greg Kurz751f8cf2017-10-17 10:16:22 +0200269
Kevin Wolf87e6f4a2020-10-05 17:58:43 +0200270 if (mon->mon_cpu_path) {
271 cpu = (CPUState *) object_resolve_path_type(mon->mon_cpu_path,
Greg Kurz751f8cf2017-10-17 10:16:22 +0200272 TYPE_CPU, NULL);
273 if (!cpu) {
Kevin Wolf87e6f4a2020-10-05 17:58:43 +0200274 g_free(mon->mon_cpu_path);
275 mon->mon_cpu_path = NULL;
Greg Kurz751f8cf2017-10-17 10:16:22 +0200276 }
277 }
Kevin Wolf87e6f4a2020-10-05 17:58:43 +0200278 if (!mon->mon_cpu_path) {
Thomas Huth854e67f2017-01-13 13:12:35 +0100279 if (!first_cpu) {
280 return NULL;
281 }
Kevin Wolf87e6f4a2020-10-05 17:58:43 +0200282 monitor_set_cpu(mon, first_cpu->cpu_index);
Greg Kurz751f8cf2017-10-17 10:16:22 +0200283 cpu = first_cpu;
bellard6a00d602005-11-21 23:25:50 +0000284 }
Marc-André Lureaua85d0bf2019-10-04 15:18:20 +0400285 assert(cpu != NULL);
Viktor Mihajlovski137b5cb2018-02-16 17:08:41 +0100286 if (synchronize) {
287 cpu_synchronize_state(cpu);
288 }
Greg Kurz751f8cf2017-10-17 10:16:22 +0200289 return cpu;
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700290}
291
Kevin Wolf2fc5d012020-11-13 12:43:24 +0100292CPUState *mon_get_cpu(Monitor *mon)
Viktor Mihajlovski137b5cb2018-02-16 17:08:41 +0100293{
Kevin Wolf2fc5d012020-11-13 12:43:24 +0100294 return mon_get_cpu_sync(mon, true);
Viktor Mihajlovski137b5cb2018-02-16 17:08:41 +0100295}
296
Pavel Butsykinbf957282015-09-10 18:38:59 +0300297CPUArchState *mon_get_cpu_env(void)
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700298{
Kevin Wolf2fc5d012020-11-13 12:43:24 +0100299 CPUState *cs = mon_get_cpu(monitor_cur());
Thomas Huth854e67f2017-01-13 13:12:35 +0100300
301 return cs ? cs->env_ptr : NULL;
bellard6a00d602005-11-21 23:25:50 +0000302}
303
Kevin Wolf87e6f4a2020-10-05 17:58:43 +0200304int monitor_get_cpu_index(Monitor *mon)
Luiz Capitulino99b77962011-10-24 10:53:44 -0200305{
Kevin Wolf87e6f4a2020-10-05 17:58:43 +0200306 CPUState *cs = mon_get_cpu_sync(mon, false);
Thomas Huth854e67f2017-01-13 13:12:35 +0100307
308 return cs ? cs->cpu_index : UNASSIGNED_CPU_INDEX;
Luiz Capitulino99b77962011-10-24 10:53:44 -0200309}
310
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100311static void hmp_info_registers(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000312{
Suraj Jitindar Singh18f08282017-06-08 15:41:16 +1000313 bool all_cpus = qdict_get_try_bool(qdict, "cpustate_all", false);
314 CPUState *cs;
Thomas Huth854e67f2017-01-13 13:12:35 +0100315
Suraj Jitindar Singh18f08282017-06-08 15:41:16 +1000316 if (all_cpus) {
317 CPU_FOREACH(cs) {
318 monitor_printf(mon, "\nCPU#%d\n", cs->cpu_index);
Markus Armbruster90c84c52019-04-17 21:18:02 +0200319 cpu_dump_state(cs, NULL, CPU_DUMP_FPU);
Suraj Jitindar Singh18f08282017-06-08 15:41:16 +1000320 }
321 } else {
Kevin Wolf2fc5d012020-11-13 12:43:24 +0100322 cs = mon_get_cpu(mon);
Suraj Jitindar Singh18f08282017-06-08 15:41:16 +1000323
324 if (!cs) {
325 monitor_printf(mon, "No CPU available\n");
326 return;
327 }
328
Markus Armbruster90c84c52019-04-17 21:18:02 +0200329 cpu_dump_state(cs, NULL, CPU_DUMP_FPU);
Thomas Huth854e67f2017-01-13 13:12:35 +0100330 }
bellard9307c4c2004-04-04 12:57:25 +0000331}
332
Paolo Bonzinif0d14a92012-09-17 13:42:41 +0200333#ifdef CONFIG_TCG
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100334static void hmp_info_jit(Monitor *mon, const QDict *qdict)
bellarde3db7222005-01-26 22:00:47 +0000335{
Thomas Huthb7da97e2017-04-26 06:11:47 +0200336 if (!tcg_enabled()) {
337 error_report("JIT information is only available with accel=tcg");
338 return;
339 }
340
Markus Armbruster3de2faa2019-04-17 21:17:52 +0200341 dump_exec_info();
Markus Armbruster76c86612019-04-17 21:17:53 +0200342 dump_drift_info();
bellarde3db7222005-01-26 22:00:47 +0000343}
344
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100345static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
Max Filippov246ae242014-11-02 11:04:18 +0300346{
Markus Armbrusterd4c51a02019-04-17 21:17:51 +0200347 dump_opcount_info();
Max Filippov246ae242014-11-02 11:04:18 +0300348}
Paolo Bonzinif0d14a92012-09-17 13:42:41 +0200349#endif
Max Filippov246ae242014-11-02 11:04:18 +0300350
Emilio G. Cota97bfafe2017-08-08 13:54:42 -0400351static void hmp_info_sync_profile(Monitor *mon, const QDict *qdict)
352{
353 int64_t max = qdict_get_try_int(qdict, "max", 10);
354 bool mean = qdict_get_try_bool(qdict, "mean", false);
355 bool coalesce = !qdict_get_try_bool(qdict, "no_coalesce", false);
356 enum QSPSortBy sort_by;
357
358 sort_by = mean ? QSP_SORT_BY_AVG_WAIT_TIME : QSP_SORT_BY_TOTAL_WAIT_TIME;
Markus Armbrusterac7ff4c2019-04-17 21:17:54 +0200359 qsp_report(max, sort_by, coalesce);
Emilio G. Cota97bfafe2017-08-08 13:54:42 -0400360}
361
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100362static void hmp_info_history(Monitor *mon, const QDict *qdict)
bellardaa455482004-04-04 13:07:25 +0000363{
Kevin Wolf5f9dba12019-06-13 17:33:54 +0200364 MonitorHMP *hmp_mon = container_of(mon, MonitorHMP, common);
bellardaa455482004-04-04 13:07:25 +0000365 int i;
bellard7e2515e2004-08-01 21:52:19 +0000366 const char *str;
ths3b46e622007-09-17 08:09:54 +0000367
Kevin Wolf5f9dba12019-06-13 17:33:54 +0200368 if (!hmp_mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000369 return;
Kevin Wolf5f9dba12019-06-13 17:33:54 +0200370 }
bellard7e2515e2004-08-01 21:52:19 +0000371 i = 0;
372 for(;;) {
Kevin Wolf5f9dba12019-06-13 17:33:54 +0200373 str = readline_get_history(hmp_mon->rs, i);
374 if (!str) {
bellard7e2515e2004-08-01 21:52:19 +0000375 break;
Kevin Wolf5f9dba12019-06-13 17:33:54 +0200376 }
aliguori376253e2009-03-05 23:01:23 +0000377 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000378 i++;
bellardaa455482004-04-04 13:07:25 +0000379 }
380}
381
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100382static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
j_mayer76a66252007-03-07 08:32:30 +0000383{
Kevin Wolf2fc5d012020-11-13 12:43:24 +0100384 CPUState *cs = mon_get_cpu(mon);
Thomas Huth854e67f2017-01-13 13:12:35 +0100385
386 if (!cs) {
387 monitor_printf(mon, "No CPU available\n");
388 return;
389 }
Markus Armbruster11cb6c12019-04-17 21:18:00 +0200390 cpu_dump_statistics(cs, 0);
j_mayer76a66252007-03-07 08:32:30 +0000391}
j_mayer76a66252007-03-07 08:32:30 +0000392
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100393static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530394{
Lluís Vilanovabd712112016-07-11 12:53:51 +0200395 const char *name = qdict_get_try_str(qdict, "name");
Lluís Vilanova77e2b172016-07-11 12:53:57 +0200396 bool has_vcpu = qdict_haskey(qdict, "vcpu");
397 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
Lluís Vilanovabd712112016-07-11 12:53:51 +0200398 TraceEventInfoList *events;
Lluís Vilanova14101d02014-08-25 13:20:03 +0200399 TraceEventInfoList *elem;
Lluís Vilanovabd712112016-07-11 12:53:51 +0200400 Error *local_err = NULL;
401
402 if (name == NULL) {
403 name = "*";
404 }
Lluís Vilanova77e2b172016-07-11 12:53:57 +0200405 if (vcpu < 0) {
406 monitor_printf(mon, "argument vcpu must be positive");
407 return;
408 }
Lluís Vilanovabd712112016-07-11 12:53:51 +0200409
Lluís Vilanova77e2b172016-07-11 12:53:57 +0200410 events = qmp_trace_event_get_state(name, has_vcpu, vcpu, &local_err);
Lluís Vilanovabd712112016-07-11 12:53:51 +0200411 if (local_err) {
412 error_report_err(local_err);
413 return;
414 }
Lluís Vilanova14101d02014-08-25 13:20:03 +0200415
416 for (elem = events; elem != NULL; elem = elem->next) {
417 monitor_printf(mon, "%s : state %u\n",
418 elem->value->name,
419 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
420 }
421 qapi_free_TraceEventInfoList(events);
Prerna Saxena22890ab2010-06-24 17:04:53 +0530422}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530423
Markus Armbrusterb8a185b2015-03-05 17:29:02 +0100424void qmp_client_migrate_info(const char *protocol, const char *hostname,
425 bool has_port, int64_t port,
426 bool has_tls_port, int64_t tls_port,
427 bool has_cert_subject, const char *cert_subject,
428 Error **errp)
Gerd Hoffmanne866e232010-04-23 13:28:21 +0200429{
Gerd Hoffmanne866e232010-04-23 13:28:21 +0200430 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb8a185b2015-03-05 17:29:02 +0100431 if (!qemu_using_spice(errp)) {
432 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +0200433 }
434
Markus Armbrusterb8a185b2015-03-05 17:29:02 +0100435 if (!has_port && !has_tls_port) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100436 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
Markus Armbrusterb8a185b2015-03-05 17:29:02 +0100437 return;
Yonit Halperin6ec5dae2012-03-18 09:42:39 +0200438 }
439
Gerd Hoffmann74774772020-10-19 09:52:12 +0200440 if (qemu_spice.migrate_info(hostname,
Markus Armbrusterb8a185b2015-03-05 17:29:02 +0100441 has_port ? port : -1,
442 has_tls_port ? tls_port : -1,
443 cert_subject)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100444 error_setg(errp, QERR_UNDEFINED_ERROR);
Markus Armbrusterb8a185b2015-03-05 17:29:02 +0100445 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +0200446 }
Markus Armbrusterb8a185b2015-03-05 17:29:02 +0100447 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +0200448 }
449
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100450 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
Gerd Hoffmanne866e232010-04-23 13:28:21 +0200451}
452
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100453static void hmp_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +0000454{
Markus Armbrusterdaa76aa2016-06-15 19:27:16 +0200455 Error *err = NULL;
456
457 qemu_set_log_filename(qdict_get_str(qdict, "filename"), &err);
458 if (err) {
459 error_report_err(err);
460 }
pbrooke735b912007-06-30 13:53:24 +0000461}
462
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100463static void hmp_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +0000464{
465 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300466 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +0000467
bellard9307c4c2004-04-04 12:57:25 +0000468 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +0000469 mask = 0;
470 } else {
Peter Maydell4fde1eb2013-02-11 16:41:22 +0000471 mask = qemu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +0000472 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +0000473 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +0000474 return;
475 }
476 }
Peter Maydell24537a02013-02-11 16:41:23 +0000477 qemu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +0000478}
479
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100480static void hmp_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +0000481{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300482 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +0000483 if (!option || !strcmp(option, "on")) {
484 singlestep = 1;
485 } else if (!strcmp(option, "off")) {
486 singlestep = 0;
487 } else {
488 monitor_printf(mon, "unexpected option %s\n", option);
489 }
490}
491
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100492static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +0000493{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300494 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +0000495 if (!device)
496 device = "tcp::" DEFAULT_GDBSTUB_PORT;
497 if (gdbserver_start(device) < 0) {
498 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
499 device);
500 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +0000501 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +0000502 } else {
aliguori59030a82009-04-05 18:43:41 +0000503 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
504 device);
bellard8a7ddc32004-03-31 19:00:16 +0000505 }
506}
507
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100508static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100509{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300510 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100511 if (select_watchdog_action(action) == -1) {
512 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
513 }
514}
515
aliguori376253e2009-03-05 23:01:23 +0000516static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +0000517{
aliguori376253e2009-03-05 23:01:23 +0000518 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000519 switch(c) {
520 case '\'':
aliguori376253e2009-03-05 23:01:23 +0000521 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +0000522 break;
523 case '\\':
aliguori376253e2009-03-05 23:01:23 +0000524 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +0000525 break;
526 case '\n':
aliguori376253e2009-03-05 23:01:23 +0000527 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +0000528 break;
529 case '\r':
aliguori376253e2009-03-05 23:01:23 +0000530 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +0000531 break;
532 default:
533 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +0000534 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +0000535 } else {
aliguori376253e2009-03-05 23:01:23 +0000536 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +0000537 }
538 break;
539 }
aliguori376253e2009-03-05 23:01:23 +0000540 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000541}
542
aliguori376253e2009-03-05 23:01:23 +0000543static void memory_dump(Monitor *mon, int count, int format, int wsize,
Avi Kivitya8170e52012-10-23 12:30:10 +0200544 hwaddr addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +0000545{
Blue Swirl23842aa2010-01-12 20:27:43 +0000546 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +0000547 uint8_t buf[16];
548 uint64_t v;
Kevin Wolf2fc5d012020-11-13 12:43:24 +0100549 CPUState *cs = mon_get_cpu(mon);
Thomas Huth854e67f2017-01-13 13:12:35 +0100550
551 if (!cs && (format == 'i' || !is_physical)) {
552 monitor_printf(mon, "Can not dump without CPU\n");
553 return;
554 }
bellard9307c4c2004-04-04 12:57:25 +0000555
556 if (format == 'i') {
Richard Henderson1d484742017-09-14 08:38:35 -0700557 monitor_disas(mon, cs, addr, count, is_physical);
bellard9307c4c2004-04-04 12:57:25 +0000558 return;
559 }
560
561 len = wsize * count;
562 if (wsize == 1)
563 line_size = 8;
564 else
565 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +0000566 max_digits = 0;
567
568 switch(format) {
569 case 'o':
Marc-André Lureau69db8df2017-06-22 13:04:16 +0200570 max_digits = DIV_ROUND_UP(wsize * 8, 3);
bellard9307c4c2004-04-04 12:57:25 +0000571 break;
572 default:
573 case 'x':
574 max_digits = (wsize * 8) / 4;
575 break;
576 case 'u':
577 case 'd':
Marc-André Lureau69db8df2017-06-22 13:04:16 +0200578 max_digits = DIV_ROUND_UP(wsize * 8 * 10, 33);
bellard9307c4c2004-04-04 12:57:25 +0000579 break;
580 case 'c':
581 wsize = 1;
582 break;
583 }
584
585 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +0000586 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +0000587 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +0000588 else
aliguori376253e2009-03-05 23:01:23 +0000589 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +0000590 l = len;
591 if (l > line_size)
592 l = line_size;
593 if (is_physical) {
Peter Maydell6f89ae52018-12-14 13:30:49 +0000594 AddressSpace *as = cs ? cs->as : &address_space_memory;
595 MemTxResult r = address_space_read(as, addr,
596 MEMTXATTRS_UNSPECIFIED, buf, l);
597 if (r != MEMTX_OK) {
598 monitor_printf(mon, " Cannot access memory\n");
599 break;
600 }
bellard9307c4c2004-04-04 12:57:25 +0000601 } else {
Thomas Huth854e67f2017-01-13 13:12:35 +0100602 if (cpu_memory_rw_debug(cs, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +0000603 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +0000604 break;
605 }
bellard9307c4c2004-04-04 12:57:25 +0000606 }
ths5fafdf22007-09-16 21:08:06 +0000607 i = 0;
bellard9307c4c2004-04-04 12:57:25 +0000608 while (i < l) {
609 switch(wsize) {
610 default:
611 case 1:
Peter Maydell24e60302015-01-20 15:19:32 +0000612 v = ldub_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000613 break;
614 case 2:
Peter Maydell24e60302015-01-20 15:19:32 +0000615 v = lduw_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000616 break;
617 case 4:
Peter Maydell24e60302015-01-20 15:19:32 +0000618 v = (uint32_t)ldl_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000619 break;
620 case 8:
Peter Maydell24e60302015-01-20 15:19:32 +0000621 v = ldq_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000622 break;
623 }
aliguori376253e2009-03-05 23:01:23 +0000624 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +0000625 switch(format) {
626 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000627 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000628 break;
629 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000630 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000631 break;
632 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000633 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000634 break;
635 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000636 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000637 break;
638 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000639 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +0000640 break;
641 }
642 i += wsize;
643 }
aliguori376253e2009-03-05 23:01:23 +0000644 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000645 addr += l;
646 len -= l;
647 }
648}
649
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100650static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000651{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300652 int count = qdict_get_int(qdict, "count");
653 int format = qdict_get_int(qdict, "format");
654 int size = qdict_get_int(qdict, "size");
655 target_long addr = qdict_get_int(qdict, "addr");
656
aliguori376253e2009-03-05 23:01:23 +0000657 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +0000658}
659
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100660static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000661{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300662 int count = qdict_get_int(qdict, "count");
663 int format = qdict_get_int(qdict, "format");
664 int size = qdict_get_int(qdict, "size");
Avi Kivitya8170e52012-10-23 12:30:10 +0200665 hwaddr addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300666
aliguori376253e2009-03-05 23:01:23 +0000667 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +0000668}
669
Paolo Bonzinie9628442017-04-20 15:30:58 +0200670static void *gpa2hva(MemoryRegion **p_mr, hwaddr addr, Error **errp)
671{
672 MemoryRegionSection mrs = memory_region_find(get_system_memory(),
673 addr, 1);
674
675 if (!mrs.mr) {
676 error_setg(errp, "No memory is mapped at address 0x%" HWADDR_PRIx, addr);
677 return NULL;
678 }
679
680 if (!memory_region_is_ram(mrs.mr) && !memory_region_is_romd(mrs.mr)) {
681 error_setg(errp, "Memory at address 0x%" HWADDR_PRIx "is not RAM", addr);
682 memory_region_unref(mrs.mr);
683 return NULL;
684 }
685
686 *p_mr = mrs.mr;
687 return qemu_map_ram_ptr(mrs.mr->ram_block, mrs.offset_within_region);
688}
689
690static void hmp_gpa2hva(Monitor *mon, const QDict *qdict)
691{
692 hwaddr addr = qdict_get_int(qdict, "addr");
693 Error *local_err = NULL;
694 MemoryRegion *mr = NULL;
695 void *ptr;
696
697 ptr = gpa2hva(&mr, addr, &local_err);
698 if (local_err) {
699 error_report_err(local_err);
700 return;
701 }
702
703 monitor_printf(mon, "Host virtual address for 0x%" HWADDR_PRIx
704 " (%s) is %p\n",
705 addr, mr->name, ptr);
706
707 memory_region_unref(mr);
708}
709
Dr. David Alan Gilbert574d9692019-04-12 16:26:52 +0100710static void hmp_gva2gpa(Monitor *mon, const QDict *qdict)
711{
712 target_ulong addr = qdict_get_int(qdict, "addr");
713 MemTxAttrs attrs;
Kevin Wolf2fc5d012020-11-13 12:43:24 +0100714 CPUState *cs = mon_get_cpu(mon);
Dr. David Alan Gilbert574d9692019-04-12 16:26:52 +0100715 hwaddr gpa;
716
717 if (!cs) {
718 monitor_printf(mon, "No cpu\n");
719 return;
720 }
721
Eduardo Habkost9d3250d2019-05-10 15:56:20 -0300722 gpa = cpu_get_phys_page_attrs_debug(cs, addr & TARGET_PAGE_MASK, &attrs);
Dr. David Alan Gilbert574d9692019-04-12 16:26:52 +0100723 if (gpa == -1) {
724 monitor_printf(mon, "Unmapped\n");
725 } else {
726 monitor_printf(mon, "gpa: %#" HWADDR_PRIx "\n",
727 gpa + (addr & ~TARGET_PAGE_MASK));
728 }
729}
730
Paolo Bonzinie9628442017-04-20 15:30:58 +0200731#ifdef CONFIG_LINUX
732static uint64_t vtop(void *ptr, Error **errp)
733{
734 uint64_t pinfo;
735 uint64_t ret = -1;
736 uintptr_t addr = (uintptr_t) ptr;
Wei Yang038adc22019-10-13 10:11:45 +0800737 uintptr_t pagesize = qemu_real_host_page_size;
Paolo Bonzinie9628442017-04-20 15:30:58 +0200738 off_t offset = addr / pagesize * sizeof(pinfo);
739 int fd;
740
741 fd = open("/proc/self/pagemap", O_RDONLY);
742 if (fd == -1) {
743 error_setg_errno(errp, errno, "Cannot open /proc/self/pagemap");
744 return -1;
745 }
746
747 /* Force copy-on-write if necessary. */
Stefan Hajnoczid73415a2020-09-23 11:56:46 +0100748 qatomic_add((uint8_t *)ptr, 0);
Paolo Bonzinie9628442017-04-20 15:30:58 +0200749
750 if (pread(fd, &pinfo, sizeof(pinfo), offset) != sizeof(pinfo)) {
751 error_setg_errno(errp, errno, "Cannot read pagemap");
752 goto out;
753 }
754 if ((pinfo & (1ull << 63)) == 0) {
755 error_setg(errp, "Page not present");
756 goto out;
757 }
758 ret = ((pinfo & 0x007fffffffffffffull) * pagesize) | (addr & (pagesize - 1));
759
760out:
761 close(fd);
762 return ret;
763}
764
765static void hmp_gpa2hpa(Monitor *mon, const QDict *qdict)
766{
767 hwaddr addr = qdict_get_int(qdict, "addr");
768 Error *local_err = NULL;
769 MemoryRegion *mr = NULL;
770 void *ptr;
771 uint64_t physaddr;
772
773 ptr = gpa2hva(&mr, addr, &local_err);
774 if (local_err) {
775 error_report_err(local_err);
776 return;
777 }
778
779 physaddr = vtop(ptr, &local_err);
780 if (local_err) {
781 error_report_err(local_err);
782 } else {
783 monitor_printf(mon, "Host physical address for 0x%" HWADDR_PRIx
784 " (%s) is 0x%" PRIx64 "\n",
785 addr, mr->name, (uint64_t) physaddr);
786 }
787
788 memory_region_unref(mr);
789}
790#endif
791
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300792static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000793{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300794 int format = qdict_get_int(qdict, "format");
Avi Kivitya8170e52012-10-23 12:30:10 +0200795 hwaddr val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300796
bellard9307c4c2004-04-04 12:57:25 +0000797 switch(format) {
798 case 'o':
Avi Kivitya8170e52012-10-23 12:30:10 +0200799 monitor_printf(mon, "%#" HWADDR_PRIo, val);
bellard9307c4c2004-04-04 12:57:25 +0000800 break;
801 case 'x':
Avi Kivitya8170e52012-10-23 12:30:10 +0200802 monitor_printf(mon, "%#" HWADDR_PRIx, val);
bellard9307c4c2004-04-04 12:57:25 +0000803 break;
804 case 'u':
Avi Kivitya8170e52012-10-23 12:30:10 +0200805 monitor_printf(mon, "%" HWADDR_PRIu, val);
bellard9307c4c2004-04-04 12:57:25 +0000806 break;
807 default:
808 case 'd':
Avi Kivitya8170e52012-10-23 12:30:10 +0200809 monitor_printf(mon, "%" HWADDR_PRId, val);
bellard9307c4c2004-04-04 12:57:25 +0000810 break;
811 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000812 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +0000813 break;
814 }
aliguori376253e2009-03-05 23:01:23 +0000815 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000816}
817
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100818static void hmp_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +0000819{
820 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +0000821 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300822 uint32_t start = qdict_get_int(qdict, "start");
823 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +0000824
825 sum = 0;
826 for(addr = start; addr < (start + size); addr++) {
Peter Maydell42874d32015-04-26 16:49:24 +0100827 uint8_t val = address_space_ldub(&address_space_memory, addr,
828 MEMTXATTRS_UNSPECIFIED, NULL);
bellarde4cf1ad2005-06-04 20:15:57 +0000829 /* BSD sum algorithm ('sum' Unix command) */
830 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +0200831 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +0000832 }
aliguori376253e2009-03-05 23:01:23 +0000833 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +0000834}
835
bellard13224a82006-07-14 22:03:35 +0000836static int mouse_button_state;
837
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100838static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +0000839{
Gerd Hoffmannc751a742013-12-04 15:02:28 +0100840 int dx, dy, dz, button;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300841 const char *dx_str = qdict_get_str(qdict, "dx_str");
842 const char *dy_str = qdict_get_str(qdict, "dy_str");
843 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
Gerd Hoffmannc751a742013-12-04 15:02:28 +0100844
bellard13224a82006-07-14 22:03:35 +0000845 dx = strtol(dx_str, NULL, 0);
846 dy = strtol(dy_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +0100847 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
848 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
849
850 if (dz_str) {
bellard13224a82006-07-14 22:03:35 +0000851 dz = strtol(dz_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +0100852 if (dz != 0) {
Gerd Hoffmannf22d0af2016-01-12 12:14:12 +0100853 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
Gerd Hoffmannc751a742013-12-04 15:02:28 +0100854 qemu_input_queue_btn(NULL, button, true);
855 qemu_input_event_sync();
856 qemu_input_queue_btn(NULL, button, false);
857 }
858 }
859 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +0000860}
861
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100862static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +0000863{
Eric Blake7fb1cf12015-11-18 01:52:57 -0700864 static uint32_t bmap[INPUT_BUTTON__MAX] = {
Gerd Hoffmannc751a742013-12-04 15:02:28 +0100865 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
866 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
867 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
868 };
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300869 int button_state = qdict_get_int(qdict, "button_state");
Gerd Hoffmannc751a742013-12-04 15:02:28 +0100870
871 if (mouse_button_state == button_state) {
872 return;
873 }
874 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
875 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +0000876 mouse_button_state = button_state;
bellard13224a82006-07-14 22:03:35 +0000877}
878
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100879static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +0000880{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -0300881 int size = qdict_get_int(qdict, "size");
882 int addr = qdict_get_int(qdict, "addr");
883 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +0000884 uint32_t val;
885 int suffix;
886
887 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -0300888 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +0000889 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +0000890 addr++;
891 }
892 addr &= 0xffff;
893
894 switch(size) {
895 default:
896 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +0000897 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +0000898 suffix = 'b';
899 break;
900 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +0000901 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +0000902 suffix = 'w';
903 break;
904 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +0000905 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +0000906 suffix = 'l';
907 break;
908 }
aliguori376253e2009-03-05 23:01:23 +0000909 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
910 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +0000911}
bellarda3a91a32004-06-04 11:06:21 +0000912
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100913static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +0200914{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300915 int size = qdict_get_int(qdict, "size");
916 int addr = qdict_get_int(qdict, "addr");
917 int val = qdict_get_int(qdict, "val");
918
Jan Kiszkaf1147842009-07-14 10:20:11 +0200919 addr &= IOPORTS_MASK;
920
921 switch (size) {
922 default:
923 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +0000924 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +0200925 break;
926 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +0000927 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +0200928 break;
929 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +0000930 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +0200931 break;
932 }
933}
934
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100935static void hmp_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +0000936{
Gongleif1839932014-12-03 18:20:58 +0000937 Error *local_err = NULL;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300938 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +0000939
Gongleif1839932014-12-03 18:20:58 +0000940 qemu_boot_set(bootdevice, &local_err);
941 if (local_err) {
Markus Armbruster193227f2015-12-18 16:35:06 +0100942 error_report_err(local_err);
aurel320ecdffb2008-05-04 20:11:34 +0000943 } else {
Gongleif1839932014-12-03 18:20:58 +0000944 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +0000945 }
946}
947
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100948static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
Blue Swirl314e2982011-09-11 20:22:05 +0000949{
Peter Xu57bb40c2017-01-16 16:40:05 +0800950 bool flatview = qdict_get_try_bool(qdict, "flatview", false);
Alexey Kardashevskiy5e8fd942017-09-21 18:51:06 +1000951 bool dispatch_tree = qdict_get_try_bool(qdict, "dispatch_tree", false);
Alexey Kardashevskiyfc051ae2018-06-04 13:25:11 +1000952 bool owner = qdict_get_try_bool(qdict, "owner", false);
Philippe Mathieu-Daudé2261d392020-05-29 14:53:25 +0200953 bool disabled = qdict_get_try_bool(qdict, "disabled", false);
Peter Xu57bb40c2017-01-16 16:40:05 +0800954
Philippe Mathieu-Daudé2261d392020-05-29 14:53:25 +0200955 mtree_info(flatview, dispatch_tree, owner, disabled);
Blue Swirl314e2982011-09-11 20:22:05 +0000956}
957
bellard5f1ce942006-02-08 22:40:15 +0000958#ifdef CONFIG_PROFILER
959
Aurelien Jarnoe9a66252009-09-30 14:09:52 +0200960int64_t dev_time;
961
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100962static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +0000963{
Emilio G. Cota72fd2ef2018-10-10 10:48:53 -0400964 static int64_t last_cpu_exec_time;
965 int64_t cpu_exec_time;
966 int64_t delta;
967
968 cpu_exec_time = tcg_cpu_exec_time();
969 delta = cpu_exec_time - last_cpu_exec_time;
970
aliguori376253e2009-03-05 23:01:23 +0000971 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Rutuja Shah73bcb242016-03-21 21:32:30 +0530972 dev_time, dev_time / (double)NANOSECONDS_PER_SECOND);
aliguori376253e2009-03-05 23:01:23 +0000973 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Emilio G. Cota72fd2ef2018-10-10 10:48:53 -0400974 delta, delta / (double)NANOSECONDS_PER_SECOND);
975 last_cpu_exec_time = cpu_exec_time;
bellard5f1ce942006-02-08 22:40:15 +0000976 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +0000977}
978#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100979static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +0000980{
aliguori376253e2009-03-05 23:01:23 +0000981 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +0000982}
983#endif
984
bellardec36b692006-07-16 18:57:03 +0000985/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +0000986static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +0000987
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100988static void hmp_info_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +0000989{
990 int i;
991 CaptureState *s;
992
993 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +0000994 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +0000995 s->ops.info (s->opaque);
996 }
997}
998
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100999static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001000{
1001 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001002 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001003 CaptureState *s;
1004
1005 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1006 if (i == n) {
1007 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001008 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001009 g_free (s);
bellardec36b692006-07-16 18:57:03 +00001010 return;
1011 }
1012 }
1013}
1014
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001015static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001016{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001017 const char *path = qdict_get_str(qdict, "path");
Kővágó, Zoltánf0b9f362019-08-19 01:06:48 +02001018 int freq = qdict_get_try_int(qdict, "freq", 44100);
1019 int bits = qdict_get_try_int(qdict, "bits", 16);
1020 int nchannels = qdict_get_try_int(qdict, "nchannels", 2);
1021 const char *audiodev = qdict_get_str(qdict, "audiodev");
bellardec36b692006-07-16 18:57:03 +00001022 CaptureState *s;
Kővágó, Zoltánf0b9f362019-08-19 01:06:48 +02001023 AudioState *as = audio_state_by_name(audiodev);
1024
1025 if (!as) {
1026 monitor_printf(mon, "Audiodev '%s' not found\n", audiodev);
1027 return;
1028 }
bellardec36b692006-07-16 18:57:03 +00001029
Anthony Liguori7267c092011-08-20 22:09:37 -05001030 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001031
Kővágó, Zoltánf0b9f362019-08-19 01:06:48 +02001032 if (wav_start_capture(as, s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001033 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05001034 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001035 return;
bellardec36b692006-07-16 18:57:03 +00001036 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001037 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001038}
bellardec36b692006-07-16 18:57:03 +00001039
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00001040static QAuthZList *find_auth(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001041{
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00001042 Object *obj;
1043 Object *container;
aliguori76655d62009-03-06 20:27:37 +00001044
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00001045 container = object_get_objects_root();
1046 obj = object_resolve_path_component(container, name);
1047 if (!obj) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001048 monitor_printf(mon, "acl: unknown list '%s'\n", name);
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00001049 return NULL;
aliguori76655d62009-03-06 20:27:37 +00001050 }
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00001051
1052 return QAUTHZ_LIST(obj);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001053}
aliguori76655d62009-03-06 20:27:37 +00001054
Daniel P. Berrangé01438402019-02-27 14:57:55 +00001055static bool warn_acl;
1056static void hmp_warn_acl(void)
1057{
1058 if (warn_acl) {
1059 return;
1060 }
1061 error_report("The acl_show, acl_reset, acl_policy, acl_add, acl_remove "
1062 "commands are deprecated with no replacement. Authorization "
1063 "for VNC should be performed using the pluggable QAuthZ "
1064 "objects");
1065 warn_acl = true;
1066}
1067
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001068static void hmp_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001069{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001070 const char *aclname = qdict_get_str(qdict, "aclname");
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00001071 QAuthZList *auth = find_auth(mon, aclname);
1072 QAuthZListRuleList *rules;
1073 size_t i = 0;
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001074
Daniel P. Berrangé01438402019-02-27 14:57:55 +00001075 hmp_warn_acl();
1076
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00001077 if (!auth) {
1078 return;
1079 }
1080
1081 monitor_printf(mon, "policy: %s\n",
1082 QAuthZListPolicy_str(auth->policy));
1083
1084 rules = auth->rules;
1085 while (rules) {
1086 QAuthZListRule *rule = rules->value;
1087 i++;
1088 monitor_printf(mon, "%zu: %s %s\n", i,
1089 QAuthZListPolicy_str(rule->policy),
1090 rule->match);
1091 rules = rules->next;
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001092 }
1093}
1094
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001095static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001096{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001097 const char *aclname = qdict_get_str(qdict, "aclname");
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00001098 QAuthZList *auth = find_auth(mon, aclname);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001099
Daniel P. Berrangé01438402019-02-27 14:57:55 +00001100 hmp_warn_acl();
1101
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00001102 if (!auth) {
1103 return;
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001104 }
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00001105
1106 auth->policy = QAUTHZ_LIST_POLICY_DENY;
1107 qapi_free_QAuthZListRuleList(auth->rules);
1108 auth->rules = NULL;
1109 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001110}
aliguori76655d62009-03-06 20:27:37 +00001111
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001112static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001113{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001114 const char *aclname = qdict_get_str(qdict, "aclname");
1115 const char *policy = qdict_get_str(qdict, "policy");
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00001116 QAuthZList *auth = find_auth(mon, aclname);
1117 int val;
1118 Error *err = NULL;
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001119
Daniel P. Berrangé01438402019-02-27 14:57:55 +00001120 hmp_warn_acl();
1121
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00001122 if (!auth) {
1123 return;
1124 }
1125
1126 val = qapi_enum_parse(&QAuthZListPolicy_lookup,
1127 policy,
1128 QAUTHZ_LIST_POLICY_DENY,
1129 &err);
1130 if (err) {
1131 error_free(err);
1132 monitor_printf(mon, "acl: unknown policy '%s', "
1133 "expected 'deny' or 'allow'\n", policy);
1134 } else {
1135 auth->policy = val;
1136 if (auth->policy == QAUTHZ_LIST_POLICY_ALLOW) {
aliguori28a76be2009-03-06 20:27:40 +00001137 monitor_printf(mon, "acl: policy set to 'allow'\n");
aliguori28a76be2009-03-06 20:27:40 +00001138 } else {
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00001139 monitor_printf(mon, "acl: policy set to 'deny'\n");
aliguori28a76be2009-03-06 20:27:40 +00001140 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001141 }
1142}
aliguori76655d62009-03-06 20:27:37 +00001143
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00001144static QAuthZListFormat hmp_acl_get_format(const char *match)
1145{
1146 if (strchr(match, '*')) {
1147 return QAUTHZ_LIST_FORMAT_GLOB;
1148 } else {
1149 return QAUTHZ_LIST_FORMAT_EXACT;
1150 }
1151}
1152
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001153static void hmp_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001154{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001155 const char *aclname = qdict_get_str(qdict, "aclname");
1156 const char *match = qdict_get_str(qdict, "match");
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00001157 const char *policystr = qdict_get_str(qdict, "policy");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001158 int has_index = qdict_haskey(qdict, "index");
1159 int index = qdict_get_try_int(qdict, "index", -1);
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00001160 QAuthZList *auth = find_auth(mon, aclname);
1161 Error *err = NULL;
1162 QAuthZListPolicy policy;
1163 QAuthZListFormat format;
1164 size_t i = 0;
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001165
Daniel P. Berrangé01438402019-02-27 14:57:55 +00001166 hmp_warn_acl();
1167
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00001168 if (!auth) {
1169 return;
1170 }
1171
1172 policy = qapi_enum_parse(&QAuthZListPolicy_lookup,
1173 policystr,
1174 QAUTHZ_LIST_POLICY_DENY,
1175 &err);
1176 if (err) {
1177 error_free(err);
1178 monitor_printf(mon, "acl: unknown policy '%s', "
1179 "expected 'deny' or 'allow'\n", policystr);
1180 return;
1181 }
1182
1183 format = hmp_acl_get_format(match);
1184
1185 if (has_index && index == 0) {
1186 monitor_printf(mon, "acl: unable to add acl entry\n");
1187 return;
1188 }
1189
1190 if (has_index) {
1191 i = qauthz_list_insert_rule(auth, match, policy,
1192 format, index - 1, &err);
1193 } else {
1194 i = qauthz_list_append_rule(auth, match, policy,
1195 format, &err);
1196 }
1197 if (err) {
1198 monitor_printf(mon, "acl: unable to add rule: %s",
1199 error_get_pretty(err));
1200 error_free(err);
1201 } else {
1202 monitor_printf(mon, "acl: added rule at position %zu\n", i + 1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001203 }
1204}
aliguori76655d62009-03-06 20:27:37 +00001205
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001206static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001207{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001208 const char *aclname = qdict_get_str(qdict, "aclname");
1209 const char *match = qdict_get_str(qdict, "match");
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00001210 QAuthZList *auth = find_auth(mon, aclname);
1211 ssize_t i = 0;
aliguori76655d62009-03-06 20:27:37 +00001212
Daniel P. Berrangé01438402019-02-27 14:57:55 +00001213 hmp_warn_acl();
1214
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +00001215 if (!auth) {
1216 return;
1217 }
1218
1219 i = qauthz_list_delete_rule(auth, match);
1220 if (i >= 0) {
1221 monitor_printf(mon, "acl: removed rule at position %zu\n", i + 1);
1222 } else {
1223 monitor_printf(mon, "acl: no matching acl entry\n");
aliguori76655d62009-03-06 20:27:37 +00001224 }
1225}
1226
Corey Bryant208c9d12012-06-22 14:36:09 -04001227void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001228{
Kevin Wolf947e4742020-10-05 17:58:44 +02001229 Monitor *cur_mon = monitor_cur();
Anthony Liguoric227f092009-10-01 16:12:16 -05001230 mon_fd_t *monfd;
Peter Xu9409fc02018-06-08 11:55:06 +08001231 int fd, tmp_fd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001232
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03001233 fd = qemu_chr_fe_get_msgfd(&cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001234 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001235 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryant208c9d12012-06-22 14:36:09 -04001236 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001237 }
1238
1239 if (qemu_isdigit(fdname[0])) {
Stefan Hajnoczi0b9f0e22014-04-24 13:58:18 +02001240 close(fd);
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001241 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
1242 "a name not starting with a digit");
Corey Bryant208c9d12012-06-22 14:36:09 -04001243 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001244 }
1245
Dr. David Alan Gilbert0210c3b2020-09-22 10:57:41 +01001246 QEMU_LOCK_GUARD(&cur_mon->mon_lock);
Corey Bryant208c9d12012-06-22 14:36:09 -04001247 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001248 if (strcmp(monfd->name, fdname) != 0) {
1249 continue;
1250 }
1251
Peter Xu9409fc02018-06-08 11:55:06 +08001252 tmp_fd = monfd->fd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001253 monfd->fd = fd;
Markus Armbruster774a6b62018-07-03 10:53:57 +02001254 /* Make sure close() is outside critical section */
Peter Xu9409fc02018-06-08 11:55:06 +08001255 close(tmp_fd);
Corey Bryant208c9d12012-06-22 14:36:09 -04001256 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001257 }
1258
Anthony Liguori7267c092011-08-20 22:09:37 -05001259 monfd = g_malloc0(sizeof(mon_fd_t));
1260 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001261 monfd->fd = fd;
1262
Corey Bryant208c9d12012-06-22 14:36:09 -04001263 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001264}
1265
Corey Bryant208c9d12012-06-22 14:36:09 -04001266void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001267{
Kevin Wolf947e4742020-10-05 17:58:44 +02001268 Monitor *cur_mon = monitor_cur();
Anthony Liguoric227f092009-10-01 16:12:16 -05001269 mon_fd_t *monfd;
Peter Xu9409fc02018-06-08 11:55:06 +08001270 int tmp_fd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001271
Peter Xu9409fc02018-06-08 11:55:06 +08001272 qemu_mutex_lock(&cur_mon->mon_lock);
Corey Bryant208c9d12012-06-22 14:36:09 -04001273 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001274 if (strcmp(monfd->name, fdname) != 0) {
1275 continue;
1276 }
1277
Blue Swirl72cf2d42009-09-12 07:36:22 +00001278 QLIST_REMOVE(monfd, next);
Peter Xu9409fc02018-06-08 11:55:06 +08001279 tmp_fd = monfd->fd;
Anthony Liguori7267c092011-08-20 22:09:37 -05001280 g_free(monfd->name);
1281 g_free(monfd);
Peter Xu9409fc02018-06-08 11:55:06 +08001282 qemu_mutex_unlock(&cur_mon->mon_lock);
Markus Armbruster774a6b62018-07-03 10:53:57 +02001283 /* Make sure close() is outside critical section */
Peter Xu9409fc02018-06-08 11:55:06 +08001284 close(tmp_fd);
Corey Bryant208c9d12012-06-22 14:36:09 -04001285 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001286 }
1287
Peter Xu9409fc02018-06-08 11:55:06 +08001288 qemu_mutex_unlock(&cur_mon->mon_lock);
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001289 error_setg(errp, QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001290}
1291
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001292int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
Mark McLoughlin7768e042009-07-22 09:11:41 +01001293{
Anthony Liguoric227f092009-10-01 16:12:16 -05001294 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01001295
Dr. David Alan Gilbert0210c3b2020-09-22 10:57:41 +01001296 QEMU_LOCK_GUARD(&mon->mon_lock);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001297 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01001298 int fd;
1299
1300 if (strcmp(monfd->name, fdname) != 0) {
1301 continue;
1302 }
1303
1304 fd = monfd->fd;
1305
1306 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001307 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05001308 g_free(monfd->name);
1309 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001310
1311 return fd;
1312 }
1313
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001314 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001315 return -1;
1316}
1317
Corey Bryantba1c0482012-08-14 16:43:43 -04001318static void monitor_fdset_cleanup(MonFdset *mon_fdset)
1319{
1320 MonFdsetFd *mon_fdset_fd;
1321 MonFdsetFd *mon_fdset_fd_next;
1322
1323 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantebe52b52012-10-18 15:19:33 -04001324 if ((mon_fdset_fd->removed ||
1325 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
1326 runstate_is_running()) {
Corey Bryantba1c0482012-08-14 16:43:43 -04001327 close(mon_fdset_fd->fd);
1328 g_free(mon_fdset_fd->opaque);
1329 QLIST_REMOVE(mon_fdset_fd, next);
1330 g_free(mon_fdset_fd);
1331 }
1332 }
1333
Corey Bryantadb696f2012-08-14 16:43:47 -04001334 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04001335 QLIST_REMOVE(mon_fdset, next);
1336 g_free(mon_fdset);
1337 }
1338}
1339
Kevin Wolf7e3c0de2019-06-13 17:34:00 +02001340void monitor_fdsets_cleanup(void)
Corey Bryantefb87c12012-08-14 16:43:48 -04001341{
1342 MonFdset *mon_fdset;
1343 MonFdset *mon_fdset_next;
1344
Dr. David Alan Gilbert0210c3b2020-09-22 10:57:41 +01001345 QEMU_LOCK_GUARD(&mon_fdsets_lock);
Corey Bryantefb87c12012-08-14 16:43:48 -04001346 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
1347 monitor_fdset_cleanup(mon_fdset);
1348 }
1349}
1350
Corey Bryantba1c0482012-08-14 16:43:43 -04001351AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
1352 const char *opaque, Error **errp)
1353{
1354 int fd;
Kevin Wolf947e4742020-10-05 17:58:44 +02001355 Monitor *mon = monitor_cur();
Corey Bryantba1c0482012-08-14 16:43:43 -04001356 AddfdInfo *fdinfo;
1357
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03001358 fd = qemu_chr_fe_get_msgfd(&mon->chr);
Corey Bryantba1c0482012-08-14 16:43:43 -04001359 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001360 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryantba1c0482012-08-14 16:43:43 -04001361 goto error;
1362 }
1363
Corey Bryante446f702012-10-18 15:19:32 -04001364 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
1365 has_opaque, opaque, errp);
1366 if (fdinfo) {
1367 return fdinfo;
Corey Bryant9ac54af2012-10-18 15:19:31 -04001368 }
1369
Corey Bryantba1c0482012-08-14 16:43:43 -04001370error:
1371 if (fd != -1) {
1372 close(fd);
1373 }
1374 return NULL;
1375}
1376
1377void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
1378{
1379 MonFdset *mon_fdset;
1380 MonFdsetFd *mon_fdset_fd;
1381 char fd_str[60];
1382
Dr. David Alan Gilbert0210c3b2020-09-22 10:57:41 +01001383 QEMU_LOCK_GUARD(&mon_fdsets_lock);
Corey Bryantba1c0482012-08-14 16:43:43 -04001384 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1385 if (mon_fdset->id != fdset_id) {
1386 continue;
1387 }
1388 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1389 if (has_fd) {
1390 if (mon_fdset_fd->fd != fd) {
1391 continue;
1392 }
1393 mon_fdset_fd->removed = true;
1394 break;
1395 } else {
1396 mon_fdset_fd->removed = true;
1397 }
1398 }
1399 if (has_fd && !mon_fdset_fd) {
1400 goto error;
1401 }
1402 monitor_fdset_cleanup(mon_fdset);
1403 return;
1404 }
1405
1406error:
1407 if (has_fd) {
1408 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
1409 fdset_id, fd);
1410 } else {
1411 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
1412 }
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001413 error_setg(errp, QERR_FD_NOT_FOUND, fd_str);
Corey Bryantba1c0482012-08-14 16:43:43 -04001414}
1415
1416FdsetInfoList *qmp_query_fdsets(Error **errp)
1417{
1418 MonFdset *mon_fdset;
1419 MonFdsetFd *mon_fdset_fd;
1420 FdsetInfoList *fdset_list = NULL;
1421
Dr. David Alan Gilbert0210c3b2020-09-22 10:57:41 +01001422 QEMU_LOCK_GUARD(&mon_fdsets_lock);
Corey Bryantba1c0482012-08-14 16:43:43 -04001423 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1424 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
1425 FdsetFdInfoList *fdsetfd_list = NULL;
1426
1427 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
1428 fdset_info->value->fdset_id = mon_fdset->id;
1429
1430 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1431 FdsetFdInfoList *fdsetfd_info;
1432
1433 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
1434 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
1435 fdsetfd_info->value->fd = mon_fdset_fd->fd;
1436 if (mon_fdset_fd->opaque) {
1437 fdsetfd_info->value->has_opaque = true;
1438 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
1439 } else {
1440 fdsetfd_info->value->has_opaque = false;
1441 }
1442
1443 fdsetfd_info->next = fdsetfd_list;
1444 fdsetfd_list = fdsetfd_info;
1445 }
1446
1447 fdset_info->value->fds = fdsetfd_list;
1448
1449 fdset_info->next = fdset_list;
1450 fdset_list = fdset_info;
1451 }
1452
1453 return fdset_list;
1454}
1455
Corey Bryante446f702012-10-18 15:19:32 -04001456AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
1457 bool has_opaque, const char *opaque,
1458 Error **errp)
1459{
1460 MonFdset *mon_fdset = NULL;
1461 MonFdsetFd *mon_fdset_fd;
1462 AddfdInfo *fdinfo;
1463
Daniel Brodsky6e8a3552020-04-03 21:21:08 -07001464 QEMU_LOCK_GUARD(&mon_fdsets_lock);
Corey Bryante446f702012-10-18 15:19:32 -04001465 if (has_fdset_id) {
1466 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1467 /* Break if match found or match impossible due to ordering by ID */
1468 if (fdset_id <= mon_fdset->id) {
1469 if (fdset_id < mon_fdset->id) {
1470 mon_fdset = NULL;
1471 }
1472 break;
1473 }
1474 }
1475 }
1476
1477 if (mon_fdset == NULL) {
1478 int64_t fdset_id_prev = -1;
1479 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
1480
1481 if (has_fdset_id) {
1482 if (fdset_id < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001483 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
1484 "a non-negative value");
Corey Bryante446f702012-10-18 15:19:32 -04001485 return NULL;
1486 }
1487 /* Use specified fdset ID */
1488 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1489 mon_fdset_cur = mon_fdset;
1490 if (fdset_id < mon_fdset_cur->id) {
1491 break;
1492 }
1493 }
1494 } else {
1495 /* Use first available fdset ID */
1496 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1497 mon_fdset_cur = mon_fdset;
1498 if (fdset_id_prev == mon_fdset_cur->id - 1) {
1499 fdset_id_prev = mon_fdset_cur->id;
1500 continue;
1501 }
1502 break;
1503 }
1504 }
1505
1506 mon_fdset = g_malloc0(sizeof(*mon_fdset));
1507 if (has_fdset_id) {
1508 mon_fdset->id = fdset_id;
1509 } else {
1510 mon_fdset->id = fdset_id_prev + 1;
1511 }
1512
1513 /* The fdset list is ordered by fdset ID */
1514 if (!mon_fdset_cur) {
1515 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
1516 } else if (mon_fdset->id < mon_fdset_cur->id) {
1517 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
1518 } else {
1519 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
1520 }
1521 }
1522
1523 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
1524 mon_fdset_fd->fd = fd;
1525 mon_fdset_fd->removed = false;
1526 if (has_opaque) {
1527 mon_fdset_fd->opaque = g_strdup(opaque);
1528 }
1529 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
1530
1531 fdinfo = g_malloc0(sizeof(*fdinfo));
1532 fdinfo->fdset_id = mon_fdset->id;
1533 fdinfo->fd = mon_fdset_fd->fd;
1534
1535 return fdinfo;
1536}
1537
Daniel P. Berrangé60efffa2020-08-27 13:27:00 +01001538int monitor_fdset_dup_fd_add(int64_t fdset_id, int flags)
Corey Bryantadb696f2012-08-14 16:43:47 -04001539{
Peter Xu47451462018-06-08 11:55:11 +08001540#ifdef _WIN32
1541 return -ENOENT;
1542#else
Corey Bryantadb696f2012-08-14 16:43:47 -04001543 MonFdset *mon_fdset;
Corey Bryantadb696f2012-08-14 16:43:47 -04001544
Dr. David Alan Gilbert0210c3b2020-09-22 10:57:41 +01001545 QEMU_LOCK_GUARD(&mon_fdsets_lock);
Corey Bryantadb696f2012-08-14 16:43:47 -04001546 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
Daniel P. Berrangé60efffa2020-08-27 13:27:00 +01001547 MonFdsetFd *mon_fdset_fd;
1548 MonFdsetFd *mon_fdset_fd_dup;
1549 int fd = -1;
1550 int dup_fd;
1551 int mon_fd_flags;
1552
Corey Bryantadb696f2012-08-14 16:43:47 -04001553 if (mon_fdset->id != fdset_id) {
1554 continue;
1555 }
Daniel P. Berrangé60efffa2020-08-27 13:27:00 +01001556
Corey Bryantadb696f2012-08-14 16:43:47 -04001557 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1558 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
1559 if (mon_fd_flags == -1) {
Daniel P. Berrangé60efffa2020-08-27 13:27:00 +01001560 return -1;
Corey Bryantadb696f2012-08-14 16:43:47 -04001561 }
1562
1563 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
Daniel P. Berrangé60efffa2020-08-27 13:27:00 +01001564 fd = mon_fdset_fd->fd;
1565 break;
Corey Bryantadb696f2012-08-14 16:43:47 -04001566 }
1567 }
Corey Bryantadb696f2012-08-14 16:43:47 -04001568
Daniel P. Berrangé60efffa2020-08-27 13:27:00 +01001569 if (fd == -1) {
Daniel P. Berrangé60efffa2020-08-27 13:27:00 +01001570 errno = EACCES;
1571 return -1;
Corey Bryantadb696f2012-08-14 16:43:47 -04001572 }
Daniel P. Berrangé60efffa2020-08-27 13:27:00 +01001573
1574 dup_fd = qemu_dup_flags(fd, flags);
1575 if (dup_fd == -1) {
Daniel P. Berrangé60efffa2020-08-27 13:27:00 +01001576 return -1;
Corey Bryantadb696f2012-08-14 16:43:47 -04001577 }
Daniel P. Berrangé60efffa2020-08-27 13:27:00 +01001578
Corey Bryantadb696f2012-08-14 16:43:47 -04001579 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
1580 mon_fdset_fd_dup->fd = dup_fd;
1581 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
Daniel P. Berrangé60efffa2020-08-27 13:27:00 +01001582 return dup_fd;
Corey Bryantadb696f2012-08-14 16:43:47 -04001583 }
Peter Xu47451462018-06-08 11:55:11 +08001584
Daniel P. Berrangé60efffa2020-08-27 13:27:00 +01001585 errno = ENOENT;
Corey Bryantadb696f2012-08-14 16:43:47 -04001586 return -1;
Daniel P. Berrangé60efffa2020-08-27 13:27:00 +01001587#endif
Corey Bryantadb696f2012-08-14 16:43:47 -04001588}
1589
Yury Kotov854f63d2019-05-23 12:44:33 +03001590static int64_t monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
Corey Bryantadb696f2012-08-14 16:43:47 -04001591{
1592 MonFdset *mon_fdset;
1593 MonFdsetFd *mon_fdset_fd_dup;
1594
Dr. David Alan Gilbert0210c3b2020-09-22 10:57:41 +01001595 QEMU_LOCK_GUARD(&mon_fdsets_lock);
Corey Bryantadb696f2012-08-14 16:43:47 -04001596 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1597 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
1598 if (mon_fdset_fd_dup->fd == dup_fd) {
1599 if (remove) {
1600 QLIST_REMOVE(mon_fdset_fd_dup, next);
Chen Quna6616142020-01-15 15:20:16 +08001601 g_free(mon_fdset_fd_dup);
Corey Bryantadb696f2012-08-14 16:43:47 -04001602 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
1603 monitor_fdset_cleanup(mon_fdset);
1604 }
Dr. David Alan Gilbert0210c3b2020-09-22 10:57:41 +01001605 return -1;
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02001606 } else {
1607 return mon_fdset->id;
Corey Bryantadb696f2012-08-14 16:43:47 -04001608 }
Corey Bryantadb696f2012-08-14 16:43:47 -04001609 }
1610 }
1611 }
Peter Xu47451462018-06-08 11:55:11 +08001612
Corey Bryantadb696f2012-08-14 16:43:47 -04001613 return -1;
1614}
1615
Yury Kotov854f63d2019-05-23 12:44:33 +03001616int64_t monitor_fdset_dup_fd_find(int dup_fd)
Corey Bryantadb696f2012-08-14 16:43:47 -04001617{
1618 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
1619}
1620
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02001621void monitor_fdset_dup_fd_remove(int dup_fd)
Corey Bryantadb696f2012-08-14 16:43:47 -04001622{
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02001623 monitor_fdset_dup_fd_find_remove(dup_fd, true);
Corey Bryantadb696f2012-08-14 16:43:47 -04001624}
1625
Markus Armbruster1677f4c2015-02-09 14:03:19 +01001626int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
Laszlo Ersek59063662014-04-10 10:24:31 +02001627{
1628 int fd;
1629 Error *local_err = NULL;
1630
1631 if (!qemu_isdigit(fdname[0]) && mon) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001632 fd = monitor_get_fd(mon, fdname, &local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00001633 } else {
1634 fd = qemu_parse_fd(fdname);
Laszlo Ersek59063662014-04-10 10:24:31 +02001635 if (fd == -1) {
1636 error_setg(&local_err, "Invalid file descriptor number '%s'",
1637 fdname);
1638 }
1639 }
1640 if (local_err) {
1641 error_propagate(errp, local_err);
1642 assert(fd == -1);
1643 } else {
1644 assert(fd != -1);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00001645 }
1646
1647 return fd;
1648}
1649
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03001650/* Please update hmp-commands.hx when adding or changing commands */
Kevin Wolfa0cd5e12019-06-13 17:33:56 +02001651static HMPCommand hmp_info_cmds[] = {
Pavel Butsykinda76ee72015-09-10 18:38:58 +03001652#include "hmp-commands-info.h"
1653 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00001654};
1655
Kevin Wolfa0cd5e12019-06-13 17:33:56 +02001656/* hmp_cmds and hmp_info_cmds would be sorted at runtime */
Kevin Wolfed7bda52019-06-13 17:34:01 +02001657HMPCommand hmp_cmds[] = {
Wenchao Xiaa13ced52013-01-14 14:06:28 +08001658#include "hmp-commands.h"
1659 { NULL, NULL, },
1660};
1661
Kevin Wolfed7bda52019-06-13 17:34:01 +02001662/*
1663 * Set @pval to the value in the register identified by @name.
1664 * return 0 if OK, -1 if not found
1665 */
Kevin Wolf2fc5d012020-11-13 12:43:24 +01001666int get_monitor_def(Monitor *mon, int64_t *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00001667{
Pavel Butsykinbf957282015-09-10 18:38:59 +03001668 const MonitorDef *md = target_monitor_defs();
Kevin Wolf2fc5d012020-11-13 12:43:24 +01001669 CPUState *cs = mon_get_cpu(mon);
bellard92a31b12005-02-10 22:00:52 +00001670 void *ptr;
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11001671 uint64_t tmp = 0;
1672 int ret;
bellard92a31b12005-02-10 22:00:52 +00001673
Thomas Huth854e67f2017-01-13 13:12:35 +01001674 if (cs == NULL || md == NULL) {
Pavel Butsykinbf957282015-09-10 18:38:59 +03001675 return -1;
1676 }
1677
1678 for(; md->name != NULL; md++) {
Kevin Wolfed7bda52019-06-13 17:34:01 +02001679 if (hmp_compare_cmd(name, md->name)) {
bellard9307c4c2004-04-04 12:57:25 +00001680 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00001681 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00001682 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001683 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00001684 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00001685 switch(md->type) {
1686 case MD_I32:
1687 *pval = *(int32_t *)ptr;
1688 break;
1689 case MD_TLONG:
1690 *pval = *(target_long *)ptr;
1691 break;
1692 default:
1693 *pval = 0;
1694 break;
1695 }
bellard9307c4c2004-04-04 12:57:25 +00001696 }
1697 return 0;
1698 }
1699 }
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11001700
Thomas Huth854e67f2017-01-13 13:12:35 +01001701 ret = target_get_monitor_def(cs, name, &tmp);
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11001702 if (!ret) {
1703 *pval = (target_long) tmp;
1704 }
1705
1706 return ret;
bellard9307c4c2004-04-04 12:57:25 +00001707}
1708
Hani Benhabiles40d19392014-05-07 23:41:30 +01001709static void add_completion_option(ReadLineState *rs, const char *str,
1710 const char *option)
1711{
1712 if (!str || !option) {
1713 return;
1714 }
1715 if (!strncmp(option, str, strlen(str))) {
1716 readline_add_completion(rs, option);
1717 }
1718}
1719
Hani Benhabiles13e315d2014-05-07 23:41:29 +01001720void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
1721{
1722 size_t len;
1723 ChardevBackendInfoList *list, *start;
1724
1725 if (nb_args != 2) {
1726 return;
1727 }
1728 len = strlen(str);
1729 readline_set_completion_index(rs, len);
1730
1731 start = list = qmp_query_chardev_backends(NULL);
1732 while (list) {
1733 const char *chr_name = list->value->name;
1734
1735 if (!strncmp(chr_name, str, len)) {
1736 readline_add_completion(rs, chr_name);
1737 }
1738 list = list->next;
1739 }
1740 qapi_free_ChardevBackendInfoList(start);
1741}
1742
Hani Benhabilesb162b492014-05-07 23:41:31 +01001743void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
1744{
1745 size_t len;
1746 int i;
1747
1748 if (nb_args != 2) {
1749 return;
1750 }
1751 len = strlen(str);
1752 readline_set_completion_index(rs, len);
Markus Armbruster1c236ba2017-08-24 10:46:06 +02001753 for (i = 0; i < NET_CLIENT_DRIVER__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02001754 add_completion_option(rs, str, NetClientDriver_str(i));
Hani Benhabilesb162b492014-05-07 23:41:31 +01001755 }
1756}
1757
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01001758void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles992d3e62014-02-06 23:30:11 +01001759{
1760 GSList *list, *elt;
1761 size_t len;
1762
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01001763 if (nb_args != 2) {
1764 return;
1765 }
1766
Hani Benhabiles992d3e62014-02-06 23:30:11 +01001767 len = strlen(str);
1768 readline_set_completion_index(rs, len);
1769 list = elt = object_class_get_list(TYPE_DEVICE, false);
1770 while (elt) {
1771 const char *name;
1772 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
1773 TYPE_DEVICE);
1774 name = object_class_get_name(OBJECT_CLASS(dc));
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01001775
Eduardo Habkoste90f2a82017-05-03 17:35:44 -03001776 if (dc->user_creatable
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01001777 && !strncmp(name, str, len)) {
Hani Benhabiles992d3e62014-02-06 23:30:11 +01001778 readline_add_completion(rs, name);
1779 }
1780 elt = elt->next;
1781 }
1782 g_slist_free(list);
1783}
1784
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01001785void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles1094fd32014-02-06 23:30:13 +01001786{
1787 GSList *list, *elt;
1788 size_t len;
1789
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01001790 if (nb_args != 2) {
1791 return;
1792 }
1793
Hani Benhabiles1094fd32014-02-06 23:30:13 +01001794 len = strlen(str);
1795 readline_set_completion_index(rs, len);
1796 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
1797 while (elt) {
1798 const char *name;
1799
1800 name = object_class_get_name(OBJECT_CLASS(elt->data));
1801 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
1802 readline_add_completion(rs, name);
1803 }
1804 elt = elt->next;
1805 }
1806 g_slist_free(list);
1807}
1808
Marc-André Lureau91590152020-01-10 19:30:17 +04001809static int qdev_add_hotpluggable_device(Object *obj, void *opaque)
1810{
1811 GSList **list = opaque;
Philippe Mathieu-Daudé688ffbb2020-05-12 09:00:19 +02001812 DeviceState *dev = (DeviceState *)object_dynamic_cast(obj, TYPE_DEVICE);
Marc-André Lureau91590152020-01-10 19:30:17 +04001813
1814 if (dev == NULL) {
1815 return 0;
1816 }
1817
1818 if (dev->realized && object_property_get_bool(obj, "hotpluggable", NULL)) {
1819 *list = g_slist_append(*list, dev);
1820 }
1821
1822 return 0;
1823}
1824
1825static GSList *qdev_build_hotpluggable_device_list(Object *peripheral)
1826{
1827 GSList *list = NULL;
1828
1829 object_child_foreach(peripheral, qdev_add_hotpluggable_device, &list);
1830
1831 return list;
1832}
1833
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08001834static void peripheral_device_del_completion(ReadLineState *rs,
1835 const char *str, size_t len)
1836{
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02001837 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
1838 GSList *list, *item;
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08001839
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02001840 list = qdev_build_hotpluggable_device_list(peripheral);
1841 if (!list) {
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08001842 return;
1843 }
1844
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08001845 for (item = list; item; item = g_slist_next(item)) {
1846 DeviceState *dev = item->data;
1847
1848 if (dev->id && !strncmp(str, dev->id, len)) {
1849 readline_add_completion(rs, dev->id);
1850 }
1851 }
1852
1853 g_slist_free(list);
1854}
1855
Hani Benhabiles6297d9a2014-05-07 23:41:28 +01001856void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
1857{
1858 size_t len;
1859 ChardevInfoList *list, *start;
1860
1861 if (nb_args != 2) {
1862 return;
1863 }
1864 len = strlen(str);
1865 readline_set_completion_index(rs, len);
1866
1867 start = list = qmp_query_chardev(NULL);
1868 while (list) {
1869 ChardevInfo *chr = list->value;
1870
1871 if (!strncmp(chr->label, str, len)) {
1872 readline_add_completion(rs, chr->label);
1873 }
1874 list = list->next;
1875 }
1876 qapi_free_ChardevInfoList(start);
1877}
1878
Hani Benhabiles8e597772014-05-27 23:39:30 +01001879static void ringbuf_completion(ReadLineState *rs, const char *str)
1880{
1881 size_t len;
1882 ChardevInfoList *list, *start;
1883
1884 len = strlen(str);
1885 readline_set_completion_index(rs, len);
1886
1887 start = list = qmp_query_chardev(NULL);
1888 while (list) {
1889 ChardevInfo *chr_info = list->value;
1890
1891 if (!strncmp(chr_info->label, str, len)) {
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03001892 Chardev *chr = qemu_chr_find(chr_info->label);
Marc-André Lureau777357d2016-12-07 18:39:10 +03001893 if (chr && CHARDEV_IS_RINGBUF(chr)) {
Hani Benhabiles8e597772014-05-27 23:39:30 +01001894 readline_add_completion(rs, chr_info->label);
1895 }
1896 }
1897 list = list->next;
1898 }
1899 qapi_free_ChardevInfoList(start);
1900}
1901
Hani Benhabiles8e597772014-05-27 23:39:30 +01001902void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
1903{
1904 if (nb_args != 2) {
1905 return;
1906 }
1907 ringbuf_completion(rs, str);
1908}
1909
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01001910void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
1911{
1912 size_t len;
1913
1914 if (nb_args != 2) {
1915 return;
1916 }
1917
1918 len = strlen(str);
1919 readline_set_completion_index(rs, len);
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08001920 peripheral_device_del_completion(rs, str, len);
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01001921}
1922
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01001923void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabilesb48fa072014-02-06 23:30:12 +01001924{
1925 ObjectPropertyInfoList *list, *start;
1926 size_t len;
1927
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01001928 if (nb_args != 2) {
1929 return;
1930 }
Hani Benhabilesb48fa072014-02-06 23:30:12 +01001931 len = strlen(str);
1932 readline_set_completion_index(rs, len);
1933
1934 start = list = qmp_qom_list("/objects", NULL);
1935 while (list) {
1936 ObjectPropertyInfo *info = list->value;
1937
1938 if (!strncmp(info->type, "child<", 5)
1939 && !strncmp(info->name, str, len)) {
1940 readline_add_completion(rs, info->name);
1941 }
1942 list = list->next;
1943 }
1944 qapi_free_ObjectPropertyInfoList(start);
1945}
1946
Hani Benhabiles29136cd2014-05-07 23:41:27 +01001947void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
1948{
1949 int i;
1950 char *sep;
1951 size_t len;
1952
1953 if (nb_args != 2) {
1954 return;
1955 }
1956 sep = strrchr(str, '-');
1957 if (sep) {
1958 str = sep + 1;
1959 }
1960 len = strlen(str);
1961 readline_set_completion_index(rs, len);
Eric Blake7fb1cf12015-11-18 01:52:57 -07001962 for (i = 0; i < Q_KEY_CODE__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02001963 if (!strncmp(str, QKeyCode_str(i), len)) {
1964 readline_add_completion(rs, QKeyCode_str(i));
Hani Benhabiles29136cd2014-05-07 23:41:27 +01001965 }
1966 }
1967}
1968
Hani Benhabiles40d19392014-05-07 23:41:30 +01001969void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
1970{
1971 size_t len;
1972
1973 len = strlen(str);
1974 readline_set_completion_index(rs, len);
1975 if (nb_args == 2) {
Jason Wangeaed4832015-04-23 14:21:38 +08001976 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles40d19392014-05-07 23:41:30 +01001977 int count, i;
1978 count = qemu_find_net_clients_except(NULL, ncs,
Eric Blakef394b2e2016-07-13 21:50:23 -06001979 NET_CLIENT_DRIVER_NONE,
Jason Wangeaed4832015-04-23 14:21:38 +08001980 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08001981 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles40d19392014-05-07 23:41:30 +01001982 const char *name = ncs[i]->name;
1983 if (!strncmp(str, name, len)) {
1984 readline_add_completion(rs, name);
1985 }
1986 }
1987 } else if (nb_args == 3) {
1988 add_completion_option(rs, str, "on");
1989 add_completion_option(rs, str, "off");
1990 }
1991}
1992
Hani Benhabiles11b389f2014-05-07 23:41:32 +01001993void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
1994{
1995 int len, count, i;
Jason Wangeaed4832015-04-23 14:21:38 +08001996 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles11b389f2014-05-07 23:41:32 +01001997
1998 if (nb_args != 2) {
1999 return;
2000 }
2001
2002 len = strlen(str);
2003 readline_set_completion_index(rs, len);
Eric Blakef394b2e2016-07-13 21:50:23 -06002004 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_DRIVER_NIC,
Jason Wangeaed4832015-04-23 14:21:38 +08002005 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08002006 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles11b389f2014-05-07 23:41:32 +01002007 const char *name = ncs[i]->name;
2008 if (strncmp(str, name, len)) {
2009 continue;
2010 }
Eric Blake08712fc2020-03-17 15:17:11 -05002011 if (ncs[i]->is_netdev) {
Hani Benhabiles11b389f2014-05-07 23:41:32 +01002012 readline_add_completion(rs, name);
2013 }
2014 }
2015}
2016
Lluís Vilanovabd712112016-07-11 12:53:51 +02002017void info_trace_events_completion(ReadLineState *rs, int nb_args, const char *str)
2018{
2019 size_t len;
2020
2021 len = strlen(str);
2022 readline_set_completion_index(rs, len);
2023 if (nb_args == 2) {
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01002024 TraceEventIter iter;
2025 TraceEvent *ev;
2026 char *pattern = g_strdup_printf("%s*", str);
2027 trace_event_iter_init(&iter, pattern);
2028 while ((ev = trace_event_iter_next(&iter)) != NULL) {
2029 readline_add_completion(rs, trace_event_get_name(ev));
Lluís Vilanovabd712112016-07-11 12:53:51 +02002030 }
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01002031 g_free(pattern);
Lluís Vilanovabd712112016-07-11 12:53:51 +02002032 }
2033}
2034
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01002035void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
2036{
2037 size_t len;
2038
2039 len = strlen(str);
2040 readline_set_completion_index(rs, len);
2041 if (nb_args == 2) {
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01002042 TraceEventIter iter;
2043 TraceEvent *ev;
2044 char *pattern = g_strdup_printf("%s*", str);
2045 trace_event_iter_init(&iter, pattern);
2046 while ((ev = trace_event_iter_next(&iter)) != NULL) {
2047 readline_add_completion(rs, trace_event_get_name(ev));
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01002048 }
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01002049 g_free(pattern);
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01002050 } else if (nb_args == 3) {
2051 add_completion_option(rs, str, "on");
2052 add_completion_option(rs, str, "off");
2053 }
2054}
2055
Hani Benhabilesd0ece342014-05-27 23:39:31 +01002056void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
2057{
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01002058 int i;
2059
Hani Benhabilesd0ece342014-05-27 23:39:31 +01002060 if (nb_args != 2) {
2061 return;
2062 }
2063 readline_set_completion_index(rs, strlen(str));
Michal Privoznik14d53b42017-09-07 10:05:24 +02002064 for (i = 0; i < WATCHDOG_ACTION__MAX; i++) {
2065 add_completion_option(rs, str, WatchdogAction_str(i));
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01002066 }
Hani Benhabilesd0ece342014-05-27 23:39:31 +01002067}
2068
Hani Benhabilesc68a0402014-05-27 23:39:32 +01002069void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
2070 const char *str)
2071{
2072 size_t len;
2073
2074 len = strlen(str);
2075 readline_set_completion_index(rs, len);
2076 if (nb_args == 2) {
2077 int i;
Eric Blake7fb1cf12015-11-18 01:52:57 -07002078 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02002079 const char *name = MigrationCapability_str(i);
Hani Benhabilesc68a0402014-05-27 23:39:32 +01002080 if (!strncmp(str, name, len)) {
2081 readline_add_completion(rs, name);
2082 }
2083 }
2084 } else if (nb_args == 3) {
2085 add_completion_option(rs, str, "on");
2086 add_completion_option(rs, str, "off");
2087 }
2088}
2089
Liang Li50e9a622015-03-23 16:32:29 +08002090void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
2091 const char *str)
2092{
2093 size_t len;
2094
2095 len = strlen(str);
2096 readline_set_completion_index(rs, len);
2097 if (nb_args == 2) {
2098 int i;
Eric Blake7fb1cf12015-11-18 01:52:57 -07002099 for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02002100 const char *name = MigrationParameter_str(i);
Liang Li50e9a622015-03-23 16:32:29 +08002101 if (!strncmp(str, name, len)) {
2102 readline_add_completion(rs, name);
2103 }
2104 }
2105 }
2106}
2107
Hani Benhabilesb21631f2014-05-27 23:39:37 +01002108static void vm_completion(ReadLineState *rs, const char *str)
2109{
2110 size_t len;
Kevin Wolf7c8eece2016-03-22 18:58:50 +01002111 BlockDriverState *bs;
Kevin Wolf88be7b42016-05-20 18:49:07 +02002112 BdrvNextIterator it;
Hani Benhabilesb21631f2014-05-27 23:39:37 +01002113
2114 len = strlen(str);
2115 readline_set_completion_index(rs, len);
Kevin Wolf7c8eece2016-03-22 18:58:50 +01002116
Kevin Wolf88be7b42016-05-20 18:49:07 +02002117 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Hani Benhabilesb21631f2014-05-27 23:39:37 +01002118 SnapshotInfoList *snapshots, *snapshot;
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03002119 AioContext *ctx = bdrv_get_aio_context(bs);
2120 bool ok = false;
Hani Benhabilesb21631f2014-05-27 23:39:37 +01002121
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03002122 aio_context_acquire(ctx);
2123 if (bdrv_can_snapshot(bs)) {
2124 ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0;
2125 }
2126 aio_context_release(ctx);
2127 if (!ok) {
Hani Benhabilesb21631f2014-05-27 23:39:37 +01002128 continue;
2129 }
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03002130
Hani Benhabilesb21631f2014-05-27 23:39:37 +01002131 snapshot = snapshots;
2132 while (snapshot) {
2133 char *completion = snapshot->value->name;
2134 if (!strncmp(str, completion, len)) {
2135 readline_add_completion(rs, completion);
2136 }
2137 completion = snapshot->value->id;
2138 if (!strncmp(str, completion, len)) {
2139 readline_add_completion(rs, completion);
2140 }
2141 snapshot = snapshot->next;
2142 }
2143 qapi_free_SnapshotInfoList(snapshots);
2144 }
2145
2146}
2147
2148void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
2149{
2150 if (nb_args == 2) {
2151 vm_completion(rs, str);
2152 }
2153}
2154
2155void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
2156{
2157 if (nb_args == 2) {
2158 vm_completion(rs, str);
2159 }
2160}
2161
Wayne Xia816f8922011-10-12 11:32:41 +08002162static int
2163compare_mon_cmd(const void *a, const void *b)
2164{
Kevin Wolfa0cd5e12019-06-13 17:33:56 +02002165 return strcmp(((const HMPCommand *)a)->name,
2166 ((const HMPCommand *)b)->name);
Wayne Xia816f8922011-10-12 11:32:41 +08002167}
2168
2169static void sortcmdlist(void)
2170{
Kevin Wolfa0cd5e12019-06-13 17:33:56 +02002171 qsort(hmp_cmds, ARRAY_SIZE(hmp_cmds) - 1,
2172 sizeof(*hmp_cmds),
2173 compare_mon_cmd);
2174 qsort(hmp_info_cmds, ARRAY_SIZE(hmp_info_cmds) - 1,
2175 sizeof(*hmp_info_cmds),
2176 compare_mon_cmd);
Wayne Xia816f8922011-10-12 11:32:41 +08002177}
2178
Peter Xu6adf08d2018-03-09 16:59:50 +08002179void monitor_init_globals(void)
2180{
Kevin Wolf1d95db72019-06-13 17:34:02 +02002181 monitor_init_globals_core();
Peter Xu6adf08d2018-03-09 16:59:50 +08002182 monitor_init_qmp_commands();
Peter Xu6adf08d2018-03-09 16:59:50 +08002183 sortcmdlist();
Peter Xu47451462018-06-08 11:55:11 +08002184 qemu_mutex_init(&mon_fdsets_lock);
Peter Xu6adf08d2018-03-09 16:59:50 +08002185}