blob: d5d6e95e9db0cc3dddc7c888bf025c5acdd1e571 [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"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020038#include "sysemu/char.h"
Gerd Hoffmann75721502010-10-07 12:22:54 +020039#include "ui/qemu-spice.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010040#include "sysemu/sysemu.h"
Eduardo Habkoste35704b2015-02-08 16:51:16 -020041#include "sysemu/numa.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010042#include "monitor/monitor.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"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010052#include "migration/migration.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010053#include "sysemu/kvm.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010054#include "qemu/acl.h"
Paolo Bonzinibdee56f2013-04-02 18:28:41 +020055#include "sysemu/tpm.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010056#include "qapi/qmp/qerror.h"
Eric Blakec7eb39c2016-06-09 10:48:32 -060057#include "qapi/qmp/types.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010058#include "qapi/qmp/qjson.h"
59#include "qapi/qmp/json-streamer.h"
60#include "qapi/qmp/json-parser.h"
Markus Armbrustera9c94272016-06-22 19:11:19 +020061#include "qom/object_interfaces.h"
Blue Swirl2b41f102011-06-19 20:38:22 +000062#include "cpu.h"
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +010063#include "trace.h"
Lluís31965ae2011-08-31 20:31:24 +020064#include "trace/control.h"
Pavel Butsykinbf957282015-09-10 18:38:59 +030065#include "monitor/hmp-target.h"
Lluís6d8a7642011-08-31 20:30:43 +020066#ifdef CONFIG_TRACE_SIMPLE
Lluís31965ae2011-08-31 20:31:24 +020067#include "trace/simple.h"
Prerna Saxena22890ab2010-06-24 17:04:53 +053068#endif
Paolo Bonzini022c62c2012-12-17 18:19:49 +010069#include "exec/memory.h"
Paolo Bonzini63c91552016-03-15 13:18:37 +010070#include "exec/exec-all.h"
Paolo Bonzini03dd0242015-12-15 13:16:16 +010071#include "qemu/log.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050072#include "qmp-commands.h"
73#include "hmp.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010074#include "qemu/thread.h"
Hani Benhabilesb21631f2014-05-27 23:39:37 +010075#include "block/qapi.h"
Wenchao Xia43a14cf2014-06-18 08:43:31 +020076#include "qapi/qmp-event.h"
77#include "qapi-event.h"
Markus Armbruster39a18152015-09-16 13:06:28 +020078#include "qmp-introspect.h"
Fam Zheng55606252015-03-02 19:36:46 +080079#include "sysemu/block-backend.h"
Alberto Garciadc599972016-03-10 13:55:26 +020080#include "sysemu/qtest.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020081#include "qemu/cutils.h"
Marc-André Lureauedcfaef2016-09-12 13:19:00 +040082#include "qapi/qmp/dispatch.h"
ths6a5bd302007-12-03 17:05:38 +000083
Markus Armbruster1ce6be22015-02-06 14:18:24 +010084/* for hmp_info_irq/pic */
Jan Kiszka661f1922011-10-16 11:53:13 +020085#if defined(TARGET_SPARC)
Paolo Bonzini0d09e412013-02-05 17:06:20 +010086#include "hw/sparc/sun4m.h"
Jan Kiszka661f1922011-10-16 11:53:13 +020087#endif
Paolo Bonzini0d09e412013-02-05 17:06:20 +010088#include "hw/lm32/lm32_pic.h"
Jan Kiszka661f1922011-10-16 11:53:13 +020089
Jason J. Hernea4538a52015-06-26 14:07:21 -040090#if defined(TARGET_S390X)
91#include "hw/s390x/storage-keys.h"
92#endif
93
bellard9307c4c2004-04-04 12:57:25 +000094/*
95 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000096 *
bellard9307c4c2004-04-04 12:57:25 +000097 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000098 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000099 * 's' string (accept optional quote)
Wenchao Xia129be002013-08-27 20:38:26 +0800100 * 'S' it just appends the rest of the string (accept optional quote)
Markus Armbruster361127d2010-02-10 20:24:35 +0100101 * 'O' option string of the form NAME=VALUE,...
102 * parsed according to QemuOptsList given by its name
103 * Example: 'device:O' uses qemu_device_opts.
104 * Restriction: only lists with empty desc are supported
105 * TODO lift the restriction
bellard92a31b12005-02-10 22:00:52 +0000106 * 'i' 32 bit integer
107 * 'l' target long (32 or 64 bit)
Luiz Capitulino91162842012-04-26 17:34:30 -0300108 * 'M' Non-negative target long (32 or 64 bit), in user mode the
109 * value is multiplied by 2^20 (think Mebibyte)
Jes Sorensendbc0c672010-10-21 17:15:47 +0200110 * 'o' octets (aka bytes)
Kevin Wolf5e009842013-06-05 14:19:27 +0200111 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
112 * K, k suffix, which multiplies the value by 2^60 for suffixes E
113 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
114 * 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 +0100115 * 'T' double
116 * user mode accepts an optional ms, us, ns suffix,
117 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +0000118 * '/' optional gdb-like print format (like "/10x")
119 *
Luiz Capitulinofb466602009-08-28 15:27:27 -0300120 * '?' optional type (for all types, except '/')
121 * '.' other form of optional type (for 'i' and 'l')
Markus Armbruster942cd1f2010-03-26 09:07:09 +0100122 * 'b' boolean
123 * user mode accepts "on" or "off"
Luiz Capitulinofb466602009-08-28 15:27:27 -0300124 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +0000125 *
126 */
127
Anthony Liguoric227f092009-10-01 16:12:16 -0500128typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +0000129 const char *name;
bellard9307c4c2004-04-04 12:57:25 +0000130 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +0000131 const char *params;
132 const char *help;
Marc-André Lureau2b9e3572016-09-12 13:19:06 +0400133 void (*cmd)(Monitor *mon, const QDict *qdict);
134 /* @sub_table is a list of 2nd level of commands. If it does not exist,
135 * cmd should be used. If it exists, sub_table[?].cmd should be
136 * used, and cmd of 1st level plays the role of help function.
Wenchao Xia5f3d3352013-01-14 14:06:27 +0800137 */
138 struct mon_cmd_t *sub_table;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +0100139 void (*command_completion)(ReadLineState *rs, int nb_args, const char *str);
Anthony Liguoric227f092009-10-01 16:12:16 -0500140} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000141
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100142/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500143typedef struct mon_fd_t mon_fd_t;
144struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100145 char *name;
146 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500147 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100148};
149
Corey Bryantba1c0482012-08-14 16:43:43 -0400150/* file descriptor associated with a file descriptor set */
151typedef struct MonFdsetFd MonFdsetFd;
152struct MonFdsetFd {
153 int fd;
154 bool removed;
155 char *opaque;
156 QLIST_ENTRY(MonFdsetFd) next;
157};
158
159/* file descriptor set containing fds passed via SCM_RIGHTS */
160typedef struct MonFdset MonFdset;
161struct MonFdset {
162 int64_t id;
163 QLIST_HEAD(, MonFdsetFd) fds;
Corey Bryantadb696f2012-08-14 16:43:47 -0400164 QLIST_HEAD(, MonFdsetFd) dup_fds;
Corey Bryantba1c0482012-08-14 16:43:43 -0400165 QLIST_ENTRY(MonFdset) next;
166};
167
Markus Armbruster74358f22015-03-06 19:35:59 +0100168typedef struct {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200169 QObject *id;
170 JSONMessageParser parser;
Markus Armbrusterf994b252015-03-06 19:51:51 +0100171 /*
172 * When a client connects, we're in capabilities negotiation mode.
173 * When command qmp_capabilities succeeds, we go into command
174 * mode.
175 */
176 bool in_command_mode; /* are we in command mode? */
Markus Armbruster74358f22015-03-06 19:35:59 +0100177} MonitorQMP;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200178
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100179/*
180 * To prevent flooding clients, events can be throttled. The
181 * throttling is calculated globally, rather than per-Monitor
182 * instance.
183 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200184typedef struct MonitorQAPIEventState {
Markus Armbruster7de0be62015-10-15 17:08:35 +0200185 QAPIEvent event; /* Throttling state for this event type and... */
186 QDict *data; /* ... data, see qapi_event_throttle_equal() */
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100187 QEMUTimer *timer; /* Timer for handling delayed events */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200188 QDict *qdict; /* Delayed event (if any) */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200189} MonitorQAPIEventState;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100190
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200191typedef struct {
192 int64_t rate; /* Minimum time (in ns) between two events */
193} MonitorQAPIEventConf;
194
aliguori87127162009-03-05 23:01:29 +0000195struct Monitor {
196 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200197 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000198 int flags;
199 int suspend_cnt;
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400200 bool skip_flush;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200201
202 QemuMutex out_lock;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400203 QString *outbuf;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200204 guint out_watch;
205
206 /* Read under either BQL or out_lock, written with BQL+out_lock. */
207 int mux_out;
208
aliguori731b0362009-03-05 23:01:42 +0000209 ReadLineState *rs;
Markus Armbruster74358f22015-03-06 19:35:59 +0100210 MonitorQMP qmp;
Andreas Färbercb446ec2013-05-01 14:24:52 +0200211 CPUState *mon_cpu;
Markus Armbruster097310b2014-10-07 13:59:15 +0200212 BlockCompletionFunc *password_completion_cb;
aliguori731b0362009-03-05 23:01:42 +0000213 void *password_opaque;
Wenchao Xia77172392013-08-27 20:38:20 +0800214 mon_cmd_t *cmd_table;
Anthony Liguoric227f092009-10-01 16:12:16 -0500215 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000216 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000217};
218
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300219/* QMP checker flags */
220#define QMP_ACCEPT_UNKNOWNS 1
221
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200222/* Protects mon_list, monitor_event_state. */
223static QemuMutex monitor_lock;
224
Blue Swirl72cf2d42009-09-12 07:36:22 +0000225static QLIST_HEAD(mon_list, Monitor) mon_list;
Corey Bryantba1c0482012-08-14 16:43:43 -0400226static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
Corey Bryantefb87c12012-08-14 16:43:48 -0400227static int mon_refcount;
bellard7e2515e2004-08-01 21:52:19 +0000228
Wayne Xia816f8922011-10-12 11:32:41 +0800229static mon_cmd_t mon_cmds[];
230static mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000231
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300232static const mon_cmd_t qmp_cmds[];
233
Markus Armbruster8631b602010-02-18 11:41:55 +0100234Monitor *cur_mon;
aliguori376253e2009-03-05 23:01:23 +0000235
Alberto Garciadc599972016-03-10 13:55:26 +0200236static QEMUClockType event_clock_type = QEMU_CLOCK_REALTIME;
237
Stefan Hajnoczic60bf332013-11-14 11:54:14 +0100238static void monitor_command_cb(void *opaque, const char *cmdline,
239 void *readline_opaque);
aliguori83ab7952008-08-19 14:44:22 +0000240
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100241/**
242 * Is @mon a QMP monitor?
243 */
244static inline bool monitor_is_qmp(const Monitor *mon)
Luiz Capitulino418173c2009-11-26 22:58:51 -0200245{
246 return (mon->flags & MONITOR_USE_CONTROL);
247}
248
Markus Armbruster489653b2015-03-06 20:01:05 +0100249/**
250 * Is the current monitor, if any, a QMP monitor?
251 */
252bool monitor_cur_is_qmp(void)
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100253{
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100254 return cur_mon && monitor_is_qmp(cur_mon);
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100255}
256
Anthony Liguori7060b472011-09-02 12:34:50 -0500257void monitor_read_command(Monitor *mon, int show_prompt)
aliguori731b0362009-03-05 23:01:42 +0000258{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200259 if (!mon->rs)
260 return;
261
aliguori731b0362009-03-05 23:01:42 +0000262 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
263 if (show_prompt)
264 readline_show_prompt(mon->rs);
265}
bellard6a00d602005-11-21 23:25:50 +0000266
Anthony Liguori7060b472011-09-02 12:34:50 -0500267int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
268 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000269{
Markus Armbrusterbcf5d192015-03-12 17:26:46 +0100270 if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000271 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
272 /* prompt is printed on return from the command handler */
273 return 0;
274 } else {
275 monitor_printf(mon, "terminal does not support password prompting\n");
276 return -ENOTTY;
277 }
aliguoribb5fc202009-03-05 23:01:15 +0000278}
279
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200280static void monitor_flush_locked(Monitor *mon);
281
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100282static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
283 void *opaque)
284{
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200285 Monitor *mon = opaque;
286
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200287 qemu_mutex_lock(&mon->out_lock);
288 mon->out_watch = 0;
289 monitor_flush_locked(mon);
290 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100291 return FALSE;
292}
293
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200294/* Called with mon->out_lock held. */
295static void monitor_flush_locked(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000296{
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100297 int rc;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400298 size_t len;
299 const char *buf;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100300
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400301 if (mon->skip_flush) {
302 return;
303 }
304
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400305 buf = qstring_get_str(mon->outbuf);
306 len = qstring_get_length(mon->outbuf);
307
Paolo Bonzinia4cc73d2013-05-31 14:00:27 +0200308 if (len && !mon->mux_out) {
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400309 rc = qemu_chr_fe_write(mon->chr, (const uint8_t *) buf, len);
Stratos Psomadakis056f49f2014-01-27 12:30:15 +0200310 if ((rc < 0 && errno != EAGAIN) || (rc == len)) {
311 /* all flushed or error */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400312 QDECREF(mon->outbuf);
313 mon->outbuf = qstring_new();
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100314 return;
315 }
316 if (rc > 0) {
Eric Blake3b7c78c2016-05-17 16:00:15 -0600317 /* partial write */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400318 QString *tmp = qstring_from_str(buf + rc);
319 QDECREF(mon->outbuf);
320 mon->outbuf = tmp;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100321 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200322 if (mon->out_watch == 0) {
Roger Pau Monnee02bc6d2014-05-23 17:57:49 +0200323 mon->out_watch = qemu_chr_fe_add_watch(mon->chr, G_IO_OUT|G_IO_HUP,
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200324 monitor_unblocked, mon);
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200325 }
bellard7e2515e2004-08-01 21:52:19 +0000326 }
327}
328
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200329void monitor_flush(Monitor *mon)
330{
331 qemu_mutex_lock(&mon->out_lock);
332 monitor_flush_locked(mon);
333 qemu_mutex_unlock(&mon->out_lock);
334}
335
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400336/* flush at every end of line */
aliguori376253e2009-03-05 23:01:23 +0000337static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000338{
ths60fe76f2007-12-16 03:02:09 +0000339 char c;
aliguori731b0362009-03-05 23:01:42 +0000340
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200341 qemu_mutex_lock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000342 for(;;) {
343 c = *str++;
344 if (c == '\0')
345 break;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400346 if (c == '\n') {
347 qstring_append_chr(mon->outbuf, '\r');
348 }
349 qstring_append_chr(mon->outbuf, c);
350 if (c == '\n') {
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200351 monitor_flush_locked(mon);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400352 }
bellard7e2515e2004-08-01 21:52:19 +0000353 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200354 qemu_mutex_unlock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000355}
356
aliguori376253e2009-03-05 23:01:23 +0000357void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000358{
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400359 char *buf;
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200360
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200361 if (!mon)
362 return;
363
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100364 if (monitor_is_qmp(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200365 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200366 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200367
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400368 buf = g_strdup_vprintf(fmt, ap);
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200369 monitor_puts(mon, buf);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400370 g_free(buf);
bellard7e2515e2004-08-01 21:52:19 +0000371}
372
aliguori376253e2009-03-05 23:01:23 +0000373void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000374{
375 va_list ap;
376 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000377 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000378 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000379}
380
Pavel Butsykincaf15312015-09-22 16:18:17 +0300381int monitor_fprintf(FILE *stream, const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000382{
383 va_list ap;
384 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000385 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000386 va_end(ap);
387 return 0;
388}
389
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200390static void monitor_json_emitter(Monitor *mon, const QObject *data)
391{
392 QString *json;
393
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200394 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
395 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200396 assert(json != NULL);
397
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200398 qstring_append_chr(json, '\n');
399 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200400
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200401 QDECREF(json);
402}
403
Markus Armbruster710aec92015-03-06 11:28:00 +0100404static QDict *build_qmp_error_dict(Error *err)
Luiz Capitulinode253f12012-07-27 16:18:16 -0300405{
406 QObject *obj;
407
Markus Armbruster710aec92015-03-06 11:28:00 +0100408 obj = qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %s } }",
Eric Blakef22a28b2015-11-18 01:53:00 -0700409 QapiErrorClass_lookup[error_get_class(err)],
Markus Armbruster710aec92015-03-06 11:28:00 +0100410 error_get_pretty(err));
Luiz Capitulinode253f12012-07-27 16:18:16 -0300411
412 return qobject_to_qdict(obj);
413}
414
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100415static void monitor_protocol_emitter(Monitor *mon, QObject *data,
Markus Armbruster710aec92015-03-06 11:28:00 +0100416 Error *err)
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200417{
418 QDict *qmp;
419
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +0100420 trace_monitor_protocol_emitter(mon);
421
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100422 if (!err) {
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200423 /* success response */
Luiz Capitulinode253f12012-07-27 16:18:16 -0300424 qmp = qdict_new();
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200425 if (data) {
426 qobject_incref(data);
427 qdict_put_obj(qmp, "return", data);
428 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200429 /* return an empty QDict by default */
430 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200431 }
432 } else {
433 /* error response */
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100434 qmp = build_qmp_error_dict(err);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200435 }
436
Markus Armbruster74358f22015-03-06 19:35:59 +0100437 if (mon->qmp.id) {
438 qdict_put_obj(qmp, "id", mon->qmp.id);
439 mon->qmp.id = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200440 }
441
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200442 monitor_json_emitter(mon, QOBJECT(qmp));
443 QDECREF(qmp);
444}
445
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200446
Eric Blake7fb1cf12015-11-18 01:52:57 -0700447static MonitorQAPIEventConf monitor_qapi_event_conf[QAPI_EVENT__MAX] = {
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200448 /* Limit guest-triggerable events to 1 per second */
449 [QAPI_EVENT_RTC_CHANGE] = { 1000 * SCALE_MS },
450 [QAPI_EVENT_WATCHDOG] = { 1000 * SCALE_MS },
451 [QAPI_EVENT_BALLOON_CHANGE] = { 1000 * SCALE_MS },
452 [QAPI_EVENT_QUORUM_REPORT_BAD] = { 1000 * SCALE_MS },
453 [QAPI_EVENT_QUORUM_FAILURE] = { 1000 * SCALE_MS },
454 [QAPI_EVENT_VSERPORT_CHANGE] = { 1000 * SCALE_MS },
455};
456
Markus Armbrustera24712a2015-10-15 17:08:34 +0200457GHashTable *monitor_qapi_event_state;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100458
459/*
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200460 * Emits the event to every monitor instance, @event is only used for trace
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200461 * Called with monitor_lock held.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100462 */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200463static void monitor_qapi_event_emit(QAPIEvent event, QDict *qdict)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100464{
465 Monitor *mon;
466
Markus Armbruster688b4b72015-10-15 17:08:30 +0200467 trace_monitor_protocol_event_emit(event, qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100468 QLIST_FOREACH(mon, &mon_list, entry) {
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100469 if (monitor_is_qmp(mon) && mon->qmp.in_command_mode) {
Markus Armbruster688b4b72015-10-15 17:08:30 +0200470 monitor_json_emitter(mon, QOBJECT(qdict));
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100471 }
472 }
473}
474
Markus Armbrustera24712a2015-10-15 17:08:34 +0200475static void monitor_qapi_event_handler(void *opaque);
476
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100477/*
478 * Queue a new event for emission to Monitor instances,
479 * applying any rate limiting if required.
480 */
481static void
Markus Armbruster688b4b72015-10-15 17:08:30 +0200482monitor_qapi_event_queue(QAPIEvent event, QDict *qdict, Error **errp)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100483{
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200484 MonitorQAPIEventConf *evconf;
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200485 MonitorQAPIEventState *evstate;
Markus Armbruster93f8f982015-10-15 17:08:31 +0200486
Eric Blake7fb1cf12015-11-18 01:52:57 -0700487 assert(event < QAPI_EVENT__MAX);
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200488 evconf = &monitor_qapi_event_conf[event];
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200489 trace_monitor_protocol_event_queue(event, qdict, evconf->rate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100490
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200491 qemu_mutex_lock(&monitor_lock);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200492
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200493 if (!evconf->rate) {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200494 /* Unthrottled event */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200495 monitor_qapi_event_emit(event, qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100496 } else {
Markus Armbruster7de0be62015-10-15 17:08:35 +0200497 QDict *data = qobject_to_qdict(qdict_get(qdict, "data"));
498 MonitorQAPIEventState key = { .event = event, .data = data };
Markus Armbrustera24712a2015-10-15 17:08:34 +0200499
500 evstate = g_hash_table_lookup(monitor_qapi_event_state, &key);
501 assert(!evstate || timer_pending(evstate->timer));
502
503 if (evstate) {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200504 /*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200505 * Timer is pending for (at least) evconf->rate ns after
Markus Armbruster93f8f982015-10-15 17:08:31 +0200506 * last send. Store event for sending when timer fires,
507 * replacing a prior stored event if any.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100508 */
Markus Armbruster93f8f982015-10-15 17:08:31 +0200509 QDECREF(evstate->qdict);
Markus Armbruster688b4b72015-10-15 17:08:30 +0200510 evstate->qdict = qdict;
511 QINCREF(evstate->qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100512 } else {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200513 /*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200514 * Last send was (at least) evconf->rate ns ago.
Markus Armbruster93f8f982015-10-15 17:08:31 +0200515 * Send immediately, and arm the timer to call
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200516 * monitor_qapi_event_handler() in evconf->rate ns. Any
Markus Armbruster93f8f982015-10-15 17:08:31 +0200517 * events arriving before then will be delayed until then.
518 */
Alberto Garciadc599972016-03-10 13:55:26 +0200519 int64_t now = qemu_clock_get_ns(event_clock_type);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200520
Markus Armbruster688b4b72015-10-15 17:08:30 +0200521 monitor_qapi_event_emit(event, qdict);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200522
523 evstate = g_new(MonitorQAPIEventState, 1);
524 evstate->event = event;
Markus Armbruster7de0be62015-10-15 17:08:35 +0200525 evstate->data = data;
526 QINCREF(evstate->data);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200527 evstate->qdict = NULL;
Alberto Garciadc599972016-03-10 13:55:26 +0200528 evstate->timer = timer_new_ns(event_clock_type,
Markus Armbrustera24712a2015-10-15 17:08:34 +0200529 monitor_qapi_event_handler,
530 evstate);
531 g_hash_table_add(monitor_qapi_event_state, evstate);
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200532 timer_mod_ns(evstate->timer, now + evconf->rate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100533 }
534 }
Markus Armbruster93f8f982015-10-15 17:08:31 +0200535
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200536 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100537}
538
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100539/*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200540 * This function runs evconf->rate ns after sending a throttled
Markus Armbruster93f8f982015-10-15 17:08:31 +0200541 * event.
542 * If another event has since been stored, send it.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100543 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200544static void monitor_qapi_event_handler(void *opaque)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100545{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200546 MonitorQAPIEventState *evstate = opaque;
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200547 MonitorQAPIEventConf *evconf = &monitor_qapi_event_conf[evstate->event];
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100548
Markus Armbruster93f8f982015-10-15 17:08:31 +0200549 trace_monitor_protocol_event_handler(evstate->event, evstate->qdict);
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200550 qemu_mutex_lock(&monitor_lock);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200551
Markus Armbruster688b4b72015-10-15 17:08:30 +0200552 if (evstate->qdict) {
Alberto Garciadc599972016-03-10 13:55:26 +0200553 int64_t now = qemu_clock_get_ns(event_clock_type);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200554
Markus Armbruster688b4b72015-10-15 17:08:30 +0200555 monitor_qapi_event_emit(evstate->event, evstate->qdict);
556 QDECREF(evstate->qdict);
557 evstate->qdict = NULL;
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200558 timer_mod_ns(evstate->timer, now + evconf->rate);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200559 } else {
560 g_hash_table_remove(monitor_qapi_event_state, evstate);
Markus Armbruster7de0be62015-10-15 17:08:35 +0200561 QDECREF(evstate->data);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200562 timer_free(evstate->timer);
563 g_free(evstate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100564 }
Markus Armbruster93f8f982015-10-15 17:08:31 +0200565
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200566 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100567}
568
Markus Armbrustera24712a2015-10-15 17:08:34 +0200569static unsigned int qapi_event_throttle_hash(const void *key)
570{
571 const MonitorQAPIEventState *evstate = key;
Markus Armbruster7de0be62015-10-15 17:08:35 +0200572 unsigned int hash = evstate->event * 255;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200573
Markus Armbruster7de0be62015-10-15 17:08:35 +0200574 if (evstate->event == QAPI_EVENT_VSERPORT_CHANGE) {
575 hash += g_str_hash(qdict_get_str(evstate->data, "id"));
576 }
577
Alberto Garcia6d425eb2016-03-10 13:55:25 +0200578 if (evstate->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
579 hash += g_str_hash(qdict_get_str(evstate->data, "node-name"));
580 }
581
Markus Armbruster7de0be62015-10-15 17:08:35 +0200582 return hash;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200583}
584
585static gboolean qapi_event_throttle_equal(const void *a, const void *b)
586{
587 const MonitorQAPIEventState *eva = a;
588 const MonitorQAPIEventState *evb = b;
589
Markus Armbruster7de0be62015-10-15 17:08:35 +0200590 if (eva->event != evb->event) {
591 return FALSE;
592 }
593
594 if (eva->event == QAPI_EVENT_VSERPORT_CHANGE) {
595 return !strcmp(qdict_get_str(eva->data, "id"),
596 qdict_get_str(evb->data, "id"));
597 }
598
Alberto Garcia6d425eb2016-03-10 13:55:25 +0200599 if (eva->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
600 return !strcmp(qdict_get_str(eva->data, "node-name"),
601 qdict_get_str(evb->data, "node-name"));
602 }
603
Markus Armbruster7de0be62015-10-15 17:08:35 +0200604 return TRUE;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200605}
606
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200607static void monitor_qapi_event_init(void)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100608{
Alberto Garciadc599972016-03-10 13:55:26 +0200609 if (qtest_enabled()) {
610 event_clock_type = QEMU_CLOCK_VIRTUAL;
611 }
612
Markus Armbrustera24712a2015-10-15 17:08:34 +0200613 monitor_qapi_event_state = g_hash_table_new(qapi_event_throttle_hash,
614 qapi_event_throttle_equal);
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200615 qmp_event_set_func_emit(monitor_qapi_event_queue);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200616}
617
Marc-André Lureau119ebac2016-09-12 13:18:57 +0400618void qmp_qmp_capabilities(Error **errp)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200619{
Markus Armbruster485febc2015-03-13 17:25:50 +0100620 cur_mon->qmp.in_command_mode = true;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200621}
622
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100623static void handle_hmp_command(Monitor *mon, const char *cmdline);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200624
Wenchao Xiab01fe892013-08-27 20:38:19 +0800625static void monitor_data_init(Monitor *mon)
626{
627 memset(mon, 0, sizeof(Monitor));
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200628 qemu_mutex_init(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800629 mon->outbuf = qstring_new();
Wenchao Xia77172392013-08-27 20:38:20 +0800630 /* Use *mon_cmds by default. */
631 mon->cmd_table = mon_cmds;
Wenchao Xiab01fe892013-08-27 20:38:19 +0800632}
633
634static void monitor_data_destroy(Monitor *mon)
635{
Marc-André Lureau2ef45712016-08-01 15:23:42 +0400636 if (mon->chr) {
637 qemu_chr_add_handlers(mon->chr, NULL, NULL, NULL, NULL);
638 }
639 if (monitor_is_qmp(mon)) {
640 json_message_parser_destroy(&mon->qmp.parser);
641 }
642 g_free(mon->rs);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800643 QDECREF(mon->outbuf);
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200644 qemu_mutex_destroy(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800645}
646
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200647char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
648 int64_t cpu_index, Error **errp)
Luiz Capitulino0268d972010-10-22 10:08:02 -0200649{
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200650 char *output = NULL;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200651 Monitor *old_mon, hmp;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200652
Wenchao Xiab01fe892013-08-27 20:38:19 +0800653 monitor_data_init(&hmp);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400654 hmp.skip_flush = true;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200655
656 old_mon = cur_mon;
657 cur_mon = &hmp;
658
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200659 if (has_cpu_index) {
660 int ret = monitor_set_cpu(cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200661 if (ret < 0) {
662 cur_mon = old_mon;
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100663 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
664 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200665 goto out;
666 }
667 }
668
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100669 handle_hmp_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200670 cur_mon = old_mon;
671
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200672 qemu_mutex_lock(&hmp.out_lock);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400673 if (qstring_get_length(hmp.outbuf) > 0) {
674 output = g_strdup(qstring_get_str(hmp.outbuf));
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200675 } else {
676 output = g_strdup("");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200677 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200678 qemu_mutex_unlock(&hmp.out_lock);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200679
680out:
Wenchao Xiab01fe892013-08-27 20:38:19 +0800681 monitor_data_destroy(&hmp);
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200682 return output;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200683}
684
bellard9dc39cb2004-03-14 21:38:27 +0000685static int compare_cmd(const char *name, const char *list)
686{
687 const char *p, *pstart;
688 int len;
689 len = strlen(name);
690 p = list;
691 for(;;) {
692 pstart = p;
693 p = strchr(p, '|');
694 if (!p)
695 p = pstart + strlen(pstart);
696 if ((p - pstart) == len && !memcmp(pstart, name, len))
697 return 1;
698 if (*p == '\0')
699 break;
700 p++;
701 }
702 return 0;
703}
704
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800705static int get_str(char *buf, int buf_size, const char **pp)
706{
707 const char *p;
708 char *q;
709 int c;
710
711 q = buf;
712 p = *pp;
713 while (qemu_isspace(*p)) {
714 p++;
715 }
716 if (*p == '\0') {
717 fail:
718 *q = '\0';
719 *pp = p;
720 return -1;
721 }
722 if (*p == '\"') {
723 p++;
724 while (*p != '\0' && *p != '\"') {
725 if (*p == '\\') {
726 p++;
727 c = *p++;
728 switch (c) {
729 case 'n':
730 c = '\n';
731 break;
732 case 'r':
733 c = '\r';
734 break;
735 case '\\':
736 case '\'':
737 case '\"':
738 break;
739 default:
Peter Maydell71baf782015-08-19 16:20:19 +0100740 printf("unsupported escape code: '\\%c'\n", c);
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800741 goto fail;
742 }
743 if ((q - buf) < buf_size - 1) {
744 *q++ = c;
745 }
746 } else {
747 if ((q - buf) < buf_size - 1) {
748 *q++ = *p;
749 }
750 p++;
751 }
752 }
753 if (*p != '\"') {
Peter Maydell71baf782015-08-19 16:20:19 +0100754 printf("unterminated string\n");
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800755 goto fail;
756 }
757 p++;
758 } else {
759 while (*p != '\0' && !qemu_isspace(*p)) {
760 if ((q - buf) < buf_size - 1) {
761 *q++ = *p;
762 }
763 p++;
764 }
765 }
766 *q = '\0';
767 *pp = p;
768 return 0;
769}
770
771#define MAX_ARGS 16
772
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800773static void free_cmdline_args(char **args, int nb_args)
774{
775 int i;
776
777 assert(nb_args <= MAX_ARGS);
778
779 for (i = 0; i < nb_args; i++) {
780 g_free(args[i]);
781 }
782
783}
784
785/*
786 * Parse the command line to get valid args.
787 * @cmdline: command line to be parsed.
788 * @pnb_args: location to store the number of args, must NOT be NULL.
789 * @args: location to store the args, which should be freed by caller, must
790 * NOT be NULL.
791 *
792 * Returns 0 on success, negative on failure.
793 *
794 * NOTE: this parser is an approximate form of the real command parser. Number
795 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
796 * return with failure.
797 */
798static int parse_cmdline(const char *cmdline,
799 int *pnb_args, char **args)
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800800{
801 const char *p;
802 int nb_args, ret;
803 char buf[1024];
804
805 p = cmdline;
806 nb_args = 0;
807 for (;;) {
808 while (qemu_isspace(*p)) {
809 p++;
810 }
811 if (*p == '\0') {
812 break;
813 }
814 if (nb_args >= MAX_ARGS) {
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800815 goto fail;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800816 }
817 ret = get_str(buf, sizeof(buf), &p);
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800818 if (ret < 0) {
819 goto fail;
820 }
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800821 args[nb_args] = g_strdup(buf);
822 nb_args++;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800823 }
824 *pnb_args = nb_args;
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800825 return 0;
826
827 fail:
828 free_cmdline_args(args, nb_args);
829 return -1;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800830}
831
Wenchao Xia66855492013-08-27 20:38:23 +0800832static void help_cmd_dump_one(Monitor *mon,
833 const mon_cmd_t *cmd,
834 char **prefix_args,
835 int prefix_args_nb)
836{
837 int i;
838
839 for (i = 0; i < prefix_args_nb; i++) {
840 monitor_printf(mon, "%s ", prefix_args[i]);
841 }
842 monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
843}
844
845/* @args[@arg_index] is the valid command need to find in @cmds */
Anthony Liguoric227f092009-10-01 16:12:16 -0500846static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
Wenchao Xia66855492013-08-27 20:38:23 +0800847 char **args, int nb_args, int arg_index)
bellard9dc39cb2004-03-14 21:38:27 +0000848{
Anthony Liguoric227f092009-10-01 16:12:16 -0500849 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000850
Wenchao Xia66855492013-08-27 20:38:23 +0800851 /* No valid arg need to compare with, dump all in *cmds */
852 if (arg_index >= nb_args) {
853 for (cmd = cmds; cmd->name != NULL; cmd++) {
854 help_cmd_dump_one(mon, cmd, args, arg_index);
855 }
856 return;
857 }
858
859 /* Find one entry to dump */
860 for (cmd = cmds; cmd->name != NULL; cmd++) {
861 if (compare_cmd(args[arg_index], cmd->name)) {
862 if (cmd->sub_table) {
863 /* continue with next arg */
864 help_cmd_dump(mon, cmd->sub_table,
865 args, nb_args, arg_index + 1);
866 } else {
867 help_cmd_dump_one(mon, cmd, args, arg_index);
868 }
869 break;
870 }
bellard9dc39cb2004-03-14 21:38:27 +0000871 }
872}
873
aliguori376253e2009-03-05 23:01:23 +0000874static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000875{
Wenchao Xia66855492013-08-27 20:38:23 +0800876 char *args[MAX_ARGS];
877 int nb_args = 0;
878
879 /* 1. parse user input */
880 if (name) {
881 /* special case for log, directly dump and return */
882 if (!strcmp(name, "log")) {
Peter Maydell38dad9e2013-02-11 16:41:25 +0000883 const QEMULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000884 monitor_printf(mon, "Log items (comma separated):\n");
885 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
Peter Maydell38dad9e2013-02-11 16:41:25 +0000886 for (item = qemu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000887 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000888 }
Wenchao Xia66855492013-08-27 20:38:23 +0800889 return;
890 }
891
892 if (parse_cmdline(name, &nb_args, args) < 0) {
893 return;
bellardf193c792004-03-21 17:06:25 +0000894 }
bellard9dc39cb2004-03-14 21:38:27 +0000895 }
Wenchao Xia66855492013-08-27 20:38:23 +0800896
897 /* 2. dump the contents according to parsed args */
898 help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0);
899
900 free_cmdline_args(args, nb_args);
bellard9dc39cb2004-03-14 21:38:27 +0000901}
902
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300903static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300904{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300905 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300906}
907
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100908static void hmp_trace_event(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530909{
910 const char *tp_name = qdict_get_str(qdict, "name");
911 bool new_state = qdict_get_bool(qdict, "option");
Lluís Vilanova77e2b172016-07-11 12:53:57 +0200912 bool has_vcpu = qdict_haskey(qdict, "vcpu");
913 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
Lluís Vilanova14101d02014-08-25 13:20:03 +0200914 Error *local_err = NULL;
Blue Swirlf871d682010-10-13 19:14:29 +0000915
Lluís Vilanova77e2b172016-07-11 12:53:57 +0200916 if (vcpu < 0) {
917 monitor_printf(mon, "argument vcpu must be positive");
918 return;
919 }
920
921 qmp_trace_event_set_state(tp_name, new_state, true, true, has_vcpu, vcpu, &local_err);
Lluís Vilanova14101d02014-08-25 13:20:03 +0200922 if (local_err) {
Markus Armbruster091e38b2015-02-10 15:15:43 +0100923 error_report_err(local_err);
Blue Swirlf871d682010-10-13 19:14:29 +0000924 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530925}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100926
Michael Rothc45a8162011-10-02 08:44:37 -0500927#ifdef CONFIG_TRACE_SIMPLE
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100928static void hmp_trace_file(Monitor *mon, const QDict *qdict)
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100929{
930 const char *op = qdict_get_try_str(qdict, "op");
931 const char *arg = qdict_get_try_str(qdict, "arg");
932
933 if (!op) {
934 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
935 } else if (!strcmp(op, "on")) {
936 st_set_trace_file_enabled(true);
937 } else if (!strcmp(op, "off")) {
938 st_set_trace_file_enabled(false);
939 } else if (!strcmp(op, "flush")) {
940 st_flush_trace_buffer();
941 } else if (!strcmp(op, "set")) {
942 if (arg) {
943 st_set_trace_file(arg);
944 }
945 } else {
946 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
947 help_cmd(mon, "trace-file");
948 }
949}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530950#endif
951
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100952static void hmp_info_help(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000953{
Luiz Capitulino13c74252009-10-07 13:41:55 -0300954 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000955}
956
Marc-André Lureau9e812b62016-09-12 13:19:05 +0400957static void query_commands_cb(QmpCommand *cmd, void *opaque)
bellard9bc9d1c2004-10-10 15:15:51 +0000958{
Marc-André Lureau9e812b62016-09-12 13:19:05 +0400959 CommandInfoList *info, **list = opaque;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200960
Marc-André Lureau9e812b62016-09-12 13:19:05 +0400961 if (!cmd->enabled) {
962 return;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200963 }
964
Marc-André Lureau9e812b62016-09-12 13:19:05 +0400965 info = g_malloc0(sizeof(*info));
966 info->value = g_malloc0(sizeof(*info->value));
967 info->value->name = g_strdup(cmd->name);
968 info->next = *list;
969 *list = info;
970}
971
972CommandInfoList *qmp_query_commands(Error **errp)
973{
974 CommandInfoList *list = NULL;
975
976 qmp_for_each_command(query_commands_cb, &list);
977
978 return list;
thsa36e69d2007-12-02 05:18:19 +0000979}
980
Daniel P. Berrange48608532012-05-21 17:59:51 +0100981EventInfoList *qmp_query_events(Error **errp)
982{
983 EventInfoList *info, *ev_list = NULL;
Wenchao Xia75175172014-06-18 08:43:54 +0200984 QAPIEvent e;
Daniel P. Berrange48608532012-05-21 17:59:51 +0100985
Eric Blake7fb1cf12015-11-18 01:52:57 -0700986 for (e = 0 ; e < QAPI_EVENT__MAX ; e++) {
Wenchao Xia75175172014-06-18 08:43:54 +0200987 const char *event_name = QAPIEvent_lookup[e];
Daniel P. Berrange48608532012-05-21 17:59:51 +0100988 assert(event_name != NULL);
989 info = g_malloc0(sizeof(*info));
990 info->value = g_malloc0(sizeof(*info->value));
991 info->value->name = g_strdup(event_name);
992
993 info->next = ev_list;
994 ev_list = info;
995 }
996
997 return ev_list;
998}
999
Markus Armbruster39a18152015-09-16 13:06:28 +02001000/*
1001 * Minor hack: generated marshalling suppressed for this command
1002 * ('gen': false in the schema) so we can parse the JSON string
1003 * directly into QObject instead of first parsing it with
1004 * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
1005 * to QObject with generated output marshallers, every time. Instead,
1006 * we do it in test-qmp-input-visitor.c, just to make sure
1007 * qapi-introspect.py's output actually conforms to the schema.
1008 */
1009static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
1010 Error **errp)
1011{
1012 *ret_data = qobject_from_json(qmp_schema_json);
1013}
1014
Marc-André Lureau5032a162016-09-12 13:19:02 +04001015/*
Marc-André Lureau5032a162016-09-12 13:19:02 +04001016 * We used to define commands in qmp-commands.hx in addition to the
1017 * QAPI schema. This permitted defining some of them only in certain
1018 * configurations. query-commands has always reflected that (good,
1019 * because it lets QMP clients figure out what's actually available),
1020 * while query-qmp-schema never did (not so good). This function is a
1021 * hack to keep the configuration-specific commands defined exactly as
1022 * before, even though qmp-commands.hx is gone.
1023 *
1024 * FIXME Educate the QAPI schema on configuration-specific commands,
1025 * and drop this hack.
1026 */
1027static void qmp_unregister_commands_hack(void)
1028{
1029#ifndef CONFIG_SPICE
1030 qmp_unregister_command("query-spice");
1031#endif
1032#ifndef TARGET_I386
1033 qmp_unregister_command("rtc-reset-reinjection");
1034#endif
1035#ifndef TARGET_S390X
1036 qmp_unregister_command("dump-skeys");
1037#endif
1038#ifndef TARGET_ARM
1039 qmp_unregister_command("query-gic-capabilities");
1040#endif
1041}
1042
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001043static void qmp_init_marshal(void)
1044{
1045 qmp_register_command("query-qmp-schema", qmp_query_qmp_schema,
1046 QCO_NO_OPTIONS);
1047 qmp_register_command("device_add", qmp_device_add,
1048 QCO_NO_OPTIONS);
1049 qmp_register_command("netdev_add", qmp_netdev_add,
1050 QCO_NO_OPTIONS);
Marc-André Lureau5032a162016-09-12 13:19:02 +04001051
1052 /* call it after the rest of qapi_init() */
1053 register_module_init(qmp_unregister_commands_hack, MODULE_INIT_QAPI);
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001054}
1055
1056qapi_init(qmp_init_marshal);
1057
Luiz Capitulinob025c8b2011-10-06 14:02:57 -03001058/* set the current CPU defined by the user */
1059int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +00001060{
Andreas Färber55e5c282012-12-17 06:18:02 +01001061 CPUState *cpu;
bellard6a00d602005-11-21 23:25:50 +00001062
Andreas Färber1c8bb3c2013-02-15 17:01:09 +01001063 cpu = qemu_get_cpu(cpu_index);
1064 if (cpu == NULL) {
1065 return -1;
bellard6a00d602005-11-21 23:25:50 +00001066 }
Andreas Färbercb446ec2013-05-01 14:24:52 +02001067 cur_mon->mon_cpu = cpu;
Andreas Färber1c8bb3c2013-02-15 17:01:09 +01001068 return 0;
bellard6a00d602005-11-21 23:25:50 +00001069}
1070
Pavel Butsykincaf15312015-09-22 16:18:17 +03001071CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +00001072{
aliguori731b0362009-03-05 23:01:42 +00001073 if (!cur_mon->mon_cpu) {
Luiz Capitulinob025c8b2011-10-06 14:02:57 -03001074 monitor_set_cpu(0);
bellard6a00d602005-11-21 23:25:50 +00001075 }
Avi Kivity4c0960c2009-08-17 23:19:53 +03001076 cpu_synchronize_state(cur_mon->mon_cpu);
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001077 return cur_mon->mon_cpu;
1078}
1079
Pavel Butsykinbf957282015-09-10 18:38:59 +03001080CPUArchState *mon_get_cpu_env(void)
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001081{
1082 return mon_get_cpu()->env_ptr;
bellard6a00d602005-11-21 23:25:50 +00001083}
1084
Luiz Capitulino99b77962011-10-24 10:53:44 -02001085int monitor_get_cpu_index(void)
1086{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001087 return mon_get_cpu()->cpu_index;
Luiz Capitulino99b77962011-10-24 10:53:44 -02001088}
1089
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001090static void hmp_info_registers(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001091{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001092 cpu_dump_state(mon_get_cpu(), (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +00001093}
1094
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001095static void hmp_info_jit(Monitor *mon, const QDict *qdict)
bellarde3db7222005-01-26 22:00:47 +00001096{
aliguori376253e2009-03-05 23:01:23 +00001097 dump_exec_info((FILE *)mon, monitor_fprintf);
Sebastian Tanase27498be2014-07-25 11:56:33 +02001098 dump_drift_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +00001099}
1100
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001101static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
Max Filippov246ae242014-11-02 11:04:18 +03001102{
1103 dump_opcount_info((FILE *)mon, monitor_fprintf);
1104}
1105
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001106static void hmp_info_history(Monitor *mon, const QDict *qdict)
bellardaa455482004-04-04 13:07:25 +00001107{
1108 int i;
bellard7e2515e2004-08-01 21:52:19 +00001109 const char *str;
ths3b46e622007-09-17 08:09:54 +00001110
aliguoricde76ee2009-03-05 23:01:51 +00001111 if (!mon->rs)
1112 return;
bellard7e2515e2004-08-01 21:52:19 +00001113 i = 0;
1114 for(;;) {
aliguori731b0362009-03-05 23:01:42 +00001115 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +00001116 if (!str)
1117 break;
aliguori376253e2009-03-05 23:01:23 +00001118 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +00001119 i++;
bellardaa455482004-04-04 13:07:25 +00001120 }
1121}
1122
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001123static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
j_mayer76a66252007-03-07 08:32:30 +00001124{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001125 cpu_dump_statistics(mon_get_cpu(), (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +00001126}
j_mayer76a66252007-03-07 08:32:30 +00001127
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001128static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301129{
Lluís Vilanovabd712112016-07-11 12:53:51 +02001130 const char *name = qdict_get_try_str(qdict, "name");
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001131 bool has_vcpu = qdict_haskey(qdict, "vcpu");
1132 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
Lluís Vilanovabd712112016-07-11 12:53:51 +02001133 TraceEventInfoList *events;
Lluís Vilanova14101d02014-08-25 13:20:03 +02001134 TraceEventInfoList *elem;
Lluís Vilanovabd712112016-07-11 12:53:51 +02001135 Error *local_err = NULL;
1136
1137 if (name == NULL) {
1138 name = "*";
1139 }
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001140 if (vcpu < 0) {
1141 monitor_printf(mon, "argument vcpu must be positive");
1142 return;
1143 }
Lluís Vilanovabd712112016-07-11 12:53:51 +02001144
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001145 events = qmp_trace_event_get_state(name, has_vcpu, vcpu, &local_err);
Lluís Vilanovabd712112016-07-11 12:53:51 +02001146 if (local_err) {
1147 error_report_err(local_err);
1148 return;
1149 }
Lluís Vilanova14101d02014-08-25 13:20:03 +02001150
1151 for (elem = events; elem != NULL; elem = elem->next) {
1152 monitor_printf(mon, "%s : state %u\n",
1153 elem->value->name,
1154 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
1155 }
1156 qapi_free_TraceEventInfoList(events);
Prerna Saxena22890ab2010-06-24 17:04:53 +05301157}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301158
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001159void qmp_client_migrate_info(const char *protocol, const char *hostname,
1160 bool has_port, int64_t port,
1161 bool has_tls_port, int64_t tls_port,
1162 bool has_cert_subject, const char *cert_subject,
1163 Error **errp)
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001164{
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001165 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001166 if (!qemu_using_spice(errp)) {
1167 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001168 }
1169
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001170 if (!has_port && !has_tls_port) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001171 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001172 return;
Yonit Halperin6ec5dae2012-03-18 09:42:39 +02001173 }
1174
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001175 if (qemu_spice_migrate_info(hostname,
1176 has_port ? port : -1,
1177 has_tls_port ? tls_port : -1,
1178 cert_subject)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001179 error_setg(errp, QERR_UNDEFINED_ERROR);
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001180 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001181 }
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001182 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001183 }
1184
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001185 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001186}
1187
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001188static void hmp_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001189{
Markus Armbrusterdaa76aa2016-06-15 19:27:16 +02001190 Error *err = NULL;
1191
1192 qemu_set_log_filename(qdict_get_str(qdict, "filename"), &err);
1193 if (err) {
1194 error_report_err(err);
1195 }
pbrooke735b912007-06-30 13:53:24 +00001196}
1197
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001198static void hmp_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001199{
1200 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001201 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001202
bellard9307c4c2004-04-04 12:57:25 +00001203 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001204 mask = 0;
1205 } else {
Peter Maydell4fde1eb2013-02-11 16:41:22 +00001206 mask = qemu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001207 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001208 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001209 return;
1210 }
1211 }
Peter Maydell24537a02013-02-11 16:41:23 +00001212 qemu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00001213}
1214
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001215static void hmp_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001216{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001217 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001218 if (!option || !strcmp(option, "on")) {
1219 singlestep = 1;
1220 } else if (!strcmp(option, "off")) {
1221 singlestep = 0;
1222 } else {
1223 monitor_printf(mon, "unexpected option %s\n", option);
1224 }
1225}
1226
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001227static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001228{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001229 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001230 if (!device)
1231 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1232 if (gdbserver_start(device) < 0) {
1233 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1234 device);
1235 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001236 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001237 } else {
aliguori59030a82009-04-05 18:43:41 +00001238 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1239 device);
bellard8a7ddc32004-03-31 19:00:16 +00001240 }
1241}
1242
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001243static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001244{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001245 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001246 if (select_watchdog_action(action) == -1) {
1247 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1248 }
1249}
1250
aliguori376253e2009-03-05 23:01:23 +00001251static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001252{
aliguori376253e2009-03-05 23:01:23 +00001253 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001254 switch(c) {
1255 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001256 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001257 break;
1258 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001259 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001260 break;
1261 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001262 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001263 break;
1264 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001265 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001266 break;
1267 default:
1268 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001269 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001270 } else {
aliguori376253e2009-03-05 23:01:23 +00001271 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001272 }
1273 break;
1274 }
aliguori376253e2009-03-05 23:01:23 +00001275 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001276}
1277
aliguori376253e2009-03-05 23:01:23 +00001278static void memory_dump(Monitor *mon, int count, int format, int wsize,
Avi Kivitya8170e52012-10-23 12:30:10 +02001279 hwaddr addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001280{
Blue Swirl23842aa2010-01-12 20:27:43 +00001281 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001282 uint8_t buf[16];
1283 uint64_t v;
1284
1285 if (format == 'i') {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001286 int flags = 0;
bellard9307c4c2004-04-04 12:57:25 +00001287#ifdef TARGET_I386
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001288 CPUArchState *env = mon_get_cpu_env();
bellard4c27ba22004-04-25 18:05:08 +00001289 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001290 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001291 } else if (wsize == 4) {
1292 flags = 0;
1293 } else {
bellard6a15fd12006-04-12 21:07:07 +00001294 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001295 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001296 if (env) {
1297#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001298 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001299 (env->segs[R_CS].flags & DESC_L_MASK))
1300 flags = 2;
1301 else
1302#endif
1303 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1304 flags = 1;
1305 }
bellard4c27ba22004-04-25 18:05:08 +00001306 }
1307#endif
Tom Musta1c38f842014-04-09 14:53:24 -05001308#ifdef TARGET_PPC
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001309 CPUArchState *env = mon_get_cpu_env();
Tom Musta1c38f842014-04-09 14:53:24 -05001310 flags = msr_le << 16;
1311 flags |= env->bfd_mach;
1312#endif
Peter Crosthwaited49190c2015-05-24 14:20:41 -07001313 monitor_disas(mon, mon_get_cpu(), addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001314 return;
1315 }
1316
1317 len = wsize * count;
1318 if (wsize == 1)
1319 line_size = 8;
1320 else
1321 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001322 max_digits = 0;
1323
1324 switch(format) {
1325 case 'o':
1326 max_digits = (wsize * 8 + 2) / 3;
1327 break;
1328 default:
1329 case 'x':
1330 max_digits = (wsize * 8) / 4;
1331 break;
1332 case 'u':
1333 case 'd':
1334 max_digits = (wsize * 8 * 10 + 32) / 33;
1335 break;
1336 case 'c':
1337 wsize = 1;
1338 break;
1339 }
1340
1341 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001342 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001343 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001344 else
aliguori376253e2009-03-05 23:01:23 +00001345 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001346 l = len;
1347 if (l > line_size)
1348 l = line_size;
1349 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001350 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001351 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001352 if (cpu_memory_rw_debug(mon_get_cpu(), addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001353 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001354 break;
1355 }
bellard9307c4c2004-04-04 12:57:25 +00001356 }
ths5fafdf22007-09-16 21:08:06 +00001357 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001358 while (i < l) {
1359 switch(wsize) {
1360 default:
1361 case 1:
Peter Maydell24e60302015-01-20 15:19:32 +00001362 v = ldub_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001363 break;
1364 case 2:
Peter Maydell24e60302015-01-20 15:19:32 +00001365 v = lduw_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001366 break;
1367 case 4:
Peter Maydell24e60302015-01-20 15:19:32 +00001368 v = (uint32_t)ldl_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001369 break;
1370 case 8:
Peter Maydell24e60302015-01-20 15:19:32 +00001371 v = ldq_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001372 break;
1373 }
aliguori376253e2009-03-05 23:01:23 +00001374 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001375 switch(format) {
1376 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001377 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001378 break;
1379 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001380 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001381 break;
1382 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001383 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001384 break;
1385 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001386 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001387 break;
1388 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001389 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001390 break;
1391 }
1392 i += wsize;
1393 }
aliguori376253e2009-03-05 23:01:23 +00001394 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001395 addr += l;
1396 len -= l;
1397 }
1398}
1399
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001400static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001401{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001402 int count = qdict_get_int(qdict, "count");
1403 int format = qdict_get_int(qdict, "format");
1404 int size = qdict_get_int(qdict, "size");
1405 target_long addr = qdict_get_int(qdict, "addr");
1406
aliguori376253e2009-03-05 23:01:23 +00001407 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001408}
1409
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001410static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001411{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001412 int count = qdict_get_int(qdict, "count");
1413 int format = qdict_get_int(qdict, "format");
1414 int size = qdict_get_int(qdict, "size");
Avi Kivitya8170e52012-10-23 12:30:10 +02001415 hwaddr addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001416
aliguori376253e2009-03-05 23:01:23 +00001417 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001418}
1419
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001420static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001421{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001422 int format = qdict_get_int(qdict, "format");
Avi Kivitya8170e52012-10-23 12:30:10 +02001423 hwaddr val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001424
bellard9307c4c2004-04-04 12:57:25 +00001425 switch(format) {
1426 case 'o':
Avi Kivitya8170e52012-10-23 12:30:10 +02001427 monitor_printf(mon, "%#" HWADDR_PRIo, val);
bellard9307c4c2004-04-04 12:57:25 +00001428 break;
1429 case 'x':
Avi Kivitya8170e52012-10-23 12:30:10 +02001430 monitor_printf(mon, "%#" HWADDR_PRIx, val);
bellard9307c4c2004-04-04 12:57:25 +00001431 break;
1432 case 'u':
Avi Kivitya8170e52012-10-23 12:30:10 +02001433 monitor_printf(mon, "%" HWADDR_PRIu, val);
bellard9307c4c2004-04-04 12:57:25 +00001434 break;
1435 default:
1436 case 'd':
Avi Kivitya8170e52012-10-23 12:30:10 +02001437 monitor_printf(mon, "%" HWADDR_PRId, val);
bellard9307c4c2004-04-04 12:57:25 +00001438 break;
1439 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001440 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001441 break;
1442 }
aliguori376253e2009-03-05 23:01:23 +00001443 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001444}
1445
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001446static void hmp_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001447{
1448 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001449 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001450 uint32_t start = qdict_get_int(qdict, "start");
1451 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001452
1453 sum = 0;
1454 for(addr = start; addr < (start + size); addr++) {
Peter Maydell42874d32015-04-26 16:49:24 +01001455 uint8_t val = address_space_ldub(&address_space_memory, addr,
1456 MEMTXATTRS_UNSPECIFIED, NULL);
bellarde4cf1ad2005-06-04 20:15:57 +00001457 /* BSD sum algorithm ('sum' Unix command) */
1458 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001459 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001460 }
aliguori376253e2009-03-05 23:01:23 +00001461 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001462}
1463
bellard13224a82006-07-14 22:03:35 +00001464static int mouse_button_state;
1465
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001466static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001467{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001468 int dx, dy, dz, button;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001469 const char *dx_str = qdict_get_str(qdict, "dx_str");
1470 const char *dy_str = qdict_get_str(qdict, "dy_str");
1471 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001472
bellard13224a82006-07-14 22:03:35 +00001473 dx = strtol(dx_str, NULL, 0);
1474 dy = strtol(dy_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001475 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
1476 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
1477
1478 if (dz_str) {
bellard13224a82006-07-14 22:03:35 +00001479 dz = strtol(dz_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001480 if (dz != 0) {
Gerd Hoffmannf22d0af2016-01-12 12:14:12 +01001481 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001482 qemu_input_queue_btn(NULL, button, true);
1483 qemu_input_event_sync();
1484 qemu_input_queue_btn(NULL, button, false);
1485 }
1486 }
1487 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001488}
1489
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001490static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001491{
Eric Blake7fb1cf12015-11-18 01:52:57 -07001492 static uint32_t bmap[INPUT_BUTTON__MAX] = {
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001493 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1494 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
1495 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1496 };
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001497 int button_state = qdict_get_int(qdict, "button_state");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001498
1499 if (mouse_button_state == button_state) {
1500 return;
1501 }
1502 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1503 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001504 mouse_button_state = button_state;
bellard13224a82006-07-14 22:03:35 +00001505}
1506
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001507static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001508{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001509 int size = qdict_get_int(qdict, "size");
1510 int addr = qdict_get_int(qdict, "addr");
1511 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001512 uint32_t val;
1513 int suffix;
1514
1515 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001516 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001517 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001518 addr++;
1519 }
1520 addr &= 0xffff;
1521
1522 switch(size) {
1523 default:
1524 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001525 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001526 suffix = 'b';
1527 break;
1528 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001529 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001530 suffix = 'w';
1531 break;
1532 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001533 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001534 suffix = 'l';
1535 break;
1536 }
aliguori376253e2009-03-05 23:01:23 +00001537 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1538 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001539}
bellarda3a91a32004-06-04 11:06:21 +00001540
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001541static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001542{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001543 int size = qdict_get_int(qdict, "size");
1544 int addr = qdict_get_int(qdict, "addr");
1545 int val = qdict_get_int(qdict, "val");
1546
Jan Kiszkaf1147842009-07-14 10:20:11 +02001547 addr &= IOPORTS_MASK;
1548
1549 switch (size) {
1550 default:
1551 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001552 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001553 break;
1554 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001555 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001556 break;
1557 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001558 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001559 break;
1560 }
1561}
1562
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001563static void hmp_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001564{
Gongleif1839932014-12-03 18:20:58 +00001565 Error *local_err = NULL;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001566 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001567
Gongleif1839932014-12-03 18:20:58 +00001568 qemu_boot_set(bootdevice, &local_err);
1569 if (local_err) {
Markus Armbruster193227f2015-12-18 16:35:06 +01001570 error_report_err(local_err);
aurel320ecdffb2008-05-04 20:11:34 +00001571 } else {
Gongleif1839932014-12-03 18:20:58 +00001572 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001573 }
1574}
1575
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001576static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
Blue Swirl314e2982011-09-11 20:22:05 +00001577{
1578 mtree_info((fprintf_function)monitor_printf, mon);
1579}
1580
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001581static void hmp_info_numa(Monitor *mon, const QDict *qdict)
aliguori030ea372009-04-21 22:30:47 +00001582{
aliguorib28b6232009-04-22 20:20:29 +00001583 int i;
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001584 CPUState *cpu;
zhanghailiang5b009e42014-11-04 19:49:30 +08001585 uint64_t *node_mem;
aliguori030ea372009-04-21 22:30:47 +00001586
zhanghailiang5b009e42014-11-04 19:49:30 +08001587 node_mem = g_new0(uint64_t, nb_numa_nodes);
1588 query_numa_node_mem(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001589 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1590 for (i = 0; i < nb_numa_nodes; i++) {
1591 monitor_printf(mon, "node %d cpus:", i);
Andreas Färberbdc44642013-06-24 23:50:24 +02001592 CPU_FOREACH(cpu) {
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001593 if (cpu->numa_node == i) {
Andreas Färber55e5c282012-12-17 06:18:02 +01001594 monitor_printf(mon, " %d", cpu->cpu_index);
aliguori030ea372009-04-21 22:30:47 +00001595 }
1596 }
1597 monitor_printf(mon, "\n");
1598 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
zhanghailiang5b009e42014-11-04 19:49:30 +08001599 node_mem[i] >> 20);
aliguori030ea372009-04-21 22:30:47 +00001600 }
zhanghailiang5b009e42014-11-04 19:49:30 +08001601 g_free(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001602}
1603
bellard5f1ce942006-02-08 22:40:15 +00001604#ifdef CONFIG_PROFILER
1605
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001606int64_t tcg_time;
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001607int64_t dev_time;
1608
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001609static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001610{
aliguori376253e2009-03-05 23:01:23 +00001611 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Rutuja Shah73bcb242016-03-21 21:32:30 +05301612 dev_time, dev_time / (double)NANOSECONDS_PER_SECOND);
aliguori376253e2009-03-05 23:01:23 +00001613 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Rutuja Shah73bcb242016-03-21 21:32:30 +05301614 tcg_time, tcg_time / (double)NANOSECONDS_PER_SECOND);
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001615 tcg_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001616 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001617}
1618#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001619static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001620{
aliguori376253e2009-03-05 23:01:23 +00001621 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001622}
1623#endif
1624
bellardec36b692006-07-16 18:57:03 +00001625/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001626static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001627
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001628static void hmp_info_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001629{
1630 int i;
1631 CaptureState *s;
1632
1633 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001634 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001635 s->ops.info (s->opaque);
1636 }
1637}
1638
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001639static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001640{
1641 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001642 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001643 CaptureState *s;
1644
1645 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1646 if (i == n) {
1647 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001648 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001649 g_free (s);
bellardec36b692006-07-16 18:57:03 +00001650 return;
1651 }
1652 }
1653}
1654
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001655static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001656{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001657 const char *path = qdict_get_str(qdict, "path");
1658 int has_freq = qdict_haskey(qdict, "freq");
1659 int freq = qdict_get_try_int(qdict, "freq", -1);
1660 int has_bits = qdict_haskey(qdict, "bits");
1661 int bits = qdict_get_try_int(qdict, "bits", -1);
1662 int has_channels = qdict_haskey(qdict, "nchannels");
1663 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001664 CaptureState *s;
1665
Anthony Liguori7267c092011-08-20 22:09:37 -05001666 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001667
1668 freq = has_freq ? freq : 44100;
1669 bits = has_bits ? bits : 16;
1670 nchannels = has_channels ? nchannels : 2;
1671
1672 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001673 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05001674 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001675 return;
bellardec36b692006-07-16 18:57:03 +00001676 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001677 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001678}
bellardec36b692006-07-16 18:57:03 +00001679
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001680static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001681{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001682 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001683
aliguori76655d62009-03-06 20:27:37 +00001684 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001685 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001686 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001687 return acl;
1688}
aliguori76655d62009-03-06 20:27:37 +00001689
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001690static void hmp_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001691{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001692 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001693 qemu_acl *acl = find_acl(mon, aclname);
1694 qemu_acl_entry *entry;
1695 int i = 0;
1696
1697 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001698 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001699 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00001700 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00001701 i++;
1702 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001703 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001704 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001705 }
1706}
1707
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001708static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001709{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001710 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001711 qemu_acl *acl = find_acl(mon, aclname);
1712
1713 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001714 qemu_acl_reset(acl);
1715 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001716 }
1717}
aliguori76655d62009-03-06 20:27:37 +00001718
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001719static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001720{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001721 const char *aclname = qdict_get_str(qdict, "aclname");
1722 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001723 qemu_acl *acl = find_acl(mon, aclname);
1724
1725 if (acl) {
1726 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001727 acl->defaultDeny = 0;
1728 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001729 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001730 acl->defaultDeny = 1;
1731 monitor_printf(mon, "acl: policy set to 'deny'\n");
1732 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001733 monitor_printf(mon, "acl: unknown policy '%s', "
1734 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001735 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001736 }
1737}
aliguori76655d62009-03-06 20:27:37 +00001738
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001739static void hmp_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001740{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001741 const char *aclname = qdict_get_str(qdict, "aclname");
1742 const char *match = qdict_get_str(qdict, "match");
1743 const char *policy = qdict_get_str(qdict, "policy");
1744 int has_index = qdict_haskey(qdict, "index");
1745 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001746 qemu_acl *acl = find_acl(mon, aclname);
1747 int deny, ret;
1748
1749 if (acl) {
1750 if (strcmp(policy, "allow") == 0) {
1751 deny = 0;
1752 } else if (strcmp(policy, "deny") == 0) {
1753 deny = 1;
1754 } else {
1755 monitor_printf(mon, "acl: unknown policy '%s', "
1756 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001757 return;
1758 }
aliguori28a76be2009-03-06 20:27:40 +00001759 if (has_index)
1760 ret = qemu_acl_insert(acl, deny, match, index);
1761 else
1762 ret = qemu_acl_append(acl, deny, match);
1763 if (ret < 0)
1764 monitor_printf(mon, "acl: unable to add acl entry\n");
1765 else
1766 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001767 }
1768}
aliguori76655d62009-03-06 20:27:37 +00001769
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001770static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001771{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001772 const char *aclname = qdict_get_str(qdict, "aclname");
1773 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001774 qemu_acl *acl = find_acl(mon, aclname);
1775 int ret;
aliguori76655d62009-03-06 20:27:37 +00001776
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001777 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001778 ret = qemu_acl_remove(acl, match);
1779 if (ret < 0)
1780 monitor_printf(mon, "acl: no matching acl entry\n");
1781 else
1782 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001783 }
1784}
1785
Corey Bryant208c9d12012-06-22 14:36:09 -04001786void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001787{
Anthony Liguoric227f092009-10-01 16:12:16 -05001788 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001789 int fd;
1790
Corey Bryant208c9d12012-06-22 14:36:09 -04001791 fd = qemu_chr_fe_get_msgfd(cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001792 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001793 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryant208c9d12012-06-22 14:36:09 -04001794 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001795 }
1796
1797 if (qemu_isdigit(fdname[0])) {
Stefan Hajnoczi0b9f0e22014-04-24 13:58:18 +02001798 close(fd);
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001799 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
1800 "a name not starting with a digit");
Corey Bryant208c9d12012-06-22 14:36:09 -04001801 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001802 }
1803
Corey Bryant208c9d12012-06-22 14:36:09 -04001804 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001805 if (strcmp(monfd->name, fdname) != 0) {
1806 continue;
1807 }
1808
1809 close(monfd->fd);
1810 monfd->fd = fd;
Corey Bryant208c9d12012-06-22 14:36:09 -04001811 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001812 }
1813
Anthony Liguori7267c092011-08-20 22:09:37 -05001814 monfd = g_malloc0(sizeof(mon_fd_t));
1815 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001816 monfd->fd = fd;
1817
Corey Bryant208c9d12012-06-22 14:36:09 -04001818 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001819}
1820
Corey Bryant208c9d12012-06-22 14:36:09 -04001821void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001822{
Anthony Liguoric227f092009-10-01 16:12:16 -05001823 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001824
Corey Bryant208c9d12012-06-22 14:36:09 -04001825 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001826 if (strcmp(monfd->name, fdname) != 0) {
1827 continue;
1828 }
1829
Blue Swirl72cf2d42009-09-12 07:36:22 +00001830 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001831 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001832 g_free(monfd->name);
1833 g_free(monfd);
Corey Bryant208c9d12012-06-22 14:36:09 -04001834 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001835 }
1836
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001837 error_setg(errp, QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001838}
1839
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001840static void hmp_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001841{
Luiz Capitulino13548692011-07-29 15:36:43 -03001842 int saved_vm_running = runstate_is_running();
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001843 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02001844
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001845 vm_stop(RUN_STATE_RESTORE_VM);
Juan Quintelac8d41b22009-08-20 19:42:21 +02001846
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03001847 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02001848 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03001849 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02001850}
1851
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001852int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
Mark McLoughlin7768e042009-07-22 09:11:41 +01001853{
Anthony Liguoric227f092009-10-01 16:12:16 -05001854 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01001855
Blue Swirl72cf2d42009-09-12 07:36:22 +00001856 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01001857 int fd;
1858
1859 if (strcmp(monfd->name, fdname) != 0) {
1860 continue;
1861 }
1862
1863 fd = monfd->fd;
1864
1865 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001866 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05001867 g_free(monfd->name);
1868 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001869
1870 return fd;
1871 }
1872
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001873 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001874 return -1;
1875}
1876
Corey Bryantba1c0482012-08-14 16:43:43 -04001877static void monitor_fdset_cleanup(MonFdset *mon_fdset)
1878{
1879 MonFdsetFd *mon_fdset_fd;
1880 MonFdsetFd *mon_fdset_fd_next;
1881
1882 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantebe52b52012-10-18 15:19:33 -04001883 if ((mon_fdset_fd->removed ||
1884 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
1885 runstate_is_running()) {
Corey Bryantba1c0482012-08-14 16:43:43 -04001886 close(mon_fdset_fd->fd);
1887 g_free(mon_fdset_fd->opaque);
1888 QLIST_REMOVE(mon_fdset_fd, next);
1889 g_free(mon_fdset_fd);
1890 }
1891 }
1892
Corey Bryantadb696f2012-08-14 16:43:47 -04001893 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04001894 QLIST_REMOVE(mon_fdset, next);
1895 g_free(mon_fdset);
1896 }
1897}
1898
Corey Bryantefb87c12012-08-14 16:43:48 -04001899static void monitor_fdsets_cleanup(void)
1900{
1901 MonFdset *mon_fdset;
1902 MonFdset *mon_fdset_next;
1903
1904 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
1905 monitor_fdset_cleanup(mon_fdset);
1906 }
1907}
1908
Corey Bryantba1c0482012-08-14 16:43:43 -04001909AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
1910 const char *opaque, Error **errp)
1911{
1912 int fd;
1913 Monitor *mon = cur_mon;
Corey Bryantba1c0482012-08-14 16:43:43 -04001914 AddfdInfo *fdinfo;
1915
1916 fd = qemu_chr_fe_get_msgfd(mon->chr);
1917 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001918 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryantba1c0482012-08-14 16:43:43 -04001919 goto error;
1920 }
1921
Corey Bryante446f702012-10-18 15:19:32 -04001922 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
1923 has_opaque, opaque, errp);
1924 if (fdinfo) {
1925 return fdinfo;
Corey Bryant9ac54af2012-10-18 15:19:31 -04001926 }
1927
Corey Bryantba1c0482012-08-14 16:43:43 -04001928error:
1929 if (fd != -1) {
1930 close(fd);
1931 }
1932 return NULL;
1933}
1934
1935void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
1936{
1937 MonFdset *mon_fdset;
1938 MonFdsetFd *mon_fdset_fd;
1939 char fd_str[60];
1940
1941 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1942 if (mon_fdset->id != fdset_id) {
1943 continue;
1944 }
1945 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1946 if (has_fd) {
1947 if (mon_fdset_fd->fd != fd) {
1948 continue;
1949 }
1950 mon_fdset_fd->removed = true;
1951 break;
1952 } else {
1953 mon_fdset_fd->removed = true;
1954 }
1955 }
1956 if (has_fd && !mon_fdset_fd) {
1957 goto error;
1958 }
1959 monitor_fdset_cleanup(mon_fdset);
1960 return;
1961 }
1962
1963error:
1964 if (has_fd) {
1965 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
1966 fdset_id, fd);
1967 } else {
1968 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
1969 }
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001970 error_setg(errp, QERR_FD_NOT_FOUND, fd_str);
Corey Bryantba1c0482012-08-14 16:43:43 -04001971}
1972
1973FdsetInfoList *qmp_query_fdsets(Error **errp)
1974{
1975 MonFdset *mon_fdset;
1976 MonFdsetFd *mon_fdset_fd;
1977 FdsetInfoList *fdset_list = NULL;
1978
1979 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1980 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
1981 FdsetFdInfoList *fdsetfd_list = NULL;
1982
1983 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
1984 fdset_info->value->fdset_id = mon_fdset->id;
1985
1986 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1987 FdsetFdInfoList *fdsetfd_info;
1988
1989 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
1990 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
1991 fdsetfd_info->value->fd = mon_fdset_fd->fd;
1992 if (mon_fdset_fd->opaque) {
1993 fdsetfd_info->value->has_opaque = true;
1994 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
1995 } else {
1996 fdsetfd_info->value->has_opaque = false;
1997 }
1998
1999 fdsetfd_info->next = fdsetfd_list;
2000 fdsetfd_list = fdsetfd_info;
2001 }
2002
2003 fdset_info->value->fds = fdsetfd_list;
2004
2005 fdset_info->next = fdset_list;
2006 fdset_list = fdset_info;
2007 }
2008
2009 return fdset_list;
2010}
2011
Corey Bryante446f702012-10-18 15:19:32 -04002012AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
2013 bool has_opaque, const char *opaque,
2014 Error **errp)
2015{
2016 MonFdset *mon_fdset = NULL;
2017 MonFdsetFd *mon_fdset_fd;
2018 AddfdInfo *fdinfo;
2019
2020 if (has_fdset_id) {
2021 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2022 /* Break if match found or match impossible due to ordering by ID */
2023 if (fdset_id <= mon_fdset->id) {
2024 if (fdset_id < mon_fdset->id) {
2025 mon_fdset = NULL;
2026 }
2027 break;
2028 }
2029 }
2030 }
2031
2032 if (mon_fdset == NULL) {
2033 int64_t fdset_id_prev = -1;
2034 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
2035
2036 if (has_fdset_id) {
2037 if (fdset_id < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002038 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
2039 "a non-negative value");
Corey Bryante446f702012-10-18 15:19:32 -04002040 return NULL;
2041 }
2042 /* Use specified fdset ID */
2043 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2044 mon_fdset_cur = mon_fdset;
2045 if (fdset_id < mon_fdset_cur->id) {
2046 break;
2047 }
2048 }
2049 } else {
2050 /* Use first available fdset ID */
2051 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2052 mon_fdset_cur = mon_fdset;
2053 if (fdset_id_prev == mon_fdset_cur->id - 1) {
2054 fdset_id_prev = mon_fdset_cur->id;
2055 continue;
2056 }
2057 break;
2058 }
2059 }
2060
2061 mon_fdset = g_malloc0(sizeof(*mon_fdset));
2062 if (has_fdset_id) {
2063 mon_fdset->id = fdset_id;
2064 } else {
2065 mon_fdset->id = fdset_id_prev + 1;
2066 }
2067
2068 /* The fdset list is ordered by fdset ID */
2069 if (!mon_fdset_cur) {
2070 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
2071 } else if (mon_fdset->id < mon_fdset_cur->id) {
2072 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
2073 } else {
2074 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
2075 }
2076 }
2077
2078 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
2079 mon_fdset_fd->fd = fd;
2080 mon_fdset_fd->removed = false;
2081 if (has_opaque) {
2082 mon_fdset_fd->opaque = g_strdup(opaque);
2083 }
2084 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
2085
2086 fdinfo = g_malloc0(sizeof(*fdinfo));
2087 fdinfo->fdset_id = mon_fdset->id;
2088 fdinfo->fd = mon_fdset_fd->fd;
2089
2090 return fdinfo;
2091}
2092
Corey Bryantadb696f2012-08-14 16:43:47 -04002093int monitor_fdset_get_fd(int64_t fdset_id, int flags)
2094{
Blue Swirlb2dc64c2012-08-18 20:14:54 +00002095#ifndef _WIN32
Corey Bryantadb696f2012-08-14 16:43:47 -04002096 MonFdset *mon_fdset;
2097 MonFdsetFd *mon_fdset_fd;
2098 int mon_fd_flags;
2099
Corey Bryantadb696f2012-08-14 16:43:47 -04002100 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2101 if (mon_fdset->id != fdset_id) {
2102 continue;
2103 }
2104 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2105 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
2106 if (mon_fd_flags == -1) {
2107 return -1;
2108 }
2109
2110 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
2111 return mon_fdset_fd->fd;
2112 }
2113 }
2114 errno = EACCES;
2115 return -1;
2116 }
2117#endif
2118
2119 errno = ENOENT;
2120 return -1;
2121}
2122
2123int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
2124{
2125 MonFdset *mon_fdset;
2126 MonFdsetFd *mon_fdset_fd_dup;
2127
2128 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2129 if (mon_fdset->id != fdset_id) {
2130 continue;
2131 }
2132 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2133 if (mon_fdset_fd_dup->fd == dup_fd) {
2134 return -1;
2135 }
2136 }
2137 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2138 mon_fdset_fd_dup->fd = dup_fd;
2139 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
2140 return 0;
2141 }
2142 return -1;
2143}
2144
2145static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2146{
2147 MonFdset *mon_fdset;
2148 MonFdsetFd *mon_fdset_fd_dup;
2149
2150 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2151 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2152 if (mon_fdset_fd_dup->fd == dup_fd) {
2153 if (remove) {
2154 QLIST_REMOVE(mon_fdset_fd_dup, next);
2155 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2156 monitor_fdset_cleanup(mon_fdset);
2157 }
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002158 return -1;
2159 } else {
2160 return mon_fdset->id;
Corey Bryantadb696f2012-08-14 16:43:47 -04002161 }
Corey Bryantadb696f2012-08-14 16:43:47 -04002162 }
2163 }
2164 }
2165 return -1;
2166}
2167
2168int monitor_fdset_dup_fd_find(int dup_fd)
2169{
2170 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2171}
2172
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002173void monitor_fdset_dup_fd_remove(int dup_fd)
Corey Bryantadb696f2012-08-14 16:43:47 -04002174{
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002175 monitor_fdset_dup_fd_find_remove(dup_fd, true);
Corey Bryantadb696f2012-08-14 16:43:47 -04002176}
2177
Markus Armbruster1677f4c2015-02-09 14:03:19 +01002178int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
Laszlo Ersek59063662014-04-10 10:24:31 +02002179{
2180 int fd;
2181 Error *local_err = NULL;
2182
2183 if (!qemu_isdigit(fdname[0]) && mon) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002184 fd = monitor_get_fd(mon, fdname, &local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002185 } else {
2186 fd = qemu_parse_fd(fdname);
Laszlo Ersek59063662014-04-10 10:24:31 +02002187 if (fd == -1) {
2188 error_setg(&local_err, "Invalid file descriptor number '%s'",
2189 fdname);
2190 }
2191 }
2192 if (local_err) {
2193 error_propagate(errp, local_err);
2194 assert(fd == -1);
2195 } else {
2196 assert(fd != -1);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002197 }
2198
2199 return fd;
2200}
2201
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002202/* Please update hmp-commands.hx when adding or changing commands */
Wayne Xia816f8922011-10-12 11:32:41 +08002203static mon_cmd_t info_cmds[] = {
Pavel Butsykinda76ee72015-09-10 18:38:58 +03002204#include "hmp-commands-info.h"
2205 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002206};
2207
Wenchao Xiaa13ced52013-01-14 14:06:28 +08002208/* mon_cmds and info_cmds would be sorted at runtime */
2209static mon_cmd_t mon_cmds[] = {
2210#include "hmp-commands.h"
2211 { NULL, NULL, },
2212};
2213
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002214static const mon_cmd_t qmp_cmds[] = {
Anthony Liguorie3193602011-09-02 12:34:47 -05002215#include "qmp-commands-old.h"
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002216 { /* NULL */ },
2217};
2218
bellard9307c4c2004-04-04 12:57:25 +00002219/*******************************************************************/
2220
2221static const char *pch;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002222static sigjmp_buf expr_env;
bellard9307c4c2004-04-04 12:57:25 +00002223
bellard9307c4c2004-04-04 12:57:25 +00002224
Stefan Weil9c3175c2013-08-22 21:30:09 +02002225static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
2226expr_error(Monitor *mon, const char *fmt, ...)
bellard9dc39cb2004-03-14 21:38:27 +00002227{
Fam Zheng277acfe2013-08-20 10:58:21 +08002228 va_list ap;
2229 va_start(ap, fmt);
2230 monitor_vprintf(mon, fmt, ap);
2231 monitor_printf(mon, "\n");
2232 va_end(ap);
Peter Maydell6ab7e542013-02-20 15:21:09 +00002233 siglongjmp(expr_env, 1);
bellard9307c4c2004-04-04 12:57:25 +00002234}
2235
Markus Armbruster09b94182010-01-20 13:07:30 +01002236/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00002237static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002238{
Pavel Butsykinbf957282015-09-10 18:38:59 +03002239 const MonitorDef *md = target_monitor_defs();
bellard92a31b12005-02-10 22:00:52 +00002240 void *ptr;
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002241 uint64_t tmp = 0;
2242 int ret;
bellard92a31b12005-02-10 22:00:52 +00002243
Pavel Butsykinbf957282015-09-10 18:38:59 +03002244 if (md == NULL) {
2245 return -1;
2246 }
2247
2248 for(; md->name != NULL; md++) {
bellard9307c4c2004-04-04 12:57:25 +00002249 if (compare_cmd(name, md->name)) {
2250 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002251 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002252 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002253 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00002254 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002255 switch(md->type) {
2256 case MD_I32:
2257 *pval = *(int32_t *)ptr;
2258 break;
2259 case MD_TLONG:
2260 *pval = *(target_long *)ptr;
2261 break;
2262 default:
2263 *pval = 0;
2264 break;
2265 }
bellard9307c4c2004-04-04 12:57:25 +00002266 }
2267 return 0;
2268 }
2269 }
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002270
2271 ret = target_get_monitor_def(mon_get_cpu(), name, &tmp);
2272 if (!ret) {
2273 *pval = (target_long) tmp;
2274 }
2275
2276 return ret;
bellard9307c4c2004-04-04 12:57:25 +00002277}
2278
2279static void next(void)
2280{
Blue Swirl660f11b2009-07-31 21:16:51 +00002281 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002282 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002283 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002284 pch++;
2285 }
2286}
2287
aliguori376253e2009-03-05 23:01:23 +00002288static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002289
aliguori376253e2009-03-05 23:01:23 +00002290static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002291{
blueswir1c2efc952007-09-25 17:28:42 +00002292 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002293 char *p;
bellard6a00d602005-11-21 23:25:50 +00002294 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002295
2296 switch(*pch) {
2297 case '+':
2298 next();
aliguori376253e2009-03-05 23:01:23 +00002299 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002300 break;
2301 case '-':
2302 next();
aliguori376253e2009-03-05 23:01:23 +00002303 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002304 break;
2305 case '~':
2306 next();
aliguori376253e2009-03-05 23:01:23 +00002307 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002308 break;
2309 case '(':
2310 next();
aliguori376253e2009-03-05 23:01:23 +00002311 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002312 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002313 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002314 }
2315 next();
2316 break;
bellard81d09122004-07-14 17:21:37 +00002317 case '\'':
2318 pch++;
2319 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002320 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002321 n = *pch;
2322 pch++;
2323 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002324 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002325 next();
2326 break;
bellard9307c4c2004-04-04 12:57:25 +00002327 case '$':
2328 {
2329 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002330 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002331
bellard9307c4c2004-04-04 12:57:25 +00002332 pch++;
2333 q = buf;
2334 while ((*pch >= 'a' && *pch <= 'z') ||
2335 (*pch >= 'A' && *pch <= 'Z') ||
2336 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002337 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002338 if ((q - buf) < sizeof(buf) - 1)
2339 *q++ = *pch;
2340 pch++;
2341 }
blueswir1cd390082008-11-16 13:53:32 +00002342 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002343 pch++;
2344 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002345 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01002346 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00002347 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00002348 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002349 }
2350 break;
2351 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002352 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002353 n = 0;
2354 break;
2355 default:
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002356 errno = 0;
bellard4f4fbf72006-06-25 18:28:12 +00002357 n = strtoull(pch, &p, 0);
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002358 if (errno == ERANGE) {
2359 expr_error(mon, "number too large");
2360 }
bellard9307c4c2004-04-04 12:57:25 +00002361 if (pch == p) {
Fam Zheng277acfe2013-08-20 10:58:21 +08002362 expr_error(mon, "invalid char '%c' in expression", *p);
bellard9307c4c2004-04-04 12:57:25 +00002363 }
2364 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002365 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002366 pch++;
2367 break;
2368 }
2369 return n;
2370}
2371
2372
aliguori376253e2009-03-05 23:01:23 +00002373static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002374{
blueswir1c2efc952007-09-25 17:28:42 +00002375 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002376 int op;
ths3b46e622007-09-17 08:09:54 +00002377
aliguori376253e2009-03-05 23:01:23 +00002378 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002379 for(;;) {
2380 op = *pch;
2381 if (op != '*' && op != '/' && op != '%')
2382 break;
2383 next();
aliguori376253e2009-03-05 23:01:23 +00002384 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002385 switch(op) {
2386 default:
2387 case '*':
2388 val *= val2;
2389 break;
2390 case '/':
2391 case '%':
ths5fafdf22007-09-16 21:08:06 +00002392 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002393 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002394 if (op == '/')
2395 val /= val2;
2396 else
2397 val %= val2;
2398 break;
2399 }
2400 }
2401 return val;
2402}
2403
aliguori376253e2009-03-05 23:01:23 +00002404static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002405{
blueswir1c2efc952007-09-25 17:28:42 +00002406 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002407 int op;
bellard9307c4c2004-04-04 12:57:25 +00002408
aliguori376253e2009-03-05 23:01:23 +00002409 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002410 for(;;) {
2411 op = *pch;
2412 if (op != '&' && op != '|' && op != '^')
2413 break;
2414 next();
aliguori376253e2009-03-05 23:01:23 +00002415 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002416 switch(op) {
2417 default:
2418 case '&':
2419 val &= val2;
2420 break;
2421 case '|':
2422 val |= val2;
2423 break;
2424 case '^':
2425 val ^= val2;
2426 break;
2427 }
2428 }
2429 return val;
2430}
2431
aliguori376253e2009-03-05 23:01:23 +00002432static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002433{
blueswir1c2efc952007-09-25 17:28:42 +00002434 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002435 int op;
bellard9307c4c2004-04-04 12:57:25 +00002436
aliguori376253e2009-03-05 23:01:23 +00002437 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002438 for(;;) {
2439 op = *pch;
2440 if (op != '+' && op != '-')
2441 break;
2442 next();
aliguori376253e2009-03-05 23:01:23 +00002443 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002444 if (op == '+')
2445 val += val2;
2446 else
2447 val -= val2;
2448 }
2449 return val;
2450}
2451
aliguori376253e2009-03-05 23:01:23 +00002452static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002453{
2454 pch = *pp;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002455 if (sigsetjmp(expr_env, 0)) {
bellard9307c4c2004-04-04 12:57:25 +00002456 *pp = pch;
2457 return -1;
2458 }
blueswir1cd390082008-11-16 13:53:32 +00002459 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002460 pch++;
aliguori376253e2009-03-05 23:01:23 +00002461 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002462 *pp = pch;
2463 return 0;
2464}
2465
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002466static int get_double(Monitor *mon, double *pval, const char **pp)
2467{
2468 const char *p = *pp;
2469 char *tailp;
2470 double d;
2471
2472 d = strtod(p, &tailp);
2473 if (tailp == p) {
2474 monitor_printf(mon, "Number expected\n");
2475 return -1;
2476 }
2477 if (d != d || d - d != 0) {
2478 /* NaN or infinity */
2479 monitor_printf(mon, "Bad number\n");
2480 return -1;
2481 }
2482 *pval = d;
2483 *pp = tailp;
2484 return 0;
2485}
2486
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002487/*
2488 * Store the command-name in cmdname, and return a pointer to
2489 * the remaining of the command string.
2490 */
2491static const char *get_command_name(const char *cmdline,
2492 char *cmdname, size_t nlen)
2493{
2494 size_t len;
2495 const char *p, *pstart;
2496
2497 p = cmdline;
2498 while (qemu_isspace(*p))
2499 p++;
2500 if (*p == '\0')
2501 return NULL;
2502 pstart = p;
2503 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2504 p++;
2505 len = p - pstart;
2506 if (len > nlen - 1)
2507 len = nlen - 1;
2508 memcpy(cmdname, pstart, len);
2509 cmdname[len] = '\0';
2510 return p;
2511}
2512
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002513/**
2514 * Read key of 'type' into 'key' and return the current
2515 * 'type' pointer.
2516 */
2517static char *key_get_info(const char *type, char **key)
2518{
2519 size_t len;
2520 char *p, *str;
2521
2522 if (*type == ',')
2523 type++;
2524
2525 p = strchr(type, ':');
2526 if (!p) {
2527 *key = NULL;
2528 return NULL;
2529 }
2530 len = p - type;
2531
Anthony Liguori7267c092011-08-20 22:09:37 -05002532 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002533 memcpy(str, type, len);
2534 str[len] = '\0';
2535
2536 *key = str;
2537 return ++p;
2538}
2539
bellard9307c4c2004-04-04 12:57:25 +00002540static int default_fmt_format = 'x';
2541static int default_fmt_size = 4;
2542
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002543static int is_valid_option(const char *c, const char *typestr)
2544{
2545 char option[3];
2546
2547 option[0] = '-';
2548 option[1] = *c;
2549 option[2] = '\0';
2550
2551 typestr = strstr(typestr, option);
2552 return (typestr != NULL);
2553}
2554
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03002555static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
2556 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002557{
2558 const mon_cmd_t *cmd;
2559
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03002560 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002561 if (compare_cmd(cmdname, cmd->name)) {
2562 return cmd;
2563 }
2564 }
2565
2566 return NULL;
2567}
2568
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03002569static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
2570{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002571 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03002572}
2573
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002574/*
Bandan Dasae502122015-06-03 18:38:08 -04002575 * Parse command name from @cmdp according to command table @table.
2576 * If blank, return NULL.
2577 * Else, if no valid command can be found, report to @mon, and return
2578 * NULL.
2579 * Else, change @cmdp to point right behind the name, and return its
2580 * command table entry.
2581 * Do not assume the return value points into @table! It doesn't when
2582 * the command is found in a sub-command table.
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002583 */
Anthony Liguoric227f092009-10-01 16:12:16 -05002584static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Bandan Dasae502122015-06-03 18:38:08 -04002585 const char **cmdp,
2586 mon_cmd_t *table)
bellard9307c4c2004-04-04 12:57:25 +00002587{
Bandan Dasae502122015-06-03 18:38:08 -04002588 const char *p;
Anthony Liguoric227f092009-10-01 16:12:16 -05002589 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002590 char cmdname[256];
bellard9dc39cb2004-03-14 21:38:27 +00002591
bellard9307c4c2004-04-04 12:57:25 +00002592 /* extract the command name */
Bandan Dasae502122015-06-03 18:38:08 -04002593 p = get_command_name(*cmdp, cmdname, sizeof(cmdname));
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002594 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002595 return NULL;
ths3b46e622007-09-17 08:09:54 +00002596
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002597 cmd = search_dispatch_table(table, cmdname);
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002598 if (!cmd) {
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002599 monitor_printf(mon, "unknown command: '%.*s'\n",
Bandan Dasae502122015-06-03 18:38:08 -04002600 (int)(p - *cmdp), *cmdp);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002601 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002602 }
bellard9307c4c2004-04-04 12:57:25 +00002603
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002604 /* filter out following useless space */
2605 while (qemu_isspace(*p)) {
2606 p++;
2607 }
Bandan Dasae502122015-06-03 18:38:08 -04002608
2609 *cmdp = p;
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002610 /* search sub command */
Bandan Dasae502122015-06-03 18:38:08 -04002611 if (cmd->sub_table != NULL && *p != '\0') {
2612 return monitor_parse_command(mon, cmdp, cmd->sub_table);
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002613 }
2614
Bandan Dasae502122015-06-03 18:38:08 -04002615 return cmd;
2616}
2617
2618/*
2619 * Parse arguments for @cmd.
2620 * If it can't be parsed, report to @mon, and return NULL.
2621 * Else, insert command arguments into a QDict, and return it.
2622 * Note: On success, caller has to free the QDict structure.
2623 */
2624
2625static QDict *monitor_parse_arguments(Monitor *mon,
2626 const char **endp,
2627 const mon_cmd_t *cmd)
2628{
2629 const char *typestr;
2630 char *key;
2631 int c;
2632 const char *p = *endp;
2633 char buf[1024];
2634 QDict *qdict = qdict_new();
2635
bellard9307c4c2004-04-04 12:57:25 +00002636 /* parse the parameters */
2637 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00002638 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002639 typestr = key_get_info(typestr, &key);
2640 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00002641 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002642 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00002643 typestr++;
2644 switch(c) {
2645 case 'F':
bellard81d09122004-07-14 17:21:37 +00002646 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002647 case 's':
2648 {
2649 int ret;
ths3b46e622007-09-17 08:09:54 +00002650
blueswir1cd390082008-11-16 13:53:32 +00002651 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002652 p++;
2653 if (*typestr == '?') {
2654 typestr++;
2655 if (*p == '\0') {
2656 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03002657 break;
bellard9307c4c2004-04-04 12:57:25 +00002658 }
2659 }
2660 ret = get_str(buf, sizeof(buf), &p);
2661 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002662 switch(c) {
2663 case 'F':
aliguori376253e2009-03-05 23:01:23 +00002664 monitor_printf(mon, "%s: filename expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002665 cmd->name);
bellard81d09122004-07-14 17:21:37 +00002666 break;
2667 case 'B':
aliguori376253e2009-03-05 23:01:23 +00002668 monitor_printf(mon, "%s: block device name expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002669 cmd->name);
bellard81d09122004-07-14 17:21:37 +00002670 break;
2671 default:
Bandan Dasae502122015-06-03 18:38:08 -04002672 monitor_printf(mon, "%s: string expected\n", cmd->name);
bellard81d09122004-07-14 17:21:37 +00002673 break;
2674 }
bellard9307c4c2004-04-04 12:57:25 +00002675 goto fail;
2676 }
Luiz Capitulino53773582009-08-28 15:27:25 -03002677 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00002678 }
2679 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01002680 case 'O':
2681 {
2682 QemuOptsList *opts_list;
2683 QemuOpts *opts;
2684
2685 opts_list = qemu_find_opts(key);
2686 if (!opts_list || opts_list->desc->name) {
2687 goto bad_type;
2688 }
2689 while (qemu_isspace(*p)) {
2690 p++;
2691 }
2692 if (!*p)
2693 break;
2694 if (get_str(buf, sizeof(buf), &p) < 0) {
2695 goto fail;
2696 }
Markus Armbruster70b94332015-02-13 12:50:26 +01002697 opts = qemu_opts_parse_noisily(opts_list, buf, true);
Markus Armbruster361127d2010-02-10 20:24:35 +01002698 if (!opts) {
2699 goto fail;
2700 }
2701 qemu_opts_to_qdict(opts, qdict);
2702 qemu_opts_del(opts);
2703 }
2704 break;
bellard9307c4c2004-04-04 12:57:25 +00002705 case '/':
2706 {
2707 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002708
blueswir1cd390082008-11-16 13:53:32 +00002709 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002710 p++;
2711 if (*p == '/') {
2712 /* format found */
2713 p++;
2714 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002715 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002716 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002717 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002718 count = count * 10 + (*p - '0');
2719 p++;
2720 }
2721 }
2722 size = -1;
2723 format = -1;
2724 for(;;) {
2725 switch(*p) {
2726 case 'o':
2727 case 'd':
2728 case 'u':
2729 case 'x':
2730 case 'i':
2731 case 'c':
2732 format = *p++;
2733 break;
2734 case 'b':
2735 size = 1;
2736 p++;
2737 break;
2738 case 'h':
2739 size = 2;
2740 p++;
2741 break;
2742 case 'w':
2743 size = 4;
2744 p++;
2745 break;
2746 case 'g':
2747 case 'L':
2748 size = 8;
2749 p++;
2750 break;
2751 default:
2752 goto next;
2753 }
2754 }
2755 next:
blueswir1cd390082008-11-16 13:53:32 +00002756 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00002757 monitor_printf(mon, "invalid char in format: '%c'\n",
2758 *p);
bellard9307c4c2004-04-04 12:57:25 +00002759 goto fail;
2760 }
bellard9307c4c2004-04-04 12:57:25 +00002761 if (format < 0)
2762 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002763 if (format != 'i') {
2764 /* for 'i', not specifying a size gives -1 as size */
2765 if (size < 0)
2766 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002767 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002768 }
bellard9307c4c2004-04-04 12:57:25 +00002769 default_fmt_format = format;
2770 } else {
2771 count = 1;
2772 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002773 if (format != 'i') {
2774 size = default_fmt_size;
2775 } else {
2776 size = -1;
2777 }
bellard9307c4c2004-04-04 12:57:25 +00002778 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002779 qdict_put(qdict, "count", qint_from_int(count));
2780 qdict_put(qdict, "format", qint_from_int(format));
2781 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00002782 }
2783 break;
2784 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002785 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002786 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00002787 {
blueswir1c2efc952007-09-25 17:28:42 +00002788 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00002789
blueswir1cd390082008-11-16 13:53:32 +00002790 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002791 p++;
bellard34405572004-06-08 00:55:58 +00002792 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002793 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03002794 if (*p == '\0') {
2795 typestr++;
2796 break;
2797 }
bellard34405572004-06-08 00:55:58 +00002798 } else {
2799 if (*p == '.') {
2800 p++;
blueswir1cd390082008-11-16 13:53:32 +00002801 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00002802 p++;
bellard34405572004-06-08 00:55:58 +00002803 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03002804 typestr++;
2805 break;
bellard34405572004-06-08 00:55:58 +00002806 }
2807 }
bellard13224a82006-07-14 22:03:35 +00002808 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002809 }
aliguori376253e2009-03-05 23:01:23 +00002810 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00002811 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002812 /* Check if 'i' is greater than 32-bit */
2813 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
Bandan Dasae502122015-06-03 18:38:08 -04002814 monitor_printf(mon, "\'%s\' has failed: ", cmd->name);
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002815 monitor_printf(mon, "integer is for 32-bit values\n");
2816 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002817 } else if (c == 'M') {
Luiz Capitulino91162842012-04-26 17:34:30 -03002818 if (val < 0) {
2819 monitor_printf(mon, "enter a positive value\n");
2820 goto fail;
2821 }
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002822 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002823 }
Luiz Capitulino53773582009-08-28 15:27:25 -03002824 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00002825 }
2826 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02002827 case 'o':
2828 {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01002829 int64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02002830 char *end;
2831
2832 while (qemu_isspace(*p)) {
2833 p++;
2834 }
2835 if (*typestr == '?') {
2836 typestr++;
2837 if (*p == '\0') {
2838 break;
2839 }
2840 }
Marc-André Lureau4677bb42015-09-16 18:02:56 +02002841 val = qemu_strtosz(p, &end);
Jes Sorensendbc0c672010-10-21 17:15:47 +02002842 if (val < 0) {
2843 monitor_printf(mon, "invalid size\n");
2844 goto fail;
2845 }
2846 qdict_put(qdict, key, qint_from_int(val));
2847 p = end;
2848 }
2849 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01002850 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002851 {
2852 double val;
2853
2854 while (qemu_isspace(*p))
2855 p++;
2856 if (*typestr == '?') {
2857 typestr++;
2858 if (*p == '\0') {
2859 break;
2860 }
2861 }
2862 if (get_double(mon, &val, &p) < 0) {
2863 goto fail;
2864 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02002865 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01002866 switch (*p) {
2867 case 'm':
2868 val /= 1e3; p += 2; break;
2869 case 'u':
2870 val /= 1e6; p += 2; break;
2871 case 'n':
2872 val /= 1e9; p += 2; break;
2873 }
2874 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002875 if (*p && !qemu_isspace(*p)) {
2876 monitor_printf(mon, "Unknown unit suffix\n");
2877 goto fail;
2878 }
2879 qdict_put(qdict, key, qfloat_from_double(val));
2880 }
2881 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002882 case 'b':
2883 {
2884 const char *beg;
Eric Blakefc48ffc2015-05-15 16:24:59 -06002885 bool val;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002886
2887 while (qemu_isspace(*p)) {
2888 p++;
2889 }
2890 beg = p;
2891 while (qemu_isgraph(*p)) {
2892 p++;
2893 }
2894 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06002895 val = true;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002896 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06002897 val = false;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002898 } else {
2899 monitor_printf(mon, "Expected 'on' or 'off'\n");
2900 goto fail;
2901 }
Eric Blakefc48ffc2015-05-15 16:24:59 -06002902 qdict_put(qdict, key, qbool_from_bool(val));
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002903 }
2904 break;
bellard9307c4c2004-04-04 12:57:25 +00002905 case '-':
2906 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002907 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03002908 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00002909 /* option */
ths3b46e622007-09-17 08:09:54 +00002910
bellard9307c4c2004-04-04 12:57:25 +00002911 c = *typestr++;
2912 if (c == '\0')
2913 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00002914 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002915 p++;
bellard9307c4c2004-04-04 12:57:25 +00002916 if (*p == '-') {
2917 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002918 if(c != *p) {
2919 if(!is_valid_option(p, typestr)) {
2920
2921 monitor_printf(mon, "%s: unsupported option -%c\n",
Bandan Dasae502122015-06-03 18:38:08 -04002922 cmd->name, *p);
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002923 goto fail;
2924 } else {
2925 skip_key = 1;
2926 }
bellard9307c4c2004-04-04 12:57:25 +00002927 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002928 if(skip_key) {
2929 p = tmp;
2930 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03002931 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002932 p++;
Eric Blakefc48ffc2015-05-15 16:24:59 -06002933 qdict_put(qdict, key, qbool_from_bool(true));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002934 }
bellard9307c4c2004-04-04 12:57:25 +00002935 }
bellard9307c4c2004-04-04 12:57:25 +00002936 }
2937 break;
Wenchao Xia129be002013-08-27 20:38:26 +08002938 case 'S':
2939 {
2940 /* package all remaining string */
2941 int len;
2942
2943 while (qemu_isspace(*p)) {
2944 p++;
2945 }
2946 if (*typestr == '?') {
2947 typestr++;
2948 if (*p == '\0') {
2949 /* no remaining string: NULL argument */
2950 break;
2951 }
2952 }
2953 len = strlen(p);
2954 if (len <= 0) {
2955 monitor_printf(mon, "%s: string expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002956 cmd->name);
Bandan Dase549d2a2015-06-03 18:38:10 -04002957 goto fail;
Wenchao Xia129be002013-08-27 20:38:26 +08002958 }
2959 qdict_put(qdict, key, qstring_from_str(p));
2960 p += len;
2961 }
2962 break;
bellard9307c4c2004-04-04 12:57:25 +00002963 default:
2964 bad_type:
Bandan Dasae502122015-06-03 18:38:08 -04002965 monitor_printf(mon, "%s: unknown type '%c'\n", cmd->name, c);
bellard9307c4c2004-04-04 12:57:25 +00002966 goto fail;
2967 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002968 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002969 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00002970 }
2971 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00002972 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002973 p++;
2974 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00002975 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
Bandan Dasae502122015-06-03 18:38:08 -04002976 cmd->name);
bellard9307c4c2004-04-04 12:57:25 +00002977 goto fail;
2978 }
2979
Bandan Dasae502122015-06-03 18:38:08 -04002980 return qdict;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02002981
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002982fail:
Bandan Dasae502122015-06-03 18:38:08 -04002983 QDECREF(qdict);
Anthony Liguori7267c092011-08-20 22:09:37 -05002984 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002985 return NULL;
2986}
2987
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01002988static void handle_hmp_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002989{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002990 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05002991 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002992
Bandan Dasae502122015-06-03 18:38:08 -04002993 cmd = monitor_parse_command(mon, &cmdline, mon->cmd_table);
2994 if (!cmd) {
2995 return;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002996 }
2997
Bandan Dasae502122015-06-03 18:38:08 -04002998 qdict = monitor_parse_arguments(mon, &cmdline, cmd);
2999 if (!qdict) {
Bandan Dasdd41eea2015-06-03 18:38:09 -04003000 monitor_printf(mon, "Try \"help %s\" for more information\n",
3001 cmd->name);
Bandan Dasae502122015-06-03 18:38:08 -04003002 return;
3003 }
3004
Marc-André Lureau2b9e3572016-09-12 13:19:06 +04003005 cmd->cmd(mon, qdict);
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003006 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00003007}
3008
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003009static void cmd_completion(Monitor *mon, const char *name, const char *list)
bellard81d09122004-07-14 17:21:37 +00003010{
3011 const char *p, *pstart;
3012 char cmd[128];
3013 int len;
3014
3015 p = list;
3016 for(;;) {
3017 pstart = p;
3018 p = strchr(p, '|');
3019 if (!p)
3020 p = pstart + strlen(pstart);
3021 len = p - pstart;
3022 if (len > sizeof(cmd) - 2)
3023 len = sizeof(cmd) - 2;
3024 memcpy(cmd, pstart, len);
3025 cmd[len] = '\0';
3026 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003027 readline_add_completion(mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00003028 }
3029 if (*p == '\0')
3030 break;
3031 p++;
3032 }
3033}
3034
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003035static void file_completion(Monitor *mon, const char *input)
bellard81d09122004-07-14 17:21:37 +00003036{
3037 DIR *ffs;
3038 struct dirent *d;
3039 char path[1024];
3040 char file[1024], file_prefix[1024];
3041 int input_path_len;
3042 const char *p;
3043
ths5fafdf22007-09-16 21:08:06 +00003044 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00003045 if (!p) {
3046 input_path_len = 0;
3047 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00003048 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00003049 } else {
3050 input_path_len = p - input + 1;
3051 memcpy(path, input, input_path_len);
3052 if (input_path_len > sizeof(path) - 1)
3053 input_path_len = sizeof(path) - 1;
3054 path[input_path_len] = '\0';
3055 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3056 }
Bandan Das19f2db52015-06-03 18:38:07 -04003057
bellard81d09122004-07-14 17:21:37 +00003058 ffs = opendir(path);
3059 if (!ffs)
3060 return;
3061 for(;;) {
3062 struct stat sb;
3063 d = readdir(ffs);
3064 if (!d)
3065 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09003066
3067 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
3068 continue;
3069 }
3070
bellard81d09122004-07-14 17:21:37 +00003071 if (strstart(d->d_name, file_prefix, NULL)) {
3072 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00003073 if (input_path_len < sizeof(file))
3074 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3075 d->d_name);
bellard81d09122004-07-14 17:21:37 +00003076 /* stat the file to find out if it's a directory.
3077 * In that case add a slash to speed up typing long paths
3078 */
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01003079 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
blueswir1363a37d2008-08-21 17:58:08 +00003080 pstrcat(file, sizeof(file), "/");
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01003081 }
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003082 readline_add_completion(mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003083 }
3084 }
3085 closedir(ffs);
3086}
3087
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003088static const char *next_arg_type(const char *typestr)
3089{
3090 const char *p = strchr(typestr, ':');
3091 return (p != NULL ? ++p : typestr);
3092}
3093
Hani Benhabiles40d19392014-05-07 23:41:30 +01003094static void add_completion_option(ReadLineState *rs, const char *str,
3095 const char *option)
3096{
3097 if (!str || !option) {
3098 return;
3099 }
3100 if (!strncmp(option, str, strlen(str))) {
3101 readline_add_completion(rs, option);
3102 }
3103}
3104
Hani Benhabiles13e315d2014-05-07 23:41:29 +01003105void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
3106{
3107 size_t len;
3108 ChardevBackendInfoList *list, *start;
3109
3110 if (nb_args != 2) {
3111 return;
3112 }
3113 len = strlen(str);
3114 readline_set_completion_index(rs, len);
3115
3116 start = list = qmp_query_chardev_backends(NULL);
3117 while (list) {
3118 const char *chr_name = list->value->name;
3119
3120 if (!strncmp(chr_name, str, len)) {
3121 readline_add_completion(rs, chr_name);
3122 }
3123 list = list->next;
3124 }
3125 qapi_free_ChardevBackendInfoList(start);
3126}
3127
Hani Benhabilesb162b492014-05-07 23:41:31 +01003128void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
3129{
3130 size_t len;
3131 int i;
3132
3133 if (nb_args != 2) {
3134 return;
3135 }
3136 len = strlen(str);
3137 readline_set_completion_index(rs, len);
Eric Blakef394b2e2016-07-13 21:50:23 -06003138 for (i = 0; NetClientDriver_lookup[i]; i++) {
3139 add_completion_option(rs, str, NetClientDriver_lookup[i]);
Hani Benhabilesb162b492014-05-07 23:41:31 +01003140 }
3141}
3142
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003143void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003144{
3145 GSList *list, *elt;
3146 size_t len;
3147
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003148 if (nb_args != 2) {
3149 return;
3150 }
3151
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003152 len = strlen(str);
3153 readline_set_completion_index(rs, len);
3154 list = elt = object_class_get_list(TYPE_DEVICE, false);
3155 while (elt) {
3156 const char *name;
3157 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
3158 TYPE_DEVICE);
3159 name = object_class_get_name(OBJECT_CLASS(dc));
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003160
3161 if (!dc->cannot_instantiate_with_device_add_yet
3162 && !strncmp(name, str, len)) {
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003163 readline_add_completion(rs, name);
3164 }
3165 elt = elt->next;
3166 }
3167 g_slist_free(list);
3168}
3169
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003170void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003171{
3172 GSList *list, *elt;
3173 size_t len;
3174
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003175 if (nb_args != 2) {
3176 return;
3177 }
3178
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003179 len = strlen(str);
3180 readline_set_completion_index(rs, len);
3181 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
3182 while (elt) {
3183 const char *name;
3184
3185 name = object_class_get_name(OBJECT_CLASS(elt->data));
3186 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
3187 readline_add_completion(rs, name);
3188 }
3189 elt = elt->next;
3190 }
3191 g_slist_free(list);
3192}
3193
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003194static void peripheral_device_del_completion(ReadLineState *rs,
3195 const char *str, size_t len)
3196{
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003197 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
3198 GSList *list, *item;
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003199
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003200 list = qdev_build_hotpluggable_device_list(peripheral);
3201 if (!list) {
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003202 return;
3203 }
3204
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003205 for (item = list; item; item = g_slist_next(item)) {
3206 DeviceState *dev = item->data;
3207
3208 if (dev->id && !strncmp(str, dev->id, len)) {
3209 readline_add_completion(rs, dev->id);
3210 }
3211 }
3212
3213 g_slist_free(list);
3214}
3215
Hani Benhabiles6297d9a2014-05-07 23:41:28 +01003216void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3217{
3218 size_t len;
3219 ChardevInfoList *list, *start;
3220
3221 if (nb_args != 2) {
3222 return;
3223 }
3224 len = strlen(str);
3225 readline_set_completion_index(rs, len);
3226
3227 start = list = qmp_query_chardev(NULL);
3228 while (list) {
3229 ChardevInfo *chr = list->value;
3230
3231 if (!strncmp(chr->label, str, len)) {
3232 readline_add_completion(rs, chr->label);
3233 }
3234 list = list->next;
3235 }
3236 qapi_free_ChardevInfoList(start);
3237}
3238
Hani Benhabiles8e597772014-05-27 23:39:30 +01003239static void ringbuf_completion(ReadLineState *rs, const char *str)
3240{
3241 size_t len;
3242 ChardevInfoList *list, *start;
3243
3244 len = strlen(str);
3245 readline_set_completion_index(rs, len);
3246
3247 start = list = qmp_query_chardev(NULL);
3248 while (list) {
3249 ChardevInfo *chr_info = list->value;
3250
3251 if (!strncmp(chr_info->label, str, len)) {
3252 CharDriverState *chr = qemu_chr_find(chr_info->label);
3253 if (chr && chr_is_ringbuf(chr)) {
3254 readline_add_completion(rs, chr_info->label);
3255 }
3256 }
3257 list = list->next;
3258 }
3259 qapi_free_ChardevInfoList(start);
3260}
3261
Hani Benhabiles8e597772014-05-27 23:39:30 +01003262void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
3263{
3264 if (nb_args != 2) {
3265 return;
3266 }
3267 ringbuf_completion(rs, str);
3268}
3269
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003270void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
3271{
3272 size_t len;
3273
3274 if (nb_args != 2) {
3275 return;
3276 }
3277
3278 len = strlen(str);
3279 readline_set_completion_index(rs, len);
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003280 peripheral_device_del_completion(rs, str, len);
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003281}
3282
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003283void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003284{
3285 ObjectPropertyInfoList *list, *start;
3286 size_t len;
3287
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003288 if (nb_args != 2) {
3289 return;
3290 }
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003291 len = strlen(str);
3292 readline_set_completion_index(rs, len);
3293
3294 start = list = qmp_qom_list("/objects", NULL);
3295 while (list) {
3296 ObjectPropertyInfo *info = list->value;
3297
3298 if (!strncmp(info->type, "child<", 5)
3299 && !strncmp(info->name, str, len)) {
3300 readline_add_completion(rs, info->name);
3301 }
3302 list = list->next;
3303 }
3304 qapi_free_ObjectPropertyInfoList(start);
3305}
3306
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003307void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
3308{
3309 int i;
3310 char *sep;
3311 size_t len;
3312
3313 if (nb_args != 2) {
3314 return;
3315 }
3316 sep = strrchr(str, '-');
3317 if (sep) {
3318 str = sep + 1;
3319 }
3320 len = strlen(str);
3321 readline_set_completion_index(rs, len);
Eric Blake7fb1cf12015-11-18 01:52:57 -07003322 for (i = 0; i < Q_KEY_CODE__MAX; i++) {
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003323 if (!strncmp(str, QKeyCode_lookup[i], len)) {
3324 readline_add_completion(rs, QKeyCode_lookup[i]);
3325 }
3326 }
3327}
3328
Hani Benhabiles40d19392014-05-07 23:41:30 +01003329void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
3330{
3331 size_t len;
3332
3333 len = strlen(str);
3334 readline_set_completion_index(rs, len);
3335 if (nb_args == 2) {
Jason Wangeaed4832015-04-23 14:21:38 +08003336 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles40d19392014-05-07 23:41:30 +01003337 int count, i;
3338 count = qemu_find_net_clients_except(NULL, ncs,
Eric Blakef394b2e2016-07-13 21:50:23 -06003339 NET_CLIENT_DRIVER_NONE,
Jason Wangeaed4832015-04-23 14:21:38 +08003340 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003341 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles40d19392014-05-07 23:41:30 +01003342 const char *name = ncs[i]->name;
3343 if (!strncmp(str, name, len)) {
3344 readline_add_completion(rs, name);
3345 }
3346 }
3347 } else if (nb_args == 3) {
3348 add_completion_option(rs, str, "on");
3349 add_completion_option(rs, str, "off");
3350 }
3351}
3352
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003353void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
3354{
3355 int len, count, i;
Jason Wangeaed4832015-04-23 14:21:38 +08003356 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003357
3358 if (nb_args != 2) {
3359 return;
3360 }
3361
3362 len = strlen(str);
3363 readline_set_completion_index(rs, len);
Eric Blakef394b2e2016-07-13 21:50:23 -06003364 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_DRIVER_NIC,
Jason Wangeaed4832015-04-23 14:21:38 +08003365 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003366 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003367 QemuOpts *opts;
3368 const char *name = ncs[i]->name;
3369 if (strncmp(str, name, len)) {
3370 continue;
3371 }
3372 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
3373 if (opts) {
3374 readline_add_completion(rs, name);
3375 }
3376 }
3377}
3378
Lluís Vilanovabd712112016-07-11 12:53:51 +02003379void info_trace_events_completion(ReadLineState *rs, int nb_args, const char *str)
3380{
3381 size_t len;
3382
3383 len = strlen(str);
3384 readline_set_completion_index(rs, len);
3385 if (nb_args == 2) {
3386 TraceEventID id;
3387 for (id = 0; id < trace_event_count(); id++) {
3388 const char *event_name = trace_event_get_name(trace_event_id(id));
3389 if (!strncmp(str, event_name, len)) {
3390 readline_add_completion(rs, event_name);
3391 }
3392 }
3393 }
3394}
3395
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003396void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
3397{
3398 size_t len;
3399
3400 len = strlen(str);
3401 readline_set_completion_index(rs, len);
3402 if (nb_args == 2) {
3403 TraceEventID id;
3404 for (id = 0; id < trace_event_count(); id++) {
3405 const char *event_name = trace_event_get_name(trace_event_id(id));
3406 if (!strncmp(str, event_name, len)) {
3407 readline_add_completion(rs, event_name);
3408 }
3409 }
3410 } else if (nb_args == 3) {
3411 add_completion_option(rs, str, "on");
3412 add_completion_option(rs, str, "off");
3413 }
3414}
3415
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003416void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
3417{
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003418 int i;
3419
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003420 if (nb_args != 2) {
3421 return;
3422 }
3423 readline_set_completion_index(rs, strlen(str));
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003424 for (i = 0; WatchdogExpirationAction_lookup[i]; i++) {
3425 add_completion_option(rs, str, WatchdogExpirationAction_lookup[i]);
3426 }
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003427}
3428
Hani Benhabilesc68a0402014-05-27 23:39:32 +01003429void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
3430 const char *str)
3431{
3432 size_t len;
3433
3434 len = strlen(str);
3435 readline_set_completion_index(rs, len);
3436 if (nb_args == 2) {
3437 int i;
Eric Blake7fb1cf12015-11-18 01:52:57 -07003438 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Hani Benhabilesc68a0402014-05-27 23:39:32 +01003439 const char *name = MigrationCapability_lookup[i];
3440 if (!strncmp(str, name, len)) {
3441 readline_add_completion(rs, name);
3442 }
3443 }
3444 } else if (nb_args == 3) {
3445 add_completion_option(rs, str, "on");
3446 add_completion_option(rs, str, "off");
3447 }
3448}
3449
Liang Li50e9a622015-03-23 16:32:29 +08003450void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
3451 const char *str)
3452{
3453 size_t len;
3454
3455 len = strlen(str);
3456 readline_set_completion_index(rs, len);
3457 if (nb_args == 2) {
3458 int i;
Eric Blake7fb1cf12015-11-18 01:52:57 -07003459 for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
Liang Li50e9a622015-03-23 16:32:29 +08003460 const char *name = MigrationParameter_lookup[i];
3461 if (!strncmp(str, name, len)) {
3462 readline_add_completion(rs, name);
3463 }
3464 }
3465 }
3466}
3467
Hani Benhabilese3bb5322014-05-27 23:39:34 +01003468void host_net_add_completion(ReadLineState *rs, int nb_args, const char *str)
3469{
3470 int i;
3471 size_t len;
3472 if (nb_args != 2) {
3473 return;
3474 }
3475 len = strlen(str);
3476 readline_set_completion_index(rs, len);
3477 for (i = 0; host_net_devices[i]; i++) {
3478 if (!strncmp(host_net_devices[i], str, len)) {
3479 readline_add_completion(rs, host_net_devices[i]);
3480 }
3481 }
3482}
3483
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003484void host_net_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3485{
Jason Wangeaed4832015-04-23 14:21:38 +08003486 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003487 int count, i, len;
3488
3489 len = strlen(str);
3490 readline_set_completion_index(rs, len);
3491 if (nb_args == 2) {
3492 count = qemu_find_net_clients_except(NULL, ncs,
Eric Blakef394b2e2016-07-13 21:50:23 -06003493 NET_CLIENT_DRIVER_NONE,
Jason Wangeaed4832015-04-23 14:21:38 +08003494 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003495 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003496 int id;
3497 char name[16];
3498
3499 if (net_hub_id_for_client(ncs[i], &id)) {
3500 continue;
3501 }
3502 snprintf(name, sizeof(name), "%d", id);
3503 if (!strncmp(str, name, len)) {
3504 readline_add_completion(rs, name);
3505 }
3506 }
3507 return;
3508 } else if (nb_args == 3) {
3509 count = qemu_find_net_clients_except(NULL, ncs,
Eric Blakef394b2e2016-07-13 21:50:23 -06003510 NET_CLIENT_DRIVER_NIC,
Jason Wangeaed4832015-04-23 14:21:38 +08003511 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003512 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Jason Wang2c4681f2015-02-02 15:06:38 +08003513 int id;
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003514 const char *name;
3515
Eric Blakef394b2e2016-07-13 21:50:23 -06003516 if (ncs[i]->info->type == NET_CLIENT_DRIVER_HUBPORT ||
Jason Wang2c4681f2015-02-02 15:06:38 +08003517 net_hub_id_for_client(ncs[i], &id)) {
3518 continue;
3519 }
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003520 name = ncs[i]->name;
3521 if (!strncmp(str, name, len)) {
3522 readline_add_completion(rs, name);
3523 }
3524 }
3525 return;
3526 }
3527}
3528
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003529static void vm_completion(ReadLineState *rs, const char *str)
3530{
3531 size_t len;
Kevin Wolf7c8eece2016-03-22 18:58:50 +01003532 BlockDriverState *bs;
Kevin Wolf88be7b42016-05-20 18:49:07 +02003533 BdrvNextIterator it;
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003534
3535 len = strlen(str);
3536 readline_set_completion_index(rs, len);
Kevin Wolf7c8eece2016-03-22 18:58:50 +01003537
Kevin Wolf88be7b42016-05-20 18:49:07 +02003538 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003539 SnapshotInfoList *snapshots, *snapshot;
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003540 AioContext *ctx = bdrv_get_aio_context(bs);
3541 bool ok = false;
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003542
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003543 aio_context_acquire(ctx);
3544 if (bdrv_can_snapshot(bs)) {
3545 ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0;
3546 }
3547 aio_context_release(ctx);
3548 if (!ok) {
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003549 continue;
3550 }
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003551
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003552 snapshot = snapshots;
3553 while (snapshot) {
3554 char *completion = snapshot->value->name;
3555 if (!strncmp(str, completion, len)) {
3556 readline_add_completion(rs, completion);
3557 }
3558 completion = snapshot->value->id;
3559 if (!strncmp(str, completion, len)) {
3560 readline_add_completion(rs, completion);
3561 }
3562 snapshot = snapshot->next;
3563 }
3564 qapi_free_SnapshotInfoList(snapshots);
3565 }
3566
3567}
3568
3569void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
3570{
3571 if (nb_args == 2) {
3572 vm_completion(rs, str);
3573 }
3574}
3575
3576void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
3577{
3578 if (nb_args == 2) {
3579 vm_completion(rs, str);
3580 }
3581}
3582
Wenchao Xiac35b6402013-08-27 20:38:24 +08003583static void monitor_find_completion_by_table(Monitor *mon,
3584 const mon_cmd_t *cmd_table,
3585 char **args,
3586 int nb_args)
bellard81d09122004-07-14 17:21:37 +00003587{
3588 const char *cmdname;
Wenchao Xiac35b6402013-08-27 20:38:24 +08003589 int i;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003590 const char *ptype, *str, *name;
Anthony Liguoric227f092009-10-01 16:12:16 -05003591 const mon_cmd_t *cmd;
Max Reitzda27a002016-03-16 19:54:29 +01003592 BlockBackend *blk = NULL;
bellard81d09122004-07-14 17:21:37 +00003593
bellard81d09122004-07-14 17:21:37 +00003594 if (nb_args <= 1) {
3595 /* command completion */
3596 if (nb_args == 0)
3597 cmdname = "";
3598 else
3599 cmdname = args[0];
Wenchao Xiad2674b22013-08-27 20:38:16 +08003600 readline_set_completion_index(mon->rs, strlen(cmdname));
Wenchao Xiac35b6402013-08-27 20:38:24 +08003601 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003602 cmd_completion(mon, cmdname, cmd->name);
bellard81d09122004-07-14 17:21:37 +00003603 }
3604 } else {
3605 /* find the command */
Wenchao Xiac35b6402013-08-27 20:38:24 +08003606 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Jan Kiszka03a63482010-06-16 00:38:33 +02003607 if (compare_cmd(args[0], cmd->name)) {
3608 break;
3609 }
bellard81d09122004-07-14 17:21:37 +00003610 }
Jan Kiszka03a63482010-06-16 00:38:33 +02003611 if (!cmd->name) {
Wenchao Xiac35b6402013-08-27 20:38:24 +08003612 return;
Jan Kiszka03a63482010-06-16 00:38:33 +02003613 }
3614
Wenchao Xiad903a772013-08-27 20:38:25 +08003615 if (cmd->sub_table) {
3616 /* do the job again */
Stefan Weile7ae7712015-03-08 19:30:01 +01003617 monitor_find_completion_by_table(mon, cmd->sub_table,
3618 &args[1], nb_args - 1);
3619 return;
Wenchao Xiad903a772013-08-27 20:38:25 +08003620 }
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003621 if (cmd->command_completion) {
Stefan Weile7ae7712015-03-08 19:30:01 +01003622 cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
3623 return;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003624 }
Wenchao Xiad903a772013-08-27 20:38:25 +08003625
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003626 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003627 for(i = 0; i < nb_args - 2; i++) {
3628 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003629 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003630 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003631 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003632 }
3633 }
3634 str = args[nb_args - 1];
Kevin Wolf48fe86f2014-11-12 16:24:02 +01003635 while (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02003636 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00003637 }
bellard81d09122004-07-14 17:21:37 +00003638 switch(*ptype) {
3639 case 'F':
3640 /* file completion */
Wenchao Xiad2674b22013-08-27 20:38:16 +08003641 readline_set_completion_index(mon->rs, strlen(str));
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003642 file_completion(mon, str);
bellard81d09122004-07-14 17:21:37 +00003643 break;
3644 case 'B':
3645 /* block device name completion */
Wenchao Xia599a9262013-08-27 20:38:15 +08003646 readline_set_completion_index(mon->rs, strlen(str));
Max Reitzda27a002016-03-16 19:54:29 +01003647 while ((blk = blk_next(blk)) != NULL) {
3648 name = blk_name(blk);
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003649 if (str[0] == '\0' ||
3650 !strncmp(name, str, strlen(str))) {
3651 readline_add_completion(mon->rs, name);
3652 }
3653 }
bellard81d09122004-07-14 17:21:37 +00003654 break;
bellard7fe48482004-10-09 18:08:01 +00003655 case 's':
Wenchao Xia129be002013-08-27 20:38:26 +08003656 case 'S':
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003657 if (!strcmp(cmd->name, "help|?")) {
Wenchao Xia7ca0e062013-08-27 20:38:27 +08003658 monitor_find_completion_by_table(mon, cmd_table,
3659 &args[1], nb_args - 1);
bellard7fe48482004-10-09 18:08:01 +00003660 }
3661 break;
bellard81d09122004-07-14 17:21:37 +00003662 default:
3663 break;
3664 }
3665 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08003666}
3667
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003668static void monitor_find_completion(void *opaque,
Wenchao Xiac35b6402013-08-27 20:38:24 +08003669 const char *cmdline)
3670{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003671 Monitor *mon = opaque;
Wenchao Xiac35b6402013-08-27 20:38:24 +08003672 char *args[MAX_ARGS];
3673 int nb_args, len;
3674
3675 /* 1. parse the cmdline */
3676 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
3677 return;
3678 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08003679
3680 /* if the line ends with a space, it means we want to complete the
3681 next arg */
3682 len = strlen(cmdline);
3683 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
3684 if (nb_args >= MAX_ARGS) {
3685 goto cleanup;
3686 }
3687 args[nb_args++] = g_strdup("");
3688 }
3689
3690 /* 2. auto complete according to args */
3691 monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args);
Jan Kiszka03a63482010-06-16 00:38:33 +02003692
3693cleanup:
Wenchao Xiadcc70cd2013-08-27 20:38:22 +08003694 free_cmdline_args(args, nb_args);
bellard81d09122004-07-14 17:21:37 +00003695}
3696
aliguori731b0362009-03-05 23:01:42 +00003697static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003698{
aliguori731b0362009-03-05 23:01:42 +00003699 Monitor *mon = opaque;
3700
Jan Kiszkac62313b2009-12-04 14:05:29 +01003701 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003702}
3703
Marc-André Lureaud79bedf2016-09-12 13:18:59 +04003704static bool invalid_qmp_mode(const Monitor *mon, const char *cmd,
Markus Armbruster40861822015-05-29 10:27:16 +02003705 Error **errp)
Luiz Capitulino09069b12010-02-04 18:10:06 -02003706{
Marc-André Lureaud79bedf2016-09-12 13:18:59 +04003707 bool is_cap = g_str_equal(cmd, "qmp_capabilities");
Markus Armbrusterf994b252015-03-06 19:51:51 +01003708
3709 if (is_cap && mon->qmp.in_command_mode) {
Markus Armbruster40861822015-05-29 10:27:16 +02003710 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
3711 "Capabilities negotiation is already complete, command "
Marc-André Lureaud79bedf2016-09-12 13:18:59 +04003712 "'%s' ignored", cmd);
Eric Blake2d5a8342015-04-15 09:19:23 -06003713 return true;
3714 }
Markus Armbrusterf994b252015-03-06 19:51:51 +01003715 if (!is_cap && !mon->qmp.in_command_mode) {
Markus Armbruster40861822015-05-29 10:27:16 +02003716 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
3717 "Expecting capabilities negotiation with "
Marc-André Lureaud79bedf2016-09-12 13:18:59 +04003718 "'qmp_capabilities' before command '%s'", cmd);
Eric Blake2d5a8342015-04-15 09:19:23 -06003719 return true;
3720 }
3721 return false;
Luiz Capitulino09069b12010-02-04 18:10:06 -02003722}
3723
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003724/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03003725 * Argument validation rules:
3726 *
3727 * 1. The argument must exist in cmd_args qdict
3728 * 2. The argument type must be the expected one
3729 *
3730 * Special case: If the argument doesn't exist in cmd_args and
3731 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
3732 * checking is skipped for it.
3733 */
Markus Armbruster326283a2015-03-02 18:39:09 +01003734static void check_client_args_type(const QDict *client_args,
3735 const QDict *cmd_args, int flags,
3736 Error **errp)
Luiz Capitulino4af91932010-06-22 11:44:05 -03003737{
3738 const QDictEntry *ent;
3739
3740 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
3741 QObject *obj;
3742 QString *arg_type;
3743 const QObject *client_arg = qdict_entry_value(ent);
3744 const char *client_arg_name = qdict_entry_key(ent);
3745
3746 obj = qdict_get(cmd_args, client_arg_name);
3747 if (!obj) {
3748 if (flags & QMP_ACCEPT_UNKNOWNS) {
3749 /* handler accepts unknowns */
3750 continue;
3751 }
3752 /* client arg doesn't exist */
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003753 error_setg(errp, QERR_INVALID_PARAMETER, client_arg_name);
Markus Armbruster326283a2015-03-02 18:39:09 +01003754 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003755 }
3756
3757 arg_type = qobject_to_qstring(obj);
3758 assert(arg_type != NULL);
3759
3760 /* check if argument's type is correct */
3761 switch (qstring_get_str(arg_type)[0]) {
3762 case 'F':
3763 case 'B':
3764 case 's':
3765 if (qobject_type(client_arg) != QTYPE_QSTRING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003766 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3767 client_arg_name, "string");
Markus Armbruster326283a2015-03-02 18:39:09 +01003768 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003769 }
3770 break;
3771 case 'i':
3772 case 'l':
3773 case 'M':
Jes Sorensendbc0c672010-10-21 17:15:47 +02003774 case 'o':
Luiz Capitulino4af91932010-06-22 11:44:05 -03003775 if (qobject_type(client_arg) != QTYPE_QINT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003776 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3777 client_arg_name, "int");
Markus Armbruster326283a2015-03-02 18:39:09 +01003778 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003779 }
3780 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003781 case 'T':
3782 if (qobject_type(client_arg) != QTYPE_QINT &&
3783 qobject_type(client_arg) != QTYPE_QFLOAT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003784 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3785 client_arg_name, "number");
Markus Armbruster326283a2015-03-02 18:39:09 +01003786 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003787 }
3788 break;
3789 case 'b':
3790 case '-':
3791 if (qobject_type(client_arg) != QTYPE_QBOOL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003792 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3793 client_arg_name, "bool");
Markus Armbruster326283a2015-03-02 18:39:09 +01003794 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003795 }
3796 break;
3797 case 'O':
3798 assert(flags & QMP_ACCEPT_UNKNOWNS);
3799 break;
Paolo Bonzinib9f89782012-03-22 12:51:11 +01003800 case 'q':
3801 /* Any QObject can be passed. */
3802 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003803 case '/':
3804 case '.':
3805 /*
3806 * These types are not supported by QMP and thus are not
3807 * handled here. Fall through.
3808 */
3809 default:
3810 abort();
3811 }
3812 }
Luiz Capitulino4af91932010-06-22 11:44:05 -03003813}
3814
3815/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003816 * - Check if the client has passed all mandatory args
3817 * - Set special flags for argument validation
3818 */
Markus Armbruster326283a2015-03-02 18:39:09 +01003819static void check_mandatory_args(const QDict *cmd_args,
3820 const QDict *client_args, int *flags,
3821 Error **errp)
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003822{
3823 const QDictEntry *ent;
3824
3825 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
3826 const char *cmd_arg_name = qdict_entry_key(ent);
3827 QString *type = qobject_to_qstring(qdict_entry_value(ent));
3828 assert(type != NULL);
3829
3830 if (qstring_get_str(type)[0] == 'O') {
3831 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
3832 *flags |= QMP_ACCEPT_UNKNOWNS;
3833 } else if (qstring_get_str(type)[0] != '-' &&
3834 qstring_get_str(type)[1] != '?' &&
3835 !qdict_haskey(client_args, cmd_arg_name)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003836 error_setg(errp, QERR_MISSING_PARAMETER, cmd_arg_name);
Markus Armbruster326283a2015-03-02 18:39:09 +01003837 return;
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003838 }
3839 }
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003840}
3841
3842static QDict *qdict_from_args_type(const char *args_type)
3843{
3844 int i;
3845 QDict *qdict;
3846 QString *key, *type, *cur_qs;
3847
3848 assert(args_type != NULL);
3849
3850 qdict = qdict_new();
3851
3852 if (args_type == NULL || args_type[0] == '\0') {
3853 /* no args, empty qdict */
3854 goto out;
3855 }
3856
3857 key = qstring_new();
3858 type = qstring_new();
3859
3860 cur_qs = key;
3861
3862 for (i = 0;; i++) {
3863 switch (args_type[i]) {
3864 case ',':
3865 case '\0':
3866 qdict_put(qdict, qstring_get_str(key), type);
3867 QDECREF(key);
3868 if (args_type[i] == '\0') {
3869 goto out;
3870 }
3871 type = qstring_new(); /* qdict has ref */
3872 cur_qs = key = qstring_new();
3873 break;
3874 case ':':
3875 cur_qs = type;
3876 break;
3877 default:
3878 qstring_append_chr(cur_qs, args_type[i]);
3879 break;
3880 }
3881 }
3882
3883out:
3884 return qdict;
3885}
3886
3887/*
3888 * Client argument checking rules:
3889 *
3890 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03003891 * 2. Each argument provided by the client must be expected
3892 * 3. Each argument provided by the client must have the type expected
3893 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003894 */
Markus Armbruster326283a2015-03-02 18:39:09 +01003895static void qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args,
3896 Error **errp)
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003897{
Markus Armbruster326283a2015-03-02 18:39:09 +01003898 Error *err = NULL;
3899 int flags;
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003900 QDict *cmd_args;
3901
3902 cmd_args = qdict_from_args_type(cmd->args_type);
3903
3904 flags = 0;
Markus Armbruster326283a2015-03-02 18:39:09 +01003905 check_mandatory_args(cmd_args, client_args, &flags, &err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003906 if (err) {
3907 goto out;
3908 }
3909
Markus Armbruster326283a2015-03-02 18:39:09 +01003910 check_client_args_type(client_args, cmd_args, flags, &err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003911
3912out:
Markus Armbruster326283a2015-03-02 18:39:09 +01003913 error_propagate(errp, err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003914 QDECREF(cmd_args);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003915}
3916
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003917/*
3918 * Input object checking rules
3919 *
3920 * 1. Input object must be a dict
3921 * 2. The "execute" key must exist
3922 * 3. The "execute" key must be a string
3923 * 4. If the "arguments" key exists, it must be a dict
3924 * 5. If the "id" key exists, it can be anything (ie. json-value)
3925 * 6. Any argument not listed above is considered invalid
3926 */
Markus Armbrusterba0510a2015-03-02 18:41:43 +01003927static QDict *qmp_check_input_obj(QObject *input_obj, Error **errp)
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003928{
3929 const QDictEntry *ent;
3930 int has_exec_key = 0;
3931 QDict *input_dict;
3932
3933 if (qobject_type(input_obj) != QTYPE_QDICT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003934 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "object");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003935 return NULL;
3936 }
3937
3938 input_dict = qobject_to_qdict(input_obj);
3939
3940 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
3941 const char *arg_name = qdict_entry_key(ent);
3942 const QObject *arg_obj = qdict_entry_value(ent);
3943
3944 if (!strcmp(arg_name, "execute")) {
3945 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003946 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
3947 "execute", "string");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003948 return NULL;
3949 }
3950 has_exec_key = 1;
3951 } else if (!strcmp(arg_name, "arguments")) {
3952 if (qobject_type(arg_obj) != QTYPE_QDICT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003953 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
3954 "arguments", "object");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003955 return NULL;
3956 }
Markus Armbruster779cec42015-06-08 10:44:30 +02003957 } else if (!strcmp(arg_name, "id")) {
3958 /* Any string is acceptable as "id", so nothing to check */
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003959 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003960 error_setg(errp, QERR_QMP_EXTRA_MEMBER, arg_name);
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003961 return NULL;
3962 }
3963 }
3964
3965 if (!has_exec_key) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003966 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "execute");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003967 return NULL;
3968 }
3969
3970 return input_dict;
3971}
3972
Paolo Bonzini95385fe2015-11-25 22:23:31 +01003973static void handle_qmp_command(JSONMessageParser *parser, GQueue *tokens)
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003974{
Markus Armbruster326283a2015-03-02 18:39:09 +01003975 Error *local_err = NULL;
Markus Armbruster84add862015-03-05 16:45:15 +01003976 QObject *obj, *data;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003977 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003978 const mon_cmd_t *cmd;
Marc-André Lureauc8235012016-09-12 13:19:04 +04003979 QmpCommand *qcmd;
Luiz Capitulino40e5a012011-10-21 16:15:31 -02003980 const char *cmd_name;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003981 Monitor *mon = cur_mon;
3982
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03003983 args = input = NULL;
Markus Armbruster84add862015-03-05 16:45:15 +01003984 data = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003985
3986 obj = json_parser_parse(tokens, NULL);
3987 if (!obj) {
3988 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003989 error_setg(&local_err, QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003990 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003991 }
3992
Markus Armbrusterba0510a2015-03-02 18:41:43 +01003993 input = qmp_check_input_obj(obj, &local_err);
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003994 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003995 qobject_decref(obj);
3996 goto err_out;
3997 }
3998
Markus Armbruster74358f22015-03-06 19:35:59 +01003999 mon->qmp.id = qdict_get(input, "id");
4000 qobject_incref(mon->qmp.id);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004001
Luiz Capitulino0bbab462010-05-31 17:32:50 -03004002 cmd_name = qdict_get_str(input, "execute");
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +01004003 trace_handle_qmp_command(mon, cmd_name);
Anthony Liguorie3193602011-09-02 12:34:47 -05004004 cmd = qmp_find_cmd(cmd_name);
Marc-André Lureauc8235012016-09-12 13:19:04 +04004005 qcmd = qmp_find_command(cmd_name);
4006 if (!qcmd || !cmd) {
Markus Armbruster710aec92015-03-06 11:28:00 +01004007 error_set(&local_err, ERROR_CLASS_COMMAND_NOT_FOUND,
4008 "The command %s has not been found", cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004009 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004010 }
Marc-André Lureaud79bedf2016-09-12 13:18:59 +04004011 if (invalid_qmp_mode(mon, cmd_name, &local_err)) {
Eric Blake2d5a8342015-04-15 09:19:23 -06004012 goto err_out;
4013 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004014
4015 obj = qdict_get(input, "arguments");
4016 if (!obj) {
4017 args = qdict_new();
4018 } else {
4019 args = qobject_to_qdict(obj);
4020 QINCREF(args);
4021 }
4022
Markus Armbruster326283a2015-03-02 18:39:09 +01004023 qmp_check_client_args(cmd, args, &local_err);
4024 if (local_err) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004025 goto err_out;
4026 }
4027
Marc-André Lureauc8235012016-09-12 13:19:04 +04004028 qcmd->fn(args, &data, &local_err);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004029
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004030err_out:
Markus Armbruster710aec92015-03-06 11:28:00 +01004031 monitor_protocol_emitter(mon, data, local_err);
Markus Armbruster84add862015-03-05 16:45:15 +01004032 qobject_decref(data);
Markus Armbruster3a81a102015-10-29 12:15:09 +01004033 error_free(local_err);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004034 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004035 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004036}
4037
Markus Armbrusterc83fe232015-03-06 19:20:51 +01004038static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004039{
4040 Monitor *old_mon = cur_mon;
4041
4042 cur_mon = opaque;
4043
Markus Armbruster74358f22015-03-06 19:35:59 +01004044 json_message_parser_feed(&cur_mon->qmp.parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004045
4046 cur_mon = old_mon;
4047}
4048
aliguori731b0362009-03-05 23:01:42 +00004049static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00004050{
aliguori731b0362009-03-05 23:01:42 +00004051 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00004052 int i;
aliguori376253e2009-03-05 23:01:23 +00004053
aliguori731b0362009-03-05 23:01:42 +00004054 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00004055
aliguoricde76ee2009-03-05 23:01:51 +00004056 if (cur_mon->rs) {
4057 for (i = 0; i < size; i++)
4058 readline_handle_byte(cur_mon->rs, buf[i]);
4059 } else {
4060 if (size == 0 || buf[size - 1] != 0)
4061 monitor_printf(cur_mon, "corrupted command\n");
4062 else
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01004063 handle_hmp_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00004064 }
aliguori731b0362009-03-05 23:01:42 +00004065
4066 cur_mon = old_mon;
4067}
aliguorid8f44602008-10-06 13:52:44 +00004068
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004069static void monitor_command_cb(void *opaque, const char *cmdline,
4070 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00004071{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004072 Monitor *mon = opaque;
4073
aliguori731b0362009-03-05 23:01:42 +00004074 monitor_suspend(mon);
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01004075 handle_hmp_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00004076 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00004077}
4078
aliguoricde76ee2009-03-05 23:01:51 +00004079int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004080{
aliguoricde76ee2009-03-05 23:01:51 +00004081 if (!mon->rs)
4082 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00004083 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00004084 return 0;
aliguorid8f44602008-10-06 13:52:44 +00004085}
4086
aliguori376253e2009-03-05 23:01:23 +00004087void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004088{
aliguoricde76ee2009-03-05 23:01:51 +00004089 if (!mon->rs)
4090 return;
aliguori731b0362009-03-05 23:01:42 +00004091 if (--mon->suspend_cnt == 0)
4092 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00004093}
4094
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004095static QObject *get_qmp_greeting(void)
4096{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03004097 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004098
Markus Armbruster7fad30f2015-09-16 13:06:19 +02004099 qmp_marshal_query_version(NULL, &ver, NULL);
Marc-André Lureauc8235012016-09-12 13:19:04 +04004100
4101 return qobject_from_jsonf("{'QMP': {'version': %p, 'capabilities': []}}",
4102 ver);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004103}
4104
Markus Armbrusterc83fe232015-03-06 19:20:51 +01004105static void monitor_qmp_event(void *opaque, int event)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004106{
Luiz Capitulino47116d12010-02-08 17:01:30 -02004107 QObject *data;
4108 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004109
Luiz Capitulino47116d12010-02-08 17:01:30 -02004110 switch (event) {
4111 case CHR_EVENT_OPENED:
Markus Armbrusterf994b252015-03-06 19:51:51 +01004112 mon->qmp.in_command_mode = false;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004113 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004114 monitor_json_emitter(mon, data);
4115 qobject_decref(data);
Corey Bryantefb87c12012-08-14 16:43:48 -04004116 mon_refcount++;
Luiz Capitulino47116d12010-02-08 17:01:30 -02004117 break;
4118 case CHR_EVENT_CLOSED:
Markus Armbruster74358f22015-03-06 19:35:59 +01004119 json_message_parser_destroy(&mon->qmp.parser);
4120 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Corey Bryantefb87c12012-08-14 16:43:48 -04004121 mon_refcount--;
4122 monitor_fdsets_cleanup();
Luiz Capitulino47116d12010-02-08 17:01:30 -02004123 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004124 }
4125}
4126
aliguori731b0362009-03-05 23:01:42 +00004127static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004128{
aliguori376253e2009-03-05 23:01:23 +00004129 Monitor *mon = opaque;
4130
aliguori2724b182009-03-05 23:01:47 +00004131 switch (event) {
4132 case CHR_EVENT_MUX_IN:
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02004133 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004134 mon->mux_out = 0;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02004135 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004136 if (mon->reset_seen) {
4137 readline_restart(mon->rs);
4138 monitor_resume(mon);
4139 monitor_flush(mon);
4140 } else {
4141 mon->suspend_cnt = 0;
4142 }
aliguori2724b182009-03-05 23:01:47 +00004143 break;
ths86e94de2007-01-05 22:01:59 +00004144
aliguori2724b182009-03-05 23:01:47 +00004145 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004146 if (mon->reset_seen) {
4147 if (mon->suspend_cnt == 0) {
4148 monitor_printf(mon, "\n");
4149 }
4150 monitor_flush(mon);
4151 monitor_suspend(mon);
4152 } else {
4153 mon->suspend_cnt++;
4154 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02004155 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004156 mon->mux_out = 1;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02004157 qemu_mutex_unlock(&mon->out_lock);
aliguori2724b182009-03-05 23:01:47 +00004158 break;
4159
Amit Shahb6b8df52009-10-07 18:31:16 +05304160 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004161 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4162 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004163 if (!mon->mux_out) {
Stratos Psomadakise5554e22014-09-15 15:34:57 +03004164 readline_restart(mon->rs);
aliguori2724b182009-03-05 23:01:47 +00004165 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004166 }
4167 mon->reset_seen = 1;
Corey Bryantefb87c12012-08-14 16:43:48 -04004168 mon_refcount++;
4169 break;
4170
4171 case CHR_EVENT_CLOSED:
4172 mon_refcount--;
4173 monitor_fdsets_cleanup();
aliguori2724b182009-03-05 23:01:47 +00004174 break;
4175 }
ths86e94de2007-01-05 22:01:59 +00004176}
4177
Wayne Xia816f8922011-10-12 11:32:41 +08004178static int
4179compare_mon_cmd(const void *a, const void *b)
4180{
4181 return strcmp(((const mon_cmd_t *)a)->name,
4182 ((const mon_cmd_t *)b)->name);
4183}
4184
4185static void sortcmdlist(void)
4186{
4187 int array_num;
4188 int elem_size = sizeof(mon_cmd_t);
4189
4190 array_num = sizeof(mon_cmds)/elem_size-1;
4191 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
4192
4193 array_num = sizeof(info_cmds)/elem_size-1;
4194 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
4195}
4196
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004197/* These functions just adapt the readline interface in a typesafe way. We
4198 * could cast function pointers but that discards compiler checks.
4199 */
Stefan Weild5d15072014-01-25 18:18:23 +01004200static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
4201 const char *fmt, ...)
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004202{
4203 va_list ap;
4204 va_start(ap, fmt);
4205 monitor_vprintf(opaque, fmt, ap);
4206 va_end(ap);
4207}
4208
4209static void monitor_readline_flush(void *opaque)
4210{
4211 monitor_flush(opaque);
4212}
4213
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004214static void __attribute__((constructor)) monitor_lock_init(void)
4215{
4216 qemu_mutex_init(&monitor_lock);
4217}
4218
aliguori731b0362009-03-05 23:01:42 +00004219void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004220{
aliguori731b0362009-03-05 23:01:42 +00004221 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004222 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004223
4224 if (is_first_init) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +02004225 monitor_qapi_event_init();
Wenchao Xiad0383172013-08-27 20:38:18 +08004226 sortcmdlist();
ths20d8a3e2007-02-18 17:04:49 +00004227 is_first_init = 0;
4228 }
aliguori87127162009-03-05 23:01:29 +00004229
Wenchao Xiab01fe892013-08-27 20:38:19 +08004230 mon = g_malloc(sizeof(*mon));
4231 monitor_data_init(mon);
ths20d8a3e2007-02-18 17:04:49 +00004232
aliguori87127162009-03-05 23:01:29 +00004233 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00004234 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004235 if (flags & MONITOR_USE_READLINE) {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004236 mon->rs = readline_init(monitor_readline_printf,
4237 monitor_readline_flush,
4238 mon,
4239 monitor_find_completion);
aliguoricde76ee2009-03-05 23:01:51 +00004240 monitor_read_command(mon, 0);
4241 }
aliguori87127162009-03-05 23:01:29 +00004242
Markus Armbruster9f3982f2015-03-06 19:56:38 +01004243 if (monitor_is_qmp(mon)) {
Markus Armbrusterc83fe232015-03-06 19:20:51 +01004244 qemu_chr_add_handlers(chr, monitor_can_read, monitor_qmp_read,
4245 monitor_qmp_event, mon);
Anthony Liguori15f31512011-08-15 11:17:35 -05004246 qemu_chr_fe_set_echo(chr, true);
Markus Armbruster74358f22015-03-06 19:35:59 +01004247 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004248 } else {
4249 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4250 monitor_event, mon);
4251 }
aliguori87127162009-03-05 23:01:29 +00004252
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004253 qemu_mutex_lock(&monitor_lock);
Blue Swirl72cf2d42009-09-12 07:36:22 +00004254 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004255 qemu_mutex_unlock(&monitor_lock);
bellard7e2515e2004-08-01 21:52:19 +00004256}
4257
Marc-André Lureau2ef45712016-08-01 15:23:42 +04004258void monitor_cleanup(void)
4259{
4260 Monitor *mon, *next;
4261
4262 qemu_mutex_lock(&monitor_lock);
4263 QLIST_FOREACH_SAFE(mon, &mon_list, entry, next) {
4264 QLIST_REMOVE(mon, entry);
4265 monitor_data_destroy(mon);
4266 g_free(mon);
4267 }
4268 qemu_mutex_unlock(&monitor_lock);
4269}
4270
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004271static void bdrv_password_cb(void *opaque, const char *password,
4272 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00004273{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004274 Monitor *mon = opaque;
4275 BlockDriverState *bs = readline_opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004276 int ret = 0;
Markus Armbruster4d2855a2015-01-29 10:37:00 +01004277 Error *local_err = NULL;
bellard7e2515e2004-08-01 21:52:19 +00004278
Markus Armbruster4d2855a2015-01-29 10:37:00 +01004279 bdrv_add_key(bs, password, &local_err);
4280 if (local_err) {
Markus Armbruster193227f2015-12-18 16:35:06 +01004281 error_report_err(local_err);
aliguoribb5fc202009-03-05 23:01:15 +00004282 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004283 }
aliguori731b0362009-03-05 23:01:42 +00004284 if (mon->password_completion_cb)
4285 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004286
aliguori731b0362009-03-05 23:01:42 +00004287 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004288}
aliguoric0f4ce72009-03-05 23:01:01 +00004289
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004290int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
Markus Armbruster097310b2014-10-07 13:59:15 +02004291 BlockCompletionFunc *completion_cb,
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004292 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004293{
aliguoricde76ee2009-03-05 23:01:51 +00004294 int err;
4295
aliguori376253e2009-03-05 23:01:23 +00004296 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4297 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00004298
aliguori731b0362009-03-05 23:01:42 +00004299 mon->password_completion_cb = completion_cb;
4300 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004301
aliguoricde76ee2009-03-05 23:01:51 +00004302 err = monitor_read_password(mon, bdrv_password_cb, bs);
4303
4304 if (err && completion_cb)
4305 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004306
4307 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00004308}
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004309
4310int monitor_read_block_device_key(Monitor *mon, const char *device,
Markus Armbruster097310b2014-10-07 13:59:15 +02004311 BlockCompletionFunc *completion_cb,
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004312 void *opaque)
4313{
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01004314 Error *err = NULL;
Fam Zheng55606252015-03-02 19:36:46 +08004315 BlockBackend *blk;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004316
Fam Zheng55606252015-03-02 19:36:46 +08004317 blk = blk_by_name(device);
4318 if (!blk) {
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004319 monitor_printf(mon, "Device not found %s\n", device);
4320 return -1;
4321 }
Max Reitz5433c242015-10-19 17:53:29 +02004322 if (!blk_bs(blk)) {
4323 monitor_printf(mon, "Device '%s' has no medium\n", device);
4324 return -1;
4325 }
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004326
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01004327 bdrv_add_key(blk_bs(blk), NULL, &err);
4328 if (err) {
4329 error_free(err);
4330 return monitor_read_bdrv_key_start(mon, blk_bs(blk), completion_cb, opaque);
4331 }
4332
4333 if (completion_cb) {
4334 completion_cb(opaque, 0);
4335 }
4336 return 0;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004337}
Paolo Bonzini4d454572012-11-26 16:03:42 +01004338
4339QemuOptsList qemu_mon_opts = {
4340 .name = "mon",
4341 .implied_opt_name = "chardev",
4342 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
4343 .desc = {
4344 {
4345 .name = "mode",
4346 .type = QEMU_OPT_STRING,
4347 },{
4348 .name = "chardev",
4349 .type = QEMU_OPT_STRING,
4350 },{
4351 .name = "default",
4352 .type = QEMU_OPT_BOOL,
4353 },{
4354 .name = "pretty",
4355 .type = QEMU_OPT_BOOL,
4356 },
4357 { /* end of list */ }
4358 },
4359};
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004360
4361#ifndef TARGET_I386
4362void qmp_rtc_reset_reinjection(Error **errp)
4363{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004364 error_setg(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection");
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004365}
4366#endif
Jason J. Herne7ee0c3e2015-06-26 14:03:16 -04004367
4368#ifndef TARGET_S390X
4369void qmp_dump_skeys(const char *filename, Error **errp)
4370{
4371 error_setg(errp, QERR_FEATURE_DISABLED, "dump-skeys");
4372}
4373#endif
Peter Xuae50a772016-03-30 17:27:24 +01004374
4375#ifndef TARGET_ARM
4376GICCapabilityList *qmp_query_gic_capabilities(Error **errp)
4377{
4378 error_setg(errp, QERR_FEATURE_DISABLED, "query-gic-capabilities");
4379 return NULL;
4380}
4381#endif
Igor Mammedovd4633542016-06-10 06:29:06 +05304382
4383HotpluggableCPUList *qmp_query_hotpluggable_cpus(Error **errp)
4384{
4385 MachineState *ms = MACHINE(qdev_get_machine());
4386 MachineClass *mc = MACHINE_GET_CLASS(ms);
4387
4388 if (!mc->query_hotpluggable_cpus) {
4389 error_setg(errp, QERR_FEATURE_DISABLED, "query-hotpluggable-cpus");
4390 return NULL;
4391 }
4392
4393 return mc->query_hotpluggable_cpus(ms);
4394}