blob: dde1f220a1f131ba8286a906e23604481957fdf0 [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 */
blueswir1511d2b12009-03-07 15:32:56 +000024#include <dirent.h>
pbrook87ecb682007-11-17 17:14:51 +000025#include "hw/hw.h"
Paolo Bonzinib4a42f82013-02-04 11:37:52 +010026#include "monitor/qdev.h"
pbrook87ecb682007-11-17 17:14:51 +000027#include "hw/usb.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010028#include "hw/i386/pc.h"
Michael S. Tsirkina2cb15b2012-12-12 14:24:50 +020029#include "hw/pci/pci.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010030#include "sysemu/watchdog.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +020031#include "hw/loader.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010032#include "exec/gdbstub.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020033#include "net/net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000034#include "net/slirp.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020035#include "sysemu/char.h"
Gerd Hoffmann75721502010-10-07 12:22:54 +020036#include "ui/qemu-spice.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010037#include "sysemu/sysemu.h"
Eduardo Habkoste35704b2015-02-08 16:51:16 -020038#include "sysemu/numa.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010039#include "monitor/monitor.h"
Stefan Hajnoczi0150cd82013-11-14 11:54:15 +010040#include "qemu/readline.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010041#include "ui/console.h"
Gerd Hoffmannc751a742013-12-04 15:02:28 +010042#include "ui/input.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010043#include "sysemu/blockdev.h"
pbrook87ecb682007-11-17 17:14:51 +000044#include "audio/audio.h"
Paolo Bonzini76cad712012-10-24 11:12:21 +020045#include "disas/disas.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010046#include "sysemu/balloon.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010047#include "qemu/timer.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010048#include "migration/migration.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010049#include "sysemu/kvm.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010050#include "qemu/acl.h"
Paolo Bonzinibdee56f2013-04-02 18:28:41 +020051#include "sysemu/tpm.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010052#include "qapi/qmp/qerror.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010053#include "qapi/qmp/qint.h"
54#include "qapi/qmp/qfloat.h"
55#include "qapi/qmp/qlist.h"
56#include "qapi/qmp/qbool.h"
57#include "qapi/qmp/qstring.h"
58#include "qapi/qmp/qjson.h"
59#include "qapi/qmp/json-streamer.h"
60#include "qapi/qmp/json-parser.h"
Hani Benhabiles1094fd32014-02-06 23:30:13 +010061#include <qom/object_interfaces.h>
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010062#include "qemu/osdep.h"
Blue Swirl2b41f102011-06-19 20:38:22 +000063#include "cpu.h"
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +010064#include "trace.h"
Lluís31965ae2011-08-31 20:31:24 +020065#include "trace/control.h"
Pavel Butsykinbf957282015-09-10 18:38:59 +030066#include "monitor/hmp-target.h"
Lluís6d8a7642011-08-31 20:30:43 +020067#ifdef CONFIG_TRACE_SIMPLE
Lluís31965ae2011-08-31 20:31:24 +020068#include "trace/simple.h"
Prerna Saxena22890ab2010-06-24 17:04:53 +053069#endif
Paolo Bonzini022c62c2012-12-17 18:19:49 +010070#include "exec/memory.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050071#include "qmp-commands.h"
72#include "hmp.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010073#include "qemu/thread.h"
Hani Benhabilesb21631f2014-05-27 23:39:37 +010074#include "block/qapi.h"
Wenchao Xia43a14cf2014-06-18 08:43:31 +020075#include "qapi/qmp-event.h"
76#include "qapi-event.h"
Fam Zheng55606252015-03-02 19:36:46 +080077#include "sysemu/block-backend.h"
ths6a5bd302007-12-03 17:05:38 +000078
Markus Armbruster1ce6be22015-02-06 14:18:24 +010079/* for hmp_info_irq/pic */
Jan Kiszka661f1922011-10-16 11:53:13 +020080#if defined(TARGET_SPARC)
Paolo Bonzini0d09e412013-02-05 17:06:20 +010081#include "hw/sparc/sun4m.h"
Jan Kiszka661f1922011-10-16 11:53:13 +020082#endif
Paolo Bonzini0d09e412013-02-05 17:06:20 +010083#include "hw/lm32/lm32_pic.h"
Jan Kiszka661f1922011-10-16 11:53:13 +020084
Jason J. Hernea4538a52015-06-26 14:07:21 -040085#if defined(TARGET_S390X)
86#include "hw/s390x/storage-keys.h"
87#endif
88
bellard9307c4c2004-04-04 12:57:25 +000089/*
90 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000091 *
bellard9307c4c2004-04-04 12:57:25 +000092 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000093 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000094 * 's' string (accept optional quote)
Wenchao Xia129be002013-08-27 20:38:26 +080095 * 'S' it just appends the rest of the string (accept optional quote)
Markus Armbruster361127d2010-02-10 20:24:35 +010096 * 'O' option string of the form NAME=VALUE,...
97 * parsed according to QemuOptsList given by its name
98 * Example: 'device:O' uses qemu_device_opts.
99 * Restriction: only lists with empty desc are supported
100 * TODO lift the restriction
bellard92a31b12005-02-10 22:00:52 +0000101 * 'i' 32 bit integer
102 * 'l' target long (32 or 64 bit)
Luiz Capitulino91162842012-04-26 17:34:30 -0300103 * 'M' Non-negative target long (32 or 64 bit), in user mode the
104 * value is multiplied by 2^20 (think Mebibyte)
Jes Sorensendbc0c672010-10-21 17:15:47 +0200105 * 'o' octets (aka bytes)
Kevin Wolf5e009842013-06-05 14:19:27 +0200106 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
107 * K, k suffix, which multiplies the value by 2^60 for suffixes E
108 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
109 * 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 +0100110 * 'T' double
111 * user mode accepts an optional ms, us, ns suffix,
112 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +0000113 * '/' optional gdb-like print format (like "/10x")
114 *
Luiz Capitulinofb466602009-08-28 15:27:27 -0300115 * '?' optional type (for all types, except '/')
116 * '.' other form of optional type (for 'i' and 'l')
Markus Armbruster942cd1f2010-03-26 09:07:09 +0100117 * 'b' boolean
118 * user mode accepts "on" or "off"
Luiz Capitulinofb466602009-08-28 15:27:27 -0300119 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +0000120 *
121 */
122
Anthony Liguoric227f092009-10-01 16:12:16 -0500123typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +0000124 const char *name;
bellard9307c4c2004-04-04 12:57:25 +0000125 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +0000126 const char *params;
127 const char *help;
Luiz Capitulino910df892009-10-07 13:41:51 -0300128 union {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -0300129 void (*cmd)(Monitor *mon, const QDict *qdict);
Markus Armbruster485febc2015-03-13 17:25:50 +0100130 void (*cmd_new)(QDict *params, QObject **ret_data, Error **errp);
Luiz Capitulino910df892009-10-07 13:41:51 -0300131 } mhandler;
Wenchao Xia5f3d3352013-01-14 14:06:27 +0800132 /* @sub_table is a list of 2nd level of commands. If it do not exist,
133 * mhandler should be used. If it exist, sub_table[?].mhandler should be
134 * used, and mhandler of 1st level plays the role of help function.
135 */
136 struct mon_cmd_t *sub_table;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +0100137 void (*command_completion)(ReadLineState *rs, int nb_args, const char *str);
Anthony Liguoric227f092009-10-01 16:12:16 -0500138} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000139
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100140/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500141typedef struct mon_fd_t mon_fd_t;
142struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100143 char *name;
144 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500145 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100146};
147
Corey Bryantba1c0482012-08-14 16:43:43 -0400148/* file descriptor associated with a file descriptor set */
149typedef struct MonFdsetFd MonFdsetFd;
150struct MonFdsetFd {
151 int fd;
152 bool removed;
153 char *opaque;
154 QLIST_ENTRY(MonFdsetFd) next;
155};
156
157/* file descriptor set containing fds passed via SCM_RIGHTS */
158typedef struct MonFdset MonFdset;
159struct MonFdset {
160 int64_t id;
161 QLIST_HEAD(, MonFdsetFd) fds;
Corey Bryantadb696f2012-08-14 16:43:47 -0400162 QLIST_HEAD(, MonFdsetFd) dup_fds;
Corey Bryantba1c0482012-08-14 16:43:43 -0400163 QLIST_ENTRY(MonFdset) next;
164};
165
Markus Armbruster74358f22015-03-06 19:35:59 +0100166typedef struct {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200167 QObject *id;
168 JSONMessageParser parser;
Markus Armbrusterf994b252015-03-06 19:51:51 +0100169 /*
170 * When a client connects, we're in capabilities negotiation mode.
171 * When command qmp_capabilities succeeds, we go into command
172 * mode.
173 */
174 bool in_command_mode; /* are we in command mode? */
Markus Armbruster74358f22015-03-06 19:35:59 +0100175} MonitorQMP;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200176
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100177/*
178 * To prevent flooding clients, events can be throttled. The
179 * throttling is calculated globally, rather than per-Monitor
180 * instance.
181 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200182typedef struct MonitorQAPIEventState {
183 QAPIEvent event; /* Event being tracked */
184 int64_t rate; /* Minimum time (in ns) between two events */
185 int64_t last; /* QEMU_CLOCK_REALTIME value at last emission */
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100186 QEMUTimer *timer; /* Timer for handling delayed events */
187 QObject *data; /* Event pending delayed dispatch */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200188} MonitorQAPIEventState;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100189
aliguori87127162009-03-05 23:01:29 +0000190struct Monitor {
191 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200192 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000193 int flags;
194 int suspend_cnt;
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400195 bool skip_flush;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200196
197 QemuMutex out_lock;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400198 QString *outbuf;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200199 guint out_watch;
200
201 /* Read under either BQL or out_lock, written with BQL+out_lock. */
202 int mux_out;
203
aliguori731b0362009-03-05 23:01:42 +0000204 ReadLineState *rs;
Markus Armbruster74358f22015-03-06 19:35:59 +0100205 MonitorQMP qmp;
Andreas Färbercb446ec2013-05-01 14:24:52 +0200206 CPUState *mon_cpu;
Markus Armbruster097310b2014-10-07 13:59:15 +0200207 BlockCompletionFunc *password_completion_cb;
aliguori731b0362009-03-05 23:01:42 +0000208 void *password_opaque;
Wenchao Xia77172392013-08-27 20:38:20 +0800209 mon_cmd_t *cmd_table;
Anthony Liguoric227f092009-10-01 16:12:16 -0500210 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000211 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000212};
213
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300214/* QMP checker flags */
215#define QMP_ACCEPT_UNKNOWNS 1
216
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200217/* Protects mon_list, monitor_event_state. */
218static QemuMutex monitor_lock;
219
Blue Swirl72cf2d42009-09-12 07:36:22 +0000220static QLIST_HEAD(mon_list, Monitor) mon_list;
Corey Bryantba1c0482012-08-14 16:43:43 -0400221static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
Corey Bryantefb87c12012-08-14 16:43:48 -0400222static int mon_refcount;
bellard7e2515e2004-08-01 21:52:19 +0000223
Wayne Xia816f8922011-10-12 11:32:41 +0800224static mon_cmd_t mon_cmds[];
225static mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000226
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300227static const mon_cmd_t qmp_cmds[];
228
Markus Armbruster8631b602010-02-18 11:41:55 +0100229Monitor *cur_mon;
aliguori376253e2009-03-05 23:01:23 +0000230
Stefan Hajnoczic60bf332013-11-14 11:54:14 +0100231static void monitor_command_cb(void *opaque, const char *cmdline,
232 void *readline_opaque);
aliguori83ab7952008-08-19 14:44:22 +0000233
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100234/**
235 * Is @mon a QMP monitor?
236 */
237static inline bool monitor_is_qmp(const Monitor *mon)
Luiz Capitulino418173c2009-11-26 22:58:51 -0200238{
239 return (mon->flags & MONITOR_USE_CONTROL);
240}
241
Markus Armbruster489653b2015-03-06 20:01:05 +0100242/**
243 * Is the current monitor, if any, a QMP monitor?
244 */
245bool monitor_cur_is_qmp(void)
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100246{
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100247 return cur_mon && monitor_is_qmp(cur_mon);
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100248}
249
Anthony Liguori7060b472011-09-02 12:34:50 -0500250void monitor_read_command(Monitor *mon, int show_prompt)
aliguori731b0362009-03-05 23:01:42 +0000251{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200252 if (!mon->rs)
253 return;
254
aliguori731b0362009-03-05 23:01:42 +0000255 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
256 if (show_prompt)
257 readline_show_prompt(mon->rs);
258}
bellard6a00d602005-11-21 23:25:50 +0000259
Anthony Liguori7060b472011-09-02 12:34:50 -0500260int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
261 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000262{
Markus Armbrusterbcf5d192015-03-12 17:26:46 +0100263 if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000264 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
265 /* prompt is printed on return from the command handler */
266 return 0;
267 } else {
268 monitor_printf(mon, "terminal does not support password prompting\n");
269 return -ENOTTY;
270 }
aliguoribb5fc202009-03-05 23:01:15 +0000271}
272
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200273static void monitor_flush_locked(Monitor *mon);
274
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100275static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
276 void *opaque)
277{
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200278 Monitor *mon = opaque;
279
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200280 qemu_mutex_lock(&mon->out_lock);
281 mon->out_watch = 0;
282 monitor_flush_locked(mon);
283 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100284 return FALSE;
285}
286
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200287/* Called with mon->out_lock held. */
288static void monitor_flush_locked(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000289{
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100290 int rc;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400291 size_t len;
292 const char *buf;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100293
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400294 if (mon->skip_flush) {
295 return;
296 }
297
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400298 buf = qstring_get_str(mon->outbuf);
299 len = qstring_get_length(mon->outbuf);
300
Paolo Bonzinia4cc73d2013-05-31 14:00:27 +0200301 if (len && !mon->mux_out) {
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400302 rc = qemu_chr_fe_write(mon->chr, (const uint8_t *) buf, len);
Stratos Psomadakis056f49f2014-01-27 12:30:15 +0200303 if ((rc < 0 && errno != EAGAIN) || (rc == len)) {
304 /* all flushed or error */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400305 QDECREF(mon->outbuf);
306 mon->outbuf = qstring_new();
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100307 return;
308 }
309 if (rc > 0) {
310 /* partinal write */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400311 QString *tmp = qstring_from_str(buf + rc);
312 QDECREF(mon->outbuf);
313 mon->outbuf = tmp;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100314 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200315 if (mon->out_watch == 0) {
Roger Pau Monnee02bc6d2014-05-23 17:57:49 +0200316 mon->out_watch = qemu_chr_fe_add_watch(mon->chr, G_IO_OUT|G_IO_HUP,
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200317 monitor_unblocked, mon);
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200318 }
bellard7e2515e2004-08-01 21:52:19 +0000319 }
320}
321
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200322void monitor_flush(Monitor *mon)
323{
324 qemu_mutex_lock(&mon->out_lock);
325 monitor_flush_locked(mon);
326 qemu_mutex_unlock(&mon->out_lock);
327}
328
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400329/* flush at every end of line */
aliguori376253e2009-03-05 23:01:23 +0000330static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000331{
ths60fe76f2007-12-16 03:02:09 +0000332 char c;
aliguori731b0362009-03-05 23:01:42 +0000333
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200334 qemu_mutex_lock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000335 for(;;) {
336 c = *str++;
337 if (c == '\0')
338 break;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400339 if (c == '\n') {
340 qstring_append_chr(mon->outbuf, '\r');
341 }
342 qstring_append_chr(mon->outbuf, c);
343 if (c == '\n') {
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200344 monitor_flush_locked(mon);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400345 }
bellard7e2515e2004-08-01 21:52:19 +0000346 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200347 qemu_mutex_unlock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000348}
349
aliguori376253e2009-03-05 23:01:23 +0000350void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000351{
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400352 char *buf;
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200353
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200354 if (!mon)
355 return;
356
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100357 if (monitor_is_qmp(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200358 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200359 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200360
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400361 buf = g_strdup_vprintf(fmt, ap);
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200362 monitor_puts(mon, buf);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400363 g_free(buf);
bellard7e2515e2004-08-01 21:52:19 +0000364}
365
aliguori376253e2009-03-05 23:01:23 +0000366void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000367{
368 va_list ap;
369 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000370 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000371 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000372}
373
Stefan Weil8b7968f2010-09-23 21:28:05 +0200374static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream,
375 const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000376{
377 va_list ap;
378 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000379 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000380 va_end(ap);
381 return 0;
382}
383
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200384static void monitor_json_emitter(Monitor *mon, const QObject *data)
385{
386 QString *json;
387
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200388 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
389 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200390 assert(json != NULL);
391
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200392 qstring_append_chr(json, '\n');
393 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200394
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200395 QDECREF(json);
396}
397
Markus Armbruster710aec92015-03-06 11:28:00 +0100398static QDict *build_qmp_error_dict(Error *err)
Luiz Capitulinode253f12012-07-27 16:18:16 -0300399{
400 QObject *obj;
401
Markus Armbruster710aec92015-03-06 11:28:00 +0100402 obj = qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %s } }",
403 ErrorClass_lookup[error_get_class(err)],
404 error_get_pretty(err));
Luiz Capitulinode253f12012-07-27 16:18:16 -0300405
406 return qobject_to_qdict(obj);
407}
408
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100409static void monitor_protocol_emitter(Monitor *mon, QObject *data,
Markus Armbruster710aec92015-03-06 11:28:00 +0100410 Error *err)
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200411{
412 QDict *qmp;
413
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +0100414 trace_monitor_protocol_emitter(mon);
415
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100416 if (!err) {
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200417 /* success response */
Luiz Capitulinode253f12012-07-27 16:18:16 -0300418 qmp = qdict_new();
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200419 if (data) {
420 qobject_incref(data);
421 qdict_put_obj(qmp, "return", data);
422 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200423 /* return an empty QDict by default */
424 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200425 }
426 } else {
427 /* error response */
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100428 qmp = build_qmp_error_dict(err);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200429 }
430
Markus Armbruster74358f22015-03-06 19:35:59 +0100431 if (mon->qmp.id) {
432 qdict_put_obj(qmp, "id", mon->qmp.id);
433 mon->qmp.id = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200434 }
435
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200436 monitor_json_emitter(mon, QOBJECT(qmp));
437 QDECREF(qmp);
438}
439
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200440
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200441static MonitorQAPIEventState monitor_qapi_event_state[QAPI_EVENT_MAX];
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100442
443/*
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200444 * Emits the event to every monitor instance, @event is only used for trace
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200445 * Called with monitor_lock held.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100446 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200447static void monitor_qapi_event_emit(QAPIEvent event, QObject *data)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100448{
449 Monitor *mon;
450
451 trace_monitor_protocol_event_emit(event, data);
452 QLIST_FOREACH(mon, &mon_list, entry) {
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100453 if (monitor_is_qmp(mon) && mon->qmp.in_command_mode) {
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100454 monitor_json_emitter(mon, data);
455 }
456 }
457}
458
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100459/*
460 * Queue a new event for emission to Monitor instances,
461 * applying any rate limiting if required.
462 */
463static void
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200464monitor_qapi_event_queue(QAPIEvent event, QDict *data, Error **errp)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100465{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200466 MonitorQAPIEventState *evstate;
467 assert(event < QAPI_EVENT_MAX);
Alex Blighbc72ad62013-08-21 16:03:08 +0100468 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100469
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200470 evstate = &(monitor_qapi_event_state[event]);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100471 trace_monitor_protocol_event_queue(event,
472 data,
473 evstate->rate,
474 evstate->last,
475 now);
476
477 /* Rate limit of 0 indicates no throttling */
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200478 qemu_mutex_lock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100479 if (!evstate->rate) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200480 monitor_qapi_event_emit(event, QOBJECT(data));
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100481 evstate->last = now;
482 } else {
483 int64_t delta = now - evstate->last;
484 if (evstate->data ||
485 delta < evstate->rate) {
486 /* If there's an existing event pending, replace
487 * it with the new event, otherwise schedule a
488 * timer for delayed emission
489 */
490 if (evstate->data) {
491 qobject_decref(evstate->data);
492 } else {
493 int64_t then = evstate->last + evstate->rate;
Alex Blighbc72ad62013-08-21 16:03:08 +0100494 timer_mod_ns(evstate->timer, then);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100495 }
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200496 evstate->data = QOBJECT(data);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100497 qobject_incref(evstate->data);
498 } else {
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200499 monitor_qapi_event_emit(event, QOBJECT(data));
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100500 evstate->last = now;
501 }
502 }
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200503 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100504}
505
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100506/*
507 * The callback invoked by QemuTimer when a delayed
508 * event is ready to be emitted
509 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200510static void monitor_qapi_event_handler(void *opaque)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100511{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200512 MonitorQAPIEventState *evstate = opaque;
Alex Blighbc72ad62013-08-21 16:03:08 +0100513 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100514
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100515 trace_monitor_protocol_event_handler(evstate->event,
516 evstate->data,
517 evstate->last,
518 now);
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200519 qemu_mutex_lock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100520 if (evstate->data) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200521 monitor_qapi_event_emit(evstate->event, evstate->data);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100522 qobject_decref(evstate->data);
523 evstate->data = NULL;
524 }
525 evstate->last = now;
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200526 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100527}
528
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100529/*
530 * @event: the event ID to be limited
531 * @rate: the rate limit in milliseconds
532 *
533 * Sets a rate limit on a particular event, so no
534 * more than 1 event will be emitted within @rate
535 * milliseconds
536 */
537static void
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200538monitor_qapi_event_throttle(QAPIEvent event, int64_t rate)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100539{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200540 MonitorQAPIEventState *evstate;
541 assert(event < QAPI_EVENT_MAX);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100542
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200543 evstate = &(monitor_qapi_event_state[event]);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100544
545 trace_monitor_protocol_event_throttle(event, rate);
546 evstate->event = event;
Wenchao Xia2f44a082014-06-24 16:34:00 -0700547 assert(rate * SCALE_MS <= INT64_MAX);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100548 evstate->rate = rate * SCALE_MS;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100549 evstate->last = 0;
550 evstate->data = NULL;
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200551 evstate->timer = timer_new(QEMU_CLOCK_REALTIME,
552 SCALE_MS,
553 monitor_qapi_event_handler,
554 evstate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100555}
556
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200557static void monitor_qapi_event_init(void)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100558{
Wenchao Xiae010ad82014-06-18 08:43:41 +0200559 /* Limit guest-triggerable events to 1 per second */
560 monitor_qapi_event_throttle(QAPI_EVENT_RTC_CHANGE, 1000);
Wenchao Xia99eaf092014-06-18 08:43:42 +0200561 monitor_qapi_event_throttle(QAPI_EVENT_WATCHDOG, 1000);
Wenchao Xiaaef9d312014-06-18 08:43:51 +0200562 monitor_qapi_event_throttle(QAPI_EVENT_BALLOON_CHANGE, 1000);
Wenchao Xiafe069d92014-06-18 08:43:53 +0200563 monitor_qapi_event_throttle(QAPI_EVENT_QUORUM_REPORT_BAD, 1000);
564 monitor_qapi_event_throttle(QAPI_EVENT_QUORUM_FAILURE, 1000);
Laszlo Erseke2ae6152014-06-26 17:50:02 +0200565 monitor_qapi_event_throttle(QAPI_EVENT_VSERPORT_CHANGE, 1000);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100566
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200567 qmp_event_set_func_emit(monitor_qapi_event_queue);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200568}
569
Markus Armbruster485febc2015-03-13 17:25:50 +0100570static void qmp_capabilities(QDict *params, QObject **ret_data, Error **errp)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200571{
Markus Armbruster485febc2015-03-13 17:25:50 +0100572 cur_mon->qmp.in_command_mode = true;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200573}
574
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100575static void handle_hmp_command(Monitor *mon, const char *cmdline);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200576
Wenchao Xiab01fe892013-08-27 20:38:19 +0800577static void monitor_data_init(Monitor *mon)
578{
579 memset(mon, 0, sizeof(Monitor));
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200580 qemu_mutex_init(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800581 mon->outbuf = qstring_new();
Wenchao Xia77172392013-08-27 20:38:20 +0800582 /* Use *mon_cmds by default. */
583 mon->cmd_table = mon_cmds;
Wenchao Xiab01fe892013-08-27 20:38:19 +0800584}
585
586static void monitor_data_destroy(Monitor *mon)
587{
588 QDECREF(mon->outbuf);
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200589 qemu_mutex_destroy(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800590}
591
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200592char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
593 int64_t cpu_index, Error **errp)
Luiz Capitulino0268d972010-10-22 10:08:02 -0200594{
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200595 char *output = NULL;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200596 Monitor *old_mon, hmp;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200597
Wenchao Xiab01fe892013-08-27 20:38:19 +0800598 monitor_data_init(&hmp);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400599 hmp.skip_flush = true;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200600
601 old_mon = cur_mon;
602 cur_mon = &hmp;
603
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200604 if (has_cpu_index) {
605 int ret = monitor_set_cpu(cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200606 if (ret < 0) {
607 cur_mon = old_mon;
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100608 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
609 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200610 goto out;
611 }
612 }
613
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100614 handle_hmp_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200615 cur_mon = old_mon;
616
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200617 qemu_mutex_lock(&hmp.out_lock);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400618 if (qstring_get_length(hmp.outbuf) > 0) {
619 output = g_strdup(qstring_get_str(hmp.outbuf));
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200620 } else {
621 output = g_strdup("");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200622 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200623 qemu_mutex_unlock(&hmp.out_lock);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200624
625out:
Wenchao Xiab01fe892013-08-27 20:38:19 +0800626 monitor_data_destroy(&hmp);
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200627 return output;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200628}
629
bellard9dc39cb2004-03-14 21:38:27 +0000630static int compare_cmd(const char *name, const char *list)
631{
632 const char *p, *pstart;
633 int len;
634 len = strlen(name);
635 p = list;
636 for(;;) {
637 pstart = p;
638 p = strchr(p, '|');
639 if (!p)
640 p = pstart + strlen(pstart);
641 if ((p - pstart) == len && !memcmp(pstart, name, len))
642 return 1;
643 if (*p == '\0')
644 break;
645 p++;
646 }
647 return 0;
648}
649
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800650static int get_str(char *buf, int buf_size, const char **pp)
651{
652 const char *p;
653 char *q;
654 int c;
655
656 q = buf;
657 p = *pp;
658 while (qemu_isspace(*p)) {
659 p++;
660 }
661 if (*p == '\0') {
662 fail:
663 *q = '\0';
664 *pp = p;
665 return -1;
666 }
667 if (*p == '\"') {
668 p++;
669 while (*p != '\0' && *p != '\"') {
670 if (*p == '\\') {
671 p++;
672 c = *p++;
673 switch (c) {
674 case 'n':
675 c = '\n';
676 break;
677 case 'r':
678 c = '\r';
679 break;
680 case '\\':
681 case '\'':
682 case '\"':
683 break;
684 default:
Peter Maydell71baf782015-08-19 16:20:19 +0100685 printf("unsupported escape code: '\\%c'\n", c);
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800686 goto fail;
687 }
688 if ((q - buf) < buf_size - 1) {
689 *q++ = c;
690 }
691 } else {
692 if ((q - buf) < buf_size - 1) {
693 *q++ = *p;
694 }
695 p++;
696 }
697 }
698 if (*p != '\"') {
Peter Maydell71baf782015-08-19 16:20:19 +0100699 printf("unterminated string\n");
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800700 goto fail;
701 }
702 p++;
703 } else {
704 while (*p != '\0' && !qemu_isspace(*p)) {
705 if ((q - buf) < buf_size - 1) {
706 *q++ = *p;
707 }
708 p++;
709 }
710 }
711 *q = '\0';
712 *pp = p;
713 return 0;
714}
715
716#define MAX_ARGS 16
717
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800718static void free_cmdline_args(char **args, int nb_args)
719{
720 int i;
721
722 assert(nb_args <= MAX_ARGS);
723
724 for (i = 0; i < nb_args; i++) {
725 g_free(args[i]);
726 }
727
728}
729
730/*
731 * Parse the command line to get valid args.
732 * @cmdline: command line to be parsed.
733 * @pnb_args: location to store the number of args, must NOT be NULL.
734 * @args: location to store the args, which should be freed by caller, must
735 * NOT be NULL.
736 *
737 * Returns 0 on success, negative on failure.
738 *
739 * NOTE: this parser is an approximate form of the real command parser. Number
740 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
741 * return with failure.
742 */
743static int parse_cmdline(const char *cmdline,
744 int *pnb_args, char **args)
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800745{
746 const char *p;
747 int nb_args, ret;
748 char buf[1024];
749
750 p = cmdline;
751 nb_args = 0;
752 for (;;) {
753 while (qemu_isspace(*p)) {
754 p++;
755 }
756 if (*p == '\0') {
757 break;
758 }
759 if (nb_args >= MAX_ARGS) {
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800760 goto fail;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800761 }
762 ret = get_str(buf, sizeof(buf), &p);
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800763 if (ret < 0) {
764 goto fail;
765 }
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800766 args[nb_args] = g_strdup(buf);
767 nb_args++;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800768 }
769 *pnb_args = nb_args;
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800770 return 0;
771
772 fail:
773 free_cmdline_args(args, nb_args);
774 return -1;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800775}
776
Wenchao Xia66855492013-08-27 20:38:23 +0800777static void help_cmd_dump_one(Monitor *mon,
778 const mon_cmd_t *cmd,
779 char **prefix_args,
780 int prefix_args_nb)
781{
782 int i;
783
784 for (i = 0; i < prefix_args_nb; i++) {
785 monitor_printf(mon, "%s ", prefix_args[i]);
786 }
787 monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
788}
789
790/* @args[@arg_index] is the valid command need to find in @cmds */
Anthony Liguoric227f092009-10-01 16:12:16 -0500791static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
Wenchao Xia66855492013-08-27 20:38:23 +0800792 char **args, int nb_args, int arg_index)
bellard9dc39cb2004-03-14 21:38:27 +0000793{
Anthony Liguoric227f092009-10-01 16:12:16 -0500794 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000795
Wenchao Xia66855492013-08-27 20:38:23 +0800796 /* No valid arg need to compare with, dump all in *cmds */
797 if (arg_index >= nb_args) {
798 for (cmd = cmds; cmd->name != NULL; cmd++) {
799 help_cmd_dump_one(mon, cmd, args, arg_index);
800 }
801 return;
802 }
803
804 /* Find one entry to dump */
805 for (cmd = cmds; cmd->name != NULL; cmd++) {
806 if (compare_cmd(args[arg_index], cmd->name)) {
807 if (cmd->sub_table) {
808 /* continue with next arg */
809 help_cmd_dump(mon, cmd->sub_table,
810 args, nb_args, arg_index + 1);
811 } else {
812 help_cmd_dump_one(mon, cmd, args, arg_index);
813 }
814 break;
815 }
bellard9dc39cb2004-03-14 21:38:27 +0000816 }
817}
818
aliguori376253e2009-03-05 23:01:23 +0000819static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000820{
Wenchao Xia66855492013-08-27 20:38:23 +0800821 char *args[MAX_ARGS];
822 int nb_args = 0;
823
824 /* 1. parse user input */
825 if (name) {
826 /* special case for log, directly dump and return */
827 if (!strcmp(name, "log")) {
Peter Maydell38dad9e2013-02-11 16:41:25 +0000828 const QEMULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000829 monitor_printf(mon, "Log items (comma separated):\n");
830 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
Peter Maydell38dad9e2013-02-11 16:41:25 +0000831 for (item = qemu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000832 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000833 }
Wenchao Xia66855492013-08-27 20:38:23 +0800834 return;
835 }
836
837 if (parse_cmdline(name, &nb_args, args) < 0) {
838 return;
bellardf193c792004-03-21 17:06:25 +0000839 }
bellard9dc39cb2004-03-14 21:38:27 +0000840 }
Wenchao Xia66855492013-08-27 20:38:23 +0800841
842 /* 2. dump the contents according to parsed args */
843 help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0);
844
845 free_cmdline_args(args, nb_args);
bellard9dc39cb2004-03-14 21:38:27 +0000846}
847
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300848static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300849{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300850 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300851}
852
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100853static void hmp_trace_event(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530854{
855 const char *tp_name = qdict_get_str(qdict, "name");
856 bool new_state = qdict_get_bool(qdict, "option");
Lluís Vilanova14101d02014-08-25 13:20:03 +0200857 Error *local_err = NULL;
Blue Swirlf871d682010-10-13 19:14:29 +0000858
Lluís Vilanova14101d02014-08-25 13:20:03 +0200859 qmp_trace_event_set_state(tp_name, new_state, true, true, &local_err);
860 if (local_err) {
Markus Armbruster091e38b2015-02-10 15:15:43 +0100861 error_report_err(local_err);
Blue Swirlf871d682010-10-13 19:14:29 +0000862 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530863}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100864
Michael Rothc45a8162011-10-02 08:44:37 -0500865#ifdef CONFIG_TRACE_SIMPLE
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100866static void hmp_trace_file(Monitor *mon, const QDict *qdict)
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100867{
868 const char *op = qdict_get_try_str(qdict, "op");
869 const char *arg = qdict_get_try_str(qdict, "arg");
870
871 if (!op) {
872 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
873 } else if (!strcmp(op, "on")) {
874 st_set_trace_file_enabled(true);
875 } else if (!strcmp(op, "off")) {
876 st_set_trace_file_enabled(false);
877 } else if (!strcmp(op, "flush")) {
878 st_flush_trace_buffer();
879 } else if (!strcmp(op, "set")) {
880 if (arg) {
881 st_set_trace_file(arg);
882 }
883 } else {
884 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
885 help_cmd(mon, "trace-file");
886 }
887}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530888#endif
889
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100890static void hmp_info_help(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000891{
Luiz Capitulino13c74252009-10-07 13:41:55 -0300892 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000893}
894
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300895CommandInfoList *qmp_query_commands(Error **errp)
bellard9bc9d1c2004-10-10 15:15:51 +0000896{
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300897 CommandInfoList *info, *cmd_list = NULL;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200898 const mon_cmd_t *cmd;
899
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300900 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino40e5a012011-10-21 16:15:31 -0200901 info = g_malloc0(sizeof(*info));
902 info->value = g_malloc0(sizeof(*info->value));
903 info->value->name = g_strdup(cmd->name);
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200904
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300905 info->next = cmd_list;
906 cmd_list = info;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200907 }
908
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300909 return cmd_list;
thsa36e69d2007-12-02 05:18:19 +0000910}
911
Daniel P. Berrange48608532012-05-21 17:59:51 +0100912EventInfoList *qmp_query_events(Error **errp)
913{
914 EventInfoList *info, *ev_list = NULL;
Wenchao Xia75175172014-06-18 08:43:54 +0200915 QAPIEvent e;
Daniel P. Berrange48608532012-05-21 17:59:51 +0100916
Wenchao Xia75175172014-06-18 08:43:54 +0200917 for (e = 0 ; e < QAPI_EVENT_MAX ; e++) {
918 const char *event_name = QAPIEvent_lookup[e];
Daniel P. Berrange48608532012-05-21 17:59:51 +0100919 assert(event_name != NULL);
920 info = g_malloc0(sizeof(*info));
921 info->value = g_malloc0(sizeof(*info->value));
922 info->value->name = g_strdup(event_name);
923
924 info->next = ev_list;
925 ev_list = info;
926 }
927
928 return ev_list;
929}
930
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300931/* set the current CPU defined by the user */
932int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000933{
Andreas Färber55e5c282012-12-17 06:18:02 +0100934 CPUState *cpu;
bellard6a00d602005-11-21 23:25:50 +0000935
Andreas Färber1c8bb3c2013-02-15 17:01:09 +0100936 cpu = qemu_get_cpu(cpu_index);
937 if (cpu == NULL) {
938 return -1;
bellard6a00d602005-11-21 23:25:50 +0000939 }
Andreas Färbercb446ec2013-05-01 14:24:52 +0200940 cur_mon->mon_cpu = cpu;
Andreas Färber1c8bb3c2013-02-15 17:01:09 +0100941 return 0;
bellard6a00d602005-11-21 23:25:50 +0000942}
943
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700944static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000945{
aliguori731b0362009-03-05 23:01:42 +0000946 if (!cur_mon->mon_cpu) {
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300947 monitor_set_cpu(0);
bellard6a00d602005-11-21 23:25:50 +0000948 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300949 cpu_synchronize_state(cur_mon->mon_cpu);
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700950 return cur_mon->mon_cpu;
951}
952
Pavel Butsykinbf957282015-09-10 18:38:59 +0300953CPUArchState *mon_get_cpu_env(void)
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700954{
955 return mon_get_cpu()->env_ptr;
bellard6a00d602005-11-21 23:25:50 +0000956}
957
Luiz Capitulino99b77962011-10-24 10:53:44 -0200958int monitor_get_cpu_index(void)
959{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700960 return mon_get_cpu()->cpu_index;
Luiz Capitulino99b77962011-10-24 10:53:44 -0200961}
962
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100963static void hmp_info_registers(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000964{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700965 cpu_dump_state(mon_get_cpu(), (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000966}
967
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100968static void hmp_info_jit(Monitor *mon, const QDict *qdict)
bellarde3db7222005-01-26 22:00:47 +0000969{
aliguori376253e2009-03-05 23:01:23 +0000970 dump_exec_info((FILE *)mon, monitor_fprintf);
Sebastian Tanase27498be2014-07-25 11:56:33 +0200971 dump_drift_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000972}
973
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100974static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
Max Filippov246ae242014-11-02 11:04:18 +0300975{
976 dump_opcount_info((FILE *)mon, monitor_fprintf);
977}
978
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100979static void hmp_info_history(Monitor *mon, const QDict *qdict)
bellardaa455482004-04-04 13:07:25 +0000980{
981 int i;
bellard7e2515e2004-08-01 21:52:19 +0000982 const char *str;
ths3b46e622007-09-17 08:09:54 +0000983
aliguoricde76ee2009-03-05 23:01:51 +0000984 if (!mon->rs)
985 return;
bellard7e2515e2004-08-01 21:52:19 +0000986 i = 0;
987 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000988 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000989 if (!str)
990 break;
aliguori376253e2009-03-05 23:01:23 +0000991 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000992 i++;
bellardaa455482004-04-04 13:07:25 +0000993 }
994}
995
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100996static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
j_mayer76a66252007-03-07 08:32:30 +0000997{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700998 cpu_dump_statistics(mon_get_cpu(), (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000999}
j_mayer76a66252007-03-07 08:32:30 +00001000
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001001static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301002{
Lluís Vilanova14101d02014-08-25 13:20:03 +02001003 TraceEventInfoList *events = qmp_trace_event_get_state("*", NULL);
1004 TraceEventInfoList *elem;
1005
1006 for (elem = events; elem != NULL; elem = elem->next) {
1007 monitor_printf(mon, "%s : state %u\n",
1008 elem->value->name,
1009 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
1010 }
1011 qapi_free_TraceEventInfoList(events);
Prerna Saxena22890ab2010-06-24 17:04:53 +05301012}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301013
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001014void qmp_client_migrate_info(const char *protocol, const char *hostname,
1015 bool has_port, int64_t port,
1016 bool has_tls_port, int64_t tls_port,
1017 bool has_cert_subject, const char *cert_subject,
1018 Error **errp)
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001019{
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001020 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001021 if (!qemu_using_spice(errp)) {
1022 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001023 }
1024
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001025 if (!has_port && !has_tls_port) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001026 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001027 return;
Yonit Halperin6ec5dae2012-03-18 09:42:39 +02001028 }
1029
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001030 if (qemu_spice_migrate_info(hostname,
1031 has_port ? port : -1,
1032 has_tls_port ? tls_port : -1,
1033 cert_subject)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001034 error_setg(errp, QERR_UNDEFINED_ERROR);
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001035 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001036 }
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001037 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001038 }
1039
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001040 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001041}
1042
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001043static void hmp_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001044{
Peter Maydell9a7e5422013-02-11 16:41:20 +00001045 qemu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001046}
1047
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001048static void hmp_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001049{
1050 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001051 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001052
bellard9307c4c2004-04-04 12:57:25 +00001053 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001054 mask = 0;
1055 } else {
Peter Maydell4fde1eb2013-02-11 16:41:22 +00001056 mask = qemu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001057 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001058 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001059 return;
1060 }
1061 }
Peter Maydell24537a02013-02-11 16:41:23 +00001062 qemu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00001063}
1064
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001065static void hmp_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001066{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001067 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001068 if (!option || !strcmp(option, "on")) {
1069 singlestep = 1;
1070 } else if (!strcmp(option, "off")) {
1071 singlestep = 0;
1072 } else {
1073 monitor_printf(mon, "unexpected option %s\n", option);
1074 }
1075}
1076
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001077static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001078{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001079 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001080 if (!device)
1081 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1082 if (gdbserver_start(device) < 0) {
1083 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1084 device);
1085 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001086 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001087 } else {
aliguori59030a82009-04-05 18:43:41 +00001088 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1089 device);
bellard8a7ddc32004-03-31 19:00:16 +00001090 }
1091}
1092
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001093static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001094{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001095 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001096 if (select_watchdog_action(action) == -1) {
1097 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1098 }
1099}
1100
aliguori376253e2009-03-05 23:01:23 +00001101static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001102{
aliguori376253e2009-03-05 23:01:23 +00001103 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001104 switch(c) {
1105 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001106 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001107 break;
1108 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001109 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001110 break;
1111 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001112 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001113 break;
1114 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001115 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001116 break;
1117 default:
1118 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001119 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001120 } else {
aliguori376253e2009-03-05 23:01:23 +00001121 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001122 }
1123 break;
1124 }
aliguori376253e2009-03-05 23:01:23 +00001125 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001126}
1127
aliguori376253e2009-03-05 23:01:23 +00001128static void memory_dump(Monitor *mon, int count, int format, int wsize,
Avi Kivitya8170e52012-10-23 12:30:10 +02001129 hwaddr addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001130{
Blue Swirl23842aa2010-01-12 20:27:43 +00001131 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001132 uint8_t buf[16];
1133 uint64_t v;
1134
1135 if (format == 'i') {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001136 int flags = 0;
bellard9307c4c2004-04-04 12:57:25 +00001137#ifdef TARGET_I386
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001138 CPUArchState *env = mon_get_cpu_env();
bellard4c27ba22004-04-25 18:05:08 +00001139 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001140 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001141 } else if (wsize == 4) {
1142 flags = 0;
1143 } else {
bellard6a15fd12006-04-12 21:07:07 +00001144 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001145 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001146 if (env) {
1147#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001148 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001149 (env->segs[R_CS].flags & DESC_L_MASK))
1150 flags = 2;
1151 else
1152#endif
1153 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1154 flags = 1;
1155 }
bellard4c27ba22004-04-25 18:05:08 +00001156 }
1157#endif
Tom Musta1c38f842014-04-09 14:53:24 -05001158#ifdef TARGET_PPC
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001159 CPUArchState *env = mon_get_cpu_env();
Tom Musta1c38f842014-04-09 14:53:24 -05001160 flags = msr_le << 16;
1161 flags |= env->bfd_mach;
1162#endif
Peter Crosthwaited49190c2015-05-24 14:20:41 -07001163 monitor_disas(mon, mon_get_cpu(), addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001164 return;
1165 }
1166
1167 len = wsize * count;
1168 if (wsize == 1)
1169 line_size = 8;
1170 else
1171 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001172 max_digits = 0;
1173
1174 switch(format) {
1175 case 'o':
1176 max_digits = (wsize * 8 + 2) / 3;
1177 break;
1178 default:
1179 case 'x':
1180 max_digits = (wsize * 8) / 4;
1181 break;
1182 case 'u':
1183 case 'd':
1184 max_digits = (wsize * 8 * 10 + 32) / 33;
1185 break;
1186 case 'c':
1187 wsize = 1;
1188 break;
1189 }
1190
1191 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001192 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001193 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001194 else
aliguori376253e2009-03-05 23:01:23 +00001195 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001196 l = len;
1197 if (l > line_size)
1198 l = line_size;
1199 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001200 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001201 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001202 if (cpu_memory_rw_debug(mon_get_cpu(), addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001203 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001204 break;
1205 }
bellard9307c4c2004-04-04 12:57:25 +00001206 }
ths5fafdf22007-09-16 21:08:06 +00001207 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001208 while (i < l) {
1209 switch(wsize) {
1210 default:
1211 case 1:
Peter Maydell24e60302015-01-20 15:19:32 +00001212 v = ldub_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001213 break;
1214 case 2:
Peter Maydell24e60302015-01-20 15:19:32 +00001215 v = lduw_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001216 break;
1217 case 4:
Peter Maydell24e60302015-01-20 15:19:32 +00001218 v = (uint32_t)ldl_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001219 break;
1220 case 8:
Peter Maydell24e60302015-01-20 15:19:32 +00001221 v = ldq_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001222 break;
1223 }
aliguori376253e2009-03-05 23:01:23 +00001224 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001225 switch(format) {
1226 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001227 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001228 break;
1229 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001230 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001231 break;
1232 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001233 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001234 break;
1235 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001236 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001237 break;
1238 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001239 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001240 break;
1241 }
1242 i += wsize;
1243 }
aliguori376253e2009-03-05 23:01:23 +00001244 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001245 addr += l;
1246 len -= l;
1247 }
1248}
1249
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001250static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001251{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001252 int count = qdict_get_int(qdict, "count");
1253 int format = qdict_get_int(qdict, "format");
1254 int size = qdict_get_int(qdict, "size");
1255 target_long addr = qdict_get_int(qdict, "addr");
1256
aliguori376253e2009-03-05 23:01:23 +00001257 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001258}
1259
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001260static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001261{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001262 int count = qdict_get_int(qdict, "count");
1263 int format = qdict_get_int(qdict, "format");
1264 int size = qdict_get_int(qdict, "size");
Avi Kivitya8170e52012-10-23 12:30:10 +02001265 hwaddr addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001266
aliguori376253e2009-03-05 23:01:23 +00001267 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001268}
1269
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001270static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001271{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001272 int format = qdict_get_int(qdict, "format");
Avi Kivitya8170e52012-10-23 12:30:10 +02001273 hwaddr val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001274
bellard9307c4c2004-04-04 12:57:25 +00001275 switch(format) {
1276 case 'o':
Avi Kivitya8170e52012-10-23 12:30:10 +02001277 monitor_printf(mon, "%#" HWADDR_PRIo, val);
bellard9307c4c2004-04-04 12:57:25 +00001278 break;
1279 case 'x':
Avi Kivitya8170e52012-10-23 12:30:10 +02001280 monitor_printf(mon, "%#" HWADDR_PRIx, val);
bellard9307c4c2004-04-04 12:57:25 +00001281 break;
1282 case 'u':
Avi Kivitya8170e52012-10-23 12:30:10 +02001283 monitor_printf(mon, "%" HWADDR_PRIu, val);
bellard9307c4c2004-04-04 12:57:25 +00001284 break;
1285 default:
1286 case 'd':
Avi Kivitya8170e52012-10-23 12:30:10 +02001287 monitor_printf(mon, "%" HWADDR_PRId, val);
bellard9307c4c2004-04-04 12:57:25 +00001288 break;
1289 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001290 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001291 break;
1292 }
aliguori376253e2009-03-05 23:01:23 +00001293 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001294}
1295
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001296static void hmp_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001297{
1298 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001299 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001300 uint32_t start = qdict_get_int(qdict, "start");
1301 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001302
1303 sum = 0;
1304 for(addr = start; addr < (start + size); addr++) {
Peter Maydell42874d32015-04-26 16:49:24 +01001305 uint8_t val = address_space_ldub(&address_space_memory, addr,
1306 MEMTXATTRS_UNSPECIFIED, NULL);
bellarde4cf1ad2005-06-04 20:15:57 +00001307 /* BSD sum algorithm ('sum' Unix command) */
1308 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001309 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001310 }
aliguori376253e2009-03-05 23:01:23 +00001311 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001312}
1313
bellard13224a82006-07-14 22:03:35 +00001314static int mouse_button_state;
1315
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001316static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001317{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001318 int dx, dy, dz, button;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001319 const char *dx_str = qdict_get_str(qdict, "dx_str");
1320 const char *dy_str = qdict_get_str(qdict, "dy_str");
1321 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001322
bellard13224a82006-07-14 22:03:35 +00001323 dx = strtol(dx_str, NULL, 0);
1324 dy = strtol(dy_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001325 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
1326 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
1327
1328 if (dz_str) {
bellard13224a82006-07-14 22:03:35 +00001329 dz = strtol(dz_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001330 if (dz != 0) {
1331 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
1332 qemu_input_queue_btn(NULL, button, true);
1333 qemu_input_event_sync();
1334 qemu_input_queue_btn(NULL, button, false);
1335 }
1336 }
1337 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001338}
1339
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001340static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001341{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001342 static uint32_t bmap[INPUT_BUTTON_MAX] = {
1343 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1344 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
1345 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1346 };
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001347 int button_state = qdict_get_int(qdict, "button_state");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001348
1349 if (mouse_button_state == button_state) {
1350 return;
1351 }
1352 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1353 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001354 mouse_button_state = button_state;
bellard13224a82006-07-14 22:03:35 +00001355}
1356
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001357static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001358{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001359 int size = qdict_get_int(qdict, "size");
1360 int addr = qdict_get_int(qdict, "addr");
1361 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001362 uint32_t val;
1363 int suffix;
1364
1365 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001366 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001367 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001368 addr++;
1369 }
1370 addr &= 0xffff;
1371
1372 switch(size) {
1373 default:
1374 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001375 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001376 suffix = 'b';
1377 break;
1378 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001379 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001380 suffix = 'w';
1381 break;
1382 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001383 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001384 suffix = 'l';
1385 break;
1386 }
aliguori376253e2009-03-05 23:01:23 +00001387 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1388 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001389}
bellarda3a91a32004-06-04 11:06:21 +00001390
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001391static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001392{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001393 int size = qdict_get_int(qdict, "size");
1394 int addr = qdict_get_int(qdict, "addr");
1395 int val = qdict_get_int(qdict, "val");
1396
Jan Kiszkaf1147842009-07-14 10:20:11 +02001397 addr &= IOPORTS_MASK;
1398
1399 switch (size) {
1400 default:
1401 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001402 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001403 break;
1404 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001405 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001406 break;
1407 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001408 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001409 break;
1410 }
1411}
1412
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001413static void hmp_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001414{
Gongleif1839932014-12-03 18:20:58 +00001415 Error *local_err = NULL;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001416 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001417
Gongleif1839932014-12-03 18:20:58 +00001418 qemu_boot_set(bootdevice, &local_err);
1419 if (local_err) {
1420 monitor_printf(mon, "%s\n", error_get_pretty(local_err));
1421 error_free(local_err);
aurel320ecdffb2008-05-04 20:11:34 +00001422 } else {
Gongleif1839932014-12-03 18:20:58 +00001423 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001424 }
1425}
1426
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001427static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
Blue Swirl314e2982011-09-11 20:22:05 +00001428{
1429 mtree_info((fprintf_function)monitor_printf, mon);
1430}
1431
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001432static void hmp_info_numa(Monitor *mon, const QDict *qdict)
aliguori030ea372009-04-21 22:30:47 +00001433{
aliguorib28b6232009-04-22 20:20:29 +00001434 int i;
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001435 CPUState *cpu;
zhanghailiang5b009e42014-11-04 19:49:30 +08001436 uint64_t *node_mem;
aliguori030ea372009-04-21 22:30:47 +00001437
zhanghailiang5b009e42014-11-04 19:49:30 +08001438 node_mem = g_new0(uint64_t, nb_numa_nodes);
1439 query_numa_node_mem(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001440 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1441 for (i = 0; i < nb_numa_nodes; i++) {
1442 monitor_printf(mon, "node %d cpus:", i);
Andreas Färberbdc44642013-06-24 23:50:24 +02001443 CPU_FOREACH(cpu) {
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001444 if (cpu->numa_node == i) {
Andreas Färber55e5c282012-12-17 06:18:02 +01001445 monitor_printf(mon, " %d", cpu->cpu_index);
aliguori030ea372009-04-21 22:30:47 +00001446 }
1447 }
1448 monitor_printf(mon, "\n");
1449 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
zhanghailiang5b009e42014-11-04 19:49:30 +08001450 node_mem[i] >> 20);
aliguori030ea372009-04-21 22:30:47 +00001451 }
zhanghailiang5b009e42014-11-04 19:49:30 +08001452 g_free(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001453}
1454
bellard5f1ce942006-02-08 22:40:15 +00001455#ifdef CONFIG_PROFILER
1456
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001457int64_t tcg_time;
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001458int64_t dev_time;
1459
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001460static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001461{
aliguori376253e2009-03-05 23:01:23 +00001462 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001463 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00001464 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001465 tcg_time, tcg_time / (double)get_ticks_per_sec());
1466 tcg_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001467 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001468}
1469#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001470static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001471{
aliguori376253e2009-03-05 23:01:23 +00001472 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001473}
1474#endif
1475
bellardec36b692006-07-16 18:57:03 +00001476/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001477static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001478
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001479static void hmp_info_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001480{
1481 int i;
1482 CaptureState *s;
1483
1484 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001485 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001486 s->ops.info (s->opaque);
1487 }
1488}
1489
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001490static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001491{
1492 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001493 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001494 CaptureState *s;
1495
1496 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1497 if (i == n) {
1498 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001499 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001500 g_free (s);
bellardec36b692006-07-16 18:57:03 +00001501 return;
1502 }
1503 }
1504}
1505
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001506static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001507{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001508 const char *path = qdict_get_str(qdict, "path");
1509 int has_freq = qdict_haskey(qdict, "freq");
1510 int freq = qdict_get_try_int(qdict, "freq", -1);
1511 int has_bits = qdict_haskey(qdict, "bits");
1512 int bits = qdict_get_try_int(qdict, "bits", -1);
1513 int has_channels = qdict_haskey(qdict, "nchannels");
1514 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001515 CaptureState *s;
1516
Anthony Liguori7267c092011-08-20 22:09:37 -05001517 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001518
1519 freq = has_freq ? freq : 44100;
1520 bits = has_bits ? bits : 16;
1521 nchannels = has_channels ? nchannels : 2;
1522
1523 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001524 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05001525 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001526 return;
bellardec36b692006-07-16 18:57:03 +00001527 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001528 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001529}
bellardec36b692006-07-16 18:57:03 +00001530
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001531static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001532{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001533 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001534
aliguori76655d62009-03-06 20:27:37 +00001535 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001536 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001537 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001538 return acl;
1539}
aliguori76655d62009-03-06 20:27:37 +00001540
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001541static void hmp_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001542{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001543 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001544 qemu_acl *acl = find_acl(mon, aclname);
1545 qemu_acl_entry *entry;
1546 int i = 0;
1547
1548 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001549 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001550 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00001551 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00001552 i++;
1553 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001554 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001555 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001556 }
1557}
1558
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001559static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001560{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001561 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001562 qemu_acl *acl = find_acl(mon, aclname);
1563
1564 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001565 qemu_acl_reset(acl);
1566 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001567 }
1568}
aliguori76655d62009-03-06 20:27:37 +00001569
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001570static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001571{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001572 const char *aclname = qdict_get_str(qdict, "aclname");
1573 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001574 qemu_acl *acl = find_acl(mon, aclname);
1575
1576 if (acl) {
1577 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001578 acl->defaultDeny = 0;
1579 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001580 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001581 acl->defaultDeny = 1;
1582 monitor_printf(mon, "acl: policy set to 'deny'\n");
1583 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001584 monitor_printf(mon, "acl: unknown policy '%s', "
1585 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001586 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001587 }
1588}
aliguori76655d62009-03-06 20:27:37 +00001589
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001590static void hmp_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001591{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001592 const char *aclname = qdict_get_str(qdict, "aclname");
1593 const char *match = qdict_get_str(qdict, "match");
1594 const char *policy = qdict_get_str(qdict, "policy");
1595 int has_index = qdict_haskey(qdict, "index");
1596 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001597 qemu_acl *acl = find_acl(mon, aclname);
1598 int deny, ret;
1599
1600 if (acl) {
1601 if (strcmp(policy, "allow") == 0) {
1602 deny = 0;
1603 } else if (strcmp(policy, "deny") == 0) {
1604 deny = 1;
1605 } else {
1606 monitor_printf(mon, "acl: unknown policy '%s', "
1607 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001608 return;
1609 }
aliguori28a76be2009-03-06 20:27:40 +00001610 if (has_index)
1611 ret = qemu_acl_insert(acl, deny, match, index);
1612 else
1613 ret = qemu_acl_append(acl, deny, match);
1614 if (ret < 0)
1615 monitor_printf(mon, "acl: unable to add acl entry\n");
1616 else
1617 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001618 }
1619}
aliguori76655d62009-03-06 20:27:37 +00001620
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001621static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001622{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001623 const char *aclname = qdict_get_str(qdict, "aclname");
1624 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001625 qemu_acl *acl = find_acl(mon, aclname);
1626 int ret;
aliguori76655d62009-03-06 20:27:37 +00001627
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001628 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001629 ret = qemu_acl_remove(acl, match);
1630 if (ret < 0)
1631 monitor_printf(mon, "acl: no matching acl entry\n");
1632 else
1633 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001634 }
1635}
1636
Corey Bryant208c9d12012-06-22 14:36:09 -04001637void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001638{
Anthony Liguoric227f092009-10-01 16:12:16 -05001639 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001640 int fd;
1641
Corey Bryant208c9d12012-06-22 14:36:09 -04001642 fd = qemu_chr_fe_get_msgfd(cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001643 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001644 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryant208c9d12012-06-22 14:36:09 -04001645 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001646 }
1647
1648 if (qemu_isdigit(fdname[0])) {
Stefan Hajnoczi0b9f0e22014-04-24 13:58:18 +02001649 close(fd);
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001650 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
1651 "a name not starting with a digit");
Corey Bryant208c9d12012-06-22 14:36:09 -04001652 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001653 }
1654
Corey Bryant208c9d12012-06-22 14:36:09 -04001655 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001656 if (strcmp(monfd->name, fdname) != 0) {
1657 continue;
1658 }
1659
1660 close(monfd->fd);
1661 monfd->fd = fd;
Corey Bryant208c9d12012-06-22 14:36:09 -04001662 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001663 }
1664
Anthony Liguori7267c092011-08-20 22:09:37 -05001665 monfd = g_malloc0(sizeof(mon_fd_t));
1666 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001667 monfd->fd = fd;
1668
Corey Bryant208c9d12012-06-22 14:36:09 -04001669 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001670}
1671
Corey Bryant208c9d12012-06-22 14:36:09 -04001672void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001673{
Anthony Liguoric227f092009-10-01 16:12:16 -05001674 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001675
Corey Bryant208c9d12012-06-22 14:36:09 -04001676 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001677 if (strcmp(monfd->name, fdname) != 0) {
1678 continue;
1679 }
1680
Blue Swirl72cf2d42009-09-12 07:36:22 +00001681 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001682 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001683 g_free(monfd->name);
1684 g_free(monfd);
Corey Bryant208c9d12012-06-22 14:36:09 -04001685 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001686 }
1687
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001688 error_setg(errp, QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001689}
1690
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001691static void hmp_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001692{
Luiz Capitulino13548692011-07-29 15:36:43 -03001693 int saved_vm_running = runstate_is_running();
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001694 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02001695
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001696 vm_stop(RUN_STATE_RESTORE_VM);
Juan Quintelac8d41b22009-08-20 19:42:21 +02001697
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03001698 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02001699 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03001700 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02001701}
1702
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001703int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
Mark McLoughlin7768e042009-07-22 09:11:41 +01001704{
Anthony Liguoric227f092009-10-01 16:12:16 -05001705 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01001706
Blue Swirl72cf2d42009-09-12 07:36:22 +00001707 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01001708 int fd;
1709
1710 if (strcmp(monfd->name, fdname) != 0) {
1711 continue;
1712 }
1713
1714 fd = monfd->fd;
1715
1716 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001717 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05001718 g_free(monfd->name);
1719 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001720
1721 return fd;
1722 }
1723
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001724 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001725 return -1;
1726}
1727
Corey Bryantba1c0482012-08-14 16:43:43 -04001728static void monitor_fdset_cleanup(MonFdset *mon_fdset)
1729{
1730 MonFdsetFd *mon_fdset_fd;
1731 MonFdsetFd *mon_fdset_fd_next;
1732
1733 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantebe52b52012-10-18 15:19:33 -04001734 if ((mon_fdset_fd->removed ||
1735 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
1736 runstate_is_running()) {
Corey Bryantba1c0482012-08-14 16:43:43 -04001737 close(mon_fdset_fd->fd);
1738 g_free(mon_fdset_fd->opaque);
1739 QLIST_REMOVE(mon_fdset_fd, next);
1740 g_free(mon_fdset_fd);
1741 }
1742 }
1743
Corey Bryantadb696f2012-08-14 16:43:47 -04001744 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04001745 QLIST_REMOVE(mon_fdset, next);
1746 g_free(mon_fdset);
1747 }
1748}
1749
Corey Bryantefb87c12012-08-14 16:43:48 -04001750static void monitor_fdsets_cleanup(void)
1751{
1752 MonFdset *mon_fdset;
1753 MonFdset *mon_fdset_next;
1754
1755 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
1756 monitor_fdset_cleanup(mon_fdset);
1757 }
1758}
1759
Corey Bryantba1c0482012-08-14 16:43:43 -04001760AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
1761 const char *opaque, Error **errp)
1762{
1763 int fd;
1764 Monitor *mon = cur_mon;
Corey Bryantba1c0482012-08-14 16:43:43 -04001765 AddfdInfo *fdinfo;
1766
1767 fd = qemu_chr_fe_get_msgfd(mon->chr);
1768 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001769 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryantba1c0482012-08-14 16:43:43 -04001770 goto error;
1771 }
1772
Corey Bryante446f702012-10-18 15:19:32 -04001773 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
1774 has_opaque, opaque, errp);
1775 if (fdinfo) {
1776 return fdinfo;
Corey Bryant9ac54af2012-10-18 15:19:31 -04001777 }
1778
Corey Bryantba1c0482012-08-14 16:43:43 -04001779error:
1780 if (fd != -1) {
1781 close(fd);
1782 }
1783 return NULL;
1784}
1785
1786void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
1787{
1788 MonFdset *mon_fdset;
1789 MonFdsetFd *mon_fdset_fd;
1790 char fd_str[60];
1791
1792 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1793 if (mon_fdset->id != fdset_id) {
1794 continue;
1795 }
1796 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1797 if (has_fd) {
1798 if (mon_fdset_fd->fd != fd) {
1799 continue;
1800 }
1801 mon_fdset_fd->removed = true;
1802 break;
1803 } else {
1804 mon_fdset_fd->removed = true;
1805 }
1806 }
1807 if (has_fd && !mon_fdset_fd) {
1808 goto error;
1809 }
1810 monitor_fdset_cleanup(mon_fdset);
1811 return;
1812 }
1813
1814error:
1815 if (has_fd) {
1816 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
1817 fdset_id, fd);
1818 } else {
1819 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
1820 }
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001821 error_setg(errp, QERR_FD_NOT_FOUND, fd_str);
Corey Bryantba1c0482012-08-14 16:43:43 -04001822}
1823
1824FdsetInfoList *qmp_query_fdsets(Error **errp)
1825{
1826 MonFdset *mon_fdset;
1827 MonFdsetFd *mon_fdset_fd;
1828 FdsetInfoList *fdset_list = NULL;
1829
1830 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1831 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
1832 FdsetFdInfoList *fdsetfd_list = NULL;
1833
1834 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
1835 fdset_info->value->fdset_id = mon_fdset->id;
1836
1837 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1838 FdsetFdInfoList *fdsetfd_info;
1839
1840 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
1841 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
1842 fdsetfd_info->value->fd = mon_fdset_fd->fd;
1843 if (mon_fdset_fd->opaque) {
1844 fdsetfd_info->value->has_opaque = true;
1845 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
1846 } else {
1847 fdsetfd_info->value->has_opaque = false;
1848 }
1849
1850 fdsetfd_info->next = fdsetfd_list;
1851 fdsetfd_list = fdsetfd_info;
1852 }
1853
1854 fdset_info->value->fds = fdsetfd_list;
1855
1856 fdset_info->next = fdset_list;
1857 fdset_list = fdset_info;
1858 }
1859
1860 return fdset_list;
1861}
1862
Corey Bryante446f702012-10-18 15:19:32 -04001863AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
1864 bool has_opaque, const char *opaque,
1865 Error **errp)
1866{
1867 MonFdset *mon_fdset = NULL;
1868 MonFdsetFd *mon_fdset_fd;
1869 AddfdInfo *fdinfo;
1870
1871 if (has_fdset_id) {
1872 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1873 /* Break if match found or match impossible due to ordering by ID */
1874 if (fdset_id <= mon_fdset->id) {
1875 if (fdset_id < mon_fdset->id) {
1876 mon_fdset = NULL;
1877 }
1878 break;
1879 }
1880 }
1881 }
1882
1883 if (mon_fdset == NULL) {
1884 int64_t fdset_id_prev = -1;
1885 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
1886
1887 if (has_fdset_id) {
1888 if (fdset_id < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001889 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
1890 "a non-negative value");
Corey Bryante446f702012-10-18 15:19:32 -04001891 return NULL;
1892 }
1893 /* Use specified fdset ID */
1894 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1895 mon_fdset_cur = mon_fdset;
1896 if (fdset_id < mon_fdset_cur->id) {
1897 break;
1898 }
1899 }
1900 } else {
1901 /* Use first available fdset ID */
1902 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1903 mon_fdset_cur = mon_fdset;
1904 if (fdset_id_prev == mon_fdset_cur->id - 1) {
1905 fdset_id_prev = mon_fdset_cur->id;
1906 continue;
1907 }
1908 break;
1909 }
1910 }
1911
1912 mon_fdset = g_malloc0(sizeof(*mon_fdset));
1913 if (has_fdset_id) {
1914 mon_fdset->id = fdset_id;
1915 } else {
1916 mon_fdset->id = fdset_id_prev + 1;
1917 }
1918
1919 /* The fdset list is ordered by fdset ID */
1920 if (!mon_fdset_cur) {
1921 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
1922 } else if (mon_fdset->id < mon_fdset_cur->id) {
1923 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
1924 } else {
1925 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
1926 }
1927 }
1928
1929 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
1930 mon_fdset_fd->fd = fd;
1931 mon_fdset_fd->removed = false;
1932 if (has_opaque) {
1933 mon_fdset_fd->opaque = g_strdup(opaque);
1934 }
1935 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
1936
1937 fdinfo = g_malloc0(sizeof(*fdinfo));
1938 fdinfo->fdset_id = mon_fdset->id;
1939 fdinfo->fd = mon_fdset_fd->fd;
1940
1941 return fdinfo;
1942}
1943
Corey Bryantadb696f2012-08-14 16:43:47 -04001944int monitor_fdset_get_fd(int64_t fdset_id, int flags)
1945{
Blue Swirlb2dc64c2012-08-18 20:14:54 +00001946#ifndef _WIN32
Corey Bryantadb696f2012-08-14 16:43:47 -04001947 MonFdset *mon_fdset;
1948 MonFdsetFd *mon_fdset_fd;
1949 int mon_fd_flags;
1950
Corey Bryantadb696f2012-08-14 16:43:47 -04001951 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1952 if (mon_fdset->id != fdset_id) {
1953 continue;
1954 }
1955 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1956 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
1957 if (mon_fd_flags == -1) {
1958 return -1;
1959 }
1960
1961 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
1962 return mon_fdset_fd->fd;
1963 }
1964 }
1965 errno = EACCES;
1966 return -1;
1967 }
1968#endif
1969
1970 errno = ENOENT;
1971 return -1;
1972}
1973
1974int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
1975{
1976 MonFdset *mon_fdset;
1977 MonFdsetFd *mon_fdset_fd_dup;
1978
1979 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1980 if (mon_fdset->id != fdset_id) {
1981 continue;
1982 }
1983 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
1984 if (mon_fdset_fd_dup->fd == dup_fd) {
1985 return -1;
1986 }
1987 }
1988 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
1989 mon_fdset_fd_dup->fd = dup_fd;
1990 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
1991 return 0;
1992 }
1993 return -1;
1994}
1995
1996static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
1997{
1998 MonFdset *mon_fdset;
1999 MonFdsetFd *mon_fdset_fd_dup;
2000
2001 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2002 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2003 if (mon_fdset_fd_dup->fd == dup_fd) {
2004 if (remove) {
2005 QLIST_REMOVE(mon_fdset_fd_dup, next);
2006 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2007 monitor_fdset_cleanup(mon_fdset);
2008 }
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002009 return -1;
2010 } else {
2011 return mon_fdset->id;
Corey Bryantadb696f2012-08-14 16:43:47 -04002012 }
Corey Bryantadb696f2012-08-14 16:43:47 -04002013 }
2014 }
2015 }
2016 return -1;
2017}
2018
2019int monitor_fdset_dup_fd_find(int dup_fd)
2020{
2021 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2022}
2023
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002024void monitor_fdset_dup_fd_remove(int dup_fd)
Corey Bryantadb696f2012-08-14 16:43:47 -04002025{
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002026 monitor_fdset_dup_fd_find_remove(dup_fd, true);
Corey Bryantadb696f2012-08-14 16:43:47 -04002027}
2028
Markus Armbruster1677f4c2015-02-09 14:03:19 +01002029int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
Laszlo Ersek59063662014-04-10 10:24:31 +02002030{
2031 int fd;
2032 Error *local_err = NULL;
2033
2034 if (!qemu_isdigit(fdname[0]) && mon) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002035 fd = monitor_get_fd(mon, fdname, &local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002036 } else {
2037 fd = qemu_parse_fd(fdname);
Laszlo Ersek59063662014-04-10 10:24:31 +02002038 if (fd == -1) {
2039 error_setg(&local_err, "Invalid file descriptor number '%s'",
2040 fdname);
2041 }
2042 }
2043 if (local_err) {
2044 error_propagate(errp, local_err);
2045 assert(fd == -1);
2046 } else {
2047 assert(fd != -1);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002048 }
2049
2050 return fd;
2051}
2052
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002053/* Please update hmp-commands.hx when adding or changing commands */
Wayne Xia816f8922011-10-12 11:32:41 +08002054static mon_cmd_t info_cmds[] = {
Pavel Butsykinda76ee72015-09-10 18:38:58 +03002055#include "hmp-commands-info.h"
2056 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002057};
2058
Wenchao Xiaa13ced52013-01-14 14:06:28 +08002059/* mon_cmds and info_cmds would be sorted at runtime */
2060static mon_cmd_t mon_cmds[] = {
2061#include "hmp-commands.h"
2062 { NULL, NULL, },
2063};
2064
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002065static const mon_cmd_t qmp_cmds[] = {
Anthony Liguorie3193602011-09-02 12:34:47 -05002066#include "qmp-commands-old.h"
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002067 { /* NULL */ },
2068};
2069
bellard9307c4c2004-04-04 12:57:25 +00002070/*******************************************************************/
2071
2072static const char *pch;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002073static sigjmp_buf expr_env;
bellard9307c4c2004-04-04 12:57:25 +00002074
bellard9307c4c2004-04-04 12:57:25 +00002075
Stefan Weil9c3175c2013-08-22 21:30:09 +02002076static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
2077expr_error(Monitor *mon, const char *fmt, ...)
bellard9dc39cb2004-03-14 21:38:27 +00002078{
Fam Zheng277acfe2013-08-20 10:58:21 +08002079 va_list ap;
2080 va_start(ap, fmt);
2081 monitor_vprintf(mon, fmt, ap);
2082 monitor_printf(mon, "\n");
2083 va_end(ap);
Peter Maydell6ab7e542013-02-20 15:21:09 +00002084 siglongjmp(expr_env, 1);
bellard9307c4c2004-04-04 12:57:25 +00002085}
2086
Markus Armbruster09b94182010-01-20 13:07:30 +01002087/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00002088static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002089{
Pavel Butsykinbf957282015-09-10 18:38:59 +03002090 const MonitorDef *md = target_monitor_defs();
bellard92a31b12005-02-10 22:00:52 +00002091 void *ptr;
2092
Pavel Butsykinbf957282015-09-10 18:38:59 +03002093 if (md == NULL) {
2094 return -1;
2095 }
2096
2097 for(; md->name != NULL; md++) {
bellard9307c4c2004-04-04 12:57:25 +00002098 if (compare_cmd(name, md->name)) {
2099 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002100 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002101 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002102 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00002103 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002104 switch(md->type) {
2105 case MD_I32:
2106 *pval = *(int32_t *)ptr;
2107 break;
2108 case MD_TLONG:
2109 *pval = *(target_long *)ptr;
2110 break;
2111 default:
2112 *pval = 0;
2113 break;
2114 }
bellard9307c4c2004-04-04 12:57:25 +00002115 }
2116 return 0;
2117 }
2118 }
2119 return -1;
2120}
2121
2122static void next(void)
2123{
Blue Swirl660f11b2009-07-31 21:16:51 +00002124 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002125 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002126 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002127 pch++;
2128 }
2129}
2130
aliguori376253e2009-03-05 23:01:23 +00002131static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002132
aliguori376253e2009-03-05 23:01:23 +00002133static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002134{
blueswir1c2efc952007-09-25 17:28:42 +00002135 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002136 char *p;
bellard6a00d602005-11-21 23:25:50 +00002137 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002138
2139 switch(*pch) {
2140 case '+':
2141 next();
aliguori376253e2009-03-05 23:01:23 +00002142 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002143 break;
2144 case '-':
2145 next();
aliguori376253e2009-03-05 23:01:23 +00002146 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002147 break;
2148 case '~':
2149 next();
aliguori376253e2009-03-05 23:01:23 +00002150 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002151 break;
2152 case '(':
2153 next();
aliguori376253e2009-03-05 23:01:23 +00002154 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002155 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002156 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002157 }
2158 next();
2159 break;
bellard81d09122004-07-14 17:21:37 +00002160 case '\'':
2161 pch++;
2162 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002163 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002164 n = *pch;
2165 pch++;
2166 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002167 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002168 next();
2169 break;
bellard9307c4c2004-04-04 12:57:25 +00002170 case '$':
2171 {
2172 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002173 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002174
bellard9307c4c2004-04-04 12:57:25 +00002175 pch++;
2176 q = buf;
2177 while ((*pch >= 'a' && *pch <= 'z') ||
2178 (*pch >= 'A' && *pch <= 'Z') ||
2179 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002180 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002181 if ((q - buf) < sizeof(buf) - 1)
2182 *q++ = *pch;
2183 pch++;
2184 }
blueswir1cd390082008-11-16 13:53:32 +00002185 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002186 pch++;
2187 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002188 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01002189 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00002190 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00002191 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002192 }
2193 break;
2194 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002195 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002196 n = 0;
2197 break;
2198 default:
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002199 errno = 0;
bellard4f4fbf72006-06-25 18:28:12 +00002200 n = strtoull(pch, &p, 0);
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002201 if (errno == ERANGE) {
2202 expr_error(mon, "number too large");
2203 }
bellard9307c4c2004-04-04 12:57:25 +00002204 if (pch == p) {
Fam Zheng277acfe2013-08-20 10:58:21 +08002205 expr_error(mon, "invalid char '%c' in expression", *p);
bellard9307c4c2004-04-04 12:57:25 +00002206 }
2207 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002208 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002209 pch++;
2210 break;
2211 }
2212 return n;
2213}
2214
2215
aliguori376253e2009-03-05 23:01:23 +00002216static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002217{
blueswir1c2efc952007-09-25 17:28:42 +00002218 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002219 int op;
ths3b46e622007-09-17 08:09:54 +00002220
aliguori376253e2009-03-05 23:01:23 +00002221 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002222 for(;;) {
2223 op = *pch;
2224 if (op != '*' && op != '/' && op != '%')
2225 break;
2226 next();
aliguori376253e2009-03-05 23:01:23 +00002227 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002228 switch(op) {
2229 default:
2230 case '*':
2231 val *= val2;
2232 break;
2233 case '/':
2234 case '%':
ths5fafdf22007-09-16 21:08:06 +00002235 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002236 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002237 if (op == '/')
2238 val /= val2;
2239 else
2240 val %= val2;
2241 break;
2242 }
2243 }
2244 return val;
2245}
2246
aliguori376253e2009-03-05 23:01:23 +00002247static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002248{
blueswir1c2efc952007-09-25 17:28:42 +00002249 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002250 int op;
bellard9307c4c2004-04-04 12:57:25 +00002251
aliguori376253e2009-03-05 23:01:23 +00002252 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002253 for(;;) {
2254 op = *pch;
2255 if (op != '&' && op != '|' && op != '^')
2256 break;
2257 next();
aliguori376253e2009-03-05 23:01:23 +00002258 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002259 switch(op) {
2260 default:
2261 case '&':
2262 val &= val2;
2263 break;
2264 case '|':
2265 val |= val2;
2266 break;
2267 case '^':
2268 val ^= val2;
2269 break;
2270 }
2271 }
2272 return val;
2273}
2274
aliguori376253e2009-03-05 23:01:23 +00002275static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002276{
blueswir1c2efc952007-09-25 17:28:42 +00002277 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002278 int op;
bellard9307c4c2004-04-04 12:57:25 +00002279
aliguori376253e2009-03-05 23:01:23 +00002280 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002281 for(;;) {
2282 op = *pch;
2283 if (op != '+' && op != '-')
2284 break;
2285 next();
aliguori376253e2009-03-05 23:01:23 +00002286 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002287 if (op == '+')
2288 val += val2;
2289 else
2290 val -= val2;
2291 }
2292 return val;
2293}
2294
aliguori376253e2009-03-05 23:01:23 +00002295static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002296{
2297 pch = *pp;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002298 if (sigsetjmp(expr_env, 0)) {
bellard9307c4c2004-04-04 12:57:25 +00002299 *pp = pch;
2300 return -1;
2301 }
blueswir1cd390082008-11-16 13:53:32 +00002302 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002303 pch++;
aliguori376253e2009-03-05 23:01:23 +00002304 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002305 *pp = pch;
2306 return 0;
2307}
2308
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002309static int get_double(Monitor *mon, double *pval, const char **pp)
2310{
2311 const char *p = *pp;
2312 char *tailp;
2313 double d;
2314
2315 d = strtod(p, &tailp);
2316 if (tailp == p) {
2317 monitor_printf(mon, "Number expected\n");
2318 return -1;
2319 }
2320 if (d != d || d - d != 0) {
2321 /* NaN or infinity */
2322 monitor_printf(mon, "Bad number\n");
2323 return -1;
2324 }
2325 *pval = d;
2326 *pp = tailp;
2327 return 0;
2328}
2329
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002330/*
2331 * Store the command-name in cmdname, and return a pointer to
2332 * the remaining of the command string.
2333 */
2334static const char *get_command_name(const char *cmdline,
2335 char *cmdname, size_t nlen)
2336{
2337 size_t len;
2338 const char *p, *pstart;
2339
2340 p = cmdline;
2341 while (qemu_isspace(*p))
2342 p++;
2343 if (*p == '\0')
2344 return NULL;
2345 pstart = p;
2346 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2347 p++;
2348 len = p - pstart;
2349 if (len > nlen - 1)
2350 len = nlen - 1;
2351 memcpy(cmdname, pstart, len);
2352 cmdname[len] = '\0';
2353 return p;
2354}
2355
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002356/**
2357 * Read key of 'type' into 'key' and return the current
2358 * 'type' pointer.
2359 */
2360static char *key_get_info(const char *type, char **key)
2361{
2362 size_t len;
2363 char *p, *str;
2364
2365 if (*type == ',')
2366 type++;
2367
2368 p = strchr(type, ':');
2369 if (!p) {
2370 *key = NULL;
2371 return NULL;
2372 }
2373 len = p - type;
2374
Anthony Liguori7267c092011-08-20 22:09:37 -05002375 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002376 memcpy(str, type, len);
2377 str[len] = '\0';
2378
2379 *key = str;
2380 return ++p;
2381}
2382
bellard9307c4c2004-04-04 12:57:25 +00002383static int default_fmt_format = 'x';
2384static int default_fmt_size = 4;
2385
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002386static int is_valid_option(const char *c, const char *typestr)
2387{
2388 char option[3];
2389
2390 option[0] = '-';
2391 option[1] = *c;
2392 option[2] = '\0';
2393
2394 typestr = strstr(typestr, option);
2395 return (typestr != NULL);
2396}
2397
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03002398static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
2399 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002400{
2401 const mon_cmd_t *cmd;
2402
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03002403 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002404 if (compare_cmd(cmdname, cmd->name)) {
2405 return cmd;
2406 }
2407 }
2408
2409 return NULL;
2410}
2411
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03002412static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
2413{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002414 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03002415}
2416
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002417/*
Bandan Dasae502122015-06-03 18:38:08 -04002418 * Parse command name from @cmdp according to command table @table.
2419 * If blank, return NULL.
2420 * Else, if no valid command can be found, report to @mon, and return
2421 * NULL.
2422 * Else, change @cmdp to point right behind the name, and return its
2423 * command table entry.
2424 * Do not assume the return value points into @table! It doesn't when
2425 * the command is found in a sub-command table.
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002426 */
Anthony Liguoric227f092009-10-01 16:12:16 -05002427static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Bandan Dasae502122015-06-03 18:38:08 -04002428 const char **cmdp,
2429 mon_cmd_t *table)
bellard9307c4c2004-04-04 12:57:25 +00002430{
Bandan Dasae502122015-06-03 18:38:08 -04002431 const char *p;
Anthony Liguoric227f092009-10-01 16:12:16 -05002432 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002433 char cmdname[256];
bellard9dc39cb2004-03-14 21:38:27 +00002434
bellard9307c4c2004-04-04 12:57:25 +00002435 /* extract the command name */
Bandan Dasae502122015-06-03 18:38:08 -04002436 p = get_command_name(*cmdp, cmdname, sizeof(cmdname));
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002437 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002438 return NULL;
ths3b46e622007-09-17 08:09:54 +00002439
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002440 cmd = search_dispatch_table(table, cmdname);
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002441 if (!cmd) {
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002442 monitor_printf(mon, "unknown command: '%.*s'\n",
Bandan Dasae502122015-06-03 18:38:08 -04002443 (int)(p - *cmdp), *cmdp);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002444 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002445 }
bellard9307c4c2004-04-04 12:57:25 +00002446
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002447 /* filter out following useless space */
2448 while (qemu_isspace(*p)) {
2449 p++;
2450 }
Bandan Dasae502122015-06-03 18:38:08 -04002451
2452 *cmdp = p;
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002453 /* search sub command */
Bandan Dasae502122015-06-03 18:38:08 -04002454 if (cmd->sub_table != NULL && *p != '\0') {
2455 return monitor_parse_command(mon, cmdp, cmd->sub_table);
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002456 }
2457
Bandan Dasae502122015-06-03 18:38:08 -04002458 return cmd;
2459}
2460
2461/*
2462 * Parse arguments for @cmd.
2463 * If it can't be parsed, report to @mon, and return NULL.
2464 * Else, insert command arguments into a QDict, and return it.
2465 * Note: On success, caller has to free the QDict structure.
2466 */
2467
2468static QDict *monitor_parse_arguments(Monitor *mon,
2469 const char **endp,
2470 const mon_cmd_t *cmd)
2471{
2472 const char *typestr;
2473 char *key;
2474 int c;
2475 const char *p = *endp;
2476 char buf[1024];
2477 QDict *qdict = qdict_new();
2478
bellard9307c4c2004-04-04 12:57:25 +00002479 /* parse the parameters */
2480 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00002481 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002482 typestr = key_get_info(typestr, &key);
2483 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00002484 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002485 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00002486 typestr++;
2487 switch(c) {
2488 case 'F':
bellard81d09122004-07-14 17:21:37 +00002489 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002490 case 's':
2491 {
2492 int ret;
ths3b46e622007-09-17 08:09:54 +00002493
blueswir1cd390082008-11-16 13:53:32 +00002494 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002495 p++;
2496 if (*typestr == '?') {
2497 typestr++;
2498 if (*p == '\0') {
2499 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03002500 break;
bellard9307c4c2004-04-04 12:57:25 +00002501 }
2502 }
2503 ret = get_str(buf, sizeof(buf), &p);
2504 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002505 switch(c) {
2506 case 'F':
aliguori376253e2009-03-05 23:01:23 +00002507 monitor_printf(mon, "%s: filename expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002508 cmd->name);
bellard81d09122004-07-14 17:21:37 +00002509 break;
2510 case 'B':
aliguori376253e2009-03-05 23:01:23 +00002511 monitor_printf(mon, "%s: block device name expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002512 cmd->name);
bellard81d09122004-07-14 17:21:37 +00002513 break;
2514 default:
Bandan Dasae502122015-06-03 18:38:08 -04002515 monitor_printf(mon, "%s: string expected\n", cmd->name);
bellard81d09122004-07-14 17:21:37 +00002516 break;
2517 }
bellard9307c4c2004-04-04 12:57:25 +00002518 goto fail;
2519 }
Luiz Capitulino53773582009-08-28 15:27:25 -03002520 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00002521 }
2522 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01002523 case 'O':
2524 {
2525 QemuOptsList *opts_list;
2526 QemuOpts *opts;
2527
2528 opts_list = qemu_find_opts(key);
2529 if (!opts_list || opts_list->desc->name) {
2530 goto bad_type;
2531 }
2532 while (qemu_isspace(*p)) {
2533 p++;
2534 }
2535 if (!*p)
2536 break;
2537 if (get_str(buf, sizeof(buf), &p) < 0) {
2538 goto fail;
2539 }
Markus Armbruster70b94332015-02-13 12:50:26 +01002540 opts = qemu_opts_parse_noisily(opts_list, buf, true);
Markus Armbruster361127d2010-02-10 20:24:35 +01002541 if (!opts) {
2542 goto fail;
2543 }
2544 qemu_opts_to_qdict(opts, qdict);
2545 qemu_opts_del(opts);
2546 }
2547 break;
bellard9307c4c2004-04-04 12:57:25 +00002548 case '/':
2549 {
2550 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002551
blueswir1cd390082008-11-16 13:53:32 +00002552 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002553 p++;
2554 if (*p == '/') {
2555 /* format found */
2556 p++;
2557 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002558 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002559 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002560 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002561 count = count * 10 + (*p - '0');
2562 p++;
2563 }
2564 }
2565 size = -1;
2566 format = -1;
2567 for(;;) {
2568 switch(*p) {
2569 case 'o':
2570 case 'd':
2571 case 'u':
2572 case 'x':
2573 case 'i':
2574 case 'c':
2575 format = *p++;
2576 break;
2577 case 'b':
2578 size = 1;
2579 p++;
2580 break;
2581 case 'h':
2582 size = 2;
2583 p++;
2584 break;
2585 case 'w':
2586 size = 4;
2587 p++;
2588 break;
2589 case 'g':
2590 case 'L':
2591 size = 8;
2592 p++;
2593 break;
2594 default:
2595 goto next;
2596 }
2597 }
2598 next:
blueswir1cd390082008-11-16 13:53:32 +00002599 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00002600 monitor_printf(mon, "invalid char in format: '%c'\n",
2601 *p);
bellard9307c4c2004-04-04 12:57:25 +00002602 goto fail;
2603 }
bellard9307c4c2004-04-04 12:57:25 +00002604 if (format < 0)
2605 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002606 if (format != 'i') {
2607 /* for 'i', not specifying a size gives -1 as size */
2608 if (size < 0)
2609 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002610 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002611 }
bellard9307c4c2004-04-04 12:57:25 +00002612 default_fmt_format = format;
2613 } else {
2614 count = 1;
2615 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002616 if (format != 'i') {
2617 size = default_fmt_size;
2618 } else {
2619 size = -1;
2620 }
bellard9307c4c2004-04-04 12:57:25 +00002621 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002622 qdict_put(qdict, "count", qint_from_int(count));
2623 qdict_put(qdict, "format", qint_from_int(format));
2624 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00002625 }
2626 break;
2627 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002628 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002629 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00002630 {
blueswir1c2efc952007-09-25 17:28:42 +00002631 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00002632
blueswir1cd390082008-11-16 13:53:32 +00002633 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002634 p++;
bellard34405572004-06-08 00:55:58 +00002635 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002636 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03002637 if (*p == '\0') {
2638 typestr++;
2639 break;
2640 }
bellard34405572004-06-08 00:55:58 +00002641 } else {
2642 if (*p == '.') {
2643 p++;
blueswir1cd390082008-11-16 13:53:32 +00002644 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00002645 p++;
bellard34405572004-06-08 00:55:58 +00002646 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03002647 typestr++;
2648 break;
bellard34405572004-06-08 00:55:58 +00002649 }
2650 }
bellard13224a82006-07-14 22:03:35 +00002651 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002652 }
aliguori376253e2009-03-05 23:01:23 +00002653 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00002654 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002655 /* Check if 'i' is greater than 32-bit */
2656 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
Bandan Dasae502122015-06-03 18:38:08 -04002657 monitor_printf(mon, "\'%s\' has failed: ", cmd->name);
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002658 monitor_printf(mon, "integer is for 32-bit values\n");
2659 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002660 } else if (c == 'M') {
Luiz Capitulino91162842012-04-26 17:34:30 -03002661 if (val < 0) {
2662 monitor_printf(mon, "enter a positive value\n");
2663 goto fail;
2664 }
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002665 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002666 }
Luiz Capitulino53773582009-08-28 15:27:25 -03002667 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00002668 }
2669 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02002670 case 'o':
2671 {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01002672 int64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02002673 char *end;
2674
2675 while (qemu_isspace(*p)) {
2676 p++;
2677 }
2678 if (*typestr == '?') {
2679 typestr++;
2680 if (*p == '\0') {
2681 break;
2682 }
2683 }
2684 val = strtosz(p, &end);
2685 if (val < 0) {
2686 monitor_printf(mon, "invalid size\n");
2687 goto fail;
2688 }
2689 qdict_put(qdict, key, qint_from_int(val));
2690 p = end;
2691 }
2692 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01002693 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002694 {
2695 double val;
2696
2697 while (qemu_isspace(*p))
2698 p++;
2699 if (*typestr == '?') {
2700 typestr++;
2701 if (*p == '\0') {
2702 break;
2703 }
2704 }
2705 if (get_double(mon, &val, &p) < 0) {
2706 goto fail;
2707 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02002708 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01002709 switch (*p) {
2710 case 'm':
2711 val /= 1e3; p += 2; break;
2712 case 'u':
2713 val /= 1e6; p += 2; break;
2714 case 'n':
2715 val /= 1e9; p += 2; break;
2716 }
2717 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002718 if (*p && !qemu_isspace(*p)) {
2719 monitor_printf(mon, "Unknown unit suffix\n");
2720 goto fail;
2721 }
2722 qdict_put(qdict, key, qfloat_from_double(val));
2723 }
2724 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002725 case 'b':
2726 {
2727 const char *beg;
Eric Blakefc48ffc2015-05-15 16:24:59 -06002728 bool val;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002729
2730 while (qemu_isspace(*p)) {
2731 p++;
2732 }
2733 beg = p;
2734 while (qemu_isgraph(*p)) {
2735 p++;
2736 }
2737 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06002738 val = true;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002739 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06002740 val = false;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002741 } else {
2742 monitor_printf(mon, "Expected 'on' or 'off'\n");
2743 goto fail;
2744 }
Eric Blakefc48ffc2015-05-15 16:24:59 -06002745 qdict_put(qdict, key, qbool_from_bool(val));
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002746 }
2747 break;
bellard9307c4c2004-04-04 12:57:25 +00002748 case '-':
2749 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002750 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03002751 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00002752 /* option */
ths3b46e622007-09-17 08:09:54 +00002753
bellard9307c4c2004-04-04 12:57:25 +00002754 c = *typestr++;
2755 if (c == '\0')
2756 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00002757 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002758 p++;
bellard9307c4c2004-04-04 12:57:25 +00002759 if (*p == '-') {
2760 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002761 if(c != *p) {
2762 if(!is_valid_option(p, typestr)) {
2763
2764 monitor_printf(mon, "%s: unsupported option -%c\n",
Bandan Dasae502122015-06-03 18:38:08 -04002765 cmd->name, *p);
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002766 goto fail;
2767 } else {
2768 skip_key = 1;
2769 }
bellard9307c4c2004-04-04 12:57:25 +00002770 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002771 if(skip_key) {
2772 p = tmp;
2773 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03002774 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002775 p++;
Eric Blakefc48ffc2015-05-15 16:24:59 -06002776 qdict_put(qdict, key, qbool_from_bool(true));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002777 }
bellard9307c4c2004-04-04 12:57:25 +00002778 }
bellard9307c4c2004-04-04 12:57:25 +00002779 }
2780 break;
Wenchao Xia129be002013-08-27 20:38:26 +08002781 case 'S':
2782 {
2783 /* package all remaining string */
2784 int len;
2785
2786 while (qemu_isspace(*p)) {
2787 p++;
2788 }
2789 if (*typestr == '?') {
2790 typestr++;
2791 if (*p == '\0') {
2792 /* no remaining string: NULL argument */
2793 break;
2794 }
2795 }
2796 len = strlen(p);
2797 if (len <= 0) {
2798 monitor_printf(mon, "%s: string expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002799 cmd->name);
Bandan Dase549d2a2015-06-03 18:38:10 -04002800 goto fail;
Wenchao Xia129be002013-08-27 20:38:26 +08002801 }
2802 qdict_put(qdict, key, qstring_from_str(p));
2803 p += len;
2804 }
2805 break;
bellard9307c4c2004-04-04 12:57:25 +00002806 default:
2807 bad_type:
Bandan Dasae502122015-06-03 18:38:08 -04002808 monitor_printf(mon, "%s: unknown type '%c'\n", cmd->name, c);
bellard9307c4c2004-04-04 12:57:25 +00002809 goto fail;
2810 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002811 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002812 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00002813 }
2814 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00002815 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002816 p++;
2817 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00002818 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
Bandan Dasae502122015-06-03 18:38:08 -04002819 cmd->name);
bellard9307c4c2004-04-04 12:57:25 +00002820 goto fail;
2821 }
2822
Bandan Dasae502122015-06-03 18:38:08 -04002823 return qdict;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02002824
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002825fail:
Bandan Dasae502122015-06-03 18:38:08 -04002826 QDECREF(qdict);
Anthony Liguori7267c092011-08-20 22:09:37 -05002827 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002828 return NULL;
2829}
2830
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01002831static void handle_hmp_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002832{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002833 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05002834 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002835
Bandan Dasae502122015-06-03 18:38:08 -04002836 cmd = monitor_parse_command(mon, &cmdline, mon->cmd_table);
2837 if (!cmd) {
2838 return;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002839 }
2840
Bandan Dasae502122015-06-03 18:38:08 -04002841 qdict = monitor_parse_arguments(mon, &cmdline, cmd);
2842 if (!qdict) {
Bandan Dasdd41eea2015-06-03 18:38:09 -04002843 monitor_printf(mon, "Try \"help %s\" for more information\n",
2844 cmd->name);
Bandan Dasae502122015-06-03 18:38:08 -04002845 return;
2846 }
2847
2848 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002849 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00002850}
2851
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08002852static void cmd_completion(Monitor *mon, const char *name, const char *list)
bellard81d09122004-07-14 17:21:37 +00002853{
2854 const char *p, *pstart;
2855 char cmd[128];
2856 int len;
2857
2858 p = list;
2859 for(;;) {
2860 pstart = p;
2861 p = strchr(p, '|');
2862 if (!p)
2863 p = pstart + strlen(pstart);
2864 len = p - pstart;
2865 if (len > sizeof(cmd) - 2)
2866 len = sizeof(cmd) - 2;
2867 memcpy(cmd, pstart, len);
2868 cmd[len] = '\0';
2869 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08002870 readline_add_completion(mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00002871 }
2872 if (*p == '\0')
2873 break;
2874 p++;
2875 }
2876}
2877
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08002878static void file_completion(Monitor *mon, const char *input)
bellard81d09122004-07-14 17:21:37 +00002879{
2880 DIR *ffs;
2881 struct dirent *d;
2882 char path[1024];
2883 char file[1024], file_prefix[1024];
2884 int input_path_len;
2885 const char *p;
2886
ths5fafdf22007-09-16 21:08:06 +00002887 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00002888 if (!p) {
2889 input_path_len = 0;
2890 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00002891 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00002892 } else {
2893 input_path_len = p - input + 1;
2894 memcpy(path, input, input_path_len);
2895 if (input_path_len > sizeof(path) - 1)
2896 input_path_len = sizeof(path) - 1;
2897 path[input_path_len] = '\0';
2898 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2899 }
Bandan Das19f2db52015-06-03 18:38:07 -04002900
bellard81d09122004-07-14 17:21:37 +00002901 ffs = opendir(path);
2902 if (!ffs)
2903 return;
2904 for(;;) {
2905 struct stat sb;
2906 d = readdir(ffs);
2907 if (!d)
2908 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09002909
2910 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
2911 continue;
2912 }
2913
bellard81d09122004-07-14 17:21:37 +00002914 if (strstart(d->d_name, file_prefix, NULL)) {
2915 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00002916 if (input_path_len < sizeof(file))
2917 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2918 d->d_name);
bellard81d09122004-07-14 17:21:37 +00002919 /* stat the file to find out if it's a directory.
2920 * In that case add a slash to speed up typing long paths
2921 */
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01002922 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
blueswir1363a37d2008-08-21 17:58:08 +00002923 pstrcat(file, sizeof(file), "/");
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01002924 }
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08002925 readline_add_completion(mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00002926 }
2927 }
2928 closedir(ffs);
2929}
2930
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002931static const char *next_arg_type(const char *typestr)
2932{
2933 const char *p = strchr(typestr, ':');
2934 return (p != NULL ? ++p : typestr);
2935}
2936
Hani Benhabiles40d19392014-05-07 23:41:30 +01002937static void add_completion_option(ReadLineState *rs, const char *str,
2938 const char *option)
2939{
2940 if (!str || !option) {
2941 return;
2942 }
2943 if (!strncmp(option, str, strlen(str))) {
2944 readline_add_completion(rs, option);
2945 }
2946}
2947
Hani Benhabiles13e315d2014-05-07 23:41:29 +01002948void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
2949{
2950 size_t len;
2951 ChardevBackendInfoList *list, *start;
2952
2953 if (nb_args != 2) {
2954 return;
2955 }
2956 len = strlen(str);
2957 readline_set_completion_index(rs, len);
2958
2959 start = list = qmp_query_chardev_backends(NULL);
2960 while (list) {
2961 const char *chr_name = list->value->name;
2962
2963 if (!strncmp(chr_name, str, len)) {
2964 readline_add_completion(rs, chr_name);
2965 }
2966 list = list->next;
2967 }
2968 qapi_free_ChardevBackendInfoList(start);
2969}
2970
Hani Benhabilesb162b492014-05-07 23:41:31 +01002971void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
2972{
2973 size_t len;
2974 int i;
2975
2976 if (nb_args != 2) {
2977 return;
2978 }
2979 len = strlen(str);
2980 readline_set_completion_index(rs, len);
2981 for (i = 0; NetClientOptionsKind_lookup[i]; i++) {
2982 add_completion_option(rs, str, NetClientOptionsKind_lookup[i]);
2983 }
2984}
2985
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01002986void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles992d3e62014-02-06 23:30:11 +01002987{
2988 GSList *list, *elt;
2989 size_t len;
2990
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01002991 if (nb_args != 2) {
2992 return;
2993 }
2994
Hani Benhabiles992d3e62014-02-06 23:30:11 +01002995 len = strlen(str);
2996 readline_set_completion_index(rs, len);
2997 list = elt = object_class_get_list(TYPE_DEVICE, false);
2998 while (elt) {
2999 const char *name;
3000 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
3001 TYPE_DEVICE);
3002 name = object_class_get_name(OBJECT_CLASS(dc));
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003003
3004 if (!dc->cannot_instantiate_with_device_add_yet
3005 && !strncmp(name, str, len)) {
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003006 readline_add_completion(rs, name);
3007 }
3008 elt = elt->next;
3009 }
3010 g_slist_free(list);
3011}
3012
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003013void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003014{
3015 GSList *list, *elt;
3016 size_t len;
3017
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003018 if (nb_args != 2) {
3019 return;
3020 }
3021
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003022 len = strlen(str);
3023 readline_set_completion_index(rs, len);
3024 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
3025 while (elt) {
3026 const char *name;
3027
3028 name = object_class_get_name(OBJECT_CLASS(elt->data));
3029 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
3030 readline_add_completion(rs, name);
3031 }
3032 elt = elt->next;
3033 }
3034 g_slist_free(list);
3035}
3036
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003037static void peripheral_device_del_completion(ReadLineState *rs,
3038 const char *str, size_t len)
3039{
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003040 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
3041 GSList *list, *item;
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003042
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003043 list = qdev_build_hotpluggable_device_list(peripheral);
3044 if (!list) {
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003045 return;
3046 }
3047
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003048 for (item = list; item; item = g_slist_next(item)) {
3049 DeviceState *dev = item->data;
3050
3051 if (dev->id && !strncmp(str, dev->id, len)) {
3052 readline_add_completion(rs, dev->id);
3053 }
3054 }
3055
3056 g_slist_free(list);
3057}
3058
Hani Benhabiles6297d9a2014-05-07 23:41:28 +01003059void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3060{
3061 size_t len;
3062 ChardevInfoList *list, *start;
3063
3064 if (nb_args != 2) {
3065 return;
3066 }
3067 len = strlen(str);
3068 readline_set_completion_index(rs, len);
3069
3070 start = list = qmp_query_chardev(NULL);
3071 while (list) {
3072 ChardevInfo *chr = list->value;
3073
3074 if (!strncmp(chr->label, str, len)) {
3075 readline_add_completion(rs, chr->label);
3076 }
3077 list = list->next;
3078 }
3079 qapi_free_ChardevInfoList(start);
3080}
3081
Hani Benhabiles8e597772014-05-27 23:39:30 +01003082static void ringbuf_completion(ReadLineState *rs, const char *str)
3083{
3084 size_t len;
3085 ChardevInfoList *list, *start;
3086
3087 len = strlen(str);
3088 readline_set_completion_index(rs, len);
3089
3090 start = list = qmp_query_chardev(NULL);
3091 while (list) {
3092 ChardevInfo *chr_info = list->value;
3093
3094 if (!strncmp(chr_info->label, str, len)) {
3095 CharDriverState *chr = qemu_chr_find(chr_info->label);
3096 if (chr && chr_is_ringbuf(chr)) {
3097 readline_add_completion(rs, chr_info->label);
3098 }
3099 }
3100 list = list->next;
3101 }
3102 qapi_free_ChardevInfoList(start);
3103}
3104
Hani Benhabiles8e597772014-05-27 23:39:30 +01003105void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
3106{
3107 if (nb_args != 2) {
3108 return;
3109 }
3110 ringbuf_completion(rs, str);
3111}
3112
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003113void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
3114{
3115 size_t len;
3116
3117 if (nb_args != 2) {
3118 return;
3119 }
3120
3121 len = strlen(str);
3122 readline_set_completion_index(rs, len);
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003123 peripheral_device_del_completion(rs, str, len);
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003124}
3125
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003126void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003127{
3128 ObjectPropertyInfoList *list, *start;
3129 size_t len;
3130
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003131 if (nb_args != 2) {
3132 return;
3133 }
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003134 len = strlen(str);
3135 readline_set_completion_index(rs, len);
3136
3137 start = list = qmp_qom_list("/objects", NULL);
3138 while (list) {
3139 ObjectPropertyInfo *info = list->value;
3140
3141 if (!strncmp(info->type, "child<", 5)
3142 && !strncmp(info->name, str, len)) {
3143 readline_add_completion(rs, info->name);
3144 }
3145 list = list->next;
3146 }
3147 qapi_free_ObjectPropertyInfoList(start);
3148}
3149
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003150void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
3151{
3152 int i;
3153 char *sep;
3154 size_t len;
3155
3156 if (nb_args != 2) {
3157 return;
3158 }
3159 sep = strrchr(str, '-');
3160 if (sep) {
3161 str = sep + 1;
3162 }
3163 len = strlen(str);
3164 readline_set_completion_index(rs, len);
3165 for (i = 0; i < Q_KEY_CODE_MAX; i++) {
3166 if (!strncmp(str, QKeyCode_lookup[i], len)) {
3167 readline_add_completion(rs, QKeyCode_lookup[i]);
3168 }
3169 }
3170}
3171
Hani Benhabiles40d19392014-05-07 23:41:30 +01003172void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
3173{
3174 size_t len;
3175
3176 len = strlen(str);
3177 readline_set_completion_index(rs, len);
3178 if (nb_args == 2) {
Jason Wangeaed4832015-04-23 14:21:38 +08003179 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles40d19392014-05-07 23:41:30 +01003180 int count, i;
3181 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08003182 NET_CLIENT_OPTIONS_KIND_NONE,
3183 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003184 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles40d19392014-05-07 23:41:30 +01003185 const char *name = ncs[i]->name;
3186 if (!strncmp(str, name, len)) {
3187 readline_add_completion(rs, name);
3188 }
3189 }
3190 } else if (nb_args == 3) {
3191 add_completion_option(rs, str, "on");
3192 add_completion_option(rs, str, "off");
3193 }
3194}
3195
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003196void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
3197{
3198 int len, count, i;
Jason Wangeaed4832015-04-23 14:21:38 +08003199 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003200
3201 if (nb_args != 2) {
3202 return;
3203 }
3204
3205 len = strlen(str);
3206 readline_set_completion_index(rs, len);
3207 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_OPTIONS_KIND_NIC,
Jason Wangeaed4832015-04-23 14:21:38 +08003208 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003209 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003210 QemuOpts *opts;
3211 const char *name = ncs[i]->name;
3212 if (strncmp(str, name, len)) {
3213 continue;
3214 }
3215 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
3216 if (opts) {
3217 readline_add_completion(rs, name);
3218 }
3219 }
3220}
3221
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003222void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
3223{
3224 size_t len;
3225
3226 len = strlen(str);
3227 readline_set_completion_index(rs, len);
3228 if (nb_args == 2) {
3229 TraceEventID id;
3230 for (id = 0; id < trace_event_count(); id++) {
3231 const char *event_name = trace_event_get_name(trace_event_id(id));
3232 if (!strncmp(str, event_name, len)) {
3233 readline_add_completion(rs, event_name);
3234 }
3235 }
3236 } else if (nb_args == 3) {
3237 add_completion_option(rs, str, "on");
3238 add_completion_option(rs, str, "off");
3239 }
3240}
3241
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003242void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
3243{
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003244 int i;
3245
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003246 if (nb_args != 2) {
3247 return;
3248 }
3249 readline_set_completion_index(rs, strlen(str));
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003250 for (i = 0; WatchdogExpirationAction_lookup[i]; i++) {
3251 add_completion_option(rs, str, WatchdogExpirationAction_lookup[i]);
3252 }
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003253}
3254
Hani Benhabilesc68a0402014-05-27 23:39:32 +01003255void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
3256 const char *str)
3257{
3258 size_t len;
3259
3260 len = strlen(str);
3261 readline_set_completion_index(rs, len);
3262 if (nb_args == 2) {
3263 int i;
3264 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
3265 const char *name = MigrationCapability_lookup[i];
3266 if (!strncmp(str, name, len)) {
3267 readline_add_completion(rs, name);
3268 }
3269 }
3270 } else if (nb_args == 3) {
3271 add_completion_option(rs, str, "on");
3272 add_completion_option(rs, str, "off");
3273 }
3274}
3275
Liang Li50e9a622015-03-23 16:32:29 +08003276void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
3277 const char *str)
3278{
3279 size_t len;
3280
3281 len = strlen(str);
3282 readline_set_completion_index(rs, len);
3283 if (nb_args == 2) {
3284 int i;
3285 for (i = 0; i < MIGRATION_PARAMETER_MAX; i++) {
3286 const char *name = MigrationParameter_lookup[i];
3287 if (!strncmp(str, name, len)) {
3288 readline_add_completion(rs, name);
3289 }
3290 }
3291 }
3292}
3293
Hani Benhabilese3bb5322014-05-27 23:39:34 +01003294void host_net_add_completion(ReadLineState *rs, int nb_args, const char *str)
3295{
3296 int i;
3297 size_t len;
3298 if (nb_args != 2) {
3299 return;
3300 }
3301 len = strlen(str);
3302 readline_set_completion_index(rs, len);
3303 for (i = 0; host_net_devices[i]; i++) {
3304 if (!strncmp(host_net_devices[i], str, len)) {
3305 readline_add_completion(rs, host_net_devices[i]);
3306 }
3307 }
3308}
3309
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003310void host_net_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3311{
Jason Wangeaed4832015-04-23 14:21:38 +08003312 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003313 int count, i, len;
3314
3315 len = strlen(str);
3316 readline_set_completion_index(rs, len);
3317 if (nb_args == 2) {
3318 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08003319 NET_CLIENT_OPTIONS_KIND_NONE,
3320 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003321 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003322 int id;
3323 char name[16];
3324
3325 if (net_hub_id_for_client(ncs[i], &id)) {
3326 continue;
3327 }
3328 snprintf(name, sizeof(name), "%d", id);
3329 if (!strncmp(str, name, len)) {
3330 readline_add_completion(rs, name);
3331 }
3332 }
3333 return;
3334 } else if (nb_args == 3) {
3335 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08003336 NET_CLIENT_OPTIONS_KIND_NIC,
3337 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003338 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Jason Wang2c4681f2015-02-02 15:06:38 +08003339 int id;
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003340 const char *name;
3341
Jason Wang2c4681f2015-02-02 15:06:38 +08003342 if (ncs[i]->info->type == NET_CLIENT_OPTIONS_KIND_HUBPORT ||
3343 net_hub_id_for_client(ncs[i], &id)) {
3344 continue;
3345 }
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003346 name = ncs[i]->name;
3347 if (!strncmp(str, name, len)) {
3348 readline_add_completion(rs, name);
3349 }
3350 }
3351 return;
3352 }
3353}
3354
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003355static void vm_completion(ReadLineState *rs, const char *str)
3356{
3357 size_t len;
3358 BlockDriverState *bs = NULL;
3359
3360 len = strlen(str);
3361 readline_set_completion_index(rs, len);
3362 while ((bs = bdrv_next(bs))) {
3363 SnapshotInfoList *snapshots, *snapshot;
3364
3365 if (!bdrv_can_snapshot(bs)) {
3366 continue;
3367 }
3368 if (bdrv_query_snapshot_info_list(bs, &snapshots, NULL)) {
3369 continue;
3370 }
3371 snapshot = snapshots;
3372 while (snapshot) {
3373 char *completion = snapshot->value->name;
3374 if (!strncmp(str, completion, len)) {
3375 readline_add_completion(rs, completion);
3376 }
3377 completion = snapshot->value->id;
3378 if (!strncmp(str, completion, len)) {
3379 readline_add_completion(rs, completion);
3380 }
3381 snapshot = snapshot->next;
3382 }
3383 qapi_free_SnapshotInfoList(snapshots);
3384 }
3385
3386}
3387
3388void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
3389{
3390 if (nb_args == 2) {
3391 vm_completion(rs, str);
3392 }
3393}
3394
3395void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
3396{
3397 if (nb_args == 2) {
3398 vm_completion(rs, str);
3399 }
3400}
3401
Wenchao Xiac35b6402013-08-27 20:38:24 +08003402static void monitor_find_completion_by_table(Monitor *mon,
3403 const mon_cmd_t *cmd_table,
3404 char **args,
3405 int nb_args)
bellard81d09122004-07-14 17:21:37 +00003406{
3407 const char *cmdname;
Wenchao Xiac35b6402013-08-27 20:38:24 +08003408 int i;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003409 const char *ptype, *str, *name;
Anthony Liguoric227f092009-10-01 16:12:16 -05003410 const mon_cmd_t *cmd;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003411 BlockDriverState *bs;
bellard81d09122004-07-14 17:21:37 +00003412
bellard81d09122004-07-14 17:21:37 +00003413 if (nb_args <= 1) {
3414 /* command completion */
3415 if (nb_args == 0)
3416 cmdname = "";
3417 else
3418 cmdname = args[0];
Wenchao Xiad2674b22013-08-27 20:38:16 +08003419 readline_set_completion_index(mon->rs, strlen(cmdname));
Wenchao Xiac35b6402013-08-27 20:38:24 +08003420 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003421 cmd_completion(mon, cmdname, cmd->name);
bellard81d09122004-07-14 17:21:37 +00003422 }
3423 } else {
3424 /* find the command */
Wenchao Xiac35b6402013-08-27 20:38:24 +08003425 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Jan Kiszka03a63482010-06-16 00:38:33 +02003426 if (compare_cmd(args[0], cmd->name)) {
3427 break;
3428 }
bellard81d09122004-07-14 17:21:37 +00003429 }
Jan Kiszka03a63482010-06-16 00:38:33 +02003430 if (!cmd->name) {
Wenchao Xiac35b6402013-08-27 20:38:24 +08003431 return;
Jan Kiszka03a63482010-06-16 00:38:33 +02003432 }
3433
Wenchao Xiad903a772013-08-27 20:38:25 +08003434 if (cmd->sub_table) {
3435 /* do the job again */
Stefan Weile7ae7712015-03-08 19:30:01 +01003436 monitor_find_completion_by_table(mon, cmd->sub_table,
3437 &args[1], nb_args - 1);
3438 return;
Wenchao Xiad903a772013-08-27 20:38:25 +08003439 }
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003440 if (cmd->command_completion) {
Stefan Weile7ae7712015-03-08 19:30:01 +01003441 cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
3442 return;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003443 }
Wenchao Xiad903a772013-08-27 20:38:25 +08003444
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003445 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003446 for(i = 0; i < nb_args - 2; i++) {
3447 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003448 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003449 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003450 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003451 }
3452 }
3453 str = args[nb_args - 1];
Kevin Wolf48fe86f2014-11-12 16:24:02 +01003454 while (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02003455 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00003456 }
bellard81d09122004-07-14 17:21:37 +00003457 switch(*ptype) {
3458 case 'F':
3459 /* file completion */
Wenchao Xiad2674b22013-08-27 20:38:16 +08003460 readline_set_completion_index(mon->rs, strlen(str));
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003461 file_completion(mon, str);
bellard81d09122004-07-14 17:21:37 +00003462 break;
3463 case 'B':
3464 /* block device name completion */
Wenchao Xia599a9262013-08-27 20:38:15 +08003465 readline_set_completion_index(mon->rs, strlen(str));
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003466 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
3467 name = bdrv_get_device_name(bs);
3468 if (str[0] == '\0' ||
3469 !strncmp(name, str, strlen(str))) {
3470 readline_add_completion(mon->rs, name);
3471 }
3472 }
bellard81d09122004-07-14 17:21:37 +00003473 break;
bellard7fe48482004-10-09 18:08:01 +00003474 case 's':
Wenchao Xia129be002013-08-27 20:38:26 +08003475 case 'S':
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003476 if (!strcmp(cmd->name, "help|?")) {
Wenchao Xia7ca0e062013-08-27 20:38:27 +08003477 monitor_find_completion_by_table(mon, cmd_table,
3478 &args[1], nb_args - 1);
bellard7fe48482004-10-09 18:08:01 +00003479 }
3480 break;
bellard81d09122004-07-14 17:21:37 +00003481 default:
3482 break;
3483 }
3484 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08003485}
3486
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003487static void monitor_find_completion(void *opaque,
Wenchao Xiac35b6402013-08-27 20:38:24 +08003488 const char *cmdline)
3489{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003490 Monitor *mon = opaque;
Wenchao Xiac35b6402013-08-27 20:38:24 +08003491 char *args[MAX_ARGS];
3492 int nb_args, len;
3493
3494 /* 1. parse the cmdline */
3495 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
3496 return;
3497 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08003498
3499 /* if the line ends with a space, it means we want to complete the
3500 next arg */
3501 len = strlen(cmdline);
3502 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
3503 if (nb_args >= MAX_ARGS) {
3504 goto cleanup;
3505 }
3506 args[nb_args++] = g_strdup("");
3507 }
3508
3509 /* 2. auto complete according to args */
3510 monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args);
Jan Kiszka03a63482010-06-16 00:38:33 +02003511
3512cleanup:
Wenchao Xiadcc70cd2013-08-27 20:38:22 +08003513 free_cmdline_args(args, nb_args);
bellard81d09122004-07-14 17:21:37 +00003514}
3515
aliguori731b0362009-03-05 23:01:42 +00003516static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003517{
aliguori731b0362009-03-05 23:01:42 +00003518 Monitor *mon = opaque;
3519
Jan Kiszkac62313b2009-12-04 14:05:29 +01003520 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003521}
3522
Markus Armbruster40861822015-05-29 10:27:16 +02003523static bool invalid_qmp_mode(const Monitor *mon, const mon_cmd_t *cmd,
3524 Error **errp)
Luiz Capitulino09069b12010-02-04 18:10:06 -02003525{
Markus Armbruster485febc2015-03-13 17:25:50 +01003526 bool is_cap = cmd->mhandler.cmd_new == qmp_capabilities;
Markus Armbrusterf994b252015-03-06 19:51:51 +01003527
3528 if (is_cap && mon->qmp.in_command_mode) {
Markus Armbruster40861822015-05-29 10:27:16 +02003529 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
3530 "Capabilities negotiation is already complete, command "
3531 "'%s' ignored", cmd->name);
Eric Blake2d5a8342015-04-15 09:19:23 -06003532 return true;
3533 }
Markus Armbrusterf994b252015-03-06 19:51:51 +01003534 if (!is_cap && !mon->qmp.in_command_mode) {
Markus Armbruster40861822015-05-29 10:27:16 +02003535 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
3536 "Expecting capabilities negotiation with "
3537 "'qmp_capabilities' before command '%s'", cmd->name);
Eric Blake2d5a8342015-04-15 09:19:23 -06003538 return true;
3539 }
3540 return false;
Luiz Capitulino09069b12010-02-04 18:10:06 -02003541}
3542
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003543/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03003544 * Argument validation rules:
3545 *
3546 * 1. The argument must exist in cmd_args qdict
3547 * 2. The argument type must be the expected one
3548 *
3549 * Special case: If the argument doesn't exist in cmd_args and
3550 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
3551 * checking is skipped for it.
3552 */
Markus Armbruster326283a2015-03-02 18:39:09 +01003553static void check_client_args_type(const QDict *client_args,
3554 const QDict *cmd_args, int flags,
3555 Error **errp)
Luiz Capitulino4af91932010-06-22 11:44:05 -03003556{
3557 const QDictEntry *ent;
3558
3559 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
3560 QObject *obj;
3561 QString *arg_type;
3562 const QObject *client_arg = qdict_entry_value(ent);
3563 const char *client_arg_name = qdict_entry_key(ent);
3564
3565 obj = qdict_get(cmd_args, client_arg_name);
3566 if (!obj) {
3567 if (flags & QMP_ACCEPT_UNKNOWNS) {
3568 /* handler accepts unknowns */
3569 continue;
3570 }
3571 /* client arg doesn't exist */
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003572 error_setg(errp, QERR_INVALID_PARAMETER, client_arg_name);
Markus Armbruster326283a2015-03-02 18:39:09 +01003573 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003574 }
3575
3576 arg_type = qobject_to_qstring(obj);
3577 assert(arg_type != NULL);
3578
3579 /* check if argument's type is correct */
3580 switch (qstring_get_str(arg_type)[0]) {
3581 case 'F':
3582 case 'B':
3583 case 's':
3584 if (qobject_type(client_arg) != QTYPE_QSTRING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003585 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3586 client_arg_name, "string");
Markus Armbruster326283a2015-03-02 18:39:09 +01003587 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003588 }
3589 break;
3590 case 'i':
3591 case 'l':
3592 case 'M':
Jes Sorensendbc0c672010-10-21 17:15:47 +02003593 case 'o':
Luiz Capitulino4af91932010-06-22 11:44:05 -03003594 if (qobject_type(client_arg) != QTYPE_QINT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003595 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3596 client_arg_name, "int");
Markus Armbruster326283a2015-03-02 18:39:09 +01003597 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003598 }
3599 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003600 case 'T':
3601 if (qobject_type(client_arg) != QTYPE_QINT &&
3602 qobject_type(client_arg) != QTYPE_QFLOAT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003603 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3604 client_arg_name, "number");
Markus Armbruster326283a2015-03-02 18:39:09 +01003605 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003606 }
3607 break;
3608 case 'b':
3609 case '-':
3610 if (qobject_type(client_arg) != QTYPE_QBOOL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003611 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3612 client_arg_name, "bool");
Markus Armbruster326283a2015-03-02 18:39:09 +01003613 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003614 }
3615 break;
3616 case 'O':
3617 assert(flags & QMP_ACCEPT_UNKNOWNS);
3618 break;
Paolo Bonzinib9f89782012-03-22 12:51:11 +01003619 case 'q':
3620 /* Any QObject can be passed. */
3621 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003622 case '/':
3623 case '.':
3624 /*
3625 * These types are not supported by QMP and thus are not
3626 * handled here. Fall through.
3627 */
3628 default:
3629 abort();
3630 }
3631 }
Luiz Capitulino4af91932010-06-22 11:44:05 -03003632}
3633
3634/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003635 * - Check if the client has passed all mandatory args
3636 * - Set special flags for argument validation
3637 */
Markus Armbruster326283a2015-03-02 18:39:09 +01003638static void check_mandatory_args(const QDict *cmd_args,
3639 const QDict *client_args, int *flags,
3640 Error **errp)
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003641{
3642 const QDictEntry *ent;
3643
3644 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
3645 const char *cmd_arg_name = qdict_entry_key(ent);
3646 QString *type = qobject_to_qstring(qdict_entry_value(ent));
3647 assert(type != NULL);
3648
3649 if (qstring_get_str(type)[0] == 'O') {
3650 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
3651 *flags |= QMP_ACCEPT_UNKNOWNS;
3652 } else if (qstring_get_str(type)[0] != '-' &&
3653 qstring_get_str(type)[1] != '?' &&
3654 !qdict_haskey(client_args, cmd_arg_name)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003655 error_setg(errp, QERR_MISSING_PARAMETER, cmd_arg_name);
Markus Armbruster326283a2015-03-02 18:39:09 +01003656 return;
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003657 }
3658 }
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003659}
3660
3661static QDict *qdict_from_args_type(const char *args_type)
3662{
3663 int i;
3664 QDict *qdict;
3665 QString *key, *type, *cur_qs;
3666
3667 assert(args_type != NULL);
3668
3669 qdict = qdict_new();
3670
3671 if (args_type == NULL || args_type[0] == '\0') {
3672 /* no args, empty qdict */
3673 goto out;
3674 }
3675
3676 key = qstring_new();
3677 type = qstring_new();
3678
3679 cur_qs = key;
3680
3681 for (i = 0;; i++) {
3682 switch (args_type[i]) {
3683 case ',':
3684 case '\0':
3685 qdict_put(qdict, qstring_get_str(key), type);
3686 QDECREF(key);
3687 if (args_type[i] == '\0') {
3688 goto out;
3689 }
3690 type = qstring_new(); /* qdict has ref */
3691 cur_qs = key = qstring_new();
3692 break;
3693 case ':':
3694 cur_qs = type;
3695 break;
3696 default:
3697 qstring_append_chr(cur_qs, args_type[i]);
3698 break;
3699 }
3700 }
3701
3702out:
3703 return qdict;
3704}
3705
3706/*
3707 * Client argument checking rules:
3708 *
3709 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03003710 * 2. Each argument provided by the client must be expected
3711 * 3. Each argument provided by the client must have the type expected
3712 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003713 */
Markus Armbruster326283a2015-03-02 18:39:09 +01003714static void qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args,
3715 Error **errp)
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003716{
Markus Armbruster326283a2015-03-02 18:39:09 +01003717 Error *err = NULL;
3718 int flags;
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003719 QDict *cmd_args;
3720
3721 cmd_args = qdict_from_args_type(cmd->args_type);
3722
3723 flags = 0;
Markus Armbruster326283a2015-03-02 18:39:09 +01003724 check_mandatory_args(cmd_args, client_args, &flags, &err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003725 if (err) {
3726 goto out;
3727 }
3728
Markus Armbruster326283a2015-03-02 18:39:09 +01003729 check_client_args_type(client_args, cmd_args, flags, &err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003730
3731out:
Markus Armbruster326283a2015-03-02 18:39:09 +01003732 error_propagate(errp, err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003733 QDECREF(cmd_args);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003734}
3735
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003736/*
3737 * Input object checking rules
3738 *
3739 * 1. Input object must be a dict
3740 * 2. The "execute" key must exist
3741 * 3. The "execute" key must be a string
3742 * 4. If the "arguments" key exists, it must be a dict
3743 * 5. If the "id" key exists, it can be anything (ie. json-value)
3744 * 6. Any argument not listed above is considered invalid
3745 */
Markus Armbrusterba0510a2015-03-02 18:41:43 +01003746static QDict *qmp_check_input_obj(QObject *input_obj, Error **errp)
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003747{
3748 const QDictEntry *ent;
3749 int has_exec_key = 0;
3750 QDict *input_dict;
3751
3752 if (qobject_type(input_obj) != QTYPE_QDICT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003753 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "object");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003754 return NULL;
3755 }
3756
3757 input_dict = qobject_to_qdict(input_obj);
3758
3759 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
3760 const char *arg_name = qdict_entry_key(ent);
3761 const QObject *arg_obj = qdict_entry_value(ent);
3762
3763 if (!strcmp(arg_name, "execute")) {
3764 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003765 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
3766 "execute", "string");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003767 return NULL;
3768 }
3769 has_exec_key = 1;
3770 } else if (!strcmp(arg_name, "arguments")) {
3771 if (qobject_type(arg_obj) != QTYPE_QDICT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003772 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
3773 "arguments", "object");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003774 return NULL;
3775 }
Markus Armbruster779cec42015-06-08 10:44:30 +02003776 } else if (!strcmp(arg_name, "id")) {
3777 /* Any string is acceptable as "id", so nothing to check */
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003778 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003779 error_setg(errp, QERR_QMP_EXTRA_MEMBER, arg_name);
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003780 return NULL;
3781 }
3782 }
3783
3784 if (!has_exec_key) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003785 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "execute");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003786 return NULL;
3787 }
3788
3789 return input_dict;
3790}
3791
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003792static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
3793{
Markus Armbruster326283a2015-03-02 18:39:09 +01003794 Error *local_err = NULL;
Markus Armbruster84add862015-03-05 16:45:15 +01003795 QObject *obj, *data;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003796 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003797 const mon_cmd_t *cmd;
Luiz Capitulino40e5a012011-10-21 16:15:31 -02003798 const char *cmd_name;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003799 Monitor *mon = cur_mon;
3800
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03003801 args = input = NULL;
Markus Armbruster84add862015-03-05 16:45:15 +01003802 data = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003803
3804 obj = json_parser_parse(tokens, NULL);
3805 if (!obj) {
3806 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003807 error_setg(&local_err, QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003808 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003809 }
3810
Markus Armbrusterba0510a2015-03-02 18:41:43 +01003811 input = qmp_check_input_obj(obj, &local_err);
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003812 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003813 qobject_decref(obj);
3814 goto err_out;
3815 }
3816
Markus Armbruster74358f22015-03-06 19:35:59 +01003817 mon->qmp.id = qdict_get(input, "id");
3818 qobject_incref(mon->qmp.id);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003819
Luiz Capitulino0bbab462010-05-31 17:32:50 -03003820 cmd_name = qdict_get_str(input, "execute");
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +01003821 trace_handle_qmp_command(mon, cmd_name);
Anthony Liguorie3193602011-09-02 12:34:47 -05003822 cmd = qmp_find_cmd(cmd_name);
Eric Blake2d5a8342015-04-15 09:19:23 -06003823 if (!cmd) {
Markus Armbruster710aec92015-03-06 11:28:00 +01003824 error_set(&local_err, ERROR_CLASS_COMMAND_NOT_FOUND,
3825 "The command %s has not been found", cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03003826 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003827 }
Markus Armbruster40861822015-05-29 10:27:16 +02003828 if (invalid_qmp_mode(mon, cmd, &local_err)) {
Eric Blake2d5a8342015-04-15 09:19:23 -06003829 goto err_out;
3830 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003831
3832 obj = qdict_get(input, "arguments");
3833 if (!obj) {
3834 args = qdict_new();
3835 } else {
3836 args = qobject_to_qdict(obj);
3837 QINCREF(args);
3838 }
3839
Markus Armbruster326283a2015-03-02 18:39:09 +01003840 qmp_check_client_args(cmd, args, &local_err);
3841 if (local_err) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003842 goto err_out;
3843 }
3844
Markus Armbruster485febc2015-03-13 17:25:50 +01003845 cmd->mhandler.cmd_new(args, &data, &local_err);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003846
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003847err_out:
Markus Armbruster710aec92015-03-06 11:28:00 +01003848 monitor_protocol_emitter(mon, data, local_err);
Markus Armbruster84add862015-03-05 16:45:15 +01003849 qobject_decref(data);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03003850 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003851 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003852}
3853
Markus Armbrusterc83fe232015-03-06 19:20:51 +01003854static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003855{
3856 Monitor *old_mon = cur_mon;
3857
3858 cur_mon = opaque;
3859
Markus Armbruster74358f22015-03-06 19:35:59 +01003860 json_message_parser_feed(&cur_mon->qmp.parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003861
3862 cur_mon = old_mon;
3863}
3864
aliguori731b0362009-03-05 23:01:42 +00003865static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00003866{
aliguori731b0362009-03-05 23:01:42 +00003867 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00003868 int i;
aliguori376253e2009-03-05 23:01:23 +00003869
aliguori731b0362009-03-05 23:01:42 +00003870 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00003871
aliguoricde76ee2009-03-05 23:01:51 +00003872 if (cur_mon->rs) {
3873 for (i = 0; i < size; i++)
3874 readline_handle_byte(cur_mon->rs, buf[i]);
3875 } else {
3876 if (size == 0 || buf[size - 1] != 0)
3877 monitor_printf(cur_mon, "corrupted command\n");
3878 else
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003879 handle_hmp_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00003880 }
aliguori731b0362009-03-05 23:01:42 +00003881
3882 cur_mon = old_mon;
3883}
aliguorid8f44602008-10-06 13:52:44 +00003884
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003885static void monitor_command_cb(void *opaque, const char *cmdline,
3886 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00003887{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003888 Monitor *mon = opaque;
3889
aliguori731b0362009-03-05 23:01:42 +00003890 monitor_suspend(mon);
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003891 handle_hmp_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00003892 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00003893}
3894
aliguoricde76ee2009-03-05 23:01:51 +00003895int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003896{
aliguoricde76ee2009-03-05 23:01:51 +00003897 if (!mon->rs)
3898 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00003899 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00003900 return 0;
aliguorid8f44602008-10-06 13:52:44 +00003901}
3902
aliguori376253e2009-03-05 23:01:23 +00003903void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003904{
aliguoricde76ee2009-03-05 23:01:51 +00003905 if (!mon->rs)
3906 return;
aliguori731b0362009-03-05 23:01:42 +00003907 if (--mon->suspend_cnt == 0)
3908 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00003909}
3910
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003911static QObject *get_qmp_greeting(void)
3912{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03003913 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003914
Markus Armbruster7fad30f2015-09-16 13:06:19 +02003915 qmp_marshal_query_version(NULL, &ver, NULL);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003916 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
3917}
3918
Markus Armbrusterc83fe232015-03-06 19:20:51 +01003919static void monitor_qmp_event(void *opaque, int event)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003920{
Luiz Capitulino47116d12010-02-08 17:01:30 -02003921 QObject *data;
3922 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003923
Luiz Capitulino47116d12010-02-08 17:01:30 -02003924 switch (event) {
3925 case CHR_EVENT_OPENED:
Markus Armbrusterf994b252015-03-06 19:51:51 +01003926 mon->qmp.in_command_mode = false;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003927 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003928 monitor_json_emitter(mon, data);
3929 qobject_decref(data);
Corey Bryantefb87c12012-08-14 16:43:48 -04003930 mon_refcount++;
Luiz Capitulino47116d12010-02-08 17:01:30 -02003931 break;
3932 case CHR_EVENT_CLOSED:
Markus Armbruster74358f22015-03-06 19:35:59 +01003933 json_message_parser_destroy(&mon->qmp.parser);
3934 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Corey Bryantefb87c12012-08-14 16:43:48 -04003935 mon_refcount--;
3936 monitor_fdsets_cleanup();
Luiz Capitulino47116d12010-02-08 17:01:30 -02003937 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003938 }
3939}
3940
aliguori731b0362009-03-05 23:01:42 +00003941static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00003942{
aliguori376253e2009-03-05 23:01:23 +00003943 Monitor *mon = opaque;
3944
aliguori2724b182009-03-05 23:01:47 +00003945 switch (event) {
3946 case CHR_EVENT_MUX_IN:
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003947 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003948 mon->mux_out = 0;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003949 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003950 if (mon->reset_seen) {
3951 readline_restart(mon->rs);
3952 monitor_resume(mon);
3953 monitor_flush(mon);
3954 } else {
3955 mon->suspend_cnt = 0;
3956 }
aliguori2724b182009-03-05 23:01:47 +00003957 break;
ths86e94de2007-01-05 22:01:59 +00003958
aliguori2724b182009-03-05 23:01:47 +00003959 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003960 if (mon->reset_seen) {
3961 if (mon->suspend_cnt == 0) {
3962 monitor_printf(mon, "\n");
3963 }
3964 monitor_flush(mon);
3965 monitor_suspend(mon);
3966 } else {
3967 mon->suspend_cnt++;
3968 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003969 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003970 mon->mux_out = 1;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003971 qemu_mutex_unlock(&mon->out_lock);
aliguori2724b182009-03-05 23:01:47 +00003972 break;
3973
Amit Shahb6b8df52009-10-07 18:31:16 +05303974 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00003975 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3976 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003977 if (!mon->mux_out) {
Stratos Psomadakise5554e22014-09-15 15:34:57 +03003978 readline_restart(mon->rs);
aliguori2724b182009-03-05 23:01:47 +00003979 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003980 }
3981 mon->reset_seen = 1;
Corey Bryantefb87c12012-08-14 16:43:48 -04003982 mon_refcount++;
3983 break;
3984
3985 case CHR_EVENT_CLOSED:
3986 mon_refcount--;
3987 monitor_fdsets_cleanup();
aliguori2724b182009-03-05 23:01:47 +00003988 break;
3989 }
ths86e94de2007-01-05 22:01:59 +00003990}
3991
Wayne Xia816f8922011-10-12 11:32:41 +08003992static int
3993compare_mon_cmd(const void *a, const void *b)
3994{
3995 return strcmp(((const mon_cmd_t *)a)->name,
3996 ((const mon_cmd_t *)b)->name);
3997}
3998
3999static void sortcmdlist(void)
4000{
4001 int array_num;
4002 int elem_size = sizeof(mon_cmd_t);
4003
4004 array_num = sizeof(mon_cmds)/elem_size-1;
4005 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
4006
4007 array_num = sizeof(info_cmds)/elem_size-1;
4008 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
4009}
4010
aliguori76655d62009-03-06 20:27:37 +00004011
4012/*
4013 * Local variables:
4014 * c-indent-level: 4
4015 * c-basic-offset: 4
4016 * tab-width: 8
4017 * End:
4018 */
4019
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004020/* These functions just adapt the readline interface in a typesafe way. We
4021 * could cast function pointers but that discards compiler checks.
4022 */
Stefan Weild5d15072014-01-25 18:18:23 +01004023static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
4024 const char *fmt, ...)
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004025{
4026 va_list ap;
4027 va_start(ap, fmt);
4028 monitor_vprintf(opaque, fmt, ap);
4029 va_end(ap);
4030}
4031
4032static void monitor_readline_flush(void *opaque)
4033{
4034 monitor_flush(opaque);
4035}
4036
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004037static void __attribute__((constructor)) monitor_lock_init(void)
4038{
4039 qemu_mutex_init(&monitor_lock);
4040}
4041
aliguori731b0362009-03-05 23:01:42 +00004042void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004043{
aliguori731b0362009-03-05 23:01:42 +00004044 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004045 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004046
4047 if (is_first_init) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +02004048 monitor_qapi_event_init();
Wenchao Xiad0383172013-08-27 20:38:18 +08004049 sortcmdlist();
ths20d8a3e2007-02-18 17:04:49 +00004050 is_first_init = 0;
4051 }
aliguori87127162009-03-05 23:01:29 +00004052
Wenchao Xiab01fe892013-08-27 20:38:19 +08004053 mon = g_malloc(sizeof(*mon));
4054 monitor_data_init(mon);
ths20d8a3e2007-02-18 17:04:49 +00004055
aliguori87127162009-03-05 23:01:29 +00004056 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00004057 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004058 if (flags & MONITOR_USE_READLINE) {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004059 mon->rs = readline_init(monitor_readline_printf,
4060 monitor_readline_flush,
4061 mon,
4062 monitor_find_completion);
aliguoricde76ee2009-03-05 23:01:51 +00004063 monitor_read_command(mon, 0);
4064 }
aliguori87127162009-03-05 23:01:29 +00004065
Markus Armbruster9f3982f2015-03-06 19:56:38 +01004066 if (monitor_is_qmp(mon)) {
Markus Armbrusterc83fe232015-03-06 19:20:51 +01004067 qemu_chr_add_handlers(chr, monitor_can_read, monitor_qmp_read,
4068 monitor_qmp_event, mon);
Anthony Liguori15f31512011-08-15 11:17:35 -05004069 qemu_chr_fe_set_echo(chr, true);
Markus Armbruster74358f22015-03-06 19:35:59 +01004070 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004071 } else {
4072 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4073 monitor_event, mon);
4074 }
aliguori87127162009-03-05 23:01:29 +00004075
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004076 qemu_mutex_lock(&monitor_lock);
Blue Swirl72cf2d42009-09-12 07:36:22 +00004077 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004078 qemu_mutex_unlock(&monitor_lock);
bellard7e2515e2004-08-01 21:52:19 +00004079}
4080
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004081static void bdrv_password_cb(void *opaque, const char *password,
4082 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00004083{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004084 Monitor *mon = opaque;
4085 BlockDriverState *bs = readline_opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004086 int ret = 0;
Markus Armbruster4d2855a2015-01-29 10:37:00 +01004087 Error *local_err = NULL;
bellard7e2515e2004-08-01 21:52:19 +00004088
Markus Armbruster4d2855a2015-01-29 10:37:00 +01004089 bdrv_add_key(bs, password, &local_err);
4090 if (local_err) {
4091 monitor_printf(mon, "%s\n", error_get_pretty(local_err));
4092 error_free(local_err);
aliguoribb5fc202009-03-05 23:01:15 +00004093 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004094 }
aliguori731b0362009-03-05 23:01:42 +00004095 if (mon->password_completion_cb)
4096 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004097
aliguori731b0362009-03-05 23:01:42 +00004098 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004099}
aliguoric0f4ce72009-03-05 23:01:01 +00004100
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004101int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
Markus Armbruster097310b2014-10-07 13:59:15 +02004102 BlockCompletionFunc *completion_cb,
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004103 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004104{
aliguoricde76ee2009-03-05 23:01:51 +00004105 int err;
4106
aliguori376253e2009-03-05 23:01:23 +00004107 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4108 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00004109
aliguori731b0362009-03-05 23:01:42 +00004110 mon->password_completion_cb = completion_cb;
4111 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004112
aliguoricde76ee2009-03-05 23:01:51 +00004113 err = monitor_read_password(mon, bdrv_password_cb, bs);
4114
4115 if (err && completion_cb)
4116 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004117
4118 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00004119}
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004120
4121int monitor_read_block_device_key(Monitor *mon, const char *device,
Markus Armbruster097310b2014-10-07 13:59:15 +02004122 BlockCompletionFunc *completion_cb,
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004123 void *opaque)
4124{
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01004125 Error *err = NULL;
Fam Zheng55606252015-03-02 19:36:46 +08004126 BlockBackend *blk;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004127
Fam Zheng55606252015-03-02 19:36:46 +08004128 blk = blk_by_name(device);
4129 if (!blk) {
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004130 monitor_printf(mon, "Device not found %s\n", device);
4131 return -1;
4132 }
4133
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01004134 bdrv_add_key(blk_bs(blk), NULL, &err);
4135 if (err) {
4136 error_free(err);
4137 return monitor_read_bdrv_key_start(mon, blk_bs(blk), completion_cb, opaque);
4138 }
4139
4140 if (completion_cb) {
4141 completion_cb(opaque, 0);
4142 }
4143 return 0;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004144}
Paolo Bonzini4d454572012-11-26 16:03:42 +01004145
4146QemuOptsList qemu_mon_opts = {
4147 .name = "mon",
4148 .implied_opt_name = "chardev",
4149 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
4150 .desc = {
4151 {
4152 .name = "mode",
4153 .type = QEMU_OPT_STRING,
4154 },{
4155 .name = "chardev",
4156 .type = QEMU_OPT_STRING,
4157 },{
4158 .name = "default",
4159 .type = QEMU_OPT_BOOL,
4160 },{
4161 .name = "pretty",
4162 .type = QEMU_OPT_BOOL,
4163 },
4164 { /* end of list */ }
4165 },
4166};
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004167
4168#ifndef TARGET_I386
4169void qmp_rtc_reset_reinjection(Error **errp)
4170{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004171 error_setg(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection");
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004172}
4173#endif
Jason J. Herne7ee0c3e2015-06-26 14:03:16 -04004174
4175#ifndef TARGET_S390X
4176void qmp_dump_skeys(const char *filename, Error **errp)
4177{
4178 error_setg(errp, QERR_FEATURE_DISABLED, "dump-skeys");
4179}
4180#endif