blob: b9da5e20d1e76d878e84632314e6837fe9ba0c62 [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 */
Peter Maydelld38ea872016-01-29 17:50:05 +000024#include "qemu/osdep.h"
blueswir1511d2b12009-03-07 15:32:56 +000025#include <dirent.h>
Paolo Bonzini33c11872016-03-15 16:58:45 +010026#include "qemu-common.h"
27#include "cpu.h"
pbrook87ecb682007-11-17 17:14:51 +000028#include "hw/hw.h"
Paolo Bonzinib4a42f82013-02-04 11:37:52 +010029#include "monitor/qdev.h"
pbrook87ecb682007-11-17 17:14:51 +000030#include "hw/usb.h"
Michael S. Tsirkina2cb15b2012-12-12 14:24:50 +020031#include "hw/pci/pci.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010032#include "sysemu/watchdog.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +020033#include "hw/loader.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010034#include "exec/gdbstub.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020035#include "net/net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000036#include "net/slirp.h"
Marc-André Lureau4d43a602017-01-26 18:26:44 +040037#include "chardev/char-fe.h"
Gerd Hoffmann75721502010-10-07 12:22:54 +020038#include "ui/qemu-spice.h"
Eduardo Habkoste35704b2015-02-08 16:51:16 -020039#include "sysemu/numa.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010040#include "monitor/monitor.h"
Marc-André Lureau213dcb02016-12-12 20:22:24 +030041#include "qemu/config-file.h"
Stefan Hajnoczi0150cd82013-11-14 11:54:15 +010042#include "qemu/readline.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010043#include "ui/console.h"
Gerd Hoffmannc751a742013-12-04 15:02:28 +010044#include "ui/input.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010045#include "sysemu/blockdev.h"
Max Reitzda27a002016-03-16 19:54:29 +010046#include "sysemu/block-backend.h"
pbrook87ecb682007-11-17 17:14:51 +000047#include "audio/audio.h"
Paolo Bonzini76cad712012-10-24 11:12:21 +020048#include "disas/disas.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010049#include "sysemu/balloon.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010050#include "qemu/timer.h"
Vincent Palatinb3946622017-01-10 11:59:55 +010051#include "sysemu/hw_accel.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010052#include "qemu/acl.h"
Paolo Bonzinibdee56f2013-04-02 18:28:41 +020053#include "sysemu/tpm.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010054#include "qapi/qmp/qerror.h"
Eric Blakec7eb39c2016-06-09 10:48:32 -060055#include "qapi/qmp/types.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010056#include "qapi/qmp/qjson.h"
57#include "qapi/qmp/json-streamer.h"
58#include "qapi/qmp/json-parser.h"
Markus Armbrustera9c94272016-06-22 19:11:19 +020059#include "qom/object_interfaces.h"
Daniel P. Berrange0ab8ed12017-01-25 16:14:15 +000060#include "trace-root.h"
Lluís31965ae2011-08-31 20:31:24 +020061#include "trace/control.h"
Pavel Butsykinbf957282015-09-10 18:38:59 +030062#include "monitor/hmp-target.h"
Lluís6d8a7642011-08-31 20:30:43 +020063#ifdef CONFIG_TRACE_SIMPLE
Lluís31965ae2011-08-31 20:31:24 +020064#include "trace/simple.h"
Prerna Saxena22890ab2010-06-24 17:04:53 +053065#endif
Paolo Bonzini022c62c2012-12-17 18:19:49 +010066#include "exec/memory.h"
Paolo Bonzini63c91552016-03-15 13:18:37 +010067#include "exec/exec-all.h"
Paolo Bonzini03dd0242015-12-15 13:16:16 +010068#include "qemu/log.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050069#include "qmp-commands.h"
70#include "hmp.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010071#include "qemu/thread.h"
Hani Benhabilesb21631f2014-05-27 23:39:37 +010072#include "block/qapi.h"
Wenchao Xia43a14cf2014-06-18 08:43:31 +020073#include "qapi/qmp-event.h"
74#include "qapi-event.h"
Markus Armbruster39a18152015-09-16 13:06:28 +020075#include "qmp-introspect.h"
Alberto Garciadc599972016-03-10 13:55:26 +020076#include "sysemu/qtest.h"
Paolo Bonzinid2528bd2017-03-03 12:01:16 +010077#include "sysemu/cpus.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020078#include "qemu/cutils.h"
Marc-André Lureauedcfaef2016-09-12 13:19:00 +040079#include "qapi/qmp/dispatch.h"
ths6a5bd302007-12-03 17:05:38 +000080
Jason J. Hernea4538a52015-06-26 14:07:21 -040081#if defined(TARGET_S390X)
82#include "hw/s390x/storage-keys.h"
Claudio Imbrendaf860d492016-08-15 18:44:04 +020083#include "hw/s390x/storage-attributes.h"
Jason J. Hernea4538a52015-06-26 14:07:21 -040084#endif
85
bellard9307c4c2004-04-04 12:57:25 +000086/*
87 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000088 *
bellard9307c4c2004-04-04 12:57:25 +000089 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000090 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000091 * 's' string (accept optional quote)
Wenchao Xia129be002013-08-27 20:38:26 +080092 * 'S' it just appends the rest of the string (accept optional quote)
Markus Armbruster361127d2010-02-10 20:24:35 +010093 * 'O' option string of the form NAME=VALUE,...
94 * parsed according to QemuOptsList given by its name
95 * Example: 'device:O' uses qemu_device_opts.
96 * Restriction: only lists with empty desc are supported
97 * TODO lift the restriction
bellard92a31b12005-02-10 22:00:52 +000098 * 'i' 32 bit integer
99 * 'l' target long (32 or 64 bit)
Luiz Capitulino91162842012-04-26 17:34:30 -0300100 * 'M' Non-negative target long (32 or 64 bit), in user mode the
101 * value is multiplied by 2^20 (think Mebibyte)
Jes Sorensendbc0c672010-10-21 17:15:47 +0200102 * 'o' octets (aka bytes)
Kevin Wolf5e009842013-06-05 14:19:27 +0200103 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
104 * K, k suffix, which multiplies the value by 2^60 for suffixes E
105 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
106 * 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 +0100107 * 'T' double
108 * user mode accepts an optional ms, us, ns suffix,
109 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +0000110 * '/' optional gdb-like print format (like "/10x")
111 *
Luiz Capitulinofb466602009-08-28 15:27:27 -0300112 * '?' optional type (for all types, except '/')
113 * '.' other form of optional type (for 'i' and 'l')
Markus Armbruster942cd1f2010-03-26 09:07:09 +0100114 * 'b' boolean
115 * user mode accepts "on" or "off"
Luiz Capitulinofb466602009-08-28 15:27:27 -0300116 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +0000117 *
118 */
119
Anthony Liguoric227f092009-10-01 16:12:16 -0500120typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +0000121 const char *name;
bellard9307c4c2004-04-04 12:57:25 +0000122 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +0000123 const char *params;
124 const char *help;
Marc-André Lureau2b9e3572016-09-12 13:19:06 +0400125 void (*cmd)(Monitor *mon, const QDict *qdict);
126 /* @sub_table is a list of 2nd level of commands. If it does not exist,
127 * cmd should be used. If it exists, sub_table[?].cmd should be
128 * used, and cmd of 1st level plays the role of help function.
Wenchao Xia5f3d3352013-01-14 14:06:27 +0800129 */
130 struct mon_cmd_t *sub_table;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +0100131 void (*command_completion)(ReadLineState *rs, int nb_args, const char *str);
Anthony Liguoric227f092009-10-01 16:12:16 -0500132} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000133
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100134/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500135typedef struct mon_fd_t mon_fd_t;
136struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100137 char *name;
138 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500139 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100140};
141
Corey Bryantba1c0482012-08-14 16:43:43 -0400142/* file descriptor associated with a file descriptor set */
143typedef struct MonFdsetFd MonFdsetFd;
144struct MonFdsetFd {
145 int fd;
146 bool removed;
147 char *opaque;
148 QLIST_ENTRY(MonFdsetFd) next;
149};
150
151/* file descriptor set containing fds passed via SCM_RIGHTS */
152typedef struct MonFdset MonFdset;
153struct MonFdset {
154 int64_t id;
155 QLIST_HEAD(, MonFdsetFd) fds;
Corey Bryantadb696f2012-08-14 16:43:47 -0400156 QLIST_HEAD(, MonFdsetFd) dup_fds;
Corey Bryantba1c0482012-08-14 16:43:43 -0400157 QLIST_ENTRY(MonFdset) next;
158};
159
Markus Armbruster74358f22015-03-06 19:35:59 +0100160typedef struct {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200161 JSONMessageParser parser;
Markus Armbrusterf994b252015-03-06 19:51:51 +0100162 /*
163 * When a client connects, we're in capabilities negotiation mode.
164 * When command qmp_capabilities succeeds, we go into command
165 * mode.
166 */
Markus Armbruster635db182017-03-03 13:32:27 +0100167 QmpCommandList *commands;
Markus Armbruster74358f22015-03-06 19:35:59 +0100168} MonitorQMP;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200169
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100170/*
171 * To prevent flooding clients, events can be throttled. The
172 * throttling is calculated globally, rather than per-Monitor
173 * instance.
174 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200175typedef struct MonitorQAPIEventState {
Markus Armbruster7de0be62015-10-15 17:08:35 +0200176 QAPIEvent event; /* Throttling state for this event type and... */
177 QDict *data; /* ... data, see qapi_event_throttle_equal() */
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100178 QEMUTimer *timer; /* Timer for handling delayed events */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200179 QDict *qdict; /* Delayed event (if any) */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200180} MonitorQAPIEventState;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100181
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200182typedef struct {
183 int64_t rate; /* Minimum time (in ns) between two events */
184} MonitorQAPIEventConf;
185
aliguori87127162009-03-05 23:01:29 +0000186struct Monitor {
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300187 CharBackend chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200188 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000189 int flags;
190 int suspend_cnt;
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400191 bool skip_flush;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200192
193 QemuMutex out_lock;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400194 QString *outbuf;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200195 guint out_watch;
196
197 /* Read under either BQL or out_lock, written with BQL+out_lock. */
198 int mux_out;
199
aliguori731b0362009-03-05 23:01:42 +0000200 ReadLineState *rs;
Markus Armbruster74358f22015-03-06 19:35:59 +0100201 MonitorQMP qmp;
Greg Kurz751f8cf2017-10-17 10:16:22 +0200202 gchar *mon_cpu_path;
Markus Armbruster097310b2014-10-07 13:59:15 +0200203 BlockCompletionFunc *password_completion_cb;
aliguori731b0362009-03-05 23:01:42 +0000204 void *password_opaque;
Wenchao Xia77172392013-08-27 20:38:20 +0800205 mon_cmd_t *cmd_table;
Anthony Liguoric227f092009-10-01 16:12:16 -0500206 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000207 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000208};
209
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300210/* QMP checker flags */
211#define QMP_ACCEPT_UNKNOWNS 1
212
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200213/* Protects mon_list, monitor_event_state. */
214static QemuMutex monitor_lock;
215
Blue Swirl72cf2d42009-09-12 07:36:22 +0000216static QLIST_HEAD(mon_list, Monitor) mon_list;
Corey Bryantba1c0482012-08-14 16:43:43 -0400217static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
Corey Bryantefb87c12012-08-14 16:43:48 -0400218static int mon_refcount;
bellard7e2515e2004-08-01 21:52:19 +0000219
Wayne Xia816f8922011-10-12 11:32:41 +0800220static mon_cmd_t mon_cmds[];
221static mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000222
Markus Armbruster635db182017-03-03 13:32:27 +0100223QmpCommandList qmp_commands, qmp_cap_negotiation_commands;
Markus Armbruster1527bad2017-03-03 13:32:25 +0100224
Markus Armbruster8631b602010-02-18 11:41:55 +0100225Monitor *cur_mon;
aliguori376253e2009-03-05 23:01:23 +0000226
Alberto Garciadc599972016-03-10 13:55:26 +0200227static QEMUClockType event_clock_type = QEMU_CLOCK_REALTIME;
228
Stefan Hajnoczic60bf332013-11-14 11:54:14 +0100229static void monitor_command_cb(void *opaque, const char *cmdline,
230 void *readline_opaque);
aliguori83ab7952008-08-19 14:44:22 +0000231
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100232/**
233 * Is @mon a QMP monitor?
234 */
235static inline bool monitor_is_qmp(const Monitor *mon)
Luiz Capitulino418173c2009-11-26 22:58:51 -0200236{
237 return (mon->flags & MONITOR_USE_CONTROL);
238}
239
Markus Armbruster489653b2015-03-06 20:01:05 +0100240/**
241 * Is the current monitor, if any, a QMP monitor?
242 */
243bool monitor_cur_is_qmp(void)
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100244{
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100245 return cur_mon && monitor_is_qmp(cur_mon);
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100246}
247
Anthony Liguori7060b472011-09-02 12:34:50 -0500248void monitor_read_command(Monitor *mon, int show_prompt)
aliguori731b0362009-03-05 23:01:42 +0000249{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200250 if (!mon->rs)
251 return;
252
aliguori731b0362009-03-05 23:01:42 +0000253 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
254 if (show_prompt)
255 readline_show_prompt(mon->rs);
256}
bellard6a00d602005-11-21 23:25:50 +0000257
Anthony Liguori7060b472011-09-02 12:34:50 -0500258int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
259 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000260{
Markus Armbrusterbcf5d192015-03-12 17:26:46 +0100261 if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000262 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
263 /* prompt is printed on return from the command handler */
264 return 0;
265 } else {
266 monitor_printf(mon, "terminal does not support password prompting\n");
267 return -ENOTTY;
268 }
aliguoribb5fc202009-03-05 23:01:15 +0000269}
270
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200271static void monitor_flush_locked(Monitor *mon);
272
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100273static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
274 void *opaque)
275{
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200276 Monitor *mon = opaque;
277
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200278 qemu_mutex_lock(&mon->out_lock);
279 mon->out_watch = 0;
280 monitor_flush_locked(mon);
281 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100282 return FALSE;
283}
284
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200285/* Called with mon->out_lock held. */
286static void monitor_flush_locked(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000287{
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100288 int rc;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400289 size_t len;
290 const char *buf;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100291
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400292 if (mon->skip_flush) {
293 return;
294 }
295
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400296 buf = qstring_get_str(mon->outbuf);
297 len = qstring_get_length(mon->outbuf);
298
Paolo Bonzinia4cc73d2013-05-31 14:00:27 +0200299 if (len && !mon->mux_out) {
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300300 rc = qemu_chr_fe_write(&mon->chr, (const uint8_t *) buf, len);
Stratos Psomadakis056f49f2014-01-27 12:30:15 +0200301 if ((rc < 0 && errno != EAGAIN) || (rc == len)) {
302 /* all flushed or error */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400303 QDECREF(mon->outbuf);
304 mon->outbuf = qstring_new();
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100305 return;
306 }
307 if (rc > 0) {
Eric Blake3b7c78c2016-05-17 16:00:15 -0600308 /* partial write */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400309 QString *tmp = qstring_from_str(buf + rc);
310 QDECREF(mon->outbuf);
311 mon->outbuf = tmp;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100312 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200313 if (mon->out_watch == 0) {
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300314 mon->out_watch =
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300315 qemu_chr_fe_add_watch(&mon->chr, G_IO_OUT | G_IO_HUP,
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300316 monitor_unblocked, mon);
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200317 }
bellard7e2515e2004-08-01 21:52:19 +0000318 }
319}
320
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200321void monitor_flush(Monitor *mon)
322{
323 qemu_mutex_lock(&mon->out_lock);
324 monitor_flush_locked(mon);
325 qemu_mutex_unlock(&mon->out_lock);
326}
327
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400328/* flush at every end of line */
aliguori376253e2009-03-05 23:01:23 +0000329static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000330{
ths60fe76f2007-12-16 03:02:09 +0000331 char c;
aliguori731b0362009-03-05 23:01:42 +0000332
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200333 qemu_mutex_lock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000334 for(;;) {
335 c = *str++;
336 if (c == '\0')
337 break;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400338 if (c == '\n') {
339 qstring_append_chr(mon->outbuf, '\r');
340 }
341 qstring_append_chr(mon->outbuf, c);
342 if (c == '\n') {
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200343 monitor_flush_locked(mon);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400344 }
bellard7e2515e2004-08-01 21:52:19 +0000345 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200346 qemu_mutex_unlock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000347}
348
aliguori376253e2009-03-05 23:01:23 +0000349void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000350{
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400351 char *buf;
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200352
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200353 if (!mon)
354 return;
355
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100356 if (monitor_is_qmp(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200357 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200358 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200359
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400360 buf = g_strdup_vprintf(fmt, ap);
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200361 monitor_puts(mon, buf);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400362 g_free(buf);
bellard7e2515e2004-08-01 21:52:19 +0000363}
364
aliguori376253e2009-03-05 23:01:23 +0000365void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000366{
367 va_list ap;
368 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000369 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000370 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000371}
372
Pavel Butsykincaf15312015-09-22 16:18:17 +0300373int monitor_fprintf(FILE *stream, const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000374{
375 va_list ap;
376 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000377 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000378 va_end(ap);
379 return 0;
380}
381
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200382static void monitor_json_emitter(Monitor *mon, const QObject *data)
383{
384 QString *json;
385
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200386 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
387 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200388 assert(json != NULL);
389
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200390 qstring_append_chr(json, '\n');
391 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200392
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200393 QDECREF(json);
394}
395
Eric Blake7fb1cf12015-11-18 01:52:57 -0700396static MonitorQAPIEventConf monitor_qapi_event_conf[QAPI_EVENT__MAX] = {
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200397 /* Limit guest-triggerable events to 1 per second */
398 [QAPI_EVENT_RTC_CHANGE] = { 1000 * SCALE_MS },
399 [QAPI_EVENT_WATCHDOG] = { 1000 * SCALE_MS },
400 [QAPI_EVENT_BALLOON_CHANGE] = { 1000 * SCALE_MS },
401 [QAPI_EVENT_QUORUM_REPORT_BAD] = { 1000 * SCALE_MS },
402 [QAPI_EVENT_QUORUM_FAILURE] = { 1000 * SCALE_MS },
403 [QAPI_EVENT_VSERPORT_CHANGE] = { 1000 * SCALE_MS },
404};
405
Markus Armbrustera24712a2015-10-15 17:08:34 +0200406GHashTable *monitor_qapi_event_state;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100407
408/*
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200409 * Emits the event to every monitor instance, @event is only used for trace
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200410 * Called with monitor_lock held.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100411 */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200412static void monitor_qapi_event_emit(QAPIEvent event, QDict *qdict)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100413{
414 Monitor *mon;
415
Markus Armbruster688b4b72015-10-15 17:08:30 +0200416 trace_monitor_protocol_event_emit(event, qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100417 QLIST_FOREACH(mon, &mon_list, entry) {
Markus Armbruster635db182017-03-03 13:32:27 +0100418 if (monitor_is_qmp(mon)
419 && mon->qmp.commands != &qmp_cap_negotiation_commands) {
Markus Armbruster688b4b72015-10-15 17:08:30 +0200420 monitor_json_emitter(mon, QOBJECT(qdict));
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100421 }
422 }
423}
424
Markus Armbrustera24712a2015-10-15 17:08:34 +0200425static void monitor_qapi_event_handler(void *opaque);
426
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100427/*
428 * Queue a new event for emission to Monitor instances,
429 * applying any rate limiting if required.
430 */
431static void
Markus Armbruster688b4b72015-10-15 17:08:30 +0200432monitor_qapi_event_queue(QAPIEvent event, QDict *qdict, Error **errp)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100433{
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200434 MonitorQAPIEventConf *evconf;
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200435 MonitorQAPIEventState *evstate;
Markus Armbruster93f8f982015-10-15 17:08:31 +0200436
Eric Blake7fb1cf12015-11-18 01:52:57 -0700437 assert(event < QAPI_EVENT__MAX);
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200438 evconf = &monitor_qapi_event_conf[event];
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200439 trace_monitor_protocol_event_queue(event, qdict, evconf->rate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100440
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200441 qemu_mutex_lock(&monitor_lock);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200442
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200443 if (!evconf->rate) {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200444 /* Unthrottled event */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200445 monitor_qapi_event_emit(event, qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100446 } else {
Markus Armbruster7de0be62015-10-15 17:08:35 +0200447 QDict *data = qobject_to_qdict(qdict_get(qdict, "data"));
448 MonitorQAPIEventState key = { .event = event, .data = data };
Markus Armbrustera24712a2015-10-15 17:08:34 +0200449
450 evstate = g_hash_table_lookup(monitor_qapi_event_state, &key);
451 assert(!evstate || timer_pending(evstate->timer));
452
453 if (evstate) {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200454 /*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200455 * Timer is pending for (at least) evconf->rate ns after
Markus Armbruster93f8f982015-10-15 17:08:31 +0200456 * last send. Store event for sending when timer fires,
457 * replacing a prior stored event if any.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100458 */
Markus Armbruster93f8f982015-10-15 17:08:31 +0200459 QDECREF(evstate->qdict);
Markus Armbruster688b4b72015-10-15 17:08:30 +0200460 evstate->qdict = qdict;
461 QINCREF(evstate->qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100462 } else {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200463 /*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200464 * Last send was (at least) evconf->rate ns ago.
Markus Armbruster93f8f982015-10-15 17:08:31 +0200465 * Send immediately, and arm the timer to call
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200466 * monitor_qapi_event_handler() in evconf->rate ns. Any
Markus Armbruster93f8f982015-10-15 17:08:31 +0200467 * events arriving before then will be delayed until then.
468 */
Alberto Garciadc599972016-03-10 13:55:26 +0200469 int64_t now = qemu_clock_get_ns(event_clock_type);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200470
Markus Armbruster688b4b72015-10-15 17:08:30 +0200471 monitor_qapi_event_emit(event, qdict);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200472
473 evstate = g_new(MonitorQAPIEventState, 1);
474 evstate->event = event;
Markus Armbruster7de0be62015-10-15 17:08:35 +0200475 evstate->data = data;
476 QINCREF(evstate->data);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200477 evstate->qdict = NULL;
Alberto Garciadc599972016-03-10 13:55:26 +0200478 evstate->timer = timer_new_ns(event_clock_type,
Markus Armbrustera24712a2015-10-15 17:08:34 +0200479 monitor_qapi_event_handler,
480 evstate);
481 g_hash_table_add(monitor_qapi_event_state, evstate);
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200482 timer_mod_ns(evstate->timer, now + evconf->rate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100483 }
484 }
Markus Armbruster93f8f982015-10-15 17:08:31 +0200485
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200486 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100487}
488
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100489/*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200490 * This function runs evconf->rate ns after sending a throttled
Markus Armbruster93f8f982015-10-15 17:08:31 +0200491 * event.
492 * If another event has since been stored, send it.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100493 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200494static void monitor_qapi_event_handler(void *opaque)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100495{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200496 MonitorQAPIEventState *evstate = opaque;
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200497 MonitorQAPIEventConf *evconf = &monitor_qapi_event_conf[evstate->event];
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100498
Markus Armbruster93f8f982015-10-15 17:08:31 +0200499 trace_monitor_protocol_event_handler(evstate->event, evstate->qdict);
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200500 qemu_mutex_lock(&monitor_lock);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200501
Markus Armbruster688b4b72015-10-15 17:08:30 +0200502 if (evstate->qdict) {
Alberto Garciadc599972016-03-10 13:55:26 +0200503 int64_t now = qemu_clock_get_ns(event_clock_type);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200504
Markus Armbruster688b4b72015-10-15 17:08:30 +0200505 monitor_qapi_event_emit(evstate->event, evstate->qdict);
506 QDECREF(evstate->qdict);
507 evstate->qdict = NULL;
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200508 timer_mod_ns(evstate->timer, now + evconf->rate);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200509 } else {
510 g_hash_table_remove(monitor_qapi_event_state, evstate);
Markus Armbruster7de0be62015-10-15 17:08:35 +0200511 QDECREF(evstate->data);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200512 timer_free(evstate->timer);
513 g_free(evstate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100514 }
Markus Armbruster93f8f982015-10-15 17:08:31 +0200515
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200516 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100517}
518
Markus Armbrustera24712a2015-10-15 17:08:34 +0200519static unsigned int qapi_event_throttle_hash(const void *key)
520{
521 const MonitorQAPIEventState *evstate = key;
Markus Armbruster7de0be62015-10-15 17:08:35 +0200522 unsigned int hash = evstate->event * 255;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200523
Markus Armbruster7de0be62015-10-15 17:08:35 +0200524 if (evstate->event == QAPI_EVENT_VSERPORT_CHANGE) {
525 hash += g_str_hash(qdict_get_str(evstate->data, "id"));
526 }
527
Alberto Garcia6d425eb2016-03-10 13:55:25 +0200528 if (evstate->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
529 hash += g_str_hash(qdict_get_str(evstate->data, "node-name"));
530 }
531
Markus Armbruster7de0be62015-10-15 17:08:35 +0200532 return hash;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200533}
534
535static gboolean qapi_event_throttle_equal(const void *a, const void *b)
536{
537 const MonitorQAPIEventState *eva = a;
538 const MonitorQAPIEventState *evb = b;
539
Markus Armbruster7de0be62015-10-15 17:08:35 +0200540 if (eva->event != evb->event) {
541 return FALSE;
542 }
543
544 if (eva->event == QAPI_EVENT_VSERPORT_CHANGE) {
545 return !strcmp(qdict_get_str(eva->data, "id"),
546 qdict_get_str(evb->data, "id"));
547 }
548
Alberto Garcia6d425eb2016-03-10 13:55:25 +0200549 if (eva->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
550 return !strcmp(qdict_get_str(eva->data, "node-name"),
551 qdict_get_str(evb->data, "node-name"));
552 }
553
Markus Armbruster7de0be62015-10-15 17:08:35 +0200554 return TRUE;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200555}
556
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200557static void monitor_qapi_event_init(void)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100558{
Alberto Garciadc599972016-03-10 13:55:26 +0200559 if (qtest_enabled()) {
560 event_clock_type = QEMU_CLOCK_VIRTUAL;
561 }
562
Markus Armbrustera24712a2015-10-15 17:08:34 +0200563 monitor_qapi_event_state = g_hash_table_new(qapi_event_throttle_hash,
564 qapi_event_throttle_equal);
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200565 qmp_event_set_func_emit(monitor_qapi_event_queue);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200566}
567
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100568static void handle_hmp_command(Monitor *mon, const char *cmdline);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200569
Wenchao Xiab01fe892013-08-27 20:38:19 +0800570static void monitor_data_init(Monitor *mon)
571{
572 memset(mon, 0, sizeof(Monitor));
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200573 qemu_mutex_init(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800574 mon->outbuf = qstring_new();
Wenchao Xia77172392013-08-27 20:38:20 +0800575 /* Use *mon_cmds by default. */
576 mon->cmd_table = mon_cmds;
Wenchao Xiab01fe892013-08-27 20:38:19 +0800577}
578
579static void monitor_data_destroy(Monitor *mon)
580{
Greg Kurz751f8cf2017-10-17 10:16:22 +0200581 g_free(mon->mon_cpu_path);
Marc-André Lureau1ce26102017-01-27 00:49:13 +0400582 qemu_chr_fe_deinit(&mon->chr, false);
Marc-André Lureau2ef45712016-08-01 15:23:42 +0400583 if (monitor_is_qmp(mon)) {
584 json_message_parser_destroy(&mon->qmp.parser);
585 }
Marc-André Lureaue5dc1a62018-01-04 17:05:15 +0100586 readline_free(mon->rs);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800587 QDECREF(mon->outbuf);
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200588 qemu_mutex_destroy(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800589}
590
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200591char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
592 int64_t cpu_index, Error **errp)
Luiz Capitulino0268d972010-10-22 10:08:02 -0200593{
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200594 char *output = NULL;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200595 Monitor *old_mon, hmp;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200596
Wenchao Xiab01fe892013-08-27 20:38:19 +0800597 monitor_data_init(&hmp);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400598 hmp.skip_flush = true;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200599
600 old_mon = cur_mon;
601 cur_mon = &hmp;
602
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200603 if (has_cpu_index) {
604 int ret = monitor_set_cpu(cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200605 if (ret < 0) {
606 cur_mon = old_mon;
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100607 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
608 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200609 goto out;
610 }
611 }
612
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100613 handle_hmp_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200614 cur_mon = old_mon;
615
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200616 qemu_mutex_lock(&hmp.out_lock);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400617 if (qstring_get_length(hmp.outbuf) > 0) {
618 output = g_strdup(qstring_get_str(hmp.outbuf));
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200619 } else {
620 output = g_strdup("");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200621 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200622 qemu_mutex_unlock(&hmp.out_lock);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200623
624out:
Wenchao Xiab01fe892013-08-27 20:38:19 +0800625 monitor_data_destroy(&hmp);
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200626 return output;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200627}
628
bellard9dc39cb2004-03-14 21:38:27 +0000629static int compare_cmd(const char *name, const char *list)
630{
631 const char *p, *pstart;
632 int len;
633 len = strlen(name);
634 p = list;
635 for(;;) {
636 pstart = p;
637 p = strchr(p, '|');
638 if (!p)
639 p = pstart + strlen(pstart);
640 if ((p - pstart) == len && !memcmp(pstart, name, len))
641 return 1;
642 if (*p == '\0')
643 break;
644 p++;
645 }
646 return 0;
647}
648
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800649static int get_str(char *buf, int buf_size, const char **pp)
650{
651 const char *p;
652 char *q;
653 int c;
654
655 q = buf;
656 p = *pp;
657 while (qemu_isspace(*p)) {
658 p++;
659 }
660 if (*p == '\0') {
661 fail:
662 *q = '\0';
663 *pp = p;
664 return -1;
665 }
666 if (*p == '\"') {
667 p++;
668 while (*p != '\0' && *p != '\"') {
669 if (*p == '\\') {
670 p++;
671 c = *p++;
672 switch (c) {
673 case 'n':
674 c = '\n';
675 break;
676 case 'r':
677 c = '\r';
678 break;
679 case '\\':
680 case '\'':
681 case '\"':
682 break;
683 default:
Peter Maydell71baf782015-08-19 16:20:19 +0100684 printf("unsupported escape code: '\\%c'\n", c);
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800685 goto fail;
686 }
687 if ((q - buf) < buf_size - 1) {
688 *q++ = c;
689 }
690 } else {
691 if ((q - buf) < buf_size - 1) {
692 *q++ = *p;
693 }
694 p++;
695 }
696 }
697 if (*p != '\"') {
Peter Maydell71baf782015-08-19 16:20:19 +0100698 printf("unterminated string\n");
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800699 goto fail;
700 }
701 p++;
702 } else {
703 while (*p != '\0' && !qemu_isspace(*p)) {
704 if ((q - buf) < buf_size - 1) {
705 *q++ = *p;
706 }
707 p++;
708 }
709 }
710 *q = '\0';
711 *pp = p;
712 return 0;
713}
714
715#define MAX_ARGS 16
716
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800717static void free_cmdline_args(char **args, int nb_args)
718{
719 int i;
720
721 assert(nb_args <= MAX_ARGS);
722
723 for (i = 0; i < nb_args; i++) {
724 g_free(args[i]);
725 }
726
727}
728
729/*
730 * Parse the command line to get valid args.
731 * @cmdline: command line to be parsed.
732 * @pnb_args: location to store the number of args, must NOT be NULL.
733 * @args: location to store the args, which should be freed by caller, must
734 * NOT be NULL.
735 *
736 * Returns 0 on success, negative on failure.
737 *
738 * NOTE: this parser is an approximate form of the real command parser. Number
739 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
740 * return with failure.
741 */
742static int parse_cmdline(const char *cmdline,
743 int *pnb_args, char **args)
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800744{
745 const char *p;
746 int nb_args, ret;
747 char buf[1024];
748
749 p = cmdline;
750 nb_args = 0;
751 for (;;) {
752 while (qemu_isspace(*p)) {
753 p++;
754 }
755 if (*p == '\0') {
756 break;
757 }
758 if (nb_args >= MAX_ARGS) {
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800759 goto fail;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800760 }
761 ret = get_str(buf, sizeof(buf), &p);
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800762 if (ret < 0) {
763 goto fail;
764 }
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800765 args[nb_args] = g_strdup(buf);
766 nb_args++;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800767 }
768 *pnb_args = nb_args;
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800769 return 0;
770
771 fail:
772 free_cmdline_args(args, nb_args);
773 return -1;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800774}
775
Wenchao Xia66855492013-08-27 20:38:23 +0800776static void help_cmd_dump_one(Monitor *mon,
777 const mon_cmd_t *cmd,
778 char **prefix_args,
779 int prefix_args_nb)
780{
781 int i;
782
783 for (i = 0; i < prefix_args_nb; i++) {
784 monitor_printf(mon, "%s ", prefix_args[i]);
785 }
786 monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
787}
788
789/* @args[@arg_index] is the valid command need to find in @cmds */
Anthony Liguoric227f092009-10-01 16:12:16 -0500790static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
Wenchao Xia66855492013-08-27 20:38:23 +0800791 char **args, int nb_args, int arg_index)
bellard9dc39cb2004-03-14 21:38:27 +0000792{
Anthony Liguoric227f092009-10-01 16:12:16 -0500793 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000794
Wenchao Xia66855492013-08-27 20:38:23 +0800795 /* No valid arg need to compare with, dump all in *cmds */
796 if (arg_index >= nb_args) {
797 for (cmd = cmds; cmd->name != NULL; cmd++) {
798 help_cmd_dump_one(mon, cmd, args, arg_index);
799 }
800 return;
801 }
802
803 /* Find one entry to dump */
804 for (cmd = cmds; cmd->name != NULL; cmd++) {
805 if (compare_cmd(args[arg_index], cmd->name)) {
806 if (cmd->sub_table) {
807 /* continue with next arg */
808 help_cmd_dump(mon, cmd->sub_table,
809 args, nb_args, arg_index + 1);
810 } else {
811 help_cmd_dump_one(mon, cmd, args, arg_index);
812 }
813 break;
814 }
bellard9dc39cb2004-03-14 21:38:27 +0000815 }
816}
817
aliguori376253e2009-03-05 23:01:23 +0000818static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000819{
Wenchao Xia66855492013-08-27 20:38:23 +0800820 char *args[MAX_ARGS];
821 int nb_args = 0;
822
823 /* 1. parse user input */
824 if (name) {
825 /* special case for log, directly dump and return */
826 if (!strcmp(name, "log")) {
Peter Maydell38dad9e2013-02-11 16:41:25 +0000827 const QEMULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000828 monitor_printf(mon, "Log items (comma separated):\n");
829 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
Peter Maydell38dad9e2013-02-11 16:41:25 +0000830 for (item = qemu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000831 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000832 }
Wenchao Xia66855492013-08-27 20:38:23 +0800833 return;
834 }
835
836 if (parse_cmdline(name, &nb_args, args) < 0) {
837 return;
bellardf193c792004-03-21 17:06:25 +0000838 }
bellard9dc39cb2004-03-14 21:38:27 +0000839 }
Wenchao Xia66855492013-08-27 20:38:23 +0800840
841 /* 2. dump the contents according to parsed args */
842 help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0);
843
844 free_cmdline_args(args, nb_args);
bellard9dc39cb2004-03-14 21:38:27 +0000845}
846
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300847static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300848{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300849 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300850}
851
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100852static void hmp_trace_event(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530853{
854 const char *tp_name = qdict_get_str(qdict, "name");
855 bool new_state = qdict_get_bool(qdict, "option");
Lluís Vilanova77e2b172016-07-11 12:53:57 +0200856 bool has_vcpu = qdict_haskey(qdict, "vcpu");
857 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
Lluís Vilanova14101d02014-08-25 13:20:03 +0200858 Error *local_err = NULL;
Blue Swirlf871d682010-10-13 19:14:29 +0000859
Lluís Vilanova77e2b172016-07-11 12:53:57 +0200860 if (vcpu < 0) {
861 monitor_printf(mon, "argument vcpu must be positive");
862 return;
863 }
864
865 qmp_trace_event_set_state(tp_name, new_state, true, true, has_vcpu, vcpu, &local_err);
Lluís Vilanova14101d02014-08-25 13:20:03 +0200866 if (local_err) {
Markus Armbruster091e38b2015-02-10 15:15:43 +0100867 error_report_err(local_err);
Blue Swirlf871d682010-10-13 19:14:29 +0000868 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530869}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100870
Michael Rothc45a8162011-10-02 08:44:37 -0500871#ifdef CONFIG_TRACE_SIMPLE
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100872static void hmp_trace_file(Monitor *mon, const QDict *qdict)
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100873{
874 const char *op = qdict_get_try_str(qdict, "op");
875 const char *arg = qdict_get_try_str(qdict, "arg");
876
877 if (!op) {
878 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
879 } else if (!strcmp(op, "on")) {
880 st_set_trace_file_enabled(true);
881 } else if (!strcmp(op, "off")) {
882 st_set_trace_file_enabled(false);
883 } else if (!strcmp(op, "flush")) {
884 st_flush_trace_buffer();
885 } else if (!strcmp(op, "set")) {
886 if (arg) {
887 st_set_trace_file(arg);
888 }
889 } else {
890 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
891 help_cmd(mon, "trace-file");
892 }
893}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530894#endif
895
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100896static void hmp_info_help(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000897{
Luiz Capitulino13c74252009-10-07 13:41:55 -0300898 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000899}
900
Marc-André Lureau9e812b62016-09-12 13:19:05 +0400901static void query_commands_cb(QmpCommand *cmd, void *opaque)
bellard9bc9d1c2004-10-10 15:15:51 +0000902{
Marc-André Lureau9e812b62016-09-12 13:19:05 +0400903 CommandInfoList *info, **list = opaque;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200904
Marc-André Lureau9e812b62016-09-12 13:19:05 +0400905 if (!cmd->enabled) {
906 return;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200907 }
908
Marc-André Lureau9e812b62016-09-12 13:19:05 +0400909 info = g_malloc0(sizeof(*info));
910 info->value = g_malloc0(sizeof(*info->value));
911 info->value->name = g_strdup(cmd->name);
912 info->next = *list;
913 *list = info;
914}
915
916CommandInfoList *qmp_query_commands(Error **errp)
917{
918 CommandInfoList *list = NULL;
919
Markus Armbruster635db182017-03-03 13:32:27 +0100920 qmp_for_each_command(cur_mon->qmp.commands, query_commands_cb, &list);
Marc-André Lureau9e812b62016-09-12 13:19:05 +0400921
922 return list;
thsa36e69d2007-12-02 05:18:19 +0000923}
924
Daniel P. Berrange48608532012-05-21 17:59:51 +0100925EventInfoList *qmp_query_events(Error **errp)
926{
927 EventInfoList *info, *ev_list = NULL;
Wenchao Xia75175172014-06-18 08:43:54 +0200928 QAPIEvent e;
Daniel P. Berrange48608532012-05-21 17:59:51 +0100929
Eric Blake7fb1cf12015-11-18 01:52:57 -0700930 for (e = 0 ; e < QAPI_EVENT__MAX ; e++) {
Markus Armbruster977c7362017-08-24 10:46:08 +0200931 const char *event_name = QAPIEvent_str(e);
Daniel P. Berrange48608532012-05-21 17:59:51 +0100932 assert(event_name != NULL);
933 info = g_malloc0(sizeof(*info));
934 info->value = g_malloc0(sizeof(*info->value));
935 info->value->name = g_strdup(event_name);
936
937 info->next = ev_list;
938 ev_list = info;
939 }
940
941 return ev_list;
942}
943
Markus Armbruster39a18152015-09-16 13:06:28 +0200944/*
945 * Minor hack: generated marshalling suppressed for this command
946 * ('gen': false in the schema) so we can parse the JSON string
947 * directly into QObject instead of first parsing it with
948 * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
949 * to QObject with generated output marshallers, every time. Instead,
Daniel P. Berrangeb3db2112016-09-30 15:45:27 +0100950 * we do it in test-qobject-input-visitor.c, just to make sure
Markus Armbruster39a18152015-09-16 13:06:28 +0200951 * qapi-introspect.py's output actually conforms to the schema.
952 */
953static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
954 Error **errp)
955{
Markus Armbrusterb0f36b22017-02-28 22:27:02 +0100956 *ret_data = qobject_from_json(qmp_schema_json, &error_abort);
Markus Armbruster39a18152015-09-16 13:06:28 +0200957}
958
Marc-André Lureau5032a162016-09-12 13:19:02 +0400959/*
Marc-André Lureau5032a162016-09-12 13:19:02 +0400960 * We used to define commands in qmp-commands.hx in addition to the
961 * QAPI schema. This permitted defining some of them only in certain
962 * configurations. query-commands has always reflected that (good,
963 * because it lets QMP clients figure out what's actually available),
964 * while query-qmp-schema never did (not so good). This function is a
965 * hack to keep the configuration-specific commands defined exactly as
966 * before, even though qmp-commands.hx is gone.
967 *
968 * FIXME Educate the QAPI schema on configuration-specific commands,
969 * and drop this hack.
970 */
971static void qmp_unregister_commands_hack(void)
972{
973#ifndef CONFIG_SPICE
Markus Armbruster1527bad2017-03-03 13:32:25 +0100974 qmp_unregister_command(&qmp_commands, "query-spice");
Marc-André Lureau5032a162016-09-12 13:19:02 +0400975#endif
Markus Armbruster38bb54f2017-04-27 15:00:53 +0200976#ifndef CONFIG_REPLICATION
977 qmp_unregister_command(&qmp_commands, "xen-set-replication");
978 qmp_unregister_command(&qmp_commands, "query-xen-replication-status");
979 qmp_unregister_command(&qmp_commands, "xen-colo-do-checkpoint");
980#endif
Marc-André Lureau5032a162016-09-12 13:19:02 +0400981#ifndef TARGET_I386
Markus Armbruster1527bad2017-03-03 13:32:25 +0100982 qmp_unregister_command(&qmp_commands, "rtc-reset-reinjection");
Marc-André Lureau5032a162016-09-12 13:19:02 +0400983#endif
984#ifndef TARGET_S390X
Markus Armbruster1527bad2017-03-03 13:32:25 +0100985 qmp_unregister_command(&qmp_commands, "dump-skeys");
Marc-André Lureau5032a162016-09-12 13:19:02 +0400986#endif
987#ifndef TARGET_ARM
Markus Armbruster1527bad2017-03-03 13:32:25 +0100988 qmp_unregister_command(&qmp_commands, "query-gic-capabilities");
Marc-André Lureau5032a162016-09-12 13:19:02 +0400989#endif
Eduardo Habkostf99fd7c2017-02-22 16:00:28 -0300990#if !defined(TARGET_S390X) && !defined(TARGET_I386)
Markus Armbruster1527bad2017-03-03 13:32:25 +0100991 qmp_unregister_command(&qmp_commands, "query-cpu-model-expansion");
Eduardo Habkostf99fd7c2017-02-22 16:00:28 -0300992#endif
993#if !defined(TARGET_S390X)
Markus Armbruster1527bad2017-03-03 13:32:25 +0100994 qmp_unregister_command(&qmp_commands, "query-cpu-model-baseline");
995 qmp_unregister_command(&qmp_commands, "query-cpu-model-comparison");
Eduardo Habkost728b1422016-10-05 16:49:01 -0300996#endif
997#if !defined(TARGET_PPC) && !defined(TARGET_ARM) && !defined(TARGET_I386) \
998 && !defined(TARGET_S390X)
Markus Armbruster1527bad2017-03-03 13:32:25 +0100999 qmp_unregister_command(&qmp_commands, "query-cpu-definitions");
Eduardo Habkost728b1422016-10-05 16:49:01 -03001000#endif
Marc-André Lureau5032a162016-09-12 13:19:02 +04001001}
1002
Markus Armbruster05875682017-03-03 13:32:24 +01001003void monitor_init_qmp_commands(void)
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001004{
Markus Armbruster635db182017-03-03 13:32:27 +01001005 /*
1006 * Two command lists:
1007 * - qmp_commands contains all QMP commands
1008 * - qmp_cap_negotiation_commands contains just
1009 * "qmp_capabilities", to enforce capability negotiation
1010 */
1011
Markus Armbruster1527bad2017-03-03 13:32:25 +01001012 qmp_init_marshal(&qmp_commands);
Markus Armbruster05875682017-03-03 13:32:24 +01001013
Markus Armbruster1527bad2017-03-03 13:32:25 +01001014 qmp_register_command(&qmp_commands, "query-qmp-schema",
1015 qmp_query_qmp_schema,
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001016 QCO_NO_OPTIONS);
Markus Armbruster1527bad2017-03-03 13:32:25 +01001017 qmp_register_command(&qmp_commands, "device_add", qmp_device_add,
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001018 QCO_NO_OPTIONS);
Markus Armbruster1527bad2017-03-03 13:32:25 +01001019 qmp_register_command(&qmp_commands, "netdev_add", qmp_netdev_add,
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001020 QCO_NO_OPTIONS);
Marc-André Lureau5032a162016-09-12 13:19:02 +04001021
Markus Armbruster05875682017-03-03 13:32:24 +01001022 qmp_unregister_commands_hack();
Markus Armbruster635db182017-03-03 13:32:27 +01001023
1024 QTAILQ_INIT(&qmp_cap_negotiation_commands);
1025 qmp_register_command(&qmp_cap_negotiation_commands, "qmp_capabilities",
1026 qmp_marshal_qmp_capabilities, QCO_NO_OPTIONS);
1027}
1028
1029void qmp_qmp_capabilities(Error **errp)
1030{
1031 if (cur_mon->qmp.commands == &qmp_commands) {
1032 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
1033 "Capabilities negotiation is already complete, command "
1034 "ignored");
1035 return;
1036 }
1037
1038 cur_mon->qmp.commands = &qmp_commands;
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001039}
1040
Luiz Capitulinob025c8b2011-10-06 14:02:57 -03001041/* set the current CPU defined by the user */
1042int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +00001043{
Andreas Färber55e5c282012-12-17 06:18:02 +01001044 CPUState *cpu;
bellard6a00d602005-11-21 23:25:50 +00001045
Andreas Färber1c8bb3c2013-02-15 17:01:09 +01001046 cpu = qemu_get_cpu(cpu_index);
1047 if (cpu == NULL) {
1048 return -1;
bellard6a00d602005-11-21 23:25:50 +00001049 }
Greg Kurz751f8cf2017-10-17 10:16:22 +02001050 g_free(cur_mon->mon_cpu_path);
1051 cur_mon->mon_cpu_path = object_get_canonical_path(OBJECT(cpu));
Andreas Färber1c8bb3c2013-02-15 17:01:09 +01001052 return 0;
bellard6a00d602005-11-21 23:25:50 +00001053}
1054
Pavel Butsykincaf15312015-09-22 16:18:17 +03001055CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +00001056{
Greg Kurz751f8cf2017-10-17 10:16:22 +02001057 CPUState *cpu;
1058
1059 if (cur_mon->mon_cpu_path) {
1060 cpu = (CPUState *) object_resolve_path_type(cur_mon->mon_cpu_path,
1061 TYPE_CPU, NULL);
1062 if (!cpu) {
1063 g_free(cur_mon->mon_cpu_path);
1064 cur_mon->mon_cpu_path = NULL;
1065 }
1066 }
1067 if (!cur_mon->mon_cpu_path) {
Thomas Huth854e67f2017-01-13 13:12:35 +01001068 if (!first_cpu) {
1069 return NULL;
1070 }
David Gibson27a83f82016-09-21 15:29:26 +10001071 monitor_set_cpu(first_cpu->cpu_index);
Greg Kurz751f8cf2017-10-17 10:16:22 +02001072 cpu = first_cpu;
bellard6a00d602005-11-21 23:25:50 +00001073 }
Greg Kurz751f8cf2017-10-17 10:16:22 +02001074 cpu_synchronize_state(cpu);
1075 return cpu;
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001076}
1077
Pavel Butsykinbf957282015-09-10 18:38:59 +03001078CPUArchState *mon_get_cpu_env(void)
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001079{
Thomas Huth854e67f2017-01-13 13:12:35 +01001080 CPUState *cs = mon_get_cpu();
1081
1082 return cs ? cs->env_ptr : NULL;
bellard6a00d602005-11-21 23:25:50 +00001083}
1084
Luiz Capitulino99b77962011-10-24 10:53:44 -02001085int monitor_get_cpu_index(void)
1086{
Thomas Huth854e67f2017-01-13 13:12:35 +01001087 CPUState *cs = mon_get_cpu();
1088
1089 return cs ? cs->cpu_index : UNASSIGNED_CPU_INDEX;
Luiz Capitulino99b77962011-10-24 10:53:44 -02001090}
1091
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001092static void hmp_info_registers(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001093{
Suraj Jitindar Singh18f08282017-06-08 15:41:16 +10001094 bool all_cpus = qdict_get_try_bool(qdict, "cpustate_all", false);
1095 CPUState *cs;
Thomas Huth854e67f2017-01-13 13:12:35 +01001096
Suraj Jitindar Singh18f08282017-06-08 15:41:16 +10001097 if (all_cpus) {
1098 CPU_FOREACH(cs) {
1099 monitor_printf(mon, "\nCPU#%d\n", cs->cpu_index);
1100 cpu_dump_state(cs, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
1101 }
1102 } else {
1103 cs = mon_get_cpu();
1104
1105 if (!cs) {
1106 monitor_printf(mon, "No CPU available\n");
1107 return;
1108 }
1109
1110 cpu_dump_state(cs, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
Thomas Huth854e67f2017-01-13 13:12:35 +01001111 }
bellard9307c4c2004-04-04 12:57:25 +00001112}
1113
Paolo Bonzinif0d14a92012-09-17 13:42:41 +02001114#ifdef CONFIG_TCG
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001115static void hmp_info_jit(Monitor *mon, const QDict *qdict)
bellarde3db7222005-01-26 22:00:47 +00001116{
Thomas Huthb7da97e2017-04-26 06:11:47 +02001117 if (!tcg_enabled()) {
1118 error_report("JIT information is only available with accel=tcg");
1119 return;
1120 }
1121
aliguori376253e2009-03-05 23:01:23 +00001122 dump_exec_info((FILE *)mon, monitor_fprintf);
Sebastian Tanase27498be2014-07-25 11:56:33 +02001123 dump_drift_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +00001124}
1125
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001126static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
Max Filippov246ae242014-11-02 11:04:18 +03001127{
1128 dump_opcount_info((FILE *)mon, monitor_fprintf);
1129}
Paolo Bonzinif0d14a92012-09-17 13:42:41 +02001130#endif
Max Filippov246ae242014-11-02 11:04:18 +03001131
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001132static void hmp_info_history(Monitor *mon, const QDict *qdict)
bellardaa455482004-04-04 13:07:25 +00001133{
1134 int i;
bellard7e2515e2004-08-01 21:52:19 +00001135 const char *str;
ths3b46e622007-09-17 08:09:54 +00001136
aliguoricde76ee2009-03-05 23:01:51 +00001137 if (!mon->rs)
1138 return;
bellard7e2515e2004-08-01 21:52:19 +00001139 i = 0;
1140 for(;;) {
aliguori731b0362009-03-05 23:01:42 +00001141 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +00001142 if (!str)
1143 break;
aliguori376253e2009-03-05 23:01:23 +00001144 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +00001145 i++;
bellardaa455482004-04-04 13:07:25 +00001146 }
1147}
1148
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001149static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
j_mayer76a66252007-03-07 08:32:30 +00001150{
Thomas Huth854e67f2017-01-13 13:12:35 +01001151 CPUState *cs = mon_get_cpu();
1152
1153 if (!cs) {
1154 monitor_printf(mon, "No CPU available\n");
1155 return;
1156 }
1157 cpu_dump_statistics(cs, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +00001158}
j_mayer76a66252007-03-07 08:32:30 +00001159
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001160static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301161{
Lluís Vilanovabd712112016-07-11 12:53:51 +02001162 const char *name = qdict_get_try_str(qdict, "name");
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001163 bool has_vcpu = qdict_haskey(qdict, "vcpu");
1164 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
Lluís Vilanovabd712112016-07-11 12:53:51 +02001165 TraceEventInfoList *events;
Lluís Vilanova14101d02014-08-25 13:20:03 +02001166 TraceEventInfoList *elem;
Lluís Vilanovabd712112016-07-11 12:53:51 +02001167 Error *local_err = NULL;
1168
1169 if (name == NULL) {
1170 name = "*";
1171 }
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001172 if (vcpu < 0) {
1173 monitor_printf(mon, "argument vcpu must be positive");
1174 return;
1175 }
Lluís Vilanovabd712112016-07-11 12:53:51 +02001176
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001177 events = qmp_trace_event_get_state(name, has_vcpu, vcpu, &local_err);
Lluís Vilanovabd712112016-07-11 12:53:51 +02001178 if (local_err) {
1179 error_report_err(local_err);
1180 return;
1181 }
Lluís Vilanova14101d02014-08-25 13:20:03 +02001182
1183 for (elem = events; elem != NULL; elem = elem->next) {
1184 monitor_printf(mon, "%s : state %u\n",
1185 elem->value->name,
1186 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
1187 }
1188 qapi_free_TraceEventInfoList(events);
Prerna Saxena22890ab2010-06-24 17:04:53 +05301189}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301190
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001191void qmp_client_migrate_info(const char *protocol, const char *hostname,
1192 bool has_port, int64_t port,
1193 bool has_tls_port, int64_t tls_port,
1194 bool has_cert_subject, const char *cert_subject,
1195 Error **errp)
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001196{
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001197 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001198 if (!qemu_using_spice(errp)) {
1199 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001200 }
1201
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001202 if (!has_port && !has_tls_port) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001203 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001204 return;
Yonit Halperin6ec5dae2012-03-18 09:42:39 +02001205 }
1206
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001207 if (qemu_spice_migrate_info(hostname,
1208 has_port ? port : -1,
1209 has_tls_port ? tls_port : -1,
1210 cert_subject)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001211 error_setg(errp, QERR_UNDEFINED_ERROR);
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001212 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001213 }
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001214 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001215 }
1216
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001217 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001218}
1219
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001220static void hmp_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001221{
Markus Armbrusterdaa76aa2016-06-15 19:27:16 +02001222 Error *err = NULL;
1223
1224 qemu_set_log_filename(qdict_get_str(qdict, "filename"), &err);
1225 if (err) {
1226 error_report_err(err);
1227 }
pbrooke735b912007-06-30 13:53:24 +00001228}
1229
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001230static void hmp_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001231{
1232 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001233 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001234
bellard9307c4c2004-04-04 12:57:25 +00001235 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001236 mask = 0;
1237 } else {
Peter Maydell4fde1eb2013-02-11 16:41:22 +00001238 mask = qemu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001239 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001240 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001241 return;
1242 }
1243 }
Peter Maydell24537a02013-02-11 16:41:23 +00001244 qemu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00001245}
1246
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001247static void hmp_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001248{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001249 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001250 if (!option || !strcmp(option, "on")) {
1251 singlestep = 1;
1252 } else if (!strcmp(option, "off")) {
1253 singlestep = 0;
1254 } else {
1255 monitor_printf(mon, "unexpected option %s\n", option);
1256 }
1257}
1258
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001259static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001260{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001261 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001262 if (!device)
1263 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1264 if (gdbserver_start(device) < 0) {
1265 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1266 device);
1267 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001268 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001269 } else {
aliguori59030a82009-04-05 18:43:41 +00001270 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1271 device);
bellard8a7ddc32004-03-31 19:00:16 +00001272 }
1273}
1274
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001275static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001276{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001277 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001278 if (select_watchdog_action(action) == -1) {
1279 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1280 }
1281}
1282
aliguori376253e2009-03-05 23:01:23 +00001283static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001284{
aliguori376253e2009-03-05 23:01:23 +00001285 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001286 switch(c) {
1287 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001288 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001289 break;
1290 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001291 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001292 break;
1293 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001294 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001295 break;
1296 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001297 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001298 break;
1299 default:
1300 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001301 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001302 } else {
aliguori376253e2009-03-05 23:01:23 +00001303 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001304 }
1305 break;
1306 }
aliguori376253e2009-03-05 23:01:23 +00001307 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001308}
1309
aliguori376253e2009-03-05 23:01:23 +00001310static void memory_dump(Monitor *mon, int count, int format, int wsize,
Avi Kivitya8170e52012-10-23 12:30:10 +02001311 hwaddr addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001312{
Blue Swirl23842aa2010-01-12 20:27:43 +00001313 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001314 uint8_t buf[16];
1315 uint64_t v;
Thomas Huth854e67f2017-01-13 13:12:35 +01001316 CPUState *cs = mon_get_cpu();
1317
1318 if (!cs && (format == 'i' || !is_physical)) {
1319 monitor_printf(mon, "Can not dump without CPU\n");
1320 return;
1321 }
bellard9307c4c2004-04-04 12:57:25 +00001322
1323 if (format == 'i') {
Richard Henderson1d484742017-09-14 08:38:35 -07001324 monitor_disas(mon, cs, addr, count, is_physical);
bellard9307c4c2004-04-04 12:57:25 +00001325 return;
1326 }
1327
1328 len = wsize * count;
1329 if (wsize == 1)
1330 line_size = 8;
1331 else
1332 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001333 max_digits = 0;
1334
1335 switch(format) {
1336 case 'o':
Marc-André Lureau69db8df2017-06-22 13:04:16 +02001337 max_digits = DIV_ROUND_UP(wsize * 8, 3);
bellard9307c4c2004-04-04 12:57:25 +00001338 break;
1339 default:
1340 case 'x':
1341 max_digits = (wsize * 8) / 4;
1342 break;
1343 case 'u':
1344 case 'd':
Marc-André Lureau69db8df2017-06-22 13:04:16 +02001345 max_digits = DIV_ROUND_UP(wsize * 8 * 10, 33);
bellard9307c4c2004-04-04 12:57:25 +00001346 break;
1347 case 'c':
1348 wsize = 1;
1349 break;
1350 }
1351
1352 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001353 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001354 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001355 else
aliguori376253e2009-03-05 23:01:23 +00001356 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001357 l = len;
1358 if (l > line_size)
1359 l = line_size;
1360 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001361 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001362 } else {
Thomas Huth854e67f2017-01-13 13:12:35 +01001363 if (cpu_memory_rw_debug(cs, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001364 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001365 break;
1366 }
bellard9307c4c2004-04-04 12:57:25 +00001367 }
ths5fafdf22007-09-16 21:08:06 +00001368 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001369 while (i < l) {
1370 switch(wsize) {
1371 default:
1372 case 1:
Peter Maydell24e60302015-01-20 15:19:32 +00001373 v = ldub_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001374 break;
1375 case 2:
Peter Maydell24e60302015-01-20 15:19:32 +00001376 v = lduw_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001377 break;
1378 case 4:
Peter Maydell24e60302015-01-20 15:19:32 +00001379 v = (uint32_t)ldl_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001380 break;
1381 case 8:
Peter Maydell24e60302015-01-20 15:19:32 +00001382 v = ldq_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001383 break;
1384 }
aliguori376253e2009-03-05 23:01:23 +00001385 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001386 switch(format) {
1387 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001388 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001389 break;
1390 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001391 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001392 break;
1393 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001394 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001395 break;
1396 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001397 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001398 break;
1399 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001400 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001401 break;
1402 }
1403 i += wsize;
1404 }
aliguori376253e2009-03-05 23:01:23 +00001405 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001406 addr += l;
1407 len -= l;
1408 }
1409}
1410
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001411static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001412{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001413 int count = qdict_get_int(qdict, "count");
1414 int format = qdict_get_int(qdict, "format");
1415 int size = qdict_get_int(qdict, "size");
1416 target_long addr = qdict_get_int(qdict, "addr");
1417
aliguori376253e2009-03-05 23:01:23 +00001418 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001419}
1420
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001421static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001422{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001423 int count = qdict_get_int(qdict, "count");
1424 int format = qdict_get_int(qdict, "format");
1425 int size = qdict_get_int(qdict, "size");
Avi Kivitya8170e52012-10-23 12:30:10 +02001426 hwaddr addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001427
aliguori376253e2009-03-05 23:01:23 +00001428 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001429}
1430
Paolo Bonzinie9628442017-04-20 15:30:58 +02001431static void *gpa2hva(MemoryRegion **p_mr, hwaddr addr, Error **errp)
1432{
1433 MemoryRegionSection mrs = memory_region_find(get_system_memory(),
1434 addr, 1);
1435
1436 if (!mrs.mr) {
1437 error_setg(errp, "No memory is mapped at address 0x%" HWADDR_PRIx, addr);
1438 return NULL;
1439 }
1440
1441 if (!memory_region_is_ram(mrs.mr) && !memory_region_is_romd(mrs.mr)) {
1442 error_setg(errp, "Memory at address 0x%" HWADDR_PRIx "is not RAM", addr);
1443 memory_region_unref(mrs.mr);
1444 return NULL;
1445 }
1446
1447 *p_mr = mrs.mr;
1448 return qemu_map_ram_ptr(mrs.mr->ram_block, mrs.offset_within_region);
1449}
1450
1451static void hmp_gpa2hva(Monitor *mon, const QDict *qdict)
1452{
1453 hwaddr addr = qdict_get_int(qdict, "addr");
1454 Error *local_err = NULL;
1455 MemoryRegion *mr = NULL;
1456 void *ptr;
1457
1458 ptr = gpa2hva(&mr, addr, &local_err);
1459 if (local_err) {
1460 error_report_err(local_err);
1461 return;
1462 }
1463
1464 monitor_printf(mon, "Host virtual address for 0x%" HWADDR_PRIx
1465 " (%s) is %p\n",
1466 addr, mr->name, ptr);
1467
1468 memory_region_unref(mr);
1469}
1470
1471#ifdef CONFIG_LINUX
1472static uint64_t vtop(void *ptr, Error **errp)
1473{
1474 uint64_t pinfo;
1475 uint64_t ret = -1;
1476 uintptr_t addr = (uintptr_t) ptr;
1477 uintptr_t pagesize = getpagesize();
1478 off_t offset = addr / pagesize * sizeof(pinfo);
1479 int fd;
1480
1481 fd = open("/proc/self/pagemap", O_RDONLY);
1482 if (fd == -1) {
1483 error_setg_errno(errp, errno, "Cannot open /proc/self/pagemap");
1484 return -1;
1485 }
1486
1487 /* Force copy-on-write if necessary. */
1488 atomic_add((uint8_t *)ptr, 0);
1489
1490 if (pread(fd, &pinfo, sizeof(pinfo), offset) != sizeof(pinfo)) {
1491 error_setg_errno(errp, errno, "Cannot read pagemap");
1492 goto out;
1493 }
1494 if ((pinfo & (1ull << 63)) == 0) {
1495 error_setg(errp, "Page not present");
1496 goto out;
1497 }
1498 ret = ((pinfo & 0x007fffffffffffffull) * pagesize) | (addr & (pagesize - 1));
1499
1500out:
1501 close(fd);
1502 return ret;
1503}
1504
1505static void hmp_gpa2hpa(Monitor *mon, const QDict *qdict)
1506{
1507 hwaddr addr = qdict_get_int(qdict, "addr");
1508 Error *local_err = NULL;
1509 MemoryRegion *mr = NULL;
1510 void *ptr;
1511 uint64_t physaddr;
1512
1513 ptr = gpa2hva(&mr, addr, &local_err);
1514 if (local_err) {
1515 error_report_err(local_err);
1516 return;
1517 }
1518
1519 physaddr = vtop(ptr, &local_err);
1520 if (local_err) {
1521 error_report_err(local_err);
1522 } else {
1523 monitor_printf(mon, "Host physical address for 0x%" HWADDR_PRIx
1524 " (%s) is 0x%" PRIx64 "\n",
1525 addr, mr->name, (uint64_t) physaddr);
1526 }
1527
1528 memory_region_unref(mr);
1529}
1530#endif
1531
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001532static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001533{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001534 int format = qdict_get_int(qdict, "format");
Avi Kivitya8170e52012-10-23 12:30:10 +02001535 hwaddr val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001536
bellard9307c4c2004-04-04 12:57:25 +00001537 switch(format) {
1538 case 'o':
Avi Kivitya8170e52012-10-23 12:30:10 +02001539 monitor_printf(mon, "%#" HWADDR_PRIo, val);
bellard9307c4c2004-04-04 12:57:25 +00001540 break;
1541 case 'x':
Avi Kivitya8170e52012-10-23 12:30:10 +02001542 monitor_printf(mon, "%#" HWADDR_PRIx, val);
bellard9307c4c2004-04-04 12:57:25 +00001543 break;
1544 case 'u':
Avi Kivitya8170e52012-10-23 12:30:10 +02001545 monitor_printf(mon, "%" HWADDR_PRIu, val);
bellard9307c4c2004-04-04 12:57:25 +00001546 break;
1547 default:
1548 case 'd':
Avi Kivitya8170e52012-10-23 12:30:10 +02001549 monitor_printf(mon, "%" HWADDR_PRId, val);
bellard9307c4c2004-04-04 12:57:25 +00001550 break;
1551 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001552 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001553 break;
1554 }
aliguori376253e2009-03-05 23:01:23 +00001555 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001556}
1557
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001558static void hmp_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001559{
1560 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001561 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001562 uint32_t start = qdict_get_int(qdict, "start");
1563 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001564
1565 sum = 0;
1566 for(addr = start; addr < (start + size); addr++) {
Peter Maydell42874d32015-04-26 16:49:24 +01001567 uint8_t val = address_space_ldub(&address_space_memory, addr,
1568 MEMTXATTRS_UNSPECIFIED, NULL);
bellarde4cf1ad2005-06-04 20:15:57 +00001569 /* BSD sum algorithm ('sum' Unix command) */
1570 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001571 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001572 }
aliguori376253e2009-03-05 23:01:23 +00001573 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001574}
1575
bellard13224a82006-07-14 22:03:35 +00001576static int mouse_button_state;
1577
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001578static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001579{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001580 int dx, dy, dz, button;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001581 const char *dx_str = qdict_get_str(qdict, "dx_str");
1582 const char *dy_str = qdict_get_str(qdict, "dy_str");
1583 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001584
bellard13224a82006-07-14 22:03:35 +00001585 dx = strtol(dx_str, NULL, 0);
1586 dy = strtol(dy_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001587 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
1588 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
1589
1590 if (dz_str) {
bellard13224a82006-07-14 22:03:35 +00001591 dz = strtol(dz_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001592 if (dz != 0) {
Gerd Hoffmannf22d0af2016-01-12 12:14:12 +01001593 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001594 qemu_input_queue_btn(NULL, button, true);
1595 qemu_input_event_sync();
1596 qemu_input_queue_btn(NULL, button, false);
1597 }
1598 }
1599 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001600}
1601
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001602static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001603{
Eric Blake7fb1cf12015-11-18 01:52:57 -07001604 static uint32_t bmap[INPUT_BUTTON__MAX] = {
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001605 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1606 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
1607 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1608 };
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001609 int button_state = qdict_get_int(qdict, "button_state");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001610
1611 if (mouse_button_state == button_state) {
1612 return;
1613 }
1614 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1615 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001616 mouse_button_state = button_state;
bellard13224a82006-07-14 22:03:35 +00001617}
1618
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001619static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001620{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001621 int size = qdict_get_int(qdict, "size");
1622 int addr = qdict_get_int(qdict, "addr");
1623 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001624 uint32_t val;
1625 int suffix;
1626
1627 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001628 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001629 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001630 addr++;
1631 }
1632 addr &= 0xffff;
1633
1634 switch(size) {
1635 default:
1636 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001637 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001638 suffix = 'b';
1639 break;
1640 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001641 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001642 suffix = 'w';
1643 break;
1644 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001645 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001646 suffix = 'l';
1647 break;
1648 }
aliguori376253e2009-03-05 23:01:23 +00001649 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1650 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001651}
bellarda3a91a32004-06-04 11:06:21 +00001652
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001653static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001654{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001655 int size = qdict_get_int(qdict, "size");
1656 int addr = qdict_get_int(qdict, "addr");
1657 int val = qdict_get_int(qdict, "val");
1658
Jan Kiszkaf1147842009-07-14 10:20:11 +02001659 addr &= IOPORTS_MASK;
1660
1661 switch (size) {
1662 default:
1663 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001664 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001665 break;
1666 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001667 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001668 break;
1669 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001670 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001671 break;
1672 }
1673}
1674
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001675static void hmp_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001676{
Gongleif1839932014-12-03 18:20:58 +00001677 Error *local_err = NULL;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001678 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001679
Gongleif1839932014-12-03 18:20:58 +00001680 qemu_boot_set(bootdevice, &local_err);
1681 if (local_err) {
Markus Armbruster193227f2015-12-18 16:35:06 +01001682 error_report_err(local_err);
aurel320ecdffb2008-05-04 20:11:34 +00001683 } else {
Gongleif1839932014-12-03 18:20:58 +00001684 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001685 }
1686}
1687
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001688static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
Blue Swirl314e2982011-09-11 20:22:05 +00001689{
Peter Xu57bb40c2017-01-16 16:40:05 +08001690 bool flatview = qdict_get_try_bool(qdict, "flatview", false);
Alexey Kardashevskiy5e8fd942017-09-21 18:51:06 +10001691 bool dispatch_tree = qdict_get_try_bool(qdict, "dispatch_tree", false);
Peter Xu57bb40c2017-01-16 16:40:05 +08001692
Alexey Kardashevskiy5e8fd942017-09-21 18:51:06 +10001693 mtree_info((fprintf_function)monitor_printf, mon, flatview, dispatch_tree);
Blue Swirl314e2982011-09-11 20:22:05 +00001694}
1695
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001696static void hmp_info_numa(Monitor *mon, const QDict *qdict)
aliguori030ea372009-04-21 22:30:47 +00001697{
aliguorib28b6232009-04-22 20:20:29 +00001698 int i;
Vadim Galitsyn31959e82017-08-29 17:30:20 +02001699 NumaNodeMem *node_mem;
Igor Mammedovf75cd442017-05-30 18:23:59 +02001700 CpuInfoList *cpu_list, *cpu;
aliguori030ea372009-04-21 22:30:47 +00001701
Igor Mammedovf75cd442017-05-30 18:23:59 +02001702 cpu_list = qmp_query_cpus(&error_abort);
Vadim Galitsyn31959e82017-08-29 17:30:20 +02001703 node_mem = g_new0(NumaNodeMem, nb_numa_nodes);
1704
zhanghailiang5b009e42014-11-04 19:49:30 +08001705 query_numa_node_mem(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001706 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1707 for (i = 0; i < nb_numa_nodes; i++) {
1708 monitor_printf(mon, "node %d cpus:", i);
Igor Mammedovf75cd442017-05-30 18:23:59 +02001709 for (cpu = cpu_list; cpu; cpu = cpu->next) {
1710 if (cpu->value->has_props && cpu->value->props->has_node_id &&
1711 cpu->value->props->node_id == i) {
1712 monitor_printf(mon, " %" PRIi64, cpu->value->CPU);
aliguori030ea372009-04-21 22:30:47 +00001713 }
1714 }
1715 monitor_printf(mon, "\n");
1716 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
Vadim Galitsyn31959e82017-08-29 17:30:20 +02001717 node_mem[i].node_mem >> 20);
1718 monitor_printf(mon, "node %d plugged: %" PRId64 " MB\n", i,
1719 node_mem[i].node_plugged_mem >> 20);
aliguori030ea372009-04-21 22:30:47 +00001720 }
Igor Mammedovf75cd442017-05-30 18:23:59 +02001721 qapi_free_CpuInfoList(cpu_list);
zhanghailiang5b009e42014-11-04 19:49:30 +08001722 g_free(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001723}
1724
bellard5f1ce942006-02-08 22:40:15 +00001725#ifdef CONFIG_PROFILER
1726
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001727int64_t tcg_time;
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001728int64_t dev_time;
1729
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001730static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001731{
aliguori376253e2009-03-05 23:01:23 +00001732 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Rutuja Shah73bcb242016-03-21 21:32:30 +05301733 dev_time, dev_time / (double)NANOSECONDS_PER_SECOND);
aliguori376253e2009-03-05 23:01:23 +00001734 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Rutuja Shah73bcb242016-03-21 21:32:30 +05301735 tcg_time, tcg_time / (double)NANOSECONDS_PER_SECOND);
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001736 tcg_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001737 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001738}
1739#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001740static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001741{
aliguori376253e2009-03-05 23:01:23 +00001742 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001743}
1744#endif
1745
bellardec36b692006-07-16 18:57:03 +00001746/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001747static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001748
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001749static void hmp_info_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001750{
1751 int i;
1752 CaptureState *s;
1753
1754 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001755 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001756 s->ops.info (s->opaque);
1757 }
1758}
1759
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001760static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001761{
1762 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001763 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001764 CaptureState *s;
1765
1766 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1767 if (i == n) {
1768 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001769 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001770 g_free (s);
bellardec36b692006-07-16 18:57:03 +00001771 return;
1772 }
1773 }
1774}
1775
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001776static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001777{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001778 const char *path = qdict_get_str(qdict, "path");
1779 int has_freq = qdict_haskey(qdict, "freq");
1780 int freq = qdict_get_try_int(qdict, "freq", -1);
1781 int has_bits = qdict_haskey(qdict, "bits");
1782 int bits = qdict_get_try_int(qdict, "bits", -1);
1783 int has_channels = qdict_haskey(qdict, "nchannels");
1784 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001785 CaptureState *s;
1786
Anthony Liguori7267c092011-08-20 22:09:37 -05001787 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001788
1789 freq = has_freq ? freq : 44100;
1790 bits = has_bits ? bits : 16;
1791 nchannels = has_channels ? nchannels : 2;
1792
1793 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001794 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05001795 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001796 return;
bellardec36b692006-07-16 18:57:03 +00001797 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001798 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001799}
bellardec36b692006-07-16 18:57:03 +00001800
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001801static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001802{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001803 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001804
aliguori76655d62009-03-06 20:27:37 +00001805 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001806 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001807 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001808 return acl;
1809}
aliguori76655d62009-03-06 20:27:37 +00001810
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001811static void hmp_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001812{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001813 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001814 qemu_acl *acl = find_acl(mon, aclname);
1815 qemu_acl_entry *entry;
1816 int i = 0;
1817
1818 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001819 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001820 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00001821 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00001822 i++;
1823 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001824 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001825 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001826 }
1827}
1828
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001829static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001830{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001831 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001832 qemu_acl *acl = find_acl(mon, aclname);
1833
1834 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001835 qemu_acl_reset(acl);
1836 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001837 }
1838}
aliguori76655d62009-03-06 20:27:37 +00001839
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001840static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001841{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001842 const char *aclname = qdict_get_str(qdict, "aclname");
1843 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001844 qemu_acl *acl = find_acl(mon, aclname);
1845
1846 if (acl) {
1847 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001848 acl->defaultDeny = 0;
1849 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001850 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001851 acl->defaultDeny = 1;
1852 monitor_printf(mon, "acl: policy set to 'deny'\n");
1853 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001854 monitor_printf(mon, "acl: unknown policy '%s', "
1855 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001856 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001857 }
1858}
aliguori76655d62009-03-06 20:27:37 +00001859
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001860static void hmp_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001861{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001862 const char *aclname = qdict_get_str(qdict, "aclname");
1863 const char *match = qdict_get_str(qdict, "match");
1864 const char *policy = qdict_get_str(qdict, "policy");
1865 int has_index = qdict_haskey(qdict, "index");
1866 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001867 qemu_acl *acl = find_acl(mon, aclname);
1868 int deny, ret;
1869
1870 if (acl) {
1871 if (strcmp(policy, "allow") == 0) {
1872 deny = 0;
1873 } else if (strcmp(policy, "deny") == 0) {
1874 deny = 1;
1875 } else {
1876 monitor_printf(mon, "acl: unknown policy '%s', "
1877 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001878 return;
1879 }
aliguori28a76be2009-03-06 20:27:40 +00001880 if (has_index)
1881 ret = qemu_acl_insert(acl, deny, match, index);
1882 else
1883 ret = qemu_acl_append(acl, deny, match);
1884 if (ret < 0)
1885 monitor_printf(mon, "acl: unable to add acl entry\n");
1886 else
1887 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001888 }
1889}
aliguori76655d62009-03-06 20:27:37 +00001890
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001891static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001892{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001893 const char *aclname = qdict_get_str(qdict, "aclname");
1894 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001895 qemu_acl *acl = find_acl(mon, aclname);
1896 int ret;
aliguori76655d62009-03-06 20:27:37 +00001897
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001898 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001899 ret = qemu_acl_remove(acl, match);
1900 if (ret < 0)
1901 monitor_printf(mon, "acl: no matching acl entry\n");
1902 else
1903 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001904 }
1905}
1906
Corey Bryant208c9d12012-06-22 14:36:09 -04001907void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001908{
Anthony Liguoric227f092009-10-01 16:12:16 -05001909 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001910 int fd;
1911
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03001912 fd = qemu_chr_fe_get_msgfd(&cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001913 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001914 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryant208c9d12012-06-22 14:36:09 -04001915 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001916 }
1917
1918 if (qemu_isdigit(fdname[0])) {
Stefan Hajnoczi0b9f0e22014-04-24 13:58:18 +02001919 close(fd);
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001920 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
1921 "a name not starting with a digit");
Corey Bryant208c9d12012-06-22 14:36:09 -04001922 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001923 }
1924
Corey Bryant208c9d12012-06-22 14:36:09 -04001925 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001926 if (strcmp(monfd->name, fdname) != 0) {
1927 continue;
1928 }
1929
1930 close(monfd->fd);
1931 monfd->fd = fd;
Corey Bryant208c9d12012-06-22 14:36:09 -04001932 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001933 }
1934
Anthony Liguori7267c092011-08-20 22:09:37 -05001935 monfd = g_malloc0(sizeof(mon_fd_t));
1936 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001937 monfd->fd = fd;
1938
Corey Bryant208c9d12012-06-22 14:36:09 -04001939 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001940}
1941
Corey Bryant208c9d12012-06-22 14:36:09 -04001942void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001943{
Anthony Liguoric227f092009-10-01 16:12:16 -05001944 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001945
Corey Bryant208c9d12012-06-22 14:36:09 -04001946 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001947 if (strcmp(monfd->name, fdname) != 0) {
1948 continue;
1949 }
1950
Blue Swirl72cf2d42009-09-12 07:36:22 +00001951 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001952 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001953 g_free(monfd->name);
1954 g_free(monfd);
Corey Bryant208c9d12012-06-22 14:36:09 -04001955 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001956 }
1957
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001958 error_setg(errp, QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001959}
1960
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001961int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
Mark McLoughlin7768e042009-07-22 09:11:41 +01001962{
Anthony Liguoric227f092009-10-01 16:12:16 -05001963 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01001964
Blue Swirl72cf2d42009-09-12 07:36:22 +00001965 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01001966 int fd;
1967
1968 if (strcmp(monfd->name, fdname) != 0) {
1969 continue;
1970 }
1971
1972 fd = monfd->fd;
1973
1974 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001975 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05001976 g_free(monfd->name);
1977 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001978
1979 return fd;
1980 }
1981
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001982 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001983 return -1;
1984}
1985
Corey Bryantba1c0482012-08-14 16:43:43 -04001986static void monitor_fdset_cleanup(MonFdset *mon_fdset)
1987{
1988 MonFdsetFd *mon_fdset_fd;
1989 MonFdsetFd *mon_fdset_fd_next;
1990
1991 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantebe52b52012-10-18 15:19:33 -04001992 if ((mon_fdset_fd->removed ||
1993 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
1994 runstate_is_running()) {
Corey Bryantba1c0482012-08-14 16:43:43 -04001995 close(mon_fdset_fd->fd);
1996 g_free(mon_fdset_fd->opaque);
1997 QLIST_REMOVE(mon_fdset_fd, next);
1998 g_free(mon_fdset_fd);
1999 }
2000 }
2001
Corey Bryantadb696f2012-08-14 16:43:47 -04002002 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002003 QLIST_REMOVE(mon_fdset, next);
2004 g_free(mon_fdset);
2005 }
2006}
2007
Corey Bryantefb87c12012-08-14 16:43:48 -04002008static void monitor_fdsets_cleanup(void)
2009{
2010 MonFdset *mon_fdset;
2011 MonFdset *mon_fdset_next;
2012
2013 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
2014 monitor_fdset_cleanup(mon_fdset);
2015 }
2016}
2017
Corey Bryantba1c0482012-08-14 16:43:43 -04002018AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
2019 const char *opaque, Error **errp)
2020{
2021 int fd;
2022 Monitor *mon = cur_mon;
Corey Bryantba1c0482012-08-14 16:43:43 -04002023 AddfdInfo *fdinfo;
2024
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03002025 fd = qemu_chr_fe_get_msgfd(&mon->chr);
Corey Bryantba1c0482012-08-14 16:43:43 -04002026 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002027 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryantba1c0482012-08-14 16:43:43 -04002028 goto error;
2029 }
2030
Corey Bryante446f702012-10-18 15:19:32 -04002031 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
2032 has_opaque, opaque, errp);
2033 if (fdinfo) {
2034 return fdinfo;
Corey Bryant9ac54af2012-10-18 15:19:31 -04002035 }
2036
Corey Bryantba1c0482012-08-14 16:43:43 -04002037error:
2038 if (fd != -1) {
2039 close(fd);
2040 }
2041 return NULL;
2042}
2043
2044void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
2045{
2046 MonFdset *mon_fdset;
2047 MonFdsetFd *mon_fdset_fd;
2048 char fd_str[60];
2049
2050 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2051 if (mon_fdset->id != fdset_id) {
2052 continue;
2053 }
2054 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2055 if (has_fd) {
2056 if (mon_fdset_fd->fd != fd) {
2057 continue;
2058 }
2059 mon_fdset_fd->removed = true;
2060 break;
2061 } else {
2062 mon_fdset_fd->removed = true;
2063 }
2064 }
2065 if (has_fd && !mon_fdset_fd) {
2066 goto error;
2067 }
2068 monitor_fdset_cleanup(mon_fdset);
2069 return;
2070 }
2071
2072error:
2073 if (has_fd) {
2074 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
2075 fdset_id, fd);
2076 } else {
2077 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
2078 }
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002079 error_setg(errp, QERR_FD_NOT_FOUND, fd_str);
Corey Bryantba1c0482012-08-14 16:43:43 -04002080}
2081
2082FdsetInfoList *qmp_query_fdsets(Error **errp)
2083{
2084 MonFdset *mon_fdset;
2085 MonFdsetFd *mon_fdset_fd;
2086 FdsetInfoList *fdset_list = NULL;
2087
2088 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2089 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
2090 FdsetFdInfoList *fdsetfd_list = NULL;
2091
2092 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
2093 fdset_info->value->fdset_id = mon_fdset->id;
2094
2095 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2096 FdsetFdInfoList *fdsetfd_info;
2097
2098 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
2099 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
2100 fdsetfd_info->value->fd = mon_fdset_fd->fd;
2101 if (mon_fdset_fd->opaque) {
2102 fdsetfd_info->value->has_opaque = true;
2103 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
2104 } else {
2105 fdsetfd_info->value->has_opaque = false;
2106 }
2107
2108 fdsetfd_info->next = fdsetfd_list;
2109 fdsetfd_list = fdsetfd_info;
2110 }
2111
2112 fdset_info->value->fds = fdsetfd_list;
2113
2114 fdset_info->next = fdset_list;
2115 fdset_list = fdset_info;
2116 }
2117
2118 return fdset_list;
2119}
2120
Corey Bryante446f702012-10-18 15:19:32 -04002121AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
2122 bool has_opaque, const char *opaque,
2123 Error **errp)
2124{
2125 MonFdset *mon_fdset = NULL;
2126 MonFdsetFd *mon_fdset_fd;
2127 AddfdInfo *fdinfo;
2128
2129 if (has_fdset_id) {
2130 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2131 /* Break if match found or match impossible due to ordering by ID */
2132 if (fdset_id <= mon_fdset->id) {
2133 if (fdset_id < mon_fdset->id) {
2134 mon_fdset = NULL;
2135 }
2136 break;
2137 }
2138 }
2139 }
2140
2141 if (mon_fdset == NULL) {
2142 int64_t fdset_id_prev = -1;
2143 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
2144
2145 if (has_fdset_id) {
2146 if (fdset_id < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002147 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
2148 "a non-negative value");
Corey Bryante446f702012-10-18 15:19:32 -04002149 return NULL;
2150 }
2151 /* Use specified fdset ID */
2152 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2153 mon_fdset_cur = mon_fdset;
2154 if (fdset_id < mon_fdset_cur->id) {
2155 break;
2156 }
2157 }
2158 } else {
2159 /* Use first available fdset ID */
2160 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2161 mon_fdset_cur = mon_fdset;
2162 if (fdset_id_prev == mon_fdset_cur->id - 1) {
2163 fdset_id_prev = mon_fdset_cur->id;
2164 continue;
2165 }
2166 break;
2167 }
2168 }
2169
2170 mon_fdset = g_malloc0(sizeof(*mon_fdset));
2171 if (has_fdset_id) {
2172 mon_fdset->id = fdset_id;
2173 } else {
2174 mon_fdset->id = fdset_id_prev + 1;
2175 }
2176
2177 /* The fdset list is ordered by fdset ID */
2178 if (!mon_fdset_cur) {
2179 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
2180 } else if (mon_fdset->id < mon_fdset_cur->id) {
2181 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
2182 } else {
2183 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
2184 }
2185 }
2186
2187 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
2188 mon_fdset_fd->fd = fd;
2189 mon_fdset_fd->removed = false;
2190 if (has_opaque) {
2191 mon_fdset_fd->opaque = g_strdup(opaque);
2192 }
2193 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
2194
2195 fdinfo = g_malloc0(sizeof(*fdinfo));
2196 fdinfo->fdset_id = mon_fdset->id;
2197 fdinfo->fd = mon_fdset_fd->fd;
2198
2199 return fdinfo;
2200}
2201
Corey Bryantadb696f2012-08-14 16:43:47 -04002202int monitor_fdset_get_fd(int64_t fdset_id, int flags)
2203{
Blue Swirlb2dc64c2012-08-18 20:14:54 +00002204#ifndef _WIN32
Corey Bryantadb696f2012-08-14 16:43:47 -04002205 MonFdset *mon_fdset;
2206 MonFdsetFd *mon_fdset_fd;
2207 int mon_fd_flags;
2208
Corey Bryantadb696f2012-08-14 16:43:47 -04002209 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2210 if (mon_fdset->id != fdset_id) {
2211 continue;
2212 }
2213 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2214 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
2215 if (mon_fd_flags == -1) {
2216 return -1;
2217 }
2218
2219 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
2220 return mon_fdset_fd->fd;
2221 }
2222 }
2223 errno = EACCES;
2224 return -1;
2225 }
2226#endif
2227
2228 errno = ENOENT;
2229 return -1;
2230}
2231
2232int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
2233{
2234 MonFdset *mon_fdset;
2235 MonFdsetFd *mon_fdset_fd_dup;
2236
2237 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2238 if (mon_fdset->id != fdset_id) {
2239 continue;
2240 }
2241 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2242 if (mon_fdset_fd_dup->fd == dup_fd) {
2243 return -1;
2244 }
2245 }
2246 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2247 mon_fdset_fd_dup->fd = dup_fd;
2248 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
2249 return 0;
2250 }
2251 return -1;
2252}
2253
2254static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2255{
2256 MonFdset *mon_fdset;
2257 MonFdsetFd *mon_fdset_fd_dup;
2258
2259 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2260 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2261 if (mon_fdset_fd_dup->fd == dup_fd) {
2262 if (remove) {
2263 QLIST_REMOVE(mon_fdset_fd_dup, next);
2264 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2265 monitor_fdset_cleanup(mon_fdset);
2266 }
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002267 return -1;
2268 } else {
2269 return mon_fdset->id;
Corey Bryantadb696f2012-08-14 16:43:47 -04002270 }
Corey Bryantadb696f2012-08-14 16:43:47 -04002271 }
2272 }
2273 }
2274 return -1;
2275}
2276
2277int monitor_fdset_dup_fd_find(int dup_fd)
2278{
2279 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2280}
2281
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002282void monitor_fdset_dup_fd_remove(int dup_fd)
Corey Bryantadb696f2012-08-14 16:43:47 -04002283{
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002284 monitor_fdset_dup_fd_find_remove(dup_fd, true);
Corey Bryantadb696f2012-08-14 16:43:47 -04002285}
2286
Markus Armbruster1677f4c2015-02-09 14:03:19 +01002287int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
Laszlo Ersek59063662014-04-10 10:24:31 +02002288{
2289 int fd;
2290 Error *local_err = NULL;
2291
2292 if (!qemu_isdigit(fdname[0]) && mon) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002293 fd = monitor_get_fd(mon, fdname, &local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002294 } else {
2295 fd = qemu_parse_fd(fdname);
Laszlo Ersek59063662014-04-10 10:24:31 +02002296 if (fd == -1) {
2297 error_setg(&local_err, "Invalid file descriptor number '%s'",
2298 fdname);
2299 }
2300 }
2301 if (local_err) {
2302 error_propagate(errp, local_err);
2303 assert(fd == -1);
2304 } else {
2305 assert(fd != -1);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002306 }
2307
2308 return fd;
2309}
2310
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002311/* Please update hmp-commands.hx when adding or changing commands */
Wayne Xia816f8922011-10-12 11:32:41 +08002312static mon_cmd_t info_cmds[] = {
Pavel Butsykinda76ee72015-09-10 18:38:58 +03002313#include "hmp-commands-info.h"
2314 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002315};
2316
Wenchao Xiaa13ced52013-01-14 14:06:28 +08002317/* mon_cmds and info_cmds would be sorted at runtime */
2318static mon_cmd_t mon_cmds[] = {
2319#include "hmp-commands.h"
2320 { NULL, NULL, },
2321};
2322
bellard9307c4c2004-04-04 12:57:25 +00002323/*******************************************************************/
2324
2325static const char *pch;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002326static sigjmp_buf expr_env;
bellard9307c4c2004-04-04 12:57:25 +00002327
bellard9307c4c2004-04-04 12:57:25 +00002328
Stefan Weil9c3175c2013-08-22 21:30:09 +02002329static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
2330expr_error(Monitor *mon, const char *fmt, ...)
bellard9dc39cb2004-03-14 21:38:27 +00002331{
Fam Zheng277acfe2013-08-20 10:58:21 +08002332 va_list ap;
2333 va_start(ap, fmt);
2334 monitor_vprintf(mon, fmt, ap);
2335 monitor_printf(mon, "\n");
2336 va_end(ap);
Peter Maydell6ab7e542013-02-20 15:21:09 +00002337 siglongjmp(expr_env, 1);
bellard9307c4c2004-04-04 12:57:25 +00002338}
2339
Markus Armbruster09b94182010-01-20 13:07:30 +01002340/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00002341static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002342{
Pavel Butsykinbf957282015-09-10 18:38:59 +03002343 const MonitorDef *md = target_monitor_defs();
Thomas Huth854e67f2017-01-13 13:12:35 +01002344 CPUState *cs = mon_get_cpu();
bellard92a31b12005-02-10 22:00:52 +00002345 void *ptr;
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002346 uint64_t tmp = 0;
2347 int ret;
bellard92a31b12005-02-10 22:00:52 +00002348
Thomas Huth854e67f2017-01-13 13:12:35 +01002349 if (cs == NULL || md == NULL) {
Pavel Butsykinbf957282015-09-10 18:38:59 +03002350 return -1;
2351 }
2352
2353 for(; md->name != NULL; md++) {
bellard9307c4c2004-04-04 12:57:25 +00002354 if (compare_cmd(name, md->name)) {
2355 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002356 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002357 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002358 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00002359 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002360 switch(md->type) {
2361 case MD_I32:
2362 *pval = *(int32_t *)ptr;
2363 break;
2364 case MD_TLONG:
2365 *pval = *(target_long *)ptr;
2366 break;
2367 default:
2368 *pval = 0;
2369 break;
2370 }
bellard9307c4c2004-04-04 12:57:25 +00002371 }
2372 return 0;
2373 }
2374 }
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002375
Thomas Huth854e67f2017-01-13 13:12:35 +01002376 ret = target_get_monitor_def(cs, name, &tmp);
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002377 if (!ret) {
2378 *pval = (target_long) tmp;
2379 }
2380
2381 return ret;
bellard9307c4c2004-04-04 12:57:25 +00002382}
2383
2384static void next(void)
2385{
Blue Swirl660f11b2009-07-31 21:16:51 +00002386 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002387 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002388 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002389 pch++;
2390 }
2391}
2392
aliguori376253e2009-03-05 23:01:23 +00002393static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002394
aliguori376253e2009-03-05 23:01:23 +00002395static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002396{
blueswir1c2efc952007-09-25 17:28:42 +00002397 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002398 char *p;
bellard6a00d602005-11-21 23:25:50 +00002399 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002400
2401 switch(*pch) {
2402 case '+':
2403 next();
aliguori376253e2009-03-05 23:01:23 +00002404 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002405 break;
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_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002417 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002418 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002419 }
2420 next();
2421 break;
bellard81d09122004-07-14 17:21:37 +00002422 case '\'':
2423 pch++;
2424 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002425 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002426 n = *pch;
2427 pch++;
2428 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002429 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002430 next();
2431 break;
bellard9307c4c2004-04-04 12:57:25 +00002432 case '$':
2433 {
2434 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002435 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002436
bellard9307c4c2004-04-04 12:57:25 +00002437 pch++;
2438 q = buf;
2439 while ((*pch >= 'a' && *pch <= 'z') ||
2440 (*pch >= 'A' && *pch <= 'Z') ||
2441 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002442 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002443 if ((q - buf) < sizeof(buf) - 1)
2444 *q++ = *pch;
2445 pch++;
2446 }
blueswir1cd390082008-11-16 13:53:32 +00002447 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002448 pch++;
2449 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002450 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01002451 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00002452 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00002453 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002454 }
2455 break;
2456 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002457 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002458 n = 0;
2459 break;
2460 default:
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002461 errno = 0;
bellard4f4fbf72006-06-25 18:28:12 +00002462 n = strtoull(pch, &p, 0);
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002463 if (errno == ERANGE) {
2464 expr_error(mon, "number too large");
2465 }
bellard9307c4c2004-04-04 12:57:25 +00002466 if (pch == p) {
Fam Zheng277acfe2013-08-20 10:58:21 +08002467 expr_error(mon, "invalid char '%c' in expression", *p);
bellard9307c4c2004-04-04 12:57:25 +00002468 }
2469 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002470 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002471 pch++;
2472 break;
2473 }
2474 return n;
2475}
2476
2477
aliguori376253e2009-03-05 23:01:23 +00002478static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002479{
blueswir1c2efc952007-09-25 17:28:42 +00002480 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002481 int op;
ths3b46e622007-09-17 08:09:54 +00002482
aliguori376253e2009-03-05 23:01:23 +00002483 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002484 for(;;) {
2485 op = *pch;
2486 if (op != '*' && op != '/' && op != '%')
2487 break;
2488 next();
aliguori376253e2009-03-05 23:01:23 +00002489 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002490 switch(op) {
2491 default:
2492 case '*':
2493 val *= val2;
2494 break;
2495 case '/':
2496 case '%':
ths5fafdf22007-09-16 21:08:06 +00002497 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002498 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002499 if (op == '/')
2500 val /= val2;
2501 else
2502 val %= val2;
2503 break;
2504 }
2505 }
2506 return val;
2507}
2508
aliguori376253e2009-03-05 23:01:23 +00002509static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002510{
blueswir1c2efc952007-09-25 17:28:42 +00002511 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002512 int op;
bellard9307c4c2004-04-04 12:57:25 +00002513
aliguori376253e2009-03-05 23:01:23 +00002514 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002515 for(;;) {
2516 op = *pch;
2517 if (op != '&' && op != '|' && op != '^')
2518 break;
2519 next();
aliguori376253e2009-03-05 23:01:23 +00002520 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002521 switch(op) {
2522 default:
2523 case '&':
2524 val &= val2;
2525 break;
2526 case '|':
2527 val |= val2;
2528 break;
2529 case '^':
2530 val ^= val2;
2531 break;
2532 }
2533 }
2534 return val;
2535}
2536
aliguori376253e2009-03-05 23:01:23 +00002537static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002538{
blueswir1c2efc952007-09-25 17:28:42 +00002539 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002540 int op;
bellard9307c4c2004-04-04 12:57:25 +00002541
aliguori376253e2009-03-05 23:01:23 +00002542 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002543 for(;;) {
2544 op = *pch;
2545 if (op != '+' && op != '-')
2546 break;
2547 next();
aliguori376253e2009-03-05 23:01:23 +00002548 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002549 if (op == '+')
2550 val += val2;
2551 else
2552 val -= val2;
2553 }
2554 return val;
2555}
2556
aliguori376253e2009-03-05 23:01:23 +00002557static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002558{
2559 pch = *pp;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002560 if (sigsetjmp(expr_env, 0)) {
bellard9307c4c2004-04-04 12:57:25 +00002561 *pp = pch;
2562 return -1;
2563 }
blueswir1cd390082008-11-16 13:53:32 +00002564 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002565 pch++;
aliguori376253e2009-03-05 23:01:23 +00002566 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002567 *pp = pch;
2568 return 0;
2569}
2570
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002571static int get_double(Monitor *mon, double *pval, const char **pp)
2572{
2573 const char *p = *pp;
2574 char *tailp;
2575 double d;
2576
2577 d = strtod(p, &tailp);
2578 if (tailp == p) {
2579 monitor_printf(mon, "Number expected\n");
2580 return -1;
2581 }
2582 if (d != d || d - d != 0) {
2583 /* NaN or infinity */
2584 monitor_printf(mon, "Bad number\n");
2585 return -1;
2586 }
2587 *pval = d;
2588 *pp = tailp;
2589 return 0;
2590}
2591
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002592/*
2593 * Store the command-name in cmdname, and return a pointer to
2594 * the remaining of the command string.
2595 */
2596static const char *get_command_name(const char *cmdline,
2597 char *cmdname, size_t nlen)
2598{
2599 size_t len;
2600 const char *p, *pstart;
2601
2602 p = cmdline;
2603 while (qemu_isspace(*p))
2604 p++;
2605 if (*p == '\0')
2606 return NULL;
2607 pstart = p;
2608 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2609 p++;
2610 len = p - pstart;
2611 if (len > nlen - 1)
2612 len = nlen - 1;
2613 memcpy(cmdname, pstart, len);
2614 cmdname[len] = '\0';
2615 return p;
2616}
2617
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002618/**
2619 * Read key of 'type' into 'key' and return the current
2620 * 'type' pointer.
2621 */
2622static char *key_get_info(const char *type, char **key)
2623{
2624 size_t len;
2625 char *p, *str;
2626
2627 if (*type == ',')
2628 type++;
2629
2630 p = strchr(type, ':');
2631 if (!p) {
2632 *key = NULL;
2633 return NULL;
2634 }
2635 len = p - type;
2636
Anthony Liguori7267c092011-08-20 22:09:37 -05002637 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002638 memcpy(str, type, len);
2639 str[len] = '\0';
2640
2641 *key = str;
2642 return ++p;
2643}
2644
bellard9307c4c2004-04-04 12:57:25 +00002645static int default_fmt_format = 'x';
2646static int default_fmt_size = 4;
2647
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002648static int is_valid_option(const char *c, const char *typestr)
2649{
2650 char option[3];
2651
2652 option[0] = '-';
2653 option[1] = *c;
2654 option[2] = '\0';
2655
2656 typestr = strstr(typestr, option);
2657 return (typestr != NULL);
2658}
2659
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03002660static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
2661 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002662{
2663 const mon_cmd_t *cmd;
2664
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03002665 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002666 if (compare_cmd(cmdname, cmd->name)) {
2667 return cmd;
2668 }
2669 }
2670
2671 return NULL;
2672}
2673
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002674/*
Bandan Dasae502122015-06-03 18:38:08 -04002675 * Parse command name from @cmdp according to command table @table.
2676 * If blank, return NULL.
2677 * Else, if no valid command can be found, report to @mon, and return
2678 * NULL.
2679 * Else, change @cmdp to point right behind the name, and return its
2680 * command table entry.
2681 * Do not assume the return value points into @table! It doesn't when
2682 * the command is found in a sub-command table.
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002683 */
Anthony Liguoric227f092009-10-01 16:12:16 -05002684static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Dr. David Alan Gilbert250b8192017-08-17 11:42:16 +01002685 const char *cmdp_start,
Bandan Dasae502122015-06-03 18:38:08 -04002686 const char **cmdp,
2687 mon_cmd_t *table)
bellard9307c4c2004-04-04 12:57:25 +00002688{
Bandan Dasae502122015-06-03 18:38:08 -04002689 const char *p;
Anthony Liguoric227f092009-10-01 16:12:16 -05002690 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002691 char cmdname[256];
bellard9dc39cb2004-03-14 21:38:27 +00002692
bellard9307c4c2004-04-04 12:57:25 +00002693 /* extract the command name */
Bandan Dasae502122015-06-03 18:38:08 -04002694 p = get_command_name(*cmdp, cmdname, sizeof(cmdname));
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002695 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002696 return NULL;
ths3b46e622007-09-17 08:09:54 +00002697
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002698 cmd = search_dispatch_table(table, cmdname);
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002699 if (!cmd) {
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002700 monitor_printf(mon, "unknown command: '%.*s'\n",
Dr. David Alan Gilbert250b8192017-08-17 11:42:16 +01002701 (int)(p - cmdp_start), cmdp_start);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002702 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002703 }
bellard9307c4c2004-04-04 12:57:25 +00002704
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002705 /* filter out following useless space */
2706 while (qemu_isspace(*p)) {
2707 p++;
2708 }
Bandan Dasae502122015-06-03 18:38:08 -04002709
2710 *cmdp = p;
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002711 /* search sub command */
Bandan Dasae502122015-06-03 18:38:08 -04002712 if (cmd->sub_table != NULL && *p != '\0') {
Dr. David Alan Gilbert250b8192017-08-17 11:42:16 +01002713 return monitor_parse_command(mon, cmdp_start, cmdp, cmd->sub_table);
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002714 }
2715
Bandan Dasae502122015-06-03 18:38:08 -04002716 return cmd;
2717}
2718
2719/*
2720 * Parse arguments for @cmd.
2721 * If it can't be parsed, report to @mon, and return NULL.
2722 * Else, insert command arguments into a QDict, and return it.
2723 * Note: On success, caller has to free the QDict structure.
2724 */
2725
2726static QDict *monitor_parse_arguments(Monitor *mon,
2727 const char **endp,
2728 const mon_cmd_t *cmd)
2729{
2730 const char *typestr;
2731 char *key;
2732 int c;
2733 const char *p = *endp;
2734 char buf[1024];
2735 QDict *qdict = qdict_new();
2736
bellard9307c4c2004-04-04 12:57:25 +00002737 /* parse the parameters */
2738 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00002739 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002740 typestr = key_get_info(typestr, &key);
2741 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00002742 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002743 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00002744 typestr++;
2745 switch(c) {
2746 case 'F':
bellard81d09122004-07-14 17:21:37 +00002747 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002748 case 's':
2749 {
2750 int ret;
ths3b46e622007-09-17 08:09:54 +00002751
blueswir1cd390082008-11-16 13:53:32 +00002752 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002753 p++;
2754 if (*typestr == '?') {
2755 typestr++;
2756 if (*p == '\0') {
2757 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03002758 break;
bellard9307c4c2004-04-04 12:57:25 +00002759 }
2760 }
2761 ret = get_str(buf, sizeof(buf), &p);
2762 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002763 switch(c) {
2764 case 'F':
aliguori376253e2009-03-05 23:01:23 +00002765 monitor_printf(mon, "%s: filename expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002766 cmd->name);
bellard81d09122004-07-14 17:21:37 +00002767 break;
2768 case 'B':
aliguori376253e2009-03-05 23:01:23 +00002769 monitor_printf(mon, "%s: block device name expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002770 cmd->name);
bellard81d09122004-07-14 17:21:37 +00002771 break;
2772 default:
Bandan Dasae502122015-06-03 18:38:08 -04002773 monitor_printf(mon, "%s: string expected\n", cmd->name);
bellard81d09122004-07-14 17:21:37 +00002774 break;
2775 }
bellard9307c4c2004-04-04 12:57:25 +00002776 goto fail;
2777 }
Eric Blake46f5ac22017-04-27 16:58:17 -05002778 qdict_put_str(qdict, key, buf);
bellard9307c4c2004-04-04 12:57:25 +00002779 }
2780 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01002781 case 'O':
2782 {
2783 QemuOptsList *opts_list;
2784 QemuOpts *opts;
2785
2786 opts_list = qemu_find_opts(key);
2787 if (!opts_list || opts_list->desc->name) {
2788 goto bad_type;
2789 }
2790 while (qemu_isspace(*p)) {
2791 p++;
2792 }
2793 if (!*p)
2794 break;
2795 if (get_str(buf, sizeof(buf), &p) < 0) {
2796 goto fail;
2797 }
Markus Armbruster70b94332015-02-13 12:50:26 +01002798 opts = qemu_opts_parse_noisily(opts_list, buf, true);
Markus Armbruster361127d2010-02-10 20:24:35 +01002799 if (!opts) {
2800 goto fail;
2801 }
2802 qemu_opts_to_qdict(opts, qdict);
2803 qemu_opts_del(opts);
2804 }
2805 break;
bellard9307c4c2004-04-04 12:57:25 +00002806 case '/':
2807 {
2808 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002809
blueswir1cd390082008-11-16 13:53:32 +00002810 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002811 p++;
2812 if (*p == '/') {
2813 /* format found */
2814 p++;
2815 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002816 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002817 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002818 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002819 count = count * 10 + (*p - '0');
2820 p++;
2821 }
2822 }
2823 size = -1;
2824 format = -1;
2825 for(;;) {
2826 switch(*p) {
2827 case 'o':
2828 case 'd':
2829 case 'u':
2830 case 'x':
2831 case 'i':
2832 case 'c':
2833 format = *p++;
2834 break;
2835 case 'b':
2836 size = 1;
2837 p++;
2838 break;
2839 case 'h':
2840 size = 2;
2841 p++;
2842 break;
2843 case 'w':
2844 size = 4;
2845 p++;
2846 break;
2847 case 'g':
2848 case 'L':
2849 size = 8;
2850 p++;
2851 break;
2852 default:
2853 goto next;
2854 }
2855 }
2856 next:
blueswir1cd390082008-11-16 13:53:32 +00002857 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00002858 monitor_printf(mon, "invalid char in format: '%c'\n",
2859 *p);
bellard9307c4c2004-04-04 12:57:25 +00002860 goto fail;
2861 }
bellard9307c4c2004-04-04 12:57:25 +00002862 if (format < 0)
2863 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002864 if (format != 'i') {
2865 /* for 'i', not specifying a size gives -1 as size */
2866 if (size < 0)
2867 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002868 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002869 }
bellard9307c4c2004-04-04 12:57:25 +00002870 default_fmt_format = format;
2871 } else {
2872 count = 1;
2873 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002874 if (format != 'i') {
2875 size = default_fmt_size;
2876 } else {
2877 size = -1;
2878 }
bellard9307c4c2004-04-04 12:57:25 +00002879 }
Eric Blake46f5ac22017-04-27 16:58:17 -05002880 qdict_put_int(qdict, "count", count);
2881 qdict_put_int(qdict, "format", format);
2882 qdict_put_int(qdict, "size", size);
bellard9307c4c2004-04-04 12:57:25 +00002883 }
2884 break;
2885 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002886 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002887 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00002888 {
blueswir1c2efc952007-09-25 17:28:42 +00002889 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00002890
blueswir1cd390082008-11-16 13:53:32 +00002891 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002892 p++;
bellard34405572004-06-08 00:55:58 +00002893 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002894 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03002895 if (*p == '\0') {
2896 typestr++;
2897 break;
2898 }
bellard34405572004-06-08 00:55:58 +00002899 } else {
2900 if (*p == '.') {
2901 p++;
blueswir1cd390082008-11-16 13:53:32 +00002902 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00002903 p++;
bellard34405572004-06-08 00:55:58 +00002904 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03002905 typestr++;
2906 break;
bellard34405572004-06-08 00:55:58 +00002907 }
2908 }
bellard13224a82006-07-14 22:03:35 +00002909 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002910 }
aliguori376253e2009-03-05 23:01:23 +00002911 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00002912 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002913 /* Check if 'i' is greater than 32-bit */
2914 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
Bandan Dasae502122015-06-03 18:38:08 -04002915 monitor_printf(mon, "\'%s\' has failed: ", cmd->name);
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002916 monitor_printf(mon, "integer is for 32-bit values\n");
2917 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002918 } else if (c == 'M') {
Luiz Capitulino91162842012-04-26 17:34:30 -03002919 if (val < 0) {
2920 monitor_printf(mon, "enter a positive value\n");
2921 goto fail;
2922 }
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002923 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002924 }
Eric Blake46f5ac22017-04-27 16:58:17 -05002925 qdict_put_int(qdict, key, val);
bellard9307c4c2004-04-04 12:57:25 +00002926 }
2927 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02002928 case 'o':
2929 {
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +01002930 int ret;
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +01002931 uint64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02002932 char *end;
2933
2934 while (qemu_isspace(*p)) {
2935 p++;
2936 }
2937 if (*typestr == '?') {
2938 typestr++;
2939 if (*p == '\0') {
2940 break;
2941 }
2942 }
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +01002943 ret = qemu_strtosz_MiB(p, &end, &val);
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +01002944 if (ret < 0 || val > INT64_MAX) {
Jes Sorensendbc0c672010-10-21 17:15:47 +02002945 monitor_printf(mon, "invalid size\n");
2946 goto fail;
2947 }
Eric Blake46f5ac22017-04-27 16:58:17 -05002948 qdict_put_int(qdict, key, val);
Jes Sorensendbc0c672010-10-21 17:15:47 +02002949 p = end;
2950 }
2951 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01002952 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002953 {
2954 double val;
2955
2956 while (qemu_isspace(*p))
2957 p++;
2958 if (*typestr == '?') {
2959 typestr++;
2960 if (*p == '\0') {
2961 break;
2962 }
2963 }
2964 if (get_double(mon, &val, &p) < 0) {
2965 goto fail;
2966 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02002967 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01002968 switch (*p) {
2969 case 'm':
2970 val /= 1e3; p += 2; break;
2971 case 'u':
2972 val /= 1e6; p += 2; break;
2973 case 'n':
2974 val /= 1e9; p += 2; break;
2975 }
2976 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002977 if (*p && !qemu_isspace(*p)) {
2978 monitor_printf(mon, "Unknown unit suffix\n");
2979 goto fail;
2980 }
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +04002981 qdict_put(qdict, key, qnum_from_double(val));
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002982 }
2983 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002984 case 'b':
2985 {
2986 const char *beg;
Eric Blakefc48ffc2015-05-15 16:24:59 -06002987 bool val;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002988
2989 while (qemu_isspace(*p)) {
2990 p++;
2991 }
2992 beg = p;
2993 while (qemu_isgraph(*p)) {
2994 p++;
2995 }
2996 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06002997 val = true;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002998 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06002999 val = false;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003000 } else {
3001 monitor_printf(mon, "Expected 'on' or 'off'\n");
3002 goto fail;
3003 }
Eric Blake46f5ac22017-04-27 16:58:17 -05003004 qdict_put_bool(qdict, key, val);
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003005 }
3006 break;
bellard9307c4c2004-04-04 12:57:25 +00003007 case '-':
3008 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003009 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003010 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003011 /* option */
ths3b46e622007-09-17 08:09:54 +00003012
bellard9307c4c2004-04-04 12:57:25 +00003013 c = *typestr++;
3014 if (c == '\0')
3015 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003016 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003017 p++;
bellard9307c4c2004-04-04 12:57:25 +00003018 if (*p == '-') {
3019 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003020 if(c != *p) {
3021 if(!is_valid_option(p, typestr)) {
3022
3023 monitor_printf(mon, "%s: unsupported option -%c\n",
Bandan Dasae502122015-06-03 18:38:08 -04003024 cmd->name, *p);
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003025 goto fail;
3026 } else {
3027 skip_key = 1;
3028 }
bellard9307c4c2004-04-04 12:57:25 +00003029 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003030 if(skip_key) {
3031 p = tmp;
3032 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003033 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003034 p++;
Eric Blake46f5ac22017-04-27 16:58:17 -05003035 qdict_put_bool(qdict, key, true);
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003036 }
bellard9307c4c2004-04-04 12:57:25 +00003037 }
bellard9307c4c2004-04-04 12:57:25 +00003038 }
3039 break;
Wenchao Xia129be002013-08-27 20:38:26 +08003040 case 'S':
3041 {
3042 /* package all remaining string */
3043 int len;
3044
3045 while (qemu_isspace(*p)) {
3046 p++;
3047 }
3048 if (*typestr == '?') {
3049 typestr++;
3050 if (*p == '\0') {
3051 /* no remaining string: NULL argument */
3052 break;
3053 }
3054 }
3055 len = strlen(p);
3056 if (len <= 0) {
3057 monitor_printf(mon, "%s: string expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04003058 cmd->name);
Bandan Dase549d2a2015-06-03 18:38:10 -04003059 goto fail;
Wenchao Xia129be002013-08-27 20:38:26 +08003060 }
Eric Blake46f5ac22017-04-27 16:58:17 -05003061 qdict_put_str(qdict, key, p);
Wenchao Xia129be002013-08-27 20:38:26 +08003062 p += len;
3063 }
3064 break;
bellard9307c4c2004-04-04 12:57:25 +00003065 default:
3066 bad_type:
Bandan Dasae502122015-06-03 18:38:08 -04003067 monitor_printf(mon, "%s: unknown type '%c'\n", cmd->name, c);
bellard9307c4c2004-04-04 12:57:25 +00003068 goto fail;
3069 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003070 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003071 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003072 }
3073 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003074 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003075 p++;
3076 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003077 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
Bandan Dasae502122015-06-03 18:38:08 -04003078 cmd->name);
bellard9307c4c2004-04-04 12:57:25 +00003079 goto fail;
3080 }
3081
Bandan Dasae502122015-06-03 18:38:08 -04003082 return qdict;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003083
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003084fail:
Bandan Dasae502122015-06-03 18:38:08 -04003085 QDECREF(qdict);
Anthony Liguori7267c092011-08-20 22:09:37 -05003086 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003087 return NULL;
3088}
3089
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003090static void handle_hmp_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003091{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003092 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003093 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003094
Stefan Hajnoczi79cad8b2017-06-05 11:42:15 +01003095 trace_handle_hmp_command(mon, cmdline);
3096
Dr. David Alan Gilbert250b8192017-08-17 11:42:16 +01003097 cmd = monitor_parse_command(mon, cmdline, &cmdline, mon->cmd_table);
Bandan Dasae502122015-06-03 18:38:08 -04003098 if (!cmd) {
3099 return;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003100 }
3101
Bandan Dasae502122015-06-03 18:38:08 -04003102 qdict = monitor_parse_arguments(mon, &cmdline, cmd);
3103 if (!qdict) {
Bandan Dasdd41eea2015-06-03 18:38:09 -04003104 monitor_printf(mon, "Try \"help %s\" for more information\n",
3105 cmd->name);
Bandan Dasae502122015-06-03 18:38:08 -04003106 return;
3107 }
3108
Marc-André Lureau2b9e3572016-09-12 13:19:06 +04003109 cmd->cmd(mon, qdict);
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003110 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00003111}
3112
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003113static void cmd_completion(Monitor *mon, const char *name, const char *list)
bellard81d09122004-07-14 17:21:37 +00003114{
3115 const char *p, *pstart;
3116 char cmd[128];
3117 int len;
3118
3119 p = list;
3120 for(;;) {
3121 pstart = p;
3122 p = strchr(p, '|');
3123 if (!p)
3124 p = pstart + strlen(pstart);
3125 len = p - pstart;
3126 if (len > sizeof(cmd) - 2)
3127 len = sizeof(cmd) - 2;
3128 memcpy(cmd, pstart, len);
3129 cmd[len] = '\0';
3130 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003131 readline_add_completion(mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00003132 }
3133 if (*p == '\0')
3134 break;
3135 p++;
3136 }
3137}
3138
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003139static void file_completion(Monitor *mon, const char *input)
bellard81d09122004-07-14 17:21:37 +00003140{
3141 DIR *ffs;
3142 struct dirent *d;
3143 char path[1024];
3144 char file[1024], file_prefix[1024];
3145 int input_path_len;
3146 const char *p;
3147
ths5fafdf22007-09-16 21:08:06 +00003148 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00003149 if (!p) {
3150 input_path_len = 0;
3151 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00003152 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00003153 } else {
3154 input_path_len = p - input + 1;
3155 memcpy(path, input, input_path_len);
3156 if (input_path_len > sizeof(path) - 1)
3157 input_path_len = sizeof(path) - 1;
3158 path[input_path_len] = '\0';
3159 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3160 }
Bandan Das19f2db52015-06-03 18:38:07 -04003161
bellard81d09122004-07-14 17:21:37 +00003162 ffs = opendir(path);
3163 if (!ffs)
3164 return;
3165 for(;;) {
3166 struct stat sb;
3167 d = readdir(ffs);
3168 if (!d)
3169 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09003170
3171 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
3172 continue;
3173 }
3174
bellard81d09122004-07-14 17:21:37 +00003175 if (strstart(d->d_name, file_prefix, NULL)) {
3176 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00003177 if (input_path_len < sizeof(file))
3178 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3179 d->d_name);
bellard81d09122004-07-14 17:21:37 +00003180 /* stat the file to find out if it's a directory.
3181 * In that case add a slash to speed up typing long paths
3182 */
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01003183 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
blueswir1363a37d2008-08-21 17:58:08 +00003184 pstrcat(file, sizeof(file), "/");
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01003185 }
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003186 readline_add_completion(mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003187 }
3188 }
3189 closedir(ffs);
3190}
3191
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003192static const char *next_arg_type(const char *typestr)
3193{
3194 const char *p = strchr(typestr, ':');
3195 return (p != NULL ? ++p : typestr);
3196}
3197
Hani Benhabiles40d19392014-05-07 23:41:30 +01003198static void add_completion_option(ReadLineState *rs, const char *str,
3199 const char *option)
3200{
3201 if (!str || !option) {
3202 return;
3203 }
3204 if (!strncmp(option, str, strlen(str))) {
3205 readline_add_completion(rs, option);
3206 }
3207}
3208
Hani Benhabiles13e315d2014-05-07 23:41:29 +01003209void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
3210{
3211 size_t len;
3212 ChardevBackendInfoList *list, *start;
3213
3214 if (nb_args != 2) {
3215 return;
3216 }
3217 len = strlen(str);
3218 readline_set_completion_index(rs, len);
3219
3220 start = list = qmp_query_chardev_backends(NULL);
3221 while (list) {
3222 const char *chr_name = list->value->name;
3223
3224 if (!strncmp(chr_name, str, len)) {
3225 readline_add_completion(rs, chr_name);
3226 }
3227 list = list->next;
3228 }
3229 qapi_free_ChardevBackendInfoList(start);
3230}
3231
Hani Benhabilesb162b492014-05-07 23:41:31 +01003232void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
3233{
3234 size_t len;
3235 int i;
3236
3237 if (nb_args != 2) {
3238 return;
3239 }
3240 len = strlen(str);
3241 readline_set_completion_index(rs, len);
Markus Armbruster1c236ba2017-08-24 10:46:06 +02003242 for (i = 0; i < NET_CLIENT_DRIVER__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02003243 add_completion_option(rs, str, NetClientDriver_str(i));
Hani Benhabilesb162b492014-05-07 23:41:31 +01003244 }
3245}
3246
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003247void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003248{
3249 GSList *list, *elt;
3250 size_t len;
3251
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003252 if (nb_args != 2) {
3253 return;
3254 }
3255
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003256 len = strlen(str);
3257 readline_set_completion_index(rs, len);
3258 list = elt = object_class_get_list(TYPE_DEVICE, false);
3259 while (elt) {
3260 const char *name;
3261 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
3262 TYPE_DEVICE);
3263 name = object_class_get_name(OBJECT_CLASS(dc));
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003264
Eduardo Habkoste90f2a82017-05-03 17:35:44 -03003265 if (dc->user_creatable
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003266 && !strncmp(name, str, len)) {
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003267 readline_add_completion(rs, name);
3268 }
3269 elt = elt->next;
3270 }
3271 g_slist_free(list);
3272}
3273
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003274void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003275{
3276 GSList *list, *elt;
3277 size_t len;
3278
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003279 if (nb_args != 2) {
3280 return;
3281 }
3282
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003283 len = strlen(str);
3284 readline_set_completion_index(rs, len);
3285 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
3286 while (elt) {
3287 const char *name;
3288
3289 name = object_class_get_name(OBJECT_CLASS(elt->data));
3290 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
3291 readline_add_completion(rs, name);
3292 }
3293 elt = elt->next;
3294 }
3295 g_slist_free(list);
3296}
3297
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003298static void peripheral_device_del_completion(ReadLineState *rs,
3299 const char *str, size_t len)
3300{
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003301 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
3302 GSList *list, *item;
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003303
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003304 list = qdev_build_hotpluggable_device_list(peripheral);
3305 if (!list) {
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003306 return;
3307 }
3308
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003309 for (item = list; item; item = g_slist_next(item)) {
3310 DeviceState *dev = item->data;
3311
3312 if (dev->id && !strncmp(str, dev->id, len)) {
3313 readline_add_completion(rs, dev->id);
3314 }
3315 }
3316
3317 g_slist_free(list);
3318}
3319
Hani Benhabiles6297d9a2014-05-07 23:41:28 +01003320void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3321{
3322 size_t len;
3323 ChardevInfoList *list, *start;
3324
3325 if (nb_args != 2) {
3326 return;
3327 }
3328 len = strlen(str);
3329 readline_set_completion_index(rs, len);
3330
3331 start = list = qmp_query_chardev(NULL);
3332 while (list) {
3333 ChardevInfo *chr = list->value;
3334
3335 if (!strncmp(chr->label, str, len)) {
3336 readline_add_completion(rs, chr->label);
3337 }
3338 list = list->next;
3339 }
3340 qapi_free_ChardevInfoList(start);
3341}
3342
Hani Benhabiles8e597772014-05-27 23:39:30 +01003343static void ringbuf_completion(ReadLineState *rs, const char *str)
3344{
3345 size_t len;
3346 ChardevInfoList *list, *start;
3347
3348 len = strlen(str);
3349 readline_set_completion_index(rs, len);
3350
3351 start = list = qmp_query_chardev(NULL);
3352 while (list) {
3353 ChardevInfo *chr_info = list->value;
3354
3355 if (!strncmp(chr_info->label, str, len)) {
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03003356 Chardev *chr = qemu_chr_find(chr_info->label);
Marc-André Lureau777357d2016-12-07 18:39:10 +03003357 if (chr && CHARDEV_IS_RINGBUF(chr)) {
Hani Benhabiles8e597772014-05-27 23:39:30 +01003358 readline_add_completion(rs, chr_info->label);
3359 }
3360 }
3361 list = list->next;
3362 }
3363 qapi_free_ChardevInfoList(start);
3364}
3365
Hani Benhabiles8e597772014-05-27 23:39:30 +01003366void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
3367{
3368 if (nb_args != 2) {
3369 return;
3370 }
3371 ringbuf_completion(rs, str);
3372}
3373
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003374void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
3375{
3376 size_t len;
3377
3378 if (nb_args != 2) {
3379 return;
3380 }
3381
3382 len = strlen(str);
3383 readline_set_completion_index(rs, len);
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003384 peripheral_device_del_completion(rs, str, len);
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003385}
3386
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003387void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003388{
3389 ObjectPropertyInfoList *list, *start;
3390 size_t len;
3391
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003392 if (nb_args != 2) {
3393 return;
3394 }
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003395 len = strlen(str);
3396 readline_set_completion_index(rs, len);
3397
3398 start = list = qmp_qom_list("/objects", NULL);
3399 while (list) {
3400 ObjectPropertyInfo *info = list->value;
3401
3402 if (!strncmp(info->type, "child<", 5)
3403 && !strncmp(info->name, str, len)) {
3404 readline_add_completion(rs, info->name);
3405 }
3406 list = list->next;
3407 }
3408 qapi_free_ObjectPropertyInfoList(start);
3409}
3410
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003411void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
3412{
3413 int i;
3414 char *sep;
3415 size_t len;
3416
3417 if (nb_args != 2) {
3418 return;
3419 }
3420 sep = strrchr(str, '-');
3421 if (sep) {
3422 str = sep + 1;
3423 }
3424 len = strlen(str);
3425 readline_set_completion_index(rs, len);
Eric Blake7fb1cf12015-11-18 01:52:57 -07003426 for (i = 0; i < Q_KEY_CODE__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02003427 if (!strncmp(str, QKeyCode_str(i), len)) {
3428 readline_add_completion(rs, QKeyCode_str(i));
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003429 }
3430 }
3431}
3432
Hani Benhabiles40d19392014-05-07 23:41:30 +01003433void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
3434{
3435 size_t len;
3436
3437 len = strlen(str);
3438 readline_set_completion_index(rs, len);
3439 if (nb_args == 2) {
Jason Wangeaed4832015-04-23 14:21:38 +08003440 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles40d19392014-05-07 23:41:30 +01003441 int count, i;
3442 count = qemu_find_net_clients_except(NULL, ncs,
Eric Blakef394b2e2016-07-13 21:50:23 -06003443 NET_CLIENT_DRIVER_NONE,
Jason Wangeaed4832015-04-23 14:21:38 +08003444 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003445 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles40d19392014-05-07 23:41:30 +01003446 const char *name = ncs[i]->name;
3447 if (!strncmp(str, name, len)) {
3448 readline_add_completion(rs, name);
3449 }
3450 }
3451 } else if (nb_args == 3) {
3452 add_completion_option(rs, str, "on");
3453 add_completion_option(rs, str, "off");
3454 }
3455}
3456
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003457void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
3458{
3459 int len, count, i;
Jason Wangeaed4832015-04-23 14:21:38 +08003460 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003461
3462 if (nb_args != 2) {
3463 return;
3464 }
3465
3466 len = strlen(str);
3467 readline_set_completion_index(rs, len);
Eric Blakef394b2e2016-07-13 21:50:23 -06003468 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_DRIVER_NIC,
Jason Wangeaed4832015-04-23 14:21:38 +08003469 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003470 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003471 QemuOpts *opts;
3472 const char *name = ncs[i]->name;
3473 if (strncmp(str, name, len)) {
3474 continue;
3475 }
3476 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
3477 if (opts) {
3478 readline_add_completion(rs, name);
3479 }
3480 }
3481}
3482
Lluís Vilanovabd712112016-07-11 12:53:51 +02003483void info_trace_events_completion(ReadLineState *rs, int nb_args, const char *str)
3484{
3485 size_t len;
3486
3487 len = strlen(str);
3488 readline_set_completion_index(rs, len);
3489 if (nb_args == 2) {
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003490 TraceEventIter iter;
3491 TraceEvent *ev;
3492 char *pattern = g_strdup_printf("%s*", str);
3493 trace_event_iter_init(&iter, pattern);
3494 while ((ev = trace_event_iter_next(&iter)) != NULL) {
3495 readline_add_completion(rs, trace_event_get_name(ev));
Lluís Vilanovabd712112016-07-11 12:53:51 +02003496 }
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003497 g_free(pattern);
Lluís Vilanovabd712112016-07-11 12:53:51 +02003498 }
3499}
3500
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003501void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
3502{
3503 size_t len;
3504
3505 len = strlen(str);
3506 readline_set_completion_index(rs, len);
3507 if (nb_args == 2) {
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003508 TraceEventIter iter;
3509 TraceEvent *ev;
3510 char *pattern = g_strdup_printf("%s*", str);
3511 trace_event_iter_init(&iter, pattern);
3512 while ((ev = trace_event_iter_next(&iter)) != NULL) {
3513 readline_add_completion(rs, trace_event_get_name(ev));
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003514 }
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003515 g_free(pattern);
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003516 } else if (nb_args == 3) {
3517 add_completion_option(rs, str, "on");
3518 add_completion_option(rs, str, "off");
3519 }
3520}
3521
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003522void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
3523{
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003524 int i;
3525
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003526 if (nb_args != 2) {
3527 return;
3528 }
3529 readline_set_completion_index(rs, strlen(str));
Michal Privoznik14d53b42017-09-07 10:05:24 +02003530 for (i = 0; i < WATCHDOG_ACTION__MAX; i++) {
3531 add_completion_option(rs, str, WatchdogAction_str(i));
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003532 }
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003533}
3534
Hani Benhabilesc68a0402014-05-27 23:39:32 +01003535void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
3536 const char *str)
3537{
3538 size_t len;
3539
3540 len = strlen(str);
3541 readline_set_completion_index(rs, len);
3542 if (nb_args == 2) {
3543 int i;
Eric Blake7fb1cf12015-11-18 01:52:57 -07003544 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02003545 const char *name = MigrationCapability_str(i);
Hani Benhabilesc68a0402014-05-27 23:39:32 +01003546 if (!strncmp(str, name, len)) {
3547 readline_add_completion(rs, name);
3548 }
3549 }
3550 } else if (nb_args == 3) {
3551 add_completion_option(rs, str, "on");
3552 add_completion_option(rs, str, "off");
3553 }
3554}
3555
Liang Li50e9a622015-03-23 16:32:29 +08003556void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
3557 const char *str)
3558{
3559 size_t len;
3560
3561 len = strlen(str);
3562 readline_set_completion_index(rs, len);
3563 if (nb_args == 2) {
3564 int i;
Eric Blake7fb1cf12015-11-18 01:52:57 -07003565 for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
Markus Armbruster977c7362017-08-24 10:46:08 +02003566 const char *name = MigrationParameter_str(i);
Liang Li50e9a622015-03-23 16:32:29 +08003567 if (!strncmp(str, name, len)) {
3568 readline_add_completion(rs, name);
3569 }
3570 }
3571 }
3572}
3573
Hani Benhabilese3bb5322014-05-27 23:39:34 +01003574void host_net_add_completion(ReadLineState *rs, int nb_args, const char *str)
3575{
3576 int i;
3577 size_t len;
3578 if (nb_args != 2) {
3579 return;
3580 }
3581 len = strlen(str);
3582 readline_set_completion_index(rs, len);
3583 for (i = 0; host_net_devices[i]; i++) {
3584 if (!strncmp(host_net_devices[i], str, len)) {
3585 readline_add_completion(rs, host_net_devices[i]);
3586 }
3587 }
3588}
3589
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003590void host_net_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3591{
Jason Wangeaed4832015-04-23 14:21:38 +08003592 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003593 int count, i, len;
3594
3595 len = strlen(str);
3596 readline_set_completion_index(rs, len);
3597 if (nb_args == 2) {
3598 count = qemu_find_net_clients_except(NULL, ncs,
Eric Blakef394b2e2016-07-13 21:50:23 -06003599 NET_CLIENT_DRIVER_NONE,
Jason Wangeaed4832015-04-23 14:21:38 +08003600 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003601 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003602 int id;
3603 char name[16];
3604
3605 if (net_hub_id_for_client(ncs[i], &id)) {
3606 continue;
3607 }
3608 snprintf(name, sizeof(name), "%d", id);
3609 if (!strncmp(str, name, len)) {
3610 readline_add_completion(rs, name);
3611 }
3612 }
3613 return;
3614 } else if (nb_args == 3) {
3615 count = qemu_find_net_clients_except(NULL, ncs,
Eric Blakef394b2e2016-07-13 21:50:23 -06003616 NET_CLIENT_DRIVER_NIC,
Jason Wangeaed4832015-04-23 14:21:38 +08003617 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003618 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Jason Wang2c4681f2015-02-02 15:06:38 +08003619 int id;
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003620 const char *name;
3621
Eric Blakef394b2e2016-07-13 21:50:23 -06003622 if (ncs[i]->info->type == NET_CLIENT_DRIVER_HUBPORT ||
Jason Wang2c4681f2015-02-02 15:06:38 +08003623 net_hub_id_for_client(ncs[i], &id)) {
3624 continue;
3625 }
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003626 name = ncs[i]->name;
3627 if (!strncmp(str, name, len)) {
3628 readline_add_completion(rs, name);
3629 }
3630 }
3631 return;
3632 }
3633}
3634
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003635static void vm_completion(ReadLineState *rs, const char *str)
3636{
3637 size_t len;
Kevin Wolf7c8eece2016-03-22 18:58:50 +01003638 BlockDriverState *bs;
Kevin Wolf88be7b42016-05-20 18:49:07 +02003639 BdrvNextIterator it;
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003640
3641 len = strlen(str);
3642 readline_set_completion_index(rs, len);
Kevin Wolf7c8eece2016-03-22 18:58:50 +01003643
Kevin Wolf88be7b42016-05-20 18:49:07 +02003644 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003645 SnapshotInfoList *snapshots, *snapshot;
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003646 AioContext *ctx = bdrv_get_aio_context(bs);
3647 bool ok = false;
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003648
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003649 aio_context_acquire(ctx);
3650 if (bdrv_can_snapshot(bs)) {
3651 ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0;
3652 }
3653 aio_context_release(ctx);
3654 if (!ok) {
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003655 continue;
3656 }
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003657
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003658 snapshot = snapshots;
3659 while (snapshot) {
3660 char *completion = snapshot->value->name;
3661 if (!strncmp(str, completion, len)) {
3662 readline_add_completion(rs, completion);
3663 }
3664 completion = snapshot->value->id;
3665 if (!strncmp(str, completion, len)) {
3666 readline_add_completion(rs, completion);
3667 }
3668 snapshot = snapshot->next;
3669 }
3670 qapi_free_SnapshotInfoList(snapshots);
3671 }
3672
3673}
3674
3675void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
3676{
3677 if (nb_args == 2) {
3678 vm_completion(rs, str);
3679 }
3680}
3681
3682void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
3683{
3684 if (nb_args == 2) {
3685 vm_completion(rs, str);
3686 }
3687}
3688
Wenchao Xiac35b6402013-08-27 20:38:24 +08003689static void monitor_find_completion_by_table(Monitor *mon,
3690 const mon_cmd_t *cmd_table,
3691 char **args,
3692 int nb_args)
bellard81d09122004-07-14 17:21:37 +00003693{
3694 const char *cmdname;
Wenchao Xiac35b6402013-08-27 20:38:24 +08003695 int i;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003696 const char *ptype, *str, *name;
Anthony Liguoric227f092009-10-01 16:12:16 -05003697 const mon_cmd_t *cmd;
Max Reitzda27a002016-03-16 19:54:29 +01003698 BlockBackend *blk = NULL;
bellard81d09122004-07-14 17:21:37 +00003699
bellard81d09122004-07-14 17:21:37 +00003700 if (nb_args <= 1) {
3701 /* command completion */
3702 if (nb_args == 0)
3703 cmdname = "";
3704 else
3705 cmdname = args[0];
Wenchao Xiad2674b22013-08-27 20:38:16 +08003706 readline_set_completion_index(mon->rs, strlen(cmdname));
Wenchao Xiac35b6402013-08-27 20:38:24 +08003707 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003708 cmd_completion(mon, cmdname, cmd->name);
bellard81d09122004-07-14 17:21:37 +00003709 }
3710 } else {
3711 /* find the command */
Wenchao Xiac35b6402013-08-27 20:38:24 +08003712 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Jan Kiszka03a63482010-06-16 00:38:33 +02003713 if (compare_cmd(args[0], cmd->name)) {
3714 break;
3715 }
bellard81d09122004-07-14 17:21:37 +00003716 }
Jan Kiszka03a63482010-06-16 00:38:33 +02003717 if (!cmd->name) {
Wenchao Xiac35b6402013-08-27 20:38:24 +08003718 return;
Jan Kiszka03a63482010-06-16 00:38:33 +02003719 }
3720
Wenchao Xiad903a772013-08-27 20:38:25 +08003721 if (cmd->sub_table) {
3722 /* do the job again */
Stefan Weile7ae7712015-03-08 19:30:01 +01003723 monitor_find_completion_by_table(mon, cmd->sub_table,
3724 &args[1], nb_args - 1);
3725 return;
Wenchao Xiad903a772013-08-27 20:38:25 +08003726 }
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003727 if (cmd->command_completion) {
Stefan Weile7ae7712015-03-08 19:30:01 +01003728 cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
3729 return;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003730 }
Wenchao Xiad903a772013-08-27 20:38:25 +08003731
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003732 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003733 for(i = 0; i < nb_args - 2; i++) {
3734 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003735 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003736 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003737 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003738 }
3739 }
3740 str = args[nb_args - 1];
Kevin Wolf48fe86f2014-11-12 16:24:02 +01003741 while (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02003742 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00003743 }
bellard81d09122004-07-14 17:21:37 +00003744 switch(*ptype) {
3745 case 'F':
3746 /* file completion */
Wenchao Xiad2674b22013-08-27 20:38:16 +08003747 readline_set_completion_index(mon->rs, strlen(str));
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003748 file_completion(mon, str);
bellard81d09122004-07-14 17:21:37 +00003749 break;
3750 case 'B':
3751 /* block device name completion */
Wenchao Xia599a9262013-08-27 20:38:15 +08003752 readline_set_completion_index(mon->rs, strlen(str));
Max Reitzda27a002016-03-16 19:54:29 +01003753 while ((blk = blk_next(blk)) != NULL) {
3754 name = blk_name(blk);
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003755 if (str[0] == '\0' ||
3756 !strncmp(name, str, strlen(str))) {
3757 readline_add_completion(mon->rs, name);
3758 }
3759 }
bellard81d09122004-07-14 17:21:37 +00003760 break;
bellard7fe48482004-10-09 18:08:01 +00003761 case 's':
Wenchao Xia129be002013-08-27 20:38:26 +08003762 case 'S':
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003763 if (!strcmp(cmd->name, "help|?")) {
Wenchao Xia7ca0e062013-08-27 20:38:27 +08003764 monitor_find_completion_by_table(mon, cmd_table,
3765 &args[1], nb_args - 1);
bellard7fe48482004-10-09 18:08:01 +00003766 }
3767 break;
bellard81d09122004-07-14 17:21:37 +00003768 default:
3769 break;
3770 }
3771 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08003772}
3773
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003774static void monitor_find_completion(void *opaque,
Wenchao Xiac35b6402013-08-27 20:38:24 +08003775 const char *cmdline)
3776{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003777 Monitor *mon = opaque;
Wenchao Xiac35b6402013-08-27 20:38:24 +08003778 char *args[MAX_ARGS];
3779 int nb_args, len;
3780
3781 /* 1. parse the cmdline */
3782 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
3783 return;
3784 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08003785
3786 /* if the line ends with a space, it means we want to complete the
3787 next arg */
3788 len = strlen(cmdline);
3789 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
3790 if (nb_args >= MAX_ARGS) {
3791 goto cleanup;
3792 }
3793 args[nb_args++] = g_strdup("");
3794 }
3795
3796 /* 2. auto complete according to args */
3797 monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args);
Jan Kiszka03a63482010-06-16 00:38:33 +02003798
3799cleanup:
Wenchao Xiadcc70cd2013-08-27 20:38:22 +08003800 free_cmdline_args(args, nb_args);
bellard81d09122004-07-14 17:21:37 +00003801}
3802
aliguori731b0362009-03-05 23:01:42 +00003803static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003804{
aliguori731b0362009-03-05 23:01:42 +00003805 Monitor *mon = opaque;
3806
Jan Kiszkac62313b2009-12-04 14:05:29 +01003807 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003808}
3809
Paolo Bonzini95385fe2015-11-25 22:23:31 +01003810static void handle_qmp_command(JSONMessageParser *parser, GQueue *tokens)
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003811{
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04003812 QObject *req, *rsp = NULL, *id = NULL;
3813 QDict *qdict = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003814 Monitor *mon = cur_mon;
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04003815 Error *err = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003816
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04003817 req = json_parser_parse_err(tokens, NULL, &err);
Markus Armbrusterbbf10282017-02-17 21:38:24 +01003818 if (!req && !err) {
3819 /* json_parser_parse_err() sucks: can fail without setting @err */
3820 error_setg(&err, QERR_JSON_PARSING);
3821 }
3822 if (err) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003823 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003824 }
3825
Markus Armbruster104fc302017-03-03 13:32:28 +01003826 qdict = qobject_to_qdict(req);
3827 if (qdict) {
3828 id = qdict_get(qdict, "id");
3829 qobject_incref(id);
3830 qdict_del(qdict, "id");
3831 } /* else will fail qmp_dispatch() */
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04003832
Denis V. Lunevbd6952a2017-07-25 17:39:23 +03003833 if (trace_event_get_state_backends(TRACE_HANDLE_QMP_COMMAND)) {
3834 QString *req_json = qobject_to_json(req);
3835 trace_handle_qmp_command(mon, qstring_get_str(req_json));
3836 QDECREF(req_json);
3837 }
Stefan Hajnoczib097efc2017-06-05 11:42:16 +01003838
Markus Armbruster635db182017-03-03 13:32:27 +01003839 rsp = qmp_dispatch(cur_mon->qmp.commands, req);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003840
Markus Armbruster635db182017-03-03 13:32:27 +01003841 if (mon->qmp.commands == &qmp_cap_negotiation_commands) {
3842 qdict = qdict_get_qdict(qobject_to_qdict(rsp), "error");
3843 if (qdict
3844 && !g_strcmp0(qdict_get_try_str(qdict, "class"),
Markus Armbruster977c7362017-08-24 10:46:08 +02003845 QapiErrorClass_str(ERROR_CLASS_COMMAND_NOT_FOUND))) {
Markus Armbruster635db182017-03-03 13:32:27 +01003846 /* Provide a more useful error message */
3847 qdict_del(qdict, "desc");
Eric Blake46f5ac22017-04-27 16:58:17 -05003848 qdict_put_str(qdict, "desc", "Expecting capabilities negotiation"
3849 " with 'qmp_capabilities'");
Markus Armbruster635db182017-03-03 13:32:27 +01003850 }
3851 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003852
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003853err_out:
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04003854 if (err) {
3855 qdict = qdict_new();
3856 qdict_put_obj(qdict, "error", qmp_build_error_object(err));
3857 error_free(err);
3858 rsp = QOBJECT(qdict);
3859 }
3860
3861 if (rsp) {
3862 if (id) {
3863 qdict_put_obj(qobject_to_qdict(rsp), "id", id);
3864 id = NULL;
3865 }
3866
3867 monitor_json_emitter(mon, rsp);
3868 }
3869
3870 qobject_decref(id);
3871 qobject_decref(rsp);
3872 qobject_decref(req);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003873}
3874
Markus Armbrusterc83fe232015-03-06 19:20:51 +01003875static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003876{
3877 Monitor *old_mon = cur_mon;
3878
3879 cur_mon = opaque;
3880
Markus Armbruster74358f22015-03-06 19:35:59 +01003881 json_message_parser_feed(&cur_mon->qmp.parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003882
3883 cur_mon = old_mon;
3884}
3885
aliguori731b0362009-03-05 23:01:42 +00003886static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00003887{
aliguori731b0362009-03-05 23:01:42 +00003888 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00003889 int i;
aliguori376253e2009-03-05 23:01:23 +00003890
aliguori731b0362009-03-05 23:01:42 +00003891 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00003892
aliguoricde76ee2009-03-05 23:01:51 +00003893 if (cur_mon->rs) {
3894 for (i = 0; i < size; i++)
3895 readline_handle_byte(cur_mon->rs, buf[i]);
3896 } else {
3897 if (size == 0 || buf[size - 1] != 0)
3898 monitor_printf(cur_mon, "corrupted command\n");
3899 else
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003900 handle_hmp_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00003901 }
aliguori731b0362009-03-05 23:01:42 +00003902
3903 cur_mon = old_mon;
3904}
aliguorid8f44602008-10-06 13:52:44 +00003905
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003906static void monitor_command_cb(void *opaque, const char *cmdline,
3907 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00003908{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003909 Monitor *mon = opaque;
3910
aliguori731b0362009-03-05 23:01:42 +00003911 monitor_suspend(mon);
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003912 handle_hmp_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00003913 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00003914}
3915
aliguoricde76ee2009-03-05 23:01:51 +00003916int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003917{
aliguoricde76ee2009-03-05 23:01:51 +00003918 if (!mon->rs)
3919 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00003920 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00003921 return 0;
aliguorid8f44602008-10-06 13:52:44 +00003922}
3923
aliguori376253e2009-03-05 23:01:23 +00003924void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003925{
aliguoricde76ee2009-03-05 23:01:51 +00003926 if (!mon->rs)
3927 return;
aliguori731b0362009-03-05 23:01:42 +00003928 if (--mon->suspend_cnt == 0)
3929 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00003930}
3931
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003932static QObject *get_qmp_greeting(void)
3933{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03003934 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003935
Markus Armbruster7fad30f2015-09-16 13:06:19 +02003936 qmp_marshal_query_version(NULL, &ver, NULL);
Marc-André Lureauc8235012016-09-12 13:19:04 +04003937
3938 return qobject_from_jsonf("{'QMP': {'version': %p, 'capabilities': []}}",
3939 ver);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003940}
3941
Markus Armbrusterc83fe232015-03-06 19:20:51 +01003942static void monitor_qmp_event(void *opaque, int event)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003943{
Luiz Capitulino47116d12010-02-08 17:01:30 -02003944 QObject *data;
3945 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003946
Luiz Capitulino47116d12010-02-08 17:01:30 -02003947 switch (event) {
3948 case CHR_EVENT_OPENED:
Markus Armbruster635db182017-03-03 13:32:27 +01003949 mon->qmp.commands = &qmp_cap_negotiation_commands;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003950 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003951 monitor_json_emitter(mon, data);
3952 qobject_decref(data);
Corey Bryantefb87c12012-08-14 16:43:48 -04003953 mon_refcount++;
Luiz Capitulino47116d12010-02-08 17:01:30 -02003954 break;
3955 case CHR_EVENT_CLOSED:
Markus Armbruster74358f22015-03-06 19:35:59 +01003956 json_message_parser_destroy(&mon->qmp.parser);
3957 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Corey Bryantefb87c12012-08-14 16:43:48 -04003958 mon_refcount--;
3959 monitor_fdsets_cleanup();
Luiz Capitulino47116d12010-02-08 17:01:30 -02003960 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003961 }
3962}
3963
aliguori731b0362009-03-05 23:01:42 +00003964static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00003965{
aliguori376253e2009-03-05 23:01:23 +00003966 Monitor *mon = opaque;
3967
aliguori2724b182009-03-05 23:01:47 +00003968 switch (event) {
3969 case CHR_EVENT_MUX_IN:
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003970 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003971 mon->mux_out = 0;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003972 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003973 if (mon->reset_seen) {
3974 readline_restart(mon->rs);
3975 monitor_resume(mon);
3976 monitor_flush(mon);
3977 } else {
3978 mon->suspend_cnt = 0;
3979 }
aliguori2724b182009-03-05 23:01:47 +00003980 break;
ths86e94de2007-01-05 22:01:59 +00003981
aliguori2724b182009-03-05 23:01:47 +00003982 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003983 if (mon->reset_seen) {
3984 if (mon->suspend_cnt == 0) {
3985 monitor_printf(mon, "\n");
3986 }
3987 monitor_flush(mon);
3988 monitor_suspend(mon);
3989 } else {
3990 mon->suspend_cnt++;
3991 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003992 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003993 mon->mux_out = 1;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003994 qemu_mutex_unlock(&mon->out_lock);
aliguori2724b182009-03-05 23:01:47 +00003995 break;
3996
Amit Shahb6b8df52009-10-07 18:31:16 +05303997 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00003998 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3999 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004000 if (!mon->mux_out) {
Stratos Psomadakise5554e22014-09-15 15:34:57 +03004001 readline_restart(mon->rs);
aliguori2724b182009-03-05 23:01:47 +00004002 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004003 }
4004 mon->reset_seen = 1;
Corey Bryantefb87c12012-08-14 16:43:48 -04004005 mon_refcount++;
4006 break;
4007
4008 case CHR_EVENT_CLOSED:
4009 mon_refcount--;
4010 monitor_fdsets_cleanup();
aliguori2724b182009-03-05 23:01:47 +00004011 break;
4012 }
ths86e94de2007-01-05 22:01:59 +00004013}
4014
Wayne Xia816f8922011-10-12 11:32:41 +08004015static int
4016compare_mon_cmd(const void *a, const void *b)
4017{
4018 return strcmp(((const mon_cmd_t *)a)->name,
4019 ((const mon_cmd_t *)b)->name);
4020}
4021
4022static void sortcmdlist(void)
4023{
4024 int array_num;
4025 int elem_size = sizeof(mon_cmd_t);
4026
4027 array_num = sizeof(mon_cmds)/elem_size-1;
4028 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
4029
4030 array_num = sizeof(info_cmds)/elem_size-1;
4031 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
4032}
4033
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004034/* These functions just adapt the readline interface in a typesafe way. We
4035 * could cast function pointers but that discards compiler checks.
4036 */
Stefan Weild5d15072014-01-25 18:18:23 +01004037static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
4038 const char *fmt, ...)
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004039{
4040 va_list ap;
4041 va_start(ap, fmt);
4042 monitor_vprintf(opaque, fmt, ap);
4043 va_end(ap);
4044}
4045
4046static void monitor_readline_flush(void *opaque)
4047{
4048 monitor_flush(opaque);
4049}
4050
Paolo Bonzini397d30e2016-10-24 18:31:02 +02004051/*
4052 * Print to current monitor if we have one, else to stderr.
4053 * TODO should return int, so callers can calculate width, but that
4054 * requires surgery to monitor_vprintf(). Left for another day.
4055 */
4056void error_vprintf(const char *fmt, va_list ap)
4057{
4058 if (cur_mon && !monitor_cur_is_qmp()) {
4059 monitor_vprintf(cur_mon, fmt, ap);
4060 } else {
4061 vfprintf(stderr, fmt, ap);
4062 }
4063}
4064
4065void error_vprintf_unless_qmp(const char *fmt, va_list ap)
4066{
4067 if (cur_mon && !monitor_cur_is_qmp()) {
4068 monitor_vprintf(cur_mon, fmt, ap);
Marc-André Lureau0d6b50d2017-01-05 14:59:57 +01004069 } else if (!cur_mon) {
4070 vfprintf(stderr, fmt, ap);
Paolo Bonzini397d30e2016-10-24 18:31:02 +02004071 }
4072}
4073
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004074static void __attribute__((constructor)) monitor_lock_init(void)
4075{
4076 qemu_mutex_init(&monitor_lock);
4077}
4078
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03004079void monitor_init(Chardev *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004080{
aliguori731b0362009-03-05 23:01:42 +00004081 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004082 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004083
4084 if (is_first_init) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +02004085 monitor_qapi_event_init();
Wenchao Xiad0383172013-08-27 20:38:18 +08004086 sortcmdlist();
ths20d8a3e2007-02-18 17:04:49 +00004087 is_first_init = 0;
4088 }
aliguori87127162009-03-05 23:01:29 +00004089
Wenchao Xiab01fe892013-08-27 20:38:19 +08004090 mon = g_malloc(sizeof(*mon));
4091 monitor_data_init(mon);
ths20d8a3e2007-02-18 17:04:49 +00004092
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03004093 qemu_chr_fe_init(&mon->chr, chr, &error_abort);
aliguori731b0362009-03-05 23:01:42 +00004094 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004095 if (flags & MONITOR_USE_READLINE) {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004096 mon->rs = readline_init(monitor_readline_printf,
4097 monitor_readline_flush,
4098 mon,
4099 monitor_find_completion);
aliguoricde76ee2009-03-05 23:01:51 +00004100 monitor_read_command(mon, 0);
4101 }
aliguori87127162009-03-05 23:01:29 +00004102
Markus Armbruster9f3982f2015-03-06 19:56:38 +01004103 if (monitor_is_qmp(mon)) {
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03004104 qemu_chr_fe_set_handlers(&mon->chr, monitor_can_read, monitor_qmp_read,
Anton Nefedov81517ba2017-07-06 15:08:49 +03004105 monitor_qmp_event, NULL, mon, NULL, true);
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03004106 qemu_chr_fe_set_echo(&mon->chr, true);
Markus Armbruster74358f22015-03-06 19:35:59 +01004107 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004108 } else {
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03004109 qemu_chr_fe_set_handlers(&mon->chr, monitor_can_read, monitor_read,
Anton Nefedov81517ba2017-07-06 15:08:49 +03004110 monitor_event, NULL, mon, NULL, true);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004111 }
aliguori87127162009-03-05 23:01:29 +00004112
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004113 qemu_mutex_lock(&monitor_lock);
Blue Swirl72cf2d42009-09-12 07:36:22 +00004114 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004115 qemu_mutex_unlock(&monitor_lock);
bellard7e2515e2004-08-01 21:52:19 +00004116}
4117
Marc-André Lureau2ef45712016-08-01 15:23:42 +04004118void monitor_cleanup(void)
4119{
4120 Monitor *mon, *next;
4121
4122 qemu_mutex_lock(&monitor_lock);
4123 QLIST_FOREACH_SAFE(mon, &mon_list, entry, next) {
4124 QLIST_REMOVE(mon, entry);
4125 monitor_data_destroy(mon);
4126 g_free(mon);
4127 }
4128 qemu_mutex_unlock(&monitor_lock);
4129}
4130
Paolo Bonzini4d454572012-11-26 16:03:42 +01004131QemuOptsList qemu_mon_opts = {
4132 .name = "mon",
4133 .implied_opt_name = "chardev",
4134 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
4135 .desc = {
4136 {
4137 .name = "mode",
4138 .type = QEMU_OPT_STRING,
4139 },{
4140 .name = "chardev",
4141 .type = QEMU_OPT_STRING,
4142 },{
Marc-André Lureaubdbcb542016-10-11 21:41:21 +04004143 .name = "default", /* deprecated */
Paolo Bonzini4d454572012-11-26 16:03:42 +01004144 .type = QEMU_OPT_BOOL,
4145 },{
4146 .name = "pretty",
4147 .type = QEMU_OPT_BOOL,
4148 },
4149 { /* end of list */ }
4150 },
4151};
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004152
4153#ifndef TARGET_I386
4154void qmp_rtc_reset_reinjection(Error **errp)
4155{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004156 error_setg(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection");
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004157}
4158#endif
Jason J. Herne7ee0c3e2015-06-26 14:03:16 -04004159
4160#ifndef TARGET_S390X
4161void qmp_dump_skeys(const char *filename, Error **errp)
4162{
4163 error_setg(errp, QERR_FEATURE_DISABLED, "dump-skeys");
4164}
4165#endif
Peter Xuae50a772016-03-30 17:27:24 +01004166
4167#ifndef TARGET_ARM
4168GICCapabilityList *qmp_query_gic_capabilities(Error **errp)
4169{
4170 error_setg(errp, QERR_FEATURE_DISABLED, "query-gic-capabilities");
4171 return NULL;
4172}
4173#endif
Igor Mammedovd4633542016-06-10 06:29:06 +05304174
4175HotpluggableCPUList *qmp_query_hotpluggable_cpus(Error **errp)
4176{
4177 MachineState *ms = MACHINE(qdev_get_machine());
4178 MachineClass *mc = MACHINE_GET_CLASS(ms);
4179
Igor Mammedovc5514d02017-02-10 11:20:57 +01004180 if (!mc->has_hotpluggable_cpus) {
Igor Mammedovd4633542016-06-10 06:29:06 +05304181 error_setg(errp, QERR_FEATURE_DISABLED, "query-hotpluggable-cpus");
4182 return NULL;
4183 }
4184
Igor Mammedovc5514d02017-02-10 11:20:57 +01004185 return machine_query_hotpluggable_cpus(ms);
Igor Mammedovd4633542016-06-10 06:29:06 +05304186}