blob: a27e11524f2efd0b2cbec66b82e17eaa64907c05 [file] [log] [blame]
bellard9dc39cb2004-03-14 21:38:27 +00001/*
2 * QEMU monitor
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard9dc39cb2004-03-14 21:38:27 +00004 * Copyright (c) 2003-2004 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard9dc39cb2004-03-14 21:38:27 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
Peter Maydelld38ea872016-01-29 17:50:05 +000024#include "qemu/osdep.h"
blueswir1511d2b12009-03-07 15:32:56 +000025#include <dirent.h>
Paolo Bonzini33c11872016-03-15 16:58:45 +010026#include "qemu-common.h"
27#include "cpu.h"
pbrook87ecb682007-11-17 17:14:51 +000028#include "hw/hw.h"
Paolo Bonzinib4a42f82013-02-04 11:37:52 +010029#include "monitor/qdev.h"
pbrook87ecb682007-11-17 17:14:51 +000030#include "hw/usb.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010031#include "hw/i386/pc.h"
Michael S. Tsirkina2cb15b2012-12-12 14:24:50 +020032#include "hw/pci/pci.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010033#include "sysemu/watchdog.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +020034#include "hw/loader.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010035#include "exec/gdbstub.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020036#include "net/net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000037#include "net/slirp.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020038#include "sysemu/char.h"
Gerd Hoffmann75721502010-10-07 12:22:54 +020039#include "ui/qemu-spice.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010040#include "sysemu/sysemu.h"
Eduardo Habkoste35704b2015-02-08 16:51:16 -020041#include "sysemu/numa.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010042#include "monitor/monitor.h"
Stefan Hajnoczi0150cd82013-11-14 11:54:15 +010043#include "qemu/readline.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010044#include "ui/console.h"
Gerd Hoffmannc751a742013-12-04 15:02:28 +010045#include "ui/input.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010046#include "sysemu/blockdev.h"
Max Reitzda27a002016-03-16 19:54:29 +010047#include "sysemu/block-backend.h"
pbrook87ecb682007-11-17 17:14:51 +000048#include "audio/audio.h"
Paolo Bonzini76cad712012-10-24 11:12:21 +020049#include "disas/disas.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010050#include "sysemu/balloon.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010051#include "qemu/timer.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010052#include "migration/migration.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010053#include "sysemu/kvm.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010054#include "qemu/acl.h"
Paolo Bonzinibdee56f2013-04-02 18:28:41 +020055#include "sysemu/tpm.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010056#include "qapi/qmp/qerror.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010057#include "qapi/qmp/qint.h"
58#include "qapi/qmp/qfloat.h"
59#include "qapi/qmp/qlist.h"
60#include "qapi/qmp/qbool.h"
61#include "qapi/qmp/qstring.h"
62#include "qapi/qmp/qjson.h"
63#include "qapi/qmp/json-streamer.h"
64#include "qapi/qmp/json-parser.h"
Hani Benhabiles1094fd32014-02-06 23:30:13 +010065#include <qom/object_interfaces.h>
Blue Swirl2b41f102011-06-19 20:38:22 +000066#include "cpu.h"
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +010067#include "trace.h"
Lluís31965ae2011-08-31 20:31:24 +020068#include "trace/control.h"
Pavel Butsykinbf957282015-09-10 18:38:59 +030069#include "monitor/hmp-target.h"
Lluís6d8a7642011-08-31 20:30:43 +020070#ifdef CONFIG_TRACE_SIMPLE
Lluís31965ae2011-08-31 20:31:24 +020071#include "trace/simple.h"
Prerna Saxena22890ab2010-06-24 17:04:53 +053072#endif
Paolo Bonzini022c62c2012-12-17 18:19:49 +010073#include "exec/memory.h"
Paolo Bonzini63c91552016-03-15 13:18:37 +010074#include "exec/exec-all.h"
Paolo Bonzini03dd0242015-12-15 13:16:16 +010075#include "qemu/log.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050076#include "qmp-commands.h"
77#include "hmp.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010078#include "qemu/thread.h"
Hani Benhabilesb21631f2014-05-27 23:39:37 +010079#include "block/qapi.h"
Wenchao Xia43a14cf2014-06-18 08:43:31 +020080#include "qapi/qmp-event.h"
81#include "qapi-event.h"
Markus Armbruster39a18152015-09-16 13:06:28 +020082#include "qmp-introspect.h"
Fam Zheng55606252015-03-02 19:36:46 +080083#include "sysemu/block-backend.h"
Alberto Garciadc599972016-03-10 13:55:26 +020084#include "sysemu/qtest.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020085#include "qemu/cutils.h"
ths6a5bd302007-12-03 17:05:38 +000086
Markus Armbruster1ce6be22015-02-06 14:18:24 +010087/* for hmp_info_irq/pic */
Jan Kiszka661f1922011-10-16 11:53:13 +020088#if defined(TARGET_SPARC)
Paolo Bonzini0d09e412013-02-05 17:06:20 +010089#include "hw/sparc/sun4m.h"
Jan Kiszka661f1922011-10-16 11:53:13 +020090#endif
Paolo Bonzini0d09e412013-02-05 17:06:20 +010091#include "hw/lm32/lm32_pic.h"
Jan Kiszka661f1922011-10-16 11:53:13 +020092
Jason J. Hernea4538a52015-06-26 14:07:21 -040093#if defined(TARGET_S390X)
94#include "hw/s390x/storage-keys.h"
95#endif
96
bellard9307c4c2004-04-04 12:57:25 +000097/*
98 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000099 *
bellard9307c4c2004-04-04 12:57:25 +0000100 * 'F' filename
bellard81d09122004-07-14 17:21:37 +0000101 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +0000102 * 's' string (accept optional quote)
Wenchao Xia129be002013-08-27 20:38:26 +0800103 * 'S' it just appends the rest of the string (accept optional quote)
Markus Armbruster361127d2010-02-10 20:24:35 +0100104 * 'O' option string of the form NAME=VALUE,...
105 * parsed according to QemuOptsList given by its name
106 * Example: 'device:O' uses qemu_device_opts.
107 * Restriction: only lists with empty desc are supported
108 * TODO lift the restriction
bellard92a31b12005-02-10 22:00:52 +0000109 * 'i' 32 bit integer
110 * 'l' target long (32 or 64 bit)
Luiz Capitulino91162842012-04-26 17:34:30 -0300111 * 'M' Non-negative target long (32 or 64 bit), in user mode the
112 * value is multiplied by 2^20 (think Mebibyte)
Jes Sorensendbc0c672010-10-21 17:15:47 +0200113 * 'o' octets (aka bytes)
Kevin Wolf5e009842013-06-05 14:19:27 +0200114 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
115 * K, k suffix, which multiplies the value by 2^60 for suffixes E
116 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
117 * 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 +0100118 * 'T' double
119 * user mode accepts an optional ms, us, ns suffix,
120 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +0000121 * '/' optional gdb-like print format (like "/10x")
122 *
Luiz Capitulinofb466602009-08-28 15:27:27 -0300123 * '?' optional type (for all types, except '/')
124 * '.' other form of optional type (for 'i' and 'l')
Markus Armbruster942cd1f2010-03-26 09:07:09 +0100125 * 'b' boolean
126 * user mode accepts "on" or "off"
Luiz Capitulinofb466602009-08-28 15:27:27 -0300127 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +0000128 *
129 */
130
Anthony Liguoric227f092009-10-01 16:12:16 -0500131typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +0000132 const char *name;
bellard9307c4c2004-04-04 12:57:25 +0000133 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +0000134 const char *params;
135 const char *help;
Luiz Capitulino910df892009-10-07 13:41:51 -0300136 union {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -0300137 void (*cmd)(Monitor *mon, const QDict *qdict);
Markus Armbruster485febc2015-03-13 17:25:50 +0100138 void (*cmd_new)(QDict *params, QObject **ret_data, Error **errp);
Luiz Capitulino910df892009-10-07 13:41:51 -0300139 } mhandler;
Wenchao Xia5f3d3352013-01-14 14:06:27 +0800140 /* @sub_table is a list of 2nd level of commands. If it do not exist,
141 * mhandler should be used. If it exist, sub_table[?].mhandler should be
142 * used, and mhandler of 1st level plays the role of help function.
143 */
144 struct mon_cmd_t *sub_table;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +0100145 void (*command_completion)(ReadLineState *rs, int nb_args, const char *str);
Anthony Liguoric227f092009-10-01 16:12:16 -0500146} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000147
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100148/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500149typedef struct mon_fd_t mon_fd_t;
150struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100151 char *name;
152 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500153 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100154};
155
Corey Bryantba1c0482012-08-14 16:43:43 -0400156/* file descriptor associated with a file descriptor set */
157typedef struct MonFdsetFd MonFdsetFd;
158struct MonFdsetFd {
159 int fd;
160 bool removed;
161 char *opaque;
162 QLIST_ENTRY(MonFdsetFd) next;
163};
164
165/* file descriptor set containing fds passed via SCM_RIGHTS */
166typedef struct MonFdset MonFdset;
167struct MonFdset {
168 int64_t id;
169 QLIST_HEAD(, MonFdsetFd) fds;
Corey Bryantadb696f2012-08-14 16:43:47 -0400170 QLIST_HEAD(, MonFdsetFd) dup_fds;
Corey Bryantba1c0482012-08-14 16:43:43 -0400171 QLIST_ENTRY(MonFdset) next;
172};
173
Markus Armbruster74358f22015-03-06 19:35:59 +0100174typedef struct {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200175 QObject *id;
176 JSONMessageParser parser;
Markus Armbrusterf994b252015-03-06 19:51:51 +0100177 /*
178 * When a client connects, we're in capabilities negotiation mode.
179 * When command qmp_capabilities succeeds, we go into command
180 * mode.
181 */
182 bool in_command_mode; /* are we in command mode? */
Markus Armbruster74358f22015-03-06 19:35:59 +0100183} MonitorQMP;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200184
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100185/*
186 * To prevent flooding clients, events can be throttled. The
187 * throttling is calculated globally, rather than per-Monitor
188 * instance.
189 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200190typedef struct MonitorQAPIEventState {
Markus Armbruster7de0be62015-10-15 17:08:35 +0200191 QAPIEvent event; /* Throttling state for this event type and... */
192 QDict *data; /* ... data, see qapi_event_throttle_equal() */
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100193 QEMUTimer *timer; /* Timer for handling delayed events */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200194 QDict *qdict; /* Delayed event (if any) */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200195} MonitorQAPIEventState;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100196
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200197typedef struct {
198 int64_t rate; /* Minimum time (in ns) between two events */
199} MonitorQAPIEventConf;
200
aliguori87127162009-03-05 23:01:29 +0000201struct Monitor {
202 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200203 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000204 int flags;
205 int suspend_cnt;
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400206 bool skip_flush;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200207
208 QemuMutex out_lock;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400209 QString *outbuf;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200210 guint out_watch;
211
212 /* Read under either BQL or out_lock, written with BQL+out_lock. */
213 int mux_out;
214
aliguori731b0362009-03-05 23:01:42 +0000215 ReadLineState *rs;
Markus Armbruster74358f22015-03-06 19:35:59 +0100216 MonitorQMP qmp;
Andreas Färbercb446ec2013-05-01 14:24:52 +0200217 CPUState *mon_cpu;
Markus Armbruster097310b2014-10-07 13:59:15 +0200218 BlockCompletionFunc *password_completion_cb;
aliguori731b0362009-03-05 23:01:42 +0000219 void *password_opaque;
Wenchao Xia77172392013-08-27 20:38:20 +0800220 mon_cmd_t *cmd_table;
Anthony Liguoric227f092009-10-01 16:12:16 -0500221 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000222 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000223};
224
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300225/* QMP checker flags */
226#define QMP_ACCEPT_UNKNOWNS 1
227
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200228/* Protects mon_list, monitor_event_state. */
229static QemuMutex monitor_lock;
230
Blue Swirl72cf2d42009-09-12 07:36:22 +0000231static QLIST_HEAD(mon_list, Monitor) mon_list;
Corey Bryantba1c0482012-08-14 16:43:43 -0400232static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
Corey Bryantefb87c12012-08-14 16:43:48 -0400233static int mon_refcount;
bellard7e2515e2004-08-01 21:52:19 +0000234
Wayne Xia816f8922011-10-12 11:32:41 +0800235static mon_cmd_t mon_cmds[];
236static mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000237
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300238static const mon_cmd_t qmp_cmds[];
239
Markus Armbruster8631b602010-02-18 11:41:55 +0100240Monitor *cur_mon;
aliguori376253e2009-03-05 23:01:23 +0000241
Alberto Garciadc599972016-03-10 13:55:26 +0200242static QEMUClockType event_clock_type = QEMU_CLOCK_REALTIME;
243
Stefan Hajnoczic60bf332013-11-14 11:54:14 +0100244static void monitor_command_cb(void *opaque, const char *cmdline,
245 void *readline_opaque);
aliguori83ab7952008-08-19 14:44:22 +0000246
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100247/**
248 * Is @mon a QMP monitor?
249 */
250static inline bool monitor_is_qmp(const Monitor *mon)
Luiz Capitulino418173c2009-11-26 22:58:51 -0200251{
252 return (mon->flags & MONITOR_USE_CONTROL);
253}
254
Markus Armbruster489653b2015-03-06 20:01:05 +0100255/**
256 * Is the current monitor, if any, a QMP monitor?
257 */
258bool monitor_cur_is_qmp(void)
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100259{
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100260 return cur_mon && monitor_is_qmp(cur_mon);
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100261}
262
Anthony Liguori7060b472011-09-02 12:34:50 -0500263void monitor_read_command(Monitor *mon, int show_prompt)
aliguori731b0362009-03-05 23:01:42 +0000264{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200265 if (!mon->rs)
266 return;
267
aliguori731b0362009-03-05 23:01:42 +0000268 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
269 if (show_prompt)
270 readline_show_prompt(mon->rs);
271}
bellard6a00d602005-11-21 23:25:50 +0000272
Anthony Liguori7060b472011-09-02 12:34:50 -0500273int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
274 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000275{
Markus Armbrusterbcf5d192015-03-12 17:26:46 +0100276 if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000277 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
278 /* prompt is printed on return from the command handler */
279 return 0;
280 } else {
281 monitor_printf(mon, "terminal does not support password prompting\n");
282 return -ENOTTY;
283 }
aliguoribb5fc202009-03-05 23:01:15 +0000284}
285
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200286static void monitor_flush_locked(Monitor *mon);
287
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100288static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
289 void *opaque)
290{
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200291 Monitor *mon = opaque;
292
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200293 qemu_mutex_lock(&mon->out_lock);
294 mon->out_watch = 0;
295 monitor_flush_locked(mon);
296 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100297 return FALSE;
298}
299
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200300/* Called with mon->out_lock held. */
301static void monitor_flush_locked(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000302{
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100303 int rc;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400304 size_t len;
305 const char *buf;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100306
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400307 if (mon->skip_flush) {
308 return;
309 }
310
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400311 buf = qstring_get_str(mon->outbuf);
312 len = qstring_get_length(mon->outbuf);
313
Paolo Bonzinia4cc73d2013-05-31 14:00:27 +0200314 if (len && !mon->mux_out) {
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400315 rc = qemu_chr_fe_write(mon->chr, (const uint8_t *) buf, len);
Stratos Psomadakis056f49f2014-01-27 12:30:15 +0200316 if ((rc < 0 && errno != EAGAIN) || (rc == len)) {
317 /* all flushed or error */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400318 QDECREF(mon->outbuf);
319 mon->outbuf = qstring_new();
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100320 return;
321 }
322 if (rc > 0) {
Eric Blake3b7c78c2016-05-17 16:00:15 -0600323 /* partial write */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400324 QString *tmp = qstring_from_str(buf + rc);
325 QDECREF(mon->outbuf);
326 mon->outbuf = tmp;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100327 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200328 if (mon->out_watch == 0) {
Roger Pau Monnee02bc6d2014-05-23 17:57:49 +0200329 mon->out_watch = qemu_chr_fe_add_watch(mon->chr, G_IO_OUT|G_IO_HUP,
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200330 monitor_unblocked, mon);
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200331 }
bellard7e2515e2004-08-01 21:52:19 +0000332 }
333}
334
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200335void monitor_flush(Monitor *mon)
336{
337 qemu_mutex_lock(&mon->out_lock);
338 monitor_flush_locked(mon);
339 qemu_mutex_unlock(&mon->out_lock);
340}
341
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400342/* flush at every end of line */
aliguori376253e2009-03-05 23:01:23 +0000343static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000344{
ths60fe76f2007-12-16 03:02:09 +0000345 char c;
aliguori731b0362009-03-05 23:01:42 +0000346
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200347 qemu_mutex_lock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000348 for(;;) {
349 c = *str++;
350 if (c == '\0')
351 break;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400352 if (c == '\n') {
353 qstring_append_chr(mon->outbuf, '\r');
354 }
355 qstring_append_chr(mon->outbuf, c);
356 if (c == '\n') {
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200357 monitor_flush_locked(mon);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400358 }
bellard7e2515e2004-08-01 21:52:19 +0000359 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200360 qemu_mutex_unlock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000361}
362
aliguori376253e2009-03-05 23:01:23 +0000363void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000364{
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400365 char *buf;
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200366
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200367 if (!mon)
368 return;
369
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100370 if (monitor_is_qmp(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200371 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200372 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200373
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400374 buf = g_strdup_vprintf(fmt, ap);
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200375 monitor_puts(mon, buf);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400376 g_free(buf);
bellard7e2515e2004-08-01 21:52:19 +0000377}
378
aliguori376253e2009-03-05 23:01:23 +0000379void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000380{
381 va_list ap;
382 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000383 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000384 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000385}
386
Pavel Butsykincaf15312015-09-22 16:18:17 +0300387int monitor_fprintf(FILE *stream, const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000388{
389 va_list ap;
390 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000391 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000392 va_end(ap);
393 return 0;
394}
395
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200396static void monitor_json_emitter(Monitor *mon, const QObject *data)
397{
398 QString *json;
399
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200400 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
401 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200402 assert(json != NULL);
403
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200404 qstring_append_chr(json, '\n');
405 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200406
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200407 QDECREF(json);
408}
409
Markus Armbruster710aec92015-03-06 11:28:00 +0100410static QDict *build_qmp_error_dict(Error *err)
Luiz Capitulinode253f12012-07-27 16:18:16 -0300411{
412 QObject *obj;
413
Markus Armbruster710aec92015-03-06 11:28:00 +0100414 obj = qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %s } }",
Eric Blakef22a28b2015-11-18 01:53:00 -0700415 QapiErrorClass_lookup[error_get_class(err)],
Markus Armbruster710aec92015-03-06 11:28:00 +0100416 error_get_pretty(err));
Luiz Capitulinode253f12012-07-27 16:18:16 -0300417
418 return qobject_to_qdict(obj);
419}
420
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100421static void monitor_protocol_emitter(Monitor *mon, QObject *data,
Markus Armbruster710aec92015-03-06 11:28:00 +0100422 Error *err)
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200423{
424 QDict *qmp;
425
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +0100426 trace_monitor_protocol_emitter(mon);
427
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100428 if (!err) {
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200429 /* success response */
Luiz Capitulinode253f12012-07-27 16:18:16 -0300430 qmp = qdict_new();
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200431 if (data) {
432 qobject_incref(data);
433 qdict_put_obj(qmp, "return", data);
434 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200435 /* return an empty QDict by default */
436 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200437 }
438 } else {
439 /* error response */
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100440 qmp = build_qmp_error_dict(err);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200441 }
442
Markus Armbruster74358f22015-03-06 19:35:59 +0100443 if (mon->qmp.id) {
444 qdict_put_obj(qmp, "id", mon->qmp.id);
445 mon->qmp.id = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200446 }
447
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200448 monitor_json_emitter(mon, QOBJECT(qmp));
449 QDECREF(qmp);
450}
451
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200452
Eric Blake7fb1cf12015-11-18 01:52:57 -0700453static MonitorQAPIEventConf monitor_qapi_event_conf[QAPI_EVENT__MAX] = {
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200454 /* Limit guest-triggerable events to 1 per second */
455 [QAPI_EVENT_RTC_CHANGE] = { 1000 * SCALE_MS },
456 [QAPI_EVENT_WATCHDOG] = { 1000 * SCALE_MS },
457 [QAPI_EVENT_BALLOON_CHANGE] = { 1000 * SCALE_MS },
458 [QAPI_EVENT_QUORUM_REPORT_BAD] = { 1000 * SCALE_MS },
459 [QAPI_EVENT_QUORUM_FAILURE] = { 1000 * SCALE_MS },
460 [QAPI_EVENT_VSERPORT_CHANGE] = { 1000 * SCALE_MS },
461};
462
Markus Armbrustera24712a2015-10-15 17:08:34 +0200463GHashTable *monitor_qapi_event_state;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100464
465/*
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200466 * Emits the event to every monitor instance, @event is only used for trace
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200467 * Called with monitor_lock held.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100468 */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200469static void monitor_qapi_event_emit(QAPIEvent event, QDict *qdict)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100470{
471 Monitor *mon;
472
Markus Armbruster688b4b72015-10-15 17:08:30 +0200473 trace_monitor_protocol_event_emit(event, qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100474 QLIST_FOREACH(mon, &mon_list, entry) {
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100475 if (monitor_is_qmp(mon) && mon->qmp.in_command_mode) {
Markus Armbruster688b4b72015-10-15 17:08:30 +0200476 monitor_json_emitter(mon, QOBJECT(qdict));
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100477 }
478 }
479}
480
Markus Armbrustera24712a2015-10-15 17:08:34 +0200481static void monitor_qapi_event_handler(void *opaque);
482
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100483/*
484 * Queue a new event for emission to Monitor instances,
485 * applying any rate limiting if required.
486 */
487static void
Markus Armbruster688b4b72015-10-15 17:08:30 +0200488monitor_qapi_event_queue(QAPIEvent event, QDict *qdict, Error **errp)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100489{
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200490 MonitorQAPIEventConf *evconf;
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200491 MonitorQAPIEventState *evstate;
Markus Armbruster93f8f982015-10-15 17:08:31 +0200492
Eric Blake7fb1cf12015-11-18 01:52:57 -0700493 assert(event < QAPI_EVENT__MAX);
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200494 evconf = &monitor_qapi_event_conf[event];
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200495 trace_monitor_protocol_event_queue(event, qdict, evconf->rate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100496
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200497 qemu_mutex_lock(&monitor_lock);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200498
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200499 if (!evconf->rate) {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200500 /* Unthrottled event */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200501 monitor_qapi_event_emit(event, qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100502 } else {
Markus Armbruster7de0be62015-10-15 17:08:35 +0200503 QDict *data = qobject_to_qdict(qdict_get(qdict, "data"));
504 MonitorQAPIEventState key = { .event = event, .data = data };
Markus Armbrustera24712a2015-10-15 17:08:34 +0200505
506 evstate = g_hash_table_lookup(monitor_qapi_event_state, &key);
507 assert(!evstate || timer_pending(evstate->timer));
508
509 if (evstate) {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200510 /*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200511 * Timer is pending for (at least) evconf->rate ns after
Markus Armbruster93f8f982015-10-15 17:08:31 +0200512 * last send. Store event for sending when timer fires,
513 * replacing a prior stored event if any.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100514 */
Markus Armbruster93f8f982015-10-15 17:08:31 +0200515 QDECREF(evstate->qdict);
Markus Armbruster688b4b72015-10-15 17:08:30 +0200516 evstate->qdict = qdict;
517 QINCREF(evstate->qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100518 } else {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200519 /*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200520 * Last send was (at least) evconf->rate ns ago.
Markus Armbruster93f8f982015-10-15 17:08:31 +0200521 * Send immediately, and arm the timer to call
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200522 * monitor_qapi_event_handler() in evconf->rate ns. Any
Markus Armbruster93f8f982015-10-15 17:08:31 +0200523 * events arriving before then will be delayed until then.
524 */
Alberto Garciadc599972016-03-10 13:55:26 +0200525 int64_t now = qemu_clock_get_ns(event_clock_type);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200526
Markus Armbruster688b4b72015-10-15 17:08:30 +0200527 monitor_qapi_event_emit(event, qdict);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200528
529 evstate = g_new(MonitorQAPIEventState, 1);
530 evstate->event = event;
Markus Armbruster7de0be62015-10-15 17:08:35 +0200531 evstate->data = data;
532 QINCREF(evstate->data);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200533 evstate->qdict = NULL;
Alberto Garciadc599972016-03-10 13:55:26 +0200534 evstate->timer = timer_new_ns(event_clock_type,
Markus Armbrustera24712a2015-10-15 17:08:34 +0200535 monitor_qapi_event_handler,
536 evstate);
537 g_hash_table_add(monitor_qapi_event_state, evstate);
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200538 timer_mod_ns(evstate->timer, now + evconf->rate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100539 }
540 }
Markus Armbruster93f8f982015-10-15 17:08:31 +0200541
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200542 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100543}
544
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100545/*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200546 * This function runs evconf->rate ns after sending a throttled
Markus Armbruster93f8f982015-10-15 17:08:31 +0200547 * event.
548 * If another event has since been stored, send it.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100549 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200550static void monitor_qapi_event_handler(void *opaque)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100551{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200552 MonitorQAPIEventState *evstate = opaque;
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200553 MonitorQAPIEventConf *evconf = &monitor_qapi_event_conf[evstate->event];
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100554
Markus Armbruster93f8f982015-10-15 17:08:31 +0200555 trace_monitor_protocol_event_handler(evstate->event, evstate->qdict);
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200556 qemu_mutex_lock(&monitor_lock);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200557
Markus Armbruster688b4b72015-10-15 17:08:30 +0200558 if (evstate->qdict) {
Alberto Garciadc599972016-03-10 13:55:26 +0200559 int64_t now = qemu_clock_get_ns(event_clock_type);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200560
Markus Armbruster688b4b72015-10-15 17:08:30 +0200561 monitor_qapi_event_emit(evstate->event, evstate->qdict);
562 QDECREF(evstate->qdict);
563 evstate->qdict = NULL;
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200564 timer_mod_ns(evstate->timer, now + evconf->rate);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200565 } else {
566 g_hash_table_remove(monitor_qapi_event_state, evstate);
Markus Armbruster7de0be62015-10-15 17:08:35 +0200567 QDECREF(evstate->data);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200568 timer_free(evstate->timer);
569 g_free(evstate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100570 }
Markus Armbruster93f8f982015-10-15 17:08:31 +0200571
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200572 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100573}
574
Markus Armbrustera24712a2015-10-15 17:08:34 +0200575static unsigned int qapi_event_throttle_hash(const void *key)
576{
577 const MonitorQAPIEventState *evstate = key;
Markus Armbruster7de0be62015-10-15 17:08:35 +0200578 unsigned int hash = evstate->event * 255;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200579
Markus Armbruster7de0be62015-10-15 17:08:35 +0200580 if (evstate->event == QAPI_EVENT_VSERPORT_CHANGE) {
581 hash += g_str_hash(qdict_get_str(evstate->data, "id"));
582 }
583
Alberto Garcia6d425eb2016-03-10 13:55:25 +0200584 if (evstate->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
585 hash += g_str_hash(qdict_get_str(evstate->data, "node-name"));
586 }
587
Markus Armbruster7de0be62015-10-15 17:08:35 +0200588 return hash;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200589}
590
591static gboolean qapi_event_throttle_equal(const void *a, const void *b)
592{
593 const MonitorQAPIEventState *eva = a;
594 const MonitorQAPIEventState *evb = b;
595
Markus Armbruster7de0be62015-10-15 17:08:35 +0200596 if (eva->event != evb->event) {
597 return FALSE;
598 }
599
600 if (eva->event == QAPI_EVENT_VSERPORT_CHANGE) {
601 return !strcmp(qdict_get_str(eva->data, "id"),
602 qdict_get_str(evb->data, "id"));
603 }
604
Alberto Garcia6d425eb2016-03-10 13:55:25 +0200605 if (eva->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
606 return !strcmp(qdict_get_str(eva->data, "node-name"),
607 qdict_get_str(evb->data, "node-name"));
608 }
609
Markus Armbruster7de0be62015-10-15 17:08:35 +0200610 return TRUE;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200611}
612
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200613static void monitor_qapi_event_init(void)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100614{
Alberto Garciadc599972016-03-10 13:55:26 +0200615 if (qtest_enabled()) {
616 event_clock_type = QEMU_CLOCK_VIRTUAL;
617 }
618
Markus Armbrustera24712a2015-10-15 17:08:34 +0200619 monitor_qapi_event_state = g_hash_table_new(qapi_event_throttle_hash,
620 qapi_event_throttle_equal);
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200621 qmp_event_set_func_emit(monitor_qapi_event_queue);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200622}
623
Markus Armbruster485febc2015-03-13 17:25:50 +0100624static void qmp_capabilities(QDict *params, QObject **ret_data, Error **errp)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200625{
Markus Armbruster485febc2015-03-13 17:25:50 +0100626 cur_mon->qmp.in_command_mode = true;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200627}
628
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100629static void handle_hmp_command(Monitor *mon, const char *cmdline);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200630
Wenchao Xiab01fe892013-08-27 20:38:19 +0800631static void monitor_data_init(Monitor *mon)
632{
633 memset(mon, 0, sizeof(Monitor));
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200634 qemu_mutex_init(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800635 mon->outbuf = qstring_new();
Wenchao Xia77172392013-08-27 20:38:20 +0800636 /* Use *mon_cmds by default. */
637 mon->cmd_table = mon_cmds;
Wenchao Xiab01fe892013-08-27 20:38:19 +0800638}
639
640static void monitor_data_destroy(Monitor *mon)
641{
642 QDECREF(mon->outbuf);
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200643 qemu_mutex_destroy(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800644}
645
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200646char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
647 int64_t cpu_index, Error **errp)
Luiz Capitulino0268d972010-10-22 10:08:02 -0200648{
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200649 char *output = NULL;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200650 Monitor *old_mon, hmp;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200651
Wenchao Xiab01fe892013-08-27 20:38:19 +0800652 monitor_data_init(&hmp);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400653 hmp.skip_flush = true;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200654
655 old_mon = cur_mon;
656 cur_mon = &hmp;
657
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200658 if (has_cpu_index) {
659 int ret = monitor_set_cpu(cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200660 if (ret < 0) {
661 cur_mon = old_mon;
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100662 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
663 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200664 goto out;
665 }
666 }
667
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100668 handle_hmp_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200669 cur_mon = old_mon;
670
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200671 qemu_mutex_lock(&hmp.out_lock);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400672 if (qstring_get_length(hmp.outbuf) > 0) {
673 output = g_strdup(qstring_get_str(hmp.outbuf));
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200674 } else {
675 output = g_strdup("");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200676 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200677 qemu_mutex_unlock(&hmp.out_lock);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200678
679out:
Wenchao Xiab01fe892013-08-27 20:38:19 +0800680 monitor_data_destroy(&hmp);
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200681 return output;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200682}
683
bellard9dc39cb2004-03-14 21:38:27 +0000684static int compare_cmd(const char *name, const char *list)
685{
686 const char *p, *pstart;
687 int len;
688 len = strlen(name);
689 p = list;
690 for(;;) {
691 pstart = p;
692 p = strchr(p, '|');
693 if (!p)
694 p = pstart + strlen(pstart);
695 if ((p - pstart) == len && !memcmp(pstart, name, len))
696 return 1;
697 if (*p == '\0')
698 break;
699 p++;
700 }
701 return 0;
702}
703
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800704static int get_str(char *buf, int buf_size, const char **pp)
705{
706 const char *p;
707 char *q;
708 int c;
709
710 q = buf;
711 p = *pp;
712 while (qemu_isspace(*p)) {
713 p++;
714 }
715 if (*p == '\0') {
716 fail:
717 *q = '\0';
718 *pp = p;
719 return -1;
720 }
721 if (*p == '\"') {
722 p++;
723 while (*p != '\0' && *p != '\"') {
724 if (*p == '\\') {
725 p++;
726 c = *p++;
727 switch (c) {
728 case 'n':
729 c = '\n';
730 break;
731 case 'r':
732 c = '\r';
733 break;
734 case '\\':
735 case '\'':
736 case '\"':
737 break;
738 default:
Peter Maydell71baf782015-08-19 16:20:19 +0100739 printf("unsupported escape code: '\\%c'\n", c);
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800740 goto fail;
741 }
742 if ((q - buf) < buf_size - 1) {
743 *q++ = c;
744 }
745 } else {
746 if ((q - buf) < buf_size - 1) {
747 *q++ = *p;
748 }
749 p++;
750 }
751 }
752 if (*p != '\"') {
Peter Maydell71baf782015-08-19 16:20:19 +0100753 printf("unterminated string\n");
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800754 goto fail;
755 }
756 p++;
757 } else {
758 while (*p != '\0' && !qemu_isspace(*p)) {
759 if ((q - buf) < buf_size - 1) {
760 *q++ = *p;
761 }
762 p++;
763 }
764 }
765 *q = '\0';
766 *pp = p;
767 return 0;
768}
769
770#define MAX_ARGS 16
771
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800772static void free_cmdline_args(char **args, int nb_args)
773{
774 int i;
775
776 assert(nb_args <= MAX_ARGS);
777
778 for (i = 0; i < nb_args; i++) {
779 g_free(args[i]);
780 }
781
782}
783
784/*
785 * Parse the command line to get valid args.
786 * @cmdline: command line to be parsed.
787 * @pnb_args: location to store the number of args, must NOT be NULL.
788 * @args: location to store the args, which should be freed by caller, must
789 * NOT be NULL.
790 *
791 * Returns 0 on success, negative on failure.
792 *
793 * NOTE: this parser is an approximate form of the real command parser. Number
794 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
795 * return with failure.
796 */
797static int parse_cmdline(const char *cmdline,
798 int *pnb_args, char **args)
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800799{
800 const char *p;
801 int nb_args, ret;
802 char buf[1024];
803
804 p = cmdline;
805 nb_args = 0;
806 for (;;) {
807 while (qemu_isspace(*p)) {
808 p++;
809 }
810 if (*p == '\0') {
811 break;
812 }
813 if (nb_args >= MAX_ARGS) {
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800814 goto fail;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800815 }
816 ret = get_str(buf, sizeof(buf), &p);
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800817 if (ret < 0) {
818 goto fail;
819 }
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800820 args[nb_args] = g_strdup(buf);
821 nb_args++;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800822 }
823 *pnb_args = nb_args;
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800824 return 0;
825
826 fail:
827 free_cmdline_args(args, nb_args);
828 return -1;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800829}
830
Wenchao Xia66855492013-08-27 20:38:23 +0800831static void help_cmd_dump_one(Monitor *mon,
832 const mon_cmd_t *cmd,
833 char **prefix_args,
834 int prefix_args_nb)
835{
836 int i;
837
838 for (i = 0; i < prefix_args_nb; i++) {
839 monitor_printf(mon, "%s ", prefix_args[i]);
840 }
841 monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
842}
843
844/* @args[@arg_index] is the valid command need to find in @cmds */
Anthony Liguoric227f092009-10-01 16:12:16 -0500845static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
Wenchao Xia66855492013-08-27 20:38:23 +0800846 char **args, int nb_args, int arg_index)
bellard9dc39cb2004-03-14 21:38:27 +0000847{
Anthony Liguoric227f092009-10-01 16:12:16 -0500848 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000849
Wenchao Xia66855492013-08-27 20:38:23 +0800850 /* No valid arg need to compare with, dump all in *cmds */
851 if (arg_index >= nb_args) {
852 for (cmd = cmds; cmd->name != NULL; cmd++) {
853 help_cmd_dump_one(mon, cmd, args, arg_index);
854 }
855 return;
856 }
857
858 /* Find one entry to dump */
859 for (cmd = cmds; cmd->name != NULL; cmd++) {
860 if (compare_cmd(args[arg_index], cmd->name)) {
861 if (cmd->sub_table) {
862 /* continue with next arg */
863 help_cmd_dump(mon, cmd->sub_table,
864 args, nb_args, arg_index + 1);
865 } else {
866 help_cmd_dump_one(mon, cmd, args, arg_index);
867 }
868 break;
869 }
bellard9dc39cb2004-03-14 21:38:27 +0000870 }
871}
872
aliguori376253e2009-03-05 23:01:23 +0000873static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000874{
Wenchao Xia66855492013-08-27 20:38:23 +0800875 char *args[MAX_ARGS];
876 int nb_args = 0;
877
878 /* 1. parse user input */
879 if (name) {
880 /* special case for log, directly dump and return */
881 if (!strcmp(name, "log")) {
Peter Maydell38dad9e2013-02-11 16:41:25 +0000882 const QEMULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000883 monitor_printf(mon, "Log items (comma separated):\n");
884 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
Peter Maydell38dad9e2013-02-11 16:41:25 +0000885 for (item = qemu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000886 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000887 }
Wenchao Xia66855492013-08-27 20:38:23 +0800888 return;
889 }
890
891 if (parse_cmdline(name, &nb_args, args) < 0) {
892 return;
bellardf193c792004-03-21 17:06:25 +0000893 }
bellard9dc39cb2004-03-14 21:38:27 +0000894 }
Wenchao Xia66855492013-08-27 20:38:23 +0800895
896 /* 2. dump the contents according to parsed args */
897 help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0);
898
899 free_cmdline_args(args, nb_args);
bellard9dc39cb2004-03-14 21:38:27 +0000900}
901
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300902static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300903{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300904 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300905}
906
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100907static void hmp_trace_event(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530908{
909 const char *tp_name = qdict_get_str(qdict, "name");
910 bool new_state = qdict_get_bool(qdict, "option");
Lluís Vilanova14101d02014-08-25 13:20:03 +0200911 Error *local_err = NULL;
Blue Swirlf871d682010-10-13 19:14:29 +0000912
Lluís Vilanova14101d02014-08-25 13:20:03 +0200913 qmp_trace_event_set_state(tp_name, new_state, true, true, &local_err);
914 if (local_err) {
Markus Armbruster091e38b2015-02-10 15:15:43 +0100915 error_report_err(local_err);
Blue Swirlf871d682010-10-13 19:14:29 +0000916 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530917}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100918
Michael Rothc45a8162011-10-02 08:44:37 -0500919#ifdef CONFIG_TRACE_SIMPLE
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100920static void hmp_trace_file(Monitor *mon, const QDict *qdict)
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100921{
922 const char *op = qdict_get_try_str(qdict, "op");
923 const char *arg = qdict_get_try_str(qdict, "arg");
924
925 if (!op) {
926 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
927 } else if (!strcmp(op, "on")) {
928 st_set_trace_file_enabled(true);
929 } else if (!strcmp(op, "off")) {
930 st_set_trace_file_enabled(false);
931 } else if (!strcmp(op, "flush")) {
932 st_flush_trace_buffer();
933 } else if (!strcmp(op, "set")) {
934 if (arg) {
935 st_set_trace_file(arg);
936 }
937 } else {
938 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
939 help_cmd(mon, "trace-file");
940 }
941}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530942#endif
943
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100944static void hmp_info_help(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000945{
Luiz Capitulino13c74252009-10-07 13:41:55 -0300946 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000947}
948
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300949CommandInfoList *qmp_query_commands(Error **errp)
bellard9bc9d1c2004-10-10 15:15:51 +0000950{
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300951 CommandInfoList *info, *cmd_list = NULL;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200952 const mon_cmd_t *cmd;
953
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300954 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino40e5a012011-10-21 16:15:31 -0200955 info = g_malloc0(sizeof(*info));
956 info->value = g_malloc0(sizeof(*info->value));
957 info->value->name = g_strdup(cmd->name);
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200958
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300959 info->next = cmd_list;
960 cmd_list = info;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200961 }
962
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300963 return cmd_list;
thsa36e69d2007-12-02 05:18:19 +0000964}
965
Daniel P. Berrange48608532012-05-21 17:59:51 +0100966EventInfoList *qmp_query_events(Error **errp)
967{
968 EventInfoList *info, *ev_list = NULL;
Wenchao Xia75175172014-06-18 08:43:54 +0200969 QAPIEvent e;
Daniel P. Berrange48608532012-05-21 17:59:51 +0100970
Eric Blake7fb1cf12015-11-18 01:52:57 -0700971 for (e = 0 ; e < QAPI_EVENT__MAX ; e++) {
Wenchao Xia75175172014-06-18 08:43:54 +0200972 const char *event_name = QAPIEvent_lookup[e];
Daniel P. Berrange48608532012-05-21 17:59:51 +0100973 assert(event_name != NULL);
974 info = g_malloc0(sizeof(*info));
975 info->value = g_malloc0(sizeof(*info->value));
976 info->value->name = g_strdup(event_name);
977
978 info->next = ev_list;
979 ev_list = info;
980 }
981
982 return ev_list;
983}
984
Markus Armbruster39a18152015-09-16 13:06:28 +0200985/*
986 * Minor hack: generated marshalling suppressed for this command
987 * ('gen': false in the schema) so we can parse the JSON string
988 * directly into QObject instead of first parsing it with
989 * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
990 * to QObject with generated output marshallers, every time. Instead,
991 * we do it in test-qmp-input-visitor.c, just to make sure
992 * qapi-introspect.py's output actually conforms to the schema.
993 */
994static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
995 Error **errp)
996{
997 *ret_data = qobject_from_json(qmp_schema_json);
998}
999
Luiz Capitulinob025c8b2011-10-06 14:02:57 -03001000/* set the current CPU defined by the user */
1001int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +00001002{
Andreas Färber55e5c282012-12-17 06:18:02 +01001003 CPUState *cpu;
bellard6a00d602005-11-21 23:25:50 +00001004
Andreas Färber1c8bb3c2013-02-15 17:01:09 +01001005 cpu = qemu_get_cpu(cpu_index);
1006 if (cpu == NULL) {
1007 return -1;
bellard6a00d602005-11-21 23:25:50 +00001008 }
Andreas Färbercb446ec2013-05-01 14:24:52 +02001009 cur_mon->mon_cpu = cpu;
Andreas Färber1c8bb3c2013-02-15 17:01:09 +01001010 return 0;
bellard6a00d602005-11-21 23:25:50 +00001011}
1012
Pavel Butsykincaf15312015-09-22 16:18:17 +03001013CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +00001014{
aliguori731b0362009-03-05 23:01:42 +00001015 if (!cur_mon->mon_cpu) {
Luiz Capitulinob025c8b2011-10-06 14:02:57 -03001016 monitor_set_cpu(0);
bellard6a00d602005-11-21 23:25:50 +00001017 }
Avi Kivity4c0960c2009-08-17 23:19:53 +03001018 cpu_synchronize_state(cur_mon->mon_cpu);
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001019 return cur_mon->mon_cpu;
1020}
1021
Pavel Butsykinbf957282015-09-10 18:38:59 +03001022CPUArchState *mon_get_cpu_env(void)
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001023{
1024 return mon_get_cpu()->env_ptr;
bellard6a00d602005-11-21 23:25:50 +00001025}
1026
Luiz Capitulino99b77962011-10-24 10:53:44 -02001027int monitor_get_cpu_index(void)
1028{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001029 return mon_get_cpu()->cpu_index;
Luiz Capitulino99b77962011-10-24 10:53:44 -02001030}
1031
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001032static void hmp_info_registers(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001033{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001034 cpu_dump_state(mon_get_cpu(), (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +00001035}
1036
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001037static void hmp_info_jit(Monitor *mon, const QDict *qdict)
bellarde3db7222005-01-26 22:00:47 +00001038{
aliguori376253e2009-03-05 23:01:23 +00001039 dump_exec_info((FILE *)mon, monitor_fprintf);
Sebastian Tanase27498be2014-07-25 11:56:33 +02001040 dump_drift_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +00001041}
1042
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001043static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
Max Filippov246ae242014-11-02 11:04:18 +03001044{
1045 dump_opcount_info((FILE *)mon, monitor_fprintf);
1046}
1047
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001048static void hmp_info_history(Monitor *mon, const QDict *qdict)
bellardaa455482004-04-04 13:07:25 +00001049{
1050 int i;
bellard7e2515e2004-08-01 21:52:19 +00001051 const char *str;
ths3b46e622007-09-17 08:09:54 +00001052
aliguoricde76ee2009-03-05 23:01:51 +00001053 if (!mon->rs)
1054 return;
bellard7e2515e2004-08-01 21:52:19 +00001055 i = 0;
1056 for(;;) {
aliguori731b0362009-03-05 23:01:42 +00001057 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +00001058 if (!str)
1059 break;
aliguori376253e2009-03-05 23:01:23 +00001060 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +00001061 i++;
bellardaa455482004-04-04 13:07:25 +00001062 }
1063}
1064
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001065static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
j_mayer76a66252007-03-07 08:32:30 +00001066{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001067 cpu_dump_statistics(mon_get_cpu(), (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +00001068}
j_mayer76a66252007-03-07 08:32:30 +00001069
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001070static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301071{
Lluís Vilanova14101d02014-08-25 13:20:03 +02001072 TraceEventInfoList *events = qmp_trace_event_get_state("*", NULL);
1073 TraceEventInfoList *elem;
1074
1075 for (elem = events; elem != NULL; elem = elem->next) {
1076 monitor_printf(mon, "%s : state %u\n",
1077 elem->value->name,
1078 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
1079 }
1080 qapi_free_TraceEventInfoList(events);
Prerna Saxena22890ab2010-06-24 17:04:53 +05301081}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301082
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001083void qmp_client_migrate_info(const char *protocol, const char *hostname,
1084 bool has_port, int64_t port,
1085 bool has_tls_port, int64_t tls_port,
1086 bool has_cert_subject, const char *cert_subject,
1087 Error **errp)
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001088{
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001089 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001090 if (!qemu_using_spice(errp)) {
1091 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001092 }
1093
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001094 if (!has_port && !has_tls_port) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001095 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001096 return;
Yonit Halperin6ec5dae2012-03-18 09:42:39 +02001097 }
1098
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001099 if (qemu_spice_migrate_info(hostname,
1100 has_port ? port : -1,
1101 has_tls_port ? tls_port : -1,
1102 cert_subject)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001103 error_setg(errp, QERR_UNDEFINED_ERROR);
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001104 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001105 }
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001106 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001107 }
1108
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001109 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001110}
1111
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001112static void hmp_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001113{
Peter Maydell9a7e5422013-02-11 16:41:20 +00001114 qemu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001115}
1116
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001117static void hmp_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001118{
1119 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001120 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001121
bellard9307c4c2004-04-04 12:57:25 +00001122 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001123 mask = 0;
1124 } else {
Peter Maydell4fde1eb2013-02-11 16:41:22 +00001125 mask = qemu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001126 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001127 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001128 return;
1129 }
1130 }
Peter Maydell24537a02013-02-11 16:41:23 +00001131 qemu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00001132}
1133
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001134static void hmp_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001135{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001136 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001137 if (!option || !strcmp(option, "on")) {
1138 singlestep = 1;
1139 } else if (!strcmp(option, "off")) {
1140 singlestep = 0;
1141 } else {
1142 monitor_printf(mon, "unexpected option %s\n", option);
1143 }
1144}
1145
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001146static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001147{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001148 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001149 if (!device)
1150 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1151 if (gdbserver_start(device) < 0) {
1152 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1153 device);
1154 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001155 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001156 } else {
aliguori59030a82009-04-05 18:43:41 +00001157 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1158 device);
bellard8a7ddc32004-03-31 19:00:16 +00001159 }
1160}
1161
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001162static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001163{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001164 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001165 if (select_watchdog_action(action) == -1) {
1166 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1167 }
1168}
1169
aliguori376253e2009-03-05 23:01:23 +00001170static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001171{
aliguori376253e2009-03-05 23:01:23 +00001172 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001173 switch(c) {
1174 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001175 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001176 break;
1177 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001178 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001179 break;
1180 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001181 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001182 break;
1183 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001184 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001185 break;
1186 default:
1187 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001188 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001189 } else {
aliguori376253e2009-03-05 23:01:23 +00001190 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001191 }
1192 break;
1193 }
aliguori376253e2009-03-05 23:01:23 +00001194 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001195}
1196
aliguori376253e2009-03-05 23:01:23 +00001197static void memory_dump(Monitor *mon, int count, int format, int wsize,
Avi Kivitya8170e52012-10-23 12:30:10 +02001198 hwaddr addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001199{
Blue Swirl23842aa2010-01-12 20:27:43 +00001200 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001201 uint8_t buf[16];
1202 uint64_t v;
1203
1204 if (format == 'i') {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001205 int flags = 0;
bellard9307c4c2004-04-04 12:57:25 +00001206#ifdef TARGET_I386
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001207 CPUArchState *env = mon_get_cpu_env();
bellard4c27ba22004-04-25 18:05:08 +00001208 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001209 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001210 } else if (wsize == 4) {
1211 flags = 0;
1212 } else {
bellard6a15fd12006-04-12 21:07:07 +00001213 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001214 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001215 if (env) {
1216#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001217 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001218 (env->segs[R_CS].flags & DESC_L_MASK))
1219 flags = 2;
1220 else
1221#endif
1222 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1223 flags = 1;
1224 }
bellard4c27ba22004-04-25 18:05:08 +00001225 }
1226#endif
Tom Musta1c38f842014-04-09 14:53:24 -05001227#ifdef TARGET_PPC
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001228 CPUArchState *env = mon_get_cpu_env();
Tom Musta1c38f842014-04-09 14:53:24 -05001229 flags = msr_le << 16;
1230 flags |= env->bfd_mach;
1231#endif
Peter Crosthwaited49190c2015-05-24 14:20:41 -07001232 monitor_disas(mon, mon_get_cpu(), addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001233 return;
1234 }
1235
1236 len = wsize * count;
1237 if (wsize == 1)
1238 line_size = 8;
1239 else
1240 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001241 max_digits = 0;
1242
1243 switch(format) {
1244 case 'o':
1245 max_digits = (wsize * 8 + 2) / 3;
1246 break;
1247 default:
1248 case 'x':
1249 max_digits = (wsize * 8) / 4;
1250 break;
1251 case 'u':
1252 case 'd':
1253 max_digits = (wsize * 8 * 10 + 32) / 33;
1254 break;
1255 case 'c':
1256 wsize = 1;
1257 break;
1258 }
1259
1260 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001261 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001262 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001263 else
aliguori376253e2009-03-05 23:01:23 +00001264 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001265 l = len;
1266 if (l > line_size)
1267 l = line_size;
1268 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001269 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001270 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001271 if (cpu_memory_rw_debug(mon_get_cpu(), addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001272 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001273 break;
1274 }
bellard9307c4c2004-04-04 12:57:25 +00001275 }
ths5fafdf22007-09-16 21:08:06 +00001276 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001277 while (i < l) {
1278 switch(wsize) {
1279 default:
1280 case 1:
Peter Maydell24e60302015-01-20 15:19:32 +00001281 v = ldub_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001282 break;
1283 case 2:
Peter Maydell24e60302015-01-20 15:19:32 +00001284 v = lduw_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001285 break;
1286 case 4:
Peter Maydell24e60302015-01-20 15:19:32 +00001287 v = (uint32_t)ldl_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001288 break;
1289 case 8:
Peter Maydell24e60302015-01-20 15:19:32 +00001290 v = ldq_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001291 break;
1292 }
aliguori376253e2009-03-05 23:01:23 +00001293 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001294 switch(format) {
1295 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001296 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001297 break;
1298 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001299 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001300 break;
1301 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001302 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001303 break;
1304 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001305 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001306 break;
1307 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001308 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001309 break;
1310 }
1311 i += wsize;
1312 }
aliguori376253e2009-03-05 23:01:23 +00001313 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001314 addr += l;
1315 len -= l;
1316 }
1317}
1318
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001319static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001320{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001321 int count = qdict_get_int(qdict, "count");
1322 int format = qdict_get_int(qdict, "format");
1323 int size = qdict_get_int(qdict, "size");
1324 target_long addr = qdict_get_int(qdict, "addr");
1325
aliguori376253e2009-03-05 23:01:23 +00001326 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001327}
1328
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001329static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001330{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001331 int count = qdict_get_int(qdict, "count");
1332 int format = qdict_get_int(qdict, "format");
1333 int size = qdict_get_int(qdict, "size");
Avi Kivitya8170e52012-10-23 12:30:10 +02001334 hwaddr addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001335
aliguori376253e2009-03-05 23:01:23 +00001336 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001337}
1338
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001339static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001340{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001341 int format = qdict_get_int(qdict, "format");
Avi Kivitya8170e52012-10-23 12:30:10 +02001342 hwaddr val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001343
bellard9307c4c2004-04-04 12:57:25 +00001344 switch(format) {
1345 case 'o':
Avi Kivitya8170e52012-10-23 12:30:10 +02001346 monitor_printf(mon, "%#" HWADDR_PRIo, val);
bellard9307c4c2004-04-04 12:57:25 +00001347 break;
1348 case 'x':
Avi Kivitya8170e52012-10-23 12:30:10 +02001349 monitor_printf(mon, "%#" HWADDR_PRIx, val);
bellard9307c4c2004-04-04 12:57:25 +00001350 break;
1351 case 'u':
Avi Kivitya8170e52012-10-23 12:30:10 +02001352 monitor_printf(mon, "%" HWADDR_PRIu, val);
bellard9307c4c2004-04-04 12:57:25 +00001353 break;
1354 default:
1355 case 'd':
Avi Kivitya8170e52012-10-23 12:30:10 +02001356 monitor_printf(mon, "%" HWADDR_PRId, val);
bellard9307c4c2004-04-04 12:57:25 +00001357 break;
1358 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001359 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001360 break;
1361 }
aliguori376253e2009-03-05 23:01:23 +00001362 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001363}
1364
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001365static void hmp_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001366{
1367 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001368 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001369 uint32_t start = qdict_get_int(qdict, "start");
1370 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001371
1372 sum = 0;
1373 for(addr = start; addr < (start + size); addr++) {
Peter Maydell42874d32015-04-26 16:49:24 +01001374 uint8_t val = address_space_ldub(&address_space_memory, addr,
1375 MEMTXATTRS_UNSPECIFIED, NULL);
bellarde4cf1ad2005-06-04 20:15:57 +00001376 /* BSD sum algorithm ('sum' Unix command) */
1377 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001378 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001379 }
aliguori376253e2009-03-05 23:01:23 +00001380 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001381}
1382
bellard13224a82006-07-14 22:03:35 +00001383static int mouse_button_state;
1384
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001385static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001386{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001387 int dx, dy, dz, button;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001388 const char *dx_str = qdict_get_str(qdict, "dx_str");
1389 const char *dy_str = qdict_get_str(qdict, "dy_str");
1390 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001391
bellard13224a82006-07-14 22:03:35 +00001392 dx = strtol(dx_str, NULL, 0);
1393 dy = strtol(dy_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001394 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
1395 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
1396
1397 if (dz_str) {
bellard13224a82006-07-14 22:03:35 +00001398 dz = strtol(dz_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001399 if (dz != 0) {
Gerd Hoffmannf22d0af2016-01-12 12:14:12 +01001400 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001401 qemu_input_queue_btn(NULL, button, true);
1402 qemu_input_event_sync();
1403 qemu_input_queue_btn(NULL, button, false);
1404 }
1405 }
1406 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001407}
1408
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001409static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001410{
Eric Blake7fb1cf12015-11-18 01:52:57 -07001411 static uint32_t bmap[INPUT_BUTTON__MAX] = {
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001412 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1413 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
1414 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1415 };
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001416 int button_state = qdict_get_int(qdict, "button_state");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001417
1418 if (mouse_button_state == button_state) {
1419 return;
1420 }
1421 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1422 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001423 mouse_button_state = button_state;
bellard13224a82006-07-14 22:03:35 +00001424}
1425
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001426static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001427{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001428 int size = qdict_get_int(qdict, "size");
1429 int addr = qdict_get_int(qdict, "addr");
1430 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001431 uint32_t val;
1432 int suffix;
1433
1434 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001435 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001436 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001437 addr++;
1438 }
1439 addr &= 0xffff;
1440
1441 switch(size) {
1442 default:
1443 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001444 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001445 suffix = 'b';
1446 break;
1447 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001448 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001449 suffix = 'w';
1450 break;
1451 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001452 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001453 suffix = 'l';
1454 break;
1455 }
aliguori376253e2009-03-05 23:01:23 +00001456 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1457 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001458}
bellarda3a91a32004-06-04 11:06:21 +00001459
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001460static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001461{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001462 int size = qdict_get_int(qdict, "size");
1463 int addr = qdict_get_int(qdict, "addr");
1464 int val = qdict_get_int(qdict, "val");
1465
Jan Kiszkaf1147842009-07-14 10:20:11 +02001466 addr &= IOPORTS_MASK;
1467
1468 switch (size) {
1469 default:
1470 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001471 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001472 break;
1473 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001474 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001475 break;
1476 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001477 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001478 break;
1479 }
1480}
1481
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001482static void hmp_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001483{
Gongleif1839932014-12-03 18:20:58 +00001484 Error *local_err = NULL;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001485 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001486
Gongleif1839932014-12-03 18:20:58 +00001487 qemu_boot_set(bootdevice, &local_err);
1488 if (local_err) {
Markus Armbruster193227f2015-12-18 16:35:06 +01001489 error_report_err(local_err);
aurel320ecdffb2008-05-04 20:11:34 +00001490 } else {
Gongleif1839932014-12-03 18:20:58 +00001491 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001492 }
1493}
1494
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001495static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
Blue Swirl314e2982011-09-11 20:22:05 +00001496{
1497 mtree_info((fprintf_function)monitor_printf, mon);
1498}
1499
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001500static void hmp_info_numa(Monitor *mon, const QDict *qdict)
aliguori030ea372009-04-21 22:30:47 +00001501{
aliguorib28b6232009-04-22 20:20:29 +00001502 int i;
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001503 CPUState *cpu;
zhanghailiang5b009e42014-11-04 19:49:30 +08001504 uint64_t *node_mem;
aliguori030ea372009-04-21 22:30:47 +00001505
zhanghailiang5b009e42014-11-04 19:49:30 +08001506 node_mem = g_new0(uint64_t, nb_numa_nodes);
1507 query_numa_node_mem(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001508 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1509 for (i = 0; i < nb_numa_nodes; i++) {
1510 monitor_printf(mon, "node %d cpus:", i);
Andreas Färberbdc44642013-06-24 23:50:24 +02001511 CPU_FOREACH(cpu) {
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001512 if (cpu->numa_node == i) {
Andreas Färber55e5c282012-12-17 06:18:02 +01001513 monitor_printf(mon, " %d", cpu->cpu_index);
aliguori030ea372009-04-21 22:30:47 +00001514 }
1515 }
1516 monitor_printf(mon, "\n");
1517 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
zhanghailiang5b009e42014-11-04 19:49:30 +08001518 node_mem[i] >> 20);
aliguori030ea372009-04-21 22:30:47 +00001519 }
zhanghailiang5b009e42014-11-04 19:49:30 +08001520 g_free(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001521}
1522
bellard5f1ce942006-02-08 22:40:15 +00001523#ifdef CONFIG_PROFILER
1524
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001525int64_t tcg_time;
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001526int64_t dev_time;
1527
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001528static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001529{
aliguori376253e2009-03-05 23:01:23 +00001530 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Rutuja Shah73bcb242016-03-21 21:32:30 +05301531 dev_time, dev_time / (double)NANOSECONDS_PER_SECOND);
aliguori376253e2009-03-05 23:01:23 +00001532 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Rutuja Shah73bcb242016-03-21 21:32:30 +05301533 tcg_time, tcg_time / (double)NANOSECONDS_PER_SECOND);
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001534 tcg_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001535 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001536}
1537#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001538static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001539{
aliguori376253e2009-03-05 23:01:23 +00001540 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001541}
1542#endif
1543
bellardec36b692006-07-16 18:57:03 +00001544/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001545static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001546
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001547static void hmp_info_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001548{
1549 int i;
1550 CaptureState *s;
1551
1552 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001553 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001554 s->ops.info (s->opaque);
1555 }
1556}
1557
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001558static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001559{
1560 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001561 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001562 CaptureState *s;
1563
1564 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1565 if (i == n) {
1566 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001567 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001568 g_free (s);
bellardec36b692006-07-16 18:57:03 +00001569 return;
1570 }
1571 }
1572}
1573
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001574static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001575{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001576 const char *path = qdict_get_str(qdict, "path");
1577 int has_freq = qdict_haskey(qdict, "freq");
1578 int freq = qdict_get_try_int(qdict, "freq", -1);
1579 int has_bits = qdict_haskey(qdict, "bits");
1580 int bits = qdict_get_try_int(qdict, "bits", -1);
1581 int has_channels = qdict_haskey(qdict, "nchannels");
1582 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001583 CaptureState *s;
1584
Anthony Liguori7267c092011-08-20 22:09:37 -05001585 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001586
1587 freq = has_freq ? freq : 44100;
1588 bits = has_bits ? bits : 16;
1589 nchannels = has_channels ? nchannels : 2;
1590
1591 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001592 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05001593 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001594 return;
bellardec36b692006-07-16 18:57:03 +00001595 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001596 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001597}
bellardec36b692006-07-16 18:57:03 +00001598
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001599static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001600{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001601 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001602
aliguori76655d62009-03-06 20:27:37 +00001603 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001604 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001605 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001606 return acl;
1607}
aliguori76655d62009-03-06 20:27:37 +00001608
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001609static void hmp_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001610{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001611 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001612 qemu_acl *acl = find_acl(mon, aclname);
1613 qemu_acl_entry *entry;
1614 int i = 0;
1615
1616 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001617 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001618 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00001619 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00001620 i++;
1621 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001622 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001623 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001624 }
1625}
1626
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001627static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001628{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001629 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001630 qemu_acl *acl = find_acl(mon, aclname);
1631
1632 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001633 qemu_acl_reset(acl);
1634 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001635 }
1636}
aliguori76655d62009-03-06 20:27:37 +00001637
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001638static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001639{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001640 const char *aclname = qdict_get_str(qdict, "aclname");
1641 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001642 qemu_acl *acl = find_acl(mon, aclname);
1643
1644 if (acl) {
1645 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001646 acl->defaultDeny = 0;
1647 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001648 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001649 acl->defaultDeny = 1;
1650 monitor_printf(mon, "acl: policy set to 'deny'\n");
1651 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001652 monitor_printf(mon, "acl: unknown policy '%s', "
1653 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001654 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001655 }
1656}
aliguori76655d62009-03-06 20:27:37 +00001657
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001658static void hmp_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001659{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001660 const char *aclname = qdict_get_str(qdict, "aclname");
1661 const char *match = qdict_get_str(qdict, "match");
1662 const char *policy = qdict_get_str(qdict, "policy");
1663 int has_index = qdict_haskey(qdict, "index");
1664 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001665 qemu_acl *acl = find_acl(mon, aclname);
1666 int deny, ret;
1667
1668 if (acl) {
1669 if (strcmp(policy, "allow") == 0) {
1670 deny = 0;
1671 } else if (strcmp(policy, "deny") == 0) {
1672 deny = 1;
1673 } else {
1674 monitor_printf(mon, "acl: unknown policy '%s', "
1675 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001676 return;
1677 }
aliguori28a76be2009-03-06 20:27:40 +00001678 if (has_index)
1679 ret = qemu_acl_insert(acl, deny, match, index);
1680 else
1681 ret = qemu_acl_append(acl, deny, match);
1682 if (ret < 0)
1683 monitor_printf(mon, "acl: unable to add acl entry\n");
1684 else
1685 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001686 }
1687}
aliguori76655d62009-03-06 20:27:37 +00001688
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001689static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001690{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001691 const char *aclname = qdict_get_str(qdict, "aclname");
1692 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001693 qemu_acl *acl = find_acl(mon, aclname);
1694 int ret;
aliguori76655d62009-03-06 20:27:37 +00001695
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001696 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001697 ret = qemu_acl_remove(acl, match);
1698 if (ret < 0)
1699 monitor_printf(mon, "acl: no matching acl entry\n");
1700 else
1701 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001702 }
1703}
1704
Corey Bryant208c9d12012-06-22 14:36:09 -04001705void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001706{
Anthony Liguoric227f092009-10-01 16:12:16 -05001707 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001708 int fd;
1709
Corey Bryant208c9d12012-06-22 14:36:09 -04001710 fd = qemu_chr_fe_get_msgfd(cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001711 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001712 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryant208c9d12012-06-22 14:36:09 -04001713 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001714 }
1715
1716 if (qemu_isdigit(fdname[0])) {
Stefan Hajnoczi0b9f0e22014-04-24 13:58:18 +02001717 close(fd);
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001718 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
1719 "a name not starting with a digit");
Corey Bryant208c9d12012-06-22 14:36:09 -04001720 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001721 }
1722
Corey Bryant208c9d12012-06-22 14:36:09 -04001723 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001724 if (strcmp(monfd->name, fdname) != 0) {
1725 continue;
1726 }
1727
1728 close(monfd->fd);
1729 monfd->fd = fd;
Corey Bryant208c9d12012-06-22 14:36:09 -04001730 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001731 }
1732
Anthony Liguori7267c092011-08-20 22:09:37 -05001733 monfd = g_malloc0(sizeof(mon_fd_t));
1734 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001735 monfd->fd = fd;
1736
Corey Bryant208c9d12012-06-22 14:36:09 -04001737 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001738}
1739
Corey Bryant208c9d12012-06-22 14:36:09 -04001740void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001741{
Anthony Liguoric227f092009-10-01 16:12:16 -05001742 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001743
Corey Bryant208c9d12012-06-22 14:36:09 -04001744 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001745 if (strcmp(monfd->name, fdname) != 0) {
1746 continue;
1747 }
1748
Blue Swirl72cf2d42009-09-12 07:36:22 +00001749 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001750 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001751 g_free(monfd->name);
1752 g_free(monfd);
Corey Bryant208c9d12012-06-22 14:36:09 -04001753 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001754 }
1755
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001756 error_setg(errp, QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001757}
1758
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001759static void hmp_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001760{
Luiz Capitulino13548692011-07-29 15:36:43 -03001761 int saved_vm_running = runstate_is_running();
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001762 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02001763
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001764 vm_stop(RUN_STATE_RESTORE_VM);
Juan Quintelac8d41b22009-08-20 19:42:21 +02001765
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03001766 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02001767 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03001768 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02001769}
1770
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001771int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
Mark McLoughlin7768e042009-07-22 09:11:41 +01001772{
Anthony Liguoric227f092009-10-01 16:12:16 -05001773 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01001774
Blue Swirl72cf2d42009-09-12 07:36:22 +00001775 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01001776 int fd;
1777
1778 if (strcmp(monfd->name, fdname) != 0) {
1779 continue;
1780 }
1781
1782 fd = monfd->fd;
1783
1784 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001785 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05001786 g_free(monfd->name);
1787 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001788
1789 return fd;
1790 }
1791
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001792 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001793 return -1;
1794}
1795
Corey Bryantba1c0482012-08-14 16:43:43 -04001796static void monitor_fdset_cleanup(MonFdset *mon_fdset)
1797{
1798 MonFdsetFd *mon_fdset_fd;
1799 MonFdsetFd *mon_fdset_fd_next;
1800
1801 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantebe52b52012-10-18 15:19:33 -04001802 if ((mon_fdset_fd->removed ||
1803 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
1804 runstate_is_running()) {
Corey Bryantba1c0482012-08-14 16:43:43 -04001805 close(mon_fdset_fd->fd);
1806 g_free(mon_fdset_fd->opaque);
1807 QLIST_REMOVE(mon_fdset_fd, next);
1808 g_free(mon_fdset_fd);
1809 }
1810 }
1811
Corey Bryantadb696f2012-08-14 16:43:47 -04001812 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04001813 QLIST_REMOVE(mon_fdset, next);
1814 g_free(mon_fdset);
1815 }
1816}
1817
Corey Bryantefb87c12012-08-14 16:43:48 -04001818static void monitor_fdsets_cleanup(void)
1819{
1820 MonFdset *mon_fdset;
1821 MonFdset *mon_fdset_next;
1822
1823 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
1824 monitor_fdset_cleanup(mon_fdset);
1825 }
1826}
1827
Corey Bryantba1c0482012-08-14 16:43:43 -04001828AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
1829 const char *opaque, Error **errp)
1830{
1831 int fd;
1832 Monitor *mon = cur_mon;
Corey Bryantba1c0482012-08-14 16:43:43 -04001833 AddfdInfo *fdinfo;
1834
1835 fd = qemu_chr_fe_get_msgfd(mon->chr);
1836 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001837 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryantba1c0482012-08-14 16:43:43 -04001838 goto error;
1839 }
1840
Corey Bryante446f702012-10-18 15:19:32 -04001841 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
1842 has_opaque, opaque, errp);
1843 if (fdinfo) {
1844 return fdinfo;
Corey Bryant9ac54af2012-10-18 15:19:31 -04001845 }
1846
Corey Bryantba1c0482012-08-14 16:43:43 -04001847error:
1848 if (fd != -1) {
1849 close(fd);
1850 }
1851 return NULL;
1852}
1853
1854void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
1855{
1856 MonFdset *mon_fdset;
1857 MonFdsetFd *mon_fdset_fd;
1858 char fd_str[60];
1859
1860 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1861 if (mon_fdset->id != fdset_id) {
1862 continue;
1863 }
1864 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1865 if (has_fd) {
1866 if (mon_fdset_fd->fd != fd) {
1867 continue;
1868 }
1869 mon_fdset_fd->removed = true;
1870 break;
1871 } else {
1872 mon_fdset_fd->removed = true;
1873 }
1874 }
1875 if (has_fd && !mon_fdset_fd) {
1876 goto error;
1877 }
1878 monitor_fdset_cleanup(mon_fdset);
1879 return;
1880 }
1881
1882error:
1883 if (has_fd) {
1884 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
1885 fdset_id, fd);
1886 } else {
1887 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
1888 }
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001889 error_setg(errp, QERR_FD_NOT_FOUND, fd_str);
Corey Bryantba1c0482012-08-14 16:43:43 -04001890}
1891
1892FdsetInfoList *qmp_query_fdsets(Error **errp)
1893{
1894 MonFdset *mon_fdset;
1895 MonFdsetFd *mon_fdset_fd;
1896 FdsetInfoList *fdset_list = NULL;
1897
1898 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1899 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
1900 FdsetFdInfoList *fdsetfd_list = NULL;
1901
1902 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
1903 fdset_info->value->fdset_id = mon_fdset->id;
1904
1905 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1906 FdsetFdInfoList *fdsetfd_info;
1907
1908 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
1909 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
1910 fdsetfd_info->value->fd = mon_fdset_fd->fd;
1911 if (mon_fdset_fd->opaque) {
1912 fdsetfd_info->value->has_opaque = true;
1913 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
1914 } else {
1915 fdsetfd_info->value->has_opaque = false;
1916 }
1917
1918 fdsetfd_info->next = fdsetfd_list;
1919 fdsetfd_list = fdsetfd_info;
1920 }
1921
1922 fdset_info->value->fds = fdsetfd_list;
1923
1924 fdset_info->next = fdset_list;
1925 fdset_list = fdset_info;
1926 }
1927
1928 return fdset_list;
1929}
1930
Corey Bryante446f702012-10-18 15:19:32 -04001931AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
1932 bool has_opaque, const char *opaque,
1933 Error **errp)
1934{
1935 MonFdset *mon_fdset = NULL;
1936 MonFdsetFd *mon_fdset_fd;
1937 AddfdInfo *fdinfo;
1938
1939 if (has_fdset_id) {
1940 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1941 /* Break if match found or match impossible due to ordering by ID */
1942 if (fdset_id <= mon_fdset->id) {
1943 if (fdset_id < mon_fdset->id) {
1944 mon_fdset = NULL;
1945 }
1946 break;
1947 }
1948 }
1949 }
1950
1951 if (mon_fdset == NULL) {
1952 int64_t fdset_id_prev = -1;
1953 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
1954
1955 if (has_fdset_id) {
1956 if (fdset_id < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001957 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
1958 "a non-negative value");
Corey Bryante446f702012-10-18 15:19:32 -04001959 return NULL;
1960 }
1961 /* Use specified fdset ID */
1962 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1963 mon_fdset_cur = mon_fdset;
1964 if (fdset_id < mon_fdset_cur->id) {
1965 break;
1966 }
1967 }
1968 } else {
1969 /* Use first available fdset ID */
1970 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1971 mon_fdset_cur = mon_fdset;
1972 if (fdset_id_prev == mon_fdset_cur->id - 1) {
1973 fdset_id_prev = mon_fdset_cur->id;
1974 continue;
1975 }
1976 break;
1977 }
1978 }
1979
1980 mon_fdset = g_malloc0(sizeof(*mon_fdset));
1981 if (has_fdset_id) {
1982 mon_fdset->id = fdset_id;
1983 } else {
1984 mon_fdset->id = fdset_id_prev + 1;
1985 }
1986
1987 /* The fdset list is ordered by fdset ID */
1988 if (!mon_fdset_cur) {
1989 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
1990 } else if (mon_fdset->id < mon_fdset_cur->id) {
1991 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
1992 } else {
1993 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
1994 }
1995 }
1996
1997 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
1998 mon_fdset_fd->fd = fd;
1999 mon_fdset_fd->removed = false;
2000 if (has_opaque) {
2001 mon_fdset_fd->opaque = g_strdup(opaque);
2002 }
2003 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
2004
2005 fdinfo = g_malloc0(sizeof(*fdinfo));
2006 fdinfo->fdset_id = mon_fdset->id;
2007 fdinfo->fd = mon_fdset_fd->fd;
2008
2009 return fdinfo;
2010}
2011
Corey Bryantadb696f2012-08-14 16:43:47 -04002012int monitor_fdset_get_fd(int64_t fdset_id, int flags)
2013{
Blue Swirlb2dc64c2012-08-18 20:14:54 +00002014#ifndef _WIN32
Corey Bryantadb696f2012-08-14 16:43:47 -04002015 MonFdset *mon_fdset;
2016 MonFdsetFd *mon_fdset_fd;
2017 int mon_fd_flags;
2018
Corey Bryantadb696f2012-08-14 16:43:47 -04002019 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2020 if (mon_fdset->id != fdset_id) {
2021 continue;
2022 }
2023 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2024 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
2025 if (mon_fd_flags == -1) {
2026 return -1;
2027 }
2028
2029 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
2030 return mon_fdset_fd->fd;
2031 }
2032 }
2033 errno = EACCES;
2034 return -1;
2035 }
2036#endif
2037
2038 errno = ENOENT;
2039 return -1;
2040}
2041
2042int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
2043{
2044 MonFdset *mon_fdset;
2045 MonFdsetFd *mon_fdset_fd_dup;
2046
2047 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2048 if (mon_fdset->id != fdset_id) {
2049 continue;
2050 }
2051 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2052 if (mon_fdset_fd_dup->fd == dup_fd) {
2053 return -1;
2054 }
2055 }
2056 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2057 mon_fdset_fd_dup->fd = dup_fd;
2058 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
2059 return 0;
2060 }
2061 return -1;
2062}
2063
2064static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2065{
2066 MonFdset *mon_fdset;
2067 MonFdsetFd *mon_fdset_fd_dup;
2068
2069 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2070 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2071 if (mon_fdset_fd_dup->fd == dup_fd) {
2072 if (remove) {
2073 QLIST_REMOVE(mon_fdset_fd_dup, next);
2074 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2075 monitor_fdset_cleanup(mon_fdset);
2076 }
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002077 return -1;
2078 } else {
2079 return mon_fdset->id;
Corey Bryantadb696f2012-08-14 16:43:47 -04002080 }
Corey Bryantadb696f2012-08-14 16:43:47 -04002081 }
2082 }
2083 }
2084 return -1;
2085}
2086
2087int monitor_fdset_dup_fd_find(int dup_fd)
2088{
2089 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2090}
2091
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002092void monitor_fdset_dup_fd_remove(int dup_fd)
Corey Bryantadb696f2012-08-14 16:43:47 -04002093{
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002094 monitor_fdset_dup_fd_find_remove(dup_fd, true);
Corey Bryantadb696f2012-08-14 16:43:47 -04002095}
2096
Markus Armbruster1677f4c2015-02-09 14:03:19 +01002097int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
Laszlo Ersek59063662014-04-10 10:24:31 +02002098{
2099 int fd;
2100 Error *local_err = NULL;
2101
2102 if (!qemu_isdigit(fdname[0]) && mon) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002103 fd = monitor_get_fd(mon, fdname, &local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002104 } else {
2105 fd = qemu_parse_fd(fdname);
Laszlo Ersek59063662014-04-10 10:24:31 +02002106 if (fd == -1) {
2107 error_setg(&local_err, "Invalid file descriptor number '%s'",
2108 fdname);
2109 }
2110 }
2111 if (local_err) {
2112 error_propagate(errp, local_err);
2113 assert(fd == -1);
2114 } else {
2115 assert(fd != -1);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002116 }
2117
2118 return fd;
2119}
2120
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002121/* Please update hmp-commands.hx when adding or changing commands */
Wayne Xia816f8922011-10-12 11:32:41 +08002122static mon_cmd_t info_cmds[] = {
Pavel Butsykinda76ee72015-09-10 18:38:58 +03002123#include "hmp-commands-info.h"
2124 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002125};
2126
Wenchao Xiaa13ced52013-01-14 14:06:28 +08002127/* mon_cmds and info_cmds would be sorted at runtime */
2128static mon_cmd_t mon_cmds[] = {
2129#include "hmp-commands.h"
2130 { NULL, NULL, },
2131};
2132
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002133static const mon_cmd_t qmp_cmds[] = {
Anthony Liguorie3193602011-09-02 12:34:47 -05002134#include "qmp-commands-old.h"
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002135 { /* NULL */ },
2136};
2137
bellard9307c4c2004-04-04 12:57:25 +00002138/*******************************************************************/
2139
2140static const char *pch;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002141static sigjmp_buf expr_env;
bellard9307c4c2004-04-04 12:57:25 +00002142
bellard9307c4c2004-04-04 12:57:25 +00002143
Stefan Weil9c3175c2013-08-22 21:30:09 +02002144static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
2145expr_error(Monitor *mon, const char *fmt, ...)
bellard9dc39cb2004-03-14 21:38:27 +00002146{
Fam Zheng277acfe2013-08-20 10:58:21 +08002147 va_list ap;
2148 va_start(ap, fmt);
2149 monitor_vprintf(mon, fmt, ap);
2150 monitor_printf(mon, "\n");
2151 va_end(ap);
Peter Maydell6ab7e542013-02-20 15:21:09 +00002152 siglongjmp(expr_env, 1);
bellard9307c4c2004-04-04 12:57:25 +00002153}
2154
Markus Armbruster09b94182010-01-20 13:07:30 +01002155/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00002156static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002157{
Pavel Butsykinbf957282015-09-10 18:38:59 +03002158 const MonitorDef *md = target_monitor_defs();
bellard92a31b12005-02-10 22:00:52 +00002159 void *ptr;
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002160 uint64_t tmp = 0;
2161 int ret;
bellard92a31b12005-02-10 22:00:52 +00002162
Pavel Butsykinbf957282015-09-10 18:38:59 +03002163 if (md == NULL) {
2164 return -1;
2165 }
2166
2167 for(; md->name != NULL; md++) {
bellard9307c4c2004-04-04 12:57:25 +00002168 if (compare_cmd(name, md->name)) {
2169 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002170 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002171 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002172 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00002173 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002174 switch(md->type) {
2175 case MD_I32:
2176 *pval = *(int32_t *)ptr;
2177 break;
2178 case MD_TLONG:
2179 *pval = *(target_long *)ptr;
2180 break;
2181 default:
2182 *pval = 0;
2183 break;
2184 }
bellard9307c4c2004-04-04 12:57:25 +00002185 }
2186 return 0;
2187 }
2188 }
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002189
2190 ret = target_get_monitor_def(mon_get_cpu(), name, &tmp);
2191 if (!ret) {
2192 *pval = (target_long) tmp;
2193 }
2194
2195 return ret;
bellard9307c4c2004-04-04 12:57:25 +00002196}
2197
2198static void next(void)
2199{
Blue Swirl660f11b2009-07-31 21:16:51 +00002200 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002201 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002202 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002203 pch++;
2204 }
2205}
2206
aliguori376253e2009-03-05 23:01:23 +00002207static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002208
aliguori376253e2009-03-05 23:01:23 +00002209static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002210{
blueswir1c2efc952007-09-25 17:28:42 +00002211 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002212 char *p;
bellard6a00d602005-11-21 23:25:50 +00002213 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002214
2215 switch(*pch) {
2216 case '+':
2217 next();
aliguori376253e2009-03-05 23:01:23 +00002218 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002219 break;
2220 case '-':
2221 next();
aliguori376253e2009-03-05 23:01:23 +00002222 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002223 break;
2224 case '~':
2225 next();
aliguori376253e2009-03-05 23:01:23 +00002226 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002227 break;
2228 case '(':
2229 next();
aliguori376253e2009-03-05 23:01:23 +00002230 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002231 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002232 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002233 }
2234 next();
2235 break;
bellard81d09122004-07-14 17:21:37 +00002236 case '\'':
2237 pch++;
2238 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002239 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002240 n = *pch;
2241 pch++;
2242 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002243 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002244 next();
2245 break;
bellard9307c4c2004-04-04 12:57:25 +00002246 case '$':
2247 {
2248 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002249 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002250
bellard9307c4c2004-04-04 12:57:25 +00002251 pch++;
2252 q = buf;
2253 while ((*pch >= 'a' && *pch <= 'z') ||
2254 (*pch >= 'A' && *pch <= 'Z') ||
2255 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002256 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002257 if ((q - buf) < sizeof(buf) - 1)
2258 *q++ = *pch;
2259 pch++;
2260 }
blueswir1cd390082008-11-16 13:53:32 +00002261 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002262 pch++;
2263 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002264 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01002265 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00002266 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00002267 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002268 }
2269 break;
2270 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002271 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002272 n = 0;
2273 break;
2274 default:
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002275 errno = 0;
bellard4f4fbf72006-06-25 18:28:12 +00002276 n = strtoull(pch, &p, 0);
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002277 if (errno == ERANGE) {
2278 expr_error(mon, "number too large");
2279 }
bellard9307c4c2004-04-04 12:57:25 +00002280 if (pch == p) {
Fam Zheng277acfe2013-08-20 10:58:21 +08002281 expr_error(mon, "invalid char '%c' in expression", *p);
bellard9307c4c2004-04-04 12:57:25 +00002282 }
2283 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002284 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002285 pch++;
2286 break;
2287 }
2288 return n;
2289}
2290
2291
aliguori376253e2009-03-05 23:01:23 +00002292static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002293{
blueswir1c2efc952007-09-25 17:28:42 +00002294 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002295 int op;
ths3b46e622007-09-17 08:09:54 +00002296
aliguori376253e2009-03-05 23:01:23 +00002297 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002298 for(;;) {
2299 op = *pch;
2300 if (op != '*' && op != '/' && op != '%')
2301 break;
2302 next();
aliguori376253e2009-03-05 23:01:23 +00002303 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002304 switch(op) {
2305 default:
2306 case '*':
2307 val *= val2;
2308 break;
2309 case '/':
2310 case '%':
ths5fafdf22007-09-16 21:08:06 +00002311 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002312 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002313 if (op == '/')
2314 val /= val2;
2315 else
2316 val %= val2;
2317 break;
2318 }
2319 }
2320 return val;
2321}
2322
aliguori376253e2009-03-05 23:01:23 +00002323static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002324{
blueswir1c2efc952007-09-25 17:28:42 +00002325 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002326 int op;
bellard9307c4c2004-04-04 12:57:25 +00002327
aliguori376253e2009-03-05 23:01:23 +00002328 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002329 for(;;) {
2330 op = *pch;
2331 if (op != '&' && op != '|' && op != '^')
2332 break;
2333 next();
aliguori376253e2009-03-05 23:01:23 +00002334 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002335 switch(op) {
2336 default:
2337 case '&':
2338 val &= val2;
2339 break;
2340 case '|':
2341 val |= val2;
2342 break;
2343 case '^':
2344 val ^= val2;
2345 break;
2346 }
2347 }
2348 return val;
2349}
2350
aliguori376253e2009-03-05 23:01:23 +00002351static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002352{
blueswir1c2efc952007-09-25 17:28:42 +00002353 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002354 int op;
bellard9307c4c2004-04-04 12:57:25 +00002355
aliguori376253e2009-03-05 23:01:23 +00002356 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002357 for(;;) {
2358 op = *pch;
2359 if (op != '+' && op != '-')
2360 break;
2361 next();
aliguori376253e2009-03-05 23:01:23 +00002362 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002363 if (op == '+')
2364 val += val2;
2365 else
2366 val -= val2;
2367 }
2368 return val;
2369}
2370
aliguori376253e2009-03-05 23:01:23 +00002371static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002372{
2373 pch = *pp;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002374 if (sigsetjmp(expr_env, 0)) {
bellard9307c4c2004-04-04 12:57:25 +00002375 *pp = pch;
2376 return -1;
2377 }
blueswir1cd390082008-11-16 13:53:32 +00002378 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002379 pch++;
aliguori376253e2009-03-05 23:01:23 +00002380 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002381 *pp = pch;
2382 return 0;
2383}
2384
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002385static int get_double(Monitor *mon, double *pval, const char **pp)
2386{
2387 const char *p = *pp;
2388 char *tailp;
2389 double d;
2390
2391 d = strtod(p, &tailp);
2392 if (tailp == p) {
2393 monitor_printf(mon, "Number expected\n");
2394 return -1;
2395 }
2396 if (d != d || d - d != 0) {
2397 /* NaN or infinity */
2398 monitor_printf(mon, "Bad number\n");
2399 return -1;
2400 }
2401 *pval = d;
2402 *pp = tailp;
2403 return 0;
2404}
2405
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002406/*
2407 * Store the command-name in cmdname, and return a pointer to
2408 * the remaining of the command string.
2409 */
2410static const char *get_command_name(const char *cmdline,
2411 char *cmdname, size_t nlen)
2412{
2413 size_t len;
2414 const char *p, *pstart;
2415
2416 p = cmdline;
2417 while (qemu_isspace(*p))
2418 p++;
2419 if (*p == '\0')
2420 return NULL;
2421 pstart = p;
2422 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2423 p++;
2424 len = p - pstart;
2425 if (len > nlen - 1)
2426 len = nlen - 1;
2427 memcpy(cmdname, pstart, len);
2428 cmdname[len] = '\0';
2429 return p;
2430}
2431
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002432/**
2433 * Read key of 'type' into 'key' and return the current
2434 * 'type' pointer.
2435 */
2436static char *key_get_info(const char *type, char **key)
2437{
2438 size_t len;
2439 char *p, *str;
2440
2441 if (*type == ',')
2442 type++;
2443
2444 p = strchr(type, ':');
2445 if (!p) {
2446 *key = NULL;
2447 return NULL;
2448 }
2449 len = p - type;
2450
Anthony Liguori7267c092011-08-20 22:09:37 -05002451 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002452 memcpy(str, type, len);
2453 str[len] = '\0';
2454
2455 *key = str;
2456 return ++p;
2457}
2458
bellard9307c4c2004-04-04 12:57:25 +00002459static int default_fmt_format = 'x';
2460static int default_fmt_size = 4;
2461
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002462static int is_valid_option(const char *c, const char *typestr)
2463{
2464 char option[3];
2465
2466 option[0] = '-';
2467 option[1] = *c;
2468 option[2] = '\0';
2469
2470 typestr = strstr(typestr, option);
2471 return (typestr != NULL);
2472}
2473
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03002474static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
2475 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002476{
2477 const mon_cmd_t *cmd;
2478
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03002479 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002480 if (compare_cmd(cmdname, cmd->name)) {
2481 return cmd;
2482 }
2483 }
2484
2485 return NULL;
2486}
2487
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03002488static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
2489{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002490 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03002491}
2492
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002493/*
Bandan Dasae502122015-06-03 18:38:08 -04002494 * Parse command name from @cmdp according to command table @table.
2495 * If blank, return NULL.
2496 * Else, if no valid command can be found, report to @mon, and return
2497 * NULL.
2498 * Else, change @cmdp to point right behind the name, and return its
2499 * command table entry.
2500 * Do not assume the return value points into @table! It doesn't when
2501 * the command is found in a sub-command table.
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002502 */
Anthony Liguoric227f092009-10-01 16:12:16 -05002503static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Bandan Dasae502122015-06-03 18:38:08 -04002504 const char **cmdp,
2505 mon_cmd_t *table)
bellard9307c4c2004-04-04 12:57:25 +00002506{
Bandan Dasae502122015-06-03 18:38:08 -04002507 const char *p;
Anthony Liguoric227f092009-10-01 16:12:16 -05002508 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002509 char cmdname[256];
bellard9dc39cb2004-03-14 21:38:27 +00002510
bellard9307c4c2004-04-04 12:57:25 +00002511 /* extract the command name */
Bandan Dasae502122015-06-03 18:38:08 -04002512 p = get_command_name(*cmdp, cmdname, sizeof(cmdname));
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002513 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002514 return NULL;
ths3b46e622007-09-17 08:09:54 +00002515
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002516 cmd = search_dispatch_table(table, cmdname);
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002517 if (!cmd) {
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002518 monitor_printf(mon, "unknown command: '%.*s'\n",
Bandan Dasae502122015-06-03 18:38:08 -04002519 (int)(p - *cmdp), *cmdp);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002520 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002521 }
bellard9307c4c2004-04-04 12:57:25 +00002522
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002523 /* filter out following useless space */
2524 while (qemu_isspace(*p)) {
2525 p++;
2526 }
Bandan Dasae502122015-06-03 18:38:08 -04002527
2528 *cmdp = p;
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002529 /* search sub command */
Bandan Dasae502122015-06-03 18:38:08 -04002530 if (cmd->sub_table != NULL && *p != '\0') {
2531 return monitor_parse_command(mon, cmdp, cmd->sub_table);
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002532 }
2533
Bandan Dasae502122015-06-03 18:38:08 -04002534 return cmd;
2535}
2536
2537/*
2538 * Parse arguments for @cmd.
2539 * If it can't be parsed, report to @mon, and return NULL.
2540 * Else, insert command arguments into a QDict, and return it.
2541 * Note: On success, caller has to free the QDict structure.
2542 */
2543
2544static QDict *monitor_parse_arguments(Monitor *mon,
2545 const char **endp,
2546 const mon_cmd_t *cmd)
2547{
2548 const char *typestr;
2549 char *key;
2550 int c;
2551 const char *p = *endp;
2552 char buf[1024];
2553 QDict *qdict = qdict_new();
2554
bellard9307c4c2004-04-04 12:57:25 +00002555 /* parse the parameters */
2556 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00002557 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002558 typestr = key_get_info(typestr, &key);
2559 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00002560 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002561 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00002562 typestr++;
2563 switch(c) {
2564 case 'F':
bellard81d09122004-07-14 17:21:37 +00002565 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002566 case 's':
2567 {
2568 int ret;
ths3b46e622007-09-17 08:09:54 +00002569
blueswir1cd390082008-11-16 13:53:32 +00002570 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002571 p++;
2572 if (*typestr == '?') {
2573 typestr++;
2574 if (*p == '\0') {
2575 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03002576 break;
bellard9307c4c2004-04-04 12:57:25 +00002577 }
2578 }
2579 ret = get_str(buf, sizeof(buf), &p);
2580 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002581 switch(c) {
2582 case 'F':
aliguori376253e2009-03-05 23:01:23 +00002583 monitor_printf(mon, "%s: filename expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002584 cmd->name);
bellard81d09122004-07-14 17:21:37 +00002585 break;
2586 case 'B':
aliguori376253e2009-03-05 23:01:23 +00002587 monitor_printf(mon, "%s: block device name expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002588 cmd->name);
bellard81d09122004-07-14 17:21:37 +00002589 break;
2590 default:
Bandan Dasae502122015-06-03 18:38:08 -04002591 monitor_printf(mon, "%s: string expected\n", cmd->name);
bellard81d09122004-07-14 17:21:37 +00002592 break;
2593 }
bellard9307c4c2004-04-04 12:57:25 +00002594 goto fail;
2595 }
Luiz Capitulino53773582009-08-28 15:27:25 -03002596 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00002597 }
2598 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01002599 case 'O':
2600 {
2601 QemuOptsList *opts_list;
2602 QemuOpts *opts;
2603
2604 opts_list = qemu_find_opts(key);
2605 if (!opts_list || opts_list->desc->name) {
2606 goto bad_type;
2607 }
2608 while (qemu_isspace(*p)) {
2609 p++;
2610 }
2611 if (!*p)
2612 break;
2613 if (get_str(buf, sizeof(buf), &p) < 0) {
2614 goto fail;
2615 }
Markus Armbruster70b94332015-02-13 12:50:26 +01002616 opts = qemu_opts_parse_noisily(opts_list, buf, true);
Markus Armbruster361127d2010-02-10 20:24:35 +01002617 if (!opts) {
2618 goto fail;
2619 }
2620 qemu_opts_to_qdict(opts, qdict);
2621 qemu_opts_del(opts);
2622 }
2623 break;
bellard9307c4c2004-04-04 12:57:25 +00002624 case '/':
2625 {
2626 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002627
blueswir1cd390082008-11-16 13:53:32 +00002628 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002629 p++;
2630 if (*p == '/') {
2631 /* format found */
2632 p++;
2633 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002634 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002635 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002636 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002637 count = count * 10 + (*p - '0');
2638 p++;
2639 }
2640 }
2641 size = -1;
2642 format = -1;
2643 for(;;) {
2644 switch(*p) {
2645 case 'o':
2646 case 'd':
2647 case 'u':
2648 case 'x':
2649 case 'i':
2650 case 'c':
2651 format = *p++;
2652 break;
2653 case 'b':
2654 size = 1;
2655 p++;
2656 break;
2657 case 'h':
2658 size = 2;
2659 p++;
2660 break;
2661 case 'w':
2662 size = 4;
2663 p++;
2664 break;
2665 case 'g':
2666 case 'L':
2667 size = 8;
2668 p++;
2669 break;
2670 default:
2671 goto next;
2672 }
2673 }
2674 next:
blueswir1cd390082008-11-16 13:53:32 +00002675 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00002676 monitor_printf(mon, "invalid char in format: '%c'\n",
2677 *p);
bellard9307c4c2004-04-04 12:57:25 +00002678 goto fail;
2679 }
bellard9307c4c2004-04-04 12:57:25 +00002680 if (format < 0)
2681 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002682 if (format != 'i') {
2683 /* for 'i', not specifying a size gives -1 as size */
2684 if (size < 0)
2685 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002686 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002687 }
bellard9307c4c2004-04-04 12:57:25 +00002688 default_fmt_format = format;
2689 } else {
2690 count = 1;
2691 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002692 if (format != 'i') {
2693 size = default_fmt_size;
2694 } else {
2695 size = -1;
2696 }
bellard9307c4c2004-04-04 12:57:25 +00002697 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002698 qdict_put(qdict, "count", qint_from_int(count));
2699 qdict_put(qdict, "format", qint_from_int(format));
2700 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00002701 }
2702 break;
2703 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002704 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002705 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00002706 {
blueswir1c2efc952007-09-25 17:28:42 +00002707 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00002708
blueswir1cd390082008-11-16 13:53:32 +00002709 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002710 p++;
bellard34405572004-06-08 00:55:58 +00002711 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002712 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03002713 if (*p == '\0') {
2714 typestr++;
2715 break;
2716 }
bellard34405572004-06-08 00:55:58 +00002717 } else {
2718 if (*p == '.') {
2719 p++;
blueswir1cd390082008-11-16 13:53:32 +00002720 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00002721 p++;
bellard34405572004-06-08 00:55:58 +00002722 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03002723 typestr++;
2724 break;
bellard34405572004-06-08 00:55:58 +00002725 }
2726 }
bellard13224a82006-07-14 22:03:35 +00002727 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002728 }
aliguori376253e2009-03-05 23:01:23 +00002729 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00002730 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002731 /* Check if 'i' is greater than 32-bit */
2732 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
Bandan Dasae502122015-06-03 18:38:08 -04002733 monitor_printf(mon, "\'%s\' has failed: ", cmd->name);
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002734 monitor_printf(mon, "integer is for 32-bit values\n");
2735 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002736 } else if (c == 'M') {
Luiz Capitulino91162842012-04-26 17:34:30 -03002737 if (val < 0) {
2738 monitor_printf(mon, "enter a positive value\n");
2739 goto fail;
2740 }
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002741 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002742 }
Luiz Capitulino53773582009-08-28 15:27:25 -03002743 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00002744 }
2745 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02002746 case 'o':
2747 {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01002748 int64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02002749 char *end;
2750
2751 while (qemu_isspace(*p)) {
2752 p++;
2753 }
2754 if (*typestr == '?') {
2755 typestr++;
2756 if (*p == '\0') {
2757 break;
2758 }
2759 }
Marc-André Lureau4677bb42015-09-16 18:02:56 +02002760 val = qemu_strtosz(p, &end);
Jes Sorensendbc0c672010-10-21 17:15:47 +02002761 if (val < 0) {
2762 monitor_printf(mon, "invalid size\n");
2763 goto fail;
2764 }
2765 qdict_put(qdict, key, qint_from_int(val));
2766 p = end;
2767 }
2768 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01002769 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002770 {
2771 double val;
2772
2773 while (qemu_isspace(*p))
2774 p++;
2775 if (*typestr == '?') {
2776 typestr++;
2777 if (*p == '\0') {
2778 break;
2779 }
2780 }
2781 if (get_double(mon, &val, &p) < 0) {
2782 goto fail;
2783 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02002784 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01002785 switch (*p) {
2786 case 'm':
2787 val /= 1e3; p += 2; break;
2788 case 'u':
2789 val /= 1e6; p += 2; break;
2790 case 'n':
2791 val /= 1e9; p += 2; break;
2792 }
2793 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002794 if (*p && !qemu_isspace(*p)) {
2795 monitor_printf(mon, "Unknown unit suffix\n");
2796 goto fail;
2797 }
2798 qdict_put(qdict, key, qfloat_from_double(val));
2799 }
2800 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002801 case 'b':
2802 {
2803 const char *beg;
Eric Blakefc48ffc2015-05-15 16:24:59 -06002804 bool val;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002805
2806 while (qemu_isspace(*p)) {
2807 p++;
2808 }
2809 beg = p;
2810 while (qemu_isgraph(*p)) {
2811 p++;
2812 }
2813 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06002814 val = true;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002815 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06002816 val = false;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002817 } else {
2818 monitor_printf(mon, "Expected 'on' or 'off'\n");
2819 goto fail;
2820 }
Eric Blakefc48ffc2015-05-15 16:24:59 -06002821 qdict_put(qdict, key, qbool_from_bool(val));
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002822 }
2823 break;
bellard9307c4c2004-04-04 12:57:25 +00002824 case '-':
2825 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002826 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03002827 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00002828 /* option */
ths3b46e622007-09-17 08:09:54 +00002829
bellard9307c4c2004-04-04 12:57:25 +00002830 c = *typestr++;
2831 if (c == '\0')
2832 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00002833 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002834 p++;
bellard9307c4c2004-04-04 12:57:25 +00002835 if (*p == '-') {
2836 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002837 if(c != *p) {
2838 if(!is_valid_option(p, typestr)) {
2839
2840 monitor_printf(mon, "%s: unsupported option -%c\n",
Bandan Dasae502122015-06-03 18:38:08 -04002841 cmd->name, *p);
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002842 goto fail;
2843 } else {
2844 skip_key = 1;
2845 }
bellard9307c4c2004-04-04 12:57:25 +00002846 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002847 if(skip_key) {
2848 p = tmp;
2849 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03002850 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002851 p++;
Eric Blakefc48ffc2015-05-15 16:24:59 -06002852 qdict_put(qdict, key, qbool_from_bool(true));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002853 }
bellard9307c4c2004-04-04 12:57:25 +00002854 }
bellard9307c4c2004-04-04 12:57:25 +00002855 }
2856 break;
Wenchao Xia129be002013-08-27 20:38:26 +08002857 case 'S':
2858 {
2859 /* package all remaining string */
2860 int len;
2861
2862 while (qemu_isspace(*p)) {
2863 p++;
2864 }
2865 if (*typestr == '?') {
2866 typestr++;
2867 if (*p == '\0') {
2868 /* no remaining string: NULL argument */
2869 break;
2870 }
2871 }
2872 len = strlen(p);
2873 if (len <= 0) {
2874 monitor_printf(mon, "%s: string expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002875 cmd->name);
Bandan Dase549d2a2015-06-03 18:38:10 -04002876 goto fail;
Wenchao Xia129be002013-08-27 20:38:26 +08002877 }
2878 qdict_put(qdict, key, qstring_from_str(p));
2879 p += len;
2880 }
2881 break;
bellard9307c4c2004-04-04 12:57:25 +00002882 default:
2883 bad_type:
Bandan Dasae502122015-06-03 18:38:08 -04002884 monitor_printf(mon, "%s: unknown type '%c'\n", cmd->name, c);
bellard9307c4c2004-04-04 12:57:25 +00002885 goto fail;
2886 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002887 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002888 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00002889 }
2890 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00002891 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002892 p++;
2893 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00002894 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
Bandan Dasae502122015-06-03 18:38:08 -04002895 cmd->name);
bellard9307c4c2004-04-04 12:57:25 +00002896 goto fail;
2897 }
2898
Bandan Dasae502122015-06-03 18:38:08 -04002899 return qdict;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02002900
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002901fail:
Bandan Dasae502122015-06-03 18:38:08 -04002902 QDECREF(qdict);
Anthony Liguori7267c092011-08-20 22:09:37 -05002903 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002904 return NULL;
2905}
2906
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01002907static void handle_hmp_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002908{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002909 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05002910 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002911
Bandan Dasae502122015-06-03 18:38:08 -04002912 cmd = monitor_parse_command(mon, &cmdline, mon->cmd_table);
2913 if (!cmd) {
2914 return;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002915 }
2916
Bandan Dasae502122015-06-03 18:38:08 -04002917 qdict = monitor_parse_arguments(mon, &cmdline, cmd);
2918 if (!qdict) {
Bandan Dasdd41eea2015-06-03 18:38:09 -04002919 monitor_printf(mon, "Try \"help %s\" for more information\n",
2920 cmd->name);
Bandan Dasae502122015-06-03 18:38:08 -04002921 return;
2922 }
2923
2924 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002925 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00002926}
2927
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08002928static void cmd_completion(Monitor *mon, const char *name, const char *list)
bellard81d09122004-07-14 17:21:37 +00002929{
2930 const char *p, *pstart;
2931 char cmd[128];
2932 int len;
2933
2934 p = list;
2935 for(;;) {
2936 pstart = p;
2937 p = strchr(p, '|');
2938 if (!p)
2939 p = pstart + strlen(pstart);
2940 len = p - pstart;
2941 if (len > sizeof(cmd) - 2)
2942 len = sizeof(cmd) - 2;
2943 memcpy(cmd, pstart, len);
2944 cmd[len] = '\0';
2945 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08002946 readline_add_completion(mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00002947 }
2948 if (*p == '\0')
2949 break;
2950 p++;
2951 }
2952}
2953
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08002954static void file_completion(Monitor *mon, const char *input)
bellard81d09122004-07-14 17:21:37 +00002955{
2956 DIR *ffs;
2957 struct dirent *d;
2958 char path[1024];
2959 char file[1024], file_prefix[1024];
2960 int input_path_len;
2961 const char *p;
2962
ths5fafdf22007-09-16 21:08:06 +00002963 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00002964 if (!p) {
2965 input_path_len = 0;
2966 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00002967 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00002968 } else {
2969 input_path_len = p - input + 1;
2970 memcpy(path, input, input_path_len);
2971 if (input_path_len > sizeof(path) - 1)
2972 input_path_len = sizeof(path) - 1;
2973 path[input_path_len] = '\0';
2974 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2975 }
Bandan Das19f2db52015-06-03 18:38:07 -04002976
bellard81d09122004-07-14 17:21:37 +00002977 ffs = opendir(path);
2978 if (!ffs)
2979 return;
2980 for(;;) {
2981 struct stat sb;
2982 d = readdir(ffs);
2983 if (!d)
2984 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09002985
2986 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
2987 continue;
2988 }
2989
bellard81d09122004-07-14 17:21:37 +00002990 if (strstart(d->d_name, file_prefix, NULL)) {
2991 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00002992 if (input_path_len < sizeof(file))
2993 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2994 d->d_name);
bellard81d09122004-07-14 17:21:37 +00002995 /* stat the file to find out if it's a directory.
2996 * In that case add a slash to speed up typing long paths
2997 */
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01002998 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
blueswir1363a37d2008-08-21 17:58:08 +00002999 pstrcat(file, sizeof(file), "/");
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01003000 }
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003001 readline_add_completion(mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003002 }
3003 }
3004 closedir(ffs);
3005}
3006
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003007static const char *next_arg_type(const char *typestr)
3008{
3009 const char *p = strchr(typestr, ':');
3010 return (p != NULL ? ++p : typestr);
3011}
3012
Hani Benhabiles40d19392014-05-07 23:41:30 +01003013static void add_completion_option(ReadLineState *rs, const char *str,
3014 const char *option)
3015{
3016 if (!str || !option) {
3017 return;
3018 }
3019 if (!strncmp(option, str, strlen(str))) {
3020 readline_add_completion(rs, option);
3021 }
3022}
3023
Hani Benhabiles13e315d2014-05-07 23:41:29 +01003024void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
3025{
3026 size_t len;
3027 ChardevBackendInfoList *list, *start;
3028
3029 if (nb_args != 2) {
3030 return;
3031 }
3032 len = strlen(str);
3033 readline_set_completion_index(rs, len);
3034
3035 start = list = qmp_query_chardev_backends(NULL);
3036 while (list) {
3037 const char *chr_name = list->value->name;
3038
3039 if (!strncmp(chr_name, str, len)) {
3040 readline_add_completion(rs, chr_name);
3041 }
3042 list = list->next;
3043 }
3044 qapi_free_ChardevBackendInfoList(start);
3045}
3046
Hani Benhabilesb162b492014-05-07 23:41:31 +01003047void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
3048{
3049 size_t len;
3050 int i;
3051
3052 if (nb_args != 2) {
3053 return;
3054 }
3055 len = strlen(str);
3056 readline_set_completion_index(rs, len);
3057 for (i = 0; NetClientOptionsKind_lookup[i]; i++) {
3058 add_completion_option(rs, str, NetClientOptionsKind_lookup[i]);
3059 }
3060}
3061
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003062void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003063{
3064 GSList *list, *elt;
3065 size_t len;
3066
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003067 if (nb_args != 2) {
3068 return;
3069 }
3070
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003071 len = strlen(str);
3072 readline_set_completion_index(rs, len);
3073 list = elt = object_class_get_list(TYPE_DEVICE, false);
3074 while (elt) {
3075 const char *name;
3076 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
3077 TYPE_DEVICE);
3078 name = object_class_get_name(OBJECT_CLASS(dc));
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003079
3080 if (!dc->cannot_instantiate_with_device_add_yet
3081 && !strncmp(name, str, len)) {
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003082 readline_add_completion(rs, name);
3083 }
3084 elt = elt->next;
3085 }
3086 g_slist_free(list);
3087}
3088
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003089void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003090{
3091 GSList *list, *elt;
3092 size_t len;
3093
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003094 if (nb_args != 2) {
3095 return;
3096 }
3097
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003098 len = strlen(str);
3099 readline_set_completion_index(rs, len);
3100 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
3101 while (elt) {
3102 const char *name;
3103
3104 name = object_class_get_name(OBJECT_CLASS(elt->data));
3105 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
3106 readline_add_completion(rs, name);
3107 }
3108 elt = elt->next;
3109 }
3110 g_slist_free(list);
3111}
3112
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003113static void peripheral_device_del_completion(ReadLineState *rs,
3114 const char *str, size_t len)
3115{
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003116 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
3117 GSList *list, *item;
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003118
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003119 list = qdev_build_hotpluggable_device_list(peripheral);
3120 if (!list) {
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003121 return;
3122 }
3123
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003124 for (item = list; item; item = g_slist_next(item)) {
3125 DeviceState *dev = item->data;
3126
3127 if (dev->id && !strncmp(str, dev->id, len)) {
3128 readline_add_completion(rs, dev->id);
3129 }
3130 }
3131
3132 g_slist_free(list);
3133}
3134
Hani Benhabiles6297d9a2014-05-07 23:41:28 +01003135void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3136{
3137 size_t len;
3138 ChardevInfoList *list, *start;
3139
3140 if (nb_args != 2) {
3141 return;
3142 }
3143 len = strlen(str);
3144 readline_set_completion_index(rs, len);
3145
3146 start = list = qmp_query_chardev(NULL);
3147 while (list) {
3148 ChardevInfo *chr = list->value;
3149
3150 if (!strncmp(chr->label, str, len)) {
3151 readline_add_completion(rs, chr->label);
3152 }
3153 list = list->next;
3154 }
3155 qapi_free_ChardevInfoList(start);
3156}
3157
Hani Benhabiles8e597772014-05-27 23:39:30 +01003158static void ringbuf_completion(ReadLineState *rs, const char *str)
3159{
3160 size_t len;
3161 ChardevInfoList *list, *start;
3162
3163 len = strlen(str);
3164 readline_set_completion_index(rs, len);
3165
3166 start = list = qmp_query_chardev(NULL);
3167 while (list) {
3168 ChardevInfo *chr_info = list->value;
3169
3170 if (!strncmp(chr_info->label, str, len)) {
3171 CharDriverState *chr = qemu_chr_find(chr_info->label);
3172 if (chr && chr_is_ringbuf(chr)) {
3173 readline_add_completion(rs, chr_info->label);
3174 }
3175 }
3176 list = list->next;
3177 }
3178 qapi_free_ChardevInfoList(start);
3179}
3180
Hani Benhabiles8e597772014-05-27 23:39:30 +01003181void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
3182{
3183 if (nb_args != 2) {
3184 return;
3185 }
3186 ringbuf_completion(rs, str);
3187}
3188
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003189void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
3190{
3191 size_t len;
3192
3193 if (nb_args != 2) {
3194 return;
3195 }
3196
3197 len = strlen(str);
3198 readline_set_completion_index(rs, len);
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003199 peripheral_device_del_completion(rs, str, len);
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003200}
3201
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003202void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003203{
3204 ObjectPropertyInfoList *list, *start;
3205 size_t len;
3206
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003207 if (nb_args != 2) {
3208 return;
3209 }
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003210 len = strlen(str);
3211 readline_set_completion_index(rs, len);
3212
3213 start = list = qmp_qom_list("/objects", NULL);
3214 while (list) {
3215 ObjectPropertyInfo *info = list->value;
3216
3217 if (!strncmp(info->type, "child<", 5)
3218 && !strncmp(info->name, str, len)) {
3219 readline_add_completion(rs, info->name);
3220 }
3221 list = list->next;
3222 }
3223 qapi_free_ObjectPropertyInfoList(start);
3224}
3225
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003226void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
3227{
3228 int i;
3229 char *sep;
3230 size_t len;
3231
3232 if (nb_args != 2) {
3233 return;
3234 }
3235 sep = strrchr(str, '-');
3236 if (sep) {
3237 str = sep + 1;
3238 }
3239 len = strlen(str);
3240 readline_set_completion_index(rs, len);
Eric Blake7fb1cf12015-11-18 01:52:57 -07003241 for (i = 0; i < Q_KEY_CODE__MAX; i++) {
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003242 if (!strncmp(str, QKeyCode_lookup[i], len)) {
3243 readline_add_completion(rs, QKeyCode_lookup[i]);
3244 }
3245 }
3246}
3247
Hani Benhabiles40d19392014-05-07 23:41:30 +01003248void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
3249{
3250 size_t len;
3251
3252 len = strlen(str);
3253 readline_set_completion_index(rs, len);
3254 if (nb_args == 2) {
Jason Wangeaed4832015-04-23 14:21:38 +08003255 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles40d19392014-05-07 23:41:30 +01003256 int count, i;
3257 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08003258 NET_CLIENT_OPTIONS_KIND_NONE,
3259 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003260 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles40d19392014-05-07 23:41:30 +01003261 const char *name = ncs[i]->name;
3262 if (!strncmp(str, name, len)) {
3263 readline_add_completion(rs, name);
3264 }
3265 }
3266 } else if (nb_args == 3) {
3267 add_completion_option(rs, str, "on");
3268 add_completion_option(rs, str, "off");
3269 }
3270}
3271
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003272void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
3273{
3274 int len, count, i;
Jason Wangeaed4832015-04-23 14:21:38 +08003275 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003276
3277 if (nb_args != 2) {
3278 return;
3279 }
3280
3281 len = strlen(str);
3282 readline_set_completion_index(rs, len);
3283 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_OPTIONS_KIND_NIC,
Jason Wangeaed4832015-04-23 14:21:38 +08003284 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003285 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003286 QemuOpts *opts;
3287 const char *name = ncs[i]->name;
3288 if (strncmp(str, name, len)) {
3289 continue;
3290 }
3291 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
3292 if (opts) {
3293 readline_add_completion(rs, name);
3294 }
3295 }
3296}
3297
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003298void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
3299{
3300 size_t len;
3301
3302 len = strlen(str);
3303 readline_set_completion_index(rs, len);
3304 if (nb_args == 2) {
3305 TraceEventID id;
3306 for (id = 0; id < trace_event_count(); id++) {
3307 const char *event_name = trace_event_get_name(trace_event_id(id));
3308 if (!strncmp(str, event_name, len)) {
3309 readline_add_completion(rs, event_name);
3310 }
3311 }
3312 } else if (nb_args == 3) {
3313 add_completion_option(rs, str, "on");
3314 add_completion_option(rs, str, "off");
3315 }
3316}
3317
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003318void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
3319{
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003320 int i;
3321
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003322 if (nb_args != 2) {
3323 return;
3324 }
3325 readline_set_completion_index(rs, strlen(str));
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003326 for (i = 0; WatchdogExpirationAction_lookup[i]; i++) {
3327 add_completion_option(rs, str, WatchdogExpirationAction_lookup[i]);
3328 }
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003329}
3330
Hani Benhabilesc68a0402014-05-27 23:39:32 +01003331void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
3332 const char *str)
3333{
3334 size_t len;
3335
3336 len = strlen(str);
3337 readline_set_completion_index(rs, len);
3338 if (nb_args == 2) {
3339 int i;
Eric Blake7fb1cf12015-11-18 01:52:57 -07003340 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Hani Benhabilesc68a0402014-05-27 23:39:32 +01003341 const char *name = MigrationCapability_lookup[i];
3342 if (!strncmp(str, name, len)) {
3343 readline_add_completion(rs, name);
3344 }
3345 }
3346 } else if (nb_args == 3) {
3347 add_completion_option(rs, str, "on");
3348 add_completion_option(rs, str, "off");
3349 }
3350}
3351
Liang Li50e9a622015-03-23 16:32:29 +08003352void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
3353 const char *str)
3354{
3355 size_t len;
3356
3357 len = strlen(str);
3358 readline_set_completion_index(rs, len);
3359 if (nb_args == 2) {
3360 int i;
Eric Blake7fb1cf12015-11-18 01:52:57 -07003361 for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
Liang Li50e9a622015-03-23 16:32:29 +08003362 const char *name = MigrationParameter_lookup[i];
3363 if (!strncmp(str, name, len)) {
3364 readline_add_completion(rs, name);
3365 }
3366 }
3367 }
3368}
3369
Hani Benhabilese3bb5322014-05-27 23:39:34 +01003370void host_net_add_completion(ReadLineState *rs, int nb_args, const char *str)
3371{
3372 int i;
3373 size_t len;
3374 if (nb_args != 2) {
3375 return;
3376 }
3377 len = strlen(str);
3378 readline_set_completion_index(rs, len);
3379 for (i = 0; host_net_devices[i]; i++) {
3380 if (!strncmp(host_net_devices[i], str, len)) {
3381 readline_add_completion(rs, host_net_devices[i]);
3382 }
3383 }
3384}
3385
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003386void host_net_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3387{
Jason Wangeaed4832015-04-23 14:21:38 +08003388 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003389 int count, i, len;
3390
3391 len = strlen(str);
3392 readline_set_completion_index(rs, len);
3393 if (nb_args == 2) {
3394 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08003395 NET_CLIENT_OPTIONS_KIND_NONE,
3396 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003397 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003398 int id;
3399 char name[16];
3400
3401 if (net_hub_id_for_client(ncs[i], &id)) {
3402 continue;
3403 }
3404 snprintf(name, sizeof(name), "%d", id);
3405 if (!strncmp(str, name, len)) {
3406 readline_add_completion(rs, name);
3407 }
3408 }
3409 return;
3410 } else if (nb_args == 3) {
3411 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08003412 NET_CLIENT_OPTIONS_KIND_NIC,
3413 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003414 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Jason Wang2c4681f2015-02-02 15:06:38 +08003415 int id;
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003416 const char *name;
3417
Jason Wang2c4681f2015-02-02 15:06:38 +08003418 if (ncs[i]->info->type == NET_CLIENT_OPTIONS_KIND_HUBPORT ||
3419 net_hub_id_for_client(ncs[i], &id)) {
3420 continue;
3421 }
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003422 name = ncs[i]->name;
3423 if (!strncmp(str, name, len)) {
3424 readline_add_completion(rs, name);
3425 }
3426 }
3427 return;
3428 }
3429}
3430
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003431static void vm_completion(ReadLineState *rs, const char *str)
3432{
3433 size_t len;
Kevin Wolf7c8eece2016-03-22 18:58:50 +01003434 BlockDriverState *bs;
Kevin Wolf88be7b42016-05-20 18:49:07 +02003435 BdrvNextIterator it;
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003436
3437 len = strlen(str);
3438 readline_set_completion_index(rs, len);
Kevin Wolf7c8eece2016-03-22 18:58:50 +01003439
Kevin Wolf88be7b42016-05-20 18:49:07 +02003440 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003441 SnapshotInfoList *snapshots, *snapshot;
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003442 AioContext *ctx = bdrv_get_aio_context(bs);
3443 bool ok = false;
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003444
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003445 aio_context_acquire(ctx);
3446 if (bdrv_can_snapshot(bs)) {
3447 ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0;
3448 }
3449 aio_context_release(ctx);
3450 if (!ok) {
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003451 continue;
3452 }
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003453
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003454 snapshot = snapshots;
3455 while (snapshot) {
3456 char *completion = snapshot->value->name;
3457 if (!strncmp(str, completion, len)) {
3458 readline_add_completion(rs, completion);
3459 }
3460 completion = snapshot->value->id;
3461 if (!strncmp(str, completion, len)) {
3462 readline_add_completion(rs, completion);
3463 }
3464 snapshot = snapshot->next;
3465 }
3466 qapi_free_SnapshotInfoList(snapshots);
3467 }
3468
3469}
3470
3471void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
3472{
3473 if (nb_args == 2) {
3474 vm_completion(rs, str);
3475 }
3476}
3477
3478void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
3479{
3480 if (nb_args == 2) {
3481 vm_completion(rs, str);
3482 }
3483}
3484
Wenchao Xiac35b6402013-08-27 20:38:24 +08003485static void monitor_find_completion_by_table(Monitor *mon,
3486 const mon_cmd_t *cmd_table,
3487 char **args,
3488 int nb_args)
bellard81d09122004-07-14 17:21:37 +00003489{
3490 const char *cmdname;
Wenchao Xiac35b6402013-08-27 20:38:24 +08003491 int i;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003492 const char *ptype, *str, *name;
Anthony Liguoric227f092009-10-01 16:12:16 -05003493 const mon_cmd_t *cmd;
Max Reitzda27a002016-03-16 19:54:29 +01003494 BlockBackend *blk = NULL;
bellard81d09122004-07-14 17:21:37 +00003495
bellard81d09122004-07-14 17:21:37 +00003496 if (nb_args <= 1) {
3497 /* command completion */
3498 if (nb_args == 0)
3499 cmdname = "";
3500 else
3501 cmdname = args[0];
Wenchao Xiad2674b22013-08-27 20:38:16 +08003502 readline_set_completion_index(mon->rs, strlen(cmdname));
Wenchao Xiac35b6402013-08-27 20:38:24 +08003503 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003504 cmd_completion(mon, cmdname, cmd->name);
bellard81d09122004-07-14 17:21:37 +00003505 }
3506 } else {
3507 /* find the command */
Wenchao Xiac35b6402013-08-27 20:38:24 +08003508 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Jan Kiszka03a63482010-06-16 00:38:33 +02003509 if (compare_cmd(args[0], cmd->name)) {
3510 break;
3511 }
bellard81d09122004-07-14 17:21:37 +00003512 }
Jan Kiszka03a63482010-06-16 00:38:33 +02003513 if (!cmd->name) {
Wenchao Xiac35b6402013-08-27 20:38:24 +08003514 return;
Jan Kiszka03a63482010-06-16 00:38:33 +02003515 }
3516
Wenchao Xiad903a772013-08-27 20:38:25 +08003517 if (cmd->sub_table) {
3518 /* do the job again */
Stefan Weile7ae7712015-03-08 19:30:01 +01003519 monitor_find_completion_by_table(mon, cmd->sub_table,
3520 &args[1], nb_args - 1);
3521 return;
Wenchao Xiad903a772013-08-27 20:38:25 +08003522 }
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003523 if (cmd->command_completion) {
Stefan Weile7ae7712015-03-08 19:30:01 +01003524 cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
3525 return;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003526 }
Wenchao Xiad903a772013-08-27 20:38:25 +08003527
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003528 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003529 for(i = 0; i < nb_args - 2; i++) {
3530 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003531 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003532 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003533 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003534 }
3535 }
3536 str = args[nb_args - 1];
Kevin Wolf48fe86f2014-11-12 16:24:02 +01003537 while (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02003538 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00003539 }
bellard81d09122004-07-14 17:21:37 +00003540 switch(*ptype) {
3541 case 'F':
3542 /* file completion */
Wenchao Xiad2674b22013-08-27 20:38:16 +08003543 readline_set_completion_index(mon->rs, strlen(str));
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003544 file_completion(mon, str);
bellard81d09122004-07-14 17:21:37 +00003545 break;
3546 case 'B':
3547 /* block device name completion */
Wenchao Xia599a9262013-08-27 20:38:15 +08003548 readline_set_completion_index(mon->rs, strlen(str));
Max Reitzda27a002016-03-16 19:54:29 +01003549 while ((blk = blk_next(blk)) != NULL) {
3550 name = blk_name(blk);
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003551 if (str[0] == '\0' ||
3552 !strncmp(name, str, strlen(str))) {
3553 readline_add_completion(mon->rs, name);
3554 }
3555 }
bellard81d09122004-07-14 17:21:37 +00003556 break;
bellard7fe48482004-10-09 18:08:01 +00003557 case 's':
Wenchao Xia129be002013-08-27 20:38:26 +08003558 case 'S':
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003559 if (!strcmp(cmd->name, "help|?")) {
Wenchao Xia7ca0e062013-08-27 20:38:27 +08003560 monitor_find_completion_by_table(mon, cmd_table,
3561 &args[1], nb_args - 1);
bellard7fe48482004-10-09 18:08:01 +00003562 }
3563 break;
bellard81d09122004-07-14 17:21:37 +00003564 default:
3565 break;
3566 }
3567 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08003568}
3569
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003570static void monitor_find_completion(void *opaque,
Wenchao Xiac35b6402013-08-27 20:38:24 +08003571 const char *cmdline)
3572{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003573 Monitor *mon = opaque;
Wenchao Xiac35b6402013-08-27 20:38:24 +08003574 char *args[MAX_ARGS];
3575 int nb_args, len;
3576
3577 /* 1. parse the cmdline */
3578 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
3579 return;
3580 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08003581
3582 /* if the line ends with a space, it means we want to complete the
3583 next arg */
3584 len = strlen(cmdline);
3585 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
3586 if (nb_args >= MAX_ARGS) {
3587 goto cleanup;
3588 }
3589 args[nb_args++] = g_strdup("");
3590 }
3591
3592 /* 2. auto complete according to args */
3593 monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args);
Jan Kiszka03a63482010-06-16 00:38:33 +02003594
3595cleanup:
Wenchao Xiadcc70cd2013-08-27 20:38:22 +08003596 free_cmdline_args(args, nb_args);
bellard81d09122004-07-14 17:21:37 +00003597}
3598
aliguori731b0362009-03-05 23:01:42 +00003599static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003600{
aliguori731b0362009-03-05 23:01:42 +00003601 Monitor *mon = opaque;
3602
Jan Kiszkac62313b2009-12-04 14:05:29 +01003603 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003604}
3605
Markus Armbruster40861822015-05-29 10:27:16 +02003606static bool invalid_qmp_mode(const Monitor *mon, const mon_cmd_t *cmd,
3607 Error **errp)
Luiz Capitulino09069b12010-02-04 18:10:06 -02003608{
Markus Armbruster485febc2015-03-13 17:25:50 +01003609 bool is_cap = cmd->mhandler.cmd_new == qmp_capabilities;
Markus Armbrusterf994b252015-03-06 19:51:51 +01003610
3611 if (is_cap && mon->qmp.in_command_mode) {
Markus Armbruster40861822015-05-29 10:27:16 +02003612 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
3613 "Capabilities negotiation is already complete, command "
3614 "'%s' ignored", cmd->name);
Eric Blake2d5a8342015-04-15 09:19:23 -06003615 return true;
3616 }
Markus Armbrusterf994b252015-03-06 19:51:51 +01003617 if (!is_cap && !mon->qmp.in_command_mode) {
Markus Armbruster40861822015-05-29 10:27:16 +02003618 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
3619 "Expecting capabilities negotiation with "
3620 "'qmp_capabilities' before command '%s'", cmd->name);
Eric Blake2d5a8342015-04-15 09:19:23 -06003621 return true;
3622 }
3623 return false;
Luiz Capitulino09069b12010-02-04 18:10:06 -02003624}
3625
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003626/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03003627 * Argument validation rules:
3628 *
3629 * 1. The argument must exist in cmd_args qdict
3630 * 2. The argument type must be the expected one
3631 *
3632 * Special case: If the argument doesn't exist in cmd_args and
3633 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
3634 * checking is skipped for it.
3635 */
Markus Armbruster326283a2015-03-02 18:39:09 +01003636static void check_client_args_type(const QDict *client_args,
3637 const QDict *cmd_args, int flags,
3638 Error **errp)
Luiz Capitulino4af91932010-06-22 11:44:05 -03003639{
3640 const QDictEntry *ent;
3641
3642 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
3643 QObject *obj;
3644 QString *arg_type;
3645 const QObject *client_arg = qdict_entry_value(ent);
3646 const char *client_arg_name = qdict_entry_key(ent);
3647
3648 obj = qdict_get(cmd_args, client_arg_name);
3649 if (!obj) {
3650 if (flags & QMP_ACCEPT_UNKNOWNS) {
3651 /* handler accepts unknowns */
3652 continue;
3653 }
3654 /* client arg doesn't exist */
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003655 error_setg(errp, QERR_INVALID_PARAMETER, client_arg_name);
Markus Armbruster326283a2015-03-02 18:39:09 +01003656 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003657 }
3658
3659 arg_type = qobject_to_qstring(obj);
3660 assert(arg_type != NULL);
3661
3662 /* check if argument's type is correct */
3663 switch (qstring_get_str(arg_type)[0]) {
3664 case 'F':
3665 case 'B':
3666 case 's':
3667 if (qobject_type(client_arg) != QTYPE_QSTRING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003668 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3669 client_arg_name, "string");
Markus Armbruster326283a2015-03-02 18:39:09 +01003670 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003671 }
3672 break;
3673 case 'i':
3674 case 'l':
3675 case 'M':
Jes Sorensendbc0c672010-10-21 17:15:47 +02003676 case 'o':
Luiz Capitulino4af91932010-06-22 11:44:05 -03003677 if (qobject_type(client_arg) != QTYPE_QINT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003678 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3679 client_arg_name, "int");
Markus Armbruster326283a2015-03-02 18:39:09 +01003680 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003681 }
3682 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003683 case 'T':
3684 if (qobject_type(client_arg) != QTYPE_QINT &&
3685 qobject_type(client_arg) != QTYPE_QFLOAT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003686 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3687 client_arg_name, "number");
Markus Armbruster326283a2015-03-02 18:39:09 +01003688 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003689 }
3690 break;
3691 case 'b':
3692 case '-':
3693 if (qobject_type(client_arg) != QTYPE_QBOOL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003694 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3695 client_arg_name, "bool");
Markus Armbruster326283a2015-03-02 18:39:09 +01003696 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003697 }
3698 break;
3699 case 'O':
3700 assert(flags & QMP_ACCEPT_UNKNOWNS);
3701 break;
Paolo Bonzinib9f89782012-03-22 12:51:11 +01003702 case 'q':
3703 /* Any QObject can be passed. */
3704 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003705 case '/':
3706 case '.':
3707 /*
3708 * These types are not supported by QMP and thus are not
3709 * handled here. Fall through.
3710 */
3711 default:
3712 abort();
3713 }
3714 }
Luiz Capitulino4af91932010-06-22 11:44:05 -03003715}
3716
3717/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003718 * - Check if the client has passed all mandatory args
3719 * - Set special flags for argument validation
3720 */
Markus Armbruster326283a2015-03-02 18:39:09 +01003721static void check_mandatory_args(const QDict *cmd_args,
3722 const QDict *client_args, int *flags,
3723 Error **errp)
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003724{
3725 const QDictEntry *ent;
3726
3727 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
3728 const char *cmd_arg_name = qdict_entry_key(ent);
3729 QString *type = qobject_to_qstring(qdict_entry_value(ent));
3730 assert(type != NULL);
3731
3732 if (qstring_get_str(type)[0] == 'O') {
3733 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
3734 *flags |= QMP_ACCEPT_UNKNOWNS;
3735 } else if (qstring_get_str(type)[0] != '-' &&
3736 qstring_get_str(type)[1] != '?' &&
3737 !qdict_haskey(client_args, cmd_arg_name)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003738 error_setg(errp, QERR_MISSING_PARAMETER, cmd_arg_name);
Markus Armbruster326283a2015-03-02 18:39:09 +01003739 return;
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003740 }
3741 }
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003742}
3743
3744static QDict *qdict_from_args_type(const char *args_type)
3745{
3746 int i;
3747 QDict *qdict;
3748 QString *key, *type, *cur_qs;
3749
3750 assert(args_type != NULL);
3751
3752 qdict = qdict_new();
3753
3754 if (args_type == NULL || args_type[0] == '\0') {
3755 /* no args, empty qdict */
3756 goto out;
3757 }
3758
3759 key = qstring_new();
3760 type = qstring_new();
3761
3762 cur_qs = key;
3763
3764 for (i = 0;; i++) {
3765 switch (args_type[i]) {
3766 case ',':
3767 case '\0':
3768 qdict_put(qdict, qstring_get_str(key), type);
3769 QDECREF(key);
3770 if (args_type[i] == '\0') {
3771 goto out;
3772 }
3773 type = qstring_new(); /* qdict has ref */
3774 cur_qs = key = qstring_new();
3775 break;
3776 case ':':
3777 cur_qs = type;
3778 break;
3779 default:
3780 qstring_append_chr(cur_qs, args_type[i]);
3781 break;
3782 }
3783 }
3784
3785out:
3786 return qdict;
3787}
3788
3789/*
3790 * Client argument checking rules:
3791 *
3792 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03003793 * 2. Each argument provided by the client must be expected
3794 * 3. Each argument provided by the client must have the type expected
3795 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003796 */
Markus Armbruster326283a2015-03-02 18:39:09 +01003797static void qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args,
3798 Error **errp)
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003799{
Markus Armbruster326283a2015-03-02 18:39:09 +01003800 Error *err = NULL;
3801 int flags;
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003802 QDict *cmd_args;
3803
3804 cmd_args = qdict_from_args_type(cmd->args_type);
3805
3806 flags = 0;
Markus Armbruster326283a2015-03-02 18:39:09 +01003807 check_mandatory_args(cmd_args, client_args, &flags, &err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003808 if (err) {
3809 goto out;
3810 }
3811
Markus Armbruster326283a2015-03-02 18:39:09 +01003812 check_client_args_type(client_args, cmd_args, flags, &err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003813
3814out:
Markus Armbruster326283a2015-03-02 18:39:09 +01003815 error_propagate(errp, err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003816 QDECREF(cmd_args);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003817}
3818
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003819/*
3820 * Input object checking rules
3821 *
3822 * 1. Input object must be a dict
3823 * 2. The "execute" key must exist
3824 * 3. The "execute" key must be a string
3825 * 4. If the "arguments" key exists, it must be a dict
3826 * 5. If the "id" key exists, it can be anything (ie. json-value)
3827 * 6. Any argument not listed above is considered invalid
3828 */
Markus Armbrusterba0510a2015-03-02 18:41:43 +01003829static QDict *qmp_check_input_obj(QObject *input_obj, Error **errp)
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003830{
3831 const QDictEntry *ent;
3832 int has_exec_key = 0;
3833 QDict *input_dict;
3834
3835 if (qobject_type(input_obj) != QTYPE_QDICT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003836 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "object");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003837 return NULL;
3838 }
3839
3840 input_dict = qobject_to_qdict(input_obj);
3841
3842 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
3843 const char *arg_name = qdict_entry_key(ent);
3844 const QObject *arg_obj = qdict_entry_value(ent);
3845
3846 if (!strcmp(arg_name, "execute")) {
3847 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003848 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
3849 "execute", "string");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003850 return NULL;
3851 }
3852 has_exec_key = 1;
3853 } else if (!strcmp(arg_name, "arguments")) {
3854 if (qobject_type(arg_obj) != QTYPE_QDICT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003855 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
3856 "arguments", "object");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003857 return NULL;
3858 }
Markus Armbruster779cec42015-06-08 10:44:30 +02003859 } else if (!strcmp(arg_name, "id")) {
3860 /* Any string is acceptable as "id", so nothing to check */
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003861 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003862 error_setg(errp, QERR_QMP_EXTRA_MEMBER, arg_name);
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003863 return NULL;
3864 }
3865 }
3866
3867 if (!has_exec_key) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003868 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "execute");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003869 return NULL;
3870 }
3871
3872 return input_dict;
3873}
3874
Paolo Bonzini95385fe2015-11-25 22:23:31 +01003875static void handle_qmp_command(JSONMessageParser *parser, GQueue *tokens)
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003876{
Markus Armbruster326283a2015-03-02 18:39:09 +01003877 Error *local_err = NULL;
Markus Armbruster84add862015-03-05 16:45:15 +01003878 QObject *obj, *data;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003879 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003880 const mon_cmd_t *cmd;
Luiz Capitulino40e5a012011-10-21 16:15:31 -02003881 const char *cmd_name;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003882 Monitor *mon = cur_mon;
3883
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03003884 args = input = NULL;
Markus Armbruster84add862015-03-05 16:45:15 +01003885 data = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003886
3887 obj = json_parser_parse(tokens, NULL);
3888 if (!obj) {
3889 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003890 error_setg(&local_err, QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003891 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003892 }
3893
Markus Armbrusterba0510a2015-03-02 18:41:43 +01003894 input = qmp_check_input_obj(obj, &local_err);
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003895 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003896 qobject_decref(obj);
3897 goto err_out;
3898 }
3899
Markus Armbruster74358f22015-03-06 19:35:59 +01003900 mon->qmp.id = qdict_get(input, "id");
3901 qobject_incref(mon->qmp.id);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003902
Luiz Capitulino0bbab462010-05-31 17:32:50 -03003903 cmd_name = qdict_get_str(input, "execute");
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +01003904 trace_handle_qmp_command(mon, cmd_name);
Anthony Liguorie3193602011-09-02 12:34:47 -05003905 cmd = qmp_find_cmd(cmd_name);
Eric Blake2d5a8342015-04-15 09:19:23 -06003906 if (!cmd) {
Markus Armbruster710aec92015-03-06 11:28:00 +01003907 error_set(&local_err, ERROR_CLASS_COMMAND_NOT_FOUND,
3908 "The command %s has not been found", cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03003909 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003910 }
Markus Armbruster40861822015-05-29 10:27:16 +02003911 if (invalid_qmp_mode(mon, cmd, &local_err)) {
Eric Blake2d5a8342015-04-15 09:19:23 -06003912 goto err_out;
3913 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003914
3915 obj = qdict_get(input, "arguments");
3916 if (!obj) {
3917 args = qdict_new();
3918 } else {
3919 args = qobject_to_qdict(obj);
3920 QINCREF(args);
3921 }
3922
Markus Armbruster326283a2015-03-02 18:39:09 +01003923 qmp_check_client_args(cmd, args, &local_err);
3924 if (local_err) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003925 goto err_out;
3926 }
3927
Markus Armbruster485febc2015-03-13 17:25:50 +01003928 cmd->mhandler.cmd_new(args, &data, &local_err);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003929
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003930err_out:
Markus Armbruster710aec92015-03-06 11:28:00 +01003931 monitor_protocol_emitter(mon, data, local_err);
Markus Armbruster84add862015-03-05 16:45:15 +01003932 qobject_decref(data);
Markus Armbruster3a81a102015-10-29 12:15:09 +01003933 error_free(local_err);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03003934 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003935 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003936}
3937
Markus Armbrusterc83fe232015-03-06 19:20:51 +01003938static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003939{
3940 Monitor *old_mon = cur_mon;
3941
3942 cur_mon = opaque;
3943
Markus Armbruster74358f22015-03-06 19:35:59 +01003944 json_message_parser_feed(&cur_mon->qmp.parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003945
3946 cur_mon = old_mon;
3947}
3948
aliguori731b0362009-03-05 23:01:42 +00003949static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00003950{
aliguori731b0362009-03-05 23:01:42 +00003951 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00003952 int i;
aliguori376253e2009-03-05 23:01:23 +00003953
aliguori731b0362009-03-05 23:01:42 +00003954 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00003955
aliguoricde76ee2009-03-05 23:01:51 +00003956 if (cur_mon->rs) {
3957 for (i = 0; i < size; i++)
3958 readline_handle_byte(cur_mon->rs, buf[i]);
3959 } else {
3960 if (size == 0 || buf[size - 1] != 0)
3961 monitor_printf(cur_mon, "corrupted command\n");
3962 else
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003963 handle_hmp_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00003964 }
aliguori731b0362009-03-05 23:01:42 +00003965
3966 cur_mon = old_mon;
3967}
aliguorid8f44602008-10-06 13:52:44 +00003968
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003969static void monitor_command_cb(void *opaque, const char *cmdline,
3970 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00003971{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003972 Monitor *mon = opaque;
3973
aliguori731b0362009-03-05 23:01:42 +00003974 monitor_suspend(mon);
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003975 handle_hmp_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00003976 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00003977}
3978
aliguoricde76ee2009-03-05 23:01:51 +00003979int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003980{
aliguoricde76ee2009-03-05 23:01:51 +00003981 if (!mon->rs)
3982 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00003983 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00003984 return 0;
aliguorid8f44602008-10-06 13:52:44 +00003985}
3986
aliguori376253e2009-03-05 23:01:23 +00003987void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003988{
aliguoricde76ee2009-03-05 23:01:51 +00003989 if (!mon->rs)
3990 return;
aliguori731b0362009-03-05 23:01:42 +00003991 if (--mon->suspend_cnt == 0)
3992 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00003993}
3994
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003995static QObject *get_qmp_greeting(void)
3996{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03003997 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003998
Markus Armbruster7fad30f2015-09-16 13:06:19 +02003999 qmp_marshal_query_version(NULL, &ver, NULL);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004000 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
4001}
4002
Markus Armbrusterc83fe232015-03-06 19:20:51 +01004003static void monitor_qmp_event(void *opaque, int event)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004004{
Luiz Capitulino47116d12010-02-08 17:01:30 -02004005 QObject *data;
4006 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004007
Luiz Capitulino47116d12010-02-08 17:01:30 -02004008 switch (event) {
4009 case CHR_EVENT_OPENED:
Markus Armbrusterf994b252015-03-06 19:51:51 +01004010 mon->qmp.in_command_mode = false;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004011 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004012 monitor_json_emitter(mon, data);
4013 qobject_decref(data);
Corey Bryantefb87c12012-08-14 16:43:48 -04004014 mon_refcount++;
Luiz Capitulino47116d12010-02-08 17:01:30 -02004015 break;
4016 case CHR_EVENT_CLOSED:
Markus Armbruster74358f22015-03-06 19:35:59 +01004017 json_message_parser_destroy(&mon->qmp.parser);
4018 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Corey Bryantefb87c12012-08-14 16:43:48 -04004019 mon_refcount--;
4020 monitor_fdsets_cleanup();
Luiz Capitulino47116d12010-02-08 17:01:30 -02004021 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004022 }
4023}
4024
aliguori731b0362009-03-05 23:01:42 +00004025static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004026{
aliguori376253e2009-03-05 23:01:23 +00004027 Monitor *mon = opaque;
4028
aliguori2724b182009-03-05 23:01:47 +00004029 switch (event) {
4030 case CHR_EVENT_MUX_IN:
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02004031 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004032 mon->mux_out = 0;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02004033 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004034 if (mon->reset_seen) {
4035 readline_restart(mon->rs);
4036 monitor_resume(mon);
4037 monitor_flush(mon);
4038 } else {
4039 mon->suspend_cnt = 0;
4040 }
aliguori2724b182009-03-05 23:01:47 +00004041 break;
ths86e94de2007-01-05 22:01:59 +00004042
aliguori2724b182009-03-05 23:01:47 +00004043 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004044 if (mon->reset_seen) {
4045 if (mon->suspend_cnt == 0) {
4046 monitor_printf(mon, "\n");
4047 }
4048 monitor_flush(mon);
4049 monitor_suspend(mon);
4050 } else {
4051 mon->suspend_cnt++;
4052 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02004053 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004054 mon->mux_out = 1;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02004055 qemu_mutex_unlock(&mon->out_lock);
aliguori2724b182009-03-05 23:01:47 +00004056 break;
4057
Amit Shahb6b8df52009-10-07 18:31:16 +05304058 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004059 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4060 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004061 if (!mon->mux_out) {
Stratos Psomadakise5554e22014-09-15 15:34:57 +03004062 readline_restart(mon->rs);
aliguori2724b182009-03-05 23:01:47 +00004063 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004064 }
4065 mon->reset_seen = 1;
Corey Bryantefb87c12012-08-14 16:43:48 -04004066 mon_refcount++;
4067 break;
4068
4069 case CHR_EVENT_CLOSED:
4070 mon_refcount--;
4071 monitor_fdsets_cleanup();
aliguori2724b182009-03-05 23:01:47 +00004072 break;
4073 }
ths86e94de2007-01-05 22:01:59 +00004074}
4075
Wayne Xia816f8922011-10-12 11:32:41 +08004076static int
4077compare_mon_cmd(const void *a, const void *b)
4078{
4079 return strcmp(((const mon_cmd_t *)a)->name,
4080 ((const mon_cmd_t *)b)->name);
4081}
4082
4083static void sortcmdlist(void)
4084{
4085 int array_num;
4086 int elem_size = sizeof(mon_cmd_t);
4087
4088 array_num = sizeof(mon_cmds)/elem_size-1;
4089 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
4090
4091 array_num = sizeof(info_cmds)/elem_size-1;
4092 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
4093}
4094
aliguori76655d62009-03-06 20:27:37 +00004095
4096/*
4097 * Local variables:
4098 * c-indent-level: 4
4099 * c-basic-offset: 4
4100 * tab-width: 8
4101 * End:
4102 */
4103
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004104/* These functions just adapt the readline interface in a typesafe way. We
4105 * could cast function pointers but that discards compiler checks.
4106 */
Stefan Weild5d15072014-01-25 18:18:23 +01004107static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
4108 const char *fmt, ...)
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004109{
4110 va_list ap;
4111 va_start(ap, fmt);
4112 monitor_vprintf(opaque, fmt, ap);
4113 va_end(ap);
4114}
4115
4116static void monitor_readline_flush(void *opaque)
4117{
4118 monitor_flush(opaque);
4119}
4120
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004121static void __attribute__((constructor)) monitor_lock_init(void)
4122{
4123 qemu_mutex_init(&monitor_lock);
4124}
4125
aliguori731b0362009-03-05 23:01:42 +00004126void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004127{
aliguori731b0362009-03-05 23:01:42 +00004128 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004129 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004130
4131 if (is_first_init) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +02004132 monitor_qapi_event_init();
Wenchao Xiad0383172013-08-27 20:38:18 +08004133 sortcmdlist();
ths20d8a3e2007-02-18 17:04:49 +00004134 is_first_init = 0;
4135 }
aliguori87127162009-03-05 23:01:29 +00004136
Wenchao Xiab01fe892013-08-27 20:38:19 +08004137 mon = g_malloc(sizeof(*mon));
4138 monitor_data_init(mon);
ths20d8a3e2007-02-18 17:04:49 +00004139
aliguori87127162009-03-05 23:01:29 +00004140 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00004141 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004142 if (flags & MONITOR_USE_READLINE) {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004143 mon->rs = readline_init(monitor_readline_printf,
4144 monitor_readline_flush,
4145 mon,
4146 monitor_find_completion);
aliguoricde76ee2009-03-05 23:01:51 +00004147 monitor_read_command(mon, 0);
4148 }
aliguori87127162009-03-05 23:01:29 +00004149
Markus Armbruster9f3982f2015-03-06 19:56:38 +01004150 if (monitor_is_qmp(mon)) {
Markus Armbrusterc83fe232015-03-06 19:20:51 +01004151 qemu_chr_add_handlers(chr, monitor_can_read, monitor_qmp_read,
4152 monitor_qmp_event, mon);
Anthony Liguori15f31512011-08-15 11:17:35 -05004153 qemu_chr_fe_set_echo(chr, true);
Markus Armbruster74358f22015-03-06 19:35:59 +01004154 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004155 } else {
4156 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4157 monitor_event, mon);
4158 }
aliguori87127162009-03-05 23:01:29 +00004159
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004160 qemu_mutex_lock(&monitor_lock);
Blue Swirl72cf2d42009-09-12 07:36:22 +00004161 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004162 qemu_mutex_unlock(&monitor_lock);
bellard7e2515e2004-08-01 21:52:19 +00004163}
4164
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004165static void bdrv_password_cb(void *opaque, const char *password,
4166 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00004167{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004168 Monitor *mon = opaque;
4169 BlockDriverState *bs = readline_opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004170 int ret = 0;
Markus Armbruster4d2855a2015-01-29 10:37:00 +01004171 Error *local_err = NULL;
bellard7e2515e2004-08-01 21:52:19 +00004172
Markus Armbruster4d2855a2015-01-29 10:37:00 +01004173 bdrv_add_key(bs, password, &local_err);
4174 if (local_err) {
Markus Armbruster193227f2015-12-18 16:35:06 +01004175 error_report_err(local_err);
aliguoribb5fc202009-03-05 23:01:15 +00004176 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004177 }
aliguori731b0362009-03-05 23:01:42 +00004178 if (mon->password_completion_cb)
4179 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004180
aliguori731b0362009-03-05 23:01:42 +00004181 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004182}
aliguoric0f4ce72009-03-05 23:01:01 +00004183
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004184int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
Markus Armbruster097310b2014-10-07 13:59:15 +02004185 BlockCompletionFunc *completion_cb,
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004186 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004187{
aliguoricde76ee2009-03-05 23:01:51 +00004188 int err;
4189
aliguori376253e2009-03-05 23:01:23 +00004190 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4191 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00004192
aliguori731b0362009-03-05 23:01:42 +00004193 mon->password_completion_cb = completion_cb;
4194 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004195
aliguoricde76ee2009-03-05 23:01:51 +00004196 err = monitor_read_password(mon, bdrv_password_cb, bs);
4197
4198 if (err && completion_cb)
4199 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004200
4201 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00004202}
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004203
4204int monitor_read_block_device_key(Monitor *mon, const char *device,
Markus Armbruster097310b2014-10-07 13:59:15 +02004205 BlockCompletionFunc *completion_cb,
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004206 void *opaque)
4207{
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01004208 Error *err = NULL;
Fam Zheng55606252015-03-02 19:36:46 +08004209 BlockBackend *blk;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004210
Fam Zheng55606252015-03-02 19:36:46 +08004211 blk = blk_by_name(device);
4212 if (!blk) {
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004213 monitor_printf(mon, "Device not found %s\n", device);
4214 return -1;
4215 }
Max Reitz5433c242015-10-19 17:53:29 +02004216 if (!blk_bs(blk)) {
4217 monitor_printf(mon, "Device '%s' has no medium\n", device);
4218 return -1;
4219 }
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004220
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01004221 bdrv_add_key(blk_bs(blk), NULL, &err);
4222 if (err) {
4223 error_free(err);
4224 return monitor_read_bdrv_key_start(mon, blk_bs(blk), completion_cb, opaque);
4225 }
4226
4227 if (completion_cb) {
4228 completion_cb(opaque, 0);
4229 }
4230 return 0;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004231}
Paolo Bonzini4d454572012-11-26 16:03:42 +01004232
4233QemuOptsList qemu_mon_opts = {
4234 .name = "mon",
4235 .implied_opt_name = "chardev",
4236 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
4237 .desc = {
4238 {
4239 .name = "mode",
4240 .type = QEMU_OPT_STRING,
4241 },{
4242 .name = "chardev",
4243 .type = QEMU_OPT_STRING,
4244 },{
4245 .name = "default",
4246 .type = QEMU_OPT_BOOL,
4247 },{
4248 .name = "pretty",
4249 .type = QEMU_OPT_BOOL,
4250 },
4251 { /* end of list */ }
4252 },
4253};
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004254
4255#ifndef TARGET_I386
4256void qmp_rtc_reset_reinjection(Error **errp)
4257{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004258 error_setg(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection");
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004259}
4260#endif
Jason J. Herne7ee0c3e2015-06-26 14:03:16 -04004261
4262#ifndef TARGET_S390X
4263void qmp_dump_skeys(const char *filename, Error **errp)
4264{
4265 error_setg(errp, QERR_FEATURE_DISABLED, "dump-skeys");
4266}
4267#endif
Peter Xuae50a772016-03-30 17:27:24 +01004268
4269#ifndef TARGET_ARM
4270GICCapabilityList *qmp_query_gic_capabilities(Error **errp)
4271{
4272 error_setg(errp, QERR_FEATURE_DISABLED, "query-gic-capabilities");
4273 return NULL;
4274}
4275#endif