blob: d8ac20f6caf7302dccb9fc716731d80fcd86489c [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"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010031#include "hw/i386/pc.h"
Michael S. Tsirkina2cb15b2012-12-12 14:24:50 +020032#include "hw/pci/pci.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010033#include "sysemu/watchdog.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +020034#include "hw/loader.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010035#include "exec/gdbstub.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020036#include "net/net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000037#include "net/slirp.h"
Marc-André Lureau4d43a602017-01-26 18:26:44 +040038#include "chardev/char-fe.h"
Gerd Hoffmann75721502010-10-07 12:22:54 +020039#include "ui/qemu-spice.h"
Eduardo Habkoste35704b2015-02-08 16:51:16 -020040#include "sysemu/numa.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010041#include "monitor/monitor.h"
Marc-André Lureau213dcb02016-12-12 20:22:24 +030042#include "qemu/config-file.h"
Stefan Hajnoczi0150cd82013-11-14 11:54:15 +010043#include "qemu/readline.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010044#include "ui/console.h"
Gerd Hoffmannc751a742013-12-04 15:02:28 +010045#include "ui/input.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010046#include "sysemu/blockdev.h"
Max Reitzda27a002016-03-16 19:54:29 +010047#include "sysemu/block-backend.h"
pbrook87ecb682007-11-17 17:14:51 +000048#include "audio/audio.h"
Paolo Bonzini76cad712012-10-24 11:12:21 +020049#include "disas/disas.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010050#include "sysemu/balloon.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010051#include "qemu/timer.h"
Vincent Palatinb3946622017-01-10 11:59:55 +010052#include "sysemu/hw_accel.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010053#include "qemu/acl.h"
Paolo Bonzinibdee56f2013-04-02 18:28:41 +020054#include "sysemu/tpm.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010055#include "qapi/qmp/qerror.h"
Eric Blakec7eb39c2016-06-09 10:48:32 -060056#include "qapi/qmp/types.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010057#include "qapi/qmp/qjson.h"
58#include "qapi/qmp/json-streamer.h"
59#include "qapi/qmp/json-parser.h"
Markus Armbrustera9c94272016-06-22 19:11:19 +020060#include "qom/object_interfaces.h"
Daniel P. Berrange0ab8ed12017-01-25 16:14:15 +000061#include "trace-root.h"
Lluís31965ae2011-08-31 20:31:24 +020062#include "trace/control.h"
Pavel Butsykinbf957282015-09-10 18:38:59 +030063#include "monitor/hmp-target.h"
Lluís6d8a7642011-08-31 20:30:43 +020064#ifdef CONFIG_TRACE_SIMPLE
Lluís31965ae2011-08-31 20:31:24 +020065#include "trace/simple.h"
Prerna Saxena22890ab2010-06-24 17:04:53 +053066#endif
Paolo Bonzini022c62c2012-12-17 18:19:49 +010067#include "exec/memory.h"
Paolo Bonzini63c91552016-03-15 13:18:37 +010068#include "exec/exec-all.h"
Paolo Bonzini03dd0242015-12-15 13:16:16 +010069#include "qemu/log.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050070#include "qmp-commands.h"
71#include "hmp.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010072#include "qemu/thread.h"
Hani Benhabilesb21631f2014-05-27 23:39:37 +010073#include "block/qapi.h"
Wenchao Xia43a14cf2014-06-18 08:43:31 +020074#include "qapi/qmp-event.h"
75#include "qapi-event.h"
Markus Armbruster39a18152015-09-16 13:06:28 +020076#include "qmp-introspect.h"
Alberto Garciadc599972016-03-10 13:55:26 +020077#include "sysemu/qtest.h"
Paolo Bonzinid2528bd2017-03-03 12:01:16 +010078#include "sysemu/cpus.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020079#include "qemu/cutils.h"
Marc-André Lureauedcfaef2016-09-12 13:19:00 +040080#include "qapi/qmp/dispatch.h"
ths6a5bd302007-12-03 17:05:38 +000081
Jason J. Hernea4538a52015-06-26 14:07:21 -040082#if defined(TARGET_S390X)
83#include "hw/s390x/storage-keys.h"
84#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;
Andreas Färbercb446ec2013-05-01 14:24:52 +0200202 CPUState *mon_cpu;
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{
Marc-André Lureau1ce26102017-01-27 00:49:13 +0400581 qemu_chr_fe_deinit(&mon->chr, false);
Marc-André Lureau2ef45712016-08-01 15:23:42 +0400582 if (monitor_is_qmp(mon)) {
583 json_message_parser_destroy(&mon->qmp.parser);
584 }
585 g_free(mon->rs);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800586 QDECREF(mon->outbuf);
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200587 qemu_mutex_destroy(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800588}
589
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200590char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
591 int64_t cpu_index, Error **errp)
Luiz Capitulino0268d972010-10-22 10:08:02 -0200592{
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200593 char *output = NULL;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200594 Monitor *old_mon, hmp;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200595
Wenchao Xiab01fe892013-08-27 20:38:19 +0800596 monitor_data_init(&hmp);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400597 hmp.skip_flush = true;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200598
599 old_mon = cur_mon;
600 cur_mon = &hmp;
601
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200602 if (has_cpu_index) {
603 int ret = monitor_set_cpu(cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200604 if (ret < 0) {
605 cur_mon = old_mon;
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100606 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
607 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200608 goto out;
609 }
610 }
611
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100612 handle_hmp_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200613 cur_mon = old_mon;
614
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200615 qemu_mutex_lock(&hmp.out_lock);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400616 if (qstring_get_length(hmp.outbuf) > 0) {
617 output = g_strdup(qstring_get_str(hmp.outbuf));
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200618 } else {
619 output = g_strdup("");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200620 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200621 qemu_mutex_unlock(&hmp.out_lock);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200622
623out:
Wenchao Xiab01fe892013-08-27 20:38:19 +0800624 monitor_data_destroy(&hmp);
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200625 return output;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200626}
627
bellard9dc39cb2004-03-14 21:38:27 +0000628static int compare_cmd(const char *name, const char *list)
629{
630 const char *p, *pstart;
631 int len;
632 len = strlen(name);
633 p = list;
634 for(;;) {
635 pstart = p;
636 p = strchr(p, '|');
637 if (!p)
638 p = pstart + strlen(pstart);
639 if ((p - pstart) == len && !memcmp(pstart, name, len))
640 return 1;
641 if (*p == '\0')
642 break;
643 p++;
644 }
645 return 0;
646}
647
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800648static int get_str(char *buf, int buf_size, const char **pp)
649{
650 const char *p;
651 char *q;
652 int c;
653
654 q = buf;
655 p = *pp;
656 while (qemu_isspace(*p)) {
657 p++;
658 }
659 if (*p == '\0') {
660 fail:
661 *q = '\0';
662 *pp = p;
663 return -1;
664 }
665 if (*p == '\"') {
666 p++;
667 while (*p != '\0' && *p != '\"') {
668 if (*p == '\\') {
669 p++;
670 c = *p++;
671 switch (c) {
672 case 'n':
673 c = '\n';
674 break;
675 case 'r':
676 c = '\r';
677 break;
678 case '\\':
679 case '\'':
680 case '\"':
681 break;
682 default:
Peter Maydell71baf782015-08-19 16:20:19 +0100683 printf("unsupported escape code: '\\%c'\n", c);
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800684 goto fail;
685 }
686 if ((q - buf) < buf_size - 1) {
687 *q++ = c;
688 }
689 } else {
690 if ((q - buf) < buf_size - 1) {
691 *q++ = *p;
692 }
693 p++;
694 }
695 }
696 if (*p != '\"') {
Peter Maydell71baf782015-08-19 16:20:19 +0100697 printf("unterminated string\n");
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800698 goto fail;
699 }
700 p++;
701 } else {
702 while (*p != '\0' && !qemu_isspace(*p)) {
703 if ((q - buf) < buf_size - 1) {
704 *q++ = *p;
705 }
706 p++;
707 }
708 }
709 *q = '\0';
710 *pp = p;
711 return 0;
712}
713
714#define MAX_ARGS 16
715
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800716static void free_cmdline_args(char **args, int nb_args)
717{
718 int i;
719
720 assert(nb_args <= MAX_ARGS);
721
722 for (i = 0; i < nb_args; i++) {
723 g_free(args[i]);
724 }
725
726}
727
728/*
729 * Parse the command line to get valid args.
730 * @cmdline: command line to be parsed.
731 * @pnb_args: location to store the number of args, must NOT be NULL.
732 * @args: location to store the args, which should be freed by caller, must
733 * NOT be NULL.
734 *
735 * Returns 0 on success, negative on failure.
736 *
737 * NOTE: this parser is an approximate form of the real command parser. Number
738 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
739 * return with failure.
740 */
741static int parse_cmdline(const char *cmdline,
742 int *pnb_args, char **args)
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800743{
744 const char *p;
745 int nb_args, ret;
746 char buf[1024];
747
748 p = cmdline;
749 nb_args = 0;
750 for (;;) {
751 while (qemu_isspace(*p)) {
752 p++;
753 }
754 if (*p == '\0') {
755 break;
756 }
757 if (nb_args >= MAX_ARGS) {
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800758 goto fail;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800759 }
760 ret = get_str(buf, sizeof(buf), &p);
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800761 if (ret < 0) {
762 goto fail;
763 }
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800764 args[nb_args] = g_strdup(buf);
765 nb_args++;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800766 }
767 *pnb_args = nb_args;
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800768 return 0;
769
770 fail:
771 free_cmdline_args(args, nb_args);
772 return -1;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800773}
774
Wenchao Xia66855492013-08-27 20:38:23 +0800775static void help_cmd_dump_one(Monitor *mon,
776 const mon_cmd_t *cmd,
777 char **prefix_args,
778 int prefix_args_nb)
779{
780 int i;
781
782 for (i = 0; i < prefix_args_nb; i++) {
783 monitor_printf(mon, "%s ", prefix_args[i]);
784 }
785 monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
786}
787
788/* @args[@arg_index] is the valid command need to find in @cmds */
Anthony Liguoric227f092009-10-01 16:12:16 -0500789static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
Wenchao Xia66855492013-08-27 20:38:23 +0800790 char **args, int nb_args, int arg_index)
bellard9dc39cb2004-03-14 21:38:27 +0000791{
Anthony Liguoric227f092009-10-01 16:12:16 -0500792 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000793
Wenchao Xia66855492013-08-27 20:38:23 +0800794 /* No valid arg need to compare with, dump all in *cmds */
795 if (arg_index >= nb_args) {
796 for (cmd = cmds; cmd->name != NULL; cmd++) {
797 help_cmd_dump_one(mon, cmd, args, arg_index);
798 }
799 return;
800 }
801
802 /* Find one entry to dump */
803 for (cmd = cmds; cmd->name != NULL; cmd++) {
804 if (compare_cmd(args[arg_index], cmd->name)) {
805 if (cmd->sub_table) {
806 /* continue with next arg */
807 help_cmd_dump(mon, cmd->sub_table,
808 args, nb_args, arg_index + 1);
809 } else {
810 help_cmd_dump_one(mon, cmd, args, arg_index);
811 }
812 break;
813 }
bellard9dc39cb2004-03-14 21:38:27 +0000814 }
815}
816
aliguori376253e2009-03-05 23:01:23 +0000817static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000818{
Wenchao Xia66855492013-08-27 20:38:23 +0800819 char *args[MAX_ARGS];
820 int nb_args = 0;
821
822 /* 1. parse user input */
823 if (name) {
824 /* special case for log, directly dump and return */
825 if (!strcmp(name, "log")) {
Peter Maydell38dad9e2013-02-11 16:41:25 +0000826 const QEMULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000827 monitor_printf(mon, "Log items (comma separated):\n");
828 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
Peter Maydell38dad9e2013-02-11 16:41:25 +0000829 for (item = qemu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000830 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000831 }
Wenchao Xia66855492013-08-27 20:38:23 +0800832 return;
833 }
834
835 if (parse_cmdline(name, &nb_args, args) < 0) {
836 return;
bellardf193c792004-03-21 17:06:25 +0000837 }
bellard9dc39cb2004-03-14 21:38:27 +0000838 }
Wenchao Xia66855492013-08-27 20:38:23 +0800839
840 /* 2. dump the contents according to parsed args */
841 help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0);
842
843 free_cmdline_args(args, nb_args);
bellard9dc39cb2004-03-14 21:38:27 +0000844}
845
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300846static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300847{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300848 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300849}
850
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100851static void hmp_trace_event(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530852{
853 const char *tp_name = qdict_get_str(qdict, "name");
854 bool new_state = qdict_get_bool(qdict, "option");
Lluís Vilanova77e2b172016-07-11 12:53:57 +0200855 bool has_vcpu = qdict_haskey(qdict, "vcpu");
856 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
Lluís Vilanova14101d02014-08-25 13:20:03 +0200857 Error *local_err = NULL;
Blue Swirlf871d682010-10-13 19:14:29 +0000858
Lluís Vilanova77e2b172016-07-11 12:53:57 +0200859 if (vcpu < 0) {
860 monitor_printf(mon, "argument vcpu must be positive");
861 return;
862 }
863
864 qmp_trace_event_set_state(tp_name, new_state, true, true, has_vcpu, vcpu, &local_err);
Lluís Vilanova14101d02014-08-25 13:20:03 +0200865 if (local_err) {
Markus Armbruster091e38b2015-02-10 15:15:43 +0100866 error_report_err(local_err);
Blue Swirlf871d682010-10-13 19:14:29 +0000867 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530868}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100869
Michael Rothc45a8162011-10-02 08:44:37 -0500870#ifdef CONFIG_TRACE_SIMPLE
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100871static void hmp_trace_file(Monitor *mon, const QDict *qdict)
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100872{
873 const char *op = qdict_get_try_str(qdict, "op");
874 const char *arg = qdict_get_try_str(qdict, "arg");
875
876 if (!op) {
877 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
878 } else if (!strcmp(op, "on")) {
879 st_set_trace_file_enabled(true);
880 } else if (!strcmp(op, "off")) {
881 st_set_trace_file_enabled(false);
882 } else if (!strcmp(op, "flush")) {
883 st_flush_trace_buffer();
884 } else if (!strcmp(op, "set")) {
885 if (arg) {
886 st_set_trace_file(arg);
887 }
888 } else {
889 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
890 help_cmd(mon, "trace-file");
891 }
892}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530893#endif
894
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100895static void hmp_info_help(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000896{
Luiz Capitulino13c74252009-10-07 13:41:55 -0300897 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000898}
899
Marc-André Lureau9e812b62016-09-12 13:19:05 +0400900static void query_commands_cb(QmpCommand *cmd, void *opaque)
bellard9bc9d1c2004-10-10 15:15:51 +0000901{
Marc-André Lureau9e812b62016-09-12 13:19:05 +0400902 CommandInfoList *info, **list = opaque;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200903
Marc-André Lureau9e812b62016-09-12 13:19:05 +0400904 if (!cmd->enabled) {
905 return;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200906 }
907
Marc-André Lureau9e812b62016-09-12 13:19:05 +0400908 info = g_malloc0(sizeof(*info));
909 info->value = g_malloc0(sizeof(*info->value));
910 info->value->name = g_strdup(cmd->name);
911 info->next = *list;
912 *list = info;
913}
914
915CommandInfoList *qmp_query_commands(Error **errp)
916{
917 CommandInfoList *list = NULL;
918
Markus Armbruster635db182017-03-03 13:32:27 +0100919 qmp_for_each_command(cur_mon->qmp.commands, query_commands_cb, &list);
Marc-André Lureau9e812b62016-09-12 13:19:05 +0400920
921 return list;
thsa36e69d2007-12-02 05:18:19 +0000922}
923
Daniel P. Berrange48608532012-05-21 17:59:51 +0100924EventInfoList *qmp_query_events(Error **errp)
925{
926 EventInfoList *info, *ev_list = NULL;
Wenchao Xia75175172014-06-18 08:43:54 +0200927 QAPIEvent e;
Daniel P. Berrange48608532012-05-21 17:59:51 +0100928
Eric Blake7fb1cf12015-11-18 01:52:57 -0700929 for (e = 0 ; e < QAPI_EVENT__MAX ; e++) {
Wenchao Xia75175172014-06-18 08:43:54 +0200930 const char *event_name = QAPIEvent_lookup[e];
Daniel P. Berrange48608532012-05-21 17:59:51 +0100931 assert(event_name != NULL);
932 info = g_malloc0(sizeof(*info));
933 info->value = g_malloc0(sizeof(*info->value));
934 info->value->name = g_strdup(event_name);
935
936 info->next = ev_list;
937 ev_list = info;
938 }
939
940 return ev_list;
941}
942
Markus Armbruster39a18152015-09-16 13:06:28 +0200943/*
944 * Minor hack: generated marshalling suppressed for this command
945 * ('gen': false in the schema) so we can parse the JSON string
946 * directly into QObject instead of first parsing it with
947 * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
948 * to QObject with generated output marshallers, every time. Instead,
Daniel P. Berrangeb3db2112016-09-30 15:45:27 +0100949 * we do it in test-qobject-input-visitor.c, just to make sure
Markus Armbruster39a18152015-09-16 13:06:28 +0200950 * qapi-introspect.py's output actually conforms to the schema.
951 */
952static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
953 Error **errp)
954{
Markus Armbrusterb0f36b22017-02-28 22:27:02 +0100955 *ret_data = qobject_from_json(qmp_schema_json, &error_abort);
Markus Armbruster39a18152015-09-16 13:06:28 +0200956}
957
Marc-André Lureau5032a162016-09-12 13:19:02 +0400958/*
Marc-André Lureau5032a162016-09-12 13:19:02 +0400959 * We used to define commands in qmp-commands.hx in addition to the
960 * QAPI schema. This permitted defining some of them only in certain
961 * configurations. query-commands has always reflected that (good,
962 * because it lets QMP clients figure out what's actually available),
963 * while query-qmp-schema never did (not so good). This function is a
964 * hack to keep the configuration-specific commands defined exactly as
965 * before, even though qmp-commands.hx is gone.
966 *
967 * FIXME Educate the QAPI schema on configuration-specific commands,
968 * and drop this hack.
969 */
970static void qmp_unregister_commands_hack(void)
971{
972#ifndef CONFIG_SPICE
Markus Armbruster1527bad2017-03-03 13:32:25 +0100973 qmp_unregister_command(&qmp_commands, "query-spice");
Marc-André Lureau5032a162016-09-12 13:19:02 +0400974#endif
Markus Armbruster38bb54f2017-04-27 15:00:53 +0200975#ifndef CONFIG_REPLICATION
976 qmp_unregister_command(&qmp_commands, "xen-set-replication");
977 qmp_unregister_command(&qmp_commands, "query-xen-replication-status");
978 qmp_unregister_command(&qmp_commands, "xen-colo-do-checkpoint");
979#endif
Marc-André Lureau5032a162016-09-12 13:19:02 +0400980#ifndef TARGET_I386
Markus Armbruster1527bad2017-03-03 13:32:25 +0100981 qmp_unregister_command(&qmp_commands, "rtc-reset-reinjection");
Marc-André Lureau5032a162016-09-12 13:19:02 +0400982#endif
983#ifndef TARGET_S390X
Markus Armbruster1527bad2017-03-03 13:32:25 +0100984 qmp_unregister_command(&qmp_commands, "dump-skeys");
Marc-André Lureau5032a162016-09-12 13:19:02 +0400985#endif
986#ifndef TARGET_ARM
Markus Armbruster1527bad2017-03-03 13:32:25 +0100987 qmp_unregister_command(&qmp_commands, "query-gic-capabilities");
Marc-André Lureau5032a162016-09-12 13:19:02 +0400988#endif
Eduardo Habkostf99fd7c2017-02-22 16:00:28 -0300989#if !defined(TARGET_S390X) && !defined(TARGET_I386)
Markus Armbruster1527bad2017-03-03 13:32:25 +0100990 qmp_unregister_command(&qmp_commands, "query-cpu-model-expansion");
Eduardo Habkostf99fd7c2017-02-22 16:00:28 -0300991#endif
992#if !defined(TARGET_S390X)
Markus Armbruster1527bad2017-03-03 13:32:25 +0100993 qmp_unregister_command(&qmp_commands, "query-cpu-model-baseline");
994 qmp_unregister_command(&qmp_commands, "query-cpu-model-comparison");
Eduardo Habkost728b1422016-10-05 16:49:01 -0300995#endif
996#if !defined(TARGET_PPC) && !defined(TARGET_ARM) && !defined(TARGET_I386) \
997 && !defined(TARGET_S390X)
Markus Armbruster1527bad2017-03-03 13:32:25 +0100998 qmp_unregister_command(&qmp_commands, "query-cpu-definitions");
Eduardo Habkost728b1422016-10-05 16:49:01 -0300999#endif
Marc-André Lureau5032a162016-09-12 13:19:02 +04001000}
1001
Markus Armbruster05875682017-03-03 13:32:24 +01001002void monitor_init_qmp_commands(void)
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001003{
Markus Armbruster635db182017-03-03 13:32:27 +01001004 /*
1005 * Two command lists:
1006 * - qmp_commands contains all QMP commands
1007 * - qmp_cap_negotiation_commands contains just
1008 * "qmp_capabilities", to enforce capability negotiation
1009 */
1010
Markus Armbruster1527bad2017-03-03 13:32:25 +01001011 qmp_init_marshal(&qmp_commands);
Markus Armbruster05875682017-03-03 13:32:24 +01001012
Markus Armbruster1527bad2017-03-03 13:32:25 +01001013 qmp_register_command(&qmp_commands, "query-qmp-schema",
1014 qmp_query_qmp_schema,
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001015 QCO_NO_OPTIONS);
Markus Armbruster1527bad2017-03-03 13:32:25 +01001016 qmp_register_command(&qmp_commands, "device_add", qmp_device_add,
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001017 QCO_NO_OPTIONS);
Markus Armbruster1527bad2017-03-03 13:32:25 +01001018 qmp_register_command(&qmp_commands, "netdev_add", qmp_netdev_add,
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001019 QCO_NO_OPTIONS);
Marc-André Lureau5032a162016-09-12 13:19:02 +04001020
Markus Armbruster05875682017-03-03 13:32:24 +01001021 qmp_unregister_commands_hack();
Markus Armbruster635db182017-03-03 13:32:27 +01001022
1023 QTAILQ_INIT(&qmp_cap_negotiation_commands);
1024 qmp_register_command(&qmp_cap_negotiation_commands, "qmp_capabilities",
1025 qmp_marshal_qmp_capabilities, QCO_NO_OPTIONS);
1026}
1027
1028void qmp_qmp_capabilities(Error **errp)
1029{
1030 if (cur_mon->qmp.commands == &qmp_commands) {
1031 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
1032 "Capabilities negotiation is already complete, command "
1033 "ignored");
1034 return;
1035 }
1036
1037 cur_mon->qmp.commands = &qmp_commands;
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001038}
1039
Luiz Capitulinob025c8b2011-10-06 14:02:57 -03001040/* set the current CPU defined by the user */
1041int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +00001042{
Andreas Färber55e5c282012-12-17 06:18:02 +01001043 CPUState *cpu;
bellard6a00d602005-11-21 23:25:50 +00001044
Andreas Färber1c8bb3c2013-02-15 17:01:09 +01001045 cpu = qemu_get_cpu(cpu_index);
1046 if (cpu == NULL) {
1047 return -1;
bellard6a00d602005-11-21 23:25:50 +00001048 }
Andreas Färbercb446ec2013-05-01 14:24:52 +02001049 cur_mon->mon_cpu = cpu;
Andreas Färber1c8bb3c2013-02-15 17:01:09 +01001050 return 0;
bellard6a00d602005-11-21 23:25:50 +00001051}
1052
Pavel Butsykincaf15312015-09-22 16:18:17 +03001053CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +00001054{
aliguori731b0362009-03-05 23:01:42 +00001055 if (!cur_mon->mon_cpu) {
Thomas Huth854e67f2017-01-13 13:12:35 +01001056 if (!first_cpu) {
1057 return NULL;
1058 }
David Gibson27a83f82016-09-21 15:29:26 +10001059 monitor_set_cpu(first_cpu->cpu_index);
bellard6a00d602005-11-21 23:25:50 +00001060 }
Avi Kivity4c0960c2009-08-17 23:19:53 +03001061 cpu_synchronize_state(cur_mon->mon_cpu);
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001062 return cur_mon->mon_cpu;
1063}
1064
Pavel Butsykinbf957282015-09-10 18:38:59 +03001065CPUArchState *mon_get_cpu_env(void)
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001066{
Thomas Huth854e67f2017-01-13 13:12:35 +01001067 CPUState *cs = mon_get_cpu();
1068
1069 return cs ? cs->env_ptr : NULL;
bellard6a00d602005-11-21 23:25:50 +00001070}
1071
Luiz Capitulino99b77962011-10-24 10:53:44 -02001072int monitor_get_cpu_index(void)
1073{
Thomas Huth854e67f2017-01-13 13:12:35 +01001074 CPUState *cs = mon_get_cpu();
1075
1076 return cs ? cs->cpu_index : UNASSIGNED_CPU_INDEX;
Luiz Capitulino99b77962011-10-24 10:53:44 -02001077}
1078
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001079static void hmp_info_registers(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001080{
Suraj Jitindar Singh18f08282017-06-08 15:41:16 +10001081 bool all_cpus = qdict_get_try_bool(qdict, "cpustate_all", false);
1082 CPUState *cs;
Thomas Huth854e67f2017-01-13 13:12:35 +01001083
Suraj Jitindar Singh18f08282017-06-08 15:41:16 +10001084 if (all_cpus) {
1085 CPU_FOREACH(cs) {
1086 monitor_printf(mon, "\nCPU#%d\n", cs->cpu_index);
1087 cpu_dump_state(cs, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
1088 }
1089 } else {
1090 cs = mon_get_cpu();
1091
1092 if (!cs) {
1093 monitor_printf(mon, "No CPU available\n");
1094 return;
1095 }
1096
1097 cpu_dump_state(cs, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
Thomas Huth854e67f2017-01-13 13:12:35 +01001098 }
bellard9307c4c2004-04-04 12:57:25 +00001099}
1100
Paolo Bonzinif0d14a92012-09-17 13:42:41 +02001101#ifdef CONFIG_TCG
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001102static void hmp_info_jit(Monitor *mon, const QDict *qdict)
bellarde3db7222005-01-26 22:00:47 +00001103{
Thomas Huthb7da97e2017-04-26 06:11:47 +02001104 if (!tcg_enabled()) {
1105 error_report("JIT information is only available with accel=tcg");
1106 return;
1107 }
1108
aliguori376253e2009-03-05 23:01:23 +00001109 dump_exec_info((FILE *)mon, monitor_fprintf);
Sebastian Tanase27498be2014-07-25 11:56:33 +02001110 dump_drift_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +00001111}
1112
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001113static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
Max Filippov246ae242014-11-02 11:04:18 +03001114{
1115 dump_opcount_info((FILE *)mon, monitor_fprintf);
1116}
Paolo Bonzinif0d14a92012-09-17 13:42:41 +02001117#endif
Max Filippov246ae242014-11-02 11:04:18 +03001118
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001119static void hmp_info_history(Monitor *mon, const QDict *qdict)
bellardaa455482004-04-04 13:07:25 +00001120{
1121 int i;
bellard7e2515e2004-08-01 21:52:19 +00001122 const char *str;
ths3b46e622007-09-17 08:09:54 +00001123
aliguoricde76ee2009-03-05 23:01:51 +00001124 if (!mon->rs)
1125 return;
bellard7e2515e2004-08-01 21:52:19 +00001126 i = 0;
1127 for(;;) {
aliguori731b0362009-03-05 23:01:42 +00001128 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +00001129 if (!str)
1130 break;
aliguori376253e2009-03-05 23:01:23 +00001131 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +00001132 i++;
bellardaa455482004-04-04 13:07:25 +00001133 }
1134}
1135
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001136static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
j_mayer76a66252007-03-07 08:32:30 +00001137{
Thomas Huth854e67f2017-01-13 13:12:35 +01001138 CPUState *cs = mon_get_cpu();
1139
1140 if (!cs) {
1141 monitor_printf(mon, "No CPU available\n");
1142 return;
1143 }
1144 cpu_dump_statistics(cs, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +00001145}
j_mayer76a66252007-03-07 08:32:30 +00001146
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001147static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301148{
Lluís Vilanovabd712112016-07-11 12:53:51 +02001149 const char *name = qdict_get_try_str(qdict, "name");
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001150 bool has_vcpu = qdict_haskey(qdict, "vcpu");
1151 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
Lluís Vilanovabd712112016-07-11 12:53:51 +02001152 TraceEventInfoList *events;
Lluís Vilanova14101d02014-08-25 13:20:03 +02001153 TraceEventInfoList *elem;
Lluís Vilanovabd712112016-07-11 12:53:51 +02001154 Error *local_err = NULL;
1155
1156 if (name == NULL) {
1157 name = "*";
1158 }
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001159 if (vcpu < 0) {
1160 monitor_printf(mon, "argument vcpu must be positive");
1161 return;
1162 }
Lluís Vilanovabd712112016-07-11 12:53:51 +02001163
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001164 events = qmp_trace_event_get_state(name, has_vcpu, vcpu, &local_err);
Lluís Vilanovabd712112016-07-11 12:53:51 +02001165 if (local_err) {
1166 error_report_err(local_err);
1167 return;
1168 }
Lluís Vilanova14101d02014-08-25 13:20:03 +02001169
1170 for (elem = events; elem != NULL; elem = elem->next) {
1171 monitor_printf(mon, "%s : state %u\n",
1172 elem->value->name,
1173 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
1174 }
1175 qapi_free_TraceEventInfoList(events);
Prerna Saxena22890ab2010-06-24 17:04:53 +05301176}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301177
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001178void qmp_client_migrate_info(const char *protocol, const char *hostname,
1179 bool has_port, int64_t port,
1180 bool has_tls_port, int64_t tls_port,
1181 bool has_cert_subject, const char *cert_subject,
1182 Error **errp)
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001183{
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001184 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001185 if (!qemu_using_spice(errp)) {
1186 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001187 }
1188
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001189 if (!has_port && !has_tls_port) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001190 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001191 return;
Yonit Halperin6ec5dae2012-03-18 09:42:39 +02001192 }
1193
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001194 if (qemu_spice_migrate_info(hostname,
1195 has_port ? port : -1,
1196 has_tls_port ? tls_port : -1,
1197 cert_subject)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001198 error_setg(errp, QERR_UNDEFINED_ERROR);
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001199 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001200 }
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001201 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001202 }
1203
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001204 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001205}
1206
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001207static void hmp_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001208{
Markus Armbrusterdaa76aa2016-06-15 19:27:16 +02001209 Error *err = NULL;
1210
1211 qemu_set_log_filename(qdict_get_str(qdict, "filename"), &err);
1212 if (err) {
1213 error_report_err(err);
1214 }
pbrooke735b912007-06-30 13:53:24 +00001215}
1216
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001217static void hmp_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001218{
1219 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001220 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001221
bellard9307c4c2004-04-04 12:57:25 +00001222 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001223 mask = 0;
1224 } else {
Peter Maydell4fde1eb2013-02-11 16:41:22 +00001225 mask = qemu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001226 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001227 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001228 return;
1229 }
1230 }
Peter Maydell24537a02013-02-11 16:41:23 +00001231 qemu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00001232}
1233
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001234static void hmp_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001235{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001236 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001237 if (!option || !strcmp(option, "on")) {
1238 singlestep = 1;
1239 } else if (!strcmp(option, "off")) {
1240 singlestep = 0;
1241 } else {
1242 monitor_printf(mon, "unexpected option %s\n", option);
1243 }
1244}
1245
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001246static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001247{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001248 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001249 if (!device)
1250 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1251 if (gdbserver_start(device) < 0) {
1252 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1253 device);
1254 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001255 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001256 } else {
aliguori59030a82009-04-05 18:43:41 +00001257 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1258 device);
bellard8a7ddc32004-03-31 19:00:16 +00001259 }
1260}
1261
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001262static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001263{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001264 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001265 if (select_watchdog_action(action) == -1) {
1266 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1267 }
1268}
1269
aliguori376253e2009-03-05 23:01:23 +00001270static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001271{
aliguori376253e2009-03-05 23:01:23 +00001272 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001273 switch(c) {
1274 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001275 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001276 break;
1277 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001278 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001279 break;
1280 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001281 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001282 break;
1283 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001284 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001285 break;
1286 default:
1287 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001288 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001289 } else {
aliguori376253e2009-03-05 23:01:23 +00001290 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001291 }
1292 break;
1293 }
aliguori376253e2009-03-05 23:01:23 +00001294 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001295}
1296
aliguori376253e2009-03-05 23:01:23 +00001297static void memory_dump(Monitor *mon, int count, int format, int wsize,
Avi Kivitya8170e52012-10-23 12:30:10 +02001298 hwaddr addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001299{
Blue Swirl23842aa2010-01-12 20:27:43 +00001300 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001301 uint8_t buf[16];
1302 uint64_t v;
Thomas Huth854e67f2017-01-13 13:12:35 +01001303 CPUState *cs = mon_get_cpu();
1304
1305 if (!cs && (format == 'i' || !is_physical)) {
1306 monitor_printf(mon, "Can not dump without CPU\n");
1307 return;
1308 }
bellard9307c4c2004-04-04 12:57:25 +00001309
1310 if (format == 'i') {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001311 int flags = 0;
bellard9307c4c2004-04-04 12:57:25 +00001312#ifdef TARGET_I386
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001313 CPUArchState *env = mon_get_cpu_env();
bellard4c27ba22004-04-25 18:05:08 +00001314 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001315 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001316 } else if (wsize == 4) {
1317 flags = 0;
1318 } else {
bellard6a15fd12006-04-12 21:07:07 +00001319 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001320 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001321 if (env) {
1322#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001323 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001324 (env->segs[R_CS].flags & DESC_L_MASK))
1325 flags = 2;
1326 else
1327#endif
1328 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1329 flags = 1;
1330 }
bellard4c27ba22004-04-25 18:05:08 +00001331 }
1332#endif
Tom Musta1c38f842014-04-09 14:53:24 -05001333#ifdef TARGET_PPC
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001334 CPUArchState *env = mon_get_cpu_env();
Tom Musta1c38f842014-04-09 14:53:24 -05001335 flags = msr_le << 16;
1336 flags |= env->bfd_mach;
1337#endif
Thomas Huth854e67f2017-01-13 13:12:35 +01001338 monitor_disas(mon, cs, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001339 return;
1340 }
1341
1342 len = wsize * count;
1343 if (wsize == 1)
1344 line_size = 8;
1345 else
1346 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001347 max_digits = 0;
1348
1349 switch(format) {
1350 case 'o':
1351 max_digits = (wsize * 8 + 2) / 3;
1352 break;
1353 default:
1354 case 'x':
1355 max_digits = (wsize * 8) / 4;
1356 break;
1357 case 'u':
1358 case 'd':
1359 max_digits = (wsize * 8 * 10 + 32) / 33;
1360 break;
1361 case 'c':
1362 wsize = 1;
1363 break;
1364 }
1365
1366 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001367 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001368 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001369 else
aliguori376253e2009-03-05 23:01:23 +00001370 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001371 l = len;
1372 if (l > line_size)
1373 l = line_size;
1374 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001375 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001376 } else {
Thomas Huth854e67f2017-01-13 13:12:35 +01001377 if (cpu_memory_rw_debug(cs, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001378 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001379 break;
1380 }
bellard9307c4c2004-04-04 12:57:25 +00001381 }
ths5fafdf22007-09-16 21:08:06 +00001382 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001383 while (i < l) {
1384 switch(wsize) {
1385 default:
1386 case 1:
Peter Maydell24e60302015-01-20 15:19:32 +00001387 v = ldub_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001388 break;
1389 case 2:
Peter Maydell24e60302015-01-20 15:19:32 +00001390 v = lduw_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001391 break;
1392 case 4:
Peter Maydell24e60302015-01-20 15:19:32 +00001393 v = (uint32_t)ldl_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001394 break;
1395 case 8:
Peter Maydell24e60302015-01-20 15:19:32 +00001396 v = ldq_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001397 break;
1398 }
aliguori376253e2009-03-05 23:01:23 +00001399 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001400 switch(format) {
1401 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001402 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001403 break;
1404 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001405 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001406 break;
1407 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001408 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001409 break;
1410 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001411 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001412 break;
1413 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001414 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001415 break;
1416 }
1417 i += wsize;
1418 }
aliguori376253e2009-03-05 23:01:23 +00001419 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001420 addr += l;
1421 len -= l;
1422 }
1423}
1424
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001425static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001426{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001427 int count = qdict_get_int(qdict, "count");
1428 int format = qdict_get_int(qdict, "format");
1429 int size = qdict_get_int(qdict, "size");
1430 target_long addr = qdict_get_int(qdict, "addr");
1431
aliguori376253e2009-03-05 23:01:23 +00001432 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001433}
1434
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001435static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001436{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001437 int count = qdict_get_int(qdict, "count");
1438 int format = qdict_get_int(qdict, "format");
1439 int size = qdict_get_int(qdict, "size");
Avi Kivitya8170e52012-10-23 12:30:10 +02001440 hwaddr addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001441
aliguori376253e2009-03-05 23:01:23 +00001442 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001443}
1444
Paolo Bonzinie9628442017-04-20 15:30:58 +02001445static void *gpa2hva(MemoryRegion **p_mr, hwaddr addr, Error **errp)
1446{
1447 MemoryRegionSection mrs = memory_region_find(get_system_memory(),
1448 addr, 1);
1449
1450 if (!mrs.mr) {
1451 error_setg(errp, "No memory is mapped at address 0x%" HWADDR_PRIx, addr);
1452 return NULL;
1453 }
1454
1455 if (!memory_region_is_ram(mrs.mr) && !memory_region_is_romd(mrs.mr)) {
1456 error_setg(errp, "Memory at address 0x%" HWADDR_PRIx "is not RAM", addr);
1457 memory_region_unref(mrs.mr);
1458 return NULL;
1459 }
1460
1461 *p_mr = mrs.mr;
1462 return qemu_map_ram_ptr(mrs.mr->ram_block, mrs.offset_within_region);
1463}
1464
1465static void hmp_gpa2hva(Monitor *mon, const QDict *qdict)
1466{
1467 hwaddr addr = qdict_get_int(qdict, "addr");
1468 Error *local_err = NULL;
1469 MemoryRegion *mr = NULL;
1470 void *ptr;
1471
1472 ptr = gpa2hva(&mr, addr, &local_err);
1473 if (local_err) {
1474 error_report_err(local_err);
1475 return;
1476 }
1477
1478 monitor_printf(mon, "Host virtual address for 0x%" HWADDR_PRIx
1479 " (%s) is %p\n",
1480 addr, mr->name, ptr);
1481
1482 memory_region_unref(mr);
1483}
1484
1485#ifdef CONFIG_LINUX
1486static uint64_t vtop(void *ptr, Error **errp)
1487{
1488 uint64_t pinfo;
1489 uint64_t ret = -1;
1490 uintptr_t addr = (uintptr_t) ptr;
1491 uintptr_t pagesize = getpagesize();
1492 off_t offset = addr / pagesize * sizeof(pinfo);
1493 int fd;
1494
1495 fd = open("/proc/self/pagemap", O_RDONLY);
1496 if (fd == -1) {
1497 error_setg_errno(errp, errno, "Cannot open /proc/self/pagemap");
1498 return -1;
1499 }
1500
1501 /* Force copy-on-write if necessary. */
1502 atomic_add((uint8_t *)ptr, 0);
1503
1504 if (pread(fd, &pinfo, sizeof(pinfo), offset) != sizeof(pinfo)) {
1505 error_setg_errno(errp, errno, "Cannot read pagemap");
1506 goto out;
1507 }
1508 if ((pinfo & (1ull << 63)) == 0) {
1509 error_setg(errp, "Page not present");
1510 goto out;
1511 }
1512 ret = ((pinfo & 0x007fffffffffffffull) * pagesize) | (addr & (pagesize - 1));
1513
1514out:
1515 close(fd);
1516 return ret;
1517}
1518
1519static void hmp_gpa2hpa(Monitor *mon, const QDict *qdict)
1520{
1521 hwaddr addr = qdict_get_int(qdict, "addr");
1522 Error *local_err = NULL;
1523 MemoryRegion *mr = NULL;
1524 void *ptr;
1525 uint64_t physaddr;
1526
1527 ptr = gpa2hva(&mr, addr, &local_err);
1528 if (local_err) {
1529 error_report_err(local_err);
1530 return;
1531 }
1532
1533 physaddr = vtop(ptr, &local_err);
1534 if (local_err) {
1535 error_report_err(local_err);
1536 } else {
1537 monitor_printf(mon, "Host physical address for 0x%" HWADDR_PRIx
1538 " (%s) is 0x%" PRIx64 "\n",
1539 addr, mr->name, (uint64_t) physaddr);
1540 }
1541
1542 memory_region_unref(mr);
1543}
1544#endif
1545
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001546static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001547{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001548 int format = qdict_get_int(qdict, "format");
Avi Kivitya8170e52012-10-23 12:30:10 +02001549 hwaddr val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001550
bellard9307c4c2004-04-04 12:57:25 +00001551 switch(format) {
1552 case 'o':
Avi Kivitya8170e52012-10-23 12:30:10 +02001553 monitor_printf(mon, "%#" HWADDR_PRIo, val);
bellard9307c4c2004-04-04 12:57:25 +00001554 break;
1555 case 'x':
Avi Kivitya8170e52012-10-23 12:30:10 +02001556 monitor_printf(mon, "%#" HWADDR_PRIx, val);
bellard9307c4c2004-04-04 12:57:25 +00001557 break;
1558 case 'u':
Avi Kivitya8170e52012-10-23 12:30:10 +02001559 monitor_printf(mon, "%" HWADDR_PRIu, val);
bellard9307c4c2004-04-04 12:57:25 +00001560 break;
1561 default:
1562 case 'd':
Avi Kivitya8170e52012-10-23 12:30:10 +02001563 monitor_printf(mon, "%" HWADDR_PRId, val);
bellard9307c4c2004-04-04 12:57:25 +00001564 break;
1565 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001566 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001567 break;
1568 }
aliguori376253e2009-03-05 23:01:23 +00001569 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001570}
1571
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001572static void hmp_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001573{
1574 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001575 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001576 uint32_t start = qdict_get_int(qdict, "start");
1577 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001578
1579 sum = 0;
1580 for(addr = start; addr < (start + size); addr++) {
Peter Maydell42874d32015-04-26 16:49:24 +01001581 uint8_t val = address_space_ldub(&address_space_memory, addr,
1582 MEMTXATTRS_UNSPECIFIED, NULL);
bellarde4cf1ad2005-06-04 20:15:57 +00001583 /* BSD sum algorithm ('sum' Unix command) */
1584 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001585 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001586 }
aliguori376253e2009-03-05 23:01:23 +00001587 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001588}
1589
bellard13224a82006-07-14 22:03:35 +00001590static int mouse_button_state;
1591
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001592static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001593{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001594 int dx, dy, dz, button;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001595 const char *dx_str = qdict_get_str(qdict, "dx_str");
1596 const char *dy_str = qdict_get_str(qdict, "dy_str");
1597 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001598
bellard13224a82006-07-14 22:03:35 +00001599 dx = strtol(dx_str, NULL, 0);
1600 dy = strtol(dy_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001601 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
1602 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
1603
1604 if (dz_str) {
bellard13224a82006-07-14 22:03:35 +00001605 dz = strtol(dz_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001606 if (dz != 0) {
Gerd Hoffmannf22d0af2016-01-12 12:14:12 +01001607 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001608 qemu_input_queue_btn(NULL, button, true);
1609 qemu_input_event_sync();
1610 qemu_input_queue_btn(NULL, button, false);
1611 }
1612 }
1613 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001614}
1615
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001616static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001617{
Eric Blake7fb1cf12015-11-18 01:52:57 -07001618 static uint32_t bmap[INPUT_BUTTON__MAX] = {
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001619 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1620 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
1621 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1622 };
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001623 int button_state = qdict_get_int(qdict, "button_state");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001624
1625 if (mouse_button_state == button_state) {
1626 return;
1627 }
1628 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1629 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001630 mouse_button_state = button_state;
bellard13224a82006-07-14 22:03:35 +00001631}
1632
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001633static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001634{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001635 int size = qdict_get_int(qdict, "size");
1636 int addr = qdict_get_int(qdict, "addr");
1637 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001638 uint32_t val;
1639 int suffix;
1640
1641 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001642 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001643 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001644 addr++;
1645 }
1646 addr &= 0xffff;
1647
1648 switch(size) {
1649 default:
1650 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001651 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001652 suffix = 'b';
1653 break;
1654 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001655 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001656 suffix = 'w';
1657 break;
1658 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001659 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001660 suffix = 'l';
1661 break;
1662 }
aliguori376253e2009-03-05 23:01:23 +00001663 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1664 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001665}
bellarda3a91a32004-06-04 11:06:21 +00001666
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001667static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001668{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001669 int size = qdict_get_int(qdict, "size");
1670 int addr = qdict_get_int(qdict, "addr");
1671 int val = qdict_get_int(qdict, "val");
1672
Jan Kiszkaf1147842009-07-14 10:20:11 +02001673 addr &= IOPORTS_MASK;
1674
1675 switch (size) {
1676 default:
1677 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001678 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001679 break;
1680 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001681 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001682 break;
1683 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001684 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001685 break;
1686 }
1687}
1688
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001689static void hmp_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001690{
Gongleif1839932014-12-03 18:20:58 +00001691 Error *local_err = NULL;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001692 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001693
Gongleif1839932014-12-03 18:20:58 +00001694 qemu_boot_set(bootdevice, &local_err);
1695 if (local_err) {
Markus Armbruster193227f2015-12-18 16:35:06 +01001696 error_report_err(local_err);
aurel320ecdffb2008-05-04 20:11:34 +00001697 } else {
Gongleif1839932014-12-03 18:20:58 +00001698 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001699 }
1700}
1701
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001702static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
Blue Swirl314e2982011-09-11 20:22:05 +00001703{
Peter Xu57bb40c2017-01-16 16:40:05 +08001704 bool flatview = qdict_get_try_bool(qdict, "flatview", false);
1705
1706 mtree_info((fprintf_function)monitor_printf, mon, flatview);
Blue Swirl314e2982011-09-11 20:22:05 +00001707}
1708
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001709static void hmp_info_numa(Monitor *mon, const QDict *qdict)
aliguori030ea372009-04-21 22:30:47 +00001710{
aliguorib28b6232009-04-22 20:20:29 +00001711 int i;
zhanghailiang5b009e42014-11-04 19:49:30 +08001712 uint64_t *node_mem;
Igor Mammedovf75cd442017-05-30 18:23:59 +02001713 CpuInfoList *cpu_list, *cpu;
aliguori030ea372009-04-21 22:30:47 +00001714
Igor Mammedovf75cd442017-05-30 18:23:59 +02001715 cpu_list = qmp_query_cpus(&error_abort);
zhanghailiang5b009e42014-11-04 19:49:30 +08001716 node_mem = g_new0(uint64_t, nb_numa_nodes);
1717 query_numa_node_mem(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001718 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1719 for (i = 0; i < nb_numa_nodes; i++) {
1720 monitor_printf(mon, "node %d cpus:", i);
Igor Mammedovf75cd442017-05-30 18:23:59 +02001721 for (cpu = cpu_list; cpu; cpu = cpu->next) {
1722 if (cpu->value->has_props && cpu->value->props->has_node_id &&
1723 cpu->value->props->node_id == i) {
1724 monitor_printf(mon, " %" PRIi64, cpu->value->CPU);
aliguori030ea372009-04-21 22:30:47 +00001725 }
1726 }
1727 monitor_printf(mon, "\n");
1728 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
zhanghailiang5b009e42014-11-04 19:49:30 +08001729 node_mem[i] >> 20);
aliguori030ea372009-04-21 22:30:47 +00001730 }
Igor Mammedovf75cd442017-05-30 18:23:59 +02001731 qapi_free_CpuInfoList(cpu_list);
zhanghailiang5b009e42014-11-04 19:49:30 +08001732 g_free(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001733}
1734
bellard5f1ce942006-02-08 22:40:15 +00001735#ifdef CONFIG_PROFILER
1736
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001737int64_t tcg_time;
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001738int64_t dev_time;
1739
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, "async time %" PRId64 " (%0.3f)\n",
Rutuja Shah73bcb242016-03-21 21:32:30 +05301743 dev_time, dev_time / (double)NANOSECONDS_PER_SECOND);
aliguori376253e2009-03-05 23:01:23 +00001744 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Rutuja Shah73bcb242016-03-21 21:32:30 +05301745 tcg_time, tcg_time / (double)NANOSECONDS_PER_SECOND);
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001746 tcg_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001747 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001748}
1749#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001750static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001751{
aliguori376253e2009-03-05 23:01:23 +00001752 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001753}
1754#endif
1755
bellardec36b692006-07-16 18:57:03 +00001756/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001757static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001758
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001759static void hmp_info_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001760{
1761 int i;
1762 CaptureState *s;
1763
1764 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001765 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001766 s->ops.info (s->opaque);
1767 }
1768}
1769
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001770static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001771{
1772 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001773 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001774 CaptureState *s;
1775
1776 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1777 if (i == n) {
1778 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001779 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001780 g_free (s);
bellardec36b692006-07-16 18:57:03 +00001781 return;
1782 }
1783 }
1784}
1785
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001786static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001787{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001788 const char *path = qdict_get_str(qdict, "path");
1789 int has_freq = qdict_haskey(qdict, "freq");
1790 int freq = qdict_get_try_int(qdict, "freq", -1);
1791 int has_bits = qdict_haskey(qdict, "bits");
1792 int bits = qdict_get_try_int(qdict, "bits", -1);
1793 int has_channels = qdict_haskey(qdict, "nchannels");
1794 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001795 CaptureState *s;
1796
Anthony Liguori7267c092011-08-20 22:09:37 -05001797 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001798
1799 freq = has_freq ? freq : 44100;
1800 bits = has_bits ? bits : 16;
1801 nchannels = has_channels ? nchannels : 2;
1802
1803 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001804 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05001805 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001806 return;
bellardec36b692006-07-16 18:57:03 +00001807 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001808 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001809}
bellardec36b692006-07-16 18:57:03 +00001810
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001811static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001812{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001813 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001814
aliguori76655d62009-03-06 20:27:37 +00001815 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001816 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001817 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001818 return acl;
1819}
aliguori76655d62009-03-06 20:27:37 +00001820
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001821static void hmp_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001822{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001823 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001824 qemu_acl *acl = find_acl(mon, aclname);
1825 qemu_acl_entry *entry;
1826 int i = 0;
1827
1828 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001829 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001830 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00001831 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00001832 i++;
1833 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001834 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001835 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001836 }
1837}
1838
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001839static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001840{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001841 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001842 qemu_acl *acl = find_acl(mon, aclname);
1843
1844 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001845 qemu_acl_reset(acl);
1846 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001847 }
1848}
aliguori76655d62009-03-06 20:27:37 +00001849
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001850static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001851{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001852 const char *aclname = qdict_get_str(qdict, "aclname");
1853 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001854 qemu_acl *acl = find_acl(mon, aclname);
1855
1856 if (acl) {
1857 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001858 acl->defaultDeny = 0;
1859 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001860 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001861 acl->defaultDeny = 1;
1862 monitor_printf(mon, "acl: policy set to 'deny'\n");
1863 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001864 monitor_printf(mon, "acl: unknown policy '%s', "
1865 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001866 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001867 }
1868}
aliguori76655d62009-03-06 20:27:37 +00001869
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001870static void hmp_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001871{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001872 const char *aclname = qdict_get_str(qdict, "aclname");
1873 const char *match = qdict_get_str(qdict, "match");
1874 const char *policy = qdict_get_str(qdict, "policy");
1875 int has_index = qdict_haskey(qdict, "index");
1876 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001877 qemu_acl *acl = find_acl(mon, aclname);
1878 int deny, ret;
1879
1880 if (acl) {
1881 if (strcmp(policy, "allow") == 0) {
1882 deny = 0;
1883 } else if (strcmp(policy, "deny") == 0) {
1884 deny = 1;
1885 } else {
1886 monitor_printf(mon, "acl: unknown policy '%s', "
1887 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001888 return;
1889 }
aliguori28a76be2009-03-06 20:27:40 +00001890 if (has_index)
1891 ret = qemu_acl_insert(acl, deny, match, index);
1892 else
1893 ret = qemu_acl_append(acl, deny, match);
1894 if (ret < 0)
1895 monitor_printf(mon, "acl: unable to add acl entry\n");
1896 else
1897 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001898 }
1899}
aliguori76655d62009-03-06 20:27:37 +00001900
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001901static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001902{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001903 const char *aclname = qdict_get_str(qdict, "aclname");
1904 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001905 qemu_acl *acl = find_acl(mon, aclname);
1906 int ret;
aliguori76655d62009-03-06 20:27:37 +00001907
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001908 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001909 ret = qemu_acl_remove(acl, match);
1910 if (ret < 0)
1911 monitor_printf(mon, "acl: no matching acl entry\n");
1912 else
1913 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001914 }
1915}
1916
Corey Bryant208c9d12012-06-22 14:36:09 -04001917void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001918{
Anthony Liguoric227f092009-10-01 16:12:16 -05001919 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001920 int fd;
1921
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03001922 fd = qemu_chr_fe_get_msgfd(&cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001923 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001924 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryant208c9d12012-06-22 14:36:09 -04001925 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001926 }
1927
1928 if (qemu_isdigit(fdname[0])) {
Stefan Hajnoczi0b9f0e22014-04-24 13:58:18 +02001929 close(fd);
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001930 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
1931 "a name not starting with a digit");
Corey Bryant208c9d12012-06-22 14:36:09 -04001932 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001933 }
1934
Corey Bryant208c9d12012-06-22 14:36:09 -04001935 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001936 if (strcmp(monfd->name, fdname) != 0) {
1937 continue;
1938 }
1939
1940 close(monfd->fd);
1941 monfd->fd = fd;
Corey Bryant208c9d12012-06-22 14:36:09 -04001942 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001943 }
1944
Anthony Liguori7267c092011-08-20 22:09:37 -05001945 monfd = g_malloc0(sizeof(mon_fd_t));
1946 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001947 monfd->fd = fd;
1948
Corey Bryant208c9d12012-06-22 14:36:09 -04001949 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001950}
1951
Corey Bryant208c9d12012-06-22 14:36:09 -04001952void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001953{
Anthony Liguoric227f092009-10-01 16:12:16 -05001954 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001955
Corey Bryant208c9d12012-06-22 14:36:09 -04001956 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001957 if (strcmp(monfd->name, fdname) != 0) {
1958 continue;
1959 }
1960
Blue Swirl72cf2d42009-09-12 07:36:22 +00001961 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001962 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001963 g_free(monfd->name);
1964 g_free(monfd);
Corey Bryant208c9d12012-06-22 14:36:09 -04001965 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001966 }
1967
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001968 error_setg(errp, QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001969}
1970
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001971int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
Mark McLoughlin7768e042009-07-22 09:11:41 +01001972{
Anthony Liguoric227f092009-10-01 16:12:16 -05001973 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01001974
Blue Swirl72cf2d42009-09-12 07:36:22 +00001975 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01001976 int fd;
1977
1978 if (strcmp(monfd->name, fdname) != 0) {
1979 continue;
1980 }
1981
1982 fd = monfd->fd;
1983
1984 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001985 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05001986 g_free(monfd->name);
1987 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001988
1989 return fd;
1990 }
1991
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001992 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001993 return -1;
1994}
1995
Corey Bryantba1c0482012-08-14 16:43:43 -04001996static void monitor_fdset_cleanup(MonFdset *mon_fdset)
1997{
1998 MonFdsetFd *mon_fdset_fd;
1999 MonFdsetFd *mon_fdset_fd_next;
2000
2001 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantebe52b52012-10-18 15:19:33 -04002002 if ((mon_fdset_fd->removed ||
2003 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
2004 runstate_is_running()) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002005 close(mon_fdset_fd->fd);
2006 g_free(mon_fdset_fd->opaque);
2007 QLIST_REMOVE(mon_fdset_fd, next);
2008 g_free(mon_fdset_fd);
2009 }
2010 }
2011
Corey Bryantadb696f2012-08-14 16:43:47 -04002012 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002013 QLIST_REMOVE(mon_fdset, next);
2014 g_free(mon_fdset);
2015 }
2016}
2017
Corey Bryantefb87c12012-08-14 16:43:48 -04002018static void monitor_fdsets_cleanup(void)
2019{
2020 MonFdset *mon_fdset;
2021 MonFdset *mon_fdset_next;
2022
2023 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
2024 monitor_fdset_cleanup(mon_fdset);
2025 }
2026}
2027
Corey Bryantba1c0482012-08-14 16:43:43 -04002028AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
2029 const char *opaque, Error **errp)
2030{
2031 int fd;
2032 Monitor *mon = cur_mon;
Corey Bryantba1c0482012-08-14 16:43:43 -04002033 AddfdInfo *fdinfo;
2034
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03002035 fd = qemu_chr_fe_get_msgfd(&mon->chr);
Corey Bryantba1c0482012-08-14 16:43:43 -04002036 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002037 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryantba1c0482012-08-14 16:43:43 -04002038 goto error;
2039 }
2040
Corey Bryante446f702012-10-18 15:19:32 -04002041 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
2042 has_opaque, opaque, errp);
2043 if (fdinfo) {
2044 return fdinfo;
Corey Bryant9ac54af2012-10-18 15:19:31 -04002045 }
2046
Corey Bryantba1c0482012-08-14 16:43:43 -04002047error:
2048 if (fd != -1) {
2049 close(fd);
2050 }
2051 return NULL;
2052}
2053
2054void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
2055{
2056 MonFdset *mon_fdset;
2057 MonFdsetFd *mon_fdset_fd;
2058 char fd_str[60];
2059
2060 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2061 if (mon_fdset->id != fdset_id) {
2062 continue;
2063 }
2064 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2065 if (has_fd) {
2066 if (mon_fdset_fd->fd != fd) {
2067 continue;
2068 }
2069 mon_fdset_fd->removed = true;
2070 break;
2071 } else {
2072 mon_fdset_fd->removed = true;
2073 }
2074 }
2075 if (has_fd && !mon_fdset_fd) {
2076 goto error;
2077 }
2078 monitor_fdset_cleanup(mon_fdset);
2079 return;
2080 }
2081
2082error:
2083 if (has_fd) {
2084 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
2085 fdset_id, fd);
2086 } else {
2087 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
2088 }
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002089 error_setg(errp, QERR_FD_NOT_FOUND, fd_str);
Corey Bryantba1c0482012-08-14 16:43:43 -04002090}
2091
2092FdsetInfoList *qmp_query_fdsets(Error **errp)
2093{
2094 MonFdset *mon_fdset;
2095 MonFdsetFd *mon_fdset_fd;
2096 FdsetInfoList *fdset_list = NULL;
2097
2098 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2099 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
2100 FdsetFdInfoList *fdsetfd_list = NULL;
2101
2102 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
2103 fdset_info->value->fdset_id = mon_fdset->id;
2104
2105 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2106 FdsetFdInfoList *fdsetfd_info;
2107
2108 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
2109 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
2110 fdsetfd_info->value->fd = mon_fdset_fd->fd;
2111 if (mon_fdset_fd->opaque) {
2112 fdsetfd_info->value->has_opaque = true;
2113 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
2114 } else {
2115 fdsetfd_info->value->has_opaque = false;
2116 }
2117
2118 fdsetfd_info->next = fdsetfd_list;
2119 fdsetfd_list = fdsetfd_info;
2120 }
2121
2122 fdset_info->value->fds = fdsetfd_list;
2123
2124 fdset_info->next = fdset_list;
2125 fdset_list = fdset_info;
2126 }
2127
2128 return fdset_list;
2129}
2130
Corey Bryante446f702012-10-18 15:19:32 -04002131AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
2132 bool has_opaque, const char *opaque,
2133 Error **errp)
2134{
2135 MonFdset *mon_fdset = NULL;
2136 MonFdsetFd *mon_fdset_fd;
2137 AddfdInfo *fdinfo;
2138
2139 if (has_fdset_id) {
2140 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2141 /* Break if match found or match impossible due to ordering by ID */
2142 if (fdset_id <= mon_fdset->id) {
2143 if (fdset_id < mon_fdset->id) {
2144 mon_fdset = NULL;
2145 }
2146 break;
2147 }
2148 }
2149 }
2150
2151 if (mon_fdset == NULL) {
2152 int64_t fdset_id_prev = -1;
2153 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
2154
2155 if (has_fdset_id) {
2156 if (fdset_id < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002157 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
2158 "a non-negative value");
Corey Bryante446f702012-10-18 15:19:32 -04002159 return NULL;
2160 }
2161 /* Use specified fdset ID */
2162 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2163 mon_fdset_cur = mon_fdset;
2164 if (fdset_id < mon_fdset_cur->id) {
2165 break;
2166 }
2167 }
2168 } else {
2169 /* Use first available fdset ID */
2170 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2171 mon_fdset_cur = mon_fdset;
2172 if (fdset_id_prev == mon_fdset_cur->id - 1) {
2173 fdset_id_prev = mon_fdset_cur->id;
2174 continue;
2175 }
2176 break;
2177 }
2178 }
2179
2180 mon_fdset = g_malloc0(sizeof(*mon_fdset));
2181 if (has_fdset_id) {
2182 mon_fdset->id = fdset_id;
2183 } else {
2184 mon_fdset->id = fdset_id_prev + 1;
2185 }
2186
2187 /* The fdset list is ordered by fdset ID */
2188 if (!mon_fdset_cur) {
2189 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
2190 } else if (mon_fdset->id < mon_fdset_cur->id) {
2191 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
2192 } else {
2193 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
2194 }
2195 }
2196
2197 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
2198 mon_fdset_fd->fd = fd;
2199 mon_fdset_fd->removed = false;
2200 if (has_opaque) {
2201 mon_fdset_fd->opaque = g_strdup(opaque);
2202 }
2203 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
2204
2205 fdinfo = g_malloc0(sizeof(*fdinfo));
2206 fdinfo->fdset_id = mon_fdset->id;
2207 fdinfo->fd = mon_fdset_fd->fd;
2208
2209 return fdinfo;
2210}
2211
Corey Bryantadb696f2012-08-14 16:43:47 -04002212int monitor_fdset_get_fd(int64_t fdset_id, int flags)
2213{
Blue Swirlb2dc64c2012-08-18 20:14:54 +00002214#ifndef _WIN32
Corey Bryantadb696f2012-08-14 16:43:47 -04002215 MonFdset *mon_fdset;
2216 MonFdsetFd *mon_fdset_fd;
2217 int mon_fd_flags;
2218
Corey Bryantadb696f2012-08-14 16:43:47 -04002219 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2220 if (mon_fdset->id != fdset_id) {
2221 continue;
2222 }
2223 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2224 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
2225 if (mon_fd_flags == -1) {
2226 return -1;
2227 }
2228
2229 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
2230 return mon_fdset_fd->fd;
2231 }
2232 }
2233 errno = EACCES;
2234 return -1;
2235 }
2236#endif
2237
2238 errno = ENOENT;
2239 return -1;
2240}
2241
2242int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
2243{
2244 MonFdset *mon_fdset;
2245 MonFdsetFd *mon_fdset_fd_dup;
2246
2247 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2248 if (mon_fdset->id != fdset_id) {
2249 continue;
2250 }
2251 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2252 if (mon_fdset_fd_dup->fd == dup_fd) {
2253 return -1;
2254 }
2255 }
2256 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2257 mon_fdset_fd_dup->fd = dup_fd;
2258 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
2259 return 0;
2260 }
2261 return -1;
2262}
2263
2264static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2265{
2266 MonFdset *mon_fdset;
2267 MonFdsetFd *mon_fdset_fd_dup;
2268
2269 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2270 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2271 if (mon_fdset_fd_dup->fd == dup_fd) {
2272 if (remove) {
2273 QLIST_REMOVE(mon_fdset_fd_dup, next);
2274 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2275 monitor_fdset_cleanup(mon_fdset);
2276 }
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002277 return -1;
2278 } else {
2279 return mon_fdset->id;
Corey Bryantadb696f2012-08-14 16:43:47 -04002280 }
Corey Bryantadb696f2012-08-14 16:43:47 -04002281 }
2282 }
2283 }
2284 return -1;
2285}
2286
2287int monitor_fdset_dup_fd_find(int dup_fd)
2288{
2289 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2290}
2291
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002292void monitor_fdset_dup_fd_remove(int dup_fd)
Corey Bryantadb696f2012-08-14 16:43:47 -04002293{
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002294 monitor_fdset_dup_fd_find_remove(dup_fd, true);
Corey Bryantadb696f2012-08-14 16:43:47 -04002295}
2296
Markus Armbruster1677f4c2015-02-09 14:03:19 +01002297int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
Laszlo Ersek59063662014-04-10 10:24:31 +02002298{
2299 int fd;
2300 Error *local_err = NULL;
2301
2302 if (!qemu_isdigit(fdname[0]) && mon) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002303 fd = monitor_get_fd(mon, fdname, &local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002304 } else {
2305 fd = qemu_parse_fd(fdname);
Laszlo Ersek59063662014-04-10 10:24:31 +02002306 if (fd == -1) {
2307 error_setg(&local_err, "Invalid file descriptor number '%s'",
2308 fdname);
2309 }
2310 }
2311 if (local_err) {
2312 error_propagate(errp, local_err);
2313 assert(fd == -1);
2314 } else {
2315 assert(fd != -1);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002316 }
2317
2318 return fd;
2319}
2320
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002321/* Please update hmp-commands.hx when adding or changing commands */
Wayne Xia816f8922011-10-12 11:32:41 +08002322static mon_cmd_t info_cmds[] = {
Pavel Butsykinda76ee72015-09-10 18:38:58 +03002323#include "hmp-commands-info.h"
2324 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002325};
2326
Wenchao Xiaa13ced52013-01-14 14:06:28 +08002327/* mon_cmds and info_cmds would be sorted at runtime */
2328static mon_cmd_t mon_cmds[] = {
2329#include "hmp-commands.h"
2330 { NULL, NULL, },
2331};
2332
bellard9307c4c2004-04-04 12:57:25 +00002333/*******************************************************************/
2334
2335static const char *pch;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002336static sigjmp_buf expr_env;
bellard9307c4c2004-04-04 12:57:25 +00002337
bellard9307c4c2004-04-04 12:57:25 +00002338
Stefan Weil9c3175c2013-08-22 21:30:09 +02002339static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
2340expr_error(Monitor *mon, const char *fmt, ...)
bellard9dc39cb2004-03-14 21:38:27 +00002341{
Fam Zheng277acfe2013-08-20 10:58:21 +08002342 va_list ap;
2343 va_start(ap, fmt);
2344 monitor_vprintf(mon, fmt, ap);
2345 monitor_printf(mon, "\n");
2346 va_end(ap);
Peter Maydell6ab7e542013-02-20 15:21:09 +00002347 siglongjmp(expr_env, 1);
bellard9307c4c2004-04-04 12:57:25 +00002348}
2349
Markus Armbruster09b94182010-01-20 13:07:30 +01002350/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00002351static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002352{
Pavel Butsykinbf957282015-09-10 18:38:59 +03002353 const MonitorDef *md = target_monitor_defs();
Thomas Huth854e67f2017-01-13 13:12:35 +01002354 CPUState *cs = mon_get_cpu();
bellard92a31b12005-02-10 22:00:52 +00002355 void *ptr;
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002356 uint64_t tmp = 0;
2357 int ret;
bellard92a31b12005-02-10 22:00:52 +00002358
Thomas Huth854e67f2017-01-13 13:12:35 +01002359 if (cs == NULL || md == NULL) {
Pavel Butsykinbf957282015-09-10 18:38:59 +03002360 return -1;
2361 }
2362
2363 for(; md->name != NULL; md++) {
bellard9307c4c2004-04-04 12:57:25 +00002364 if (compare_cmd(name, md->name)) {
2365 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002366 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002367 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002368 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00002369 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002370 switch(md->type) {
2371 case MD_I32:
2372 *pval = *(int32_t *)ptr;
2373 break;
2374 case MD_TLONG:
2375 *pval = *(target_long *)ptr;
2376 break;
2377 default:
2378 *pval = 0;
2379 break;
2380 }
bellard9307c4c2004-04-04 12:57:25 +00002381 }
2382 return 0;
2383 }
2384 }
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002385
Thomas Huth854e67f2017-01-13 13:12:35 +01002386 ret = target_get_monitor_def(cs, name, &tmp);
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002387 if (!ret) {
2388 *pval = (target_long) tmp;
2389 }
2390
2391 return ret;
bellard9307c4c2004-04-04 12:57:25 +00002392}
2393
2394static void next(void)
2395{
Blue Swirl660f11b2009-07-31 21:16:51 +00002396 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002397 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002398 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002399 pch++;
2400 }
2401}
2402
aliguori376253e2009-03-05 23:01:23 +00002403static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002404
aliguori376253e2009-03-05 23:01:23 +00002405static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002406{
blueswir1c2efc952007-09-25 17:28:42 +00002407 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002408 char *p;
bellard6a00d602005-11-21 23:25:50 +00002409 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002410
2411 switch(*pch) {
2412 case '+':
2413 next();
aliguori376253e2009-03-05 23:01:23 +00002414 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002415 break;
2416 case '-':
2417 next();
aliguori376253e2009-03-05 23:01:23 +00002418 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002419 break;
2420 case '~':
2421 next();
aliguori376253e2009-03-05 23:01:23 +00002422 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002423 break;
2424 case '(':
2425 next();
aliguori376253e2009-03-05 23:01:23 +00002426 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002427 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002428 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002429 }
2430 next();
2431 break;
bellard81d09122004-07-14 17:21:37 +00002432 case '\'':
2433 pch++;
2434 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002435 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002436 n = *pch;
2437 pch++;
2438 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002439 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002440 next();
2441 break;
bellard9307c4c2004-04-04 12:57:25 +00002442 case '$':
2443 {
2444 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002445 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002446
bellard9307c4c2004-04-04 12:57:25 +00002447 pch++;
2448 q = buf;
2449 while ((*pch >= 'a' && *pch <= 'z') ||
2450 (*pch >= 'A' && *pch <= 'Z') ||
2451 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002452 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002453 if ((q - buf) < sizeof(buf) - 1)
2454 *q++ = *pch;
2455 pch++;
2456 }
blueswir1cd390082008-11-16 13:53:32 +00002457 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002458 pch++;
2459 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002460 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01002461 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00002462 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00002463 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002464 }
2465 break;
2466 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002467 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002468 n = 0;
2469 break;
2470 default:
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002471 errno = 0;
bellard4f4fbf72006-06-25 18:28:12 +00002472 n = strtoull(pch, &p, 0);
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002473 if (errno == ERANGE) {
2474 expr_error(mon, "number too large");
2475 }
bellard9307c4c2004-04-04 12:57:25 +00002476 if (pch == p) {
Fam Zheng277acfe2013-08-20 10:58:21 +08002477 expr_error(mon, "invalid char '%c' in expression", *p);
bellard9307c4c2004-04-04 12:57:25 +00002478 }
2479 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002480 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002481 pch++;
2482 break;
2483 }
2484 return n;
2485}
2486
2487
aliguori376253e2009-03-05 23:01:23 +00002488static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002489{
blueswir1c2efc952007-09-25 17:28:42 +00002490 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002491 int op;
ths3b46e622007-09-17 08:09:54 +00002492
aliguori376253e2009-03-05 23:01:23 +00002493 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002494 for(;;) {
2495 op = *pch;
2496 if (op != '*' && op != '/' && op != '%')
2497 break;
2498 next();
aliguori376253e2009-03-05 23:01:23 +00002499 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002500 switch(op) {
2501 default:
2502 case '*':
2503 val *= val2;
2504 break;
2505 case '/':
2506 case '%':
ths5fafdf22007-09-16 21:08:06 +00002507 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002508 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002509 if (op == '/')
2510 val /= val2;
2511 else
2512 val %= val2;
2513 break;
2514 }
2515 }
2516 return val;
2517}
2518
aliguori376253e2009-03-05 23:01:23 +00002519static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002520{
blueswir1c2efc952007-09-25 17:28:42 +00002521 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002522 int op;
bellard9307c4c2004-04-04 12:57:25 +00002523
aliguori376253e2009-03-05 23:01:23 +00002524 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002525 for(;;) {
2526 op = *pch;
2527 if (op != '&' && op != '|' && op != '^')
2528 break;
2529 next();
aliguori376253e2009-03-05 23:01:23 +00002530 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002531 switch(op) {
2532 default:
2533 case '&':
2534 val &= val2;
2535 break;
2536 case '|':
2537 val |= val2;
2538 break;
2539 case '^':
2540 val ^= val2;
2541 break;
2542 }
2543 }
2544 return val;
2545}
2546
aliguori376253e2009-03-05 23:01:23 +00002547static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002548{
blueswir1c2efc952007-09-25 17:28:42 +00002549 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002550 int op;
bellard9307c4c2004-04-04 12:57:25 +00002551
aliguori376253e2009-03-05 23:01:23 +00002552 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002553 for(;;) {
2554 op = *pch;
2555 if (op != '+' && op != '-')
2556 break;
2557 next();
aliguori376253e2009-03-05 23:01:23 +00002558 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002559 if (op == '+')
2560 val += val2;
2561 else
2562 val -= val2;
2563 }
2564 return val;
2565}
2566
aliguori376253e2009-03-05 23:01:23 +00002567static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002568{
2569 pch = *pp;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002570 if (sigsetjmp(expr_env, 0)) {
bellard9307c4c2004-04-04 12:57:25 +00002571 *pp = pch;
2572 return -1;
2573 }
blueswir1cd390082008-11-16 13:53:32 +00002574 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002575 pch++;
aliguori376253e2009-03-05 23:01:23 +00002576 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002577 *pp = pch;
2578 return 0;
2579}
2580
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002581static int get_double(Monitor *mon, double *pval, const char **pp)
2582{
2583 const char *p = *pp;
2584 char *tailp;
2585 double d;
2586
2587 d = strtod(p, &tailp);
2588 if (tailp == p) {
2589 monitor_printf(mon, "Number expected\n");
2590 return -1;
2591 }
2592 if (d != d || d - d != 0) {
2593 /* NaN or infinity */
2594 monitor_printf(mon, "Bad number\n");
2595 return -1;
2596 }
2597 *pval = d;
2598 *pp = tailp;
2599 return 0;
2600}
2601
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002602/*
2603 * Store the command-name in cmdname, and return a pointer to
2604 * the remaining of the command string.
2605 */
2606static const char *get_command_name(const char *cmdline,
2607 char *cmdname, size_t nlen)
2608{
2609 size_t len;
2610 const char *p, *pstart;
2611
2612 p = cmdline;
2613 while (qemu_isspace(*p))
2614 p++;
2615 if (*p == '\0')
2616 return NULL;
2617 pstart = p;
2618 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2619 p++;
2620 len = p - pstart;
2621 if (len > nlen - 1)
2622 len = nlen - 1;
2623 memcpy(cmdname, pstart, len);
2624 cmdname[len] = '\0';
2625 return p;
2626}
2627
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002628/**
2629 * Read key of 'type' into 'key' and return the current
2630 * 'type' pointer.
2631 */
2632static char *key_get_info(const char *type, char **key)
2633{
2634 size_t len;
2635 char *p, *str;
2636
2637 if (*type == ',')
2638 type++;
2639
2640 p = strchr(type, ':');
2641 if (!p) {
2642 *key = NULL;
2643 return NULL;
2644 }
2645 len = p - type;
2646
Anthony Liguori7267c092011-08-20 22:09:37 -05002647 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002648 memcpy(str, type, len);
2649 str[len] = '\0';
2650
2651 *key = str;
2652 return ++p;
2653}
2654
bellard9307c4c2004-04-04 12:57:25 +00002655static int default_fmt_format = 'x';
2656static int default_fmt_size = 4;
2657
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002658static int is_valid_option(const char *c, const char *typestr)
2659{
2660 char option[3];
2661
2662 option[0] = '-';
2663 option[1] = *c;
2664 option[2] = '\0';
2665
2666 typestr = strstr(typestr, option);
2667 return (typestr != NULL);
2668}
2669
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03002670static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
2671 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002672{
2673 const mon_cmd_t *cmd;
2674
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03002675 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002676 if (compare_cmd(cmdname, cmd->name)) {
2677 return cmd;
2678 }
2679 }
2680
2681 return NULL;
2682}
2683
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002684/*
Bandan Dasae502122015-06-03 18:38:08 -04002685 * Parse command name from @cmdp according to command table @table.
2686 * If blank, return NULL.
2687 * Else, if no valid command can be found, report to @mon, and return
2688 * NULL.
2689 * Else, change @cmdp to point right behind the name, and return its
2690 * command table entry.
2691 * Do not assume the return value points into @table! It doesn't when
2692 * the command is found in a sub-command table.
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002693 */
Anthony Liguoric227f092009-10-01 16:12:16 -05002694static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Bandan Dasae502122015-06-03 18:38:08 -04002695 const char **cmdp,
2696 mon_cmd_t *table)
bellard9307c4c2004-04-04 12:57:25 +00002697{
Bandan Dasae502122015-06-03 18:38:08 -04002698 const char *p;
Anthony Liguoric227f092009-10-01 16:12:16 -05002699 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002700 char cmdname[256];
bellard9dc39cb2004-03-14 21:38:27 +00002701
bellard9307c4c2004-04-04 12:57:25 +00002702 /* extract the command name */
Bandan Dasae502122015-06-03 18:38:08 -04002703 p = get_command_name(*cmdp, cmdname, sizeof(cmdname));
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002704 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002705 return NULL;
ths3b46e622007-09-17 08:09:54 +00002706
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002707 cmd = search_dispatch_table(table, cmdname);
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002708 if (!cmd) {
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002709 monitor_printf(mon, "unknown command: '%.*s'\n",
Bandan Dasae502122015-06-03 18:38:08 -04002710 (int)(p - *cmdp), *cmdp);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002711 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002712 }
bellard9307c4c2004-04-04 12:57:25 +00002713
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002714 /* filter out following useless space */
2715 while (qemu_isspace(*p)) {
2716 p++;
2717 }
Bandan Dasae502122015-06-03 18:38:08 -04002718
2719 *cmdp = p;
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002720 /* search sub command */
Bandan Dasae502122015-06-03 18:38:08 -04002721 if (cmd->sub_table != NULL && *p != '\0') {
2722 return monitor_parse_command(mon, cmdp, cmd->sub_table);
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002723 }
2724
Bandan Dasae502122015-06-03 18:38:08 -04002725 return cmd;
2726}
2727
2728/*
2729 * Parse arguments for @cmd.
2730 * If it can't be parsed, report to @mon, and return NULL.
2731 * Else, insert command arguments into a QDict, and return it.
2732 * Note: On success, caller has to free the QDict structure.
2733 */
2734
2735static QDict *monitor_parse_arguments(Monitor *mon,
2736 const char **endp,
2737 const mon_cmd_t *cmd)
2738{
2739 const char *typestr;
2740 char *key;
2741 int c;
2742 const char *p = *endp;
2743 char buf[1024];
2744 QDict *qdict = qdict_new();
2745
bellard9307c4c2004-04-04 12:57:25 +00002746 /* parse the parameters */
2747 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00002748 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002749 typestr = key_get_info(typestr, &key);
2750 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00002751 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002752 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00002753 typestr++;
2754 switch(c) {
2755 case 'F':
bellard81d09122004-07-14 17:21:37 +00002756 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002757 case 's':
2758 {
2759 int ret;
ths3b46e622007-09-17 08:09:54 +00002760
blueswir1cd390082008-11-16 13:53:32 +00002761 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002762 p++;
2763 if (*typestr == '?') {
2764 typestr++;
2765 if (*p == '\0') {
2766 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03002767 break;
bellard9307c4c2004-04-04 12:57:25 +00002768 }
2769 }
2770 ret = get_str(buf, sizeof(buf), &p);
2771 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002772 switch(c) {
2773 case 'F':
aliguori376253e2009-03-05 23:01:23 +00002774 monitor_printf(mon, "%s: filename expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002775 cmd->name);
bellard81d09122004-07-14 17:21:37 +00002776 break;
2777 case 'B':
aliguori376253e2009-03-05 23:01:23 +00002778 monitor_printf(mon, "%s: block device name expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002779 cmd->name);
bellard81d09122004-07-14 17:21:37 +00002780 break;
2781 default:
Bandan Dasae502122015-06-03 18:38:08 -04002782 monitor_printf(mon, "%s: string expected\n", cmd->name);
bellard81d09122004-07-14 17:21:37 +00002783 break;
2784 }
bellard9307c4c2004-04-04 12:57:25 +00002785 goto fail;
2786 }
Eric Blake46f5ac22017-04-27 16:58:17 -05002787 qdict_put_str(qdict, key, buf);
bellard9307c4c2004-04-04 12:57:25 +00002788 }
2789 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01002790 case 'O':
2791 {
2792 QemuOptsList *opts_list;
2793 QemuOpts *opts;
2794
2795 opts_list = qemu_find_opts(key);
2796 if (!opts_list || opts_list->desc->name) {
2797 goto bad_type;
2798 }
2799 while (qemu_isspace(*p)) {
2800 p++;
2801 }
2802 if (!*p)
2803 break;
2804 if (get_str(buf, sizeof(buf), &p) < 0) {
2805 goto fail;
2806 }
Markus Armbruster70b94332015-02-13 12:50:26 +01002807 opts = qemu_opts_parse_noisily(opts_list, buf, true);
Markus Armbruster361127d2010-02-10 20:24:35 +01002808 if (!opts) {
2809 goto fail;
2810 }
2811 qemu_opts_to_qdict(opts, qdict);
2812 qemu_opts_del(opts);
2813 }
2814 break;
bellard9307c4c2004-04-04 12:57:25 +00002815 case '/':
2816 {
2817 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002818
blueswir1cd390082008-11-16 13:53:32 +00002819 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002820 p++;
2821 if (*p == '/') {
2822 /* format found */
2823 p++;
2824 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002825 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002826 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002827 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002828 count = count * 10 + (*p - '0');
2829 p++;
2830 }
2831 }
2832 size = -1;
2833 format = -1;
2834 for(;;) {
2835 switch(*p) {
2836 case 'o':
2837 case 'd':
2838 case 'u':
2839 case 'x':
2840 case 'i':
2841 case 'c':
2842 format = *p++;
2843 break;
2844 case 'b':
2845 size = 1;
2846 p++;
2847 break;
2848 case 'h':
2849 size = 2;
2850 p++;
2851 break;
2852 case 'w':
2853 size = 4;
2854 p++;
2855 break;
2856 case 'g':
2857 case 'L':
2858 size = 8;
2859 p++;
2860 break;
2861 default:
2862 goto next;
2863 }
2864 }
2865 next:
blueswir1cd390082008-11-16 13:53:32 +00002866 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00002867 monitor_printf(mon, "invalid char in format: '%c'\n",
2868 *p);
bellard9307c4c2004-04-04 12:57:25 +00002869 goto fail;
2870 }
bellard9307c4c2004-04-04 12:57:25 +00002871 if (format < 0)
2872 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002873 if (format != 'i') {
2874 /* for 'i', not specifying a size gives -1 as size */
2875 if (size < 0)
2876 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002877 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002878 }
bellard9307c4c2004-04-04 12:57:25 +00002879 default_fmt_format = format;
2880 } else {
2881 count = 1;
2882 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002883 if (format != 'i') {
2884 size = default_fmt_size;
2885 } else {
2886 size = -1;
2887 }
bellard9307c4c2004-04-04 12:57:25 +00002888 }
Eric Blake46f5ac22017-04-27 16:58:17 -05002889 qdict_put_int(qdict, "count", count);
2890 qdict_put_int(qdict, "format", format);
2891 qdict_put_int(qdict, "size", size);
bellard9307c4c2004-04-04 12:57:25 +00002892 }
2893 break;
2894 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002895 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002896 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00002897 {
blueswir1c2efc952007-09-25 17:28:42 +00002898 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00002899
blueswir1cd390082008-11-16 13:53:32 +00002900 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002901 p++;
bellard34405572004-06-08 00:55:58 +00002902 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002903 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03002904 if (*p == '\0') {
2905 typestr++;
2906 break;
2907 }
bellard34405572004-06-08 00:55:58 +00002908 } else {
2909 if (*p == '.') {
2910 p++;
blueswir1cd390082008-11-16 13:53:32 +00002911 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00002912 p++;
bellard34405572004-06-08 00:55:58 +00002913 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03002914 typestr++;
2915 break;
bellard34405572004-06-08 00:55:58 +00002916 }
2917 }
bellard13224a82006-07-14 22:03:35 +00002918 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002919 }
aliguori376253e2009-03-05 23:01:23 +00002920 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00002921 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002922 /* Check if 'i' is greater than 32-bit */
2923 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
Bandan Dasae502122015-06-03 18:38:08 -04002924 monitor_printf(mon, "\'%s\' has failed: ", cmd->name);
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002925 monitor_printf(mon, "integer is for 32-bit values\n");
2926 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002927 } else if (c == 'M') {
Luiz Capitulino91162842012-04-26 17:34:30 -03002928 if (val < 0) {
2929 monitor_printf(mon, "enter a positive value\n");
2930 goto fail;
2931 }
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002932 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002933 }
Eric Blake46f5ac22017-04-27 16:58:17 -05002934 qdict_put_int(qdict, key, val);
bellard9307c4c2004-04-04 12:57:25 +00002935 }
2936 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02002937 case 'o':
2938 {
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +01002939 int ret;
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +01002940 uint64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02002941 char *end;
2942
2943 while (qemu_isspace(*p)) {
2944 p++;
2945 }
2946 if (*typestr == '?') {
2947 typestr++;
2948 if (*p == '\0') {
2949 break;
2950 }
2951 }
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +01002952 ret = qemu_strtosz_MiB(p, &end, &val);
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +01002953 if (ret < 0 || val > INT64_MAX) {
Jes Sorensendbc0c672010-10-21 17:15:47 +02002954 monitor_printf(mon, "invalid size\n");
2955 goto fail;
2956 }
Eric Blake46f5ac22017-04-27 16:58:17 -05002957 qdict_put_int(qdict, key, val);
Jes Sorensendbc0c672010-10-21 17:15:47 +02002958 p = end;
2959 }
2960 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01002961 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002962 {
2963 double val;
2964
2965 while (qemu_isspace(*p))
2966 p++;
2967 if (*typestr == '?') {
2968 typestr++;
2969 if (*p == '\0') {
2970 break;
2971 }
2972 }
2973 if (get_double(mon, &val, &p) < 0) {
2974 goto fail;
2975 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02002976 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01002977 switch (*p) {
2978 case 'm':
2979 val /= 1e3; p += 2; break;
2980 case 'u':
2981 val /= 1e6; p += 2; break;
2982 case 'n':
2983 val /= 1e9; p += 2; break;
2984 }
2985 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002986 if (*p && !qemu_isspace(*p)) {
2987 monitor_printf(mon, "Unknown unit suffix\n");
2988 goto fail;
2989 }
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +04002990 qdict_put(qdict, key, qnum_from_double(val));
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002991 }
2992 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002993 case 'b':
2994 {
2995 const char *beg;
Eric Blakefc48ffc2015-05-15 16:24:59 -06002996 bool val;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002997
2998 while (qemu_isspace(*p)) {
2999 p++;
3000 }
3001 beg = p;
3002 while (qemu_isgraph(*p)) {
3003 p++;
3004 }
3005 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06003006 val = true;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003007 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06003008 val = false;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003009 } else {
3010 monitor_printf(mon, "Expected 'on' or 'off'\n");
3011 goto fail;
3012 }
Eric Blake46f5ac22017-04-27 16:58:17 -05003013 qdict_put_bool(qdict, key, val);
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003014 }
3015 break;
bellard9307c4c2004-04-04 12:57:25 +00003016 case '-':
3017 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003018 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003019 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003020 /* option */
ths3b46e622007-09-17 08:09:54 +00003021
bellard9307c4c2004-04-04 12:57:25 +00003022 c = *typestr++;
3023 if (c == '\0')
3024 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003025 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003026 p++;
bellard9307c4c2004-04-04 12:57:25 +00003027 if (*p == '-') {
3028 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003029 if(c != *p) {
3030 if(!is_valid_option(p, typestr)) {
3031
3032 monitor_printf(mon, "%s: unsupported option -%c\n",
Bandan Dasae502122015-06-03 18:38:08 -04003033 cmd->name, *p);
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003034 goto fail;
3035 } else {
3036 skip_key = 1;
3037 }
bellard9307c4c2004-04-04 12:57:25 +00003038 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003039 if(skip_key) {
3040 p = tmp;
3041 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003042 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003043 p++;
Eric Blake46f5ac22017-04-27 16:58:17 -05003044 qdict_put_bool(qdict, key, true);
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003045 }
bellard9307c4c2004-04-04 12:57:25 +00003046 }
bellard9307c4c2004-04-04 12:57:25 +00003047 }
3048 break;
Wenchao Xia129be002013-08-27 20:38:26 +08003049 case 'S':
3050 {
3051 /* package all remaining string */
3052 int len;
3053
3054 while (qemu_isspace(*p)) {
3055 p++;
3056 }
3057 if (*typestr == '?') {
3058 typestr++;
3059 if (*p == '\0') {
3060 /* no remaining string: NULL argument */
3061 break;
3062 }
3063 }
3064 len = strlen(p);
3065 if (len <= 0) {
3066 monitor_printf(mon, "%s: string expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04003067 cmd->name);
Bandan Dase549d2a2015-06-03 18:38:10 -04003068 goto fail;
Wenchao Xia129be002013-08-27 20:38:26 +08003069 }
Eric Blake46f5ac22017-04-27 16:58:17 -05003070 qdict_put_str(qdict, key, p);
Wenchao Xia129be002013-08-27 20:38:26 +08003071 p += len;
3072 }
3073 break;
bellard9307c4c2004-04-04 12:57:25 +00003074 default:
3075 bad_type:
Bandan Dasae502122015-06-03 18:38:08 -04003076 monitor_printf(mon, "%s: unknown type '%c'\n", cmd->name, c);
bellard9307c4c2004-04-04 12:57:25 +00003077 goto fail;
3078 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003079 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003080 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003081 }
3082 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003083 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003084 p++;
3085 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003086 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
Bandan Dasae502122015-06-03 18:38:08 -04003087 cmd->name);
bellard9307c4c2004-04-04 12:57:25 +00003088 goto fail;
3089 }
3090
Bandan Dasae502122015-06-03 18:38:08 -04003091 return qdict;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003092
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003093fail:
Bandan Dasae502122015-06-03 18:38:08 -04003094 QDECREF(qdict);
Anthony Liguori7267c092011-08-20 22:09:37 -05003095 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003096 return NULL;
3097}
3098
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003099static void handle_hmp_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003100{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003101 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003102 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003103
Stefan Hajnoczi79cad8b2017-06-05 11:42:15 +01003104 trace_handle_hmp_command(mon, cmdline);
3105
Bandan Dasae502122015-06-03 18:38:08 -04003106 cmd = monitor_parse_command(mon, &cmdline, mon->cmd_table);
3107 if (!cmd) {
3108 return;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003109 }
3110
Bandan Dasae502122015-06-03 18:38:08 -04003111 qdict = monitor_parse_arguments(mon, &cmdline, cmd);
3112 if (!qdict) {
Bandan Dasdd41eea2015-06-03 18:38:09 -04003113 monitor_printf(mon, "Try \"help %s\" for more information\n",
3114 cmd->name);
Bandan Dasae502122015-06-03 18:38:08 -04003115 return;
3116 }
3117
Marc-André Lureau2b9e3572016-09-12 13:19:06 +04003118 cmd->cmd(mon, qdict);
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003119 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00003120}
3121
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003122static void cmd_completion(Monitor *mon, const char *name, const char *list)
bellard81d09122004-07-14 17:21:37 +00003123{
3124 const char *p, *pstart;
3125 char cmd[128];
3126 int len;
3127
3128 p = list;
3129 for(;;) {
3130 pstart = p;
3131 p = strchr(p, '|');
3132 if (!p)
3133 p = pstart + strlen(pstart);
3134 len = p - pstart;
3135 if (len > sizeof(cmd) - 2)
3136 len = sizeof(cmd) - 2;
3137 memcpy(cmd, pstart, len);
3138 cmd[len] = '\0';
3139 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003140 readline_add_completion(mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00003141 }
3142 if (*p == '\0')
3143 break;
3144 p++;
3145 }
3146}
3147
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003148static void file_completion(Monitor *mon, const char *input)
bellard81d09122004-07-14 17:21:37 +00003149{
3150 DIR *ffs;
3151 struct dirent *d;
3152 char path[1024];
3153 char file[1024], file_prefix[1024];
3154 int input_path_len;
3155 const char *p;
3156
ths5fafdf22007-09-16 21:08:06 +00003157 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00003158 if (!p) {
3159 input_path_len = 0;
3160 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00003161 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00003162 } else {
3163 input_path_len = p - input + 1;
3164 memcpy(path, input, input_path_len);
3165 if (input_path_len > sizeof(path) - 1)
3166 input_path_len = sizeof(path) - 1;
3167 path[input_path_len] = '\0';
3168 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3169 }
Bandan Das19f2db52015-06-03 18:38:07 -04003170
bellard81d09122004-07-14 17:21:37 +00003171 ffs = opendir(path);
3172 if (!ffs)
3173 return;
3174 for(;;) {
3175 struct stat sb;
3176 d = readdir(ffs);
3177 if (!d)
3178 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09003179
3180 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
3181 continue;
3182 }
3183
bellard81d09122004-07-14 17:21:37 +00003184 if (strstart(d->d_name, file_prefix, NULL)) {
3185 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00003186 if (input_path_len < sizeof(file))
3187 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3188 d->d_name);
bellard81d09122004-07-14 17:21:37 +00003189 /* stat the file to find out if it's a directory.
3190 * In that case add a slash to speed up typing long paths
3191 */
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01003192 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
blueswir1363a37d2008-08-21 17:58:08 +00003193 pstrcat(file, sizeof(file), "/");
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01003194 }
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003195 readline_add_completion(mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003196 }
3197 }
3198 closedir(ffs);
3199}
3200
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003201static const char *next_arg_type(const char *typestr)
3202{
3203 const char *p = strchr(typestr, ':');
3204 return (p != NULL ? ++p : typestr);
3205}
3206
Hani Benhabiles40d19392014-05-07 23:41:30 +01003207static void add_completion_option(ReadLineState *rs, const char *str,
3208 const char *option)
3209{
3210 if (!str || !option) {
3211 return;
3212 }
3213 if (!strncmp(option, str, strlen(str))) {
3214 readline_add_completion(rs, option);
3215 }
3216}
3217
Hani Benhabiles13e315d2014-05-07 23:41:29 +01003218void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
3219{
3220 size_t len;
3221 ChardevBackendInfoList *list, *start;
3222
3223 if (nb_args != 2) {
3224 return;
3225 }
3226 len = strlen(str);
3227 readline_set_completion_index(rs, len);
3228
3229 start = list = qmp_query_chardev_backends(NULL);
3230 while (list) {
3231 const char *chr_name = list->value->name;
3232
3233 if (!strncmp(chr_name, str, len)) {
3234 readline_add_completion(rs, chr_name);
3235 }
3236 list = list->next;
3237 }
3238 qapi_free_ChardevBackendInfoList(start);
3239}
3240
Hani Benhabilesb162b492014-05-07 23:41:31 +01003241void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
3242{
3243 size_t len;
3244 int i;
3245
3246 if (nb_args != 2) {
3247 return;
3248 }
3249 len = strlen(str);
3250 readline_set_completion_index(rs, len);
Eric Blakef394b2e2016-07-13 21:50:23 -06003251 for (i = 0; NetClientDriver_lookup[i]; i++) {
3252 add_completion_option(rs, str, NetClientDriver_lookup[i]);
Hani Benhabilesb162b492014-05-07 23:41:31 +01003253 }
3254}
3255
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003256void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003257{
3258 GSList *list, *elt;
3259 size_t len;
3260
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003261 if (nb_args != 2) {
3262 return;
3263 }
3264
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003265 len = strlen(str);
3266 readline_set_completion_index(rs, len);
3267 list = elt = object_class_get_list(TYPE_DEVICE, false);
3268 while (elt) {
3269 const char *name;
3270 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
3271 TYPE_DEVICE);
3272 name = object_class_get_name(OBJECT_CLASS(dc));
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003273
Eduardo Habkoste90f2a82017-05-03 17:35:44 -03003274 if (dc->user_creatable
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003275 && !strncmp(name, str, len)) {
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003276 readline_add_completion(rs, name);
3277 }
3278 elt = elt->next;
3279 }
3280 g_slist_free(list);
3281}
3282
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003283void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003284{
3285 GSList *list, *elt;
3286 size_t len;
3287
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003288 if (nb_args != 2) {
3289 return;
3290 }
3291
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003292 len = strlen(str);
3293 readline_set_completion_index(rs, len);
3294 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
3295 while (elt) {
3296 const char *name;
3297
3298 name = object_class_get_name(OBJECT_CLASS(elt->data));
3299 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
3300 readline_add_completion(rs, name);
3301 }
3302 elt = elt->next;
3303 }
3304 g_slist_free(list);
3305}
3306
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003307static void peripheral_device_del_completion(ReadLineState *rs,
3308 const char *str, size_t len)
3309{
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003310 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
3311 GSList *list, *item;
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003312
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003313 list = qdev_build_hotpluggable_device_list(peripheral);
3314 if (!list) {
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003315 return;
3316 }
3317
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003318 for (item = list; item; item = g_slist_next(item)) {
3319 DeviceState *dev = item->data;
3320
3321 if (dev->id && !strncmp(str, dev->id, len)) {
3322 readline_add_completion(rs, dev->id);
3323 }
3324 }
3325
3326 g_slist_free(list);
3327}
3328
Hani Benhabiles6297d9a2014-05-07 23:41:28 +01003329void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3330{
3331 size_t len;
3332 ChardevInfoList *list, *start;
3333
3334 if (nb_args != 2) {
3335 return;
3336 }
3337 len = strlen(str);
3338 readline_set_completion_index(rs, len);
3339
3340 start = list = qmp_query_chardev(NULL);
3341 while (list) {
3342 ChardevInfo *chr = list->value;
3343
3344 if (!strncmp(chr->label, str, len)) {
3345 readline_add_completion(rs, chr->label);
3346 }
3347 list = list->next;
3348 }
3349 qapi_free_ChardevInfoList(start);
3350}
3351
Hani Benhabiles8e597772014-05-27 23:39:30 +01003352static void ringbuf_completion(ReadLineState *rs, const char *str)
3353{
3354 size_t len;
3355 ChardevInfoList *list, *start;
3356
3357 len = strlen(str);
3358 readline_set_completion_index(rs, len);
3359
3360 start = list = qmp_query_chardev(NULL);
3361 while (list) {
3362 ChardevInfo *chr_info = list->value;
3363
3364 if (!strncmp(chr_info->label, str, len)) {
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03003365 Chardev *chr = qemu_chr_find(chr_info->label);
Marc-André Lureau777357d2016-12-07 18:39:10 +03003366 if (chr && CHARDEV_IS_RINGBUF(chr)) {
Hani Benhabiles8e597772014-05-27 23:39:30 +01003367 readline_add_completion(rs, chr_info->label);
3368 }
3369 }
3370 list = list->next;
3371 }
3372 qapi_free_ChardevInfoList(start);
3373}
3374
Hani Benhabiles8e597772014-05-27 23:39:30 +01003375void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
3376{
3377 if (nb_args != 2) {
3378 return;
3379 }
3380 ringbuf_completion(rs, str);
3381}
3382
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003383void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
3384{
3385 size_t len;
3386
3387 if (nb_args != 2) {
3388 return;
3389 }
3390
3391 len = strlen(str);
3392 readline_set_completion_index(rs, len);
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003393 peripheral_device_del_completion(rs, str, len);
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003394}
3395
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003396void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003397{
3398 ObjectPropertyInfoList *list, *start;
3399 size_t len;
3400
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003401 if (nb_args != 2) {
3402 return;
3403 }
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003404 len = strlen(str);
3405 readline_set_completion_index(rs, len);
3406
3407 start = list = qmp_qom_list("/objects", NULL);
3408 while (list) {
3409 ObjectPropertyInfo *info = list->value;
3410
3411 if (!strncmp(info->type, "child<", 5)
3412 && !strncmp(info->name, str, len)) {
3413 readline_add_completion(rs, info->name);
3414 }
3415 list = list->next;
3416 }
3417 qapi_free_ObjectPropertyInfoList(start);
3418}
3419
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003420void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
3421{
3422 int i;
3423 char *sep;
3424 size_t len;
3425
3426 if (nb_args != 2) {
3427 return;
3428 }
3429 sep = strrchr(str, '-');
3430 if (sep) {
3431 str = sep + 1;
3432 }
3433 len = strlen(str);
3434 readline_set_completion_index(rs, len);
Eric Blake7fb1cf12015-11-18 01:52:57 -07003435 for (i = 0; i < Q_KEY_CODE__MAX; i++) {
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003436 if (!strncmp(str, QKeyCode_lookup[i], len)) {
3437 readline_add_completion(rs, QKeyCode_lookup[i]);
3438 }
3439 }
3440}
3441
Hani Benhabiles40d19392014-05-07 23:41:30 +01003442void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
3443{
3444 size_t len;
3445
3446 len = strlen(str);
3447 readline_set_completion_index(rs, len);
3448 if (nb_args == 2) {
Jason Wangeaed4832015-04-23 14:21:38 +08003449 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles40d19392014-05-07 23:41:30 +01003450 int count, i;
3451 count = qemu_find_net_clients_except(NULL, ncs,
Eric Blakef394b2e2016-07-13 21:50:23 -06003452 NET_CLIENT_DRIVER_NONE,
Jason Wangeaed4832015-04-23 14:21:38 +08003453 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003454 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles40d19392014-05-07 23:41:30 +01003455 const char *name = ncs[i]->name;
3456 if (!strncmp(str, name, len)) {
3457 readline_add_completion(rs, name);
3458 }
3459 }
3460 } else if (nb_args == 3) {
3461 add_completion_option(rs, str, "on");
3462 add_completion_option(rs, str, "off");
3463 }
3464}
3465
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003466void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
3467{
3468 int len, count, i;
Jason Wangeaed4832015-04-23 14:21:38 +08003469 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003470
3471 if (nb_args != 2) {
3472 return;
3473 }
3474
3475 len = strlen(str);
3476 readline_set_completion_index(rs, len);
Eric Blakef394b2e2016-07-13 21:50:23 -06003477 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_DRIVER_NIC,
Jason Wangeaed4832015-04-23 14:21:38 +08003478 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003479 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003480 QemuOpts *opts;
3481 const char *name = ncs[i]->name;
3482 if (strncmp(str, name, len)) {
3483 continue;
3484 }
3485 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
3486 if (opts) {
3487 readline_add_completion(rs, name);
3488 }
3489 }
3490}
3491
Lluís Vilanovabd712112016-07-11 12:53:51 +02003492void info_trace_events_completion(ReadLineState *rs, int nb_args, const char *str)
3493{
3494 size_t len;
3495
3496 len = strlen(str);
3497 readline_set_completion_index(rs, len);
3498 if (nb_args == 2) {
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003499 TraceEventIter iter;
3500 TraceEvent *ev;
3501 char *pattern = g_strdup_printf("%s*", str);
3502 trace_event_iter_init(&iter, pattern);
3503 while ((ev = trace_event_iter_next(&iter)) != NULL) {
3504 readline_add_completion(rs, trace_event_get_name(ev));
Lluís Vilanovabd712112016-07-11 12:53:51 +02003505 }
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003506 g_free(pattern);
Lluís Vilanovabd712112016-07-11 12:53:51 +02003507 }
3508}
3509
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003510void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
3511{
3512 size_t len;
3513
3514 len = strlen(str);
3515 readline_set_completion_index(rs, len);
3516 if (nb_args == 2) {
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003517 TraceEventIter iter;
3518 TraceEvent *ev;
3519 char *pattern = g_strdup_printf("%s*", str);
3520 trace_event_iter_init(&iter, pattern);
3521 while ((ev = trace_event_iter_next(&iter)) != NULL) {
3522 readline_add_completion(rs, trace_event_get_name(ev));
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003523 }
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003524 g_free(pattern);
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003525 } else if (nb_args == 3) {
3526 add_completion_option(rs, str, "on");
3527 add_completion_option(rs, str, "off");
3528 }
3529}
3530
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003531void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
3532{
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003533 int i;
3534
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003535 if (nb_args != 2) {
3536 return;
3537 }
3538 readline_set_completion_index(rs, strlen(str));
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003539 for (i = 0; WatchdogExpirationAction_lookup[i]; i++) {
3540 add_completion_option(rs, str, WatchdogExpirationAction_lookup[i]);
3541 }
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003542}
3543
Hani Benhabilesc68a0402014-05-27 23:39:32 +01003544void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
3545 const char *str)
3546{
3547 size_t len;
3548
3549 len = strlen(str);
3550 readline_set_completion_index(rs, len);
3551 if (nb_args == 2) {
3552 int i;
Eric Blake7fb1cf12015-11-18 01:52:57 -07003553 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Hani Benhabilesc68a0402014-05-27 23:39:32 +01003554 const char *name = MigrationCapability_lookup[i];
3555 if (!strncmp(str, name, len)) {
3556 readline_add_completion(rs, name);
3557 }
3558 }
3559 } else if (nb_args == 3) {
3560 add_completion_option(rs, str, "on");
3561 add_completion_option(rs, str, "off");
3562 }
3563}
3564
Liang Li50e9a622015-03-23 16:32:29 +08003565void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
3566 const char *str)
3567{
3568 size_t len;
3569
3570 len = strlen(str);
3571 readline_set_completion_index(rs, len);
3572 if (nb_args == 2) {
3573 int i;
Eric Blake7fb1cf12015-11-18 01:52:57 -07003574 for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
Liang Li50e9a622015-03-23 16:32:29 +08003575 const char *name = MigrationParameter_lookup[i];
3576 if (!strncmp(str, name, len)) {
3577 readline_add_completion(rs, name);
3578 }
3579 }
3580 }
3581}
3582
Hani Benhabilese3bb5322014-05-27 23:39:34 +01003583void host_net_add_completion(ReadLineState *rs, int nb_args, const char *str)
3584{
3585 int i;
3586 size_t len;
3587 if (nb_args != 2) {
3588 return;
3589 }
3590 len = strlen(str);
3591 readline_set_completion_index(rs, len);
3592 for (i = 0; host_net_devices[i]; i++) {
3593 if (!strncmp(host_net_devices[i], str, len)) {
3594 readline_add_completion(rs, host_net_devices[i]);
3595 }
3596 }
3597}
3598
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003599void host_net_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3600{
Jason Wangeaed4832015-04-23 14:21:38 +08003601 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003602 int count, i, len;
3603
3604 len = strlen(str);
3605 readline_set_completion_index(rs, len);
3606 if (nb_args == 2) {
3607 count = qemu_find_net_clients_except(NULL, ncs,
Eric Blakef394b2e2016-07-13 21:50:23 -06003608 NET_CLIENT_DRIVER_NONE,
Jason Wangeaed4832015-04-23 14:21:38 +08003609 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003610 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003611 int id;
3612 char name[16];
3613
3614 if (net_hub_id_for_client(ncs[i], &id)) {
3615 continue;
3616 }
3617 snprintf(name, sizeof(name), "%d", id);
3618 if (!strncmp(str, name, len)) {
3619 readline_add_completion(rs, name);
3620 }
3621 }
3622 return;
3623 } else if (nb_args == 3) {
3624 count = qemu_find_net_clients_except(NULL, ncs,
Eric Blakef394b2e2016-07-13 21:50:23 -06003625 NET_CLIENT_DRIVER_NIC,
Jason Wangeaed4832015-04-23 14:21:38 +08003626 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003627 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Jason Wang2c4681f2015-02-02 15:06:38 +08003628 int id;
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003629 const char *name;
3630
Eric Blakef394b2e2016-07-13 21:50:23 -06003631 if (ncs[i]->info->type == NET_CLIENT_DRIVER_HUBPORT ||
Jason Wang2c4681f2015-02-02 15:06:38 +08003632 net_hub_id_for_client(ncs[i], &id)) {
3633 continue;
3634 }
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003635 name = ncs[i]->name;
3636 if (!strncmp(str, name, len)) {
3637 readline_add_completion(rs, name);
3638 }
3639 }
3640 return;
3641 }
3642}
3643
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003644static void vm_completion(ReadLineState *rs, const char *str)
3645{
3646 size_t len;
Kevin Wolf7c8eece2016-03-22 18:58:50 +01003647 BlockDriverState *bs;
Kevin Wolf88be7b42016-05-20 18:49:07 +02003648 BdrvNextIterator it;
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003649
3650 len = strlen(str);
3651 readline_set_completion_index(rs, len);
Kevin Wolf7c8eece2016-03-22 18:58:50 +01003652
Kevin Wolf88be7b42016-05-20 18:49:07 +02003653 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003654 SnapshotInfoList *snapshots, *snapshot;
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003655 AioContext *ctx = bdrv_get_aio_context(bs);
3656 bool ok = false;
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003657
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003658 aio_context_acquire(ctx);
3659 if (bdrv_can_snapshot(bs)) {
3660 ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0;
3661 }
3662 aio_context_release(ctx);
3663 if (!ok) {
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003664 continue;
3665 }
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003666
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003667 snapshot = snapshots;
3668 while (snapshot) {
3669 char *completion = snapshot->value->name;
3670 if (!strncmp(str, completion, len)) {
3671 readline_add_completion(rs, completion);
3672 }
3673 completion = snapshot->value->id;
3674 if (!strncmp(str, completion, len)) {
3675 readline_add_completion(rs, completion);
3676 }
3677 snapshot = snapshot->next;
3678 }
3679 qapi_free_SnapshotInfoList(snapshots);
3680 }
3681
3682}
3683
3684void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
3685{
3686 if (nb_args == 2) {
3687 vm_completion(rs, str);
3688 }
3689}
3690
3691void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
3692{
3693 if (nb_args == 2) {
3694 vm_completion(rs, str);
3695 }
3696}
3697
Wenchao Xiac35b6402013-08-27 20:38:24 +08003698static void monitor_find_completion_by_table(Monitor *mon,
3699 const mon_cmd_t *cmd_table,
3700 char **args,
3701 int nb_args)
bellard81d09122004-07-14 17:21:37 +00003702{
3703 const char *cmdname;
Wenchao Xiac35b6402013-08-27 20:38:24 +08003704 int i;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003705 const char *ptype, *str, *name;
Anthony Liguoric227f092009-10-01 16:12:16 -05003706 const mon_cmd_t *cmd;
Max Reitzda27a002016-03-16 19:54:29 +01003707 BlockBackend *blk = NULL;
bellard81d09122004-07-14 17:21:37 +00003708
bellard81d09122004-07-14 17:21:37 +00003709 if (nb_args <= 1) {
3710 /* command completion */
3711 if (nb_args == 0)
3712 cmdname = "";
3713 else
3714 cmdname = args[0];
Wenchao Xiad2674b22013-08-27 20:38:16 +08003715 readline_set_completion_index(mon->rs, strlen(cmdname));
Wenchao Xiac35b6402013-08-27 20:38:24 +08003716 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003717 cmd_completion(mon, cmdname, cmd->name);
bellard81d09122004-07-14 17:21:37 +00003718 }
3719 } else {
3720 /* find the command */
Wenchao Xiac35b6402013-08-27 20:38:24 +08003721 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Jan Kiszka03a63482010-06-16 00:38:33 +02003722 if (compare_cmd(args[0], cmd->name)) {
3723 break;
3724 }
bellard81d09122004-07-14 17:21:37 +00003725 }
Jan Kiszka03a63482010-06-16 00:38:33 +02003726 if (!cmd->name) {
Wenchao Xiac35b6402013-08-27 20:38:24 +08003727 return;
Jan Kiszka03a63482010-06-16 00:38:33 +02003728 }
3729
Wenchao Xiad903a772013-08-27 20:38:25 +08003730 if (cmd->sub_table) {
3731 /* do the job again */
Stefan Weile7ae7712015-03-08 19:30:01 +01003732 monitor_find_completion_by_table(mon, cmd->sub_table,
3733 &args[1], nb_args - 1);
3734 return;
Wenchao Xiad903a772013-08-27 20:38:25 +08003735 }
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003736 if (cmd->command_completion) {
Stefan Weile7ae7712015-03-08 19:30:01 +01003737 cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
3738 return;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003739 }
Wenchao Xiad903a772013-08-27 20:38:25 +08003740
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003741 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003742 for(i = 0; i < nb_args - 2; i++) {
3743 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003744 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003745 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003746 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003747 }
3748 }
3749 str = args[nb_args - 1];
Kevin Wolf48fe86f2014-11-12 16:24:02 +01003750 while (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02003751 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00003752 }
bellard81d09122004-07-14 17:21:37 +00003753 switch(*ptype) {
3754 case 'F':
3755 /* file completion */
Wenchao Xiad2674b22013-08-27 20:38:16 +08003756 readline_set_completion_index(mon->rs, strlen(str));
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003757 file_completion(mon, str);
bellard81d09122004-07-14 17:21:37 +00003758 break;
3759 case 'B':
3760 /* block device name completion */
Wenchao Xia599a9262013-08-27 20:38:15 +08003761 readline_set_completion_index(mon->rs, strlen(str));
Max Reitzda27a002016-03-16 19:54:29 +01003762 while ((blk = blk_next(blk)) != NULL) {
3763 name = blk_name(blk);
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003764 if (str[0] == '\0' ||
3765 !strncmp(name, str, strlen(str))) {
3766 readline_add_completion(mon->rs, name);
3767 }
3768 }
bellard81d09122004-07-14 17:21:37 +00003769 break;
bellard7fe48482004-10-09 18:08:01 +00003770 case 's':
Wenchao Xia129be002013-08-27 20:38:26 +08003771 case 'S':
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003772 if (!strcmp(cmd->name, "help|?")) {
Wenchao Xia7ca0e062013-08-27 20:38:27 +08003773 monitor_find_completion_by_table(mon, cmd_table,
3774 &args[1], nb_args - 1);
bellard7fe48482004-10-09 18:08:01 +00003775 }
3776 break;
bellard81d09122004-07-14 17:21:37 +00003777 default:
3778 break;
3779 }
3780 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08003781}
3782
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003783static void monitor_find_completion(void *opaque,
Wenchao Xiac35b6402013-08-27 20:38:24 +08003784 const char *cmdline)
3785{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003786 Monitor *mon = opaque;
Wenchao Xiac35b6402013-08-27 20:38:24 +08003787 char *args[MAX_ARGS];
3788 int nb_args, len;
3789
3790 /* 1. parse the cmdline */
3791 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
3792 return;
3793 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08003794
3795 /* if the line ends with a space, it means we want to complete the
3796 next arg */
3797 len = strlen(cmdline);
3798 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
3799 if (nb_args >= MAX_ARGS) {
3800 goto cleanup;
3801 }
3802 args[nb_args++] = g_strdup("");
3803 }
3804
3805 /* 2. auto complete according to args */
3806 monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args);
Jan Kiszka03a63482010-06-16 00:38:33 +02003807
3808cleanup:
Wenchao Xiadcc70cd2013-08-27 20:38:22 +08003809 free_cmdline_args(args, nb_args);
bellard81d09122004-07-14 17:21:37 +00003810}
3811
aliguori731b0362009-03-05 23:01:42 +00003812static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003813{
aliguori731b0362009-03-05 23:01:42 +00003814 Monitor *mon = opaque;
3815
Jan Kiszkac62313b2009-12-04 14:05:29 +01003816 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003817}
3818
Paolo Bonzini95385fe2015-11-25 22:23:31 +01003819static void handle_qmp_command(JSONMessageParser *parser, GQueue *tokens)
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003820{
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04003821 QObject *req, *rsp = NULL, *id = NULL;
3822 QDict *qdict = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003823 Monitor *mon = cur_mon;
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04003824 Error *err = NULL;
Stefan Hajnoczib097efc2017-06-05 11:42:16 +01003825 QString *req_json;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003826
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04003827 req = json_parser_parse_err(tokens, NULL, &err);
Markus Armbrusterbbf10282017-02-17 21:38:24 +01003828 if (!req && !err) {
3829 /* json_parser_parse_err() sucks: can fail without setting @err */
3830 error_setg(&err, QERR_JSON_PARSING);
3831 }
3832 if (err) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003833 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003834 }
3835
Markus Armbruster104fc302017-03-03 13:32:28 +01003836 qdict = qobject_to_qdict(req);
3837 if (qdict) {
3838 id = qdict_get(qdict, "id");
3839 qobject_incref(id);
3840 qdict_del(qdict, "id");
3841 } /* else will fail qmp_dispatch() */
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04003842
Stefan Hajnoczib097efc2017-06-05 11:42:16 +01003843 req_json = qobject_to_json(req);
3844 trace_handle_qmp_command(mon, qstring_get_str(req_json));
3845 qobject_decref(QOBJECT(req_json));
3846
Markus Armbruster635db182017-03-03 13:32:27 +01003847 rsp = qmp_dispatch(cur_mon->qmp.commands, req);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003848
Markus Armbruster635db182017-03-03 13:32:27 +01003849 if (mon->qmp.commands == &qmp_cap_negotiation_commands) {
3850 qdict = qdict_get_qdict(qobject_to_qdict(rsp), "error");
3851 if (qdict
3852 && !g_strcmp0(qdict_get_try_str(qdict, "class"),
3853 QapiErrorClass_lookup[ERROR_CLASS_COMMAND_NOT_FOUND])) {
3854 /* Provide a more useful error message */
3855 qdict_del(qdict, "desc");
Eric Blake46f5ac22017-04-27 16:58:17 -05003856 qdict_put_str(qdict, "desc", "Expecting capabilities negotiation"
3857 " with 'qmp_capabilities'");
Markus Armbruster635db182017-03-03 13:32:27 +01003858 }
3859 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003860
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003861err_out:
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04003862 if (err) {
3863 qdict = qdict_new();
3864 qdict_put_obj(qdict, "error", qmp_build_error_object(err));
3865 error_free(err);
3866 rsp = QOBJECT(qdict);
3867 }
3868
3869 if (rsp) {
3870 if (id) {
3871 qdict_put_obj(qobject_to_qdict(rsp), "id", id);
3872 id = NULL;
3873 }
3874
3875 monitor_json_emitter(mon, rsp);
3876 }
3877
3878 qobject_decref(id);
3879 qobject_decref(rsp);
3880 qobject_decref(req);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003881}
3882
Markus Armbrusterc83fe232015-03-06 19:20:51 +01003883static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003884{
3885 Monitor *old_mon = cur_mon;
3886
3887 cur_mon = opaque;
3888
Markus Armbruster74358f22015-03-06 19:35:59 +01003889 json_message_parser_feed(&cur_mon->qmp.parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003890
3891 cur_mon = old_mon;
3892}
3893
aliguori731b0362009-03-05 23:01:42 +00003894static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00003895{
aliguori731b0362009-03-05 23:01:42 +00003896 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00003897 int i;
aliguori376253e2009-03-05 23:01:23 +00003898
aliguori731b0362009-03-05 23:01:42 +00003899 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00003900
aliguoricde76ee2009-03-05 23:01:51 +00003901 if (cur_mon->rs) {
3902 for (i = 0; i < size; i++)
3903 readline_handle_byte(cur_mon->rs, buf[i]);
3904 } else {
3905 if (size == 0 || buf[size - 1] != 0)
3906 monitor_printf(cur_mon, "corrupted command\n");
3907 else
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003908 handle_hmp_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00003909 }
aliguori731b0362009-03-05 23:01:42 +00003910
3911 cur_mon = old_mon;
3912}
aliguorid8f44602008-10-06 13:52:44 +00003913
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003914static void monitor_command_cb(void *opaque, const char *cmdline,
3915 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00003916{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003917 Monitor *mon = opaque;
3918
aliguori731b0362009-03-05 23:01:42 +00003919 monitor_suspend(mon);
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003920 handle_hmp_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00003921 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00003922}
3923
aliguoricde76ee2009-03-05 23:01:51 +00003924int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003925{
aliguoricde76ee2009-03-05 23:01:51 +00003926 if (!mon->rs)
3927 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00003928 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00003929 return 0;
aliguorid8f44602008-10-06 13:52:44 +00003930}
3931
aliguori376253e2009-03-05 23:01:23 +00003932void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003933{
aliguoricde76ee2009-03-05 23:01:51 +00003934 if (!mon->rs)
3935 return;
aliguori731b0362009-03-05 23:01:42 +00003936 if (--mon->suspend_cnt == 0)
3937 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00003938}
3939
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003940static QObject *get_qmp_greeting(void)
3941{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03003942 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003943
Markus Armbruster7fad30f2015-09-16 13:06:19 +02003944 qmp_marshal_query_version(NULL, &ver, NULL);
Marc-André Lureauc8235012016-09-12 13:19:04 +04003945
3946 return qobject_from_jsonf("{'QMP': {'version': %p, 'capabilities': []}}",
3947 ver);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003948}
3949
Markus Armbrusterc83fe232015-03-06 19:20:51 +01003950static void monitor_qmp_event(void *opaque, int event)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003951{
Luiz Capitulino47116d12010-02-08 17:01:30 -02003952 QObject *data;
3953 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003954
Luiz Capitulino47116d12010-02-08 17:01:30 -02003955 switch (event) {
3956 case CHR_EVENT_OPENED:
Markus Armbruster635db182017-03-03 13:32:27 +01003957 mon->qmp.commands = &qmp_cap_negotiation_commands;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003958 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003959 monitor_json_emitter(mon, data);
3960 qobject_decref(data);
Corey Bryantefb87c12012-08-14 16:43:48 -04003961 mon_refcount++;
Luiz Capitulino47116d12010-02-08 17:01:30 -02003962 break;
3963 case CHR_EVENT_CLOSED:
Markus Armbruster74358f22015-03-06 19:35:59 +01003964 json_message_parser_destroy(&mon->qmp.parser);
3965 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Corey Bryantefb87c12012-08-14 16:43:48 -04003966 mon_refcount--;
3967 monitor_fdsets_cleanup();
Luiz Capitulino47116d12010-02-08 17:01:30 -02003968 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003969 }
3970}
3971
aliguori731b0362009-03-05 23:01:42 +00003972static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00003973{
aliguori376253e2009-03-05 23:01:23 +00003974 Monitor *mon = opaque;
3975
aliguori2724b182009-03-05 23:01:47 +00003976 switch (event) {
3977 case CHR_EVENT_MUX_IN:
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003978 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003979 mon->mux_out = 0;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003980 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003981 if (mon->reset_seen) {
3982 readline_restart(mon->rs);
3983 monitor_resume(mon);
3984 monitor_flush(mon);
3985 } else {
3986 mon->suspend_cnt = 0;
3987 }
aliguori2724b182009-03-05 23:01:47 +00003988 break;
ths86e94de2007-01-05 22:01:59 +00003989
aliguori2724b182009-03-05 23:01:47 +00003990 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003991 if (mon->reset_seen) {
3992 if (mon->suspend_cnt == 0) {
3993 monitor_printf(mon, "\n");
3994 }
3995 monitor_flush(mon);
3996 monitor_suspend(mon);
3997 } else {
3998 mon->suspend_cnt++;
3999 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02004000 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004001 mon->mux_out = 1;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02004002 qemu_mutex_unlock(&mon->out_lock);
aliguori2724b182009-03-05 23:01:47 +00004003 break;
4004
Amit Shahb6b8df52009-10-07 18:31:16 +05304005 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004006 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4007 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004008 if (!mon->mux_out) {
Stratos Psomadakise5554e22014-09-15 15:34:57 +03004009 readline_restart(mon->rs);
aliguori2724b182009-03-05 23:01:47 +00004010 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004011 }
4012 mon->reset_seen = 1;
Corey Bryantefb87c12012-08-14 16:43:48 -04004013 mon_refcount++;
4014 break;
4015
4016 case CHR_EVENT_CLOSED:
4017 mon_refcount--;
4018 monitor_fdsets_cleanup();
aliguori2724b182009-03-05 23:01:47 +00004019 break;
4020 }
ths86e94de2007-01-05 22:01:59 +00004021}
4022
Wayne Xia816f8922011-10-12 11:32:41 +08004023static int
4024compare_mon_cmd(const void *a, const void *b)
4025{
4026 return strcmp(((const mon_cmd_t *)a)->name,
4027 ((const mon_cmd_t *)b)->name);
4028}
4029
4030static void sortcmdlist(void)
4031{
4032 int array_num;
4033 int elem_size = sizeof(mon_cmd_t);
4034
4035 array_num = sizeof(mon_cmds)/elem_size-1;
4036 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
4037
4038 array_num = sizeof(info_cmds)/elem_size-1;
4039 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
4040}
4041
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004042/* These functions just adapt the readline interface in a typesafe way. We
4043 * could cast function pointers but that discards compiler checks.
4044 */
Stefan Weild5d15072014-01-25 18:18:23 +01004045static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
4046 const char *fmt, ...)
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004047{
4048 va_list ap;
4049 va_start(ap, fmt);
4050 monitor_vprintf(opaque, fmt, ap);
4051 va_end(ap);
4052}
4053
4054static void monitor_readline_flush(void *opaque)
4055{
4056 monitor_flush(opaque);
4057}
4058
Paolo Bonzini397d30e2016-10-24 18:31:02 +02004059/*
4060 * Print to current monitor if we have one, else to stderr.
4061 * TODO should return int, so callers can calculate width, but that
4062 * requires surgery to monitor_vprintf(). Left for another day.
4063 */
4064void error_vprintf(const char *fmt, va_list ap)
4065{
4066 if (cur_mon && !monitor_cur_is_qmp()) {
4067 monitor_vprintf(cur_mon, fmt, ap);
4068 } else {
4069 vfprintf(stderr, fmt, ap);
4070 }
4071}
4072
4073void error_vprintf_unless_qmp(const char *fmt, va_list ap)
4074{
4075 if (cur_mon && !monitor_cur_is_qmp()) {
4076 monitor_vprintf(cur_mon, fmt, ap);
Marc-André Lureau0d6b50d2017-01-05 14:59:57 +01004077 } else if (!cur_mon) {
4078 vfprintf(stderr, fmt, ap);
Paolo Bonzini397d30e2016-10-24 18:31:02 +02004079 }
4080}
4081
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004082static void __attribute__((constructor)) monitor_lock_init(void)
4083{
4084 qemu_mutex_init(&monitor_lock);
4085}
4086
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03004087void monitor_init(Chardev *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004088{
aliguori731b0362009-03-05 23:01:42 +00004089 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004090 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004091
4092 if (is_first_init) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +02004093 monitor_qapi_event_init();
Wenchao Xiad0383172013-08-27 20:38:18 +08004094 sortcmdlist();
ths20d8a3e2007-02-18 17:04:49 +00004095 is_first_init = 0;
4096 }
aliguori87127162009-03-05 23:01:29 +00004097
Wenchao Xiab01fe892013-08-27 20:38:19 +08004098 mon = g_malloc(sizeof(*mon));
4099 monitor_data_init(mon);
ths20d8a3e2007-02-18 17:04:49 +00004100
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03004101 qemu_chr_fe_init(&mon->chr, chr, &error_abort);
aliguori731b0362009-03-05 23:01:42 +00004102 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004103 if (flags & MONITOR_USE_READLINE) {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004104 mon->rs = readline_init(monitor_readline_printf,
4105 monitor_readline_flush,
4106 mon,
4107 monitor_find_completion);
aliguoricde76ee2009-03-05 23:01:51 +00004108 monitor_read_command(mon, 0);
4109 }
aliguori87127162009-03-05 23:01:29 +00004110
Markus Armbruster9f3982f2015-03-06 19:56:38 +01004111 if (monitor_is_qmp(mon)) {
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03004112 qemu_chr_fe_set_handlers(&mon->chr, monitor_can_read, monitor_qmp_read,
Marc-André Lureau39ab61c2016-10-22 12:53:03 +03004113 monitor_qmp_event, mon, NULL, true);
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03004114 qemu_chr_fe_set_echo(&mon->chr, true);
Markus Armbruster74358f22015-03-06 19:35:59 +01004115 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004116 } else {
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03004117 qemu_chr_fe_set_handlers(&mon->chr, monitor_can_read, monitor_read,
Marc-André Lureau39ab61c2016-10-22 12:53:03 +03004118 monitor_event, mon, NULL, true);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004119 }
aliguori87127162009-03-05 23:01:29 +00004120
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004121 qemu_mutex_lock(&monitor_lock);
Blue Swirl72cf2d42009-09-12 07:36:22 +00004122 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004123 qemu_mutex_unlock(&monitor_lock);
bellard7e2515e2004-08-01 21:52:19 +00004124}
4125
Marc-André Lureau2ef45712016-08-01 15:23:42 +04004126void monitor_cleanup(void)
4127{
4128 Monitor *mon, *next;
4129
4130 qemu_mutex_lock(&monitor_lock);
4131 QLIST_FOREACH_SAFE(mon, &mon_list, entry, next) {
4132 QLIST_REMOVE(mon, entry);
4133 monitor_data_destroy(mon);
4134 g_free(mon);
4135 }
4136 qemu_mutex_unlock(&monitor_lock);
4137}
4138
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004139static void bdrv_password_cb(void *opaque, const char *password,
4140 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00004141{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004142 Monitor *mon = opaque;
4143 BlockDriverState *bs = readline_opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004144 int ret = 0;
Markus Armbruster4d2855a2015-01-29 10:37:00 +01004145 Error *local_err = NULL;
bellard7e2515e2004-08-01 21:52:19 +00004146
Markus Armbruster4d2855a2015-01-29 10:37:00 +01004147 bdrv_add_key(bs, password, &local_err);
4148 if (local_err) {
Markus Armbruster193227f2015-12-18 16:35:06 +01004149 error_report_err(local_err);
aliguoribb5fc202009-03-05 23:01:15 +00004150 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004151 }
aliguori731b0362009-03-05 23:01:42 +00004152 if (mon->password_completion_cb)
4153 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004154
aliguori731b0362009-03-05 23:01:42 +00004155 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004156}
aliguoric0f4ce72009-03-05 23:01:01 +00004157
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004158int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
Markus Armbruster097310b2014-10-07 13:59:15 +02004159 BlockCompletionFunc *completion_cb,
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004160 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004161{
aliguoricde76ee2009-03-05 23:01:51 +00004162 int err;
4163
aliguori376253e2009-03-05 23:01:23 +00004164 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4165 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00004166
aliguori731b0362009-03-05 23:01:42 +00004167 mon->password_completion_cb = completion_cb;
4168 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004169
aliguoricde76ee2009-03-05 23:01:51 +00004170 err = monitor_read_password(mon, bdrv_password_cb, bs);
4171
4172 if (err && completion_cb)
4173 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004174
4175 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00004176}
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004177
4178int monitor_read_block_device_key(Monitor *mon, const char *device,
Markus Armbruster097310b2014-10-07 13:59:15 +02004179 BlockCompletionFunc *completion_cb,
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004180 void *opaque)
4181{
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01004182 Error *err = NULL;
Fam Zheng55606252015-03-02 19:36:46 +08004183 BlockBackend *blk;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004184
Fam Zheng55606252015-03-02 19:36:46 +08004185 blk = blk_by_name(device);
4186 if (!blk) {
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004187 monitor_printf(mon, "Device not found %s\n", device);
4188 return -1;
4189 }
Max Reitz5433c242015-10-19 17:53:29 +02004190 if (!blk_bs(blk)) {
4191 monitor_printf(mon, "Device '%s' has no medium\n", device);
4192 return -1;
4193 }
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004194
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01004195 bdrv_add_key(blk_bs(blk), NULL, &err);
4196 if (err) {
4197 error_free(err);
4198 return monitor_read_bdrv_key_start(mon, blk_bs(blk), completion_cb, opaque);
4199 }
4200
4201 if (completion_cb) {
4202 completion_cb(opaque, 0);
4203 }
4204 return 0;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004205}
Paolo Bonzini4d454572012-11-26 16:03:42 +01004206
4207QemuOptsList qemu_mon_opts = {
4208 .name = "mon",
4209 .implied_opt_name = "chardev",
4210 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
4211 .desc = {
4212 {
4213 .name = "mode",
4214 .type = QEMU_OPT_STRING,
4215 },{
4216 .name = "chardev",
4217 .type = QEMU_OPT_STRING,
4218 },{
Marc-André Lureaubdbcb542016-10-11 21:41:21 +04004219 .name = "default", /* deprecated */
Paolo Bonzini4d454572012-11-26 16:03:42 +01004220 .type = QEMU_OPT_BOOL,
4221 },{
4222 .name = "pretty",
4223 .type = QEMU_OPT_BOOL,
4224 },
4225 { /* end of list */ }
4226 },
4227};
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004228
4229#ifndef TARGET_I386
4230void qmp_rtc_reset_reinjection(Error **errp)
4231{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004232 error_setg(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection");
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004233}
4234#endif
Jason J. Herne7ee0c3e2015-06-26 14:03:16 -04004235
4236#ifndef TARGET_S390X
4237void qmp_dump_skeys(const char *filename, Error **errp)
4238{
4239 error_setg(errp, QERR_FEATURE_DISABLED, "dump-skeys");
4240}
4241#endif
Peter Xuae50a772016-03-30 17:27:24 +01004242
4243#ifndef TARGET_ARM
4244GICCapabilityList *qmp_query_gic_capabilities(Error **errp)
4245{
4246 error_setg(errp, QERR_FEATURE_DISABLED, "query-gic-capabilities");
4247 return NULL;
4248}
4249#endif
Igor Mammedovd4633542016-06-10 06:29:06 +05304250
4251HotpluggableCPUList *qmp_query_hotpluggable_cpus(Error **errp)
4252{
4253 MachineState *ms = MACHINE(qdev_get_machine());
4254 MachineClass *mc = MACHINE_GET_CLASS(ms);
4255
Igor Mammedovc5514d02017-02-10 11:20:57 +01004256 if (!mc->has_hotpluggable_cpus) {
Igor Mammedovd4633542016-06-10 06:29:06 +05304257 error_setg(errp, QERR_FEATURE_DISABLED, "query-hotpluggable-cpus");
4258 return NULL;
4259 }
4260
Igor Mammedovc5514d02017-02-10 11:20:57 +01004261 return machine_query_hotpluggable_cpus(ms);
Igor Mammedovd4633542016-06-10 06:29:06 +05304262}