blob: 6f960f13a6059c39713df5ca0562cfc5b8bb0882 [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{
Markus Armbrusterdaa76aa2016-06-15 19:27:16 +02001114 Error *err = NULL;
1115
1116 qemu_set_log_filename(qdict_get_str(qdict, "filename"), &err);
1117 if (err) {
1118 error_report_err(err);
1119 }
pbrooke735b912007-06-30 13:53:24 +00001120}
1121
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001122static void hmp_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001123{
1124 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001125 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001126
bellard9307c4c2004-04-04 12:57:25 +00001127 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001128 mask = 0;
1129 } else {
Peter Maydell4fde1eb2013-02-11 16:41:22 +00001130 mask = qemu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001131 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001132 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001133 return;
1134 }
1135 }
Peter Maydell24537a02013-02-11 16:41:23 +00001136 qemu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00001137}
1138
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001139static void hmp_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001140{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001141 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001142 if (!option || !strcmp(option, "on")) {
1143 singlestep = 1;
1144 } else if (!strcmp(option, "off")) {
1145 singlestep = 0;
1146 } else {
1147 monitor_printf(mon, "unexpected option %s\n", option);
1148 }
1149}
1150
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001151static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001152{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001153 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001154 if (!device)
1155 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1156 if (gdbserver_start(device) < 0) {
1157 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1158 device);
1159 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001160 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001161 } else {
aliguori59030a82009-04-05 18:43:41 +00001162 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1163 device);
bellard8a7ddc32004-03-31 19:00:16 +00001164 }
1165}
1166
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001167static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001168{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001169 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001170 if (select_watchdog_action(action) == -1) {
1171 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1172 }
1173}
1174
aliguori376253e2009-03-05 23:01:23 +00001175static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001176{
aliguori376253e2009-03-05 23:01:23 +00001177 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001178 switch(c) {
1179 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001180 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001181 break;
1182 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001183 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001184 break;
1185 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001186 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001187 break;
1188 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001189 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001190 break;
1191 default:
1192 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001193 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001194 } else {
aliguori376253e2009-03-05 23:01:23 +00001195 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001196 }
1197 break;
1198 }
aliguori376253e2009-03-05 23:01:23 +00001199 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001200}
1201
aliguori376253e2009-03-05 23:01:23 +00001202static void memory_dump(Monitor *mon, int count, int format, int wsize,
Avi Kivitya8170e52012-10-23 12:30:10 +02001203 hwaddr addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001204{
Blue Swirl23842aa2010-01-12 20:27:43 +00001205 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001206 uint8_t buf[16];
1207 uint64_t v;
1208
1209 if (format == 'i') {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001210 int flags = 0;
bellard9307c4c2004-04-04 12:57:25 +00001211#ifdef TARGET_I386
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001212 CPUArchState *env = mon_get_cpu_env();
bellard4c27ba22004-04-25 18:05:08 +00001213 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001214 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001215 } else if (wsize == 4) {
1216 flags = 0;
1217 } else {
bellard6a15fd12006-04-12 21:07:07 +00001218 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001219 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001220 if (env) {
1221#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001222 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001223 (env->segs[R_CS].flags & DESC_L_MASK))
1224 flags = 2;
1225 else
1226#endif
1227 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1228 flags = 1;
1229 }
bellard4c27ba22004-04-25 18:05:08 +00001230 }
1231#endif
Tom Musta1c38f842014-04-09 14:53:24 -05001232#ifdef TARGET_PPC
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001233 CPUArchState *env = mon_get_cpu_env();
Tom Musta1c38f842014-04-09 14:53:24 -05001234 flags = msr_le << 16;
1235 flags |= env->bfd_mach;
1236#endif
Peter Crosthwaited49190c2015-05-24 14:20:41 -07001237 monitor_disas(mon, mon_get_cpu(), addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001238 return;
1239 }
1240
1241 len = wsize * count;
1242 if (wsize == 1)
1243 line_size = 8;
1244 else
1245 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001246 max_digits = 0;
1247
1248 switch(format) {
1249 case 'o':
1250 max_digits = (wsize * 8 + 2) / 3;
1251 break;
1252 default:
1253 case 'x':
1254 max_digits = (wsize * 8) / 4;
1255 break;
1256 case 'u':
1257 case 'd':
1258 max_digits = (wsize * 8 * 10 + 32) / 33;
1259 break;
1260 case 'c':
1261 wsize = 1;
1262 break;
1263 }
1264
1265 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001266 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001267 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001268 else
aliguori376253e2009-03-05 23:01:23 +00001269 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001270 l = len;
1271 if (l > line_size)
1272 l = line_size;
1273 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001274 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001275 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001276 if (cpu_memory_rw_debug(mon_get_cpu(), addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001277 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001278 break;
1279 }
bellard9307c4c2004-04-04 12:57:25 +00001280 }
ths5fafdf22007-09-16 21:08:06 +00001281 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001282 while (i < l) {
1283 switch(wsize) {
1284 default:
1285 case 1:
Peter Maydell24e60302015-01-20 15:19:32 +00001286 v = ldub_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001287 break;
1288 case 2:
Peter Maydell24e60302015-01-20 15:19:32 +00001289 v = lduw_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001290 break;
1291 case 4:
Peter Maydell24e60302015-01-20 15:19:32 +00001292 v = (uint32_t)ldl_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001293 break;
1294 case 8:
Peter Maydell24e60302015-01-20 15:19:32 +00001295 v = ldq_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001296 break;
1297 }
aliguori376253e2009-03-05 23:01:23 +00001298 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001299 switch(format) {
1300 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001301 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001302 break;
1303 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001304 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001305 break;
1306 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001307 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001308 break;
1309 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001310 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001311 break;
1312 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001313 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001314 break;
1315 }
1316 i += wsize;
1317 }
aliguori376253e2009-03-05 23:01:23 +00001318 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001319 addr += l;
1320 len -= l;
1321 }
1322}
1323
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001324static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001325{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001326 int count = qdict_get_int(qdict, "count");
1327 int format = qdict_get_int(qdict, "format");
1328 int size = qdict_get_int(qdict, "size");
1329 target_long addr = qdict_get_int(qdict, "addr");
1330
aliguori376253e2009-03-05 23:01:23 +00001331 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001332}
1333
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001334static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001335{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001336 int count = qdict_get_int(qdict, "count");
1337 int format = qdict_get_int(qdict, "format");
1338 int size = qdict_get_int(qdict, "size");
Avi Kivitya8170e52012-10-23 12:30:10 +02001339 hwaddr addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001340
aliguori376253e2009-03-05 23:01:23 +00001341 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001342}
1343
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001344static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001345{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001346 int format = qdict_get_int(qdict, "format");
Avi Kivitya8170e52012-10-23 12:30:10 +02001347 hwaddr val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001348
bellard9307c4c2004-04-04 12:57:25 +00001349 switch(format) {
1350 case 'o':
Avi Kivitya8170e52012-10-23 12:30:10 +02001351 monitor_printf(mon, "%#" HWADDR_PRIo, val);
bellard9307c4c2004-04-04 12:57:25 +00001352 break;
1353 case 'x':
Avi Kivitya8170e52012-10-23 12:30:10 +02001354 monitor_printf(mon, "%#" HWADDR_PRIx, val);
bellard9307c4c2004-04-04 12:57:25 +00001355 break;
1356 case 'u':
Avi Kivitya8170e52012-10-23 12:30:10 +02001357 monitor_printf(mon, "%" HWADDR_PRIu, val);
bellard9307c4c2004-04-04 12:57:25 +00001358 break;
1359 default:
1360 case 'd':
Avi Kivitya8170e52012-10-23 12:30:10 +02001361 monitor_printf(mon, "%" HWADDR_PRId, val);
bellard9307c4c2004-04-04 12:57:25 +00001362 break;
1363 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001364 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001365 break;
1366 }
aliguori376253e2009-03-05 23:01:23 +00001367 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001368}
1369
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001370static void hmp_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001371{
1372 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001373 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001374 uint32_t start = qdict_get_int(qdict, "start");
1375 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001376
1377 sum = 0;
1378 for(addr = start; addr < (start + size); addr++) {
Peter Maydell42874d32015-04-26 16:49:24 +01001379 uint8_t val = address_space_ldub(&address_space_memory, addr,
1380 MEMTXATTRS_UNSPECIFIED, NULL);
bellarde4cf1ad2005-06-04 20:15:57 +00001381 /* BSD sum algorithm ('sum' Unix command) */
1382 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001383 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001384 }
aliguori376253e2009-03-05 23:01:23 +00001385 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001386}
1387
bellard13224a82006-07-14 22:03:35 +00001388static int mouse_button_state;
1389
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001390static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001391{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001392 int dx, dy, dz, button;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001393 const char *dx_str = qdict_get_str(qdict, "dx_str");
1394 const char *dy_str = qdict_get_str(qdict, "dy_str");
1395 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001396
bellard13224a82006-07-14 22:03:35 +00001397 dx = strtol(dx_str, NULL, 0);
1398 dy = strtol(dy_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001399 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
1400 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
1401
1402 if (dz_str) {
bellard13224a82006-07-14 22:03:35 +00001403 dz = strtol(dz_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001404 if (dz != 0) {
Gerd Hoffmannf22d0af2016-01-12 12:14:12 +01001405 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001406 qemu_input_queue_btn(NULL, button, true);
1407 qemu_input_event_sync();
1408 qemu_input_queue_btn(NULL, button, false);
1409 }
1410 }
1411 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001412}
1413
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001414static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001415{
Eric Blake7fb1cf12015-11-18 01:52:57 -07001416 static uint32_t bmap[INPUT_BUTTON__MAX] = {
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001417 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1418 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
1419 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1420 };
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001421 int button_state = qdict_get_int(qdict, "button_state");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001422
1423 if (mouse_button_state == button_state) {
1424 return;
1425 }
1426 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1427 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001428 mouse_button_state = button_state;
bellard13224a82006-07-14 22:03:35 +00001429}
1430
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001431static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001432{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001433 int size = qdict_get_int(qdict, "size");
1434 int addr = qdict_get_int(qdict, "addr");
1435 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001436 uint32_t val;
1437 int suffix;
1438
1439 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001440 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001441 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001442 addr++;
1443 }
1444 addr &= 0xffff;
1445
1446 switch(size) {
1447 default:
1448 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001449 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001450 suffix = 'b';
1451 break;
1452 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001453 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001454 suffix = 'w';
1455 break;
1456 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001457 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001458 suffix = 'l';
1459 break;
1460 }
aliguori376253e2009-03-05 23:01:23 +00001461 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1462 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001463}
bellarda3a91a32004-06-04 11:06:21 +00001464
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001465static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001466{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001467 int size = qdict_get_int(qdict, "size");
1468 int addr = qdict_get_int(qdict, "addr");
1469 int val = qdict_get_int(qdict, "val");
1470
Jan Kiszkaf1147842009-07-14 10:20:11 +02001471 addr &= IOPORTS_MASK;
1472
1473 switch (size) {
1474 default:
1475 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001476 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001477 break;
1478 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001479 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001480 break;
1481 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001482 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001483 break;
1484 }
1485}
1486
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001487static void hmp_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001488{
Gongleif1839932014-12-03 18:20:58 +00001489 Error *local_err = NULL;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001490 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001491
Gongleif1839932014-12-03 18:20:58 +00001492 qemu_boot_set(bootdevice, &local_err);
1493 if (local_err) {
Markus Armbruster193227f2015-12-18 16:35:06 +01001494 error_report_err(local_err);
aurel320ecdffb2008-05-04 20:11:34 +00001495 } else {
Gongleif1839932014-12-03 18:20:58 +00001496 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001497 }
1498}
1499
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001500static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
Blue Swirl314e2982011-09-11 20:22:05 +00001501{
1502 mtree_info((fprintf_function)monitor_printf, mon);
1503}
1504
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001505static void hmp_info_numa(Monitor *mon, const QDict *qdict)
aliguori030ea372009-04-21 22:30:47 +00001506{
aliguorib28b6232009-04-22 20:20:29 +00001507 int i;
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001508 CPUState *cpu;
zhanghailiang5b009e42014-11-04 19:49:30 +08001509 uint64_t *node_mem;
aliguori030ea372009-04-21 22:30:47 +00001510
zhanghailiang5b009e42014-11-04 19:49:30 +08001511 node_mem = g_new0(uint64_t, nb_numa_nodes);
1512 query_numa_node_mem(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001513 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1514 for (i = 0; i < nb_numa_nodes; i++) {
1515 monitor_printf(mon, "node %d cpus:", i);
Andreas Färberbdc44642013-06-24 23:50:24 +02001516 CPU_FOREACH(cpu) {
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001517 if (cpu->numa_node == i) {
Andreas Färber55e5c282012-12-17 06:18:02 +01001518 monitor_printf(mon, " %d", cpu->cpu_index);
aliguori030ea372009-04-21 22:30:47 +00001519 }
1520 }
1521 monitor_printf(mon, "\n");
1522 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
zhanghailiang5b009e42014-11-04 19:49:30 +08001523 node_mem[i] >> 20);
aliguori030ea372009-04-21 22:30:47 +00001524 }
zhanghailiang5b009e42014-11-04 19:49:30 +08001525 g_free(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001526}
1527
bellard5f1ce942006-02-08 22:40:15 +00001528#ifdef CONFIG_PROFILER
1529
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001530int64_t tcg_time;
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001531int64_t dev_time;
1532
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001533static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001534{
aliguori376253e2009-03-05 23:01:23 +00001535 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Rutuja Shah73bcb242016-03-21 21:32:30 +05301536 dev_time, dev_time / (double)NANOSECONDS_PER_SECOND);
aliguori376253e2009-03-05 23:01:23 +00001537 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Rutuja Shah73bcb242016-03-21 21:32:30 +05301538 tcg_time, tcg_time / (double)NANOSECONDS_PER_SECOND);
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001539 tcg_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001540 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001541}
1542#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001543static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001544{
aliguori376253e2009-03-05 23:01:23 +00001545 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001546}
1547#endif
1548
bellardec36b692006-07-16 18:57:03 +00001549/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001550static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001551
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001552static void hmp_info_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001553{
1554 int i;
1555 CaptureState *s;
1556
1557 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001558 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001559 s->ops.info (s->opaque);
1560 }
1561}
1562
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001563static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001564{
1565 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001566 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001567 CaptureState *s;
1568
1569 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1570 if (i == n) {
1571 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001572 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001573 g_free (s);
bellardec36b692006-07-16 18:57:03 +00001574 return;
1575 }
1576 }
1577}
1578
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001579static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001580{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001581 const char *path = qdict_get_str(qdict, "path");
1582 int has_freq = qdict_haskey(qdict, "freq");
1583 int freq = qdict_get_try_int(qdict, "freq", -1);
1584 int has_bits = qdict_haskey(qdict, "bits");
1585 int bits = qdict_get_try_int(qdict, "bits", -1);
1586 int has_channels = qdict_haskey(qdict, "nchannels");
1587 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001588 CaptureState *s;
1589
Anthony Liguori7267c092011-08-20 22:09:37 -05001590 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001591
1592 freq = has_freq ? freq : 44100;
1593 bits = has_bits ? bits : 16;
1594 nchannels = has_channels ? nchannels : 2;
1595
1596 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001597 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05001598 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001599 return;
bellardec36b692006-07-16 18:57:03 +00001600 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001601 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001602}
bellardec36b692006-07-16 18:57:03 +00001603
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001604static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001605{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001606 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001607
aliguori76655d62009-03-06 20:27:37 +00001608 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001609 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001610 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001611 return acl;
1612}
aliguori76655d62009-03-06 20:27:37 +00001613
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001614static void hmp_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001615{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001616 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001617 qemu_acl *acl = find_acl(mon, aclname);
1618 qemu_acl_entry *entry;
1619 int i = 0;
1620
1621 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001622 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001623 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00001624 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00001625 i++;
1626 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001627 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001628 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001629 }
1630}
1631
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001632static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001633{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001634 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001635 qemu_acl *acl = find_acl(mon, aclname);
1636
1637 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001638 qemu_acl_reset(acl);
1639 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001640 }
1641}
aliguori76655d62009-03-06 20:27:37 +00001642
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001643static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001644{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001645 const char *aclname = qdict_get_str(qdict, "aclname");
1646 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001647 qemu_acl *acl = find_acl(mon, aclname);
1648
1649 if (acl) {
1650 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001651 acl->defaultDeny = 0;
1652 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001653 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001654 acl->defaultDeny = 1;
1655 monitor_printf(mon, "acl: policy set to 'deny'\n");
1656 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001657 monitor_printf(mon, "acl: unknown policy '%s', "
1658 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001659 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001660 }
1661}
aliguori76655d62009-03-06 20:27:37 +00001662
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001663static void hmp_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001664{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001665 const char *aclname = qdict_get_str(qdict, "aclname");
1666 const char *match = qdict_get_str(qdict, "match");
1667 const char *policy = qdict_get_str(qdict, "policy");
1668 int has_index = qdict_haskey(qdict, "index");
1669 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001670 qemu_acl *acl = find_acl(mon, aclname);
1671 int deny, ret;
1672
1673 if (acl) {
1674 if (strcmp(policy, "allow") == 0) {
1675 deny = 0;
1676 } else if (strcmp(policy, "deny") == 0) {
1677 deny = 1;
1678 } else {
1679 monitor_printf(mon, "acl: unknown policy '%s', "
1680 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001681 return;
1682 }
aliguori28a76be2009-03-06 20:27:40 +00001683 if (has_index)
1684 ret = qemu_acl_insert(acl, deny, match, index);
1685 else
1686 ret = qemu_acl_append(acl, deny, match);
1687 if (ret < 0)
1688 monitor_printf(mon, "acl: unable to add acl entry\n");
1689 else
1690 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001691 }
1692}
aliguori76655d62009-03-06 20:27:37 +00001693
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001694static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001695{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001696 const char *aclname = qdict_get_str(qdict, "aclname");
1697 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001698 qemu_acl *acl = find_acl(mon, aclname);
1699 int ret;
aliguori76655d62009-03-06 20:27:37 +00001700
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001701 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001702 ret = qemu_acl_remove(acl, match);
1703 if (ret < 0)
1704 monitor_printf(mon, "acl: no matching acl entry\n");
1705 else
1706 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001707 }
1708}
1709
Corey Bryant208c9d12012-06-22 14:36:09 -04001710void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001711{
Anthony Liguoric227f092009-10-01 16:12:16 -05001712 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001713 int fd;
1714
Corey Bryant208c9d12012-06-22 14:36:09 -04001715 fd = qemu_chr_fe_get_msgfd(cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001716 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001717 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryant208c9d12012-06-22 14:36:09 -04001718 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001719 }
1720
1721 if (qemu_isdigit(fdname[0])) {
Stefan Hajnoczi0b9f0e22014-04-24 13:58:18 +02001722 close(fd);
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001723 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
1724 "a name not starting with a digit");
Corey Bryant208c9d12012-06-22 14:36:09 -04001725 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001726 }
1727
Corey Bryant208c9d12012-06-22 14:36:09 -04001728 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001729 if (strcmp(monfd->name, fdname) != 0) {
1730 continue;
1731 }
1732
1733 close(monfd->fd);
1734 monfd->fd = fd;
Corey Bryant208c9d12012-06-22 14:36:09 -04001735 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001736 }
1737
Anthony Liguori7267c092011-08-20 22:09:37 -05001738 monfd = g_malloc0(sizeof(mon_fd_t));
1739 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001740 monfd->fd = fd;
1741
Corey Bryant208c9d12012-06-22 14:36:09 -04001742 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001743}
1744
Corey Bryant208c9d12012-06-22 14:36:09 -04001745void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001746{
Anthony Liguoric227f092009-10-01 16:12:16 -05001747 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001748
Corey Bryant208c9d12012-06-22 14:36:09 -04001749 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001750 if (strcmp(monfd->name, fdname) != 0) {
1751 continue;
1752 }
1753
Blue Swirl72cf2d42009-09-12 07:36:22 +00001754 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001755 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001756 g_free(monfd->name);
1757 g_free(monfd);
Corey Bryant208c9d12012-06-22 14:36:09 -04001758 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001759 }
1760
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001761 error_setg(errp, QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001762}
1763
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001764static void hmp_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001765{
Luiz Capitulino13548692011-07-29 15:36:43 -03001766 int saved_vm_running = runstate_is_running();
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001767 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02001768
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001769 vm_stop(RUN_STATE_RESTORE_VM);
Juan Quintelac8d41b22009-08-20 19:42:21 +02001770
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03001771 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02001772 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03001773 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02001774}
1775
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001776int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
Mark McLoughlin7768e042009-07-22 09:11:41 +01001777{
Anthony Liguoric227f092009-10-01 16:12:16 -05001778 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01001779
Blue Swirl72cf2d42009-09-12 07:36:22 +00001780 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01001781 int fd;
1782
1783 if (strcmp(monfd->name, fdname) != 0) {
1784 continue;
1785 }
1786
1787 fd = monfd->fd;
1788
1789 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001790 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05001791 g_free(monfd->name);
1792 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001793
1794 return fd;
1795 }
1796
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001797 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001798 return -1;
1799}
1800
Corey Bryantba1c0482012-08-14 16:43:43 -04001801static void monitor_fdset_cleanup(MonFdset *mon_fdset)
1802{
1803 MonFdsetFd *mon_fdset_fd;
1804 MonFdsetFd *mon_fdset_fd_next;
1805
1806 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantebe52b52012-10-18 15:19:33 -04001807 if ((mon_fdset_fd->removed ||
1808 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
1809 runstate_is_running()) {
Corey Bryantba1c0482012-08-14 16:43:43 -04001810 close(mon_fdset_fd->fd);
1811 g_free(mon_fdset_fd->opaque);
1812 QLIST_REMOVE(mon_fdset_fd, next);
1813 g_free(mon_fdset_fd);
1814 }
1815 }
1816
Corey Bryantadb696f2012-08-14 16:43:47 -04001817 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04001818 QLIST_REMOVE(mon_fdset, next);
1819 g_free(mon_fdset);
1820 }
1821}
1822
Corey Bryantefb87c12012-08-14 16:43:48 -04001823static void monitor_fdsets_cleanup(void)
1824{
1825 MonFdset *mon_fdset;
1826 MonFdset *mon_fdset_next;
1827
1828 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
1829 monitor_fdset_cleanup(mon_fdset);
1830 }
1831}
1832
Corey Bryantba1c0482012-08-14 16:43:43 -04001833AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
1834 const char *opaque, Error **errp)
1835{
1836 int fd;
1837 Monitor *mon = cur_mon;
Corey Bryantba1c0482012-08-14 16:43:43 -04001838 AddfdInfo *fdinfo;
1839
1840 fd = qemu_chr_fe_get_msgfd(mon->chr);
1841 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001842 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryantba1c0482012-08-14 16:43:43 -04001843 goto error;
1844 }
1845
Corey Bryante446f702012-10-18 15:19:32 -04001846 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
1847 has_opaque, opaque, errp);
1848 if (fdinfo) {
1849 return fdinfo;
Corey Bryant9ac54af2012-10-18 15:19:31 -04001850 }
1851
Corey Bryantba1c0482012-08-14 16:43:43 -04001852error:
1853 if (fd != -1) {
1854 close(fd);
1855 }
1856 return NULL;
1857}
1858
1859void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
1860{
1861 MonFdset *mon_fdset;
1862 MonFdsetFd *mon_fdset_fd;
1863 char fd_str[60];
1864
1865 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1866 if (mon_fdset->id != fdset_id) {
1867 continue;
1868 }
1869 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1870 if (has_fd) {
1871 if (mon_fdset_fd->fd != fd) {
1872 continue;
1873 }
1874 mon_fdset_fd->removed = true;
1875 break;
1876 } else {
1877 mon_fdset_fd->removed = true;
1878 }
1879 }
1880 if (has_fd && !mon_fdset_fd) {
1881 goto error;
1882 }
1883 monitor_fdset_cleanup(mon_fdset);
1884 return;
1885 }
1886
1887error:
1888 if (has_fd) {
1889 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
1890 fdset_id, fd);
1891 } else {
1892 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
1893 }
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001894 error_setg(errp, QERR_FD_NOT_FOUND, fd_str);
Corey Bryantba1c0482012-08-14 16:43:43 -04001895}
1896
1897FdsetInfoList *qmp_query_fdsets(Error **errp)
1898{
1899 MonFdset *mon_fdset;
1900 MonFdsetFd *mon_fdset_fd;
1901 FdsetInfoList *fdset_list = NULL;
1902
1903 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1904 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
1905 FdsetFdInfoList *fdsetfd_list = NULL;
1906
1907 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
1908 fdset_info->value->fdset_id = mon_fdset->id;
1909
1910 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1911 FdsetFdInfoList *fdsetfd_info;
1912
1913 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
1914 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
1915 fdsetfd_info->value->fd = mon_fdset_fd->fd;
1916 if (mon_fdset_fd->opaque) {
1917 fdsetfd_info->value->has_opaque = true;
1918 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
1919 } else {
1920 fdsetfd_info->value->has_opaque = false;
1921 }
1922
1923 fdsetfd_info->next = fdsetfd_list;
1924 fdsetfd_list = fdsetfd_info;
1925 }
1926
1927 fdset_info->value->fds = fdsetfd_list;
1928
1929 fdset_info->next = fdset_list;
1930 fdset_list = fdset_info;
1931 }
1932
1933 return fdset_list;
1934}
1935
Corey Bryante446f702012-10-18 15:19:32 -04001936AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
1937 bool has_opaque, const char *opaque,
1938 Error **errp)
1939{
1940 MonFdset *mon_fdset = NULL;
1941 MonFdsetFd *mon_fdset_fd;
1942 AddfdInfo *fdinfo;
1943
1944 if (has_fdset_id) {
1945 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1946 /* Break if match found or match impossible due to ordering by ID */
1947 if (fdset_id <= mon_fdset->id) {
1948 if (fdset_id < mon_fdset->id) {
1949 mon_fdset = NULL;
1950 }
1951 break;
1952 }
1953 }
1954 }
1955
1956 if (mon_fdset == NULL) {
1957 int64_t fdset_id_prev = -1;
1958 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
1959
1960 if (has_fdset_id) {
1961 if (fdset_id < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001962 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
1963 "a non-negative value");
Corey Bryante446f702012-10-18 15:19:32 -04001964 return NULL;
1965 }
1966 /* Use specified fdset ID */
1967 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1968 mon_fdset_cur = mon_fdset;
1969 if (fdset_id < mon_fdset_cur->id) {
1970 break;
1971 }
1972 }
1973 } else {
1974 /* Use first available fdset ID */
1975 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1976 mon_fdset_cur = mon_fdset;
1977 if (fdset_id_prev == mon_fdset_cur->id - 1) {
1978 fdset_id_prev = mon_fdset_cur->id;
1979 continue;
1980 }
1981 break;
1982 }
1983 }
1984
1985 mon_fdset = g_malloc0(sizeof(*mon_fdset));
1986 if (has_fdset_id) {
1987 mon_fdset->id = fdset_id;
1988 } else {
1989 mon_fdset->id = fdset_id_prev + 1;
1990 }
1991
1992 /* The fdset list is ordered by fdset ID */
1993 if (!mon_fdset_cur) {
1994 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
1995 } else if (mon_fdset->id < mon_fdset_cur->id) {
1996 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
1997 } else {
1998 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
1999 }
2000 }
2001
2002 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
2003 mon_fdset_fd->fd = fd;
2004 mon_fdset_fd->removed = false;
2005 if (has_opaque) {
2006 mon_fdset_fd->opaque = g_strdup(opaque);
2007 }
2008 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
2009
2010 fdinfo = g_malloc0(sizeof(*fdinfo));
2011 fdinfo->fdset_id = mon_fdset->id;
2012 fdinfo->fd = mon_fdset_fd->fd;
2013
2014 return fdinfo;
2015}
2016
Corey Bryantadb696f2012-08-14 16:43:47 -04002017int monitor_fdset_get_fd(int64_t fdset_id, int flags)
2018{
Blue Swirlb2dc64c2012-08-18 20:14:54 +00002019#ifndef _WIN32
Corey Bryantadb696f2012-08-14 16:43:47 -04002020 MonFdset *mon_fdset;
2021 MonFdsetFd *mon_fdset_fd;
2022 int mon_fd_flags;
2023
Corey Bryantadb696f2012-08-14 16:43:47 -04002024 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2025 if (mon_fdset->id != fdset_id) {
2026 continue;
2027 }
2028 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2029 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
2030 if (mon_fd_flags == -1) {
2031 return -1;
2032 }
2033
2034 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
2035 return mon_fdset_fd->fd;
2036 }
2037 }
2038 errno = EACCES;
2039 return -1;
2040 }
2041#endif
2042
2043 errno = ENOENT;
2044 return -1;
2045}
2046
2047int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
2048{
2049 MonFdset *mon_fdset;
2050 MonFdsetFd *mon_fdset_fd_dup;
2051
2052 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2053 if (mon_fdset->id != fdset_id) {
2054 continue;
2055 }
2056 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2057 if (mon_fdset_fd_dup->fd == dup_fd) {
2058 return -1;
2059 }
2060 }
2061 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2062 mon_fdset_fd_dup->fd = dup_fd;
2063 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
2064 return 0;
2065 }
2066 return -1;
2067}
2068
2069static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2070{
2071 MonFdset *mon_fdset;
2072 MonFdsetFd *mon_fdset_fd_dup;
2073
2074 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2075 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2076 if (mon_fdset_fd_dup->fd == dup_fd) {
2077 if (remove) {
2078 QLIST_REMOVE(mon_fdset_fd_dup, next);
2079 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2080 monitor_fdset_cleanup(mon_fdset);
2081 }
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002082 return -1;
2083 } else {
2084 return mon_fdset->id;
Corey Bryantadb696f2012-08-14 16:43:47 -04002085 }
Corey Bryantadb696f2012-08-14 16:43:47 -04002086 }
2087 }
2088 }
2089 return -1;
2090}
2091
2092int monitor_fdset_dup_fd_find(int dup_fd)
2093{
2094 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2095}
2096
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002097void monitor_fdset_dup_fd_remove(int dup_fd)
Corey Bryantadb696f2012-08-14 16:43:47 -04002098{
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002099 monitor_fdset_dup_fd_find_remove(dup_fd, true);
Corey Bryantadb696f2012-08-14 16:43:47 -04002100}
2101
Markus Armbruster1677f4c2015-02-09 14:03:19 +01002102int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
Laszlo Ersek59063662014-04-10 10:24:31 +02002103{
2104 int fd;
2105 Error *local_err = NULL;
2106
2107 if (!qemu_isdigit(fdname[0]) && mon) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002108 fd = monitor_get_fd(mon, fdname, &local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002109 } else {
2110 fd = qemu_parse_fd(fdname);
Laszlo Ersek59063662014-04-10 10:24:31 +02002111 if (fd == -1) {
2112 error_setg(&local_err, "Invalid file descriptor number '%s'",
2113 fdname);
2114 }
2115 }
2116 if (local_err) {
2117 error_propagate(errp, local_err);
2118 assert(fd == -1);
2119 } else {
2120 assert(fd != -1);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002121 }
2122
2123 return fd;
2124}
2125
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002126/* Please update hmp-commands.hx when adding or changing commands */
Wayne Xia816f8922011-10-12 11:32:41 +08002127static mon_cmd_t info_cmds[] = {
Pavel Butsykinda76ee72015-09-10 18:38:58 +03002128#include "hmp-commands-info.h"
2129 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002130};
2131
Wenchao Xiaa13ced52013-01-14 14:06:28 +08002132/* mon_cmds and info_cmds would be sorted at runtime */
2133static mon_cmd_t mon_cmds[] = {
2134#include "hmp-commands.h"
2135 { NULL, NULL, },
2136};
2137
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002138static const mon_cmd_t qmp_cmds[] = {
Anthony Liguorie3193602011-09-02 12:34:47 -05002139#include "qmp-commands-old.h"
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002140 { /* NULL */ },
2141};
2142
bellard9307c4c2004-04-04 12:57:25 +00002143/*******************************************************************/
2144
2145static const char *pch;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002146static sigjmp_buf expr_env;
bellard9307c4c2004-04-04 12:57:25 +00002147
bellard9307c4c2004-04-04 12:57:25 +00002148
Stefan Weil9c3175c2013-08-22 21:30:09 +02002149static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
2150expr_error(Monitor *mon, const char *fmt, ...)
bellard9dc39cb2004-03-14 21:38:27 +00002151{
Fam Zheng277acfe2013-08-20 10:58:21 +08002152 va_list ap;
2153 va_start(ap, fmt);
2154 monitor_vprintf(mon, fmt, ap);
2155 monitor_printf(mon, "\n");
2156 va_end(ap);
Peter Maydell6ab7e542013-02-20 15:21:09 +00002157 siglongjmp(expr_env, 1);
bellard9307c4c2004-04-04 12:57:25 +00002158}
2159
Markus Armbruster09b94182010-01-20 13:07:30 +01002160/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00002161static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002162{
Pavel Butsykinbf957282015-09-10 18:38:59 +03002163 const MonitorDef *md = target_monitor_defs();
bellard92a31b12005-02-10 22:00:52 +00002164 void *ptr;
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002165 uint64_t tmp = 0;
2166 int ret;
bellard92a31b12005-02-10 22:00:52 +00002167
Pavel Butsykinbf957282015-09-10 18:38:59 +03002168 if (md == NULL) {
2169 return -1;
2170 }
2171
2172 for(; md->name != NULL; md++) {
bellard9307c4c2004-04-04 12:57:25 +00002173 if (compare_cmd(name, md->name)) {
2174 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002175 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002176 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002177 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00002178 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002179 switch(md->type) {
2180 case MD_I32:
2181 *pval = *(int32_t *)ptr;
2182 break;
2183 case MD_TLONG:
2184 *pval = *(target_long *)ptr;
2185 break;
2186 default:
2187 *pval = 0;
2188 break;
2189 }
bellard9307c4c2004-04-04 12:57:25 +00002190 }
2191 return 0;
2192 }
2193 }
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002194
2195 ret = target_get_monitor_def(mon_get_cpu(), name, &tmp);
2196 if (!ret) {
2197 *pval = (target_long) tmp;
2198 }
2199
2200 return ret;
bellard9307c4c2004-04-04 12:57:25 +00002201}
2202
2203static void next(void)
2204{
Blue Swirl660f11b2009-07-31 21:16:51 +00002205 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002206 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002207 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002208 pch++;
2209 }
2210}
2211
aliguori376253e2009-03-05 23:01:23 +00002212static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002213
aliguori376253e2009-03-05 23:01:23 +00002214static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002215{
blueswir1c2efc952007-09-25 17:28:42 +00002216 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002217 char *p;
bellard6a00d602005-11-21 23:25:50 +00002218 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002219
2220 switch(*pch) {
2221 case '+':
2222 next();
aliguori376253e2009-03-05 23:01:23 +00002223 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002224 break;
2225 case '-':
2226 next();
aliguori376253e2009-03-05 23:01:23 +00002227 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002228 break;
2229 case '~':
2230 next();
aliguori376253e2009-03-05 23:01:23 +00002231 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002232 break;
2233 case '(':
2234 next();
aliguori376253e2009-03-05 23:01:23 +00002235 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002236 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002237 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002238 }
2239 next();
2240 break;
bellard81d09122004-07-14 17:21:37 +00002241 case '\'':
2242 pch++;
2243 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002244 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002245 n = *pch;
2246 pch++;
2247 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002248 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002249 next();
2250 break;
bellard9307c4c2004-04-04 12:57:25 +00002251 case '$':
2252 {
2253 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002254 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002255
bellard9307c4c2004-04-04 12:57:25 +00002256 pch++;
2257 q = buf;
2258 while ((*pch >= 'a' && *pch <= 'z') ||
2259 (*pch >= 'A' && *pch <= 'Z') ||
2260 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002261 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002262 if ((q - buf) < sizeof(buf) - 1)
2263 *q++ = *pch;
2264 pch++;
2265 }
blueswir1cd390082008-11-16 13:53:32 +00002266 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002267 pch++;
2268 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002269 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01002270 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00002271 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00002272 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002273 }
2274 break;
2275 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002276 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002277 n = 0;
2278 break;
2279 default:
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002280 errno = 0;
bellard4f4fbf72006-06-25 18:28:12 +00002281 n = strtoull(pch, &p, 0);
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002282 if (errno == ERANGE) {
2283 expr_error(mon, "number too large");
2284 }
bellard9307c4c2004-04-04 12:57:25 +00002285 if (pch == p) {
Fam Zheng277acfe2013-08-20 10:58:21 +08002286 expr_error(mon, "invalid char '%c' in expression", *p);
bellard9307c4c2004-04-04 12:57:25 +00002287 }
2288 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002289 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002290 pch++;
2291 break;
2292 }
2293 return n;
2294}
2295
2296
aliguori376253e2009-03-05 23:01:23 +00002297static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002298{
blueswir1c2efc952007-09-25 17:28:42 +00002299 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002300 int op;
ths3b46e622007-09-17 08:09:54 +00002301
aliguori376253e2009-03-05 23:01:23 +00002302 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002303 for(;;) {
2304 op = *pch;
2305 if (op != '*' && op != '/' && op != '%')
2306 break;
2307 next();
aliguori376253e2009-03-05 23:01:23 +00002308 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002309 switch(op) {
2310 default:
2311 case '*':
2312 val *= val2;
2313 break;
2314 case '/':
2315 case '%':
ths5fafdf22007-09-16 21:08:06 +00002316 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002317 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002318 if (op == '/')
2319 val /= val2;
2320 else
2321 val %= val2;
2322 break;
2323 }
2324 }
2325 return val;
2326}
2327
aliguori376253e2009-03-05 23:01:23 +00002328static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002329{
blueswir1c2efc952007-09-25 17:28:42 +00002330 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002331 int op;
bellard9307c4c2004-04-04 12:57:25 +00002332
aliguori376253e2009-03-05 23:01:23 +00002333 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002334 for(;;) {
2335 op = *pch;
2336 if (op != '&' && op != '|' && op != '^')
2337 break;
2338 next();
aliguori376253e2009-03-05 23:01:23 +00002339 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002340 switch(op) {
2341 default:
2342 case '&':
2343 val &= val2;
2344 break;
2345 case '|':
2346 val |= val2;
2347 break;
2348 case '^':
2349 val ^= val2;
2350 break;
2351 }
2352 }
2353 return val;
2354}
2355
aliguori376253e2009-03-05 23:01:23 +00002356static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002357{
blueswir1c2efc952007-09-25 17:28:42 +00002358 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002359 int op;
bellard9307c4c2004-04-04 12:57:25 +00002360
aliguori376253e2009-03-05 23:01:23 +00002361 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002362 for(;;) {
2363 op = *pch;
2364 if (op != '+' && op != '-')
2365 break;
2366 next();
aliguori376253e2009-03-05 23:01:23 +00002367 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002368 if (op == '+')
2369 val += val2;
2370 else
2371 val -= val2;
2372 }
2373 return val;
2374}
2375
aliguori376253e2009-03-05 23:01:23 +00002376static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002377{
2378 pch = *pp;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002379 if (sigsetjmp(expr_env, 0)) {
bellard9307c4c2004-04-04 12:57:25 +00002380 *pp = pch;
2381 return -1;
2382 }
blueswir1cd390082008-11-16 13:53:32 +00002383 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002384 pch++;
aliguori376253e2009-03-05 23:01:23 +00002385 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002386 *pp = pch;
2387 return 0;
2388}
2389
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002390static int get_double(Monitor *mon, double *pval, const char **pp)
2391{
2392 const char *p = *pp;
2393 char *tailp;
2394 double d;
2395
2396 d = strtod(p, &tailp);
2397 if (tailp == p) {
2398 monitor_printf(mon, "Number expected\n");
2399 return -1;
2400 }
2401 if (d != d || d - d != 0) {
2402 /* NaN or infinity */
2403 monitor_printf(mon, "Bad number\n");
2404 return -1;
2405 }
2406 *pval = d;
2407 *pp = tailp;
2408 return 0;
2409}
2410
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002411/*
2412 * Store the command-name in cmdname, and return a pointer to
2413 * the remaining of the command string.
2414 */
2415static const char *get_command_name(const char *cmdline,
2416 char *cmdname, size_t nlen)
2417{
2418 size_t len;
2419 const char *p, *pstart;
2420
2421 p = cmdline;
2422 while (qemu_isspace(*p))
2423 p++;
2424 if (*p == '\0')
2425 return NULL;
2426 pstart = p;
2427 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2428 p++;
2429 len = p - pstart;
2430 if (len > nlen - 1)
2431 len = nlen - 1;
2432 memcpy(cmdname, pstart, len);
2433 cmdname[len] = '\0';
2434 return p;
2435}
2436
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002437/**
2438 * Read key of 'type' into 'key' and return the current
2439 * 'type' pointer.
2440 */
2441static char *key_get_info(const char *type, char **key)
2442{
2443 size_t len;
2444 char *p, *str;
2445
2446 if (*type == ',')
2447 type++;
2448
2449 p = strchr(type, ':');
2450 if (!p) {
2451 *key = NULL;
2452 return NULL;
2453 }
2454 len = p - type;
2455
Anthony Liguori7267c092011-08-20 22:09:37 -05002456 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002457 memcpy(str, type, len);
2458 str[len] = '\0';
2459
2460 *key = str;
2461 return ++p;
2462}
2463
bellard9307c4c2004-04-04 12:57:25 +00002464static int default_fmt_format = 'x';
2465static int default_fmt_size = 4;
2466
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002467static int is_valid_option(const char *c, const char *typestr)
2468{
2469 char option[3];
2470
2471 option[0] = '-';
2472 option[1] = *c;
2473 option[2] = '\0';
2474
2475 typestr = strstr(typestr, option);
2476 return (typestr != NULL);
2477}
2478
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03002479static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
2480 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002481{
2482 const mon_cmd_t *cmd;
2483
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03002484 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002485 if (compare_cmd(cmdname, cmd->name)) {
2486 return cmd;
2487 }
2488 }
2489
2490 return NULL;
2491}
2492
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03002493static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
2494{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002495 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03002496}
2497
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002498/*
Bandan Dasae502122015-06-03 18:38:08 -04002499 * Parse command name from @cmdp according to command table @table.
2500 * If blank, return NULL.
2501 * Else, if no valid command can be found, report to @mon, and return
2502 * NULL.
2503 * Else, change @cmdp to point right behind the name, and return its
2504 * command table entry.
2505 * Do not assume the return value points into @table! It doesn't when
2506 * the command is found in a sub-command table.
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002507 */
Anthony Liguoric227f092009-10-01 16:12:16 -05002508static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Bandan Dasae502122015-06-03 18:38:08 -04002509 const char **cmdp,
2510 mon_cmd_t *table)
bellard9307c4c2004-04-04 12:57:25 +00002511{
Bandan Dasae502122015-06-03 18:38:08 -04002512 const char *p;
Anthony Liguoric227f092009-10-01 16:12:16 -05002513 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002514 char cmdname[256];
bellard9dc39cb2004-03-14 21:38:27 +00002515
bellard9307c4c2004-04-04 12:57:25 +00002516 /* extract the command name */
Bandan Dasae502122015-06-03 18:38:08 -04002517 p = get_command_name(*cmdp, cmdname, sizeof(cmdname));
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002518 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002519 return NULL;
ths3b46e622007-09-17 08:09:54 +00002520
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002521 cmd = search_dispatch_table(table, cmdname);
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002522 if (!cmd) {
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002523 monitor_printf(mon, "unknown command: '%.*s'\n",
Bandan Dasae502122015-06-03 18:38:08 -04002524 (int)(p - *cmdp), *cmdp);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002525 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002526 }
bellard9307c4c2004-04-04 12:57:25 +00002527
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002528 /* filter out following useless space */
2529 while (qemu_isspace(*p)) {
2530 p++;
2531 }
Bandan Dasae502122015-06-03 18:38:08 -04002532
2533 *cmdp = p;
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002534 /* search sub command */
Bandan Dasae502122015-06-03 18:38:08 -04002535 if (cmd->sub_table != NULL && *p != '\0') {
2536 return monitor_parse_command(mon, cmdp, cmd->sub_table);
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002537 }
2538
Bandan Dasae502122015-06-03 18:38:08 -04002539 return cmd;
2540}
2541
2542/*
2543 * Parse arguments for @cmd.
2544 * If it can't be parsed, report to @mon, and return NULL.
2545 * Else, insert command arguments into a QDict, and return it.
2546 * Note: On success, caller has to free the QDict structure.
2547 */
2548
2549static QDict *monitor_parse_arguments(Monitor *mon,
2550 const char **endp,
2551 const mon_cmd_t *cmd)
2552{
2553 const char *typestr;
2554 char *key;
2555 int c;
2556 const char *p = *endp;
2557 char buf[1024];
2558 QDict *qdict = qdict_new();
2559
bellard9307c4c2004-04-04 12:57:25 +00002560 /* parse the parameters */
2561 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00002562 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002563 typestr = key_get_info(typestr, &key);
2564 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00002565 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002566 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00002567 typestr++;
2568 switch(c) {
2569 case 'F':
bellard81d09122004-07-14 17:21:37 +00002570 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002571 case 's':
2572 {
2573 int ret;
ths3b46e622007-09-17 08:09:54 +00002574
blueswir1cd390082008-11-16 13:53:32 +00002575 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002576 p++;
2577 if (*typestr == '?') {
2578 typestr++;
2579 if (*p == '\0') {
2580 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03002581 break;
bellard9307c4c2004-04-04 12:57:25 +00002582 }
2583 }
2584 ret = get_str(buf, sizeof(buf), &p);
2585 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002586 switch(c) {
2587 case 'F':
aliguori376253e2009-03-05 23:01:23 +00002588 monitor_printf(mon, "%s: filename expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002589 cmd->name);
bellard81d09122004-07-14 17:21:37 +00002590 break;
2591 case 'B':
aliguori376253e2009-03-05 23:01:23 +00002592 monitor_printf(mon, "%s: block device name expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002593 cmd->name);
bellard81d09122004-07-14 17:21:37 +00002594 break;
2595 default:
Bandan Dasae502122015-06-03 18:38:08 -04002596 monitor_printf(mon, "%s: string expected\n", cmd->name);
bellard81d09122004-07-14 17:21:37 +00002597 break;
2598 }
bellard9307c4c2004-04-04 12:57:25 +00002599 goto fail;
2600 }
Luiz Capitulino53773582009-08-28 15:27:25 -03002601 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00002602 }
2603 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01002604 case 'O':
2605 {
2606 QemuOptsList *opts_list;
2607 QemuOpts *opts;
2608
2609 opts_list = qemu_find_opts(key);
2610 if (!opts_list || opts_list->desc->name) {
2611 goto bad_type;
2612 }
2613 while (qemu_isspace(*p)) {
2614 p++;
2615 }
2616 if (!*p)
2617 break;
2618 if (get_str(buf, sizeof(buf), &p) < 0) {
2619 goto fail;
2620 }
Markus Armbruster70b94332015-02-13 12:50:26 +01002621 opts = qemu_opts_parse_noisily(opts_list, buf, true);
Markus Armbruster361127d2010-02-10 20:24:35 +01002622 if (!opts) {
2623 goto fail;
2624 }
2625 qemu_opts_to_qdict(opts, qdict);
2626 qemu_opts_del(opts);
2627 }
2628 break;
bellard9307c4c2004-04-04 12:57:25 +00002629 case '/':
2630 {
2631 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002632
blueswir1cd390082008-11-16 13:53:32 +00002633 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002634 p++;
2635 if (*p == '/') {
2636 /* format found */
2637 p++;
2638 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002639 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002640 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002641 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002642 count = count * 10 + (*p - '0');
2643 p++;
2644 }
2645 }
2646 size = -1;
2647 format = -1;
2648 for(;;) {
2649 switch(*p) {
2650 case 'o':
2651 case 'd':
2652 case 'u':
2653 case 'x':
2654 case 'i':
2655 case 'c':
2656 format = *p++;
2657 break;
2658 case 'b':
2659 size = 1;
2660 p++;
2661 break;
2662 case 'h':
2663 size = 2;
2664 p++;
2665 break;
2666 case 'w':
2667 size = 4;
2668 p++;
2669 break;
2670 case 'g':
2671 case 'L':
2672 size = 8;
2673 p++;
2674 break;
2675 default:
2676 goto next;
2677 }
2678 }
2679 next:
blueswir1cd390082008-11-16 13:53:32 +00002680 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00002681 monitor_printf(mon, "invalid char in format: '%c'\n",
2682 *p);
bellard9307c4c2004-04-04 12:57:25 +00002683 goto fail;
2684 }
bellard9307c4c2004-04-04 12:57:25 +00002685 if (format < 0)
2686 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002687 if (format != 'i') {
2688 /* for 'i', not specifying a size gives -1 as size */
2689 if (size < 0)
2690 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002691 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002692 }
bellard9307c4c2004-04-04 12:57:25 +00002693 default_fmt_format = format;
2694 } else {
2695 count = 1;
2696 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002697 if (format != 'i') {
2698 size = default_fmt_size;
2699 } else {
2700 size = -1;
2701 }
bellard9307c4c2004-04-04 12:57:25 +00002702 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002703 qdict_put(qdict, "count", qint_from_int(count));
2704 qdict_put(qdict, "format", qint_from_int(format));
2705 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00002706 }
2707 break;
2708 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002709 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002710 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00002711 {
blueswir1c2efc952007-09-25 17:28:42 +00002712 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00002713
blueswir1cd390082008-11-16 13:53:32 +00002714 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002715 p++;
bellard34405572004-06-08 00:55:58 +00002716 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002717 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03002718 if (*p == '\0') {
2719 typestr++;
2720 break;
2721 }
bellard34405572004-06-08 00:55:58 +00002722 } else {
2723 if (*p == '.') {
2724 p++;
blueswir1cd390082008-11-16 13:53:32 +00002725 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00002726 p++;
bellard34405572004-06-08 00:55:58 +00002727 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03002728 typestr++;
2729 break;
bellard34405572004-06-08 00:55:58 +00002730 }
2731 }
bellard13224a82006-07-14 22:03:35 +00002732 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002733 }
aliguori376253e2009-03-05 23:01:23 +00002734 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00002735 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002736 /* Check if 'i' is greater than 32-bit */
2737 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
Bandan Dasae502122015-06-03 18:38:08 -04002738 monitor_printf(mon, "\'%s\' has failed: ", cmd->name);
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002739 monitor_printf(mon, "integer is for 32-bit values\n");
2740 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002741 } else if (c == 'M') {
Luiz Capitulino91162842012-04-26 17:34:30 -03002742 if (val < 0) {
2743 monitor_printf(mon, "enter a positive value\n");
2744 goto fail;
2745 }
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002746 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002747 }
Luiz Capitulino53773582009-08-28 15:27:25 -03002748 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00002749 }
2750 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02002751 case 'o':
2752 {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01002753 int64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02002754 char *end;
2755
2756 while (qemu_isspace(*p)) {
2757 p++;
2758 }
2759 if (*typestr == '?') {
2760 typestr++;
2761 if (*p == '\0') {
2762 break;
2763 }
2764 }
Marc-André Lureau4677bb42015-09-16 18:02:56 +02002765 val = qemu_strtosz(p, &end);
Jes Sorensendbc0c672010-10-21 17:15:47 +02002766 if (val < 0) {
2767 monitor_printf(mon, "invalid size\n");
2768 goto fail;
2769 }
2770 qdict_put(qdict, key, qint_from_int(val));
2771 p = end;
2772 }
2773 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01002774 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002775 {
2776 double val;
2777
2778 while (qemu_isspace(*p))
2779 p++;
2780 if (*typestr == '?') {
2781 typestr++;
2782 if (*p == '\0') {
2783 break;
2784 }
2785 }
2786 if (get_double(mon, &val, &p) < 0) {
2787 goto fail;
2788 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02002789 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01002790 switch (*p) {
2791 case 'm':
2792 val /= 1e3; p += 2; break;
2793 case 'u':
2794 val /= 1e6; p += 2; break;
2795 case 'n':
2796 val /= 1e9; p += 2; break;
2797 }
2798 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002799 if (*p && !qemu_isspace(*p)) {
2800 monitor_printf(mon, "Unknown unit suffix\n");
2801 goto fail;
2802 }
2803 qdict_put(qdict, key, qfloat_from_double(val));
2804 }
2805 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002806 case 'b':
2807 {
2808 const char *beg;
Eric Blakefc48ffc2015-05-15 16:24:59 -06002809 bool val;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002810
2811 while (qemu_isspace(*p)) {
2812 p++;
2813 }
2814 beg = p;
2815 while (qemu_isgraph(*p)) {
2816 p++;
2817 }
2818 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06002819 val = true;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002820 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06002821 val = false;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002822 } else {
2823 monitor_printf(mon, "Expected 'on' or 'off'\n");
2824 goto fail;
2825 }
Eric Blakefc48ffc2015-05-15 16:24:59 -06002826 qdict_put(qdict, key, qbool_from_bool(val));
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002827 }
2828 break;
bellard9307c4c2004-04-04 12:57:25 +00002829 case '-':
2830 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002831 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03002832 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00002833 /* option */
ths3b46e622007-09-17 08:09:54 +00002834
bellard9307c4c2004-04-04 12:57:25 +00002835 c = *typestr++;
2836 if (c == '\0')
2837 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00002838 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002839 p++;
bellard9307c4c2004-04-04 12:57:25 +00002840 if (*p == '-') {
2841 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002842 if(c != *p) {
2843 if(!is_valid_option(p, typestr)) {
2844
2845 monitor_printf(mon, "%s: unsupported option -%c\n",
Bandan Dasae502122015-06-03 18:38:08 -04002846 cmd->name, *p);
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002847 goto fail;
2848 } else {
2849 skip_key = 1;
2850 }
bellard9307c4c2004-04-04 12:57:25 +00002851 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002852 if(skip_key) {
2853 p = tmp;
2854 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03002855 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002856 p++;
Eric Blakefc48ffc2015-05-15 16:24:59 -06002857 qdict_put(qdict, key, qbool_from_bool(true));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002858 }
bellard9307c4c2004-04-04 12:57:25 +00002859 }
bellard9307c4c2004-04-04 12:57:25 +00002860 }
2861 break;
Wenchao Xia129be002013-08-27 20:38:26 +08002862 case 'S':
2863 {
2864 /* package all remaining string */
2865 int len;
2866
2867 while (qemu_isspace(*p)) {
2868 p++;
2869 }
2870 if (*typestr == '?') {
2871 typestr++;
2872 if (*p == '\0') {
2873 /* no remaining string: NULL argument */
2874 break;
2875 }
2876 }
2877 len = strlen(p);
2878 if (len <= 0) {
2879 monitor_printf(mon, "%s: string expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002880 cmd->name);
Bandan Dase549d2a2015-06-03 18:38:10 -04002881 goto fail;
Wenchao Xia129be002013-08-27 20:38:26 +08002882 }
2883 qdict_put(qdict, key, qstring_from_str(p));
2884 p += len;
2885 }
2886 break;
bellard9307c4c2004-04-04 12:57:25 +00002887 default:
2888 bad_type:
Bandan Dasae502122015-06-03 18:38:08 -04002889 monitor_printf(mon, "%s: unknown type '%c'\n", cmd->name, c);
bellard9307c4c2004-04-04 12:57:25 +00002890 goto fail;
2891 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002892 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002893 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00002894 }
2895 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00002896 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002897 p++;
2898 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00002899 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
Bandan Dasae502122015-06-03 18:38:08 -04002900 cmd->name);
bellard9307c4c2004-04-04 12:57:25 +00002901 goto fail;
2902 }
2903
Bandan Dasae502122015-06-03 18:38:08 -04002904 return qdict;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02002905
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002906fail:
Bandan Dasae502122015-06-03 18:38:08 -04002907 QDECREF(qdict);
Anthony Liguori7267c092011-08-20 22:09:37 -05002908 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002909 return NULL;
2910}
2911
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01002912static void handle_hmp_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002913{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002914 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05002915 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002916
Bandan Dasae502122015-06-03 18:38:08 -04002917 cmd = monitor_parse_command(mon, &cmdline, mon->cmd_table);
2918 if (!cmd) {
2919 return;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002920 }
2921
Bandan Dasae502122015-06-03 18:38:08 -04002922 qdict = monitor_parse_arguments(mon, &cmdline, cmd);
2923 if (!qdict) {
Bandan Dasdd41eea2015-06-03 18:38:09 -04002924 monitor_printf(mon, "Try \"help %s\" for more information\n",
2925 cmd->name);
Bandan Dasae502122015-06-03 18:38:08 -04002926 return;
2927 }
2928
2929 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002930 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00002931}
2932
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08002933static void cmd_completion(Monitor *mon, const char *name, const char *list)
bellard81d09122004-07-14 17:21:37 +00002934{
2935 const char *p, *pstart;
2936 char cmd[128];
2937 int len;
2938
2939 p = list;
2940 for(;;) {
2941 pstart = p;
2942 p = strchr(p, '|');
2943 if (!p)
2944 p = pstart + strlen(pstart);
2945 len = p - pstart;
2946 if (len > sizeof(cmd) - 2)
2947 len = sizeof(cmd) - 2;
2948 memcpy(cmd, pstart, len);
2949 cmd[len] = '\0';
2950 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08002951 readline_add_completion(mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00002952 }
2953 if (*p == '\0')
2954 break;
2955 p++;
2956 }
2957}
2958
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08002959static void file_completion(Monitor *mon, const char *input)
bellard81d09122004-07-14 17:21:37 +00002960{
2961 DIR *ffs;
2962 struct dirent *d;
2963 char path[1024];
2964 char file[1024], file_prefix[1024];
2965 int input_path_len;
2966 const char *p;
2967
ths5fafdf22007-09-16 21:08:06 +00002968 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00002969 if (!p) {
2970 input_path_len = 0;
2971 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00002972 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00002973 } else {
2974 input_path_len = p - input + 1;
2975 memcpy(path, input, input_path_len);
2976 if (input_path_len > sizeof(path) - 1)
2977 input_path_len = sizeof(path) - 1;
2978 path[input_path_len] = '\0';
2979 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2980 }
Bandan Das19f2db52015-06-03 18:38:07 -04002981
bellard81d09122004-07-14 17:21:37 +00002982 ffs = opendir(path);
2983 if (!ffs)
2984 return;
2985 for(;;) {
2986 struct stat sb;
2987 d = readdir(ffs);
2988 if (!d)
2989 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09002990
2991 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
2992 continue;
2993 }
2994
bellard81d09122004-07-14 17:21:37 +00002995 if (strstart(d->d_name, file_prefix, NULL)) {
2996 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00002997 if (input_path_len < sizeof(file))
2998 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2999 d->d_name);
bellard81d09122004-07-14 17:21:37 +00003000 /* stat the file to find out if it's a directory.
3001 * In that case add a slash to speed up typing long paths
3002 */
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01003003 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
blueswir1363a37d2008-08-21 17:58:08 +00003004 pstrcat(file, sizeof(file), "/");
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01003005 }
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003006 readline_add_completion(mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003007 }
3008 }
3009 closedir(ffs);
3010}
3011
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003012static const char *next_arg_type(const char *typestr)
3013{
3014 const char *p = strchr(typestr, ':');
3015 return (p != NULL ? ++p : typestr);
3016}
3017
Hani Benhabiles40d19392014-05-07 23:41:30 +01003018static void add_completion_option(ReadLineState *rs, const char *str,
3019 const char *option)
3020{
3021 if (!str || !option) {
3022 return;
3023 }
3024 if (!strncmp(option, str, strlen(str))) {
3025 readline_add_completion(rs, option);
3026 }
3027}
3028
Hani Benhabiles13e315d2014-05-07 23:41:29 +01003029void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
3030{
3031 size_t len;
3032 ChardevBackendInfoList *list, *start;
3033
3034 if (nb_args != 2) {
3035 return;
3036 }
3037 len = strlen(str);
3038 readline_set_completion_index(rs, len);
3039
3040 start = list = qmp_query_chardev_backends(NULL);
3041 while (list) {
3042 const char *chr_name = list->value->name;
3043
3044 if (!strncmp(chr_name, str, len)) {
3045 readline_add_completion(rs, chr_name);
3046 }
3047 list = list->next;
3048 }
3049 qapi_free_ChardevBackendInfoList(start);
3050}
3051
Hani Benhabilesb162b492014-05-07 23:41:31 +01003052void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
3053{
3054 size_t len;
3055 int i;
3056
3057 if (nb_args != 2) {
3058 return;
3059 }
3060 len = strlen(str);
3061 readline_set_completion_index(rs, len);
3062 for (i = 0; NetClientOptionsKind_lookup[i]; i++) {
3063 add_completion_option(rs, str, NetClientOptionsKind_lookup[i]);
3064 }
3065}
3066
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003067void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003068{
3069 GSList *list, *elt;
3070 size_t len;
3071
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003072 if (nb_args != 2) {
3073 return;
3074 }
3075
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003076 len = strlen(str);
3077 readline_set_completion_index(rs, len);
3078 list = elt = object_class_get_list(TYPE_DEVICE, false);
3079 while (elt) {
3080 const char *name;
3081 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
3082 TYPE_DEVICE);
3083 name = object_class_get_name(OBJECT_CLASS(dc));
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003084
3085 if (!dc->cannot_instantiate_with_device_add_yet
3086 && !strncmp(name, str, len)) {
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003087 readline_add_completion(rs, name);
3088 }
3089 elt = elt->next;
3090 }
3091 g_slist_free(list);
3092}
3093
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003094void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003095{
3096 GSList *list, *elt;
3097 size_t len;
3098
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003099 if (nb_args != 2) {
3100 return;
3101 }
3102
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003103 len = strlen(str);
3104 readline_set_completion_index(rs, len);
3105 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
3106 while (elt) {
3107 const char *name;
3108
3109 name = object_class_get_name(OBJECT_CLASS(elt->data));
3110 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
3111 readline_add_completion(rs, name);
3112 }
3113 elt = elt->next;
3114 }
3115 g_slist_free(list);
3116}
3117
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003118static void peripheral_device_del_completion(ReadLineState *rs,
3119 const char *str, size_t len)
3120{
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003121 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
3122 GSList *list, *item;
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003123
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003124 list = qdev_build_hotpluggable_device_list(peripheral);
3125 if (!list) {
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003126 return;
3127 }
3128
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003129 for (item = list; item; item = g_slist_next(item)) {
3130 DeviceState *dev = item->data;
3131
3132 if (dev->id && !strncmp(str, dev->id, len)) {
3133 readline_add_completion(rs, dev->id);
3134 }
3135 }
3136
3137 g_slist_free(list);
3138}
3139
Hani Benhabiles6297d9a2014-05-07 23:41:28 +01003140void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3141{
3142 size_t len;
3143 ChardevInfoList *list, *start;
3144
3145 if (nb_args != 2) {
3146 return;
3147 }
3148 len = strlen(str);
3149 readline_set_completion_index(rs, len);
3150
3151 start = list = qmp_query_chardev(NULL);
3152 while (list) {
3153 ChardevInfo *chr = list->value;
3154
3155 if (!strncmp(chr->label, str, len)) {
3156 readline_add_completion(rs, chr->label);
3157 }
3158 list = list->next;
3159 }
3160 qapi_free_ChardevInfoList(start);
3161}
3162
Hani Benhabiles8e597772014-05-27 23:39:30 +01003163static void ringbuf_completion(ReadLineState *rs, const char *str)
3164{
3165 size_t len;
3166 ChardevInfoList *list, *start;
3167
3168 len = strlen(str);
3169 readline_set_completion_index(rs, len);
3170
3171 start = list = qmp_query_chardev(NULL);
3172 while (list) {
3173 ChardevInfo *chr_info = list->value;
3174
3175 if (!strncmp(chr_info->label, str, len)) {
3176 CharDriverState *chr = qemu_chr_find(chr_info->label);
3177 if (chr && chr_is_ringbuf(chr)) {
3178 readline_add_completion(rs, chr_info->label);
3179 }
3180 }
3181 list = list->next;
3182 }
3183 qapi_free_ChardevInfoList(start);
3184}
3185
Hani Benhabiles8e597772014-05-27 23:39:30 +01003186void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
3187{
3188 if (nb_args != 2) {
3189 return;
3190 }
3191 ringbuf_completion(rs, str);
3192}
3193
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003194void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
3195{
3196 size_t len;
3197
3198 if (nb_args != 2) {
3199 return;
3200 }
3201
3202 len = strlen(str);
3203 readline_set_completion_index(rs, len);
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003204 peripheral_device_del_completion(rs, str, len);
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003205}
3206
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003207void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003208{
3209 ObjectPropertyInfoList *list, *start;
3210 size_t len;
3211
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003212 if (nb_args != 2) {
3213 return;
3214 }
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003215 len = strlen(str);
3216 readline_set_completion_index(rs, len);
3217
3218 start = list = qmp_qom_list("/objects", NULL);
3219 while (list) {
3220 ObjectPropertyInfo *info = list->value;
3221
3222 if (!strncmp(info->type, "child<", 5)
3223 && !strncmp(info->name, str, len)) {
3224 readline_add_completion(rs, info->name);
3225 }
3226 list = list->next;
3227 }
3228 qapi_free_ObjectPropertyInfoList(start);
3229}
3230
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003231void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
3232{
3233 int i;
3234 char *sep;
3235 size_t len;
3236
3237 if (nb_args != 2) {
3238 return;
3239 }
3240 sep = strrchr(str, '-');
3241 if (sep) {
3242 str = sep + 1;
3243 }
3244 len = strlen(str);
3245 readline_set_completion_index(rs, len);
Eric Blake7fb1cf12015-11-18 01:52:57 -07003246 for (i = 0; i < Q_KEY_CODE__MAX; i++) {
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003247 if (!strncmp(str, QKeyCode_lookup[i], len)) {
3248 readline_add_completion(rs, QKeyCode_lookup[i]);
3249 }
3250 }
3251}
3252
Hani Benhabiles40d19392014-05-07 23:41:30 +01003253void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
3254{
3255 size_t len;
3256
3257 len = strlen(str);
3258 readline_set_completion_index(rs, len);
3259 if (nb_args == 2) {
Jason Wangeaed4832015-04-23 14:21:38 +08003260 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles40d19392014-05-07 23:41:30 +01003261 int count, i;
3262 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08003263 NET_CLIENT_OPTIONS_KIND_NONE,
3264 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003265 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles40d19392014-05-07 23:41:30 +01003266 const char *name = ncs[i]->name;
3267 if (!strncmp(str, name, len)) {
3268 readline_add_completion(rs, name);
3269 }
3270 }
3271 } else if (nb_args == 3) {
3272 add_completion_option(rs, str, "on");
3273 add_completion_option(rs, str, "off");
3274 }
3275}
3276
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003277void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
3278{
3279 int len, count, i;
Jason Wangeaed4832015-04-23 14:21:38 +08003280 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003281
3282 if (nb_args != 2) {
3283 return;
3284 }
3285
3286 len = strlen(str);
3287 readline_set_completion_index(rs, len);
3288 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_OPTIONS_KIND_NIC,
Jason Wangeaed4832015-04-23 14:21:38 +08003289 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003290 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003291 QemuOpts *opts;
3292 const char *name = ncs[i]->name;
3293 if (strncmp(str, name, len)) {
3294 continue;
3295 }
3296 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
3297 if (opts) {
3298 readline_add_completion(rs, name);
3299 }
3300 }
3301}
3302
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003303void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
3304{
3305 size_t len;
3306
3307 len = strlen(str);
3308 readline_set_completion_index(rs, len);
3309 if (nb_args == 2) {
3310 TraceEventID id;
3311 for (id = 0; id < trace_event_count(); id++) {
3312 const char *event_name = trace_event_get_name(trace_event_id(id));
3313 if (!strncmp(str, event_name, len)) {
3314 readline_add_completion(rs, event_name);
3315 }
3316 }
3317 } else if (nb_args == 3) {
3318 add_completion_option(rs, str, "on");
3319 add_completion_option(rs, str, "off");
3320 }
3321}
3322
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003323void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
3324{
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003325 int i;
3326
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003327 if (nb_args != 2) {
3328 return;
3329 }
3330 readline_set_completion_index(rs, strlen(str));
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003331 for (i = 0; WatchdogExpirationAction_lookup[i]; i++) {
3332 add_completion_option(rs, str, WatchdogExpirationAction_lookup[i]);
3333 }
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003334}
3335
Hani Benhabilesc68a0402014-05-27 23:39:32 +01003336void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
3337 const char *str)
3338{
3339 size_t len;
3340
3341 len = strlen(str);
3342 readline_set_completion_index(rs, len);
3343 if (nb_args == 2) {
3344 int i;
Eric Blake7fb1cf12015-11-18 01:52:57 -07003345 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Hani Benhabilesc68a0402014-05-27 23:39:32 +01003346 const char *name = MigrationCapability_lookup[i];
3347 if (!strncmp(str, name, len)) {
3348 readline_add_completion(rs, name);
3349 }
3350 }
3351 } else if (nb_args == 3) {
3352 add_completion_option(rs, str, "on");
3353 add_completion_option(rs, str, "off");
3354 }
3355}
3356
Liang Li50e9a622015-03-23 16:32:29 +08003357void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
3358 const char *str)
3359{
3360 size_t len;
3361
3362 len = strlen(str);
3363 readline_set_completion_index(rs, len);
3364 if (nb_args == 2) {
3365 int i;
Eric Blake7fb1cf12015-11-18 01:52:57 -07003366 for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
Liang Li50e9a622015-03-23 16:32:29 +08003367 const char *name = MigrationParameter_lookup[i];
3368 if (!strncmp(str, name, len)) {
3369 readline_add_completion(rs, name);
3370 }
3371 }
3372 }
3373}
3374
Hani Benhabilese3bb5322014-05-27 23:39:34 +01003375void host_net_add_completion(ReadLineState *rs, int nb_args, const char *str)
3376{
3377 int i;
3378 size_t len;
3379 if (nb_args != 2) {
3380 return;
3381 }
3382 len = strlen(str);
3383 readline_set_completion_index(rs, len);
3384 for (i = 0; host_net_devices[i]; i++) {
3385 if (!strncmp(host_net_devices[i], str, len)) {
3386 readline_add_completion(rs, host_net_devices[i]);
3387 }
3388 }
3389}
3390
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003391void host_net_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3392{
Jason Wangeaed4832015-04-23 14:21:38 +08003393 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003394 int count, i, len;
3395
3396 len = strlen(str);
3397 readline_set_completion_index(rs, len);
3398 if (nb_args == 2) {
3399 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08003400 NET_CLIENT_OPTIONS_KIND_NONE,
3401 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003402 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003403 int id;
3404 char name[16];
3405
3406 if (net_hub_id_for_client(ncs[i], &id)) {
3407 continue;
3408 }
3409 snprintf(name, sizeof(name), "%d", id);
3410 if (!strncmp(str, name, len)) {
3411 readline_add_completion(rs, name);
3412 }
3413 }
3414 return;
3415 } else if (nb_args == 3) {
3416 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08003417 NET_CLIENT_OPTIONS_KIND_NIC,
3418 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003419 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Jason Wang2c4681f2015-02-02 15:06:38 +08003420 int id;
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003421 const char *name;
3422
Jason Wang2c4681f2015-02-02 15:06:38 +08003423 if (ncs[i]->info->type == NET_CLIENT_OPTIONS_KIND_HUBPORT ||
3424 net_hub_id_for_client(ncs[i], &id)) {
3425 continue;
3426 }
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003427 name = ncs[i]->name;
3428 if (!strncmp(str, name, len)) {
3429 readline_add_completion(rs, name);
3430 }
3431 }
3432 return;
3433 }
3434}
3435
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003436static void vm_completion(ReadLineState *rs, const char *str)
3437{
3438 size_t len;
Kevin Wolf7c8eece2016-03-22 18:58:50 +01003439 BlockDriverState *bs;
Kevin Wolf88be7b42016-05-20 18:49:07 +02003440 BdrvNextIterator it;
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003441
3442 len = strlen(str);
3443 readline_set_completion_index(rs, len);
Kevin Wolf7c8eece2016-03-22 18:58:50 +01003444
Kevin Wolf88be7b42016-05-20 18:49:07 +02003445 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003446 SnapshotInfoList *snapshots, *snapshot;
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003447 AioContext *ctx = bdrv_get_aio_context(bs);
3448 bool ok = false;
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003449
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003450 aio_context_acquire(ctx);
3451 if (bdrv_can_snapshot(bs)) {
3452 ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0;
3453 }
3454 aio_context_release(ctx);
3455 if (!ok) {
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003456 continue;
3457 }
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003458
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003459 snapshot = snapshots;
3460 while (snapshot) {
3461 char *completion = snapshot->value->name;
3462 if (!strncmp(str, completion, len)) {
3463 readline_add_completion(rs, completion);
3464 }
3465 completion = snapshot->value->id;
3466 if (!strncmp(str, completion, len)) {
3467 readline_add_completion(rs, completion);
3468 }
3469 snapshot = snapshot->next;
3470 }
3471 qapi_free_SnapshotInfoList(snapshots);
3472 }
3473
3474}
3475
3476void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
3477{
3478 if (nb_args == 2) {
3479 vm_completion(rs, str);
3480 }
3481}
3482
3483void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
3484{
3485 if (nb_args == 2) {
3486 vm_completion(rs, str);
3487 }
3488}
3489
Wenchao Xiac35b6402013-08-27 20:38:24 +08003490static void monitor_find_completion_by_table(Monitor *mon,
3491 const mon_cmd_t *cmd_table,
3492 char **args,
3493 int nb_args)
bellard81d09122004-07-14 17:21:37 +00003494{
3495 const char *cmdname;
Wenchao Xiac35b6402013-08-27 20:38:24 +08003496 int i;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003497 const char *ptype, *str, *name;
Anthony Liguoric227f092009-10-01 16:12:16 -05003498 const mon_cmd_t *cmd;
Max Reitzda27a002016-03-16 19:54:29 +01003499 BlockBackend *blk = NULL;
bellard81d09122004-07-14 17:21:37 +00003500
bellard81d09122004-07-14 17:21:37 +00003501 if (nb_args <= 1) {
3502 /* command completion */
3503 if (nb_args == 0)
3504 cmdname = "";
3505 else
3506 cmdname = args[0];
Wenchao Xiad2674b22013-08-27 20:38:16 +08003507 readline_set_completion_index(mon->rs, strlen(cmdname));
Wenchao Xiac35b6402013-08-27 20:38:24 +08003508 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003509 cmd_completion(mon, cmdname, cmd->name);
bellard81d09122004-07-14 17:21:37 +00003510 }
3511 } else {
3512 /* find the command */
Wenchao Xiac35b6402013-08-27 20:38:24 +08003513 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Jan Kiszka03a63482010-06-16 00:38:33 +02003514 if (compare_cmd(args[0], cmd->name)) {
3515 break;
3516 }
bellard81d09122004-07-14 17:21:37 +00003517 }
Jan Kiszka03a63482010-06-16 00:38:33 +02003518 if (!cmd->name) {
Wenchao Xiac35b6402013-08-27 20:38:24 +08003519 return;
Jan Kiszka03a63482010-06-16 00:38:33 +02003520 }
3521
Wenchao Xiad903a772013-08-27 20:38:25 +08003522 if (cmd->sub_table) {
3523 /* do the job again */
Stefan Weile7ae7712015-03-08 19:30:01 +01003524 monitor_find_completion_by_table(mon, cmd->sub_table,
3525 &args[1], nb_args - 1);
3526 return;
Wenchao Xiad903a772013-08-27 20:38:25 +08003527 }
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003528 if (cmd->command_completion) {
Stefan Weile7ae7712015-03-08 19:30:01 +01003529 cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
3530 return;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003531 }
Wenchao Xiad903a772013-08-27 20:38:25 +08003532
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003533 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003534 for(i = 0; i < nb_args - 2; i++) {
3535 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003536 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003537 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003538 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003539 }
3540 }
3541 str = args[nb_args - 1];
Kevin Wolf48fe86f2014-11-12 16:24:02 +01003542 while (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02003543 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00003544 }
bellard81d09122004-07-14 17:21:37 +00003545 switch(*ptype) {
3546 case 'F':
3547 /* file completion */
Wenchao Xiad2674b22013-08-27 20:38:16 +08003548 readline_set_completion_index(mon->rs, strlen(str));
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003549 file_completion(mon, str);
bellard81d09122004-07-14 17:21:37 +00003550 break;
3551 case 'B':
3552 /* block device name completion */
Wenchao Xia599a9262013-08-27 20:38:15 +08003553 readline_set_completion_index(mon->rs, strlen(str));
Max Reitzda27a002016-03-16 19:54:29 +01003554 while ((blk = blk_next(blk)) != NULL) {
3555 name = blk_name(blk);
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003556 if (str[0] == '\0' ||
3557 !strncmp(name, str, strlen(str))) {
3558 readline_add_completion(mon->rs, name);
3559 }
3560 }
bellard81d09122004-07-14 17:21:37 +00003561 break;
bellard7fe48482004-10-09 18:08:01 +00003562 case 's':
Wenchao Xia129be002013-08-27 20:38:26 +08003563 case 'S':
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003564 if (!strcmp(cmd->name, "help|?")) {
Wenchao Xia7ca0e062013-08-27 20:38:27 +08003565 monitor_find_completion_by_table(mon, cmd_table,
3566 &args[1], nb_args - 1);
bellard7fe48482004-10-09 18:08:01 +00003567 }
3568 break;
bellard81d09122004-07-14 17:21:37 +00003569 default:
3570 break;
3571 }
3572 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08003573}
3574
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003575static void monitor_find_completion(void *opaque,
Wenchao Xiac35b6402013-08-27 20:38:24 +08003576 const char *cmdline)
3577{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003578 Monitor *mon = opaque;
Wenchao Xiac35b6402013-08-27 20:38:24 +08003579 char *args[MAX_ARGS];
3580 int nb_args, len;
3581
3582 /* 1. parse the cmdline */
3583 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
3584 return;
3585 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08003586
3587 /* if the line ends with a space, it means we want to complete the
3588 next arg */
3589 len = strlen(cmdline);
3590 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
3591 if (nb_args >= MAX_ARGS) {
3592 goto cleanup;
3593 }
3594 args[nb_args++] = g_strdup("");
3595 }
3596
3597 /* 2. auto complete according to args */
3598 monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args);
Jan Kiszka03a63482010-06-16 00:38:33 +02003599
3600cleanup:
Wenchao Xiadcc70cd2013-08-27 20:38:22 +08003601 free_cmdline_args(args, nb_args);
bellard81d09122004-07-14 17:21:37 +00003602}
3603
aliguori731b0362009-03-05 23:01:42 +00003604static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003605{
aliguori731b0362009-03-05 23:01:42 +00003606 Monitor *mon = opaque;
3607
Jan Kiszkac62313b2009-12-04 14:05:29 +01003608 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003609}
3610
Markus Armbruster40861822015-05-29 10:27:16 +02003611static bool invalid_qmp_mode(const Monitor *mon, const mon_cmd_t *cmd,
3612 Error **errp)
Luiz Capitulino09069b12010-02-04 18:10:06 -02003613{
Markus Armbruster485febc2015-03-13 17:25:50 +01003614 bool is_cap = cmd->mhandler.cmd_new == qmp_capabilities;
Markus Armbrusterf994b252015-03-06 19:51:51 +01003615
3616 if (is_cap && mon->qmp.in_command_mode) {
Markus Armbruster40861822015-05-29 10:27:16 +02003617 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
3618 "Capabilities negotiation is already complete, command "
3619 "'%s' ignored", cmd->name);
Eric Blake2d5a8342015-04-15 09:19:23 -06003620 return true;
3621 }
Markus Armbrusterf994b252015-03-06 19:51:51 +01003622 if (!is_cap && !mon->qmp.in_command_mode) {
Markus Armbruster40861822015-05-29 10:27:16 +02003623 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
3624 "Expecting capabilities negotiation with "
3625 "'qmp_capabilities' before command '%s'", cmd->name);
Eric Blake2d5a8342015-04-15 09:19:23 -06003626 return true;
3627 }
3628 return false;
Luiz Capitulino09069b12010-02-04 18:10:06 -02003629}
3630
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003631/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03003632 * Argument validation rules:
3633 *
3634 * 1. The argument must exist in cmd_args qdict
3635 * 2. The argument type must be the expected one
3636 *
3637 * Special case: If the argument doesn't exist in cmd_args and
3638 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
3639 * checking is skipped for it.
3640 */
Markus Armbruster326283a2015-03-02 18:39:09 +01003641static void check_client_args_type(const QDict *client_args,
3642 const QDict *cmd_args, int flags,
3643 Error **errp)
Luiz Capitulino4af91932010-06-22 11:44:05 -03003644{
3645 const QDictEntry *ent;
3646
3647 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
3648 QObject *obj;
3649 QString *arg_type;
3650 const QObject *client_arg = qdict_entry_value(ent);
3651 const char *client_arg_name = qdict_entry_key(ent);
3652
3653 obj = qdict_get(cmd_args, client_arg_name);
3654 if (!obj) {
3655 if (flags & QMP_ACCEPT_UNKNOWNS) {
3656 /* handler accepts unknowns */
3657 continue;
3658 }
3659 /* client arg doesn't exist */
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003660 error_setg(errp, QERR_INVALID_PARAMETER, client_arg_name);
Markus Armbruster326283a2015-03-02 18:39:09 +01003661 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003662 }
3663
3664 arg_type = qobject_to_qstring(obj);
3665 assert(arg_type != NULL);
3666
3667 /* check if argument's type is correct */
3668 switch (qstring_get_str(arg_type)[0]) {
3669 case 'F':
3670 case 'B':
3671 case 's':
3672 if (qobject_type(client_arg) != QTYPE_QSTRING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003673 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3674 client_arg_name, "string");
Markus Armbruster326283a2015-03-02 18:39:09 +01003675 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003676 }
3677 break;
3678 case 'i':
3679 case 'l':
3680 case 'M':
Jes Sorensendbc0c672010-10-21 17:15:47 +02003681 case 'o':
Luiz Capitulino4af91932010-06-22 11:44:05 -03003682 if (qobject_type(client_arg) != QTYPE_QINT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003683 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3684 client_arg_name, "int");
Markus Armbruster326283a2015-03-02 18:39:09 +01003685 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003686 }
3687 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003688 case 'T':
3689 if (qobject_type(client_arg) != QTYPE_QINT &&
3690 qobject_type(client_arg) != QTYPE_QFLOAT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003691 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3692 client_arg_name, "number");
Markus Armbruster326283a2015-03-02 18:39:09 +01003693 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003694 }
3695 break;
3696 case 'b':
3697 case '-':
3698 if (qobject_type(client_arg) != QTYPE_QBOOL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003699 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3700 client_arg_name, "bool");
Markus Armbruster326283a2015-03-02 18:39:09 +01003701 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003702 }
3703 break;
3704 case 'O':
3705 assert(flags & QMP_ACCEPT_UNKNOWNS);
3706 break;
Paolo Bonzinib9f89782012-03-22 12:51:11 +01003707 case 'q':
3708 /* Any QObject can be passed. */
3709 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003710 case '/':
3711 case '.':
3712 /*
3713 * These types are not supported by QMP and thus are not
3714 * handled here. Fall through.
3715 */
3716 default:
3717 abort();
3718 }
3719 }
Luiz Capitulino4af91932010-06-22 11:44:05 -03003720}
3721
3722/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003723 * - Check if the client has passed all mandatory args
3724 * - Set special flags for argument validation
3725 */
Markus Armbruster326283a2015-03-02 18:39:09 +01003726static void check_mandatory_args(const QDict *cmd_args,
3727 const QDict *client_args, int *flags,
3728 Error **errp)
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003729{
3730 const QDictEntry *ent;
3731
3732 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
3733 const char *cmd_arg_name = qdict_entry_key(ent);
3734 QString *type = qobject_to_qstring(qdict_entry_value(ent));
3735 assert(type != NULL);
3736
3737 if (qstring_get_str(type)[0] == 'O') {
3738 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
3739 *flags |= QMP_ACCEPT_UNKNOWNS;
3740 } else if (qstring_get_str(type)[0] != '-' &&
3741 qstring_get_str(type)[1] != '?' &&
3742 !qdict_haskey(client_args, cmd_arg_name)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003743 error_setg(errp, QERR_MISSING_PARAMETER, cmd_arg_name);
Markus Armbruster326283a2015-03-02 18:39:09 +01003744 return;
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003745 }
3746 }
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003747}
3748
3749static QDict *qdict_from_args_type(const char *args_type)
3750{
3751 int i;
3752 QDict *qdict;
3753 QString *key, *type, *cur_qs;
3754
3755 assert(args_type != NULL);
3756
3757 qdict = qdict_new();
3758
3759 if (args_type == NULL || args_type[0] == '\0') {
3760 /* no args, empty qdict */
3761 goto out;
3762 }
3763
3764 key = qstring_new();
3765 type = qstring_new();
3766
3767 cur_qs = key;
3768
3769 for (i = 0;; i++) {
3770 switch (args_type[i]) {
3771 case ',':
3772 case '\0':
3773 qdict_put(qdict, qstring_get_str(key), type);
3774 QDECREF(key);
3775 if (args_type[i] == '\0') {
3776 goto out;
3777 }
3778 type = qstring_new(); /* qdict has ref */
3779 cur_qs = key = qstring_new();
3780 break;
3781 case ':':
3782 cur_qs = type;
3783 break;
3784 default:
3785 qstring_append_chr(cur_qs, args_type[i]);
3786 break;
3787 }
3788 }
3789
3790out:
3791 return qdict;
3792}
3793
3794/*
3795 * Client argument checking rules:
3796 *
3797 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03003798 * 2. Each argument provided by the client must be expected
3799 * 3. Each argument provided by the client must have the type expected
3800 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003801 */
Markus Armbruster326283a2015-03-02 18:39:09 +01003802static void qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args,
3803 Error **errp)
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003804{
Markus Armbruster326283a2015-03-02 18:39:09 +01003805 Error *err = NULL;
3806 int flags;
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003807 QDict *cmd_args;
3808
3809 cmd_args = qdict_from_args_type(cmd->args_type);
3810
3811 flags = 0;
Markus Armbruster326283a2015-03-02 18:39:09 +01003812 check_mandatory_args(cmd_args, client_args, &flags, &err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003813 if (err) {
3814 goto out;
3815 }
3816
Markus Armbruster326283a2015-03-02 18:39:09 +01003817 check_client_args_type(client_args, cmd_args, flags, &err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003818
3819out:
Markus Armbruster326283a2015-03-02 18:39:09 +01003820 error_propagate(errp, err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003821 QDECREF(cmd_args);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003822}
3823
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003824/*
3825 * Input object checking rules
3826 *
3827 * 1. Input object must be a dict
3828 * 2. The "execute" key must exist
3829 * 3. The "execute" key must be a string
3830 * 4. If the "arguments" key exists, it must be a dict
3831 * 5. If the "id" key exists, it can be anything (ie. json-value)
3832 * 6. Any argument not listed above is considered invalid
3833 */
Markus Armbrusterba0510a2015-03-02 18:41:43 +01003834static QDict *qmp_check_input_obj(QObject *input_obj, Error **errp)
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003835{
3836 const QDictEntry *ent;
3837 int has_exec_key = 0;
3838 QDict *input_dict;
3839
3840 if (qobject_type(input_obj) != QTYPE_QDICT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003841 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "object");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003842 return NULL;
3843 }
3844
3845 input_dict = qobject_to_qdict(input_obj);
3846
3847 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
3848 const char *arg_name = qdict_entry_key(ent);
3849 const QObject *arg_obj = qdict_entry_value(ent);
3850
3851 if (!strcmp(arg_name, "execute")) {
3852 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003853 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
3854 "execute", "string");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003855 return NULL;
3856 }
3857 has_exec_key = 1;
3858 } else if (!strcmp(arg_name, "arguments")) {
3859 if (qobject_type(arg_obj) != QTYPE_QDICT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003860 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
3861 "arguments", "object");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003862 return NULL;
3863 }
Markus Armbruster779cec42015-06-08 10:44:30 +02003864 } else if (!strcmp(arg_name, "id")) {
3865 /* Any string is acceptable as "id", so nothing to check */
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003866 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003867 error_setg(errp, QERR_QMP_EXTRA_MEMBER, arg_name);
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003868 return NULL;
3869 }
3870 }
3871
3872 if (!has_exec_key) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003873 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "execute");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003874 return NULL;
3875 }
3876
3877 return input_dict;
3878}
3879
Paolo Bonzini95385fe2015-11-25 22:23:31 +01003880static void handle_qmp_command(JSONMessageParser *parser, GQueue *tokens)
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003881{
Markus Armbruster326283a2015-03-02 18:39:09 +01003882 Error *local_err = NULL;
Markus Armbruster84add862015-03-05 16:45:15 +01003883 QObject *obj, *data;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003884 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003885 const mon_cmd_t *cmd;
Luiz Capitulino40e5a012011-10-21 16:15:31 -02003886 const char *cmd_name;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003887 Monitor *mon = cur_mon;
3888
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03003889 args = input = NULL;
Markus Armbruster84add862015-03-05 16:45:15 +01003890 data = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003891
3892 obj = json_parser_parse(tokens, NULL);
3893 if (!obj) {
3894 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003895 error_setg(&local_err, QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003896 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003897 }
3898
Markus Armbrusterba0510a2015-03-02 18:41:43 +01003899 input = qmp_check_input_obj(obj, &local_err);
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003900 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003901 qobject_decref(obj);
3902 goto err_out;
3903 }
3904
Markus Armbruster74358f22015-03-06 19:35:59 +01003905 mon->qmp.id = qdict_get(input, "id");
3906 qobject_incref(mon->qmp.id);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003907
Luiz Capitulino0bbab462010-05-31 17:32:50 -03003908 cmd_name = qdict_get_str(input, "execute");
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +01003909 trace_handle_qmp_command(mon, cmd_name);
Anthony Liguorie3193602011-09-02 12:34:47 -05003910 cmd = qmp_find_cmd(cmd_name);
Eric Blake2d5a8342015-04-15 09:19:23 -06003911 if (!cmd) {
Markus Armbruster710aec92015-03-06 11:28:00 +01003912 error_set(&local_err, ERROR_CLASS_COMMAND_NOT_FOUND,
3913 "The command %s has not been found", cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03003914 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003915 }
Markus Armbruster40861822015-05-29 10:27:16 +02003916 if (invalid_qmp_mode(mon, cmd, &local_err)) {
Eric Blake2d5a8342015-04-15 09:19:23 -06003917 goto err_out;
3918 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003919
3920 obj = qdict_get(input, "arguments");
3921 if (!obj) {
3922 args = qdict_new();
3923 } else {
3924 args = qobject_to_qdict(obj);
3925 QINCREF(args);
3926 }
3927
Markus Armbruster326283a2015-03-02 18:39:09 +01003928 qmp_check_client_args(cmd, args, &local_err);
3929 if (local_err) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003930 goto err_out;
3931 }
3932
Markus Armbruster485febc2015-03-13 17:25:50 +01003933 cmd->mhandler.cmd_new(args, &data, &local_err);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003934
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003935err_out:
Markus Armbruster710aec92015-03-06 11:28:00 +01003936 monitor_protocol_emitter(mon, data, local_err);
Markus Armbruster84add862015-03-05 16:45:15 +01003937 qobject_decref(data);
Markus Armbruster3a81a102015-10-29 12:15:09 +01003938 error_free(local_err);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03003939 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003940 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003941}
3942
Markus Armbrusterc83fe232015-03-06 19:20:51 +01003943static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003944{
3945 Monitor *old_mon = cur_mon;
3946
3947 cur_mon = opaque;
3948
Markus Armbruster74358f22015-03-06 19:35:59 +01003949 json_message_parser_feed(&cur_mon->qmp.parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003950
3951 cur_mon = old_mon;
3952}
3953
aliguori731b0362009-03-05 23:01:42 +00003954static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00003955{
aliguori731b0362009-03-05 23:01:42 +00003956 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00003957 int i;
aliguori376253e2009-03-05 23:01:23 +00003958
aliguori731b0362009-03-05 23:01:42 +00003959 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00003960
aliguoricde76ee2009-03-05 23:01:51 +00003961 if (cur_mon->rs) {
3962 for (i = 0; i < size; i++)
3963 readline_handle_byte(cur_mon->rs, buf[i]);
3964 } else {
3965 if (size == 0 || buf[size - 1] != 0)
3966 monitor_printf(cur_mon, "corrupted command\n");
3967 else
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003968 handle_hmp_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00003969 }
aliguori731b0362009-03-05 23:01:42 +00003970
3971 cur_mon = old_mon;
3972}
aliguorid8f44602008-10-06 13:52:44 +00003973
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003974static void monitor_command_cb(void *opaque, const char *cmdline,
3975 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00003976{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003977 Monitor *mon = opaque;
3978
aliguori731b0362009-03-05 23:01:42 +00003979 monitor_suspend(mon);
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003980 handle_hmp_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00003981 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00003982}
3983
aliguoricde76ee2009-03-05 23:01:51 +00003984int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003985{
aliguoricde76ee2009-03-05 23:01:51 +00003986 if (!mon->rs)
3987 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00003988 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00003989 return 0;
aliguorid8f44602008-10-06 13:52:44 +00003990}
3991
aliguori376253e2009-03-05 23:01:23 +00003992void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003993{
aliguoricde76ee2009-03-05 23:01:51 +00003994 if (!mon->rs)
3995 return;
aliguori731b0362009-03-05 23:01:42 +00003996 if (--mon->suspend_cnt == 0)
3997 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00003998}
3999
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004000static QObject *get_qmp_greeting(void)
4001{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03004002 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004003
Markus Armbruster7fad30f2015-09-16 13:06:19 +02004004 qmp_marshal_query_version(NULL, &ver, NULL);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004005 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
4006}
4007
Markus Armbrusterc83fe232015-03-06 19:20:51 +01004008static void monitor_qmp_event(void *opaque, int event)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004009{
Luiz Capitulino47116d12010-02-08 17:01:30 -02004010 QObject *data;
4011 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004012
Luiz Capitulino47116d12010-02-08 17:01:30 -02004013 switch (event) {
4014 case CHR_EVENT_OPENED:
Markus Armbrusterf994b252015-03-06 19:51:51 +01004015 mon->qmp.in_command_mode = false;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004016 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004017 monitor_json_emitter(mon, data);
4018 qobject_decref(data);
Corey Bryantefb87c12012-08-14 16:43:48 -04004019 mon_refcount++;
Luiz Capitulino47116d12010-02-08 17:01:30 -02004020 break;
4021 case CHR_EVENT_CLOSED:
Markus Armbruster74358f22015-03-06 19:35:59 +01004022 json_message_parser_destroy(&mon->qmp.parser);
4023 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Corey Bryantefb87c12012-08-14 16:43:48 -04004024 mon_refcount--;
4025 monitor_fdsets_cleanup();
Luiz Capitulino47116d12010-02-08 17:01:30 -02004026 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004027 }
4028}
4029
aliguori731b0362009-03-05 23:01:42 +00004030static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004031{
aliguori376253e2009-03-05 23:01:23 +00004032 Monitor *mon = opaque;
4033
aliguori2724b182009-03-05 23:01:47 +00004034 switch (event) {
4035 case CHR_EVENT_MUX_IN:
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02004036 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004037 mon->mux_out = 0;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02004038 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004039 if (mon->reset_seen) {
4040 readline_restart(mon->rs);
4041 monitor_resume(mon);
4042 monitor_flush(mon);
4043 } else {
4044 mon->suspend_cnt = 0;
4045 }
aliguori2724b182009-03-05 23:01:47 +00004046 break;
ths86e94de2007-01-05 22:01:59 +00004047
aliguori2724b182009-03-05 23:01:47 +00004048 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004049 if (mon->reset_seen) {
4050 if (mon->suspend_cnt == 0) {
4051 monitor_printf(mon, "\n");
4052 }
4053 monitor_flush(mon);
4054 monitor_suspend(mon);
4055 } else {
4056 mon->suspend_cnt++;
4057 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02004058 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004059 mon->mux_out = 1;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02004060 qemu_mutex_unlock(&mon->out_lock);
aliguori2724b182009-03-05 23:01:47 +00004061 break;
4062
Amit Shahb6b8df52009-10-07 18:31:16 +05304063 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004064 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4065 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004066 if (!mon->mux_out) {
Stratos Psomadakise5554e22014-09-15 15:34:57 +03004067 readline_restart(mon->rs);
aliguori2724b182009-03-05 23:01:47 +00004068 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004069 }
4070 mon->reset_seen = 1;
Corey Bryantefb87c12012-08-14 16:43:48 -04004071 mon_refcount++;
4072 break;
4073
4074 case CHR_EVENT_CLOSED:
4075 mon_refcount--;
4076 monitor_fdsets_cleanup();
aliguori2724b182009-03-05 23:01:47 +00004077 break;
4078 }
ths86e94de2007-01-05 22:01:59 +00004079}
4080
Wayne Xia816f8922011-10-12 11:32:41 +08004081static int
4082compare_mon_cmd(const void *a, const void *b)
4083{
4084 return strcmp(((const mon_cmd_t *)a)->name,
4085 ((const mon_cmd_t *)b)->name);
4086}
4087
4088static void sortcmdlist(void)
4089{
4090 int array_num;
4091 int elem_size = sizeof(mon_cmd_t);
4092
4093 array_num = sizeof(mon_cmds)/elem_size-1;
4094 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
4095
4096 array_num = sizeof(info_cmds)/elem_size-1;
4097 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
4098}
4099
aliguori76655d62009-03-06 20:27:37 +00004100
4101/*
4102 * Local variables:
4103 * c-indent-level: 4
4104 * c-basic-offset: 4
4105 * tab-width: 8
4106 * End:
4107 */
4108
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004109/* These functions just adapt the readline interface in a typesafe way. We
4110 * could cast function pointers but that discards compiler checks.
4111 */
Stefan Weild5d15072014-01-25 18:18:23 +01004112static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
4113 const char *fmt, ...)
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004114{
4115 va_list ap;
4116 va_start(ap, fmt);
4117 monitor_vprintf(opaque, fmt, ap);
4118 va_end(ap);
4119}
4120
4121static void monitor_readline_flush(void *opaque)
4122{
4123 monitor_flush(opaque);
4124}
4125
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004126static void __attribute__((constructor)) monitor_lock_init(void)
4127{
4128 qemu_mutex_init(&monitor_lock);
4129}
4130
aliguori731b0362009-03-05 23:01:42 +00004131void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004132{
aliguori731b0362009-03-05 23:01:42 +00004133 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004134 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004135
4136 if (is_first_init) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +02004137 monitor_qapi_event_init();
Wenchao Xiad0383172013-08-27 20:38:18 +08004138 sortcmdlist();
ths20d8a3e2007-02-18 17:04:49 +00004139 is_first_init = 0;
4140 }
aliguori87127162009-03-05 23:01:29 +00004141
Wenchao Xiab01fe892013-08-27 20:38:19 +08004142 mon = g_malloc(sizeof(*mon));
4143 monitor_data_init(mon);
ths20d8a3e2007-02-18 17:04:49 +00004144
aliguori87127162009-03-05 23:01:29 +00004145 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00004146 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004147 if (flags & MONITOR_USE_READLINE) {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004148 mon->rs = readline_init(monitor_readline_printf,
4149 monitor_readline_flush,
4150 mon,
4151 monitor_find_completion);
aliguoricde76ee2009-03-05 23:01:51 +00004152 monitor_read_command(mon, 0);
4153 }
aliguori87127162009-03-05 23:01:29 +00004154
Markus Armbruster9f3982f2015-03-06 19:56:38 +01004155 if (monitor_is_qmp(mon)) {
Markus Armbrusterc83fe232015-03-06 19:20:51 +01004156 qemu_chr_add_handlers(chr, monitor_can_read, monitor_qmp_read,
4157 monitor_qmp_event, mon);
Anthony Liguori15f31512011-08-15 11:17:35 -05004158 qemu_chr_fe_set_echo(chr, true);
Markus Armbruster74358f22015-03-06 19:35:59 +01004159 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004160 } else {
4161 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4162 monitor_event, mon);
4163 }
aliguori87127162009-03-05 23:01:29 +00004164
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004165 qemu_mutex_lock(&monitor_lock);
Blue Swirl72cf2d42009-09-12 07:36:22 +00004166 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004167 qemu_mutex_unlock(&monitor_lock);
bellard7e2515e2004-08-01 21:52:19 +00004168}
4169
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004170static void bdrv_password_cb(void *opaque, const char *password,
4171 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00004172{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004173 Monitor *mon = opaque;
4174 BlockDriverState *bs = readline_opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004175 int ret = 0;
Markus Armbruster4d2855a2015-01-29 10:37:00 +01004176 Error *local_err = NULL;
bellard7e2515e2004-08-01 21:52:19 +00004177
Markus Armbruster4d2855a2015-01-29 10:37:00 +01004178 bdrv_add_key(bs, password, &local_err);
4179 if (local_err) {
Markus Armbruster193227f2015-12-18 16:35:06 +01004180 error_report_err(local_err);
aliguoribb5fc202009-03-05 23:01:15 +00004181 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004182 }
aliguori731b0362009-03-05 23:01:42 +00004183 if (mon->password_completion_cb)
4184 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004185
aliguori731b0362009-03-05 23:01:42 +00004186 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004187}
aliguoric0f4ce72009-03-05 23:01:01 +00004188
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004189int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
Markus Armbruster097310b2014-10-07 13:59:15 +02004190 BlockCompletionFunc *completion_cb,
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004191 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004192{
aliguoricde76ee2009-03-05 23:01:51 +00004193 int err;
4194
aliguori376253e2009-03-05 23:01:23 +00004195 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4196 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00004197
aliguori731b0362009-03-05 23:01:42 +00004198 mon->password_completion_cb = completion_cb;
4199 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004200
aliguoricde76ee2009-03-05 23:01:51 +00004201 err = monitor_read_password(mon, bdrv_password_cb, bs);
4202
4203 if (err && completion_cb)
4204 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004205
4206 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00004207}
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004208
4209int monitor_read_block_device_key(Monitor *mon, const char *device,
Markus Armbruster097310b2014-10-07 13:59:15 +02004210 BlockCompletionFunc *completion_cb,
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004211 void *opaque)
4212{
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01004213 Error *err = NULL;
Fam Zheng55606252015-03-02 19:36:46 +08004214 BlockBackend *blk;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004215
Fam Zheng55606252015-03-02 19:36:46 +08004216 blk = blk_by_name(device);
4217 if (!blk) {
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004218 monitor_printf(mon, "Device not found %s\n", device);
4219 return -1;
4220 }
Max Reitz5433c242015-10-19 17:53:29 +02004221 if (!blk_bs(blk)) {
4222 monitor_printf(mon, "Device '%s' has no medium\n", device);
4223 return -1;
4224 }
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004225
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01004226 bdrv_add_key(blk_bs(blk), NULL, &err);
4227 if (err) {
4228 error_free(err);
4229 return monitor_read_bdrv_key_start(mon, blk_bs(blk), completion_cb, opaque);
4230 }
4231
4232 if (completion_cb) {
4233 completion_cb(opaque, 0);
4234 }
4235 return 0;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004236}
Paolo Bonzini4d454572012-11-26 16:03:42 +01004237
4238QemuOptsList qemu_mon_opts = {
4239 .name = "mon",
4240 .implied_opt_name = "chardev",
4241 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
4242 .desc = {
4243 {
4244 .name = "mode",
4245 .type = QEMU_OPT_STRING,
4246 },{
4247 .name = "chardev",
4248 .type = QEMU_OPT_STRING,
4249 },{
4250 .name = "default",
4251 .type = QEMU_OPT_BOOL,
4252 },{
4253 .name = "pretty",
4254 .type = QEMU_OPT_BOOL,
4255 },
4256 { /* end of list */ }
4257 },
4258};
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004259
4260#ifndef TARGET_I386
4261void qmp_rtc_reset_reinjection(Error **errp)
4262{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004263 error_setg(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection");
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004264}
4265#endif
Jason J. Herne7ee0c3e2015-06-26 14:03:16 -04004266
4267#ifndef TARGET_S390X
4268void qmp_dump_skeys(const char *filename, Error **errp)
4269{
4270 error_setg(errp, QERR_FEATURE_DISABLED, "dump-skeys");
4271}
4272#endif
Peter Xuae50a772016-03-30 17:27:24 +01004273
4274#ifndef TARGET_ARM
4275GICCapabilityList *qmp_query_gic_capabilities(Error **errp)
4276{
4277 error_setg(errp, QERR_FEATURE_DISABLED, "query-gic-capabilities");
4278 return NULL;
4279}
4280#endif
Igor Mammedovd4633542016-06-10 06:29:06 +05304281
4282HotpluggableCPUList *qmp_query_hotpluggable_cpus(Error **errp)
4283{
4284 MachineState *ms = MACHINE(qdev_get_machine());
4285 MachineClass *mc = MACHINE_GET_CLASS(ms);
4286
4287 if (!mc->query_hotpluggable_cpus) {
4288 error_setg(errp, QERR_FEATURE_DISABLED, "query-hotpluggable-cpus");
4289 return NULL;
4290 }
4291
4292 return mc->query_hotpluggable_cpus(ms);
4293}