blob: ffe79668706204c4dbd27d93f32900d5da51a541 [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
Emilio G. Cota97bfafe2017-08-08 13:54:42 -0400323static void hmp_info_sync_profile(Monitor *mon, const QDict *qdict)
324{
325 int64_t max = qdict_get_try_int(qdict, "max", 10);
326 bool mean = qdict_get_try_bool(qdict, "mean", false);
327 bool coalesce = !qdict_get_try_bool(qdict, "no_coalesce", false);
328 enum QSPSortBy sort_by;
329
330 sort_by = mean ? QSP_SORT_BY_AVG_WAIT_TIME : QSP_SORT_BY_TOTAL_WAIT_TIME;
Markus Armbrusterac7ff4c2019-04-17 21:17:54 +0200331 qsp_report(max, sort_by, coalesce);
Emilio G. Cota97bfafe2017-08-08 13:54:42 -0400332}
333
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100334static void hmp_info_history(Monitor *mon, const QDict *qdict)
bellardaa455482004-04-04 13:07:25 +0000335{
Kevin Wolf5f9dba12019-06-13 17:33:54 +0200336 MonitorHMP *hmp_mon = container_of(mon, MonitorHMP, common);
bellardaa455482004-04-04 13:07:25 +0000337 int i;
bellard7e2515e2004-08-01 21:52:19 +0000338 const char *str;
ths3b46e622007-09-17 08:09:54 +0000339
Kevin Wolf5f9dba12019-06-13 17:33:54 +0200340 if (!hmp_mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000341 return;
Kevin Wolf5f9dba12019-06-13 17:33:54 +0200342 }
bellard7e2515e2004-08-01 21:52:19 +0000343 i = 0;
344 for(;;) {
Kevin Wolf5f9dba12019-06-13 17:33:54 +0200345 str = readline_get_history(hmp_mon->rs, i);
346 if (!str) {
bellard7e2515e2004-08-01 21:52:19 +0000347 break;
Kevin Wolf5f9dba12019-06-13 17:33:54 +0200348 }
aliguori376253e2009-03-05 23:01:23 +0000349 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000350 i++;
bellardaa455482004-04-04 13:07:25 +0000351 }
352}
353
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100354static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530355{
Lluís Vilanovabd712112016-07-11 12:53:51 +0200356 const char *name = qdict_get_try_str(qdict, "name");
Lluís Vilanova77e2b172016-07-11 12:53:57 +0200357 bool has_vcpu = qdict_haskey(qdict, "vcpu");
358 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
Lluís Vilanovabd712112016-07-11 12:53:51 +0200359 TraceEventInfoList *events;
Lluís Vilanova14101d02014-08-25 13:20:03 +0200360 TraceEventInfoList *elem;
Lluís Vilanovabd712112016-07-11 12:53:51 +0200361 Error *local_err = NULL;
362
363 if (name == NULL) {
364 name = "*";
365 }
Lluís Vilanova77e2b172016-07-11 12:53:57 +0200366 if (vcpu < 0) {
367 monitor_printf(mon, "argument vcpu must be positive");
368 return;
369 }
Lluís Vilanovabd712112016-07-11 12:53:51 +0200370
Lluís Vilanova77e2b172016-07-11 12:53:57 +0200371 events = qmp_trace_event_get_state(name, has_vcpu, vcpu, &local_err);
Lluís Vilanovabd712112016-07-11 12:53:51 +0200372 if (local_err) {
373 error_report_err(local_err);
374 return;
375 }
Lluís Vilanova14101d02014-08-25 13:20:03 +0200376
377 for (elem = events; elem != NULL; elem = elem->next) {
378 monitor_printf(mon, "%s : state %u\n",
379 elem->value->name,
380 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
381 }
382 qapi_free_TraceEventInfoList(events);
Prerna Saxena22890ab2010-06-24 17:04:53 +0530383}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530384
Markus Armbrusterb8a185b2015-03-05 17:29:02 +0100385void qmp_client_migrate_info(const char *protocol, const char *hostname,
386 bool has_port, int64_t port,
387 bool has_tls_port, int64_t tls_port,
388 bool has_cert_subject, const char *cert_subject,
389 Error **errp)
Gerd Hoffmanne866e232010-04-23 13:28:21 +0200390{
Gerd Hoffmanne866e232010-04-23 13:28:21 +0200391 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb8a185b2015-03-05 17:29:02 +0100392 if (!qemu_using_spice(errp)) {
393 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +0200394 }
395
Markus Armbrusterb8a185b2015-03-05 17:29:02 +0100396 if (!has_port && !has_tls_port) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100397 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
Markus Armbrusterb8a185b2015-03-05 17:29:02 +0100398 return;
Yonit Halperin6ec5dae2012-03-18 09:42:39 +0200399 }
400
Gerd Hoffmann74774772020-10-19 09:52:12 +0200401 if (qemu_spice.migrate_info(hostname,
Markus Armbrusterb8a185b2015-03-05 17:29:02 +0100402 has_port ? port : -1,
403 has_tls_port ? tls_port : -1,
404 cert_subject)) {
Markus Armbruster9e1b9c62020-11-13 09:26:21 +0100405 error_setg(errp, "Could not set up display for migration");
Markus Armbrusterb8a185b2015-03-05 17:29:02 +0100406 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +0200407 }
Markus Armbrusterb8a185b2015-03-05 17:29:02 +0100408 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +0200409 }
410
Markus Armbruster99750d82020-11-13 09:26:22 +0100411 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "'spice'");
Gerd Hoffmanne866e232010-04-23 13:28:21 +0200412}
413
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100414static void hmp_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +0000415{
Markus Armbrusterdaa76aa2016-06-15 19:27:16 +0200416 Error *err = NULL;
417
418 qemu_set_log_filename(qdict_get_str(qdict, "filename"), &err);
419 if (err) {
420 error_report_err(err);
421 }
pbrooke735b912007-06-30 13:53:24 +0000422}
423
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100424static void hmp_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +0000425{
426 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300427 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +0000428
bellard9307c4c2004-04-04 12:57:25 +0000429 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +0000430 mask = 0;
431 } else {
Peter Maydell4fde1eb2013-02-11 16:41:22 +0000432 mask = qemu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +0000433 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +0000434 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +0000435 return;
436 }
437 }
Peter Maydell24537a02013-02-11 16:41:23 +0000438 qemu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +0000439}
440
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100441static void hmp_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +0000442{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300443 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +0000444 if (!option || !strcmp(option, "on")) {
445 singlestep = 1;
446 } else if (!strcmp(option, "off")) {
447 singlestep = 0;
448 } else {
449 monitor_printf(mon, "unexpected option %s\n", option);
450 }
451}
452
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100453static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +0000454{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300455 const char *device = qdict_get_try_str(qdict, "device");
Yutao Ai89854b92020-11-25 01:45:13 +0000456 if (!device) {
aliguori59030a82009-04-05 18:43:41 +0000457 device = "tcp::" DEFAULT_GDBSTUB_PORT;
Yutao Ai89854b92020-11-25 01:45:13 +0000458 }
459
aliguori59030a82009-04-05 18:43:41 +0000460 if (gdbserver_start(device) < 0) {
461 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
462 device);
463 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +0000464 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +0000465 } else {
aliguori59030a82009-04-05 18:43:41 +0000466 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
467 device);
bellard8a7ddc32004-03-31 19:00:16 +0000468 }
469}
470
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100471static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100472{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300473 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100474 if (select_watchdog_action(action) == -1) {
475 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
476 }
477}
478
aliguori376253e2009-03-05 23:01:23 +0000479static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +0000480{
aliguori376253e2009-03-05 23:01:23 +0000481 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000482 switch(c) {
483 case '\'':
aliguori376253e2009-03-05 23:01:23 +0000484 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +0000485 break;
486 case '\\':
aliguori376253e2009-03-05 23:01:23 +0000487 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +0000488 break;
489 case '\n':
aliguori376253e2009-03-05 23:01:23 +0000490 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +0000491 break;
492 case '\r':
aliguori376253e2009-03-05 23:01:23 +0000493 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +0000494 break;
495 default:
496 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +0000497 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +0000498 } else {
aliguori376253e2009-03-05 23:01:23 +0000499 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +0000500 }
501 break;
502 }
aliguori376253e2009-03-05 23:01:23 +0000503 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000504}
505
aliguori376253e2009-03-05 23:01:23 +0000506static void memory_dump(Monitor *mon, int count, int format, int wsize,
Avi Kivitya8170e52012-10-23 12:30:10 +0200507 hwaddr addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +0000508{
Blue Swirl23842aa2010-01-12 20:27:43 +0000509 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +0000510 uint8_t buf[16];
511 uint64_t v;
Kevin Wolf2fc5d012020-11-13 12:43:24 +0100512 CPUState *cs = mon_get_cpu(mon);
Thomas Huth854e67f2017-01-13 13:12:35 +0100513
514 if (!cs && (format == 'i' || !is_physical)) {
515 monitor_printf(mon, "Can not dump without CPU\n");
516 return;
517 }
bellard9307c4c2004-04-04 12:57:25 +0000518
519 if (format == 'i') {
Richard Henderson1d484742017-09-14 08:38:35 -0700520 monitor_disas(mon, cs, addr, count, is_physical);
bellard9307c4c2004-04-04 12:57:25 +0000521 return;
522 }
523
524 len = wsize * count;
Yutao Ai89854b92020-11-25 01:45:13 +0000525 if (wsize == 1) {
bellard9307c4c2004-04-04 12:57:25 +0000526 line_size = 8;
Yutao Ai89854b92020-11-25 01:45:13 +0000527 } else {
bellard9307c4c2004-04-04 12:57:25 +0000528 line_size = 16;
Yutao Ai89854b92020-11-25 01:45:13 +0000529 }
bellard9307c4c2004-04-04 12:57:25 +0000530 max_digits = 0;
531
532 switch(format) {
533 case 'o':
Marc-André Lureau69db8df2017-06-22 13:04:16 +0200534 max_digits = DIV_ROUND_UP(wsize * 8, 3);
bellard9307c4c2004-04-04 12:57:25 +0000535 break;
536 default:
537 case 'x':
538 max_digits = (wsize * 8) / 4;
539 break;
540 case 'u':
541 case 'd':
Marc-André Lureau69db8df2017-06-22 13:04:16 +0200542 max_digits = DIV_ROUND_UP(wsize * 8 * 10, 33);
bellard9307c4c2004-04-04 12:57:25 +0000543 break;
544 case 'c':
545 wsize = 1;
546 break;
547 }
548
549 while (len > 0) {
Yutao Ai89854b92020-11-25 01:45:13 +0000550 if (is_physical) {
aliguori376253e2009-03-05 23:01:23 +0000551 monitor_printf(mon, TARGET_FMT_plx ":", addr);
Yutao Ai89854b92020-11-25 01:45:13 +0000552 } else {
aliguori376253e2009-03-05 23:01:23 +0000553 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
Yutao Ai89854b92020-11-25 01:45:13 +0000554 }
bellard9307c4c2004-04-04 12:57:25 +0000555 l = len;
556 if (l > line_size)
557 l = line_size;
558 if (is_physical) {
Peter Maydell6f89ae52018-12-14 13:30:49 +0000559 AddressSpace *as = cs ? cs->as : &address_space_memory;
560 MemTxResult r = address_space_read(as, addr,
561 MEMTXATTRS_UNSPECIFIED, buf, l);
562 if (r != MEMTX_OK) {
563 monitor_printf(mon, " Cannot access memory\n");
564 break;
565 }
bellard9307c4c2004-04-04 12:57:25 +0000566 } else {
Thomas Huth854e67f2017-01-13 13:12:35 +0100567 if (cpu_memory_rw_debug(cs, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +0000568 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +0000569 break;
570 }
bellard9307c4c2004-04-04 12:57:25 +0000571 }
ths5fafdf22007-09-16 21:08:06 +0000572 i = 0;
bellard9307c4c2004-04-04 12:57:25 +0000573 while (i < l) {
574 switch(wsize) {
575 default:
576 case 1:
Peter Maydell24e60302015-01-20 15:19:32 +0000577 v = ldub_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000578 break;
579 case 2:
Peter Maydell24e60302015-01-20 15:19:32 +0000580 v = lduw_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000581 break;
582 case 4:
Peter Maydell24e60302015-01-20 15:19:32 +0000583 v = (uint32_t)ldl_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000584 break;
585 case 8:
Peter Maydell24e60302015-01-20 15:19:32 +0000586 v = ldq_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000587 break;
588 }
aliguori376253e2009-03-05 23:01:23 +0000589 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +0000590 switch(format) {
591 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000592 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000593 break;
594 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000595 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000596 break;
597 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000598 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000599 break;
600 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000601 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000602 break;
603 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000604 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +0000605 break;
606 }
607 i += wsize;
608 }
aliguori376253e2009-03-05 23:01:23 +0000609 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000610 addr += l;
611 len -= l;
612 }
613}
614
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100615static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000616{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300617 int count = qdict_get_int(qdict, "count");
618 int format = qdict_get_int(qdict, "format");
619 int size = qdict_get_int(qdict, "size");
620 target_long addr = qdict_get_int(qdict, "addr");
621
aliguori376253e2009-03-05 23:01:23 +0000622 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +0000623}
624
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100625static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000626{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300627 int count = qdict_get_int(qdict, "count");
628 int format = qdict_get_int(qdict, "format");
629 int size = qdict_get_int(qdict, "size");
Avi Kivitya8170e52012-10-23 12:30:10 +0200630 hwaddr addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300631
aliguori376253e2009-03-05 23:01:23 +0000632 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +0000633}
634
Tobin Feldman-Fitzthumc7f7e692020-10-27 13:03:03 -0400635void *gpa2hva(MemoryRegion **p_mr, hwaddr addr, uint64_t size, Error **errp)
Paolo Bonzinie9628442017-04-20 15:30:58 +0200636{
Tobin Feldman-Fitzthumc7f7e692020-10-27 13:03:03 -0400637 Int128 gpa_region_size;
Paolo Bonzinie9628442017-04-20 15:30:58 +0200638 MemoryRegionSection mrs = memory_region_find(get_system_memory(),
Tobin Feldman-Fitzthumc7f7e692020-10-27 13:03:03 -0400639 addr, size);
Paolo Bonzinie9628442017-04-20 15:30:58 +0200640
641 if (!mrs.mr) {
642 error_setg(errp, "No memory is mapped at address 0x%" HWADDR_PRIx, addr);
643 return NULL;
644 }
645
646 if (!memory_region_is_ram(mrs.mr) && !memory_region_is_romd(mrs.mr)) {
647 error_setg(errp, "Memory at address 0x%" HWADDR_PRIx "is not RAM", addr);
648 memory_region_unref(mrs.mr);
649 return NULL;
650 }
651
Tobin Feldman-Fitzthumc7f7e692020-10-27 13:03:03 -0400652 gpa_region_size = int128_make64(size);
653 if (int128_lt(mrs.size, gpa_region_size)) {
654 error_setg(errp, "Size of memory region at 0x%" HWADDR_PRIx
655 " exceeded.", addr);
656 memory_region_unref(mrs.mr);
657 return NULL;
658 }
659
Paolo Bonzinie9628442017-04-20 15:30:58 +0200660 *p_mr = mrs.mr;
661 return qemu_map_ram_ptr(mrs.mr->ram_block, mrs.offset_within_region);
662}
663
664static void hmp_gpa2hva(Monitor *mon, const QDict *qdict)
665{
666 hwaddr addr = qdict_get_int(qdict, "addr");
667 Error *local_err = NULL;
668 MemoryRegion *mr = NULL;
669 void *ptr;
670
Tobin Feldman-Fitzthumc7f7e692020-10-27 13:03:03 -0400671 ptr = gpa2hva(&mr, addr, 1, &local_err);
Paolo Bonzinie9628442017-04-20 15:30:58 +0200672 if (local_err) {
673 error_report_err(local_err);
674 return;
675 }
676
677 monitor_printf(mon, "Host virtual address for 0x%" HWADDR_PRIx
678 " (%s) is %p\n",
679 addr, mr->name, ptr);
680
681 memory_region_unref(mr);
682}
683
Dr. David Alan Gilbert574d9692019-04-12 16:26:52 +0100684static void hmp_gva2gpa(Monitor *mon, const QDict *qdict)
685{
686 target_ulong addr = qdict_get_int(qdict, "addr");
687 MemTxAttrs attrs;
Kevin Wolf2fc5d012020-11-13 12:43:24 +0100688 CPUState *cs = mon_get_cpu(mon);
Dr. David Alan Gilbert574d9692019-04-12 16:26:52 +0100689 hwaddr gpa;
690
691 if (!cs) {
692 monitor_printf(mon, "No cpu\n");
693 return;
694 }
695
Eduardo Habkost9d3250d2019-05-10 15:56:20 -0300696 gpa = cpu_get_phys_page_attrs_debug(cs, addr & TARGET_PAGE_MASK, &attrs);
Dr. David Alan Gilbert574d9692019-04-12 16:26:52 +0100697 if (gpa == -1) {
698 monitor_printf(mon, "Unmapped\n");
699 } else {
700 monitor_printf(mon, "gpa: %#" HWADDR_PRIx "\n",
701 gpa + (addr & ~TARGET_PAGE_MASK));
702 }
703}
704
Paolo Bonzinie9628442017-04-20 15:30:58 +0200705#ifdef CONFIG_LINUX
706static uint64_t vtop(void *ptr, Error **errp)
707{
708 uint64_t pinfo;
709 uint64_t ret = -1;
710 uintptr_t addr = (uintptr_t) ptr;
Wei Yang038adc22019-10-13 10:11:45 +0800711 uintptr_t pagesize = qemu_real_host_page_size;
Paolo Bonzinie9628442017-04-20 15:30:58 +0200712 off_t offset = addr / pagesize * sizeof(pinfo);
713 int fd;
714
715 fd = open("/proc/self/pagemap", O_RDONLY);
716 if (fd == -1) {
717 error_setg_errno(errp, errno, "Cannot open /proc/self/pagemap");
718 return -1;
719 }
720
721 /* Force copy-on-write if necessary. */
Stefan Hajnoczid73415a2020-09-23 11:56:46 +0100722 qatomic_add((uint8_t *)ptr, 0);
Paolo Bonzinie9628442017-04-20 15:30:58 +0200723
724 if (pread(fd, &pinfo, sizeof(pinfo), offset) != sizeof(pinfo)) {
725 error_setg_errno(errp, errno, "Cannot read pagemap");
726 goto out;
727 }
728 if ((pinfo & (1ull << 63)) == 0) {
729 error_setg(errp, "Page not present");
730 goto out;
731 }
732 ret = ((pinfo & 0x007fffffffffffffull) * pagesize) | (addr & (pagesize - 1));
733
734out:
735 close(fd);
736 return ret;
737}
738
739static void hmp_gpa2hpa(Monitor *mon, const QDict *qdict)
740{
741 hwaddr addr = qdict_get_int(qdict, "addr");
742 Error *local_err = NULL;
743 MemoryRegion *mr = NULL;
744 void *ptr;
745 uint64_t physaddr;
746
Tobin Feldman-Fitzthumc7f7e692020-10-27 13:03:03 -0400747 ptr = gpa2hva(&mr, addr, 1, &local_err);
Paolo Bonzinie9628442017-04-20 15:30:58 +0200748 if (local_err) {
749 error_report_err(local_err);
750 return;
751 }
752
753 physaddr = vtop(ptr, &local_err);
754 if (local_err) {
755 error_report_err(local_err);
756 } else {
757 monitor_printf(mon, "Host physical address for 0x%" HWADDR_PRIx
758 " (%s) is 0x%" PRIx64 "\n",
759 addr, mr->name, (uint64_t) physaddr);
760 }
761
762 memory_region_unref(mr);
763}
764#endif
765
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300766static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000767{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300768 int format = qdict_get_int(qdict, "format");
Avi Kivitya8170e52012-10-23 12:30:10 +0200769 hwaddr val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300770
bellard9307c4c2004-04-04 12:57:25 +0000771 switch(format) {
772 case 'o':
Avi Kivitya8170e52012-10-23 12:30:10 +0200773 monitor_printf(mon, "%#" HWADDR_PRIo, val);
bellard9307c4c2004-04-04 12:57:25 +0000774 break;
775 case 'x':
Avi Kivitya8170e52012-10-23 12:30:10 +0200776 monitor_printf(mon, "%#" HWADDR_PRIx, val);
bellard9307c4c2004-04-04 12:57:25 +0000777 break;
778 case 'u':
Avi Kivitya8170e52012-10-23 12:30:10 +0200779 monitor_printf(mon, "%" HWADDR_PRIu, val);
bellard9307c4c2004-04-04 12:57:25 +0000780 break;
781 default:
782 case 'd':
Avi Kivitya8170e52012-10-23 12:30:10 +0200783 monitor_printf(mon, "%" HWADDR_PRId, val);
bellard9307c4c2004-04-04 12:57:25 +0000784 break;
785 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000786 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +0000787 break;
788 }
aliguori376253e2009-03-05 23:01:23 +0000789 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000790}
791
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100792static void hmp_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +0000793{
794 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +0000795 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300796 uint32_t start = qdict_get_int(qdict, "start");
797 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +0000798
799 sum = 0;
800 for(addr = start; addr < (start + size); addr++) {
Peter Maydell42874d32015-04-26 16:49:24 +0100801 uint8_t val = address_space_ldub(&address_space_memory, addr,
802 MEMTXATTRS_UNSPECIFIED, NULL);
bellarde4cf1ad2005-06-04 20:15:57 +0000803 /* BSD sum algorithm ('sum' Unix command) */
804 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +0200805 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +0000806 }
aliguori376253e2009-03-05 23:01:23 +0000807 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +0000808}
809
bellard13224a82006-07-14 22:03:35 +0000810static int mouse_button_state;
811
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100812static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +0000813{
Gerd Hoffmannc751a742013-12-04 15:02:28 +0100814 int dx, dy, dz, button;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300815 const char *dx_str = qdict_get_str(qdict, "dx_str");
816 const char *dy_str = qdict_get_str(qdict, "dy_str");
817 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
Gerd Hoffmannc751a742013-12-04 15:02:28 +0100818
bellard13224a82006-07-14 22:03:35 +0000819 dx = strtol(dx_str, NULL, 0);
820 dy = strtol(dy_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +0100821 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
822 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
823
824 if (dz_str) {
bellard13224a82006-07-14 22:03:35 +0000825 dz = strtol(dz_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +0100826 if (dz != 0) {
Gerd Hoffmannf22d0af2016-01-12 12:14:12 +0100827 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
Gerd Hoffmannc751a742013-12-04 15:02:28 +0100828 qemu_input_queue_btn(NULL, button, true);
829 qemu_input_event_sync();
830 qemu_input_queue_btn(NULL, button, false);
831 }
832 }
833 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +0000834}
835
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100836static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +0000837{
Eric Blake7fb1cf12015-11-18 01:52:57 -0700838 static uint32_t bmap[INPUT_BUTTON__MAX] = {
Gerd Hoffmannc751a742013-12-04 15:02:28 +0100839 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
840 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
841 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
842 };
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300843 int button_state = qdict_get_int(qdict, "button_state");
Gerd Hoffmannc751a742013-12-04 15:02:28 +0100844
845 if (mouse_button_state == button_state) {
846 return;
847 }
848 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
849 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +0000850 mouse_button_state = button_state;
bellard13224a82006-07-14 22:03:35 +0000851}
852
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100853static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +0000854{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -0300855 int size = qdict_get_int(qdict, "size");
856 int addr = qdict_get_int(qdict, "addr");
857 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +0000858 uint32_t val;
859 int suffix;
860
861 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -0300862 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +0000863 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +0000864 addr++;
865 }
866 addr &= 0xffff;
867
868 switch(size) {
869 default:
870 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +0000871 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +0000872 suffix = 'b';
873 break;
874 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +0000875 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +0000876 suffix = 'w';
877 break;
878 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +0000879 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +0000880 suffix = 'l';
881 break;
882 }
Yutao Ai33b1fa92020-11-25 01:45:14 +0000883 monitor_printf(mon, "port%c[0x%04x] = 0x%0*x\n",
aliguori376253e2009-03-05 23:01:23 +0000884 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +0000885}
bellarda3a91a32004-06-04 11:06:21 +0000886
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100887static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +0200888{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300889 int size = qdict_get_int(qdict, "size");
890 int addr = qdict_get_int(qdict, "addr");
891 int val = qdict_get_int(qdict, "val");
892
Jan Kiszkaf1147842009-07-14 10:20:11 +0200893 addr &= IOPORTS_MASK;
894
895 switch (size) {
896 default:
897 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +0000898 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +0200899 break;
900 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +0000901 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +0200902 break;
903 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +0000904 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +0200905 break;
906 }
907}
908
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100909static void hmp_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +0000910{
Gongleif1839932014-12-03 18:20:58 +0000911 Error *local_err = NULL;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300912 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +0000913
Gongleif1839932014-12-03 18:20:58 +0000914 qemu_boot_set(bootdevice, &local_err);
915 if (local_err) {
Markus Armbruster193227f2015-12-18 16:35:06 +0100916 error_report_err(local_err);
aurel320ecdffb2008-05-04 20:11:34 +0000917 } else {
Gongleif1839932014-12-03 18:20:58 +0000918 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +0000919 }
920}
921
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100922static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
Blue Swirl314e2982011-09-11 20:22:05 +0000923{
Peter Xu57bb40c2017-01-16 16:40:05 +0800924 bool flatview = qdict_get_try_bool(qdict, "flatview", false);
Alexey Kardashevskiy5e8fd942017-09-21 18:51:06 +1000925 bool dispatch_tree = qdict_get_try_bool(qdict, "dispatch_tree", false);
Alexey Kardashevskiyfc051ae2018-06-04 13:25:11 +1000926 bool owner = qdict_get_try_bool(qdict, "owner", false);
Philippe Mathieu-Daudé2261d392020-05-29 14:53:25 +0200927 bool disabled = qdict_get_try_bool(qdict, "disabled", false);
Peter Xu57bb40c2017-01-16 16:40:05 +0800928
Philippe Mathieu-Daudé2261d392020-05-29 14:53:25 +0200929 mtree_info(flatview, dispatch_tree, owner, disabled);
Blue Swirl314e2982011-09-11 20:22:05 +0000930}
931
bellard5f1ce942006-02-08 22:40:15 +0000932#ifdef CONFIG_PROFILER
933
Aurelien Jarnoe9a66252009-09-30 14:09:52 +0200934int64_t dev_time;
935
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100936static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +0000937{
Emilio G. Cota72fd2ef2018-10-10 10:48:53 -0400938 static int64_t last_cpu_exec_time;
939 int64_t cpu_exec_time;
940 int64_t delta;
941
942 cpu_exec_time = tcg_cpu_exec_time();
943 delta = cpu_exec_time - last_cpu_exec_time;
944
aliguori376253e2009-03-05 23:01:23 +0000945 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Rutuja Shah73bcb242016-03-21 21:32:30 +0530946 dev_time, dev_time / (double)NANOSECONDS_PER_SECOND);
aliguori376253e2009-03-05 23:01:23 +0000947 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Emilio G. Cota72fd2ef2018-10-10 10:48:53 -0400948 delta, delta / (double)NANOSECONDS_PER_SECOND);
949 last_cpu_exec_time = cpu_exec_time;
bellard5f1ce942006-02-08 22:40:15 +0000950 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +0000951}
952#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100953static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +0000954{
aliguori376253e2009-03-05 23:01:23 +0000955 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +0000956}
957#endif
958
bellardec36b692006-07-16 18:57:03 +0000959/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +0000960static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +0000961
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100962static void hmp_info_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +0000963{
964 int i;
965 CaptureState *s;
966
967 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +0000968 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +0000969 s->ops.info (s->opaque);
970 }
971}
972
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100973static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +0000974{
975 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300976 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +0000977 CaptureState *s;
978
979 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
980 if (i == n) {
981 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +0000982 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -0500983 g_free (s);
bellardec36b692006-07-16 18:57:03 +0000984 return;
985 }
986 }
987}
988
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100989static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +0000990{
Luiz Capitulinoc1925482009-08-28 15:27:19 -0300991 const char *path = qdict_get_str(qdict, "path");
Kővágó, Zoltánf0b9f362019-08-19 01:06:48 +0200992 int freq = qdict_get_try_int(qdict, "freq", 44100);
993 int bits = qdict_get_try_int(qdict, "bits", 16);
994 int nchannels = qdict_get_try_int(qdict, "nchannels", 2);
995 const char *audiodev = qdict_get_str(qdict, "audiodev");
bellardec36b692006-07-16 18:57:03 +0000996 CaptureState *s;
Kővágó, Zoltánf0b9f362019-08-19 01:06:48 +0200997 AudioState *as = audio_state_by_name(audiodev);
998
999 if (!as) {
1000 monitor_printf(mon, "Audiodev '%s' not found\n", audiodev);
1001 return;
1002 }
bellardec36b692006-07-16 18:57:03 +00001003
Anthony Liguori7267c092011-08-20 22:09:37 -05001004 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001005
Kővágó, Zoltánf0b9f362019-08-19 01:06:48 +02001006 if (wav_start_capture(as, s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001007 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05001008 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001009 return;
bellardec36b692006-07-16 18:57:03 +00001010 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001011 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001012}
bellardec36b692006-07-16 18:57:03 +00001013
Corey Bryant208c9d12012-06-22 14:36:09 -04001014void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001015{
Kevin Wolf947e4742020-10-05 17:58:44 +02001016 Monitor *cur_mon = monitor_cur();
Anthony Liguoric227f092009-10-01 16:12:16 -05001017 mon_fd_t *monfd;
Peter Xu9409fc02018-06-08 11:55:06 +08001018 int fd, tmp_fd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001019
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03001020 fd = qemu_chr_fe_get_msgfd(&cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001021 if (fd == -1) {
Markus Armbrusterf820af82020-11-13 09:26:18 +01001022 error_setg(errp, "No file descriptor supplied via SCM_RIGHTS");
Corey Bryant208c9d12012-06-22 14:36:09 -04001023 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001024 }
1025
1026 if (qemu_isdigit(fdname[0])) {
Stefan Hajnoczi0b9f0e22014-04-24 13:58:18 +02001027 close(fd);
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001028 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
1029 "a name not starting with a digit");
Corey Bryant208c9d12012-06-22 14:36:09 -04001030 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001031 }
1032
Dr. David Alan Gilbert0210c3b2020-09-22 10:57:41 +01001033 QEMU_LOCK_GUARD(&cur_mon->mon_lock);
Corey Bryant208c9d12012-06-22 14:36:09 -04001034 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001035 if (strcmp(monfd->name, fdname) != 0) {
1036 continue;
1037 }
1038
Peter Xu9409fc02018-06-08 11:55:06 +08001039 tmp_fd = monfd->fd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001040 monfd->fd = fd;
Markus Armbruster774a6b62018-07-03 10:53:57 +02001041 /* Make sure close() is outside critical section */
Peter Xu9409fc02018-06-08 11:55:06 +08001042 close(tmp_fd);
Corey Bryant208c9d12012-06-22 14:36:09 -04001043 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001044 }
1045
Anthony Liguori7267c092011-08-20 22:09:37 -05001046 monfd = g_malloc0(sizeof(mon_fd_t));
1047 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001048 monfd->fd = fd;
1049
Corey Bryant208c9d12012-06-22 14:36:09 -04001050 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001051}
1052
Corey Bryant208c9d12012-06-22 14:36:09 -04001053void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001054{
Kevin Wolf947e4742020-10-05 17:58:44 +02001055 Monitor *cur_mon = monitor_cur();
Anthony Liguoric227f092009-10-01 16:12:16 -05001056 mon_fd_t *monfd;
Peter Xu9409fc02018-06-08 11:55:06 +08001057 int tmp_fd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001058
Peter Xu9409fc02018-06-08 11:55:06 +08001059 qemu_mutex_lock(&cur_mon->mon_lock);
Corey Bryant208c9d12012-06-22 14:36:09 -04001060 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001061 if (strcmp(monfd->name, fdname) != 0) {
1062 continue;
1063 }
1064
Blue Swirl72cf2d42009-09-12 07:36:22 +00001065 QLIST_REMOVE(monfd, next);
Peter Xu9409fc02018-06-08 11:55:06 +08001066 tmp_fd = monfd->fd;
Anthony Liguori7267c092011-08-20 22:09:37 -05001067 g_free(monfd->name);
1068 g_free(monfd);
Peter Xu9409fc02018-06-08 11:55:06 +08001069 qemu_mutex_unlock(&cur_mon->mon_lock);
Markus Armbruster774a6b62018-07-03 10:53:57 +02001070 /* Make sure close() is outside critical section */
Peter Xu9409fc02018-06-08 11:55:06 +08001071 close(tmp_fd);
Corey Bryant208c9d12012-06-22 14:36:09 -04001072 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001073 }
1074
Peter Xu9409fc02018-06-08 11:55:06 +08001075 qemu_mutex_unlock(&cur_mon->mon_lock);
Markus Armbrusterf820af82020-11-13 09:26:18 +01001076 error_setg(errp, "File descriptor named '%s' not found", fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001077}
1078
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001079int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
Mark McLoughlin7768e042009-07-22 09:11:41 +01001080{
Anthony Liguoric227f092009-10-01 16:12:16 -05001081 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01001082
Dr. David Alan Gilbert0210c3b2020-09-22 10:57:41 +01001083 QEMU_LOCK_GUARD(&mon->mon_lock);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001084 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01001085 int fd;
1086
1087 if (strcmp(monfd->name, fdname) != 0) {
1088 continue;
1089 }
1090
1091 fd = monfd->fd;
1092
1093 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001094 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05001095 g_free(monfd->name);
1096 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001097
1098 return fd;
1099 }
1100
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001101 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001102 return -1;
1103}
1104
Corey Bryantba1c0482012-08-14 16:43:43 -04001105static void monitor_fdset_cleanup(MonFdset *mon_fdset)
1106{
1107 MonFdsetFd *mon_fdset_fd;
1108 MonFdsetFd *mon_fdset_fd_next;
1109
1110 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantebe52b52012-10-18 15:19:33 -04001111 if ((mon_fdset_fd->removed ||
1112 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
1113 runstate_is_running()) {
Corey Bryantba1c0482012-08-14 16:43:43 -04001114 close(mon_fdset_fd->fd);
1115 g_free(mon_fdset_fd->opaque);
1116 QLIST_REMOVE(mon_fdset_fd, next);
1117 g_free(mon_fdset_fd);
1118 }
1119 }
1120
Corey Bryantadb696f2012-08-14 16:43:47 -04001121 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04001122 QLIST_REMOVE(mon_fdset, next);
1123 g_free(mon_fdset);
1124 }
1125}
1126
Kevin Wolf7e3c0de2019-06-13 17:34:00 +02001127void monitor_fdsets_cleanup(void)
Corey Bryantefb87c12012-08-14 16:43:48 -04001128{
1129 MonFdset *mon_fdset;
1130 MonFdset *mon_fdset_next;
1131
Dr. David Alan Gilbert0210c3b2020-09-22 10:57:41 +01001132 QEMU_LOCK_GUARD(&mon_fdsets_lock);
Corey Bryantefb87c12012-08-14 16:43:48 -04001133 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
1134 monitor_fdset_cleanup(mon_fdset);
1135 }
1136}
1137
Corey Bryantba1c0482012-08-14 16:43:43 -04001138AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
1139 const char *opaque, Error **errp)
1140{
1141 int fd;
Kevin Wolf947e4742020-10-05 17:58:44 +02001142 Monitor *mon = monitor_cur();
Corey Bryantba1c0482012-08-14 16:43:43 -04001143 AddfdInfo *fdinfo;
1144
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03001145 fd = qemu_chr_fe_get_msgfd(&mon->chr);
Corey Bryantba1c0482012-08-14 16:43:43 -04001146 if (fd == -1) {
Markus Armbrusterf820af82020-11-13 09:26:18 +01001147 error_setg(errp, "No file descriptor supplied via SCM_RIGHTS");
Corey Bryantba1c0482012-08-14 16:43:43 -04001148 goto error;
1149 }
1150
Corey Bryante446f702012-10-18 15:19:32 -04001151 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
1152 has_opaque, opaque, errp);
1153 if (fdinfo) {
1154 return fdinfo;
Corey Bryant9ac54af2012-10-18 15:19:31 -04001155 }
1156
Corey Bryantba1c0482012-08-14 16:43:43 -04001157error:
1158 if (fd != -1) {
1159 close(fd);
1160 }
1161 return NULL;
1162}
1163
1164void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
1165{
1166 MonFdset *mon_fdset;
1167 MonFdsetFd *mon_fdset_fd;
1168 char fd_str[60];
1169
Dr. David Alan Gilbert0210c3b2020-09-22 10:57:41 +01001170 QEMU_LOCK_GUARD(&mon_fdsets_lock);
Corey Bryantba1c0482012-08-14 16:43:43 -04001171 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1172 if (mon_fdset->id != fdset_id) {
1173 continue;
1174 }
1175 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1176 if (has_fd) {
1177 if (mon_fdset_fd->fd != fd) {
1178 continue;
1179 }
1180 mon_fdset_fd->removed = true;
1181 break;
1182 } else {
1183 mon_fdset_fd->removed = true;
1184 }
1185 }
1186 if (has_fd && !mon_fdset_fd) {
1187 goto error;
1188 }
1189 monitor_fdset_cleanup(mon_fdset);
1190 return;
1191 }
1192
1193error:
1194 if (has_fd) {
1195 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
1196 fdset_id, fd);
1197 } else {
1198 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
1199 }
Markus Armbrusterf820af82020-11-13 09:26:18 +01001200 error_setg(errp, "File descriptor named '%s' not found", fd_str);
Corey Bryantba1c0482012-08-14 16:43:43 -04001201}
1202
1203FdsetInfoList *qmp_query_fdsets(Error **errp)
1204{
1205 MonFdset *mon_fdset;
1206 MonFdsetFd *mon_fdset_fd;
1207 FdsetInfoList *fdset_list = NULL;
1208
Dr. David Alan Gilbert0210c3b2020-09-22 10:57:41 +01001209 QEMU_LOCK_GUARD(&mon_fdsets_lock);
Corey Bryantba1c0482012-08-14 16:43:43 -04001210 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
Eric Blake54aa3de2020-11-12 19:13:37 -06001211 FdsetInfo *fdset_info = g_malloc0(sizeof(*fdset_info));
Corey Bryantba1c0482012-08-14 16:43:43 -04001212
Eric Blake54aa3de2020-11-12 19:13:37 -06001213 fdset_info->fdset_id = mon_fdset->id;
Corey Bryantba1c0482012-08-14 16:43:43 -04001214
1215 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
Eric Blake54aa3de2020-11-12 19:13:37 -06001216 FdsetFdInfo *fdsetfd_info;
Corey Bryantba1c0482012-08-14 16:43:43 -04001217
1218 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
Eric Blake54aa3de2020-11-12 19:13:37 -06001219 fdsetfd_info->fd = mon_fdset_fd->fd;
Corey Bryantba1c0482012-08-14 16:43:43 -04001220 if (mon_fdset_fd->opaque) {
Eric Blake54aa3de2020-11-12 19:13:37 -06001221 fdsetfd_info->has_opaque = true;
1222 fdsetfd_info->opaque = g_strdup(mon_fdset_fd->opaque);
Corey Bryantba1c0482012-08-14 16:43:43 -04001223 } else {
Eric Blake54aa3de2020-11-12 19:13:37 -06001224 fdsetfd_info->has_opaque = false;
Corey Bryantba1c0482012-08-14 16:43:43 -04001225 }
1226
Eric Blake54aa3de2020-11-12 19:13:37 -06001227 QAPI_LIST_PREPEND(fdset_info->fds, fdsetfd_info);
Corey Bryantba1c0482012-08-14 16:43:43 -04001228 }
1229
Eric Blake54aa3de2020-11-12 19:13:37 -06001230 QAPI_LIST_PREPEND(fdset_list, fdset_info);
Corey Bryantba1c0482012-08-14 16:43:43 -04001231 }
1232
1233 return fdset_list;
1234}
1235
Corey Bryante446f702012-10-18 15:19:32 -04001236AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
1237 bool has_opaque, const char *opaque,
1238 Error **errp)
1239{
1240 MonFdset *mon_fdset = NULL;
1241 MonFdsetFd *mon_fdset_fd;
1242 AddfdInfo *fdinfo;
1243
Daniel Brodsky6e8a3552020-04-03 21:21:08 -07001244 QEMU_LOCK_GUARD(&mon_fdsets_lock);
Corey Bryante446f702012-10-18 15:19:32 -04001245 if (has_fdset_id) {
1246 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1247 /* Break if match found or match impossible due to ordering by ID */
1248 if (fdset_id <= mon_fdset->id) {
1249 if (fdset_id < mon_fdset->id) {
1250 mon_fdset = NULL;
1251 }
1252 break;
1253 }
1254 }
1255 }
1256
1257 if (mon_fdset == NULL) {
1258 int64_t fdset_id_prev = -1;
1259 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
1260
1261 if (has_fdset_id) {
1262 if (fdset_id < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001263 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
1264 "a non-negative value");
Corey Bryante446f702012-10-18 15:19:32 -04001265 return NULL;
1266 }
1267 /* Use specified fdset ID */
1268 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1269 mon_fdset_cur = mon_fdset;
1270 if (fdset_id < mon_fdset_cur->id) {
1271 break;
1272 }
1273 }
1274 } else {
1275 /* Use first available fdset ID */
1276 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1277 mon_fdset_cur = mon_fdset;
1278 if (fdset_id_prev == mon_fdset_cur->id - 1) {
1279 fdset_id_prev = mon_fdset_cur->id;
1280 continue;
1281 }
1282 break;
1283 }
1284 }
1285
1286 mon_fdset = g_malloc0(sizeof(*mon_fdset));
1287 if (has_fdset_id) {
1288 mon_fdset->id = fdset_id;
1289 } else {
1290 mon_fdset->id = fdset_id_prev + 1;
1291 }
1292
1293 /* The fdset list is ordered by fdset ID */
1294 if (!mon_fdset_cur) {
1295 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
1296 } else if (mon_fdset->id < mon_fdset_cur->id) {
1297 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
1298 } else {
1299 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
1300 }
1301 }
1302
1303 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
1304 mon_fdset_fd->fd = fd;
1305 mon_fdset_fd->removed = false;
1306 if (has_opaque) {
1307 mon_fdset_fd->opaque = g_strdup(opaque);
1308 }
1309 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
1310
1311 fdinfo = g_malloc0(sizeof(*fdinfo));
1312 fdinfo->fdset_id = mon_fdset->id;
1313 fdinfo->fd = mon_fdset_fd->fd;
1314
1315 return fdinfo;
1316}
1317
Daniel P. Berrangé60efffa2020-08-27 13:27:00 +01001318int monitor_fdset_dup_fd_add(int64_t fdset_id, int flags)
Corey Bryantadb696f2012-08-14 16:43:47 -04001319{
Peter Xu47451462018-06-08 11:55:11 +08001320#ifdef _WIN32
1321 return -ENOENT;
1322#else
Corey Bryantadb696f2012-08-14 16:43:47 -04001323 MonFdset *mon_fdset;
Corey Bryantadb696f2012-08-14 16:43:47 -04001324
Dr. David Alan Gilbert0210c3b2020-09-22 10:57:41 +01001325 QEMU_LOCK_GUARD(&mon_fdsets_lock);
Corey Bryantadb696f2012-08-14 16:43:47 -04001326 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
Daniel P. Berrangé60efffa2020-08-27 13:27:00 +01001327 MonFdsetFd *mon_fdset_fd;
1328 MonFdsetFd *mon_fdset_fd_dup;
1329 int fd = -1;
1330 int dup_fd;
1331 int mon_fd_flags;
1332
Corey Bryantadb696f2012-08-14 16:43:47 -04001333 if (mon_fdset->id != fdset_id) {
1334 continue;
1335 }
Daniel P. Berrangé60efffa2020-08-27 13:27:00 +01001336
Corey Bryantadb696f2012-08-14 16:43:47 -04001337 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1338 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
1339 if (mon_fd_flags == -1) {
Daniel P. Berrangé60efffa2020-08-27 13:27:00 +01001340 return -1;
Corey Bryantadb696f2012-08-14 16:43:47 -04001341 }
1342
1343 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
Daniel P. Berrangé60efffa2020-08-27 13:27:00 +01001344 fd = mon_fdset_fd->fd;
1345 break;
Corey Bryantadb696f2012-08-14 16:43:47 -04001346 }
1347 }
Corey Bryantadb696f2012-08-14 16:43:47 -04001348
Daniel P. Berrangé60efffa2020-08-27 13:27:00 +01001349 if (fd == -1) {
Daniel P. Berrangé60efffa2020-08-27 13:27:00 +01001350 errno = EACCES;
1351 return -1;
Corey Bryantadb696f2012-08-14 16:43:47 -04001352 }
Daniel P. Berrangé60efffa2020-08-27 13:27:00 +01001353
1354 dup_fd = qemu_dup_flags(fd, flags);
1355 if (dup_fd == -1) {
Daniel P. Berrangé60efffa2020-08-27 13:27:00 +01001356 return -1;
Corey Bryantadb696f2012-08-14 16:43:47 -04001357 }
Daniel P. Berrangé60efffa2020-08-27 13:27:00 +01001358
Corey Bryantadb696f2012-08-14 16:43:47 -04001359 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
1360 mon_fdset_fd_dup->fd = dup_fd;
1361 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
Daniel P. Berrangé60efffa2020-08-27 13:27:00 +01001362 return dup_fd;
Corey Bryantadb696f2012-08-14 16:43:47 -04001363 }
Peter Xu47451462018-06-08 11:55:11 +08001364
Daniel P. Berrangé60efffa2020-08-27 13:27:00 +01001365 errno = ENOENT;
Corey Bryantadb696f2012-08-14 16:43:47 -04001366 return -1;
Daniel P. Berrangé60efffa2020-08-27 13:27:00 +01001367#endif
Corey Bryantadb696f2012-08-14 16:43:47 -04001368}
1369
Yury Kotov854f63d2019-05-23 12:44:33 +03001370static int64_t monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
Corey Bryantadb696f2012-08-14 16:43:47 -04001371{
1372 MonFdset *mon_fdset;
1373 MonFdsetFd *mon_fdset_fd_dup;
1374
Dr. David Alan Gilbert0210c3b2020-09-22 10:57:41 +01001375 QEMU_LOCK_GUARD(&mon_fdsets_lock);
Corey Bryantadb696f2012-08-14 16:43:47 -04001376 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1377 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
1378 if (mon_fdset_fd_dup->fd == dup_fd) {
1379 if (remove) {
1380 QLIST_REMOVE(mon_fdset_fd_dup, next);
Chen Quna6616142020-01-15 15:20:16 +08001381 g_free(mon_fdset_fd_dup);
Corey Bryantadb696f2012-08-14 16:43:47 -04001382 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
1383 monitor_fdset_cleanup(mon_fdset);
1384 }
Dr. David Alan Gilbert0210c3b2020-09-22 10:57:41 +01001385 return -1;
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02001386 } else {
1387 return mon_fdset->id;
Corey Bryantadb696f2012-08-14 16:43:47 -04001388 }
Corey Bryantadb696f2012-08-14 16:43:47 -04001389 }
1390 }
1391 }
Peter Xu47451462018-06-08 11:55:11 +08001392
Corey Bryantadb696f2012-08-14 16:43:47 -04001393 return -1;
1394}
1395
Yury Kotov854f63d2019-05-23 12:44:33 +03001396int64_t monitor_fdset_dup_fd_find(int dup_fd)
Corey Bryantadb696f2012-08-14 16:43:47 -04001397{
1398 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
1399}
1400
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02001401void monitor_fdset_dup_fd_remove(int dup_fd)
Corey Bryantadb696f2012-08-14 16:43:47 -04001402{
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02001403 monitor_fdset_dup_fd_find_remove(dup_fd, true);
Corey Bryantadb696f2012-08-14 16:43:47 -04001404}
1405
Markus Armbruster1677f4c2015-02-09 14:03:19 +01001406int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
Laszlo Ersek59063662014-04-10 10:24:31 +02001407{
1408 int fd;
1409 Error *local_err = NULL;
1410
1411 if (!qemu_isdigit(fdname[0]) && mon) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001412 fd = monitor_get_fd(mon, fdname, &local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00001413 } else {
1414 fd = qemu_parse_fd(fdname);
Laszlo Ersek59063662014-04-10 10:24:31 +02001415 if (fd == -1) {
1416 error_setg(&local_err, "Invalid file descriptor number '%s'",
1417 fdname);
1418 }
1419 }
1420 if (local_err) {
1421 error_propagate(errp, local_err);
1422 assert(fd == -1);
1423 } else {
1424 assert(fd != -1);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00001425 }
1426
1427 return fd;
1428}
1429
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03001430/* Please update hmp-commands.hx when adding or changing commands */
Kevin Wolfa0cd5e12019-06-13 17:33:56 +02001431static HMPCommand hmp_info_cmds[] = {
Pavel Butsykinda76ee72015-09-10 18:38:58 +03001432#include "hmp-commands-info.h"
1433 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00001434};
1435
Kevin Wolfa0cd5e12019-06-13 17:33:56 +02001436/* hmp_cmds and hmp_info_cmds would be sorted at runtime */
Kevin Wolfed7bda52019-06-13 17:34:01 +02001437HMPCommand hmp_cmds[] = {
Wenchao Xiaa13ced52013-01-14 14:06:28 +08001438#include "hmp-commands.h"
1439 { NULL, NULL, },
1440};
1441
Kevin Wolfed7bda52019-06-13 17:34:01 +02001442/*
1443 * Set @pval to the value in the register identified by @name.
1444 * return 0 if OK, -1 if not found
1445 */
Kevin Wolf2fc5d012020-11-13 12:43:24 +01001446int get_monitor_def(Monitor *mon, int64_t *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00001447{
Pavel Butsykinbf957282015-09-10 18:38:59 +03001448 const MonitorDef *md = target_monitor_defs();
Kevin Wolf2fc5d012020-11-13 12:43:24 +01001449 CPUState *cs = mon_get_cpu(mon);
bellard92a31b12005-02-10 22:00:52 +00001450 void *ptr;
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11001451 uint64_t tmp = 0;
1452 int ret;
bellard92a31b12005-02-10 22:00:52 +00001453
Thomas Huth854e67f2017-01-13 13:12:35 +01001454 if (cs == NULL || md == NULL) {
Pavel Butsykinbf957282015-09-10 18:38:59 +03001455 return -1;
1456 }
1457
1458 for(; md->name != NULL; md++) {
Kevin Wolfed7bda52019-06-13 17:34:01 +02001459 if (hmp_compare_cmd(name, md->name)) {
bellard9307c4c2004-04-04 12:57:25 +00001460 if (md->get_value) {
Kevin Wolf43cf0672020-11-13 12:43:25 +01001461 *pval = md->get_value(mon, md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00001462 } else {
Kevin Wolfe7cff9c2020-11-13 12:43:26 +01001463 CPUArchState *env = mon_get_cpu_env(mon);
bellard6a00d602005-11-21 23:25:50 +00001464 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00001465 switch(md->type) {
1466 case MD_I32:
1467 *pval = *(int32_t *)ptr;
1468 break;
1469 case MD_TLONG:
1470 *pval = *(target_long *)ptr;
1471 break;
1472 default:
1473 *pval = 0;
1474 break;
1475 }
bellard9307c4c2004-04-04 12:57:25 +00001476 }
1477 return 0;
1478 }
1479 }
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11001480
Thomas Huth854e67f2017-01-13 13:12:35 +01001481 ret = target_get_monitor_def(cs, name, &tmp);
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11001482 if (!ret) {
1483 *pval = (target_long) tmp;
1484 }
1485
1486 return ret;
bellard9307c4c2004-04-04 12:57:25 +00001487}
1488
Hani Benhabiles40d19392014-05-07 23:41:30 +01001489static void add_completion_option(ReadLineState *rs, const char *str,
1490 const char *option)
1491{
1492 if (!str || !option) {
1493 return;
1494 }
1495 if (!strncmp(option, str, strlen(str))) {
1496 readline_add_completion(rs, option);
1497 }
1498}
1499
Hani Benhabiles13e315d2014-05-07 23:41:29 +01001500void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
1501{
1502 size_t len;
1503 ChardevBackendInfoList *list, *start;
1504
1505 if (nb_args != 2) {
1506 return;
1507 }
1508 len = strlen(str);
1509 readline_set_completion_index(rs, len);
1510
1511 start = list = qmp_query_chardev_backends(NULL);
1512 while (list) {
1513 const char *chr_name = list->value->name;
1514
1515 if (!strncmp(chr_name, str, len)) {
1516 readline_add_completion(rs, chr_name);
1517 }
1518 list = list->next;
1519 }
1520 qapi_free_ChardevBackendInfoList(start);
1521}
1522
Hani Benhabilesb162b492014-05-07 23:41:31 +01001523void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
1524{
1525 size_t len;
1526 int i;
1527
1528 if (nb_args != 2) {
1529 return;
1530 }
1531 len = strlen(str);
1532 readline_set_completion_index(rs, len);
Markus Armbruster1c236ba2017-08-24 10:46:06 +02001533 for (i = 0; i < NET_CLIENT_DRIVER__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02001534 add_completion_option(rs, str, NetClientDriver_str(i));
Hani Benhabilesb162b492014-05-07 23:41:31 +01001535 }
1536}
1537
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01001538void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles992d3e62014-02-06 23:30:11 +01001539{
1540 GSList *list, *elt;
1541 size_t len;
1542
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01001543 if (nb_args != 2) {
1544 return;
1545 }
1546
Hani Benhabiles992d3e62014-02-06 23:30:11 +01001547 len = strlen(str);
1548 readline_set_completion_index(rs, len);
1549 list = elt = object_class_get_list(TYPE_DEVICE, false);
1550 while (elt) {
1551 const char *name;
1552 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
1553 TYPE_DEVICE);
1554 name = object_class_get_name(OBJECT_CLASS(dc));
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01001555
Eduardo Habkoste90f2a82017-05-03 17:35:44 -03001556 if (dc->user_creatable
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01001557 && !strncmp(name, str, len)) {
Hani Benhabiles992d3e62014-02-06 23:30:11 +01001558 readline_add_completion(rs, name);
1559 }
1560 elt = elt->next;
1561 }
1562 g_slist_free(list);
1563}
1564
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01001565void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles1094fd32014-02-06 23:30:13 +01001566{
1567 GSList *list, *elt;
1568 size_t len;
1569
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01001570 if (nb_args != 2) {
1571 return;
1572 }
1573
Hani Benhabiles1094fd32014-02-06 23:30:13 +01001574 len = strlen(str);
1575 readline_set_completion_index(rs, len);
1576 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
1577 while (elt) {
1578 const char *name;
1579
1580 name = object_class_get_name(OBJECT_CLASS(elt->data));
1581 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
1582 readline_add_completion(rs, name);
1583 }
1584 elt = elt->next;
1585 }
1586 g_slist_free(list);
1587}
1588
Marc-André Lureau91590152020-01-10 19:30:17 +04001589static int qdev_add_hotpluggable_device(Object *obj, void *opaque)
1590{
1591 GSList **list = opaque;
Philippe Mathieu-Daudé688ffbb2020-05-12 09:00:19 +02001592 DeviceState *dev = (DeviceState *)object_dynamic_cast(obj, TYPE_DEVICE);
Marc-André Lureau91590152020-01-10 19:30:17 +04001593
1594 if (dev == NULL) {
1595 return 0;
1596 }
1597
1598 if (dev->realized && object_property_get_bool(obj, "hotpluggable", NULL)) {
1599 *list = g_slist_append(*list, dev);
1600 }
1601
1602 return 0;
1603}
1604
1605static GSList *qdev_build_hotpluggable_device_list(Object *peripheral)
1606{
1607 GSList *list = NULL;
1608
1609 object_child_foreach(peripheral, qdev_add_hotpluggable_device, &list);
1610
1611 return list;
1612}
1613
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08001614static void peripheral_device_del_completion(ReadLineState *rs,
1615 const char *str, size_t len)
1616{
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02001617 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
1618 GSList *list, *item;
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08001619
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02001620 list = qdev_build_hotpluggable_device_list(peripheral);
1621 if (!list) {
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08001622 return;
1623 }
1624
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08001625 for (item = list; item; item = g_slist_next(item)) {
1626 DeviceState *dev = item->data;
1627
1628 if (dev->id && !strncmp(str, dev->id, len)) {
1629 readline_add_completion(rs, dev->id);
1630 }
1631 }
1632
1633 g_slist_free(list);
1634}
1635
Hani Benhabiles6297d9a2014-05-07 23:41:28 +01001636void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
1637{
1638 size_t len;
1639 ChardevInfoList *list, *start;
1640
1641 if (nb_args != 2) {
1642 return;
1643 }
1644 len = strlen(str);
1645 readline_set_completion_index(rs, len);
1646
1647 start = list = qmp_query_chardev(NULL);
1648 while (list) {
1649 ChardevInfo *chr = list->value;
1650
1651 if (!strncmp(chr->label, str, len)) {
1652 readline_add_completion(rs, chr->label);
1653 }
1654 list = list->next;
1655 }
1656 qapi_free_ChardevInfoList(start);
1657}
1658
Hani Benhabiles8e597772014-05-27 23:39:30 +01001659static void ringbuf_completion(ReadLineState *rs, const char *str)
1660{
1661 size_t len;
1662 ChardevInfoList *list, *start;
1663
1664 len = strlen(str);
1665 readline_set_completion_index(rs, len);
1666
1667 start = list = qmp_query_chardev(NULL);
1668 while (list) {
1669 ChardevInfo *chr_info = list->value;
1670
1671 if (!strncmp(chr_info->label, str, len)) {
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03001672 Chardev *chr = qemu_chr_find(chr_info->label);
Marc-André Lureau777357d2016-12-07 18:39:10 +03001673 if (chr && CHARDEV_IS_RINGBUF(chr)) {
Hani Benhabiles8e597772014-05-27 23:39:30 +01001674 readline_add_completion(rs, chr_info->label);
1675 }
1676 }
1677 list = list->next;
1678 }
1679 qapi_free_ChardevInfoList(start);
1680}
1681
Hani Benhabiles8e597772014-05-27 23:39:30 +01001682void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
1683{
1684 if (nb_args != 2) {
1685 return;
1686 }
1687 ringbuf_completion(rs, str);
1688}
1689
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01001690void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
1691{
1692 size_t len;
1693
1694 if (nb_args != 2) {
1695 return;
1696 }
1697
1698 len = strlen(str);
1699 readline_set_completion_index(rs, len);
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08001700 peripheral_device_del_completion(rs, str, len);
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01001701}
1702
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01001703void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabilesb48fa072014-02-06 23:30:12 +01001704{
1705 ObjectPropertyInfoList *list, *start;
1706 size_t len;
1707
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01001708 if (nb_args != 2) {
1709 return;
1710 }
Hani Benhabilesb48fa072014-02-06 23:30:12 +01001711 len = strlen(str);
1712 readline_set_completion_index(rs, len);
1713
1714 start = list = qmp_qom_list("/objects", NULL);
1715 while (list) {
1716 ObjectPropertyInfo *info = list->value;
1717
1718 if (!strncmp(info->type, "child<", 5)
1719 && !strncmp(info->name, str, len)) {
1720 readline_add_completion(rs, info->name);
1721 }
1722 list = list->next;
1723 }
1724 qapi_free_ObjectPropertyInfoList(start);
1725}
1726
Hani Benhabiles29136cd2014-05-07 23:41:27 +01001727void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
1728{
1729 int i;
1730 char *sep;
1731 size_t len;
1732
1733 if (nb_args != 2) {
1734 return;
1735 }
1736 sep = strrchr(str, '-');
1737 if (sep) {
1738 str = sep + 1;
1739 }
1740 len = strlen(str);
1741 readline_set_completion_index(rs, len);
Eric Blake7fb1cf12015-11-18 01:52:57 -07001742 for (i = 0; i < Q_KEY_CODE__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02001743 if (!strncmp(str, QKeyCode_str(i), len)) {
1744 readline_add_completion(rs, QKeyCode_str(i));
Hani Benhabiles29136cd2014-05-07 23:41:27 +01001745 }
1746 }
1747}
1748
Hani Benhabiles40d19392014-05-07 23:41:30 +01001749void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
1750{
1751 size_t len;
1752
1753 len = strlen(str);
1754 readline_set_completion_index(rs, len);
1755 if (nb_args == 2) {
Jason Wangeaed4832015-04-23 14:21:38 +08001756 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles40d19392014-05-07 23:41:30 +01001757 int count, i;
1758 count = qemu_find_net_clients_except(NULL, ncs,
Eric Blakef394b2e2016-07-13 21:50:23 -06001759 NET_CLIENT_DRIVER_NONE,
Jason Wangeaed4832015-04-23 14:21:38 +08001760 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08001761 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles40d19392014-05-07 23:41:30 +01001762 const char *name = ncs[i]->name;
1763 if (!strncmp(str, name, len)) {
1764 readline_add_completion(rs, name);
1765 }
1766 }
1767 } else if (nb_args == 3) {
1768 add_completion_option(rs, str, "on");
1769 add_completion_option(rs, str, "off");
1770 }
1771}
1772
Hani Benhabiles11b389f2014-05-07 23:41:32 +01001773void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
1774{
1775 int len, count, i;
Jason Wangeaed4832015-04-23 14:21:38 +08001776 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles11b389f2014-05-07 23:41:32 +01001777
1778 if (nb_args != 2) {
1779 return;
1780 }
1781
1782 len = strlen(str);
1783 readline_set_completion_index(rs, len);
Eric Blakef394b2e2016-07-13 21:50:23 -06001784 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_DRIVER_NIC,
Jason Wangeaed4832015-04-23 14:21:38 +08001785 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08001786 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles11b389f2014-05-07 23:41:32 +01001787 const char *name = ncs[i]->name;
1788 if (strncmp(str, name, len)) {
1789 continue;
1790 }
Eric Blake08712fc2020-03-17 15:17:11 -05001791 if (ncs[i]->is_netdev) {
Hani Benhabiles11b389f2014-05-07 23:41:32 +01001792 readline_add_completion(rs, name);
1793 }
1794 }
1795}
1796
Lluís Vilanovabd712112016-07-11 12:53:51 +02001797void info_trace_events_completion(ReadLineState *rs, int nb_args, const char *str)
1798{
1799 size_t len;
1800
1801 len = strlen(str);
1802 readline_set_completion_index(rs, len);
1803 if (nb_args == 2) {
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01001804 TraceEventIter iter;
1805 TraceEvent *ev;
1806 char *pattern = g_strdup_printf("%s*", str);
Gerd Hoffmann117856c2021-06-01 15:24:03 +02001807 trace_event_iter_init_pattern(&iter, pattern);
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01001808 while ((ev = trace_event_iter_next(&iter)) != NULL) {
1809 readline_add_completion(rs, trace_event_get_name(ev));
Lluís Vilanovabd712112016-07-11 12:53:51 +02001810 }
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01001811 g_free(pattern);
Lluís Vilanovabd712112016-07-11 12:53:51 +02001812 }
1813}
1814
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01001815void trace_event_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);
Gerd Hoffmann117856c2021-06-01 15:24:03 +02001825 trace_event_iter_init_pattern(&iter, pattern);
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01001826 while ((ev = trace_event_iter_next(&iter)) != NULL) {
1827 readline_add_completion(rs, trace_event_get_name(ev));
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01001828 }
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01001829 g_free(pattern);
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01001830 } else if (nb_args == 3) {
1831 add_completion_option(rs, str, "on");
1832 add_completion_option(rs, str, "off");
1833 }
1834}
1835
Hani Benhabilesd0ece342014-05-27 23:39:31 +01001836void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
1837{
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01001838 int i;
1839
Hani Benhabilesd0ece342014-05-27 23:39:31 +01001840 if (nb_args != 2) {
1841 return;
1842 }
1843 readline_set_completion_index(rs, strlen(str));
Michal Privoznik14d53b42017-09-07 10:05:24 +02001844 for (i = 0; i < WATCHDOG_ACTION__MAX; i++) {
1845 add_completion_option(rs, str, WatchdogAction_str(i));
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01001846 }
Hani Benhabilesd0ece342014-05-27 23:39:31 +01001847}
1848
Hani Benhabilesc68a0402014-05-27 23:39:32 +01001849void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
1850 const char *str)
1851{
1852 size_t len;
1853
1854 len = strlen(str);
1855 readline_set_completion_index(rs, len);
1856 if (nb_args == 2) {
1857 int i;
Eric Blake7fb1cf12015-11-18 01:52:57 -07001858 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02001859 const char *name = MigrationCapability_str(i);
Hani Benhabilesc68a0402014-05-27 23:39:32 +01001860 if (!strncmp(str, name, len)) {
1861 readline_add_completion(rs, name);
1862 }
1863 }
1864 } else if (nb_args == 3) {
1865 add_completion_option(rs, str, "on");
1866 add_completion_option(rs, str, "off");
1867 }
1868}
1869
Liang Li50e9a622015-03-23 16:32:29 +08001870void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
1871 const char *str)
1872{
1873 size_t len;
1874
1875 len = strlen(str);
1876 readline_set_completion_index(rs, len);
1877 if (nb_args == 2) {
1878 int i;
Eric Blake7fb1cf12015-11-18 01:52:57 -07001879 for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02001880 const char *name = MigrationParameter_str(i);
Liang Li50e9a622015-03-23 16:32:29 +08001881 if (!strncmp(str, name, len)) {
1882 readline_add_completion(rs, name);
1883 }
1884 }
1885 }
1886}
1887
Hani Benhabilesb21631f2014-05-27 23:39:37 +01001888static void vm_completion(ReadLineState *rs, const char *str)
1889{
1890 size_t len;
Kevin Wolf7c8eece2016-03-22 18:58:50 +01001891 BlockDriverState *bs;
Kevin Wolf88be7b42016-05-20 18:49:07 +02001892 BdrvNextIterator it;
Hani Benhabilesb21631f2014-05-27 23:39:37 +01001893
1894 len = strlen(str);
1895 readline_set_completion_index(rs, len);
Kevin Wolf7c8eece2016-03-22 18:58:50 +01001896
Kevin Wolf88be7b42016-05-20 18:49:07 +02001897 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Hani Benhabilesb21631f2014-05-27 23:39:37 +01001898 SnapshotInfoList *snapshots, *snapshot;
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03001899 AioContext *ctx = bdrv_get_aio_context(bs);
1900 bool ok = false;
Hani Benhabilesb21631f2014-05-27 23:39:37 +01001901
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03001902 aio_context_acquire(ctx);
1903 if (bdrv_can_snapshot(bs)) {
1904 ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0;
1905 }
1906 aio_context_release(ctx);
1907 if (!ok) {
Hani Benhabilesb21631f2014-05-27 23:39:37 +01001908 continue;
1909 }
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03001910
Hani Benhabilesb21631f2014-05-27 23:39:37 +01001911 snapshot = snapshots;
1912 while (snapshot) {
1913 char *completion = snapshot->value->name;
1914 if (!strncmp(str, completion, len)) {
1915 readline_add_completion(rs, completion);
1916 }
1917 completion = snapshot->value->id;
1918 if (!strncmp(str, completion, len)) {
1919 readline_add_completion(rs, completion);
1920 }
1921 snapshot = snapshot->next;
1922 }
1923 qapi_free_SnapshotInfoList(snapshots);
1924 }
1925
1926}
1927
1928void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
1929{
1930 if (nb_args == 2) {
1931 vm_completion(rs, str);
1932 }
1933}
1934
1935void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
1936{
1937 if (nb_args == 2) {
1938 vm_completion(rs, str);
1939 }
1940}
1941
Wayne Xia816f8922011-10-12 11:32:41 +08001942static int
1943compare_mon_cmd(const void *a, const void *b)
1944{
Kevin Wolfa0cd5e12019-06-13 17:33:56 +02001945 return strcmp(((const HMPCommand *)a)->name,
1946 ((const HMPCommand *)b)->name);
Wayne Xia816f8922011-10-12 11:32:41 +08001947}
1948
1949static void sortcmdlist(void)
1950{
Kevin Wolfa0cd5e12019-06-13 17:33:56 +02001951 qsort(hmp_cmds, ARRAY_SIZE(hmp_cmds) - 1,
1952 sizeof(*hmp_cmds),
1953 compare_mon_cmd);
1954 qsort(hmp_info_cmds, ARRAY_SIZE(hmp_info_cmds) - 1,
1955 sizeof(*hmp_info_cmds),
1956 compare_mon_cmd);
Wayne Xia816f8922011-10-12 11:32:41 +08001957}
1958
Gerd Hoffmannf0e48cb2021-06-24 12:38:32 +02001959void monitor_register_hmp(const char *name, bool info,
1960 void (*cmd)(Monitor *mon, const QDict *qdict))
1961{
1962 HMPCommand *table = info ? hmp_info_cmds : hmp_cmds;
1963
1964 while (table->name != NULL) {
1965 if (strcmp(table->name, name) == 0) {
1966 g_assert(table->cmd == NULL);
1967 table->cmd = cmd;
1968 return;
1969 }
1970 table++;
1971 }
1972 g_assert_not_reached();
1973}
1974
Peter Xu6adf08d2018-03-09 16:59:50 +08001975void monitor_init_globals(void)
1976{
Kevin Wolf1d95db72019-06-13 17:34:02 +02001977 monitor_init_globals_core();
Peter Xu6adf08d2018-03-09 16:59:50 +08001978 monitor_init_qmp_commands();
Peter Xu6adf08d2018-03-09 16:59:50 +08001979 sortcmdlist();
Peter Xu47451462018-06-08 11:55:11 +08001980 qemu_mutex_init(&mon_fdsets_lock);
Peter Xu6adf08d2018-03-09 16:59:50 +08001981}