blob: a22d911087529fd580887640ad9e02d938800920 [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"
blueswir1511d2b12009-03-07 15:32:56 +000026#include <dirent.h>
Paolo Bonzini33c11872016-03-15 16:58:45 +010027#include "qemu-common.h"
28#include "cpu.h"
pbrook87ecb682007-11-17 17:14:51 +000029#include "hw/hw.h"
Paolo Bonzinib4a42f82013-02-04 11:37:52 +010030#include "monitor/qdev.h"
pbrook87ecb682007-11-17 17:14:51 +000031#include "hw/usb.h"
Michael S. Tsirkina2cb15b2012-12-12 14:24:50 +020032#include "hw/pci/pci.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010033#include "sysemu/watchdog.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +020034#include "hw/loader.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010035#include "exec/gdbstub.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020036#include "net/net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000037#include "net/slirp.h"
Marc-André Lureau4d43a602017-01-26 18:26:44 +040038#include "chardev/char-fe.h"
Gerd Hoffmann75721502010-10-07 12:22:54 +020039#include "ui/qemu-spice.h"
Eduardo Habkoste35704b2015-02-08 16:51:16 -020040#include "sysemu/numa.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010041#include "monitor/monitor.h"
Marc-André Lureau213dcb02016-12-12 20:22:24 +030042#include "qemu/config-file.h"
Stefan Hajnoczi0150cd82013-11-14 11:54:15 +010043#include "qemu/readline.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010044#include "ui/console.h"
Gerd Hoffmannc751a742013-12-04 15:02:28 +010045#include "ui/input.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010046#include "sysemu/blockdev.h"
Max Reitzda27a002016-03-16 19:54:29 +010047#include "sysemu/block-backend.h"
pbrook87ecb682007-11-17 17:14:51 +000048#include "audio/audio.h"
Paolo Bonzini76cad712012-10-24 11:12:21 +020049#include "disas/disas.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010050#include "sysemu/balloon.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010051#include "qemu/timer.h"
Vincent Palatinb3946622017-01-10 11:59:55 +010052#include "sysemu/hw_accel.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010053#include "qemu/acl.h"
Paolo Bonzinibdee56f2013-04-02 18:28:41 +020054#include "sysemu/tpm.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010055#include "qapi/qmp/qdict.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010056#include "qapi/qmp/qerror.h"
Markus Armbruster6b673952018-02-01 12:18:35 +010057#include "qapi/qmp/qbool.h"
Markus Armbruster15280c32018-02-01 12:18:36 +010058#include "qapi/qmp/qnum.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010059#include "qapi/qmp/qjson.h"
60#include "qapi/qmp/json-streamer.h"
61#include "qapi/qmp/json-parser.h"
Markus Armbrustera9c94272016-06-22 19:11:19 +020062#include "qom/object_interfaces.h"
Daniel P. Berrange0ab8ed12017-01-25 16:14:15 +000063#include "trace-root.h"
Lluís31965ae2011-08-31 20:31:24 +020064#include "trace/control.h"
Pavel Butsykinbf957282015-09-10 18:38:59 +030065#include "monitor/hmp-target.h"
Lluís6d8a7642011-08-31 20:30:43 +020066#ifdef CONFIG_TRACE_SIMPLE
Lluís31965ae2011-08-31 20:31:24 +020067#include "trace/simple.h"
Prerna Saxena22890ab2010-06-24 17:04:53 +053068#endif
Paolo Bonzini022c62c2012-12-17 18:19:49 +010069#include "exec/memory.h"
Paolo Bonzini63c91552016-03-15 13:18:37 +010070#include "exec/exec-all.h"
Paolo Bonzini03dd0242015-12-15 13:16:16 +010071#include "qemu/log.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050072#include "qmp-commands.h"
73#include "hmp.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010074#include "qemu/thread.h"
Hani Benhabilesb21631f2014-05-27 23:39:37 +010075#include "block/qapi.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010076#include "qapi/error.h"
Wenchao Xia43a14cf2014-06-18 08:43:31 +020077#include "qapi/qmp-event.h"
78#include "qapi-event.h"
Markus Armbruster39a18152015-09-16 13:06:28 +020079#include "qmp-introspect.h"
Alberto Garciadc599972016-03-10 13:55:26 +020080#include "sysemu/qtest.h"
Paolo Bonzinid2528bd2017-03-03 12:01:16 +010081#include "sysemu/cpus.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020082#include "qemu/cutils.h"
Marc-André Lureauedcfaef2016-09-12 13:19:00 +040083#include "qapi/qmp/dispatch.h"
ths6a5bd302007-12-03 17:05:38 +000084
Jason J. Hernea4538a52015-06-26 14:07:21 -040085#if defined(TARGET_S390X)
86#include "hw/s390x/storage-keys.h"
Claudio Imbrendaf860d492016-08-15 18:44:04 +020087#include "hw/s390x/storage-attributes.h"
Jason J. Hernea4538a52015-06-26 14:07:21 -040088#endif
89
bellard9307c4c2004-04-04 12:57:25 +000090/*
91 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000092 *
bellard9307c4c2004-04-04 12:57:25 +000093 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000094 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000095 * 's' string (accept optional quote)
Wenchao Xia129be002013-08-27 20:38:26 +080096 * 'S' it just appends the rest of the string (accept optional quote)
Markus Armbruster361127d2010-02-10 20:24:35 +010097 * 'O' option string of the form NAME=VALUE,...
98 * parsed according to QemuOptsList given by its name
99 * Example: 'device:O' uses qemu_device_opts.
100 * Restriction: only lists with empty desc are supported
101 * TODO lift the restriction
bellard92a31b12005-02-10 22:00:52 +0000102 * 'i' 32 bit integer
103 * 'l' target long (32 or 64 bit)
Luiz Capitulino91162842012-04-26 17:34:30 -0300104 * 'M' Non-negative target long (32 or 64 bit), in user mode the
105 * value is multiplied by 2^20 (think Mebibyte)
Jes Sorensendbc0c672010-10-21 17:15:47 +0200106 * 'o' octets (aka bytes)
Kevin Wolf5e009842013-06-05 14:19:27 +0200107 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
108 * K, k suffix, which multiplies the value by 2^60 for suffixes E
109 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
110 * 2^30 for suffixes G and g, 2^20 for M and m, 2^10 for K and k
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +0100111 * 'T' double
112 * user mode accepts an optional ms, us, ns suffix,
113 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +0000114 * '/' optional gdb-like print format (like "/10x")
115 *
Luiz Capitulinofb466602009-08-28 15:27:27 -0300116 * '?' optional type (for all types, except '/')
117 * '.' other form of optional type (for 'i' and 'l')
Markus Armbruster942cd1f2010-03-26 09:07:09 +0100118 * 'b' boolean
119 * user mode accepts "on" or "off"
Luiz Capitulinofb466602009-08-28 15:27:27 -0300120 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +0000121 *
122 */
123
Anthony Liguoric227f092009-10-01 16:12:16 -0500124typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +0000125 const char *name;
bellard9307c4c2004-04-04 12:57:25 +0000126 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +0000127 const char *params;
128 const char *help;
Marc-André Lureau2b9e3572016-09-12 13:19:06 +0400129 void (*cmd)(Monitor *mon, const QDict *qdict);
130 /* @sub_table is a list of 2nd level of commands. If it does not exist,
131 * cmd should be used. If it exists, sub_table[?].cmd should be
132 * used, and cmd of 1st level plays the role of help function.
Wenchao Xia5f3d3352013-01-14 14:06:27 +0800133 */
134 struct mon_cmd_t *sub_table;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +0100135 void (*command_completion)(ReadLineState *rs, int nb_args, const char *str);
Anthony Liguoric227f092009-10-01 16:12:16 -0500136} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000137
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100138/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500139typedef struct mon_fd_t mon_fd_t;
140struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100141 char *name;
142 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500143 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100144};
145
Corey Bryantba1c0482012-08-14 16:43:43 -0400146/* file descriptor associated with a file descriptor set */
147typedef struct MonFdsetFd MonFdsetFd;
148struct MonFdsetFd {
149 int fd;
150 bool removed;
151 char *opaque;
152 QLIST_ENTRY(MonFdsetFd) next;
153};
154
155/* file descriptor set containing fds passed via SCM_RIGHTS */
156typedef struct MonFdset MonFdset;
157struct MonFdset {
158 int64_t id;
159 QLIST_HEAD(, MonFdsetFd) fds;
Corey Bryantadb696f2012-08-14 16:43:47 -0400160 QLIST_HEAD(, MonFdsetFd) dup_fds;
Corey Bryantba1c0482012-08-14 16:43:43 -0400161 QLIST_ENTRY(MonFdset) next;
162};
163
Markus Armbruster74358f22015-03-06 19:35:59 +0100164typedef struct {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200165 JSONMessageParser parser;
Markus Armbrusterf994b252015-03-06 19:51:51 +0100166 /*
167 * When a client connects, we're in capabilities negotiation mode.
168 * When command qmp_capabilities succeeds, we go into command
169 * mode.
170 */
Markus Armbruster635db182017-03-03 13:32:27 +0100171 QmpCommandList *commands;
Markus Armbruster74358f22015-03-06 19:35:59 +0100172} MonitorQMP;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200173
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100174/*
175 * To prevent flooding clients, events can be throttled. The
176 * throttling is calculated globally, rather than per-Monitor
177 * instance.
178 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200179typedef struct MonitorQAPIEventState {
Markus Armbruster7de0be62015-10-15 17:08:35 +0200180 QAPIEvent event; /* Throttling state for this event type and... */
181 QDict *data; /* ... data, see qapi_event_throttle_equal() */
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100182 QEMUTimer *timer; /* Timer for handling delayed events */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200183 QDict *qdict; /* Delayed event (if any) */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200184} MonitorQAPIEventState;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100185
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200186typedef struct {
187 int64_t rate; /* Minimum time (in ns) between two events */
188} MonitorQAPIEventConf;
189
aliguori87127162009-03-05 23:01:29 +0000190struct Monitor {
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300191 CharBackend chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200192 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000193 int flags;
194 int suspend_cnt;
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400195 bool skip_flush;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200196
197 QemuMutex out_lock;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400198 QString *outbuf;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200199 guint out_watch;
200
201 /* Read under either BQL or out_lock, written with BQL+out_lock. */
202 int mux_out;
203
aliguori731b0362009-03-05 23:01:42 +0000204 ReadLineState *rs;
Markus Armbruster74358f22015-03-06 19:35:59 +0100205 MonitorQMP qmp;
Greg Kurz751f8cf2017-10-17 10:16:22 +0200206 gchar *mon_cpu_path;
Markus Armbruster097310b2014-10-07 13:59:15 +0200207 BlockCompletionFunc *password_completion_cb;
aliguori731b0362009-03-05 23:01:42 +0000208 void *password_opaque;
Wenchao Xia77172392013-08-27 20:38:20 +0800209 mon_cmd_t *cmd_table;
Anthony Liguoric227f092009-10-01 16:12:16 -0500210 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000211 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000212};
213
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300214/* QMP checker flags */
215#define QMP_ACCEPT_UNKNOWNS 1
216
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200217/* Protects mon_list, monitor_event_state. */
218static QemuMutex monitor_lock;
219
Blue Swirl72cf2d42009-09-12 07:36:22 +0000220static QLIST_HEAD(mon_list, Monitor) mon_list;
Corey Bryantba1c0482012-08-14 16:43:43 -0400221static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
Corey Bryantefb87c12012-08-14 16:43:48 -0400222static int mon_refcount;
bellard7e2515e2004-08-01 21:52:19 +0000223
Wayne Xia816f8922011-10-12 11:32:41 +0800224static mon_cmd_t mon_cmds[];
225static mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000226
Markus Armbruster635db182017-03-03 13:32:27 +0100227QmpCommandList qmp_commands, qmp_cap_negotiation_commands;
Markus Armbruster1527bad2017-03-03 13:32:25 +0100228
Markus Armbruster8631b602010-02-18 11:41:55 +0100229Monitor *cur_mon;
aliguori376253e2009-03-05 23:01:23 +0000230
Alberto Garciadc599972016-03-10 13:55:26 +0200231static QEMUClockType event_clock_type = QEMU_CLOCK_REALTIME;
232
Stefan Hajnoczic60bf332013-11-14 11:54:14 +0100233static void monitor_command_cb(void *opaque, const char *cmdline,
234 void *readline_opaque);
aliguori83ab7952008-08-19 14:44:22 +0000235
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100236/**
237 * Is @mon a QMP monitor?
238 */
239static inline bool monitor_is_qmp(const Monitor *mon)
Luiz Capitulino418173c2009-11-26 22:58:51 -0200240{
241 return (mon->flags & MONITOR_USE_CONTROL);
242}
243
Markus Armbruster489653b2015-03-06 20:01:05 +0100244/**
245 * Is the current monitor, if any, a QMP monitor?
246 */
247bool monitor_cur_is_qmp(void)
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100248{
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100249 return cur_mon && monitor_is_qmp(cur_mon);
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100250}
251
Anthony Liguori7060b472011-09-02 12:34:50 -0500252void monitor_read_command(Monitor *mon, int show_prompt)
aliguori731b0362009-03-05 23:01:42 +0000253{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200254 if (!mon->rs)
255 return;
256
aliguori731b0362009-03-05 23:01:42 +0000257 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
258 if (show_prompt)
259 readline_show_prompt(mon->rs);
260}
bellard6a00d602005-11-21 23:25:50 +0000261
Anthony Liguori7060b472011-09-02 12:34:50 -0500262int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
263 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000264{
Markus Armbrusterbcf5d192015-03-12 17:26:46 +0100265 if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000266 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
267 /* prompt is printed on return from the command handler */
268 return 0;
269 } else {
270 monitor_printf(mon, "terminal does not support password prompting\n");
271 return -ENOTTY;
272 }
aliguoribb5fc202009-03-05 23:01:15 +0000273}
274
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200275static void monitor_flush_locked(Monitor *mon);
276
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100277static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
278 void *opaque)
279{
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200280 Monitor *mon = opaque;
281
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200282 qemu_mutex_lock(&mon->out_lock);
283 mon->out_watch = 0;
284 monitor_flush_locked(mon);
285 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100286 return FALSE;
287}
288
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200289/* Called with mon->out_lock held. */
290static void monitor_flush_locked(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000291{
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100292 int rc;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400293 size_t len;
294 const char *buf;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100295
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400296 if (mon->skip_flush) {
297 return;
298 }
299
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400300 buf = qstring_get_str(mon->outbuf);
301 len = qstring_get_length(mon->outbuf);
302
Paolo Bonzinia4cc73d2013-05-31 14:00:27 +0200303 if (len && !mon->mux_out) {
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300304 rc = qemu_chr_fe_write(&mon->chr, (const uint8_t *) buf, len);
Stratos Psomadakis056f49f2014-01-27 12:30:15 +0200305 if ((rc < 0 && errno != EAGAIN) || (rc == len)) {
306 /* all flushed or error */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400307 QDECREF(mon->outbuf);
308 mon->outbuf = qstring_new();
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100309 return;
310 }
311 if (rc > 0) {
Eric Blake3b7c78c2016-05-17 16:00:15 -0600312 /* partial write */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400313 QString *tmp = qstring_from_str(buf + rc);
314 QDECREF(mon->outbuf);
315 mon->outbuf = tmp;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100316 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200317 if (mon->out_watch == 0) {
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300318 mon->out_watch =
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300319 qemu_chr_fe_add_watch(&mon->chr, G_IO_OUT | G_IO_HUP,
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300320 monitor_unblocked, mon);
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200321 }
bellard7e2515e2004-08-01 21:52:19 +0000322 }
323}
324
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200325void monitor_flush(Monitor *mon)
326{
327 qemu_mutex_lock(&mon->out_lock);
328 monitor_flush_locked(mon);
329 qemu_mutex_unlock(&mon->out_lock);
330}
331
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400332/* flush at every end of line */
aliguori376253e2009-03-05 23:01:23 +0000333static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000334{
ths60fe76f2007-12-16 03:02:09 +0000335 char c;
aliguori731b0362009-03-05 23:01:42 +0000336
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200337 qemu_mutex_lock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000338 for(;;) {
339 c = *str++;
340 if (c == '\0')
341 break;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400342 if (c == '\n') {
343 qstring_append_chr(mon->outbuf, '\r');
344 }
345 qstring_append_chr(mon->outbuf, c);
346 if (c == '\n') {
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200347 monitor_flush_locked(mon);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400348 }
bellard7e2515e2004-08-01 21:52:19 +0000349 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200350 qemu_mutex_unlock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000351}
352
aliguori376253e2009-03-05 23:01:23 +0000353void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000354{
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400355 char *buf;
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200356
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200357 if (!mon)
358 return;
359
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100360 if (monitor_is_qmp(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200361 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200362 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200363
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400364 buf = g_strdup_vprintf(fmt, ap);
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200365 monitor_puts(mon, buf);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400366 g_free(buf);
bellard7e2515e2004-08-01 21:52:19 +0000367}
368
aliguori376253e2009-03-05 23:01:23 +0000369void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000370{
371 va_list ap;
372 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000373 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000374 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000375}
376
Pavel Butsykincaf15312015-09-22 16:18:17 +0300377int monitor_fprintf(FILE *stream, const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000378{
379 va_list ap;
380 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000381 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000382 va_end(ap);
383 return 0;
384}
385
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200386static void monitor_json_emitter(Monitor *mon, const QObject *data)
387{
388 QString *json;
389
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200390 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
391 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200392 assert(json != NULL);
393
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200394 qstring_append_chr(json, '\n');
395 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200396
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200397 QDECREF(json);
398}
399
Eric Blake7fb1cf12015-11-18 01:52:57 -0700400static MonitorQAPIEventConf monitor_qapi_event_conf[QAPI_EVENT__MAX] = {
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200401 /* Limit guest-triggerable events to 1 per second */
402 [QAPI_EVENT_RTC_CHANGE] = { 1000 * SCALE_MS },
403 [QAPI_EVENT_WATCHDOG] = { 1000 * SCALE_MS },
404 [QAPI_EVENT_BALLOON_CHANGE] = { 1000 * SCALE_MS },
405 [QAPI_EVENT_QUORUM_REPORT_BAD] = { 1000 * SCALE_MS },
406 [QAPI_EVENT_QUORUM_FAILURE] = { 1000 * SCALE_MS },
407 [QAPI_EVENT_VSERPORT_CHANGE] = { 1000 * SCALE_MS },
408};
409
Markus Armbrustera24712a2015-10-15 17:08:34 +0200410GHashTable *monitor_qapi_event_state;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100411
412/*
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200413 * Emits the event to every monitor instance, @event is only used for trace
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200414 * Called with monitor_lock held.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100415 */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200416static void monitor_qapi_event_emit(QAPIEvent event, QDict *qdict)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100417{
418 Monitor *mon;
419
Markus Armbruster688b4b72015-10-15 17:08:30 +0200420 trace_monitor_protocol_event_emit(event, qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100421 QLIST_FOREACH(mon, &mon_list, entry) {
Markus Armbruster635db182017-03-03 13:32:27 +0100422 if (monitor_is_qmp(mon)
423 && mon->qmp.commands != &qmp_cap_negotiation_commands) {
Markus Armbruster688b4b72015-10-15 17:08:30 +0200424 monitor_json_emitter(mon, QOBJECT(qdict));
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100425 }
426 }
427}
428
Markus Armbrustera24712a2015-10-15 17:08:34 +0200429static void monitor_qapi_event_handler(void *opaque);
430
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100431/*
432 * Queue a new event for emission to Monitor instances,
433 * applying any rate limiting if required.
434 */
435static void
Markus Armbruster688b4b72015-10-15 17:08:30 +0200436monitor_qapi_event_queue(QAPIEvent event, QDict *qdict, Error **errp)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100437{
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200438 MonitorQAPIEventConf *evconf;
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200439 MonitorQAPIEventState *evstate;
Markus Armbruster93f8f982015-10-15 17:08:31 +0200440
Eric Blake7fb1cf12015-11-18 01:52:57 -0700441 assert(event < QAPI_EVENT__MAX);
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200442 evconf = &monitor_qapi_event_conf[event];
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200443 trace_monitor_protocol_event_queue(event, qdict, evconf->rate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100444
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200445 qemu_mutex_lock(&monitor_lock);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200446
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200447 if (!evconf->rate) {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200448 /* Unthrottled event */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200449 monitor_qapi_event_emit(event, qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100450 } else {
Markus Armbruster7de0be62015-10-15 17:08:35 +0200451 QDict *data = qobject_to_qdict(qdict_get(qdict, "data"));
452 MonitorQAPIEventState key = { .event = event, .data = data };
Markus Armbrustera24712a2015-10-15 17:08:34 +0200453
454 evstate = g_hash_table_lookup(monitor_qapi_event_state, &key);
455 assert(!evstate || timer_pending(evstate->timer));
456
457 if (evstate) {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200458 /*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200459 * Timer is pending for (at least) evconf->rate ns after
Markus Armbruster93f8f982015-10-15 17:08:31 +0200460 * last send. Store event for sending when timer fires,
461 * replacing a prior stored event if any.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100462 */
Markus Armbruster93f8f982015-10-15 17:08:31 +0200463 QDECREF(evstate->qdict);
Markus Armbruster688b4b72015-10-15 17:08:30 +0200464 evstate->qdict = qdict;
465 QINCREF(evstate->qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100466 } else {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200467 /*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200468 * Last send was (at least) evconf->rate ns ago.
Markus Armbruster93f8f982015-10-15 17:08:31 +0200469 * Send immediately, and arm the timer to call
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200470 * monitor_qapi_event_handler() in evconf->rate ns. Any
Markus Armbruster93f8f982015-10-15 17:08:31 +0200471 * events arriving before then will be delayed until then.
472 */
Alberto Garciadc599972016-03-10 13:55:26 +0200473 int64_t now = qemu_clock_get_ns(event_clock_type);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200474
Markus Armbruster688b4b72015-10-15 17:08:30 +0200475 monitor_qapi_event_emit(event, qdict);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200476
477 evstate = g_new(MonitorQAPIEventState, 1);
478 evstate->event = event;
Markus Armbruster7de0be62015-10-15 17:08:35 +0200479 evstate->data = data;
480 QINCREF(evstate->data);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200481 evstate->qdict = NULL;
Alberto Garciadc599972016-03-10 13:55:26 +0200482 evstate->timer = timer_new_ns(event_clock_type,
Markus Armbrustera24712a2015-10-15 17:08:34 +0200483 monitor_qapi_event_handler,
484 evstate);
485 g_hash_table_add(monitor_qapi_event_state, evstate);
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200486 timer_mod_ns(evstate->timer, now + evconf->rate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100487 }
488 }
Markus Armbruster93f8f982015-10-15 17:08:31 +0200489
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200490 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100491}
492
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100493/*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200494 * This function runs evconf->rate ns after sending a throttled
Markus Armbruster93f8f982015-10-15 17:08:31 +0200495 * event.
496 * If another event has since been stored, send it.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100497 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200498static void monitor_qapi_event_handler(void *opaque)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100499{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200500 MonitorQAPIEventState *evstate = opaque;
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200501 MonitorQAPIEventConf *evconf = &monitor_qapi_event_conf[evstate->event];
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100502
Markus Armbruster93f8f982015-10-15 17:08:31 +0200503 trace_monitor_protocol_event_handler(evstate->event, evstate->qdict);
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200504 qemu_mutex_lock(&monitor_lock);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200505
Markus Armbruster688b4b72015-10-15 17:08:30 +0200506 if (evstate->qdict) {
Alberto Garciadc599972016-03-10 13:55:26 +0200507 int64_t now = qemu_clock_get_ns(event_clock_type);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200508
Markus Armbruster688b4b72015-10-15 17:08:30 +0200509 monitor_qapi_event_emit(evstate->event, evstate->qdict);
510 QDECREF(evstate->qdict);
511 evstate->qdict = NULL;
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200512 timer_mod_ns(evstate->timer, now + evconf->rate);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200513 } else {
514 g_hash_table_remove(monitor_qapi_event_state, evstate);
Markus Armbruster7de0be62015-10-15 17:08:35 +0200515 QDECREF(evstate->data);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200516 timer_free(evstate->timer);
517 g_free(evstate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100518 }
Markus Armbruster93f8f982015-10-15 17:08:31 +0200519
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200520 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100521}
522
Markus Armbrustera24712a2015-10-15 17:08:34 +0200523static unsigned int qapi_event_throttle_hash(const void *key)
524{
525 const MonitorQAPIEventState *evstate = key;
Markus Armbruster7de0be62015-10-15 17:08:35 +0200526 unsigned int hash = evstate->event * 255;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200527
Markus Armbruster7de0be62015-10-15 17:08:35 +0200528 if (evstate->event == QAPI_EVENT_VSERPORT_CHANGE) {
529 hash += g_str_hash(qdict_get_str(evstate->data, "id"));
530 }
531
Alberto Garcia6d425eb2016-03-10 13:55:25 +0200532 if (evstate->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
533 hash += g_str_hash(qdict_get_str(evstate->data, "node-name"));
534 }
535
Markus Armbruster7de0be62015-10-15 17:08:35 +0200536 return hash;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200537}
538
539static gboolean qapi_event_throttle_equal(const void *a, const void *b)
540{
541 const MonitorQAPIEventState *eva = a;
542 const MonitorQAPIEventState *evb = b;
543
Markus Armbruster7de0be62015-10-15 17:08:35 +0200544 if (eva->event != evb->event) {
545 return FALSE;
546 }
547
548 if (eva->event == QAPI_EVENT_VSERPORT_CHANGE) {
549 return !strcmp(qdict_get_str(eva->data, "id"),
550 qdict_get_str(evb->data, "id"));
551 }
552
Alberto Garcia6d425eb2016-03-10 13:55:25 +0200553 if (eva->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
554 return !strcmp(qdict_get_str(eva->data, "node-name"),
555 qdict_get_str(evb->data, "node-name"));
556 }
557
Markus Armbruster7de0be62015-10-15 17:08:35 +0200558 return TRUE;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200559}
560
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200561static void monitor_qapi_event_init(void)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100562{
Alberto Garciadc599972016-03-10 13:55:26 +0200563 if (qtest_enabled()) {
564 event_clock_type = QEMU_CLOCK_VIRTUAL;
565 }
566
Markus Armbrustera24712a2015-10-15 17:08:34 +0200567 monitor_qapi_event_state = g_hash_table_new(qapi_event_throttle_hash,
568 qapi_event_throttle_equal);
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200569 qmp_event_set_func_emit(monitor_qapi_event_queue);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200570}
571
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100572static void handle_hmp_command(Monitor *mon, const char *cmdline);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200573
Wenchao Xiab01fe892013-08-27 20:38:19 +0800574static void monitor_data_init(Monitor *mon)
575{
576 memset(mon, 0, sizeof(Monitor));
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200577 qemu_mutex_init(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800578 mon->outbuf = qstring_new();
Wenchao Xia77172392013-08-27 20:38:20 +0800579 /* Use *mon_cmds by default. */
580 mon->cmd_table = mon_cmds;
Wenchao Xiab01fe892013-08-27 20:38:19 +0800581}
582
583static void monitor_data_destroy(Monitor *mon)
584{
Greg Kurz751f8cf2017-10-17 10:16:22 +0200585 g_free(mon->mon_cpu_path);
Marc-André Lureau1ce26102017-01-27 00:49:13 +0400586 qemu_chr_fe_deinit(&mon->chr, false);
Marc-André Lureau2ef45712016-08-01 15:23:42 +0400587 if (monitor_is_qmp(mon)) {
588 json_message_parser_destroy(&mon->qmp.parser);
589 }
Marc-André Lureaue5dc1a62018-01-04 17:05:15 +0100590 readline_free(mon->rs);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800591 QDECREF(mon->outbuf);
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200592 qemu_mutex_destroy(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800593}
594
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200595char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
596 int64_t cpu_index, Error **errp)
Luiz Capitulino0268d972010-10-22 10:08:02 -0200597{
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200598 char *output = NULL;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200599 Monitor *old_mon, hmp;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200600
Wenchao Xiab01fe892013-08-27 20:38:19 +0800601 monitor_data_init(&hmp);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400602 hmp.skip_flush = true;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200603
604 old_mon = cur_mon;
605 cur_mon = &hmp;
606
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200607 if (has_cpu_index) {
608 int ret = monitor_set_cpu(cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200609 if (ret < 0) {
610 cur_mon = old_mon;
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100611 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
612 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200613 goto out;
614 }
615 }
616
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100617 handle_hmp_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200618 cur_mon = old_mon;
619
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200620 qemu_mutex_lock(&hmp.out_lock);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400621 if (qstring_get_length(hmp.outbuf) > 0) {
622 output = g_strdup(qstring_get_str(hmp.outbuf));
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200623 } else {
624 output = g_strdup("");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200625 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200626 qemu_mutex_unlock(&hmp.out_lock);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200627
628out:
Wenchao Xiab01fe892013-08-27 20:38:19 +0800629 monitor_data_destroy(&hmp);
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200630 return output;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200631}
632
bellard9dc39cb2004-03-14 21:38:27 +0000633static int compare_cmd(const char *name, const char *list)
634{
635 const char *p, *pstart;
636 int len;
637 len = strlen(name);
638 p = list;
639 for(;;) {
640 pstart = p;
641 p = strchr(p, '|');
642 if (!p)
643 p = pstart + strlen(pstart);
644 if ((p - pstart) == len && !memcmp(pstart, name, len))
645 return 1;
646 if (*p == '\0')
647 break;
648 p++;
649 }
650 return 0;
651}
652
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800653static int get_str(char *buf, int buf_size, const char **pp)
654{
655 const char *p;
656 char *q;
657 int c;
658
659 q = buf;
660 p = *pp;
661 while (qemu_isspace(*p)) {
662 p++;
663 }
664 if (*p == '\0') {
665 fail:
666 *q = '\0';
667 *pp = p;
668 return -1;
669 }
670 if (*p == '\"') {
671 p++;
672 while (*p != '\0' && *p != '\"') {
673 if (*p == '\\') {
674 p++;
675 c = *p++;
676 switch (c) {
677 case 'n':
678 c = '\n';
679 break;
680 case 'r':
681 c = '\r';
682 break;
683 case '\\':
684 case '\'':
685 case '\"':
686 break;
687 default:
Peter Maydell71baf782015-08-19 16:20:19 +0100688 printf("unsupported escape code: '\\%c'\n", c);
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800689 goto fail;
690 }
691 if ((q - buf) < buf_size - 1) {
692 *q++ = c;
693 }
694 } else {
695 if ((q - buf) < buf_size - 1) {
696 *q++ = *p;
697 }
698 p++;
699 }
700 }
701 if (*p != '\"') {
Peter Maydell71baf782015-08-19 16:20:19 +0100702 printf("unterminated string\n");
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800703 goto fail;
704 }
705 p++;
706 } else {
707 while (*p != '\0' && !qemu_isspace(*p)) {
708 if ((q - buf) < buf_size - 1) {
709 *q++ = *p;
710 }
711 p++;
712 }
713 }
714 *q = '\0';
715 *pp = p;
716 return 0;
717}
718
719#define MAX_ARGS 16
720
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800721static void free_cmdline_args(char **args, int nb_args)
722{
723 int i;
724
725 assert(nb_args <= MAX_ARGS);
726
727 for (i = 0; i < nb_args; i++) {
728 g_free(args[i]);
729 }
730
731}
732
733/*
734 * Parse the command line to get valid args.
735 * @cmdline: command line to be parsed.
736 * @pnb_args: location to store the number of args, must NOT be NULL.
737 * @args: location to store the args, which should be freed by caller, must
738 * NOT be NULL.
739 *
740 * Returns 0 on success, negative on failure.
741 *
742 * NOTE: this parser is an approximate form of the real command parser. Number
743 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
744 * return with failure.
745 */
746static int parse_cmdline(const char *cmdline,
747 int *pnb_args, char **args)
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800748{
749 const char *p;
750 int nb_args, ret;
751 char buf[1024];
752
753 p = cmdline;
754 nb_args = 0;
755 for (;;) {
756 while (qemu_isspace(*p)) {
757 p++;
758 }
759 if (*p == '\0') {
760 break;
761 }
762 if (nb_args >= MAX_ARGS) {
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800763 goto fail;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800764 }
765 ret = get_str(buf, sizeof(buf), &p);
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800766 if (ret < 0) {
767 goto fail;
768 }
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800769 args[nb_args] = g_strdup(buf);
770 nb_args++;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800771 }
772 *pnb_args = nb_args;
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800773 return 0;
774
775 fail:
776 free_cmdline_args(args, nb_args);
777 return -1;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800778}
779
Wenchao Xia66855492013-08-27 20:38:23 +0800780static void help_cmd_dump_one(Monitor *mon,
781 const mon_cmd_t *cmd,
782 char **prefix_args,
783 int prefix_args_nb)
784{
785 int i;
786
787 for (i = 0; i < prefix_args_nb; i++) {
788 monitor_printf(mon, "%s ", prefix_args[i]);
789 }
790 monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
791}
792
793/* @args[@arg_index] is the valid command need to find in @cmds */
Anthony Liguoric227f092009-10-01 16:12:16 -0500794static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
Wenchao Xia66855492013-08-27 20:38:23 +0800795 char **args, int nb_args, int arg_index)
bellard9dc39cb2004-03-14 21:38:27 +0000796{
Anthony Liguoric227f092009-10-01 16:12:16 -0500797 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000798
Wenchao Xia66855492013-08-27 20:38:23 +0800799 /* No valid arg need to compare with, dump all in *cmds */
800 if (arg_index >= nb_args) {
801 for (cmd = cmds; cmd->name != NULL; cmd++) {
802 help_cmd_dump_one(mon, cmd, args, arg_index);
803 }
804 return;
805 }
806
807 /* Find one entry to dump */
808 for (cmd = cmds; cmd->name != NULL; cmd++) {
809 if (compare_cmd(args[arg_index], cmd->name)) {
810 if (cmd->sub_table) {
811 /* continue with next arg */
812 help_cmd_dump(mon, cmd->sub_table,
813 args, nb_args, arg_index + 1);
814 } else {
815 help_cmd_dump_one(mon, cmd, args, arg_index);
816 }
817 break;
818 }
bellard9dc39cb2004-03-14 21:38:27 +0000819 }
820}
821
aliguori376253e2009-03-05 23:01:23 +0000822static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000823{
Wenchao Xia66855492013-08-27 20:38:23 +0800824 char *args[MAX_ARGS];
825 int nb_args = 0;
826
827 /* 1. parse user input */
828 if (name) {
829 /* special case for log, directly dump and return */
830 if (!strcmp(name, "log")) {
Peter Maydell38dad9e2013-02-11 16:41:25 +0000831 const QEMULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000832 monitor_printf(mon, "Log items (comma separated):\n");
833 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
Peter Maydell38dad9e2013-02-11 16:41:25 +0000834 for (item = qemu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000835 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000836 }
Wenchao Xia66855492013-08-27 20:38:23 +0800837 return;
838 }
839
840 if (parse_cmdline(name, &nb_args, args) < 0) {
841 return;
bellardf193c792004-03-21 17:06:25 +0000842 }
bellard9dc39cb2004-03-14 21:38:27 +0000843 }
Wenchao Xia66855492013-08-27 20:38:23 +0800844
845 /* 2. dump the contents according to parsed args */
846 help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0);
847
848 free_cmdline_args(args, nb_args);
bellard9dc39cb2004-03-14 21:38:27 +0000849}
850
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300851static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300852{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300853 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300854}
855
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100856static void hmp_trace_event(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530857{
858 const char *tp_name = qdict_get_str(qdict, "name");
859 bool new_state = qdict_get_bool(qdict, "option");
Lluís Vilanova77e2b172016-07-11 12:53:57 +0200860 bool has_vcpu = qdict_haskey(qdict, "vcpu");
861 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
Lluís Vilanova14101d02014-08-25 13:20:03 +0200862 Error *local_err = NULL;
Blue Swirlf871d682010-10-13 19:14:29 +0000863
Lluís Vilanova77e2b172016-07-11 12:53:57 +0200864 if (vcpu < 0) {
865 monitor_printf(mon, "argument vcpu must be positive");
866 return;
867 }
868
869 qmp_trace_event_set_state(tp_name, new_state, true, true, has_vcpu, vcpu, &local_err);
Lluís Vilanova14101d02014-08-25 13:20:03 +0200870 if (local_err) {
Markus Armbruster091e38b2015-02-10 15:15:43 +0100871 error_report_err(local_err);
Blue Swirlf871d682010-10-13 19:14:29 +0000872 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530873}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100874
Michael Rothc45a8162011-10-02 08:44:37 -0500875#ifdef CONFIG_TRACE_SIMPLE
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100876static void hmp_trace_file(Monitor *mon, const QDict *qdict)
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100877{
878 const char *op = qdict_get_try_str(qdict, "op");
879 const char *arg = qdict_get_try_str(qdict, "arg");
880
881 if (!op) {
882 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
883 } else if (!strcmp(op, "on")) {
884 st_set_trace_file_enabled(true);
885 } else if (!strcmp(op, "off")) {
886 st_set_trace_file_enabled(false);
887 } else if (!strcmp(op, "flush")) {
888 st_flush_trace_buffer();
889 } else if (!strcmp(op, "set")) {
890 if (arg) {
891 st_set_trace_file(arg);
892 }
893 } else {
894 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
895 help_cmd(mon, "trace-file");
896 }
897}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530898#endif
899
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100900static void hmp_info_help(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000901{
Luiz Capitulino13c74252009-10-07 13:41:55 -0300902 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000903}
904
Marc-André Lureau9e812b62016-09-12 13:19:05 +0400905static void query_commands_cb(QmpCommand *cmd, void *opaque)
bellard9bc9d1c2004-10-10 15:15:51 +0000906{
Marc-André Lureau9e812b62016-09-12 13:19:05 +0400907 CommandInfoList *info, **list = opaque;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200908
Marc-André Lureau9e812b62016-09-12 13:19:05 +0400909 if (!cmd->enabled) {
910 return;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200911 }
912
Marc-André Lureau9e812b62016-09-12 13:19:05 +0400913 info = g_malloc0(sizeof(*info));
914 info->value = g_malloc0(sizeof(*info->value));
915 info->value->name = g_strdup(cmd->name);
916 info->next = *list;
917 *list = info;
918}
919
920CommandInfoList *qmp_query_commands(Error **errp)
921{
922 CommandInfoList *list = NULL;
923
Markus Armbruster635db182017-03-03 13:32:27 +0100924 qmp_for_each_command(cur_mon->qmp.commands, query_commands_cb, &list);
Marc-André Lureau9e812b62016-09-12 13:19:05 +0400925
926 return list;
thsa36e69d2007-12-02 05:18:19 +0000927}
928
Daniel P. Berrange48608532012-05-21 17:59:51 +0100929EventInfoList *qmp_query_events(Error **errp)
930{
931 EventInfoList *info, *ev_list = NULL;
Wenchao Xia75175172014-06-18 08:43:54 +0200932 QAPIEvent e;
Daniel P. Berrange48608532012-05-21 17:59:51 +0100933
Eric Blake7fb1cf12015-11-18 01:52:57 -0700934 for (e = 0 ; e < QAPI_EVENT__MAX ; e++) {
Markus Armbruster977c7362017-08-24 10:46:08 +0200935 const char *event_name = QAPIEvent_str(e);
Daniel P. Berrange48608532012-05-21 17:59:51 +0100936 assert(event_name != NULL);
937 info = g_malloc0(sizeof(*info));
938 info->value = g_malloc0(sizeof(*info->value));
939 info->value->name = g_strdup(event_name);
940
941 info->next = ev_list;
942 ev_list = info;
943 }
944
945 return ev_list;
946}
947
Markus Armbruster39a18152015-09-16 13:06:28 +0200948/*
949 * Minor hack: generated marshalling suppressed for this command
950 * ('gen': false in the schema) so we can parse the JSON string
951 * directly into QObject instead of first parsing it with
952 * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
953 * to QObject with generated output marshallers, every time. Instead,
Daniel P. Berrangeb3db2112016-09-30 15:45:27 +0100954 * we do it in test-qobject-input-visitor.c, just to make sure
Markus Armbruster39a18152015-09-16 13:06:28 +0200955 * qapi-introspect.py's output actually conforms to the schema.
956 */
957static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
958 Error **errp)
959{
Markus Armbrusterb0f36b22017-02-28 22:27:02 +0100960 *ret_data = qobject_from_json(qmp_schema_json, &error_abort);
Markus Armbruster39a18152015-09-16 13:06:28 +0200961}
962
Marc-André Lureau5032a162016-09-12 13:19:02 +0400963/*
Marc-André Lureau5032a162016-09-12 13:19:02 +0400964 * We used to define commands in qmp-commands.hx in addition to the
965 * QAPI schema. This permitted defining some of them only in certain
966 * configurations. query-commands has always reflected that (good,
967 * because it lets QMP clients figure out what's actually available),
968 * while query-qmp-schema never did (not so good). This function is a
969 * hack to keep the configuration-specific commands defined exactly as
970 * before, even though qmp-commands.hx is gone.
971 *
972 * FIXME Educate the QAPI schema on configuration-specific commands,
973 * and drop this hack.
974 */
975static void qmp_unregister_commands_hack(void)
976{
977#ifndef CONFIG_SPICE
Markus Armbruster1527bad2017-03-03 13:32:25 +0100978 qmp_unregister_command(&qmp_commands, "query-spice");
Marc-André Lureau5032a162016-09-12 13:19:02 +0400979#endif
Markus Armbruster38bb54f2017-04-27 15:00:53 +0200980#ifndef CONFIG_REPLICATION
981 qmp_unregister_command(&qmp_commands, "xen-set-replication");
982 qmp_unregister_command(&qmp_commands, "query-xen-replication-status");
983 qmp_unregister_command(&qmp_commands, "xen-colo-do-checkpoint");
984#endif
Marc-André Lureau5032a162016-09-12 13:19:02 +0400985#ifndef TARGET_I386
Markus Armbruster1527bad2017-03-03 13:32:25 +0100986 qmp_unregister_command(&qmp_commands, "rtc-reset-reinjection");
Marc-André Lureau5032a162016-09-12 13:19:02 +0400987#endif
988#ifndef TARGET_S390X
Markus Armbruster1527bad2017-03-03 13:32:25 +0100989 qmp_unregister_command(&qmp_commands, "dump-skeys");
Marc-André Lureau5032a162016-09-12 13:19:02 +0400990#endif
991#ifndef TARGET_ARM
Markus Armbruster1527bad2017-03-03 13:32:25 +0100992 qmp_unregister_command(&qmp_commands, "query-gic-capabilities");
Marc-André Lureau5032a162016-09-12 13:19:02 +0400993#endif
Eduardo Habkostf99fd7c2017-02-22 16:00:28 -0300994#if !defined(TARGET_S390X) && !defined(TARGET_I386)
Markus Armbruster1527bad2017-03-03 13:32:25 +0100995 qmp_unregister_command(&qmp_commands, "query-cpu-model-expansion");
Eduardo Habkostf99fd7c2017-02-22 16:00:28 -0300996#endif
997#if !defined(TARGET_S390X)
Markus Armbruster1527bad2017-03-03 13:32:25 +0100998 qmp_unregister_command(&qmp_commands, "query-cpu-model-baseline");
999 qmp_unregister_command(&qmp_commands, "query-cpu-model-comparison");
Eduardo Habkost728b1422016-10-05 16:49:01 -03001000#endif
1001#if !defined(TARGET_PPC) && !defined(TARGET_ARM) && !defined(TARGET_I386) \
1002 && !defined(TARGET_S390X)
Markus Armbruster1527bad2017-03-03 13:32:25 +01001003 qmp_unregister_command(&qmp_commands, "query-cpu-definitions");
Eduardo Habkost728b1422016-10-05 16:49:01 -03001004#endif
Marc-André Lureau5032a162016-09-12 13:19:02 +04001005}
1006
Markus Armbruster05875682017-03-03 13:32:24 +01001007void monitor_init_qmp_commands(void)
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001008{
Markus Armbruster635db182017-03-03 13:32:27 +01001009 /*
1010 * Two command lists:
1011 * - qmp_commands contains all QMP commands
1012 * - qmp_cap_negotiation_commands contains just
1013 * "qmp_capabilities", to enforce capability negotiation
1014 */
1015
Markus Armbruster1527bad2017-03-03 13:32:25 +01001016 qmp_init_marshal(&qmp_commands);
Markus Armbruster05875682017-03-03 13:32:24 +01001017
Markus Armbruster1527bad2017-03-03 13:32:25 +01001018 qmp_register_command(&qmp_commands, "query-qmp-schema",
1019 qmp_query_qmp_schema,
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001020 QCO_NO_OPTIONS);
Markus Armbruster1527bad2017-03-03 13:32:25 +01001021 qmp_register_command(&qmp_commands, "device_add", qmp_device_add,
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001022 QCO_NO_OPTIONS);
Markus Armbruster1527bad2017-03-03 13:32:25 +01001023 qmp_register_command(&qmp_commands, "netdev_add", qmp_netdev_add,
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001024 QCO_NO_OPTIONS);
Marc-André Lureau5032a162016-09-12 13:19:02 +04001025
Markus Armbruster05875682017-03-03 13:32:24 +01001026 qmp_unregister_commands_hack();
Markus Armbruster635db182017-03-03 13:32:27 +01001027
1028 QTAILQ_INIT(&qmp_cap_negotiation_commands);
1029 qmp_register_command(&qmp_cap_negotiation_commands, "qmp_capabilities",
1030 qmp_marshal_qmp_capabilities, QCO_NO_OPTIONS);
1031}
1032
1033void qmp_qmp_capabilities(Error **errp)
1034{
1035 if (cur_mon->qmp.commands == &qmp_commands) {
1036 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
1037 "Capabilities negotiation is already complete, command "
1038 "ignored");
1039 return;
1040 }
1041
1042 cur_mon->qmp.commands = &qmp_commands;
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001043}
1044
Luiz Capitulinob025c8b2011-10-06 14:02:57 -03001045/* set the current CPU defined by the user */
1046int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +00001047{
Andreas Färber55e5c282012-12-17 06:18:02 +01001048 CPUState *cpu;
bellard6a00d602005-11-21 23:25:50 +00001049
Andreas Färber1c8bb3c2013-02-15 17:01:09 +01001050 cpu = qemu_get_cpu(cpu_index);
1051 if (cpu == NULL) {
1052 return -1;
bellard6a00d602005-11-21 23:25:50 +00001053 }
Greg Kurz751f8cf2017-10-17 10:16:22 +02001054 g_free(cur_mon->mon_cpu_path);
1055 cur_mon->mon_cpu_path = object_get_canonical_path(OBJECT(cpu));
Andreas Färber1c8bb3c2013-02-15 17:01:09 +01001056 return 0;
bellard6a00d602005-11-21 23:25:50 +00001057}
1058
Pavel Butsykincaf15312015-09-22 16:18:17 +03001059CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +00001060{
Greg Kurz751f8cf2017-10-17 10:16:22 +02001061 CPUState *cpu;
1062
1063 if (cur_mon->mon_cpu_path) {
1064 cpu = (CPUState *) object_resolve_path_type(cur_mon->mon_cpu_path,
1065 TYPE_CPU, NULL);
1066 if (!cpu) {
1067 g_free(cur_mon->mon_cpu_path);
1068 cur_mon->mon_cpu_path = NULL;
1069 }
1070 }
1071 if (!cur_mon->mon_cpu_path) {
Thomas Huth854e67f2017-01-13 13:12:35 +01001072 if (!first_cpu) {
1073 return NULL;
1074 }
David Gibson27a83f82016-09-21 15:29:26 +10001075 monitor_set_cpu(first_cpu->cpu_index);
Greg Kurz751f8cf2017-10-17 10:16:22 +02001076 cpu = first_cpu;
bellard6a00d602005-11-21 23:25:50 +00001077 }
Greg Kurz751f8cf2017-10-17 10:16:22 +02001078 cpu_synchronize_state(cpu);
1079 return cpu;
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001080}
1081
Pavel Butsykinbf957282015-09-10 18:38:59 +03001082CPUArchState *mon_get_cpu_env(void)
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001083{
Thomas Huth854e67f2017-01-13 13:12:35 +01001084 CPUState *cs = mon_get_cpu();
1085
1086 return cs ? cs->env_ptr : NULL;
bellard6a00d602005-11-21 23:25:50 +00001087}
1088
Luiz Capitulino99b77962011-10-24 10:53:44 -02001089int monitor_get_cpu_index(void)
1090{
Thomas Huth854e67f2017-01-13 13:12:35 +01001091 CPUState *cs = mon_get_cpu();
1092
1093 return cs ? cs->cpu_index : UNASSIGNED_CPU_INDEX;
Luiz Capitulino99b77962011-10-24 10:53:44 -02001094}
1095
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001096static void hmp_info_registers(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001097{
Suraj Jitindar Singh18f08282017-06-08 15:41:16 +10001098 bool all_cpus = qdict_get_try_bool(qdict, "cpustate_all", false);
1099 CPUState *cs;
Thomas Huth854e67f2017-01-13 13:12:35 +01001100
Suraj Jitindar Singh18f08282017-06-08 15:41:16 +10001101 if (all_cpus) {
1102 CPU_FOREACH(cs) {
1103 monitor_printf(mon, "\nCPU#%d\n", cs->cpu_index);
1104 cpu_dump_state(cs, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
1105 }
1106 } else {
1107 cs = mon_get_cpu();
1108
1109 if (!cs) {
1110 monitor_printf(mon, "No CPU available\n");
1111 return;
1112 }
1113
1114 cpu_dump_state(cs, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
Thomas Huth854e67f2017-01-13 13:12:35 +01001115 }
bellard9307c4c2004-04-04 12:57:25 +00001116}
1117
Paolo Bonzinif0d14a92012-09-17 13:42:41 +02001118#ifdef CONFIG_TCG
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001119static void hmp_info_jit(Monitor *mon, const QDict *qdict)
bellarde3db7222005-01-26 22:00:47 +00001120{
Thomas Huthb7da97e2017-04-26 06:11:47 +02001121 if (!tcg_enabled()) {
1122 error_report("JIT information is only available with accel=tcg");
1123 return;
1124 }
1125
aliguori376253e2009-03-05 23:01:23 +00001126 dump_exec_info((FILE *)mon, monitor_fprintf);
Sebastian Tanase27498be2014-07-25 11:56:33 +02001127 dump_drift_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +00001128}
1129
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001130static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
Max Filippov246ae242014-11-02 11:04:18 +03001131{
1132 dump_opcount_info((FILE *)mon, monitor_fprintf);
1133}
Paolo Bonzinif0d14a92012-09-17 13:42:41 +02001134#endif
Max Filippov246ae242014-11-02 11:04:18 +03001135
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001136static void hmp_info_history(Monitor *mon, const QDict *qdict)
bellardaa455482004-04-04 13:07:25 +00001137{
1138 int i;
bellard7e2515e2004-08-01 21:52:19 +00001139 const char *str;
ths3b46e622007-09-17 08:09:54 +00001140
aliguoricde76ee2009-03-05 23:01:51 +00001141 if (!mon->rs)
1142 return;
bellard7e2515e2004-08-01 21:52:19 +00001143 i = 0;
1144 for(;;) {
aliguori731b0362009-03-05 23:01:42 +00001145 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +00001146 if (!str)
1147 break;
aliguori376253e2009-03-05 23:01:23 +00001148 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +00001149 i++;
bellardaa455482004-04-04 13:07:25 +00001150 }
1151}
1152
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001153static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
j_mayer76a66252007-03-07 08:32:30 +00001154{
Thomas Huth854e67f2017-01-13 13:12:35 +01001155 CPUState *cs = mon_get_cpu();
1156
1157 if (!cs) {
1158 monitor_printf(mon, "No CPU available\n");
1159 return;
1160 }
1161 cpu_dump_statistics(cs, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +00001162}
j_mayer76a66252007-03-07 08:32:30 +00001163
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001164static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301165{
Lluís Vilanovabd712112016-07-11 12:53:51 +02001166 const char *name = qdict_get_try_str(qdict, "name");
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001167 bool has_vcpu = qdict_haskey(qdict, "vcpu");
1168 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
Lluís Vilanovabd712112016-07-11 12:53:51 +02001169 TraceEventInfoList *events;
Lluís Vilanova14101d02014-08-25 13:20:03 +02001170 TraceEventInfoList *elem;
Lluís Vilanovabd712112016-07-11 12:53:51 +02001171 Error *local_err = NULL;
1172
1173 if (name == NULL) {
1174 name = "*";
1175 }
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001176 if (vcpu < 0) {
1177 monitor_printf(mon, "argument vcpu must be positive");
1178 return;
1179 }
Lluís Vilanovabd712112016-07-11 12:53:51 +02001180
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001181 events = qmp_trace_event_get_state(name, has_vcpu, vcpu, &local_err);
Lluís Vilanovabd712112016-07-11 12:53:51 +02001182 if (local_err) {
1183 error_report_err(local_err);
1184 return;
1185 }
Lluís Vilanova14101d02014-08-25 13:20:03 +02001186
1187 for (elem = events; elem != NULL; elem = elem->next) {
1188 monitor_printf(mon, "%s : state %u\n",
1189 elem->value->name,
1190 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
1191 }
1192 qapi_free_TraceEventInfoList(events);
Prerna Saxena22890ab2010-06-24 17:04:53 +05301193}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301194
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001195void qmp_client_migrate_info(const char *protocol, const char *hostname,
1196 bool has_port, int64_t port,
1197 bool has_tls_port, int64_t tls_port,
1198 bool has_cert_subject, const char *cert_subject,
1199 Error **errp)
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001200{
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001201 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001202 if (!qemu_using_spice(errp)) {
1203 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001204 }
1205
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001206 if (!has_port && !has_tls_port) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001207 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001208 return;
Yonit Halperin6ec5dae2012-03-18 09:42:39 +02001209 }
1210
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001211 if (qemu_spice_migrate_info(hostname,
1212 has_port ? port : -1,
1213 has_tls_port ? tls_port : -1,
1214 cert_subject)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001215 error_setg(errp, QERR_UNDEFINED_ERROR);
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001216 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001217 }
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001218 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001219 }
1220
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001221 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001222}
1223
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001224static void hmp_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001225{
Markus Armbrusterdaa76aa2016-06-15 19:27:16 +02001226 Error *err = NULL;
1227
1228 qemu_set_log_filename(qdict_get_str(qdict, "filename"), &err);
1229 if (err) {
1230 error_report_err(err);
1231 }
pbrooke735b912007-06-30 13:53:24 +00001232}
1233
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001234static void hmp_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001235{
1236 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001237 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001238
bellard9307c4c2004-04-04 12:57:25 +00001239 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001240 mask = 0;
1241 } else {
Peter Maydell4fde1eb2013-02-11 16:41:22 +00001242 mask = qemu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001243 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001244 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001245 return;
1246 }
1247 }
Peter Maydell24537a02013-02-11 16:41:23 +00001248 qemu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00001249}
1250
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001251static void hmp_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001252{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001253 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001254 if (!option || !strcmp(option, "on")) {
1255 singlestep = 1;
1256 } else if (!strcmp(option, "off")) {
1257 singlestep = 0;
1258 } else {
1259 monitor_printf(mon, "unexpected option %s\n", option);
1260 }
1261}
1262
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001263static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001264{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001265 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001266 if (!device)
1267 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1268 if (gdbserver_start(device) < 0) {
1269 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1270 device);
1271 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001272 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001273 } else {
aliguori59030a82009-04-05 18:43:41 +00001274 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1275 device);
bellard8a7ddc32004-03-31 19:00:16 +00001276 }
1277}
1278
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001279static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001280{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001281 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001282 if (select_watchdog_action(action) == -1) {
1283 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1284 }
1285}
1286
aliguori376253e2009-03-05 23:01:23 +00001287static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001288{
aliguori376253e2009-03-05 23:01:23 +00001289 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001290 switch(c) {
1291 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001292 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001293 break;
1294 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001295 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001296 break;
1297 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001298 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001299 break;
1300 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001301 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001302 break;
1303 default:
1304 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001305 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001306 } else {
aliguori376253e2009-03-05 23:01:23 +00001307 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001308 }
1309 break;
1310 }
aliguori376253e2009-03-05 23:01:23 +00001311 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001312}
1313
aliguori376253e2009-03-05 23:01:23 +00001314static void memory_dump(Monitor *mon, int count, int format, int wsize,
Avi Kivitya8170e52012-10-23 12:30:10 +02001315 hwaddr addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001316{
Blue Swirl23842aa2010-01-12 20:27:43 +00001317 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001318 uint8_t buf[16];
1319 uint64_t v;
Thomas Huth854e67f2017-01-13 13:12:35 +01001320 CPUState *cs = mon_get_cpu();
1321
1322 if (!cs && (format == 'i' || !is_physical)) {
1323 monitor_printf(mon, "Can not dump without CPU\n");
1324 return;
1325 }
bellard9307c4c2004-04-04 12:57:25 +00001326
1327 if (format == 'i') {
Richard Henderson1d484742017-09-14 08:38:35 -07001328 monitor_disas(mon, cs, addr, count, is_physical);
bellard9307c4c2004-04-04 12:57:25 +00001329 return;
1330 }
1331
1332 len = wsize * count;
1333 if (wsize == 1)
1334 line_size = 8;
1335 else
1336 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001337 max_digits = 0;
1338
1339 switch(format) {
1340 case 'o':
Marc-André Lureau69db8df2017-06-22 13:04:16 +02001341 max_digits = DIV_ROUND_UP(wsize * 8, 3);
bellard9307c4c2004-04-04 12:57:25 +00001342 break;
1343 default:
1344 case 'x':
1345 max_digits = (wsize * 8) / 4;
1346 break;
1347 case 'u':
1348 case 'd':
Marc-André Lureau69db8df2017-06-22 13:04:16 +02001349 max_digits = DIV_ROUND_UP(wsize * 8 * 10, 33);
bellard9307c4c2004-04-04 12:57:25 +00001350 break;
1351 case 'c':
1352 wsize = 1;
1353 break;
1354 }
1355
1356 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001357 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001358 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001359 else
aliguori376253e2009-03-05 23:01:23 +00001360 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001361 l = len;
1362 if (l > line_size)
1363 l = line_size;
1364 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001365 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001366 } else {
Thomas Huth854e67f2017-01-13 13:12:35 +01001367 if (cpu_memory_rw_debug(cs, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001368 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001369 break;
1370 }
bellard9307c4c2004-04-04 12:57:25 +00001371 }
ths5fafdf22007-09-16 21:08:06 +00001372 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001373 while (i < l) {
1374 switch(wsize) {
1375 default:
1376 case 1:
Peter Maydell24e60302015-01-20 15:19:32 +00001377 v = ldub_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001378 break;
1379 case 2:
Peter Maydell24e60302015-01-20 15:19:32 +00001380 v = lduw_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001381 break;
1382 case 4:
Peter Maydell24e60302015-01-20 15:19:32 +00001383 v = (uint32_t)ldl_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001384 break;
1385 case 8:
Peter Maydell24e60302015-01-20 15:19:32 +00001386 v = ldq_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001387 break;
1388 }
aliguori376253e2009-03-05 23:01:23 +00001389 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001390 switch(format) {
1391 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001392 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001393 break;
1394 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001395 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001396 break;
1397 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001398 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001399 break;
1400 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001401 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001402 break;
1403 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001404 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001405 break;
1406 }
1407 i += wsize;
1408 }
aliguori376253e2009-03-05 23:01:23 +00001409 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001410 addr += l;
1411 len -= l;
1412 }
1413}
1414
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001415static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001416{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001417 int count = qdict_get_int(qdict, "count");
1418 int format = qdict_get_int(qdict, "format");
1419 int size = qdict_get_int(qdict, "size");
1420 target_long addr = qdict_get_int(qdict, "addr");
1421
aliguori376253e2009-03-05 23:01:23 +00001422 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001423}
1424
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001425static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001426{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001427 int count = qdict_get_int(qdict, "count");
1428 int format = qdict_get_int(qdict, "format");
1429 int size = qdict_get_int(qdict, "size");
Avi Kivitya8170e52012-10-23 12:30:10 +02001430 hwaddr addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001431
aliguori376253e2009-03-05 23:01:23 +00001432 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001433}
1434
Paolo Bonzinie9628442017-04-20 15:30:58 +02001435static void *gpa2hva(MemoryRegion **p_mr, hwaddr addr, Error **errp)
1436{
1437 MemoryRegionSection mrs = memory_region_find(get_system_memory(),
1438 addr, 1);
1439
1440 if (!mrs.mr) {
1441 error_setg(errp, "No memory is mapped at address 0x%" HWADDR_PRIx, addr);
1442 return NULL;
1443 }
1444
1445 if (!memory_region_is_ram(mrs.mr) && !memory_region_is_romd(mrs.mr)) {
1446 error_setg(errp, "Memory at address 0x%" HWADDR_PRIx "is not RAM", addr);
1447 memory_region_unref(mrs.mr);
1448 return NULL;
1449 }
1450
1451 *p_mr = mrs.mr;
1452 return qemu_map_ram_ptr(mrs.mr->ram_block, mrs.offset_within_region);
1453}
1454
1455static void hmp_gpa2hva(Monitor *mon, const QDict *qdict)
1456{
1457 hwaddr addr = qdict_get_int(qdict, "addr");
1458 Error *local_err = NULL;
1459 MemoryRegion *mr = NULL;
1460 void *ptr;
1461
1462 ptr = gpa2hva(&mr, addr, &local_err);
1463 if (local_err) {
1464 error_report_err(local_err);
1465 return;
1466 }
1467
1468 monitor_printf(mon, "Host virtual address for 0x%" HWADDR_PRIx
1469 " (%s) is %p\n",
1470 addr, mr->name, ptr);
1471
1472 memory_region_unref(mr);
1473}
1474
1475#ifdef CONFIG_LINUX
1476static uint64_t vtop(void *ptr, Error **errp)
1477{
1478 uint64_t pinfo;
1479 uint64_t ret = -1;
1480 uintptr_t addr = (uintptr_t) ptr;
1481 uintptr_t pagesize = getpagesize();
1482 off_t offset = addr / pagesize * sizeof(pinfo);
1483 int fd;
1484
1485 fd = open("/proc/self/pagemap", O_RDONLY);
1486 if (fd == -1) {
1487 error_setg_errno(errp, errno, "Cannot open /proc/self/pagemap");
1488 return -1;
1489 }
1490
1491 /* Force copy-on-write if necessary. */
1492 atomic_add((uint8_t *)ptr, 0);
1493
1494 if (pread(fd, &pinfo, sizeof(pinfo), offset) != sizeof(pinfo)) {
1495 error_setg_errno(errp, errno, "Cannot read pagemap");
1496 goto out;
1497 }
1498 if ((pinfo & (1ull << 63)) == 0) {
1499 error_setg(errp, "Page not present");
1500 goto out;
1501 }
1502 ret = ((pinfo & 0x007fffffffffffffull) * pagesize) | (addr & (pagesize - 1));
1503
1504out:
1505 close(fd);
1506 return ret;
1507}
1508
1509static void hmp_gpa2hpa(Monitor *mon, const QDict *qdict)
1510{
1511 hwaddr addr = qdict_get_int(qdict, "addr");
1512 Error *local_err = NULL;
1513 MemoryRegion *mr = NULL;
1514 void *ptr;
1515 uint64_t physaddr;
1516
1517 ptr = gpa2hva(&mr, addr, &local_err);
1518 if (local_err) {
1519 error_report_err(local_err);
1520 return;
1521 }
1522
1523 physaddr = vtop(ptr, &local_err);
1524 if (local_err) {
1525 error_report_err(local_err);
1526 } else {
1527 monitor_printf(mon, "Host physical address for 0x%" HWADDR_PRIx
1528 " (%s) is 0x%" PRIx64 "\n",
1529 addr, mr->name, (uint64_t) physaddr);
1530 }
1531
1532 memory_region_unref(mr);
1533}
1534#endif
1535
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001536static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001537{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001538 int format = qdict_get_int(qdict, "format");
Avi Kivitya8170e52012-10-23 12:30:10 +02001539 hwaddr val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001540
bellard9307c4c2004-04-04 12:57:25 +00001541 switch(format) {
1542 case 'o':
Avi Kivitya8170e52012-10-23 12:30:10 +02001543 monitor_printf(mon, "%#" HWADDR_PRIo, val);
bellard9307c4c2004-04-04 12:57:25 +00001544 break;
1545 case 'x':
Avi Kivitya8170e52012-10-23 12:30:10 +02001546 monitor_printf(mon, "%#" HWADDR_PRIx, val);
bellard9307c4c2004-04-04 12:57:25 +00001547 break;
1548 case 'u':
Avi Kivitya8170e52012-10-23 12:30:10 +02001549 monitor_printf(mon, "%" HWADDR_PRIu, val);
bellard9307c4c2004-04-04 12:57:25 +00001550 break;
1551 default:
1552 case 'd':
Avi Kivitya8170e52012-10-23 12:30:10 +02001553 monitor_printf(mon, "%" HWADDR_PRId, val);
bellard9307c4c2004-04-04 12:57:25 +00001554 break;
1555 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001556 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001557 break;
1558 }
aliguori376253e2009-03-05 23:01:23 +00001559 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001560}
1561
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001562static void hmp_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001563{
1564 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001565 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001566 uint32_t start = qdict_get_int(qdict, "start");
1567 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001568
1569 sum = 0;
1570 for(addr = start; addr < (start + size); addr++) {
Peter Maydell42874d32015-04-26 16:49:24 +01001571 uint8_t val = address_space_ldub(&address_space_memory, addr,
1572 MEMTXATTRS_UNSPECIFIED, NULL);
bellarde4cf1ad2005-06-04 20:15:57 +00001573 /* BSD sum algorithm ('sum' Unix command) */
1574 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001575 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001576 }
aliguori376253e2009-03-05 23:01:23 +00001577 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001578}
1579
bellard13224a82006-07-14 22:03:35 +00001580static int mouse_button_state;
1581
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001582static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001583{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001584 int dx, dy, dz, button;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001585 const char *dx_str = qdict_get_str(qdict, "dx_str");
1586 const char *dy_str = qdict_get_str(qdict, "dy_str");
1587 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001588
bellard13224a82006-07-14 22:03:35 +00001589 dx = strtol(dx_str, NULL, 0);
1590 dy = strtol(dy_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001591 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
1592 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
1593
1594 if (dz_str) {
bellard13224a82006-07-14 22:03:35 +00001595 dz = strtol(dz_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001596 if (dz != 0) {
Gerd Hoffmannf22d0af2016-01-12 12:14:12 +01001597 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001598 qemu_input_queue_btn(NULL, button, true);
1599 qemu_input_event_sync();
1600 qemu_input_queue_btn(NULL, button, false);
1601 }
1602 }
1603 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001604}
1605
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001606static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001607{
Eric Blake7fb1cf12015-11-18 01:52:57 -07001608 static uint32_t bmap[INPUT_BUTTON__MAX] = {
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001609 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1610 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
1611 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1612 };
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001613 int button_state = qdict_get_int(qdict, "button_state");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001614
1615 if (mouse_button_state == button_state) {
1616 return;
1617 }
1618 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1619 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001620 mouse_button_state = button_state;
bellard13224a82006-07-14 22:03:35 +00001621}
1622
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001623static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001624{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001625 int size = qdict_get_int(qdict, "size");
1626 int addr = qdict_get_int(qdict, "addr");
1627 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001628 uint32_t val;
1629 int suffix;
1630
1631 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001632 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001633 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001634 addr++;
1635 }
1636 addr &= 0xffff;
1637
1638 switch(size) {
1639 default:
1640 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001641 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001642 suffix = 'b';
1643 break;
1644 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001645 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001646 suffix = 'w';
1647 break;
1648 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001649 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001650 suffix = 'l';
1651 break;
1652 }
aliguori376253e2009-03-05 23:01:23 +00001653 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1654 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001655}
bellarda3a91a32004-06-04 11:06:21 +00001656
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001657static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001658{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001659 int size = qdict_get_int(qdict, "size");
1660 int addr = qdict_get_int(qdict, "addr");
1661 int val = qdict_get_int(qdict, "val");
1662
Jan Kiszkaf1147842009-07-14 10:20:11 +02001663 addr &= IOPORTS_MASK;
1664
1665 switch (size) {
1666 default:
1667 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001668 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001669 break;
1670 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001671 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001672 break;
1673 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001674 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001675 break;
1676 }
1677}
1678
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001679static void hmp_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001680{
Gongleif1839932014-12-03 18:20:58 +00001681 Error *local_err = NULL;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001682 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001683
Gongleif1839932014-12-03 18:20:58 +00001684 qemu_boot_set(bootdevice, &local_err);
1685 if (local_err) {
Markus Armbruster193227f2015-12-18 16:35:06 +01001686 error_report_err(local_err);
aurel320ecdffb2008-05-04 20:11:34 +00001687 } else {
Gongleif1839932014-12-03 18:20:58 +00001688 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001689 }
1690}
1691
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001692static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
Blue Swirl314e2982011-09-11 20:22:05 +00001693{
Peter Xu57bb40c2017-01-16 16:40:05 +08001694 bool flatview = qdict_get_try_bool(qdict, "flatview", false);
Alexey Kardashevskiy5e8fd942017-09-21 18:51:06 +10001695 bool dispatch_tree = qdict_get_try_bool(qdict, "dispatch_tree", false);
Peter Xu57bb40c2017-01-16 16:40:05 +08001696
Alexey Kardashevskiy5e8fd942017-09-21 18:51:06 +10001697 mtree_info((fprintf_function)monitor_printf, mon, flatview, dispatch_tree);
Blue Swirl314e2982011-09-11 20:22:05 +00001698}
1699
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001700static void hmp_info_numa(Monitor *mon, const QDict *qdict)
aliguori030ea372009-04-21 22:30:47 +00001701{
aliguorib28b6232009-04-22 20:20:29 +00001702 int i;
Vadim Galitsyn31959e82017-08-29 17:30:20 +02001703 NumaNodeMem *node_mem;
Igor Mammedovf75cd442017-05-30 18:23:59 +02001704 CpuInfoList *cpu_list, *cpu;
aliguori030ea372009-04-21 22:30:47 +00001705
Igor Mammedovf75cd442017-05-30 18:23:59 +02001706 cpu_list = qmp_query_cpus(&error_abort);
Vadim Galitsyn31959e82017-08-29 17:30:20 +02001707 node_mem = g_new0(NumaNodeMem, nb_numa_nodes);
1708
zhanghailiang5b009e42014-11-04 19:49:30 +08001709 query_numa_node_mem(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001710 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1711 for (i = 0; i < nb_numa_nodes; i++) {
1712 monitor_printf(mon, "node %d cpus:", i);
Igor Mammedovf75cd442017-05-30 18:23:59 +02001713 for (cpu = cpu_list; cpu; cpu = cpu->next) {
1714 if (cpu->value->has_props && cpu->value->props->has_node_id &&
1715 cpu->value->props->node_id == i) {
1716 monitor_printf(mon, " %" PRIi64, cpu->value->CPU);
aliguori030ea372009-04-21 22:30:47 +00001717 }
1718 }
1719 monitor_printf(mon, "\n");
1720 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
Vadim Galitsyn31959e82017-08-29 17:30:20 +02001721 node_mem[i].node_mem >> 20);
1722 monitor_printf(mon, "node %d plugged: %" PRId64 " MB\n", i,
1723 node_mem[i].node_plugged_mem >> 20);
aliguori030ea372009-04-21 22:30:47 +00001724 }
Igor Mammedovf75cd442017-05-30 18:23:59 +02001725 qapi_free_CpuInfoList(cpu_list);
zhanghailiang5b009e42014-11-04 19:49:30 +08001726 g_free(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001727}
1728
bellard5f1ce942006-02-08 22:40:15 +00001729#ifdef CONFIG_PROFILER
1730
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001731int64_t tcg_time;
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001732int64_t dev_time;
1733
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001734static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001735{
aliguori376253e2009-03-05 23:01:23 +00001736 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Rutuja Shah73bcb242016-03-21 21:32:30 +05301737 dev_time, dev_time / (double)NANOSECONDS_PER_SECOND);
aliguori376253e2009-03-05 23:01:23 +00001738 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Rutuja Shah73bcb242016-03-21 21:32:30 +05301739 tcg_time, tcg_time / (double)NANOSECONDS_PER_SECOND);
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001740 tcg_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001741 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001742}
1743#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001744static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001745{
aliguori376253e2009-03-05 23:01:23 +00001746 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001747}
1748#endif
1749
bellardec36b692006-07-16 18:57:03 +00001750/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001751static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001752
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001753static void hmp_info_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001754{
1755 int i;
1756 CaptureState *s;
1757
1758 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001759 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001760 s->ops.info (s->opaque);
1761 }
1762}
1763
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001764static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001765{
1766 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001767 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001768 CaptureState *s;
1769
1770 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1771 if (i == n) {
1772 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001773 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001774 g_free (s);
bellardec36b692006-07-16 18:57:03 +00001775 return;
1776 }
1777 }
1778}
1779
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001780static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001781{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001782 const char *path = qdict_get_str(qdict, "path");
1783 int has_freq = qdict_haskey(qdict, "freq");
1784 int freq = qdict_get_try_int(qdict, "freq", -1);
1785 int has_bits = qdict_haskey(qdict, "bits");
1786 int bits = qdict_get_try_int(qdict, "bits", -1);
1787 int has_channels = qdict_haskey(qdict, "nchannels");
1788 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001789 CaptureState *s;
1790
Anthony Liguori7267c092011-08-20 22:09:37 -05001791 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001792
1793 freq = has_freq ? freq : 44100;
1794 bits = has_bits ? bits : 16;
1795 nchannels = has_channels ? nchannels : 2;
1796
1797 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001798 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05001799 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001800 return;
bellardec36b692006-07-16 18:57:03 +00001801 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001802 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001803}
bellardec36b692006-07-16 18:57:03 +00001804
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001805static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001806{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001807 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001808
aliguori76655d62009-03-06 20:27:37 +00001809 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001810 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001811 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001812 return acl;
1813}
aliguori76655d62009-03-06 20:27:37 +00001814
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001815static void hmp_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001816{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001817 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001818 qemu_acl *acl = find_acl(mon, aclname);
1819 qemu_acl_entry *entry;
1820 int i = 0;
1821
1822 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001823 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001824 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00001825 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00001826 i++;
1827 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001828 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001829 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001830 }
1831}
1832
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001833static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001834{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001835 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001836 qemu_acl *acl = find_acl(mon, aclname);
1837
1838 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001839 qemu_acl_reset(acl);
1840 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001841 }
1842}
aliguori76655d62009-03-06 20:27:37 +00001843
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001844static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001845{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001846 const char *aclname = qdict_get_str(qdict, "aclname");
1847 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001848 qemu_acl *acl = find_acl(mon, aclname);
1849
1850 if (acl) {
1851 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001852 acl->defaultDeny = 0;
1853 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001854 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001855 acl->defaultDeny = 1;
1856 monitor_printf(mon, "acl: policy set to 'deny'\n");
1857 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001858 monitor_printf(mon, "acl: unknown policy '%s', "
1859 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001860 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001861 }
1862}
aliguori76655d62009-03-06 20:27:37 +00001863
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001864static void hmp_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001865{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001866 const char *aclname = qdict_get_str(qdict, "aclname");
1867 const char *match = qdict_get_str(qdict, "match");
1868 const char *policy = qdict_get_str(qdict, "policy");
1869 int has_index = qdict_haskey(qdict, "index");
1870 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001871 qemu_acl *acl = find_acl(mon, aclname);
1872 int deny, ret;
1873
1874 if (acl) {
1875 if (strcmp(policy, "allow") == 0) {
1876 deny = 0;
1877 } else if (strcmp(policy, "deny") == 0) {
1878 deny = 1;
1879 } else {
1880 monitor_printf(mon, "acl: unknown policy '%s', "
1881 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001882 return;
1883 }
aliguori28a76be2009-03-06 20:27:40 +00001884 if (has_index)
1885 ret = qemu_acl_insert(acl, deny, match, index);
1886 else
1887 ret = qemu_acl_append(acl, deny, match);
1888 if (ret < 0)
1889 monitor_printf(mon, "acl: unable to add acl entry\n");
1890 else
1891 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001892 }
1893}
aliguori76655d62009-03-06 20:27:37 +00001894
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001895static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001896{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001897 const char *aclname = qdict_get_str(qdict, "aclname");
1898 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001899 qemu_acl *acl = find_acl(mon, aclname);
1900 int ret;
aliguori76655d62009-03-06 20:27:37 +00001901
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001902 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001903 ret = qemu_acl_remove(acl, match);
1904 if (ret < 0)
1905 monitor_printf(mon, "acl: no matching acl entry\n");
1906 else
1907 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001908 }
1909}
1910
Corey Bryant208c9d12012-06-22 14:36:09 -04001911void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001912{
Anthony Liguoric227f092009-10-01 16:12:16 -05001913 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001914 int fd;
1915
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03001916 fd = qemu_chr_fe_get_msgfd(&cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001917 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001918 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryant208c9d12012-06-22 14:36:09 -04001919 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001920 }
1921
1922 if (qemu_isdigit(fdname[0])) {
Stefan Hajnoczi0b9f0e22014-04-24 13:58:18 +02001923 close(fd);
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001924 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
1925 "a name not starting with a digit");
Corey Bryant208c9d12012-06-22 14:36:09 -04001926 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001927 }
1928
Corey Bryant208c9d12012-06-22 14:36:09 -04001929 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001930 if (strcmp(monfd->name, fdname) != 0) {
1931 continue;
1932 }
1933
1934 close(monfd->fd);
1935 monfd->fd = fd;
Corey Bryant208c9d12012-06-22 14:36:09 -04001936 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001937 }
1938
Anthony Liguori7267c092011-08-20 22:09:37 -05001939 monfd = g_malloc0(sizeof(mon_fd_t));
1940 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001941 monfd->fd = fd;
1942
Corey Bryant208c9d12012-06-22 14:36:09 -04001943 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001944}
1945
Corey Bryant208c9d12012-06-22 14:36:09 -04001946void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001947{
Anthony Liguoric227f092009-10-01 16:12:16 -05001948 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001949
Corey Bryant208c9d12012-06-22 14:36:09 -04001950 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001951 if (strcmp(monfd->name, fdname) != 0) {
1952 continue;
1953 }
1954
Blue Swirl72cf2d42009-09-12 07:36:22 +00001955 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001956 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001957 g_free(monfd->name);
1958 g_free(monfd);
Corey Bryant208c9d12012-06-22 14:36:09 -04001959 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001960 }
1961
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001962 error_setg(errp, QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001963}
1964
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001965int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
Mark McLoughlin7768e042009-07-22 09:11:41 +01001966{
Anthony Liguoric227f092009-10-01 16:12:16 -05001967 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01001968
Blue Swirl72cf2d42009-09-12 07:36:22 +00001969 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01001970 int fd;
1971
1972 if (strcmp(monfd->name, fdname) != 0) {
1973 continue;
1974 }
1975
1976 fd = monfd->fd;
1977
1978 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001979 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05001980 g_free(monfd->name);
1981 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001982
1983 return fd;
1984 }
1985
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001986 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001987 return -1;
1988}
1989
Corey Bryantba1c0482012-08-14 16:43:43 -04001990static void monitor_fdset_cleanup(MonFdset *mon_fdset)
1991{
1992 MonFdsetFd *mon_fdset_fd;
1993 MonFdsetFd *mon_fdset_fd_next;
1994
1995 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantebe52b52012-10-18 15:19:33 -04001996 if ((mon_fdset_fd->removed ||
1997 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
1998 runstate_is_running()) {
Corey Bryantba1c0482012-08-14 16:43:43 -04001999 close(mon_fdset_fd->fd);
2000 g_free(mon_fdset_fd->opaque);
2001 QLIST_REMOVE(mon_fdset_fd, next);
2002 g_free(mon_fdset_fd);
2003 }
2004 }
2005
Corey Bryantadb696f2012-08-14 16:43:47 -04002006 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002007 QLIST_REMOVE(mon_fdset, next);
2008 g_free(mon_fdset);
2009 }
2010}
2011
Corey Bryantefb87c12012-08-14 16:43:48 -04002012static void monitor_fdsets_cleanup(void)
2013{
2014 MonFdset *mon_fdset;
2015 MonFdset *mon_fdset_next;
2016
2017 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
2018 monitor_fdset_cleanup(mon_fdset);
2019 }
2020}
2021
Corey Bryantba1c0482012-08-14 16:43:43 -04002022AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
2023 const char *opaque, Error **errp)
2024{
2025 int fd;
2026 Monitor *mon = cur_mon;
Corey Bryantba1c0482012-08-14 16:43:43 -04002027 AddfdInfo *fdinfo;
2028
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03002029 fd = qemu_chr_fe_get_msgfd(&mon->chr);
Corey Bryantba1c0482012-08-14 16:43:43 -04002030 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002031 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryantba1c0482012-08-14 16:43:43 -04002032 goto error;
2033 }
2034
Corey Bryante446f702012-10-18 15:19:32 -04002035 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
2036 has_opaque, opaque, errp);
2037 if (fdinfo) {
2038 return fdinfo;
Corey Bryant9ac54af2012-10-18 15:19:31 -04002039 }
2040
Corey Bryantba1c0482012-08-14 16:43:43 -04002041error:
2042 if (fd != -1) {
2043 close(fd);
2044 }
2045 return NULL;
2046}
2047
2048void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
2049{
2050 MonFdset *mon_fdset;
2051 MonFdsetFd *mon_fdset_fd;
2052 char fd_str[60];
2053
2054 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2055 if (mon_fdset->id != fdset_id) {
2056 continue;
2057 }
2058 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2059 if (has_fd) {
2060 if (mon_fdset_fd->fd != fd) {
2061 continue;
2062 }
2063 mon_fdset_fd->removed = true;
2064 break;
2065 } else {
2066 mon_fdset_fd->removed = true;
2067 }
2068 }
2069 if (has_fd && !mon_fdset_fd) {
2070 goto error;
2071 }
2072 monitor_fdset_cleanup(mon_fdset);
2073 return;
2074 }
2075
2076error:
2077 if (has_fd) {
2078 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
2079 fdset_id, fd);
2080 } else {
2081 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
2082 }
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002083 error_setg(errp, QERR_FD_NOT_FOUND, fd_str);
Corey Bryantba1c0482012-08-14 16:43:43 -04002084}
2085
2086FdsetInfoList *qmp_query_fdsets(Error **errp)
2087{
2088 MonFdset *mon_fdset;
2089 MonFdsetFd *mon_fdset_fd;
2090 FdsetInfoList *fdset_list = NULL;
2091
2092 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2093 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
2094 FdsetFdInfoList *fdsetfd_list = NULL;
2095
2096 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
2097 fdset_info->value->fdset_id = mon_fdset->id;
2098
2099 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2100 FdsetFdInfoList *fdsetfd_info;
2101
2102 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
2103 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
2104 fdsetfd_info->value->fd = mon_fdset_fd->fd;
2105 if (mon_fdset_fd->opaque) {
2106 fdsetfd_info->value->has_opaque = true;
2107 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
2108 } else {
2109 fdsetfd_info->value->has_opaque = false;
2110 }
2111
2112 fdsetfd_info->next = fdsetfd_list;
2113 fdsetfd_list = fdsetfd_info;
2114 }
2115
2116 fdset_info->value->fds = fdsetfd_list;
2117
2118 fdset_info->next = fdset_list;
2119 fdset_list = fdset_info;
2120 }
2121
2122 return fdset_list;
2123}
2124
Corey Bryante446f702012-10-18 15:19:32 -04002125AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
2126 bool has_opaque, const char *opaque,
2127 Error **errp)
2128{
2129 MonFdset *mon_fdset = NULL;
2130 MonFdsetFd *mon_fdset_fd;
2131 AddfdInfo *fdinfo;
2132
2133 if (has_fdset_id) {
2134 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2135 /* Break if match found or match impossible due to ordering by ID */
2136 if (fdset_id <= mon_fdset->id) {
2137 if (fdset_id < mon_fdset->id) {
2138 mon_fdset = NULL;
2139 }
2140 break;
2141 }
2142 }
2143 }
2144
2145 if (mon_fdset == NULL) {
2146 int64_t fdset_id_prev = -1;
2147 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
2148
2149 if (has_fdset_id) {
2150 if (fdset_id < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002151 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
2152 "a non-negative value");
Corey Bryante446f702012-10-18 15:19:32 -04002153 return NULL;
2154 }
2155 /* Use specified fdset ID */
2156 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2157 mon_fdset_cur = mon_fdset;
2158 if (fdset_id < mon_fdset_cur->id) {
2159 break;
2160 }
2161 }
2162 } else {
2163 /* Use first available fdset ID */
2164 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2165 mon_fdset_cur = mon_fdset;
2166 if (fdset_id_prev == mon_fdset_cur->id - 1) {
2167 fdset_id_prev = mon_fdset_cur->id;
2168 continue;
2169 }
2170 break;
2171 }
2172 }
2173
2174 mon_fdset = g_malloc0(sizeof(*mon_fdset));
2175 if (has_fdset_id) {
2176 mon_fdset->id = fdset_id;
2177 } else {
2178 mon_fdset->id = fdset_id_prev + 1;
2179 }
2180
2181 /* The fdset list is ordered by fdset ID */
2182 if (!mon_fdset_cur) {
2183 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
2184 } else if (mon_fdset->id < mon_fdset_cur->id) {
2185 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
2186 } else {
2187 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
2188 }
2189 }
2190
2191 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
2192 mon_fdset_fd->fd = fd;
2193 mon_fdset_fd->removed = false;
2194 if (has_opaque) {
2195 mon_fdset_fd->opaque = g_strdup(opaque);
2196 }
2197 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
2198
2199 fdinfo = g_malloc0(sizeof(*fdinfo));
2200 fdinfo->fdset_id = mon_fdset->id;
2201 fdinfo->fd = mon_fdset_fd->fd;
2202
2203 return fdinfo;
2204}
2205
Corey Bryantadb696f2012-08-14 16:43:47 -04002206int monitor_fdset_get_fd(int64_t fdset_id, int flags)
2207{
Blue Swirlb2dc64c2012-08-18 20:14:54 +00002208#ifndef _WIN32
Corey Bryantadb696f2012-08-14 16:43:47 -04002209 MonFdset *mon_fdset;
2210 MonFdsetFd *mon_fdset_fd;
2211 int mon_fd_flags;
2212
Corey Bryantadb696f2012-08-14 16:43:47 -04002213 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2214 if (mon_fdset->id != fdset_id) {
2215 continue;
2216 }
2217 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2218 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
2219 if (mon_fd_flags == -1) {
2220 return -1;
2221 }
2222
2223 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
2224 return mon_fdset_fd->fd;
2225 }
2226 }
2227 errno = EACCES;
2228 return -1;
2229 }
2230#endif
2231
2232 errno = ENOENT;
2233 return -1;
2234}
2235
2236int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
2237{
2238 MonFdset *mon_fdset;
2239 MonFdsetFd *mon_fdset_fd_dup;
2240
2241 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2242 if (mon_fdset->id != fdset_id) {
2243 continue;
2244 }
2245 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2246 if (mon_fdset_fd_dup->fd == dup_fd) {
2247 return -1;
2248 }
2249 }
2250 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2251 mon_fdset_fd_dup->fd = dup_fd;
2252 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
2253 return 0;
2254 }
2255 return -1;
2256}
2257
2258static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2259{
2260 MonFdset *mon_fdset;
2261 MonFdsetFd *mon_fdset_fd_dup;
2262
2263 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2264 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2265 if (mon_fdset_fd_dup->fd == dup_fd) {
2266 if (remove) {
2267 QLIST_REMOVE(mon_fdset_fd_dup, next);
2268 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2269 monitor_fdset_cleanup(mon_fdset);
2270 }
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002271 return -1;
2272 } else {
2273 return mon_fdset->id;
Corey Bryantadb696f2012-08-14 16:43:47 -04002274 }
Corey Bryantadb696f2012-08-14 16:43:47 -04002275 }
2276 }
2277 }
2278 return -1;
2279}
2280
2281int monitor_fdset_dup_fd_find(int dup_fd)
2282{
2283 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2284}
2285
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002286void monitor_fdset_dup_fd_remove(int dup_fd)
Corey Bryantadb696f2012-08-14 16:43:47 -04002287{
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002288 monitor_fdset_dup_fd_find_remove(dup_fd, true);
Corey Bryantadb696f2012-08-14 16:43:47 -04002289}
2290
Markus Armbruster1677f4c2015-02-09 14:03:19 +01002291int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
Laszlo Ersek59063662014-04-10 10:24:31 +02002292{
2293 int fd;
2294 Error *local_err = NULL;
2295
2296 if (!qemu_isdigit(fdname[0]) && mon) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002297 fd = monitor_get_fd(mon, fdname, &local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002298 } else {
2299 fd = qemu_parse_fd(fdname);
Laszlo Ersek59063662014-04-10 10:24:31 +02002300 if (fd == -1) {
2301 error_setg(&local_err, "Invalid file descriptor number '%s'",
2302 fdname);
2303 }
2304 }
2305 if (local_err) {
2306 error_propagate(errp, local_err);
2307 assert(fd == -1);
2308 } else {
2309 assert(fd != -1);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002310 }
2311
2312 return fd;
2313}
2314
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002315/* Please update hmp-commands.hx when adding or changing commands */
Wayne Xia816f8922011-10-12 11:32:41 +08002316static mon_cmd_t info_cmds[] = {
Pavel Butsykinda76ee72015-09-10 18:38:58 +03002317#include "hmp-commands-info.h"
2318 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002319};
2320
Wenchao Xiaa13ced52013-01-14 14:06:28 +08002321/* mon_cmds and info_cmds would be sorted at runtime */
2322static mon_cmd_t mon_cmds[] = {
2323#include "hmp-commands.h"
2324 { NULL, NULL, },
2325};
2326
bellard9307c4c2004-04-04 12:57:25 +00002327/*******************************************************************/
2328
2329static const char *pch;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002330static sigjmp_buf expr_env;
bellard9307c4c2004-04-04 12:57:25 +00002331
bellard9307c4c2004-04-04 12:57:25 +00002332
Stefan Weil9c3175c2013-08-22 21:30:09 +02002333static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
2334expr_error(Monitor *mon, const char *fmt, ...)
bellard9dc39cb2004-03-14 21:38:27 +00002335{
Fam Zheng277acfe2013-08-20 10:58:21 +08002336 va_list ap;
2337 va_start(ap, fmt);
2338 monitor_vprintf(mon, fmt, ap);
2339 monitor_printf(mon, "\n");
2340 va_end(ap);
Peter Maydell6ab7e542013-02-20 15:21:09 +00002341 siglongjmp(expr_env, 1);
bellard9307c4c2004-04-04 12:57:25 +00002342}
2343
Markus Armbruster09b94182010-01-20 13:07:30 +01002344/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00002345static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002346{
Pavel Butsykinbf957282015-09-10 18:38:59 +03002347 const MonitorDef *md = target_monitor_defs();
Thomas Huth854e67f2017-01-13 13:12:35 +01002348 CPUState *cs = mon_get_cpu();
bellard92a31b12005-02-10 22:00:52 +00002349 void *ptr;
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002350 uint64_t tmp = 0;
2351 int ret;
bellard92a31b12005-02-10 22:00:52 +00002352
Thomas Huth854e67f2017-01-13 13:12:35 +01002353 if (cs == NULL || md == NULL) {
Pavel Butsykinbf957282015-09-10 18:38:59 +03002354 return -1;
2355 }
2356
2357 for(; md->name != NULL; md++) {
bellard9307c4c2004-04-04 12:57:25 +00002358 if (compare_cmd(name, md->name)) {
2359 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002360 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002361 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002362 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00002363 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002364 switch(md->type) {
2365 case MD_I32:
2366 *pval = *(int32_t *)ptr;
2367 break;
2368 case MD_TLONG:
2369 *pval = *(target_long *)ptr;
2370 break;
2371 default:
2372 *pval = 0;
2373 break;
2374 }
bellard9307c4c2004-04-04 12:57:25 +00002375 }
2376 return 0;
2377 }
2378 }
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002379
Thomas Huth854e67f2017-01-13 13:12:35 +01002380 ret = target_get_monitor_def(cs, name, &tmp);
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002381 if (!ret) {
2382 *pval = (target_long) tmp;
2383 }
2384
2385 return ret;
bellard9307c4c2004-04-04 12:57:25 +00002386}
2387
2388static void next(void)
2389{
Blue Swirl660f11b2009-07-31 21:16:51 +00002390 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002391 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002392 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002393 pch++;
2394 }
2395}
2396
aliguori376253e2009-03-05 23:01:23 +00002397static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002398
aliguori376253e2009-03-05 23:01:23 +00002399static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002400{
blueswir1c2efc952007-09-25 17:28:42 +00002401 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002402 char *p;
bellard6a00d602005-11-21 23:25:50 +00002403 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002404
2405 switch(*pch) {
2406 case '+':
2407 next();
aliguori376253e2009-03-05 23:01:23 +00002408 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002409 break;
2410 case '-':
2411 next();
aliguori376253e2009-03-05 23:01:23 +00002412 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002413 break;
2414 case '~':
2415 next();
aliguori376253e2009-03-05 23:01:23 +00002416 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002417 break;
2418 case '(':
2419 next();
aliguori376253e2009-03-05 23:01:23 +00002420 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002421 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002422 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002423 }
2424 next();
2425 break;
bellard81d09122004-07-14 17:21:37 +00002426 case '\'':
2427 pch++;
2428 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002429 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002430 n = *pch;
2431 pch++;
2432 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002433 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002434 next();
2435 break;
bellard9307c4c2004-04-04 12:57:25 +00002436 case '$':
2437 {
2438 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002439 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002440
bellard9307c4c2004-04-04 12:57:25 +00002441 pch++;
2442 q = buf;
2443 while ((*pch >= 'a' && *pch <= 'z') ||
2444 (*pch >= 'A' && *pch <= 'Z') ||
2445 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002446 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002447 if ((q - buf) < sizeof(buf) - 1)
2448 *q++ = *pch;
2449 pch++;
2450 }
blueswir1cd390082008-11-16 13:53:32 +00002451 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002452 pch++;
2453 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002454 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01002455 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00002456 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00002457 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002458 }
2459 break;
2460 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002461 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002462 n = 0;
2463 break;
2464 default:
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002465 errno = 0;
bellard4f4fbf72006-06-25 18:28:12 +00002466 n = strtoull(pch, &p, 0);
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002467 if (errno == ERANGE) {
2468 expr_error(mon, "number too large");
2469 }
bellard9307c4c2004-04-04 12:57:25 +00002470 if (pch == p) {
Fam Zheng277acfe2013-08-20 10:58:21 +08002471 expr_error(mon, "invalid char '%c' in expression", *p);
bellard9307c4c2004-04-04 12:57:25 +00002472 }
2473 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002474 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002475 pch++;
2476 break;
2477 }
2478 return n;
2479}
2480
2481
aliguori376253e2009-03-05 23:01:23 +00002482static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002483{
blueswir1c2efc952007-09-25 17:28:42 +00002484 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002485 int op;
ths3b46e622007-09-17 08:09:54 +00002486
aliguori376253e2009-03-05 23:01:23 +00002487 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002488 for(;;) {
2489 op = *pch;
2490 if (op != '*' && op != '/' && op != '%')
2491 break;
2492 next();
aliguori376253e2009-03-05 23:01:23 +00002493 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002494 switch(op) {
2495 default:
2496 case '*':
2497 val *= val2;
2498 break;
2499 case '/':
2500 case '%':
ths5fafdf22007-09-16 21:08:06 +00002501 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002502 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002503 if (op == '/')
2504 val /= val2;
2505 else
2506 val %= val2;
2507 break;
2508 }
2509 }
2510 return val;
2511}
2512
aliguori376253e2009-03-05 23:01:23 +00002513static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002514{
blueswir1c2efc952007-09-25 17:28:42 +00002515 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002516 int op;
bellard9307c4c2004-04-04 12:57:25 +00002517
aliguori376253e2009-03-05 23:01:23 +00002518 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002519 for(;;) {
2520 op = *pch;
2521 if (op != '&' && op != '|' && op != '^')
2522 break;
2523 next();
aliguori376253e2009-03-05 23:01:23 +00002524 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002525 switch(op) {
2526 default:
2527 case '&':
2528 val &= val2;
2529 break;
2530 case '|':
2531 val |= val2;
2532 break;
2533 case '^':
2534 val ^= val2;
2535 break;
2536 }
2537 }
2538 return val;
2539}
2540
aliguori376253e2009-03-05 23:01:23 +00002541static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002542{
blueswir1c2efc952007-09-25 17:28:42 +00002543 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002544 int op;
bellard9307c4c2004-04-04 12:57:25 +00002545
aliguori376253e2009-03-05 23:01:23 +00002546 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002547 for(;;) {
2548 op = *pch;
2549 if (op != '+' && op != '-')
2550 break;
2551 next();
aliguori376253e2009-03-05 23:01:23 +00002552 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002553 if (op == '+')
2554 val += val2;
2555 else
2556 val -= val2;
2557 }
2558 return val;
2559}
2560
aliguori376253e2009-03-05 23:01:23 +00002561static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002562{
2563 pch = *pp;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002564 if (sigsetjmp(expr_env, 0)) {
bellard9307c4c2004-04-04 12:57:25 +00002565 *pp = pch;
2566 return -1;
2567 }
blueswir1cd390082008-11-16 13:53:32 +00002568 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002569 pch++;
aliguori376253e2009-03-05 23:01:23 +00002570 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002571 *pp = pch;
2572 return 0;
2573}
2574
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002575static int get_double(Monitor *mon, double *pval, const char **pp)
2576{
2577 const char *p = *pp;
2578 char *tailp;
2579 double d;
2580
2581 d = strtod(p, &tailp);
2582 if (tailp == p) {
2583 monitor_printf(mon, "Number expected\n");
2584 return -1;
2585 }
2586 if (d != d || d - d != 0) {
2587 /* NaN or infinity */
2588 monitor_printf(mon, "Bad number\n");
2589 return -1;
2590 }
2591 *pval = d;
2592 *pp = tailp;
2593 return 0;
2594}
2595
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002596/*
2597 * Store the command-name in cmdname, and return a pointer to
2598 * the remaining of the command string.
2599 */
2600static const char *get_command_name(const char *cmdline,
2601 char *cmdname, size_t nlen)
2602{
2603 size_t len;
2604 const char *p, *pstart;
2605
2606 p = cmdline;
2607 while (qemu_isspace(*p))
2608 p++;
2609 if (*p == '\0')
2610 return NULL;
2611 pstart = p;
2612 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2613 p++;
2614 len = p - pstart;
2615 if (len > nlen - 1)
2616 len = nlen - 1;
2617 memcpy(cmdname, pstart, len);
2618 cmdname[len] = '\0';
2619 return p;
2620}
2621
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002622/**
2623 * Read key of 'type' into 'key' and return the current
2624 * 'type' pointer.
2625 */
2626static char *key_get_info(const char *type, char **key)
2627{
2628 size_t len;
2629 char *p, *str;
2630
2631 if (*type == ',')
2632 type++;
2633
2634 p = strchr(type, ':');
2635 if (!p) {
2636 *key = NULL;
2637 return NULL;
2638 }
2639 len = p - type;
2640
Anthony Liguori7267c092011-08-20 22:09:37 -05002641 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002642 memcpy(str, type, len);
2643 str[len] = '\0';
2644
2645 *key = str;
2646 return ++p;
2647}
2648
bellard9307c4c2004-04-04 12:57:25 +00002649static int default_fmt_format = 'x';
2650static int default_fmt_size = 4;
2651
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002652static int is_valid_option(const char *c, const char *typestr)
2653{
2654 char option[3];
2655
2656 option[0] = '-';
2657 option[1] = *c;
2658 option[2] = '\0';
2659
2660 typestr = strstr(typestr, option);
2661 return (typestr != NULL);
2662}
2663
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03002664static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
2665 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002666{
2667 const mon_cmd_t *cmd;
2668
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03002669 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002670 if (compare_cmd(cmdname, cmd->name)) {
2671 return cmd;
2672 }
2673 }
2674
2675 return NULL;
2676}
2677
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002678/*
Bandan Dasae502122015-06-03 18:38:08 -04002679 * Parse command name from @cmdp according to command table @table.
2680 * If blank, return NULL.
2681 * Else, if no valid command can be found, report to @mon, and return
2682 * NULL.
2683 * Else, change @cmdp to point right behind the name, and return its
2684 * command table entry.
2685 * Do not assume the return value points into @table! It doesn't when
2686 * the command is found in a sub-command table.
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002687 */
Anthony Liguoric227f092009-10-01 16:12:16 -05002688static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Dr. David Alan Gilbert250b8192017-08-17 11:42:16 +01002689 const char *cmdp_start,
Bandan Dasae502122015-06-03 18:38:08 -04002690 const char **cmdp,
2691 mon_cmd_t *table)
bellard9307c4c2004-04-04 12:57:25 +00002692{
Bandan Dasae502122015-06-03 18:38:08 -04002693 const char *p;
Anthony Liguoric227f092009-10-01 16:12:16 -05002694 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002695 char cmdname[256];
bellard9dc39cb2004-03-14 21:38:27 +00002696
bellard9307c4c2004-04-04 12:57:25 +00002697 /* extract the command name */
Bandan Dasae502122015-06-03 18:38:08 -04002698 p = get_command_name(*cmdp, cmdname, sizeof(cmdname));
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002699 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002700 return NULL;
ths3b46e622007-09-17 08:09:54 +00002701
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002702 cmd = search_dispatch_table(table, cmdname);
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002703 if (!cmd) {
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002704 monitor_printf(mon, "unknown command: '%.*s'\n",
Dr. David Alan Gilbert250b8192017-08-17 11:42:16 +01002705 (int)(p - cmdp_start), cmdp_start);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002706 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002707 }
bellard9307c4c2004-04-04 12:57:25 +00002708
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002709 /* filter out following useless space */
2710 while (qemu_isspace(*p)) {
2711 p++;
2712 }
Bandan Dasae502122015-06-03 18:38:08 -04002713
2714 *cmdp = p;
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002715 /* search sub command */
Bandan Dasae502122015-06-03 18:38:08 -04002716 if (cmd->sub_table != NULL && *p != '\0') {
Dr. David Alan Gilbert250b8192017-08-17 11:42:16 +01002717 return monitor_parse_command(mon, cmdp_start, cmdp, cmd->sub_table);
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002718 }
2719
Bandan Dasae502122015-06-03 18:38:08 -04002720 return cmd;
2721}
2722
2723/*
2724 * Parse arguments for @cmd.
2725 * If it can't be parsed, report to @mon, and return NULL.
2726 * Else, insert command arguments into a QDict, and return it.
2727 * Note: On success, caller has to free the QDict structure.
2728 */
2729
2730static QDict *monitor_parse_arguments(Monitor *mon,
2731 const char **endp,
2732 const mon_cmd_t *cmd)
2733{
2734 const char *typestr;
2735 char *key;
2736 int c;
2737 const char *p = *endp;
2738 char buf[1024];
2739 QDict *qdict = qdict_new();
2740
bellard9307c4c2004-04-04 12:57:25 +00002741 /* parse the parameters */
2742 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00002743 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002744 typestr = key_get_info(typestr, &key);
2745 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00002746 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002747 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00002748 typestr++;
2749 switch(c) {
2750 case 'F':
bellard81d09122004-07-14 17:21:37 +00002751 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002752 case 's':
2753 {
2754 int ret;
ths3b46e622007-09-17 08:09:54 +00002755
blueswir1cd390082008-11-16 13:53:32 +00002756 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002757 p++;
2758 if (*typestr == '?') {
2759 typestr++;
2760 if (*p == '\0') {
2761 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03002762 break;
bellard9307c4c2004-04-04 12:57:25 +00002763 }
2764 }
2765 ret = get_str(buf, sizeof(buf), &p);
2766 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002767 switch(c) {
2768 case 'F':
aliguori376253e2009-03-05 23:01:23 +00002769 monitor_printf(mon, "%s: filename expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002770 cmd->name);
bellard81d09122004-07-14 17:21:37 +00002771 break;
2772 case 'B':
aliguori376253e2009-03-05 23:01:23 +00002773 monitor_printf(mon, "%s: block device name expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002774 cmd->name);
bellard81d09122004-07-14 17:21:37 +00002775 break;
2776 default:
Bandan Dasae502122015-06-03 18:38:08 -04002777 monitor_printf(mon, "%s: string expected\n", cmd->name);
bellard81d09122004-07-14 17:21:37 +00002778 break;
2779 }
bellard9307c4c2004-04-04 12:57:25 +00002780 goto fail;
2781 }
Eric Blake46f5ac22017-04-27 16:58:17 -05002782 qdict_put_str(qdict, key, buf);
bellard9307c4c2004-04-04 12:57:25 +00002783 }
2784 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01002785 case 'O':
2786 {
2787 QemuOptsList *opts_list;
2788 QemuOpts *opts;
2789
2790 opts_list = qemu_find_opts(key);
2791 if (!opts_list || opts_list->desc->name) {
2792 goto bad_type;
2793 }
2794 while (qemu_isspace(*p)) {
2795 p++;
2796 }
2797 if (!*p)
2798 break;
2799 if (get_str(buf, sizeof(buf), &p) < 0) {
2800 goto fail;
2801 }
Markus Armbruster70b94332015-02-13 12:50:26 +01002802 opts = qemu_opts_parse_noisily(opts_list, buf, true);
Markus Armbruster361127d2010-02-10 20:24:35 +01002803 if (!opts) {
2804 goto fail;
2805 }
2806 qemu_opts_to_qdict(opts, qdict);
2807 qemu_opts_del(opts);
2808 }
2809 break;
bellard9307c4c2004-04-04 12:57:25 +00002810 case '/':
2811 {
2812 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002813
blueswir1cd390082008-11-16 13:53:32 +00002814 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002815 p++;
2816 if (*p == '/') {
2817 /* format found */
2818 p++;
2819 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002820 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002821 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002822 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002823 count = count * 10 + (*p - '0');
2824 p++;
2825 }
2826 }
2827 size = -1;
2828 format = -1;
2829 for(;;) {
2830 switch(*p) {
2831 case 'o':
2832 case 'd':
2833 case 'u':
2834 case 'x':
2835 case 'i':
2836 case 'c':
2837 format = *p++;
2838 break;
2839 case 'b':
2840 size = 1;
2841 p++;
2842 break;
2843 case 'h':
2844 size = 2;
2845 p++;
2846 break;
2847 case 'w':
2848 size = 4;
2849 p++;
2850 break;
2851 case 'g':
2852 case 'L':
2853 size = 8;
2854 p++;
2855 break;
2856 default:
2857 goto next;
2858 }
2859 }
2860 next:
blueswir1cd390082008-11-16 13:53:32 +00002861 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00002862 monitor_printf(mon, "invalid char in format: '%c'\n",
2863 *p);
bellard9307c4c2004-04-04 12:57:25 +00002864 goto fail;
2865 }
bellard9307c4c2004-04-04 12:57:25 +00002866 if (format < 0)
2867 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002868 if (format != 'i') {
2869 /* for 'i', not specifying a size gives -1 as size */
2870 if (size < 0)
2871 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002872 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002873 }
bellard9307c4c2004-04-04 12:57:25 +00002874 default_fmt_format = format;
2875 } else {
2876 count = 1;
2877 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002878 if (format != 'i') {
2879 size = default_fmt_size;
2880 } else {
2881 size = -1;
2882 }
bellard9307c4c2004-04-04 12:57:25 +00002883 }
Eric Blake46f5ac22017-04-27 16:58:17 -05002884 qdict_put_int(qdict, "count", count);
2885 qdict_put_int(qdict, "format", format);
2886 qdict_put_int(qdict, "size", size);
bellard9307c4c2004-04-04 12:57:25 +00002887 }
2888 break;
2889 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002890 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002891 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00002892 {
blueswir1c2efc952007-09-25 17:28:42 +00002893 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00002894
blueswir1cd390082008-11-16 13:53:32 +00002895 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002896 p++;
bellard34405572004-06-08 00:55:58 +00002897 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002898 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03002899 if (*p == '\0') {
2900 typestr++;
2901 break;
2902 }
bellard34405572004-06-08 00:55:58 +00002903 } else {
2904 if (*p == '.') {
2905 p++;
blueswir1cd390082008-11-16 13:53:32 +00002906 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00002907 p++;
bellard34405572004-06-08 00:55:58 +00002908 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03002909 typestr++;
2910 break;
bellard34405572004-06-08 00:55:58 +00002911 }
2912 }
bellard13224a82006-07-14 22:03:35 +00002913 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002914 }
aliguori376253e2009-03-05 23:01:23 +00002915 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00002916 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002917 /* Check if 'i' is greater than 32-bit */
2918 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
Bandan Dasae502122015-06-03 18:38:08 -04002919 monitor_printf(mon, "\'%s\' has failed: ", cmd->name);
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002920 monitor_printf(mon, "integer is for 32-bit values\n");
2921 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002922 } else if (c == 'M') {
Luiz Capitulino91162842012-04-26 17:34:30 -03002923 if (val < 0) {
2924 monitor_printf(mon, "enter a positive value\n");
2925 goto fail;
2926 }
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002927 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002928 }
Eric Blake46f5ac22017-04-27 16:58:17 -05002929 qdict_put_int(qdict, key, val);
bellard9307c4c2004-04-04 12:57:25 +00002930 }
2931 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02002932 case 'o':
2933 {
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +01002934 int ret;
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +01002935 uint64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02002936 char *end;
2937
2938 while (qemu_isspace(*p)) {
2939 p++;
2940 }
2941 if (*typestr == '?') {
2942 typestr++;
2943 if (*p == '\0') {
2944 break;
2945 }
2946 }
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +01002947 ret = qemu_strtosz_MiB(p, &end, &val);
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +01002948 if (ret < 0 || val > INT64_MAX) {
Jes Sorensendbc0c672010-10-21 17:15:47 +02002949 monitor_printf(mon, "invalid size\n");
2950 goto fail;
2951 }
Eric Blake46f5ac22017-04-27 16:58:17 -05002952 qdict_put_int(qdict, key, val);
Jes Sorensendbc0c672010-10-21 17:15:47 +02002953 p = end;
2954 }
2955 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01002956 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002957 {
2958 double val;
2959
2960 while (qemu_isspace(*p))
2961 p++;
2962 if (*typestr == '?') {
2963 typestr++;
2964 if (*p == '\0') {
2965 break;
2966 }
2967 }
2968 if (get_double(mon, &val, &p) < 0) {
2969 goto fail;
2970 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02002971 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01002972 switch (*p) {
2973 case 'm':
2974 val /= 1e3; p += 2; break;
2975 case 'u':
2976 val /= 1e6; p += 2; break;
2977 case 'n':
2978 val /= 1e9; p += 2; break;
2979 }
2980 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002981 if (*p && !qemu_isspace(*p)) {
2982 monitor_printf(mon, "Unknown unit suffix\n");
2983 goto fail;
2984 }
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +04002985 qdict_put(qdict, key, qnum_from_double(val));
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002986 }
2987 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002988 case 'b':
2989 {
2990 const char *beg;
Eric Blakefc48ffc2015-05-15 16:24:59 -06002991 bool val;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002992
2993 while (qemu_isspace(*p)) {
2994 p++;
2995 }
2996 beg = p;
2997 while (qemu_isgraph(*p)) {
2998 p++;
2999 }
3000 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06003001 val = true;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003002 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06003003 val = false;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003004 } else {
3005 monitor_printf(mon, "Expected 'on' or 'off'\n");
3006 goto fail;
3007 }
Eric Blake46f5ac22017-04-27 16:58:17 -05003008 qdict_put_bool(qdict, key, val);
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003009 }
3010 break;
bellard9307c4c2004-04-04 12:57:25 +00003011 case '-':
3012 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003013 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003014 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003015 /* option */
ths3b46e622007-09-17 08:09:54 +00003016
bellard9307c4c2004-04-04 12:57:25 +00003017 c = *typestr++;
3018 if (c == '\0')
3019 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003020 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003021 p++;
bellard9307c4c2004-04-04 12:57:25 +00003022 if (*p == '-') {
3023 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003024 if(c != *p) {
3025 if(!is_valid_option(p, typestr)) {
3026
3027 monitor_printf(mon, "%s: unsupported option -%c\n",
Bandan Dasae502122015-06-03 18:38:08 -04003028 cmd->name, *p);
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003029 goto fail;
3030 } else {
3031 skip_key = 1;
3032 }
bellard9307c4c2004-04-04 12:57:25 +00003033 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003034 if(skip_key) {
3035 p = tmp;
3036 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003037 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003038 p++;
Eric Blake46f5ac22017-04-27 16:58:17 -05003039 qdict_put_bool(qdict, key, true);
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003040 }
bellard9307c4c2004-04-04 12:57:25 +00003041 }
bellard9307c4c2004-04-04 12:57:25 +00003042 }
3043 break;
Wenchao Xia129be002013-08-27 20:38:26 +08003044 case 'S':
3045 {
3046 /* package all remaining string */
3047 int len;
3048
3049 while (qemu_isspace(*p)) {
3050 p++;
3051 }
3052 if (*typestr == '?') {
3053 typestr++;
3054 if (*p == '\0') {
3055 /* no remaining string: NULL argument */
3056 break;
3057 }
3058 }
3059 len = strlen(p);
3060 if (len <= 0) {
3061 monitor_printf(mon, "%s: string expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04003062 cmd->name);
Bandan Dase549d2a2015-06-03 18:38:10 -04003063 goto fail;
Wenchao Xia129be002013-08-27 20:38:26 +08003064 }
Eric Blake46f5ac22017-04-27 16:58:17 -05003065 qdict_put_str(qdict, key, p);
Wenchao Xia129be002013-08-27 20:38:26 +08003066 p += len;
3067 }
3068 break;
bellard9307c4c2004-04-04 12:57:25 +00003069 default:
3070 bad_type:
Bandan Dasae502122015-06-03 18:38:08 -04003071 monitor_printf(mon, "%s: unknown type '%c'\n", cmd->name, c);
bellard9307c4c2004-04-04 12:57:25 +00003072 goto fail;
3073 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003074 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003075 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003076 }
3077 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003078 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003079 p++;
3080 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003081 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
Bandan Dasae502122015-06-03 18:38:08 -04003082 cmd->name);
bellard9307c4c2004-04-04 12:57:25 +00003083 goto fail;
3084 }
3085
Bandan Dasae502122015-06-03 18:38:08 -04003086 return qdict;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003087
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003088fail:
Bandan Dasae502122015-06-03 18:38:08 -04003089 QDECREF(qdict);
Anthony Liguori7267c092011-08-20 22:09:37 -05003090 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003091 return NULL;
3092}
3093
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003094static void handle_hmp_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003095{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003096 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003097 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003098
Stefan Hajnoczi79cad8b2017-06-05 11:42:15 +01003099 trace_handle_hmp_command(mon, cmdline);
3100
Dr. David Alan Gilbert250b8192017-08-17 11:42:16 +01003101 cmd = monitor_parse_command(mon, cmdline, &cmdline, mon->cmd_table);
Bandan Dasae502122015-06-03 18:38:08 -04003102 if (!cmd) {
3103 return;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003104 }
3105
Bandan Dasae502122015-06-03 18:38:08 -04003106 qdict = monitor_parse_arguments(mon, &cmdline, cmd);
3107 if (!qdict) {
Bandan Dasdd41eea2015-06-03 18:38:09 -04003108 monitor_printf(mon, "Try \"help %s\" for more information\n",
3109 cmd->name);
Bandan Dasae502122015-06-03 18:38:08 -04003110 return;
3111 }
3112
Marc-André Lureau2b9e3572016-09-12 13:19:06 +04003113 cmd->cmd(mon, qdict);
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003114 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00003115}
3116
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003117static void cmd_completion(Monitor *mon, const char *name, const char *list)
bellard81d09122004-07-14 17:21:37 +00003118{
3119 const char *p, *pstart;
3120 char cmd[128];
3121 int len;
3122
3123 p = list;
3124 for(;;) {
3125 pstart = p;
3126 p = strchr(p, '|');
3127 if (!p)
3128 p = pstart + strlen(pstart);
3129 len = p - pstart;
3130 if (len > sizeof(cmd) - 2)
3131 len = sizeof(cmd) - 2;
3132 memcpy(cmd, pstart, len);
3133 cmd[len] = '\0';
3134 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003135 readline_add_completion(mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00003136 }
3137 if (*p == '\0')
3138 break;
3139 p++;
3140 }
3141}
3142
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003143static void file_completion(Monitor *mon, const char *input)
bellard81d09122004-07-14 17:21:37 +00003144{
3145 DIR *ffs;
3146 struct dirent *d;
3147 char path[1024];
3148 char file[1024], file_prefix[1024];
3149 int input_path_len;
3150 const char *p;
3151
ths5fafdf22007-09-16 21:08:06 +00003152 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00003153 if (!p) {
3154 input_path_len = 0;
3155 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00003156 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00003157 } else {
3158 input_path_len = p - input + 1;
3159 memcpy(path, input, input_path_len);
3160 if (input_path_len > sizeof(path) - 1)
3161 input_path_len = sizeof(path) - 1;
3162 path[input_path_len] = '\0';
3163 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3164 }
Bandan Das19f2db52015-06-03 18:38:07 -04003165
bellard81d09122004-07-14 17:21:37 +00003166 ffs = opendir(path);
3167 if (!ffs)
3168 return;
3169 for(;;) {
3170 struct stat sb;
3171 d = readdir(ffs);
3172 if (!d)
3173 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09003174
3175 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
3176 continue;
3177 }
3178
bellard81d09122004-07-14 17:21:37 +00003179 if (strstart(d->d_name, file_prefix, NULL)) {
3180 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00003181 if (input_path_len < sizeof(file))
3182 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3183 d->d_name);
bellard81d09122004-07-14 17:21:37 +00003184 /* stat the file to find out if it's a directory.
3185 * In that case add a slash to speed up typing long paths
3186 */
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01003187 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
blueswir1363a37d2008-08-21 17:58:08 +00003188 pstrcat(file, sizeof(file), "/");
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01003189 }
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003190 readline_add_completion(mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003191 }
3192 }
3193 closedir(ffs);
3194}
3195
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003196static const char *next_arg_type(const char *typestr)
3197{
3198 const char *p = strchr(typestr, ':');
3199 return (p != NULL ? ++p : typestr);
3200}
3201
Hani Benhabiles40d19392014-05-07 23:41:30 +01003202static void add_completion_option(ReadLineState *rs, const char *str,
3203 const char *option)
3204{
3205 if (!str || !option) {
3206 return;
3207 }
3208 if (!strncmp(option, str, strlen(str))) {
3209 readline_add_completion(rs, option);
3210 }
3211}
3212
Hani Benhabiles13e315d2014-05-07 23:41:29 +01003213void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
3214{
3215 size_t len;
3216 ChardevBackendInfoList *list, *start;
3217
3218 if (nb_args != 2) {
3219 return;
3220 }
3221 len = strlen(str);
3222 readline_set_completion_index(rs, len);
3223
3224 start = list = qmp_query_chardev_backends(NULL);
3225 while (list) {
3226 const char *chr_name = list->value->name;
3227
3228 if (!strncmp(chr_name, str, len)) {
3229 readline_add_completion(rs, chr_name);
3230 }
3231 list = list->next;
3232 }
3233 qapi_free_ChardevBackendInfoList(start);
3234}
3235
Hani Benhabilesb162b492014-05-07 23:41:31 +01003236void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
3237{
3238 size_t len;
3239 int i;
3240
3241 if (nb_args != 2) {
3242 return;
3243 }
3244 len = strlen(str);
3245 readline_set_completion_index(rs, len);
Markus Armbruster1c236ba2017-08-24 10:46:06 +02003246 for (i = 0; i < NET_CLIENT_DRIVER__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02003247 add_completion_option(rs, str, NetClientDriver_str(i));
Hani Benhabilesb162b492014-05-07 23:41:31 +01003248 }
3249}
3250
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003251void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003252{
3253 GSList *list, *elt;
3254 size_t len;
3255
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003256 if (nb_args != 2) {
3257 return;
3258 }
3259
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003260 len = strlen(str);
3261 readline_set_completion_index(rs, len);
3262 list = elt = object_class_get_list(TYPE_DEVICE, false);
3263 while (elt) {
3264 const char *name;
3265 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
3266 TYPE_DEVICE);
3267 name = object_class_get_name(OBJECT_CLASS(dc));
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003268
Eduardo Habkoste90f2a82017-05-03 17:35:44 -03003269 if (dc->user_creatable
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003270 && !strncmp(name, str, len)) {
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003271 readline_add_completion(rs, name);
3272 }
3273 elt = elt->next;
3274 }
3275 g_slist_free(list);
3276}
3277
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003278void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003279{
3280 GSList *list, *elt;
3281 size_t len;
3282
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003283 if (nb_args != 2) {
3284 return;
3285 }
3286
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003287 len = strlen(str);
3288 readline_set_completion_index(rs, len);
3289 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
3290 while (elt) {
3291 const char *name;
3292
3293 name = object_class_get_name(OBJECT_CLASS(elt->data));
3294 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
3295 readline_add_completion(rs, name);
3296 }
3297 elt = elt->next;
3298 }
3299 g_slist_free(list);
3300}
3301
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003302static void peripheral_device_del_completion(ReadLineState *rs,
3303 const char *str, size_t len)
3304{
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003305 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
3306 GSList *list, *item;
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003307
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003308 list = qdev_build_hotpluggable_device_list(peripheral);
3309 if (!list) {
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003310 return;
3311 }
3312
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003313 for (item = list; item; item = g_slist_next(item)) {
3314 DeviceState *dev = item->data;
3315
3316 if (dev->id && !strncmp(str, dev->id, len)) {
3317 readline_add_completion(rs, dev->id);
3318 }
3319 }
3320
3321 g_slist_free(list);
3322}
3323
Hani Benhabiles6297d9a2014-05-07 23:41:28 +01003324void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3325{
3326 size_t len;
3327 ChardevInfoList *list, *start;
3328
3329 if (nb_args != 2) {
3330 return;
3331 }
3332 len = strlen(str);
3333 readline_set_completion_index(rs, len);
3334
3335 start = list = qmp_query_chardev(NULL);
3336 while (list) {
3337 ChardevInfo *chr = list->value;
3338
3339 if (!strncmp(chr->label, str, len)) {
3340 readline_add_completion(rs, chr->label);
3341 }
3342 list = list->next;
3343 }
3344 qapi_free_ChardevInfoList(start);
3345}
3346
Hani Benhabiles8e597772014-05-27 23:39:30 +01003347static void ringbuf_completion(ReadLineState *rs, const char *str)
3348{
3349 size_t len;
3350 ChardevInfoList *list, *start;
3351
3352 len = strlen(str);
3353 readline_set_completion_index(rs, len);
3354
3355 start = list = qmp_query_chardev(NULL);
3356 while (list) {
3357 ChardevInfo *chr_info = list->value;
3358
3359 if (!strncmp(chr_info->label, str, len)) {
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03003360 Chardev *chr = qemu_chr_find(chr_info->label);
Marc-André Lureau777357d2016-12-07 18:39:10 +03003361 if (chr && CHARDEV_IS_RINGBUF(chr)) {
Hani Benhabiles8e597772014-05-27 23:39:30 +01003362 readline_add_completion(rs, chr_info->label);
3363 }
3364 }
3365 list = list->next;
3366 }
3367 qapi_free_ChardevInfoList(start);
3368}
3369
Hani Benhabiles8e597772014-05-27 23:39:30 +01003370void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
3371{
3372 if (nb_args != 2) {
3373 return;
3374 }
3375 ringbuf_completion(rs, str);
3376}
3377
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003378void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
3379{
3380 size_t len;
3381
3382 if (nb_args != 2) {
3383 return;
3384 }
3385
3386 len = strlen(str);
3387 readline_set_completion_index(rs, len);
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003388 peripheral_device_del_completion(rs, str, len);
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003389}
3390
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003391void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003392{
3393 ObjectPropertyInfoList *list, *start;
3394 size_t len;
3395
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003396 if (nb_args != 2) {
3397 return;
3398 }
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003399 len = strlen(str);
3400 readline_set_completion_index(rs, len);
3401
3402 start = list = qmp_qom_list("/objects", NULL);
3403 while (list) {
3404 ObjectPropertyInfo *info = list->value;
3405
3406 if (!strncmp(info->type, "child<", 5)
3407 && !strncmp(info->name, str, len)) {
3408 readline_add_completion(rs, info->name);
3409 }
3410 list = list->next;
3411 }
3412 qapi_free_ObjectPropertyInfoList(start);
3413}
3414
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003415void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
3416{
3417 int i;
3418 char *sep;
3419 size_t len;
3420
3421 if (nb_args != 2) {
3422 return;
3423 }
3424 sep = strrchr(str, '-');
3425 if (sep) {
3426 str = sep + 1;
3427 }
3428 len = strlen(str);
3429 readline_set_completion_index(rs, len);
Eric Blake7fb1cf12015-11-18 01:52:57 -07003430 for (i = 0; i < Q_KEY_CODE__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02003431 if (!strncmp(str, QKeyCode_str(i), len)) {
3432 readline_add_completion(rs, QKeyCode_str(i));
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003433 }
3434 }
3435}
3436
Hani Benhabiles40d19392014-05-07 23:41:30 +01003437void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
3438{
3439 size_t len;
3440
3441 len = strlen(str);
3442 readline_set_completion_index(rs, len);
3443 if (nb_args == 2) {
Jason Wangeaed4832015-04-23 14:21:38 +08003444 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles40d19392014-05-07 23:41:30 +01003445 int count, i;
3446 count = qemu_find_net_clients_except(NULL, ncs,
Eric Blakef394b2e2016-07-13 21:50:23 -06003447 NET_CLIENT_DRIVER_NONE,
Jason Wangeaed4832015-04-23 14:21:38 +08003448 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003449 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles40d19392014-05-07 23:41:30 +01003450 const char *name = ncs[i]->name;
3451 if (!strncmp(str, name, len)) {
3452 readline_add_completion(rs, name);
3453 }
3454 }
3455 } else if (nb_args == 3) {
3456 add_completion_option(rs, str, "on");
3457 add_completion_option(rs, str, "off");
3458 }
3459}
3460
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003461void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
3462{
3463 int len, count, i;
Jason Wangeaed4832015-04-23 14:21:38 +08003464 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003465
3466 if (nb_args != 2) {
3467 return;
3468 }
3469
3470 len = strlen(str);
3471 readline_set_completion_index(rs, len);
Eric Blakef394b2e2016-07-13 21:50:23 -06003472 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_DRIVER_NIC,
Jason Wangeaed4832015-04-23 14:21:38 +08003473 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003474 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003475 QemuOpts *opts;
3476 const char *name = ncs[i]->name;
3477 if (strncmp(str, name, len)) {
3478 continue;
3479 }
3480 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
3481 if (opts) {
3482 readline_add_completion(rs, name);
3483 }
3484 }
3485}
3486
Lluís Vilanovabd712112016-07-11 12:53:51 +02003487void info_trace_events_completion(ReadLineState *rs, int nb_args, const char *str)
3488{
3489 size_t len;
3490
3491 len = strlen(str);
3492 readline_set_completion_index(rs, len);
3493 if (nb_args == 2) {
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003494 TraceEventIter iter;
3495 TraceEvent *ev;
3496 char *pattern = g_strdup_printf("%s*", str);
3497 trace_event_iter_init(&iter, pattern);
3498 while ((ev = trace_event_iter_next(&iter)) != NULL) {
3499 readline_add_completion(rs, trace_event_get_name(ev));
Lluís Vilanovabd712112016-07-11 12:53:51 +02003500 }
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003501 g_free(pattern);
Lluís Vilanovabd712112016-07-11 12:53:51 +02003502 }
3503}
3504
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003505void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
3506{
3507 size_t len;
3508
3509 len = strlen(str);
3510 readline_set_completion_index(rs, len);
3511 if (nb_args == 2) {
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003512 TraceEventIter iter;
3513 TraceEvent *ev;
3514 char *pattern = g_strdup_printf("%s*", str);
3515 trace_event_iter_init(&iter, pattern);
3516 while ((ev = trace_event_iter_next(&iter)) != NULL) {
3517 readline_add_completion(rs, trace_event_get_name(ev));
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003518 }
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003519 g_free(pattern);
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003520 } else if (nb_args == 3) {
3521 add_completion_option(rs, str, "on");
3522 add_completion_option(rs, str, "off");
3523 }
3524}
3525
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003526void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
3527{
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003528 int i;
3529
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003530 if (nb_args != 2) {
3531 return;
3532 }
3533 readline_set_completion_index(rs, strlen(str));
Michal Privoznik14d53b42017-09-07 10:05:24 +02003534 for (i = 0; i < WATCHDOG_ACTION__MAX; i++) {
3535 add_completion_option(rs, str, WatchdogAction_str(i));
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003536 }
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003537}
3538
Hani Benhabilesc68a0402014-05-27 23:39:32 +01003539void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
3540 const char *str)
3541{
3542 size_t len;
3543
3544 len = strlen(str);
3545 readline_set_completion_index(rs, len);
3546 if (nb_args == 2) {
3547 int i;
Eric Blake7fb1cf12015-11-18 01:52:57 -07003548 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02003549 const char *name = MigrationCapability_str(i);
Hani Benhabilesc68a0402014-05-27 23:39:32 +01003550 if (!strncmp(str, name, len)) {
3551 readline_add_completion(rs, name);
3552 }
3553 }
3554 } else if (nb_args == 3) {
3555 add_completion_option(rs, str, "on");
3556 add_completion_option(rs, str, "off");
3557 }
3558}
3559
Liang Li50e9a622015-03-23 16:32:29 +08003560void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
3561 const char *str)
3562{
3563 size_t len;
3564
3565 len = strlen(str);
3566 readline_set_completion_index(rs, len);
3567 if (nb_args == 2) {
3568 int i;
Eric Blake7fb1cf12015-11-18 01:52:57 -07003569 for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02003570 const char *name = MigrationParameter_str(i);
Liang Li50e9a622015-03-23 16:32:29 +08003571 if (!strncmp(str, name, len)) {
3572 readline_add_completion(rs, name);
3573 }
3574 }
3575 }
3576}
3577
Hani Benhabilese3bb5322014-05-27 23:39:34 +01003578void host_net_add_completion(ReadLineState *rs, int nb_args, const char *str)
3579{
3580 int i;
3581 size_t len;
3582 if (nb_args != 2) {
3583 return;
3584 }
3585 len = strlen(str);
3586 readline_set_completion_index(rs, len);
3587 for (i = 0; host_net_devices[i]; i++) {
3588 if (!strncmp(host_net_devices[i], str, len)) {
3589 readline_add_completion(rs, host_net_devices[i]);
3590 }
3591 }
3592}
3593
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003594void host_net_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3595{
Jason Wangeaed4832015-04-23 14:21:38 +08003596 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003597 int count, i, len;
3598
3599 len = strlen(str);
3600 readline_set_completion_index(rs, len);
3601 if (nb_args == 2) {
3602 count = qemu_find_net_clients_except(NULL, ncs,
Eric Blakef394b2e2016-07-13 21:50:23 -06003603 NET_CLIENT_DRIVER_NONE,
Jason Wangeaed4832015-04-23 14:21:38 +08003604 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003605 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003606 int id;
3607 char name[16];
3608
3609 if (net_hub_id_for_client(ncs[i], &id)) {
3610 continue;
3611 }
3612 snprintf(name, sizeof(name), "%d", id);
3613 if (!strncmp(str, name, len)) {
3614 readline_add_completion(rs, name);
3615 }
3616 }
3617 return;
3618 } else if (nb_args == 3) {
3619 count = qemu_find_net_clients_except(NULL, ncs,
Eric Blakef394b2e2016-07-13 21:50:23 -06003620 NET_CLIENT_DRIVER_NIC,
Jason Wangeaed4832015-04-23 14:21:38 +08003621 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003622 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Jason Wang2c4681f2015-02-02 15:06:38 +08003623 int id;
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003624 const char *name;
3625
Eric Blakef394b2e2016-07-13 21:50:23 -06003626 if (ncs[i]->info->type == NET_CLIENT_DRIVER_HUBPORT ||
Jason Wang2c4681f2015-02-02 15:06:38 +08003627 net_hub_id_for_client(ncs[i], &id)) {
3628 continue;
3629 }
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003630 name = ncs[i]->name;
3631 if (!strncmp(str, name, len)) {
3632 readline_add_completion(rs, name);
3633 }
3634 }
3635 return;
3636 }
3637}
3638
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003639static void vm_completion(ReadLineState *rs, const char *str)
3640{
3641 size_t len;
Kevin Wolf7c8eece2016-03-22 18:58:50 +01003642 BlockDriverState *bs;
Kevin Wolf88be7b42016-05-20 18:49:07 +02003643 BdrvNextIterator it;
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003644
3645 len = strlen(str);
3646 readline_set_completion_index(rs, len);
Kevin Wolf7c8eece2016-03-22 18:58:50 +01003647
Kevin Wolf88be7b42016-05-20 18:49:07 +02003648 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003649 SnapshotInfoList *snapshots, *snapshot;
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003650 AioContext *ctx = bdrv_get_aio_context(bs);
3651 bool ok = false;
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003652
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003653 aio_context_acquire(ctx);
3654 if (bdrv_can_snapshot(bs)) {
3655 ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0;
3656 }
3657 aio_context_release(ctx);
3658 if (!ok) {
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003659 continue;
3660 }
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003661
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003662 snapshot = snapshots;
3663 while (snapshot) {
3664 char *completion = snapshot->value->name;
3665 if (!strncmp(str, completion, len)) {
3666 readline_add_completion(rs, completion);
3667 }
3668 completion = snapshot->value->id;
3669 if (!strncmp(str, completion, len)) {
3670 readline_add_completion(rs, completion);
3671 }
3672 snapshot = snapshot->next;
3673 }
3674 qapi_free_SnapshotInfoList(snapshots);
3675 }
3676
3677}
3678
3679void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
3680{
3681 if (nb_args == 2) {
3682 vm_completion(rs, str);
3683 }
3684}
3685
3686void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
3687{
3688 if (nb_args == 2) {
3689 vm_completion(rs, str);
3690 }
3691}
3692
Wenchao Xiac35b6402013-08-27 20:38:24 +08003693static void monitor_find_completion_by_table(Monitor *mon,
3694 const mon_cmd_t *cmd_table,
3695 char **args,
3696 int nb_args)
bellard81d09122004-07-14 17:21:37 +00003697{
3698 const char *cmdname;
Wenchao Xiac35b6402013-08-27 20:38:24 +08003699 int i;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003700 const char *ptype, *str, *name;
Anthony Liguoric227f092009-10-01 16:12:16 -05003701 const mon_cmd_t *cmd;
Max Reitzda27a002016-03-16 19:54:29 +01003702 BlockBackend *blk = NULL;
bellard81d09122004-07-14 17:21:37 +00003703
bellard81d09122004-07-14 17:21:37 +00003704 if (nb_args <= 1) {
3705 /* command completion */
3706 if (nb_args == 0)
3707 cmdname = "";
3708 else
3709 cmdname = args[0];
Wenchao Xiad2674b22013-08-27 20:38:16 +08003710 readline_set_completion_index(mon->rs, strlen(cmdname));
Wenchao Xiac35b6402013-08-27 20:38:24 +08003711 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003712 cmd_completion(mon, cmdname, cmd->name);
bellard81d09122004-07-14 17:21:37 +00003713 }
3714 } else {
3715 /* find the command */
Wenchao Xiac35b6402013-08-27 20:38:24 +08003716 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Jan Kiszka03a63482010-06-16 00:38:33 +02003717 if (compare_cmd(args[0], cmd->name)) {
3718 break;
3719 }
bellard81d09122004-07-14 17:21:37 +00003720 }
Jan Kiszka03a63482010-06-16 00:38:33 +02003721 if (!cmd->name) {
Wenchao Xiac35b6402013-08-27 20:38:24 +08003722 return;
Jan Kiszka03a63482010-06-16 00:38:33 +02003723 }
3724
Wenchao Xiad903a772013-08-27 20:38:25 +08003725 if (cmd->sub_table) {
3726 /* do the job again */
Stefan Weile7ae7712015-03-08 19:30:01 +01003727 monitor_find_completion_by_table(mon, cmd->sub_table,
3728 &args[1], nb_args - 1);
3729 return;
Wenchao Xiad903a772013-08-27 20:38:25 +08003730 }
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003731 if (cmd->command_completion) {
Stefan Weile7ae7712015-03-08 19:30:01 +01003732 cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
3733 return;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003734 }
Wenchao Xiad903a772013-08-27 20:38:25 +08003735
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003736 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003737 for(i = 0; i < nb_args - 2; i++) {
3738 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003739 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003740 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003741 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003742 }
3743 }
3744 str = args[nb_args - 1];
Kevin Wolf48fe86f2014-11-12 16:24:02 +01003745 while (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02003746 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00003747 }
bellard81d09122004-07-14 17:21:37 +00003748 switch(*ptype) {
3749 case 'F':
3750 /* file completion */
Wenchao Xiad2674b22013-08-27 20:38:16 +08003751 readline_set_completion_index(mon->rs, strlen(str));
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003752 file_completion(mon, str);
bellard81d09122004-07-14 17:21:37 +00003753 break;
3754 case 'B':
3755 /* block device name completion */
Wenchao Xia599a9262013-08-27 20:38:15 +08003756 readline_set_completion_index(mon->rs, strlen(str));
Max Reitzda27a002016-03-16 19:54:29 +01003757 while ((blk = blk_next(blk)) != NULL) {
3758 name = blk_name(blk);
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003759 if (str[0] == '\0' ||
3760 !strncmp(name, str, strlen(str))) {
3761 readline_add_completion(mon->rs, name);
3762 }
3763 }
bellard81d09122004-07-14 17:21:37 +00003764 break;
bellard7fe48482004-10-09 18:08:01 +00003765 case 's':
Wenchao Xia129be002013-08-27 20:38:26 +08003766 case 'S':
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003767 if (!strcmp(cmd->name, "help|?")) {
Wenchao Xia7ca0e062013-08-27 20:38:27 +08003768 monitor_find_completion_by_table(mon, cmd_table,
3769 &args[1], nb_args - 1);
bellard7fe48482004-10-09 18:08:01 +00003770 }
3771 break;
bellard81d09122004-07-14 17:21:37 +00003772 default:
3773 break;
3774 }
3775 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08003776}
3777
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003778static void monitor_find_completion(void *opaque,
Wenchao Xiac35b6402013-08-27 20:38:24 +08003779 const char *cmdline)
3780{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003781 Monitor *mon = opaque;
Wenchao Xiac35b6402013-08-27 20:38:24 +08003782 char *args[MAX_ARGS];
3783 int nb_args, len;
3784
3785 /* 1. parse the cmdline */
3786 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
3787 return;
3788 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08003789
3790 /* if the line ends with a space, it means we want to complete the
3791 next arg */
3792 len = strlen(cmdline);
3793 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
3794 if (nb_args >= MAX_ARGS) {
3795 goto cleanup;
3796 }
3797 args[nb_args++] = g_strdup("");
3798 }
3799
3800 /* 2. auto complete according to args */
3801 monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args);
Jan Kiszka03a63482010-06-16 00:38:33 +02003802
3803cleanup:
Wenchao Xiadcc70cd2013-08-27 20:38:22 +08003804 free_cmdline_args(args, nb_args);
bellard81d09122004-07-14 17:21:37 +00003805}
3806
aliguori731b0362009-03-05 23:01:42 +00003807static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003808{
aliguori731b0362009-03-05 23:01:42 +00003809 Monitor *mon = opaque;
3810
Jan Kiszkac62313b2009-12-04 14:05:29 +01003811 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003812}
3813
Paolo Bonzini95385fe2015-11-25 22:23:31 +01003814static void handle_qmp_command(JSONMessageParser *parser, GQueue *tokens)
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003815{
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04003816 QObject *req, *rsp = NULL, *id = NULL;
3817 QDict *qdict = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003818 Monitor *mon = cur_mon;
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04003819 Error *err = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003820
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04003821 req = json_parser_parse_err(tokens, NULL, &err);
Markus Armbrusterbbf10282017-02-17 21:38:24 +01003822 if (!req && !err) {
3823 /* json_parser_parse_err() sucks: can fail without setting @err */
3824 error_setg(&err, QERR_JSON_PARSING);
3825 }
3826 if (err) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003827 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003828 }
3829
Markus Armbruster104fc302017-03-03 13:32:28 +01003830 qdict = qobject_to_qdict(req);
3831 if (qdict) {
3832 id = qdict_get(qdict, "id");
3833 qobject_incref(id);
3834 qdict_del(qdict, "id");
3835 } /* else will fail qmp_dispatch() */
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04003836
Denis V. Lunevbd6952a2017-07-25 17:39:23 +03003837 if (trace_event_get_state_backends(TRACE_HANDLE_QMP_COMMAND)) {
3838 QString *req_json = qobject_to_json(req);
3839 trace_handle_qmp_command(mon, qstring_get_str(req_json));
3840 QDECREF(req_json);
3841 }
Stefan Hajnoczib097efc2017-06-05 11:42:16 +01003842
Markus Armbruster635db182017-03-03 13:32:27 +01003843 rsp = qmp_dispatch(cur_mon->qmp.commands, req);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003844
Markus Armbruster635db182017-03-03 13:32:27 +01003845 if (mon->qmp.commands == &qmp_cap_negotiation_commands) {
3846 qdict = qdict_get_qdict(qobject_to_qdict(rsp), "error");
3847 if (qdict
3848 && !g_strcmp0(qdict_get_try_str(qdict, "class"),
Markus Armbruster977c7362017-08-24 10:46:08 +02003849 QapiErrorClass_str(ERROR_CLASS_COMMAND_NOT_FOUND))) {
Markus Armbruster635db182017-03-03 13:32:27 +01003850 /* Provide a more useful error message */
3851 qdict_del(qdict, "desc");
Eric Blake46f5ac22017-04-27 16:58:17 -05003852 qdict_put_str(qdict, "desc", "Expecting capabilities negotiation"
3853 " with 'qmp_capabilities'");
Markus Armbruster635db182017-03-03 13:32:27 +01003854 }
3855 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003856
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003857err_out:
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04003858 if (err) {
3859 qdict = qdict_new();
3860 qdict_put_obj(qdict, "error", qmp_build_error_object(err));
3861 error_free(err);
3862 rsp = QOBJECT(qdict);
3863 }
3864
3865 if (rsp) {
3866 if (id) {
3867 qdict_put_obj(qobject_to_qdict(rsp), "id", id);
3868 id = NULL;
3869 }
3870
3871 monitor_json_emitter(mon, rsp);
3872 }
3873
3874 qobject_decref(id);
3875 qobject_decref(rsp);
3876 qobject_decref(req);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003877}
3878
Markus Armbrusterc83fe232015-03-06 19:20:51 +01003879static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003880{
3881 Monitor *old_mon = cur_mon;
3882
3883 cur_mon = opaque;
3884
Markus Armbruster74358f22015-03-06 19:35:59 +01003885 json_message_parser_feed(&cur_mon->qmp.parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003886
3887 cur_mon = old_mon;
3888}
3889
aliguori731b0362009-03-05 23:01:42 +00003890static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00003891{
aliguori731b0362009-03-05 23:01:42 +00003892 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00003893 int i;
aliguori376253e2009-03-05 23:01:23 +00003894
aliguori731b0362009-03-05 23:01:42 +00003895 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00003896
aliguoricde76ee2009-03-05 23:01:51 +00003897 if (cur_mon->rs) {
3898 for (i = 0; i < size; i++)
3899 readline_handle_byte(cur_mon->rs, buf[i]);
3900 } else {
3901 if (size == 0 || buf[size - 1] != 0)
3902 monitor_printf(cur_mon, "corrupted command\n");
3903 else
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003904 handle_hmp_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00003905 }
aliguori731b0362009-03-05 23:01:42 +00003906
3907 cur_mon = old_mon;
3908}
aliguorid8f44602008-10-06 13:52:44 +00003909
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003910static void monitor_command_cb(void *opaque, const char *cmdline,
3911 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00003912{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003913 Monitor *mon = opaque;
3914
aliguori731b0362009-03-05 23:01:42 +00003915 monitor_suspend(mon);
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003916 handle_hmp_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00003917 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00003918}
3919
aliguoricde76ee2009-03-05 23:01:51 +00003920int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003921{
aliguoricde76ee2009-03-05 23:01:51 +00003922 if (!mon->rs)
3923 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00003924 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00003925 return 0;
aliguorid8f44602008-10-06 13:52:44 +00003926}
3927
aliguori376253e2009-03-05 23:01:23 +00003928void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003929{
aliguoricde76ee2009-03-05 23:01:51 +00003930 if (!mon->rs)
3931 return;
aliguori731b0362009-03-05 23:01:42 +00003932 if (--mon->suspend_cnt == 0)
3933 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00003934}
3935
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003936static QObject *get_qmp_greeting(void)
3937{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03003938 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003939
Markus Armbruster7fad30f2015-09-16 13:06:19 +02003940 qmp_marshal_query_version(NULL, &ver, NULL);
Marc-André Lureauc8235012016-09-12 13:19:04 +04003941
3942 return qobject_from_jsonf("{'QMP': {'version': %p, 'capabilities': []}}",
3943 ver);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003944}
3945
Markus Armbrusterc83fe232015-03-06 19:20:51 +01003946static void monitor_qmp_event(void *opaque, int event)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003947{
Luiz Capitulino47116d12010-02-08 17:01:30 -02003948 QObject *data;
3949 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003950
Luiz Capitulino47116d12010-02-08 17:01:30 -02003951 switch (event) {
3952 case CHR_EVENT_OPENED:
Markus Armbruster635db182017-03-03 13:32:27 +01003953 mon->qmp.commands = &qmp_cap_negotiation_commands;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003954 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003955 monitor_json_emitter(mon, data);
3956 qobject_decref(data);
Corey Bryantefb87c12012-08-14 16:43:48 -04003957 mon_refcount++;
Luiz Capitulino47116d12010-02-08 17:01:30 -02003958 break;
3959 case CHR_EVENT_CLOSED:
Markus Armbruster74358f22015-03-06 19:35:59 +01003960 json_message_parser_destroy(&mon->qmp.parser);
3961 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Corey Bryantefb87c12012-08-14 16:43:48 -04003962 mon_refcount--;
3963 monitor_fdsets_cleanup();
Luiz Capitulino47116d12010-02-08 17:01:30 -02003964 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003965 }
3966}
3967
aliguori731b0362009-03-05 23:01:42 +00003968static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00003969{
aliguori376253e2009-03-05 23:01:23 +00003970 Monitor *mon = opaque;
3971
aliguori2724b182009-03-05 23:01:47 +00003972 switch (event) {
3973 case CHR_EVENT_MUX_IN:
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003974 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003975 mon->mux_out = 0;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003976 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003977 if (mon->reset_seen) {
3978 readline_restart(mon->rs);
3979 monitor_resume(mon);
3980 monitor_flush(mon);
3981 } else {
3982 mon->suspend_cnt = 0;
3983 }
aliguori2724b182009-03-05 23:01:47 +00003984 break;
ths86e94de2007-01-05 22:01:59 +00003985
aliguori2724b182009-03-05 23:01:47 +00003986 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003987 if (mon->reset_seen) {
3988 if (mon->suspend_cnt == 0) {
3989 monitor_printf(mon, "\n");
3990 }
3991 monitor_flush(mon);
3992 monitor_suspend(mon);
3993 } else {
3994 mon->suspend_cnt++;
3995 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003996 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003997 mon->mux_out = 1;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003998 qemu_mutex_unlock(&mon->out_lock);
aliguori2724b182009-03-05 23:01:47 +00003999 break;
4000
Amit Shahb6b8df52009-10-07 18:31:16 +05304001 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004002 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4003 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004004 if (!mon->mux_out) {
Stratos Psomadakise5554e22014-09-15 15:34:57 +03004005 readline_restart(mon->rs);
aliguori2724b182009-03-05 23:01:47 +00004006 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004007 }
4008 mon->reset_seen = 1;
Corey Bryantefb87c12012-08-14 16:43:48 -04004009 mon_refcount++;
4010 break;
4011
4012 case CHR_EVENT_CLOSED:
4013 mon_refcount--;
4014 monitor_fdsets_cleanup();
aliguori2724b182009-03-05 23:01:47 +00004015 break;
4016 }
ths86e94de2007-01-05 22:01:59 +00004017}
4018
Wayne Xia816f8922011-10-12 11:32:41 +08004019static int
4020compare_mon_cmd(const void *a, const void *b)
4021{
4022 return strcmp(((const mon_cmd_t *)a)->name,
4023 ((const mon_cmd_t *)b)->name);
4024}
4025
4026static void sortcmdlist(void)
4027{
4028 int array_num;
4029 int elem_size = sizeof(mon_cmd_t);
4030
4031 array_num = sizeof(mon_cmds)/elem_size-1;
4032 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
4033
4034 array_num = sizeof(info_cmds)/elem_size-1;
4035 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
4036}
4037
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004038/* These functions just adapt the readline interface in a typesafe way. We
4039 * could cast function pointers but that discards compiler checks.
4040 */
Stefan Weild5d15072014-01-25 18:18:23 +01004041static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
4042 const char *fmt, ...)
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004043{
4044 va_list ap;
4045 va_start(ap, fmt);
4046 monitor_vprintf(opaque, fmt, ap);
4047 va_end(ap);
4048}
4049
4050static void monitor_readline_flush(void *opaque)
4051{
4052 monitor_flush(opaque);
4053}
4054
Paolo Bonzini397d30e2016-10-24 18:31:02 +02004055/*
4056 * Print to current monitor if we have one, else to stderr.
4057 * TODO should return int, so callers can calculate width, but that
4058 * requires surgery to monitor_vprintf(). Left for another day.
4059 */
4060void error_vprintf(const char *fmt, va_list ap)
4061{
4062 if (cur_mon && !monitor_cur_is_qmp()) {
4063 monitor_vprintf(cur_mon, fmt, ap);
4064 } else {
4065 vfprintf(stderr, fmt, ap);
4066 }
4067}
4068
4069void error_vprintf_unless_qmp(const char *fmt, va_list ap)
4070{
4071 if (cur_mon && !monitor_cur_is_qmp()) {
4072 monitor_vprintf(cur_mon, fmt, ap);
Marc-André Lureau0d6b50d2017-01-05 14:59:57 +01004073 } else if (!cur_mon) {
4074 vfprintf(stderr, fmt, ap);
Paolo Bonzini397d30e2016-10-24 18:31:02 +02004075 }
4076}
4077
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004078static void __attribute__((constructor)) monitor_lock_init(void)
4079{
4080 qemu_mutex_init(&monitor_lock);
4081}
4082
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03004083void monitor_init(Chardev *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004084{
aliguori731b0362009-03-05 23:01:42 +00004085 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004086 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004087
4088 if (is_first_init) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +02004089 monitor_qapi_event_init();
Wenchao Xiad0383172013-08-27 20:38:18 +08004090 sortcmdlist();
ths20d8a3e2007-02-18 17:04:49 +00004091 is_first_init = 0;
4092 }
aliguori87127162009-03-05 23:01:29 +00004093
Wenchao Xiab01fe892013-08-27 20:38:19 +08004094 mon = g_malloc(sizeof(*mon));
4095 monitor_data_init(mon);
ths20d8a3e2007-02-18 17:04:49 +00004096
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03004097 qemu_chr_fe_init(&mon->chr, chr, &error_abort);
aliguori731b0362009-03-05 23:01:42 +00004098 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004099 if (flags & MONITOR_USE_READLINE) {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004100 mon->rs = readline_init(monitor_readline_printf,
4101 monitor_readline_flush,
4102 mon,
4103 monitor_find_completion);
aliguoricde76ee2009-03-05 23:01:51 +00004104 monitor_read_command(mon, 0);
4105 }
aliguori87127162009-03-05 23:01:29 +00004106
Markus Armbruster9f3982f2015-03-06 19:56:38 +01004107 if (monitor_is_qmp(mon)) {
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03004108 qemu_chr_fe_set_handlers(&mon->chr, monitor_can_read, monitor_qmp_read,
Anton Nefedov81517ba2017-07-06 15:08:49 +03004109 monitor_qmp_event, NULL, mon, NULL, true);
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03004110 qemu_chr_fe_set_echo(&mon->chr, true);
Markus Armbruster74358f22015-03-06 19:35:59 +01004111 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004112 } else {
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03004113 qemu_chr_fe_set_handlers(&mon->chr, monitor_can_read, monitor_read,
Anton Nefedov81517ba2017-07-06 15:08:49 +03004114 monitor_event, NULL, mon, NULL, true);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004115 }
aliguori87127162009-03-05 23:01:29 +00004116
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004117 qemu_mutex_lock(&monitor_lock);
Blue Swirl72cf2d42009-09-12 07:36:22 +00004118 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004119 qemu_mutex_unlock(&monitor_lock);
bellard7e2515e2004-08-01 21:52:19 +00004120}
4121
Marc-André Lureau2ef45712016-08-01 15:23:42 +04004122void monitor_cleanup(void)
4123{
4124 Monitor *mon, *next;
4125
4126 qemu_mutex_lock(&monitor_lock);
4127 QLIST_FOREACH_SAFE(mon, &mon_list, entry, next) {
4128 QLIST_REMOVE(mon, entry);
4129 monitor_data_destroy(mon);
4130 g_free(mon);
4131 }
4132 qemu_mutex_unlock(&monitor_lock);
4133}
4134
Paolo Bonzini4d454572012-11-26 16:03:42 +01004135QemuOptsList qemu_mon_opts = {
4136 .name = "mon",
4137 .implied_opt_name = "chardev",
4138 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
4139 .desc = {
4140 {
4141 .name = "mode",
4142 .type = QEMU_OPT_STRING,
4143 },{
4144 .name = "chardev",
4145 .type = QEMU_OPT_STRING,
4146 },{
Marc-André Lureaubdbcb542016-10-11 21:41:21 +04004147 .name = "default", /* deprecated */
Paolo Bonzini4d454572012-11-26 16:03:42 +01004148 .type = QEMU_OPT_BOOL,
4149 },{
4150 .name = "pretty",
4151 .type = QEMU_OPT_BOOL,
4152 },
4153 { /* end of list */ }
4154 },
4155};
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004156
4157#ifndef TARGET_I386
4158void qmp_rtc_reset_reinjection(Error **errp)
4159{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004160 error_setg(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection");
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004161}
4162#endif
Jason J. Herne7ee0c3e2015-06-26 14:03:16 -04004163
4164#ifndef TARGET_S390X
4165void qmp_dump_skeys(const char *filename, Error **errp)
4166{
4167 error_setg(errp, QERR_FEATURE_DISABLED, "dump-skeys");
4168}
4169#endif
Peter Xuae50a772016-03-30 17:27:24 +01004170
4171#ifndef TARGET_ARM
4172GICCapabilityList *qmp_query_gic_capabilities(Error **errp)
4173{
4174 error_setg(errp, QERR_FEATURE_DISABLED, "query-gic-capabilities");
4175 return NULL;
4176}
4177#endif
Igor Mammedovd4633542016-06-10 06:29:06 +05304178
4179HotpluggableCPUList *qmp_query_hotpluggable_cpus(Error **errp)
4180{
4181 MachineState *ms = MACHINE(qdev_get_machine());
4182 MachineClass *mc = MACHINE_GET_CLASS(ms);
4183
Igor Mammedovc5514d02017-02-10 11:20:57 +01004184 if (!mc->has_hotpluggable_cpus) {
Igor Mammedovd4633542016-06-10 06:29:06 +05304185 error_setg(errp, QERR_FEATURE_DISABLED, "query-hotpluggable-cpus");
4186 return NULL;
4187 }
4188
Igor Mammedovc5514d02017-02-10 11:20:57 +01004189 return machine_query_hotpluggable_cpus(ms);
Igor Mammedovd4633542016-06-10 06:29:06 +05304190}