blob: 205487e2b9ed6f5841f5f0faa796a1b16a6d9271 [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 Bonzinib4a42f82013-02-04 11:37:52 +010027#include "monitor/qdev.h"
pbrook87ecb682007-11-17 17:14:51 +000028#include "hw/usb.h"
Michael S. Tsirkina2cb15b2012-12-12 14:24:50 +020029#include "hw/pci/pci.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010030#include "sysemu/watchdog.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +020031#include "hw/loader.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010032#include "exec/gdbstub.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020033#include "net/net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000034#include "net/slirp.h"
Gerd Hoffmann75721502010-10-07 12:22:54 +020035#include "ui/qemu-spice.h"
Marc-André Lureau213dcb02016-12-12 20:22:24 +030036#include "qemu/config-file.h"
Markus Armbruster856dfd82019-05-23 16:35:06 +020037#include "qemu/ctype.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010038#include "ui/console.h"
Gerd Hoffmannc751a742013-12-04 15:02:28 +010039#include "ui/input.h"
pbrook87ecb682007-11-17 17:14:51 +000040#include "audio/audio.h"
Paolo Bonzini76cad712012-10-24 11:12:21 +020041#include "disas/disas.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010042#include "sysemu/balloon.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010043#include "qemu/timer.h"
Philippe Mathieu-Daudécd617482022-02-07 09:27:56 +010044#include "qemu/log.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"
Paolo Bonzinibdee56f2013-04-02 18:28:41 +020051#include "sysemu/tpm.h"
Daniel Henrique Barbozabf353ad2022-09-26 14:38:40 -030052#include "sysemu/device_tree.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"
Paolo Bonzini22afb462021-10-27 15:03:04 +020074#include "qapi/qapi-commands-run-state.h"
Markus Armbruster00ca24f2019-11-20 19:25:48 +010075#include "qapi/qapi-commands-trace.h"
Daniel P. Berrangédd982342021-09-08 10:35:43 +010076#include "qapi/qapi-commands-machine.h"
Markus Armbruster00ca24f2019-11-20 19:25:48 +010077#include "qapi/qapi-init-commands.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010078#include "qapi/error.h"
Wenchao Xia43a14cf2014-06-18 08:43:31 +020079#include "qapi/qmp-event.h"
Paolo Bonzinid2528bd2017-03-03 12:01:16 +010080#include "sysemu/cpus.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020081#include "qemu/cutils.h"
ths6a5bd302007-12-03 17:05:38 +000082
Jason J. Hernea4538a52015-06-26 14:07:21 -040083#if defined(TARGET_S390X)
84#include "hw/s390x/storage-keys.h"
Claudio Imbrendaf860d492016-08-15 18:44:04 +020085#include "hw/s390x/storage-attributes.h"
Jason J. Hernea4538a52015-06-26 14:07:21 -040086#endif
87
Murilo Opsfelder Araujoc9f80042022-05-10 20:54:39 -030088/* Make devices configuration available for use in hmp-commands*.hx templates */
89#include CONFIG_DEVICES
90
Mark McLoughlinf07918f2009-07-22 09:11:40 +010091/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -050092typedef struct mon_fd_t mon_fd_t;
93struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +010094 char *name;
95 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -050096 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +010097};
98
Corey Bryantba1c0482012-08-14 16:43:43 -040099/* file descriptor associated with a file descriptor set */
100typedef struct MonFdsetFd MonFdsetFd;
101struct MonFdsetFd {
102 int fd;
103 bool removed;
104 char *opaque;
105 QLIST_ENTRY(MonFdsetFd) next;
106};
107
108/* file descriptor set containing fds passed via SCM_RIGHTS */
109typedef struct MonFdset MonFdset;
110struct MonFdset {
111 int64_t id;
112 QLIST_HEAD(, MonFdsetFd) fds;
Corey Bryantadb696f2012-08-14 16:43:47 -0400113 QLIST_HEAD(, MonFdsetFd) dup_fds;
Corey Bryantba1c0482012-08-14 16:43:43 -0400114 QLIST_ENTRY(MonFdset) next;
115};
116
Peter Xu47451462018-06-08 11:55:11 +0800117/* Protects mon_fdsets */
118static QemuMutex mon_fdsets_lock;
Paolo Bonzinib58deb32018-12-06 11:58:10 +0100119static QLIST_HEAD(, MonFdset) mon_fdsets;
Peter Xu47451462018-06-08 11:55:11 +0800120
Kevin Wolfa0cd5e12019-06-13 17:33:56 +0200121static HMPCommand hmp_info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000122
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200123char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
124 int64_t cpu_index, Error **errp)
Luiz Capitulino0268d972010-10-22 10:08:02 -0200125{
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200126 char *output = NULL;
Kevin Wolf5f9dba12019-06-13 17:33:54 +0200127 MonitorHMP hmp = {};
Luiz Capitulino0268d972010-10-22 10:08:02 -0200128
Kevin Wolf92082412019-06-13 17:34:03 +0200129 monitor_data_init(&hmp.common, false, true, false);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200130
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200131 if (has_cpu_index) {
Kevin Wolfdcba65f2020-10-05 17:58:42 +0200132 int ret = monitor_set_cpu(&hmp.common, cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200133 if (ret < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100134 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
135 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200136 goto out;
137 }
138 }
139
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100140 handle_hmp_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200141
Dr. David Alan Gilbert0210c3b2020-09-22 10:57:41 +0100142 WITH_QEMU_LOCK_GUARD(&hmp.common.mon_lock) {
Markus Armbruster20076f42020-12-11 18:11:34 +0100143 output = g_strdup(hmp.common.outbuf->str);
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 Armbruster6604e472021-10-28 12:25:17 +0200239 qmp_register_command(&qmp_commands, "device_add",
240 qmp_device_add, 0, 0);
Marc-André Lureau5032a162016-09-12 13:19:02 +0400241
Markus Armbruster635db182017-03-03 13:32:27 +0100242 QTAILQ_INIT(&qmp_cap_negotiation_commands);
243 qmp_register_command(&qmp_cap_negotiation_commands, "qmp_capabilities",
Markus Armbruster6604e472021-10-28 12:25:17 +0200244 qmp_marshal_qmp_capabilities,
245 QCO_ALLOW_PRECONFIG, 0);
Markus Armbruster635db182017-03-03 13:32:27 +0100246}
247
Peter Xud9f25282018-06-08 11:55:07 +0800248/* Set the current CPU defined by the user. Callers must hold BQL. */
Kevin Wolfdcba65f2020-10-05 17:58:42 +0200249int monitor_set_cpu(Monitor *mon, int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000250{
Andreas Färber55e5c282012-12-17 06:18:02 +0100251 CPUState *cpu;
bellard6a00d602005-11-21 23:25:50 +0000252
Andreas Färber1c8bb3c2013-02-15 17:01:09 +0100253 cpu = qemu_get_cpu(cpu_index);
254 if (cpu == NULL) {
255 return -1;
bellard6a00d602005-11-21 23:25:50 +0000256 }
Kevin Wolfdcba65f2020-10-05 17:58:42 +0200257 g_free(mon->mon_cpu_path);
258 mon->mon_cpu_path = object_get_canonical_path(OBJECT(cpu));
Andreas Färber1c8bb3c2013-02-15 17:01:09 +0100259 return 0;
bellard6a00d602005-11-21 23:25:50 +0000260}
261
Peter Xud9f25282018-06-08 11:55:07 +0800262/* Callers must hold BQL. */
Kevin Wolf87e6f4a2020-10-05 17:58:43 +0200263static CPUState *mon_get_cpu_sync(Monitor *mon, bool synchronize)
bellard6a00d602005-11-21 23:25:50 +0000264{
Marc-André Lureaua85d0bf2019-10-04 15:18:20 +0400265 CPUState *cpu = NULL;
Greg Kurz751f8cf2017-10-17 10:16:22 +0200266
Kevin Wolf87e6f4a2020-10-05 17:58:43 +0200267 if (mon->mon_cpu_path) {
268 cpu = (CPUState *) object_resolve_path_type(mon->mon_cpu_path,
Greg Kurz751f8cf2017-10-17 10:16:22 +0200269 TYPE_CPU, NULL);
270 if (!cpu) {
Kevin Wolf87e6f4a2020-10-05 17:58:43 +0200271 g_free(mon->mon_cpu_path);
272 mon->mon_cpu_path = NULL;
Greg Kurz751f8cf2017-10-17 10:16:22 +0200273 }
274 }
Kevin Wolf87e6f4a2020-10-05 17:58:43 +0200275 if (!mon->mon_cpu_path) {
Thomas Huth854e67f2017-01-13 13:12:35 +0100276 if (!first_cpu) {
277 return NULL;
278 }
Kevin Wolf87e6f4a2020-10-05 17:58:43 +0200279 monitor_set_cpu(mon, first_cpu->cpu_index);
Greg Kurz751f8cf2017-10-17 10:16:22 +0200280 cpu = first_cpu;
bellard6a00d602005-11-21 23:25:50 +0000281 }
Marc-André Lureaua85d0bf2019-10-04 15:18:20 +0400282 assert(cpu != NULL);
Viktor Mihajlovski137b5cb2018-02-16 17:08:41 +0100283 if (synchronize) {
284 cpu_synchronize_state(cpu);
285 }
Greg Kurz751f8cf2017-10-17 10:16:22 +0200286 return cpu;
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700287}
288
Kevin Wolf2fc5d012020-11-13 12:43:24 +0100289CPUState *mon_get_cpu(Monitor *mon)
Viktor Mihajlovski137b5cb2018-02-16 17:08:41 +0100290{
Kevin Wolf2fc5d012020-11-13 12:43:24 +0100291 return mon_get_cpu_sync(mon, true);
Viktor Mihajlovski137b5cb2018-02-16 17:08:41 +0100292}
293
Kevin Wolfe7cff9c2020-11-13 12:43:26 +0100294CPUArchState *mon_get_cpu_env(Monitor *mon)
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700295{
Kevin Wolfe7cff9c2020-11-13 12:43:26 +0100296 CPUState *cs = mon_get_cpu(mon);
Thomas Huth854e67f2017-01-13 13:12:35 +0100297
298 return cs ? cs->env_ptr : NULL;
bellard6a00d602005-11-21 23:25:50 +0000299}
300
Kevin Wolf87e6f4a2020-10-05 17:58:43 +0200301int monitor_get_cpu_index(Monitor *mon)
Luiz Capitulino99b77962011-10-24 10:53:44 -0200302{
Kevin Wolf87e6f4a2020-10-05 17:58:43 +0200303 CPUState *cs = mon_get_cpu_sync(mon, false);
Thomas Huth854e67f2017-01-13 13:12:35 +0100304
305 return cs ? cs->cpu_index : UNASSIGNED_CPU_INDEX;
Luiz Capitulino99b77962011-10-24 10:53:44 -0200306}
307
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100308static void hmp_info_registers(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000309{
Suraj Jitindar Singh18f08282017-06-08 15:41:16 +1000310 bool all_cpus = qdict_get_try_bool(qdict, "cpustate_all", false);
zhenwei pi00d60cf2022-08-02 15:37:20 +0800311 int vcpu = qdict_get_try_int(qdict, "vcpu", -1);
Suraj Jitindar Singh18f08282017-06-08 15:41:16 +1000312 CPUState *cs;
Thomas Huth854e67f2017-01-13 13:12:35 +0100313
Suraj Jitindar Singh18f08282017-06-08 15:41:16 +1000314 if (all_cpus) {
315 CPU_FOREACH(cs) {
316 monitor_printf(mon, "\nCPU#%d\n", cs->cpu_index);
Markus Armbruster90c84c52019-04-17 21:18:02 +0200317 cpu_dump_state(cs, NULL, CPU_DUMP_FPU);
Suraj Jitindar Singh18f08282017-06-08 15:41:16 +1000318 }
319 } else {
zhenwei pi00d60cf2022-08-02 15:37:20 +0800320 cs = vcpu >= 0 ? qemu_get_cpu(vcpu) : mon_get_cpu(mon);
Suraj Jitindar Singh18f08282017-06-08 15:41:16 +1000321
322 if (!cs) {
zhenwei pi00d60cf2022-08-02 15:37:20 +0800323 if (vcpu >= 0) {
324 monitor_printf(mon, "CPU#%d not available\n", vcpu);
325 } else {
326 monitor_printf(mon, "No CPU available\n");
327 }
Suraj Jitindar Singh18f08282017-06-08 15:41:16 +1000328 return;
329 }
330
zhenwei pi00d60cf2022-08-02 15:37:20 +0800331 monitor_printf(mon, "\nCPU#%d\n", cs->cpu_index);
Markus Armbruster90c84c52019-04-17 21:18:02 +0200332 cpu_dump_state(cs, NULL, CPU_DUMP_FPU);
Thomas Huth854e67f2017-01-13 13:12:35 +0100333 }
bellard9307c4c2004-04-04 12:57:25 +0000334}
335
Emilio G. Cota97bfafe2017-08-08 13:54:42 -0400336static void hmp_info_sync_profile(Monitor *mon, const QDict *qdict)
337{
338 int64_t max = qdict_get_try_int(qdict, "max", 10);
339 bool mean = qdict_get_try_bool(qdict, "mean", false);
340 bool coalesce = !qdict_get_try_bool(qdict, "no_coalesce", false);
341 enum QSPSortBy sort_by;
342
343 sort_by = mean ? QSP_SORT_BY_AVG_WAIT_TIME : QSP_SORT_BY_TOTAL_WAIT_TIME;
Markus Armbrusterac7ff4c2019-04-17 21:17:54 +0200344 qsp_report(max, sort_by, coalesce);
Emilio G. Cota97bfafe2017-08-08 13:54:42 -0400345}
346
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100347static void hmp_info_history(Monitor *mon, const QDict *qdict)
bellardaa455482004-04-04 13:07:25 +0000348{
Kevin Wolf5f9dba12019-06-13 17:33:54 +0200349 MonitorHMP *hmp_mon = container_of(mon, MonitorHMP, common);
bellardaa455482004-04-04 13:07:25 +0000350 int i;
bellard7e2515e2004-08-01 21:52:19 +0000351 const char *str;
ths3b46e622007-09-17 08:09:54 +0000352
Kevin Wolf5f9dba12019-06-13 17:33:54 +0200353 if (!hmp_mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000354 return;
Kevin Wolf5f9dba12019-06-13 17:33:54 +0200355 }
bellard7e2515e2004-08-01 21:52:19 +0000356 i = 0;
357 for(;;) {
Kevin Wolf5f9dba12019-06-13 17:33:54 +0200358 str = readline_get_history(hmp_mon->rs, i);
359 if (!str) {
bellard7e2515e2004-08-01 21:52:19 +0000360 break;
Kevin Wolf5f9dba12019-06-13 17:33:54 +0200361 }
aliguori376253e2009-03-05 23:01:23 +0000362 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000363 i++;
bellardaa455482004-04-04 13:07:25 +0000364 }
365}
366
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100367static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530368{
Lluís Vilanovabd712112016-07-11 12:53:51 +0200369 const char *name = qdict_get_try_str(qdict, "name");
Lluís Vilanova77e2b172016-07-11 12:53:57 +0200370 bool has_vcpu = qdict_haskey(qdict, "vcpu");
371 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
Lluís Vilanovabd712112016-07-11 12:53:51 +0200372 TraceEventInfoList *events;
Lluís Vilanova14101d02014-08-25 13:20:03 +0200373 TraceEventInfoList *elem;
Lluís Vilanovabd712112016-07-11 12:53:51 +0200374 Error *local_err = NULL;
375
376 if (name == NULL) {
377 name = "*";
378 }
Lluís Vilanova77e2b172016-07-11 12:53:57 +0200379 if (vcpu < 0) {
380 monitor_printf(mon, "argument vcpu must be positive");
381 return;
382 }
Lluís Vilanovabd712112016-07-11 12:53:51 +0200383
Lluís Vilanova77e2b172016-07-11 12:53:57 +0200384 events = qmp_trace_event_get_state(name, has_vcpu, vcpu, &local_err);
Lluís Vilanovabd712112016-07-11 12:53:51 +0200385 if (local_err) {
386 error_report_err(local_err);
387 return;
388 }
Lluís Vilanova14101d02014-08-25 13:20:03 +0200389
390 for (elem = events; elem != NULL; elem = elem->next) {
391 monitor_printf(mon, "%s : state %u\n",
392 elem->value->name,
393 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
394 }
395 qapi_free_TraceEventInfoList(events);
Prerna Saxena22890ab2010-06-24 17:04:53 +0530396}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530397
Markus Armbrusterb8a185b2015-03-05 17:29:02 +0100398void qmp_client_migrate_info(const char *protocol, const char *hostname,
399 bool has_port, int64_t port,
400 bool has_tls_port, int64_t tls_port,
401 bool has_cert_subject, const char *cert_subject,
402 Error **errp)
Gerd Hoffmanne866e232010-04-23 13:28:21 +0200403{
Gerd Hoffmanne866e232010-04-23 13:28:21 +0200404 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb8a185b2015-03-05 17:29:02 +0100405 if (!qemu_using_spice(errp)) {
406 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +0200407 }
408
Markus Armbrusterb8a185b2015-03-05 17:29:02 +0100409 if (!has_port && !has_tls_port) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100410 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
Markus Armbrusterb8a185b2015-03-05 17:29:02 +0100411 return;
Yonit Halperin6ec5dae2012-03-18 09:42:39 +0200412 }
413
Gerd Hoffmann74774772020-10-19 09:52:12 +0200414 if (qemu_spice.migrate_info(hostname,
Markus Armbrusterb8a185b2015-03-05 17:29:02 +0100415 has_port ? port : -1,
416 has_tls_port ? tls_port : -1,
417 cert_subject)) {
Markus Armbruster9e1b9c62020-11-13 09:26:21 +0100418 error_setg(errp, "Could not set up display for migration");
Markus Armbrusterb8a185b2015-03-05 17:29:02 +0100419 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +0200420 }
Markus Armbrusterb8a185b2015-03-05 17:29:02 +0100421 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +0200422 }
423
Markus Armbruster99750d82020-11-13 09:26:22 +0100424 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "'spice'");
Gerd Hoffmanne866e232010-04-23 13:28:21 +0200425}
426
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100427static void hmp_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +0000428{
Markus Armbrusterdaa76aa2016-06-15 19:27:16 +0200429 Error *err = NULL;
430
Richard Hendersone2c7c6a2022-04-17 11:29:43 -0700431 if (!qemu_set_log_filename(qdict_get_str(qdict, "filename"), &err)) {
Markus Armbrusterdaa76aa2016-06-15 19:27:16 +0200432 error_report_err(err);
433 }
pbrooke735b912007-06-30 13:53:24 +0000434}
435
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100436static void hmp_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +0000437{
438 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300439 const char *items = qdict_get_str(qdict, "items");
Richard Hendersonc5955f42022-04-17 11:29:44 -0700440 Error *err = NULL;
ths3b46e622007-09-17 08:09:54 +0000441
bellard9307c4c2004-04-04 12:57:25 +0000442 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +0000443 mask = 0;
444 } else {
Peter Maydell4fde1eb2013-02-11 16:41:22 +0000445 mask = qemu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +0000446 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +0000447 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +0000448 return;
449 }
450 }
Richard Hendersonc5955f42022-04-17 11:29:44 -0700451
452 if (!qemu_set_log(mask, &err)) {
453 error_report_err(err);
454 }
bellardf193c792004-03-21 17:06:25 +0000455}
456
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100457static void hmp_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +0000458{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300459 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +0000460 if (!option || !strcmp(option, "on")) {
461 singlestep = 1;
462 } else if (!strcmp(option, "off")) {
463 singlestep = 0;
464 } else {
465 monitor_printf(mon, "unexpected option %s\n", option);
466 }
467}
468
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100469static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +0000470{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300471 const char *device = qdict_get_try_str(qdict, "device");
Yutao Ai89854b92020-11-25 01:45:13 +0000472 if (!device) {
aliguori59030a82009-04-05 18:43:41 +0000473 device = "tcp::" DEFAULT_GDBSTUB_PORT;
Yutao Ai89854b92020-11-25 01:45:13 +0000474 }
475
aliguori59030a82009-04-05 18:43:41 +0000476 if (gdbserver_start(device) < 0) {
477 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
478 device);
479 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +0000480 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +0000481 } else {
aliguori59030a82009-04-05 18:43:41 +0000482 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
483 device);
bellard8a7ddc32004-03-31 19:00:16 +0000484 }
485}
486
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100487static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100488{
Paolo Bonzini22afb462021-10-27 15:03:04 +0200489 Error *err = NULL;
490 WatchdogAction action;
491 char *qapi_value;
492
493 qapi_value = g_ascii_strdown(qdict_get_str(qdict, "action"), -1);
494 action = qapi_enum_parse(&WatchdogAction_lookup, qapi_value, -1, &err);
495 g_free(qapi_value);
496 if (err) {
497 hmp_handle_error(mon, err);
498 return;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100499 }
Paolo Bonzini22afb462021-10-27 15:03:04 +0200500 qmp_watchdog_set_action(action, &error_abort);
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100501}
502
aliguori376253e2009-03-05 23:01:23 +0000503static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +0000504{
aliguori376253e2009-03-05 23:01:23 +0000505 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000506 switch(c) {
507 case '\'':
aliguori376253e2009-03-05 23:01:23 +0000508 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +0000509 break;
510 case '\\':
aliguori376253e2009-03-05 23:01:23 +0000511 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +0000512 break;
513 case '\n':
aliguori376253e2009-03-05 23:01:23 +0000514 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +0000515 break;
516 case '\r':
aliguori376253e2009-03-05 23:01:23 +0000517 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +0000518 break;
519 default:
520 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +0000521 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +0000522 } else {
aliguori376253e2009-03-05 23:01:23 +0000523 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +0000524 }
525 break;
526 }
aliguori376253e2009-03-05 23:01:23 +0000527 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000528}
529
aliguori376253e2009-03-05 23:01:23 +0000530static void memory_dump(Monitor *mon, int count, int format, int wsize,
Avi Kivitya8170e52012-10-23 12:30:10 +0200531 hwaddr addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +0000532{
Blue Swirl23842aa2010-01-12 20:27:43 +0000533 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +0000534 uint8_t buf[16];
535 uint64_t v;
Kevin Wolf2fc5d012020-11-13 12:43:24 +0100536 CPUState *cs = mon_get_cpu(mon);
Thomas Huth854e67f2017-01-13 13:12:35 +0100537
538 if (!cs && (format == 'i' || !is_physical)) {
539 monitor_printf(mon, "Can not dump without CPU\n");
540 return;
541 }
bellard9307c4c2004-04-04 12:57:25 +0000542
543 if (format == 'i') {
Richard Henderson1d484742017-09-14 08:38:35 -0700544 monitor_disas(mon, cs, addr, count, is_physical);
bellard9307c4c2004-04-04 12:57:25 +0000545 return;
546 }
547
548 len = wsize * count;
Yutao Ai89854b92020-11-25 01:45:13 +0000549 if (wsize == 1) {
bellard9307c4c2004-04-04 12:57:25 +0000550 line_size = 8;
Yutao Ai89854b92020-11-25 01:45:13 +0000551 } else {
bellard9307c4c2004-04-04 12:57:25 +0000552 line_size = 16;
Yutao Ai89854b92020-11-25 01:45:13 +0000553 }
bellard9307c4c2004-04-04 12:57:25 +0000554 max_digits = 0;
555
556 switch(format) {
557 case 'o':
Marc-André Lureau69db8df2017-06-22 13:04:16 +0200558 max_digits = DIV_ROUND_UP(wsize * 8, 3);
bellard9307c4c2004-04-04 12:57:25 +0000559 break;
560 default:
561 case 'x':
562 max_digits = (wsize * 8) / 4;
563 break;
564 case 'u':
565 case 'd':
Marc-André Lureau69db8df2017-06-22 13:04:16 +0200566 max_digits = DIV_ROUND_UP(wsize * 8 * 10, 33);
bellard9307c4c2004-04-04 12:57:25 +0000567 break;
568 case 'c':
569 wsize = 1;
570 break;
571 }
572
573 while (len > 0) {
Yutao Ai89854b92020-11-25 01:45:13 +0000574 if (is_physical) {
aliguori376253e2009-03-05 23:01:23 +0000575 monitor_printf(mon, TARGET_FMT_plx ":", addr);
Yutao Ai89854b92020-11-25 01:45:13 +0000576 } else {
aliguori376253e2009-03-05 23:01:23 +0000577 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
Yutao Ai89854b92020-11-25 01:45:13 +0000578 }
bellard9307c4c2004-04-04 12:57:25 +0000579 l = len;
580 if (l > line_size)
581 l = line_size;
582 if (is_physical) {
Peter Maydell6f89ae52018-12-14 13:30:49 +0000583 AddressSpace *as = cs ? cs->as : &address_space_memory;
584 MemTxResult r = address_space_read(as, addr,
585 MEMTXATTRS_UNSPECIFIED, buf, l);
586 if (r != MEMTX_OK) {
587 monitor_printf(mon, " Cannot access memory\n");
588 break;
589 }
bellard9307c4c2004-04-04 12:57:25 +0000590 } else {
Thomas Huth854e67f2017-01-13 13:12:35 +0100591 if (cpu_memory_rw_debug(cs, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +0000592 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +0000593 break;
594 }
bellard9307c4c2004-04-04 12:57:25 +0000595 }
ths5fafdf22007-09-16 21:08:06 +0000596 i = 0;
bellard9307c4c2004-04-04 12:57:25 +0000597 while (i < l) {
598 switch(wsize) {
599 default:
600 case 1:
Peter Maydell24e60302015-01-20 15:19:32 +0000601 v = ldub_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000602 break;
603 case 2:
Peter Maydell24e60302015-01-20 15:19:32 +0000604 v = lduw_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000605 break;
606 case 4:
Peter Maydell24e60302015-01-20 15:19:32 +0000607 v = (uint32_t)ldl_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000608 break;
609 case 8:
Peter Maydell24e60302015-01-20 15:19:32 +0000610 v = ldq_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000611 break;
612 }
aliguori376253e2009-03-05 23:01:23 +0000613 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +0000614 switch(format) {
615 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000616 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000617 break;
618 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000619 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000620 break;
621 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000622 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000623 break;
624 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000625 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000626 break;
627 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000628 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +0000629 break;
630 }
631 i += wsize;
632 }
aliguori376253e2009-03-05 23:01:23 +0000633 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000634 addr += l;
635 len -= l;
636 }
637}
638
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100639static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000640{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300641 int count = qdict_get_int(qdict, "count");
642 int format = qdict_get_int(qdict, "format");
643 int size = qdict_get_int(qdict, "size");
644 target_long addr = qdict_get_int(qdict, "addr");
645
aliguori376253e2009-03-05 23:01:23 +0000646 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +0000647}
648
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100649static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000650{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300651 int count = qdict_get_int(qdict, "count");
652 int format = qdict_get_int(qdict, "format");
653 int size = qdict_get_int(qdict, "size");
Avi Kivitya8170e52012-10-23 12:30:10 +0200654 hwaddr addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300655
aliguori376253e2009-03-05 23:01:23 +0000656 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +0000657}
658
Tobin Feldman-Fitzthumc7f7e692020-10-27 13:03:03 -0400659void *gpa2hva(MemoryRegion **p_mr, hwaddr addr, uint64_t size, Error **errp)
Paolo Bonzinie9628442017-04-20 15:30:58 +0200660{
Tobin Feldman-Fitzthumc7f7e692020-10-27 13:03:03 -0400661 Int128 gpa_region_size;
Paolo Bonzinie9628442017-04-20 15:30:58 +0200662 MemoryRegionSection mrs = memory_region_find(get_system_memory(),
Tobin Feldman-Fitzthumc7f7e692020-10-27 13:03:03 -0400663 addr, size);
Paolo Bonzinie9628442017-04-20 15:30:58 +0200664
665 if (!mrs.mr) {
666 error_setg(errp, "No memory is mapped at address 0x%" HWADDR_PRIx, addr);
667 return NULL;
668 }
669
670 if (!memory_region_is_ram(mrs.mr) && !memory_region_is_romd(mrs.mr)) {
671 error_setg(errp, "Memory at address 0x%" HWADDR_PRIx "is not RAM", addr);
672 memory_region_unref(mrs.mr);
673 return NULL;
674 }
675
Tobin Feldman-Fitzthumc7f7e692020-10-27 13:03:03 -0400676 gpa_region_size = int128_make64(size);
677 if (int128_lt(mrs.size, gpa_region_size)) {
678 error_setg(errp, "Size of memory region at 0x%" HWADDR_PRIx
679 " exceeded.", addr);
680 memory_region_unref(mrs.mr);
681 return NULL;
682 }
683
Paolo Bonzinie9628442017-04-20 15:30:58 +0200684 *p_mr = mrs.mr;
685 return qemu_map_ram_ptr(mrs.mr->ram_block, mrs.offset_within_region);
686}
687
688static void hmp_gpa2hva(Monitor *mon, const QDict *qdict)
689{
690 hwaddr addr = qdict_get_int(qdict, "addr");
691 Error *local_err = NULL;
692 MemoryRegion *mr = NULL;
693 void *ptr;
694
Tobin Feldman-Fitzthumc7f7e692020-10-27 13:03:03 -0400695 ptr = gpa2hva(&mr, addr, 1, &local_err);
Paolo Bonzinie9628442017-04-20 15:30:58 +0200696 if (local_err) {
697 error_report_err(local_err);
698 return;
699 }
700
701 monitor_printf(mon, "Host virtual address for 0x%" HWADDR_PRIx
702 " (%s) is %p\n",
703 addr, mr->name, ptr);
704
705 memory_region_unref(mr);
706}
707
Dr. David Alan Gilbert574d9692019-04-12 16:26:52 +0100708static void hmp_gva2gpa(Monitor *mon, const QDict *qdict)
709{
710 target_ulong addr = qdict_get_int(qdict, "addr");
711 MemTxAttrs attrs;
Kevin Wolf2fc5d012020-11-13 12:43:24 +0100712 CPUState *cs = mon_get_cpu(mon);
Dr. David Alan Gilbert574d9692019-04-12 16:26:52 +0100713 hwaddr gpa;
714
715 if (!cs) {
716 monitor_printf(mon, "No cpu\n");
717 return;
718 }
719
Eduardo Habkost9d3250d2019-05-10 15:56:20 -0300720 gpa = cpu_get_phys_page_attrs_debug(cs, addr & TARGET_PAGE_MASK, &attrs);
Dr. David Alan Gilbert574d9692019-04-12 16:26:52 +0100721 if (gpa == -1) {
722 monitor_printf(mon, "Unmapped\n");
723 } else {
724 monitor_printf(mon, "gpa: %#" HWADDR_PRIx "\n",
725 gpa + (addr & ~TARGET_PAGE_MASK));
726 }
727}
728
Paolo Bonzinie9628442017-04-20 15:30:58 +0200729#ifdef CONFIG_LINUX
730static uint64_t vtop(void *ptr, Error **errp)
731{
732 uint64_t pinfo;
733 uint64_t ret = -1;
734 uintptr_t addr = (uintptr_t) ptr;
Marc-André Lureau8e3b0cb2022-03-23 19:57:22 +0400735 uintptr_t pagesize = qemu_real_host_page_size();
Paolo Bonzinie9628442017-04-20 15:30:58 +0200736 off_t offset = addr / pagesize * sizeof(pinfo);
737 int fd;
738
739 fd = open("/proc/self/pagemap", O_RDONLY);
740 if (fd == -1) {
741 error_setg_errno(errp, errno, "Cannot open /proc/self/pagemap");
742 return -1;
743 }
744
745 /* Force copy-on-write if necessary. */
Stefan Hajnoczid73415a2020-09-23 11:56:46 +0100746 qatomic_add((uint8_t *)ptr, 0);
Paolo Bonzinie9628442017-04-20 15:30:58 +0200747
748 if (pread(fd, &pinfo, sizeof(pinfo), offset) != sizeof(pinfo)) {
749 error_setg_errno(errp, errno, "Cannot read pagemap");
750 goto out;
751 }
752 if ((pinfo & (1ull << 63)) == 0) {
753 error_setg(errp, "Page not present");
754 goto out;
755 }
756 ret = ((pinfo & 0x007fffffffffffffull) * pagesize) | (addr & (pagesize - 1));
757
758out:
759 close(fd);
760 return ret;
761}
762
763static void hmp_gpa2hpa(Monitor *mon, const QDict *qdict)
764{
765 hwaddr addr = qdict_get_int(qdict, "addr");
766 Error *local_err = NULL;
767 MemoryRegion *mr = NULL;
768 void *ptr;
769 uint64_t physaddr;
770
Tobin Feldman-Fitzthumc7f7e692020-10-27 13:03:03 -0400771 ptr = gpa2hva(&mr, addr, 1, &local_err);
Paolo Bonzinie9628442017-04-20 15:30:58 +0200772 if (local_err) {
773 error_report_err(local_err);
774 return;
775 }
776
777 physaddr = vtop(ptr, &local_err);
778 if (local_err) {
779 error_report_err(local_err);
780 } else {
781 monitor_printf(mon, "Host physical address for 0x%" HWADDR_PRIx
782 " (%s) is 0x%" PRIx64 "\n",
783 addr, mr->name, (uint64_t) physaddr);
784 }
785
786 memory_region_unref(mr);
787}
788#endif
789
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300790static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000791{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300792 int format = qdict_get_int(qdict, "format");
Avi Kivitya8170e52012-10-23 12:30:10 +0200793 hwaddr val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300794
bellard9307c4c2004-04-04 12:57:25 +0000795 switch(format) {
796 case 'o':
Avi Kivitya8170e52012-10-23 12:30:10 +0200797 monitor_printf(mon, "%#" HWADDR_PRIo, val);
bellard9307c4c2004-04-04 12:57:25 +0000798 break;
799 case 'x':
Avi Kivitya8170e52012-10-23 12:30:10 +0200800 monitor_printf(mon, "%#" HWADDR_PRIx, val);
bellard9307c4c2004-04-04 12:57:25 +0000801 break;
802 case 'u':
Avi Kivitya8170e52012-10-23 12:30:10 +0200803 monitor_printf(mon, "%" HWADDR_PRIu, val);
bellard9307c4c2004-04-04 12:57:25 +0000804 break;
805 default:
806 case 'd':
Avi Kivitya8170e52012-10-23 12:30:10 +0200807 monitor_printf(mon, "%" HWADDR_PRId, val);
bellard9307c4c2004-04-04 12:57:25 +0000808 break;
809 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000810 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +0000811 break;
812 }
aliguori376253e2009-03-05 23:01:23 +0000813 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000814}
815
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100816static void hmp_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +0000817{
818 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +0000819 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300820 uint32_t start = qdict_get_int(qdict, "start");
821 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +0000822
823 sum = 0;
824 for(addr = start; addr < (start + size); addr++) {
Peter Maydell42874d32015-04-26 16:49:24 +0100825 uint8_t val = address_space_ldub(&address_space_memory, addr,
826 MEMTXATTRS_UNSPECIFIED, NULL);
bellarde4cf1ad2005-06-04 20:15:57 +0000827 /* BSD sum algorithm ('sum' Unix command) */
828 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +0200829 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +0000830 }
aliguori376253e2009-03-05 23:01:23 +0000831 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +0000832}
833
bellard13224a82006-07-14 22:03:35 +0000834static int mouse_button_state;
835
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100836static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +0000837{
Gerd Hoffmannc751a742013-12-04 15:02:28 +0100838 int dx, dy, dz, button;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300839 const char *dx_str = qdict_get_str(qdict, "dx_str");
840 const char *dy_str = qdict_get_str(qdict, "dy_str");
841 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
Gerd Hoffmannc751a742013-12-04 15:02:28 +0100842
bellard13224a82006-07-14 22:03:35 +0000843 dx = strtol(dx_str, NULL, 0);
844 dy = strtol(dy_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +0100845 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
846 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
847
848 if (dz_str) {
bellard13224a82006-07-14 22:03:35 +0000849 dz = strtol(dz_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +0100850 if (dz != 0) {
Gerd Hoffmannf22d0af2016-01-12 12:14:12 +0100851 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
Gerd Hoffmannc751a742013-12-04 15:02:28 +0100852 qemu_input_queue_btn(NULL, button, true);
853 qemu_input_event_sync();
854 qemu_input_queue_btn(NULL, button, false);
855 }
856 }
857 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +0000858}
859
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100860static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +0000861{
Eric Blake7fb1cf12015-11-18 01:52:57 -0700862 static uint32_t bmap[INPUT_BUTTON__MAX] = {
Gerd Hoffmannc751a742013-12-04 15:02:28 +0100863 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
864 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
865 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
866 };
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300867 int button_state = qdict_get_int(qdict, "button_state");
Gerd Hoffmannc751a742013-12-04 15:02:28 +0100868
869 if (mouse_button_state == button_state) {
870 return;
871 }
872 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
873 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +0000874 mouse_button_state = button_state;
bellard13224a82006-07-14 22:03:35 +0000875}
876
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100877static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +0000878{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -0300879 int size = qdict_get_int(qdict, "size");
880 int addr = qdict_get_int(qdict, "addr");
881 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +0000882 uint32_t val;
883 int suffix;
884
885 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -0300886 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +0000887 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +0000888 addr++;
889 }
890 addr &= 0xffff;
891
892 switch(size) {
893 default:
894 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +0000895 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +0000896 suffix = 'b';
897 break;
898 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +0000899 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +0000900 suffix = 'w';
901 break;
902 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +0000903 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +0000904 suffix = 'l';
905 break;
906 }
Yutao Ai33b1fa92020-11-25 01:45:14 +0000907 monitor_printf(mon, "port%c[0x%04x] = 0x%0*x\n",
aliguori376253e2009-03-05 23:01:23 +0000908 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +0000909}
bellarda3a91a32004-06-04 11:06:21 +0000910
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100911static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +0200912{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300913 int size = qdict_get_int(qdict, "size");
914 int addr = qdict_get_int(qdict, "addr");
915 int val = qdict_get_int(qdict, "val");
916
Jan Kiszkaf1147842009-07-14 10:20:11 +0200917 addr &= IOPORTS_MASK;
918
919 switch (size) {
920 default:
921 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +0000922 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +0200923 break;
924 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +0000925 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +0200926 break;
927 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +0000928 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +0200929 break;
930 }
931}
932
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100933static void hmp_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +0000934{
Gongleif1839932014-12-03 18:20:58 +0000935 Error *local_err = NULL;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300936 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +0000937
Gongleif1839932014-12-03 18:20:58 +0000938 qemu_boot_set(bootdevice, &local_err);
939 if (local_err) {
Markus Armbruster193227f2015-12-18 16:35:06 +0100940 error_report_err(local_err);
aurel320ecdffb2008-05-04 20:11:34 +0000941 } else {
Gongleif1839932014-12-03 18:20:58 +0000942 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +0000943 }
944}
945
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100946static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
Blue Swirl314e2982011-09-11 20:22:05 +0000947{
Peter Xu57bb40c2017-01-16 16:40:05 +0800948 bool flatview = qdict_get_try_bool(qdict, "flatview", false);
Alexey Kardashevskiy5e8fd942017-09-21 18:51:06 +1000949 bool dispatch_tree = qdict_get_try_bool(qdict, "dispatch_tree", false);
Alexey Kardashevskiyfc051ae2018-06-04 13:25:11 +1000950 bool owner = qdict_get_try_bool(qdict, "owner", false);
Philippe Mathieu-Daudé2261d392020-05-29 14:53:25 +0200951 bool disabled = qdict_get_try_bool(qdict, "disabled", false);
Peter Xu57bb40c2017-01-16 16:40:05 +0800952
Philippe Mathieu-Daudé2261d392020-05-29 14:53:25 +0200953 mtree_info(flatview, dispatch_tree, owner, disabled);
Blue Swirl314e2982011-09-11 20:22:05 +0000954}
955
bellardec36b692006-07-16 18:57:03 +0000956/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +0000957static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +0000958
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100959static void hmp_info_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +0000960{
961 int i;
962 CaptureState *s;
963
964 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +0000965 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +0000966 s->ops.info (s->opaque);
967 }
968}
969
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100970static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +0000971{
972 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300973 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +0000974 CaptureState *s;
975
976 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
977 if (i == n) {
978 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +0000979 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -0500980 g_free (s);
bellardec36b692006-07-16 18:57:03 +0000981 return;
982 }
983 }
984}
985
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100986static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +0000987{
Luiz Capitulinoc1925482009-08-28 15:27:19 -0300988 const char *path = qdict_get_str(qdict, "path");
Kővágó, Zoltánf0b9f362019-08-19 01:06:48 +0200989 int freq = qdict_get_try_int(qdict, "freq", 44100);
990 int bits = qdict_get_try_int(qdict, "bits", 16);
991 int nchannels = qdict_get_try_int(qdict, "nchannels", 2);
992 const char *audiodev = qdict_get_str(qdict, "audiodev");
bellardec36b692006-07-16 18:57:03 +0000993 CaptureState *s;
Kővágó, Zoltánf0b9f362019-08-19 01:06:48 +0200994 AudioState *as = audio_state_by_name(audiodev);
995
996 if (!as) {
997 monitor_printf(mon, "Audiodev '%s' not found\n", audiodev);
998 return;
999 }
bellardec36b692006-07-16 18:57:03 +00001000
Anthony Liguori7267c092011-08-20 22:09:37 -05001001 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001002
Kővágó, Zoltánf0b9f362019-08-19 01:06:48 +02001003 if (wav_start_capture(as, s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001004 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05001005 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001006 return;
bellardec36b692006-07-16 18:57:03 +00001007 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001008 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001009}
bellardec36b692006-07-16 18:57:03 +00001010
Corey Bryant208c9d12012-06-22 14:36:09 -04001011void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001012{
Kevin Wolf947e4742020-10-05 17:58:44 +02001013 Monitor *cur_mon = monitor_cur();
Anthony Liguoric227f092009-10-01 16:12:16 -05001014 mon_fd_t *monfd;
Peter Xu9409fc02018-06-08 11:55:06 +08001015 int fd, tmp_fd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001016
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03001017 fd = qemu_chr_fe_get_msgfd(&cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001018 if (fd == -1) {
Markus Armbrusterf820af82020-11-13 09:26:18 +01001019 error_setg(errp, "No file descriptor supplied via SCM_RIGHTS");
Corey Bryant208c9d12012-06-22 14:36:09 -04001020 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001021 }
1022
1023 if (qemu_isdigit(fdname[0])) {
Stefan Hajnoczi0b9f0e22014-04-24 13:58:18 +02001024 close(fd);
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001025 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
1026 "a name not starting with a digit");
Corey Bryant208c9d12012-06-22 14:36:09 -04001027 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001028 }
1029
Dr. David Alan Gilbert0210c3b2020-09-22 10:57:41 +01001030 QEMU_LOCK_GUARD(&cur_mon->mon_lock);
Corey Bryant208c9d12012-06-22 14:36:09 -04001031 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001032 if (strcmp(monfd->name, fdname) != 0) {
1033 continue;
1034 }
1035
Peter Xu9409fc02018-06-08 11:55:06 +08001036 tmp_fd = monfd->fd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001037 monfd->fd = fd;
Markus Armbruster774a6b62018-07-03 10:53:57 +02001038 /* Make sure close() is outside critical section */
Peter Xu9409fc02018-06-08 11:55:06 +08001039 close(tmp_fd);
Corey Bryant208c9d12012-06-22 14:36:09 -04001040 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001041 }
1042
Markus Armbrusterb21e2382022-03-15 15:41:56 +01001043 monfd = g_new0(mon_fd_t, 1);
Anthony Liguori7267c092011-08-20 22:09:37 -05001044 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001045 monfd->fd = fd;
1046
Corey Bryant208c9d12012-06-22 14:36:09 -04001047 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001048}
1049
Corey Bryant208c9d12012-06-22 14:36:09 -04001050void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001051{
Kevin Wolf947e4742020-10-05 17:58:44 +02001052 Monitor *cur_mon = monitor_cur();
Anthony Liguoric227f092009-10-01 16:12:16 -05001053 mon_fd_t *monfd;
Peter Xu9409fc02018-06-08 11:55:06 +08001054 int tmp_fd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001055
Peter Xu9409fc02018-06-08 11:55:06 +08001056 qemu_mutex_lock(&cur_mon->mon_lock);
Corey Bryant208c9d12012-06-22 14:36:09 -04001057 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001058 if (strcmp(monfd->name, fdname) != 0) {
1059 continue;
1060 }
1061
Blue Swirl72cf2d42009-09-12 07:36:22 +00001062 QLIST_REMOVE(monfd, next);
Peter Xu9409fc02018-06-08 11:55:06 +08001063 tmp_fd = monfd->fd;
Anthony Liguori7267c092011-08-20 22:09:37 -05001064 g_free(monfd->name);
1065 g_free(monfd);
Peter Xu9409fc02018-06-08 11:55:06 +08001066 qemu_mutex_unlock(&cur_mon->mon_lock);
Markus Armbruster774a6b62018-07-03 10:53:57 +02001067 /* Make sure close() is outside critical section */
Peter Xu9409fc02018-06-08 11:55:06 +08001068 close(tmp_fd);
Corey Bryant208c9d12012-06-22 14:36:09 -04001069 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001070 }
1071
Peter Xu9409fc02018-06-08 11:55:06 +08001072 qemu_mutex_unlock(&cur_mon->mon_lock);
Markus Armbrusterf820af82020-11-13 09:26:18 +01001073 error_setg(errp, "File descriptor named '%s' not found", fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001074}
1075
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001076int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
Mark McLoughlin7768e042009-07-22 09:11:41 +01001077{
Anthony Liguoric227f092009-10-01 16:12:16 -05001078 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01001079
Dr. David Alan Gilbert0210c3b2020-09-22 10:57:41 +01001080 QEMU_LOCK_GUARD(&mon->mon_lock);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001081 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01001082 int fd;
1083
1084 if (strcmp(monfd->name, fdname) != 0) {
1085 continue;
1086 }
1087
1088 fd = monfd->fd;
1089
1090 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001091 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05001092 g_free(monfd->name);
1093 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001094
1095 return fd;
1096 }
1097
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001098 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001099 return -1;
1100}
1101
Corey Bryantba1c0482012-08-14 16:43:43 -04001102static void monitor_fdset_cleanup(MonFdset *mon_fdset)
1103{
1104 MonFdsetFd *mon_fdset_fd;
1105 MonFdsetFd *mon_fdset_fd_next;
1106
1107 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantebe52b52012-10-18 15:19:33 -04001108 if ((mon_fdset_fd->removed ||
1109 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
1110 runstate_is_running()) {
Corey Bryantba1c0482012-08-14 16:43:43 -04001111 close(mon_fdset_fd->fd);
1112 g_free(mon_fdset_fd->opaque);
1113 QLIST_REMOVE(mon_fdset_fd, next);
1114 g_free(mon_fdset_fd);
1115 }
1116 }
1117
Corey Bryantadb696f2012-08-14 16:43:47 -04001118 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04001119 QLIST_REMOVE(mon_fdset, next);
1120 g_free(mon_fdset);
1121 }
1122}
1123
Kevin Wolf7e3c0de2019-06-13 17:34:00 +02001124void monitor_fdsets_cleanup(void)
Corey Bryantefb87c12012-08-14 16:43:48 -04001125{
1126 MonFdset *mon_fdset;
1127 MonFdset *mon_fdset_next;
1128
Dr. David Alan Gilbert0210c3b2020-09-22 10:57:41 +01001129 QEMU_LOCK_GUARD(&mon_fdsets_lock);
Corey Bryantefb87c12012-08-14 16:43:48 -04001130 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
1131 monitor_fdset_cleanup(mon_fdset);
1132 }
1133}
1134
Corey Bryantba1c0482012-08-14 16:43:43 -04001135AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
1136 const char *opaque, Error **errp)
1137{
1138 int fd;
Kevin Wolf947e4742020-10-05 17:58:44 +02001139 Monitor *mon = monitor_cur();
Corey Bryantba1c0482012-08-14 16:43:43 -04001140 AddfdInfo *fdinfo;
1141
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03001142 fd = qemu_chr_fe_get_msgfd(&mon->chr);
Corey Bryantba1c0482012-08-14 16:43:43 -04001143 if (fd == -1) {
Markus Armbrusterf820af82020-11-13 09:26:18 +01001144 error_setg(errp, "No file descriptor supplied via SCM_RIGHTS");
Corey Bryantba1c0482012-08-14 16:43:43 -04001145 goto error;
1146 }
1147
Corey Bryante446f702012-10-18 15:19:32 -04001148 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
1149 has_opaque, opaque, errp);
1150 if (fdinfo) {
1151 return fdinfo;
Corey Bryant9ac54af2012-10-18 15:19:31 -04001152 }
1153
Corey Bryantba1c0482012-08-14 16:43:43 -04001154error:
1155 if (fd != -1) {
1156 close(fd);
1157 }
1158 return NULL;
1159}
1160
1161void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
1162{
1163 MonFdset *mon_fdset;
1164 MonFdsetFd *mon_fdset_fd;
1165 char fd_str[60];
1166
Dr. David Alan Gilbert0210c3b2020-09-22 10:57:41 +01001167 QEMU_LOCK_GUARD(&mon_fdsets_lock);
Corey Bryantba1c0482012-08-14 16:43:43 -04001168 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1169 if (mon_fdset->id != fdset_id) {
1170 continue;
1171 }
1172 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1173 if (has_fd) {
1174 if (mon_fdset_fd->fd != fd) {
1175 continue;
1176 }
1177 mon_fdset_fd->removed = true;
1178 break;
1179 } else {
1180 mon_fdset_fd->removed = true;
1181 }
1182 }
1183 if (has_fd && !mon_fdset_fd) {
1184 goto error;
1185 }
1186 monitor_fdset_cleanup(mon_fdset);
1187 return;
1188 }
1189
1190error:
1191 if (has_fd) {
1192 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
1193 fdset_id, fd);
1194 } else {
1195 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
1196 }
Markus Armbrusterf820af82020-11-13 09:26:18 +01001197 error_setg(errp, "File descriptor named '%s' not found", fd_str);
Corey Bryantba1c0482012-08-14 16:43:43 -04001198}
1199
1200FdsetInfoList *qmp_query_fdsets(Error **errp)
1201{
1202 MonFdset *mon_fdset;
1203 MonFdsetFd *mon_fdset_fd;
1204 FdsetInfoList *fdset_list = NULL;
1205
Dr. David Alan Gilbert0210c3b2020-09-22 10:57:41 +01001206 QEMU_LOCK_GUARD(&mon_fdsets_lock);
Corey Bryantba1c0482012-08-14 16:43:43 -04001207 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
Eric Blake54aa3de2020-11-12 19:13:37 -06001208 FdsetInfo *fdset_info = g_malloc0(sizeof(*fdset_info));
Corey Bryantba1c0482012-08-14 16:43:43 -04001209
Eric Blake54aa3de2020-11-12 19:13:37 -06001210 fdset_info->fdset_id = mon_fdset->id;
Corey Bryantba1c0482012-08-14 16:43:43 -04001211
1212 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
Eric Blake54aa3de2020-11-12 19:13:37 -06001213 FdsetFdInfo *fdsetfd_info;
Corey Bryantba1c0482012-08-14 16:43:43 -04001214
1215 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
Eric Blake54aa3de2020-11-12 19:13:37 -06001216 fdsetfd_info->fd = mon_fdset_fd->fd;
Corey Bryantba1c0482012-08-14 16:43:43 -04001217 if (mon_fdset_fd->opaque) {
Eric Blake54aa3de2020-11-12 19:13:37 -06001218 fdsetfd_info->has_opaque = true;
1219 fdsetfd_info->opaque = g_strdup(mon_fdset_fd->opaque);
Corey Bryantba1c0482012-08-14 16:43:43 -04001220 } else {
Eric Blake54aa3de2020-11-12 19:13:37 -06001221 fdsetfd_info->has_opaque = false;
Corey Bryantba1c0482012-08-14 16:43:43 -04001222 }
1223
Eric Blake54aa3de2020-11-12 19:13:37 -06001224 QAPI_LIST_PREPEND(fdset_info->fds, fdsetfd_info);
Corey Bryantba1c0482012-08-14 16:43:43 -04001225 }
1226
Eric Blake54aa3de2020-11-12 19:13:37 -06001227 QAPI_LIST_PREPEND(fdset_list, fdset_info);
Corey Bryantba1c0482012-08-14 16:43:43 -04001228 }
1229
1230 return fdset_list;
1231}
1232
Corey Bryante446f702012-10-18 15:19:32 -04001233AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
1234 bool has_opaque, const char *opaque,
1235 Error **errp)
1236{
1237 MonFdset *mon_fdset = NULL;
1238 MonFdsetFd *mon_fdset_fd;
1239 AddfdInfo *fdinfo;
1240
Daniel Brodsky6e8a3552020-04-03 21:21:08 -07001241 QEMU_LOCK_GUARD(&mon_fdsets_lock);
Corey Bryante446f702012-10-18 15:19:32 -04001242 if (has_fdset_id) {
1243 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1244 /* Break if match found or match impossible due to ordering by ID */
1245 if (fdset_id <= mon_fdset->id) {
1246 if (fdset_id < mon_fdset->id) {
1247 mon_fdset = NULL;
1248 }
1249 break;
1250 }
1251 }
1252 }
1253
1254 if (mon_fdset == NULL) {
1255 int64_t fdset_id_prev = -1;
1256 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
1257
1258 if (has_fdset_id) {
1259 if (fdset_id < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001260 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
1261 "a non-negative value");
Corey Bryante446f702012-10-18 15:19:32 -04001262 return NULL;
1263 }
1264 /* Use specified fdset ID */
1265 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1266 mon_fdset_cur = mon_fdset;
1267 if (fdset_id < mon_fdset_cur->id) {
1268 break;
1269 }
1270 }
1271 } else {
1272 /* Use first available fdset ID */
1273 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1274 mon_fdset_cur = mon_fdset;
1275 if (fdset_id_prev == mon_fdset_cur->id - 1) {
1276 fdset_id_prev = mon_fdset_cur->id;
1277 continue;
1278 }
1279 break;
1280 }
1281 }
1282
1283 mon_fdset = g_malloc0(sizeof(*mon_fdset));
1284 if (has_fdset_id) {
1285 mon_fdset->id = fdset_id;
1286 } else {
1287 mon_fdset->id = fdset_id_prev + 1;
1288 }
1289
1290 /* The fdset list is ordered by fdset ID */
1291 if (!mon_fdset_cur) {
1292 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
1293 } else if (mon_fdset->id < mon_fdset_cur->id) {
1294 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
1295 } else {
1296 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
1297 }
1298 }
1299
1300 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
1301 mon_fdset_fd->fd = fd;
1302 mon_fdset_fd->removed = false;
1303 if (has_opaque) {
1304 mon_fdset_fd->opaque = g_strdup(opaque);
1305 }
1306 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
1307
1308 fdinfo = g_malloc0(sizeof(*fdinfo));
1309 fdinfo->fdset_id = mon_fdset->id;
1310 fdinfo->fd = mon_fdset_fd->fd;
1311
1312 return fdinfo;
1313}
1314
Daniel P. Berrangé60efffa2020-08-27 13:27:00 +01001315int monitor_fdset_dup_fd_add(int64_t fdset_id, int flags)
Corey Bryantadb696f2012-08-14 16:43:47 -04001316{
Peter Xu47451462018-06-08 11:55:11 +08001317#ifdef _WIN32
1318 return -ENOENT;
1319#else
Corey Bryantadb696f2012-08-14 16:43:47 -04001320 MonFdset *mon_fdset;
Corey Bryantadb696f2012-08-14 16:43:47 -04001321
Dr. David Alan Gilbert0210c3b2020-09-22 10:57:41 +01001322 QEMU_LOCK_GUARD(&mon_fdsets_lock);
Corey Bryantadb696f2012-08-14 16:43:47 -04001323 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
Daniel P. Berrangé60efffa2020-08-27 13:27:00 +01001324 MonFdsetFd *mon_fdset_fd;
1325 MonFdsetFd *mon_fdset_fd_dup;
1326 int fd = -1;
1327 int dup_fd;
1328 int mon_fd_flags;
1329
Corey Bryantadb696f2012-08-14 16:43:47 -04001330 if (mon_fdset->id != fdset_id) {
1331 continue;
1332 }
Daniel P. Berrangé60efffa2020-08-27 13:27:00 +01001333
Corey Bryantadb696f2012-08-14 16:43:47 -04001334 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1335 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
1336 if (mon_fd_flags == -1) {
Daniel P. Berrangé60efffa2020-08-27 13:27:00 +01001337 return -1;
Corey Bryantadb696f2012-08-14 16:43:47 -04001338 }
1339
1340 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
Daniel P. Berrangé60efffa2020-08-27 13:27:00 +01001341 fd = mon_fdset_fd->fd;
1342 break;
Corey Bryantadb696f2012-08-14 16:43:47 -04001343 }
1344 }
Corey Bryantadb696f2012-08-14 16:43:47 -04001345
Daniel P. Berrangé60efffa2020-08-27 13:27:00 +01001346 if (fd == -1) {
Daniel P. Berrangé60efffa2020-08-27 13:27:00 +01001347 errno = EACCES;
1348 return -1;
Corey Bryantadb696f2012-08-14 16:43:47 -04001349 }
Daniel P. Berrangé60efffa2020-08-27 13:27:00 +01001350
1351 dup_fd = qemu_dup_flags(fd, flags);
1352 if (dup_fd == -1) {
Daniel P. Berrangé60efffa2020-08-27 13:27:00 +01001353 return -1;
Corey Bryantadb696f2012-08-14 16:43:47 -04001354 }
Daniel P. Berrangé60efffa2020-08-27 13:27:00 +01001355
Corey Bryantadb696f2012-08-14 16:43:47 -04001356 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
1357 mon_fdset_fd_dup->fd = dup_fd;
1358 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
Daniel P. Berrangé60efffa2020-08-27 13:27:00 +01001359 return dup_fd;
Corey Bryantadb696f2012-08-14 16:43:47 -04001360 }
Peter Xu47451462018-06-08 11:55:11 +08001361
Daniel P. Berrangé60efffa2020-08-27 13:27:00 +01001362 errno = ENOENT;
Corey Bryantadb696f2012-08-14 16:43:47 -04001363 return -1;
Daniel P. Berrangé60efffa2020-08-27 13:27:00 +01001364#endif
Corey Bryantadb696f2012-08-14 16:43:47 -04001365}
1366
Yury Kotov854f63d2019-05-23 12:44:33 +03001367static int64_t monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
Corey Bryantadb696f2012-08-14 16:43:47 -04001368{
1369 MonFdset *mon_fdset;
1370 MonFdsetFd *mon_fdset_fd_dup;
1371
Dr. David Alan Gilbert0210c3b2020-09-22 10:57:41 +01001372 QEMU_LOCK_GUARD(&mon_fdsets_lock);
Corey Bryantadb696f2012-08-14 16:43:47 -04001373 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1374 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
1375 if (mon_fdset_fd_dup->fd == dup_fd) {
1376 if (remove) {
1377 QLIST_REMOVE(mon_fdset_fd_dup, next);
Chen Quna6616142020-01-15 15:20:16 +08001378 g_free(mon_fdset_fd_dup);
Corey Bryantadb696f2012-08-14 16:43:47 -04001379 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
1380 monitor_fdset_cleanup(mon_fdset);
1381 }
Dr. David Alan Gilbert0210c3b2020-09-22 10:57:41 +01001382 return -1;
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02001383 } else {
1384 return mon_fdset->id;
Corey Bryantadb696f2012-08-14 16:43:47 -04001385 }
Corey Bryantadb696f2012-08-14 16:43:47 -04001386 }
1387 }
1388 }
Peter Xu47451462018-06-08 11:55:11 +08001389
Corey Bryantadb696f2012-08-14 16:43:47 -04001390 return -1;
1391}
1392
Yury Kotov854f63d2019-05-23 12:44:33 +03001393int64_t monitor_fdset_dup_fd_find(int dup_fd)
Corey Bryantadb696f2012-08-14 16:43:47 -04001394{
1395 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
1396}
1397
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02001398void monitor_fdset_dup_fd_remove(int dup_fd)
Corey Bryantadb696f2012-08-14 16:43:47 -04001399{
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02001400 monitor_fdset_dup_fd_find_remove(dup_fd, true);
Corey Bryantadb696f2012-08-14 16:43:47 -04001401}
1402
Markus Armbruster1677f4c2015-02-09 14:03:19 +01001403int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
Laszlo Ersek59063662014-04-10 10:24:31 +02001404{
1405 int fd;
1406 Error *local_err = NULL;
1407
1408 if (!qemu_isdigit(fdname[0]) && mon) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001409 fd = monitor_get_fd(mon, fdname, &local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00001410 } else {
1411 fd = qemu_parse_fd(fdname);
Laszlo Ersek59063662014-04-10 10:24:31 +02001412 if (fd == -1) {
1413 error_setg(&local_err, "Invalid file descriptor number '%s'",
1414 fdname);
1415 }
1416 }
1417 if (local_err) {
1418 error_propagate(errp, local_err);
1419 assert(fd == -1);
1420 } else {
1421 assert(fd != -1);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00001422 }
1423
1424 return fd;
1425}
1426
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03001427/* Please update hmp-commands.hx when adding or changing commands */
Kevin Wolfa0cd5e12019-06-13 17:33:56 +02001428static HMPCommand hmp_info_cmds[] = {
Pavel Butsykinda76ee72015-09-10 18:38:58 +03001429#include "hmp-commands-info.h"
1430 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00001431};
1432
Kevin Wolfa0cd5e12019-06-13 17:33:56 +02001433/* hmp_cmds and hmp_info_cmds would be sorted at runtime */
Kevin Wolfed7bda52019-06-13 17:34:01 +02001434HMPCommand hmp_cmds[] = {
Wenchao Xiaa13ced52013-01-14 14:06:28 +08001435#include "hmp-commands.h"
1436 { NULL, NULL, },
1437};
1438
Kevin Wolfed7bda52019-06-13 17:34:01 +02001439/*
1440 * Set @pval to the value in the register identified by @name.
1441 * return 0 if OK, -1 if not found
1442 */
Kevin Wolf2fc5d012020-11-13 12:43:24 +01001443int get_monitor_def(Monitor *mon, int64_t *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00001444{
Pavel Butsykinbf957282015-09-10 18:38:59 +03001445 const MonitorDef *md = target_monitor_defs();
Kevin Wolf2fc5d012020-11-13 12:43:24 +01001446 CPUState *cs = mon_get_cpu(mon);
bellard92a31b12005-02-10 22:00:52 +00001447 void *ptr;
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11001448 uint64_t tmp = 0;
1449 int ret;
bellard92a31b12005-02-10 22:00:52 +00001450
Thomas Huth854e67f2017-01-13 13:12:35 +01001451 if (cs == NULL || md == NULL) {
Pavel Butsykinbf957282015-09-10 18:38:59 +03001452 return -1;
1453 }
1454
1455 for(; md->name != NULL; md++) {
Kevin Wolfed7bda52019-06-13 17:34:01 +02001456 if (hmp_compare_cmd(name, md->name)) {
bellard9307c4c2004-04-04 12:57:25 +00001457 if (md->get_value) {
Kevin Wolf43cf0672020-11-13 12:43:25 +01001458 *pval = md->get_value(mon, md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00001459 } else {
Kevin Wolfe7cff9c2020-11-13 12:43:26 +01001460 CPUArchState *env = mon_get_cpu_env(mon);
bellard6a00d602005-11-21 23:25:50 +00001461 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00001462 switch(md->type) {
1463 case MD_I32:
1464 *pval = *(int32_t *)ptr;
1465 break;
1466 case MD_TLONG:
1467 *pval = *(target_long *)ptr;
1468 break;
1469 default:
1470 *pval = 0;
1471 break;
1472 }
bellard9307c4c2004-04-04 12:57:25 +00001473 }
1474 return 0;
1475 }
1476 }
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11001477
Thomas Huth854e67f2017-01-13 13:12:35 +01001478 ret = target_get_monitor_def(cs, name, &tmp);
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11001479 if (!ret) {
1480 *pval = (target_long) tmp;
1481 }
1482
1483 return ret;
bellard9307c4c2004-04-04 12:57:25 +00001484}
1485
Hani Benhabiles40d19392014-05-07 23:41:30 +01001486static void add_completion_option(ReadLineState *rs, const char *str,
1487 const char *option)
1488{
1489 if (!str || !option) {
1490 return;
1491 }
1492 if (!strncmp(option, str, strlen(str))) {
1493 readline_add_completion(rs, option);
1494 }
1495}
1496
Hani Benhabiles13e315d2014-05-07 23:41:29 +01001497void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
1498{
1499 size_t len;
1500 ChardevBackendInfoList *list, *start;
1501
1502 if (nb_args != 2) {
1503 return;
1504 }
1505 len = strlen(str);
1506 readline_set_completion_index(rs, len);
1507
1508 start = list = qmp_query_chardev_backends(NULL);
1509 while (list) {
1510 const char *chr_name = list->value->name;
1511
1512 if (!strncmp(chr_name, str, len)) {
1513 readline_add_completion(rs, chr_name);
1514 }
1515 list = list->next;
1516 }
1517 qapi_free_ChardevBackendInfoList(start);
1518}
1519
Hani Benhabilesb162b492014-05-07 23:41:31 +01001520void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
1521{
1522 size_t len;
1523 int i;
1524
1525 if (nb_args != 2) {
1526 return;
1527 }
1528 len = strlen(str);
1529 readline_set_completion_index(rs, len);
Markus Armbruster1c236ba2017-08-24 10:46:06 +02001530 for (i = 0; i < NET_CLIENT_DRIVER__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02001531 add_completion_option(rs, str, NetClientDriver_str(i));
Hani Benhabilesb162b492014-05-07 23:41:31 +01001532 }
1533}
1534
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01001535void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles992d3e62014-02-06 23:30:11 +01001536{
1537 GSList *list, *elt;
1538 size_t len;
1539
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01001540 if (nb_args != 2) {
1541 return;
1542 }
1543
Hani Benhabiles992d3e62014-02-06 23:30:11 +01001544 len = strlen(str);
1545 readline_set_completion_index(rs, len);
1546 list = elt = object_class_get_list(TYPE_DEVICE, false);
1547 while (elt) {
1548 const char *name;
1549 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
1550 TYPE_DEVICE);
1551 name = object_class_get_name(OBJECT_CLASS(dc));
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01001552
Eduardo Habkoste90f2a82017-05-03 17:35:44 -03001553 if (dc->user_creatable
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01001554 && !strncmp(name, str, len)) {
Hani Benhabiles992d3e62014-02-06 23:30:11 +01001555 readline_add_completion(rs, name);
1556 }
1557 elt = elt->next;
1558 }
1559 g_slist_free(list);
1560}
1561
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01001562void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles1094fd32014-02-06 23:30:13 +01001563{
1564 GSList *list, *elt;
1565 size_t len;
1566
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01001567 if (nb_args != 2) {
1568 return;
1569 }
1570
Hani Benhabiles1094fd32014-02-06 23:30:13 +01001571 len = strlen(str);
1572 readline_set_completion_index(rs, len);
1573 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
1574 while (elt) {
1575 const char *name;
1576
1577 name = object_class_get_name(OBJECT_CLASS(elt->data));
1578 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
1579 readline_add_completion(rs, name);
1580 }
1581 elt = elt->next;
1582 }
1583 g_slist_free(list);
1584}
1585
Marc-André Lureau91590152020-01-10 19:30:17 +04001586static int qdev_add_hotpluggable_device(Object *obj, void *opaque)
1587{
1588 GSList **list = opaque;
Philippe Mathieu-Daudé688ffbb2020-05-12 09:00:19 +02001589 DeviceState *dev = (DeviceState *)object_dynamic_cast(obj, TYPE_DEVICE);
Marc-André Lureau91590152020-01-10 19:30:17 +04001590
1591 if (dev == NULL) {
1592 return 0;
1593 }
1594
1595 if (dev->realized && object_property_get_bool(obj, "hotpluggable", NULL)) {
1596 *list = g_slist_append(*list, dev);
1597 }
1598
1599 return 0;
1600}
1601
1602static GSList *qdev_build_hotpluggable_device_list(Object *peripheral)
1603{
1604 GSList *list = NULL;
1605
1606 object_child_foreach(peripheral, qdev_add_hotpluggable_device, &list);
1607
1608 return list;
1609}
1610
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08001611static void peripheral_device_del_completion(ReadLineState *rs,
1612 const char *str, size_t len)
1613{
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02001614 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
1615 GSList *list, *item;
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08001616
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02001617 list = qdev_build_hotpluggable_device_list(peripheral);
1618 if (!list) {
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08001619 return;
1620 }
1621
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08001622 for (item = list; item; item = g_slist_next(item)) {
1623 DeviceState *dev = item->data;
1624
1625 if (dev->id && !strncmp(str, dev->id, len)) {
1626 readline_add_completion(rs, dev->id);
1627 }
1628 }
1629
1630 g_slist_free(list);
1631}
1632
Hani Benhabiles6297d9a2014-05-07 23:41:28 +01001633void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
1634{
1635 size_t len;
1636 ChardevInfoList *list, *start;
1637
1638 if (nb_args != 2) {
1639 return;
1640 }
1641 len = strlen(str);
1642 readline_set_completion_index(rs, len);
1643
1644 start = list = qmp_query_chardev(NULL);
1645 while (list) {
1646 ChardevInfo *chr = list->value;
1647
1648 if (!strncmp(chr->label, str, len)) {
1649 readline_add_completion(rs, chr->label);
1650 }
1651 list = list->next;
1652 }
1653 qapi_free_ChardevInfoList(start);
1654}
1655
Hani Benhabiles8e597772014-05-27 23:39:30 +01001656static void ringbuf_completion(ReadLineState *rs, const char *str)
1657{
1658 size_t len;
1659 ChardevInfoList *list, *start;
1660
1661 len = strlen(str);
1662 readline_set_completion_index(rs, len);
1663
1664 start = list = qmp_query_chardev(NULL);
1665 while (list) {
1666 ChardevInfo *chr_info = list->value;
1667
1668 if (!strncmp(chr_info->label, str, len)) {
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03001669 Chardev *chr = qemu_chr_find(chr_info->label);
Marc-André Lureau777357d2016-12-07 18:39:10 +03001670 if (chr && CHARDEV_IS_RINGBUF(chr)) {
Hani Benhabiles8e597772014-05-27 23:39:30 +01001671 readline_add_completion(rs, chr_info->label);
1672 }
1673 }
1674 list = list->next;
1675 }
1676 qapi_free_ChardevInfoList(start);
1677}
1678
Hani Benhabiles8e597772014-05-27 23:39:30 +01001679void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
1680{
1681 if (nb_args != 2) {
1682 return;
1683 }
1684 ringbuf_completion(rs, str);
1685}
1686
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01001687void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
1688{
1689 size_t len;
1690
1691 if (nb_args != 2) {
1692 return;
1693 }
1694
1695 len = strlen(str);
1696 readline_set_completion_index(rs, len);
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08001697 peripheral_device_del_completion(rs, str, len);
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01001698}
1699
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01001700void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabilesb48fa072014-02-06 23:30:12 +01001701{
1702 ObjectPropertyInfoList *list, *start;
1703 size_t len;
1704
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01001705 if (nb_args != 2) {
1706 return;
1707 }
Hani Benhabilesb48fa072014-02-06 23:30:12 +01001708 len = strlen(str);
1709 readline_set_completion_index(rs, len);
1710
1711 start = list = qmp_qom_list("/objects", NULL);
1712 while (list) {
1713 ObjectPropertyInfo *info = list->value;
1714
1715 if (!strncmp(info->type, "child<", 5)
1716 && !strncmp(info->name, str, len)) {
1717 readline_add_completion(rs, info->name);
1718 }
1719 list = list->next;
1720 }
1721 qapi_free_ObjectPropertyInfoList(start);
1722}
1723
Hani Benhabiles29136cd2014-05-07 23:41:27 +01001724void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
1725{
1726 int i;
1727 char *sep;
1728 size_t len;
1729
1730 if (nb_args != 2) {
1731 return;
1732 }
1733 sep = strrchr(str, '-');
1734 if (sep) {
1735 str = sep + 1;
1736 }
1737 len = strlen(str);
1738 readline_set_completion_index(rs, len);
Eric Blake7fb1cf12015-11-18 01:52:57 -07001739 for (i = 0; i < Q_KEY_CODE__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02001740 if (!strncmp(str, QKeyCode_str(i), len)) {
1741 readline_add_completion(rs, QKeyCode_str(i));
Hani Benhabiles29136cd2014-05-07 23:41:27 +01001742 }
1743 }
1744}
1745
Hani Benhabiles40d19392014-05-07 23:41:30 +01001746void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
1747{
1748 size_t len;
1749
1750 len = strlen(str);
1751 readline_set_completion_index(rs, len);
1752 if (nb_args == 2) {
Jason Wangeaed4832015-04-23 14:21:38 +08001753 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles40d19392014-05-07 23:41:30 +01001754 int count, i;
1755 count = qemu_find_net_clients_except(NULL, ncs,
Eric Blakef394b2e2016-07-13 21:50:23 -06001756 NET_CLIENT_DRIVER_NONE,
Jason Wangeaed4832015-04-23 14:21:38 +08001757 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08001758 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles40d19392014-05-07 23:41:30 +01001759 const char *name = ncs[i]->name;
1760 if (!strncmp(str, name, len)) {
1761 readline_add_completion(rs, name);
1762 }
1763 }
1764 } else if (nb_args == 3) {
1765 add_completion_option(rs, str, "on");
1766 add_completion_option(rs, str, "off");
1767 }
1768}
1769
Hani Benhabiles11b389f2014-05-07 23:41:32 +01001770void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
1771{
1772 int len, count, i;
Jason Wangeaed4832015-04-23 14:21:38 +08001773 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles11b389f2014-05-07 23:41:32 +01001774
1775 if (nb_args != 2) {
1776 return;
1777 }
1778
1779 len = strlen(str);
1780 readline_set_completion_index(rs, len);
Eric Blakef394b2e2016-07-13 21:50:23 -06001781 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_DRIVER_NIC,
Jason Wangeaed4832015-04-23 14:21:38 +08001782 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08001783 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles11b389f2014-05-07 23:41:32 +01001784 const char *name = ncs[i]->name;
1785 if (strncmp(str, name, len)) {
1786 continue;
1787 }
Eric Blake08712fc2020-03-17 15:17:11 -05001788 if (ncs[i]->is_netdev) {
Hani Benhabiles11b389f2014-05-07 23:41:32 +01001789 readline_add_completion(rs, name);
1790 }
1791 }
1792}
1793
Lluís Vilanovabd712112016-07-11 12:53:51 +02001794void info_trace_events_completion(ReadLineState *rs, int nb_args, const char *str)
1795{
1796 size_t len;
1797
1798 len = strlen(str);
1799 readline_set_completion_index(rs, len);
1800 if (nb_args == 2) {
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01001801 TraceEventIter iter;
1802 TraceEvent *ev;
1803 char *pattern = g_strdup_printf("%s*", str);
Gerd Hoffmann117856c2021-06-01 15:24:03 +02001804 trace_event_iter_init_pattern(&iter, pattern);
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01001805 while ((ev = trace_event_iter_next(&iter)) != NULL) {
1806 readline_add_completion(rs, trace_event_get_name(ev));
Lluís Vilanovabd712112016-07-11 12:53:51 +02001807 }
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01001808 g_free(pattern);
Lluís Vilanovabd712112016-07-11 12:53:51 +02001809 }
1810}
1811
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01001812void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
1813{
1814 size_t len;
1815
1816 len = strlen(str);
1817 readline_set_completion_index(rs, len);
1818 if (nb_args == 2) {
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01001819 TraceEventIter iter;
1820 TraceEvent *ev;
1821 char *pattern = g_strdup_printf("%s*", str);
Gerd Hoffmann117856c2021-06-01 15:24:03 +02001822 trace_event_iter_init_pattern(&iter, pattern);
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01001823 while ((ev = trace_event_iter_next(&iter)) != NULL) {
1824 readline_add_completion(rs, trace_event_get_name(ev));
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01001825 }
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01001826 g_free(pattern);
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01001827 } else if (nb_args == 3) {
1828 add_completion_option(rs, str, "on");
1829 add_completion_option(rs, str, "off");
1830 }
1831}
1832
Hani Benhabilesd0ece342014-05-27 23:39:31 +01001833void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
1834{
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01001835 int i;
1836
Hani Benhabilesd0ece342014-05-27 23:39:31 +01001837 if (nb_args != 2) {
1838 return;
1839 }
1840 readline_set_completion_index(rs, strlen(str));
Michal Privoznik14d53b42017-09-07 10:05:24 +02001841 for (i = 0; i < WATCHDOG_ACTION__MAX; i++) {
1842 add_completion_option(rs, str, WatchdogAction_str(i));
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01001843 }
Hani Benhabilesd0ece342014-05-27 23:39:31 +01001844}
1845
Hani Benhabilesc68a0402014-05-27 23:39:32 +01001846void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
1847 const char *str)
1848{
1849 size_t len;
1850
1851 len = strlen(str);
1852 readline_set_completion_index(rs, len);
1853 if (nb_args == 2) {
1854 int i;
Eric Blake7fb1cf12015-11-18 01:52:57 -07001855 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02001856 const char *name = MigrationCapability_str(i);
Hani Benhabilesc68a0402014-05-27 23:39:32 +01001857 if (!strncmp(str, name, len)) {
1858 readline_add_completion(rs, name);
1859 }
1860 }
1861 } else if (nb_args == 3) {
1862 add_completion_option(rs, str, "on");
1863 add_completion_option(rs, str, "off");
1864 }
1865}
1866
Liang Li50e9a622015-03-23 16:32:29 +08001867void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
1868 const char *str)
1869{
1870 size_t len;
1871
1872 len = strlen(str);
1873 readline_set_completion_index(rs, len);
1874 if (nb_args == 2) {
1875 int i;
Eric Blake7fb1cf12015-11-18 01:52:57 -07001876 for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02001877 const char *name = MigrationParameter_str(i);
Liang Li50e9a622015-03-23 16:32:29 +08001878 if (!strncmp(str, name, len)) {
1879 readline_add_completion(rs, name);
1880 }
1881 }
1882 }
1883}
1884
Hani Benhabilesb21631f2014-05-27 23:39:37 +01001885static void vm_completion(ReadLineState *rs, const char *str)
1886{
1887 size_t len;
Kevin Wolf7c8eece2016-03-22 18:58:50 +01001888 BlockDriverState *bs;
Kevin Wolf88be7b42016-05-20 18:49:07 +02001889 BdrvNextIterator it;
Hani Benhabilesb21631f2014-05-27 23:39:37 +01001890
1891 len = strlen(str);
1892 readline_set_completion_index(rs, len);
Kevin Wolf7c8eece2016-03-22 18:58:50 +01001893
Kevin Wolf88be7b42016-05-20 18:49:07 +02001894 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Hani Benhabilesb21631f2014-05-27 23:39:37 +01001895 SnapshotInfoList *snapshots, *snapshot;
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03001896 AioContext *ctx = bdrv_get_aio_context(bs);
1897 bool ok = false;
Hani Benhabilesb21631f2014-05-27 23:39:37 +01001898
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03001899 aio_context_acquire(ctx);
1900 if (bdrv_can_snapshot(bs)) {
1901 ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0;
1902 }
1903 aio_context_release(ctx);
1904 if (!ok) {
Hani Benhabilesb21631f2014-05-27 23:39:37 +01001905 continue;
1906 }
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03001907
Hani Benhabilesb21631f2014-05-27 23:39:37 +01001908 snapshot = snapshots;
1909 while (snapshot) {
1910 char *completion = snapshot->value->name;
1911 if (!strncmp(str, completion, len)) {
1912 readline_add_completion(rs, completion);
1913 }
1914 completion = snapshot->value->id;
1915 if (!strncmp(str, completion, len)) {
1916 readline_add_completion(rs, completion);
1917 }
1918 snapshot = snapshot->next;
1919 }
1920 qapi_free_SnapshotInfoList(snapshots);
1921 }
1922
1923}
1924
1925void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
1926{
1927 if (nb_args == 2) {
1928 vm_completion(rs, str);
1929 }
1930}
1931
1932void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
1933{
1934 if (nb_args == 2) {
1935 vm_completion(rs, str);
1936 }
1937}
1938
Wayne Xia816f8922011-10-12 11:32:41 +08001939static int
1940compare_mon_cmd(const void *a, const void *b)
1941{
Kevin Wolfa0cd5e12019-06-13 17:33:56 +02001942 return strcmp(((const HMPCommand *)a)->name,
1943 ((const HMPCommand *)b)->name);
Wayne Xia816f8922011-10-12 11:32:41 +08001944}
1945
1946static void sortcmdlist(void)
1947{
Kevin Wolfa0cd5e12019-06-13 17:33:56 +02001948 qsort(hmp_cmds, ARRAY_SIZE(hmp_cmds) - 1,
1949 sizeof(*hmp_cmds),
1950 compare_mon_cmd);
1951 qsort(hmp_info_cmds, ARRAY_SIZE(hmp_info_cmds) - 1,
1952 sizeof(*hmp_info_cmds),
1953 compare_mon_cmd);
Wayne Xia816f8922011-10-12 11:32:41 +08001954}
1955
Gerd Hoffmannf0e48cb2021-06-24 12:38:32 +02001956void monitor_register_hmp(const char *name, bool info,
1957 void (*cmd)(Monitor *mon, const QDict *qdict))
1958{
1959 HMPCommand *table = info ? hmp_info_cmds : hmp_cmds;
1960
1961 while (table->name != NULL) {
1962 if (strcmp(table->name, name) == 0) {
Daniel P. Berrangéf9429c62021-10-08 15:09:00 +01001963 g_assert(table->cmd == NULL && table->cmd_info_hrt == NULL);
Gerd Hoffmannf0e48cb2021-06-24 12:38:32 +02001964 table->cmd = cmd;
1965 return;
1966 }
1967 table++;
1968 }
1969 g_assert_not_reached();
1970}
1971
Daniel P. Berrangéf9429c62021-10-08 15:09:00 +01001972void monitor_register_hmp_info_hrt(const char *name,
1973 HumanReadableText *(*handler)(Error **errp))
1974{
1975 HMPCommand *table = hmp_info_cmds;
1976
1977 while (table->name != NULL) {
1978 if (strcmp(table->name, name) == 0) {
1979 g_assert(table->cmd == NULL && table->cmd_info_hrt == NULL);
1980 table->cmd_info_hrt = handler;
1981 return;
1982 }
1983 table++;
1984 }
1985 g_assert_not_reached();
1986}
1987
Peter Xu6adf08d2018-03-09 16:59:50 +08001988void monitor_init_globals(void)
1989{
Kevin Wolf1d95db72019-06-13 17:34:02 +02001990 monitor_init_globals_core();
Peter Xu6adf08d2018-03-09 16:59:50 +08001991 monitor_init_qmp_commands();
Peter Xu6adf08d2018-03-09 16:59:50 +08001992 sortcmdlist();
Peter Xu47451462018-06-08 11:55:11 +08001993 qemu_mutex_init(&mon_fdsets_lock);
Peter Xu6adf08d2018-03-09 16:59:50 +08001994}