blob: 894f862dd31f7359cb517c54cce15a206551544d [file] [log] [blame]
bellard9dc39cb2004-03-14 21:38:27 +00001/*
2 * QEMU monitor
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard9dc39cb2004-03-14 21:38:27 +00004 * Copyright (c) 2003-2004 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard9dc39cb2004-03-14 21:38:27 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
Peter Maydelld38ea872016-01-29 17:50:05 +000024#include "qemu/osdep.h"
blueswir1511d2b12009-03-07 15:32:56 +000025#include <dirent.h>
pbrook87ecb682007-11-17 17:14:51 +000026#include "hw/hw.h"
Paolo Bonzinib4a42f82013-02-04 11:37:52 +010027#include "monitor/qdev.h"
pbrook87ecb682007-11-17 17:14:51 +000028#include "hw/usb.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010029#include "hw/i386/pc.h"
Michael S. Tsirkina2cb15b2012-12-12 14:24:50 +020030#include "hw/pci/pci.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010031#include "sysemu/watchdog.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +020032#include "hw/loader.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010033#include "exec/gdbstub.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020034#include "net/net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000035#include "net/slirp.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020036#include "sysemu/char.h"
Gerd Hoffmann75721502010-10-07 12:22:54 +020037#include "ui/qemu-spice.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010038#include "sysemu/sysemu.h"
Eduardo Habkoste35704b2015-02-08 16:51:16 -020039#include "sysemu/numa.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010040#include "monitor/monitor.h"
Stefan Hajnoczi0150cd82013-11-14 11:54:15 +010041#include "qemu/readline.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010042#include "ui/console.h"
Gerd Hoffmannc751a742013-12-04 15:02:28 +010043#include "ui/input.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010044#include "sysemu/blockdev.h"
pbrook87ecb682007-11-17 17:14:51 +000045#include "audio/audio.h"
Paolo Bonzini76cad712012-10-24 11:12:21 +020046#include "disas/disas.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010047#include "sysemu/balloon.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010048#include "qemu/timer.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010049#include "migration/migration.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010050#include "sysemu/kvm.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010051#include "qemu/acl.h"
Paolo Bonzinibdee56f2013-04-02 18:28:41 +020052#include "sysemu/tpm.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010053#include "qapi/qmp/qerror.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010054#include "qapi/qmp/qint.h"
55#include "qapi/qmp/qfloat.h"
56#include "qapi/qmp/qlist.h"
57#include "qapi/qmp/qbool.h"
58#include "qapi/qmp/qstring.h"
59#include "qapi/qmp/qjson.h"
60#include "qapi/qmp/json-streamer.h"
61#include "qapi/qmp/json-parser.h"
Hani Benhabiles1094fd32014-02-06 23:30:13 +010062#include <qom/object_interfaces.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"
Alberto Garciadc599972016-03-10 13:55:26 +020079#include "sysemu/qtest.h"
ths6a5bd302007-12-03 17:05:38 +000080
Markus Armbruster1ce6be22015-02-06 14:18:24 +010081/* for hmp_info_irq/pic */
Jan Kiszka661f1922011-10-16 11:53:13 +020082#if defined(TARGET_SPARC)
Paolo Bonzini0d09e412013-02-05 17:06:20 +010083#include "hw/sparc/sun4m.h"
Jan Kiszka661f1922011-10-16 11:53:13 +020084#endif
Paolo Bonzini0d09e412013-02-05 17:06:20 +010085#include "hw/lm32/lm32_pic.h"
Jan Kiszka661f1922011-10-16 11:53:13 +020086
Jason J. Hernea4538a52015-06-26 14:07:21 -040087#if defined(TARGET_S390X)
88#include "hw/s390x/storage-keys.h"
89#endif
90
bellard9307c4c2004-04-04 12:57:25 +000091/*
92 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000093 *
bellard9307c4c2004-04-04 12:57:25 +000094 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000095 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000096 * 's' string (accept optional quote)
Wenchao Xia129be002013-08-27 20:38:26 +080097 * 'S' it just appends the rest of the string (accept optional quote)
Markus Armbruster361127d2010-02-10 20:24:35 +010098 * 'O' option string of the form NAME=VALUE,...
99 * parsed according to QemuOptsList given by its name
100 * Example: 'device:O' uses qemu_device_opts.
101 * Restriction: only lists with empty desc are supported
102 * TODO lift the restriction
bellard92a31b12005-02-10 22:00:52 +0000103 * 'i' 32 bit integer
104 * 'l' target long (32 or 64 bit)
Luiz Capitulino91162842012-04-26 17:34:30 -0300105 * 'M' Non-negative target long (32 or 64 bit), in user mode the
106 * value is multiplied by 2^20 (think Mebibyte)
Jes Sorensendbc0c672010-10-21 17:15:47 +0200107 * 'o' octets (aka bytes)
Kevin Wolf5e009842013-06-05 14:19:27 +0200108 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
109 * K, k suffix, which multiplies the value by 2^60 for suffixes E
110 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
111 * 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 +0100112 * 'T' double
113 * user mode accepts an optional ms, us, ns suffix,
114 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +0000115 * '/' optional gdb-like print format (like "/10x")
116 *
Luiz Capitulinofb466602009-08-28 15:27:27 -0300117 * '?' optional type (for all types, except '/')
118 * '.' other form of optional type (for 'i' and 'l')
Markus Armbruster942cd1f2010-03-26 09:07:09 +0100119 * 'b' boolean
120 * user mode accepts "on" or "off"
Luiz Capitulinofb466602009-08-28 15:27:27 -0300121 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +0000122 *
123 */
124
Anthony Liguoric227f092009-10-01 16:12:16 -0500125typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +0000126 const char *name;
bellard9307c4c2004-04-04 12:57:25 +0000127 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +0000128 const char *params;
129 const char *help;
Luiz Capitulino910df892009-10-07 13:41:51 -0300130 union {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -0300131 void (*cmd)(Monitor *mon, const QDict *qdict);
Markus Armbruster485febc2015-03-13 17:25:50 +0100132 void (*cmd_new)(QDict *params, QObject **ret_data, Error **errp);
Luiz Capitulino910df892009-10-07 13:41:51 -0300133 } mhandler;
Wenchao Xia5f3d3352013-01-14 14:06:27 +0800134 /* @sub_table is a list of 2nd level of commands. If it do not exist,
135 * mhandler should be used. If it exist, sub_table[?].mhandler should be
136 * used, and mhandler of 1st level plays the role of help function.
137 */
138 struct mon_cmd_t *sub_table;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +0100139 void (*command_completion)(ReadLineState *rs, int nb_args, const char *str);
Anthony Liguoric227f092009-10-01 16:12:16 -0500140} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000141
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100142/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500143typedef struct mon_fd_t mon_fd_t;
144struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100145 char *name;
146 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500147 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100148};
149
Corey Bryantba1c0482012-08-14 16:43:43 -0400150/* file descriptor associated with a file descriptor set */
151typedef struct MonFdsetFd MonFdsetFd;
152struct MonFdsetFd {
153 int fd;
154 bool removed;
155 char *opaque;
156 QLIST_ENTRY(MonFdsetFd) next;
157};
158
159/* file descriptor set containing fds passed via SCM_RIGHTS */
160typedef struct MonFdset MonFdset;
161struct MonFdset {
162 int64_t id;
163 QLIST_HEAD(, MonFdsetFd) fds;
Corey Bryantadb696f2012-08-14 16:43:47 -0400164 QLIST_HEAD(, MonFdsetFd) dup_fds;
Corey Bryantba1c0482012-08-14 16:43:43 -0400165 QLIST_ENTRY(MonFdset) next;
166};
167
Markus Armbruster74358f22015-03-06 19:35:59 +0100168typedef struct {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200169 QObject *id;
170 JSONMessageParser parser;
Markus Armbrusterf994b252015-03-06 19:51:51 +0100171 /*
172 * When a client connects, we're in capabilities negotiation mode.
173 * When command qmp_capabilities succeeds, we go into command
174 * mode.
175 */
176 bool in_command_mode; /* are we in command mode? */
Markus Armbruster74358f22015-03-06 19:35:59 +0100177} MonitorQMP;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200178
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100179/*
180 * To prevent flooding clients, events can be throttled. The
181 * throttling is calculated globally, rather than per-Monitor
182 * instance.
183 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200184typedef struct MonitorQAPIEventState {
Markus Armbruster7de0be62015-10-15 17:08:35 +0200185 QAPIEvent event; /* Throttling state for this event type and... */
186 QDict *data; /* ... data, see qapi_event_throttle_equal() */
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100187 QEMUTimer *timer; /* Timer for handling delayed events */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200188 QDict *qdict; /* Delayed event (if any) */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200189} MonitorQAPIEventState;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100190
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200191typedef struct {
192 int64_t rate; /* Minimum time (in ns) between two events */
193} MonitorQAPIEventConf;
194
aliguori87127162009-03-05 23:01:29 +0000195struct Monitor {
196 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200197 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000198 int flags;
199 int suspend_cnt;
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400200 bool skip_flush;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200201
202 QemuMutex out_lock;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400203 QString *outbuf;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200204 guint out_watch;
205
206 /* Read under either BQL or out_lock, written with BQL+out_lock. */
207 int mux_out;
208
aliguori731b0362009-03-05 23:01:42 +0000209 ReadLineState *rs;
Markus Armbruster74358f22015-03-06 19:35:59 +0100210 MonitorQMP qmp;
Andreas Färbercb446ec2013-05-01 14:24:52 +0200211 CPUState *mon_cpu;
Markus Armbruster097310b2014-10-07 13:59:15 +0200212 BlockCompletionFunc *password_completion_cb;
aliguori731b0362009-03-05 23:01:42 +0000213 void *password_opaque;
Wenchao Xia77172392013-08-27 20:38:20 +0800214 mon_cmd_t *cmd_table;
Anthony Liguoric227f092009-10-01 16:12:16 -0500215 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000216 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000217};
218
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300219/* QMP checker flags */
220#define QMP_ACCEPT_UNKNOWNS 1
221
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200222/* Protects mon_list, monitor_event_state. */
223static QemuMutex monitor_lock;
224
Blue Swirl72cf2d42009-09-12 07:36:22 +0000225static QLIST_HEAD(mon_list, Monitor) mon_list;
Corey Bryantba1c0482012-08-14 16:43:43 -0400226static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
Corey Bryantefb87c12012-08-14 16:43:48 -0400227static int mon_refcount;
bellard7e2515e2004-08-01 21:52:19 +0000228
Wayne Xia816f8922011-10-12 11:32:41 +0800229static mon_cmd_t mon_cmds[];
230static mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000231
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300232static const mon_cmd_t qmp_cmds[];
233
Markus Armbruster8631b602010-02-18 11:41:55 +0100234Monitor *cur_mon;
aliguori376253e2009-03-05 23:01:23 +0000235
Alberto Garciadc599972016-03-10 13:55:26 +0200236static QEMUClockType event_clock_type = QEMU_CLOCK_REALTIME;
237
Stefan Hajnoczic60bf332013-11-14 11:54:14 +0100238static void monitor_command_cb(void *opaque, const char *cmdline,
239 void *readline_opaque);
aliguori83ab7952008-08-19 14:44:22 +0000240
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100241/**
242 * Is @mon a QMP monitor?
243 */
244static inline bool monitor_is_qmp(const Monitor *mon)
Luiz Capitulino418173c2009-11-26 22:58:51 -0200245{
246 return (mon->flags & MONITOR_USE_CONTROL);
247}
248
Markus Armbruster489653b2015-03-06 20:01:05 +0100249/**
250 * Is the current monitor, if any, a QMP monitor?
251 */
252bool monitor_cur_is_qmp(void)
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100253{
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100254 return cur_mon && monitor_is_qmp(cur_mon);
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100255}
256
Anthony Liguori7060b472011-09-02 12:34:50 -0500257void monitor_read_command(Monitor *mon, int show_prompt)
aliguori731b0362009-03-05 23:01:42 +0000258{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200259 if (!mon->rs)
260 return;
261
aliguori731b0362009-03-05 23:01:42 +0000262 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
263 if (show_prompt)
264 readline_show_prompt(mon->rs);
265}
bellard6a00d602005-11-21 23:25:50 +0000266
Anthony Liguori7060b472011-09-02 12:34:50 -0500267int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
268 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000269{
Markus Armbrusterbcf5d192015-03-12 17:26:46 +0100270 if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000271 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
272 /* prompt is printed on return from the command handler */
273 return 0;
274 } else {
275 monitor_printf(mon, "terminal does not support password prompting\n");
276 return -ENOTTY;
277 }
aliguoribb5fc202009-03-05 23:01:15 +0000278}
279
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200280static void monitor_flush_locked(Monitor *mon);
281
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100282static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
283 void *opaque)
284{
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200285 Monitor *mon = opaque;
286
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200287 qemu_mutex_lock(&mon->out_lock);
288 mon->out_watch = 0;
289 monitor_flush_locked(mon);
290 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100291 return FALSE;
292}
293
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200294/* Called with mon->out_lock held. */
295static void monitor_flush_locked(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000296{
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100297 int rc;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400298 size_t len;
299 const char *buf;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100300
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400301 if (mon->skip_flush) {
302 return;
303 }
304
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400305 buf = qstring_get_str(mon->outbuf);
306 len = qstring_get_length(mon->outbuf);
307
Paolo Bonzinia4cc73d2013-05-31 14:00:27 +0200308 if (len && !mon->mux_out) {
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400309 rc = qemu_chr_fe_write(mon->chr, (const uint8_t *) buf, len);
Stratos Psomadakis056f49f2014-01-27 12:30:15 +0200310 if ((rc < 0 && errno != EAGAIN) || (rc == len)) {
311 /* all flushed or error */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400312 QDECREF(mon->outbuf);
313 mon->outbuf = qstring_new();
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100314 return;
315 }
316 if (rc > 0) {
317 /* partinal write */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400318 QString *tmp = qstring_from_str(buf + rc);
319 QDECREF(mon->outbuf);
320 mon->outbuf = tmp;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100321 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200322 if (mon->out_watch == 0) {
Roger Pau Monnee02bc6d2014-05-23 17:57:49 +0200323 mon->out_watch = qemu_chr_fe_add_watch(mon->chr, G_IO_OUT|G_IO_HUP,
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200324 monitor_unblocked, mon);
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200325 }
bellard7e2515e2004-08-01 21:52:19 +0000326 }
327}
328
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200329void monitor_flush(Monitor *mon)
330{
331 qemu_mutex_lock(&mon->out_lock);
332 monitor_flush_locked(mon);
333 qemu_mutex_unlock(&mon->out_lock);
334}
335
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400336/* flush at every end of line */
aliguori376253e2009-03-05 23:01:23 +0000337static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000338{
ths60fe76f2007-12-16 03:02:09 +0000339 char c;
aliguori731b0362009-03-05 23:01:42 +0000340
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200341 qemu_mutex_lock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000342 for(;;) {
343 c = *str++;
344 if (c == '\0')
345 break;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400346 if (c == '\n') {
347 qstring_append_chr(mon->outbuf, '\r');
348 }
349 qstring_append_chr(mon->outbuf, c);
350 if (c == '\n') {
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200351 monitor_flush_locked(mon);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400352 }
bellard7e2515e2004-08-01 21:52:19 +0000353 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200354 qemu_mutex_unlock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000355}
356
aliguori376253e2009-03-05 23:01:23 +0000357void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000358{
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400359 char *buf;
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200360
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200361 if (!mon)
362 return;
363
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100364 if (monitor_is_qmp(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200365 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200366 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200367
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400368 buf = g_strdup_vprintf(fmt, ap);
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200369 monitor_puts(mon, buf);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400370 g_free(buf);
bellard7e2515e2004-08-01 21:52:19 +0000371}
372
aliguori376253e2009-03-05 23:01:23 +0000373void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000374{
375 va_list ap;
376 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000377 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000378 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000379}
380
Pavel Butsykincaf15312015-09-22 16:18:17 +0300381int monitor_fprintf(FILE *stream, const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000382{
383 va_list ap;
384 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000385 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000386 va_end(ap);
387 return 0;
388}
389
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200390static void monitor_json_emitter(Monitor *mon, const QObject *data)
391{
392 QString *json;
393
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200394 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
395 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200396 assert(json != NULL);
397
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200398 qstring_append_chr(json, '\n');
399 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200400
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200401 QDECREF(json);
402}
403
Markus Armbruster710aec92015-03-06 11:28:00 +0100404static QDict *build_qmp_error_dict(Error *err)
Luiz Capitulinode253f12012-07-27 16:18:16 -0300405{
406 QObject *obj;
407
Markus Armbruster710aec92015-03-06 11:28:00 +0100408 obj = qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %s } }",
Eric Blakef22a28b2015-11-18 01:53:00 -0700409 QapiErrorClass_lookup[error_get_class(err)],
Markus Armbruster710aec92015-03-06 11:28:00 +0100410 error_get_pretty(err));
Luiz Capitulinode253f12012-07-27 16:18:16 -0300411
412 return qobject_to_qdict(obj);
413}
414
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100415static void monitor_protocol_emitter(Monitor *mon, QObject *data,
Markus Armbruster710aec92015-03-06 11:28:00 +0100416 Error *err)
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200417{
418 QDict *qmp;
419
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +0100420 trace_monitor_protocol_emitter(mon);
421
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100422 if (!err) {
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200423 /* success response */
Luiz Capitulinode253f12012-07-27 16:18:16 -0300424 qmp = qdict_new();
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200425 if (data) {
426 qobject_incref(data);
427 qdict_put_obj(qmp, "return", data);
428 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200429 /* return an empty QDict by default */
430 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200431 }
432 } else {
433 /* error response */
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100434 qmp = build_qmp_error_dict(err);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200435 }
436
Markus Armbruster74358f22015-03-06 19:35:59 +0100437 if (mon->qmp.id) {
438 qdict_put_obj(qmp, "id", mon->qmp.id);
439 mon->qmp.id = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200440 }
441
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200442 monitor_json_emitter(mon, QOBJECT(qmp));
443 QDECREF(qmp);
444}
445
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200446
Eric Blake7fb1cf12015-11-18 01:52:57 -0700447static MonitorQAPIEventConf monitor_qapi_event_conf[QAPI_EVENT__MAX] = {
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200448 /* Limit guest-triggerable events to 1 per second */
449 [QAPI_EVENT_RTC_CHANGE] = { 1000 * SCALE_MS },
450 [QAPI_EVENT_WATCHDOG] = { 1000 * SCALE_MS },
451 [QAPI_EVENT_BALLOON_CHANGE] = { 1000 * SCALE_MS },
452 [QAPI_EVENT_QUORUM_REPORT_BAD] = { 1000 * SCALE_MS },
453 [QAPI_EVENT_QUORUM_FAILURE] = { 1000 * SCALE_MS },
454 [QAPI_EVENT_VSERPORT_CHANGE] = { 1000 * SCALE_MS },
455};
456
Markus Armbrustera24712a2015-10-15 17:08:34 +0200457GHashTable *monitor_qapi_event_state;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100458
459/*
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200460 * Emits the event to every monitor instance, @event is only used for trace
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200461 * Called with monitor_lock held.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100462 */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200463static void monitor_qapi_event_emit(QAPIEvent event, QDict *qdict)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100464{
465 Monitor *mon;
466
Markus Armbruster688b4b72015-10-15 17:08:30 +0200467 trace_monitor_protocol_event_emit(event, qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100468 QLIST_FOREACH(mon, &mon_list, entry) {
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100469 if (monitor_is_qmp(mon) && mon->qmp.in_command_mode) {
Markus Armbruster688b4b72015-10-15 17:08:30 +0200470 monitor_json_emitter(mon, QOBJECT(qdict));
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100471 }
472 }
473}
474
Markus Armbrustera24712a2015-10-15 17:08:34 +0200475static void monitor_qapi_event_handler(void *opaque);
476
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100477/*
478 * Queue a new event for emission to Monitor instances,
479 * applying any rate limiting if required.
480 */
481static void
Markus Armbruster688b4b72015-10-15 17:08:30 +0200482monitor_qapi_event_queue(QAPIEvent event, QDict *qdict, Error **errp)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100483{
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200484 MonitorQAPIEventConf *evconf;
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200485 MonitorQAPIEventState *evstate;
Markus Armbruster93f8f982015-10-15 17:08:31 +0200486
Eric Blake7fb1cf12015-11-18 01:52:57 -0700487 assert(event < QAPI_EVENT__MAX);
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200488 evconf = &monitor_qapi_event_conf[event];
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200489 trace_monitor_protocol_event_queue(event, qdict, evconf->rate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100490
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200491 qemu_mutex_lock(&monitor_lock);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200492
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200493 if (!evconf->rate) {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200494 /* Unthrottled event */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200495 monitor_qapi_event_emit(event, qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100496 } else {
Markus Armbruster7de0be62015-10-15 17:08:35 +0200497 QDict *data = qobject_to_qdict(qdict_get(qdict, "data"));
498 MonitorQAPIEventState key = { .event = event, .data = data };
Markus Armbrustera24712a2015-10-15 17:08:34 +0200499
500 evstate = g_hash_table_lookup(monitor_qapi_event_state, &key);
501 assert(!evstate || timer_pending(evstate->timer));
502
503 if (evstate) {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200504 /*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200505 * Timer is pending for (at least) evconf->rate ns after
Markus Armbruster93f8f982015-10-15 17:08:31 +0200506 * last send. Store event for sending when timer fires,
507 * replacing a prior stored event if any.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100508 */
Markus Armbruster93f8f982015-10-15 17:08:31 +0200509 QDECREF(evstate->qdict);
Markus Armbruster688b4b72015-10-15 17:08:30 +0200510 evstate->qdict = qdict;
511 QINCREF(evstate->qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100512 } else {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200513 /*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200514 * Last send was (at least) evconf->rate ns ago.
Markus Armbruster93f8f982015-10-15 17:08:31 +0200515 * Send immediately, and arm the timer to call
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200516 * monitor_qapi_event_handler() in evconf->rate ns. Any
Markus Armbruster93f8f982015-10-15 17:08:31 +0200517 * events arriving before then will be delayed until then.
518 */
Alberto Garciadc599972016-03-10 13:55:26 +0200519 int64_t now = qemu_clock_get_ns(event_clock_type);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200520
Markus Armbruster688b4b72015-10-15 17:08:30 +0200521 monitor_qapi_event_emit(event, qdict);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200522
523 evstate = g_new(MonitorQAPIEventState, 1);
524 evstate->event = event;
Markus Armbruster7de0be62015-10-15 17:08:35 +0200525 evstate->data = data;
526 QINCREF(evstate->data);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200527 evstate->qdict = NULL;
Alberto Garciadc599972016-03-10 13:55:26 +0200528 evstate->timer = timer_new_ns(event_clock_type,
Markus Armbrustera24712a2015-10-15 17:08:34 +0200529 monitor_qapi_event_handler,
530 evstate);
531 g_hash_table_add(monitor_qapi_event_state, evstate);
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200532 timer_mod_ns(evstate->timer, now + evconf->rate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100533 }
534 }
Markus Armbruster93f8f982015-10-15 17:08:31 +0200535
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200536 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100537}
538
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100539/*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200540 * This function runs evconf->rate ns after sending a throttled
Markus Armbruster93f8f982015-10-15 17:08:31 +0200541 * event.
542 * If another event has since been stored, send it.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100543 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200544static void monitor_qapi_event_handler(void *opaque)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100545{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200546 MonitorQAPIEventState *evstate = opaque;
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200547 MonitorQAPIEventConf *evconf = &monitor_qapi_event_conf[evstate->event];
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100548
Markus Armbruster93f8f982015-10-15 17:08:31 +0200549 trace_monitor_protocol_event_handler(evstate->event, evstate->qdict);
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200550 qemu_mutex_lock(&monitor_lock);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200551
Markus Armbruster688b4b72015-10-15 17:08:30 +0200552 if (evstate->qdict) {
Alberto Garciadc599972016-03-10 13:55:26 +0200553 int64_t now = qemu_clock_get_ns(event_clock_type);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200554
Markus Armbruster688b4b72015-10-15 17:08:30 +0200555 monitor_qapi_event_emit(evstate->event, evstate->qdict);
556 QDECREF(evstate->qdict);
557 evstate->qdict = NULL;
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200558 timer_mod_ns(evstate->timer, now + evconf->rate);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200559 } else {
560 g_hash_table_remove(monitor_qapi_event_state, evstate);
Markus Armbruster7de0be62015-10-15 17:08:35 +0200561 QDECREF(evstate->data);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200562 timer_free(evstate->timer);
563 g_free(evstate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100564 }
Markus Armbruster93f8f982015-10-15 17:08:31 +0200565
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200566 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100567}
568
Markus Armbrustera24712a2015-10-15 17:08:34 +0200569static unsigned int qapi_event_throttle_hash(const void *key)
570{
571 const MonitorQAPIEventState *evstate = key;
Markus Armbruster7de0be62015-10-15 17:08:35 +0200572 unsigned int hash = evstate->event * 255;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200573
Markus Armbruster7de0be62015-10-15 17:08:35 +0200574 if (evstate->event == QAPI_EVENT_VSERPORT_CHANGE) {
575 hash += g_str_hash(qdict_get_str(evstate->data, "id"));
576 }
577
Alberto Garcia6d425eb2016-03-10 13:55:25 +0200578 if (evstate->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
579 hash += g_str_hash(qdict_get_str(evstate->data, "node-name"));
580 }
581
Markus Armbruster7de0be62015-10-15 17:08:35 +0200582 return hash;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200583}
584
585static gboolean qapi_event_throttle_equal(const void *a, const void *b)
586{
587 const MonitorQAPIEventState *eva = a;
588 const MonitorQAPIEventState *evb = b;
589
Markus Armbruster7de0be62015-10-15 17:08:35 +0200590 if (eva->event != evb->event) {
591 return FALSE;
592 }
593
594 if (eva->event == QAPI_EVENT_VSERPORT_CHANGE) {
595 return !strcmp(qdict_get_str(eva->data, "id"),
596 qdict_get_str(evb->data, "id"));
597 }
598
Alberto Garcia6d425eb2016-03-10 13:55:25 +0200599 if (eva->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
600 return !strcmp(qdict_get_str(eva->data, "node-name"),
601 qdict_get_str(evb->data, "node-name"));
602 }
603
Markus Armbruster7de0be62015-10-15 17:08:35 +0200604 return TRUE;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200605}
606
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200607static void monitor_qapi_event_init(void)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100608{
Alberto Garciadc599972016-03-10 13:55:26 +0200609 if (qtest_enabled()) {
610 event_clock_type = QEMU_CLOCK_VIRTUAL;
611 }
612
Markus Armbrustera24712a2015-10-15 17:08:34 +0200613 monitor_qapi_event_state = g_hash_table_new(qapi_event_throttle_hash,
614 qapi_event_throttle_equal);
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200615 qmp_event_set_func_emit(monitor_qapi_event_queue);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200616}
617
Markus Armbruster485febc2015-03-13 17:25:50 +0100618static void qmp_capabilities(QDict *params, QObject **ret_data, Error **errp)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200619{
Markus Armbruster485febc2015-03-13 17:25:50 +0100620 cur_mon->qmp.in_command_mode = true;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200621}
622
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100623static void handle_hmp_command(Monitor *mon, const char *cmdline);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200624
Wenchao Xiab01fe892013-08-27 20:38:19 +0800625static void monitor_data_init(Monitor *mon)
626{
627 memset(mon, 0, sizeof(Monitor));
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200628 qemu_mutex_init(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800629 mon->outbuf = qstring_new();
Wenchao Xia77172392013-08-27 20:38:20 +0800630 /* Use *mon_cmds by default. */
631 mon->cmd_table = mon_cmds;
Wenchao Xiab01fe892013-08-27 20:38:19 +0800632}
633
634static void monitor_data_destroy(Monitor *mon)
635{
636 QDECREF(mon->outbuf);
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200637 qemu_mutex_destroy(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800638}
639
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200640char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
641 int64_t cpu_index, Error **errp)
Luiz Capitulino0268d972010-10-22 10:08:02 -0200642{
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200643 char *output = NULL;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200644 Monitor *old_mon, hmp;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200645
Wenchao Xiab01fe892013-08-27 20:38:19 +0800646 monitor_data_init(&hmp);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400647 hmp.skip_flush = true;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200648
649 old_mon = cur_mon;
650 cur_mon = &hmp;
651
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200652 if (has_cpu_index) {
653 int ret = monitor_set_cpu(cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200654 if (ret < 0) {
655 cur_mon = old_mon;
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100656 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
657 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200658 goto out;
659 }
660 }
661
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100662 handle_hmp_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200663 cur_mon = old_mon;
664
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200665 qemu_mutex_lock(&hmp.out_lock);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400666 if (qstring_get_length(hmp.outbuf) > 0) {
667 output = g_strdup(qstring_get_str(hmp.outbuf));
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200668 } else {
669 output = g_strdup("");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200670 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200671 qemu_mutex_unlock(&hmp.out_lock);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200672
673out:
Wenchao Xiab01fe892013-08-27 20:38:19 +0800674 monitor_data_destroy(&hmp);
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200675 return output;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200676}
677
bellard9dc39cb2004-03-14 21:38:27 +0000678static int compare_cmd(const char *name, const char *list)
679{
680 const char *p, *pstart;
681 int len;
682 len = strlen(name);
683 p = list;
684 for(;;) {
685 pstart = p;
686 p = strchr(p, '|');
687 if (!p)
688 p = pstart + strlen(pstart);
689 if ((p - pstart) == len && !memcmp(pstart, name, len))
690 return 1;
691 if (*p == '\0')
692 break;
693 p++;
694 }
695 return 0;
696}
697
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800698static int get_str(char *buf, int buf_size, const char **pp)
699{
700 const char *p;
701 char *q;
702 int c;
703
704 q = buf;
705 p = *pp;
706 while (qemu_isspace(*p)) {
707 p++;
708 }
709 if (*p == '\0') {
710 fail:
711 *q = '\0';
712 *pp = p;
713 return -1;
714 }
715 if (*p == '\"') {
716 p++;
717 while (*p != '\0' && *p != '\"') {
718 if (*p == '\\') {
719 p++;
720 c = *p++;
721 switch (c) {
722 case 'n':
723 c = '\n';
724 break;
725 case 'r':
726 c = '\r';
727 break;
728 case '\\':
729 case '\'':
730 case '\"':
731 break;
732 default:
Peter Maydell71baf782015-08-19 16:20:19 +0100733 printf("unsupported escape code: '\\%c'\n", c);
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800734 goto fail;
735 }
736 if ((q - buf) < buf_size - 1) {
737 *q++ = c;
738 }
739 } else {
740 if ((q - buf) < buf_size - 1) {
741 *q++ = *p;
742 }
743 p++;
744 }
745 }
746 if (*p != '\"') {
Peter Maydell71baf782015-08-19 16:20:19 +0100747 printf("unterminated string\n");
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800748 goto fail;
749 }
750 p++;
751 } else {
752 while (*p != '\0' && !qemu_isspace(*p)) {
753 if ((q - buf) < buf_size - 1) {
754 *q++ = *p;
755 }
756 p++;
757 }
758 }
759 *q = '\0';
760 *pp = p;
761 return 0;
762}
763
764#define MAX_ARGS 16
765
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800766static void free_cmdline_args(char **args, int nb_args)
767{
768 int i;
769
770 assert(nb_args <= MAX_ARGS);
771
772 for (i = 0; i < nb_args; i++) {
773 g_free(args[i]);
774 }
775
776}
777
778/*
779 * Parse the command line to get valid args.
780 * @cmdline: command line to be parsed.
781 * @pnb_args: location to store the number of args, must NOT be NULL.
782 * @args: location to store the args, which should be freed by caller, must
783 * NOT be NULL.
784 *
785 * Returns 0 on success, negative on failure.
786 *
787 * NOTE: this parser is an approximate form of the real command parser. Number
788 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
789 * return with failure.
790 */
791static int parse_cmdline(const char *cmdline,
792 int *pnb_args, char **args)
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800793{
794 const char *p;
795 int nb_args, ret;
796 char buf[1024];
797
798 p = cmdline;
799 nb_args = 0;
800 for (;;) {
801 while (qemu_isspace(*p)) {
802 p++;
803 }
804 if (*p == '\0') {
805 break;
806 }
807 if (nb_args >= MAX_ARGS) {
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800808 goto fail;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800809 }
810 ret = get_str(buf, sizeof(buf), &p);
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800811 if (ret < 0) {
812 goto fail;
813 }
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800814 args[nb_args] = g_strdup(buf);
815 nb_args++;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800816 }
817 *pnb_args = nb_args;
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800818 return 0;
819
820 fail:
821 free_cmdline_args(args, nb_args);
822 return -1;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800823}
824
Wenchao Xia66855492013-08-27 20:38:23 +0800825static void help_cmd_dump_one(Monitor *mon,
826 const mon_cmd_t *cmd,
827 char **prefix_args,
828 int prefix_args_nb)
829{
830 int i;
831
832 for (i = 0; i < prefix_args_nb; i++) {
833 monitor_printf(mon, "%s ", prefix_args[i]);
834 }
835 monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
836}
837
838/* @args[@arg_index] is the valid command need to find in @cmds */
Anthony Liguoric227f092009-10-01 16:12:16 -0500839static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
Wenchao Xia66855492013-08-27 20:38:23 +0800840 char **args, int nb_args, int arg_index)
bellard9dc39cb2004-03-14 21:38:27 +0000841{
Anthony Liguoric227f092009-10-01 16:12:16 -0500842 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000843
Wenchao Xia66855492013-08-27 20:38:23 +0800844 /* No valid arg need to compare with, dump all in *cmds */
845 if (arg_index >= nb_args) {
846 for (cmd = cmds; cmd->name != NULL; cmd++) {
847 help_cmd_dump_one(mon, cmd, args, arg_index);
848 }
849 return;
850 }
851
852 /* Find one entry to dump */
853 for (cmd = cmds; cmd->name != NULL; cmd++) {
854 if (compare_cmd(args[arg_index], cmd->name)) {
855 if (cmd->sub_table) {
856 /* continue with next arg */
857 help_cmd_dump(mon, cmd->sub_table,
858 args, nb_args, arg_index + 1);
859 } else {
860 help_cmd_dump_one(mon, cmd, args, arg_index);
861 }
862 break;
863 }
bellard9dc39cb2004-03-14 21:38:27 +0000864 }
865}
866
aliguori376253e2009-03-05 23:01:23 +0000867static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000868{
Wenchao Xia66855492013-08-27 20:38:23 +0800869 char *args[MAX_ARGS];
870 int nb_args = 0;
871
872 /* 1. parse user input */
873 if (name) {
874 /* special case for log, directly dump and return */
875 if (!strcmp(name, "log")) {
Peter Maydell38dad9e2013-02-11 16:41:25 +0000876 const QEMULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000877 monitor_printf(mon, "Log items (comma separated):\n");
878 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
Peter Maydell38dad9e2013-02-11 16:41:25 +0000879 for (item = qemu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000880 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000881 }
Wenchao Xia66855492013-08-27 20:38:23 +0800882 return;
883 }
884
885 if (parse_cmdline(name, &nb_args, args) < 0) {
886 return;
bellardf193c792004-03-21 17:06:25 +0000887 }
bellard9dc39cb2004-03-14 21:38:27 +0000888 }
Wenchao Xia66855492013-08-27 20:38:23 +0800889
890 /* 2. dump the contents according to parsed args */
891 help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0);
892
893 free_cmdline_args(args, nb_args);
bellard9dc39cb2004-03-14 21:38:27 +0000894}
895
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300896static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300897{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300898 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300899}
900
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100901static void hmp_trace_event(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530902{
903 const char *tp_name = qdict_get_str(qdict, "name");
904 bool new_state = qdict_get_bool(qdict, "option");
Lluís Vilanova14101d02014-08-25 13:20:03 +0200905 Error *local_err = NULL;
Blue Swirlf871d682010-10-13 19:14:29 +0000906
Lluís Vilanova14101d02014-08-25 13:20:03 +0200907 qmp_trace_event_set_state(tp_name, new_state, true, true, &local_err);
908 if (local_err) {
Markus Armbruster091e38b2015-02-10 15:15:43 +0100909 error_report_err(local_err);
Blue Swirlf871d682010-10-13 19:14:29 +0000910 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530911}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100912
Michael Rothc45a8162011-10-02 08:44:37 -0500913#ifdef CONFIG_TRACE_SIMPLE
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100914static void hmp_trace_file(Monitor *mon, const QDict *qdict)
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100915{
916 const char *op = qdict_get_try_str(qdict, "op");
917 const char *arg = qdict_get_try_str(qdict, "arg");
918
919 if (!op) {
920 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
921 } else if (!strcmp(op, "on")) {
922 st_set_trace_file_enabled(true);
923 } else if (!strcmp(op, "off")) {
924 st_set_trace_file_enabled(false);
925 } else if (!strcmp(op, "flush")) {
926 st_flush_trace_buffer();
927 } else if (!strcmp(op, "set")) {
928 if (arg) {
929 st_set_trace_file(arg);
930 }
931 } else {
932 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
933 help_cmd(mon, "trace-file");
934 }
935}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530936#endif
937
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100938static void hmp_info_help(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000939{
Luiz Capitulino13c74252009-10-07 13:41:55 -0300940 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000941}
942
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300943CommandInfoList *qmp_query_commands(Error **errp)
bellard9bc9d1c2004-10-10 15:15:51 +0000944{
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300945 CommandInfoList *info, *cmd_list = NULL;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200946 const mon_cmd_t *cmd;
947
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300948 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino40e5a012011-10-21 16:15:31 -0200949 info = g_malloc0(sizeof(*info));
950 info->value = g_malloc0(sizeof(*info->value));
951 info->value->name = g_strdup(cmd->name);
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200952
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300953 info->next = cmd_list;
954 cmd_list = info;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200955 }
956
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300957 return cmd_list;
thsa36e69d2007-12-02 05:18:19 +0000958}
959
Daniel P. Berrange48608532012-05-21 17:59:51 +0100960EventInfoList *qmp_query_events(Error **errp)
961{
962 EventInfoList *info, *ev_list = NULL;
Wenchao Xia75175172014-06-18 08:43:54 +0200963 QAPIEvent e;
Daniel P. Berrange48608532012-05-21 17:59:51 +0100964
Eric Blake7fb1cf12015-11-18 01:52:57 -0700965 for (e = 0 ; e < QAPI_EVENT__MAX ; e++) {
Wenchao Xia75175172014-06-18 08:43:54 +0200966 const char *event_name = QAPIEvent_lookup[e];
Daniel P. Berrange48608532012-05-21 17:59:51 +0100967 assert(event_name != NULL);
968 info = g_malloc0(sizeof(*info));
969 info->value = g_malloc0(sizeof(*info->value));
970 info->value->name = g_strdup(event_name);
971
972 info->next = ev_list;
973 ev_list = info;
974 }
975
976 return ev_list;
977}
978
Markus Armbruster39a18152015-09-16 13:06:28 +0200979/*
980 * Minor hack: generated marshalling suppressed for this command
981 * ('gen': false in the schema) so we can parse the JSON string
982 * directly into QObject instead of first parsing it with
983 * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
984 * to QObject with generated output marshallers, every time. Instead,
985 * we do it in test-qmp-input-visitor.c, just to make sure
986 * qapi-introspect.py's output actually conforms to the schema.
987 */
988static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
989 Error **errp)
990{
991 *ret_data = qobject_from_json(qmp_schema_json);
992}
993
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300994/* set the current CPU defined by the user */
995int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000996{
Andreas Färber55e5c282012-12-17 06:18:02 +0100997 CPUState *cpu;
bellard6a00d602005-11-21 23:25:50 +0000998
Andreas Färber1c8bb3c2013-02-15 17:01:09 +0100999 cpu = qemu_get_cpu(cpu_index);
1000 if (cpu == NULL) {
1001 return -1;
bellard6a00d602005-11-21 23:25:50 +00001002 }
Andreas Färbercb446ec2013-05-01 14:24:52 +02001003 cur_mon->mon_cpu = cpu;
Andreas Färber1c8bb3c2013-02-15 17:01:09 +01001004 return 0;
bellard6a00d602005-11-21 23:25:50 +00001005}
1006
Pavel Butsykincaf15312015-09-22 16:18:17 +03001007CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +00001008{
aliguori731b0362009-03-05 23:01:42 +00001009 if (!cur_mon->mon_cpu) {
Luiz Capitulinob025c8b2011-10-06 14:02:57 -03001010 monitor_set_cpu(0);
bellard6a00d602005-11-21 23:25:50 +00001011 }
Avi Kivity4c0960c2009-08-17 23:19:53 +03001012 cpu_synchronize_state(cur_mon->mon_cpu);
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001013 return cur_mon->mon_cpu;
1014}
1015
Pavel Butsykinbf957282015-09-10 18:38:59 +03001016CPUArchState *mon_get_cpu_env(void)
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001017{
1018 return mon_get_cpu()->env_ptr;
bellard6a00d602005-11-21 23:25:50 +00001019}
1020
Luiz Capitulino99b77962011-10-24 10:53:44 -02001021int monitor_get_cpu_index(void)
1022{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001023 return mon_get_cpu()->cpu_index;
Luiz Capitulino99b77962011-10-24 10:53:44 -02001024}
1025
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001026static void hmp_info_registers(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001027{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001028 cpu_dump_state(mon_get_cpu(), (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +00001029}
1030
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001031static void hmp_info_jit(Monitor *mon, const QDict *qdict)
bellarde3db7222005-01-26 22:00:47 +00001032{
aliguori376253e2009-03-05 23:01:23 +00001033 dump_exec_info((FILE *)mon, monitor_fprintf);
Sebastian Tanase27498be2014-07-25 11:56:33 +02001034 dump_drift_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +00001035}
1036
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001037static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
Max Filippov246ae242014-11-02 11:04:18 +03001038{
1039 dump_opcount_info((FILE *)mon, monitor_fprintf);
1040}
1041
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001042static void hmp_info_history(Monitor *mon, const QDict *qdict)
bellardaa455482004-04-04 13:07:25 +00001043{
1044 int i;
bellard7e2515e2004-08-01 21:52:19 +00001045 const char *str;
ths3b46e622007-09-17 08:09:54 +00001046
aliguoricde76ee2009-03-05 23:01:51 +00001047 if (!mon->rs)
1048 return;
bellard7e2515e2004-08-01 21:52:19 +00001049 i = 0;
1050 for(;;) {
aliguori731b0362009-03-05 23:01:42 +00001051 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +00001052 if (!str)
1053 break;
aliguori376253e2009-03-05 23:01:23 +00001054 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +00001055 i++;
bellardaa455482004-04-04 13:07:25 +00001056 }
1057}
1058
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001059static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
j_mayer76a66252007-03-07 08:32:30 +00001060{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001061 cpu_dump_statistics(mon_get_cpu(), (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +00001062}
j_mayer76a66252007-03-07 08:32:30 +00001063
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001064static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301065{
Lluís Vilanova14101d02014-08-25 13:20:03 +02001066 TraceEventInfoList *events = qmp_trace_event_get_state("*", NULL);
1067 TraceEventInfoList *elem;
1068
1069 for (elem = events; elem != NULL; elem = elem->next) {
1070 monitor_printf(mon, "%s : state %u\n",
1071 elem->value->name,
1072 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
1073 }
1074 qapi_free_TraceEventInfoList(events);
Prerna Saxena22890ab2010-06-24 17:04:53 +05301075}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301076
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001077void qmp_client_migrate_info(const char *protocol, const char *hostname,
1078 bool has_port, int64_t port,
1079 bool has_tls_port, int64_t tls_port,
1080 bool has_cert_subject, const char *cert_subject,
1081 Error **errp)
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001082{
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001083 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001084 if (!qemu_using_spice(errp)) {
1085 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001086 }
1087
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001088 if (!has_port && !has_tls_port) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001089 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001090 return;
Yonit Halperin6ec5dae2012-03-18 09:42:39 +02001091 }
1092
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001093 if (qemu_spice_migrate_info(hostname,
1094 has_port ? port : -1,
1095 has_tls_port ? tls_port : -1,
1096 cert_subject)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001097 error_setg(errp, QERR_UNDEFINED_ERROR);
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001098 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001099 }
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001100 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001101 }
1102
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001103 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001104}
1105
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001106static void hmp_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001107{
Peter Maydell9a7e5422013-02-11 16:41:20 +00001108 qemu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001109}
1110
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001111static void hmp_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001112{
1113 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001114 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001115
bellard9307c4c2004-04-04 12:57:25 +00001116 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001117 mask = 0;
1118 } else {
Peter Maydell4fde1eb2013-02-11 16:41:22 +00001119 mask = qemu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001120 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001121 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001122 return;
1123 }
1124 }
Peter Maydell24537a02013-02-11 16:41:23 +00001125 qemu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00001126}
1127
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001128static void hmp_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001129{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001130 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001131 if (!option || !strcmp(option, "on")) {
1132 singlestep = 1;
1133 } else if (!strcmp(option, "off")) {
1134 singlestep = 0;
1135 } else {
1136 monitor_printf(mon, "unexpected option %s\n", option);
1137 }
1138}
1139
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001140static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001141{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001142 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001143 if (!device)
1144 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1145 if (gdbserver_start(device) < 0) {
1146 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1147 device);
1148 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001149 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001150 } else {
aliguori59030a82009-04-05 18:43:41 +00001151 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1152 device);
bellard8a7ddc32004-03-31 19:00:16 +00001153 }
1154}
1155
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001156static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001157{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001158 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001159 if (select_watchdog_action(action) == -1) {
1160 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1161 }
1162}
1163
aliguori376253e2009-03-05 23:01:23 +00001164static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001165{
aliguori376253e2009-03-05 23:01:23 +00001166 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001167 switch(c) {
1168 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001169 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001170 break;
1171 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001172 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001173 break;
1174 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001175 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001176 break;
1177 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001178 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001179 break;
1180 default:
1181 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001182 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001183 } else {
aliguori376253e2009-03-05 23:01:23 +00001184 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001185 }
1186 break;
1187 }
aliguori376253e2009-03-05 23:01:23 +00001188 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001189}
1190
aliguori376253e2009-03-05 23:01:23 +00001191static void memory_dump(Monitor *mon, int count, int format, int wsize,
Avi Kivitya8170e52012-10-23 12:30:10 +02001192 hwaddr addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001193{
Blue Swirl23842aa2010-01-12 20:27:43 +00001194 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001195 uint8_t buf[16];
1196 uint64_t v;
1197
1198 if (format == 'i') {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001199 int flags = 0;
bellard9307c4c2004-04-04 12:57:25 +00001200#ifdef TARGET_I386
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001201 CPUArchState *env = mon_get_cpu_env();
bellard4c27ba22004-04-25 18:05:08 +00001202 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001203 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001204 } else if (wsize == 4) {
1205 flags = 0;
1206 } else {
bellard6a15fd12006-04-12 21:07:07 +00001207 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001208 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001209 if (env) {
1210#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001211 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001212 (env->segs[R_CS].flags & DESC_L_MASK))
1213 flags = 2;
1214 else
1215#endif
1216 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1217 flags = 1;
1218 }
bellard4c27ba22004-04-25 18:05:08 +00001219 }
1220#endif
Tom Musta1c38f842014-04-09 14:53:24 -05001221#ifdef TARGET_PPC
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001222 CPUArchState *env = mon_get_cpu_env();
Tom Musta1c38f842014-04-09 14:53:24 -05001223 flags = msr_le << 16;
1224 flags |= env->bfd_mach;
1225#endif
Peter Crosthwaited49190c2015-05-24 14:20:41 -07001226 monitor_disas(mon, mon_get_cpu(), addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001227 return;
1228 }
1229
1230 len = wsize * count;
1231 if (wsize == 1)
1232 line_size = 8;
1233 else
1234 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001235 max_digits = 0;
1236
1237 switch(format) {
1238 case 'o':
1239 max_digits = (wsize * 8 + 2) / 3;
1240 break;
1241 default:
1242 case 'x':
1243 max_digits = (wsize * 8) / 4;
1244 break;
1245 case 'u':
1246 case 'd':
1247 max_digits = (wsize * 8 * 10 + 32) / 33;
1248 break;
1249 case 'c':
1250 wsize = 1;
1251 break;
1252 }
1253
1254 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001255 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001256 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001257 else
aliguori376253e2009-03-05 23:01:23 +00001258 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001259 l = len;
1260 if (l > line_size)
1261 l = line_size;
1262 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001263 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001264 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001265 if (cpu_memory_rw_debug(mon_get_cpu(), addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001266 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001267 break;
1268 }
bellard9307c4c2004-04-04 12:57:25 +00001269 }
ths5fafdf22007-09-16 21:08:06 +00001270 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001271 while (i < l) {
1272 switch(wsize) {
1273 default:
1274 case 1:
Peter Maydell24e60302015-01-20 15:19:32 +00001275 v = ldub_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001276 break;
1277 case 2:
Peter Maydell24e60302015-01-20 15:19:32 +00001278 v = lduw_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001279 break;
1280 case 4:
Peter Maydell24e60302015-01-20 15:19:32 +00001281 v = (uint32_t)ldl_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001282 break;
1283 case 8:
Peter Maydell24e60302015-01-20 15:19:32 +00001284 v = ldq_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001285 break;
1286 }
aliguori376253e2009-03-05 23:01:23 +00001287 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001288 switch(format) {
1289 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001290 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001291 break;
1292 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001293 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001294 break;
1295 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001296 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001297 break;
1298 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001299 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001300 break;
1301 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001302 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001303 break;
1304 }
1305 i += wsize;
1306 }
aliguori376253e2009-03-05 23:01:23 +00001307 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001308 addr += l;
1309 len -= l;
1310 }
1311}
1312
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001313static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001314{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001315 int count = qdict_get_int(qdict, "count");
1316 int format = qdict_get_int(qdict, "format");
1317 int size = qdict_get_int(qdict, "size");
1318 target_long addr = qdict_get_int(qdict, "addr");
1319
aliguori376253e2009-03-05 23:01:23 +00001320 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001321}
1322
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001323static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001324{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001325 int count = qdict_get_int(qdict, "count");
1326 int format = qdict_get_int(qdict, "format");
1327 int size = qdict_get_int(qdict, "size");
Avi Kivitya8170e52012-10-23 12:30:10 +02001328 hwaddr addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001329
aliguori376253e2009-03-05 23:01:23 +00001330 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001331}
1332
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001333static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001334{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001335 int format = qdict_get_int(qdict, "format");
Avi Kivitya8170e52012-10-23 12:30:10 +02001336 hwaddr val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001337
bellard9307c4c2004-04-04 12:57:25 +00001338 switch(format) {
1339 case 'o':
Avi Kivitya8170e52012-10-23 12:30:10 +02001340 monitor_printf(mon, "%#" HWADDR_PRIo, val);
bellard9307c4c2004-04-04 12:57:25 +00001341 break;
1342 case 'x':
Avi Kivitya8170e52012-10-23 12:30:10 +02001343 monitor_printf(mon, "%#" HWADDR_PRIx, val);
bellard9307c4c2004-04-04 12:57:25 +00001344 break;
1345 case 'u':
Avi Kivitya8170e52012-10-23 12:30:10 +02001346 monitor_printf(mon, "%" HWADDR_PRIu, val);
bellard9307c4c2004-04-04 12:57:25 +00001347 break;
1348 default:
1349 case 'd':
Avi Kivitya8170e52012-10-23 12:30:10 +02001350 monitor_printf(mon, "%" HWADDR_PRId, val);
bellard9307c4c2004-04-04 12:57:25 +00001351 break;
1352 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001353 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001354 break;
1355 }
aliguori376253e2009-03-05 23:01:23 +00001356 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001357}
1358
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001359static void hmp_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001360{
1361 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001362 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001363 uint32_t start = qdict_get_int(qdict, "start");
1364 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001365
1366 sum = 0;
1367 for(addr = start; addr < (start + size); addr++) {
Peter Maydell42874d32015-04-26 16:49:24 +01001368 uint8_t val = address_space_ldub(&address_space_memory, addr,
1369 MEMTXATTRS_UNSPECIFIED, NULL);
bellarde4cf1ad2005-06-04 20:15:57 +00001370 /* BSD sum algorithm ('sum' Unix command) */
1371 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001372 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001373 }
aliguori376253e2009-03-05 23:01:23 +00001374 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001375}
1376
bellard13224a82006-07-14 22:03:35 +00001377static int mouse_button_state;
1378
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001379static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001380{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001381 int dx, dy, dz, button;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001382 const char *dx_str = qdict_get_str(qdict, "dx_str");
1383 const char *dy_str = qdict_get_str(qdict, "dy_str");
1384 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001385
bellard13224a82006-07-14 22:03:35 +00001386 dx = strtol(dx_str, NULL, 0);
1387 dy = strtol(dy_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001388 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
1389 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
1390
1391 if (dz_str) {
bellard13224a82006-07-14 22:03:35 +00001392 dz = strtol(dz_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001393 if (dz != 0) {
Gerd Hoffmannf22d0af2016-01-12 12:14:12 +01001394 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001395 qemu_input_queue_btn(NULL, button, true);
1396 qemu_input_event_sync();
1397 qemu_input_queue_btn(NULL, button, false);
1398 }
1399 }
1400 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001401}
1402
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001403static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001404{
Eric Blake7fb1cf12015-11-18 01:52:57 -07001405 static uint32_t bmap[INPUT_BUTTON__MAX] = {
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001406 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1407 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
1408 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1409 };
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001410 int button_state = qdict_get_int(qdict, "button_state");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001411
1412 if (mouse_button_state == button_state) {
1413 return;
1414 }
1415 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1416 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001417 mouse_button_state = button_state;
bellard13224a82006-07-14 22:03:35 +00001418}
1419
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001420static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001421{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001422 int size = qdict_get_int(qdict, "size");
1423 int addr = qdict_get_int(qdict, "addr");
1424 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001425 uint32_t val;
1426 int suffix;
1427
1428 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001429 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001430 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001431 addr++;
1432 }
1433 addr &= 0xffff;
1434
1435 switch(size) {
1436 default:
1437 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001438 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001439 suffix = 'b';
1440 break;
1441 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001442 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001443 suffix = 'w';
1444 break;
1445 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001446 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001447 suffix = 'l';
1448 break;
1449 }
aliguori376253e2009-03-05 23:01:23 +00001450 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1451 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001452}
bellarda3a91a32004-06-04 11:06:21 +00001453
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001454static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001455{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001456 int size = qdict_get_int(qdict, "size");
1457 int addr = qdict_get_int(qdict, "addr");
1458 int val = qdict_get_int(qdict, "val");
1459
Jan Kiszkaf1147842009-07-14 10:20:11 +02001460 addr &= IOPORTS_MASK;
1461
1462 switch (size) {
1463 default:
1464 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001465 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001466 break;
1467 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001468 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001469 break;
1470 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001471 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001472 break;
1473 }
1474}
1475
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001476static void hmp_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001477{
Gongleif1839932014-12-03 18:20:58 +00001478 Error *local_err = NULL;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001479 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001480
Gongleif1839932014-12-03 18:20:58 +00001481 qemu_boot_set(bootdevice, &local_err);
1482 if (local_err) {
Markus Armbruster193227f2015-12-18 16:35:06 +01001483 error_report_err(local_err);
aurel320ecdffb2008-05-04 20:11:34 +00001484 } else {
Gongleif1839932014-12-03 18:20:58 +00001485 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001486 }
1487}
1488
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001489static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
Blue Swirl314e2982011-09-11 20:22:05 +00001490{
1491 mtree_info((fprintf_function)monitor_printf, mon);
1492}
1493
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001494static void hmp_info_numa(Monitor *mon, const QDict *qdict)
aliguori030ea372009-04-21 22:30:47 +00001495{
aliguorib28b6232009-04-22 20:20:29 +00001496 int i;
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001497 CPUState *cpu;
zhanghailiang5b009e42014-11-04 19:49:30 +08001498 uint64_t *node_mem;
aliguori030ea372009-04-21 22:30:47 +00001499
zhanghailiang5b009e42014-11-04 19:49:30 +08001500 node_mem = g_new0(uint64_t, nb_numa_nodes);
1501 query_numa_node_mem(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001502 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1503 for (i = 0; i < nb_numa_nodes; i++) {
1504 monitor_printf(mon, "node %d cpus:", i);
Andreas Färberbdc44642013-06-24 23:50:24 +02001505 CPU_FOREACH(cpu) {
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001506 if (cpu->numa_node == i) {
Andreas Färber55e5c282012-12-17 06:18:02 +01001507 monitor_printf(mon, " %d", cpu->cpu_index);
aliguori030ea372009-04-21 22:30:47 +00001508 }
1509 }
1510 monitor_printf(mon, "\n");
1511 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
zhanghailiang5b009e42014-11-04 19:49:30 +08001512 node_mem[i] >> 20);
aliguori030ea372009-04-21 22:30:47 +00001513 }
zhanghailiang5b009e42014-11-04 19:49:30 +08001514 g_free(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001515}
1516
bellard5f1ce942006-02-08 22:40:15 +00001517#ifdef CONFIG_PROFILER
1518
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001519int64_t tcg_time;
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001520int64_t dev_time;
1521
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001522static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001523{
aliguori376253e2009-03-05 23:01:23 +00001524 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001525 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00001526 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001527 tcg_time, tcg_time / (double)get_ticks_per_sec());
1528 tcg_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001529 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001530}
1531#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001532static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001533{
aliguori376253e2009-03-05 23:01:23 +00001534 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001535}
1536#endif
1537
bellardec36b692006-07-16 18:57:03 +00001538/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001539static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001540
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001541static void hmp_info_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001542{
1543 int i;
1544 CaptureState *s;
1545
1546 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001547 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001548 s->ops.info (s->opaque);
1549 }
1550}
1551
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001552static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001553{
1554 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001555 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001556 CaptureState *s;
1557
1558 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1559 if (i == n) {
1560 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001561 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001562 g_free (s);
bellardec36b692006-07-16 18:57:03 +00001563 return;
1564 }
1565 }
1566}
1567
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001568static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001569{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001570 const char *path = qdict_get_str(qdict, "path");
1571 int has_freq = qdict_haskey(qdict, "freq");
1572 int freq = qdict_get_try_int(qdict, "freq", -1);
1573 int has_bits = qdict_haskey(qdict, "bits");
1574 int bits = qdict_get_try_int(qdict, "bits", -1);
1575 int has_channels = qdict_haskey(qdict, "nchannels");
1576 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001577 CaptureState *s;
1578
Anthony Liguori7267c092011-08-20 22:09:37 -05001579 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001580
1581 freq = has_freq ? freq : 44100;
1582 bits = has_bits ? bits : 16;
1583 nchannels = has_channels ? nchannels : 2;
1584
1585 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001586 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05001587 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001588 return;
bellardec36b692006-07-16 18:57:03 +00001589 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001590 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001591}
bellardec36b692006-07-16 18:57:03 +00001592
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001593static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001594{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001595 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001596
aliguori76655d62009-03-06 20:27:37 +00001597 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001598 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001599 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001600 return acl;
1601}
aliguori76655d62009-03-06 20:27:37 +00001602
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001603static void hmp_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001604{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001605 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001606 qemu_acl *acl = find_acl(mon, aclname);
1607 qemu_acl_entry *entry;
1608 int i = 0;
1609
1610 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001611 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001612 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00001613 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00001614 i++;
1615 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001616 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001617 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001618 }
1619}
1620
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001621static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001622{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001623 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001624 qemu_acl *acl = find_acl(mon, aclname);
1625
1626 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001627 qemu_acl_reset(acl);
1628 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001629 }
1630}
aliguori76655d62009-03-06 20:27:37 +00001631
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001632static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001633{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001634 const char *aclname = qdict_get_str(qdict, "aclname");
1635 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001636 qemu_acl *acl = find_acl(mon, aclname);
1637
1638 if (acl) {
1639 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001640 acl->defaultDeny = 0;
1641 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001642 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001643 acl->defaultDeny = 1;
1644 monitor_printf(mon, "acl: policy set to 'deny'\n");
1645 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001646 monitor_printf(mon, "acl: unknown policy '%s', "
1647 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001648 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001649 }
1650}
aliguori76655d62009-03-06 20:27:37 +00001651
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001652static void hmp_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001653{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001654 const char *aclname = qdict_get_str(qdict, "aclname");
1655 const char *match = qdict_get_str(qdict, "match");
1656 const char *policy = qdict_get_str(qdict, "policy");
1657 int has_index = qdict_haskey(qdict, "index");
1658 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001659 qemu_acl *acl = find_acl(mon, aclname);
1660 int deny, ret;
1661
1662 if (acl) {
1663 if (strcmp(policy, "allow") == 0) {
1664 deny = 0;
1665 } else if (strcmp(policy, "deny") == 0) {
1666 deny = 1;
1667 } else {
1668 monitor_printf(mon, "acl: unknown policy '%s', "
1669 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001670 return;
1671 }
aliguori28a76be2009-03-06 20:27:40 +00001672 if (has_index)
1673 ret = qemu_acl_insert(acl, deny, match, index);
1674 else
1675 ret = qemu_acl_append(acl, deny, match);
1676 if (ret < 0)
1677 monitor_printf(mon, "acl: unable to add acl entry\n");
1678 else
1679 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001680 }
1681}
aliguori76655d62009-03-06 20:27:37 +00001682
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001683static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001684{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001685 const char *aclname = qdict_get_str(qdict, "aclname");
1686 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001687 qemu_acl *acl = find_acl(mon, aclname);
1688 int ret;
aliguori76655d62009-03-06 20:27:37 +00001689
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001690 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001691 ret = qemu_acl_remove(acl, match);
1692 if (ret < 0)
1693 monitor_printf(mon, "acl: no matching acl entry\n");
1694 else
1695 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001696 }
1697}
1698
Corey Bryant208c9d12012-06-22 14:36:09 -04001699void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001700{
Anthony Liguoric227f092009-10-01 16:12:16 -05001701 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001702 int fd;
1703
Corey Bryant208c9d12012-06-22 14:36:09 -04001704 fd = qemu_chr_fe_get_msgfd(cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001705 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001706 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryant208c9d12012-06-22 14:36:09 -04001707 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001708 }
1709
1710 if (qemu_isdigit(fdname[0])) {
Stefan Hajnoczi0b9f0e22014-04-24 13:58:18 +02001711 close(fd);
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001712 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
1713 "a name not starting with a digit");
Corey Bryant208c9d12012-06-22 14:36:09 -04001714 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001715 }
1716
Corey Bryant208c9d12012-06-22 14:36:09 -04001717 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001718 if (strcmp(monfd->name, fdname) != 0) {
1719 continue;
1720 }
1721
1722 close(monfd->fd);
1723 monfd->fd = fd;
Corey Bryant208c9d12012-06-22 14:36:09 -04001724 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001725 }
1726
Anthony Liguori7267c092011-08-20 22:09:37 -05001727 monfd = g_malloc0(sizeof(mon_fd_t));
1728 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001729 monfd->fd = fd;
1730
Corey Bryant208c9d12012-06-22 14:36:09 -04001731 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001732}
1733
Corey Bryant208c9d12012-06-22 14:36:09 -04001734void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001735{
Anthony Liguoric227f092009-10-01 16:12:16 -05001736 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001737
Corey Bryant208c9d12012-06-22 14:36:09 -04001738 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001739 if (strcmp(monfd->name, fdname) != 0) {
1740 continue;
1741 }
1742
Blue Swirl72cf2d42009-09-12 07:36:22 +00001743 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001744 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001745 g_free(monfd->name);
1746 g_free(monfd);
Corey Bryant208c9d12012-06-22 14:36:09 -04001747 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001748 }
1749
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001750 error_setg(errp, QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001751}
1752
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001753static void hmp_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001754{
Luiz Capitulino13548692011-07-29 15:36:43 -03001755 int saved_vm_running = runstate_is_running();
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001756 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02001757
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001758 vm_stop(RUN_STATE_RESTORE_VM);
Juan Quintelac8d41b22009-08-20 19:42:21 +02001759
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03001760 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02001761 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03001762 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02001763}
1764
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001765int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
Mark McLoughlin7768e042009-07-22 09:11:41 +01001766{
Anthony Liguoric227f092009-10-01 16:12:16 -05001767 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01001768
Blue Swirl72cf2d42009-09-12 07:36:22 +00001769 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01001770 int fd;
1771
1772 if (strcmp(monfd->name, fdname) != 0) {
1773 continue;
1774 }
1775
1776 fd = monfd->fd;
1777
1778 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001779 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05001780 g_free(monfd->name);
1781 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001782
1783 return fd;
1784 }
1785
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001786 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001787 return -1;
1788}
1789
Corey Bryantba1c0482012-08-14 16:43:43 -04001790static void monitor_fdset_cleanup(MonFdset *mon_fdset)
1791{
1792 MonFdsetFd *mon_fdset_fd;
1793 MonFdsetFd *mon_fdset_fd_next;
1794
1795 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantebe52b52012-10-18 15:19:33 -04001796 if ((mon_fdset_fd->removed ||
1797 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
1798 runstate_is_running()) {
Corey Bryantba1c0482012-08-14 16:43:43 -04001799 close(mon_fdset_fd->fd);
1800 g_free(mon_fdset_fd->opaque);
1801 QLIST_REMOVE(mon_fdset_fd, next);
1802 g_free(mon_fdset_fd);
1803 }
1804 }
1805
Corey Bryantadb696f2012-08-14 16:43:47 -04001806 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04001807 QLIST_REMOVE(mon_fdset, next);
1808 g_free(mon_fdset);
1809 }
1810}
1811
Corey Bryantefb87c12012-08-14 16:43:48 -04001812static void monitor_fdsets_cleanup(void)
1813{
1814 MonFdset *mon_fdset;
1815 MonFdset *mon_fdset_next;
1816
1817 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
1818 monitor_fdset_cleanup(mon_fdset);
1819 }
1820}
1821
Corey Bryantba1c0482012-08-14 16:43:43 -04001822AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
1823 const char *opaque, Error **errp)
1824{
1825 int fd;
1826 Monitor *mon = cur_mon;
Corey Bryantba1c0482012-08-14 16:43:43 -04001827 AddfdInfo *fdinfo;
1828
1829 fd = qemu_chr_fe_get_msgfd(mon->chr);
1830 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001831 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryantba1c0482012-08-14 16:43:43 -04001832 goto error;
1833 }
1834
Corey Bryante446f702012-10-18 15:19:32 -04001835 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
1836 has_opaque, opaque, errp);
1837 if (fdinfo) {
1838 return fdinfo;
Corey Bryant9ac54af2012-10-18 15:19:31 -04001839 }
1840
Corey Bryantba1c0482012-08-14 16:43:43 -04001841error:
1842 if (fd != -1) {
1843 close(fd);
1844 }
1845 return NULL;
1846}
1847
1848void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
1849{
1850 MonFdset *mon_fdset;
1851 MonFdsetFd *mon_fdset_fd;
1852 char fd_str[60];
1853
1854 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1855 if (mon_fdset->id != fdset_id) {
1856 continue;
1857 }
1858 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1859 if (has_fd) {
1860 if (mon_fdset_fd->fd != fd) {
1861 continue;
1862 }
1863 mon_fdset_fd->removed = true;
1864 break;
1865 } else {
1866 mon_fdset_fd->removed = true;
1867 }
1868 }
1869 if (has_fd && !mon_fdset_fd) {
1870 goto error;
1871 }
1872 monitor_fdset_cleanup(mon_fdset);
1873 return;
1874 }
1875
1876error:
1877 if (has_fd) {
1878 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
1879 fdset_id, fd);
1880 } else {
1881 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
1882 }
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001883 error_setg(errp, QERR_FD_NOT_FOUND, fd_str);
Corey Bryantba1c0482012-08-14 16:43:43 -04001884}
1885
1886FdsetInfoList *qmp_query_fdsets(Error **errp)
1887{
1888 MonFdset *mon_fdset;
1889 MonFdsetFd *mon_fdset_fd;
1890 FdsetInfoList *fdset_list = NULL;
1891
1892 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1893 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
1894 FdsetFdInfoList *fdsetfd_list = NULL;
1895
1896 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
1897 fdset_info->value->fdset_id = mon_fdset->id;
1898
1899 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1900 FdsetFdInfoList *fdsetfd_info;
1901
1902 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
1903 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
1904 fdsetfd_info->value->fd = mon_fdset_fd->fd;
1905 if (mon_fdset_fd->opaque) {
1906 fdsetfd_info->value->has_opaque = true;
1907 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
1908 } else {
1909 fdsetfd_info->value->has_opaque = false;
1910 }
1911
1912 fdsetfd_info->next = fdsetfd_list;
1913 fdsetfd_list = fdsetfd_info;
1914 }
1915
1916 fdset_info->value->fds = fdsetfd_list;
1917
1918 fdset_info->next = fdset_list;
1919 fdset_list = fdset_info;
1920 }
1921
1922 return fdset_list;
1923}
1924
Corey Bryante446f702012-10-18 15:19:32 -04001925AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
1926 bool has_opaque, const char *opaque,
1927 Error **errp)
1928{
1929 MonFdset *mon_fdset = NULL;
1930 MonFdsetFd *mon_fdset_fd;
1931 AddfdInfo *fdinfo;
1932
1933 if (has_fdset_id) {
1934 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1935 /* Break if match found or match impossible due to ordering by ID */
1936 if (fdset_id <= mon_fdset->id) {
1937 if (fdset_id < mon_fdset->id) {
1938 mon_fdset = NULL;
1939 }
1940 break;
1941 }
1942 }
1943 }
1944
1945 if (mon_fdset == NULL) {
1946 int64_t fdset_id_prev = -1;
1947 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
1948
1949 if (has_fdset_id) {
1950 if (fdset_id < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001951 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
1952 "a non-negative value");
Corey Bryante446f702012-10-18 15:19:32 -04001953 return NULL;
1954 }
1955 /* Use specified fdset ID */
1956 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1957 mon_fdset_cur = mon_fdset;
1958 if (fdset_id < mon_fdset_cur->id) {
1959 break;
1960 }
1961 }
1962 } else {
1963 /* Use first available fdset ID */
1964 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1965 mon_fdset_cur = mon_fdset;
1966 if (fdset_id_prev == mon_fdset_cur->id - 1) {
1967 fdset_id_prev = mon_fdset_cur->id;
1968 continue;
1969 }
1970 break;
1971 }
1972 }
1973
1974 mon_fdset = g_malloc0(sizeof(*mon_fdset));
1975 if (has_fdset_id) {
1976 mon_fdset->id = fdset_id;
1977 } else {
1978 mon_fdset->id = fdset_id_prev + 1;
1979 }
1980
1981 /* The fdset list is ordered by fdset ID */
1982 if (!mon_fdset_cur) {
1983 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
1984 } else if (mon_fdset->id < mon_fdset_cur->id) {
1985 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
1986 } else {
1987 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
1988 }
1989 }
1990
1991 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
1992 mon_fdset_fd->fd = fd;
1993 mon_fdset_fd->removed = false;
1994 if (has_opaque) {
1995 mon_fdset_fd->opaque = g_strdup(opaque);
1996 }
1997 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
1998
1999 fdinfo = g_malloc0(sizeof(*fdinfo));
2000 fdinfo->fdset_id = mon_fdset->id;
2001 fdinfo->fd = mon_fdset_fd->fd;
2002
2003 return fdinfo;
2004}
2005
Corey Bryantadb696f2012-08-14 16:43:47 -04002006int monitor_fdset_get_fd(int64_t fdset_id, int flags)
2007{
Blue Swirlb2dc64c2012-08-18 20:14:54 +00002008#ifndef _WIN32
Corey Bryantadb696f2012-08-14 16:43:47 -04002009 MonFdset *mon_fdset;
2010 MonFdsetFd *mon_fdset_fd;
2011 int mon_fd_flags;
2012
Corey Bryantadb696f2012-08-14 16:43:47 -04002013 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2014 if (mon_fdset->id != fdset_id) {
2015 continue;
2016 }
2017 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2018 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
2019 if (mon_fd_flags == -1) {
2020 return -1;
2021 }
2022
2023 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
2024 return mon_fdset_fd->fd;
2025 }
2026 }
2027 errno = EACCES;
2028 return -1;
2029 }
2030#endif
2031
2032 errno = ENOENT;
2033 return -1;
2034}
2035
2036int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
2037{
2038 MonFdset *mon_fdset;
2039 MonFdsetFd *mon_fdset_fd_dup;
2040
2041 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2042 if (mon_fdset->id != fdset_id) {
2043 continue;
2044 }
2045 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2046 if (mon_fdset_fd_dup->fd == dup_fd) {
2047 return -1;
2048 }
2049 }
2050 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2051 mon_fdset_fd_dup->fd = dup_fd;
2052 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
2053 return 0;
2054 }
2055 return -1;
2056}
2057
2058static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2059{
2060 MonFdset *mon_fdset;
2061 MonFdsetFd *mon_fdset_fd_dup;
2062
2063 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2064 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2065 if (mon_fdset_fd_dup->fd == dup_fd) {
2066 if (remove) {
2067 QLIST_REMOVE(mon_fdset_fd_dup, next);
2068 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2069 monitor_fdset_cleanup(mon_fdset);
2070 }
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002071 return -1;
2072 } else {
2073 return mon_fdset->id;
Corey Bryantadb696f2012-08-14 16:43:47 -04002074 }
Corey Bryantadb696f2012-08-14 16:43:47 -04002075 }
2076 }
2077 }
2078 return -1;
2079}
2080
2081int monitor_fdset_dup_fd_find(int dup_fd)
2082{
2083 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2084}
2085
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002086void monitor_fdset_dup_fd_remove(int dup_fd)
Corey Bryantadb696f2012-08-14 16:43:47 -04002087{
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002088 monitor_fdset_dup_fd_find_remove(dup_fd, true);
Corey Bryantadb696f2012-08-14 16:43:47 -04002089}
2090
Markus Armbruster1677f4c2015-02-09 14:03:19 +01002091int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
Laszlo Ersek59063662014-04-10 10:24:31 +02002092{
2093 int fd;
2094 Error *local_err = NULL;
2095
2096 if (!qemu_isdigit(fdname[0]) && mon) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002097 fd = monitor_get_fd(mon, fdname, &local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002098 } else {
2099 fd = qemu_parse_fd(fdname);
Laszlo Ersek59063662014-04-10 10:24:31 +02002100 if (fd == -1) {
2101 error_setg(&local_err, "Invalid file descriptor number '%s'",
2102 fdname);
2103 }
2104 }
2105 if (local_err) {
2106 error_propagate(errp, local_err);
2107 assert(fd == -1);
2108 } else {
2109 assert(fd != -1);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002110 }
2111
2112 return fd;
2113}
2114
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002115/* Please update hmp-commands.hx when adding or changing commands */
Wayne Xia816f8922011-10-12 11:32:41 +08002116static mon_cmd_t info_cmds[] = {
Pavel Butsykinda76ee72015-09-10 18:38:58 +03002117#include "hmp-commands-info.h"
2118 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002119};
2120
Wenchao Xiaa13ced52013-01-14 14:06:28 +08002121/* mon_cmds and info_cmds would be sorted at runtime */
2122static mon_cmd_t mon_cmds[] = {
2123#include "hmp-commands.h"
2124 { NULL, NULL, },
2125};
2126
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002127static const mon_cmd_t qmp_cmds[] = {
Anthony Liguorie3193602011-09-02 12:34:47 -05002128#include "qmp-commands-old.h"
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002129 { /* NULL */ },
2130};
2131
bellard9307c4c2004-04-04 12:57:25 +00002132/*******************************************************************/
2133
2134static const char *pch;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002135static sigjmp_buf expr_env;
bellard9307c4c2004-04-04 12:57:25 +00002136
bellard9307c4c2004-04-04 12:57:25 +00002137
Stefan Weil9c3175c2013-08-22 21:30:09 +02002138static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
2139expr_error(Monitor *mon, const char *fmt, ...)
bellard9dc39cb2004-03-14 21:38:27 +00002140{
Fam Zheng277acfe2013-08-20 10:58:21 +08002141 va_list ap;
2142 va_start(ap, fmt);
2143 monitor_vprintf(mon, fmt, ap);
2144 monitor_printf(mon, "\n");
2145 va_end(ap);
Peter Maydell6ab7e542013-02-20 15:21:09 +00002146 siglongjmp(expr_env, 1);
bellard9307c4c2004-04-04 12:57:25 +00002147}
2148
Markus Armbruster09b94182010-01-20 13:07:30 +01002149/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00002150static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002151{
Pavel Butsykinbf957282015-09-10 18:38:59 +03002152 const MonitorDef *md = target_monitor_defs();
bellard92a31b12005-02-10 22:00:52 +00002153 void *ptr;
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002154 uint64_t tmp = 0;
2155 int ret;
bellard92a31b12005-02-10 22:00:52 +00002156
Pavel Butsykinbf957282015-09-10 18:38:59 +03002157 if (md == NULL) {
2158 return -1;
2159 }
2160
2161 for(; md->name != NULL; md++) {
bellard9307c4c2004-04-04 12:57:25 +00002162 if (compare_cmd(name, md->name)) {
2163 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002164 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002165 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002166 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00002167 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002168 switch(md->type) {
2169 case MD_I32:
2170 *pval = *(int32_t *)ptr;
2171 break;
2172 case MD_TLONG:
2173 *pval = *(target_long *)ptr;
2174 break;
2175 default:
2176 *pval = 0;
2177 break;
2178 }
bellard9307c4c2004-04-04 12:57:25 +00002179 }
2180 return 0;
2181 }
2182 }
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002183
2184 ret = target_get_monitor_def(mon_get_cpu(), name, &tmp);
2185 if (!ret) {
2186 *pval = (target_long) tmp;
2187 }
2188
2189 return ret;
bellard9307c4c2004-04-04 12:57:25 +00002190}
2191
2192static void next(void)
2193{
Blue Swirl660f11b2009-07-31 21:16:51 +00002194 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002195 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002196 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002197 pch++;
2198 }
2199}
2200
aliguori376253e2009-03-05 23:01:23 +00002201static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002202
aliguori376253e2009-03-05 23:01:23 +00002203static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002204{
blueswir1c2efc952007-09-25 17:28:42 +00002205 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002206 char *p;
bellard6a00d602005-11-21 23:25:50 +00002207 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002208
2209 switch(*pch) {
2210 case '+':
2211 next();
aliguori376253e2009-03-05 23:01:23 +00002212 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002213 break;
2214 case '-':
2215 next();
aliguori376253e2009-03-05 23:01:23 +00002216 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002217 break;
2218 case '~':
2219 next();
aliguori376253e2009-03-05 23:01:23 +00002220 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002221 break;
2222 case '(':
2223 next();
aliguori376253e2009-03-05 23:01:23 +00002224 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002225 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002226 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002227 }
2228 next();
2229 break;
bellard81d09122004-07-14 17:21:37 +00002230 case '\'':
2231 pch++;
2232 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002233 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002234 n = *pch;
2235 pch++;
2236 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002237 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002238 next();
2239 break;
bellard9307c4c2004-04-04 12:57:25 +00002240 case '$':
2241 {
2242 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002243 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002244
bellard9307c4c2004-04-04 12:57:25 +00002245 pch++;
2246 q = buf;
2247 while ((*pch >= 'a' && *pch <= 'z') ||
2248 (*pch >= 'A' && *pch <= 'Z') ||
2249 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002250 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002251 if ((q - buf) < sizeof(buf) - 1)
2252 *q++ = *pch;
2253 pch++;
2254 }
blueswir1cd390082008-11-16 13:53:32 +00002255 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002256 pch++;
2257 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002258 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01002259 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00002260 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00002261 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002262 }
2263 break;
2264 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002265 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002266 n = 0;
2267 break;
2268 default:
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002269 errno = 0;
bellard4f4fbf72006-06-25 18:28:12 +00002270 n = strtoull(pch, &p, 0);
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002271 if (errno == ERANGE) {
2272 expr_error(mon, "number too large");
2273 }
bellard9307c4c2004-04-04 12:57:25 +00002274 if (pch == p) {
Fam Zheng277acfe2013-08-20 10:58:21 +08002275 expr_error(mon, "invalid char '%c' in expression", *p);
bellard9307c4c2004-04-04 12:57:25 +00002276 }
2277 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002278 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002279 pch++;
2280 break;
2281 }
2282 return n;
2283}
2284
2285
aliguori376253e2009-03-05 23:01:23 +00002286static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002287{
blueswir1c2efc952007-09-25 17:28:42 +00002288 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002289 int op;
ths3b46e622007-09-17 08:09:54 +00002290
aliguori376253e2009-03-05 23:01:23 +00002291 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002292 for(;;) {
2293 op = *pch;
2294 if (op != '*' && op != '/' && op != '%')
2295 break;
2296 next();
aliguori376253e2009-03-05 23:01:23 +00002297 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002298 switch(op) {
2299 default:
2300 case '*':
2301 val *= val2;
2302 break;
2303 case '/':
2304 case '%':
ths5fafdf22007-09-16 21:08:06 +00002305 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002306 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002307 if (op == '/')
2308 val /= val2;
2309 else
2310 val %= val2;
2311 break;
2312 }
2313 }
2314 return val;
2315}
2316
aliguori376253e2009-03-05 23:01:23 +00002317static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002318{
blueswir1c2efc952007-09-25 17:28:42 +00002319 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002320 int op;
bellard9307c4c2004-04-04 12:57:25 +00002321
aliguori376253e2009-03-05 23:01:23 +00002322 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002323 for(;;) {
2324 op = *pch;
2325 if (op != '&' && op != '|' && op != '^')
2326 break;
2327 next();
aliguori376253e2009-03-05 23:01:23 +00002328 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002329 switch(op) {
2330 default:
2331 case '&':
2332 val &= val2;
2333 break;
2334 case '|':
2335 val |= val2;
2336 break;
2337 case '^':
2338 val ^= val2;
2339 break;
2340 }
2341 }
2342 return val;
2343}
2344
aliguori376253e2009-03-05 23:01:23 +00002345static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002346{
blueswir1c2efc952007-09-25 17:28:42 +00002347 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002348 int op;
bellard9307c4c2004-04-04 12:57:25 +00002349
aliguori376253e2009-03-05 23:01:23 +00002350 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002351 for(;;) {
2352 op = *pch;
2353 if (op != '+' && op != '-')
2354 break;
2355 next();
aliguori376253e2009-03-05 23:01:23 +00002356 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002357 if (op == '+')
2358 val += val2;
2359 else
2360 val -= val2;
2361 }
2362 return val;
2363}
2364
aliguori376253e2009-03-05 23:01:23 +00002365static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002366{
2367 pch = *pp;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002368 if (sigsetjmp(expr_env, 0)) {
bellard9307c4c2004-04-04 12:57:25 +00002369 *pp = pch;
2370 return -1;
2371 }
blueswir1cd390082008-11-16 13:53:32 +00002372 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002373 pch++;
aliguori376253e2009-03-05 23:01:23 +00002374 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002375 *pp = pch;
2376 return 0;
2377}
2378
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002379static int get_double(Monitor *mon, double *pval, const char **pp)
2380{
2381 const char *p = *pp;
2382 char *tailp;
2383 double d;
2384
2385 d = strtod(p, &tailp);
2386 if (tailp == p) {
2387 monitor_printf(mon, "Number expected\n");
2388 return -1;
2389 }
2390 if (d != d || d - d != 0) {
2391 /* NaN or infinity */
2392 monitor_printf(mon, "Bad number\n");
2393 return -1;
2394 }
2395 *pval = d;
2396 *pp = tailp;
2397 return 0;
2398}
2399
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002400/*
2401 * Store the command-name in cmdname, and return a pointer to
2402 * the remaining of the command string.
2403 */
2404static const char *get_command_name(const char *cmdline,
2405 char *cmdname, size_t nlen)
2406{
2407 size_t len;
2408 const char *p, *pstart;
2409
2410 p = cmdline;
2411 while (qemu_isspace(*p))
2412 p++;
2413 if (*p == '\0')
2414 return NULL;
2415 pstart = p;
2416 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2417 p++;
2418 len = p - pstart;
2419 if (len > nlen - 1)
2420 len = nlen - 1;
2421 memcpy(cmdname, pstart, len);
2422 cmdname[len] = '\0';
2423 return p;
2424}
2425
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002426/**
2427 * Read key of 'type' into 'key' and return the current
2428 * 'type' pointer.
2429 */
2430static char *key_get_info(const char *type, char **key)
2431{
2432 size_t len;
2433 char *p, *str;
2434
2435 if (*type == ',')
2436 type++;
2437
2438 p = strchr(type, ':');
2439 if (!p) {
2440 *key = NULL;
2441 return NULL;
2442 }
2443 len = p - type;
2444
Anthony Liguori7267c092011-08-20 22:09:37 -05002445 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002446 memcpy(str, type, len);
2447 str[len] = '\0';
2448
2449 *key = str;
2450 return ++p;
2451}
2452
bellard9307c4c2004-04-04 12:57:25 +00002453static int default_fmt_format = 'x';
2454static int default_fmt_size = 4;
2455
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002456static int is_valid_option(const char *c, const char *typestr)
2457{
2458 char option[3];
2459
2460 option[0] = '-';
2461 option[1] = *c;
2462 option[2] = '\0';
2463
2464 typestr = strstr(typestr, option);
2465 return (typestr != NULL);
2466}
2467
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03002468static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
2469 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002470{
2471 const mon_cmd_t *cmd;
2472
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03002473 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002474 if (compare_cmd(cmdname, cmd->name)) {
2475 return cmd;
2476 }
2477 }
2478
2479 return NULL;
2480}
2481
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03002482static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
2483{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002484 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03002485}
2486
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002487/*
Bandan Dasae502122015-06-03 18:38:08 -04002488 * Parse command name from @cmdp according to command table @table.
2489 * If blank, return NULL.
2490 * Else, if no valid command can be found, report to @mon, and return
2491 * NULL.
2492 * Else, change @cmdp to point right behind the name, and return its
2493 * command table entry.
2494 * Do not assume the return value points into @table! It doesn't when
2495 * the command is found in a sub-command table.
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002496 */
Anthony Liguoric227f092009-10-01 16:12:16 -05002497static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Bandan Dasae502122015-06-03 18:38:08 -04002498 const char **cmdp,
2499 mon_cmd_t *table)
bellard9307c4c2004-04-04 12:57:25 +00002500{
Bandan Dasae502122015-06-03 18:38:08 -04002501 const char *p;
Anthony Liguoric227f092009-10-01 16:12:16 -05002502 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002503 char cmdname[256];
bellard9dc39cb2004-03-14 21:38:27 +00002504
bellard9307c4c2004-04-04 12:57:25 +00002505 /* extract the command name */
Bandan Dasae502122015-06-03 18:38:08 -04002506 p = get_command_name(*cmdp, cmdname, sizeof(cmdname));
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002507 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002508 return NULL;
ths3b46e622007-09-17 08:09:54 +00002509
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002510 cmd = search_dispatch_table(table, cmdname);
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002511 if (!cmd) {
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002512 monitor_printf(mon, "unknown command: '%.*s'\n",
Bandan Dasae502122015-06-03 18:38:08 -04002513 (int)(p - *cmdp), *cmdp);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002514 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002515 }
bellard9307c4c2004-04-04 12:57:25 +00002516
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002517 /* filter out following useless space */
2518 while (qemu_isspace(*p)) {
2519 p++;
2520 }
Bandan Dasae502122015-06-03 18:38:08 -04002521
2522 *cmdp = p;
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002523 /* search sub command */
Bandan Dasae502122015-06-03 18:38:08 -04002524 if (cmd->sub_table != NULL && *p != '\0') {
2525 return monitor_parse_command(mon, cmdp, cmd->sub_table);
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002526 }
2527
Bandan Dasae502122015-06-03 18:38:08 -04002528 return cmd;
2529}
2530
2531/*
2532 * Parse arguments for @cmd.
2533 * If it can't be parsed, report to @mon, and return NULL.
2534 * Else, insert command arguments into a QDict, and return it.
2535 * Note: On success, caller has to free the QDict structure.
2536 */
2537
2538static QDict *monitor_parse_arguments(Monitor *mon,
2539 const char **endp,
2540 const mon_cmd_t *cmd)
2541{
2542 const char *typestr;
2543 char *key;
2544 int c;
2545 const char *p = *endp;
2546 char buf[1024];
2547 QDict *qdict = qdict_new();
2548
bellard9307c4c2004-04-04 12:57:25 +00002549 /* parse the parameters */
2550 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00002551 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002552 typestr = key_get_info(typestr, &key);
2553 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00002554 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002555 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00002556 typestr++;
2557 switch(c) {
2558 case 'F':
bellard81d09122004-07-14 17:21:37 +00002559 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002560 case 's':
2561 {
2562 int ret;
ths3b46e622007-09-17 08:09:54 +00002563
blueswir1cd390082008-11-16 13:53:32 +00002564 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002565 p++;
2566 if (*typestr == '?') {
2567 typestr++;
2568 if (*p == '\0') {
2569 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03002570 break;
bellard9307c4c2004-04-04 12:57:25 +00002571 }
2572 }
2573 ret = get_str(buf, sizeof(buf), &p);
2574 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002575 switch(c) {
2576 case 'F':
aliguori376253e2009-03-05 23:01:23 +00002577 monitor_printf(mon, "%s: filename expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002578 cmd->name);
bellard81d09122004-07-14 17:21:37 +00002579 break;
2580 case 'B':
aliguori376253e2009-03-05 23:01:23 +00002581 monitor_printf(mon, "%s: block device name expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002582 cmd->name);
bellard81d09122004-07-14 17:21:37 +00002583 break;
2584 default:
Bandan Dasae502122015-06-03 18:38:08 -04002585 monitor_printf(mon, "%s: string expected\n", cmd->name);
bellard81d09122004-07-14 17:21:37 +00002586 break;
2587 }
bellard9307c4c2004-04-04 12:57:25 +00002588 goto fail;
2589 }
Luiz Capitulino53773582009-08-28 15:27:25 -03002590 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00002591 }
2592 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01002593 case 'O':
2594 {
2595 QemuOptsList *opts_list;
2596 QemuOpts *opts;
2597
2598 opts_list = qemu_find_opts(key);
2599 if (!opts_list || opts_list->desc->name) {
2600 goto bad_type;
2601 }
2602 while (qemu_isspace(*p)) {
2603 p++;
2604 }
2605 if (!*p)
2606 break;
2607 if (get_str(buf, sizeof(buf), &p) < 0) {
2608 goto fail;
2609 }
Markus Armbruster70b94332015-02-13 12:50:26 +01002610 opts = qemu_opts_parse_noisily(opts_list, buf, true);
Markus Armbruster361127d2010-02-10 20:24:35 +01002611 if (!opts) {
2612 goto fail;
2613 }
2614 qemu_opts_to_qdict(opts, qdict);
2615 qemu_opts_del(opts);
2616 }
2617 break;
bellard9307c4c2004-04-04 12:57:25 +00002618 case '/':
2619 {
2620 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002621
blueswir1cd390082008-11-16 13:53:32 +00002622 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002623 p++;
2624 if (*p == '/') {
2625 /* format found */
2626 p++;
2627 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002628 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002629 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002630 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002631 count = count * 10 + (*p - '0');
2632 p++;
2633 }
2634 }
2635 size = -1;
2636 format = -1;
2637 for(;;) {
2638 switch(*p) {
2639 case 'o':
2640 case 'd':
2641 case 'u':
2642 case 'x':
2643 case 'i':
2644 case 'c':
2645 format = *p++;
2646 break;
2647 case 'b':
2648 size = 1;
2649 p++;
2650 break;
2651 case 'h':
2652 size = 2;
2653 p++;
2654 break;
2655 case 'w':
2656 size = 4;
2657 p++;
2658 break;
2659 case 'g':
2660 case 'L':
2661 size = 8;
2662 p++;
2663 break;
2664 default:
2665 goto next;
2666 }
2667 }
2668 next:
blueswir1cd390082008-11-16 13:53:32 +00002669 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00002670 monitor_printf(mon, "invalid char in format: '%c'\n",
2671 *p);
bellard9307c4c2004-04-04 12:57:25 +00002672 goto fail;
2673 }
bellard9307c4c2004-04-04 12:57:25 +00002674 if (format < 0)
2675 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002676 if (format != 'i') {
2677 /* for 'i', not specifying a size gives -1 as size */
2678 if (size < 0)
2679 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002680 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002681 }
bellard9307c4c2004-04-04 12:57:25 +00002682 default_fmt_format = format;
2683 } else {
2684 count = 1;
2685 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002686 if (format != 'i') {
2687 size = default_fmt_size;
2688 } else {
2689 size = -1;
2690 }
bellard9307c4c2004-04-04 12:57:25 +00002691 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002692 qdict_put(qdict, "count", qint_from_int(count));
2693 qdict_put(qdict, "format", qint_from_int(format));
2694 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00002695 }
2696 break;
2697 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002698 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002699 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00002700 {
blueswir1c2efc952007-09-25 17:28:42 +00002701 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00002702
blueswir1cd390082008-11-16 13:53:32 +00002703 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002704 p++;
bellard34405572004-06-08 00:55:58 +00002705 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002706 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03002707 if (*p == '\0') {
2708 typestr++;
2709 break;
2710 }
bellard34405572004-06-08 00:55:58 +00002711 } else {
2712 if (*p == '.') {
2713 p++;
blueswir1cd390082008-11-16 13:53:32 +00002714 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00002715 p++;
bellard34405572004-06-08 00:55:58 +00002716 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03002717 typestr++;
2718 break;
bellard34405572004-06-08 00:55:58 +00002719 }
2720 }
bellard13224a82006-07-14 22:03:35 +00002721 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002722 }
aliguori376253e2009-03-05 23:01:23 +00002723 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00002724 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002725 /* Check if 'i' is greater than 32-bit */
2726 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
Bandan Dasae502122015-06-03 18:38:08 -04002727 monitor_printf(mon, "\'%s\' has failed: ", cmd->name);
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002728 monitor_printf(mon, "integer is for 32-bit values\n");
2729 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002730 } else if (c == 'M') {
Luiz Capitulino91162842012-04-26 17:34:30 -03002731 if (val < 0) {
2732 monitor_printf(mon, "enter a positive value\n");
2733 goto fail;
2734 }
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002735 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002736 }
Luiz Capitulino53773582009-08-28 15:27:25 -03002737 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00002738 }
2739 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02002740 case 'o':
2741 {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01002742 int64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02002743 char *end;
2744
2745 while (qemu_isspace(*p)) {
2746 p++;
2747 }
2748 if (*typestr == '?') {
2749 typestr++;
2750 if (*p == '\0') {
2751 break;
2752 }
2753 }
Marc-André Lureau4677bb42015-09-16 18:02:56 +02002754 val = qemu_strtosz(p, &end);
Jes Sorensendbc0c672010-10-21 17:15:47 +02002755 if (val < 0) {
2756 monitor_printf(mon, "invalid size\n");
2757 goto fail;
2758 }
2759 qdict_put(qdict, key, qint_from_int(val));
2760 p = end;
2761 }
2762 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01002763 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002764 {
2765 double val;
2766
2767 while (qemu_isspace(*p))
2768 p++;
2769 if (*typestr == '?') {
2770 typestr++;
2771 if (*p == '\0') {
2772 break;
2773 }
2774 }
2775 if (get_double(mon, &val, &p) < 0) {
2776 goto fail;
2777 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02002778 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01002779 switch (*p) {
2780 case 'm':
2781 val /= 1e3; p += 2; break;
2782 case 'u':
2783 val /= 1e6; p += 2; break;
2784 case 'n':
2785 val /= 1e9; p += 2; break;
2786 }
2787 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002788 if (*p && !qemu_isspace(*p)) {
2789 monitor_printf(mon, "Unknown unit suffix\n");
2790 goto fail;
2791 }
2792 qdict_put(qdict, key, qfloat_from_double(val));
2793 }
2794 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002795 case 'b':
2796 {
2797 const char *beg;
Eric Blakefc48ffc2015-05-15 16:24:59 -06002798 bool val;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002799
2800 while (qemu_isspace(*p)) {
2801 p++;
2802 }
2803 beg = p;
2804 while (qemu_isgraph(*p)) {
2805 p++;
2806 }
2807 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06002808 val = true;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002809 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06002810 val = false;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002811 } else {
2812 monitor_printf(mon, "Expected 'on' or 'off'\n");
2813 goto fail;
2814 }
Eric Blakefc48ffc2015-05-15 16:24:59 -06002815 qdict_put(qdict, key, qbool_from_bool(val));
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002816 }
2817 break;
bellard9307c4c2004-04-04 12:57:25 +00002818 case '-':
2819 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002820 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03002821 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00002822 /* option */
ths3b46e622007-09-17 08:09:54 +00002823
bellard9307c4c2004-04-04 12:57:25 +00002824 c = *typestr++;
2825 if (c == '\0')
2826 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00002827 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002828 p++;
bellard9307c4c2004-04-04 12:57:25 +00002829 if (*p == '-') {
2830 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002831 if(c != *p) {
2832 if(!is_valid_option(p, typestr)) {
2833
2834 monitor_printf(mon, "%s: unsupported option -%c\n",
Bandan Dasae502122015-06-03 18:38:08 -04002835 cmd->name, *p);
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002836 goto fail;
2837 } else {
2838 skip_key = 1;
2839 }
bellard9307c4c2004-04-04 12:57:25 +00002840 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002841 if(skip_key) {
2842 p = tmp;
2843 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03002844 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002845 p++;
Eric Blakefc48ffc2015-05-15 16:24:59 -06002846 qdict_put(qdict, key, qbool_from_bool(true));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002847 }
bellard9307c4c2004-04-04 12:57:25 +00002848 }
bellard9307c4c2004-04-04 12:57:25 +00002849 }
2850 break;
Wenchao Xia129be002013-08-27 20:38:26 +08002851 case 'S':
2852 {
2853 /* package all remaining string */
2854 int len;
2855
2856 while (qemu_isspace(*p)) {
2857 p++;
2858 }
2859 if (*typestr == '?') {
2860 typestr++;
2861 if (*p == '\0') {
2862 /* no remaining string: NULL argument */
2863 break;
2864 }
2865 }
2866 len = strlen(p);
2867 if (len <= 0) {
2868 monitor_printf(mon, "%s: string expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002869 cmd->name);
Bandan Dase549d2a2015-06-03 18:38:10 -04002870 goto fail;
Wenchao Xia129be002013-08-27 20:38:26 +08002871 }
2872 qdict_put(qdict, key, qstring_from_str(p));
2873 p += len;
2874 }
2875 break;
bellard9307c4c2004-04-04 12:57:25 +00002876 default:
2877 bad_type:
Bandan Dasae502122015-06-03 18:38:08 -04002878 monitor_printf(mon, "%s: unknown type '%c'\n", cmd->name, c);
bellard9307c4c2004-04-04 12:57:25 +00002879 goto fail;
2880 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002881 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002882 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00002883 }
2884 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00002885 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002886 p++;
2887 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00002888 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
Bandan Dasae502122015-06-03 18:38:08 -04002889 cmd->name);
bellard9307c4c2004-04-04 12:57:25 +00002890 goto fail;
2891 }
2892
Bandan Dasae502122015-06-03 18:38:08 -04002893 return qdict;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02002894
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002895fail:
Bandan Dasae502122015-06-03 18:38:08 -04002896 QDECREF(qdict);
Anthony Liguori7267c092011-08-20 22:09:37 -05002897 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002898 return NULL;
2899}
2900
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01002901static void handle_hmp_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002902{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002903 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05002904 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002905
Bandan Dasae502122015-06-03 18:38:08 -04002906 cmd = monitor_parse_command(mon, &cmdline, mon->cmd_table);
2907 if (!cmd) {
2908 return;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002909 }
2910
Bandan Dasae502122015-06-03 18:38:08 -04002911 qdict = monitor_parse_arguments(mon, &cmdline, cmd);
2912 if (!qdict) {
Bandan Dasdd41eea2015-06-03 18:38:09 -04002913 monitor_printf(mon, "Try \"help %s\" for more information\n",
2914 cmd->name);
Bandan Dasae502122015-06-03 18:38:08 -04002915 return;
2916 }
2917
2918 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002919 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00002920}
2921
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08002922static void cmd_completion(Monitor *mon, const char *name, const char *list)
bellard81d09122004-07-14 17:21:37 +00002923{
2924 const char *p, *pstart;
2925 char cmd[128];
2926 int len;
2927
2928 p = list;
2929 for(;;) {
2930 pstart = p;
2931 p = strchr(p, '|');
2932 if (!p)
2933 p = pstart + strlen(pstart);
2934 len = p - pstart;
2935 if (len > sizeof(cmd) - 2)
2936 len = sizeof(cmd) - 2;
2937 memcpy(cmd, pstart, len);
2938 cmd[len] = '\0';
2939 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08002940 readline_add_completion(mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00002941 }
2942 if (*p == '\0')
2943 break;
2944 p++;
2945 }
2946}
2947
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08002948static void file_completion(Monitor *mon, const char *input)
bellard81d09122004-07-14 17:21:37 +00002949{
2950 DIR *ffs;
2951 struct dirent *d;
2952 char path[1024];
2953 char file[1024], file_prefix[1024];
2954 int input_path_len;
2955 const char *p;
2956
ths5fafdf22007-09-16 21:08:06 +00002957 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00002958 if (!p) {
2959 input_path_len = 0;
2960 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00002961 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00002962 } else {
2963 input_path_len = p - input + 1;
2964 memcpy(path, input, input_path_len);
2965 if (input_path_len > sizeof(path) - 1)
2966 input_path_len = sizeof(path) - 1;
2967 path[input_path_len] = '\0';
2968 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2969 }
Bandan Das19f2db52015-06-03 18:38:07 -04002970
bellard81d09122004-07-14 17:21:37 +00002971 ffs = opendir(path);
2972 if (!ffs)
2973 return;
2974 for(;;) {
2975 struct stat sb;
2976 d = readdir(ffs);
2977 if (!d)
2978 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09002979
2980 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
2981 continue;
2982 }
2983
bellard81d09122004-07-14 17:21:37 +00002984 if (strstart(d->d_name, file_prefix, NULL)) {
2985 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00002986 if (input_path_len < sizeof(file))
2987 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2988 d->d_name);
bellard81d09122004-07-14 17:21:37 +00002989 /* stat the file to find out if it's a directory.
2990 * In that case add a slash to speed up typing long paths
2991 */
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01002992 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
blueswir1363a37d2008-08-21 17:58:08 +00002993 pstrcat(file, sizeof(file), "/");
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01002994 }
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08002995 readline_add_completion(mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00002996 }
2997 }
2998 closedir(ffs);
2999}
3000
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003001static const char *next_arg_type(const char *typestr)
3002{
3003 const char *p = strchr(typestr, ':');
3004 return (p != NULL ? ++p : typestr);
3005}
3006
Hani Benhabiles40d19392014-05-07 23:41:30 +01003007static void add_completion_option(ReadLineState *rs, const char *str,
3008 const char *option)
3009{
3010 if (!str || !option) {
3011 return;
3012 }
3013 if (!strncmp(option, str, strlen(str))) {
3014 readline_add_completion(rs, option);
3015 }
3016}
3017
Hani Benhabiles13e315d2014-05-07 23:41:29 +01003018void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
3019{
3020 size_t len;
3021 ChardevBackendInfoList *list, *start;
3022
3023 if (nb_args != 2) {
3024 return;
3025 }
3026 len = strlen(str);
3027 readline_set_completion_index(rs, len);
3028
3029 start = list = qmp_query_chardev_backends(NULL);
3030 while (list) {
3031 const char *chr_name = list->value->name;
3032
3033 if (!strncmp(chr_name, str, len)) {
3034 readline_add_completion(rs, chr_name);
3035 }
3036 list = list->next;
3037 }
3038 qapi_free_ChardevBackendInfoList(start);
3039}
3040
Hani Benhabilesb162b492014-05-07 23:41:31 +01003041void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
3042{
3043 size_t len;
3044 int i;
3045
3046 if (nb_args != 2) {
3047 return;
3048 }
3049 len = strlen(str);
3050 readline_set_completion_index(rs, len);
3051 for (i = 0; NetClientOptionsKind_lookup[i]; i++) {
3052 add_completion_option(rs, str, NetClientOptionsKind_lookup[i]);
3053 }
3054}
3055
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003056void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003057{
3058 GSList *list, *elt;
3059 size_t len;
3060
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003061 if (nb_args != 2) {
3062 return;
3063 }
3064
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003065 len = strlen(str);
3066 readline_set_completion_index(rs, len);
3067 list = elt = object_class_get_list(TYPE_DEVICE, false);
3068 while (elt) {
3069 const char *name;
3070 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
3071 TYPE_DEVICE);
3072 name = object_class_get_name(OBJECT_CLASS(dc));
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003073
3074 if (!dc->cannot_instantiate_with_device_add_yet
3075 && !strncmp(name, str, len)) {
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003076 readline_add_completion(rs, name);
3077 }
3078 elt = elt->next;
3079 }
3080 g_slist_free(list);
3081}
3082
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003083void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003084{
3085 GSList *list, *elt;
3086 size_t len;
3087
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003088 if (nb_args != 2) {
3089 return;
3090 }
3091
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003092 len = strlen(str);
3093 readline_set_completion_index(rs, len);
3094 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
3095 while (elt) {
3096 const char *name;
3097
3098 name = object_class_get_name(OBJECT_CLASS(elt->data));
3099 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
3100 readline_add_completion(rs, name);
3101 }
3102 elt = elt->next;
3103 }
3104 g_slist_free(list);
3105}
3106
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003107static void peripheral_device_del_completion(ReadLineState *rs,
3108 const char *str, size_t len)
3109{
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003110 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
3111 GSList *list, *item;
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003112
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003113 list = qdev_build_hotpluggable_device_list(peripheral);
3114 if (!list) {
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003115 return;
3116 }
3117
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003118 for (item = list; item; item = g_slist_next(item)) {
3119 DeviceState *dev = item->data;
3120
3121 if (dev->id && !strncmp(str, dev->id, len)) {
3122 readline_add_completion(rs, dev->id);
3123 }
3124 }
3125
3126 g_slist_free(list);
3127}
3128
Hani Benhabiles6297d9a2014-05-07 23:41:28 +01003129void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3130{
3131 size_t len;
3132 ChardevInfoList *list, *start;
3133
3134 if (nb_args != 2) {
3135 return;
3136 }
3137 len = strlen(str);
3138 readline_set_completion_index(rs, len);
3139
3140 start = list = qmp_query_chardev(NULL);
3141 while (list) {
3142 ChardevInfo *chr = list->value;
3143
3144 if (!strncmp(chr->label, str, len)) {
3145 readline_add_completion(rs, chr->label);
3146 }
3147 list = list->next;
3148 }
3149 qapi_free_ChardevInfoList(start);
3150}
3151
Hani Benhabiles8e597772014-05-27 23:39:30 +01003152static void ringbuf_completion(ReadLineState *rs, const char *str)
3153{
3154 size_t len;
3155 ChardevInfoList *list, *start;
3156
3157 len = strlen(str);
3158 readline_set_completion_index(rs, len);
3159
3160 start = list = qmp_query_chardev(NULL);
3161 while (list) {
3162 ChardevInfo *chr_info = list->value;
3163
3164 if (!strncmp(chr_info->label, str, len)) {
3165 CharDriverState *chr = qemu_chr_find(chr_info->label);
3166 if (chr && chr_is_ringbuf(chr)) {
3167 readline_add_completion(rs, chr_info->label);
3168 }
3169 }
3170 list = list->next;
3171 }
3172 qapi_free_ChardevInfoList(start);
3173}
3174
Hani Benhabiles8e597772014-05-27 23:39:30 +01003175void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
3176{
3177 if (nb_args != 2) {
3178 return;
3179 }
3180 ringbuf_completion(rs, str);
3181}
3182
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003183void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
3184{
3185 size_t len;
3186
3187 if (nb_args != 2) {
3188 return;
3189 }
3190
3191 len = strlen(str);
3192 readline_set_completion_index(rs, len);
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003193 peripheral_device_del_completion(rs, str, len);
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003194}
3195
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003196void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003197{
3198 ObjectPropertyInfoList *list, *start;
3199 size_t len;
3200
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003201 if (nb_args != 2) {
3202 return;
3203 }
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003204 len = strlen(str);
3205 readline_set_completion_index(rs, len);
3206
3207 start = list = qmp_qom_list("/objects", NULL);
3208 while (list) {
3209 ObjectPropertyInfo *info = list->value;
3210
3211 if (!strncmp(info->type, "child<", 5)
3212 && !strncmp(info->name, str, len)) {
3213 readline_add_completion(rs, info->name);
3214 }
3215 list = list->next;
3216 }
3217 qapi_free_ObjectPropertyInfoList(start);
3218}
3219
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003220void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
3221{
3222 int i;
3223 char *sep;
3224 size_t len;
3225
3226 if (nb_args != 2) {
3227 return;
3228 }
3229 sep = strrchr(str, '-');
3230 if (sep) {
3231 str = sep + 1;
3232 }
3233 len = strlen(str);
3234 readline_set_completion_index(rs, len);
Eric Blake7fb1cf12015-11-18 01:52:57 -07003235 for (i = 0; i < Q_KEY_CODE__MAX; i++) {
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003236 if (!strncmp(str, QKeyCode_lookup[i], len)) {
3237 readline_add_completion(rs, QKeyCode_lookup[i]);
3238 }
3239 }
3240}
3241
Hani Benhabiles40d19392014-05-07 23:41:30 +01003242void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
3243{
3244 size_t len;
3245
3246 len = strlen(str);
3247 readline_set_completion_index(rs, len);
3248 if (nb_args == 2) {
Jason Wangeaed4832015-04-23 14:21:38 +08003249 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles40d19392014-05-07 23:41:30 +01003250 int count, i;
3251 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08003252 NET_CLIENT_OPTIONS_KIND_NONE,
3253 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003254 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles40d19392014-05-07 23:41:30 +01003255 const char *name = ncs[i]->name;
3256 if (!strncmp(str, name, len)) {
3257 readline_add_completion(rs, name);
3258 }
3259 }
3260 } else if (nb_args == 3) {
3261 add_completion_option(rs, str, "on");
3262 add_completion_option(rs, str, "off");
3263 }
3264}
3265
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003266void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
3267{
3268 int len, count, i;
Jason Wangeaed4832015-04-23 14:21:38 +08003269 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003270
3271 if (nb_args != 2) {
3272 return;
3273 }
3274
3275 len = strlen(str);
3276 readline_set_completion_index(rs, len);
3277 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_OPTIONS_KIND_NIC,
Jason Wangeaed4832015-04-23 14:21:38 +08003278 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003279 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003280 QemuOpts *opts;
3281 const char *name = ncs[i]->name;
3282 if (strncmp(str, name, len)) {
3283 continue;
3284 }
3285 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
3286 if (opts) {
3287 readline_add_completion(rs, name);
3288 }
3289 }
3290}
3291
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003292void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
3293{
3294 size_t len;
3295
3296 len = strlen(str);
3297 readline_set_completion_index(rs, len);
3298 if (nb_args == 2) {
3299 TraceEventID id;
3300 for (id = 0; id < trace_event_count(); id++) {
3301 const char *event_name = trace_event_get_name(trace_event_id(id));
3302 if (!strncmp(str, event_name, len)) {
3303 readline_add_completion(rs, event_name);
3304 }
3305 }
3306 } else if (nb_args == 3) {
3307 add_completion_option(rs, str, "on");
3308 add_completion_option(rs, str, "off");
3309 }
3310}
3311
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003312void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
3313{
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003314 int i;
3315
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003316 if (nb_args != 2) {
3317 return;
3318 }
3319 readline_set_completion_index(rs, strlen(str));
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003320 for (i = 0; WatchdogExpirationAction_lookup[i]; i++) {
3321 add_completion_option(rs, str, WatchdogExpirationAction_lookup[i]);
3322 }
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003323}
3324
Hani Benhabilesc68a0402014-05-27 23:39:32 +01003325void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
3326 const char *str)
3327{
3328 size_t len;
3329
3330 len = strlen(str);
3331 readline_set_completion_index(rs, len);
3332 if (nb_args == 2) {
3333 int i;
Eric Blake7fb1cf12015-11-18 01:52:57 -07003334 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Hani Benhabilesc68a0402014-05-27 23:39:32 +01003335 const char *name = MigrationCapability_lookup[i];
3336 if (!strncmp(str, name, len)) {
3337 readline_add_completion(rs, name);
3338 }
3339 }
3340 } else if (nb_args == 3) {
3341 add_completion_option(rs, str, "on");
3342 add_completion_option(rs, str, "off");
3343 }
3344}
3345
Liang Li50e9a622015-03-23 16:32:29 +08003346void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
3347 const char *str)
3348{
3349 size_t len;
3350
3351 len = strlen(str);
3352 readline_set_completion_index(rs, len);
3353 if (nb_args == 2) {
3354 int i;
Eric Blake7fb1cf12015-11-18 01:52:57 -07003355 for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
Liang Li50e9a622015-03-23 16:32:29 +08003356 const char *name = MigrationParameter_lookup[i];
3357 if (!strncmp(str, name, len)) {
3358 readline_add_completion(rs, name);
3359 }
3360 }
3361 }
3362}
3363
Hani Benhabilese3bb5322014-05-27 23:39:34 +01003364void host_net_add_completion(ReadLineState *rs, int nb_args, const char *str)
3365{
3366 int i;
3367 size_t len;
3368 if (nb_args != 2) {
3369 return;
3370 }
3371 len = strlen(str);
3372 readline_set_completion_index(rs, len);
3373 for (i = 0; host_net_devices[i]; i++) {
3374 if (!strncmp(host_net_devices[i], str, len)) {
3375 readline_add_completion(rs, host_net_devices[i]);
3376 }
3377 }
3378}
3379
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003380void host_net_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3381{
Jason Wangeaed4832015-04-23 14:21:38 +08003382 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003383 int count, i, len;
3384
3385 len = strlen(str);
3386 readline_set_completion_index(rs, len);
3387 if (nb_args == 2) {
3388 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08003389 NET_CLIENT_OPTIONS_KIND_NONE,
3390 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003391 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003392 int id;
3393 char name[16];
3394
3395 if (net_hub_id_for_client(ncs[i], &id)) {
3396 continue;
3397 }
3398 snprintf(name, sizeof(name), "%d", id);
3399 if (!strncmp(str, name, len)) {
3400 readline_add_completion(rs, name);
3401 }
3402 }
3403 return;
3404 } else if (nb_args == 3) {
3405 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08003406 NET_CLIENT_OPTIONS_KIND_NIC,
3407 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003408 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Jason Wang2c4681f2015-02-02 15:06:38 +08003409 int id;
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003410 const char *name;
3411
Jason Wang2c4681f2015-02-02 15:06:38 +08003412 if (ncs[i]->info->type == NET_CLIENT_OPTIONS_KIND_HUBPORT ||
3413 net_hub_id_for_client(ncs[i], &id)) {
3414 continue;
3415 }
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003416 name = ncs[i]->name;
3417 if (!strncmp(str, name, len)) {
3418 readline_add_completion(rs, name);
3419 }
3420 }
3421 return;
3422 }
3423}
3424
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003425static void vm_completion(ReadLineState *rs, const char *str)
3426{
3427 size_t len;
3428 BlockDriverState *bs = NULL;
3429
3430 len = strlen(str);
3431 readline_set_completion_index(rs, len);
3432 while ((bs = bdrv_next(bs))) {
3433 SnapshotInfoList *snapshots, *snapshot;
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003434 AioContext *ctx = bdrv_get_aio_context(bs);
3435 bool ok = false;
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003436
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003437 aio_context_acquire(ctx);
3438 if (bdrv_can_snapshot(bs)) {
3439 ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0;
3440 }
3441 aio_context_release(ctx);
3442 if (!ok) {
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003443 continue;
3444 }
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003445
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003446 snapshot = snapshots;
3447 while (snapshot) {
3448 char *completion = snapshot->value->name;
3449 if (!strncmp(str, completion, len)) {
3450 readline_add_completion(rs, completion);
3451 }
3452 completion = snapshot->value->id;
3453 if (!strncmp(str, completion, len)) {
3454 readline_add_completion(rs, completion);
3455 }
3456 snapshot = snapshot->next;
3457 }
3458 qapi_free_SnapshotInfoList(snapshots);
3459 }
3460
3461}
3462
3463void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
3464{
3465 if (nb_args == 2) {
3466 vm_completion(rs, str);
3467 }
3468}
3469
3470void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
3471{
3472 if (nb_args == 2) {
3473 vm_completion(rs, str);
3474 }
3475}
3476
Wenchao Xiac35b6402013-08-27 20:38:24 +08003477static void monitor_find_completion_by_table(Monitor *mon,
3478 const mon_cmd_t *cmd_table,
3479 char **args,
3480 int nb_args)
bellard81d09122004-07-14 17:21:37 +00003481{
3482 const char *cmdname;
Wenchao Xiac35b6402013-08-27 20:38:24 +08003483 int i;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003484 const char *ptype, *str, *name;
Anthony Liguoric227f092009-10-01 16:12:16 -05003485 const mon_cmd_t *cmd;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003486 BlockDriverState *bs;
bellard81d09122004-07-14 17:21:37 +00003487
bellard81d09122004-07-14 17:21:37 +00003488 if (nb_args <= 1) {
3489 /* command completion */
3490 if (nb_args == 0)
3491 cmdname = "";
3492 else
3493 cmdname = args[0];
Wenchao Xiad2674b22013-08-27 20:38:16 +08003494 readline_set_completion_index(mon->rs, strlen(cmdname));
Wenchao Xiac35b6402013-08-27 20:38:24 +08003495 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003496 cmd_completion(mon, cmdname, cmd->name);
bellard81d09122004-07-14 17:21:37 +00003497 }
3498 } else {
3499 /* find the command */
Wenchao Xiac35b6402013-08-27 20:38:24 +08003500 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Jan Kiszka03a63482010-06-16 00:38:33 +02003501 if (compare_cmd(args[0], cmd->name)) {
3502 break;
3503 }
bellard81d09122004-07-14 17:21:37 +00003504 }
Jan Kiszka03a63482010-06-16 00:38:33 +02003505 if (!cmd->name) {
Wenchao Xiac35b6402013-08-27 20:38:24 +08003506 return;
Jan Kiszka03a63482010-06-16 00:38:33 +02003507 }
3508
Wenchao Xiad903a772013-08-27 20:38:25 +08003509 if (cmd->sub_table) {
3510 /* do the job again */
Stefan Weile7ae7712015-03-08 19:30:01 +01003511 monitor_find_completion_by_table(mon, cmd->sub_table,
3512 &args[1], nb_args - 1);
3513 return;
Wenchao Xiad903a772013-08-27 20:38:25 +08003514 }
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003515 if (cmd->command_completion) {
Stefan Weile7ae7712015-03-08 19:30:01 +01003516 cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
3517 return;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003518 }
Wenchao Xiad903a772013-08-27 20:38:25 +08003519
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003520 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003521 for(i = 0; i < nb_args - 2; i++) {
3522 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003523 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003524 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003525 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003526 }
3527 }
3528 str = args[nb_args - 1];
Kevin Wolf48fe86f2014-11-12 16:24:02 +01003529 while (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02003530 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00003531 }
bellard81d09122004-07-14 17:21:37 +00003532 switch(*ptype) {
3533 case 'F':
3534 /* file completion */
Wenchao Xiad2674b22013-08-27 20:38:16 +08003535 readline_set_completion_index(mon->rs, strlen(str));
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003536 file_completion(mon, str);
bellard81d09122004-07-14 17:21:37 +00003537 break;
3538 case 'B':
3539 /* block device name completion */
Wenchao Xia599a9262013-08-27 20:38:15 +08003540 readline_set_completion_index(mon->rs, strlen(str));
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003541 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
3542 name = bdrv_get_device_name(bs);
3543 if (str[0] == '\0' ||
3544 !strncmp(name, str, strlen(str))) {
3545 readline_add_completion(mon->rs, name);
3546 }
3547 }
bellard81d09122004-07-14 17:21:37 +00003548 break;
bellard7fe48482004-10-09 18:08:01 +00003549 case 's':
Wenchao Xia129be002013-08-27 20:38:26 +08003550 case 'S':
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003551 if (!strcmp(cmd->name, "help|?")) {
Wenchao Xia7ca0e062013-08-27 20:38:27 +08003552 monitor_find_completion_by_table(mon, cmd_table,
3553 &args[1], nb_args - 1);
bellard7fe48482004-10-09 18:08:01 +00003554 }
3555 break;
bellard81d09122004-07-14 17:21:37 +00003556 default:
3557 break;
3558 }
3559 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08003560}
3561
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003562static void monitor_find_completion(void *opaque,
Wenchao Xiac35b6402013-08-27 20:38:24 +08003563 const char *cmdline)
3564{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003565 Monitor *mon = opaque;
Wenchao Xiac35b6402013-08-27 20:38:24 +08003566 char *args[MAX_ARGS];
3567 int nb_args, len;
3568
3569 /* 1. parse the cmdline */
3570 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
3571 return;
3572 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08003573
3574 /* if the line ends with a space, it means we want to complete the
3575 next arg */
3576 len = strlen(cmdline);
3577 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
3578 if (nb_args >= MAX_ARGS) {
3579 goto cleanup;
3580 }
3581 args[nb_args++] = g_strdup("");
3582 }
3583
3584 /* 2. auto complete according to args */
3585 monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args);
Jan Kiszka03a63482010-06-16 00:38:33 +02003586
3587cleanup:
Wenchao Xiadcc70cd2013-08-27 20:38:22 +08003588 free_cmdline_args(args, nb_args);
bellard81d09122004-07-14 17:21:37 +00003589}
3590
aliguori731b0362009-03-05 23:01:42 +00003591static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003592{
aliguori731b0362009-03-05 23:01:42 +00003593 Monitor *mon = opaque;
3594
Jan Kiszkac62313b2009-12-04 14:05:29 +01003595 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003596}
3597
Markus Armbruster40861822015-05-29 10:27:16 +02003598static bool invalid_qmp_mode(const Monitor *mon, const mon_cmd_t *cmd,
3599 Error **errp)
Luiz Capitulino09069b12010-02-04 18:10:06 -02003600{
Markus Armbruster485febc2015-03-13 17:25:50 +01003601 bool is_cap = cmd->mhandler.cmd_new == qmp_capabilities;
Markus Armbrusterf994b252015-03-06 19:51:51 +01003602
3603 if (is_cap && mon->qmp.in_command_mode) {
Markus Armbruster40861822015-05-29 10:27:16 +02003604 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
3605 "Capabilities negotiation is already complete, command "
3606 "'%s' ignored", cmd->name);
Eric Blake2d5a8342015-04-15 09:19:23 -06003607 return true;
3608 }
Markus Armbrusterf994b252015-03-06 19:51:51 +01003609 if (!is_cap && !mon->qmp.in_command_mode) {
Markus Armbruster40861822015-05-29 10:27:16 +02003610 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
3611 "Expecting capabilities negotiation with "
3612 "'qmp_capabilities' before command '%s'", cmd->name);
Eric Blake2d5a8342015-04-15 09:19:23 -06003613 return true;
3614 }
3615 return false;
Luiz Capitulino09069b12010-02-04 18:10:06 -02003616}
3617
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003618/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03003619 * Argument validation rules:
3620 *
3621 * 1. The argument must exist in cmd_args qdict
3622 * 2. The argument type must be the expected one
3623 *
3624 * Special case: If the argument doesn't exist in cmd_args and
3625 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
3626 * checking is skipped for it.
3627 */
Markus Armbruster326283a2015-03-02 18:39:09 +01003628static void check_client_args_type(const QDict *client_args,
3629 const QDict *cmd_args, int flags,
3630 Error **errp)
Luiz Capitulino4af91932010-06-22 11:44:05 -03003631{
3632 const QDictEntry *ent;
3633
3634 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
3635 QObject *obj;
3636 QString *arg_type;
3637 const QObject *client_arg = qdict_entry_value(ent);
3638 const char *client_arg_name = qdict_entry_key(ent);
3639
3640 obj = qdict_get(cmd_args, client_arg_name);
3641 if (!obj) {
3642 if (flags & QMP_ACCEPT_UNKNOWNS) {
3643 /* handler accepts unknowns */
3644 continue;
3645 }
3646 /* client arg doesn't exist */
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003647 error_setg(errp, QERR_INVALID_PARAMETER, client_arg_name);
Markus Armbruster326283a2015-03-02 18:39:09 +01003648 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003649 }
3650
3651 arg_type = qobject_to_qstring(obj);
3652 assert(arg_type != NULL);
3653
3654 /* check if argument's type is correct */
3655 switch (qstring_get_str(arg_type)[0]) {
3656 case 'F':
3657 case 'B':
3658 case 's':
3659 if (qobject_type(client_arg) != QTYPE_QSTRING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003660 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3661 client_arg_name, "string");
Markus Armbruster326283a2015-03-02 18:39:09 +01003662 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003663 }
3664 break;
3665 case 'i':
3666 case 'l':
3667 case 'M':
Jes Sorensendbc0c672010-10-21 17:15:47 +02003668 case 'o':
Luiz Capitulino4af91932010-06-22 11:44:05 -03003669 if (qobject_type(client_arg) != QTYPE_QINT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003670 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3671 client_arg_name, "int");
Markus Armbruster326283a2015-03-02 18:39:09 +01003672 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003673 }
3674 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003675 case 'T':
3676 if (qobject_type(client_arg) != QTYPE_QINT &&
3677 qobject_type(client_arg) != QTYPE_QFLOAT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003678 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3679 client_arg_name, "number");
Markus Armbruster326283a2015-03-02 18:39:09 +01003680 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003681 }
3682 break;
3683 case 'b':
3684 case '-':
3685 if (qobject_type(client_arg) != QTYPE_QBOOL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003686 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3687 client_arg_name, "bool");
Markus Armbruster326283a2015-03-02 18:39:09 +01003688 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003689 }
3690 break;
3691 case 'O':
3692 assert(flags & QMP_ACCEPT_UNKNOWNS);
3693 break;
Paolo Bonzinib9f89782012-03-22 12:51:11 +01003694 case 'q':
3695 /* Any QObject can be passed. */
3696 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003697 case '/':
3698 case '.':
3699 /*
3700 * These types are not supported by QMP and thus are not
3701 * handled here. Fall through.
3702 */
3703 default:
3704 abort();
3705 }
3706 }
Luiz Capitulino4af91932010-06-22 11:44:05 -03003707}
3708
3709/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003710 * - Check if the client has passed all mandatory args
3711 * - Set special flags for argument validation
3712 */
Markus Armbruster326283a2015-03-02 18:39:09 +01003713static void check_mandatory_args(const QDict *cmd_args,
3714 const QDict *client_args, int *flags,
3715 Error **errp)
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003716{
3717 const QDictEntry *ent;
3718
3719 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
3720 const char *cmd_arg_name = qdict_entry_key(ent);
3721 QString *type = qobject_to_qstring(qdict_entry_value(ent));
3722 assert(type != NULL);
3723
3724 if (qstring_get_str(type)[0] == 'O') {
3725 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
3726 *flags |= QMP_ACCEPT_UNKNOWNS;
3727 } else if (qstring_get_str(type)[0] != '-' &&
3728 qstring_get_str(type)[1] != '?' &&
3729 !qdict_haskey(client_args, cmd_arg_name)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003730 error_setg(errp, QERR_MISSING_PARAMETER, cmd_arg_name);
Markus Armbruster326283a2015-03-02 18:39:09 +01003731 return;
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003732 }
3733 }
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003734}
3735
3736static QDict *qdict_from_args_type(const char *args_type)
3737{
3738 int i;
3739 QDict *qdict;
3740 QString *key, *type, *cur_qs;
3741
3742 assert(args_type != NULL);
3743
3744 qdict = qdict_new();
3745
3746 if (args_type == NULL || args_type[0] == '\0') {
3747 /* no args, empty qdict */
3748 goto out;
3749 }
3750
3751 key = qstring_new();
3752 type = qstring_new();
3753
3754 cur_qs = key;
3755
3756 for (i = 0;; i++) {
3757 switch (args_type[i]) {
3758 case ',':
3759 case '\0':
3760 qdict_put(qdict, qstring_get_str(key), type);
3761 QDECREF(key);
3762 if (args_type[i] == '\0') {
3763 goto out;
3764 }
3765 type = qstring_new(); /* qdict has ref */
3766 cur_qs = key = qstring_new();
3767 break;
3768 case ':':
3769 cur_qs = type;
3770 break;
3771 default:
3772 qstring_append_chr(cur_qs, args_type[i]);
3773 break;
3774 }
3775 }
3776
3777out:
3778 return qdict;
3779}
3780
3781/*
3782 * Client argument checking rules:
3783 *
3784 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03003785 * 2. Each argument provided by the client must be expected
3786 * 3. Each argument provided by the client must have the type expected
3787 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003788 */
Markus Armbruster326283a2015-03-02 18:39:09 +01003789static void qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args,
3790 Error **errp)
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003791{
Markus Armbruster326283a2015-03-02 18:39:09 +01003792 Error *err = NULL;
3793 int flags;
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003794 QDict *cmd_args;
3795
3796 cmd_args = qdict_from_args_type(cmd->args_type);
3797
3798 flags = 0;
Markus Armbruster326283a2015-03-02 18:39:09 +01003799 check_mandatory_args(cmd_args, client_args, &flags, &err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003800 if (err) {
3801 goto out;
3802 }
3803
Markus Armbruster326283a2015-03-02 18:39:09 +01003804 check_client_args_type(client_args, cmd_args, flags, &err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003805
3806out:
Markus Armbruster326283a2015-03-02 18:39:09 +01003807 error_propagate(errp, err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003808 QDECREF(cmd_args);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003809}
3810
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003811/*
3812 * Input object checking rules
3813 *
3814 * 1. Input object must be a dict
3815 * 2. The "execute" key must exist
3816 * 3. The "execute" key must be a string
3817 * 4. If the "arguments" key exists, it must be a dict
3818 * 5. If the "id" key exists, it can be anything (ie. json-value)
3819 * 6. Any argument not listed above is considered invalid
3820 */
Markus Armbrusterba0510a2015-03-02 18:41:43 +01003821static QDict *qmp_check_input_obj(QObject *input_obj, Error **errp)
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003822{
3823 const QDictEntry *ent;
3824 int has_exec_key = 0;
3825 QDict *input_dict;
3826
3827 if (qobject_type(input_obj) != QTYPE_QDICT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003828 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "object");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003829 return NULL;
3830 }
3831
3832 input_dict = qobject_to_qdict(input_obj);
3833
3834 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
3835 const char *arg_name = qdict_entry_key(ent);
3836 const QObject *arg_obj = qdict_entry_value(ent);
3837
3838 if (!strcmp(arg_name, "execute")) {
3839 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003840 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
3841 "execute", "string");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003842 return NULL;
3843 }
3844 has_exec_key = 1;
3845 } else if (!strcmp(arg_name, "arguments")) {
3846 if (qobject_type(arg_obj) != QTYPE_QDICT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003847 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
3848 "arguments", "object");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003849 return NULL;
3850 }
Markus Armbruster779cec42015-06-08 10:44:30 +02003851 } else if (!strcmp(arg_name, "id")) {
3852 /* Any string is acceptable as "id", so nothing to check */
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003853 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003854 error_setg(errp, QERR_QMP_EXTRA_MEMBER, arg_name);
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003855 return NULL;
3856 }
3857 }
3858
3859 if (!has_exec_key) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003860 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "execute");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003861 return NULL;
3862 }
3863
3864 return input_dict;
3865}
3866
Paolo Bonzini95385fe2015-11-25 22:23:31 +01003867static void handle_qmp_command(JSONMessageParser *parser, GQueue *tokens)
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003868{
Markus Armbruster326283a2015-03-02 18:39:09 +01003869 Error *local_err = NULL;
Markus Armbruster84add862015-03-05 16:45:15 +01003870 QObject *obj, *data;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003871 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003872 const mon_cmd_t *cmd;
Luiz Capitulino40e5a012011-10-21 16:15:31 -02003873 const char *cmd_name;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003874 Monitor *mon = cur_mon;
3875
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03003876 args = input = NULL;
Markus Armbruster84add862015-03-05 16:45:15 +01003877 data = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003878
3879 obj = json_parser_parse(tokens, NULL);
3880 if (!obj) {
3881 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003882 error_setg(&local_err, QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003883 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003884 }
3885
Markus Armbrusterba0510a2015-03-02 18:41:43 +01003886 input = qmp_check_input_obj(obj, &local_err);
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003887 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003888 qobject_decref(obj);
3889 goto err_out;
3890 }
3891
Markus Armbruster74358f22015-03-06 19:35:59 +01003892 mon->qmp.id = qdict_get(input, "id");
3893 qobject_incref(mon->qmp.id);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003894
Luiz Capitulino0bbab462010-05-31 17:32:50 -03003895 cmd_name = qdict_get_str(input, "execute");
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +01003896 trace_handle_qmp_command(mon, cmd_name);
Anthony Liguorie3193602011-09-02 12:34:47 -05003897 cmd = qmp_find_cmd(cmd_name);
Eric Blake2d5a8342015-04-15 09:19:23 -06003898 if (!cmd) {
Markus Armbruster710aec92015-03-06 11:28:00 +01003899 error_set(&local_err, ERROR_CLASS_COMMAND_NOT_FOUND,
3900 "The command %s has not been found", cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03003901 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003902 }
Markus Armbruster40861822015-05-29 10:27:16 +02003903 if (invalid_qmp_mode(mon, cmd, &local_err)) {
Eric Blake2d5a8342015-04-15 09:19:23 -06003904 goto err_out;
3905 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003906
3907 obj = qdict_get(input, "arguments");
3908 if (!obj) {
3909 args = qdict_new();
3910 } else {
3911 args = qobject_to_qdict(obj);
3912 QINCREF(args);
3913 }
3914
Markus Armbruster326283a2015-03-02 18:39:09 +01003915 qmp_check_client_args(cmd, args, &local_err);
3916 if (local_err) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003917 goto err_out;
3918 }
3919
Markus Armbruster485febc2015-03-13 17:25:50 +01003920 cmd->mhandler.cmd_new(args, &data, &local_err);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003921
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003922err_out:
Markus Armbruster710aec92015-03-06 11:28:00 +01003923 monitor_protocol_emitter(mon, data, local_err);
Markus Armbruster84add862015-03-05 16:45:15 +01003924 qobject_decref(data);
Markus Armbruster3a81a102015-10-29 12:15:09 +01003925 error_free(local_err);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03003926 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003927 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003928}
3929
Markus Armbrusterc83fe232015-03-06 19:20:51 +01003930static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003931{
3932 Monitor *old_mon = cur_mon;
3933
3934 cur_mon = opaque;
3935
Markus Armbruster74358f22015-03-06 19:35:59 +01003936 json_message_parser_feed(&cur_mon->qmp.parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003937
3938 cur_mon = old_mon;
3939}
3940
aliguori731b0362009-03-05 23:01:42 +00003941static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00003942{
aliguori731b0362009-03-05 23:01:42 +00003943 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00003944 int i;
aliguori376253e2009-03-05 23:01:23 +00003945
aliguori731b0362009-03-05 23:01:42 +00003946 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00003947
aliguoricde76ee2009-03-05 23:01:51 +00003948 if (cur_mon->rs) {
3949 for (i = 0; i < size; i++)
3950 readline_handle_byte(cur_mon->rs, buf[i]);
3951 } else {
3952 if (size == 0 || buf[size - 1] != 0)
3953 monitor_printf(cur_mon, "corrupted command\n");
3954 else
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003955 handle_hmp_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00003956 }
aliguori731b0362009-03-05 23:01:42 +00003957
3958 cur_mon = old_mon;
3959}
aliguorid8f44602008-10-06 13:52:44 +00003960
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003961static void monitor_command_cb(void *opaque, const char *cmdline,
3962 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00003963{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003964 Monitor *mon = opaque;
3965
aliguori731b0362009-03-05 23:01:42 +00003966 monitor_suspend(mon);
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003967 handle_hmp_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00003968 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00003969}
3970
aliguoricde76ee2009-03-05 23:01:51 +00003971int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003972{
aliguoricde76ee2009-03-05 23:01:51 +00003973 if (!mon->rs)
3974 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00003975 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00003976 return 0;
aliguorid8f44602008-10-06 13:52:44 +00003977}
3978
aliguori376253e2009-03-05 23:01:23 +00003979void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003980{
aliguoricde76ee2009-03-05 23:01:51 +00003981 if (!mon->rs)
3982 return;
aliguori731b0362009-03-05 23:01:42 +00003983 if (--mon->suspend_cnt == 0)
3984 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00003985}
3986
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003987static QObject *get_qmp_greeting(void)
3988{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03003989 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003990
Markus Armbruster7fad30f2015-09-16 13:06:19 +02003991 qmp_marshal_query_version(NULL, &ver, NULL);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003992 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
3993}
3994
Markus Armbrusterc83fe232015-03-06 19:20:51 +01003995static void monitor_qmp_event(void *opaque, int event)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003996{
Luiz Capitulino47116d12010-02-08 17:01:30 -02003997 QObject *data;
3998 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003999
Luiz Capitulino47116d12010-02-08 17:01:30 -02004000 switch (event) {
4001 case CHR_EVENT_OPENED:
Markus Armbrusterf994b252015-03-06 19:51:51 +01004002 mon->qmp.in_command_mode = false;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004003 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004004 monitor_json_emitter(mon, data);
4005 qobject_decref(data);
Corey Bryantefb87c12012-08-14 16:43:48 -04004006 mon_refcount++;
Luiz Capitulino47116d12010-02-08 17:01:30 -02004007 break;
4008 case CHR_EVENT_CLOSED:
Markus Armbruster74358f22015-03-06 19:35:59 +01004009 json_message_parser_destroy(&mon->qmp.parser);
4010 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Corey Bryantefb87c12012-08-14 16:43:48 -04004011 mon_refcount--;
4012 monitor_fdsets_cleanup();
Luiz Capitulino47116d12010-02-08 17:01:30 -02004013 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004014 }
4015}
4016
aliguori731b0362009-03-05 23:01:42 +00004017static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004018{
aliguori376253e2009-03-05 23:01:23 +00004019 Monitor *mon = opaque;
4020
aliguori2724b182009-03-05 23:01:47 +00004021 switch (event) {
4022 case CHR_EVENT_MUX_IN:
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02004023 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004024 mon->mux_out = 0;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02004025 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004026 if (mon->reset_seen) {
4027 readline_restart(mon->rs);
4028 monitor_resume(mon);
4029 monitor_flush(mon);
4030 } else {
4031 mon->suspend_cnt = 0;
4032 }
aliguori2724b182009-03-05 23:01:47 +00004033 break;
ths86e94de2007-01-05 22:01:59 +00004034
aliguori2724b182009-03-05 23:01:47 +00004035 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004036 if (mon->reset_seen) {
4037 if (mon->suspend_cnt == 0) {
4038 monitor_printf(mon, "\n");
4039 }
4040 monitor_flush(mon);
4041 monitor_suspend(mon);
4042 } else {
4043 mon->suspend_cnt++;
4044 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02004045 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004046 mon->mux_out = 1;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02004047 qemu_mutex_unlock(&mon->out_lock);
aliguori2724b182009-03-05 23:01:47 +00004048 break;
4049
Amit Shahb6b8df52009-10-07 18:31:16 +05304050 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004051 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4052 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004053 if (!mon->mux_out) {
Stratos Psomadakise5554e22014-09-15 15:34:57 +03004054 readline_restart(mon->rs);
aliguori2724b182009-03-05 23:01:47 +00004055 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004056 }
4057 mon->reset_seen = 1;
Corey Bryantefb87c12012-08-14 16:43:48 -04004058 mon_refcount++;
4059 break;
4060
4061 case CHR_EVENT_CLOSED:
4062 mon_refcount--;
4063 monitor_fdsets_cleanup();
aliguori2724b182009-03-05 23:01:47 +00004064 break;
4065 }
ths86e94de2007-01-05 22:01:59 +00004066}
4067
Wayne Xia816f8922011-10-12 11:32:41 +08004068static int
4069compare_mon_cmd(const void *a, const void *b)
4070{
4071 return strcmp(((const mon_cmd_t *)a)->name,
4072 ((const mon_cmd_t *)b)->name);
4073}
4074
4075static void sortcmdlist(void)
4076{
4077 int array_num;
4078 int elem_size = sizeof(mon_cmd_t);
4079
4080 array_num = sizeof(mon_cmds)/elem_size-1;
4081 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
4082
4083 array_num = sizeof(info_cmds)/elem_size-1;
4084 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
4085}
4086
aliguori76655d62009-03-06 20:27:37 +00004087
4088/*
4089 * Local variables:
4090 * c-indent-level: 4
4091 * c-basic-offset: 4
4092 * tab-width: 8
4093 * End:
4094 */
4095
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004096/* These functions just adapt the readline interface in a typesafe way. We
4097 * could cast function pointers but that discards compiler checks.
4098 */
Stefan Weild5d15072014-01-25 18:18:23 +01004099static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
4100 const char *fmt, ...)
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004101{
4102 va_list ap;
4103 va_start(ap, fmt);
4104 monitor_vprintf(opaque, fmt, ap);
4105 va_end(ap);
4106}
4107
4108static void monitor_readline_flush(void *opaque)
4109{
4110 monitor_flush(opaque);
4111}
4112
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004113static void __attribute__((constructor)) monitor_lock_init(void)
4114{
4115 qemu_mutex_init(&monitor_lock);
4116}
4117
aliguori731b0362009-03-05 23:01:42 +00004118void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004119{
aliguori731b0362009-03-05 23:01:42 +00004120 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004121 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004122
4123 if (is_first_init) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +02004124 monitor_qapi_event_init();
Wenchao Xiad0383172013-08-27 20:38:18 +08004125 sortcmdlist();
ths20d8a3e2007-02-18 17:04:49 +00004126 is_first_init = 0;
4127 }
aliguori87127162009-03-05 23:01:29 +00004128
Wenchao Xiab01fe892013-08-27 20:38:19 +08004129 mon = g_malloc(sizeof(*mon));
4130 monitor_data_init(mon);
ths20d8a3e2007-02-18 17:04:49 +00004131
aliguori87127162009-03-05 23:01:29 +00004132 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00004133 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004134 if (flags & MONITOR_USE_READLINE) {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004135 mon->rs = readline_init(monitor_readline_printf,
4136 monitor_readline_flush,
4137 mon,
4138 monitor_find_completion);
aliguoricde76ee2009-03-05 23:01:51 +00004139 monitor_read_command(mon, 0);
4140 }
aliguori87127162009-03-05 23:01:29 +00004141
Markus Armbruster9f3982f2015-03-06 19:56:38 +01004142 if (monitor_is_qmp(mon)) {
Markus Armbrusterc83fe232015-03-06 19:20:51 +01004143 qemu_chr_add_handlers(chr, monitor_can_read, monitor_qmp_read,
4144 monitor_qmp_event, mon);
Anthony Liguori15f31512011-08-15 11:17:35 -05004145 qemu_chr_fe_set_echo(chr, true);
Markus Armbruster74358f22015-03-06 19:35:59 +01004146 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004147 } else {
4148 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4149 monitor_event, mon);
4150 }
aliguori87127162009-03-05 23:01:29 +00004151
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004152 qemu_mutex_lock(&monitor_lock);
Blue Swirl72cf2d42009-09-12 07:36:22 +00004153 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004154 qemu_mutex_unlock(&monitor_lock);
bellard7e2515e2004-08-01 21:52:19 +00004155}
4156
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004157static void bdrv_password_cb(void *opaque, const char *password,
4158 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00004159{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004160 Monitor *mon = opaque;
4161 BlockDriverState *bs = readline_opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004162 int ret = 0;
Markus Armbruster4d2855a2015-01-29 10:37:00 +01004163 Error *local_err = NULL;
bellard7e2515e2004-08-01 21:52:19 +00004164
Markus Armbruster4d2855a2015-01-29 10:37:00 +01004165 bdrv_add_key(bs, password, &local_err);
4166 if (local_err) {
Markus Armbruster193227f2015-12-18 16:35:06 +01004167 error_report_err(local_err);
aliguoribb5fc202009-03-05 23:01:15 +00004168 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004169 }
aliguori731b0362009-03-05 23:01:42 +00004170 if (mon->password_completion_cb)
4171 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004172
aliguori731b0362009-03-05 23:01:42 +00004173 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004174}
aliguoric0f4ce72009-03-05 23:01:01 +00004175
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004176int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
Markus Armbruster097310b2014-10-07 13:59:15 +02004177 BlockCompletionFunc *completion_cb,
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004178 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004179{
aliguoricde76ee2009-03-05 23:01:51 +00004180 int err;
4181
aliguori376253e2009-03-05 23:01:23 +00004182 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4183 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00004184
aliguori731b0362009-03-05 23:01:42 +00004185 mon->password_completion_cb = completion_cb;
4186 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004187
aliguoricde76ee2009-03-05 23:01:51 +00004188 err = monitor_read_password(mon, bdrv_password_cb, bs);
4189
4190 if (err && completion_cb)
4191 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004192
4193 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00004194}
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004195
4196int monitor_read_block_device_key(Monitor *mon, const char *device,
Markus Armbruster097310b2014-10-07 13:59:15 +02004197 BlockCompletionFunc *completion_cb,
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004198 void *opaque)
4199{
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01004200 Error *err = NULL;
Fam Zheng55606252015-03-02 19:36:46 +08004201 BlockBackend *blk;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004202
Fam Zheng55606252015-03-02 19:36:46 +08004203 blk = blk_by_name(device);
4204 if (!blk) {
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004205 monitor_printf(mon, "Device not found %s\n", device);
4206 return -1;
4207 }
Max Reitz5433c242015-10-19 17:53:29 +02004208 if (!blk_bs(blk)) {
4209 monitor_printf(mon, "Device '%s' has no medium\n", device);
4210 return -1;
4211 }
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004212
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01004213 bdrv_add_key(blk_bs(blk), NULL, &err);
4214 if (err) {
4215 error_free(err);
4216 return monitor_read_bdrv_key_start(mon, blk_bs(blk), completion_cb, opaque);
4217 }
4218
4219 if (completion_cb) {
4220 completion_cb(opaque, 0);
4221 }
4222 return 0;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004223}
Paolo Bonzini4d454572012-11-26 16:03:42 +01004224
4225QemuOptsList qemu_mon_opts = {
4226 .name = "mon",
4227 .implied_opt_name = "chardev",
4228 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
4229 .desc = {
4230 {
4231 .name = "mode",
4232 .type = QEMU_OPT_STRING,
4233 },{
4234 .name = "chardev",
4235 .type = QEMU_OPT_STRING,
4236 },{
4237 .name = "default",
4238 .type = QEMU_OPT_BOOL,
4239 },{
4240 .name = "pretty",
4241 .type = QEMU_OPT_BOOL,
4242 },
4243 { /* end of list */ }
4244 },
4245};
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004246
4247#ifndef TARGET_I386
4248void qmp_rtc_reset_reinjection(Error **errp)
4249{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004250 error_setg(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection");
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004251}
4252#endif
Jason J. Herne7ee0c3e2015-06-26 14:03:16 -04004253
4254#ifndef TARGET_S390X
4255void qmp_dump_skeys(const char *filename, Error **errp)
4256{
4257 error_setg(errp, QERR_FEATURE_DISABLED, "dump-skeys");
4258}
4259#endif