blob: e4cf34e3cb18d199ed01e7ce15365fd43992f99e [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 {
Markus Armbruster7de0be62015-10-15 17:08:35 +0200184 QAPIEvent event; /* Throttling state for this event type and... */
185 QDict *data; /* ... data, see qapi_event_throttle_equal() */
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
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200190typedef struct {
191 int64_t rate; /* Minimum time (in ns) between two events */
192} MonitorQAPIEventConf;
193
aliguori87127162009-03-05 23:01:29 +0000194struct Monitor {
195 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200196 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000197 int flags;
198 int suspend_cnt;
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400199 bool skip_flush;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200200
201 QemuMutex out_lock;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400202 QString *outbuf;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200203 guint out_watch;
204
205 /* Read under either BQL or out_lock, written with BQL+out_lock. */
206 int mux_out;
207
aliguori731b0362009-03-05 23:01:42 +0000208 ReadLineState *rs;
Markus Armbruster74358f22015-03-06 19:35:59 +0100209 MonitorQMP qmp;
Andreas Färbercb446ec2013-05-01 14:24:52 +0200210 CPUState *mon_cpu;
Markus Armbruster097310b2014-10-07 13:59:15 +0200211 BlockCompletionFunc *password_completion_cb;
aliguori731b0362009-03-05 23:01:42 +0000212 void *password_opaque;
Wenchao Xia77172392013-08-27 20:38:20 +0800213 mon_cmd_t *cmd_table;
Anthony Liguoric227f092009-10-01 16:12:16 -0500214 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000215 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000216};
217
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300218/* QMP checker flags */
219#define QMP_ACCEPT_UNKNOWNS 1
220
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200221/* Protects mon_list, monitor_event_state. */
222static QemuMutex monitor_lock;
223
Blue Swirl72cf2d42009-09-12 07:36:22 +0000224static QLIST_HEAD(mon_list, Monitor) mon_list;
Corey Bryantba1c0482012-08-14 16:43:43 -0400225static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
Corey Bryantefb87c12012-08-14 16:43:48 -0400226static int mon_refcount;
bellard7e2515e2004-08-01 21:52:19 +0000227
Wayne Xia816f8922011-10-12 11:32:41 +0800228static mon_cmd_t mon_cmds[];
229static mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000230
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300231static const mon_cmd_t qmp_cmds[];
232
Markus Armbruster8631b602010-02-18 11:41:55 +0100233Monitor *cur_mon;
aliguori376253e2009-03-05 23:01:23 +0000234
Stefan Hajnoczic60bf332013-11-14 11:54:14 +0100235static void monitor_command_cb(void *opaque, const char *cmdline,
236 void *readline_opaque);
aliguori83ab7952008-08-19 14:44:22 +0000237
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100238/**
239 * Is @mon a QMP monitor?
240 */
241static inline bool monitor_is_qmp(const Monitor *mon)
Luiz Capitulino418173c2009-11-26 22:58:51 -0200242{
243 return (mon->flags & MONITOR_USE_CONTROL);
244}
245
Markus Armbruster489653b2015-03-06 20:01:05 +0100246/**
247 * Is the current monitor, if any, a QMP monitor?
248 */
249bool monitor_cur_is_qmp(void)
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100250{
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100251 return cur_mon && monitor_is_qmp(cur_mon);
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100252}
253
Anthony Liguori7060b472011-09-02 12:34:50 -0500254void monitor_read_command(Monitor *mon, int show_prompt)
aliguori731b0362009-03-05 23:01:42 +0000255{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200256 if (!mon->rs)
257 return;
258
aliguori731b0362009-03-05 23:01:42 +0000259 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
260 if (show_prompt)
261 readline_show_prompt(mon->rs);
262}
bellard6a00d602005-11-21 23:25:50 +0000263
Anthony Liguori7060b472011-09-02 12:34:50 -0500264int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
265 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000266{
Markus Armbrusterbcf5d192015-03-12 17:26:46 +0100267 if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000268 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
269 /* prompt is printed on return from the command handler */
270 return 0;
271 } else {
272 monitor_printf(mon, "terminal does not support password prompting\n");
273 return -ENOTTY;
274 }
aliguoribb5fc202009-03-05 23:01:15 +0000275}
276
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200277static void monitor_flush_locked(Monitor *mon);
278
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100279static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
280 void *opaque)
281{
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200282 Monitor *mon = opaque;
283
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200284 qemu_mutex_lock(&mon->out_lock);
285 mon->out_watch = 0;
286 monitor_flush_locked(mon);
287 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100288 return FALSE;
289}
290
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200291/* Called with mon->out_lock held. */
292static void monitor_flush_locked(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000293{
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100294 int rc;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400295 size_t len;
296 const char *buf;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100297
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400298 if (mon->skip_flush) {
299 return;
300 }
301
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400302 buf = qstring_get_str(mon->outbuf);
303 len = qstring_get_length(mon->outbuf);
304
Paolo Bonzinia4cc73d2013-05-31 14:00:27 +0200305 if (len && !mon->mux_out) {
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400306 rc = qemu_chr_fe_write(mon->chr, (const uint8_t *) buf, len);
Stratos Psomadakis056f49f2014-01-27 12:30:15 +0200307 if ((rc < 0 && errno != EAGAIN) || (rc == len)) {
308 /* all flushed or error */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400309 QDECREF(mon->outbuf);
310 mon->outbuf = qstring_new();
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100311 return;
312 }
313 if (rc > 0) {
314 /* partinal write */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400315 QString *tmp = qstring_from_str(buf + rc);
316 QDECREF(mon->outbuf);
317 mon->outbuf = tmp;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100318 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200319 if (mon->out_watch == 0) {
Roger Pau Monnee02bc6d2014-05-23 17:57:49 +0200320 mon->out_watch = qemu_chr_fe_add_watch(mon->chr, G_IO_OUT|G_IO_HUP,
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200321 monitor_unblocked, mon);
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200322 }
bellard7e2515e2004-08-01 21:52:19 +0000323 }
324}
325
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200326void monitor_flush(Monitor *mon)
327{
328 qemu_mutex_lock(&mon->out_lock);
329 monitor_flush_locked(mon);
330 qemu_mutex_unlock(&mon->out_lock);
331}
332
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400333/* flush at every end of line */
aliguori376253e2009-03-05 23:01:23 +0000334static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000335{
ths60fe76f2007-12-16 03:02:09 +0000336 char c;
aliguori731b0362009-03-05 23:01:42 +0000337
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200338 qemu_mutex_lock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000339 for(;;) {
340 c = *str++;
341 if (c == '\0')
342 break;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400343 if (c == '\n') {
344 qstring_append_chr(mon->outbuf, '\r');
345 }
346 qstring_append_chr(mon->outbuf, c);
347 if (c == '\n') {
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200348 monitor_flush_locked(mon);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400349 }
bellard7e2515e2004-08-01 21:52:19 +0000350 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200351 qemu_mutex_unlock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000352}
353
aliguori376253e2009-03-05 23:01:23 +0000354void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000355{
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400356 char *buf;
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200357
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200358 if (!mon)
359 return;
360
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100361 if (monitor_is_qmp(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200362 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200363 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200364
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400365 buf = g_strdup_vprintf(fmt, ap);
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200366 monitor_puts(mon, buf);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400367 g_free(buf);
bellard7e2515e2004-08-01 21:52:19 +0000368}
369
aliguori376253e2009-03-05 23:01:23 +0000370void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000371{
372 va_list ap;
373 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000374 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000375 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000376}
377
Pavel Butsykincaf15312015-09-22 16:18:17 +0300378int monitor_fprintf(FILE *stream, const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000379{
380 va_list ap;
381 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000382 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000383 va_end(ap);
384 return 0;
385}
386
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200387static void monitor_json_emitter(Monitor *mon, const QObject *data)
388{
389 QString *json;
390
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200391 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
392 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200393 assert(json != NULL);
394
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200395 qstring_append_chr(json, '\n');
396 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200397
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200398 QDECREF(json);
399}
400
Markus Armbruster710aec92015-03-06 11:28:00 +0100401static QDict *build_qmp_error_dict(Error *err)
Luiz Capitulinode253f12012-07-27 16:18:16 -0300402{
403 QObject *obj;
404
Markus Armbruster710aec92015-03-06 11:28:00 +0100405 obj = qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %s } }",
406 ErrorClass_lookup[error_get_class(err)],
407 error_get_pretty(err));
Luiz Capitulinode253f12012-07-27 16:18:16 -0300408
409 return qobject_to_qdict(obj);
410}
411
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100412static void monitor_protocol_emitter(Monitor *mon, QObject *data,
Markus Armbruster710aec92015-03-06 11:28:00 +0100413 Error *err)
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200414{
415 QDict *qmp;
416
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +0100417 trace_monitor_protocol_emitter(mon);
418
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100419 if (!err) {
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200420 /* success response */
Luiz Capitulinode253f12012-07-27 16:18:16 -0300421 qmp = qdict_new();
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200422 if (data) {
423 qobject_incref(data);
424 qdict_put_obj(qmp, "return", data);
425 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200426 /* return an empty QDict by default */
427 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200428 }
429 } else {
430 /* error response */
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100431 qmp = build_qmp_error_dict(err);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200432 }
433
Markus Armbruster74358f22015-03-06 19:35:59 +0100434 if (mon->qmp.id) {
435 qdict_put_obj(qmp, "id", mon->qmp.id);
436 mon->qmp.id = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200437 }
438
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200439 monitor_json_emitter(mon, QOBJECT(qmp));
440 QDECREF(qmp);
441}
442
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200443
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200444static MonitorQAPIEventConf monitor_qapi_event_conf[QAPI_EVENT_MAX] = {
445 /* Limit guest-triggerable events to 1 per second */
446 [QAPI_EVENT_RTC_CHANGE] = { 1000 * SCALE_MS },
447 [QAPI_EVENT_WATCHDOG] = { 1000 * SCALE_MS },
448 [QAPI_EVENT_BALLOON_CHANGE] = { 1000 * SCALE_MS },
449 [QAPI_EVENT_QUORUM_REPORT_BAD] = { 1000 * SCALE_MS },
450 [QAPI_EVENT_QUORUM_FAILURE] = { 1000 * SCALE_MS },
451 [QAPI_EVENT_VSERPORT_CHANGE] = { 1000 * SCALE_MS },
452};
453
Markus Armbrustera24712a2015-10-15 17:08:34 +0200454GHashTable *monitor_qapi_event_state;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100455
456/*
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200457 * Emits the event to every monitor instance, @event is only used for trace
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200458 * Called with monitor_lock held.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100459 */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200460static void monitor_qapi_event_emit(QAPIEvent event, QDict *qdict)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100461{
462 Monitor *mon;
463
Markus Armbruster688b4b72015-10-15 17:08:30 +0200464 trace_monitor_protocol_event_emit(event, qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100465 QLIST_FOREACH(mon, &mon_list, entry) {
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100466 if (monitor_is_qmp(mon) && mon->qmp.in_command_mode) {
Markus Armbruster688b4b72015-10-15 17:08:30 +0200467 monitor_json_emitter(mon, QOBJECT(qdict));
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100468 }
469 }
470}
471
Markus Armbrustera24712a2015-10-15 17:08:34 +0200472static void monitor_qapi_event_handler(void *opaque);
473
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100474/*
475 * Queue a new event for emission to Monitor instances,
476 * applying any rate limiting if required.
477 */
478static void
Markus Armbruster688b4b72015-10-15 17:08:30 +0200479monitor_qapi_event_queue(QAPIEvent event, QDict *qdict, Error **errp)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100480{
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200481 MonitorQAPIEventConf *evconf;
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200482 MonitorQAPIEventState *evstate;
Markus Armbruster93f8f982015-10-15 17:08:31 +0200483
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200484 assert(event < QAPI_EVENT_MAX);
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200485 evconf = &monitor_qapi_event_conf[event];
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200486 trace_monitor_protocol_event_queue(event, qdict, evconf->rate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100487
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200488 qemu_mutex_lock(&monitor_lock);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200489
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200490 if (!evconf->rate) {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200491 /* Unthrottled event */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200492 monitor_qapi_event_emit(event, qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100493 } else {
Markus Armbruster7de0be62015-10-15 17:08:35 +0200494 QDict *data = qobject_to_qdict(qdict_get(qdict, "data"));
495 MonitorQAPIEventState key = { .event = event, .data = data };
Markus Armbrustera24712a2015-10-15 17:08:34 +0200496
497 evstate = g_hash_table_lookup(monitor_qapi_event_state, &key);
498 assert(!evstate || timer_pending(evstate->timer));
499
500 if (evstate) {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200501 /*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200502 * Timer is pending for (at least) evconf->rate ns after
Markus Armbruster93f8f982015-10-15 17:08:31 +0200503 * last send. Store event for sending when timer fires,
504 * replacing a prior stored event if any.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100505 */
Markus Armbruster93f8f982015-10-15 17:08:31 +0200506 QDECREF(evstate->qdict);
Markus Armbruster688b4b72015-10-15 17:08:30 +0200507 evstate->qdict = qdict;
508 QINCREF(evstate->qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100509 } else {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200510 /*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200511 * Last send was (at least) evconf->rate ns ago.
Markus Armbruster93f8f982015-10-15 17:08:31 +0200512 * Send immediately, and arm the timer to call
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200513 * monitor_qapi_event_handler() in evconf->rate ns. Any
Markus Armbruster93f8f982015-10-15 17:08:31 +0200514 * events arriving before then will be delayed until then.
515 */
516 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
517
Markus Armbruster688b4b72015-10-15 17:08:30 +0200518 monitor_qapi_event_emit(event, qdict);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200519
520 evstate = g_new(MonitorQAPIEventState, 1);
521 evstate->event = event;
Markus Armbruster7de0be62015-10-15 17:08:35 +0200522 evstate->data = data;
523 QINCREF(evstate->data);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200524 evstate->qdict = NULL;
525 evstate->timer = timer_new_ns(QEMU_CLOCK_REALTIME,
526 monitor_qapi_event_handler,
527 evstate);
528 g_hash_table_add(monitor_qapi_event_state, evstate);
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200529 timer_mod_ns(evstate->timer, now + evconf->rate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100530 }
531 }
Markus Armbruster93f8f982015-10-15 17:08:31 +0200532
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200533 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100534}
535
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100536/*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200537 * This function runs evconf->rate ns after sending a throttled
Markus Armbruster93f8f982015-10-15 17:08:31 +0200538 * event.
539 * If another event has since been stored, send it.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100540 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200541static void monitor_qapi_event_handler(void *opaque)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100542{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200543 MonitorQAPIEventState *evstate = opaque;
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200544 MonitorQAPIEventConf *evconf = &monitor_qapi_event_conf[evstate->event];
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100545
Markus Armbruster93f8f982015-10-15 17:08:31 +0200546 trace_monitor_protocol_event_handler(evstate->event, evstate->qdict);
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200547 qemu_mutex_lock(&monitor_lock);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200548
Markus Armbruster688b4b72015-10-15 17:08:30 +0200549 if (evstate->qdict) {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200550 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
551
Markus Armbruster688b4b72015-10-15 17:08:30 +0200552 monitor_qapi_event_emit(evstate->event, evstate->qdict);
553 QDECREF(evstate->qdict);
554 evstate->qdict = NULL;
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200555 timer_mod_ns(evstate->timer, now + evconf->rate);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200556 } else {
557 g_hash_table_remove(monitor_qapi_event_state, evstate);
Markus Armbruster7de0be62015-10-15 17:08:35 +0200558 QDECREF(evstate->data);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200559 timer_free(evstate->timer);
560 g_free(evstate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100561 }
Markus Armbruster93f8f982015-10-15 17:08:31 +0200562
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200563 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100564}
565
Markus Armbrustera24712a2015-10-15 17:08:34 +0200566static unsigned int qapi_event_throttle_hash(const void *key)
567{
568 const MonitorQAPIEventState *evstate = key;
Markus Armbruster7de0be62015-10-15 17:08:35 +0200569 unsigned int hash = evstate->event * 255;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200570
Markus Armbruster7de0be62015-10-15 17:08:35 +0200571 if (evstate->event == QAPI_EVENT_VSERPORT_CHANGE) {
572 hash += g_str_hash(qdict_get_str(evstate->data, "id"));
573 }
574
575 return hash;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200576}
577
578static gboolean qapi_event_throttle_equal(const void *a, const void *b)
579{
580 const MonitorQAPIEventState *eva = a;
581 const MonitorQAPIEventState *evb = b;
582
Markus Armbruster7de0be62015-10-15 17:08:35 +0200583 if (eva->event != evb->event) {
584 return FALSE;
585 }
586
587 if (eva->event == QAPI_EVENT_VSERPORT_CHANGE) {
588 return !strcmp(qdict_get_str(eva->data, "id"),
589 qdict_get_str(evb->data, "id"));
590 }
591
592 return TRUE;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200593}
594
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200595static void monitor_qapi_event_init(void)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100596{
Markus Armbrustera24712a2015-10-15 17:08:34 +0200597 monitor_qapi_event_state = g_hash_table_new(qapi_event_throttle_hash,
598 qapi_event_throttle_equal);
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200599 qmp_event_set_func_emit(monitor_qapi_event_queue);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200600}
601
Markus Armbruster485febc2015-03-13 17:25:50 +0100602static void qmp_capabilities(QDict *params, QObject **ret_data, Error **errp)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200603{
Markus Armbruster485febc2015-03-13 17:25:50 +0100604 cur_mon->qmp.in_command_mode = true;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200605}
606
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100607static void handle_hmp_command(Monitor *mon, const char *cmdline);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200608
Wenchao Xiab01fe892013-08-27 20:38:19 +0800609static void monitor_data_init(Monitor *mon)
610{
611 memset(mon, 0, sizeof(Monitor));
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200612 qemu_mutex_init(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800613 mon->outbuf = qstring_new();
Wenchao Xia77172392013-08-27 20:38:20 +0800614 /* Use *mon_cmds by default. */
615 mon->cmd_table = mon_cmds;
Wenchao Xiab01fe892013-08-27 20:38:19 +0800616}
617
618static void monitor_data_destroy(Monitor *mon)
619{
620 QDECREF(mon->outbuf);
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200621 qemu_mutex_destroy(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800622}
623
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200624char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
625 int64_t cpu_index, Error **errp)
Luiz Capitulino0268d972010-10-22 10:08:02 -0200626{
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200627 char *output = NULL;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200628 Monitor *old_mon, hmp;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200629
Wenchao Xiab01fe892013-08-27 20:38:19 +0800630 monitor_data_init(&hmp);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400631 hmp.skip_flush = true;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200632
633 old_mon = cur_mon;
634 cur_mon = &hmp;
635
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200636 if (has_cpu_index) {
637 int ret = monitor_set_cpu(cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200638 if (ret < 0) {
639 cur_mon = old_mon;
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100640 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
641 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200642 goto out;
643 }
644 }
645
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100646 handle_hmp_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200647 cur_mon = old_mon;
648
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200649 qemu_mutex_lock(&hmp.out_lock);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400650 if (qstring_get_length(hmp.outbuf) > 0) {
651 output = g_strdup(qstring_get_str(hmp.outbuf));
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200652 } else {
653 output = g_strdup("");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200654 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200655 qemu_mutex_unlock(&hmp.out_lock);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200656
657out:
Wenchao Xiab01fe892013-08-27 20:38:19 +0800658 monitor_data_destroy(&hmp);
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200659 return output;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200660}
661
bellard9dc39cb2004-03-14 21:38:27 +0000662static int compare_cmd(const char *name, const char *list)
663{
664 const char *p, *pstart;
665 int len;
666 len = strlen(name);
667 p = list;
668 for(;;) {
669 pstart = p;
670 p = strchr(p, '|');
671 if (!p)
672 p = pstart + strlen(pstart);
673 if ((p - pstart) == len && !memcmp(pstart, name, len))
674 return 1;
675 if (*p == '\0')
676 break;
677 p++;
678 }
679 return 0;
680}
681
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800682static int get_str(char *buf, int buf_size, const char **pp)
683{
684 const char *p;
685 char *q;
686 int c;
687
688 q = buf;
689 p = *pp;
690 while (qemu_isspace(*p)) {
691 p++;
692 }
693 if (*p == '\0') {
694 fail:
695 *q = '\0';
696 *pp = p;
697 return -1;
698 }
699 if (*p == '\"') {
700 p++;
701 while (*p != '\0' && *p != '\"') {
702 if (*p == '\\') {
703 p++;
704 c = *p++;
705 switch (c) {
706 case 'n':
707 c = '\n';
708 break;
709 case 'r':
710 c = '\r';
711 break;
712 case '\\':
713 case '\'':
714 case '\"':
715 break;
716 default:
Peter Maydell71baf782015-08-19 16:20:19 +0100717 printf("unsupported escape code: '\\%c'\n", c);
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800718 goto fail;
719 }
720 if ((q - buf) < buf_size - 1) {
721 *q++ = c;
722 }
723 } else {
724 if ((q - buf) < buf_size - 1) {
725 *q++ = *p;
726 }
727 p++;
728 }
729 }
730 if (*p != '\"') {
Peter Maydell71baf782015-08-19 16:20:19 +0100731 printf("unterminated string\n");
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800732 goto fail;
733 }
734 p++;
735 } else {
736 while (*p != '\0' && !qemu_isspace(*p)) {
737 if ((q - buf) < buf_size - 1) {
738 *q++ = *p;
739 }
740 p++;
741 }
742 }
743 *q = '\0';
744 *pp = p;
745 return 0;
746}
747
748#define MAX_ARGS 16
749
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800750static void free_cmdline_args(char **args, int nb_args)
751{
752 int i;
753
754 assert(nb_args <= MAX_ARGS);
755
756 for (i = 0; i < nb_args; i++) {
757 g_free(args[i]);
758 }
759
760}
761
762/*
763 * Parse the command line to get valid args.
764 * @cmdline: command line to be parsed.
765 * @pnb_args: location to store the number of args, must NOT be NULL.
766 * @args: location to store the args, which should be freed by caller, must
767 * NOT be NULL.
768 *
769 * Returns 0 on success, negative on failure.
770 *
771 * NOTE: this parser is an approximate form of the real command parser. Number
772 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
773 * return with failure.
774 */
775static int parse_cmdline(const char *cmdline,
776 int *pnb_args, char **args)
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800777{
778 const char *p;
779 int nb_args, ret;
780 char buf[1024];
781
782 p = cmdline;
783 nb_args = 0;
784 for (;;) {
785 while (qemu_isspace(*p)) {
786 p++;
787 }
788 if (*p == '\0') {
789 break;
790 }
791 if (nb_args >= MAX_ARGS) {
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800792 goto fail;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800793 }
794 ret = get_str(buf, sizeof(buf), &p);
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800795 if (ret < 0) {
796 goto fail;
797 }
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800798 args[nb_args] = g_strdup(buf);
799 nb_args++;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800800 }
801 *pnb_args = nb_args;
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800802 return 0;
803
804 fail:
805 free_cmdline_args(args, nb_args);
806 return -1;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800807}
808
Wenchao Xia66855492013-08-27 20:38:23 +0800809static void help_cmd_dump_one(Monitor *mon,
810 const mon_cmd_t *cmd,
811 char **prefix_args,
812 int prefix_args_nb)
813{
814 int i;
815
816 for (i = 0; i < prefix_args_nb; i++) {
817 monitor_printf(mon, "%s ", prefix_args[i]);
818 }
819 monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
820}
821
822/* @args[@arg_index] is the valid command need to find in @cmds */
Anthony Liguoric227f092009-10-01 16:12:16 -0500823static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
Wenchao Xia66855492013-08-27 20:38:23 +0800824 char **args, int nb_args, int arg_index)
bellard9dc39cb2004-03-14 21:38:27 +0000825{
Anthony Liguoric227f092009-10-01 16:12:16 -0500826 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000827
Wenchao Xia66855492013-08-27 20:38:23 +0800828 /* No valid arg need to compare with, dump all in *cmds */
829 if (arg_index >= nb_args) {
830 for (cmd = cmds; cmd->name != NULL; cmd++) {
831 help_cmd_dump_one(mon, cmd, args, arg_index);
832 }
833 return;
834 }
835
836 /* Find one entry to dump */
837 for (cmd = cmds; cmd->name != NULL; cmd++) {
838 if (compare_cmd(args[arg_index], cmd->name)) {
839 if (cmd->sub_table) {
840 /* continue with next arg */
841 help_cmd_dump(mon, cmd->sub_table,
842 args, nb_args, arg_index + 1);
843 } else {
844 help_cmd_dump_one(mon, cmd, args, arg_index);
845 }
846 break;
847 }
bellard9dc39cb2004-03-14 21:38:27 +0000848 }
849}
850
aliguori376253e2009-03-05 23:01:23 +0000851static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000852{
Wenchao Xia66855492013-08-27 20:38:23 +0800853 char *args[MAX_ARGS];
854 int nb_args = 0;
855
856 /* 1. parse user input */
857 if (name) {
858 /* special case for log, directly dump and return */
859 if (!strcmp(name, "log")) {
Peter Maydell38dad9e2013-02-11 16:41:25 +0000860 const QEMULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000861 monitor_printf(mon, "Log items (comma separated):\n");
862 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
Peter Maydell38dad9e2013-02-11 16:41:25 +0000863 for (item = qemu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000864 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000865 }
Wenchao Xia66855492013-08-27 20:38:23 +0800866 return;
867 }
868
869 if (parse_cmdline(name, &nb_args, args) < 0) {
870 return;
bellardf193c792004-03-21 17:06:25 +0000871 }
bellard9dc39cb2004-03-14 21:38:27 +0000872 }
Wenchao Xia66855492013-08-27 20:38:23 +0800873
874 /* 2. dump the contents according to parsed args */
875 help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0);
876
877 free_cmdline_args(args, nb_args);
bellard9dc39cb2004-03-14 21:38:27 +0000878}
879
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300880static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300881{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300882 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300883}
884
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100885static void hmp_trace_event(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530886{
887 const char *tp_name = qdict_get_str(qdict, "name");
888 bool new_state = qdict_get_bool(qdict, "option");
Lluís Vilanova14101d02014-08-25 13:20:03 +0200889 Error *local_err = NULL;
Blue Swirlf871d682010-10-13 19:14:29 +0000890
Lluís Vilanova14101d02014-08-25 13:20:03 +0200891 qmp_trace_event_set_state(tp_name, new_state, true, true, &local_err);
892 if (local_err) {
Markus Armbruster091e38b2015-02-10 15:15:43 +0100893 error_report_err(local_err);
Blue Swirlf871d682010-10-13 19:14:29 +0000894 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530895}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100896
Michael Rothc45a8162011-10-02 08:44:37 -0500897#ifdef CONFIG_TRACE_SIMPLE
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100898static void hmp_trace_file(Monitor *mon, const QDict *qdict)
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100899{
900 const char *op = qdict_get_try_str(qdict, "op");
901 const char *arg = qdict_get_try_str(qdict, "arg");
902
903 if (!op) {
904 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
905 } else if (!strcmp(op, "on")) {
906 st_set_trace_file_enabled(true);
907 } else if (!strcmp(op, "off")) {
908 st_set_trace_file_enabled(false);
909 } else if (!strcmp(op, "flush")) {
910 st_flush_trace_buffer();
911 } else if (!strcmp(op, "set")) {
912 if (arg) {
913 st_set_trace_file(arg);
914 }
915 } else {
916 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
917 help_cmd(mon, "trace-file");
918 }
919}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530920#endif
921
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100922static void hmp_info_help(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000923{
Luiz Capitulino13c74252009-10-07 13:41:55 -0300924 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000925}
926
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300927CommandInfoList *qmp_query_commands(Error **errp)
bellard9bc9d1c2004-10-10 15:15:51 +0000928{
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300929 CommandInfoList *info, *cmd_list = NULL;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200930 const mon_cmd_t *cmd;
931
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300932 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino40e5a012011-10-21 16:15:31 -0200933 info = g_malloc0(sizeof(*info));
934 info->value = g_malloc0(sizeof(*info->value));
935 info->value->name = g_strdup(cmd->name);
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200936
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300937 info->next = cmd_list;
938 cmd_list = info;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200939 }
940
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300941 return cmd_list;
thsa36e69d2007-12-02 05:18:19 +0000942}
943
Daniel P. Berrange48608532012-05-21 17:59:51 +0100944EventInfoList *qmp_query_events(Error **errp)
945{
946 EventInfoList *info, *ev_list = NULL;
Wenchao Xia75175172014-06-18 08:43:54 +0200947 QAPIEvent e;
Daniel P. Berrange48608532012-05-21 17:59:51 +0100948
Wenchao Xia75175172014-06-18 08:43:54 +0200949 for (e = 0 ; e < QAPI_EVENT_MAX ; e++) {
950 const char *event_name = QAPIEvent_lookup[e];
Daniel P. Berrange48608532012-05-21 17:59:51 +0100951 assert(event_name != NULL);
952 info = g_malloc0(sizeof(*info));
953 info->value = g_malloc0(sizeof(*info->value));
954 info->value->name = g_strdup(event_name);
955
956 info->next = ev_list;
957 ev_list = info;
958 }
959
960 return ev_list;
961}
962
Markus Armbruster39a18152015-09-16 13:06:28 +0200963/*
964 * Minor hack: generated marshalling suppressed for this command
965 * ('gen': false in the schema) so we can parse the JSON string
966 * directly into QObject instead of first parsing it with
967 * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
968 * to QObject with generated output marshallers, every time. Instead,
969 * we do it in test-qmp-input-visitor.c, just to make sure
970 * qapi-introspect.py's output actually conforms to the schema.
971 */
972static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
973 Error **errp)
974{
975 *ret_data = qobject_from_json(qmp_schema_json);
976}
977
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300978/* set the current CPU defined by the user */
979int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000980{
Andreas Färber55e5c282012-12-17 06:18:02 +0100981 CPUState *cpu;
bellard6a00d602005-11-21 23:25:50 +0000982
Andreas Färber1c8bb3c2013-02-15 17:01:09 +0100983 cpu = qemu_get_cpu(cpu_index);
984 if (cpu == NULL) {
985 return -1;
bellard6a00d602005-11-21 23:25:50 +0000986 }
Andreas Färbercb446ec2013-05-01 14:24:52 +0200987 cur_mon->mon_cpu = cpu;
Andreas Färber1c8bb3c2013-02-15 17:01:09 +0100988 return 0;
bellard6a00d602005-11-21 23:25:50 +0000989}
990
Pavel Butsykincaf15312015-09-22 16:18:17 +0300991CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000992{
aliguori731b0362009-03-05 23:01:42 +0000993 if (!cur_mon->mon_cpu) {
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300994 monitor_set_cpu(0);
bellard6a00d602005-11-21 23:25:50 +0000995 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300996 cpu_synchronize_state(cur_mon->mon_cpu);
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700997 return cur_mon->mon_cpu;
998}
999
Pavel Butsykinbf957282015-09-10 18:38:59 +03001000CPUArchState *mon_get_cpu_env(void)
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001001{
1002 return mon_get_cpu()->env_ptr;
bellard6a00d602005-11-21 23:25:50 +00001003}
1004
Luiz Capitulino99b77962011-10-24 10:53:44 -02001005int monitor_get_cpu_index(void)
1006{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001007 return mon_get_cpu()->cpu_index;
Luiz Capitulino99b77962011-10-24 10:53:44 -02001008}
1009
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001010static void hmp_info_registers(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001011{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001012 cpu_dump_state(mon_get_cpu(), (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +00001013}
1014
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001015static void hmp_info_jit(Monitor *mon, const QDict *qdict)
bellarde3db7222005-01-26 22:00:47 +00001016{
aliguori376253e2009-03-05 23:01:23 +00001017 dump_exec_info((FILE *)mon, monitor_fprintf);
Sebastian Tanase27498be2014-07-25 11:56:33 +02001018 dump_drift_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +00001019}
1020
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001021static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
Max Filippov246ae242014-11-02 11:04:18 +03001022{
1023 dump_opcount_info((FILE *)mon, monitor_fprintf);
1024}
1025
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001026static void hmp_info_history(Monitor *mon, const QDict *qdict)
bellardaa455482004-04-04 13:07:25 +00001027{
1028 int i;
bellard7e2515e2004-08-01 21:52:19 +00001029 const char *str;
ths3b46e622007-09-17 08:09:54 +00001030
aliguoricde76ee2009-03-05 23:01:51 +00001031 if (!mon->rs)
1032 return;
bellard7e2515e2004-08-01 21:52:19 +00001033 i = 0;
1034 for(;;) {
aliguori731b0362009-03-05 23:01:42 +00001035 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +00001036 if (!str)
1037 break;
aliguori376253e2009-03-05 23:01:23 +00001038 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +00001039 i++;
bellardaa455482004-04-04 13:07:25 +00001040 }
1041}
1042
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001043static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
j_mayer76a66252007-03-07 08:32:30 +00001044{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001045 cpu_dump_statistics(mon_get_cpu(), (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +00001046}
j_mayer76a66252007-03-07 08:32:30 +00001047
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001048static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301049{
Lluís Vilanova14101d02014-08-25 13:20:03 +02001050 TraceEventInfoList *events = qmp_trace_event_get_state("*", NULL);
1051 TraceEventInfoList *elem;
1052
1053 for (elem = events; elem != NULL; elem = elem->next) {
1054 monitor_printf(mon, "%s : state %u\n",
1055 elem->value->name,
1056 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
1057 }
1058 qapi_free_TraceEventInfoList(events);
Prerna Saxena22890ab2010-06-24 17:04:53 +05301059}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301060
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001061void qmp_client_migrate_info(const char *protocol, const char *hostname,
1062 bool has_port, int64_t port,
1063 bool has_tls_port, int64_t tls_port,
1064 bool has_cert_subject, const char *cert_subject,
1065 Error **errp)
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001066{
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001067 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001068 if (!qemu_using_spice(errp)) {
1069 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001070 }
1071
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001072 if (!has_port && !has_tls_port) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001073 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001074 return;
Yonit Halperin6ec5dae2012-03-18 09:42:39 +02001075 }
1076
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001077 if (qemu_spice_migrate_info(hostname,
1078 has_port ? port : -1,
1079 has_tls_port ? tls_port : -1,
1080 cert_subject)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001081 error_setg(errp, QERR_UNDEFINED_ERROR);
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001082 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001083 }
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001084 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001085 }
1086
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001087 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001088}
1089
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001090static void hmp_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001091{
Peter Maydell9a7e5422013-02-11 16:41:20 +00001092 qemu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001093}
1094
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001095static void hmp_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001096{
1097 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001098 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001099
bellard9307c4c2004-04-04 12:57:25 +00001100 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001101 mask = 0;
1102 } else {
Peter Maydell4fde1eb2013-02-11 16:41:22 +00001103 mask = qemu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001104 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001105 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001106 return;
1107 }
1108 }
Peter Maydell24537a02013-02-11 16:41:23 +00001109 qemu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00001110}
1111
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001112static void hmp_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001113{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001114 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001115 if (!option || !strcmp(option, "on")) {
1116 singlestep = 1;
1117 } else if (!strcmp(option, "off")) {
1118 singlestep = 0;
1119 } else {
1120 monitor_printf(mon, "unexpected option %s\n", option);
1121 }
1122}
1123
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001124static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001125{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001126 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001127 if (!device)
1128 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1129 if (gdbserver_start(device) < 0) {
1130 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1131 device);
1132 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001133 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001134 } else {
aliguori59030a82009-04-05 18:43:41 +00001135 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1136 device);
bellard8a7ddc32004-03-31 19:00:16 +00001137 }
1138}
1139
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001140static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001141{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001142 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001143 if (select_watchdog_action(action) == -1) {
1144 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1145 }
1146}
1147
aliguori376253e2009-03-05 23:01:23 +00001148static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001149{
aliguori376253e2009-03-05 23:01:23 +00001150 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001151 switch(c) {
1152 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001153 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001154 break;
1155 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001156 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001157 break;
1158 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001159 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001160 break;
1161 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001162 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001163 break;
1164 default:
1165 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001166 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001167 } else {
aliguori376253e2009-03-05 23:01:23 +00001168 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001169 }
1170 break;
1171 }
aliguori376253e2009-03-05 23:01:23 +00001172 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001173}
1174
aliguori376253e2009-03-05 23:01:23 +00001175static void memory_dump(Monitor *mon, int count, int format, int wsize,
Avi Kivitya8170e52012-10-23 12:30:10 +02001176 hwaddr addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001177{
Blue Swirl23842aa2010-01-12 20:27:43 +00001178 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001179 uint8_t buf[16];
1180 uint64_t v;
1181
1182 if (format == 'i') {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001183 int flags = 0;
bellard9307c4c2004-04-04 12:57:25 +00001184#ifdef TARGET_I386
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001185 CPUArchState *env = mon_get_cpu_env();
bellard4c27ba22004-04-25 18:05:08 +00001186 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001187 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001188 } else if (wsize == 4) {
1189 flags = 0;
1190 } else {
bellard6a15fd12006-04-12 21:07:07 +00001191 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001192 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001193 if (env) {
1194#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001195 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001196 (env->segs[R_CS].flags & DESC_L_MASK))
1197 flags = 2;
1198 else
1199#endif
1200 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1201 flags = 1;
1202 }
bellard4c27ba22004-04-25 18:05:08 +00001203 }
1204#endif
Tom Musta1c38f842014-04-09 14:53:24 -05001205#ifdef TARGET_PPC
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001206 CPUArchState *env = mon_get_cpu_env();
Tom Musta1c38f842014-04-09 14:53:24 -05001207 flags = msr_le << 16;
1208 flags |= env->bfd_mach;
1209#endif
Peter Crosthwaited49190c2015-05-24 14:20:41 -07001210 monitor_disas(mon, mon_get_cpu(), addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001211 return;
1212 }
1213
1214 len = wsize * count;
1215 if (wsize == 1)
1216 line_size = 8;
1217 else
1218 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001219 max_digits = 0;
1220
1221 switch(format) {
1222 case 'o':
1223 max_digits = (wsize * 8 + 2) / 3;
1224 break;
1225 default:
1226 case 'x':
1227 max_digits = (wsize * 8) / 4;
1228 break;
1229 case 'u':
1230 case 'd':
1231 max_digits = (wsize * 8 * 10 + 32) / 33;
1232 break;
1233 case 'c':
1234 wsize = 1;
1235 break;
1236 }
1237
1238 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001239 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001240 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001241 else
aliguori376253e2009-03-05 23:01:23 +00001242 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001243 l = len;
1244 if (l > line_size)
1245 l = line_size;
1246 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001247 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001248 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001249 if (cpu_memory_rw_debug(mon_get_cpu(), addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001250 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001251 break;
1252 }
bellard9307c4c2004-04-04 12:57:25 +00001253 }
ths5fafdf22007-09-16 21:08:06 +00001254 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001255 while (i < l) {
1256 switch(wsize) {
1257 default:
1258 case 1:
Peter Maydell24e60302015-01-20 15:19:32 +00001259 v = ldub_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001260 break;
1261 case 2:
Peter Maydell24e60302015-01-20 15:19:32 +00001262 v = lduw_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001263 break;
1264 case 4:
Peter Maydell24e60302015-01-20 15:19:32 +00001265 v = (uint32_t)ldl_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001266 break;
1267 case 8:
Peter Maydell24e60302015-01-20 15:19:32 +00001268 v = ldq_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001269 break;
1270 }
aliguori376253e2009-03-05 23:01:23 +00001271 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001272 switch(format) {
1273 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001274 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001275 break;
1276 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001277 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001278 break;
1279 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001280 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001281 break;
1282 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001283 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001284 break;
1285 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001286 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001287 break;
1288 }
1289 i += wsize;
1290 }
aliguori376253e2009-03-05 23:01:23 +00001291 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001292 addr += l;
1293 len -= l;
1294 }
1295}
1296
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001297static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001298{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001299 int count = qdict_get_int(qdict, "count");
1300 int format = qdict_get_int(qdict, "format");
1301 int size = qdict_get_int(qdict, "size");
1302 target_long addr = qdict_get_int(qdict, "addr");
1303
aliguori376253e2009-03-05 23:01:23 +00001304 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001305}
1306
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001307static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001308{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001309 int count = qdict_get_int(qdict, "count");
1310 int format = qdict_get_int(qdict, "format");
1311 int size = qdict_get_int(qdict, "size");
Avi Kivitya8170e52012-10-23 12:30:10 +02001312 hwaddr addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001313
aliguori376253e2009-03-05 23:01:23 +00001314 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001315}
1316
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001317static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001318{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001319 int format = qdict_get_int(qdict, "format");
Avi Kivitya8170e52012-10-23 12:30:10 +02001320 hwaddr val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001321
bellard9307c4c2004-04-04 12:57:25 +00001322 switch(format) {
1323 case 'o':
Avi Kivitya8170e52012-10-23 12:30:10 +02001324 monitor_printf(mon, "%#" HWADDR_PRIo, val);
bellard9307c4c2004-04-04 12:57:25 +00001325 break;
1326 case 'x':
Avi Kivitya8170e52012-10-23 12:30:10 +02001327 monitor_printf(mon, "%#" HWADDR_PRIx, val);
bellard9307c4c2004-04-04 12:57:25 +00001328 break;
1329 case 'u':
Avi Kivitya8170e52012-10-23 12:30:10 +02001330 monitor_printf(mon, "%" HWADDR_PRIu, val);
bellard9307c4c2004-04-04 12:57:25 +00001331 break;
1332 default:
1333 case 'd':
Avi Kivitya8170e52012-10-23 12:30:10 +02001334 monitor_printf(mon, "%" HWADDR_PRId, val);
bellard9307c4c2004-04-04 12:57:25 +00001335 break;
1336 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001337 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001338 break;
1339 }
aliguori376253e2009-03-05 23:01:23 +00001340 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001341}
1342
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001343static void hmp_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001344{
1345 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001346 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001347 uint32_t start = qdict_get_int(qdict, "start");
1348 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001349
1350 sum = 0;
1351 for(addr = start; addr < (start + size); addr++) {
Peter Maydell42874d32015-04-26 16:49:24 +01001352 uint8_t val = address_space_ldub(&address_space_memory, addr,
1353 MEMTXATTRS_UNSPECIFIED, NULL);
bellarde4cf1ad2005-06-04 20:15:57 +00001354 /* BSD sum algorithm ('sum' Unix command) */
1355 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001356 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001357 }
aliguori376253e2009-03-05 23:01:23 +00001358 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001359}
1360
bellard13224a82006-07-14 22:03:35 +00001361static int mouse_button_state;
1362
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001363static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001364{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001365 int dx, dy, dz, button;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001366 const char *dx_str = qdict_get_str(qdict, "dx_str");
1367 const char *dy_str = qdict_get_str(qdict, "dy_str");
1368 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001369
bellard13224a82006-07-14 22:03:35 +00001370 dx = strtol(dx_str, NULL, 0);
1371 dy = strtol(dy_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001372 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
1373 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
1374
1375 if (dz_str) {
bellard13224a82006-07-14 22:03:35 +00001376 dz = strtol(dz_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001377 if (dz != 0) {
1378 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
1379 qemu_input_queue_btn(NULL, button, true);
1380 qemu_input_event_sync();
1381 qemu_input_queue_btn(NULL, button, false);
1382 }
1383 }
1384 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001385}
1386
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001387static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001388{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001389 static uint32_t bmap[INPUT_BUTTON_MAX] = {
1390 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1391 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
1392 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1393 };
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001394 int button_state = qdict_get_int(qdict, "button_state");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001395
1396 if (mouse_button_state == button_state) {
1397 return;
1398 }
1399 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1400 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001401 mouse_button_state = button_state;
bellard13224a82006-07-14 22:03:35 +00001402}
1403
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001404static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001405{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001406 int size = qdict_get_int(qdict, "size");
1407 int addr = qdict_get_int(qdict, "addr");
1408 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001409 uint32_t val;
1410 int suffix;
1411
1412 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001413 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001414 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001415 addr++;
1416 }
1417 addr &= 0xffff;
1418
1419 switch(size) {
1420 default:
1421 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001422 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001423 suffix = 'b';
1424 break;
1425 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001426 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001427 suffix = 'w';
1428 break;
1429 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001430 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001431 suffix = 'l';
1432 break;
1433 }
aliguori376253e2009-03-05 23:01:23 +00001434 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1435 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001436}
bellarda3a91a32004-06-04 11:06:21 +00001437
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001438static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001439{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001440 int size = qdict_get_int(qdict, "size");
1441 int addr = qdict_get_int(qdict, "addr");
1442 int val = qdict_get_int(qdict, "val");
1443
Jan Kiszkaf1147842009-07-14 10:20:11 +02001444 addr &= IOPORTS_MASK;
1445
1446 switch (size) {
1447 default:
1448 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001449 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001450 break;
1451 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001452 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001453 break;
1454 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001455 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001456 break;
1457 }
1458}
1459
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001460static void hmp_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001461{
Gongleif1839932014-12-03 18:20:58 +00001462 Error *local_err = NULL;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001463 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001464
Gongleif1839932014-12-03 18:20:58 +00001465 qemu_boot_set(bootdevice, &local_err);
1466 if (local_err) {
1467 monitor_printf(mon, "%s\n", error_get_pretty(local_err));
1468 error_free(local_err);
aurel320ecdffb2008-05-04 20:11:34 +00001469 } else {
Gongleif1839932014-12-03 18:20:58 +00001470 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001471 }
1472}
1473
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001474static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
Blue Swirl314e2982011-09-11 20:22:05 +00001475{
1476 mtree_info((fprintf_function)monitor_printf, mon);
1477}
1478
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001479static void hmp_info_numa(Monitor *mon, const QDict *qdict)
aliguori030ea372009-04-21 22:30:47 +00001480{
aliguorib28b6232009-04-22 20:20:29 +00001481 int i;
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001482 CPUState *cpu;
zhanghailiang5b009e42014-11-04 19:49:30 +08001483 uint64_t *node_mem;
aliguori030ea372009-04-21 22:30:47 +00001484
zhanghailiang5b009e42014-11-04 19:49:30 +08001485 node_mem = g_new0(uint64_t, nb_numa_nodes);
1486 query_numa_node_mem(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001487 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1488 for (i = 0; i < nb_numa_nodes; i++) {
1489 monitor_printf(mon, "node %d cpus:", i);
Andreas Färberbdc44642013-06-24 23:50:24 +02001490 CPU_FOREACH(cpu) {
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001491 if (cpu->numa_node == i) {
Andreas Färber55e5c282012-12-17 06:18:02 +01001492 monitor_printf(mon, " %d", cpu->cpu_index);
aliguori030ea372009-04-21 22:30:47 +00001493 }
1494 }
1495 monitor_printf(mon, "\n");
1496 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
zhanghailiang5b009e42014-11-04 19:49:30 +08001497 node_mem[i] >> 20);
aliguori030ea372009-04-21 22:30:47 +00001498 }
zhanghailiang5b009e42014-11-04 19:49:30 +08001499 g_free(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001500}
1501
bellard5f1ce942006-02-08 22:40:15 +00001502#ifdef CONFIG_PROFILER
1503
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001504int64_t tcg_time;
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001505int64_t dev_time;
1506
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001507static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001508{
aliguori376253e2009-03-05 23:01:23 +00001509 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001510 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00001511 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001512 tcg_time, tcg_time / (double)get_ticks_per_sec());
1513 tcg_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001514 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001515}
1516#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001517static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001518{
aliguori376253e2009-03-05 23:01:23 +00001519 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001520}
1521#endif
1522
bellardec36b692006-07-16 18:57:03 +00001523/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001524static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001525
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001526static void hmp_info_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001527{
1528 int i;
1529 CaptureState *s;
1530
1531 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001532 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001533 s->ops.info (s->opaque);
1534 }
1535}
1536
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001537static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001538{
1539 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001540 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001541 CaptureState *s;
1542
1543 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1544 if (i == n) {
1545 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001546 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001547 g_free (s);
bellardec36b692006-07-16 18:57:03 +00001548 return;
1549 }
1550 }
1551}
1552
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001553static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001554{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001555 const char *path = qdict_get_str(qdict, "path");
1556 int has_freq = qdict_haskey(qdict, "freq");
1557 int freq = qdict_get_try_int(qdict, "freq", -1);
1558 int has_bits = qdict_haskey(qdict, "bits");
1559 int bits = qdict_get_try_int(qdict, "bits", -1);
1560 int has_channels = qdict_haskey(qdict, "nchannels");
1561 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001562 CaptureState *s;
1563
Anthony Liguori7267c092011-08-20 22:09:37 -05001564 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001565
1566 freq = has_freq ? freq : 44100;
1567 bits = has_bits ? bits : 16;
1568 nchannels = has_channels ? nchannels : 2;
1569
1570 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001571 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05001572 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001573 return;
bellardec36b692006-07-16 18:57:03 +00001574 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001575 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001576}
bellardec36b692006-07-16 18:57:03 +00001577
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001578static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001579{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001580 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001581
aliguori76655d62009-03-06 20:27:37 +00001582 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001583 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001584 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001585 return acl;
1586}
aliguori76655d62009-03-06 20:27:37 +00001587
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001588static void hmp_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001589{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001590 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001591 qemu_acl *acl = find_acl(mon, aclname);
1592 qemu_acl_entry *entry;
1593 int i = 0;
1594
1595 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001596 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001597 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00001598 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00001599 i++;
1600 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001601 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001602 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001603 }
1604}
1605
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001606static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001607{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001608 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001609 qemu_acl *acl = find_acl(mon, aclname);
1610
1611 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001612 qemu_acl_reset(acl);
1613 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001614 }
1615}
aliguori76655d62009-03-06 20:27:37 +00001616
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001617static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001618{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001619 const char *aclname = qdict_get_str(qdict, "aclname");
1620 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001621 qemu_acl *acl = find_acl(mon, aclname);
1622
1623 if (acl) {
1624 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001625 acl->defaultDeny = 0;
1626 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001627 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001628 acl->defaultDeny = 1;
1629 monitor_printf(mon, "acl: policy set to 'deny'\n");
1630 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001631 monitor_printf(mon, "acl: unknown policy '%s', "
1632 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001633 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001634 }
1635}
aliguori76655d62009-03-06 20:27:37 +00001636
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001637static void hmp_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001638{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001639 const char *aclname = qdict_get_str(qdict, "aclname");
1640 const char *match = qdict_get_str(qdict, "match");
1641 const char *policy = qdict_get_str(qdict, "policy");
1642 int has_index = qdict_haskey(qdict, "index");
1643 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001644 qemu_acl *acl = find_acl(mon, aclname);
1645 int deny, ret;
1646
1647 if (acl) {
1648 if (strcmp(policy, "allow") == 0) {
1649 deny = 0;
1650 } else if (strcmp(policy, "deny") == 0) {
1651 deny = 1;
1652 } else {
1653 monitor_printf(mon, "acl: unknown policy '%s', "
1654 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001655 return;
1656 }
aliguori28a76be2009-03-06 20:27:40 +00001657 if (has_index)
1658 ret = qemu_acl_insert(acl, deny, match, index);
1659 else
1660 ret = qemu_acl_append(acl, deny, match);
1661 if (ret < 0)
1662 monitor_printf(mon, "acl: unable to add acl entry\n");
1663 else
1664 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001665 }
1666}
aliguori76655d62009-03-06 20:27:37 +00001667
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001668static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001669{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001670 const char *aclname = qdict_get_str(qdict, "aclname");
1671 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001672 qemu_acl *acl = find_acl(mon, aclname);
1673 int ret;
aliguori76655d62009-03-06 20:27:37 +00001674
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001675 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001676 ret = qemu_acl_remove(acl, match);
1677 if (ret < 0)
1678 monitor_printf(mon, "acl: no matching acl entry\n");
1679 else
1680 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001681 }
1682}
1683
Corey Bryant208c9d12012-06-22 14:36:09 -04001684void qmp_getfd(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 int fd;
1688
Corey Bryant208c9d12012-06-22 14:36:09 -04001689 fd = qemu_chr_fe_get_msgfd(cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001690 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001691 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryant208c9d12012-06-22 14:36:09 -04001692 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001693 }
1694
1695 if (qemu_isdigit(fdname[0])) {
Stefan Hajnoczi0b9f0e22014-04-24 13:58:18 +02001696 close(fd);
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001697 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
1698 "a name not starting with a digit");
Corey Bryant208c9d12012-06-22 14:36:09 -04001699 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001700 }
1701
Corey Bryant208c9d12012-06-22 14:36:09 -04001702 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001703 if (strcmp(monfd->name, fdname) != 0) {
1704 continue;
1705 }
1706
1707 close(monfd->fd);
1708 monfd->fd = fd;
Corey Bryant208c9d12012-06-22 14:36:09 -04001709 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001710 }
1711
Anthony Liguori7267c092011-08-20 22:09:37 -05001712 monfd = g_malloc0(sizeof(mon_fd_t));
1713 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001714 monfd->fd = fd;
1715
Corey Bryant208c9d12012-06-22 14:36:09 -04001716 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001717}
1718
Corey Bryant208c9d12012-06-22 14:36:09 -04001719void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001720{
Anthony Liguoric227f092009-10-01 16:12:16 -05001721 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001722
Corey Bryant208c9d12012-06-22 14:36:09 -04001723 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001724 if (strcmp(monfd->name, fdname) != 0) {
1725 continue;
1726 }
1727
Blue Swirl72cf2d42009-09-12 07:36:22 +00001728 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001729 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001730 g_free(monfd->name);
1731 g_free(monfd);
Corey Bryant208c9d12012-06-22 14:36:09 -04001732 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001733 }
1734
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001735 error_setg(errp, QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001736}
1737
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001738static void hmp_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001739{
Luiz Capitulino13548692011-07-29 15:36:43 -03001740 int saved_vm_running = runstate_is_running();
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001741 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02001742
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001743 vm_stop(RUN_STATE_RESTORE_VM);
Juan Quintelac8d41b22009-08-20 19:42:21 +02001744
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03001745 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02001746 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03001747 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02001748}
1749
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001750int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
Mark McLoughlin7768e042009-07-22 09:11:41 +01001751{
Anthony Liguoric227f092009-10-01 16:12:16 -05001752 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01001753
Blue Swirl72cf2d42009-09-12 07:36:22 +00001754 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01001755 int fd;
1756
1757 if (strcmp(monfd->name, fdname) != 0) {
1758 continue;
1759 }
1760
1761 fd = monfd->fd;
1762
1763 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001764 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05001765 g_free(monfd->name);
1766 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001767
1768 return fd;
1769 }
1770
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001771 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001772 return -1;
1773}
1774
Corey Bryantba1c0482012-08-14 16:43:43 -04001775static void monitor_fdset_cleanup(MonFdset *mon_fdset)
1776{
1777 MonFdsetFd *mon_fdset_fd;
1778 MonFdsetFd *mon_fdset_fd_next;
1779
1780 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantebe52b52012-10-18 15:19:33 -04001781 if ((mon_fdset_fd->removed ||
1782 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
1783 runstate_is_running()) {
Corey Bryantba1c0482012-08-14 16:43:43 -04001784 close(mon_fdset_fd->fd);
1785 g_free(mon_fdset_fd->opaque);
1786 QLIST_REMOVE(mon_fdset_fd, next);
1787 g_free(mon_fdset_fd);
1788 }
1789 }
1790
Corey Bryantadb696f2012-08-14 16:43:47 -04001791 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04001792 QLIST_REMOVE(mon_fdset, next);
1793 g_free(mon_fdset);
1794 }
1795}
1796
Corey Bryantefb87c12012-08-14 16:43:48 -04001797static void monitor_fdsets_cleanup(void)
1798{
1799 MonFdset *mon_fdset;
1800 MonFdset *mon_fdset_next;
1801
1802 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
1803 monitor_fdset_cleanup(mon_fdset);
1804 }
1805}
1806
Corey Bryantba1c0482012-08-14 16:43:43 -04001807AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
1808 const char *opaque, Error **errp)
1809{
1810 int fd;
1811 Monitor *mon = cur_mon;
Corey Bryantba1c0482012-08-14 16:43:43 -04001812 AddfdInfo *fdinfo;
1813
1814 fd = qemu_chr_fe_get_msgfd(mon->chr);
1815 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001816 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryantba1c0482012-08-14 16:43:43 -04001817 goto error;
1818 }
1819
Corey Bryante446f702012-10-18 15:19:32 -04001820 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
1821 has_opaque, opaque, errp);
1822 if (fdinfo) {
1823 return fdinfo;
Corey Bryant9ac54af2012-10-18 15:19:31 -04001824 }
1825
Corey Bryantba1c0482012-08-14 16:43:43 -04001826error:
1827 if (fd != -1) {
1828 close(fd);
1829 }
1830 return NULL;
1831}
1832
1833void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
1834{
1835 MonFdset *mon_fdset;
1836 MonFdsetFd *mon_fdset_fd;
1837 char fd_str[60];
1838
1839 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1840 if (mon_fdset->id != fdset_id) {
1841 continue;
1842 }
1843 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1844 if (has_fd) {
1845 if (mon_fdset_fd->fd != fd) {
1846 continue;
1847 }
1848 mon_fdset_fd->removed = true;
1849 break;
1850 } else {
1851 mon_fdset_fd->removed = true;
1852 }
1853 }
1854 if (has_fd && !mon_fdset_fd) {
1855 goto error;
1856 }
1857 monitor_fdset_cleanup(mon_fdset);
1858 return;
1859 }
1860
1861error:
1862 if (has_fd) {
1863 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
1864 fdset_id, fd);
1865 } else {
1866 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
1867 }
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001868 error_setg(errp, QERR_FD_NOT_FOUND, fd_str);
Corey Bryantba1c0482012-08-14 16:43:43 -04001869}
1870
1871FdsetInfoList *qmp_query_fdsets(Error **errp)
1872{
1873 MonFdset *mon_fdset;
1874 MonFdsetFd *mon_fdset_fd;
1875 FdsetInfoList *fdset_list = NULL;
1876
1877 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1878 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
1879 FdsetFdInfoList *fdsetfd_list = NULL;
1880
1881 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
1882 fdset_info->value->fdset_id = mon_fdset->id;
1883
1884 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1885 FdsetFdInfoList *fdsetfd_info;
1886
1887 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
1888 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
1889 fdsetfd_info->value->fd = mon_fdset_fd->fd;
1890 if (mon_fdset_fd->opaque) {
1891 fdsetfd_info->value->has_opaque = true;
1892 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
1893 } else {
1894 fdsetfd_info->value->has_opaque = false;
1895 }
1896
1897 fdsetfd_info->next = fdsetfd_list;
1898 fdsetfd_list = fdsetfd_info;
1899 }
1900
1901 fdset_info->value->fds = fdsetfd_list;
1902
1903 fdset_info->next = fdset_list;
1904 fdset_list = fdset_info;
1905 }
1906
1907 return fdset_list;
1908}
1909
Corey Bryante446f702012-10-18 15:19:32 -04001910AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
1911 bool has_opaque, const char *opaque,
1912 Error **errp)
1913{
1914 MonFdset *mon_fdset = NULL;
1915 MonFdsetFd *mon_fdset_fd;
1916 AddfdInfo *fdinfo;
1917
1918 if (has_fdset_id) {
1919 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1920 /* Break if match found or match impossible due to ordering by ID */
1921 if (fdset_id <= mon_fdset->id) {
1922 if (fdset_id < mon_fdset->id) {
1923 mon_fdset = NULL;
1924 }
1925 break;
1926 }
1927 }
1928 }
1929
1930 if (mon_fdset == NULL) {
1931 int64_t fdset_id_prev = -1;
1932 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
1933
1934 if (has_fdset_id) {
1935 if (fdset_id < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001936 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
1937 "a non-negative value");
Corey Bryante446f702012-10-18 15:19:32 -04001938 return NULL;
1939 }
1940 /* Use specified fdset ID */
1941 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1942 mon_fdset_cur = mon_fdset;
1943 if (fdset_id < mon_fdset_cur->id) {
1944 break;
1945 }
1946 }
1947 } else {
1948 /* Use first available fdset ID */
1949 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1950 mon_fdset_cur = mon_fdset;
1951 if (fdset_id_prev == mon_fdset_cur->id - 1) {
1952 fdset_id_prev = mon_fdset_cur->id;
1953 continue;
1954 }
1955 break;
1956 }
1957 }
1958
1959 mon_fdset = g_malloc0(sizeof(*mon_fdset));
1960 if (has_fdset_id) {
1961 mon_fdset->id = fdset_id;
1962 } else {
1963 mon_fdset->id = fdset_id_prev + 1;
1964 }
1965
1966 /* The fdset list is ordered by fdset ID */
1967 if (!mon_fdset_cur) {
1968 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
1969 } else if (mon_fdset->id < mon_fdset_cur->id) {
1970 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
1971 } else {
1972 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
1973 }
1974 }
1975
1976 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
1977 mon_fdset_fd->fd = fd;
1978 mon_fdset_fd->removed = false;
1979 if (has_opaque) {
1980 mon_fdset_fd->opaque = g_strdup(opaque);
1981 }
1982 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
1983
1984 fdinfo = g_malloc0(sizeof(*fdinfo));
1985 fdinfo->fdset_id = mon_fdset->id;
1986 fdinfo->fd = mon_fdset_fd->fd;
1987
1988 return fdinfo;
1989}
1990
Corey Bryantadb696f2012-08-14 16:43:47 -04001991int monitor_fdset_get_fd(int64_t fdset_id, int flags)
1992{
Blue Swirlb2dc64c2012-08-18 20:14:54 +00001993#ifndef _WIN32
Corey Bryantadb696f2012-08-14 16:43:47 -04001994 MonFdset *mon_fdset;
1995 MonFdsetFd *mon_fdset_fd;
1996 int mon_fd_flags;
1997
Corey Bryantadb696f2012-08-14 16:43:47 -04001998 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1999 if (mon_fdset->id != fdset_id) {
2000 continue;
2001 }
2002 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2003 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
2004 if (mon_fd_flags == -1) {
2005 return -1;
2006 }
2007
2008 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
2009 return mon_fdset_fd->fd;
2010 }
2011 }
2012 errno = EACCES;
2013 return -1;
2014 }
2015#endif
2016
2017 errno = ENOENT;
2018 return -1;
2019}
2020
2021int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
2022{
2023 MonFdset *mon_fdset;
2024 MonFdsetFd *mon_fdset_fd_dup;
2025
2026 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2027 if (mon_fdset->id != fdset_id) {
2028 continue;
2029 }
2030 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2031 if (mon_fdset_fd_dup->fd == dup_fd) {
2032 return -1;
2033 }
2034 }
2035 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2036 mon_fdset_fd_dup->fd = dup_fd;
2037 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
2038 return 0;
2039 }
2040 return -1;
2041}
2042
2043static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2044{
2045 MonFdset *mon_fdset;
2046 MonFdsetFd *mon_fdset_fd_dup;
2047
2048 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2049 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2050 if (mon_fdset_fd_dup->fd == dup_fd) {
2051 if (remove) {
2052 QLIST_REMOVE(mon_fdset_fd_dup, next);
2053 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2054 monitor_fdset_cleanup(mon_fdset);
2055 }
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002056 return -1;
2057 } else {
2058 return mon_fdset->id;
Corey Bryantadb696f2012-08-14 16:43:47 -04002059 }
Corey Bryantadb696f2012-08-14 16:43:47 -04002060 }
2061 }
2062 }
2063 return -1;
2064}
2065
2066int monitor_fdset_dup_fd_find(int dup_fd)
2067{
2068 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2069}
2070
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002071void monitor_fdset_dup_fd_remove(int dup_fd)
Corey Bryantadb696f2012-08-14 16:43:47 -04002072{
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002073 monitor_fdset_dup_fd_find_remove(dup_fd, true);
Corey Bryantadb696f2012-08-14 16:43:47 -04002074}
2075
Markus Armbruster1677f4c2015-02-09 14:03:19 +01002076int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
Laszlo Ersek59063662014-04-10 10:24:31 +02002077{
2078 int fd;
2079 Error *local_err = NULL;
2080
2081 if (!qemu_isdigit(fdname[0]) && mon) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002082 fd = monitor_get_fd(mon, fdname, &local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002083 } else {
2084 fd = qemu_parse_fd(fdname);
Laszlo Ersek59063662014-04-10 10:24:31 +02002085 if (fd == -1) {
2086 error_setg(&local_err, "Invalid file descriptor number '%s'",
2087 fdname);
2088 }
2089 }
2090 if (local_err) {
2091 error_propagate(errp, local_err);
2092 assert(fd == -1);
2093 } else {
2094 assert(fd != -1);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002095 }
2096
2097 return fd;
2098}
2099
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002100/* Please update hmp-commands.hx when adding or changing commands */
Wayne Xia816f8922011-10-12 11:32:41 +08002101static mon_cmd_t info_cmds[] = {
Pavel Butsykinda76ee72015-09-10 18:38:58 +03002102#include "hmp-commands-info.h"
2103 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002104};
2105
Wenchao Xiaa13ced52013-01-14 14:06:28 +08002106/* mon_cmds and info_cmds would be sorted at runtime */
2107static mon_cmd_t mon_cmds[] = {
2108#include "hmp-commands.h"
2109 { NULL, NULL, },
2110};
2111
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002112static const mon_cmd_t qmp_cmds[] = {
Anthony Liguorie3193602011-09-02 12:34:47 -05002113#include "qmp-commands-old.h"
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002114 { /* NULL */ },
2115};
2116
bellard9307c4c2004-04-04 12:57:25 +00002117/*******************************************************************/
2118
2119static const char *pch;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002120static sigjmp_buf expr_env;
bellard9307c4c2004-04-04 12:57:25 +00002121
bellard9307c4c2004-04-04 12:57:25 +00002122
Stefan Weil9c3175c2013-08-22 21:30:09 +02002123static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
2124expr_error(Monitor *mon, const char *fmt, ...)
bellard9dc39cb2004-03-14 21:38:27 +00002125{
Fam Zheng277acfe2013-08-20 10:58:21 +08002126 va_list ap;
2127 va_start(ap, fmt);
2128 monitor_vprintf(mon, fmt, ap);
2129 monitor_printf(mon, "\n");
2130 va_end(ap);
Peter Maydell6ab7e542013-02-20 15:21:09 +00002131 siglongjmp(expr_env, 1);
bellard9307c4c2004-04-04 12:57:25 +00002132}
2133
Markus Armbruster09b94182010-01-20 13:07:30 +01002134/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00002135static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002136{
Pavel Butsykinbf957282015-09-10 18:38:59 +03002137 const MonitorDef *md = target_monitor_defs();
bellard92a31b12005-02-10 22:00:52 +00002138 void *ptr;
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002139 uint64_t tmp = 0;
2140 int ret;
bellard92a31b12005-02-10 22:00:52 +00002141
Pavel Butsykinbf957282015-09-10 18:38:59 +03002142 if (md == NULL) {
2143 return -1;
2144 }
2145
2146 for(; md->name != NULL; md++) {
bellard9307c4c2004-04-04 12:57:25 +00002147 if (compare_cmd(name, md->name)) {
2148 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002149 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002150 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002151 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00002152 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002153 switch(md->type) {
2154 case MD_I32:
2155 *pval = *(int32_t *)ptr;
2156 break;
2157 case MD_TLONG:
2158 *pval = *(target_long *)ptr;
2159 break;
2160 default:
2161 *pval = 0;
2162 break;
2163 }
bellard9307c4c2004-04-04 12:57:25 +00002164 }
2165 return 0;
2166 }
2167 }
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002168
2169 ret = target_get_monitor_def(mon_get_cpu(), name, &tmp);
2170 if (!ret) {
2171 *pval = (target_long) tmp;
2172 }
2173
2174 return ret;
bellard9307c4c2004-04-04 12:57:25 +00002175}
2176
2177static void next(void)
2178{
Blue Swirl660f11b2009-07-31 21:16:51 +00002179 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002180 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002181 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002182 pch++;
2183 }
2184}
2185
aliguori376253e2009-03-05 23:01:23 +00002186static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002187
aliguori376253e2009-03-05 23:01:23 +00002188static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002189{
blueswir1c2efc952007-09-25 17:28:42 +00002190 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002191 char *p;
bellard6a00d602005-11-21 23:25:50 +00002192 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002193
2194 switch(*pch) {
2195 case '+':
2196 next();
aliguori376253e2009-03-05 23:01:23 +00002197 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002198 break;
2199 case '-':
2200 next();
aliguori376253e2009-03-05 23:01:23 +00002201 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002202 break;
2203 case '~':
2204 next();
aliguori376253e2009-03-05 23:01:23 +00002205 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002206 break;
2207 case '(':
2208 next();
aliguori376253e2009-03-05 23:01:23 +00002209 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002210 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002211 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002212 }
2213 next();
2214 break;
bellard81d09122004-07-14 17:21:37 +00002215 case '\'':
2216 pch++;
2217 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002218 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002219 n = *pch;
2220 pch++;
2221 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002222 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002223 next();
2224 break;
bellard9307c4c2004-04-04 12:57:25 +00002225 case '$':
2226 {
2227 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002228 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002229
bellard9307c4c2004-04-04 12:57:25 +00002230 pch++;
2231 q = buf;
2232 while ((*pch >= 'a' && *pch <= 'z') ||
2233 (*pch >= 'A' && *pch <= 'Z') ||
2234 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002235 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002236 if ((q - buf) < sizeof(buf) - 1)
2237 *q++ = *pch;
2238 pch++;
2239 }
blueswir1cd390082008-11-16 13:53:32 +00002240 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002241 pch++;
2242 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002243 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01002244 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00002245 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00002246 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002247 }
2248 break;
2249 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002250 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002251 n = 0;
2252 break;
2253 default:
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002254 errno = 0;
bellard4f4fbf72006-06-25 18:28:12 +00002255 n = strtoull(pch, &p, 0);
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002256 if (errno == ERANGE) {
2257 expr_error(mon, "number too large");
2258 }
bellard9307c4c2004-04-04 12:57:25 +00002259 if (pch == p) {
Fam Zheng277acfe2013-08-20 10:58:21 +08002260 expr_error(mon, "invalid char '%c' in expression", *p);
bellard9307c4c2004-04-04 12:57:25 +00002261 }
2262 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002263 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002264 pch++;
2265 break;
2266 }
2267 return n;
2268}
2269
2270
aliguori376253e2009-03-05 23:01:23 +00002271static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002272{
blueswir1c2efc952007-09-25 17:28:42 +00002273 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002274 int op;
ths3b46e622007-09-17 08:09:54 +00002275
aliguori376253e2009-03-05 23:01:23 +00002276 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002277 for(;;) {
2278 op = *pch;
2279 if (op != '*' && op != '/' && op != '%')
2280 break;
2281 next();
aliguori376253e2009-03-05 23:01:23 +00002282 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002283 switch(op) {
2284 default:
2285 case '*':
2286 val *= val2;
2287 break;
2288 case '/':
2289 case '%':
ths5fafdf22007-09-16 21:08:06 +00002290 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002291 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002292 if (op == '/')
2293 val /= val2;
2294 else
2295 val %= val2;
2296 break;
2297 }
2298 }
2299 return val;
2300}
2301
aliguori376253e2009-03-05 23:01:23 +00002302static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002303{
blueswir1c2efc952007-09-25 17:28:42 +00002304 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002305 int op;
bellard9307c4c2004-04-04 12:57:25 +00002306
aliguori376253e2009-03-05 23:01:23 +00002307 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002308 for(;;) {
2309 op = *pch;
2310 if (op != '&' && op != '|' && op != '^')
2311 break;
2312 next();
aliguori376253e2009-03-05 23:01:23 +00002313 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002314 switch(op) {
2315 default:
2316 case '&':
2317 val &= val2;
2318 break;
2319 case '|':
2320 val |= val2;
2321 break;
2322 case '^':
2323 val ^= val2;
2324 break;
2325 }
2326 }
2327 return val;
2328}
2329
aliguori376253e2009-03-05 23:01:23 +00002330static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002331{
blueswir1c2efc952007-09-25 17:28:42 +00002332 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002333 int op;
bellard9307c4c2004-04-04 12:57:25 +00002334
aliguori376253e2009-03-05 23:01:23 +00002335 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002336 for(;;) {
2337 op = *pch;
2338 if (op != '+' && op != '-')
2339 break;
2340 next();
aliguori376253e2009-03-05 23:01:23 +00002341 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002342 if (op == '+')
2343 val += val2;
2344 else
2345 val -= val2;
2346 }
2347 return val;
2348}
2349
aliguori376253e2009-03-05 23:01:23 +00002350static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002351{
2352 pch = *pp;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002353 if (sigsetjmp(expr_env, 0)) {
bellard9307c4c2004-04-04 12:57:25 +00002354 *pp = pch;
2355 return -1;
2356 }
blueswir1cd390082008-11-16 13:53:32 +00002357 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002358 pch++;
aliguori376253e2009-03-05 23:01:23 +00002359 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002360 *pp = pch;
2361 return 0;
2362}
2363
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002364static int get_double(Monitor *mon, double *pval, const char **pp)
2365{
2366 const char *p = *pp;
2367 char *tailp;
2368 double d;
2369
2370 d = strtod(p, &tailp);
2371 if (tailp == p) {
2372 monitor_printf(mon, "Number expected\n");
2373 return -1;
2374 }
2375 if (d != d || d - d != 0) {
2376 /* NaN or infinity */
2377 monitor_printf(mon, "Bad number\n");
2378 return -1;
2379 }
2380 *pval = d;
2381 *pp = tailp;
2382 return 0;
2383}
2384
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002385/*
2386 * Store the command-name in cmdname, and return a pointer to
2387 * the remaining of the command string.
2388 */
2389static const char *get_command_name(const char *cmdline,
2390 char *cmdname, size_t nlen)
2391{
2392 size_t len;
2393 const char *p, *pstart;
2394
2395 p = cmdline;
2396 while (qemu_isspace(*p))
2397 p++;
2398 if (*p == '\0')
2399 return NULL;
2400 pstart = p;
2401 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2402 p++;
2403 len = p - pstart;
2404 if (len > nlen - 1)
2405 len = nlen - 1;
2406 memcpy(cmdname, pstart, len);
2407 cmdname[len] = '\0';
2408 return p;
2409}
2410
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002411/**
2412 * Read key of 'type' into 'key' and return the current
2413 * 'type' pointer.
2414 */
2415static char *key_get_info(const char *type, char **key)
2416{
2417 size_t len;
2418 char *p, *str;
2419
2420 if (*type == ',')
2421 type++;
2422
2423 p = strchr(type, ':');
2424 if (!p) {
2425 *key = NULL;
2426 return NULL;
2427 }
2428 len = p - type;
2429
Anthony Liguori7267c092011-08-20 22:09:37 -05002430 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002431 memcpy(str, type, len);
2432 str[len] = '\0';
2433
2434 *key = str;
2435 return ++p;
2436}
2437
bellard9307c4c2004-04-04 12:57:25 +00002438static int default_fmt_format = 'x';
2439static int default_fmt_size = 4;
2440
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002441static int is_valid_option(const char *c, const char *typestr)
2442{
2443 char option[3];
2444
2445 option[0] = '-';
2446 option[1] = *c;
2447 option[2] = '\0';
2448
2449 typestr = strstr(typestr, option);
2450 return (typestr != NULL);
2451}
2452
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03002453static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
2454 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002455{
2456 const mon_cmd_t *cmd;
2457
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03002458 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002459 if (compare_cmd(cmdname, cmd->name)) {
2460 return cmd;
2461 }
2462 }
2463
2464 return NULL;
2465}
2466
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03002467static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
2468{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002469 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03002470}
2471
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002472/*
Bandan Dasae502122015-06-03 18:38:08 -04002473 * Parse command name from @cmdp according to command table @table.
2474 * If blank, return NULL.
2475 * Else, if no valid command can be found, report to @mon, and return
2476 * NULL.
2477 * Else, change @cmdp to point right behind the name, and return its
2478 * command table entry.
2479 * Do not assume the return value points into @table! It doesn't when
2480 * the command is found in a sub-command table.
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002481 */
Anthony Liguoric227f092009-10-01 16:12:16 -05002482static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Bandan Dasae502122015-06-03 18:38:08 -04002483 const char **cmdp,
2484 mon_cmd_t *table)
bellard9307c4c2004-04-04 12:57:25 +00002485{
Bandan Dasae502122015-06-03 18:38:08 -04002486 const char *p;
Anthony Liguoric227f092009-10-01 16:12:16 -05002487 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002488 char cmdname[256];
bellard9dc39cb2004-03-14 21:38:27 +00002489
bellard9307c4c2004-04-04 12:57:25 +00002490 /* extract the command name */
Bandan Dasae502122015-06-03 18:38:08 -04002491 p = get_command_name(*cmdp, cmdname, sizeof(cmdname));
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002492 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002493 return NULL;
ths3b46e622007-09-17 08:09:54 +00002494
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002495 cmd = search_dispatch_table(table, cmdname);
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002496 if (!cmd) {
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002497 monitor_printf(mon, "unknown command: '%.*s'\n",
Bandan Dasae502122015-06-03 18:38:08 -04002498 (int)(p - *cmdp), *cmdp);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002499 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002500 }
bellard9307c4c2004-04-04 12:57:25 +00002501
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002502 /* filter out following useless space */
2503 while (qemu_isspace(*p)) {
2504 p++;
2505 }
Bandan Dasae502122015-06-03 18:38:08 -04002506
2507 *cmdp = p;
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002508 /* search sub command */
Bandan Dasae502122015-06-03 18:38:08 -04002509 if (cmd->sub_table != NULL && *p != '\0') {
2510 return monitor_parse_command(mon, cmdp, cmd->sub_table);
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002511 }
2512
Bandan Dasae502122015-06-03 18:38:08 -04002513 return cmd;
2514}
2515
2516/*
2517 * Parse arguments for @cmd.
2518 * If it can't be parsed, report to @mon, and return NULL.
2519 * Else, insert command arguments into a QDict, and return it.
2520 * Note: On success, caller has to free the QDict structure.
2521 */
2522
2523static QDict *monitor_parse_arguments(Monitor *mon,
2524 const char **endp,
2525 const mon_cmd_t *cmd)
2526{
2527 const char *typestr;
2528 char *key;
2529 int c;
2530 const char *p = *endp;
2531 char buf[1024];
2532 QDict *qdict = qdict_new();
2533
bellard9307c4c2004-04-04 12:57:25 +00002534 /* parse the parameters */
2535 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00002536 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002537 typestr = key_get_info(typestr, &key);
2538 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00002539 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002540 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00002541 typestr++;
2542 switch(c) {
2543 case 'F':
bellard81d09122004-07-14 17:21:37 +00002544 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002545 case 's':
2546 {
2547 int ret;
ths3b46e622007-09-17 08:09:54 +00002548
blueswir1cd390082008-11-16 13:53:32 +00002549 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002550 p++;
2551 if (*typestr == '?') {
2552 typestr++;
2553 if (*p == '\0') {
2554 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03002555 break;
bellard9307c4c2004-04-04 12:57:25 +00002556 }
2557 }
2558 ret = get_str(buf, sizeof(buf), &p);
2559 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002560 switch(c) {
2561 case 'F':
aliguori376253e2009-03-05 23:01:23 +00002562 monitor_printf(mon, "%s: filename expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002563 cmd->name);
bellard81d09122004-07-14 17:21:37 +00002564 break;
2565 case 'B':
aliguori376253e2009-03-05 23:01:23 +00002566 monitor_printf(mon, "%s: block device name expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002567 cmd->name);
bellard81d09122004-07-14 17:21:37 +00002568 break;
2569 default:
Bandan Dasae502122015-06-03 18:38:08 -04002570 monitor_printf(mon, "%s: string expected\n", cmd->name);
bellard81d09122004-07-14 17:21:37 +00002571 break;
2572 }
bellard9307c4c2004-04-04 12:57:25 +00002573 goto fail;
2574 }
Luiz Capitulino53773582009-08-28 15:27:25 -03002575 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00002576 }
2577 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01002578 case 'O':
2579 {
2580 QemuOptsList *opts_list;
2581 QemuOpts *opts;
2582
2583 opts_list = qemu_find_opts(key);
2584 if (!opts_list || opts_list->desc->name) {
2585 goto bad_type;
2586 }
2587 while (qemu_isspace(*p)) {
2588 p++;
2589 }
2590 if (!*p)
2591 break;
2592 if (get_str(buf, sizeof(buf), &p) < 0) {
2593 goto fail;
2594 }
Markus Armbruster70b94332015-02-13 12:50:26 +01002595 opts = qemu_opts_parse_noisily(opts_list, buf, true);
Markus Armbruster361127d2010-02-10 20:24:35 +01002596 if (!opts) {
2597 goto fail;
2598 }
2599 qemu_opts_to_qdict(opts, qdict);
2600 qemu_opts_del(opts);
2601 }
2602 break;
bellard9307c4c2004-04-04 12:57:25 +00002603 case '/':
2604 {
2605 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002606
blueswir1cd390082008-11-16 13:53:32 +00002607 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002608 p++;
2609 if (*p == '/') {
2610 /* format found */
2611 p++;
2612 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002613 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002614 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002615 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002616 count = count * 10 + (*p - '0');
2617 p++;
2618 }
2619 }
2620 size = -1;
2621 format = -1;
2622 for(;;) {
2623 switch(*p) {
2624 case 'o':
2625 case 'd':
2626 case 'u':
2627 case 'x':
2628 case 'i':
2629 case 'c':
2630 format = *p++;
2631 break;
2632 case 'b':
2633 size = 1;
2634 p++;
2635 break;
2636 case 'h':
2637 size = 2;
2638 p++;
2639 break;
2640 case 'w':
2641 size = 4;
2642 p++;
2643 break;
2644 case 'g':
2645 case 'L':
2646 size = 8;
2647 p++;
2648 break;
2649 default:
2650 goto next;
2651 }
2652 }
2653 next:
blueswir1cd390082008-11-16 13:53:32 +00002654 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00002655 monitor_printf(mon, "invalid char in format: '%c'\n",
2656 *p);
bellard9307c4c2004-04-04 12:57:25 +00002657 goto fail;
2658 }
bellard9307c4c2004-04-04 12:57:25 +00002659 if (format < 0)
2660 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002661 if (format != 'i') {
2662 /* for 'i', not specifying a size gives -1 as size */
2663 if (size < 0)
2664 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002665 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002666 }
bellard9307c4c2004-04-04 12:57:25 +00002667 default_fmt_format = format;
2668 } else {
2669 count = 1;
2670 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002671 if (format != 'i') {
2672 size = default_fmt_size;
2673 } else {
2674 size = -1;
2675 }
bellard9307c4c2004-04-04 12:57:25 +00002676 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002677 qdict_put(qdict, "count", qint_from_int(count));
2678 qdict_put(qdict, "format", qint_from_int(format));
2679 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00002680 }
2681 break;
2682 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002683 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002684 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00002685 {
blueswir1c2efc952007-09-25 17:28:42 +00002686 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00002687
blueswir1cd390082008-11-16 13:53:32 +00002688 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002689 p++;
bellard34405572004-06-08 00:55:58 +00002690 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002691 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03002692 if (*p == '\0') {
2693 typestr++;
2694 break;
2695 }
bellard34405572004-06-08 00:55:58 +00002696 } else {
2697 if (*p == '.') {
2698 p++;
blueswir1cd390082008-11-16 13:53:32 +00002699 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00002700 p++;
bellard34405572004-06-08 00:55:58 +00002701 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03002702 typestr++;
2703 break;
bellard34405572004-06-08 00:55:58 +00002704 }
2705 }
bellard13224a82006-07-14 22:03:35 +00002706 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002707 }
aliguori376253e2009-03-05 23:01:23 +00002708 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00002709 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002710 /* Check if 'i' is greater than 32-bit */
2711 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
Bandan Dasae502122015-06-03 18:38:08 -04002712 monitor_printf(mon, "\'%s\' has failed: ", cmd->name);
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002713 monitor_printf(mon, "integer is for 32-bit values\n");
2714 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002715 } else if (c == 'M') {
Luiz Capitulino91162842012-04-26 17:34:30 -03002716 if (val < 0) {
2717 monitor_printf(mon, "enter a positive value\n");
2718 goto fail;
2719 }
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002720 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002721 }
Luiz Capitulino53773582009-08-28 15:27:25 -03002722 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00002723 }
2724 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02002725 case 'o':
2726 {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01002727 int64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02002728 char *end;
2729
2730 while (qemu_isspace(*p)) {
2731 p++;
2732 }
2733 if (*typestr == '?') {
2734 typestr++;
2735 if (*p == '\0') {
2736 break;
2737 }
2738 }
Marc-André Lureau4677bb42015-09-16 18:02:56 +02002739 val = qemu_strtosz(p, &end);
Jes Sorensendbc0c672010-10-21 17:15:47 +02002740 if (val < 0) {
2741 monitor_printf(mon, "invalid size\n");
2742 goto fail;
2743 }
2744 qdict_put(qdict, key, qint_from_int(val));
2745 p = end;
2746 }
2747 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01002748 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002749 {
2750 double val;
2751
2752 while (qemu_isspace(*p))
2753 p++;
2754 if (*typestr == '?') {
2755 typestr++;
2756 if (*p == '\0') {
2757 break;
2758 }
2759 }
2760 if (get_double(mon, &val, &p) < 0) {
2761 goto fail;
2762 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02002763 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01002764 switch (*p) {
2765 case 'm':
2766 val /= 1e3; p += 2; break;
2767 case 'u':
2768 val /= 1e6; p += 2; break;
2769 case 'n':
2770 val /= 1e9; p += 2; break;
2771 }
2772 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002773 if (*p && !qemu_isspace(*p)) {
2774 monitor_printf(mon, "Unknown unit suffix\n");
2775 goto fail;
2776 }
2777 qdict_put(qdict, key, qfloat_from_double(val));
2778 }
2779 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002780 case 'b':
2781 {
2782 const char *beg;
Eric Blakefc48ffc2015-05-15 16:24:59 -06002783 bool val;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002784
2785 while (qemu_isspace(*p)) {
2786 p++;
2787 }
2788 beg = p;
2789 while (qemu_isgraph(*p)) {
2790 p++;
2791 }
2792 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06002793 val = true;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002794 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06002795 val = false;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002796 } else {
2797 monitor_printf(mon, "Expected 'on' or 'off'\n");
2798 goto fail;
2799 }
Eric Blakefc48ffc2015-05-15 16:24:59 -06002800 qdict_put(qdict, key, qbool_from_bool(val));
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002801 }
2802 break;
bellard9307c4c2004-04-04 12:57:25 +00002803 case '-':
2804 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002805 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03002806 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00002807 /* option */
ths3b46e622007-09-17 08:09:54 +00002808
bellard9307c4c2004-04-04 12:57:25 +00002809 c = *typestr++;
2810 if (c == '\0')
2811 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00002812 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002813 p++;
bellard9307c4c2004-04-04 12:57:25 +00002814 if (*p == '-') {
2815 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002816 if(c != *p) {
2817 if(!is_valid_option(p, typestr)) {
2818
2819 monitor_printf(mon, "%s: unsupported option -%c\n",
Bandan Dasae502122015-06-03 18:38:08 -04002820 cmd->name, *p);
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002821 goto fail;
2822 } else {
2823 skip_key = 1;
2824 }
bellard9307c4c2004-04-04 12:57:25 +00002825 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002826 if(skip_key) {
2827 p = tmp;
2828 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03002829 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002830 p++;
Eric Blakefc48ffc2015-05-15 16:24:59 -06002831 qdict_put(qdict, key, qbool_from_bool(true));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002832 }
bellard9307c4c2004-04-04 12:57:25 +00002833 }
bellard9307c4c2004-04-04 12:57:25 +00002834 }
2835 break;
Wenchao Xia129be002013-08-27 20:38:26 +08002836 case 'S':
2837 {
2838 /* package all remaining string */
2839 int len;
2840
2841 while (qemu_isspace(*p)) {
2842 p++;
2843 }
2844 if (*typestr == '?') {
2845 typestr++;
2846 if (*p == '\0') {
2847 /* no remaining string: NULL argument */
2848 break;
2849 }
2850 }
2851 len = strlen(p);
2852 if (len <= 0) {
2853 monitor_printf(mon, "%s: string expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002854 cmd->name);
Bandan Dase549d2a2015-06-03 18:38:10 -04002855 goto fail;
Wenchao Xia129be002013-08-27 20:38:26 +08002856 }
2857 qdict_put(qdict, key, qstring_from_str(p));
2858 p += len;
2859 }
2860 break;
bellard9307c4c2004-04-04 12:57:25 +00002861 default:
2862 bad_type:
Bandan Dasae502122015-06-03 18:38:08 -04002863 monitor_printf(mon, "%s: unknown type '%c'\n", cmd->name, c);
bellard9307c4c2004-04-04 12:57:25 +00002864 goto fail;
2865 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002866 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002867 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00002868 }
2869 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00002870 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002871 p++;
2872 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00002873 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
Bandan Dasae502122015-06-03 18:38:08 -04002874 cmd->name);
bellard9307c4c2004-04-04 12:57:25 +00002875 goto fail;
2876 }
2877
Bandan Dasae502122015-06-03 18:38:08 -04002878 return qdict;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02002879
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002880fail:
Bandan Dasae502122015-06-03 18:38:08 -04002881 QDECREF(qdict);
Anthony Liguori7267c092011-08-20 22:09:37 -05002882 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002883 return NULL;
2884}
2885
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01002886static void handle_hmp_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002887{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002888 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05002889 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002890
Bandan Dasae502122015-06-03 18:38:08 -04002891 cmd = monitor_parse_command(mon, &cmdline, mon->cmd_table);
2892 if (!cmd) {
2893 return;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002894 }
2895
Bandan Dasae502122015-06-03 18:38:08 -04002896 qdict = monitor_parse_arguments(mon, &cmdline, cmd);
2897 if (!qdict) {
Bandan Dasdd41eea2015-06-03 18:38:09 -04002898 monitor_printf(mon, "Try \"help %s\" for more information\n",
2899 cmd->name);
Bandan Dasae502122015-06-03 18:38:08 -04002900 return;
2901 }
2902
2903 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002904 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00002905}
2906
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08002907static void cmd_completion(Monitor *mon, const char *name, const char *list)
bellard81d09122004-07-14 17:21:37 +00002908{
2909 const char *p, *pstart;
2910 char cmd[128];
2911 int len;
2912
2913 p = list;
2914 for(;;) {
2915 pstart = p;
2916 p = strchr(p, '|');
2917 if (!p)
2918 p = pstart + strlen(pstart);
2919 len = p - pstart;
2920 if (len > sizeof(cmd) - 2)
2921 len = sizeof(cmd) - 2;
2922 memcpy(cmd, pstart, len);
2923 cmd[len] = '\0';
2924 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08002925 readline_add_completion(mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00002926 }
2927 if (*p == '\0')
2928 break;
2929 p++;
2930 }
2931}
2932
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08002933static void file_completion(Monitor *mon, const char *input)
bellard81d09122004-07-14 17:21:37 +00002934{
2935 DIR *ffs;
2936 struct dirent *d;
2937 char path[1024];
2938 char file[1024], file_prefix[1024];
2939 int input_path_len;
2940 const char *p;
2941
ths5fafdf22007-09-16 21:08:06 +00002942 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00002943 if (!p) {
2944 input_path_len = 0;
2945 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00002946 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00002947 } else {
2948 input_path_len = p - input + 1;
2949 memcpy(path, input, input_path_len);
2950 if (input_path_len > sizeof(path) - 1)
2951 input_path_len = sizeof(path) - 1;
2952 path[input_path_len] = '\0';
2953 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2954 }
Bandan Das19f2db52015-06-03 18:38:07 -04002955
bellard81d09122004-07-14 17:21:37 +00002956 ffs = opendir(path);
2957 if (!ffs)
2958 return;
2959 for(;;) {
2960 struct stat sb;
2961 d = readdir(ffs);
2962 if (!d)
2963 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09002964
2965 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
2966 continue;
2967 }
2968
bellard81d09122004-07-14 17:21:37 +00002969 if (strstart(d->d_name, file_prefix, NULL)) {
2970 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00002971 if (input_path_len < sizeof(file))
2972 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2973 d->d_name);
bellard81d09122004-07-14 17:21:37 +00002974 /* stat the file to find out if it's a directory.
2975 * In that case add a slash to speed up typing long paths
2976 */
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01002977 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
blueswir1363a37d2008-08-21 17:58:08 +00002978 pstrcat(file, sizeof(file), "/");
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01002979 }
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08002980 readline_add_completion(mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00002981 }
2982 }
2983 closedir(ffs);
2984}
2985
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002986static const char *next_arg_type(const char *typestr)
2987{
2988 const char *p = strchr(typestr, ':');
2989 return (p != NULL ? ++p : typestr);
2990}
2991
Hani Benhabiles40d19392014-05-07 23:41:30 +01002992static void add_completion_option(ReadLineState *rs, const char *str,
2993 const char *option)
2994{
2995 if (!str || !option) {
2996 return;
2997 }
2998 if (!strncmp(option, str, strlen(str))) {
2999 readline_add_completion(rs, option);
3000 }
3001}
3002
Hani Benhabiles13e315d2014-05-07 23:41:29 +01003003void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
3004{
3005 size_t len;
3006 ChardevBackendInfoList *list, *start;
3007
3008 if (nb_args != 2) {
3009 return;
3010 }
3011 len = strlen(str);
3012 readline_set_completion_index(rs, len);
3013
3014 start = list = qmp_query_chardev_backends(NULL);
3015 while (list) {
3016 const char *chr_name = list->value->name;
3017
3018 if (!strncmp(chr_name, str, len)) {
3019 readline_add_completion(rs, chr_name);
3020 }
3021 list = list->next;
3022 }
3023 qapi_free_ChardevBackendInfoList(start);
3024}
3025
Hani Benhabilesb162b492014-05-07 23:41:31 +01003026void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
3027{
3028 size_t len;
3029 int i;
3030
3031 if (nb_args != 2) {
3032 return;
3033 }
3034 len = strlen(str);
3035 readline_set_completion_index(rs, len);
3036 for (i = 0; NetClientOptionsKind_lookup[i]; i++) {
3037 add_completion_option(rs, str, NetClientOptionsKind_lookup[i]);
3038 }
3039}
3040
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003041void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003042{
3043 GSList *list, *elt;
3044 size_t len;
3045
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003046 if (nb_args != 2) {
3047 return;
3048 }
3049
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003050 len = strlen(str);
3051 readline_set_completion_index(rs, len);
3052 list = elt = object_class_get_list(TYPE_DEVICE, false);
3053 while (elt) {
3054 const char *name;
3055 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
3056 TYPE_DEVICE);
3057 name = object_class_get_name(OBJECT_CLASS(dc));
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003058
3059 if (!dc->cannot_instantiate_with_device_add_yet
3060 && !strncmp(name, str, len)) {
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003061 readline_add_completion(rs, name);
3062 }
3063 elt = elt->next;
3064 }
3065 g_slist_free(list);
3066}
3067
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003068void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003069{
3070 GSList *list, *elt;
3071 size_t len;
3072
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003073 if (nb_args != 2) {
3074 return;
3075 }
3076
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003077 len = strlen(str);
3078 readline_set_completion_index(rs, len);
3079 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
3080 while (elt) {
3081 const char *name;
3082
3083 name = object_class_get_name(OBJECT_CLASS(elt->data));
3084 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
3085 readline_add_completion(rs, name);
3086 }
3087 elt = elt->next;
3088 }
3089 g_slist_free(list);
3090}
3091
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003092static void peripheral_device_del_completion(ReadLineState *rs,
3093 const char *str, size_t len)
3094{
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003095 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
3096 GSList *list, *item;
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003097
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003098 list = qdev_build_hotpluggable_device_list(peripheral);
3099 if (!list) {
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003100 return;
3101 }
3102
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003103 for (item = list; item; item = g_slist_next(item)) {
3104 DeviceState *dev = item->data;
3105
3106 if (dev->id && !strncmp(str, dev->id, len)) {
3107 readline_add_completion(rs, dev->id);
3108 }
3109 }
3110
3111 g_slist_free(list);
3112}
3113
Hani Benhabiles6297d9a2014-05-07 23:41:28 +01003114void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3115{
3116 size_t len;
3117 ChardevInfoList *list, *start;
3118
3119 if (nb_args != 2) {
3120 return;
3121 }
3122 len = strlen(str);
3123 readline_set_completion_index(rs, len);
3124
3125 start = list = qmp_query_chardev(NULL);
3126 while (list) {
3127 ChardevInfo *chr = list->value;
3128
3129 if (!strncmp(chr->label, str, len)) {
3130 readline_add_completion(rs, chr->label);
3131 }
3132 list = list->next;
3133 }
3134 qapi_free_ChardevInfoList(start);
3135}
3136
Hani Benhabiles8e597772014-05-27 23:39:30 +01003137static void ringbuf_completion(ReadLineState *rs, const char *str)
3138{
3139 size_t len;
3140 ChardevInfoList *list, *start;
3141
3142 len = strlen(str);
3143 readline_set_completion_index(rs, len);
3144
3145 start = list = qmp_query_chardev(NULL);
3146 while (list) {
3147 ChardevInfo *chr_info = list->value;
3148
3149 if (!strncmp(chr_info->label, str, len)) {
3150 CharDriverState *chr = qemu_chr_find(chr_info->label);
3151 if (chr && chr_is_ringbuf(chr)) {
3152 readline_add_completion(rs, chr_info->label);
3153 }
3154 }
3155 list = list->next;
3156 }
3157 qapi_free_ChardevInfoList(start);
3158}
3159
Hani Benhabiles8e597772014-05-27 23:39:30 +01003160void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
3161{
3162 if (nb_args != 2) {
3163 return;
3164 }
3165 ringbuf_completion(rs, str);
3166}
3167
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003168void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
3169{
3170 size_t len;
3171
3172 if (nb_args != 2) {
3173 return;
3174 }
3175
3176 len = strlen(str);
3177 readline_set_completion_index(rs, len);
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003178 peripheral_device_del_completion(rs, str, len);
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003179}
3180
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003181void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003182{
3183 ObjectPropertyInfoList *list, *start;
3184 size_t len;
3185
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003186 if (nb_args != 2) {
3187 return;
3188 }
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003189 len = strlen(str);
3190 readline_set_completion_index(rs, len);
3191
3192 start = list = qmp_qom_list("/objects", NULL);
3193 while (list) {
3194 ObjectPropertyInfo *info = list->value;
3195
3196 if (!strncmp(info->type, "child<", 5)
3197 && !strncmp(info->name, str, len)) {
3198 readline_add_completion(rs, info->name);
3199 }
3200 list = list->next;
3201 }
3202 qapi_free_ObjectPropertyInfoList(start);
3203}
3204
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003205void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
3206{
3207 int i;
3208 char *sep;
3209 size_t len;
3210
3211 if (nb_args != 2) {
3212 return;
3213 }
3214 sep = strrchr(str, '-');
3215 if (sep) {
3216 str = sep + 1;
3217 }
3218 len = strlen(str);
3219 readline_set_completion_index(rs, len);
3220 for (i = 0; i < Q_KEY_CODE_MAX; i++) {
3221 if (!strncmp(str, QKeyCode_lookup[i], len)) {
3222 readline_add_completion(rs, QKeyCode_lookup[i]);
3223 }
3224 }
3225}
3226
Hani Benhabiles40d19392014-05-07 23:41:30 +01003227void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
3228{
3229 size_t len;
3230
3231 len = strlen(str);
3232 readline_set_completion_index(rs, len);
3233 if (nb_args == 2) {
Jason Wangeaed4832015-04-23 14:21:38 +08003234 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles40d19392014-05-07 23:41:30 +01003235 int count, i;
3236 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08003237 NET_CLIENT_OPTIONS_KIND_NONE,
3238 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003239 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles40d19392014-05-07 23:41:30 +01003240 const char *name = ncs[i]->name;
3241 if (!strncmp(str, name, len)) {
3242 readline_add_completion(rs, name);
3243 }
3244 }
3245 } else if (nb_args == 3) {
3246 add_completion_option(rs, str, "on");
3247 add_completion_option(rs, str, "off");
3248 }
3249}
3250
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003251void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
3252{
3253 int len, count, i;
Jason Wangeaed4832015-04-23 14:21:38 +08003254 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003255
3256 if (nb_args != 2) {
3257 return;
3258 }
3259
3260 len = strlen(str);
3261 readline_set_completion_index(rs, len);
3262 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_OPTIONS_KIND_NIC,
Jason Wangeaed4832015-04-23 14:21:38 +08003263 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003264 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003265 QemuOpts *opts;
3266 const char *name = ncs[i]->name;
3267 if (strncmp(str, name, len)) {
3268 continue;
3269 }
3270 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
3271 if (opts) {
3272 readline_add_completion(rs, name);
3273 }
3274 }
3275}
3276
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003277void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
3278{
3279 size_t len;
3280
3281 len = strlen(str);
3282 readline_set_completion_index(rs, len);
3283 if (nb_args == 2) {
3284 TraceEventID id;
3285 for (id = 0; id < trace_event_count(); id++) {
3286 const char *event_name = trace_event_get_name(trace_event_id(id));
3287 if (!strncmp(str, event_name, len)) {
3288 readline_add_completion(rs, event_name);
3289 }
3290 }
3291 } else if (nb_args == 3) {
3292 add_completion_option(rs, str, "on");
3293 add_completion_option(rs, str, "off");
3294 }
3295}
3296
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003297void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
3298{
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003299 int i;
3300
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003301 if (nb_args != 2) {
3302 return;
3303 }
3304 readline_set_completion_index(rs, strlen(str));
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003305 for (i = 0; WatchdogExpirationAction_lookup[i]; i++) {
3306 add_completion_option(rs, str, WatchdogExpirationAction_lookup[i]);
3307 }
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003308}
3309
Hani Benhabilesc68a0402014-05-27 23:39:32 +01003310void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
3311 const char *str)
3312{
3313 size_t len;
3314
3315 len = strlen(str);
3316 readline_set_completion_index(rs, len);
3317 if (nb_args == 2) {
3318 int i;
3319 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
3320 const char *name = MigrationCapability_lookup[i];
3321 if (!strncmp(str, name, len)) {
3322 readline_add_completion(rs, name);
3323 }
3324 }
3325 } else if (nb_args == 3) {
3326 add_completion_option(rs, str, "on");
3327 add_completion_option(rs, str, "off");
3328 }
3329}
3330
Liang Li50e9a622015-03-23 16:32:29 +08003331void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
3332 const char *str)
3333{
3334 size_t len;
3335
3336 len = strlen(str);
3337 readline_set_completion_index(rs, len);
3338 if (nb_args == 2) {
3339 int i;
3340 for (i = 0; i < MIGRATION_PARAMETER_MAX; i++) {
3341 const char *name = MigrationParameter_lookup[i];
3342 if (!strncmp(str, name, len)) {
3343 readline_add_completion(rs, name);
3344 }
3345 }
3346 }
3347}
3348
Hani Benhabilese3bb5322014-05-27 23:39:34 +01003349void host_net_add_completion(ReadLineState *rs, int nb_args, const char *str)
3350{
3351 int i;
3352 size_t len;
3353 if (nb_args != 2) {
3354 return;
3355 }
3356 len = strlen(str);
3357 readline_set_completion_index(rs, len);
3358 for (i = 0; host_net_devices[i]; i++) {
3359 if (!strncmp(host_net_devices[i], str, len)) {
3360 readline_add_completion(rs, host_net_devices[i]);
3361 }
3362 }
3363}
3364
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003365void host_net_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3366{
Jason Wangeaed4832015-04-23 14:21:38 +08003367 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003368 int count, i, len;
3369
3370 len = strlen(str);
3371 readline_set_completion_index(rs, len);
3372 if (nb_args == 2) {
3373 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08003374 NET_CLIENT_OPTIONS_KIND_NONE,
3375 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003376 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003377 int id;
3378 char name[16];
3379
3380 if (net_hub_id_for_client(ncs[i], &id)) {
3381 continue;
3382 }
3383 snprintf(name, sizeof(name), "%d", id);
3384 if (!strncmp(str, name, len)) {
3385 readline_add_completion(rs, name);
3386 }
3387 }
3388 return;
3389 } else if (nb_args == 3) {
3390 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08003391 NET_CLIENT_OPTIONS_KIND_NIC,
3392 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003393 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Jason Wang2c4681f2015-02-02 15:06:38 +08003394 int id;
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003395 const char *name;
3396
Jason Wang2c4681f2015-02-02 15:06:38 +08003397 if (ncs[i]->info->type == NET_CLIENT_OPTIONS_KIND_HUBPORT ||
3398 net_hub_id_for_client(ncs[i], &id)) {
3399 continue;
3400 }
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003401 name = ncs[i]->name;
3402 if (!strncmp(str, name, len)) {
3403 readline_add_completion(rs, name);
3404 }
3405 }
3406 return;
3407 }
3408}
3409
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003410static void vm_completion(ReadLineState *rs, const char *str)
3411{
3412 size_t len;
3413 BlockDriverState *bs = NULL;
3414
3415 len = strlen(str);
3416 readline_set_completion_index(rs, len);
3417 while ((bs = bdrv_next(bs))) {
3418 SnapshotInfoList *snapshots, *snapshot;
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003419 AioContext *ctx = bdrv_get_aio_context(bs);
3420 bool ok = false;
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003421
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003422 aio_context_acquire(ctx);
3423 if (bdrv_can_snapshot(bs)) {
3424 ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0;
3425 }
3426 aio_context_release(ctx);
3427 if (!ok) {
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003428 continue;
3429 }
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003430
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003431 snapshot = snapshots;
3432 while (snapshot) {
3433 char *completion = snapshot->value->name;
3434 if (!strncmp(str, completion, len)) {
3435 readline_add_completion(rs, completion);
3436 }
3437 completion = snapshot->value->id;
3438 if (!strncmp(str, completion, len)) {
3439 readline_add_completion(rs, completion);
3440 }
3441 snapshot = snapshot->next;
3442 }
3443 qapi_free_SnapshotInfoList(snapshots);
3444 }
3445
3446}
3447
3448void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
3449{
3450 if (nb_args == 2) {
3451 vm_completion(rs, str);
3452 }
3453}
3454
3455void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
3456{
3457 if (nb_args == 2) {
3458 vm_completion(rs, str);
3459 }
3460}
3461
Wenchao Xiac35b6402013-08-27 20:38:24 +08003462static void monitor_find_completion_by_table(Monitor *mon,
3463 const mon_cmd_t *cmd_table,
3464 char **args,
3465 int nb_args)
bellard81d09122004-07-14 17:21:37 +00003466{
3467 const char *cmdname;
Wenchao Xiac35b6402013-08-27 20:38:24 +08003468 int i;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003469 const char *ptype, *str, *name;
Anthony Liguoric227f092009-10-01 16:12:16 -05003470 const mon_cmd_t *cmd;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003471 BlockDriverState *bs;
bellard81d09122004-07-14 17:21:37 +00003472
bellard81d09122004-07-14 17:21:37 +00003473 if (nb_args <= 1) {
3474 /* command completion */
3475 if (nb_args == 0)
3476 cmdname = "";
3477 else
3478 cmdname = args[0];
Wenchao Xiad2674b22013-08-27 20:38:16 +08003479 readline_set_completion_index(mon->rs, strlen(cmdname));
Wenchao Xiac35b6402013-08-27 20:38:24 +08003480 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003481 cmd_completion(mon, cmdname, cmd->name);
bellard81d09122004-07-14 17:21:37 +00003482 }
3483 } else {
3484 /* find the command */
Wenchao Xiac35b6402013-08-27 20:38:24 +08003485 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Jan Kiszka03a63482010-06-16 00:38:33 +02003486 if (compare_cmd(args[0], cmd->name)) {
3487 break;
3488 }
bellard81d09122004-07-14 17:21:37 +00003489 }
Jan Kiszka03a63482010-06-16 00:38:33 +02003490 if (!cmd->name) {
Wenchao Xiac35b6402013-08-27 20:38:24 +08003491 return;
Jan Kiszka03a63482010-06-16 00:38:33 +02003492 }
3493
Wenchao Xiad903a772013-08-27 20:38:25 +08003494 if (cmd->sub_table) {
3495 /* do the job again */
Stefan Weile7ae7712015-03-08 19:30:01 +01003496 monitor_find_completion_by_table(mon, cmd->sub_table,
3497 &args[1], nb_args - 1);
3498 return;
Wenchao Xiad903a772013-08-27 20:38:25 +08003499 }
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003500 if (cmd->command_completion) {
Stefan Weile7ae7712015-03-08 19:30:01 +01003501 cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
3502 return;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003503 }
Wenchao Xiad903a772013-08-27 20:38:25 +08003504
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003505 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003506 for(i = 0; i < nb_args - 2; i++) {
3507 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003508 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003509 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003510 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003511 }
3512 }
3513 str = args[nb_args - 1];
Kevin Wolf48fe86f2014-11-12 16:24:02 +01003514 while (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02003515 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00003516 }
bellard81d09122004-07-14 17:21:37 +00003517 switch(*ptype) {
3518 case 'F':
3519 /* file completion */
Wenchao Xiad2674b22013-08-27 20:38:16 +08003520 readline_set_completion_index(mon->rs, strlen(str));
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003521 file_completion(mon, str);
bellard81d09122004-07-14 17:21:37 +00003522 break;
3523 case 'B':
3524 /* block device name completion */
Wenchao Xia599a9262013-08-27 20:38:15 +08003525 readline_set_completion_index(mon->rs, strlen(str));
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003526 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
3527 name = bdrv_get_device_name(bs);
3528 if (str[0] == '\0' ||
3529 !strncmp(name, str, strlen(str))) {
3530 readline_add_completion(mon->rs, name);
3531 }
3532 }
bellard81d09122004-07-14 17:21:37 +00003533 break;
bellard7fe48482004-10-09 18:08:01 +00003534 case 's':
Wenchao Xia129be002013-08-27 20:38:26 +08003535 case 'S':
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003536 if (!strcmp(cmd->name, "help|?")) {
Wenchao Xia7ca0e062013-08-27 20:38:27 +08003537 monitor_find_completion_by_table(mon, cmd_table,
3538 &args[1], nb_args - 1);
bellard7fe48482004-10-09 18:08:01 +00003539 }
3540 break;
bellard81d09122004-07-14 17:21:37 +00003541 default:
3542 break;
3543 }
3544 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08003545}
3546
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003547static void monitor_find_completion(void *opaque,
Wenchao Xiac35b6402013-08-27 20:38:24 +08003548 const char *cmdline)
3549{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003550 Monitor *mon = opaque;
Wenchao Xiac35b6402013-08-27 20:38:24 +08003551 char *args[MAX_ARGS];
3552 int nb_args, len;
3553
3554 /* 1. parse the cmdline */
3555 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
3556 return;
3557 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08003558
3559 /* if the line ends with a space, it means we want to complete the
3560 next arg */
3561 len = strlen(cmdline);
3562 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
3563 if (nb_args >= MAX_ARGS) {
3564 goto cleanup;
3565 }
3566 args[nb_args++] = g_strdup("");
3567 }
3568
3569 /* 2. auto complete according to args */
3570 monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args);
Jan Kiszka03a63482010-06-16 00:38:33 +02003571
3572cleanup:
Wenchao Xiadcc70cd2013-08-27 20:38:22 +08003573 free_cmdline_args(args, nb_args);
bellard81d09122004-07-14 17:21:37 +00003574}
3575
aliguori731b0362009-03-05 23:01:42 +00003576static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003577{
aliguori731b0362009-03-05 23:01:42 +00003578 Monitor *mon = opaque;
3579
Jan Kiszkac62313b2009-12-04 14:05:29 +01003580 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003581}
3582
Markus Armbruster40861822015-05-29 10:27:16 +02003583static bool invalid_qmp_mode(const Monitor *mon, const mon_cmd_t *cmd,
3584 Error **errp)
Luiz Capitulino09069b12010-02-04 18:10:06 -02003585{
Markus Armbruster485febc2015-03-13 17:25:50 +01003586 bool is_cap = cmd->mhandler.cmd_new == qmp_capabilities;
Markus Armbrusterf994b252015-03-06 19:51:51 +01003587
3588 if (is_cap && mon->qmp.in_command_mode) {
Markus Armbruster40861822015-05-29 10:27:16 +02003589 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
3590 "Capabilities negotiation is already complete, command "
3591 "'%s' ignored", cmd->name);
Eric Blake2d5a8342015-04-15 09:19:23 -06003592 return true;
3593 }
Markus Armbrusterf994b252015-03-06 19:51:51 +01003594 if (!is_cap && !mon->qmp.in_command_mode) {
Markus Armbruster40861822015-05-29 10:27:16 +02003595 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
3596 "Expecting capabilities negotiation with "
3597 "'qmp_capabilities' before command '%s'", cmd->name);
Eric Blake2d5a8342015-04-15 09:19:23 -06003598 return true;
3599 }
3600 return false;
Luiz Capitulino09069b12010-02-04 18:10:06 -02003601}
3602
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003603/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03003604 * Argument validation rules:
3605 *
3606 * 1. The argument must exist in cmd_args qdict
3607 * 2. The argument type must be the expected one
3608 *
3609 * Special case: If the argument doesn't exist in cmd_args and
3610 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
3611 * checking is skipped for it.
3612 */
Markus Armbruster326283a2015-03-02 18:39:09 +01003613static void check_client_args_type(const QDict *client_args,
3614 const QDict *cmd_args, int flags,
3615 Error **errp)
Luiz Capitulino4af91932010-06-22 11:44:05 -03003616{
3617 const QDictEntry *ent;
3618
3619 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
3620 QObject *obj;
3621 QString *arg_type;
3622 const QObject *client_arg = qdict_entry_value(ent);
3623 const char *client_arg_name = qdict_entry_key(ent);
3624
3625 obj = qdict_get(cmd_args, client_arg_name);
3626 if (!obj) {
3627 if (flags & QMP_ACCEPT_UNKNOWNS) {
3628 /* handler accepts unknowns */
3629 continue;
3630 }
3631 /* client arg doesn't exist */
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003632 error_setg(errp, QERR_INVALID_PARAMETER, client_arg_name);
Markus Armbruster326283a2015-03-02 18:39:09 +01003633 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003634 }
3635
3636 arg_type = qobject_to_qstring(obj);
3637 assert(arg_type != NULL);
3638
3639 /* check if argument's type is correct */
3640 switch (qstring_get_str(arg_type)[0]) {
3641 case 'F':
3642 case 'B':
3643 case 's':
3644 if (qobject_type(client_arg) != QTYPE_QSTRING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003645 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3646 client_arg_name, "string");
Markus Armbruster326283a2015-03-02 18:39:09 +01003647 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003648 }
3649 break;
3650 case 'i':
3651 case 'l':
3652 case 'M':
Jes Sorensendbc0c672010-10-21 17:15:47 +02003653 case 'o':
Luiz Capitulino4af91932010-06-22 11:44:05 -03003654 if (qobject_type(client_arg) != QTYPE_QINT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003655 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3656 client_arg_name, "int");
Markus Armbruster326283a2015-03-02 18:39:09 +01003657 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003658 }
3659 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003660 case 'T':
3661 if (qobject_type(client_arg) != QTYPE_QINT &&
3662 qobject_type(client_arg) != QTYPE_QFLOAT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003663 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3664 client_arg_name, "number");
Markus Armbruster326283a2015-03-02 18:39:09 +01003665 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003666 }
3667 break;
3668 case 'b':
3669 case '-':
3670 if (qobject_type(client_arg) != QTYPE_QBOOL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003671 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3672 client_arg_name, "bool");
Markus Armbruster326283a2015-03-02 18:39:09 +01003673 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003674 }
3675 break;
3676 case 'O':
3677 assert(flags & QMP_ACCEPT_UNKNOWNS);
3678 break;
Paolo Bonzinib9f89782012-03-22 12:51:11 +01003679 case 'q':
3680 /* Any QObject can be passed. */
3681 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003682 case '/':
3683 case '.':
3684 /*
3685 * These types are not supported by QMP and thus are not
3686 * handled here. Fall through.
3687 */
3688 default:
3689 abort();
3690 }
3691 }
Luiz Capitulino4af91932010-06-22 11:44:05 -03003692}
3693
3694/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003695 * - Check if the client has passed all mandatory args
3696 * - Set special flags for argument validation
3697 */
Markus Armbruster326283a2015-03-02 18:39:09 +01003698static void check_mandatory_args(const QDict *cmd_args,
3699 const QDict *client_args, int *flags,
3700 Error **errp)
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003701{
3702 const QDictEntry *ent;
3703
3704 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
3705 const char *cmd_arg_name = qdict_entry_key(ent);
3706 QString *type = qobject_to_qstring(qdict_entry_value(ent));
3707 assert(type != NULL);
3708
3709 if (qstring_get_str(type)[0] == 'O') {
3710 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
3711 *flags |= QMP_ACCEPT_UNKNOWNS;
3712 } else if (qstring_get_str(type)[0] != '-' &&
3713 qstring_get_str(type)[1] != '?' &&
3714 !qdict_haskey(client_args, cmd_arg_name)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003715 error_setg(errp, QERR_MISSING_PARAMETER, cmd_arg_name);
Markus Armbruster326283a2015-03-02 18:39:09 +01003716 return;
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003717 }
3718 }
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003719}
3720
3721static QDict *qdict_from_args_type(const char *args_type)
3722{
3723 int i;
3724 QDict *qdict;
3725 QString *key, *type, *cur_qs;
3726
3727 assert(args_type != NULL);
3728
3729 qdict = qdict_new();
3730
3731 if (args_type == NULL || args_type[0] == '\0') {
3732 /* no args, empty qdict */
3733 goto out;
3734 }
3735
3736 key = qstring_new();
3737 type = qstring_new();
3738
3739 cur_qs = key;
3740
3741 for (i = 0;; i++) {
3742 switch (args_type[i]) {
3743 case ',':
3744 case '\0':
3745 qdict_put(qdict, qstring_get_str(key), type);
3746 QDECREF(key);
3747 if (args_type[i] == '\0') {
3748 goto out;
3749 }
3750 type = qstring_new(); /* qdict has ref */
3751 cur_qs = key = qstring_new();
3752 break;
3753 case ':':
3754 cur_qs = type;
3755 break;
3756 default:
3757 qstring_append_chr(cur_qs, args_type[i]);
3758 break;
3759 }
3760 }
3761
3762out:
3763 return qdict;
3764}
3765
3766/*
3767 * Client argument checking rules:
3768 *
3769 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03003770 * 2. Each argument provided by the client must be expected
3771 * 3. Each argument provided by the client must have the type expected
3772 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003773 */
Markus Armbruster326283a2015-03-02 18:39:09 +01003774static void qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args,
3775 Error **errp)
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003776{
Markus Armbruster326283a2015-03-02 18:39:09 +01003777 Error *err = NULL;
3778 int flags;
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003779 QDict *cmd_args;
3780
3781 cmd_args = qdict_from_args_type(cmd->args_type);
3782
3783 flags = 0;
Markus Armbruster326283a2015-03-02 18:39:09 +01003784 check_mandatory_args(cmd_args, client_args, &flags, &err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003785 if (err) {
3786 goto out;
3787 }
3788
Markus Armbruster326283a2015-03-02 18:39:09 +01003789 check_client_args_type(client_args, cmd_args, flags, &err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003790
3791out:
Markus Armbruster326283a2015-03-02 18:39:09 +01003792 error_propagate(errp, err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003793 QDECREF(cmd_args);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003794}
3795
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003796/*
3797 * Input object checking rules
3798 *
3799 * 1. Input object must be a dict
3800 * 2. The "execute" key must exist
3801 * 3. The "execute" key must be a string
3802 * 4. If the "arguments" key exists, it must be a dict
3803 * 5. If the "id" key exists, it can be anything (ie. json-value)
3804 * 6. Any argument not listed above is considered invalid
3805 */
Markus Armbrusterba0510a2015-03-02 18:41:43 +01003806static QDict *qmp_check_input_obj(QObject *input_obj, Error **errp)
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003807{
3808 const QDictEntry *ent;
3809 int has_exec_key = 0;
3810 QDict *input_dict;
3811
3812 if (qobject_type(input_obj) != QTYPE_QDICT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003813 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "object");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003814 return NULL;
3815 }
3816
3817 input_dict = qobject_to_qdict(input_obj);
3818
3819 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
3820 const char *arg_name = qdict_entry_key(ent);
3821 const QObject *arg_obj = qdict_entry_value(ent);
3822
3823 if (!strcmp(arg_name, "execute")) {
3824 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003825 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
3826 "execute", "string");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003827 return NULL;
3828 }
3829 has_exec_key = 1;
3830 } else if (!strcmp(arg_name, "arguments")) {
3831 if (qobject_type(arg_obj) != QTYPE_QDICT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003832 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
3833 "arguments", "object");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003834 return NULL;
3835 }
Markus Armbruster779cec42015-06-08 10:44:30 +02003836 } else if (!strcmp(arg_name, "id")) {
3837 /* Any string is acceptable as "id", so nothing to check */
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003838 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003839 error_setg(errp, QERR_QMP_EXTRA_MEMBER, arg_name);
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003840 return NULL;
3841 }
3842 }
3843
3844 if (!has_exec_key) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003845 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "execute");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003846 return NULL;
3847 }
3848
3849 return input_dict;
3850}
3851
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003852static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
3853{
Markus Armbruster326283a2015-03-02 18:39:09 +01003854 Error *local_err = NULL;
Markus Armbruster84add862015-03-05 16:45:15 +01003855 QObject *obj, *data;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003856 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003857 const mon_cmd_t *cmd;
Luiz Capitulino40e5a012011-10-21 16:15:31 -02003858 const char *cmd_name;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003859 Monitor *mon = cur_mon;
3860
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03003861 args = input = NULL;
Markus Armbruster84add862015-03-05 16:45:15 +01003862 data = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003863
3864 obj = json_parser_parse(tokens, NULL);
3865 if (!obj) {
3866 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003867 error_setg(&local_err, QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003868 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003869 }
3870
Markus Armbrusterba0510a2015-03-02 18:41:43 +01003871 input = qmp_check_input_obj(obj, &local_err);
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003872 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003873 qobject_decref(obj);
3874 goto err_out;
3875 }
3876
Markus Armbruster74358f22015-03-06 19:35:59 +01003877 mon->qmp.id = qdict_get(input, "id");
3878 qobject_incref(mon->qmp.id);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003879
Luiz Capitulino0bbab462010-05-31 17:32:50 -03003880 cmd_name = qdict_get_str(input, "execute");
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +01003881 trace_handle_qmp_command(mon, cmd_name);
Anthony Liguorie3193602011-09-02 12:34:47 -05003882 cmd = qmp_find_cmd(cmd_name);
Eric Blake2d5a8342015-04-15 09:19:23 -06003883 if (!cmd) {
Markus Armbruster710aec92015-03-06 11:28:00 +01003884 error_set(&local_err, ERROR_CLASS_COMMAND_NOT_FOUND,
3885 "The command %s has not been found", cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03003886 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003887 }
Markus Armbruster40861822015-05-29 10:27:16 +02003888 if (invalid_qmp_mode(mon, cmd, &local_err)) {
Eric Blake2d5a8342015-04-15 09:19:23 -06003889 goto err_out;
3890 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003891
3892 obj = qdict_get(input, "arguments");
3893 if (!obj) {
3894 args = qdict_new();
3895 } else {
3896 args = qobject_to_qdict(obj);
3897 QINCREF(args);
3898 }
3899
Markus Armbruster326283a2015-03-02 18:39:09 +01003900 qmp_check_client_args(cmd, args, &local_err);
3901 if (local_err) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003902 goto err_out;
3903 }
3904
Markus Armbruster485febc2015-03-13 17:25:50 +01003905 cmd->mhandler.cmd_new(args, &data, &local_err);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003906
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003907err_out:
Markus Armbruster710aec92015-03-06 11:28:00 +01003908 monitor_protocol_emitter(mon, data, local_err);
Markus Armbruster84add862015-03-05 16:45:15 +01003909 qobject_decref(data);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03003910 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003911 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003912}
3913
Markus Armbrusterc83fe232015-03-06 19:20:51 +01003914static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003915{
3916 Monitor *old_mon = cur_mon;
3917
3918 cur_mon = opaque;
3919
Markus Armbruster74358f22015-03-06 19:35:59 +01003920 json_message_parser_feed(&cur_mon->qmp.parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003921
3922 cur_mon = old_mon;
3923}
3924
aliguori731b0362009-03-05 23:01:42 +00003925static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00003926{
aliguori731b0362009-03-05 23:01:42 +00003927 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00003928 int i;
aliguori376253e2009-03-05 23:01:23 +00003929
aliguori731b0362009-03-05 23:01:42 +00003930 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00003931
aliguoricde76ee2009-03-05 23:01:51 +00003932 if (cur_mon->rs) {
3933 for (i = 0; i < size; i++)
3934 readline_handle_byte(cur_mon->rs, buf[i]);
3935 } else {
3936 if (size == 0 || buf[size - 1] != 0)
3937 monitor_printf(cur_mon, "corrupted command\n");
3938 else
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003939 handle_hmp_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00003940 }
aliguori731b0362009-03-05 23:01:42 +00003941
3942 cur_mon = old_mon;
3943}
aliguorid8f44602008-10-06 13:52:44 +00003944
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003945static void monitor_command_cb(void *opaque, const char *cmdline,
3946 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00003947{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003948 Monitor *mon = opaque;
3949
aliguori731b0362009-03-05 23:01:42 +00003950 monitor_suspend(mon);
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003951 handle_hmp_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00003952 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00003953}
3954
aliguoricde76ee2009-03-05 23:01:51 +00003955int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003956{
aliguoricde76ee2009-03-05 23:01:51 +00003957 if (!mon->rs)
3958 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00003959 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00003960 return 0;
aliguorid8f44602008-10-06 13:52:44 +00003961}
3962
aliguori376253e2009-03-05 23:01:23 +00003963void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003964{
aliguoricde76ee2009-03-05 23:01:51 +00003965 if (!mon->rs)
3966 return;
aliguori731b0362009-03-05 23:01:42 +00003967 if (--mon->suspend_cnt == 0)
3968 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00003969}
3970
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003971static QObject *get_qmp_greeting(void)
3972{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03003973 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003974
Markus Armbruster7fad30f2015-09-16 13:06:19 +02003975 qmp_marshal_query_version(NULL, &ver, NULL);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003976 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
3977}
3978
Markus Armbrusterc83fe232015-03-06 19:20:51 +01003979static void monitor_qmp_event(void *opaque, int event)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003980{
Luiz Capitulino47116d12010-02-08 17:01:30 -02003981 QObject *data;
3982 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003983
Luiz Capitulino47116d12010-02-08 17:01:30 -02003984 switch (event) {
3985 case CHR_EVENT_OPENED:
Markus Armbrusterf994b252015-03-06 19:51:51 +01003986 mon->qmp.in_command_mode = false;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003987 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003988 monitor_json_emitter(mon, data);
3989 qobject_decref(data);
Corey Bryantefb87c12012-08-14 16:43:48 -04003990 mon_refcount++;
Luiz Capitulino47116d12010-02-08 17:01:30 -02003991 break;
3992 case CHR_EVENT_CLOSED:
Markus Armbruster74358f22015-03-06 19:35:59 +01003993 json_message_parser_destroy(&mon->qmp.parser);
3994 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Corey Bryantefb87c12012-08-14 16:43:48 -04003995 mon_refcount--;
3996 monitor_fdsets_cleanup();
Luiz Capitulino47116d12010-02-08 17:01:30 -02003997 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003998 }
3999}
4000
aliguori731b0362009-03-05 23:01:42 +00004001static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004002{
aliguori376253e2009-03-05 23:01:23 +00004003 Monitor *mon = opaque;
4004
aliguori2724b182009-03-05 23:01:47 +00004005 switch (event) {
4006 case CHR_EVENT_MUX_IN:
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02004007 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004008 mon->mux_out = 0;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02004009 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004010 if (mon->reset_seen) {
4011 readline_restart(mon->rs);
4012 monitor_resume(mon);
4013 monitor_flush(mon);
4014 } else {
4015 mon->suspend_cnt = 0;
4016 }
aliguori2724b182009-03-05 23:01:47 +00004017 break;
ths86e94de2007-01-05 22:01:59 +00004018
aliguori2724b182009-03-05 23:01:47 +00004019 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004020 if (mon->reset_seen) {
4021 if (mon->suspend_cnt == 0) {
4022 monitor_printf(mon, "\n");
4023 }
4024 monitor_flush(mon);
4025 monitor_suspend(mon);
4026 } else {
4027 mon->suspend_cnt++;
4028 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02004029 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004030 mon->mux_out = 1;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02004031 qemu_mutex_unlock(&mon->out_lock);
aliguori2724b182009-03-05 23:01:47 +00004032 break;
4033
Amit Shahb6b8df52009-10-07 18:31:16 +05304034 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004035 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4036 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004037 if (!mon->mux_out) {
Stratos Psomadakise5554e22014-09-15 15:34:57 +03004038 readline_restart(mon->rs);
aliguori2724b182009-03-05 23:01:47 +00004039 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004040 }
4041 mon->reset_seen = 1;
Corey Bryantefb87c12012-08-14 16:43:48 -04004042 mon_refcount++;
4043 break;
4044
4045 case CHR_EVENT_CLOSED:
4046 mon_refcount--;
4047 monitor_fdsets_cleanup();
aliguori2724b182009-03-05 23:01:47 +00004048 break;
4049 }
ths86e94de2007-01-05 22:01:59 +00004050}
4051
Wayne Xia816f8922011-10-12 11:32:41 +08004052static int
4053compare_mon_cmd(const void *a, const void *b)
4054{
4055 return strcmp(((const mon_cmd_t *)a)->name,
4056 ((const mon_cmd_t *)b)->name);
4057}
4058
4059static void sortcmdlist(void)
4060{
4061 int array_num;
4062 int elem_size = sizeof(mon_cmd_t);
4063
4064 array_num = sizeof(mon_cmds)/elem_size-1;
4065 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
4066
4067 array_num = sizeof(info_cmds)/elem_size-1;
4068 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
4069}
4070
aliguori76655d62009-03-06 20:27:37 +00004071
4072/*
4073 * Local variables:
4074 * c-indent-level: 4
4075 * c-basic-offset: 4
4076 * tab-width: 8
4077 * End:
4078 */
4079
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004080/* These functions just adapt the readline interface in a typesafe way. We
4081 * could cast function pointers but that discards compiler checks.
4082 */
Stefan Weild5d15072014-01-25 18:18:23 +01004083static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
4084 const char *fmt, ...)
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004085{
4086 va_list ap;
4087 va_start(ap, fmt);
4088 monitor_vprintf(opaque, fmt, ap);
4089 va_end(ap);
4090}
4091
4092static void monitor_readline_flush(void *opaque)
4093{
4094 monitor_flush(opaque);
4095}
4096
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004097static void __attribute__((constructor)) monitor_lock_init(void)
4098{
4099 qemu_mutex_init(&monitor_lock);
4100}
4101
aliguori731b0362009-03-05 23:01:42 +00004102void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004103{
aliguori731b0362009-03-05 23:01:42 +00004104 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004105 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004106
4107 if (is_first_init) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +02004108 monitor_qapi_event_init();
Wenchao Xiad0383172013-08-27 20:38:18 +08004109 sortcmdlist();
ths20d8a3e2007-02-18 17:04:49 +00004110 is_first_init = 0;
4111 }
aliguori87127162009-03-05 23:01:29 +00004112
Wenchao Xiab01fe892013-08-27 20:38:19 +08004113 mon = g_malloc(sizeof(*mon));
4114 monitor_data_init(mon);
ths20d8a3e2007-02-18 17:04:49 +00004115
aliguori87127162009-03-05 23:01:29 +00004116 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00004117 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004118 if (flags & MONITOR_USE_READLINE) {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004119 mon->rs = readline_init(monitor_readline_printf,
4120 monitor_readline_flush,
4121 mon,
4122 monitor_find_completion);
aliguoricde76ee2009-03-05 23:01:51 +00004123 monitor_read_command(mon, 0);
4124 }
aliguori87127162009-03-05 23:01:29 +00004125
Markus Armbruster9f3982f2015-03-06 19:56:38 +01004126 if (monitor_is_qmp(mon)) {
Markus Armbrusterc83fe232015-03-06 19:20:51 +01004127 qemu_chr_add_handlers(chr, monitor_can_read, monitor_qmp_read,
4128 monitor_qmp_event, mon);
Anthony Liguori15f31512011-08-15 11:17:35 -05004129 qemu_chr_fe_set_echo(chr, true);
Markus Armbruster74358f22015-03-06 19:35:59 +01004130 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004131 } else {
4132 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4133 monitor_event, mon);
4134 }
aliguori87127162009-03-05 23:01:29 +00004135
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004136 qemu_mutex_lock(&monitor_lock);
Blue Swirl72cf2d42009-09-12 07:36:22 +00004137 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004138 qemu_mutex_unlock(&monitor_lock);
bellard7e2515e2004-08-01 21:52:19 +00004139}
4140
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004141static void bdrv_password_cb(void *opaque, const char *password,
4142 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00004143{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004144 Monitor *mon = opaque;
4145 BlockDriverState *bs = readline_opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004146 int ret = 0;
Markus Armbruster4d2855a2015-01-29 10:37:00 +01004147 Error *local_err = NULL;
bellard7e2515e2004-08-01 21:52:19 +00004148
Markus Armbruster4d2855a2015-01-29 10:37:00 +01004149 bdrv_add_key(bs, password, &local_err);
4150 if (local_err) {
4151 monitor_printf(mon, "%s\n", error_get_pretty(local_err));
4152 error_free(local_err);
aliguoribb5fc202009-03-05 23:01:15 +00004153 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004154 }
aliguori731b0362009-03-05 23:01:42 +00004155 if (mon->password_completion_cb)
4156 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004157
aliguori731b0362009-03-05 23:01:42 +00004158 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004159}
aliguoric0f4ce72009-03-05 23:01:01 +00004160
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004161int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
Markus Armbruster097310b2014-10-07 13:59:15 +02004162 BlockCompletionFunc *completion_cb,
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004163 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004164{
aliguoricde76ee2009-03-05 23:01:51 +00004165 int err;
4166
aliguori376253e2009-03-05 23:01:23 +00004167 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4168 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00004169
aliguori731b0362009-03-05 23:01:42 +00004170 mon->password_completion_cb = completion_cb;
4171 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004172
aliguoricde76ee2009-03-05 23:01:51 +00004173 err = monitor_read_password(mon, bdrv_password_cb, bs);
4174
4175 if (err && completion_cb)
4176 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004177
4178 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00004179}
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004180
4181int monitor_read_block_device_key(Monitor *mon, const char *device,
Markus Armbruster097310b2014-10-07 13:59:15 +02004182 BlockCompletionFunc *completion_cb,
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004183 void *opaque)
4184{
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01004185 Error *err = NULL;
Fam Zheng55606252015-03-02 19:36:46 +08004186 BlockBackend *blk;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004187
Fam Zheng55606252015-03-02 19:36:46 +08004188 blk = blk_by_name(device);
4189 if (!blk) {
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004190 monitor_printf(mon, "Device not found %s\n", device);
4191 return -1;
4192 }
Max Reitz5433c242015-10-19 17:53:29 +02004193 if (!blk_bs(blk)) {
4194 monitor_printf(mon, "Device '%s' has no medium\n", device);
4195 return -1;
4196 }
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004197
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01004198 bdrv_add_key(blk_bs(blk), NULL, &err);
4199 if (err) {
4200 error_free(err);
4201 return monitor_read_bdrv_key_start(mon, blk_bs(blk), completion_cb, opaque);
4202 }
4203
4204 if (completion_cb) {
4205 completion_cb(opaque, 0);
4206 }
4207 return 0;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004208}
Paolo Bonzini4d454572012-11-26 16:03:42 +01004209
4210QemuOptsList qemu_mon_opts = {
4211 .name = "mon",
4212 .implied_opt_name = "chardev",
4213 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
4214 .desc = {
4215 {
4216 .name = "mode",
4217 .type = QEMU_OPT_STRING,
4218 },{
4219 .name = "chardev",
4220 .type = QEMU_OPT_STRING,
4221 },{
4222 .name = "default",
4223 .type = QEMU_OPT_BOOL,
4224 },{
4225 .name = "pretty",
4226 .type = QEMU_OPT_BOOL,
4227 },
4228 { /* end of list */ }
4229 },
4230};
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004231
4232#ifndef TARGET_I386
4233void qmp_rtc_reset_reinjection(Error **errp)
4234{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004235 error_setg(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection");
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004236}
4237#endif
Jason J. Herne7ee0c3e2015-06-26 14:03:16 -04004238
4239#ifndef TARGET_S390X
4240void qmp_dump_skeys(const char *filename, Error **errp)
4241{
4242 error_setg(errp, QERR_FEATURE_DISABLED, "dump-skeys");
4243}
4244#endif