blob: ec4096b770c003e69b460543d4af5749fa79e983 [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"
Markus Armbruster39a18152015-09-16 13:06:28 +020077#include "qmp-introspect.h"
Fam Zheng55606252015-03-02 19:36:46 +080078#include "sysemu/block-backend.h"
ths6a5bd302007-12-03 17:05:38 +000079
Markus Armbruster1ce6be22015-02-06 14:18:24 +010080/* for hmp_info_irq/pic */
Jan Kiszka661f1922011-10-16 11:53:13 +020081#if defined(TARGET_SPARC)
Paolo Bonzini0d09e412013-02-05 17:06:20 +010082#include "hw/sparc/sun4m.h"
Jan Kiszka661f1922011-10-16 11:53:13 +020083#endif
Paolo Bonzini0d09e412013-02-05 17:06:20 +010084#include "hw/lm32/lm32_pic.h"
Jan Kiszka661f1922011-10-16 11:53:13 +020085
Jason J. Hernea4538a52015-06-26 14:07:21 -040086#if defined(TARGET_S390X)
87#include "hw/s390x/storage-keys.h"
88#endif
89
bellard9307c4c2004-04-04 12:57:25 +000090/*
91 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000092 *
bellard9307c4c2004-04-04 12:57:25 +000093 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000094 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000095 * 's' string (accept optional quote)
Wenchao Xia129be002013-08-27 20:38:26 +080096 * 'S' it just appends the rest of the string (accept optional quote)
Markus Armbruster361127d2010-02-10 20:24:35 +010097 * 'O' option string of the form NAME=VALUE,...
98 * parsed according to QemuOptsList given by its name
99 * Example: 'device:O' uses qemu_device_opts.
100 * Restriction: only lists with empty desc are supported
101 * TODO lift the restriction
bellard92a31b12005-02-10 22:00:52 +0000102 * 'i' 32 bit integer
103 * 'l' target long (32 or 64 bit)
Luiz Capitulino91162842012-04-26 17:34:30 -0300104 * 'M' Non-negative target long (32 or 64 bit), in user mode the
105 * value is multiplied by 2^20 (think Mebibyte)
Jes Sorensendbc0c672010-10-21 17:15:47 +0200106 * 'o' octets (aka bytes)
Kevin Wolf5e009842013-06-05 14:19:27 +0200107 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
108 * K, k suffix, which multiplies the value by 2^60 for suffixes E
109 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
110 * 2^30 for suffixes G and g, 2^20 for M and m, 2^10 for K and k
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +0100111 * 'T' double
112 * user mode accepts an optional ms, us, ns suffix,
113 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +0000114 * '/' optional gdb-like print format (like "/10x")
115 *
Luiz Capitulinofb466602009-08-28 15:27:27 -0300116 * '?' optional type (for all types, except '/')
117 * '.' other form of optional type (for 'i' and 'l')
Markus Armbruster942cd1f2010-03-26 09:07:09 +0100118 * 'b' boolean
119 * user mode accepts "on" or "off"
Luiz Capitulinofb466602009-08-28 15:27:27 -0300120 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +0000121 *
122 */
123
Anthony Liguoric227f092009-10-01 16:12:16 -0500124typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +0000125 const char *name;
bellard9307c4c2004-04-04 12:57:25 +0000126 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +0000127 const char *params;
128 const char *help;
Luiz Capitulino910df892009-10-07 13:41:51 -0300129 union {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -0300130 void (*cmd)(Monitor *mon, const QDict *qdict);
Markus Armbruster485febc2015-03-13 17:25:50 +0100131 void (*cmd_new)(QDict *params, QObject **ret_data, Error **errp);
Luiz Capitulino910df892009-10-07 13:41:51 -0300132 } mhandler;
Wenchao Xia5f3d3352013-01-14 14:06:27 +0800133 /* @sub_table is a list of 2nd level of commands. If it do not exist,
134 * mhandler should be used. If it exist, sub_table[?].mhandler should be
135 * used, and mhandler of 1st level plays the role of help function.
136 */
137 struct mon_cmd_t *sub_table;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +0100138 void (*command_completion)(ReadLineState *rs, int nb_args, const char *str);
Anthony Liguoric227f092009-10-01 16:12:16 -0500139} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000140
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100141/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500142typedef struct mon_fd_t mon_fd_t;
143struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100144 char *name;
145 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500146 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100147};
148
Corey Bryantba1c0482012-08-14 16:43:43 -0400149/* file descriptor associated with a file descriptor set */
150typedef struct MonFdsetFd MonFdsetFd;
151struct MonFdsetFd {
152 int fd;
153 bool removed;
154 char *opaque;
155 QLIST_ENTRY(MonFdsetFd) next;
156};
157
158/* file descriptor set containing fds passed via SCM_RIGHTS */
159typedef struct MonFdset MonFdset;
160struct MonFdset {
161 int64_t id;
162 QLIST_HEAD(, MonFdsetFd) fds;
Corey Bryantadb696f2012-08-14 16:43:47 -0400163 QLIST_HEAD(, MonFdsetFd) dup_fds;
Corey Bryantba1c0482012-08-14 16:43:43 -0400164 QLIST_ENTRY(MonFdset) next;
165};
166
Markus Armbruster74358f22015-03-06 19:35:59 +0100167typedef struct {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200168 QObject *id;
169 JSONMessageParser parser;
Markus Armbrusterf994b252015-03-06 19:51:51 +0100170 /*
171 * When a client connects, we're in capabilities negotiation mode.
172 * When command qmp_capabilities succeeds, we go into command
173 * mode.
174 */
175 bool in_command_mode; /* are we in command mode? */
Markus Armbruster74358f22015-03-06 19:35:59 +0100176} MonitorQMP;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200177
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100178/*
179 * To prevent flooding clients, events can be throttled. The
180 * throttling is calculated globally, rather than per-Monitor
181 * instance.
182 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200183typedef struct MonitorQAPIEventState {
184 QAPIEvent event; /* Event being tracked */
185 int64_t rate; /* Minimum time (in ns) between two events */
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100186 QEMUTimer *timer; /* Timer for handling delayed events */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200187 QDict *qdict; /* Delayed event (if any) */
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
Pavel Butsykincaf15312015-09-22 16:18:17 +0300374int monitor_fprintf(FILE *stream, const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000375{
376 va_list ap;
377 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000378 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000379 va_end(ap);
380 return 0;
381}
382
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200383static void monitor_json_emitter(Monitor *mon, const QObject *data)
384{
385 QString *json;
386
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200387 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
388 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200389 assert(json != NULL);
390
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200391 qstring_append_chr(json, '\n');
392 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200393
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200394 QDECREF(json);
395}
396
Markus Armbruster710aec92015-03-06 11:28:00 +0100397static QDict *build_qmp_error_dict(Error *err)
Luiz Capitulinode253f12012-07-27 16:18:16 -0300398{
399 QObject *obj;
400
Markus Armbruster710aec92015-03-06 11:28:00 +0100401 obj = qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %s } }",
402 ErrorClass_lookup[error_get_class(err)],
403 error_get_pretty(err));
Luiz Capitulinode253f12012-07-27 16:18:16 -0300404
405 return qobject_to_qdict(obj);
406}
407
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100408static void monitor_protocol_emitter(Monitor *mon, QObject *data,
Markus Armbruster710aec92015-03-06 11:28:00 +0100409 Error *err)
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200410{
411 QDict *qmp;
412
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +0100413 trace_monitor_protocol_emitter(mon);
414
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100415 if (!err) {
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200416 /* success response */
Luiz Capitulinode253f12012-07-27 16:18:16 -0300417 qmp = qdict_new();
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200418 if (data) {
419 qobject_incref(data);
420 qdict_put_obj(qmp, "return", data);
421 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200422 /* return an empty QDict by default */
423 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200424 }
425 } else {
426 /* error response */
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100427 qmp = build_qmp_error_dict(err);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200428 }
429
Markus Armbruster74358f22015-03-06 19:35:59 +0100430 if (mon->qmp.id) {
431 qdict_put_obj(qmp, "id", mon->qmp.id);
432 mon->qmp.id = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200433 }
434
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200435 monitor_json_emitter(mon, QOBJECT(qmp));
436 QDECREF(qmp);
437}
438
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200439
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200440static MonitorQAPIEventState monitor_qapi_event_state[QAPI_EVENT_MAX];
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100441
442/*
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200443 * Emits the event to every monitor instance, @event is only used for trace
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200444 * Called with monitor_lock held.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100445 */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200446static void monitor_qapi_event_emit(QAPIEvent event, QDict *qdict)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100447{
448 Monitor *mon;
449
Markus Armbruster688b4b72015-10-15 17:08:30 +0200450 trace_monitor_protocol_event_emit(event, qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100451 QLIST_FOREACH(mon, &mon_list, entry) {
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100452 if (monitor_is_qmp(mon) && mon->qmp.in_command_mode) {
Markus Armbruster688b4b72015-10-15 17:08:30 +0200453 monitor_json_emitter(mon, QOBJECT(qdict));
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100454 }
455 }
456}
457
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100458/*
459 * Queue a new event for emission to Monitor instances,
460 * applying any rate limiting if required.
461 */
462static void
Markus Armbruster688b4b72015-10-15 17:08:30 +0200463monitor_qapi_event_queue(QAPIEvent event, QDict *qdict, Error **errp)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100464{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200465 MonitorQAPIEventState *evstate;
Markus Armbruster93f8f982015-10-15 17:08:31 +0200466
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200467 assert(event < QAPI_EVENT_MAX);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200468 evstate = &monitor_qapi_event_state[event];
469 trace_monitor_protocol_event_queue(event, qdict, evstate->rate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100470
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200471 qemu_mutex_lock(&monitor_lock);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200472
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100473 if (!evstate->rate) {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200474 /* Unthrottled event */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200475 monitor_qapi_event_emit(event, qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100476 } else {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200477 if (timer_pending(evstate->timer)) {
478 /*
479 * Timer is pending for (at least) evstate->rate ns after
480 * last send. Store event for sending when timer fires,
481 * replacing a prior stored event if any.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100482 */
Markus Armbruster93f8f982015-10-15 17:08:31 +0200483 QDECREF(evstate->qdict);
Markus Armbruster688b4b72015-10-15 17:08:30 +0200484 evstate->qdict = qdict;
485 QINCREF(evstate->qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100486 } else {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200487 /*
488 * Last send was (at least) evstate->rate ns ago.
489 * Send immediately, and arm the timer to call
490 * monitor_qapi_event_handler() in evstate->rate ns. Any
491 * events arriving before then will be delayed until then.
492 */
493 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
494
Markus Armbruster688b4b72015-10-15 17:08:30 +0200495 monitor_qapi_event_emit(event, qdict);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200496 timer_mod_ns(evstate->timer, now + evstate->rate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100497 }
498 }
Markus Armbruster93f8f982015-10-15 17:08:31 +0200499
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200500 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100501}
502
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100503/*
Markus Armbruster93f8f982015-10-15 17:08:31 +0200504 * This function runs evstate->rate ns after sending a throttled
505 * event.
506 * If another event has since been stored, send it.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100507 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200508static void monitor_qapi_event_handler(void *opaque)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100509{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200510 MonitorQAPIEventState *evstate = opaque;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100511
Markus Armbruster93f8f982015-10-15 17:08:31 +0200512 trace_monitor_protocol_event_handler(evstate->event, evstate->qdict);
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200513 qemu_mutex_lock(&monitor_lock);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200514
Markus Armbruster688b4b72015-10-15 17:08:30 +0200515 if (evstate->qdict) {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200516 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
517
Markus Armbruster688b4b72015-10-15 17:08:30 +0200518 monitor_qapi_event_emit(evstate->event, evstate->qdict);
519 QDECREF(evstate->qdict);
520 evstate->qdict = NULL;
Markus Armbruster93f8f982015-10-15 17:08:31 +0200521 timer_mod_ns(evstate->timer, now + evstate->rate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100522 }
Markus Armbruster93f8f982015-10-15 17:08:31 +0200523
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200524 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100525}
526
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100527/*
528 * @event: the event ID to be limited
529 * @rate: the rate limit in milliseconds
530 *
531 * Sets a rate limit on a particular event, so no
532 * more than 1 event will be emitted within @rate
533 * milliseconds
534 */
535static void
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200536monitor_qapi_event_throttle(QAPIEvent event, int64_t rate)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100537{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200538 MonitorQAPIEventState *evstate;
539 assert(event < QAPI_EVENT_MAX);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100540
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200541 evstate = &(monitor_qapi_event_state[event]);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100542
543 trace_monitor_protocol_event_throttle(event, rate);
544 evstate->event = event;
Wenchao Xia2f44a082014-06-24 16:34:00 -0700545 assert(rate * SCALE_MS <= INT64_MAX);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100546 evstate->rate = rate * SCALE_MS;
Markus Armbruster688b4b72015-10-15 17:08:30 +0200547 evstate->qdict = NULL;
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200548 evstate->timer = timer_new(QEMU_CLOCK_REALTIME,
549 SCALE_MS,
550 monitor_qapi_event_handler,
551 evstate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100552}
553
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200554static void monitor_qapi_event_init(void)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100555{
Wenchao Xiae010ad82014-06-18 08:43:41 +0200556 /* Limit guest-triggerable events to 1 per second */
557 monitor_qapi_event_throttle(QAPI_EVENT_RTC_CHANGE, 1000);
Wenchao Xia99eaf092014-06-18 08:43:42 +0200558 monitor_qapi_event_throttle(QAPI_EVENT_WATCHDOG, 1000);
Wenchao Xiaaef9d312014-06-18 08:43:51 +0200559 monitor_qapi_event_throttle(QAPI_EVENT_BALLOON_CHANGE, 1000);
Wenchao Xiafe069d92014-06-18 08:43:53 +0200560 monitor_qapi_event_throttle(QAPI_EVENT_QUORUM_REPORT_BAD, 1000);
561 monitor_qapi_event_throttle(QAPI_EVENT_QUORUM_FAILURE, 1000);
Laszlo Erseke2ae6152014-06-26 17:50:02 +0200562 monitor_qapi_event_throttle(QAPI_EVENT_VSERPORT_CHANGE, 1000);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100563
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200564 qmp_event_set_func_emit(monitor_qapi_event_queue);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200565}
566
Markus Armbruster485febc2015-03-13 17:25:50 +0100567static void qmp_capabilities(QDict *params, QObject **ret_data, Error **errp)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200568{
Markus Armbruster485febc2015-03-13 17:25:50 +0100569 cur_mon->qmp.in_command_mode = true;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200570}
571
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100572static void handle_hmp_command(Monitor *mon, const char *cmdline);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200573
Wenchao Xiab01fe892013-08-27 20:38:19 +0800574static void monitor_data_init(Monitor *mon)
575{
576 memset(mon, 0, sizeof(Monitor));
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200577 qemu_mutex_init(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800578 mon->outbuf = qstring_new();
Wenchao Xia77172392013-08-27 20:38:20 +0800579 /* Use *mon_cmds by default. */
580 mon->cmd_table = mon_cmds;
Wenchao Xiab01fe892013-08-27 20:38:19 +0800581}
582
583static void monitor_data_destroy(Monitor *mon)
584{
585 QDECREF(mon->outbuf);
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200586 qemu_mutex_destroy(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800587}
588
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200589char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
590 int64_t cpu_index, Error **errp)
Luiz Capitulino0268d972010-10-22 10:08:02 -0200591{
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200592 char *output = NULL;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200593 Monitor *old_mon, hmp;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200594
Wenchao Xiab01fe892013-08-27 20:38:19 +0800595 monitor_data_init(&hmp);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400596 hmp.skip_flush = true;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200597
598 old_mon = cur_mon;
599 cur_mon = &hmp;
600
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200601 if (has_cpu_index) {
602 int ret = monitor_set_cpu(cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200603 if (ret < 0) {
604 cur_mon = old_mon;
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100605 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
606 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200607 goto out;
608 }
609 }
610
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100611 handle_hmp_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200612 cur_mon = old_mon;
613
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200614 qemu_mutex_lock(&hmp.out_lock);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400615 if (qstring_get_length(hmp.outbuf) > 0) {
616 output = g_strdup(qstring_get_str(hmp.outbuf));
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200617 } else {
618 output = g_strdup("");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200619 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200620 qemu_mutex_unlock(&hmp.out_lock);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200621
622out:
Wenchao Xiab01fe892013-08-27 20:38:19 +0800623 monitor_data_destroy(&hmp);
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200624 return output;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200625}
626
bellard9dc39cb2004-03-14 21:38:27 +0000627static int compare_cmd(const char *name, const char *list)
628{
629 const char *p, *pstart;
630 int len;
631 len = strlen(name);
632 p = list;
633 for(;;) {
634 pstart = p;
635 p = strchr(p, '|');
636 if (!p)
637 p = pstart + strlen(pstart);
638 if ((p - pstart) == len && !memcmp(pstart, name, len))
639 return 1;
640 if (*p == '\0')
641 break;
642 p++;
643 }
644 return 0;
645}
646
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800647static int get_str(char *buf, int buf_size, const char **pp)
648{
649 const char *p;
650 char *q;
651 int c;
652
653 q = buf;
654 p = *pp;
655 while (qemu_isspace(*p)) {
656 p++;
657 }
658 if (*p == '\0') {
659 fail:
660 *q = '\0';
661 *pp = p;
662 return -1;
663 }
664 if (*p == '\"') {
665 p++;
666 while (*p != '\0' && *p != '\"') {
667 if (*p == '\\') {
668 p++;
669 c = *p++;
670 switch (c) {
671 case 'n':
672 c = '\n';
673 break;
674 case 'r':
675 c = '\r';
676 break;
677 case '\\':
678 case '\'':
679 case '\"':
680 break;
681 default:
Peter Maydell71baf782015-08-19 16:20:19 +0100682 printf("unsupported escape code: '\\%c'\n", c);
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800683 goto fail;
684 }
685 if ((q - buf) < buf_size - 1) {
686 *q++ = c;
687 }
688 } else {
689 if ((q - buf) < buf_size - 1) {
690 *q++ = *p;
691 }
692 p++;
693 }
694 }
695 if (*p != '\"') {
Peter Maydell71baf782015-08-19 16:20:19 +0100696 printf("unterminated string\n");
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800697 goto fail;
698 }
699 p++;
700 } else {
701 while (*p != '\0' && !qemu_isspace(*p)) {
702 if ((q - buf) < buf_size - 1) {
703 *q++ = *p;
704 }
705 p++;
706 }
707 }
708 *q = '\0';
709 *pp = p;
710 return 0;
711}
712
713#define MAX_ARGS 16
714
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800715static void free_cmdline_args(char **args, int nb_args)
716{
717 int i;
718
719 assert(nb_args <= MAX_ARGS);
720
721 for (i = 0; i < nb_args; i++) {
722 g_free(args[i]);
723 }
724
725}
726
727/*
728 * Parse the command line to get valid args.
729 * @cmdline: command line to be parsed.
730 * @pnb_args: location to store the number of args, must NOT be NULL.
731 * @args: location to store the args, which should be freed by caller, must
732 * NOT be NULL.
733 *
734 * Returns 0 on success, negative on failure.
735 *
736 * NOTE: this parser is an approximate form of the real command parser. Number
737 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
738 * return with failure.
739 */
740static int parse_cmdline(const char *cmdline,
741 int *pnb_args, char **args)
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800742{
743 const char *p;
744 int nb_args, ret;
745 char buf[1024];
746
747 p = cmdline;
748 nb_args = 0;
749 for (;;) {
750 while (qemu_isspace(*p)) {
751 p++;
752 }
753 if (*p == '\0') {
754 break;
755 }
756 if (nb_args >= MAX_ARGS) {
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800757 goto fail;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800758 }
759 ret = get_str(buf, sizeof(buf), &p);
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800760 if (ret < 0) {
761 goto fail;
762 }
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800763 args[nb_args] = g_strdup(buf);
764 nb_args++;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800765 }
766 *pnb_args = nb_args;
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800767 return 0;
768
769 fail:
770 free_cmdline_args(args, nb_args);
771 return -1;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800772}
773
Wenchao Xia66855492013-08-27 20:38:23 +0800774static void help_cmd_dump_one(Monitor *mon,
775 const mon_cmd_t *cmd,
776 char **prefix_args,
777 int prefix_args_nb)
778{
779 int i;
780
781 for (i = 0; i < prefix_args_nb; i++) {
782 monitor_printf(mon, "%s ", prefix_args[i]);
783 }
784 monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
785}
786
787/* @args[@arg_index] is the valid command need to find in @cmds */
Anthony Liguoric227f092009-10-01 16:12:16 -0500788static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
Wenchao Xia66855492013-08-27 20:38:23 +0800789 char **args, int nb_args, int arg_index)
bellard9dc39cb2004-03-14 21:38:27 +0000790{
Anthony Liguoric227f092009-10-01 16:12:16 -0500791 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000792
Wenchao Xia66855492013-08-27 20:38:23 +0800793 /* No valid arg need to compare with, dump all in *cmds */
794 if (arg_index >= nb_args) {
795 for (cmd = cmds; cmd->name != NULL; cmd++) {
796 help_cmd_dump_one(mon, cmd, args, arg_index);
797 }
798 return;
799 }
800
801 /* Find one entry to dump */
802 for (cmd = cmds; cmd->name != NULL; cmd++) {
803 if (compare_cmd(args[arg_index], cmd->name)) {
804 if (cmd->sub_table) {
805 /* continue with next arg */
806 help_cmd_dump(mon, cmd->sub_table,
807 args, nb_args, arg_index + 1);
808 } else {
809 help_cmd_dump_one(mon, cmd, args, arg_index);
810 }
811 break;
812 }
bellard9dc39cb2004-03-14 21:38:27 +0000813 }
814}
815
aliguori376253e2009-03-05 23:01:23 +0000816static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000817{
Wenchao Xia66855492013-08-27 20:38:23 +0800818 char *args[MAX_ARGS];
819 int nb_args = 0;
820
821 /* 1. parse user input */
822 if (name) {
823 /* special case for log, directly dump and return */
824 if (!strcmp(name, "log")) {
Peter Maydell38dad9e2013-02-11 16:41:25 +0000825 const QEMULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000826 monitor_printf(mon, "Log items (comma separated):\n");
827 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
Peter Maydell38dad9e2013-02-11 16:41:25 +0000828 for (item = qemu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000829 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000830 }
Wenchao Xia66855492013-08-27 20:38:23 +0800831 return;
832 }
833
834 if (parse_cmdline(name, &nb_args, args) < 0) {
835 return;
bellardf193c792004-03-21 17:06:25 +0000836 }
bellard9dc39cb2004-03-14 21:38:27 +0000837 }
Wenchao Xia66855492013-08-27 20:38:23 +0800838
839 /* 2. dump the contents according to parsed args */
840 help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0);
841
842 free_cmdline_args(args, nb_args);
bellard9dc39cb2004-03-14 21:38:27 +0000843}
844
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300845static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300846{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300847 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300848}
849
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100850static void hmp_trace_event(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530851{
852 const char *tp_name = qdict_get_str(qdict, "name");
853 bool new_state = qdict_get_bool(qdict, "option");
Lluís Vilanova14101d02014-08-25 13:20:03 +0200854 Error *local_err = NULL;
Blue Swirlf871d682010-10-13 19:14:29 +0000855
Lluís Vilanova14101d02014-08-25 13:20:03 +0200856 qmp_trace_event_set_state(tp_name, new_state, true, true, &local_err);
857 if (local_err) {
Markus Armbruster091e38b2015-02-10 15:15:43 +0100858 error_report_err(local_err);
Blue Swirlf871d682010-10-13 19:14:29 +0000859 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530860}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100861
Michael Rothc45a8162011-10-02 08:44:37 -0500862#ifdef CONFIG_TRACE_SIMPLE
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100863static void hmp_trace_file(Monitor *mon, const QDict *qdict)
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100864{
865 const char *op = qdict_get_try_str(qdict, "op");
866 const char *arg = qdict_get_try_str(qdict, "arg");
867
868 if (!op) {
869 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
870 } else if (!strcmp(op, "on")) {
871 st_set_trace_file_enabled(true);
872 } else if (!strcmp(op, "off")) {
873 st_set_trace_file_enabled(false);
874 } else if (!strcmp(op, "flush")) {
875 st_flush_trace_buffer();
876 } else if (!strcmp(op, "set")) {
877 if (arg) {
878 st_set_trace_file(arg);
879 }
880 } else {
881 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
882 help_cmd(mon, "trace-file");
883 }
884}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530885#endif
886
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100887static void hmp_info_help(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000888{
Luiz Capitulino13c74252009-10-07 13:41:55 -0300889 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000890}
891
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300892CommandInfoList *qmp_query_commands(Error **errp)
bellard9bc9d1c2004-10-10 15:15:51 +0000893{
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300894 CommandInfoList *info, *cmd_list = NULL;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200895 const mon_cmd_t *cmd;
896
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300897 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino40e5a012011-10-21 16:15:31 -0200898 info = g_malloc0(sizeof(*info));
899 info->value = g_malloc0(sizeof(*info->value));
900 info->value->name = g_strdup(cmd->name);
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200901
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300902 info->next = cmd_list;
903 cmd_list = info;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200904 }
905
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300906 return cmd_list;
thsa36e69d2007-12-02 05:18:19 +0000907}
908
Daniel P. Berrange48608532012-05-21 17:59:51 +0100909EventInfoList *qmp_query_events(Error **errp)
910{
911 EventInfoList *info, *ev_list = NULL;
Wenchao Xia75175172014-06-18 08:43:54 +0200912 QAPIEvent e;
Daniel P. Berrange48608532012-05-21 17:59:51 +0100913
Wenchao Xia75175172014-06-18 08:43:54 +0200914 for (e = 0 ; e < QAPI_EVENT_MAX ; e++) {
915 const char *event_name = QAPIEvent_lookup[e];
Daniel P. Berrange48608532012-05-21 17:59:51 +0100916 assert(event_name != NULL);
917 info = g_malloc0(sizeof(*info));
918 info->value = g_malloc0(sizeof(*info->value));
919 info->value->name = g_strdup(event_name);
920
921 info->next = ev_list;
922 ev_list = info;
923 }
924
925 return ev_list;
926}
927
Markus Armbruster39a18152015-09-16 13:06:28 +0200928/*
929 * Minor hack: generated marshalling suppressed for this command
930 * ('gen': false in the schema) so we can parse the JSON string
931 * directly into QObject instead of first parsing it with
932 * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
933 * to QObject with generated output marshallers, every time. Instead,
934 * we do it in test-qmp-input-visitor.c, just to make sure
935 * qapi-introspect.py's output actually conforms to the schema.
936 */
937static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
938 Error **errp)
939{
940 *ret_data = qobject_from_json(qmp_schema_json);
941}
942
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300943/* set the current CPU defined by the user */
944int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000945{
Andreas Färber55e5c282012-12-17 06:18:02 +0100946 CPUState *cpu;
bellard6a00d602005-11-21 23:25:50 +0000947
Andreas Färber1c8bb3c2013-02-15 17:01:09 +0100948 cpu = qemu_get_cpu(cpu_index);
949 if (cpu == NULL) {
950 return -1;
bellard6a00d602005-11-21 23:25:50 +0000951 }
Andreas Färbercb446ec2013-05-01 14:24:52 +0200952 cur_mon->mon_cpu = cpu;
Andreas Färber1c8bb3c2013-02-15 17:01:09 +0100953 return 0;
bellard6a00d602005-11-21 23:25:50 +0000954}
955
Pavel Butsykincaf15312015-09-22 16:18:17 +0300956CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000957{
aliguori731b0362009-03-05 23:01:42 +0000958 if (!cur_mon->mon_cpu) {
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300959 monitor_set_cpu(0);
bellard6a00d602005-11-21 23:25:50 +0000960 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300961 cpu_synchronize_state(cur_mon->mon_cpu);
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700962 return cur_mon->mon_cpu;
963}
964
Pavel Butsykinbf957282015-09-10 18:38:59 +0300965CPUArchState *mon_get_cpu_env(void)
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700966{
967 return mon_get_cpu()->env_ptr;
bellard6a00d602005-11-21 23:25:50 +0000968}
969
Luiz Capitulino99b77962011-10-24 10:53:44 -0200970int monitor_get_cpu_index(void)
971{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700972 return mon_get_cpu()->cpu_index;
Luiz Capitulino99b77962011-10-24 10:53:44 -0200973}
974
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100975static void hmp_info_registers(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000976{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700977 cpu_dump_state(mon_get_cpu(), (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000978}
979
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100980static void hmp_info_jit(Monitor *mon, const QDict *qdict)
bellarde3db7222005-01-26 22:00:47 +0000981{
aliguori376253e2009-03-05 23:01:23 +0000982 dump_exec_info((FILE *)mon, monitor_fprintf);
Sebastian Tanase27498be2014-07-25 11:56:33 +0200983 dump_drift_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000984}
985
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100986static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
Max Filippov246ae242014-11-02 11:04:18 +0300987{
988 dump_opcount_info((FILE *)mon, monitor_fprintf);
989}
990
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100991static void hmp_info_history(Monitor *mon, const QDict *qdict)
bellardaa455482004-04-04 13:07:25 +0000992{
993 int i;
bellard7e2515e2004-08-01 21:52:19 +0000994 const char *str;
ths3b46e622007-09-17 08:09:54 +0000995
aliguoricde76ee2009-03-05 23:01:51 +0000996 if (!mon->rs)
997 return;
bellard7e2515e2004-08-01 21:52:19 +0000998 i = 0;
999 for(;;) {
aliguori731b0362009-03-05 23:01:42 +00001000 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +00001001 if (!str)
1002 break;
aliguori376253e2009-03-05 23:01:23 +00001003 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +00001004 i++;
bellardaa455482004-04-04 13:07:25 +00001005 }
1006}
1007
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001008static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
j_mayer76a66252007-03-07 08:32:30 +00001009{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001010 cpu_dump_statistics(mon_get_cpu(), (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +00001011}
j_mayer76a66252007-03-07 08:32:30 +00001012
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001013static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301014{
Lluís Vilanova14101d02014-08-25 13:20:03 +02001015 TraceEventInfoList *events = qmp_trace_event_get_state("*", NULL);
1016 TraceEventInfoList *elem;
1017
1018 for (elem = events; elem != NULL; elem = elem->next) {
1019 monitor_printf(mon, "%s : state %u\n",
1020 elem->value->name,
1021 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
1022 }
1023 qapi_free_TraceEventInfoList(events);
Prerna Saxena22890ab2010-06-24 17:04:53 +05301024}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301025
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001026void qmp_client_migrate_info(const char *protocol, const char *hostname,
1027 bool has_port, int64_t port,
1028 bool has_tls_port, int64_t tls_port,
1029 bool has_cert_subject, const char *cert_subject,
1030 Error **errp)
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001031{
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001032 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001033 if (!qemu_using_spice(errp)) {
1034 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001035 }
1036
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001037 if (!has_port && !has_tls_port) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001038 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001039 return;
Yonit Halperin6ec5dae2012-03-18 09:42:39 +02001040 }
1041
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001042 if (qemu_spice_migrate_info(hostname,
1043 has_port ? port : -1,
1044 has_tls_port ? tls_port : -1,
1045 cert_subject)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001046 error_setg(errp, QERR_UNDEFINED_ERROR);
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001047 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001048 }
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001049 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001050 }
1051
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001052 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001053}
1054
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001055static void hmp_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001056{
Peter Maydell9a7e5422013-02-11 16:41:20 +00001057 qemu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001058}
1059
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001060static void hmp_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001061{
1062 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001063 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001064
bellard9307c4c2004-04-04 12:57:25 +00001065 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001066 mask = 0;
1067 } else {
Peter Maydell4fde1eb2013-02-11 16:41:22 +00001068 mask = qemu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001069 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001070 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001071 return;
1072 }
1073 }
Peter Maydell24537a02013-02-11 16:41:23 +00001074 qemu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00001075}
1076
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001077static void hmp_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001078{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001079 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001080 if (!option || !strcmp(option, "on")) {
1081 singlestep = 1;
1082 } else if (!strcmp(option, "off")) {
1083 singlestep = 0;
1084 } else {
1085 monitor_printf(mon, "unexpected option %s\n", option);
1086 }
1087}
1088
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001089static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001090{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001091 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001092 if (!device)
1093 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1094 if (gdbserver_start(device) < 0) {
1095 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1096 device);
1097 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001098 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001099 } else {
aliguori59030a82009-04-05 18:43:41 +00001100 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1101 device);
bellard8a7ddc32004-03-31 19:00:16 +00001102 }
1103}
1104
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001105static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001106{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001107 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001108 if (select_watchdog_action(action) == -1) {
1109 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1110 }
1111}
1112
aliguori376253e2009-03-05 23:01:23 +00001113static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001114{
aliguori376253e2009-03-05 23:01:23 +00001115 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001116 switch(c) {
1117 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001118 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001119 break;
1120 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001121 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001122 break;
1123 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001124 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001125 break;
1126 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001127 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001128 break;
1129 default:
1130 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001131 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001132 } else {
aliguori376253e2009-03-05 23:01:23 +00001133 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001134 }
1135 break;
1136 }
aliguori376253e2009-03-05 23:01:23 +00001137 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001138}
1139
aliguori376253e2009-03-05 23:01:23 +00001140static void memory_dump(Monitor *mon, int count, int format, int wsize,
Avi Kivitya8170e52012-10-23 12:30:10 +02001141 hwaddr addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001142{
Blue Swirl23842aa2010-01-12 20:27:43 +00001143 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001144 uint8_t buf[16];
1145 uint64_t v;
1146
1147 if (format == 'i') {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001148 int flags = 0;
bellard9307c4c2004-04-04 12:57:25 +00001149#ifdef TARGET_I386
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001150 CPUArchState *env = mon_get_cpu_env();
bellard4c27ba22004-04-25 18:05:08 +00001151 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001152 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001153 } else if (wsize == 4) {
1154 flags = 0;
1155 } else {
bellard6a15fd12006-04-12 21:07:07 +00001156 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001157 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001158 if (env) {
1159#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001160 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001161 (env->segs[R_CS].flags & DESC_L_MASK))
1162 flags = 2;
1163 else
1164#endif
1165 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1166 flags = 1;
1167 }
bellard4c27ba22004-04-25 18:05:08 +00001168 }
1169#endif
Tom Musta1c38f842014-04-09 14:53:24 -05001170#ifdef TARGET_PPC
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001171 CPUArchState *env = mon_get_cpu_env();
Tom Musta1c38f842014-04-09 14:53:24 -05001172 flags = msr_le << 16;
1173 flags |= env->bfd_mach;
1174#endif
Peter Crosthwaited49190c2015-05-24 14:20:41 -07001175 monitor_disas(mon, mon_get_cpu(), addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001176 return;
1177 }
1178
1179 len = wsize * count;
1180 if (wsize == 1)
1181 line_size = 8;
1182 else
1183 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001184 max_digits = 0;
1185
1186 switch(format) {
1187 case 'o':
1188 max_digits = (wsize * 8 + 2) / 3;
1189 break;
1190 default:
1191 case 'x':
1192 max_digits = (wsize * 8) / 4;
1193 break;
1194 case 'u':
1195 case 'd':
1196 max_digits = (wsize * 8 * 10 + 32) / 33;
1197 break;
1198 case 'c':
1199 wsize = 1;
1200 break;
1201 }
1202
1203 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001204 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001205 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001206 else
aliguori376253e2009-03-05 23:01:23 +00001207 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001208 l = len;
1209 if (l > line_size)
1210 l = line_size;
1211 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001212 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001213 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001214 if (cpu_memory_rw_debug(mon_get_cpu(), addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001215 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001216 break;
1217 }
bellard9307c4c2004-04-04 12:57:25 +00001218 }
ths5fafdf22007-09-16 21:08:06 +00001219 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001220 while (i < l) {
1221 switch(wsize) {
1222 default:
1223 case 1:
Peter Maydell24e60302015-01-20 15:19:32 +00001224 v = ldub_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001225 break;
1226 case 2:
Peter Maydell24e60302015-01-20 15:19:32 +00001227 v = lduw_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001228 break;
1229 case 4:
Peter Maydell24e60302015-01-20 15:19:32 +00001230 v = (uint32_t)ldl_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001231 break;
1232 case 8:
Peter Maydell24e60302015-01-20 15:19:32 +00001233 v = ldq_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001234 break;
1235 }
aliguori376253e2009-03-05 23:01:23 +00001236 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001237 switch(format) {
1238 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001239 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001240 break;
1241 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001242 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001243 break;
1244 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001245 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001246 break;
1247 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001248 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001249 break;
1250 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001251 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001252 break;
1253 }
1254 i += wsize;
1255 }
aliguori376253e2009-03-05 23:01:23 +00001256 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001257 addr += l;
1258 len -= l;
1259 }
1260}
1261
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001262static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001263{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001264 int count = qdict_get_int(qdict, "count");
1265 int format = qdict_get_int(qdict, "format");
1266 int size = qdict_get_int(qdict, "size");
1267 target_long addr = qdict_get_int(qdict, "addr");
1268
aliguori376253e2009-03-05 23:01:23 +00001269 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001270}
1271
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001272static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001273{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001274 int count = qdict_get_int(qdict, "count");
1275 int format = qdict_get_int(qdict, "format");
1276 int size = qdict_get_int(qdict, "size");
Avi Kivitya8170e52012-10-23 12:30:10 +02001277 hwaddr addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001278
aliguori376253e2009-03-05 23:01:23 +00001279 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001280}
1281
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001282static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001283{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001284 int format = qdict_get_int(qdict, "format");
Avi Kivitya8170e52012-10-23 12:30:10 +02001285 hwaddr val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001286
bellard9307c4c2004-04-04 12:57:25 +00001287 switch(format) {
1288 case 'o':
Avi Kivitya8170e52012-10-23 12:30:10 +02001289 monitor_printf(mon, "%#" HWADDR_PRIo, val);
bellard9307c4c2004-04-04 12:57:25 +00001290 break;
1291 case 'x':
Avi Kivitya8170e52012-10-23 12:30:10 +02001292 monitor_printf(mon, "%#" HWADDR_PRIx, val);
bellard9307c4c2004-04-04 12:57:25 +00001293 break;
1294 case 'u':
Avi Kivitya8170e52012-10-23 12:30:10 +02001295 monitor_printf(mon, "%" HWADDR_PRIu, val);
bellard9307c4c2004-04-04 12:57:25 +00001296 break;
1297 default:
1298 case 'd':
Avi Kivitya8170e52012-10-23 12:30:10 +02001299 monitor_printf(mon, "%" HWADDR_PRId, val);
bellard9307c4c2004-04-04 12:57:25 +00001300 break;
1301 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001302 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001303 break;
1304 }
aliguori376253e2009-03-05 23:01:23 +00001305 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001306}
1307
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001308static void hmp_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001309{
1310 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001311 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001312 uint32_t start = qdict_get_int(qdict, "start");
1313 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001314
1315 sum = 0;
1316 for(addr = start; addr < (start + size); addr++) {
Peter Maydell42874d32015-04-26 16:49:24 +01001317 uint8_t val = address_space_ldub(&address_space_memory, addr,
1318 MEMTXATTRS_UNSPECIFIED, NULL);
bellarde4cf1ad2005-06-04 20:15:57 +00001319 /* BSD sum algorithm ('sum' Unix command) */
1320 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001321 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001322 }
aliguori376253e2009-03-05 23:01:23 +00001323 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001324}
1325
bellard13224a82006-07-14 22:03:35 +00001326static int mouse_button_state;
1327
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001328static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001329{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001330 int dx, dy, dz, button;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001331 const char *dx_str = qdict_get_str(qdict, "dx_str");
1332 const char *dy_str = qdict_get_str(qdict, "dy_str");
1333 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001334
bellard13224a82006-07-14 22:03:35 +00001335 dx = strtol(dx_str, NULL, 0);
1336 dy = strtol(dy_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001337 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
1338 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
1339
1340 if (dz_str) {
bellard13224a82006-07-14 22:03:35 +00001341 dz = strtol(dz_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001342 if (dz != 0) {
1343 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
1344 qemu_input_queue_btn(NULL, button, true);
1345 qemu_input_event_sync();
1346 qemu_input_queue_btn(NULL, button, false);
1347 }
1348 }
1349 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001350}
1351
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001352static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001353{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001354 static uint32_t bmap[INPUT_BUTTON_MAX] = {
1355 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1356 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
1357 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1358 };
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001359 int button_state = qdict_get_int(qdict, "button_state");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001360
1361 if (mouse_button_state == button_state) {
1362 return;
1363 }
1364 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1365 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001366 mouse_button_state = button_state;
bellard13224a82006-07-14 22:03:35 +00001367}
1368
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001369static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001370{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001371 int size = qdict_get_int(qdict, "size");
1372 int addr = qdict_get_int(qdict, "addr");
1373 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001374 uint32_t val;
1375 int suffix;
1376
1377 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001378 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001379 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001380 addr++;
1381 }
1382 addr &= 0xffff;
1383
1384 switch(size) {
1385 default:
1386 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001387 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001388 suffix = 'b';
1389 break;
1390 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001391 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001392 suffix = 'w';
1393 break;
1394 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001395 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001396 suffix = 'l';
1397 break;
1398 }
aliguori376253e2009-03-05 23:01:23 +00001399 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1400 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001401}
bellarda3a91a32004-06-04 11:06:21 +00001402
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001403static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001404{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001405 int size = qdict_get_int(qdict, "size");
1406 int addr = qdict_get_int(qdict, "addr");
1407 int val = qdict_get_int(qdict, "val");
1408
Jan Kiszkaf1147842009-07-14 10:20:11 +02001409 addr &= IOPORTS_MASK;
1410
1411 switch (size) {
1412 default:
1413 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001414 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001415 break;
1416 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001417 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001418 break;
1419 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001420 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001421 break;
1422 }
1423}
1424
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001425static void hmp_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001426{
Gongleif1839932014-12-03 18:20:58 +00001427 Error *local_err = NULL;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001428 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001429
Gongleif1839932014-12-03 18:20:58 +00001430 qemu_boot_set(bootdevice, &local_err);
1431 if (local_err) {
1432 monitor_printf(mon, "%s\n", error_get_pretty(local_err));
1433 error_free(local_err);
aurel320ecdffb2008-05-04 20:11:34 +00001434 } else {
Gongleif1839932014-12-03 18:20:58 +00001435 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001436 }
1437}
1438
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001439static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
Blue Swirl314e2982011-09-11 20:22:05 +00001440{
1441 mtree_info((fprintf_function)monitor_printf, mon);
1442}
1443
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001444static void hmp_info_numa(Monitor *mon, const QDict *qdict)
aliguori030ea372009-04-21 22:30:47 +00001445{
aliguorib28b6232009-04-22 20:20:29 +00001446 int i;
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001447 CPUState *cpu;
zhanghailiang5b009e42014-11-04 19:49:30 +08001448 uint64_t *node_mem;
aliguori030ea372009-04-21 22:30:47 +00001449
zhanghailiang5b009e42014-11-04 19:49:30 +08001450 node_mem = g_new0(uint64_t, nb_numa_nodes);
1451 query_numa_node_mem(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001452 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1453 for (i = 0; i < nb_numa_nodes; i++) {
1454 monitor_printf(mon, "node %d cpus:", i);
Andreas Färberbdc44642013-06-24 23:50:24 +02001455 CPU_FOREACH(cpu) {
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001456 if (cpu->numa_node == i) {
Andreas Färber55e5c282012-12-17 06:18:02 +01001457 monitor_printf(mon, " %d", cpu->cpu_index);
aliguori030ea372009-04-21 22:30:47 +00001458 }
1459 }
1460 monitor_printf(mon, "\n");
1461 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
zhanghailiang5b009e42014-11-04 19:49:30 +08001462 node_mem[i] >> 20);
aliguori030ea372009-04-21 22:30:47 +00001463 }
zhanghailiang5b009e42014-11-04 19:49:30 +08001464 g_free(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001465}
1466
bellard5f1ce942006-02-08 22:40:15 +00001467#ifdef CONFIG_PROFILER
1468
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001469int64_t tcg_time;
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001470int64_t dev_time;
1471
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001472static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001473{
aliguori376253e2009-03-05 23:01:23 +00001474 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001475 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00001476 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001477 tcg_time, tcg_time / (double)get_ticks_per_sec());
1478 tcg_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001479 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001480}
1481#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001482static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001483{
aliguori376253e2009-03-05 23:01:23 +00001484 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001485}
1486#endif
1487
bellardec36b692006-07-16 18:57:03 +00001488/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001489static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001490
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001491static void hmp_info_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001492{
1493 int i;
1494 CaptureState *s;
1495
1496 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001497 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001498 s->ops.info (s->opaque);
1499 }
1500}
1501
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001502static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001503{
1504 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001505 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001506 CaptureState *s;
1507
1508 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1509 if (i == n) {
1510 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001511 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001512 g_free (s);
bellardec36b692006-07-16 18:57:03 +00001513 return;
1514 }
1515 }
1516}
1517
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001518static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001519{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001520 const char *path = qdict_get_str(qdict, "path");
1521 int has_freq = qdict_haskey(qdict, "freq");
1522 int freq = qdict_get_try_int(qdict, "freq", -1);
1523 int has_bits = qdict_haskey(qdict, "bits");
1524 int bits = qdict_get_try_int(qdict, "bits", -1);
1525 int has_channels = qdict_haskey(qdict, "nchannels");
1526 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001527 CaptureState *s;
1528
Anthony Liguori7267c092011-08-20 22:09:37 -05001529 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001530
1531 freq = has_freq ? freq : 44100;
1532 bits = has_bits ? bits : 16;
1533 nchannels = has_channels ? nchannels : 2;
1534
1535 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001536 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05001537 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001538 return;
bellardec36b692006-07-16 18:57:03 +00001539 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001540 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001541}
bellardec36b692006-07-16 18:57:03 +00001542
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001543static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001544{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001545 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001546
aliguori76655d62009-03-06 20:27:37 +00001547 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001548 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001549 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001550 return acl;
1551}
aliguori76655d62009-03-06 20:27:37 +00001552
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001553static void hmp_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001554{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001555 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001556 qemu_acl *acl = find_acl(mon, aclname);
1557 qemu_acl_entry *entry;
1558 int i = 0;
1559
1560 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001561 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001562 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00001563 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00001564 i++;
1565 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001566 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001567 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001568 }
1569}
1570
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001571static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001572{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001573 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001574 qemu_acl *acl = find_acl(mon, aclname);
1575
1576 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001577 qemu_acl_reset(acl);
1578 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001579 }
1580}
aliguori76655d62009-03-06 20:27:37 +00001581
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001582static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001583{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001584 const char *aclname = qdict_get_str(qdict, "aclname");
1585 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001586 qemu_acl *acl = find_acl(mon, aclname);
1587
1588 if (acl) {
1589 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001590 acl->defaultDeny = 0;
1591 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001592 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001593 acl->defaultDeny = 1;
1594 monitor_printf(mon, "acl: policy set to 'deny'\n");
1595 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001596 monitor_printf(mon, "acl: unknown policy '%s', "
1597 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001598 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001599 }
1600}
aliguori76655d62009-03-06 20:27:37 +00001601
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001602static void hmp_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001603{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001604 const char *aclname = qdict_get_str(qdict, "aclname");
1605 const char *match = qdict_get_str(qdict, "match");
1606 const char *policy = qdict_get_str(qdict, "policy");
1607 int has_index = qdict_haskey(qdict, "index");
1608 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001609 qemu_acl *acl = find_acl(mon, aclname);
1610 int deny, ret;
1611
1612 if (acl) {
1613 if (strcmp(policy, "allow") == 0) {
1614 deny = 0;
1615 } else if (strcmp(policy, "deny") == 0) {
1616 deny = 1;
1617 } else {
1618 monitor_printf(mon, "acl: unknown policy '%s', "
1619 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001620 return;
1621 }
aliguori28a76be2009-03-06 20:27:40 +00001622 if (has_index)
1623 ret = qemu_acl_insert(acl, deny, match, index);
1624 else
1625 ret = qemu_acl_append(acl, deny, match);
1626 if (ret < 0)
1627 monitor_printf(mon, "acl: unable to add acl entry\n");
1628 else
1629 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001630 }
1631}
aliguori76655d62009-03-06 20:27:37 +00001632
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001633static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001634{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001635 const char *aclname = qdict_get_str(qdict, "aclname");
1636 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001637 qemu_acl *acl = find_acl(mon, aclname);
1638 int ret;
aliguori76655d62009-03-06 20:27:37 +00001639
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001640 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001641 ret = qemu_acl_remove(acl, match);
1642 if (ret < 0)
1643 monitor_printf(mon, "acl: no matching acl entry\n");
1644 else
1645 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001646 }
1647}
1648
Corey Bryant208c9d12012-06-22 14:36:09 -04001649void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001650{
Anthony Liguoric227f092009-10-01 16:12:16 -05001651 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001652 int fd;
1653
Corey Bryant208c9d12012-06-22 14:36:09 -04001654 fd = qemu_chr_fe_get_msgfd(cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001655 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001656 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryant208c9d12012-06-22 14:36:09 -04001657 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001658 }
1659
1660 if (qemu_isdigit(fdname[0])) {
Stefan Hajnoczi0b9f0e22014-04-24 13:58:18 +02001661 close(fd);
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001662 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
1663 "a name not starting with a digit");
Corey Bryant208c9d12012-06-22 14:36:09 -04001664 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001665 }
1666
Corey Bryant208c9d12012-06-22 14:36:09 -04001667 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001668 if (strcmp(monfd->name, fdname) != 0) {
1669 continue;
1670 }
1671
1672 close(monfd->fd);
1673 monfd->fd = fd;
Corey Bryant208c9d12012-06-22 14:36:09 -04001674 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001675 }
1676
Anthony Liguori7267c092011-08-20 22:09:37 -05001677 monfd = g_malloc0(sizeof(mon_fd_t));
1678 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001679 monfd->fd = fd;
1680
Corey Bryant208c9d12012-06-22 14:36:09 -04001681 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001682}
1683
Corey Bryant208c9d12012-06-22 14:36:09 -04001684void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001685{
Anthony Liguoric227f092009-10-01 16:12:16 -05001686 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001687
Corey Bryant208c9d12012-06-22 14:36:09 -04001688 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001689 if (strcmp(monfd->name, fdname) != 0) {
1690 continue;
1691 }
1692
Blue Swirl72cf2d42009-09-12 07:36:22 +00001693 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001694 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001695 g_free(monfd->name);
1696 g_free(monfd);
Corey Bryant208c9d12012-06-22 14:36:09 -04001697 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001698 }
1699
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001700 error_setg(errp, QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001701}
1702
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001703static void hmp_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001704{
Luiz Capitulino13548692011-07-29 15:36:43 -03001705 int saved_vm_running = runstate_is_running();
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001706 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02001707
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001708 vm_stop(RUN_STATE_RESTORE_VM);
Juan Quintelac8d41b22009-08-20 19:42:21 +02001709
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03001710 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02001711 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03001712 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02001713}
1714
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001715int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
Mark McLoughlin7768e042009-07-22 09:11:41 +01001716{
Anthony Liguoric227f092009-10-01 16:12:16 -05001717 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01001718
Blue Swirl72cf2d42009-09-12 07:36:22 +00001719 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01001720 int fd;
1721
1722 if (strcmp(monfd->name, fdname) != 0) {
1723 continue;
1724 }
1725
1726 fd = monfd->fd;
1727
1728 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001729 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05001730 g_free(monfd->name);
1731 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001732
1733 return fd;
1734 }
1735
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001736 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001737 return -1;
1738}
1739
Corey Bryantba1c0482012-08-14 16:43:43 -04001740static void monitor_fdset_cleanup(MonFdset *mon_fdset)
1741{
1742 MonFdsetFd *mon_fdset_fd;
1743 MonFdsetFd *mon_fdset_fd_next;
1744
1745 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantebe52b52012-10-18 15:19:33 -04001746 if ((mon_fdset_fd->removed ||
1747 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
1748 runstate_is_running()) {
Corey Bryantba1c0482012-08-14 16:43:43 -04001749 close(mon_fdset_fd->fd);
1750 g_free(mon_fdset_fd->opaque);
1751 QLIST_REMOVE(mon_fdset_fd, next);
1752 g_free(mon_fdset_fd);
1753 }
1754 }
1755
Corey Bryantadb696f2012-08-14 16:43:47 -04001756 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04001757 QLIST_REMOVE(mon_fdset, next);
1758 g_free(mon_fdset);
1759 }
1760}
1761
Corey Bryantefb87c12012-08-14 16:43:48 -04001762static void monitor_fdsets_cleanup(void)
1763{
1764 MonFdset *mon_fdset;
1765 MonFdset *mon_fdset_next;
1766
1767 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
1768 monitor_fdset_cleanup(mon_fdset);
1769 }
1770}
1771
Corey Bryantba1c0482012-08-14 16:43:43 -04001772AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
1773 const char *opaque, Error **errp)
1774{
1775 int fd;
1776 Monitor *mon = cur_mon;
Corey Bryantba1c0482012-08-14 16:43:43 -04001777 AddfdInfo *fdinfo;
1778
1779 fd = qemu_chr_fe_get_msgfd(mon->chr);
1780 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001781 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryantba1c0482012-08-14 16:43:43 -04001782 goto error;
1783 }
1784
Corey Bryante446f702012-10-18 15:19:32 -04001785 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
1786 has_opaque, opaque, errp);
1787 if (fdinfo) {
1788 return fdinfo;
Corey Bryant9ac54af2012-10-18 15:19:31 -04001789 }
1790
Corey Bryantba1c0482012-08-14 16:43:43 -04001791error:
1792 if (fd != -1) {
1793 close(fd);
1794 }
1795 return NULL;
1796}
1797
1798void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
1799{
1800 MonFdset *mon_fdset;
1801 MonFdsetFd *mon_fdset_fd;
1802 char fd_str[60];
1803
1804 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1805 if (mon_fdset->id != fdset_id) {
1806 continue;
1807 }
1808 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1809 if (has_fd) {
1810 if (mon_fdset_fd->fd != fd) {
1811 continue;
1812 }
1813 mon_fdset_fd->removed = true;
1814 break;
1815 } else {
1816 mon_fdset_fd->removed = true;
1817 }
1818 }
1819 if (has_fd && !mon_fdset_fd) {
1820 goto error;
1821 }
1822 monitor_fdset_cleanup(mon_fdset);
1823 return;
1824 }
1825
1826error:
1827 if (has_fd) {
1828 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
1829 fdset_id, fd);
1830 } else {
1831 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
1832 }
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001833 error_setg(errp, QERR_FD_NOT_FOUND, fd_str);
Corey Bryantba1c0482012-08-14 16:43:43 -04001834}
1835
1836FdsetInfoList *qmp_query_fdsets(Error **errp)
1837{
1838 MonFdset *mon_fdset;
1839 MonFdsetFd *mon_fdset_fd;
1840 FdsetInfoList *fdset_list = NULL;
1841
1842 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1843 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
1844 FdsetFdInfoList *fdsetfd_list = NULL;
1845
1846 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
1847 fdset_info->value->fdset_id = mon_fdset->id;
1848
1849 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1850 FdsetFdInfoList *fdsetfd_info;
1851
1852 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
1853 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
1854 fdsetfd_info->value->fd = mon_fdset_fd->fd;
1855 if (mon_fdset_fd->opaque) {
1856 fdsetfd_info->value->has_opaque = true;
1857 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
1858 } else {
1859 fdsetfd_info->value->has_opaque = false;
1860 }
1861
1862 fdsetfd_info->next = fdsetfd_list;
1863 fdsetfd_list = fdsetfd_info;
1864 }
1865
1866 fdset_info->value->fds = fdsetfd_list;
1867
1868 fdset_info->next = fdset_list;
1869 fdset_list = fdset_info;
1870 }
1871
1872 return fdset_list;
1873}
1874
Corey Bryante446f702012-10-18 15:19:32 -04001875AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
1876 bool has_opaque, const char *opaque,
1877 Error **errp)
1878{
1879 MonFdset *mon_fdset = NULL;
1880 MonFdsetFd *mon_fdset_fd;
1881 AddfdInfo *fdinfo;
1882
1883 if (has_fdset_id) {
1884 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1885 /* Break if match found or match impossible due to ordering by ID */
1886 if (fdset_id <= mon_fdset->id) {
1887 if (fdset_id < mon_fdset->id) {
1888 mon_fdset = NULL;
1889 }
1890 break;
1891 }
1892 }
1893 }
1894
1895 if (mon_fdset == NULL) {
1896 int64_t fdset_id_prev = -1;
1897 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
1898
1899 if (has_fdset_id) {
1900 if (fdset_id < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001901 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
1902 "a non-negative value");
Corey Bryante446f702012-10-18 15:19:32 -04001903 return NULL;
1904 }
1905 /* Use specified fdset ID */
1906 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1907 mon_fdset_cur = mon_fdset;
1908 if (fdset_id < mon_fdset_cur->id) {
1909 break;
1910 }
1911 }
1912 } else {
1913 /* Use first available fdset ID */
1914 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1915 mon_fdset_cur = mon_fdset;
1916 if (fdset_id_prev == mon_fdset_cur->id - 1) {
1917 fdset_id_prev = mon_fdset_cur->id;
1918 continue;
1919 }
1920 break;
1921 }
1922 }
1923
1924 mon_fdset = g_malloc0(sizeof(*mon_fdset));
1925 if (has_fdset_id) {
1926 mon_fdset->id = fdset_id;
1927 } else {
1928 mon_fdset->id = fdset_id_prev + 1;
1929 }
1930
1931 /* The fdset list is ordered by fdset ID */
1932 if (!mon_fdset_cur) {
1933 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
1934 } else if (mon_fdset->id < mon_fdset_cur->id) {
1935 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
1936 } else {
1937 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
1938 }
1939 }
1940
1941 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
1942 mon_fdset_fd->fd = fd;
1943 mon_fdset_fd->removed = false;
1944 if (has_opaque) {
1945 mon_fdset_fd->opaque = g_strdup(opaque);
1946 }
1947 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
1948
1949 fdinfo = g_malloc0(sizeof(*fdinfo));
1950 fdinfo->fdset_id = mon_fdset->id;
1951 fdinfo->fd = mon_fdset_fd->fd;
1952
1953 return fdinfo;
1954}
1955
Corey Bryantadb696f2012-08-14 16:43:47 -04001956int monitor_fdset_get_fd(int64_t fdset_id, int flags)
1957{
Blue Swirlb2dc64c2012-08-18 20:14:54 +00001958#ifndef _WIN32
Corey Bryantadb696f2012-08-14 16:43:47 -04001959 MonFdset *mon_fdset;
1960 MonFdsetFd *mon_fdset_fd;
1961 int mon_fd_flags;
1962
Corey Bryantadb696f2012-08-14 16:43:47 -04001963 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1964 if (mon_fdset->id != fdset_id) {
1965 continue;
1966 }
1967 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1968 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
1969 if (mon_fd_flags == -1) {
1970 return -1;
1971 }
1972
1973 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
1974 return mon_fdset_fd->fd;
1975 }
1976 }
1977 errno = EACCES;
1978 return -1;
1979 }
1980#endif
1981
1982 errno = ENOENT;
1983 return -1;
1984}
1985
1986int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
1987{
1988 MonFdset *mon_fdset;
1989 MonFdsetFd *mon_fdset_fd_dup;
1990
1991 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1992 if (mon_fdset->id != fdset_id) {
1993 continue;
1994 }
1995 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
1996 if (mon_fdset_fd_dup->fd == dup_fd) {
1997 return -1;
1998 }
1999 }
2000 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2001 mon_fdset_fd_dup->fd = dup_fd;
2002 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
2003 return 0;
2004 }
2005 return -1;
2006}
2007
2008static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2009{
2010 MonFdset *mon_fdset;
2011 MonFdsetFd *mon_fdset_fd_dup;
2012
2013 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2014 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2015 if (mon_fdset_fd_dup->fd == dup_fd) {
2016 if (remove) {
2017 QLIST_REMOVE(mon_fdset_fd_dup, next);
2018 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2019 monitor_fdset_cleanup(mon_fdset);
2020 }
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002021 return -1;
2022 } else {
2023 return mon_fdset->id;
Corey Bryantadb696f2012-08-14 16:43:47 -04002024 }
Corey Bryantadb696f2012-08-14 16:43:47 -04002025 }
2026 }
2027 }
2028 return -1;
2029}
2030
2031int monitor_fdset_dup_fd_find(int dup_fd)
2032{
2033 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2034}
2035
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002036void monitor_fdset_dup_fd_remove(int dup_fd)
Corey Bryantadb696f2012-08-14 16:43:47 -04002037{
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002038 monitor_fdset_dup_fd_find_remove(dup_fd, true);
Corey Bryantadb696f2012-08-14 16:43:47 -04002039}
2040
Markus Armbruster1677f4c2015-02-09 14:03:19 +01002041int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
Laszlo Ersek59063662014-04-10 10:24:31 +02002042{
2043 int fd;
2044 Error *local_err = NULL;
2045
2046 if (!qemu_isdigit(fdname[0]) && mon) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002047 fd = monitor_get_fd(mon, fdname, &local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002048 } else {
2049 fd = qemu_parse_fd(fdname);
Laszlo Ersek59063662014-04-10 10:24:31 +02002050 if (fd == -1) {
2051 error_setg(&local_err, "Invalid file descriptor number '%s'",
2052 fdname);
2053 }
2054 }
2055 if (local_err) {
2056 error_propagate(errp, local_err);
2057 assert(fd == -1);
2058 } else {
2059 assert(fd != -1);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002060 }
2061
2062 return fd;
2063}
2064
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002065/* Please update hmp-commands.hx when adding or changing commands */
Wayne Xia816f8922011-10-12 11:32:41 +08002066static mon_cmd_t info_cmds[] = {
Pavel Butsykinda76ee72015-09-10 18:38:58 +03002067#include "hmp-commands-info.h"
2068 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002069};
2070
Wenchao Xiaa13ced52013-01-14 14:06:28 +08002071/* mon_cmds and info_cmds would be sorted at runtime */
2072static mon_cmd_t mon_cmds[] = {
2073#include "hmp-commands.h"
2074 { NULL, NULL, },
2075};
2076
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002077static const mon_cmd_t qmp_cmds[] = {
Anthony Liguorie3193602011-09-02 12:34:47 -05002078#include "qmp-commands-old.h"
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002079 { /* NULL */ },
2080};
2081
bellard9307c4c2004-04-04 12:57:25 +00002082/*******************************************************************/
2083
2084static const char *pch;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002085static sigjmp_buf expr_env;
bellard9307c4c2004-04-04 12:57:25 +00002086
bellard9307c4c2004-04-04 12:57:25 +00002087
Stefan Weil9c3175c2013-08-22 21:30:09 +02002088static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
2089expr_error(Monitor *mon, const char *fmt, ...)
bellard9dc39cb2004-03-14 21:38:27 +00002090{
Fam Zheng277acfe2013-08-20 10:58:21 +08002091 va_list ap;
2092 va_start(ap, fmt);
2093 monitor_vprintf(mon, fmt, ap);
2094 monitor_printf(mon, "\n");
2095 va_end(ap);
Peter Maydell6ab7e542013-02-20 15:21:09 +00002096 siglongjmp(expr_env, 1);
bellard9307c4c2004-04-04 12:57:25 +00002097}
2098
Markus Armbruster09b94182010-01-20 13:07:30 +01002099/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00002100static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002101{
Pavel Butsykinbf957282015-09-10 18:38:59 +03002102 const MonitorDef *md = target_monitor_defs();
bellard92a31b12005-02-10 22:00:52 +00002103 void *ptr;
2104
Pavel Butsykinbf957282015-09-10 18:38:59 +03002105 if (md == NULL) {
2106 return -1;
2107 }
2108
2109 for(; md->name != NULL; md++) {
bellard9307c4c2004-04-04 12:57:25 +00002110 if (compare_cmd(name, md->name)) {
2111 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002112 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002113 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002114 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00002115 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002116 switch(md->type) {
2117 case MD_I32:
2118 *pval = *(int32_t *)ptr;
2119 break;
2120 case MD_TLONG:
2121 *pval = *(target_long *)ptr;
2122 break;
2123 default:
2124 *pval = 0;
2125 break;
2126 }
bellard9307c4c2004-04-04 12:57:25 +00002127 }
2128 return 0;
2129 }
2130 }
2131 return -1;
2132}
2133
2134static void next(void)
2135{
Blue Swirl660f11b2009-07-31 21:16:51 +00002136 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002137 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002138 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002139 pch++;
2140 }
2141}
2142
aliguori376253e2009-03-05 23:01:23 +00002143static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002144
aliguori376253e2009-03-05 23:01:23 +00002145static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002146{
blueswir1c2efc952007-09-25 17:28:42 +00002147 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002148 char *p;
bellard6a00d602005-11-21 23:25:50 +00002149 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002150
2151 switch(*pch) {
2152 case '+':
2153 next();
aliguori376253e2009-03-05 23:01:23 +00002154 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002155 break;
2156 case '-':
2157 next();
aliguori376253e2009-03-05 23:01:23 +00002158 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002159 break;
2160 case '~':
2161 next();
aliguori376253e2009-03-05 23:01:23 +00002162 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002163 break;
2164 case '(':
2165 next();
aliguori376253e2009-03-05 23:01:23 +00002166 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002167 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002168 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002169 }
2170 next();
2171 break;
bellard81d09122004-07-14 17:21:37 +00002172 case '\'':
2173 pch++;
2174 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002175 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002176 n = *pch;
2177 pch++;
2178 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002179 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002180 next();
2181 break;
bellard9307c4c2004-04-04 12:57:25 +00002182 case '$':
2183 {
2184 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002185 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002186
bellard9307c4c2004-04-04 12:57:25 +00002187 pch++;
2188 q = buf;
2189 while ((*pch >= 'a' && *pch <= 'z') ||
2190 (*pch >= 'A' && *pch <= 'Z') ||
2191 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002192 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002193 if ((q - buf) < sizeof(buf) - 1)
2194 *q++ = *pch;
2195 pch++;
2196 }
blueswir1cd390082008-11-16 13:53:32 +00002197 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002198 pch++;
2199 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002200 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01002201 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00002202 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00002203 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002204 }
2205 break;
2206 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002207 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002208 n = 0;
2209 break;
2210 default:
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002211 errno = 0;
bellard4f4fbf72006-06-25 18:28:12 +00002212 n = strtoull(pch, &p, 0);
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002213 if (errno == ERANGE) {
2214 expr_error(mon, "number too large");
2215 }
bellard9307c4c2004-04-04 12:57:25 +00002216 if (pch == p) {
Fam Zheng277acfe2013-08-20 10:58:21 +08002217 expr_error(mon, "invalid char '%c' in expression", *p);
bellard9307c4c2004-04-04 12:57:25 +00002218 }
2219 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002220 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002221 pch++;
2222 break;
2223 }
2224 return n;
2225}
2226
2227
aliguori376253e2009-03-05 23:01:23 +00002228static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002229{
blueswir1c2efc952007-09-25 17:28:42 +00002230 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002231 int op;
ths3b46e622007-09-17 08:09:54 +00002232
aliguori376253e2009-03-05 23:01:23 +00002233 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002234 for(;;) {
2235 op = *pch;
2236 if (op != '*' && op != '/' && op != '%')
2237 break;
2238 next();
aliguori376253e2009-03-05 23:01:23 +00002239 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002240 switch(op) {
2241 default:
2242 case '*':
2243 val *= val2;
2244 break;
2245 case '/':
2246 case '%':
ths5fafdf22007-09-16 21:08:06 +00002247 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002248 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002249 if (op == '/')
2250 val /= val2;
2251 else
2252 val %= val2;
2253 break;
2254 }
2255 }
2256 return val;
2257}
2258
aliguori376253e2009-03-05 23:01:23 +00002259static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002260{
blueswir1c2efc952007-09-25 17:28:42 +00002261 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002262 int op;
bellard9307c4c2004-04-04 12:57:25 +00002263
aliguori376253e2009-03-05 23:01:23 +00002264 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002265 for(;;) {
2266 op = *pch;
2267 if (op != '&' && op != '|' && op != '^')
2268 break;
2269 next();
aliguori376253e2009-03-05 23:01:23 +00002270 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002271 switch(op) {
2272 default:
2273 case '&':
2274 val &= val2;
2275 break;
2276 case '|':
2277 val |= val2;
2278 break;
2279 case '^':
2280 val ^= val2;
2281 break;
2282 }
2283 }
2284 return val;
2285}
2286
aliguori376253e2009-03-05 23:01:23 +00002287static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002288{
blueswir1c2efc952007-09-25 17:28:42 +00002289 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002290 int op;
bellard9307c4c2004-04-04 12:57:25 +00002291
aliguori376253e2009-03-05 23:01:23 +00002292 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002293 for(;;) {
2294 op = *pch;
2295 if (op != '+' && op != '-')
2296 break;
2297 next();
aliguori376253e2009-03-05 23:01:23 +00002298 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002299 if (op == '+')
2300 val += val2;
2301 else
2302 val -= val2;
2303 }
2304 return val;
2305}
2306
aliguori376253e2009-03-05 23:01:23 +00002307static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002308{
2309 pch = *pp;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002310 if (sigsetjmp(expr_env, 0)) {
bellard9307c4c2004-04-04 12:57:25 +00002311 *pp = pch;
2312 return -1;
2313 }
blueswir1cd390082008-11-16 13:53:32 +00002314 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002315 pch++;
aliguori376253e2009-03-05 23:01:23 +00002316 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002317 *pp = pch;
2318 return 0;
2319}
2320
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002321static int get_double(Monitor *mon, double *pval, const char **pp)
2322{
2323 const char *p = *pp;
2324 char *tailp;
2325 double d;
2326
2327 d = strtod(p, &tailp);
2328 if (tailp == p) {
2329 monitor_printf(mon, "Number expected\n");
2330 return -1;
2331 }
2332 if (d != d || d - d != 0) {
2333 /* NaN or infinity */
2334 monitor_printf(mon, "Bad number\n");
2335 return -1;
2336 }
2337 *pval = d;
2338 *pp = tailp;
2339 return 0;
2340}
2341
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002342/*
2343 * Store the command-name in cmdname, and return a pointer to
2344 * the remaining of the command string.
2345 */
2346static const char *get_command_name(const char *cmdline,
2347 char *cmdname, size_t nlen)
2348{
2349 size_t len;
2350 const char *p, *pstart;
2351
2352 p = cmdline;
2353 while (qemu_isspace(*p))
2354 p++;
2355 if (*p == '\0')
2356 return NULL;
2357 pstart = p;
2358 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2359 p++;
2360 len = p - pstart;
2361 if (len > nlen - 1)
2362 len = nlen - 1;
2363 memcpy(cmdname, pstart, len);
2364 cmdname[len] = '\0';
2365 return p;
2366}
2367
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002368/**
2369 * Read key of 'type' into 'key' and return the current
2370 * 'type' pointer.
2371 */
2372static char *key_get_info(const char *type, char **key)
2373{
2374 size_t len;
2375 char *p, *str;
2376
2377 if (*type == ',')
2378 type++;
2379
2380 p = strchr(type, ':');
2381 if (!p) {
2382 *key = NULL;
2383 return NULL;
2384 }
2385 len = p - type;
2386
Anthony Liguori7267c092011-08-20 22:09:37 -05002387 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002388 memcpy(str, type, len);
2389 str[len] = '\0';
2390
2391 *key = str;
2392 return ++p;
2393}
2394
bellard9307c4c2004-04-04 12:57:25 +00002395static int default_fmt_format = 'x';
2396static int default_fmt_size = 4;
2397
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002398static int is_valid_option(const char *c, const char *typestr)
2399{
2400 char option[3];
2401
2402 option[0] = '-';
2403 option[1] = *c;
2404 option[2] = '\0';
2405
2406 typestr = strstr(typestr, option);
2407 return (typestr != NULL);
2408}
2409
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03002410static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
2411 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002412{
2413 const mon_cmd_t *cmd;
2414
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03002415 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002416 if (compare_cmd(cmdname, cmd->name)) {
2417 return cmd;
2418 }
2419 }
2420
2421 return NULL;
2422}
2423
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03002424static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
2425{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002426 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03002427}
2428
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002429/*
Bandan Dasae502122015-06-03 18:38:08 -04002430 * Parse command name from @cmdp according to command table @table.
2431 * If blank, return NULL.
2432 * Else, if no valid command can be found, report to @mon, and return
2433 * NULL.
2434 * Else, change @cmdp to point right behind the name, and return its
2435 * command table entry.
2436 * Do not assume the return value points into @table! It doesn't when
2437 * the command is found in a sub-command table.
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002438 */
Anthony Liguoric227f092009-10-01 16:12:16 -05002439static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Bandan Dasae502122015-06-03 18:38:08 -04002440 const char **cmdp,
2441 mon_cmd_t *table)
bellard9307c4c2004-04-04 12:57:25 +00002442{
Bandan Dasae502122015-06-03 18:38:08 -04002443 const char *p;
Anthony Liguoric227f092009-10-01 16:12:16 -05002444 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002445 char cmdname[256];
bellard9dc39cb2004-03-14 21:38:27 +00002446
bellard9307c4c2004-04-04 12:57:25 +00002447 /* extract the command name */
Bandan Dasae502122015-06-03 18:38:08 -04002448 p = get_command_name(*cmdp, cmdname, sizeof(cmdname));
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002449 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002450 return NULL;
ths3b46e622007-09-17 08:09:54 +00002451
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002452 cmd = search_dispatch_table(table, cmdname);
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002453 if (!cmd) {
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002454 monitor_printf(mon, "unknown command: '%.*s'\n",
Bandan Dasae502122015-06-03 18:38:08 -04002455 (int)(p - *cmdp), *cmdp);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002456 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002457 }
bellard9307c4c2004-04-04 12:57:25 +00002458
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002459 /* filter out following useless space */
2460 while (qemu_isspace(*p)) {
2461 p++;
2462 }
Bandan Dasae502122015-06-03 18:38:08 -04002463
2464 *cmdp = p;
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002465 /* search sub command */
Bandan Dasae502122015-06-03 18:38:08 -04002466 if (cmd->sub_table != NULL && *p != '\0') {
2467 return monitor_parse_command(mon, cmdp, cmd->sub_table);
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002468 }
2469
Bandan Dasae502122015-06-03 18:38:08 -04002470 return cmd;
2471}
2472
2473/*
2474 * Parse arguments for @cmd.
2475 * If it can't be parsed, report to @mon, and return NULL.
2476 * Else, insert command arguments into a QDict, and return it.
2477 * Note: On success, caller has to free the QDict structure.
2478 */
2479
2480static QDict *monitor_parse_arguments(Monitor *mon,
2481 const char **endp,
2482 const mon_cmd_t *cmd)
2483{
2484 const char *typestr;
2485 char *key;
2486 int c;
2487 const char *p = *endp;
2488 char buf[1024];
2489 QDict *qdict = qdict_new();
2490
bellard9307c4c2004-04-04 12:57:25 +00002491 /* parse the parameters */
2492 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00002493 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002494 typestr = key_get_info(typestr, &key);
2495 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00002496 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002497 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00002498 typestr++;
2499 switch(c) {
2500 case 'F':
bellard81d09122004-07-14 17:21:37 +00002501 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002502 case 's':
2503 {
2504 int ret;
ths3b46e622007-09-17 08:09:54 +00002505
blueswir1cd390082008-11-16 13:53:32 +00002506 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002507 p++;
2508 if (*typestr == '?') {
2509 typestr++;
2510 if (*p == '\0') {
2511 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03002512 break;
bellard9307c4c2004-04-04 12:57:25 +00002513 }
2514 }
2515 ret = get_str(buf, sizeof(buf), &p);
2516 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002517 switch(c) {
2518 case 'F':
aliguori376253e2009-03-05 23:01:23 +00002519 monitor_printf(mon, "%s: filename expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002520 cmd->name);
bellard81d09122004-07-14 17:21:37 +00002521 break;
2522 case 'B':
aliguori376253e2009-03-05 23:01:23 +00002523 monitor_printf(mon, "%s: block device name expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002524 cmd->name);
bellard81d09122004-07-14 17:21:37 +00002525 break;
2526 default:
Bandan Dasae502122015-06-03 18:38:08 -04002527 monitor_printf(mon, "%s: string expected\n", cmd->name);
bellard81d09122004-07-14 17:21:37 +00002528 break;
2529 }
bellard9307c4c2004-04-04 12:57:25 +00002530 goto fail;
2531 }
Luiz Capitulino53773582009-08-28 15:27:25 -03002532 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00002533 }
2534 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01002535 case 'O':
2536 {
2537 QemuOptsList *opts_list;
2538 QemuOpts *opts;
2539
2540 opts_list = qemu_find_opts(key);
2541 if (!opts_list || opts_list->desc->name) {
2542 goto bad_type;
2543 }
2544 while (qemu_isspace(*p)) {
2545 p++;
2546 }
2547 if (!*p)
2548 break;
2549 if (get_str(buf, sizeof(buf), &p) < 0) {
2550 goto fail;
2551 }
Markus Armbruster70b94332015-02-13 12:50:26 +01002552 opts = qemu_opts_parse_noisily(opts_list, buf, true);
Markus Armbruster361127d2010-02-10 20:24:35 +01002553 if (!opts) {
2554 goto fail;
2555 }
2556 qemu_opts_to_qdict(opts, qdict);
2557 qemu_opts_del(opts);
2558 }
2559 break;
bellard9307c4c2004-04-04 12:57:25 +00002560 case '/':
2561 {
2562 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002563
blueswir1cd390082008-11-16 13:53:32 +00002564 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002565 p++;
2566 if (*p == '/') {
2567 /* format found */
2568 p++;
2569 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002570 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002571 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002572 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002573 count = count * 10 + (*p - '0');
2574 p++;
2575 }
2576 }
2577 size = -1;
2578 format = -1;
2579 for(;;) {
2580 switch(*p) {
2581 case 'o':
2582 case 'd':
2583 case 'u':
2584 case 'x':
2585 case 'i':
2586 case 'c':
2587 format = *p++;
2588 break;
2589 case 'b':
2590 size = 1;
2591 p++;
2592 break;
2593 case 'h':
2594 size = 2;
2595 p++;
2596 break;
2597 case 'w':
2598 size = 4;
2599 p++;
2600 break;
2601 case 'g':
2602 case 'L':
2603 size = 8;
2604 p++;
2605 break;
2606 default:
2607 goto next;
2608 }
2609 }
2610 next:
blueswir1cd390082008-11-16 13:53:32 +00002611 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00002612 monitor_printf(mon, "invalid char in format: '%c'\n",
2613 *p);
bellard9307c4c2004-04-04 12:57:25 +00002614 goto fail;
2615 }
bellard9307c4c2004-04-04 12:57:25 +00002616 if (format < 0)
2617 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002618 if (format != 'i') {
2619 /* for 'i', not specifying a size gives -1 as size */
2620 if (size < 0)
2621 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002622 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002623 }
bellard9307c4c2004-04-04 12:57:25 +00002624 default_fmt_format = format;
2625 } else {
2626 count = 1;
2627 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002628 if (format != 'i') {
2629 size = default_fmt_size;
2630 } else {
2631 size = -1;
2632 }
bellard9307c4c2004-04-04 12:57:25 +00002633 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002634 qdict_put(qdict, "count", qint_from_int(count));
2635 qdict_put(qdict, "format", qint_from_int(format));
2636 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00002637 }
2638 break;
2639 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002640 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002641 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00002642 {
blueswir1c2efc952007-09-25 17:28:42 +00002643 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00002644
blueswir1cd390082008-11-16 13:53:32 +00002645 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002646 p++;
bellard34405572004-06-08 00:55:58 +00002647 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002648 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03002649 if (*p == '\0') {
2650 typestr++;
2651 break;
2652 }
bellard34405572004-06-08 00:55:58 +00002653 } else {
2654 if (*p == '.') {
2655 p++;
blueswir1cd390082008-11-16 13:53:32 +00002656 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00002657 p++;
bellard34405572004-06-08 00:55:58 +00002658 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03002659 typestr++;
2660 break;
bellard34405572004-06-08 00:55:58 +00002661 }
2662 }
bellard13224a82006-07-14 22:03:35 +00002663 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002664 }
aliguori376253e2009-03-05 23:01:23 +00002665 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00002666 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002667 /* Check if 'i' is greater than 32-bit */
2668 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
Bandan Dasae502122015-06-03 18:38:08 -04002669 monitor_printf(mon, "\'%s\' has failed: ", cmd->name);
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002670 monitor_printf(mon, "integer is for 32-bit values\n");
2671 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002672 } else if (c == 'M') {
Luiz Capitulino91162842012-04-26 17:34:30 -03002673 if (val < 0) {
2674 monitor_printf(mon, "enter a positive value\n");
2675 goto fail;
2676 }
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002677 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002678 }
Luiz Capitulino53773582009-08-28 15:27:25 -03002679 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00002680 }
2681 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02002682 case 'o':
2683 {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01002684 int64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02002685 char *end;
2686
2687 while (qemu_isspace(*p)) {
2688 p++;
2689 }
2690 if (*typestr == '?') {
2691 typestr++;
2692 if (*p == '\0') {
2693 break;
2694 }
2695 }
Marc-André Lureau4677bb42015-09-16 18:02:56 +02002696 val = qemu_strtosz(p, &end);
Jes Sorensendbc0c672010-10-21 17:15:47 +02002697 if (val < 0) {
2698 monitor_printf(mon, "invalid size\n");
2699 goto fail;
2700 }
2701 qdict_put(qdict, key, qint_from_int(val));
2702 p = end;
2703 }
2704 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01002705 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002706 {
2707 double val;
2708
2709 while (qemu_isspace(*p))
2710 p++;
2711 if (*typestr == '?') {
2712 typestr++;
2713 if (*p == '\0') {
2714 break;
2715 }
2716 }
2717 if (get_double(mon, &val, &p) < 0) {
2718 goto fail;
2719 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02002720 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01002721 switch (*p) {
2722 case 'm':
2723 val /= 1e3; p += 2; break;
2724 case 'u':
2725 val /= 1e6; p += 2; break;
2726 case 'n':
2727 val /= 1e9; p += 2; break;
2728 }
2729 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002730 if (*p && !qemu_isspace(*p)) {
2731 monitor_printf(mon, "Unknown unit suffix\n");
2732 goto fail;
2733 }
2734 qdict_put(qdict, key, qfloat_from_double(val));
2735 }
2736 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002737 case 'b':
2738 {
2739 const char *beg;
Eric Blakefc48ffc2015-05-15 16:24:59 -06002740 bool val;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002741
2742 while (qemu_isspace(*p)) {
2743 p++;
2744 }
2745 beg = p;
2746 while (qemu_isgraph(*p)) {
2747 p++;
2748 }
2749 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06002750 val = true;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002751 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06002752 val = false;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002753 } else {
2754 monitor_printf(mon, "Expected 'on' or 'off'\n");
2755 goto fail;
2756 }
Eric Blakefc48ffc2015-05-15 16:24:59 -06002757 qdict_put(qdict, key, qbool_from_bool(val));
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002758 }
2759 break;
bellard9307c4c2004-04-04 12:57:25 +00002760 case '-':
2761 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002762 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03002763 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00002764 /* option */
ths3b46e622007-09-17 08:09:54 +00002765
bellard9307c4c2004-04-04 12:57:25 +00002766 c = *typestr++;
2767 if (c == '\0')
2768 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00002769 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002770 p++;
bellard9307c4c2004-04-04 12:57:25 +00002771 if (*p == '-') {
2772 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002773 if(c != *p) {
2774 if(!is_valid_option(p, typestr)) {
2775
2776 monitor_printf(mon, "%s: unsupported option -%c\n",
Bandan Dasae502122015-06-03 18:38:08 -04002777 cmd->name, *p);
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002778 goto fail;
2779 } else {
2780 skip_key = 1;
2781 }
bellard9307c4c2004-04-04 12:57:25 +00002782 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002783 if(skip_key) {
2784 p = tmp;
2785 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03002786 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002787 p++;
Eric Blakefc48ffc2015-05-15 16:24:59 -06002788 qdict_put(qdict, key, qbool_from_bool(true));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002789 }
bellard9307c4c2004-04-04 12:57:25 +00002790 }
bellard9307c4c2004-04-04 12:57:25 +00002791 }
2792 break;
Wenchao Xia129be002013-08-27 20:38:26 +08002793 case 'S':
2794 {
2795 /* package all remaining string */
2796 int len;
2797
2798 while (qemu_isspace(*p)) {
2799 p++;
2800 }
2801 if (*typestr == '?') {
2802 typestr++;
2803 if (*p == '\0') {
2804 /* no remaining string: NULL argument */
2805 break;
2806 }
2807 }
2808 len = strlen(p);
2809 if (len <= 0) {
2810 monitor_printf(mon, "%s: string expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002811 cmd->name);
Bandan Dase549d2a2015-06-03 18:38:10 -04002812 goto fail;
Wenchao Xia129be002013-08-27 20:38:26 +08002813 }
2814 qdict_put(qdict, key, qstring_from_str(p));
2815 p += len;
2816 }
2817 break;
bellard9307c4c2004-04-04 12:57:25 +00002818 default:
2819 bad_type:
Bandan Dasae502122015-06-03 18:38:08 -04002820 monitor_printf(mon, "%s: unknown type '%c'\n", cmd->name, c);
bellard9307c4c2004-04-04 12:57:25 +00002821 goto fail;
2822 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002823 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002824 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00002825 }
2826 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00002827 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002828 p++;
2829 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00002830 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
Bandan Dasae502122015-06-03 18:38:08 -04002831 cmd->name);
bellard9307c4c2004-04-04 12:57:25 +00002832 goto fail;
2833 }
2834
Bandan Dasae502122015-06-03 18:38:08 -04002835 return qdict;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02002836
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002837fail:
Bandan Dasae502122015-06-03 18:38:08 -04002838 QDECREF(qdict);
Anthony Liguori7267c092011-08-20 22:09:37 -05002839 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002840 return NULL;
2841}
2842
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01002843static void handle_hmp_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002844{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002845 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05002846 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002847
Bandan Dasae502122015-06-03 18:38:08 -04002848 cmd = monitor_parse_command(mon, &cmdline, mon->cmd_table);
2849 if (!cmd) {
2850 return;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002851 }
2852
Bandan Dasae502122015-06-03 18:38:08 -04002853 qdict = monitor_parse_arguments(mon, &cmdline, cmd);
2854 if (!qdict) {
Bandan Dasdd41eea2015-06-03 18:38:09 -04002855 monitor_printf(mon, "Try \"help %s\" for more information\n",
2856 cmd->name);
Bandan Dasae502122015-06-03 18:38:08 -04002857 return;
2858 }
2859
2860 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002861 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00002862}
2863
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08002864static void cmd_completion(Monitor *mon, const char *name, const char *list)
bellard81d09122004-07-14 17:21:37 +00002865{
2866 const char *p, *pstart;
2867 char cmd[128];
2868 int len;
2869
2870 p = list;
2871 for(;;) {
2872 pstart = p;
2873 p = strchr(p, '|');
2874 if (!p)
2875 p = pstart + strlen(pstart);
2876 len = p - pstart;
2877 if (len > sizeof(cmd) - 2)
2878 len = sizeof(cmd) - 2;
2879 memcpy(cmd, pstart, len);
2880 cmd[len] = '\0';
2881 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08002882 readline_add_completion(mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00002883 }
2884 if (*p == '\0')
2885 break;
2886 p++;
2887 }
2888}
2889
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08002890static void file_completion(Monitor *mon, const char *input)
bellard81d09122004-07-14 17:21:37 +00002891{
2892 DIR *ffs;
2893 struct dirent *d;
2894 char path[1024];
2895 char file[1024], file_prefix[1024];
2896 int input_path_len;
2897 const char *p;
2898
ths5fafdf22007-09-16 21:08:06 +00002899 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00002900 if (!p) {
2901 input_path_len = 0;
2902 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00002903 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00002904 } else {
2905 input_path_len = p - input + 1;
2906 memcpy(path, input, input_path_len);
2907 if (input_path_len > sizeof(path) - 1)
2908 input_path_len = sizeof(path) - 1;
2909 path[input_path_len] = '\0';
2910 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2911 }
Bandan Das19f2db52015-06-03 18:38:07 -04002912
bellard81d09122004-07-14 17:21:37 +00002913 ffs = opendir(path);
2914 if (!ffs)
2915 return;
2916 for(;;) {
2917 struct stat sb;
2918 d = readdir(ffs);
2919 if (!d)
2920 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09002921
2922 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
2923 continue;
2924 }
2925
bellard81d09122004-07-14 17:21:37 +00002926 if (strstart(d->d_name, file_prefix, NULL)) {
2927 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00002928 if (input_path_len < sizeof(file))
2929 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2930 d->d_name);
bellard81d09122004-07-14 17:21:37 +00002931 /* stat the file to find out if it's a directory.
2932 * In that case add a slash to speed up typing long paths
2933 */
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01002934 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
blueswir1363a37d2008-08-21 17:58:08 +00002935 pstrcat(file, sizeof(file), "/");
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01002936 }
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08002937 readline_add_completion(mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00002938 }
2939 }
2940 closedir(ffs);
2941}
2942
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002943static const char *next_arg_type(const char *typestr)
2944{
2945 const char *p = strchr(typestr, ':');
2946 return (p != NULL ? ++p : typestr);
2947}
2948
Hani Benhabiles40d19392014-05-07 23:41:30 +01002949static void add_completion_option(ReadLineState *rs, const char *str,
2950 const char *option)
2951{
2952 if (!str || !option) {
2953 return;
2954 }
2955 if (!strncmp(option, str, strlen(str))) {
2956 readline_add_completion(rs, option);
2957 }
2958}
2959
Hani Benhabiles13e315d2014-05-07 23:41:29 +01002960void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
2961{
2962 size_t len;
2963 ChardevBackendInfoList *list, *start;
2964
2965 if (nb_args != 2) {
2966 return;
2967 }
2968 len = strlen(str);
2969 readline_set_completion_index(rs, len);
2970
2971 start = list = qmp_query_chardev_backends(NULL);
2972 while (list) {
2973 const char *chr_name = list->value->name;
2974
2975 if (!strncmp(chr_name, str, len)) {
2976 readline_add_completion(rs, chr_name);
2977 }
2978 list = list->next;
2979 }
2980 qapi_free_ChardevBackendInfoList(start);
2981}
2982
Hani Benhabilesb162b492014-05-07 23:41:31 +01002983void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
2984{
2985 size_t len;
2986 int i;
2987
2988 if (nb_args != 2) {
2989 return;
2990 }
2991 len = strlen(str);
2992 readline_set_completion_index(rs, len);
2993 for (i = 0; NetClientOptionsKind_lookup[i]; i++) {
2994 add_completion_option(rs, str, NetClientOptionsKind_lookup[i]);
2995 }
2996}
2997
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01002998void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles992d3e62014-02-06 23:30:11 +01002999{
3000 GSList *list, *elt;
3001 size_t len;
3002
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003003 if (nb_args != 2) {
3004 return;
3005 }
3006
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003007 len = strlen(str);
3008 readline_set_completion_index(rs, len);
3009 list = elt = object_class_get_list(TYPE_DEVICE, false);
3010 while (elt) {
3011 const char *name;
3012 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
3013 TYPE_DEVICE);
3014 name = object_class_get_name(OBJECT_CLASS(dc));
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003015
3016 if (!dc->cannot_instantiate_with_device_add_yet
3017 && !strncmp(name, str, len)) {
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003018 readline_add_completion(rs, name);
3019 }
3020 elt = elt->next;
3021 }
3022 g_slist_free(list);
3023}
3024
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003025void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003026{
3027 GSList *list, *elt;
3028 size_t len;
3029
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003030 if (nb_args != 2) {
3031 return;
3032 }
3033
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003034 len = strlen(str);
3035 readline_set_completion_index(rs, len);
3036 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
3037 while (elt) {
3038 const char *name;
3039
3040 name = object_class_get_name(OBJECT_CLASS(elt->data));
3041 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
3042 readline_add_completion(rs, name);
3043 }
3044 elt = elt->next;
3045 }
3046 g_slist_free(list);
3047}
3048
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003049static void peripheral_device_del_completion(ReadLineState *rs,
3050 const char *str, size_t len)
3051{
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003052 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
3053 GSList *list, *item;
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003054
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003055 list = qdev_build_hotpluggable_device_list(peripheral);
3056 if (!list) {
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003057 return;
3058 }
3059
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003060 for (item = list; item; item = g_slist_next(item)) {
3061 DeviceState *dev = item->data;
3062
3063 if (dev->id && !strncmp(str, dev->id, len)) {
3064 readline_add_completion(rs, dev->id);
3065 }
3066 }
3067
3068 g_slist_free(list);
3069}
3070
Hani Benhabiles6297d9a2014-05-07 23:41:28 +01003071void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3072{
3073 size_t len;
3074 ChardevInfoList *list, *start;
3075
3076 if (nb_args != 2) {
3077 return;
3078 }
3079 len = strlen(str);
3080 readline_set_completion_index(rs, len);
3081
3082 start = list = qmp_query_chardev(NULL);
3083 while (list) {
3084 ChardevInfo *chr = list->value;
3085
3086 if (!strncmp(chr->label, str, len)) {
3087 readline_add_completion(rs, chr->label);
3088 }
3089 list = list->next;
3090 }
3091 qapi_free_ChardevInfoList(start);
3092}
3093
Hani Benhabiles8e597772014-05-27 23:39:30 +01003094static void ringbuf_completion(ReadLineState *rs, const char *str)
3095{
3096 size_t len;
3097 ChardevInfoList *list, *start;
3098
3099 len = strlen(str);
3100 readline_set_completion_index(rs, len);
3101
3102 start = list = qmp_query_chardev(NULL);
3103 while (list) {
3104 ChardevInfo *chr_info = list->value;
3105
3106 if (!strncmp(chr_info->label, str, len)) {
3107 CharDriverState *chr = qemu_chr_find(chr_info->label);
3108 if (chr && chr_is_ringbuf(chr)) {
3109 readline_add_completion(rs, chr_info->label);
3110 }
3111 }
3112 list = list->next;
3113 }
3114 qapi_free_ChardevInfoList(start);
3115}
3116
Hani Benhabiles8e597772014-05-27 23:39:30 +01003117void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
3118{
3119 if (nb_args != 2) {
3120 return;
3121 }
3122 ringbuf_completion(rs, str);
3123}
3124
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003125void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
3126{
3127 size_t len;
3128
3129 if (nb_args != 2) {
3130 return;
3131 }
3132
3133 len = strlen(str);
3134 readline_set_completion_index(rs, len);
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003135 peripheral_device_del_completion(rs, str, len);
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003136}
3137
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003138void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003139{
3140 ObjectPropertyInfoList *list, *start;
3141 size_t len;
3142
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003143 if (nb_args != 2) {
3144 return;
3145 }
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003146 len = strlen(str);
3147 readline_set_completion_index(rs, len);
3148
3149 start = list = qmp_qom_list("/objects", NULL);
3150 while (list) {
3151 ObjectPropertyInfo *info = list->value;
3152
3153 if (!strncmp(info->type, "child<", 5)
3154 && !strncmp(info->name, str, len)) {
3155 readline_add_completion(rs, info->name);
3156 }
3157 list = list->next;
3158 }
3159 qapi_free_ObjectPropertyInfoList(start);
3160}
3161
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003162void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
3163{
3164 int i;
3165 char *sep;
3166 size_t len;
3167
3168 if (nb_args != 2) {
3169 return;
3170 }
3171 sep = strrchr(str, '-');
3172 if (sep) {
3173 str = sep + 1;
3174 }
3175 len = strlen(str);
3176 readline_set_completion_index(rs, len);
3177 for (i = 0; i < Q_KEY_CODE_MAX; i++) {
3178 if (!strncmp(str, QKeyCode_lookup[i], len)) {
3179 readline_add_completion(rs, QKeyCode_lookup[i]);
3180 }
3181 }
3182}
3183
Hani Benhabiles40d19392014-05-07 23:41:30 +01003184void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
3185{
3186 size_t len;
3187
3188 len = strlen(str);
3189 readline_set_completion_index(rs, len);
3190 if (nb_args == 2) {
Jason Wangeaed4832015-04-23 14:21:38 +08003191 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles40d19392014-05-07 23:41:30 +01003192 int count, i;
3193 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08003194 NET_CLIENT_OPTIONS_KIND_NONE,
3195 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003196 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles40d19392014-05-07 23:41:30 +01003197 const char *name = ncs[i]->name;
3198 if (!strncmp(str, name, len)) {
3199 readline_add_completion(rs, name);
3200 }
3201 }
3202 } else if (nb_args == 3) {
3203 add_completion_option(rs, str, "on");
3204 add_completion_option(rs, str, "off");
3205 }
3206}
3207
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003208void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
3209{
3210 int len, count, i;
Jason Wangeaed4832015-04-23 14:21:38 +08003211 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003212
3213 if (nb_args != 2) {
3214 return;
3215 }
3216
3217 len = strlen(str);
3218 readline_set_completion_index(rs, len);
3219 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_OPTIONS_KIND_NIC,
Jason Wangeaed4832015-04-23 14:21:38 +08003220 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003221 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003222 QemuOpts *opts;
3223 const char *name = ncs[i]->name;
3224 if (strncmp(str, name, len)) {
3225 continue;
3226 }
3227 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
3228 if (opts) {
3229 readline_add_completion(rs, name);
3230 }
3231 }
3232}
3233
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003234void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
3235{
3236 size_t len;
3237
3238 len = strlen(str);
3239 readline_set_completion_index(rs, len);
3240 if (nb_args == 2) {
3241 TraceEventID id;
3242 for (id = 0; id < trace_event_count(); id++) {
3243 const char *event_name = trace_event_get_name(trace_event_id(id));
3244 if (!strncmp(str, event_name, len)) {
3245 readline_add_completion(rs, event_name);
3246 }
3247 }
3248 } else if (nb_args == 3) {
3249 add_completion_option(rs, str, "on");
3250 add_completion_option(rs, str, "off");
3251 }
3252}
3253
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003254void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
3255{
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003256 int i;
3257
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003258 if (nb_args != 2) {
3259 return;
3260 }
3261 readline_set_completion_index(rs, strlen(str));
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003262 for (i = 0; WatchdogExpirationAction_lookup[i]; i++) {
3263 add_completion_option(rs, str, WatchdogExpirationAction_lookup[i]);
3264 }
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003265}
3266
Hani Benhabilesc68a0402014-05-27 23:39:32 +01003267void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
3268 const char *str)
3269{
3270 size_t len;
3271
3272 len = strlen(str);
3273 readline_set_completion_index(rs, len);
3274 if (nb_args == 2) {
3275 int i;
3276 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
3277 const char *name = MigrationCapability_lookup[i];
3278 if (!strncmp(str, name, len)) {
3279 readline_add_completion(rs, name);
3280 }
3281 }
3282 } else if (nb_args == 3) {
3283 add_completion_option(rs, str, "on");
3284 add_completion_option(rs, str, "off");
3285 }
3286}
3287
Liang Li50e9a622015-03-23 16:32:29 +08003288void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
3289 const char *str)
3290{
3291 size_t len;
3292
3293 len = strlen(str);
3294 readline_set_completion_index(rs, len);
3295 if (nb_args == 2) {
3296 int i;
3297 for (i = 0; i < MIGRATION_PARAMETER_MAX; i++) {
3298 const char *name = MigrationParameter_lookup[i];
3299 if (!strncmp(str, name, len)) {
3300 readline_add_completion(rs, name);
3301 }
3302 }
3303 }
3304}
3305
Hani Benhabilese3bb5322014-05-27 23:39:34 +01003306void host_net_add_completion(ReadLineState *rs, int nb_args, const char *str)
3307{
3308 int i;
3309 size_t len;
3310 if (nb_args != 2) {
3311 return;
3312 }
3313 len = strlen(str);
3314 readline_set_completion_index(rs, len);
3315 for (i = 0; host_net_devices[i]; i++) {
3316 if (!strncmp(host_net_devices[i], str, len)) {
3317 readline_add_completion(rs, host_net_devices[i]);
3318 }
3319 }
3320}
3321
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003322void host_net_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3323{
Jason Wangeaed4832015-04-23 14:21:38 +08003324 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003325 int count, i, len;
3326
3327 len = strlen(str);
3328 readline_set_completion_index(rs, len);
3329 if (nb_args == 2) {
3330 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08003331 NET_CLIENT_OPTIONS_KIND_NONE,
3332 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003333 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003334 int id;
3335 char name[16];
3336
3337 if (net_hub_id_for_client(ncs[i], &id)) {
3338 continue;
3339 }
3340 snprintf(name, sizeof(name), "%d", id);
3341 if (!strncmp(str, name, len)) {
3342 readline_add_completion(rs, name);
3343 }
3344 }
3345 return;
3346 } else if (nb_args == 3) {
3347 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08003348 NET_CLIENT_OPTIONS_KIND_NIC,
3349 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003350 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Jason Wang2c4681f2015-02-02 15:06:38 +08003351 int id;
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003352 const char *name;
3353
Jason Wang2c4681f2015-02-02 15:06:38 +08003354 if (ncs[i]->info->type == NET_CLIENT_OPTIONS_KIND_HUBPORT ||
3355 net_hub_id_for_client(ncs[i], &id)) {
3356 continue;
3357 }
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003358 name = ncs[i]->name;
3359 if (!strncmp(str, name, len)) {
3360 readline_add_completion(rs, name);
3361 }
3362 }
3363 return;
3364 }
3365}
3366
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003367static void vm_completion(ReadLineState *rs, const char *str)
3368{
3369 size_t len;
3370 BlockDriverState *bs = NULL;
3371
3372 len = strlen(str);
3373 readline_set_completion_index(rs, len);
3374 while ((bs = bdrv_next(bs))) {
3375 SnapshotInfoList *snapshots, *snapshot;
3376
3377 if (!bdrv_can_snapshot(bs)) {
3378 continue;
3379 }
3380 if (bdrv_query_snapshot_info_list(bs, &snapshots, NULL)) {
3381 continue;
3382 }
3383 snapshot = snapshots;
3384 while (snapshot) {
3385 char *completion = snapshot->value->name;
3386 if (!strncmp(str, completion, len)) {
3387 readline_add_completion(rs, completion);
3388 }
3389 completion = snapshot->value->id;
3390 if (!strncmp(str, completion, len)) {
3391 readline_add_completion(rs, completion);
3392 }
3393 snapshot = snapshot->next;
3394 }
3395 qapi_free_SnapshotInfoList(snapshots);
3396 }
3397
3398}
3399
3400void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
3401{
3402 if (nb_args == 2) {
3403 vm_completion(rs, str);
3404 }
3405}
3406
3407void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
3408{
3409 if (nb_args == 2) {
3410 vm_completion(rs, str);
3411 }
3412}
3413
Wenchao Xiac35b6402013-08-27 20:38:24 +08003414static void monitor_find_completion_by_table(Monitor *mon,
3415 const mon_cmd_t *cmd_table,
3416 char **args,
3417 int nb_args)
bellard81d09122004-07-14 17:21:37 +00003418{
3419 const char *cmdname;
Wenchao Xiac35b6402013-08-27 20:38:24 +08003420 int i;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003421 const char *ptype, *str, *name;
Anthony Liguoric227f092009-10-01 16:12:16 -05003422 const mon_cmd_t *cmd;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003423 BlockDriverState *bs;
bellard81d09122004-07-14 17:21:37 +00003424
bellard81d09122004-07-14 17:21:37 +00003425 if (nb_args <= 1) {
3426 /* command completion */
3427 if (nb_args == 0)
3428 cmdname = "";
3429 else
3430 cmdname = args[0];
Wenchao Xiad2674b22013-08-27 20:38:16 +08003431 readline_set_completion_index(mon->rs, strlen(cmdname));
Wenchao Xiac35b6402013-08-27 20:38:24 +08003432 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003433 cmd_completion(mon, cmdname, cmd->name);
bellard81d09122004-07-14 17:21:37 +00003434 }
3435 } else {
3436 /* find the command */
Wenchao Xiac35b6402013-08-27 20:38:24 +08003437 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Jan Kiszka03a63482010-06-16 00:38:33 +02003438 if (compare_cmd(args[0], cmd->name)) {
3439 break;
3440 }
bellard81d09122004-07-14 17:21:37 +00003441 }
Jan Kiszka03a63482010-06-16 00:38:33 +02003442 if (!cmd->name) {
Wenchao Xiac35b6402013-08-27 20:38:24 +08003443 return;
Jan Kiszka03a63482010-06-16 00:38:33 +02003444 }
3445
Wenchao Xiad903a772013-08-27 20:38:25 +08003446 if (cmd->sub_table) {
3447 /* do the job again */
Stefan Weile7ae7712015-03-08 19:30:01 +01003448 monitor_find_completion_by_table(mon, cmd->sub_table,
3449 &args[1], nb_args - 1);
3450 return;
Wenchao Xiad903a772013-08-27 20:38:25 +08003451 }
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003452 if (cmd->command_completion) {
Stefan Weile7ae7712015-03-08 19:30:01 +01003453 cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
3454 return;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003455 }
Wenchao Xiad903a772013-08-27 20:38:25 +08003456
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003457 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003458 for(i = 0; i < nb_args - 2; i++) {
3459 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003460 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003461 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003462 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003463 }
3464 }
3465 str = args[nb_args - 1];
Kevin Wolf48fe86f2014-11-12 16:24:02 +01003466 while (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02003467 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00003468 }
bellard81d09122004-07-14 17:21:37 +00003469 switch(*ptype) {
3470 case 'F':
3471 /* file completion */
Wenchao Xiad2674b22013-08-27 20:38:16 +08003472 readline_set_completion_index(mon->rs, strlen(str));
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003473 file_completion(mon, str);
bellard81d09122004-07-14 17:21:37 +00003474 break;
3475 case 'B':
3476 /* block device name completion */
Wenchao Xia599a9262013-08-27 20:38:15 +08003477 readline_set_completion_index(mon->rs, strlen(str));
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003478 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
3479 name = bdrv_get_device_name(bs);
3480 if (str[0] == '\0' ||
3481 !strncmp(name, str, strlen(str))) {
3482 readline_add_completion(mon->rs, name);
3483 }
3484 }
bellard81d09122004-07-14 17:21:37 +00003485 break;
bellard7fe48482004-10-09 18:08:01 +00003486 case 's':
Wenchao Xia129be002013-08-27 20:38:26 +08003487 case 'S':
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003488 if (!strcmp(cmd->name, "help|?")) {
Wenchao Xia7ca0e062013-08-27 20:38:27 +08003489 monitor_find_completion_by_table(mon, cmd_table,
3490 &args[1], nb_args - 1);
bellard7fe48482004-10-09 18:08:01 +00003491 }
3492 break;
bellard81d09122004-07-14 17:21:37 +00003493 default:
3494 break;
3495 }
3496 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08003497}
3498
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003499static void monitor_find_completion(void *opaque,
Wenchao Xiac35b6402013-08-27 20:38:24 +08003500 const char *cmdline)
3501{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003502 Monitor *mon = opaque;
Wenchao Xiac35b6402013-08-27 20:38:24 +08003503 char *args[MAX_ARGS];
3504 int nb_args, len;
3505
3506 /* 1. parse the cmdline */
3507 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
3508 return;
3509 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08003510
3511 /* if the line ends with a space, it means we want to complete the
3512 next arg */
3513 len = strlen(cmdline);
3514 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
3515 if (nb_args >= MAX_ARGS) {
3516 goto cleanup;
3517 }
3518 args[nb_args++] = g_strdup("");
3519 }
3520
3521 /* 2. auto complete according to args */
3522 monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args);
Jan Kiszka03a63482010-06-16 00:38:33 +02003523
3524cleanup:
Wenchao Xiadcc70cd2013-08-27 20:38:22 +08003525 free_cmdline_args(args, nb_args);
bellard81d09122004-07-14 17:21:37 +00003526}
3527
aliguori731b0362009-03-05 23:01:42 +00003528static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003529{
aliguori731b0362009-03-05 23:01:42 +00003530 Monitor *mon = opaque;
3531
Jan Kiszkac62313b2009-12-04 14:05:29 +01003532 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003533}
3534
Markus Armbruster40861822015-05-29 10:27:16 +02003535static bool invalid_qmp_mode(const Monitor *mon, const mon_cmd_t *cmd,
3536 Error **errp)
Luiz Capitulino09069b12010-02-04 18:10:06 -02003537{
Markus Armbruster485febc2015-03-13 17:25:50 +01003538 bool is_cap = cmd->mhandler.cmd_new == qmp_capabilities;
Markus Armbrusterf994b252015-03-06 19:51:51 +01003539
3540 if (is_cap && mon->qmp.in_command_mode) {
Markus Armbruster40861822015-05-29 10:27:16 +02003541 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
3542 "Capabilities negotiation is already complete, command "
3543 "'%s' ignored", cmd->name);
Eric Blake2d5a8342015-04-15 09:19:23 -06003544 return true;
3545 }
Markus Armbrusterf994b252015-03-06 19:51:51 +01003546 if (!is_cap && !mon->qmp.in_command_mode) {
Markus Armbruster40861822015-05-29 10:27:16 +02003547 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
3548 "Expecting capabilities negotiation with "
3549 "'qmp_capabilities' before command '%s'", cmd->name);
Eric Blake2d5a8342015-04-15 09:19:23 -06003550 return true;
3551 }
3552 return false;
Luiz Capitulino09069b12010-02-04 18:10:06 -02003553}
3554
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003555/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03003556 * Argument validation rules:
3557 *
3558 * 1. The argument must exist in cmd_args qdict
3559 * 2. The argument type must be the expected one
3560 *
3561 * Special case: If the argument doesn't exist in cmd_args and
3562 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
3563 * checking is skipped for it.
3564 */
Markus Armbruster326283a2015-03-02 18:39:09 +01003565static void check_client_args_type(const QDict *client_args,
3566 const QDict *cmd_args, int flags,
3567 Error **errp)
Luiz Capitulino4af91932010-06-22 11:44:05 -03003568{
3569 const QDictEntry *ent;
3570
3571 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
3572 QObject *obj;
3573 QString *arg_type;
3574 const QObject *client_arg = qdict_entry_value(ent);
3575 const char *client_arg_name = qdict_entry_key(ent);
3576
3577 obj = qdict_get(cmd_args, client_arg_name);
3578 if (!obj) {
3579 if (flags & QMP_ACCEPT_UNKNOWNS) {
3580 /* handler accepts unknowns */
3581 continue;
3582 }
3583 /* client arg doesn't exist */
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003584 error_setg(errp, QERR_INVALID_PARAMETER, client_arg_name);
Markus Armbruster326283a2015-03-02 18:39:09 +01003585 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003586 }
3587
3588 arg_type = qobject_to_qstring(obj);
3589 assert(arg_type != NULL);
3590
3591 /* check if argument's type is correct */
3592 switch (qstring_get_str(arg_type)[0]) {
3593 case 'F':
3594 case 'B':
3595 case 's':
3596 if (qobject_type(client_arg) != QTYPE_QSTRING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003597 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3598 client_arg_name, "string");
Markus Armbruster326283a2015-03-02 18:39:09 +01003599 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003600 }
3601 break;
3602 case 'i':
3603 case 'l':
3604 case 'M':
Jes Sorensendbc0c672010-10-21 17:15:47 +02003605 case 'o':
Luiz Capitulino4af91932010-06-22 11:44:05 -03003606 if (qobject_type(client_arg) != QTYPE_QINT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003607 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3608 client_arg_name, "int");
Markus Armbruster326283a2015-03-02 18:39:09 +01003609 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003610 }
3611 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003612 case 'T':
3613 if (qobject_type(client_arg) != QTYPE_QINT &&
3614 qobject_type(client_arg) != QTYPE_QFLOAT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003615 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3616 client_arg_name, "number");
Markus Armbruster326283a2015-03-02 18:39:09 +01003617 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003618 }
3619 break;
3620 case 'b':
3621 case '-':
3622 if (qobject_type(client_arg) != QTYPE_QBOOL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003623 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3624 client_arg_name, "bool");
Markus Armbruster326283a2015-03-02 18:39:09 +01003625 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003626 }
3627 break;
3628 case 'O':
3629 assert(flags & QMP_ACCEPT_UNKNOWNS);
3630 break;
Paolo Bonzinib9f89782012-03-22 12:51:11 +01003631 case 'q':
3632 /* Any QObject can be passed. */
3633 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003634 case '/':
3635 case '.':
3636 /*
3637 * These types are not supported by QMP and thus are not
3638 * handled here. Fall through.
3639 */
3640 default:
3641 abort();
3642 }
3643 }
Luiz Capitulino4af91932010-06-22 11:44:05 -03003644}
3645
3646/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003647 * - Check if the client has passed all mandatory args
3648 * - Set special flags for argument validation
3649 */
Markus Armbruster326283a2015-03-02 18:39:09 +01003650static void check_mandatory_args(const QDict *cmd_args,
3651 const QDict *client_args, int *flags,
3652 Error **errp)
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003653{
3654 const QDictEntry *ent;
3655
3656 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
3657 const char *cmd_arg_name = qdict_entry_key(ent);
3658 QString *type = qobject_to_qstring(qdict_entry_value(ent));
3659 assert(type != NULL);
3660
3661 if (qstring_get_str(type)[0] == 'O') {
3662 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
3663 *flags |= QMP_ACCEPT_UNKNOWNS;
3664 } else if (qstring_get_str(type)[0] != '-' &&
3665 qstring_get_str(type)[1] != '?' &&
3666 !qdict_haskey(client_args, cmd_arg_name)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003667 error_setg(errp, QERR_MISSING_PARAMETER, cmd_arg_name);
Markus Armbruster326283a2015-03-02 18:39:09 +01003668 return;
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003669 }
3670 }
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003671}
3672
3673static QDict *qdict_from_args_type(const char *args_type)
3674{
3675 int i;
3676 QDict *qdict;
3677 QString *key, *type, *cur_qs;
3678
3679 assert(args_type != NULL);
3680
3681 qdict = qdict_new();
3682
3683 if (args_type == NULL || args_type[0] == '\0') {
3684 /* no args, empty qdict */
3685 goto out;
3686 }
3687
3688 key = qstring_new();
3689 type = qstring_new();
3690
3691 cur_qs = key;
3692
3693 for (i = 0;; i++) {
3694 switch (args_type[i]) {
3695 case ',':
3696 case '\0':
3697 qdict_put(qdict, qstring_get_str(key), type);
3698 QDECREF(key);
3699 if (args_type[i] == '\0') {
3700 goto out;
3701 }
3702 type = qstring_new(); /* qdict has ref */
3703 cur_qs = key = qstring_new();
3704 break;
3705 case ':':
3706 cur_qs = type;
3707 break;
3708 default:
3709 qstring_append_chr(cur_qs, args_type[i]);
3710 break;
3711 }
3712 }
3713
3714out:
3715 return qdict;
3716}
3717
3718/*
3719 * Client argument checking rules:
3720 *
3721 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03003722 * 2. Each argument provided by the client must be expected
3723 * 3. Each argument provided by the client must have the type expected
3724 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003725 */
Markus Armbruster326283a2015-03-02 18:39:09 +01003726static void qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args,
3727 Error **errp)
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003728{
Markus Armbruster326283a2015-03-02 18:39:09 +01003729 Error *err = NULL;
3730 int flags;
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003731 QDict *cmd_args;
3732
3733 cmd_args = qdict_from_args_type(cmd->args_type);
3734
3735 flags = 0;
Markus Armbruster326283a2015-03-02 18:39:09 +01003736 check_mandatory_args(cmd_args, client_args, &flags, &err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003737 if (err) {
3738 goto out;
3739 }
3740
Markus Armbruster326283a2015-03-02 18:39:09 +01003741 check_client_args_type(client_args, cmd_args, flags, &err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003742
3743out:
Markus Armbruster326283a2015-03-02 18:39:09 +01003744 error_propagate(errp, err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003745 QDECREF(cmd_args);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003746}
3747
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003748/*
3749 * Input object checking rules
3750 *
3751 * 1. Input object must be a dict
3752 * 2. The "execute" key must exist
3753 * 3. The "execute" key must be a string
3754 * 4. If the "arguments" key exists, it must be a dict
3755 * 5. If the "id" key exists, it can be anything (ie. json-value)
3756 * 6. Any argument not listed above is considered invalid
3757 */
Markus Armbrusterba0510a2015-03-02 18:41:43 +01003758static QDict *qmp_check_input_obj(QObject *input_obj, Error **errp)
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003759{
3760 const QDictEntry *ent;
3761 int has_exec_key = 0;
3762 QDict *input_dict;
3763
3764 if (qobject_type(input_obj) != QTYPE_QDICT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003765 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "object");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003766 return NULL;
3767 }
3768
3769 input_dict = qobject_to_qdict(input_obj);
3770
3771 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
3772 const char *arg_name = qdict_entry_key(ent);
3773 const QObject *arg_obj = qdict_entry_value(ent);
3774
3775 if (!strcmp(arg_name, "execute")) {
3776 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003777 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
3778 "execute", "string");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003779 return NULL;
3780 }
3781 has_exec_key = 1;
3782 } else if (!strcmp(arg_name, "arguments")) {
3783 if (qobject_type(arg_obj) != QTYPE_QDICT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003784 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
3785 "arguments", "object");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003786 return NULL;
3787 }
Markus Armbruster779cec42015-06-08 10:44:30 +02003788 } else if (!strcmp(arg_name, "id")) {
3789 /* Any string is acceptable as "id", so nothing to check */
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003790 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003791 error_setg(errp, QERR_QMP_EXTRA_MEMBER, arg_name);
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003792 return NULL;
3793 }
3794 }
3795
3796 if (!has_exec_key) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003797 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "execute");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003798 return NULL;
3799 }
3800
3801 return input_dict;
3802}
3803
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003804static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
3805{
Markus Armbruster326283a2015-03-02 18:39:09 +01003806 Error *local_err = NULL;
Markus Armbruster84add862015-03-05 16:45:15 +01003807 QObject *obj, *data;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003808 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003809 const mon_cmd_t *cmd;
Luiz Capitulino40e5a012011-10-21 16:15:31 -02003810 const char *cmd_name;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003811 Monitor *mon = cur_mon;
3812
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03003813 args = input = NULL;
Markus Armbruster84add862015-03-05 16:45:15 +01003814 data = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003815
3816 obj = json_parser_parse(tokens, NULL);
3817 if (!obj) {
3818 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003819 error_setg(&local_err, QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003820 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003821 }
3822
Markus Armbrusterba0510a2015-03-02 18:41:43 +01003823 input = qmp_check_input_obj(obj, &local_err);
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003824 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003825 qobject_decref(obj);
3826 goto err_out;
3827 }
3828
Markus Armbruster74358f22015-03-06 19:35:59 +01003829 mon->qmp.id = qdict_get(input, "id");
3830 qobject_incref(mon->qmp.id);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003831
Luiz Capitulino0bbab462010-05-31 17:32:50 -03003832 cmd_name = qdict_get_str(input, "execute");
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +01003833 trace_handle_qmp_command(mon, cmd_name);
Anthony Liguorie3193602011-09-02 12:34:47 -05003834 cmd = qmp_find_cmd(cmd_name);
Eric Blake2d5a8342015-04-15 09:19:23 -06003835 if (!cmd) {
Markus Armbruster710aec92015-03-06 11:28:00 +01003836 error_set(&local_err, ERROR_CLASS_COMMAND_NOT_FOUND,
3837 "The command %s has not been found", cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03003838 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003839 }
Markus Armbruster40861822015-05-29 10:27:16 +02003840 if (invalid_qmp_mode(mon, cmd, &local_err)) {
Eric Blake2d5a8342015-04-15 09:19:23 -06003841 goto err_out;
3842 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003843
3844 obj = qdict_get(input, "arguments");
3845 if (!obj) {
3846 args = qdict_new();
3847 } else {
3848 args = qobject_to_qdict(obj);
3849 QINCREF(args);
3850 }
3851
Markus Armbruster326283a2015-03-02 18:39:09 +01003852 qmp_check_client_args(cmd, args, &local_err);
3853 if (local_err) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003854 goto err_out;
3855 }
3856
Markus Armbruster485febc2015-03-13 17:25:50 +01003857 cmd->mhandler.cmd_new(args, &data, &local_err);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003858
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003859err_out:
Markus Armbruster710aec92015-03-06 11:28:00 +01003860 monitor_protocol_emitter(mon, data, local_err);
Markus Armbruster84add862015-03-05 16:45:15 +01003861 qobject_decref(data);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03003862 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003863 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003864}
3865
Markus Armbrusterc83fe232015-03-06 19:20:51 +01003866static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003867{
3868 Monitor *old_mon = cur_mon;
3869
3870 cur_mon = opaque;
3871
Markus Armbruster74358f22015-03-06 19:35:59 +01003872 json_message_parser_feed(&cur_mon->qmp.parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003873
3874 cur_mon = old_mon;
3875}
3876
aliguori731b0362009-03-05 23:01:42 +00003877static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00003878{
aliguori731b0362009-03-05 23:01:42 +00003879 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00003880 int i;
aliguori376253e2009-03-05 23:01:23 +00003881
aliguori731b0362009-03-05 23:01:42 +00003882 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00003883
aliguoricde76ee2009-03-05 23:01:51 +00003884 if (cur_mon->rs) {
3885 for (i = 0; i < size; i++)
3886 readline_handle_byte(cur_mon->rs, buf[i]);
3887 } else {
3888 if (size == 0 || buf[size - 1] != 0)
3889 monitor_printf(cur_mon, "corrupted command\n");
3890 else
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003891 handle_hmp_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00003892 }
aliguori731b0362009-03-05 23:01:42 +00003893
3894 cur_mon = old_mon;
3895}
aliguorid8f44602008-10-06 13:52:44 +00003896
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003897static void monitor_command_cb(void *opaque, const char *cmdline,
3898 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00003899{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003900 Monitor *mon = opaque;
3901
aliguori731b0362009-03-05 23:01:42 +00003902 monitor_suspend(mon);
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003903 handle_hmp_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00003904 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00003905}
3906
aliguoricde76ee2009-03-05 23:01:51 +00003907int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003908{
aliguoricde76ee2009-03-05 23:01:51 +00003909 if (!mon->rs)
3910 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00003911 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00003912 return 0;
aliguorid8f44602008-10-06 13:52:44 +00003913}
3914
aliguori376253e2009-03-05 23:01:23 +00003915void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003916{
aliguoricde76ee2009-03-05 23:01:51 +00003917 if (!mon->rs)
3918 return;
aliguori731b0362009-03-05 23:01:42 +00003919 if (--mon->suspend_cnt == 0)
3920 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00003921}
3922
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003923static QObject *get_qmp_greeting(void)
3924{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03003925 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003926
Markus Armbruster7fad30f2015-09-16 13:06:19 +02003927 qmp_marshal_query_version(NULL, &ver, NULL);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003928 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
3929}
3930
Markus Armbrusterc83fe232015-03-06 19:20:51 +01003931static void monitor_qmp_event(void *opaque, int event)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003932{
Luiz Capitulino47116d12010-02-08 17:01:30 -02003933 QObject *data;
3934 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003935
Luiz Capitulino47116d12010-02-08 17:01:30 -02003936 switch (event) {
3937 case CHR_EVENT_OPENED:
Markus Armbrusterf994b252015-03-06 19:51:51 +01003938 mon->qmp.in_command_mode = false;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003939 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003940 monitor_json_emitter(mon, data);
3941 qobject_decref(data);
Corey Bryantefb87c12012-08-14 16:43:48 -04003942 mon_refcount++;
Luiz Capitulino47116d12010-02-08 17:01:30 -02003943 break;
3944 case CHR_EVENT_CLOSED:
Markus Armbruster74358f22015-03-06 19:35:59 +01003945 json_message_parser_destroy(&mon->qmp.parser);
3946 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Corey Bryantefb87c12012-08-14 16:43:48 -04003947 mon_refcount--;
3948 monitor_fdsets_cleanup();
Luiz Capitulino47116d12010-02-08 17:01:30 -02003949 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003950 }
3951}
3952
aliguori731b0362009-03-05 23:01:42 +00003953static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00003954{
aliguori376253e2009-03-05 23:01:23 +00003955 Monitor *mon = opaque;
3956
aliguori2724b182009-03-05 23:01:47 +00003957 switch (event) {
3958 case CHR_EVENT_MUX_IN:
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003959 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003960 mon->mux_out = 0;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003961 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003962 if (mon->reset_seen) {
3963 readline_restart(mon->rs);
3964 monitor_resume(mon);
3965 monitor_flush(mon);
3966 } else {
3967 mon->suspend_cnt = 0;
3968 }
aliguori2724b182009-03-05 23:01:47 +00003969 break;
ths86e94de2007-01-05 22:01:59 +00003970
aliguori2724b182009-03-05 23:01:47 +00003971 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003972 if (mon->reset_seen) {
3973 if (mon->suspend_cnt == 0) {
3974 monitor_printf(mon, "\n");
3975 }
3976 monitor_flush(mon);
3977 monitor_suspend(mon);
3978 } else {
3979 mon->suspend_cnt++;
3980 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003981 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003982 mon->mux_out = 1;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003983 qemu_mutex_unlock(&mon->out_lock);
aliguori2724b182009-03-05 23:01:47 +00003984 break;
3985
Amit Shahb6b8df52009-10-07 18:31:16 +05303986 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00003987 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3988 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003989 if (!mon->mux_out) {
Stratos Psomadakise5554e22014-09-15 15:34:57 +03003990 readline_restart(mon->rs);
aliguori2724b182009-03-05 23:01:47 +00003991 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003992 }
3993 mon->reset_seen = 1;
Corey Bryantefb87c12012-08-14 16:43:48 -04003994 mon_refcount++;
3995 break;
3996
3997 case CHR_EVENT_CLOSED:
3998 mon_refcount--;
3999 monitor_fdsets_cleanup();
aliguori2724b182009-03-05 23:01:47 +00004000 break;
4001 }
ths86e94de2007-01-05 22:01:59 +00004002}
4003
Wayne Xia816f8922011-10-12 11:32:41 +08004004static int
4005compare_mon_cmd(const void *a, const void *b)
4006{
4007 return strcmp(((const mon_cmd_t *)a)->name,
4008 ((const mon_cmd_t *)b)->name);
4009}
4010
4011static void sortcmdlist(void)
4012{
4013 int array_num;
4014 int elem_size = sizeof(mon_cmd_t);
4015
4016 array_num = sizeof(mon_cmds)/elem_size-1;
4017 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
4018
4019 array_num = sizeof(info_cmds)/elem_size-1;
4020 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
4021}
4022
aliguori76655d62009-03-06 20:27:37 +00004023
4024/*
4025 * Local variables:
4026 * c-indent-level: 4
4027 * c-basic-offset: 4
4028 * tab-width: 8
4029 * End:
4030 */
4031
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004032/* These functions just adapt the readline interface in a typesafe way. We
4033 * could cast function pointers but that discards compiler checks.
4034 */
Stefan Weild5d15072014-01-25 18:18:23 +01004035static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
4036 const char *fmt, ...)
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004037{
4038 va_list ap;
4039 va_start(ap, fmt);
4040 monitor_vprintf(opaque, fmt, ap);
4041 va_end(ap);
4042}
4043
4044static void monitor_readline_flush(void *opaque)
4045{
4046 monitor_flush(opaque);
4047}
4048
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004049static void __attribute__((constructor)) monitor_lock_init(void)
4050{
4051 qemu_mutex_init(&monitor_lock);
4052}
4053
aliguori731b0362009-03-05 23:01:42 +00004054void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004055{
aliguori731b0362009-03-05 23:01:42 +00004056 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004057 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004058
4059 if (is_first_init) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +02004060 monitor_qapi_event_init();
Wenchao Xiad0383172013-08-27 20:38:18 +08004061 sortcmdlist();
ths20d8a3e2007-02-18 17:04:49 +00004062 is_first_init = 0;
4063 }
aliguori87127162009-03-05 23:01:29 +00004064
Wenchao Xiab01fe892013-08-27 20:38:19 +08004065 mon = g_malloc(sizeof(*mon));
4066 monitor_data_init(mon);
ths20d8a3e2007-02-18 17:04:49 +00004067
aliguori87127162009-03-05 23:01:29 +00004068 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00004069 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004070 if (flags & MONITOR_USE_READLINE) {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004071 mon->rs = readline_init(monitor_readline_printf,
4072 monitor_readline_flush,
4073 mon,
4074 monitor_find_completion);
aliguoricde76ee2009-03-05 23:01:51 +00004075 monitor_read_command(mon, 0);
4076 }
aliguori87127162009-03-05 23:01:29 +00004077
Markus Armbruster9f3982f2015-03-06 19:56:38 +01004078 if (monitor_is_qmp(mon)) {
Markus Armbrusterc83fe232015-03-06 19:20:51 +01004079 qemu_chr_add_handlers(chr, monitor_can_read, monitor_qmp_read,
4080 monitor_qmp_event, mon);
Anthony Liguori15f31512011-08-15 11:17:35 -05004081 qemu_chr_fe_set_echo(chr, true);
Markus Armbruster74358f22015-03-06 19:35:59 +01004082 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004083 } else {
4084 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4085 monitor_event, mon);
4086 }
aliguori87127162009-03-05 23:01:29 +00004087
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004088 qemu_mutex_lock(&monitor_lock);
Blue Swirl72cf2d42009-09-12 07:36:22 +00004089 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004090 qemu_mutex_unlock(&monitor_lock);
bellard7e2515e2004-08-01 21:52:19 +00004091}
4092
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004093static void bdrv_password_cb(void *opaque, const char *password,
4094 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00004095{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004096 Monitor *mon = opaque;
4097 BlockDriverState *bs = readline_opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004098 int ret = 0;
Markus Armbruster4d2855a2015-01-29 10:37:00 +01004099 Error *local_err = NULL;
bellard7e2515e2004-08-01 21:52:19 +00004100
Markus Armbruster4d2855a2015-01-29 10:37:00 +01004101 bdrv_add_key(bs, password, &local_err);
4102 if (local_err) {
4103 monitor_printf(mon, "%s\n", error_get_pretty(local_err));
4104 error_free(local_err);
aliguoribb5fc202009-03-05 23:01:15 +00004105 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004106 }
aliguori731b0362009-03-05 23:01:42 +00004107 if (mon->password_completion_cb)
4108 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004109
aliguori731b0362009-03-05 23:01:42 +00004110 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004111}
aliguoric0f4ce72009-03-05 23:01:01 +00004112
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004113int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
Markus Armbruster097310b2014-10-07 13:59:15 +02004114 BlockCompletionFunc *completion_cb,
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004115 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004116{
aliguoricde76ee2009-03-05 23:01:51 +00004117 int err;
4118
aliguori376253e2009-03-05 23:01:23 +00004119 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4120 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00004121
aliguori731b0362009-03-05 23:01:42 +00004122 mon->password_completion_cb = completion_cb;
4123 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004124
aliguoricde76ee2009-03-05 23:01:51 +00004125 err = monitor_read_password(mon, bdrv_password_cb, bs);
4126
4127 if (err && completion_cb)
4128 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004129
4130 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00004131}
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004132
4133int monitor_read_block_device_key(Monitor *mon, const char *device,
Markus Armbruster097310b2014-10-07 13:59:15 +02004134 BlockCompletionFunc *completion_cb,
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004135 void *opaque)
4136{
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01004137 Error *err = NULL;
Fam Zheng55606252015-03-02 19:36:46 +08004138 BlockBackend *blk;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004139
Fam Zheng55606252015-03-02 19:36:46 +08004140 blk = blk_by_name(device);
4141 if (!blk) {
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004142 monitor_printf(mon, "Device not found %s\n", device);
4143 return -1;
4144 }
Max Reitz5433c242015-10-19 17:53:29 +02004145 if (!blk_bs(blk)) {
4146 monitor_printf(mon, "Device '%s' has no medium\n", device);
4147 return -1;
4148 }
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004149
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01004150 bdrv_add_key(blk_bs(blk), NULL, &err);
4151 if (err) {
4152 error_free(err);
4153 return monitor_read_bdrv_key_start(mon, blk_bs(blk), completion_cb, opaque);
4154 }
4155
4156 if (completion_cb) {
4157 completion_cb(opaque, 0);
4158 }
4159 return 0;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004160}
Paolo Bonzini4d454572012-11-26 16:03:42 +01004161
4162QemuOptsList qemu_mon_opts = {
4163 .name = "mon",
4164 .implied_opt_name = "chardev",
4165 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
4166 .desc = {
4167 {
4168 .name = "mode",
4169 .type = QEMU_OPT_STRING,
4170 },{
4171 .name = "chardev",
4172 .type = QEMU_OPT_STRING,
4173 },{
4174 .name = "default",
4175 .type = QEMU_OPT_BOOL,
4176 },{
4177 .name = "pretty",
4178 .type = QEMU_OPT_BOOL,
4179 },
4180 { /* end of list */ }
4181 },
4182};
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004183
4184#ifndef TARGET_I386
4185void qmp_rtc_reset_reinjection(Error **errp)
4186{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004187 error_setg(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection");
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004188}
4189#endif
Jason J. Herne7ee0c3e2015-06-26 14:03:16 -04004190
4191#ifndef TARGET_S390X
4192void qmp_dump_skeys(const char *filename, Error **errp)
4193{
4194 error_setg(errp, QERR_FEATURE_DISABLED, "dump-skeys");
4195}
4196#endif