blob: ad476c6e51ea45311aef03a863900fca68596609 [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"
Vincent Palatinb3946622017-01-10 11:59:55 +010044#include "sysemu/hw_accel.h"
Markus Armbruster54d31232019-08-12 07:23:59 +020045#include "sysemu/runstate.h"
Daniel P. Berrangeb76806d2016-02-18 18:40:24 +000046#include "authz/list.h"
47#include "qapi/util.h"
Markus Armbruster55225c82019-06-19 22:10:44 +020048#include "sysemu/blockdev.h"
49#include "sysemu/sysemu.h"
Markus Armbruster14a48c12019-05-23 16:35:05 +020050#include "sysemu/tcg.h"
Paolo Bonzinibdee56f2013-04-02 18:28:41 +020051#include "sysemu/tpm.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010052#include "qapi/qmp/qdict.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010053#include "qapi/qmp/qerror.h"
Markus Armbrusterfc81fa12018-02-01 12:18:40 +010054#include "qapi/qmp/qstring.h"
Markus Armbrustera9c94272016-06-22 19:11:19 +020055#include "qom/object_interfaces.h"
Lluís31965ae2011-08-31 20:31:24 +020056#include "trace/control.h"
Pavel Butsykinbf957282015-09-10 18:38:59 +030057#include "monitor/hmp-target.h"
Markus Armbruster275307a2019-06-19 22:10:36 +020058#include "monitor/hmp.h"
Lluís6d8a7642011-08-31 20:30:43 +020059#ifdef CONFIG_TRACE_SIMPLE
Lluís31965ae2011-08-31 20:31:24 +020060#include "trace/simple.h"
Prerna Saxena22890ab2010-06-24 17:04:53 +053061#endif
Paolo Bonzini022c62c2012-12-17 18:19:49 +010062#include "exec/memory.h"
Paolo Bonzini63c91552016-03-15 13:18:37 +010063#include "exec/exec-all.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010064#include "qemu/option.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010065#include "qemu/thread.h"
Hani Benhabilesb21631f2014-05-27 23:39:37 +010066#include "block/qapi.h"
Maxim Levitskya2dde2f2020-03-08 11:24:32 +020067#include "block/block-hmp-cmds.h"
Markus Armbruster00ca24f2019-11-20 19:25:48 +010068#include "qapi/qapi-commands-char.h"
Kevin Wolffa4dcf52020-01-29 11:22:37 +010069#include "qapi/qapi-commands-control.h"
Markus Armbruster00ca24f2019-11-20 19:25:48 +010070#include "qapi/qapi-commands-migration.h"
71#include "qapi/qapi-commands-misc.h"
72#include "qapi/qapi-commands-qom.h"
73#include "qapi/qapi-commands-trace.h"
Markus Armbruster00ca24f2019-11-20 19:25:48 +010074#include "qapi/qapi-init-commands.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010075#include "qapi/error.h"
Wenchao Xia43a14cf2014-06-18 08:43:31 +020076#include "qapi/qmp-event.h"
Paolo Bonzinid2528bd2017-03-03 12:01:16 +010077#include "sysemu/cpus.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020078#include "qemu/cutils.h"
ths6a5bd302007-12-03 17:05:38 +000079
Jason J. Hernea4538a52015-06-26 14:07:21 -040080#if defined(TARGET_S390X)
81#include "hw/s390x/storage-keys.h"
Claudio Imbrendaf860d492016-08-15 18:44:04 +020082#include "hw/s390x/storage-attributes.h"
Jason J. Hernea4538a52015-06-26 14:07:21 -040083#endif
84
Mark McLoughlinf07918f2009-07-22 09:11:40 +010085/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -050086typedef struct mon_fd_t mon_fd_t;
87struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +010088 char *name;
89 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -050090 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +010091};
92
Corey Bryantba1c0482012-08-14 16:43:43 -040093/* file descriptor associated with a file descriptor set */
94typedef struct MonFdsetFd MonFdsetFd;
95struct MonFdsetFd {
96 int fd;
97 bool removed;
98 char *opaque;
99 QLIST_ENTRY(MonFdsetFd) next;
100};
101
102/* file descriptor set containing fds passed via SCM_RIGHTS */
103typedef struct MonFdset MonFdset;
104struct MonFdset {
105 int64_t id;
106 QLIST_HEAD(, MonFdsetFd) fds;
Corey Bryantadb696f2012-08-14 16:43:47 -0400107 QLIST_HEAD(, MonFdsetFd) dup_fds;
Corey Bryantba1c0482012-08-14 16:43:43 -0400108 QLIST_ENTRY(MonFdset) next;
109};
110
Peter Xu47451462018-06-08 11:55:11 +0800111/* Protects mon_fdsets */
112static QemuMutex mon_fdsets_lock;
Paolo Bonzinib58deb32018-12-06 11:58:10 +0100113static QLIST_HEAD(, MonFdset) mon_fdsets;
Peter Xu47451462018-06-08 11:55:11 +0800114
Kevin Wolfa0cd5e12019-06-13 17:33:56 +0200115static HMPCommand hmp_info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000116
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200117char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
118 int64_t cpu_index, Error **errp)
Luiz Capitulino0268d972010-10-22 10:08:02 -0200119{
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200120 char *output = NULL;
Kevin Wolf5f9dba12019-06-13 17:33:54 +0200121 MonitorHMP hmp = {};
Luiz Capitulino0268d972010-10-22 10:08:02 -0200122
Kevin Wolf92082412019-06-13 17:34:03 +0200123 monitor_data_init(&hmp.common, false, true, false);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200124
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200125 if (has_cpu_index) {
Kevin Wolfdcba65f2020-10-05 17:58:42 +0200126 int ret = monitor_set_cpu(&hmp.common, cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200127 if (ret < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100128 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
129 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200130 goto out;
131 }
132 }
133
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100134 handle_hmp_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200135
Dr. David Alan Gilbert0210c3b2020-09-22 10:57:41 +0100136 WITH_QEMU_LOCK_GUARD(&hmp.common.mon_lock) {
Markus Armbruster20076f42020-12-11 18:11:34 +0100137 output = g_strdup(hmp.common.outbuf->str);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200138 }
139
140out:
Kevin Wolf5f9dba12019-06-13 17:33:54 +0200141 monitor_data_destroy(&hmp.common);
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200142 return output;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200143}
144
Kevin Wolfed7bda52019-06-13 17:34:01 +0200145/**
146 * Is @name in the '|' separated list of names @list?
147 */
148int hmp_compare_cmd(const char *name, const char *list)
bellard9dc39cb2004-03-14 21:38:27 +0000149{
150 const char *p, *pstart;
151 int len;
152 len = strlen(name);
153 p = list;
Kevin Wolfed7bda52019-06-13 17:34:01 +0200154 for (;;) {
bellard9dc39cb2004-03-14 21:38:27 +0000155 pstart = p;
Keno Fischer5c99fa32018-06-29 12:32:10 +0200156 p = qemu_strchrnul(p, '|');
Kevin Wolfed7bda52019-06-13 17:34:01 +0200157 if ((p - pstart) == len && !memcmp(pstart, name, len)) {
bellard9dc39cb2004-03-14 21:38:27 +0000158 return 1;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800159 }
160 if (*p == '\0') {
161 break;
162 }
Kevin Wolfed7bda52019-06-13 17:34:01 +0200163 p++;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800164 }
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800165 return 0;
bellard9dc39cb2004-03-14 21:38:27 +0000166}
167
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300168static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300169{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300170 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300171}
172
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100173static void hmp_trace_event(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530174{
175 const char *tp_name = qdict_get_str(qdict, "name");
176 bool new_state = qdict_get_bool(qdict, "option");
Lluís Vilanova77e2b172016-07-11 12:53:57 +0200177 bool has_vcpu = qdict_haskey(qdict, "vcpu");
178 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
Lluís Vilanova14101d02014-08-25 13:20:03 +0200179 Error *local_err = NULL;
Blue Swirlf871d682010-10-13 19:14:29 +0000180
Lluís Vilanova77e2b172016-07-11 12:53:57 +0200181 if (vcpu < 0) {
182 monitor_printf(mon, "argument vcpu must be positive");
183 return;
184 }
185
186 qmp_trace_event_set_state(tp_name, new_state, true, true, has_vcpu, vcpu, &local_err);
Lluís Vilanova14101d02014-08-25 13:20:03 +0200187 if (local_err) {
Markus Armbruster091e38b2015-02-10 15:15:43 +0100188 error_report_err(local_err);
Blue Swirlf871d682010-10-13 19:14:29 +0000189 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530190}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100191
Michael Rothc45a8162011-10-02 08:44:37 -0500192#ifdef CONFIG_TRACE_SIMPLE
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100193static void hmp_trace_file(Monitor *mon, const QDict *qdict)
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100194{
195 const char *op = qdict_get_try_str(qdict, "op");
196 const char *arg = qdict_get_try_str(qdict, "arg");
197
198 if (!op) {
Markus Armbrusterba4912c2019-04-17 21:17:50 +0200199 st_print_trace_file_status();
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100200 } else if (!strcmp(op, "on")) {
201 st_set_trace_file_enabled(true);
202 } else if (!strcmp(op, "off")) {
203 st_set_trace_file_enabled(false);
204 } else if (!strcmp(op, "flush")) {
205 st_flush_trace_buffer();
206 } else if (!strcmp(op, "set")) {
207 if (arg) {
208 st_set_trace_file(arg);
209 }
210 } else {
211 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
212 help_cmd(mon, "trace-file");
213 }
214}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530215#endif
216
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100217static void hmp_info_help(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000218{
Luiz Capitulino13c74252009-10-07 13:41:55 -0300219 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000220}
221
Peter Xu6adf08d2018-03-09 16:59:50 +0800222static void monitor_init_qmp_commands(void)
Marc-André Lureauedcfaef2016-09-12 13:19:00 +0400223{
Markus Armbruster635db182017-03-03 13:32:27 +0100224 /*
225 * Two command lists:
226 * - qmp_commands contains all QMP commands
227 * - qmp_cap_negotiation_commands contains just
228 * "qmp_capabilities", to enforce capability negotiation
229 */
230
Markus Armbruster1527bad2017-03-03 13:32:25 +0100231 qmp_init_marshal(&qmp_commands);
Markus Armbruster05875682017-03-03 13:32:24 +0100232
Markus Armbruster1527bad2017-03-03 13:32:25 +0100233 qmp_register_command(&qmp_commands, "device_add", qmp_device_add,
Marc-André Lureauedcfaef2016-09-12 13:19:00 +0400234 QCO_NO_OPTIONS);
Marc-André Lureau5032a162016-09-12 13:19:02 +0400235
Markus Armbruster635db182017-03-03 13:32:27 +0100236 QTAILQ_INIT(&qmp_cap_negotiation_commands);
237 qmp_register_command(&qmp_cap_negotiation_commands, "qmp_capabilities",
Igor Mammedovd6fe3d02018-05-11 18:51:43 +0200238 qmp_marshal_qmp_capabilities, QCO_ALLOW_PRECONFIG);
Markus Armbruster635db182017-03-03 13:32:27 +0100239}
240
Peter Xud9f25282018-06-08 11:55:07 +0800241/* Set the current CPU defined by the user. Callers must hold BQL. */
Kevin Wolfdcba65f2020-10-05 17:58:42 +0200242int monitor_set_cpu(Monitor *mon, int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000243{
Andreas Färber55e5c282012-12-17 06:18:02 +0100244 CPUState *cpu;
bellard6a00d602005-11-21 23:25:50 +0000245
Andreas Färber1c8bb3c2013-02-15 17:01:09 +0100246 cpu = qemu_get_cpu(cpu_index);
247 if (cpu == NULL) {
248 return -1;
bellard6a00d602005-11-21 23:25:50 +0000249 }
Kevin Wolfdcba65f2020-10-05 17:58:42 +0200250 g_free(mon->mon_cpu_path);
251 mon->mon_cpu_path = object_get_canonical_path(OBJECT(cpu));
Andreas Färber1c8bb3c2013-02-15 17:01:09 +0100252 return 0;
bellard6a00d602005-11-21 23:25:50 +0000253}
254
Peter Xud9f25282018-06-08 11:55:07 +0800255/* Callers must hold BQL. */
Kevin Wolf87e6f4a2020-10-05 17:58:43 +0200256static CPUState *mon_get_cpu_sync(Monitor *mon, bool synchronize)
bellard6a00d602005-11-21 23:25:50 +0000257{
Marc-André Lureaua85d0bf2019-10-04 15:18:20 +0400258 CPUState *cpu = NULL;
Greg Kurz751f8cf2017-10-17 10:16:22 +0200259
Kevin Wolf87e6f4a2020-10-05 17:58:43 +0200260 if (mon->mon_cpu_path) {
261 cpu = (CPUState *) object_resolve_path_type(mon->mon_cpu_path,
Greg Kurz751f8cf2017-10-17 10:16:22 +0200262 TYPE_CPU, NULL);
263 if (!cpu) {
Kevin Wolf87e6f4a2020-10-05 17:58:43 +0200264 g_free(mon->mon_cpu_path);
265 mon->mon_cpu_path = NULL;
Greg Kurz751f8cf2017-10-17 10:16:22 +0200266 }
267 }
Kevin Wolf87e6f4a2020-10-05 17:58:43 +0200268 if (!mon->mon_cpu_path) {
Thomas Huth854e67f2017-01-13 13:12:35 +0100269 if (!first_cpu) {
270 return NULL;
271 }
Kevin Wolf87e6f4a2020-10-05 17:58:43 +0200272 monitor_set_cpu(mon, first_cpu->cpu_index);
Greg Kurz751f8cf2017-10-17 10:16:22 +0200273 cpu = first_cpu;
bellard6a00d602005-11-21 23:25:50 +0000274 }
Marc-André Lureaua85d0bf2019-10-04 15:18:20 +0400275 assert(cpu != NULL);
Viktor Mihajlovski137b5cb2018-02-16 17:08:41 +0100276 if (synchronize) {
277 cpu_synchronize_state(cpu);
278 }
Greg Kurz751f8cf2017-10-17 10:16:22 +0200279 return cpu;
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700280}
281
Kevin Wolf2fc5d012020-11-13 12:43:24 +0100282CPUState *mon_get_cpu(Monitor *mon)
Viktor Mihajlovski137b5cb2018-02-16 17:08:41 +0100283{
Kevin Wolf2fc5d012020-11-13 12:43:24 +0100284 return mon_get_cpu_sync(mon, true);
Viktor Mihajlovski137b5cb2018-02-16 17:08:41 +0100285}
286
Kevin Wolfe7cff9c2020-11-13 12:43:26 +0100287CPUArchState *mon_get_cpu_env(Monitor *mon)
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700288{
Kevin Wolfe7cff9c2020-11-13 12:43:26 +0100289 CPUState *cs = mon_get_cpu(mon);
Thomas Huth854e67f2017-01-13 13:12:35 +0100290
291 return cs ? cs->env_ptr : NULL;
bellard6a00d602005-11-21 23:25:50 +0000292}
293
Kevin Wolf87e6f4a2020-10-05 17:58:43 +0200294int monitor_get_cpu_index(Monitor *mon)
Luiz Capitulino99b77962011-10-24 10:53:44 -0200295{
Kevin Wolf87e6f4a2020-10-05 17:58:43 +0200296 CPUState *cs = mon_get_cpu_sync(mon, false);
Thomas Huth854e67f2017-01-13 13:12:35 +0100297
298 return cs ? cs->cpu_index : UNASSIGNED_CPU_INDEX;
Luiz Capitulino99b77962011-10-24 10:53:44 -0200299}
300
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100301static void hmp_info_registers(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000302{
Suraj Jitindar Singh18f08282017-06-08 15:41:16 +1000303 bool all_cpus = qdict_get_try_bool(qdict, "cpustate_all", false);
304 CPUState *cs;
Thomas Huth854e67f2017-01-13 13:12:35 +0100305
Suraj Jitindar Singh18f08282017-06-08 15:41:16 +1000306 if (all_cpus) {
307 CPU_FOREACH(cs) {
308 monitor_printf(mon, "\nCPU#%d\n", cs->cpu_index);
Markus Armbruster90c84c52019-04-17 21:18:02 +0200309 cpu_dump_state(cs, NULL, CPU_DUMP_FPU);
Suraj Jitindar Singh18f08282017-06-08 15:41:16 +1000310 }
311 } else {
Kevin Wolf2fc5d012020-11-13 12:43:24 +0100312 cs = mon_get_cpu(mon);
Suraj Jitindar Singh18f08282017-06-08 15:41:16 +1000313
314 if (!cs) {
315 monitor_printf(mon, "No CPU available\n");
316 return;
317 }
318
Markus Armbruster90c84c52019-04-17 21:18:02 +0200319 cpu_dump_state(cs, NULL, CPU_DUMP_FPU);
Thomas Huth854e67f2017-01-13 13:12:35 +0100320 }
bellard9307c4c2004-04-04 12:57:25 +0000321}
322
Paolo Bonzinif0d14a92012-09-17 13:42:41 +0200323#ifdef CONFIG_TCG
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100324static void hmp_info_jit(Monitor *mon, const QDict *qdict)
bellarde3db7222005-01-26 22:00:47 +0000325{
Thomas Huthb7da97e2017-04-26 06:11:47 +0200326 if (!tcg_enabled()) {
327 error_report("JIT information is only available with accel=tcg");
328 return;
329 }
330
Markus Armbruster3de2faa2019-04-17 21:17:52 +0200331 dump_exec_info();
Markus Armbruster76c86612019-04-17 21:17:53 +0200332 dump_drift_info();
bellarde3db7222005-01-26 22:00:47 +0000333}
334
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100335static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
Max Filippov246ae242014-11-02 11:04:18 +0300336{
Markus Armbrusterd4c51a02019-04-17 21:17:51 +0200337 dump_opcount_info();
Max Filippov246ae242014-11-02 11:04:18 +0300338}
Paolo Bonzinif0d14a92012-09-17 13:42:41 +0200339#endif
Max Filippov246ae242014-11-02 11:04:18 +0300340
Emilio G. Cota97bfafe2017-08-08 13:54:42 -0400341static void hmp_info_sync_profile(Monitor *mon, const QDict *qdict)
342{
343 int64_t max = qdict_get_try_int(qdict, "max", 10);
344 bool mean = qdict_get_try_bool(qdict, "mean", false);
345 bool coalesce = !qdict_get_try_bool(qdict, "no_coalesce", false);
346 enum QSPSortBy sort_by;
347
348 sort_by = mean ? QSP_SORT_BY_AVG_WAIT_TIME : QSP_SORT_BY_TOTAL_WAIT_TIME;
Markus Armbrusterac7ff4c2019-04-17 21:17:54 +0200349 qsp_report(max, sort_by, coalesce);
Emilio G. Cota97bfafe2017-08-08 13:54:42 -0400350}
351
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100352static void hmp_info_history(Monitor *mon, const QDict *qdict)
bellardaa455482004-04-04 13:07:25 +0000353{
Kevin Wolf5f9dba12019-06-13 17:33:54 +0200354 MonitorHMP *hmp_mon = container_of(mon, MonitorHMP, common);
bellardaa455482004-04-04 13:07:25 +0000355 int i;
bellard7e2515e2004-08-01 21:52:19 +0000356 const char *str;
ths3b46e622007-09-17 08:09:54 +0000357
Kevin Wolf5f9dba12019-06-13 17:33:54 +0200358 if (!hmp_mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000359 return;
Kevin Wolf5f9dba12019-06-13 17:33:54 +0200360 }
bellard7e2515e2004-08-01 21:52:19 +0000361 i = 0;
362 for(;;) {
Kevin Wolf5f9dba12019-06-13 17:33:54 +0200363 str = readline_get_history(hmp_mon->rs, i);
364 if (!str) {
bellard7e2515e2004-08-01 21:52:19 +0000365 break;
Kevin Wolf5f9dba12019-06-13 17:33:54 +0200366 }
aliguori376253e2009-03-05 23:01:23 +0000367 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000368 i++;
bellardaa455482004-04-04 13:07:25 +0000369 }
370}
371
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100372static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530373{
Lluís Vilanovabd712112016-07-11 12:53:51 +0200374 const char *name = qdict_get_try_str(qdict, "name");
Lluís Vilanova77e2b172016-07-11 12:53:57 +0200375 bool has_vcpu = qdict_haskey(qdict, "vcpu");
376 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
Lluís Vilanovabd712112016-07-11 12:53:51 +0200377 TraceEventInfoList *events;
Lluís Vilanova14101d02014-08-25 13:20:03 +0200378 TraceEventInfoList *elem;
Lluís Vilanovabd712112016-07-11 12:53:51 +0200379 Error *local_err = NULL;
380
381 if (name == NULL) {
382 name = "*";
383 }
Lluís Vilanova77e2b172016-07-11 12:53:57 +0200384 if (vcpu < 0) {
385 monitor_printf(mon, "argument vcpu must be positive");
386 return;
387 }
Lluís Vilanovabd712112016-07-11 12:53:51 +0200388
Lluís Vilanova77e2b172016-07-11 12:53:57 +0200389 events = qmp_trace_event_get_state(name, has_vcpu, vcpu, &local_err);
Lluís Vilanovabd712112016-07-11 12:53:51 +0200390 if (local_err) {
391 error_report_err(local_err);
392 return;
393 }
Lluís Vilanova14101d02014-08-25 13:20:03 +0200394
395 for (elem = events; elem != NULL; elem = elem->next) {
396 monitor_printf(mon, "%s : state %u\n",
397 elem->value->name,
398 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
399 }
400 qapi_free_TraceEventInfoList(events);
Prerna Saxena22890ab2010-06-24 17:04:53 +0530401}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530402
Markus Armbrusterb8a185b2015-03-05 17:29:02 +0100403void qmp_client_migrate_info(const char *protocol, const char *hostname,
404 bool has_port, int64_t port,
405 bool has_tls_port, int64_t tls_port,
406 bool has_cert_subject, const char *cert_subject,
407 Error **errp)
Gerd Hoffmanne866e232010-04-23 13:28:21 +0200408{
Gerd Hoffmanne866e232010-04-23 13:28:21 +0200409 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb8a185b2015-03-05 17:29:02 +0100410 if (!qemu_using_spice(errp)) {
411 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +0200412 }
413
Markus Armbrusterb8a185b2015-03-05 17:29:02 +0100414 if (!has_port && !has_tls_port) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100415 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
Markus Armbrusterb8a185b2015-03-05 17:29:02 +0100416 return;
Yonit Halperin6ec5dae2012-03-18 09:42:39 +0200417 }
418
Gerd Hoffmann74774772020-10-19 09:52:12 +0200419 if (qemu_spice.migrate_info(hostname,
Markus Armbrusterb8a185b2015-03-05 17:29:02 +0100420 has_port ? port : -1,
421 has_tls_port ? tls_port : -1,
422 cert_subject)) {
Markus Armbruster9e1b9c62020-11-13 09:26:21 +0100423 error_setg(errp, "Could not set up display for migration");
Markus Armbrusterb8a185b2015-03-05 17:29:02 +0100424 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +0200425 }
Markus Armbrusterb8a185b2015-03-05 17:29:02 +0100426 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +0200427 }
428
Markus Armbruster99750d82020-11-13 09:26:22 +0100429 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "'spice'");
Gerd Hoffmanne866e232010-04-23 13:28:21 +0200430}
431
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100432static void hmp_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +0000433{
Markus Armbrusterdaa76aa2016-06-15 19:27:16 +0200434 Error *err = NULL;
435
436 qemu_set_log_filename(qdict_get_str(qdict, "filename"), &err);
437 if (err) {
438 error_report_err(err);
439 }
pbrooke735b912007-06-30 13:53:24 +0000440}
441
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100442static void hmp_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +0000443{
444 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300445 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +0000446
bellard9307c4c2004-04-04 12:57:25 +0000447 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +0000448 mask = 0;
449 } else {
Peter Maydell4fde1eb2013-02-11 16:41:22 +0000450 mask = qemu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +0000451 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +0000452 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +0000453 return;
454 }
455 }
Peter Maydell24537a02013-02-11 16:41:23 +0000456 qemu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +0000457}
458
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100459static void hmp_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +0000460{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300461 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +0000462 if (!option || !strcmp(option, "on")) {
463 singlestep = 1;
464 } else if (!strcmp(option, "off")) {
465 singlestep = 0;
466 } else {
467 monitor_printf(mon, "unexpected option %s\n", option);
468 }
469}
470
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100471static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +0000472{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300473 const char *device = qdict_get_try_str(qdict, "device");
Yutao Ai89854b92020-11-25 01:45:13 +0000474 if (!device) {
aliguori59030a82009-04-05 18:43:41 +0000475 device = "tcp::" DEFAULT_GDBSTUB_PORT;
Yutao Ai89854b92020-11-25 01:45:13 +0000476 }
477
aliguori59030a82009-04-05 18:43:41 +0000478 if (gdbserver_start(device) < 0) {
479 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
480 device);
481 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +0000482 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +0000483 } else {
aliguori59030a82009-04-05 18:43:41 +0000484 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
485 device);
bellard8a7ddc32004-03-31 19:00:16 +0000486 }
487}
488
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100489static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100490{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300491 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100492 if (select_watchdog_action(action) == -1) {
493 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
494 }
495}
496
aliguori376253e2009-03-05 23:01:23 +0000497static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +0000498{
aliguori376253e2009-03-05 23:01:23 +0000499 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000500 switch(c) {
501 case '\'':
aliguori376253e2009-03-05 23:01:23 +0000502 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +0000503 break;
504 case '\\':
aliguori376253e2009-03-05 23:01:23 +0000505 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +0000506 break;
507 case '\n':
aliguori376253e2009-03-05 23:01:23 +0000508 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +0000509 break;
510 case '\r':
aliguori376253e2009-03-05 23:01:23 +0000511 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +0000512 break;
513 default:
514 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +0000515 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +0000516 } else {
aliguori376253e2009-03-05 23:01:23 +0000517 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +0000518 }
519 break;
520 }
aliguori376253e2009-03-05 23:01:23 +0000521 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000522}
523
aliguori376253e2009-03-05 23:01:23 +0000524static void memory_dump(Monitor *mon, int count, int format, int wsize,
Avi Kivitya8170e52012-10-23 12:30:10 +0200525 hwaddr addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +0000526{
Blue Swirl23842aa2010-01-12 20:27:43 +0000527 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +0000528 uint8_t buf[16];
529 uint64_t v;
Kevin Wolf2fc5d012020-11-13 12:43:24 +0100530 CPUState *cs = mon_get_cpu(mon);
Thomas Huth854e67f2017-01-13 13:12:35 +0100531
532 if (!cs && (format == 'i' || !is_physical)) {
533 monitor_printf(mon, "Can not dump without CPU\n");
534 return;
535 }
bellard9307c4c2004-04-04 12:57:25 +0000536
537 if (format == 'i') {
Richard Henderson1d484742017-09-14 08:38:35 -0700538 monitor_disas(mon, cs, addr, count, is_physical);
bellard9307c4c2004-04-04 12:57:25 +0000539 return;
540 }
541
542 len = wsize * count;
Yutao Ai89854b92020-11-25 01:45:13 +0000543 if (wsize == 1) {
bellard9307c4c2004-04-04 12:57:25 +0000544 line_size = 8;
Yutao Ai89854b92020-11-25 01:45:13 +0000545 } else {
bellard9307c4c2004-04-04 12:57:25 +0000546 line_size = 16;
Yutao Ai89854b92020-11-25 01:45:13 +0000547 }
bellard9307c4c2004-04-04 12:57:25 +0000548 max_digits = 0;
549
550 switch(format) {
551 case 'o':
Marc-André Lureau69db8df2017-06-22 13:04:16 +0200552 max_digits = DIV_ROUND_UP(wsize * 8, 3);
bellard9307c4c2004-04-04 12:57:25 +0000553 break;
554 default:
555 case 'x':
556 max_digits = (wsize * 8) / 4;
557 break;
558 case 'u':
559 case 'd':
Marc-André Lureau69db8df2017-06-22 13:04:16 +0200560 max_digits = DIV_ROUND_UP(wsize * 8 * 10, 33);
bellard9307c4c2004-04-04 12:57:25 +0000561 break;
562 case 'c':
563 wsize = 1;
564 break;
565 }
566
567 while (len > 0) {
Yutao Ai89854b92020-11-25 01:45:13 +0000568 if (is_physical) {
aliguori376253e2009-03-05 23:01:23 +0000569 monitor_printf(mon, TARGET_FMT_plx ":", addr);
Yutao Ai89854b92020-11-25 01:45:13 +0000570 } else {
aliguori376253e2009-03-05 23:01:23 +0000571 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
Yutao Ai89854b92020-11-25 01:45:13 +0000572 }
bellard9307c4c2004-04-04 12:57:25 +0000573 l = len;
574 if (l > line_size)
575 l = line_size;
576 if (is_physical) {
Peter Maydell6f89ae52018-12-14 13:30:49 +0000577 AddressSpace *as = cs ? cs->as : &address_space_memory;
578 MemTxResult r = address_space_read(as, addr,
579 MEMTXATTRS_UNSPECIFIED, buf, l);
580 if (r != MEMTX_OK) {
581 monitor_printf(mon, " Cannot access memory\n");
582 break;
583 }
bellard9307c4c2004-04-04 12:57:25 +0000584 } else {
Thomas Huth854e67f2017-01-13 13:12:35 +0100585 if (cpu_memory_rw_debug(cs, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +0000586 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +0000587 break;
588 }
bellard9307c4c2004-04-04 12:57:25 +0000589 }
ths5fafdf22007-09-16 21:08:06 +0000590 i = 0;
bellard9307c4c2004-04-04 12:57:25 +0000591 while (i < l) {
592 switch(wsize) {
593 default:
594 case 1:
Peter Maydell24e60302015-01-20 15:19:32 +0000595 v = ldub_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000596 break;
597 case 2:
Peter Maydell24e60302015-01-20 15:19:32 +0000598 v = lduw_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000599 break;
600 case 4:
Peter Maydell24e60302015-01-20 15:19:32 +0000601 v = (uint32_t)ldl_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000602 break;
603 case 8:
Peter Maydell24e60302015-01-20 15:19:32 +0000604 v = ldq_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000605 break;
606 }
aliguori376253e2009-03-05 23:01:23 +0000607 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +0000608 switch(format) {
609 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000610 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000611 break;
612 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000613 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000614 break;
615 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000616 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000617 break;
618 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000619 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000620 break;
621 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000622 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +0000623 break;
624 }
625 i += wsize;
626 }
aliguori376253e2009-03-05 23:01:23 +0000627 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000628 addr += l;
629 len -= l;
630 }
631}
632
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100633static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000634{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300635 int count = qdict_get_int(qdict, "count");
636 int format = qdict_get_int(qdict, "format");
637 int size = qdict_get_int(qdict, "size");
638 target_long addr = qdict_get_int(qdict, "addr");
639
aliguori376253e2009-03-05 23:01:23 +0000640 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +0000641}
642
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100643static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000644{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300645 int count = qdict_get_int(qdict, "count");
646 int format = qdict_get_int(qdict, "format");
647 int size = qdict_get_int(qdict, "size");
Avi Kivitya8170e52012-10-23 12:30:10 +0200648 hwaddr addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300649
aliguori376253e2009-03-05 23:01:23 +0000650 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +0000651}
652
Tobin Feldman-Fitzthumc7f7e692020-10-27 13:03:03 -0400653void *gpa2hva(MemoryRegion **p_mr, hwaddr addr, uint64_t size, Error **errp)
Paolo Bonzinie9628442017-04-20 15:30:58 +0200654{
Tobin Feldman-Fitzthumc7f7e692020-10-27 13:03:03 -0400655 Int128 gpa_region_size;
Paolo Bonzinie9628442017-04-20 15:30:58 +0200656 MemoryRegionSection mrs = memory_region_find(get_system_memory(),
Tobin Feldman-Fitzthumc7f7e692020-10-27 13:03:03 -0400657 addr, size);
Paolo Bonzinie9628442017-04-20 15:30:58 +0200658
659 if (!mrs.mr) {
660 error_setg(errp, "No memory is mapped at address 0x%" HWADDR_PRIx, addr);
661 return NULL;
662 }
663
664 if (!memory_region_is_ram(mrs.mr) && !memory_region_is_romd(mrs.mr)) {
665 error_setg(errp, "Memory at address 0x%" HWADDR_PRIx "is not RAM", addr);
666 memory_region_unref(mrs.mr);
667 return NULL;
668 }
669
Tobin Feldman-Fitzthumc7f7e692020-10-27 13:03:03 -0400670 gpa_region_size = int128_make64(size);
671 if (int128_lt(mrs.size, gpa_region_size)) {
672 error_setg(errp, "Size of memory region at 0x%" HWADDR_PRIx
673 " exceeded.", addr);
674 memory_region_unref(mrs.mr);
675 return NULL;
676 }
677
Paolo Bonzinie9628442017-04-20 15:30:58 +0200678 *p_mr = mrs.mr;
679 return qemu_map_ram_ptr(mrs.mr->ram_block, mrs.offset_within_region);
680}
681
682static void hmp_gpa2hva(Monitor *mon, const QDict *qdict)
683{
684 hwaddr addr = qdict_get_int(qdict, "addr");
685 Error *local_err = NULL;
686 MemoryRegion *mr = NULL;
687 void *ptr;
688
Tobin Feldman-Fitzthumc7f7e692020-10-27 13:03:03 -0400689 ptr = gpa2hva(&mr, addr, 1, &local_err);
Paolo Bonzinie9628442017-04-20 15:30:58 +0200690 if (local_err) {
691 error_report_err(local_err);
692 return;
693 }
694
695 monitor_printf(mon, "Host virtual address for 0x%" HWADDR_PRIx
696 " (%s) is %p\n",
697 addr, mr->name, ptr);
698
699 memory_region_unref(mr);
700}
701
Dr. David Alan Gilbert574d9692019-04-12 16:26:52 +0100702static void hmp_gva2gpa(Monitor *mon, const QDict *qdict)
703{
704 target_ulong addr = qdict_get_int(qdict, "addr");
705 MemTxAttrs attrs;
Kevin Wolf2fc5d012020-11-13 12:43:24 +0100706 CPUState *cs = mon_get_cpu(mon);
Dr. David Alan Gilbert574d9692019-04-12 16:26:52 +0100707 hwaddr gpa;
708
709 if (!cs) {
710 monitor_printf(mon, "No cpu\n");
711 return;
712 }
713
Eduardo Habkost9d3250d2019-05-10 15:56:20 -0300714 gpa = cpu_get_phys_page_attrs_debug(cs, addr & TARGET_PAGE_MASK, &attrs);
Dr. David Alan Gilbert574d9692019-04-12 16:26:52 +0100715 if (gpa == -1) {
716 monitor_printf(mon, "Unmapped\n");
717 } else {
718 monitor_printf(mon, "gpa: %#" HWADDR_PRIx "\n",
719 gpa + (addr & ~TARGET_PAGE_MASK));
720 }
721}
722
Paolo Bonzinie9628442017-04-20 15:30:58 +0200723#ifdef CONFIG_LINUX
724static uint64_t vtop(void *ptr, Error **errp)
725{
726 uint64_t pinfo;
727 uint64_t ret = -1;
728 uintptr_t addr = (uintptr_t) ptr;
Wei Yang038adc22019-10-13 10:11:45 +0800729 uintptr_t pagesize = qemu_real_host_page_size;
Paolo Bonzinie9628442017-04-20 15:30:58 +0200730 off_t offset = addr / pagesize * sizeof(pinfo);
731 int fd;
732
733 fd = open("/proc/self/pagemap", O_RDONLY);
734 if (fd == -1) {
735 error_setg_errno(errp, errno, "Cannot open /proc/self/pagemap");
736 return -1;
737 }
738
739 /* Force copy-on-write if necessary. */
Stefan Hajnoczid73415a2020-09-23 11:56:46 +0100740 qatomic_add((uint8_t *)ptr, 0);
Paolo Bonzinie9628442017-04-20 15:30:58 +0200741
742 if (pread(fd, &pinfo, sizeof(pinfo), offset) != sizeof(pinfo)) {
743 error_setg_errno(errp, errno, "Cannot read pagemap");
744 goto out;
745 }
746 if ((pinfo & (1ull << 63)) == 0) {
747 error_setg(errp, "Page not present");
748 goto out;
749 }
750 ret = ((pinfo & 0x007fffffffffffffull) * pagesize) | (addr & (pagesize - 1));
751
752out:
753 close(fd);
754 return ret;
755}
756
757static void hmp_gpa2hpa(Monitor *mon, const QDict *qdict)
758{
759 hwaddr addr = qdict_get_int(qdict, "addr");
760 Error *local_err = NULL;
761 MemoryRegion *mr = NULL;
762 void *ptr;
763 uint64_t physaddr;
764
Tobin Feldman-Fitzthumc7f7e692020-10-27 13:03:03 -0400765 ptr = gpa2hva(&mr, addr, 1, &local_err);
Paolo Bonzinie9628442017-04-20 15:30:58 +0200766 if (local_err) {
767 error_report_err(local_err);
768 return;
769 }
770
771 physaddr = vtop(ptr, &local_err);
772 if (local_err) {
773 error_report_err(local_err);
774 } else {
775 monitor_printf(mon, "Host physical address for 0x%" HWADDR_PRIx
776 " (%s) is 0x%" PRIx64 "\n",
777 addr, mr->name, (uint64_t) physaddr);
778 }
779
780 memory_region_unref(mr);
781}
782#endif
783
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300784static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000785{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300786 int format = qdict_get_int(qdict, "format");
Avi Kivitya8170e52012-10-23 12:30:10 +0200787 hwaddr val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300788
bellard9307c4c2004-04-04 12:57:25 +0000789 switch(format) {
790 case 'o':
Avi Kivitya8170e52012-10-23 12:30:10 +0200791 monitor_printf(mon, "%#" HWADDR_PRIo, val);
bellard9307c4c2004-04-04 12:57:25 +0000792 break;
793 case 'x':
Avi Kivitya8170e52012-10-23 12:30:10 +0200794 monitor_printf(mon, "%#" HWADDR_PRIx, val);
bellard9307c4c2004-04-04 12:57:25 +0000795 break;
796 case 'u':
Avi Kivitya8170e52012-10-23 12:30:10 +0200797 monitor_printf(mon, "%" HWADDR_PRIu, val);
bellard9307c4c2004-04-04 12:57:25 +0000798 break;
799 default:
800 case 'd':
Avi Kivitya8170e52012-10-23 12:30:10 +0200801 monitor_printf(mon, "%" HWADDR_PRId, val);
bellard9307c4c2004-04-04 12:57:25 +0000802 break;
803 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000804 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +0000805 break;
806 }
aliguori376253e2009-03-05 23:01:23 +0000807 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000808}
809
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100810static void hmp_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +0000811{
812 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +0000813 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300814 uint32_t start = qdict_get_int(qdict, "start");
815 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +0000816
817 sum = 0;
818 for(addr = start; addr < (start + size); addr++) {
Peter Maydell42874d32015-04-26 16:49:24 +0100819 uint8_t val = address_space_ldub(&address_space_memory, addr,
820 MEMTXATTRS_UNSPECIFIED, NULL);
bellarde4cf1ad2005-06-04 20:15:57 +0000821 /* BSD sum algorithm ('sum' Unix command) */
822 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +0200823 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +0000824 }
aliguori376253e2009-03-05 23:01:23 +0000825 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +0000826}
827
bellard13224a82006-07-14 22:03:35 +0000828static int mouse_button_state;
829
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100830static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +0000831{
Gerd Hoffmannc751a742013-12-04 15:02:28 +0100832 int dx, dy, dz, button;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300833 const char *dx_str = qdict_get_str(qdict, "dx_str");
834 const char *dy_str = qdict_get_str(qdict, "dy_str");
835 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
Gerd Hoffmannc751a742013-12-04 15:02:28 +0100836
bellard13224a82006-07-14 22:03:35 +0000837 dx = strtol(dx_str, NULL, 0);
838 dy = strtol(dy_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +0100839 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
840 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
841
842 if (dz_str) {
bellard13224a82006-07-14 22:03:35 +0000843 dz = strtol(dz_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +0100844 if (dz != 0) {
Gerd Hoffmannf22d0af2016-01-12 12:14:12 +0100845 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
Gerd Hoffmannc751a742013-12-04 15:02:28 +0100846 qemu_input_queue_btn(NULL, button, true);
847 qemu_input_event_sync();
848 qemu_input_queue_btn(NULL, button, false);
849 }
850 }
851 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +0000852}
853
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100854static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +0000855{
Eric Blake7fb1cf12015-11-18 01:52:57 -0700856 static uint32_t bmap[INPUT_BUTTON__MAX] = {
Gerd Hoffmannc751a742013-12-04 15:02:28 +0100857 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
858 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
859 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
860 };
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300861 int button_state = qdict_get_int(qdict, "button_state");
Gerd Hoffmannc751a742013-12-04 15:02:28 +0100862
863 if (mouse_button_state == button_state) {
864 return;
865 }
866 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
867 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +0000868 mouse_button_state = button_state;
bellard13224a82006-07-14 22:03:35 +0000869}
870
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100871static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +0000872{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -0300873 int size = qdict_get_int(qdict, "size");
874 int addr = qdict_get_int(qdict, "addr");
875 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +0000876 uint32_t val;
877 int suffix;
878
879 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -0300880 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +0000881 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +0000882 addr++;
883 }
884 addr &= 0xffff;
885
886 switch(size) {
887 default:
888 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +0000889 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +0000890 suffix = 'b';
891 break;
892 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +0000893 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +0000894 suffix = 'w';
895 break;
896 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +0000897 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +0000898 suffix = 'l';
899 break;
900 }
Yutao Ai33b1fa92020-11-25 01:45:14 +0000901 monitor_printf(mon, "port%c[0x%04x] = 0x%0*x\n",
aliguori376253e2009-03-05 23:01:23 +0000902 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +0000903}
bellarda3a91a32004-06-04 11:06:21 +0000904
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100905static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +0200906{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300907 int size = qdict_get_int(qdict, "size");
908 int addr = qdict_get_int(qdict, "addr");
909 int val = qdict_get_int(qdict, "val");
910
Jan Kiszkaf1147842009-07-14 10:20:11 +0200911 addr &= IOPORTS_MASK;
912
913 switch (size) {
914 default:
915 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +0000916 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +0200917 break;
918 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +0000919 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +0200920 break;
921 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +0000922 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +0200923 break;
924 }
925}
926
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100927static void hmp_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +0000928{
Gongleif1839932014-12-03 18:20:58 +0000929 Error *local_err = NULL;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300930 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +0000931
Gongleif1839932014-12-03 18:20:58 +0000932 qemu_boot_set(bootdevice, &local_err);
933 if (local_err) {
Markus Armbruster193227f2015-12-18 16:35:06 +0100934 error_report_err(local_err);
aurel320ecdffb2008-05-04 20:11:34 +0000935 } else {
Gongleif1839932014-12-03 18:20:58 +0000936 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +0000937 }
938}
939
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100940static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
Blue Swirl314e2982011-09-11 20:22:05 +0000941{
Peter Xu57bb40c2017-01-16 16:40:05 +0800942 bool flatview = qdict_get_try_bool(qdict, "flatview", false);
Alexey Kardashevskiy5e8fd942017-09-21 18:51:06 +1000943 bool dispatch_tree = qdict_get_try_bool(qdict, "dispatch_tree", false);
Alexey Kardashevskiyfc051ae2018-06-04 13:25:11 +1000944 bool owner = qdict_get_try_bool(qdict, "owner", false);
Philippe Mathieu-Daudé2261d392020-05-29 14:53:25 +0200945 bool disabled = qdict_get_try_bool(qdict, "disabled", false);
Peter Xu57bb40c2017-01-16 16:40:05 +0800946
Philippe Mathieu-Daudé2261d392020-05-29 14:53:25 +0200947 mtree_info(flatview, dispatch_tree, owner, disabled);
Blue Swirl314e2982011-09-11 20:22:05 +0000948}
949
bellard5f1ce942006-02-08 22:40:15 +0000950#ifdef CONFIG_PROFILER
951
Aurelien Jarnoe9a66252009-09-30 14:09:52 +0200952int64_t dev_time;
953
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100954static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +0000955{
Emilio G. Cota72fd2ef2018-10-10 10:48:53 -0400956 static int64_t last_cpu_exec_time;
957 int64_t cpu_exec_time;
958 int64_t delta;
959
960 cpu_exec_time = tcg_cpu_exec_time();
961 delta = cpu_exec_time - last_cpu_exec_time;
962
aliguori376253e2009-03-05 23:01:23 +0000963 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Rutuja Shah73bcb242016-03-21 21:32:30 +0530964 dev_time, dev_time / (double)NANOSECONDS_PER_SECOND);
aliguori376253e2009-03-05 23:01:23 +0000965 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Emilio G. Cota72fd2ef2018-10-10 10:48:53 -0400966 delta, delta / (double)NANOSECONDS_PER_SECOND);
967 last_cpu_exec_time = cpu_exec_time;
bellard5f1ce942006-02-08 22:40:15 +0000968 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +0000969}
970#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100971static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +0000972{
aliguori376253e2009-03-05 23:01:23 +0000973 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +0000974}
975#endif
976
bellardec36b692006-07-16 18:57:03 +0000977/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +0000978static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +0000979
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100980static void hmp_info_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +0000981{
982 int i;
983 CaptureState *s;
984
985 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +0000986 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +0000987 s->ops.info (s->opaque);
988 }
989}
990
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100991static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +0000992{
993 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300994 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +0000995 CaptureState *s;
996
997 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
998 if (i == n) {
999 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001000 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001001 g_free (s);
bellardec36b692006-07-16 18:57:03 +00001002 return;
1003 }
1004 }
1005}
1006
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001007static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001008{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001009 const char *path = qdict_get_str(qdict, "path");
Kővágó, Zoltánf0b9f362019-08-19 01:06:48 +02001010 int freq = qdict_get_try_int(qdict, "freq", 44100);
1011 int bits = qdict_get_try_int(qdict, "bits", 16);
1012 int nchannels = qdict_get_try_int(qdict, "nchannels", 2);
1013 const char *audiodev = qdict_get_str(qdict, "audiodev");
bellardec36b692006-07-16 18:57:03 +00001014 CaptureState *s;
Kővágó, Zoltánf0b9f362019-08-19 01:06:48 +02001015 AudioState *as = audio_state_by_name(audiodev);
1016
1017 if (!as) {
1018 monitor_printf(mon, "Audiodev '%s' not found\n", audiodev);
1019 return;
1020 }
bellardec36b692006-07-16 18:57:03 +00001021
Anthony Liguori7267c092011-08-20 22:09:37 -05001022 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001023
Kővágó, Zoltánf0b9f362019-08-19 01:06:48 +02001024 if (wav_start_capture(as, s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001025 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05001026 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001027 return;
bellardec36b692006-07-16 18:57:03 +00001028 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001029 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001030}
bellardec36b692006-07-16 18:57:03 +00001031
Corey Bryant208c9d12012-06-22 14:36:09 -04001032void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001033{
Kevin Wolf947e4742020-10-05 17:58:44 +02001034 Monitor *cur_mon = monitor_cur();
Anthony Liguoric227f092009-10-01 16:12:16 -05001035 mon_fd_t *monfd;
Peter Xu9409fc02018-06-08 11:55:06 +08001036 int fd, tmp_fd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001037
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03001038 fd = qemu_chr_fe_get_msgfd(&cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001039 if (fd == -1) {
Markus Armbrusterf820af82020-11-13 09:26:18 +01001040 error_setg(errp, "No file descriptor supplied via SCM_RIGHTS");
Corey Bryant208c9d12012-06-22 14:36:09 -04001041 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001042 }
1043
1044 if (qemu_isdigit(fdname[0])) {
Stefan Hajnoczi0b9f0e22014-04-24 13:58:18 +02001045 close(fd);
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001046 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
1047 "a name not starting with a digit");
Corey Bryant208c9d12012-06-22 14:36:09 -04001048 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001049 }
1050
Dr. David Alan Gilbert0210c3b2020-09-22 10:57:41 +01001051 QEMU_LOCK_GUARD(&cur_mon->mon_lock);
Corey Bryant208c9d12012-06-22 14:36:09 -04001052 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001053 if (strcmp(monfd->name, fdname) != 0) {
1054 continue;
1055 }
1056
Peter Xu9409fc02018-06-08 11:55:06 +08001057 tmp_fd = monfd->fd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001058 monfd->fd = fd;
Markus Armbruster774a6b62018-07-03 10:53:57 +02001059 /* Make sure close() is outside critical section */
Peter Xu9409fc02018-06-08 11:55:06 +08001060 close(tmp_fd);
Corey Bryant208c9d12012-06-22 14:36:09 -04001061 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001062 }
1063
Anthony Liguori7267c092011-08-20 22:09:37 -05001064 monfd = g_malloc0(sizeof(mon_fd_t));
1065 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001066 monfd->fd = fd;
1067
Corey Bryant208c9d12012-06-22 14:36:09 -04001068 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001069}
1070
Corey Bryant208c9d12012-06-22 14:36:09 -04001071void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001072{
Kevin Wolf947e4742020-10-05 17:58:44 +02001073 Monitor *cur_mon = monitor_cur();
Anthony Liguoric227f092009-10-01 16:12:16 -05001074 mon_fd_t *monfd;
Peter Xu9409fc02018-06-08 11:55:06 +08001075 int tmp_fd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001076
Peter Xu9409fc02018-06-08 11:55:06 +08001077 qemu_mutex_lock(&cur_mon->mon_lock);
Corey Bryant208c9d12012-06-22 14:36:09 -04001078 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001079 if (strcmp(monfd->name, fdname) != 0) {
1080 continue;
1081 }
1082
Blue Swirl72cf2d42009-09-12 07:36:22 +00001083 QLIST_REMOVE(monfd, next);
Peter Xu9409fc02018-06-08 11:55:06 +08001084 tmp_fd = monfd->fd;
Anthony Liguori7267c092011-08-20 22:09:37 -05001085 g_free(monfd->name);
1086 g_free(monfd);
Peter Xu9409fc02018-06-08 11:55:06 +08001087 qemu_mutex_unlock(&cur_mon->mon_lock);
Markus Armbruster774a6b62018-07-03 10:53:57 +02001088 /* Make sure close() is outside critical section */
Peter Xu9409fc02018-06-08 11:55:06 +08001089 close(tmp_fd);
Corey Bryant208c9d12012-06-22 14:36:09 -04001090 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001091 }
1092
Peter Xu9409fc02018-06-08 11:55:06 +08001093 qemu_mutex_unlock(&cur_mon->mon_lock);
Markus Armbrusterf820af82020-11-13 09:26:18 +01001094 error_setg(errp, "File descriptor named '%s' not found", fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001095}
1096
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001097int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
Mark McLoughlin7768e042009-07-22 09:11:41 +01001098{
Anthony Liguoric227f092009-10-01 16:12:16 -05001099 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01001100
Dr. David Alan Gilbert0210c3b2020-09-22 10:57:41 +01001101 QEMU_LOCK_GUARD(&mon->mon_lock);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001102 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01001103 int fd;
1104
1105 if (strcmp(monfd->name, fdname) != 0) {
1106 continue;
1107 }
1108
1109 fd = monfd->fd;
1110
1111 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001112 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05001113 g_free(monfd->name);
1114 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001115
1116 return fd;
1117 }
1118
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001119 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001120 return -1;
1121}
1122
Corey Bryantba1c0482012-08-14 16:43:43 -04001123static void monitor_fdset_cleanup(MonFdset *mon_fdset)
1124{
1125 MonFdsetFd *mon_fdset_fd;
1126 MonFdsetFd *mon_fdset_fd_next;
1127
1128 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantebe52b52012-10-18 15:19:33 -04001129 if ((mon_fdset_fd->removed ||
1130 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
1131 runstate_is_running()) {
Corey Bryantba1c0482012-08-14 16:43:43 -04001132 close(mon_fdset_fd->fd);
1133 g_free(mon_fdset_fd->opaque);
1134 QLIST_REMOVE(mon_fdset_fd, next);
1135 g_free(mon_fdset_fd);
1136 }
1137 }
1138
Corey Bryantadb696f2012-08-14 16:43:47 -04001139 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04001140 QLIST_REMOVE(mon_fdset, next);
1141 g_free(mon_fdset);
1142 }
1143}
1144
Kevin Wolf7e3c0de2019-06-13 17:34:00 +02001145void monitor_fdsets_cleanup(void)
Corey Bryantefb87c12012-08-14 16:43:48 -04001146{
1147 MonFdset *mon_fdset;
1148 MonFdset *mon_fdset_next;
1149
Dr. David Alan Gilbert0210c3b2020-09-22 10:57:41 +01001150 QEMU_LOCK_GUARD(&mon_fdsets_lock);
Corey Bryantefb87c12012-08-14 16:43:48 -04001151 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
1152 monitor_fdset_cleanup(mon_fdset);
1153 }
1154}
1155
Corey Bryantba1c0482012-08-14 16:43:43 -04001156AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
1157 const char *opaque, Error **errp)
1158{
1159 int fd;
Kevin Wolf947e4742020-10-05 17:58:44 +02001160 Monitor *mon = monitor_cur();
Corey Bryantba1c0482012-08-14 16:43:43 -04001161 AddfdInfo *fdinfo;
1162
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03001163 fd = qemu_chr_fe_get_msgfd(&mon->chr);
Corey Bryantba1c0482012-08-14 16:43:43 -04001164 if (fd == -1) {
Markus Armbrusterf820af82020-11-13 09:26:18 +01001165 error_setg(errp, "No file descriptor supplied via SCM_RIGHTS");
Corey Bryantba1c0482012-08-14 16:43:43 -04001166 goto error;
1167 }
1168
Corey Bryante446f702012-10-18 15:19:32 -04001169 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
1170 has_opaque, opaque, errp);
1171 if (fdinfo) {
1172 return fdinfo;
Corey Bryant9ac54af2012-10-18 15:19:31 -04001173 }
1174
Corey Bryantba1c0482012-08-14 16:43:43 -04001175error:
1176 if (fd != -1) {
1177 close(fd);
1178 }
1179 return NULL;
1180}
1181
1182void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
1183{
1184 MonFdset *mon_fdset;
1185 MonFdsetFd *mon_fdset_fd;
1186 char fd_str[60];
1187
Dr. David Alan Gilbert0210c3b2020-09-22 10:57:41 +01001188 QEMU_LOCK_GUARD(&mon_fdsets_lock);
Corey Bryantba1c0482012-08-14 16:43:43 -04001189 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1190 if (mon_fdset->id != fdset_id) {
1191 continue;
1192 }
1193 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1194 if (has_fd) {
1195 if (mon_fdset_fd->fd != fd) {
1196 continue;
1197 }
1198 mon_fdset_fd->removed = true;
1199 break;
1200 } else {
1201 mon_fdset_fd->removed = true;
1202 }
1203 }
1204 if (has_fd && !mon_fdset_fd) {
1205 goto error;
1206 }
1207 monitor_fdset_cleanup(mon_fdset);
1208 return;
1209 }
1210
1211error:
1212 if (has_fd) {
1213 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
1214 fdset_id, fd);
1215 } else {
1216 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
1217 }
Markus Armbrusterf820af82020-11-13 09:26:18 +01001218 error_setg(errp, "File descriptor named '%s' not found", fd_str);
Corey Bryantba1c0482012-08-14 16:43:43 -04001219}
1220
1221FdsetInfoList *qmp_query_fdsets(Error **errp)
1222{
1223 MonFdset *mon_fdset;
1224 MonFdsetFd *mon_fdset_fd;
1225 FdsetInfoList *fdset_list = NULL;
1226
Dr. David Alan Gilbert0210c3b2020-09-22 10:57:41 +01001227 QEMU_LOCK_GUARD(&mon_fdsets_lock);
Corey Bryantba1c0482012-08-14 16:43:43 -04001228 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
Eric Blake54aa3de2020-11-12 19:13:37 -06001229 FdsetInfo *fdset_info = g_malloc0(sizeof(*fdset_info));
Corey Bryantba1c0482012-08-14 16:43:43 -04001230
Eric Blake54aa3de2020-11-12 19:13:37 -06001231 fdset_info->fdset_id = mon_fdset->id;
Corey Bryantba1c0482012-08-14 16:43:43 -04001232
1233 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
Eric Blake54aa3de2020-11-12 19:13:37 -06001234 FdsetFdInfo *fdsetfd_info;
Corey Bryantba1c0482012-08-14 16:43:43 -04001235
1236 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
Eric Blake54aa3de2020-11-12 19:13:37 -06001237 fdsetfd_info->fd = mon_fdset_fd->fd;
Corey Bryantba1c0482012-08-14 16:43:43 -04001238 if (mon_fdset_fd->opaque) {
Eric Blake54aa3de2020-11-12 19:13:37 -06001239 fdsetfd_info->has_opaque = true;
1240 fdsetfd_info->opaque = g_strdup(mon_fdset_fd->opaque);
Corey Bryantba1c0482012-08-14 16:43:43 -04001241 } else {
Eric Blake54aa3de2020-11-12 19:13:37 -06001242 fdsetfd_info->has_opaque = false;
Corey Bryantba1c0482012-08-14 16:43:43 -04001243 }
1244
Eric Blake54aa3de2020-11-12 19:13:37 -06001245 QAPI_LIST_PREPEND(fdset_info->fds, fdsetfd_info);
Corey Bryantba1c0482012-08-14 16:43:43 -04001246 }
1247
Eric Blake54aa3de2020-11-12 19:13:37 -06001248 QAPI_LIST_PREPEND(fdset_list, fdset_info);
Corey Bryantba1c0482012-08-14 16:43:43 -04001249 }
1250
1251 return fdset_list;
1252}
1253
Corey Bryante446f702012-10-18 15:19:32 -04001254AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
1255 bool has_opaque, const char *opaque,
1256 Error **errp)
1257{
1258 MonFdset *mon_fdset = NULL;
1259 MonFdsetFd *mon_fdset_fd;
1260 AddfdInfo *fdinfo;
1261
Daniel Brodsky6e8a3552020-04-03 21:21:08 -07001262 QEMU_LOCK_GUARD(&mon_fdsets_lock);
Corey Bryante446f702012-10-18 15:19:32 -04001263 if (has_fdset_id) {
1264 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1265 /* Break if match found or match impossible due to ordering by ID */
1266 if (fdset_id <= mon_fdset->id) {
1267 if (fdset_id < mon_fdset->id) {
1268 mon_fdset = NULL;
1269 }
1270 break;
1271 }
1272 }
1273 }
1274
1275 if (mon_fdset == NULL) {
1276 int64_t fdset_id_prev = -1;
1277 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
1278
1279 if (has_fdset_id) {
1280 if (fdset_id < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001281 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
1282 "a non-negative value");
Corey Bryante446f702012-10-18 15:19:32 -04001283 return NULL;
1284 }
1285 /* Use specified fdset ID */
1286 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1287 mon_fdset_cur = mon_fdset;
1288 if (fdset_id < mon_fdset_cur->id) {
1289 break;
1290 }
1291 }
1292 } else {
1293 /* Use first available fdset ID */
1294 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1295 mon_fdset_cur = mon_fdset;
1296 if (fdset_id_prev == mon_fdset_cur->id - 1) {
1297 fdset_id_prev = mon_fdset_cur->id;
1298 continue;
1299 }
1300 break;
1301 }
1302 }
1303
1304 mon_fdset = g_malloc0(sizeof(*mon_fdset));
1305 if (has_fdset_id) {
1306 mon_fdset->id = fdset_id;
1307 } else {
1308 mon_fdset->id = fdset_id_prev + 1;
1309 }
1310
1311 /* The fdset list is ordered by fdset ID */
1312 if (!mon_fdset_cur) {
1313 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
1314 } else if (mon_fdset->id < mon_fdset_cur->id) {
1315 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
1316 } else {
1317 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
1318 }
1319 }
1320
1321 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
1322 mon_fdset_fd->fd = fd;
1323 mon_fdset_fd->removed = false;
1324 if (has_opaque) {
1325 mon_fdset_fd->opaque = g_strdup(opaque);
1326 }
1327 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
1328
1329 fdinfo = g_malloc0(sizeof(*fdinfo));
1330 fdinfo->fdset_id = mon_fdset->id;
1331 fdinfo->fd = mon_fdset_fd->fd;
1332
1333 return fdinfo;
1334}
1335
Daniel P. Berrangé60efffa2020-08-27 13:27:00 +01001336int monitor_fdset_dup_fd_add(int64_t fdset_id, int flags)
Corey Bryantadb696f2012-08-14 16:43:47 -04001337{
Peter Xu47451462018-06-08 11:55:11 +08001338#ifdef _WIN32
1339 return -ENOENT;
1340#else
Corey Bryantadb696f2012-08-14 16:43:47 -04001341 MonFdset *mon_fdset;
Corey Bryantadb696f2012-08-14 16:43:47 -04001342
Dr. David Alan Gilbert0210c3b2020-09-22 10:57:41 +01001343 QEMU_LOCK_GUARD(&mon_fdsets_lock);
Corey Bryantadb696f2012-08-14 16:43:47 -04001344 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
Daniel P. Berrangé60efffa2020-08-27 13:27:00 +01001345 MonFdsetFd *mon_fdset_fd;
1346 MonFdsetFd *mon_fdset_fd_dup;
1347 int fd = -1;
1348 int dup_fd;
1349 int mon_fd_flags;
1350
Corey Bryantadb696f2012-08-14 16:43:47 -04001351 if (mon_fdset->id != fdset_id) {
1352 continue;
1353 }
Daniel P. Berrangé60efffa2020-08-27 13:27:00 +01001354
Corey Bryantadb696f2012-08-14 16:43:47 -04001355 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1356 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
1357 if (mon_fd_flags == -1) {
Daniel P. Berrangé60efffa2020-08-27 13:27:00 +01001358 return -1;
Corey Bryantadb696f2012-08-14 16:43:47 -04001359 }
1360
1361 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
Daniel P. Berrangé60efffa2020-08-27 13:27:00 +01001362 fd = mon_fdset_fd->fd;
1363 break;
Corey Bryantadb696f2012-08-14 16:43:47 -04001364 }
1365 }
Corey Bryantadb696f2012-08-14 16:43:47 -04001366
Daniel P. Berrangé60efffa2020-08-27 13:27:00 +01001367 if (fd == -1) {
Daniel P. Berrangé60efffa2020-08-27 13:27:00 +01001368 errno = EACCES;
1369 return -1;
Corey Bryantadb696f2012-08-14 16:43:47 -04001370 }
Daniel P. Berrangé60efffa2020-08-27 13:27:00 +01001371
1372 dup_fd = qemu_dup_flags(fd, flags);
1373 if (dup_fd == -1) {
Daniel P. Berrangé60efffa2020-08-27 13:27:00 +01001374 return -1;
Corey Bryantadb696f2012-08-14 16:43:47 -04001375 }
Daniel P. Berrangé60efffa2020-08-27 13:27:00 +01001376
Corey Bryantadb696f2012-08-14 16:43:47 -04001377 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
1378 mon_fdset_fd_dup->fd = dup_fd;
1379 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
Daniel P. Berrangé60efffa2020-08-27 13:27:00 +01001380 return dup_fd;
Corey Bryantadb696f2012-08-14 16:43:47 -04001381 }
Peter Xu47451462018-06-08 11:55:11 +08001382
Daniel P. Berrangé60efffa2020-08-27 13:27:00 +01001383 errno = ENOENT;
Corey Bryantadb696f2012-08-14 16:43:47 -04001384 return -1;
Daniel P. Berrangé60efffa2020-08-27 13:27:00 +01001385#endif
Corey Bryantadb696f2012-08-14 16:43:47 -04001386}
1387
Yury Kotov854f63d2019-05-23 12:44:33 +03001388static int64_t monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
Corey Bryantadb696f2012-08-14 16:43:47 -04001389{
1390 MonFdset *mon_fdset;
1391 MonFdsetFd *mon_fdset_fd_dup;
1392
Dr. David Alan Gilbert0210c3b2020-09-22 10:57:41 +01001393 QEMU_LOCK_GUARD(&mon_fdsets_lock);
Corey Bryantadb696f2012-08-14 16:43:47 -04001394 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1395 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
1396 if (mon_fdset_fd_dup->fd == dup_fd) {
1397 if (remove) {
1398 QLIST_REMOVE(mon_fdset_fd_dup, next);
Chen Quna6616142020-01-15 15:20:16 +08001399 g_free(mon_fdset_fd_dup);
Corey Bryantadb696f2012-08-14 16:43:47 -04001400 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
1401 monitor_fdset_cleanup(mon_fdset);
1402 }
Dr. David Alan Gilbert0210c3b2020-09-22 10:57:41 +01001403 return -1;
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02001404 } else {
1405 return mon_fdset->id;
Corey Bryantadb696f2012-08-14 16:43:47 -04001406 }
Corey Bryantadb696f2012-08-14 16:43:47 -04001407 }
1408 }
1409 }
Peter Xu47451462018-06-08 11:55:11 +08001410
Corey Bryantadb696f2012-08-14 16:43:47 -04001411 return -1;
1412}
1413
Yury Kotov854f63d2019-05-23 12:44:33 +03001414int64_t monitor_fdset_dup_fd_find(int dup_fd)
Corey Bryantadb696f2012-08-14 16:43:47 -04001415{
1416 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
1417}
1418
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02001419void monitor_fdset_dup_fd_remove(int dup_fd)
Corey Bryantadb696f2012-08-14 16:43:47 -04001420{
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02001421 monitor_fdset_dup_fd_find_remove(dup_fd, true);
Corey Bryantadb696f2012-08-14 16:43:47 -04001422}
1423
Markus Armbruster1677f4c2015-02-09 14:03:19 +01001424int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
Laszlo Ersek59063662014-04-10 10:24:31 +02001425{
1426 int fd;
1427 Error *local_err = NULL;
1428
1429 if (!qemu_isdigit(fdname[0]) && mon) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001430 fd = monitor_get_fd(mon, fdname, &local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00001431 } else {
1432 fd = qemu_parse_fd(fdname);
Laszlo Ersek59063662014-04-10 10:24:31 +02001433 if (fd == -1) {
1434 error_setg(&local_err, "Invalid file descriptor number '%s'",
1435 fdname);
1436 }
1437 }
1438 if (local_err) {
1439 error_propagate(errp, local_err);
1440 assert(fd == -1);
1441 } else {
1442 assert(fd != -1);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00001443 }
1444
1445 return fd;
1446}
1447
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03001448/* Please update hmp-commands.hx when adding or changing commands */
Kevin Wolfa0cd5e12019-06-13 17:33:56 +02001449static HMPCommand hmp_info_cmds[] = {
Pavel Butsykinda76ee72015-09-10 18:38:58 +03001450#include "hmp-commands-info.h"
1451 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00001452};
1453
Kevin Wolfa0cd5e12019-06-13 17:33:56 +02001454/* hmp_cmds and hmp_info_cmds would be sorted at runtime */
Kevin Wolfed7bda52019-06-13 17:34:01 +02001455HMPCommand hmp_cmds[] = {
Wenchao Xiaa13ced52013-01-14 14:06:28 +08001456#include "hmp-commands.h"
1457 { NULL, NULL, },
1458};
1459
Kevin Wolfed7bda52019-06-13 17:34:01 +02001460/*
1461 * Set @pval to the value in the register identified by @name.
1462 * return 0 if OK, -1 if not found
1463 */
Kevin Wolf2fc5d012020-11-13 12:43:24 +01001464int get_monitor_def(Monitor *mon, int64_t *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00001465{
Pavel Butsykinbf957282015-09-10 18:38:59 +03001466 const MonitorDef *md = target_monitor_defs();
Kevin Wolf2fc5d012020-11-13 12:43:24 +01001467 CPUState *cs = mon_get_cpu(mon);
bellard92a31b12005-02-10 22:00:52 +00001468 void *ptr;
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11001469 uint64_t tmp = 0;
1470 int ret;
bellard92a31b12005-02-10 22:00:52 +00001471
Thomas Huth854e67f2017-01-13 13:12:35 +01001472 if (cs == NULL || md == NULL) {
Pavel Butsykinbf957282015-09-10 18:38:59 +03001473 return -1;
1474 }
1475
1476 for(; md->name != NULL; md++) {
Kevin Wolfed7bda52019-06-13 17:34:01 +02001477 if (hmp_compare_cmd(name, md->name)) {
bellard9307c4c2004-04-04 12:57:25 +00001478 if (md->get_value) {
Kevin Wolf43cf0672020-11-13 12:43:25 +01001479 *pval = md->get_value(mon, md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00001480 } else {
Kevin Wolfe7cff9c2020-11-13 12:43:26 +01001481 CPUArchState *env = mon_get_cpu_env(mon);
bellard6a00d602005-11-21 23:25:50 +00001482 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00001483 switch(md->type) {
1484 case MD_I32:
1485 *pval = *(int32_t *)ptr;
1486 break;
1487 case MD_TLONG:
1488 *pval = *(target_long *)ptr;
1489 break;
1490 default:
1491 *pval = 0;
1492 break;
1493 }
bellard9307c4c2004-04-04 12:57:25 +00001494 }
1495 return 0;
1496 }
1497 }
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11001498
Thomas Huth854e67f2017-01-13 13:12:35 +01001499 ret = target_get_monitor_def(cs, name, &tmp);
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11001500 if (!ret) {
1501 *pval = (target_long) tmp;
1502 }
1503
1504 return ret;
bellard9307c4c2004-04-04 12:57:25 +00001505}
1506
Hani Benhabiles40d19392014-05-07 23:41:30 +01001507static void add_completion_option(ReadLineState *rs, const char *str,
1508 const char *option)
1509{
1510 if (!str || !option) {
1511 return;
1512 }
1513 if (!strncmp(option, str, strlen(str))) {
1514 readline_add_completion(rs, option);
1515 }
1516}
1517
Hani Benhabiles13e315d2014-05-07 23:41:29 +01001518void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
1519{
1520 size_t len;
1521 ChardevBackendInfoList *list, *start;
1522
1523 if (nb_args != 2) {
1524 return;
1525 }
1526 len = strlen(str);
1527 readline_set_completion_index(rs, len);
1528
1529 start = list = qmp_query_chardev_backends(NULL);
1530 while (list) {
1531 const char *chr_name = list->value->name;
1532
1533 if (!strncmp(chr_name, str, len)) {
1534 readline_add_completion(rs, chr_name);
1535 }
1536 list = list->next;
1537 }
1538 qapi_free_ChardevBackendInfoList(start);
1539}
1540
Hani Benhabilesb162b492014-05-07 23:41:31 +01001541void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
1542{
1543 size_t len;
1544 int i;
1545
1546 if (nb_args != 2) {
1547 return;
1548 }
1549 len = strlen(str);
1550 readline_set_completion_index(rs, len);
Markus Armbruster1c236ba2017-08-24 10:46:06 +02001551 for (i = 0; i < NET_CLIENT_DRIVER__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02001552 add_completion_option(rs, str, NetClientDriver_str(i));
Hani Benhabilesb162b492014-05-07 23:41:31 +01001553 }
1554}
1555
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01001556void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles992d3e62014-02-06 23:30:11 +01001557{
1558 GSList *list, *elt;
1559 size_t len;
1560
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01001561 if (nb_args != 2) {
1562 return;
1563 }
1564
Hani Benhabiles992d3e62014-02-06 23:30:11 +01001565 len = strlen(str);
1566 readline_set_completion_index(rs, len);
1567 list = elt = object_class_get_list(TYPE_DEVICE, false);
1568 while (elt) {
1569 const char *name;
1570 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
1571 TYPE_DEVICE);
1572 name = object_class_get_name(OBJECT_CLASS(dc));
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01001573
Eduardo Habkoste90f2a82017-05-03 17:35:44 -03001574 if (dc->user_creatable
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01001575 && !strncmp(name, str, len)) {
Hani Benhabiles992d3e62014-02-06 23:30:11 +01001576 readline_add_completion(rs, name);
1577 }
1578 elt = elt->next;
1579 }
1580 g_slist_free(list);
1581}
1582
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01001583void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles1094fd32014-02-06 23:30:13 +01001584{
1585 GSList *list, *elt;
1586 size_t len;
1587
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01001588 if (nb_args != 2) {
1589 return;
1590 }
1591
Hani Benhabiles1094fd32014-02-06 23:30:13 +01001592 len = strlen(str);
1593 readline_set_completion_index(rs, len);
1594 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
1595 while (elt) {
1596 const char *name;
1597
1598 name = object_class_get_name(OBJECT_CLASS(elt->data));
1599 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
1600 readline_add_completion(rs, name);
1601 }
1602 elt = elt->next;
1603 }
1604 g_slist_free(list);
1605}
1606
Marc-André Lureau91590152020-01-10 19:30:17 +04001607static int qdev_add_hotpluggable_device(Object *obj, void *opaque)
1608{
1609 GSList **list = opaque;
Philippe Mathieu-Daudé688ffbb2020-05-12 09:00:19 +02001610 DeviceState *dev = (DeviceState *)object_dynamic_cast(obj, TYPE_DEVICE);
Marc-André Lureau91590152020-01-10 19:30:17 +04001611
1612 if (dev == NULL) {
1613 return 0;
1614 }
1615
1616 if (dev->realized && object_property_get_bool(obj, "hotpluggable", NULL)) {
1617 *list = g_slist_append(*list, dev);
1618 }
1619
1620 return 0;
1621}
1622
1623static GSList *qdev_build_hotpluggable_device_list(Object *peripheral)
1624{
1625 GSList *list = NULL;
1626
1627 object_child_foreach(peripheral, qdev_add_hotpluggable_device, &list);
1628
1629 return list;
1630}
1631
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08001632static void peripheral_device_del_completion(ReadLineState *rs,
1633 const char *str, size_t len)
1634{
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02001635 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
1636 GSList *list, *item;
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08001637
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02001638 list = qdev_build_hotpluggable_device_list(peripheral);
1639 if (!list) {
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08001640 return;
1641 }
1642
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08001643 for (item = list; item; item = g_slist_next(item)) {
1644 DeviceState *dev = item->data;
1645
1646 if (dev->id && !strncmp(str, dev->id, len)) {
1647 readline_add_completion(rs, dev->id);
1648 }
1649 }
1650
1651 g_slist_free(list);
1652}
1653
Hani Benhabiles6297d9a2014-05-07 23:41:28 +01001654void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
1655{
1656 size_t len;
1657 ChardevInfoList *list, *start;
1658
1659 if (nb_args != 2) {
1660 return;
1661 }
1662 len = strlen(str);
1663 readline_set_completion_index(rs, len);
1664
1665 start = list = qmp_query_chardev(NULL);
1666 while (list) {
1667 ChardevInfo *chr = list->value;
1668
1669 if (!strncmp(chr->label, str, len)) {
1670 readline_add_completion(rs, chr->label);
1671 }
1672 list = list->next;
1673 }
1674 qapi_free_ChardevInfoList(start);
1675}
1676
Hani Benhabiles8e597772014-05-27 23:39:30 +01001677static void ringbuf_completion(ReadLineState *rs, const char *str)
1678{
1679 size_t len;
1680 ChardevInfoList *list, *start;
1681
1682 len = strlen(str);
1683 readline_set_completion_index(rs, len);
1684
1685 start = list = qmp_query_chardev(NULL);
1686 while (list) {
1687 ChardevInfo *chr_info = list->value;
1688
1689 if (!strncmp(chr_info->label, str, len)) {
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03001690 Chardev *chr = qemu_chr_find(chr_info->label);
Marc-André Lureau777357d2016-12-07 18:39:10 +03001691 if (chr && CHARDEV_IS_RINGBUF(chr)) {
Hani Benhabiles8e597772014-05-27 23:39:30 +01001692 readline_add_completion(rs, chr_info->label);
1693 }
1694 }
1695 list = list->next;
1696 }
1697 qapi_free_ChardevInfoList(start);
1698}
1699
Hani Benhabiles8e597772014-05-27 23:39:30 +01001700void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
1701{
1702 if (nb_args != 2) {
1703 return;
1704 }
1705 ringbuf_completion(rs, str);
1706}
1707
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01001708void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
1709{
1710 size_t len;
1711
1712 if (nb_args != 2) {
1713 return;
1714 }
1715
1716 len = strlen(str);
1717 readline_set_completion_index(rs, len);
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08001718 peripheral_device_del_completion(rs, str, len);
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01001719}
1720
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01001721void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabilesb48fa072014-02-06 23:30:12 +01001722{
1723 ObjectPropertyInfoList *list, *start;
1724 size_t len;
1725
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01001726 if (nb_args != 2) {
1727 return;
1728 }
Hani Benhabilesb48fa072014-02-06 23:30:12 +01001729 len = strlen(str);
1730 readline_set_completion_index(rs, len);
1731
1732 start = list = qmp_qom_list("/objects", NULL);
1733 while (list) {
1734 ObjectPropertyInfo *info = list->value;
1735
1736 if (!strncmp(info->type, "child<", 5)
1737 && !strncmp(info->name, str, len)) {
1738 readline_add_completion(rs, info->name);
1739 }
1740 list = list->next;
1741 }
1742 qapi_free_ObjectPropertyInfoList(start);
1743}
1744
Hani Benhabiles29136cd2014-05-07 23:41:27 +01001745void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
1746{
1747 int i;
1748 char *sep;
1749 size_t len;
1750
1751 if (nb_args != 2) {
1752 return;
1753 }
1754 sep = strrchr(str, '-');
1755 if (sep) {
1756 str = sep + 1;
1757 }
1758 len = strlen(str);
1759 readline_set_completion_index(rs, len);
Eric Blake7fb1cf12015-11-18 01:52:57 -07001760 for (i = 0; i < Q_KEY_CODE__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02001761 if (!strncmp(str, QKeyCode_str(i), len)) {
1762 readline_add_completion(rs, QKeyCode_str(i));
Hani Benhabiles29136cd2014-05-07 23:41:27 +01001763 }
1764 }
1765}
1766
Hani Benhabiles40d19392014-05-07 23:41:30 +01001767void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
1768{
1769 size_t len;
1770
1771 len = strlen(str);
1772 readline_set_completion_index(rs, len);
1773 if (nb_args == 2) {
Jason Wangeaed4832015-04-23 14:21:38 +08001774 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles40d19392014-05-07 23:41:30 +01001775 int count, i;
1776 count = qemu_find_net_clients_except(NULL, ncs,
Eric Blakef394b2e2016-07-13 21:50:23 -06001777 NET_CLIENT_DRIVER_NONE,
Jason Wangeaed4832015-04-23 14:21:38 +08001778 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08001779 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles40d19392014-05-07 23:41:30 +01001780 const char *name = ncs[i]->name;
1781 if (!strncmp(str, name, len)) {
1782 readline_add_completion(rs, name);
1783 }
1784 }
1785 } else if (nb_args == 3) {
1786 add_completion_option(rs, str, "on");
1787 add_completion_option(rs, str, "off");
1788 }
1789}
1790
Hani Benhabiles11b389f2014-05-07 23:41:32 +01001791void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
1792{
1793 int len, count, i;
Jason Wangeaed4832015-04-23 14:21:38 +08001794 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles11b389f2014-05-07 23:41:32 +01001795
1796 if (nb_args != 2) {
1797 return;
1798 }
1799
1800 len = strlen(str);
1801 readline_set_completion_index(rs, len);
Eric Blakef394b2e2016-07-13 21:50:23 -06001802 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_DRIVER_NIC,
Jason Wangeaed4832015-04-23 14:21:38 +08001803 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08001804 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles11b389f2014-05-07 23:41:32 +01001805 const char *name = ncs[i]->name;
1806 if (strncmp(str, name, len)) {
1807 continue;
1808 }
Eric Blake08712fc2020-03-17 15:17:11 -05001809 if (ncs[i]->is_netdev) {
Hani Benhabiles11b389f2014-05-07 23:41:32 +01001810 readline_add_completion(rs, name);
1811 }
1812 }
1813}
1814
Lluís Vilanovabd712112016-07-11 12:53:51 +02001815void info_trace_events_completion(ReadLineState *rs, int nb_args, const char *str)
1816{
1817 size_t len;
1818
1819 len = strlen(str);
1820 readline_set_completion_index(rs, len);
1821 if (nb_args == 2) {
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01001822 TraceEventIter iter;
1823 TraceEvent *ev;
1824 char *pattern = g_strdup_printf("%s*", str);
1825 trace_event_iter_init(&iter, pattern);
1826 while ((ev = trace_event_iter_next(&iter)) != NULL) {
1827 readline_add_completion(rs, trace_event_get_name(ev));
Lluís Vilanovabd712112016-07-11 12:53:51 +02001828 }
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01001829 g_free(pattern);
Lluís Vilanovabd712112016-07-11 12:53:51 +02001830 }
1831}
1832
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01001833void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
1834{
1835 size_t len;
1836
1837 len = strlen(str);
1838 readline_set_completion_index(rs, len);
1839 if (nb_args == 2) {
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01001840 TraceEventIter iter;
1841 TraceEvent *ev;
1842 char *pattern = g_strdup_printf("%s*", str);
1843 trace_event_iter_init(&iter, pattern);
1844 while ((ev = trace_event_iter_next(&iter)) != NULL) {
1845 readline_add_completion(rs, trace_event_get_name(ev));
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01001846 }
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01001847 g_free(pattern);
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01001848 } else if (nb_args == 3) {
1849 add_completion_option(rs, str, "on");
1850 add_completion_option(rs, str, "off");
1851 }
1852}
1853
Hani Benhabilesd0ece342014-05-27 23:39:31 +01001854void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
1855{
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01001856 int i;
1857
Hani Benhabilesd0ece342014-05-27 23:39:31 +01001858 if (nb_args != 2) {
1859 return;
1860 }
1861 readline_set_completion_index(rs, strlen(str));
Michal Privoznik14d53b42017-09-07 10:05:24 +02001862 for (i = 0; i < WATCHDOG_ACTION__MAX; i++) {
1863 add_completion_option(rs, str, WatchdogAction_str(i));
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01001864 }
Hani Benhabilesd0ece342014-05-27 23:39:31 +01001865}
1866
Hani Benhabilesc68a0402014-05-27 23:39:32 +01001867void migrate_set_capability_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_CAPABILITY__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02001877 const char *name = MigrationCapability_str(i);
Hani Benhabilesc68a0402014-05-27 23:39:32 +01001878 if (!strncmp(str, name, len)) {
1879 readline_add_completion(rs, name);
1880 }
1881 }
1882 } else if (nb_args == 3) {
1883 add_completion_option(rs, str, "on");
1884 add_completion_option(rs, str, "off");
1885 }
1886}
1887
Liang Li50e9a622015-03-23 16:32:29 +08001888void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
1889 const char *str)
1890{
1891 size_t len;
1892
1893 len = strlen(str);
1894 readline_set_completion_index(rs, len);
1895 if (nb_args == 2) {
1896 int i;
Eric Blake7fb1cf12015-11-18 01:52:57 -07001897 for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02001898 const char *name = MigrationParameter_str(i);
Liang Li50e9a622015-03-23 16:32:29 +08001899 if (!strncmp(str, name, len)) {
1900 readline_add_completion(rs, name);
1901 }
1902 }
1903 }
1904}
1905
Hani Benhabilesb21631f2014-05-27 23:39:37 +01001906static void vm_completion(ReadLineState *rs, const char *str)
1907{
1908 size_t len;
Kevin Wolf7c8eece2016-03-22 18:58:50 +01001909 BlockDriverState *bs;
Kevin Wolf88be7b42016-05-20 18:49:07 +02001910 BdrvNextIterator it;
Hani Benhabilesb21631f2014-05-27 23:39:37 +01001911
1912 len = strlen(str);
1913 readline_set_completion_index(rs, len);
Kevin Wolf7c8eece2016-03-22 18:58:50 +01001914
Kevin Wolf88be7b42016-05-20 18:49:07 +02001915 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Hani Benhabilesb21631f2014-05-27 23:39:37 +01001916 SnapshotInfoList *snapshots, *snapshot;
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03001917 AioContext *ctx = bdrv_get_aio_context(bs);
1918 bool ok = false;
Hani Benhabilesb21631f2014-05-27 23:39:37 +01001919
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03001920 aio_context_acquire(ctx);
1921 if (bdrv_can_snapshot(bs)) {
1922 ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0;
1923 }
1924 aio_context_release(ctx);
1925 if (!ok) {
Hani Benhabilesb21631f2014-05-27 23:39:37 +01001926 continue;
1927 }
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03001928
Hani Benhabilesb21631f2014-05-27 23:39:37 +01001929 snapshot = snapshots;
1930 while (snapshot) {
1931 char *completion = snapshot->value->name;
1932 if (!strncmp(str, completion, len)) {
1933 readline_add_completion(rs, completion);
1934 }
1935 completion = snapshot->value->id;
1936 if (!strncmp(str, completion, len)) {
1937 readline_add_completion(rs, completion);
1938 }
1939 snapshot = snapshot->next;
1940 }
1941 qapi_free_SnapshotInfoList(snapshots);
1942 }
1943
1944}
1945
1946void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
1947{
1948 if (nb_args == 2) {
1949 vm_completion(rs, str);
1950 }
1951}
1952
1953void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
1954{
1955 if (nb_args == 2) {
1956 vm_completion(rs, str);
1957 }
1958}
1959
Wayne Xia816f8922011-10-12 11:32:41 +08001960static int
1961compare_mon_cmd(const void *a, const void *b)
1962{
Kevin Wolfa0cd5e12019-06-13 17:33:56 +02001963 return strcmp(((const HMPCommand *)a)->name,
1964 ((const HMPCommand *)b)->name);
Wayne Xia816f8922011-10-12 11:32:41 +08001965}
1966
1967static void sortcmdlist(void)
1968{
Kevin Wolfa0cd5e12019-06-13 17:33:56 +02001969 qsort(hmp_cmds, ARRAY_SIZE(hmp_cmds) - 1,
1970 sizeof(*hmp_cmds),
1971 compare_mon_cmd);
1972 qsort(hmp_info_cmds, ARRAY_SIZE(hmp_info_cmds) - 1,
1973 sizeof(*hmp_info_cmds),
1974 compare_mon_cmd);
Wayne Xia816f8922011-10-12 11:32:41 +08001975}
1976
Gerd Hoffmannf0e48cb2021-06-24 12:38:32 +02001977void monitor_register_hmp(const char *name, bool info,
1978 void (*cmd)(Monitor *mon, const QDict *qdict))
1979{
1980 HMPCommand *table = info ? hmp_info_cmds : hmp_cmds;
1981
1982 while (table->name != NULL) {
1983 if (strcmp(table->name, name) == 0) {
1984 g_assert(table->cmd == NULL);
1985 table->cmd = cmd;
1986 return;
1987 }
1988 table++;
1989 }
1990 g_assert_not_reached();
1991}
1992
Peter Xu6adf08d2018-03-09 16:59:50 +08001993void monitor_init_globals(void)
1994{
Kevin Wolf1d95db72019-06-13 17:34:02 +02001995 monitor_init_globals_core();
Peter Xu6adf08d2018-03-09 16:59:50 +08001996 monitor_init_qmp_commands();
Peter Xu6adf08d2018-03-09 16:59:50 +08001997 sortcmdlist();
Peter Xu47451462018-06-08 11:55:11 +08001998 qemu_mutex_init(&mon_fdsets_lock);
Peter Xu6adf08d2018-03-09 16:59:50 +08001999}