blob: 812383ff4d6f25219b193d03d783c369d3875dad [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"
Anthony Liguori48a32be2011-09-02 12:34:48 -050074#include "qmp-commands.h"
75#include "hmp.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010076#include "qemu/thread.h"
Hani Benhabilesb21631f2014-05-27 23:39:37 +010077#include "block/qapi.h"
Wenchao Xia43a14cf2014-06-18 08:43:31 +020078#include "qapi/qmp-event.h"
79#include "qapi-event.h"
Markus Armbruster39a18152015-09-16 13:06:28 +020080#include "qmp-introspect.h"
Fam Zheng55606252015-03-02 19:36:46 +080081#include "sysemu/block-backend.h"
Alberto Garciadc599972016-03-10 13:55:26 +020082#include "sysemu/qtest.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020083#include "qemu/cutils.h"
ths6a5bd302007-12-03 17:05:38 +000084
Markus Armbruster1ce6be22015-02-06 14:18:24 +010085/* for hmp_info_irq/pic */
Jan Kiszka661f1922011-10-16 11:53:13 +020086#if defined(TARGET_SPARC)
Paolo Bonzini0d09e412013-02-05 17:06:20 +010087#include "hw/sparc/sun4m.h"
Jan Kiszka661f1922011-10-16 11:53:13 +020088#endif
Paolo Bonzini0d09e412013-02-05 17:06:20 +010089#include "hw/lm32/lm32_pic.h"
Jan Kiszka661f1922011-10-16 11:53:13 +020090
Jason J. Hernea4538a52015-06-26 14:07:21 -040091#if defined(TARGET_S390X)
92#include "hw/s390x/storage-keys.h"
93#endif
94
bellard9307c4c2004-04-04 12:57:25 +000095/*
96 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000097 *
bellard9307c4c2004-04-04 12:57:25 +000098 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000099 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +0000100 * 's' string (accept optional quote)
Wenchao Xia129be002013-08-27 20:38:26 +0800101 * 'S' it just appends the rest of the string (accept optional quote)
Markus Armbruster361127d2010-02-10 20:24:35 +0100102 * 'O' option string of the form NAME=VALUE,...
103 * parsed according to QemuOptsList given by its name
104 * Example: 'device:O' uses qemu_device_opts.
105 * Restriction: only lists with empty desc are supported
106 * TODO lift the restriction
bellard92a31b12005-02-10 22:00:52 +0000107 * 'i' 32 bit integer
108 * 'l' target long (32 or 64 bit)
Luiz Capitulino91162842012-04-26 17:34:30 -0300109 * 'M' Non-negative target long (32 or 64 bit), in user mode the
110 * value is multiplied by 2^20 (think Mebibyte)
Jes Sorensendbc0c672010-10-21 17:15:47 +0200111 * 'o' octets (aka bytes)
Kevin Wolf5e009842013-06-05 14:19:27 +0200112 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
113 * K, k suffix, which multiplies the value by 2^60 for suffixes E
114 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
115 * 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 +0100116 * 'T' double
117 * user mode accepts an optional ms, us, ns suffix,
118 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +0000119 * '/' optional gdb-like print format (like "/10x")
120 *
Luiz Capitulinofb466602009-08-28 15:27:27 -0300121 * '?' optional type (for all types, except '/')
122 * '.' other form of optional type (for 'i' and 'l')
Markus Armbruster942cd1f2010-03-26 09:07:09 +0100123 * 'b' boolean
124 * user mode accepts "on" or "off"
Luiz Capitulinofb466602009-08-28 15:27:27 -0300125 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +0000126 *
127 */
128
Anthony Liguoric227f092009-10-01 16:12:16 -0500129typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +0000130 const char *name;
bellard9307c4c2004-04-04 12:57:25 +0000131 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +0000132 const char *params;
133 const char *help;
Luiz Capitulino910df892009-10-07 13:41:51 -0300134 union {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -0300135 void (*cmd)(Monitor *mon, const QDict *qdict);
Markus Armbruster485febc2015-03-13 17:25:50 +0100136 void (*cmd_new)(QDict *params, QObject **ret_data, Error **errp);
Luiz Capitulino910df892009-10-07 13:41:51 -0300137 } mhandler;
Wenchao Xia5f3d3352013-01-14 14:06:27 +0800138 /* @sub_table is a list of 2nd level of commands. If it do not exist,
139 * mhandler should be used. If it exist, sub_table[?].mhandler should be
140 * used, and mhandler of 1st level plays the role of help function.
141 */
142 struct mon_cmd_t *sub_table;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +0100143 void (*command_completion)(ReadLineState *rs, int nb_args, const char *str);
Anthony Liguoric227f092009-10-01 16:12:16 -0500144} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000145
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100146/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500147typedef struct mon_fd_t mon_fd_t;
148struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100149 char *name;
150 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500151 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100152};
153
Corey Bryantba1c0482012-08-14 16:43:43 -0400154/* file descriptor associated with a file descriptor set */
155typedef struct MonFdsetFd MonFdsetFd;
156struct MonFdsetFd {
157 int fd;
158 bool removed;
159 char *opaque;
160 QLIST_ENTRY(MonFdsetFd) next;
161};
162
163/* file descriptor set containing fds passed via SCM_RIGHTS */
164typedef struct MonFdset MonFdset;
165struct MonFdset {
166 int64_t id;
167 QLIST_HEAD(, MonFdsetFd) fds;
Corey Bryantadb696f2012-08-14 16:43:47 -0400168 QLIST_HEAD(, MonFdsetFd) dup_fds;
Corey Bryantba1c0482012-08-14 16:43:43 -0400169 QLIST_ENTRY(MonFdset) next;
170};
171
Markus Armbruster74358f22015-03-06 19:35:59 +0100172typedef struct {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200173 QObject *id;
174 JSONMessageParser parser;
Markus Armbrusterf994b252015-03-06 19:51:51 +0100175 /*
176 * When a client connects, we're in capabilities negotiation mode.
177 * When command qmp_capabilities succeeds, we go into command
178 * mode.
179 */
180 bool in_command_mode; /* are we in command mode? */
Markus Armbruster74358f22015-03-06 19:35:59 +0100181} MonitorQMP;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200182
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100183/*
184 * To prevent flooding clients, events can be throttled. The
185 * throttling is calculated globally, rather than per-Monitor
186 * instance.
187 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200188typedef struct MonitorQAPIEventState {
Markus Armbruster7de0be62015-10-15 17:08:35 +0200189 QAPIEvent event; /* Throttling state for this event type and... */
190 QDict *data; /* ... data, see qapi_event_throttle_equal() */
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100191 QEMUTimer *timer; /* Timer for handling delayed events */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200192 QDict *qdict; /* Delayed event (if any) */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200193} MonitorQAPIEventState;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100194
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200195typedef struct {
196 int64_t rate; /* Minimum time (in ns) between two events */
197} MonitorQAPIEventConf;
198
aliguori87127162009-03-05 23:01:29 +0000199struct Monitor {
200 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200201 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000202 int flags;
203 int suspend_cnt;
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400204 bool skip_flush;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200205
206 QemuMutex out_lock;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400207 QString *outbuf;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200208 guint out_watch;
209
210 /* Read under either BQL or out_lock, written with BQL+out_lock. */
211 int mux_out;
212
aliguori731b0362009-03-05 23:01:42 +0000213 ReadLineState *rs;
Markus Armbruster74358f22015-03-06 19:35:59 +0100214 MonitorQMP qmp;
Andreas Färbercb446ec2013-05-01 14:24:52 +0200215 CPUState *mon_cpu;
Markus Armbruster097310b2014-10-07 13:59:15 +0200216 BlockCompletionFunc *password_completion_cb;
aliguori731b0362009-03-05 23:01:42 +0000217 void *password_opaque;
Wenchao Xia77172392013-08-27 20:38:20 +0800218 mon_cmd_t *cmd_table;
Anthony Liguoric227f092009-10-01 16:12:16 -0500219 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000220 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000221};
222
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300223/* QMP checker flags */
224#define QMP_ACCEPT_UNKNOWNS 1
225
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200226/* Protects mon_list, monitor_event_state. */
227static QemuMutex monitor_lock;
228
Blue Swirl72cf2d42009-09-12 07:36:22 +0000229static QLIST_HEAD(mon_list, Monitor) mon_list;
Corey Bryantba1c0482012-08-14 16:43:43 -0400230static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
Corey Bryantefb87c12012-08-14 16:43:48 -0400231static int mon_refcount;
bellard7e2515e2004-08-01 21:52:19 +0000232
Wayne Xia816f8922011-10-12 11:32:41 +0800233static mon_cmd_t mon_cmds[];
234static mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000235
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300236static const mon_cmd_t qmp_cmds[];
237
Markus Armbruster8631b602010-02-18 11:41:55 +0100238Monitor *cur_mon;
aliguori376253e2009-03-05 23:01:23 +0000239
Alberto Garciadc599972016-03-10 13:55:26 +0200240static QEMUClockType event_clock_type = QEMU_CLOCK_REALTIME;
241
Stefan Hajnoczic60bf332013-11-14 11:54:14 +0100242static void monitor_command_cb(void *opaque, const char *cmdline,
243 void *readline_opaque);
aliguori83ab7952008-08-19 14:44:22 +0000244
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100245/**
246 * Is @mon a QMP monitor?
247 */
248static inline bool monitor_is_qmp(const Monitor *mon)
Luiz Capitulino418173c2009-11-26 22:58:51 -0200249{
250 return (mon->flags & MONITOR_USE_CONTROL);
251}
252
Markus Armbruster489653b2015-03-06 20:01:05 +0100253/**
254 * Is the current monitor, if any, a QMP monitor?
255 */
256bool monitor_cur_is_qmp(void)
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100257{
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100258 return cur_mon && monitor_is_qmp(cur_mon);
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100259}
260
Anthony Liguori7060b472011-09-02 12:34:50 -0500261void monitor_read_command(Monitor *mon, int show_prompt)
aliguori731b0362009-03-05 23:01:42 +0000262{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200263 if (!mon->rs)
264 return;
265
aliguori731b0362009-03-05 23:01:42 +0000266 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
267 if (show_prompt)
268 readline_show_prompt(mon->rs);
269}
bellard6a00d602005-11-21 23:25:50 +0000270
Anthony Liguori7060b472011-09-02 12:34:50 -0500271int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
272 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000273{
Markus Armbrusterbcf5d192015-03-12 17:26:46 +0100274 if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000275 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
276 /* prompt is printed on return from the command handler */
277 return 0;
278 } else {
279 monitor_printf(mon, "terminal does not support password prompting\n");
280 return -ENOTTY;
281 }
aliguoribb5fc202009-03-05 23:01:15 +0000282}
283
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200284static void monitor_flush_locked(Monitor *mon);
285
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100286static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
287 void *opaque)
288{
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200289 Monitor *mon = opaque;
290
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200291 qemu_mutex_lock(&mon->out_lock);
292 mon->out_watch = 0;
293 monitor_flush_locked(mon);
294 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100295 return FALSE;
296}
297
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200298/* Called with mon->out_lock held. */
299static void monitor_flush_locked(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000300{
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100301 int rc;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400302 size_t len;
303 const char *buf;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100304
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400305 if (mon->skip_flush) {
306 return;
307 }
308
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400309 buf = qstring_get_str(mon->outbuf);
310 len = qstring_get_length(mon->outbuf);
311
Paolo Bonzinia4cc73d2013-05-31 14:00:27 +0200312 if (len && !mon->mux_out) {
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400313 rc = qemu_chr_fe_write(mon->chr, (const uint8_t *) buf, len);
Stratos Psomadakis056f49f2014-01-27 12:30:15 +0200314 if ((rc < 0 && errno != EAGAIN) || (rc == len)) {
315 /* all flushed or error */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400316 QDECREF(mon->outbuf);
317 mon->outbuf = qstring_new();
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100318 return;
319 }
320 if (rc > 0) {
321 /* partinal write */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400322 QString *tmp = qstring_from_str(buf + rc);
323 QDECREF(mon->outbuf);
324 mon->outbuf = tmp;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100325 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200326 if (mon->out_watch == 0) {
Roger Pau Monnee02bc6d2014-05-23 17:57:49 +0200327 mon->out_watch = qemu_chr_fe_add_watch(mon->chr, G_IO_OUT|G_IO_HUP,
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200328 monitor_unblocked, mon);
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200329 }
bellard7e2515e2004-08-01 21:52:19 +0000330 }
331}
332
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200333void monitor_flush(Monitor *mon)
334{
335 qemu_mutex_lock(&mon->out_lock);
336 monitor_flush_locked(mon);
337 qemu_mutex_unlock(&mon->out_lock);
338}
339
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400340/* flush at every end of line */
aliguori376253e2009-03-05 23:01:23 +0000341static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000342{
ths60fe76f2007-12-16 03:02:09 +0000343 char c;
aliguori731b0362009-03-05 23:01:42 +0000344
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200345 qemu_mutex_lock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000346 for(;;) {
347 c = *str++;
348 if (c == '\0')
349 break;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400350 if (c == '\n') {
351 qstring_append_chr(mon->outbuf, '\r');
352 }
353 qstring_append_chr(mon->outbuf, c);
354 if (c == '\n') {
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200355 monitor_flush_locked(mon);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400356 }
bellard7e2515e2004-08-01 21:52:19 +0000357 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200358 qemu_mutex_unlock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000359}
360
aliguori376253e2009-03-05 23:01:23 +0000361void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000362{
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400363 char *buf;
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200364
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200365 if (!mon)
366 return;
367
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100368 if (monitor_is_qmp(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200369 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200370 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200371
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400372 buf = g_strdup_vprintf(fmt, ap);
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200373 monitor_puts(mon, buf);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400374 g_free(buf);
bellard7e2515e2004-08-01 21:52:19 +0000375}
376
aliguori376253e2009-03-05 23:01:23 +0000377void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000378{
379 va_list ap;
380 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000381 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000382 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000383}
384
Pavel Butsykincaf15312015-09-22 16:18:17 +0300385int monitor_fprintf(FILE *stream, const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000386{
387 va_list ap;
388 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000389 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000390 va_end(ap);
391 return 0;
392}
393
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200394static void monitor_json_emitter(Monitor *mon, const QObject *data)
395{
396 QString *json;
397
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200398 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
399 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200400 assert(json != NULL);
401
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200402 qstring_append_chr(json, '\n');
403 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200404
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200405 QDECREF(json);
406}
407
Markus Armbruster710aec92015-03-06 11:28:00 +0100408static QDict *build_qmp_error_dict(Error *err)
Luiz Capitulinode253f12012-07-27 16:18:16 -0300409{
410 QObject *obj;
411
Markus Armbruster710aec92015-03-06 11:28:00 +0100412 obj = qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %s } }",
Eric Blakef22a28b2015-11-18 01:53:00 -0700413 QapiErrorClass_lookup[error_get_class(err)],
Markus Armbruster710aec92015-03-06 11:28:00 +0100414 error_get_pretty(err));
Luiz Capitulinode253f12012-07-27 16:18:16 -0300415
416 return qobject_to_qdict(obj);
417}
418
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100419static void monitor_protocol_emitter(Monitor *mon, QObject *data,
Markus Armbruster710aec92015-03-06 11:28:00 +0100420 Error *err)
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200421{
422 QDict *qmp;
423
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +0100424 trace_monitor_protocol_emitter(mon);
425
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100426 if (!err) {
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200427 /* success response */
Luiz Capitulinode253f12012-07-27 16:18:16 -0300428 qmp = qdict_new();
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200429 if (data) {
430 qobject_incref(data);
431 qdict_put_obj(qmp, "return", data);
432 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200433 /* return an empty QDict by default */
434 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200435 }
436 } else {
437 /* error response */
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100438 qmp = build_qmp_error_dict(err);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200439 }
440
Markus Armbruster74358f22015-03-06 19:35:59 +0100441 if (mon->qmp.id) {
442 qdict_put_obj(qmp, "id", mon->qmp.id);
443 mon->qmp.id = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200444 }
445
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200446 monitor_json_emitter(mon, QOBJECT(qmp));
447 QDECREF(qmp);
448}
449
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200450
Eric Blake7fb1cf12015-11-18 01:52:57 -0700451static MonitorQAPIEventConf monitor_qapi_event_conf[QAPI_EVENT__MAX] = {
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200452 /* Limit guest-triggerable events to 1 per second */
453 [QAPI_EVENT_RTC_CHANGE] = { 1000 * SCALE_MS },
454 [QAPI_EVENT_WATCHDOG] = { 1000 * SCALE_MS },
455 [QAPI_EVENT_BALLOON_CHANGE] = { 1000 * SCALE_MS },
456 [QAPI_EVENT_QUORUM_REPORT_BAD] = { 1000 * SCALE_MS },
457 [QAPI_EVENT_QUORUM_FAILURE] = { 1000 * SCALE_MS },
458 [QAPI_EVENT_VSERPORT_CHANGE] = { 1000 * SCALE_MS },
459};
460
Markus Armbrustera24712a2015-10-15 17:08:34 +0200461GHashTable *monitor_qapi_event_state;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100462
463/*
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200464 * Emits the event to every monitor instance, @event is only used for trace
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200465 * Called with monitor_lock held.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100466 */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200467static void monitor_qapi_event_emit(QAPIEvent event, QDict *qdict)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100468{
469 Monitor *mon;
470
Markus Armbruster688b4b72015-10-15 17:08:30 +0200471 trace_monitor_protocol_event_emit(event, qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100472 QLIST_FOREACH(mon, &mon_list, entry) {
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100473 if (monitor_is_qmp(mon) && mon->qmp.in_command_mode) {
Markus Armbruster688b4b72015-10-15 17:08:30 +0200474 monitor_json_emitter(mon, QOBJECT(qdict));
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100475 }
476 }
477}
478
Markus Armbrustera24712a2015-10-15 17:08:34 +0200479static void monitor_qapi_event_handler(void *opaque);
480
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100481/*
482 * Queue a new event for emission to Monitor instances,
483 * applying any rate limiting if required.
484 */
485static void
Markus Armbruster688b4b72015-10-15 17:08:30 +0200486monitor_qapi_event_queue(QAPIEvent event, QDict *qdict, Error **errp)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100487{
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200488 MonitorQAPIEventConf *evconf;
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200489 MonitorQAPIEventState *evstate;
Markus Armbruster93f8f982015-10-15 17:08:31 +0200490
Eric Blake7fb1cf12015-11-18 01:52:57 -0700491 assert(event < QAPI_EVENT__MAX);
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200492 evconf = &monitor_qapi_event_conf[event];
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200493 trace_monitor_protocol_event_queue(event, qdict, evconf->rate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100494
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200495 qemu_mutex_lock(&monitor_lock);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200496
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200497 if (!evconf->rate) {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200498 /* Unthrottled event */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200499 monitor_qapi_event_emit(event, qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100500 } else {
Markus Armbruster7de0be62015-10-15 17:08:35 +0200501 QDict *data = qobject_to_qdict(qdict_get(qdict, "data"));
502 MonitorQAPIEventState key = { .event = event, .data = data };
Markus Armbrustera24712a2015-10-15 17:08:34 +0200503
504 evstate = g_hash_table_lookup(monitor_qapi_event_state, &key);
505 assert(!evstate || timer_pending(evstate->timer));
506
507 if (evstate) {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200508 /*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200509 * Timer is pending for (at least) evconf->rate ns after
Markus Armbruster93f8f982015-10-15 17:08:31 +0200510 * last send. Store event for sending when timer fires,
511 * replacing a prior stored event if any.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100512 */
Markus Armbruster93f8f982015-10-15 17:08:31 +0200513 QDECREF(evstate->qdict);
Markus Armbruster688b4b72015-10-15 17:08:30 +0200514 evstate->qdict = qdict;
515 QINCREF(evstate->qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100516 } else {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200517 /*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200518 * Last send was (at least) evconf->rate ns ago.
Markus Armbruster93f8f982015-10-15 17:08:31 +0200519 * Send immediately, and arm the timer to call
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200520 * monitor_qapi_event_handler() in evconf->rate ns. Any
Markus Armbruster93f8f982015-10-15 17:08:31 +0200521 * events arriving before then will be delayed until then.
522 */
Alberto Garciadc599972016-03-10 13:55:26 +0200523 int64_t now = qemu_clock_get_ns(event_clock_type);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200524
Markus Armbruster688b4b72015-10-15 17:08:30 +0200525 monitor_qapi_event_emit(event, qdict);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200526
527 evstate = g_new(MonitorQAPIEventState, 1);
528 evstate->event = event;
Markus Armbruster7de0be62015-10-15 17:08:35 +0200529 evstate->data = data;
530 QINCREF(evstate->data);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200531 evstate->qdict = NULL;
Alberto Garciadc599972016-03-10 13:55:26 +0200532 evstate->timer = timer_new_ns(event_clock_type,
Markus Armbrustera24712a2015-10-15 17:08:34 +0200533 monitor_qapi_event_handler,
534 evstate);
535 g_hash_table_add(monitor_qapi_event_state, evstate);
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200536 timer_mod_ns(evstate->timer, now + evconf->rate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100537 }
538 }
Markus Armbruster93f8f982015-10-15 17:08:31 +0200539
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200540 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100541}
542
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100543/*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200544 * This function runs evconf->rate ns after sending a throttled
Markus Armbruster93f8f982015-10-15 17:08:31 +0200545 * event.
546 * If another event has since been stored, send it.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100547 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200548static void monitor_qapi_event_handler(void *opaque)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100549{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200550 MonitorQAPIEventState *evstate = opaque;
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200551 MonitorQAPIEventConf *evconf = &monitor_qapi_event_conf[evstate->event];
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100552
Markus Armbruster93f8f982015-10-15 17:08:31 +0200553 trace_monitor_protocol_event_handler(evstate->event, evstate->qdict);
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200554 qemu_mutex_lock(&monitor_lock);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200555
Markus Armbruster688b4b72015-10-15 17:08:30 +0200556 if (evstate->qdict) {
Alberto Garciadc599972016-03-10 13:55:26 +0200557 int64_t now = qemu_clock_get_ns(event_clock_type);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200558
Markus Armbruster688b4b72015-10-15 17:08:30 +0200559 monitor_qapi_event_emit(evstate->event, evstate->qdict);
560 QDECREF(evstate->qdict);
561 evstate->qdict = NULL;
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200562 timer_mod_ns(evstate->timer, now + evconf->rate);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200563 } else {
564 g_hash_table_remove(monitor_qapi_event_state, evstate);
Markus Armbruster7de0be62015-10-15 17:08:35 +0200565 QDECREF(evstate->data);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200566 timer_free(evstate->timer);
567 g_free(evstate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100568 }
Markus Armbruster93f8f982015-10-15 17:08:31 +0200569
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200570 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100571}
572
Markus Armbrustera24712a2015-10-15 17:08:34 +0200573static unsigned int qapi_event_throttle_hash(const void *key)
574{
575 const MonitorQAPIEventState *evstate = key;
Markus Armbruster7de0be62015-10-15 17:08:35 +0200576 unsigned int hash = evstate->event * 255;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200577
Markus Armbruster7de0be62015-10-15 17:08:35 +0200578 if (evstate->event == QAPI_EVENT_VSERPORT_CHANGE) {
579 hash += g_str_hash(qdict_get_str(evstate->data, "id"));
580 }
581
Alberto Garcia6d425eb2016-03-10 13:55:25 +0200582 if (evstate->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
583 hash += g_str_hash(qdict_get_str(evstate->data, "node-name"));
584 }
585
Markus Armbruster7de0be62015-10-15 17:08:35 +0200586 return hash;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200587}
588
589static gboolean qapi_event_throttle_equal(const void *a, const void *b)
590{
591 const MonitorQAPIEventState *eva = a;
592 const MonitorQAPIEventState *evb = b;
593
Markus Armbruster7de0be62015-10-15 17:08:35 +0200594 if (eva->event != evb->event) {
595 return FALSE;
596 }
597
598 if (eva->event == QAPI_EVENT_VSERPORT_CHANGE) {
599 return !strcmp(qdict_get_str(eva->data, "id"),
600 qdict_get_str(evb->data, "id"));
601 }
602
Alberto Garcia6d425eb2016-03-10 13:55:25 +0200603 if (eva->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
604 return !strcmp(qdict_get_str(eva->data, "node-name"),
605 qdict_get_str(evb->data, "node-name"));
606 }
607
Markus Armbruster7de0be62015-10-15 17:08:35 +0200608 return TRUE;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200609}
610
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200611static void monitor_qapi_event_init(void)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100612{
Alberto Garciadc599972016-03-10 13:55:26 +0200613 if (qtest_enabled()) {
614 event_clock_type = QEMU_CLOCK_VIRTUAL;
615 }
616
Markus Armbrustera24712a2015-10-15 17:08:34 +0200617 monitor_qapi_event_state = g_hash_table_new(qapi_event_throttle_hash,
618 qapi_event_throttle_equal);
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200619 qmp_event_set_func_emit(monitor_qapi_event_queue);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200620}
621
Markus Armbruster485febc2015-03-13 17:25:50 +0100622static void qmp_capabilities(QDict *params, QObject **ret_data, Error **errp)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200623{
Markus Armbruster485febc2015-03-13 17:25:50 +0100624 cur_mon->qmp.in_command_mode = true;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200625}
626
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100627static void handle_hmp_command(Monitor *mon, const char *cmdline);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200628
Wenchao Xiab01fe892013-08-27 20:38:19 +0800629static void monitor_data_init(Monitor *mon)
630{
631 memset(mon, 0, sizeof(Monitor));
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200632 qemu_mutex_init(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800633 mon->outbuf = qstring_new();
Wenchao Xia77172392013-08-27 20:38:20 +0800634 /* Use *mon_cmds by default. */
635 mon->cmd_table = mon_cmds;
Wenchao Xiab01fe892013-08-27 20:38:19 +0800636}
637
638static void monitor_data_destroy(Monitor *mon)
639{
640 QDECREF(mon->outbuf);
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200641 qemu_mutex_destroy(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800642}
643
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200644char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
645 int64_t cpu_index, Error **errp)
Luiz Capitulino0268d972010-10-22 10:08:02 -0200646{
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200647 char *output = NULL;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200648 Monitor *old_mon, hmp;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200649
Wenchao Xiab01fe892013-08-27 20:38:19 +0800650 monitor_data_init(&hmp);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400651 hmp.skip_flush = true;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200652
653 old_mon = cur_mon;
654 cur_mon = &hmp;
655
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200656 if (has_cpu_index) {
657 int ret = monitor_set_cpu(cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200658 if (ret < 0) {
659 cur_mon = old_mon;
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100660 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
661 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200662 goto out;
663 }
664 }
665
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100666 handle_hmp_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200667 cur_mon = old_mon;
668
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200669 qemu_mutex_lock(&hmp.out_lock);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400670 if (qstring_get_length(hmp.outbuf) > 0) {
671 output = g_strdup(qstring_get_str(hmp.outbuf));
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200672 } else {
673 output = g_strdup("");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200674 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200675 qemu_mutex_unlock(&hmp.out_lock);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200676
677out:
Wenchao Xiab01fe892013-08-27 20:38:19 +0800678 monitor_data_destroy(&hmp);
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200679 return output;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200680}
681
bellard9dc39cb2004-03-14 21:38:27 +0000682static int compare_cmd(const char *name, const char *list)
683{
684 const char *p, *pstart;
685 int len;
686 len = strlen(name);
687 p = list;
688 for(;;) {
689 pstart = p;
690 p = strchr(p, '|');
691 if (!p)
692 p = pstart + strlen(pstart);
693 if ((p - pstart) == len && !memcmp(pstart, name, len))
694 return 1;
695 if (*p == '\0')
696 break;
697 p++;
698 }
699 return 0;
700}
701
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800702static int get_str(char *buf, int buf_size, const char **pp)
703{
704 const char *p;
705 char *q;
706 int c;
707
708 q = buf;
709 p = *pp;
710 while (qemu_isspace(*p)) {
711 p++;
712 }
713 if (*p == '\0') {
714 fail:
715 *q = '\0';
716 *pp = p;
717 return -1;
718 }
719 if (*p == '\"') {
720 p++;
721 while (*p != '\0' && *p != '\"') {
722 if (*p == '\\') {
723 p++;
724 c = *p++;
725 switch (c) {
726 case 'n':
727 c = '\n';
728 break;
729 case 'r':
730 c = '\r';
731 break;
732 case '\\':
733 case '\'':
734 case '\"':
735 break;
736 default:
Peter Maydell71baf782015-08-19 16:20:19 +0100737 printf("unsupported escape code: '\\%c'\n", c);
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800738 goto fail;
739 }
740 if ((q - buf) < buf_size - 1) {
741 *q++ = c;
742 }
743 } else {
744 if ((q - buf) < buf_size - 1) {
745 *q++ = *p;
746 }
747 p++;
748 }
749 }
750 if (*p != '\"') {
Peter Maydell71baf782015-08-19 16:20:19 +0100751 printf("unterminated string\n");
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800752 goto fail;
753 }
754 p++;
755 } else {
756 while (*p != '\0' && !qemu_isspace(*p)) {
757 if ((q - buf) < buf_size - 1) {
758 *q++ = *p;
759 }
760 p++;
761 }
762 }
763 *q = '\0';
764 *pp = p;
765 return 0;
766}
767
768#define MAX_ARGS 16
769
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800770static void free_cmdline_args(char **args, int nb_args)
771{
772 int i;
773
774 assert(nb_args <= MAX_ARGS);
775
776 for (i = 0; i < nb_args; i++) {
777 g_free(args[i]);
778 }
779
780}
781
782/*
783 * Parse the command line to get valid args.
784 * @cmdline: command line to be parsed.
785 * @pnb_args: location to store the number of args, must NOT be NULL.
786 * @args: location to store the args, which should be freed by caller, must
787 * NOT be NULL.
788 *
789 * Returns 0 on success, negative on failure.
790 *
791 * NOTE: this parser is an approximate form of the real command parser. Number
792 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
793 * return with failure.
794 */
795static int parse_cmdline(const char *cmdline,
796 int *pnb_args, char **args)
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800797{
798 const char *p;
799 int nb_args, ret;
800 char buf[1024];
801
802 p = cmdline;
803 nb_args = 0;
804 for (;;) {
805 while (qemu_isspace(*p)) {
806 p++;
807 }
808 if (*p == '\0') {
809 break;
810 }
811 if (nb_args >= MAX_ARGS) {
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800812 goto fail;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800813 }
814 ret = get_str(buf, sizeof(buf), &p);
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800815 if (ret < 0) {
816 goto fail;
817 }
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800818 args[nb_args] = g_strdup(buf);
819 nb_args++;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800820 }
821 *pnb_args = nb_args;
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800822 return 0;
823
824 fail:
825 free_cmdline_args(args, nb_args);
826 return -1;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800827}
828
Wenchao Xia66855492013-08-27 20:38:23 +0800829static void help_cmd_dump_one(Monitor *mon,
830 const mon_cmd_t *cmd,
831 char **prefix_args,
832 int prefix_args_nb)
833{
834 int i;
835
836 for (i = 0; i < prefix_args_nb; i++) {
837 monitor_printf(mon, "%s ", prefix_args[i]);
838 }
839 monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
840}
841
842/* @args[@arg_index] is the valid command need to find in @cmds */
Anthony Liguoric227f092009-10-01 16:12:16 -0500843static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
Wenchao Xia66855492013-08-27 20:38:23 +0800844 char **args, int nb_args, int arg_index)
bellard9dc39cb2004-03-14 21:38:27 +0000845{
Anthony Liguoric227f092009-10-01 16:12:16 -0500846 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000847
Wenchao Xia66855492013-08-27 20:38:23 +0800848 /* No valid arg need to compare with, dump all in *cmds */
849 if (arg_index >= nb_args) {
850 for (cmd = cmds; cmd->name != NULL; cmd++) {
851 help_cmd_dump_one(mon, cmd, args, arg_index);
852 }
853 return;
854 }
855
856 /* Find one entry to dump */
857 for (cmd = cmds; cmd->name != NULL; cmd++) {
858 if (compare_cmd(args[arg_index], cmd->name)) {
859 if (cmd->sub_table) {
860 /* continue with next arg */
861 help_cmd_dump(mon, cmd->sub_table,
862 args, nb_args, arg_index + 1);
863 } else {
864 help_cmd_dump_one(mon, cmd, args, arg_index);
865 }
866 break;
867 }
bellard9dc39cb2004-03-14 21:38:27 +0000868 }
869}
870
aliguori376253e2009-03-05 23:01:23 +0000871static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000872{
Wenchao Xia66855492013-08-27 20:38:23 +0800873 char *args[MAX_ARGS];
874 int nb_args = 0;
875
876 /* 1. parse user input */
877 if (name) {
878 /* special case for log, directly dump and return */
879 if (!strcmp(name, "log")) {
Peter Maydell38dad9e2013-02-11 16:41:25 +0000880 const QEMULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000881 monitor_printf(mon, "Log items (comma separated):\n");
882 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
Peter Maydell38dad9e2013-02-11 16:41:25 +0000883 for (item = qemu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000884 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000885 }
Wenchao Xia66855492013-08-27 20:38:23 +0800886 return;
887 }
888
889 if (parse_cmdline(name, &nb_args, args) < 0) {
890 return;
bellardf193c792004-03-21 17:06:25 +0000891 }
bellard9dc39cb2004-03-14 21:38:27 +0000892 }
Wenchao Xia66855492013-08-27 20:38:23 +0800893
894 /* 2. dump the contents according to parsed args */
895 help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0);
896
897 free_cmdline_args(args, nb_args);
bellard9dc39cb2004-03-14 21:38:27 +0000898}
899
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300900static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300901{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300902 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300903}
904
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100905static void hmp_trace_event(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530906{
907 const char *tp_name = qdict_get_str(qdict, "name");
908 bool new_state = qdict_get_bool(qdict, "option");
Lluís Vilanova14101d02014-08-25 13:20:03 +0200909 Error *local_err = NULL;
Blue Swirlf871d682010-10-13 19:14:29 +0000910
Lluís Vilanova14101d02014-08-25 13:20:03 +0200911 qmp_trace_event_set_state(tp_name, new_state, true, true, &local_err);
912 if (local_err) {
Markus Armbruster091e38b2015-02-10 15:15:43 +0100913 error_report_err(local_err);
Blue Swirlf871d682010-10-13 19:14:29 +0000914 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530915}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100916
Michael Rothc45a8162011-10-02 08:44:37 -0500917#ifdef CONFIG_TRACE_SIMPLE
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100918static void hmp_trace_file(Monitor *mon, const QDict *qdict)
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100919{
920 const char *op = qdict_get_try_str(qdict, "op");
921 const char *arg = qdict_get_try_str(qdict, "arg");
922
923 if (!op) {
924 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
925 } else if (!strcmp(op, "on")) {
926 st_set_trace_file_enabled(true);
927 } else if (!strcmp(op, "off")) {
928 st_set_trace_file_enabled(false);
929 } else if (!strcmp(op, "flush")) {
930 st_flush_trace_buffer();
931 } else if (!strcmp(op, "set")) {
932 if (arg) {
933 st_set_trace_file(arg);
934 }
935 } else {
936 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
937 help_cmd(mon, "trace-file");
938 }
939}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530940#endif
941
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100942static void hmp_info_help(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000943{
Luiz Capitulino13c74252009-10-07 13:41:55 -0300944 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000945}
946
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300947CommandInfoList *qmp_query_commands(Error **errp)
bellard9bc9d1c2004-10-10 15:15:51 +0000948{
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300949 CommandInfoList *info, *cmd_list = NULL;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200950 const mon_cmd_t *cmd;
951
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300952 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino40e5a012011-10-21 16:15:31 -0200953 info = g_malloc0(sizeof(*info));
954 info->value = g_malloc0(sizeof(*info->value));
955 info->value->name = g_strdup(cmd->name);
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200956
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300957 info->next = cmd_list;
958 cmd_list = info;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200959 }
960
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300961 return cmd_list;
thsa36e69d2007-12-02 05:18:19 +0000962}
963
Daniel P. Berrange48608532012-05-21 17:59:51 +0100964EventInfoList *qmp_query_events(Error **errp)
965{
966 EventInfoList *info, *ev_list = NULL;
Wenchao Xia75175172014-06-18 08:43:54 +0200967 QAPIEvent e;
Daniel P. Berrange48608532012-05-21 17:59:51 +0100968
Eric Blake7fb1cf12015-11-18 01:52:57 -0700969 for (e = 0 ; e < QAPI_EVENT__MAX ; e++) {
Wenchao Xia75175172014-06-18 08:43:54 +0200970 const char *event_name = QAPIEvent_lookup[e];
Daniel P. Berrange48608532012-05-21 17:59:51 +0100971 assert(event_name != NULL);
972 info = g_malloc0(sizeof(*info));
973 info->value = g_malloc0(sizeof(*info->value));
974 info->value->name = g_strdup(event_name);
975
976 info->next = ev_list;
977 ev_list = info;
978 }
979
980 return ev_list;
981}
982
Markus Armbruster39a18152015-09-16 13:06:28 +0200983/*
984 * Minor hack: generated marshalling suppressed for this command
985 * ('gen': false in the schema) so we can parse the JSON string
986 * directly into QObject instead of first parsing it with
987 * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
988 * to QObject with generated output marshallers, every time. Instead,
989 * we do it in test-qmp-input-visitor.c, just to make sure
990 * qapi-introspect.py's output actually conforms to the schema.
991 */
992static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
993 Error **errp)
994{
995 *ret_data = qobject_from_json(qmp_schema_json);
996}
997
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300998/* set the current CPU defined by the user */
999int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +00001000{
Andreas Färber55e5c282012-12-17 06:18:02 +01001001 CPUState *cpu;
bellard6a00d602005-11-21 23:25:50 +00001002
Andreas Färber1c8bb3c2013-02-15 17:01:09 +01001003 cpu = qemu_get_cpu(cpu_index);
1004 if (cpu == NULL) {
1005 return -1;
bellard6a00d602005-11-21 23:25:50 +00001006 }
Andreas Färbercb446ec2013-05-01 14:24:52 +02001007 cur_mon->mon_cpu = cpu;
Andreas Färber1c8bb3c2013-02-15 17:01:09 +01001008 return 0;
bellard6a00d602005-11-21 23:25:50 +00001009}
1010
Pavel Butsykincaf15312015-09-22 16:18:17 +03001011CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +00001012{
aliguori731b0362009-03-05 23:01:42 +00001013 if (!cur_mon->mon_cpu) {
Luiz Capitulinob025c8b2011-10-06 14:02:57 -03001014 monitor_set_cpu(0);
bellard6a00d602005-11-21 23:25:50 +00001015 }
Avi Kivity4c0960c2009-08-17 23:19:53 +03001016 cpu_synchronize_state(cur_mon->mon_cpu);
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001017 return cur_mon->mon_cpu;
1018}
1019
Pavel Butsykinbf957282015-09-10 18:38:59 +03001020CPUArchState *mon_get_cpu_env(void)
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001021{
1022 return mon_get_cpu()->env_ptr;
bellard6a00d602005-11-21 23:25:50 +00001023}
1024
Luiz Capitulino99b77962011-10-24 10:53:44 -02001025int monitor_get_cpu_index(void)
1026{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001027 return mon_get_cpu()->cpu_index;
Luiz Capitulino99b77962011-10-24 10:53:44 -02001028}
1029
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001030static void hmp_info_registers(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001031{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001032 cpu_dump_state(mon_get_cpu(), (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +00001033}
1034
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001035static void hmp_info_jit(Monitor *mon, const QDict *qdict)
bellarde3db7222005-01-26 22:00:47 +00001036{
aliguori376253e2009-03-05 23:01:23 +00001037 dump_exec_info((FILE *)mon, monitor_fprintf);
Sebastian Tanase27498be2014-07-25 11:56:33 +02001038 dump_drift_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +00001039}
1040
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001041static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
Max Filippov246ae242014-11-02 11:04:18 +03001042{
1043 dump_opcount_info((FILE *)mon, monitor_fprintf);
1044}
1045
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001046static void hmp_info_history(Monitor *mon, const QDict *qdict)
bellardaa455482004-04-04 13:07:25 +00001047{
1048 int i;
bellard7e2515e2004-08-01 21:52:19 +00001049 const char *str;
ths3b46e622007-09-17 08:09:54 +00001050
aliguoricde76ee2009-03-05 23:01:51 +00001051 if (!mon->rs)
1052 return;
bellard7e2515e2004-08-01 21:52:19 +00001053 i = 0;
1054 for(;;) {
aliguori731b0362009-03-05 23:01:42 +00001055 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +00001056 if (!str)
1057 break;
aliguori376253e2009-03-05 23:01:23 +00001058 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +00001059 i++;
bellardaa455482004-04-04 13:07:25 +00001060 }
1061}
1062
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001063static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
j_mayer76a66252007-03-07 08:32:30 +00001064{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001065 cpu_dump_statistics(mon_get_cpu(), (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +00001066}
j_mayer76a66252007-03-07 08:32:30 +00001067
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001068static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301069{
Lluís Vilanova14101d02014-08-25 13:20:03 +02001070 TraceEventInfoList *events = qmp_trace_event_get_state("*", NULL);
1071 TraceEventInfoList *elem;
1072
1073 for (elem = events; elem != NULL; elem = elem->next) {
1074 monitor_printf(mon, "%s : state %u\n",
1075 elem->value->name,
1076 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
1077 }
1078 qapi_free_TraceEventInfoList(events);
Prerna Saxena22890ab2010-06-24 17:04:53 +05301079}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301080
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001081void qmp_client_migrate_info(const char *protocol, const char *hostname,
1082 bool has_port, int64_t port,
1083 bool has_tls_port, int64_t tls_port,
1084 bool has_cert_subject, const char *cert_subject,
1085 Error **errp)
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001086{
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001087 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001088 if (!qemu_using_spice(errp)) {
1089 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001090 }
1091
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001092 if (!has_port && !has_tls_port) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001093 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001094 return;
Yonit Halperin6ec5dae2012-03-18 09:42:39 +02001095 }
1096
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001097 if (qemu_spice_migrate_info(hostname,
1098 has_port ? port : -1,
1099 has_tls_port ? tls_port : -1,
1100 cert_subject)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001101 error_setg(errp, QERR_UNDEFINED_ERROR);
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001102 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001103 }
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001104 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001105 }
1106
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001107 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001108}
1109
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001110static void hmp_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001111{
Peter Maydell9a7e5422013-02-11 16:41:20 +00001112 qemu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001113}
1114
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001115static void hmp_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001116{
1117 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001118 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001119
bellard9307c4c2004-04-04 12:57:25 +00001120 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001121 mask = 0;
1122 } else {
Peter Maydell4fde1eb2013-02-11 16:41:22 +00001123 mask = qemu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001124 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001125 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001126 return;
1127 }
1128 }
Peter Maydell24537a02013-02-11 16:41:23 +00001129 qemu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00001130}
1131
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001132static void hmp_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001133{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001134 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001135 if (!option || !strcmp(option, "on")) {
1136 singlestep = 1;
1137 } else if (!strcmp(option, "off")) {
1138 singlestep = 0;
1139 } else {
1140 monitor_printf(mon, "unexpected option %s\n", option);
1141 }
1142}
1143
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001144static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001145{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001146 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001147 if (!device)
1148 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1149 if (gdbserver_start(device) < 0) {
1150 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1151 device);
1152 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001153 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001154 } else {
aliguori59030a82009-04-05 18:43:41 +00001155 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1156 device);
bellard8a7ddc32004-03-31 19:00:16 +00001157 }
1158}
1159
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001160static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001161{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001162 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001163 if (select_watchdog_action(action) == -1) {
1164 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1165 }
1166}
1167
aliguori376253e2009-03-05 23:01:23 +00001168static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001169{
aliguori376253e2009-03-05 23:01:23 +00001170 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001171 switch(c) {
1172 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001173 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001174 break;
1175 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001176 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001177 break;
1178 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001179 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001180 break;
1181 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001182 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001183 break;
1184 default:
1185 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001186 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001187 } else {
aliguori376253e2009-03-05 23:01:23 +00001188 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001189 }
1190 break;
1191 }
aliguori376253e2009-03-05 23:01:23 +00001192 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001193}
1194
aliguori376253e2009-03-05 23:01:23 +00001195static void memory_dump(Monitor *mon, int count, int format, int wsize,
Avi Kivitya8170e52012-10-23 12:30:10 +02001196 hwaddr addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001197{
Blue Swirl23842aa2010-01-12 20:27:43 +00001198 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001199 uint8_t buf[16];
1200 uint64_t v;
1201
1202 if (format == 'i') {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001203 int flags = 0;
bellard9307c4c2004-04-04 12:57:25 +00001204#ifdef TARGET_I386
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001205 CPUArchState *env = mon_get_cpu_env();
bellard4c27ba22004-04-25 18:05:08 +00001206 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001207 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001208 } else if (wsize == 4) {
1209 flags = 0;
1210 } else {
bellard6a15fd12006-04-12 21:07:07 +00001211 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001212 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001213 if (env) {
1214#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001215 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001216 (env->segs[R_CS].flags & DESC_L_MASK))
1217 flags = 2;
1218 else
1219#endif
1220 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1221 flags = 1;
1222 }
bellard4c27ba22004-04-25 18:05:08 +00001223 }
1224#endif
Tom Musta1c38f842014-04-09 14:53:24 -05001225#ifdef TARGET_PPC
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001226 CPUArchState *env = mon_get_cpu_env();
Tom Musta1c38f842014-04-09 14:53:24 -05001227 flags = msr_le << 16;
1228 flags |= env->bfd_mach;
1229#endif
Peter Crosthwaited49190c2015-05-24 14:20:41 -07001230 monitor_disas(mon, mon_get_cpu(), addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001231 return;
1232 }
1233
1234 len = wsize * count;
1235 if (wsize == 1)
1236 line_size = 8;
1237 else
1238 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001239 max_digits = 0;
1240
1241 switch(format) {
1242 case 'o':
1243 max_digits = (wsize * 8 + 2) / 3;
1244 break;
1245 default:
1246 case 'x':
1247 max_digits = (wsize * 8) / 4;
1248 break;
1249 case 'u':
1250 case 'd':
1251 max_digits = (wsize * 8 * 10 + 32) / 33;
1252 break;
1253 case 'c':
1254 wsize = 1;
1255 break;
1256 }
1257
1258 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001259 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001260 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001261 else
aliguori376253e2009-03-05 23:01:23 +00001262 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001263 l = len;
1264 if (l > line_size)
1265 l = line_size;
1266 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001267 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001268 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001269 if (cpu_memory_rw_debug(mon_get_cpu(), addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001270 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001271 break;
1272 }
bellard9307c4c2004-04-04 12:57:25 +00001273 }
ths5fafdf22007-09-16 21:08:06 +00001274 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001275 while (i < l) {
1276 switch(wsize) {
1277 default:
1278 case 1:
Peter Maydell24e60302015-01-20 15:19:32 +00001279 v = ldub_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001280 break;
1281 case 2:
Peter Maydell24e60302015-01-20 15:19:32 +00001282 v = lduw_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001283 break;
1284 case 4:
Peter Maydell24e60302015-01-20 15:19:32 +00001285 v = (uint32_t)ldl_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001286 break;
1287 case 8:
Peter Maydell24e60302015-01-20 15:19:32 +00001288 v = ldq_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001289 break;
1290 }
aliguori376253e2009-03-05 23:01:23 +00001291 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001292 switch(format) {
1293 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001294 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001295 break;
1296 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001297 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001298 break;
1299 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001300 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001301 break;
1302 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001303 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001304 break;
1305 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001306 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001307 break;
1308 }
1309 i += wsize;
1310 }
aliguori376253e2009-03-05 23:01:23 +00001311 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001312 addr += l;
1313 len -= l;
1314 }
1315}
1316
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001317static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001318{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001319 int count = qdict_get_int(qdict, "count");
1320 int format = qdict_get_int(qdict, "format");
1321 int size = qdict_get_int(qdict, "size");
1322 target_long addr = qdict_get_int(qdict, "addr");
1323
aliguori376253e2009-03-05 23:01:23 +00001324 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001325}
1326
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001327static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001328{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001329 int count = qdict_get_int(qdict, "count");
1330 int format = qdict_get_int(qdict, "format");
1331 int size = qdict_get_int(qdict, "size");
Avi Kivitya8170e52012-10-23 12:30:10 +02001332 hwaddr addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001333
aliguori376253e2009-03-05 23:01:23 +00001334 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001335}
1336
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001337static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001338{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001339 int format = qdict_get_int(qdict, "format");
Avi Kivitya8170e52012-10-23 12:30:10 +02001340 hwaddr val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001341
bellard9307c4c2004-04-04 12:57:25 +00001342 switch(format) {
1343 case 'o':
Avi Kivitya8170e52012-10-23 12:30:10 +02001344 monitor_printf(mon, "%#" HWADDR_PRIo, val);
bellard9307c4c2004-04-04 12:57:25 +00001345 break;
1346 case 'x':
Avi Kivitya8170e52012-10-23 12:30:10 +02001347 monitor_printf(mon, "%#" HWADDR_PRIx, val);
bellard9307c4c2004-04-04 12:57:25 +00001348 break;
1349 case 'u':
Avi Kivitya8170e52012-10-23 12:30:10 +02001350 monitor_printf(mon, "%" HWADDR_PRIu, val);
bellard9307c4c2004-04-04 12:57:25 +00001351 break;
1352 default:
1353 case 'd':
Avi Kivitya8170e52012-10-23 12:30:10 +02001354 monitor_printf(mon, "%" HWADDR_PRId, val);
bellard9307c4c2004-04-04 12:57:25 +00001355 break;
1356 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001357 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001358 break;
1359 }
aliguori376253e2009-03-05 23:01:23 +00001360 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001361}
1362
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001363static void hmp_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001364{
1365 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001366 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001367 uint32_t start = qdict_get_int(qdict, "start");
1368 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001369
1370 sum = 0;
1371 for(addr = start; addr < (start + size); addr++) {
Peter Maydell42874d32015-04-26 16:49:24 +01001372 uint8_t val = address_space_ldub(&address_space_memory, addr,
1373 MEMTXATTRS_UNSPECIFIED, NULL);
bellarde4cf1ad2005-06-04 20:15:57 +00001374 /* BSD sum algorithm ('sum' Unix command) */
1375 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001376 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001377 }
aliguori376253e2009-03-05 23:01:23 +00001378 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001379}
1380
bellard13224a82006-07-14 22:03:35 +00001381static int mouse_button_state;
1382
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001383static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001384{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001385 int dx, dy, dz, button;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001386 const char *dx_str = qdict_get_str(qdict, "dx_str");
1387 const char *dy_str = qdict_get_str(qdict, "dy_str");
1388 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001389
bellard13224a82006-07-14 22:03:35 +00001390 dx = strtol(dx_str, NULL, 0);
1391 dy = strtol(dy_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001392 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
1393 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
1394
1395 if (dz_str) {
bellard13224a82006-07-14 22:03:35 +00001396 dz = strtol(dz_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001397 if (dz != 0) {
Gerd Hoffmannf22d0af2016-01-12 12:14:12 +01001398 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001399 qemu_input_queue_btn(NULL, button, true);
1400 qemu_input_event_sync();
1401 qemu_input_queue_btn(NULL, button, false);
1402 }
1403 }
1404 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001405}
1406
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001407static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001408{
Eric Blake7fb1cf12015-11-18 01:52:57 -07001409 static uint32_t bmap[INPUT_BUTTON__MAX] = {
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001410 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1411 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
1412 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1413 };
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001414 int button_state = qdict_get_int(qdict, "button_state");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001415
1416 if (mouse_button_state == button_state) {
1417 return;
1418 }
1419 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1420 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001421 mouse_button_state = button_state;
bellard13224a82006-07-14 22:03:35 +00001422}
1423
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001424static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001425{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001426 int size = qdict_get_int(qdict, "size");
1427 int addr = qdict_get_int(qdict, "addr");
1428 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001429 uint32_t val;
1430 int suffix;
1431
1432 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001433 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001434 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001435 addr++;
1436 }
1437 addr &= 0xffff;
1438
1439 switch(size) {
1440 default:
1441 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001442 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001443 suffix = 'b';
1444 break;
1445 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001446 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001447 suffix = 'w';
1448 break;
1449 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001450 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001451 suffix = 'l';
1452 break;
1453 }
aliguori376253e2009-03-05 23:01:23 +00001454 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1455 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001456}
bellarda3a91a32004-06-04 11:06:21 +00001457
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001458static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001459{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001460 int size = qdict_get_int(qdict, "size");
1461 int addr = qdict_get_int(qdict, "addr");
1462 int val = qdict_get_int(qdict, "val");
1463
Jan Kiszkaf1147842009-07-14 10:20:11 +02001464 addr &= IOPORTS_MASK;
1465
1466 switch (size) {
1467 default:
1468 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001469 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001470 break;
1471 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001472 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001473 break;
1474 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001475 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001476 break;
1477 }
1478}
1479
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001480static void hmp_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001481{
Gongleif1839932014-12-03 18:20:58 +00001482 Error *local_err = NULL;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001483 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001484
Gongleif1839932014-12-03 18:20:58 +00001485 qemu_boot_set(bootdevice, &local_err);
1486 if (local_err) {
Markus Armbruster193227f2015-12-18 16:35:06 +01001487 error_report_err(local_err);
aurel320ecdffb2008-05-04 20:11:34 +00001488 } else {
Gongleif1839932014-12-03 18:20:58 +00001489 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001490 }
1491}
1492
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001493static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
Blue Swirl314e2982011-09-11 20:22:05 +00001494{
1495 mtree_info((fprintf_function)monitor_printf, mon);
1496}
1497
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001498static void hmp_info_numa(Monitor *mon, const QDict *qdict)
aliguori030ea372009-04-21 22:30:47 +00001499{
aliguorib28b6232009-04-22 20:20:29 +00001500 int i;
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001501 CPUState *cpu;
zhanghailiang5b009e42014-11-04 19:49:30 +08001502 uint64_t *node_mem;
aliguori030ea372009-04-21 22:30:47 +00001503
zhanghailiang5b009e42014-11-04 19:49:30 +08001504 node_mem = g_new0(uint64_t, nb_numa_nodes);
1505 query_numa_node_mem(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001506 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1507 for (i = 0; i < nb_numa_nodes; i++) {
1508 monitor_printf(mon, "node %d cpus:", i);
Andreas Färberbdc44642013-06-24 23:50:24 +02001509 CPU_FOREACH(cpu) {
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001510 if (cpu->numa_node == i) {
Andreas Färber55e5c282012-12-17 06:18:02 +01001511 monitor_printf(mon, " %d", cpu->cpu_index);
aliguori030ea372009-04-21 22:30:47 +00001512 }
1513 }
1514 monitor_printf(mon, "\n");
1515 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
zhanghailiang5b009e42014-11-04 19:49:30 +08001516 node_mem[i] >> 20);
aliguori030ea372009-04-21 22:30:47 +00001517 }
zhanghailiang5b009e42014-11-04 19:49:30 +08001518 g_free(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001519}
1520
bellard5f1ce942006-02-08 22:40:15 +00001521#ifdef CONFIG_PROFILER
1522
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001523int64_t tcg_time;
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001524int64_t dev_time;
1525
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001526static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001527{
aliguori376253e2009-03-05 23:01:23 +00001528 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Rutuja Shah73bcb242016-03-21 21:32:30 +05301529 dev_time, dev_time / (double)NANOSECONDS_PER_SECOND);
aliguori376253e2009-03-05 23:01:23 +00001530 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Rutuja Shah73bcb242016-03-21 21:32:30 +05301531 tcg_time, tcg_time / (double)NANOSECONDS_PER_SECOND);
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001532 tcg_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001533 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001534}
1535#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001536static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001537{
aliguori376253e2009-03-05 23:01:23 +00001538 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001539}
1540#endif
1541
bellardec36b692006-07-16 18:57:03 +00001542/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001543static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001544
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001545static void hmp_info_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001546{
1547 int i;
1548 CaptureState *s;
1549
1550 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001551 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001552 s->ops.info (s->opaque);
1553 }
1554}
1555
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001556static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001557{
1558 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001559 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001560 CaptureState *s;
1561
1562 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1563 if (i == n) {
1564 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001565 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001566 g_free (s);
bellardec36b692006-07-16 18:57:03 +00001567 return;
1568 }
1569 }
1570}
1571
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001572static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001573{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001574 const char *path = qdict_get_str(qdict, "path");
1575 int has_freq = qdict_haskey(qdict, "freq");
1576 int freq = qdict_get_try_int(qdict, "freq", -1);
1577 int has_bits = qdict_haskey(qdict, "bits");
1578 int bits = qdict_get_try_int(qdict, "bits", -1);
1579 int has_channels = qdict_haskey(qdict, "nchannels");
1580 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001581 CaptureState *s;
1582
Anthony Liguori7267c092011-08-20 22:09:37 -05001583 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001584
1585 freq = has_freq ? freq : 44100;
1586 bits = has_bits ? bits : 16;
1587 nchannels = has_channels ? nchannels : 2;
1588
1589 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001590 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05001591 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001592 return;
bellardec36b692006-07-16 18:57:03 +00001593 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001594 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001595}
bellardec36b692006-07-16 18:57:03 +00001596
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001597static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001598{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001599 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001600
aliguori76655d62009-03-06 20:27:37 +00001601 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001602 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001603 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001604 return acl;
1605}
aliguori76655d62009-03-06 20:27:37 +00001606
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001607static void hmp_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001608{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001609 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001610 qemu_acl *acl = find_acl(mon, aclname);
1611 qemu_acl_entry *entry;
1612 int i = 0;
1613
1614 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001615 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001616 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00001617 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00001618 i++;
1619 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001620 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001621 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001622 }
1623}
1624
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001625static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001626{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001627 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001628 qemu_acl *acl = find_acl(mon, aclname);
1629
1630 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001631 qemu_acl_reset(acl);
1632 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001633 }
1634}
aliguori76655d62009-03-06 20:27:37 +00001635
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001636static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001637{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001638 const char *aclname = qdict_get_str(qdict, "aclname");
1639 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001640 qemu_acl *acl = find_acl(mon, aclname);
1641
1642 if (acl) {
1643 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001644 acl->defaultDeny = 0;
1645 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001646 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001647 acl->defaultDeny = 1;
1648 monitor_printf(mon, "acl: policy set to 'deny'\n");
1649 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001650 monitor_printf(mon, "acl: unknown policy '%s', "
1651 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001652 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001653 }
1654}
aliguori76655d62009-03-06 20:27:37 +00001655
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001656static void hmp_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001657{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001658 const char *aclname = qdict_get_str(qdict, "aclname");
1659 const char *match = qdict_get_str(qdict, "match");
1660 const char *policy = qdict_get_str(qdict, "policy");
1661 int has_index = qdict_haskey(qdict, "index");
1662 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001663 qemu_acl *acl = find_acl(mon, aclname);
1664 int deny, ret;
1665
1666 if (acl) {
1667 if (strcmp(policy, "allow") == 0) {
1668 deny = 0;
1669 } else if (strcmp(policy, "deny") == 0) {
1670 deny = 1;
1671 } else {
1672 monitor_printf(mon, "acl: unknown policy '%s', "
1673 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001674 return;
1675 }
aliguori28a76be2009-03-06 20:27:40 +00001676 if (has_index)
1677 ret = qemu_acl_insert(acl, deny, match, index);
1678 else
1679 ret = qemu_acl_append(acl, deny, match);
1680 if (ret < 0)
1681 monitor_printf(mon, "acl: unable to add acl entry\n");
1682 else
1683 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001684 }
1685}
aliguori76655d62009-03-06 20:27:37 +00001686
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001687static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001688{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001689 const char *aclname = qdict_get_str(qdict, "aclname");
1690 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001691 qemu_acl *acl = find_acl(mon, aclname);
1692 int ret;
aliguori76655d62009-03-06 20:27:37 +00001693
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001694 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001695 ret = qemu_acl_remove(acl, match);
1696 if (ret < 0)
1697 monitor_printf(mon, "acl: no matching acl entry\n");
1698 else
1699 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001700 }
1701}
1702
Corey Bryant208c9d12012-06-22 14:36:09 -04001703void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001704{
Anthony Liguoric227f092009-10-01 16:12:16 -05001705 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001706 int fd;
1707
Corey Bryant208c9d12012-06-22 14:36:09 -04001708 fd = qemu_chr_fe_get_msgfd(cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001709 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001710 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryant208c9d12012-06-22 14:36:09 -04001711 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001712 }
1713
1714 if (qemu_isdigit(fdname[0])) {
Stefan Hajnoczi0b9f0e22014-04-24 13:58:18 +02001715 close(fd);
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001716 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
1717 "a name not starting with a digit");
Corey Bryant208c9d12012-06-22 14:36:09 -04001718 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001719 }
1720
Corey Bryant208c9d12012-06-22 14:36:09 -04001721 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001722 if (strcmp(monfd->name, fdname) != 0) {
1723 continue;
1724 }
1725
1726 close(monfd->fd);
1727 monfd->fd = fd;
Corey Bryant208c9d12012-06-22 14:36:09 -04001728 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001729 }
1730
Anthony Liguori7267c092011-08-20 22:09:37 -05001731 monfd = g_malloc0(sizeof(mon_fd_t));
1732 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001733 monfd->fd = fd;
1734
Corey Bryant208c9d12012-06-22 14:36:09 -04001735 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001736}
1737
Corey Bryant208c9d12012-06-22 14:36:09 -04001738void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001739{
Anthony Liguoric227f092009-10-01 16:12:16 -05001740 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001741
Corey Bryant208c9d12012-06-22 14:36:09 -04001742 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001743 if (strcmp(monfd->name, fdname) != 0) {
1744 continue;
1745 }
1746
Blue Swirl72cf2d42009-09-12 07:36:22 +00001747 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001748 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001749 g_free(monfd->name);
1750 g_free(monfd);
Corey Bryant208c9d12012-06-22 14:36:09 -04001751 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001752 }
1753
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001754 error_setg(errp, QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001755}
1756
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001757static void hmp_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001758{
Luiz Capitulino13548692011-07-29 15:36:43 -03001759 int saved_vm_running = runstate_is_running();
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001760 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02001761
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001762 vm_stop(RUN_STATE_RESTORE_VM);
Juan Quintelac8d41b22009-08-20 19:42:21 +02001763
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03001764 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02001765 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03001766 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02001767}
1768
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001769int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
Mark McLoughlin7768e042009-07-22 09:11:41 +01001770{
Anthony Liguoric227f092009-10-01 16:12:16 -05001771 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01001772
Blue Swirl72cf2d42009-09-12 07:36:22 +00001773 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01001774 int fd;
1775
1776 if (strcmp(monfd->name, fdname) != 0) {
1777 continue;
1778 }
1779
1780 fd = monfd->fd;
1781
1782 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001783 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05001784 g_free(monfd->name);
1785 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001786
1787 return fd;
1788 }
1789
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001790 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001791 return -1;
1792}
1793
Corey Bryantba1c0482012-08-14 16:43:43 -04001794static void monitor_fdset_cleanup(MonFdset *mon_fdset)
1795{
1796 MonFdsetFd *mon_fdset_fd;
1797 MonFdsetFd *mon_fdset_fd_next;
1798
1799 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantebe52b52012-10-18 15:19:33 -04001800 if ((mon_fdset_fd->removed ||
1801 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
1802 runstate_is_running()) {
Corey Bryantba1c0482012-08-14 16:43:43 -04001803 close(mon_fdset_fd->fd);
1804 g_free(mon_fdset_fd->opaque);
1805 QLIST_REMOVE(mon_fdset_fd, next);
1806 g_free(mon_fdset_fd);
1807 }
1808 }
1809
Corey Bryantadb696f2012-08-14 16:43:47 -04001810 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04001811 QLIST_REMOVE(mon_fdset, next);
1812 g_free(mon_fdset);
1813 }
1814}
1815
Corey Bryantefb87c12012-08-14 16:43:48 -04001816static void monitor_fdsets_cleanup(void)
1817{
1818 MonFdset *mon_fdset;
1819 MonFdset *mon_fdset_next;
1820
1821 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
1822 monitor_fdset_cleanup(mon_fdset);
1823 }
1824}
1825
Corey Bryantba1c0482012-08-14 16:43:43 -04001826AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
1827 const char *opaque, Error **errp)
1828{
1829 int fd;
1830 Monitor *mon = cur_mon;
Corey Bryantba1c0482012-08-14 16:43:43 -04001831 AddfdInfo *fdinfo;
1832
1833 fd = qemu_chr_fe_get_msgfd(mon->chr);
1834 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001835 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryantba1c0482012-08-14 16:43:43 -04001836 goto error;
1837 }
1838
Corey Bryante446f702012-10-18 15:19:32 -04001839 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
1840 has_opaque, opaque, errp);
1841 if (fdinfo) {
1842 return fdinfo;
Corey Bryant9ac54af2012-10-18 15:19:31 -04001843 }
1844
Corey Bryantba1c0482012-08-14 16:43:43 -04001845error:
1846 if (fd != -1) {
1847 close(fd);
1848 }
1849 return NULL;
1850}
1851
1852void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
1853{
1854 MonFdset *mon_fdset;
1855 MonFdsetFd *mon_fdset_fd;
1856 char fd_str[60];
1857
1858 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1859 if (mon_fdset->id != fdset_id) {
1860 continue;
1861 }
1862 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1863 if (has_fd) {
1864 if (mon_fdset_fd->fd != fd) {
1865 continue;
1866 }
1867 mon_fdset_fd->removed = true;
1868 break;
1869 } else {
1870 mon_fdset_fd->removed = true;
1871 }
1872 }
1873 if (has_fd && !mon_fdset_fd) {
1874 goto error;
1875 }
1876 monitor_fdset_cleanup(mon_fdset);
1877 return;
1878 }
1879
1880error:
1881 if (has_fd) {
1882 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
1883 fdset_id, fd);
1884 } else {
1885 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
1886 }
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001887 error_setg(errp, QERR_FD_NOT_FOUND, fd_str);
Corey Bryantba1c0482012-08-14 16:43:43 -04001888}
1889
1890FdsetInfoList *qmp_query_fdsets(Error **errp)
1891{
1892 MonFdset *mon_fdset;
1893 MonFdsetFd *mon_fdset_fd;
1894 FdsetInfoList *fdset_list = NULL;
1895
1896 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1897 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
1898 FdsetFdInfoList *fdsetfd_list = NULL;
1899
1900 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
1901 fdset_info->value->fdset_id = mon_fdset->id;
1902
1903 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1904 FdsetFdInfoList *fdsetfd_info;
1905
1906 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
1907 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
1908 fdsetfd_info->value->fd = mon_fdset_fd->fd;
1909 if (mon_fdset_fd->opaque) {
1910 fdsetfd_info->value->has_opaque = true;
1911 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
1912 } else {
1913 fdsetfd_info->value->has_opaque = false;
1914 }
1915
1916 fdsetfd_info->next = fdsetfd_list;
1917 fdsetfd_list = fdsetfd_info;
1918 }
1919
1920 fdset_info->value->fds = fdsetfd_list;
1921
1922 fdset_info->next = fdset_list;
1923 fdset_list = fdset_info;
1924 }
1925
1926 return fdset_list;
1927}
1928
Corey Bryante446f702012-10-18 15:19:32 -04001929AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
1930 bool has_opaque, const char *opaque,
1931 Error **errp)
1932{
1933 MonFdset *mon_fdset = NULL;
1934 MonFdsetFd *mon_fdset_fd;
1935 AddfdInfo *fdinfo;
1936
1937 if (has_fdset_id) {
1938 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1939 /* Break if match found or match impossible due to ordering by ID */
1940 if (fdset_id <= mon_fdset->id) {
1941 if (fdset_id < mon_fdset->id) {
1942 mon_fdset = NULL;
1943 }
1944 break;
1945 }
1946 }
1947 }
1948
1949 if (mon_fdset == NULL) {
1950 int64_t fdset_id_prev = -1;
1951 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
1952
1953 if (has_fdset_id) {
1954 if (fdset_id < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001955 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
1956 "a non-negative value");
Corey Bryante446f702012-10-18 15:19:32 -04001957 return NULL;
1958 }
1959 /* Use specified fdset ID */
1960 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1961 mon_fdset_cur = mon_fdset;
1962 if (fdset_id < mon_fdset_cur->id) {
1963 break;
1964 }
1965 }
1966 } else {
1967 /* Use first available fdset ID */
1968 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1969 mon_fdset_cur = mon_fdset;
1970 if (fdset_id_prev == mon_fdset_cur->id - 1) {
1971 fdset_id_prev = mon_fdset_cur->id;
1972 continue;
1973 }
1974 break;
1975 }
1976 }
1977
1978 mon_fdset = g_malloc0(sizeof(*mon_fdset));
1979 if (has_fdset_id) {
1980 mon_fdset->id = fdset_id;
1981 } else {
1982 mon_fdset->id = fdset_id_prev + 1;
1983 }
1984
1985 /* The fdset list is ordered by fdset ID */
1986 if (!mon_fdset_cur) {
1987 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
1988 } else if (mon_fdset->id < mon_fdset_cur->id) {
1989 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
1990 } else {
1991 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
1992 }
1993 }
1994
1995 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
1996 mon_fdset_fd->fd = fd;
1997 mon_fdset_fd->removed = false;
1998 if (has_opaque) {
1999 mon_fdset_fd->opaque = g_strdup(opaque);
2000 }
2001 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
2002
2003 fdinfo = g_malloc0(sizeof(*fdinfo));
2004 fdinfo->fdset_id = mon_fdset->id;
2005 fdinfo->fd = mon_fdset_fd->fd;
2006
2007 return fdinfo;
2008}
2009
Corey Bryantadb696f2012-08-14 16:43:47 -04002010int monitor_fdset_get_fd(int64_t fdset_id, int flags)
2011{
Blue Swirlb2dc64c2012-08-18 20:14:54 +00002012#ifndef _WIN32
Corey Bryantadb696f2012-08-14 16:43:47 -04002013 MonFdset *mon_fdset;
2014 MonFdsetFd *mon_fdset_fd;
2015 int mon_fd_flags;
2016
Corey Bryantadb696f2012-08-14 16:43:47 -04002017 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2018 if (mon_fdset->id != fdset_id) {
2019 continue;
2020 }
2021 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2022 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
2023 if (mon_fd_flags == -1) {
2024 return -1;
2025 }
2026
2027 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
2028 return mon_fdset_fd->fd;
2029 }
2030 }
2031 errno = EACCES;
2032 return -1;
2033 }
2034#endif
2035
2036 errno = ENOENT;
2037 return -1;
2038}
2039
2040int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
2041{
2042 MonFdset *mon_fdset;
2043 MonFdsetFd *mon_fdset_fd_dup;
2044
2045 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2046 if (mon_fdset->id != fdset_id) {
2047 continue;
2048 }
2049 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2050 if (mon_fdset_fd_dup->fd == dup_fd) {
2051 return -1;
2052 }
2053 }
2054 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2055 mon_fdset_fd_dup->fd = dup_fd;
2056 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
2057 return 0;
2058 }
2059 return -1;
2060}
2061
2062static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2063{
2064 MonFdset *mon_fdset;
2065 MonFdsetFd *mon_fdset_fd_dup;
2066
2067 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2068 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2069 if (mon_fdset_fd_dup->fd == dup_fd) {
2070 if (remove) {
2071 QLIST_REMOVE(mon_fdset_fd_dup, next);
2072 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2073 monitor_fdset_cleanup(mon_fdset);
2074 }
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002075 return -1;
2076 } else {
2077 return mon_fdset->id;
Corey Bryantadb696f2012-08-14 16:43:47 -04002078 }
Corey Bryantadb696f2012-08-14 16:43:47 -04002079 }
2080 }
2081 }
2082 return -1;
2083}
2084
2085int monitor_fdset_dup_fd_find(int dup_fd)
2086{
2087 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2088}
2089
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002090void monitor_fdset_dup_fd_remove(int dup_fd)
Corey Bryantadb696f2012-08-14 16:43:47 -04002091{
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002092 monitor_fdset_dup_fd_find_remove(dup_fd, true);
Corey Bryantadb696f2012-08-14 16:43:47 -04002093}
2094
Markus Armbruster1677f4c2015-02-09 14:03:19 +01002095int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
Laszlo Ersek59063662014-04-10 10:24:31 +02002096{
2097 int fd;
2098 Error *local_err = NULL;
2099
2100 if (!qemu_isdigit(fdname[0]) && mon) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002101 fd = monitor_get_fd(mon, fdname, &local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002102 } else {
2103 fd = qemu_parse_fd(fdname);
Laszlo Ersek59063662014-04-10 10:24:31 +02002104 if (fd == -1) {
2105 error_setg(&local_err, "Invalid file descriptor number '%s'",
2106 fdname);
2107 }
2108 }
2109 if (local_err) {
2110 error_propagate(errp, local_err);
2111 assert(fd == -1);
2112 } else {
2113 assert(fd != -1);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002114 }
2115
2116 return fd;
2117}
2118
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002119/* Please update hmp-commands.hx when adding or changing commands */
Wayne Xia816f8922011-10-12 11:32:41 +08002120static mon_cmd_t info_cmds[] = {
Pavel Butsykinda76ee72015-09-10 18:38:58 +03002121#include "hmp-commands-info.h"
2122 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002123};
2124
Wenchao Xiaa13ced52013-01-14 14:06:28 +08002125/* mon_cmds and info_cmds would be sorted at runtime */
2126static mon_cmd_t mon_cmds[] = {
2127#include "hmp-commands.h"
2128 { NULL, NULL, },
2129};
2130
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002131static const mon_cmd_t qmp_cmds[] = {
Anthony Liguorie3193602011-09-02 12:34:47 -05002132#include "qmp-commands-old.h"
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002133 { /* NULL */ },
2134};
2135
bellard9307c4c2004-04-04 12:57:25 +00002136/*******************************************************************/
2137
2138static const char *pch;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002139static sigjmp_buf expr_env;
bellard9307c4c2004-04-04 12:57:25 +00002140
bellard9307c4c2004-04-04 12:57:25 +00002141
Stefan Weil9c3175c2013-08-22 21:30:09 +02002142static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
2143expr_error(Monitor *mon, const char *fmt, ...)
bellard9dc39cb2004-03-14 21:38:27 +00002144{
Fam Zheng277acfe2013-08-20 10:58:21 +08002145 va_list ap;
2146 va_start(ap, fmt);
2147 monitor_vprintf(mon, fmt, ap);
2148 monitor_printf(mon, "\n");
2149 va_end(ap);
Peter Maydell6ab7e542013-02-20 15:21:09 +00002150 siglongjmp(expr_env, 1);
bellard9307c4c2004-04-04 12:57:25 +00002151}
2152
Markus Armbruster09b94182010-01-20 13:07:30 +01002153/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00002154static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002155{
Pavel Butsykinbf957282015-09-10 18:38:59 +03002156 const MonitorDef *md = target_monitor_defs();
bellard92a31b12005-02-10 22:00:52 +00002157 void *ptr;
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002158 uint64_t tmp = 0;
2159 int ret;
bellard92a31b12005-02-10 22:00:52 +00002160
Pavel Butsykinbf957282015-09-10 18:38:59 +03002161 if (md == NULL) {
2162 return -1;
2163 }
2164
2165 for(; md->name != NULL; md++) {
bellard9307c4c2004-04-04 12:57:25 +00002166 if (compare_cmd(name, md->name)) {
2167 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002168 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002169 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002170 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00002171 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002172 switch(md->type) {
2173 case MD_I32:
2174 *pval = *(int32_t *)ptr;
2175 break;
2176 case MD_TLONG:
2177 *pval = *(target_long *)ptr;
2178 break;
2179 default:
2180 *pval = 0;
2181 break;
2182 }
bellard9307c4c2004-04-04 12:57:25 +00002183 }
2184 return 0;
2185 }
2186 }
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002187
2188 ret = target_get_monitor_def(mon_get_cpu(), name, &tmp);
2189 if (!ret) {
2190 *pval = (target_long) tmp;
2191 }
2192
2193 return ret;
bellard9307c4c2004-04-04 12:57:25 +00002194}
2195
2196static void next(void)
2197{
Blue Swirl660f11b2009-07-31 21:16:51 +00002198 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002199 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002200 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002201 pch++;
2202 }
2203}
2204
aliguori376253e2009-03-05 23:01:23 +00002205static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002206
aliguori376253e2009-03-05 23:01:23 +00002207static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002208{
blueswir1c2efc952007-09-25 17:28:42 +00002209 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002210 char *p;
bellard6a00d602005-11-21 23:25:50 +00002211 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002212
2213 switch(*pch) {
2214 case '+':
2215 next();
aliguori376253e2009-03-05 23:01:23 +00002216 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002217 break;
2218 case '-':
2219 next();
aliguori376253e2009-03-05 23:01:23 +00002220 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002221 break;
2222 case '~':
2223 next();
aliguori376253e2009-03-05 23:01:23 +00002224 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002225 break;
2226 case '(':
2227 next();
aliguori376253e2009-03-05 23:01:23 +00002228 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002229 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002230 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002231 }
2232 next();
2233 break;
bellard81d09122004-07-14 17:21:37 +00002234 case '\'':
2235 pch++;
2236 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002237 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002238 n = *pch;
2239 pch++;
2240 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002241 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002242 next();
2243 break;
bellard9307c4c2004-04-04 12:57:25 +00002244 case '$':
2245 {
2246 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002247 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002248
bellard9307c4c2004-04-04 12:57:25 +00002249 pch++;
2250 q = buf;
2251 while ((*pch >= 'a' && *pch <= 'z') ||
2252 (*pch >= 'A' && *pch <= 'Z') ||
2253 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002254 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002255 if ((q - buf) < sizeof(buf) - 1)
2256 *q++ = *pch;
2257 pch++;
2258 }
blueswir1cd390082008-11-16 13:53:32 +00002259 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002260 pch++;
2261 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002262 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01002263 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00002264 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00002265 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002266 }
2267 break;
2268 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002269 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002270 n = 0;
2271 break;
2272 default:
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002273 errno = 0;
bellard4f4fbf72006-06-25 18:28:12 +00002274 n = strtoull(pch, &p, 0);
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002275 if (errno == ERANGE) {
2276 expr_error(mon, "number too large");
2277 }
bellard9307c4c2004-04-04 12:57:25 +00002278 if (pch == p) {
Fam Zheng277acfe2013-08-20 10:58:21 +08002279 expr_error(mon, "invalid char '%c' in expression", *p);
bellard9307c4c2004-04-04 12:57:25 +00002280 }
2281 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002282 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002283 pch++;
2284 break;
2285 }
2286 return n;
2287}
2288
2289
aliguori376253e2009-03-05 23:01:23 +00002290static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002291{
blueswir1c2efc952007-09-25 17:28:42 +00002292 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002293 int op;
ths3b46e622007-09-17 08:09:54 +00002294
aliguori376253e2009-03-05 23:01:23 +00002295 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002296 for(;;) {
2297 op = *pch;
2298 if (op != '*' && op != '/' && op != '%')
2299 break;
2300 next();
aliguori376253e2009-03-05 23:01:23 +00002301 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002302 switch(op) {
2303 default:
2304 case '*':
2305 val *= val2;
2306 break;
2307 case '/':
2308 case '%':
ths5fafdf22007-09-16 21:08:06 +00002309 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002310 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002311 if (op == '/')
2312 val /= val2;
2313 else
2314 val %= val2;
2315 break;
2316 }
2317 }
2318 return val;
2319}
2320
aliguori376253e2009-03-05 23:01:23 +00002321static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002322{
blueswir1c2efc952007-09-25 17:28:42 +00002323 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002324 int op;
bellard9307c4c2004-04-04 12:57:25 +00002325
aliguori376253e2009-03-05 23:01:23 +00002326 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002327 for(;;) {
2328 op = *pch;
2329 if (op != '&' && op != '|' && op != '^')
2330 break;
2331 next();
aliguori376253e2009-03-05 23:01:23 +00002332 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002333 switch(op) {
2334 default:
2335 case '&':
2336 val &= val2;
2337 break;
2338 case '|':
2339 val |= val2;
2340 break;
2341 case '^':
2342 val ^= val2;
2343 break;
2344 }
2345 }
2346 return val;
2347}
2348
aliguori376253e2009-03-05 23:01:23 +00002349static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002350{
blueswir1c2efc952007-09-25 17:28:42 +00002351 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002352 int op;
bellard9307c4c2004-04-04 12:57:25 +00002353
aliguori376253e2009-03-05 23:01:23 +00002354 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002355 for(;;) {
2356 op = *pch;
2357 if (op != '+' && op != '-')
2358 break;
2359 next();
aliguori376253e2009-03-05 23:01:23 +00002360 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002361 if (op == '+')
2362 val += val2;
2363 else
2364 val -= val2;
2365 }
2366 return val;
2367}
2368
aliguori376253e2009-03-05 23:01:23 +00002369static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002370{
2371 pch = *pp;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002372 if (sigsetjmp(expr_env, 0)) {
bellard9307c4c2004-04-04 12:57:25 +00002373 *pp = pch;
2374 return -1;
2375 }
blueswir1cd390082008-11-16 13:53:32 +00002376 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002377 pch++;
aliguori376253e2009-03-05 23:01:23 +00002378 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002379 *pp = pch;
2380 return 0;
2381}
2382
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002383static int get_double(Monitor *mon, double *pval, const char **pp)
2384{
2385 const char *p = *pp;
2386 char *tailp;
2387 double d;
2388
2389 d = strtod(p, &tailp);
2390 if (tailp == p) {
2391 monitor_printf(mon, "Number expected\n");
2392 return -1;
2393 }
2394 if (d != d || d - d != 0) {
2395 /* NaN or infinity */
2396 monitor_printf(mon, "Bad number\n");
2397 return -1;
2398 }
2399 *pval = d;
2400 *pp = tailp;
2401 return 0;
2402}
2403
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002404/*
2405 * Store the command-name in cmdname, and return a pointer to
2406 * the remaining of the command string.
2407 */
2408static const char *get_command_name(const char *cmdline,
2409 char *cmdname, size_t nlen)
2410{
2411 size_t len;
2412 const char *p, *pstart;
2413
2414 p = cmdline;
2415 while (qemu_isspace(*p))
2416 p++;
2417 if (*p == '\0')
2418 return NULL;
2419 pstart = p;
2420 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2421 p++;
2422 len = p - pstart;
2423 if (len > nlen - 1)
2424 len = nlen - 1;
2425 memcpy(cmdname, pstart, len);
2426 cmdname[len] = '\0';
2427 return p;
2428}
2429
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002430/**
2431 * Read key of 'type' into 'key' and return the current
2432 * 'type' pointer.
2433 */
2434static char *key_get_info(const char *type, char **key)
2435{
2436 size_t len;
2437 char *p, *str;
2438
2439 if (*type == ',')
2440 type++;
2441
2442 p = strchr(type, ':');
2443 if (!p) {
2444 *key = NULL;
2445 return NULL;
2446 }
2447 len = p - type;
2448
Anthony Liguori7267c092011-08-20 22:09:37 -05002449 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002450 memcpy(str, type, len);
2451 str[len] = '\0';
2452
2453 *key = str;
2454 return ++p;
2455}
2456
bellard9307c4c2004-04-04 12:57:25 +00002457static int default_fmt_format = 'x';
2458static int default_fmt_size = 4;
2459
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002460static int is_valid_option(const char *c, const char *typestr)
2461{
2462 char option[3];
2463
2464 option[0] = '-';
2465 option[1] = *c;
2466 option[2] = '\0';
2467
2468 typestr = strstr(typestr, option);
2469 return (typestr != NULL);
2470}
2471
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03002472static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
2473 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002474{
2475 const mon_cmd_t *cmd;
2476
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03002477 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002478 if (compare_cmd(cmdname, cmd->name)) {
2479 return cmd;
2480 }
2481 }
2482
2483 return NULL;
2484}
2485
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03002486static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
2487{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002488 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03002489}
2490
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002491/*
Bandan Dasae502122015-06-03 18:38:08 -04002492 * Parse command name from @cmdp according to command table @table.
2493 * If blank, return NULL.
2494 * Else, if no valid command can be found, report to @mon, and return
2495 * NULL.
2496 * Else, change @cmdp to point right behind the name, and return its
2497 * command table entry.
2498 * Do not assume the return value points into @table! It doesn't when
2499 * the command is found in a sub-command table.
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002500 */
Anthony Liguoric227f092009-10-01 16:12:16 -05002501static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Bandan Dasae502122015-06-03 18:38:08 -04002502 const char **cmdp,
2503 mon_cmd_t *table)
bellard9307c4c2004-04-04 12:57:25 +00002504{
Bandan Dasae502122015-06-03 18:38:08 -04002505 const char *p;
Anthony Liguoric227f092009-10-01 16:12:16 -05002506 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002507 char cmdname[256];
bellard9dc39cb2004-03-14 21:38:27 +00002508
bellard9307c4c2004-04-04 12:57:25 +00002509 /* extract the command name */
Bandan Dasae502122015-06-03 18:38:08 -04002510 p = get_command_name(*cmdp, cmdname, sizeof(cmdname));
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002511 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002512 return NULL;
ths3b46e622007-09-17 08:09:54 +00002513
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002514 cmd = search_dispatch_table(table, cmdname);
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002515 if (!cmd) {
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002516 monitor_printf(mon, "unknown command: '%.*s'\n",
Bandan Dasae502122015-06-03 18:38:08 -04002517 (int)(p - *cmdp), *cmdp);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002518 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002519 }
bellard9307c4c2004-04-04 12:57:25 +00002520
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002521 /* filter out following useless space */
2522 while (qemu_isspace(*p)) {
2523 p++;
2524 }
Bandan Dasae502122015-06-03 18:38:08 -04002525
2526 *cmdp = p;
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002527 /* search sub command */
Bandan Dasae502122015-06-03 18:38:08 -04002528 if (cmd->sub_table != NULL && *p != '\0') {
2529 return monitor_parse_command(mon, cmdp, cmd->sub_table);
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002530 }
2531
Bandan Dasae502122015-06-03 18:38:08 -04002532 return cmd;
2533}
2534
2535/*
2536 * Parse arguments for @cmd.
2537 * If it can't be parsed, report to @mon, and return NULL.
2538 * Else, insert command arguments into a QDict, and return it.
2539 * Note: On success, caller has to free the QDict structure.
2540 */
2541
2542static QDict *monitor_parse_arguments(Monitor *mon,
2543 const char **endp,
2544 const mon_cmd_t *cmd)
2545{
2546 const char *typestr;
2547 char *key;
2548 int c;
2549 const char *p = *endp;
2550 char buf[1024];
2551 QDict *qdict = qdict_new();
2552
bellard9307c4c2004-04-04 12:57:25 +00002553 /* parse the parameters */
2554 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00002555 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002556 typestr = key_get_info(typestr, &key);
2557 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00002558 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002559 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00002560 typestr++;
2561 switch(c) {
2562 case 'F':
bellard81d09122004-07-14 17:21:37 +00002563 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002564 case 's':
2565 {
2566 int ret;
ths3b46e622007-09-17 08:09:54 +00002567
blueswir1cd390082008-11-16 13:53:32 +00002568 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002569 p++;
2570 if (*typestr == '?') {
2571 typestr++;
2572 if (*p == '\0') {
2573 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03002574 break;
bellard9307c4c2004-04-04 12:57:25 +00002575 }
2576 }
2577 ret = get_str(buf, sizeof(buf), &p);
2578 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002579 switch(c) {
2580 case 'F':
aliguori376253e2009-03-05 23:01:23 +00002581 monitor_printf(mon, "%s: filename expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002582 cmd->name);
bellard81d09122004-07-14 17:21:37 +00002583 break;
2584 case 'B':
aliguori376253e2009-03-05 23:01:23 +00002585 monitor_printf(mon, "%s: block device name expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002586 cmd->name);
bellard81d09122004-07-14 17:21:37 +00002587 break;
2588 default:
Bandan Dasae502122015-06-03 18:38:08 -04002589 monitor_printf(mon, "%s: string expected\n", cmd->name);
bellard81d09122004-07-14 17:21:37 +00002590 break;
2591 }
bellard9307c4c2004-04-04 12:57:25 +00002592 goto fail;
2593 }
Luiz Capitulino53773582009-08-28 15:27:25 -03002594 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00002595 }
2596 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01002597 case 'O':
2598 {
2599 QemuOptsList *opts_list;
2600 QemuOpts *opts;
2601
2602 opts_list = qemu_find_opts(key);
2603 if (!opts_list || opts_list->desc->name) {
2604 goto bad_type;
2605 }
2606 while (qemu_isspace(*p)) {
2607 p++;
2608 }
2609 if (!*p)
2610 break;
2611 if (get_str(buf, sizeof(buf), &p) < 0) {
2612 goto fail;
2613 }
Markus Armbruster70b94332015-02-13 12:50:26 +01002614 opts = qemu_opts_parse_noisily(opts_list, buf, true);
Markus Armbruster361127d2010-02-10 20:24:35 +01002615 if (!opts) {
2616 goto fail;
2617 }
2618 qemu_opts_to_qdict(opts, qdict);
2619 qemu_opts_del(opts);
2620 }
2621 break;
bellard9307c4c2004-04-04 12:57:25 +00002622 case '/':
2623 {
2624 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002625
blueswir1cd390082008-11-16 13:53:32 +00002626 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002627 p++;
2628 if (*p == '/') {
2629 /* format found */
2630 p++;
2631 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002632 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002633 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002634 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002635 count = count * 10 + (*p - '0');
2636 p++;
2637 }
2638 }
2639 size = -1;
2640 format = -1;
2641 for(;;) {
2642 switch(*p) {
2643 case 'o':
2644 case 'd':
2645 case 'u':
2646 case 'x':
2647 case 'i':
2648 case 'c':
2649 format = *p++;
2650 break;
2651 case 'b':
2652 size = 1;
2653 p++;
2654 break;
2655 case 'h':
2656 size = 2;
2657 p++;
2658 break;
2659 case 'w':
2660 size = 4;
2661 p++;
2662 break;
2663 case 'g':
2664 case 'L':
2665 size = 8;
2666 p++;
2667 break;
2668 default:
2669 goto next;
2670 }
2671 }
2672 next:
blueswir1cd390082008-11-16 13:53:32 +00002673 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00002674 monitor_printf(mon, "invalid char in format: '%c'\n",
2675 *p);
bellard9307c4c2004-04-04 12:57:25 +00002676 goto fail;
2677 }
bellard9307c4c2004-04-04 12:57:25 +00002678 if (format < 0)
2679 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002680 if (format != 'i') {
2681 /* for 'i', not specifying a size gives -1 as size */
2682 if (size < 0)
2683 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002684 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002685 }
bellard9307c4c2004-04-04 12:57:25 +00002686 default_fmt_format = format;
2687 } else {
2688 count = 1;
2689 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002690 if (format != 'i') {
2691 size = default_fmt_size;
2692 } else {
2693 size = -1;
2694 }
bellard9307c4c2004-04-04 12:57:25 +00002695 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002696 qdict_put(qdict, "count", qint_from_int(count));
2697 qdict_put(qdict, "format", qint_from_int(format));
2698 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00002699 }
2700 break;
2701 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002702 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002703 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00002704 {
blueswir1c2efc952007-09-25 17:28:42 +00002705 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00002706
blueswir1cd390082008-11-16 13:53:32 +00002707 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002708 p++;
bellard34405572004-06-08 00:55:58 +00002709 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002710 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03002711 if (*p == '\0') {
2712 typestr++;
2713 break;
2714 }
bellard34405572004-06-08 00:55:58 +00002715 } else {
2716 if (*p == '.') {
2717 p++;
blueswir1cd390082008-11-16 13:53:32 +00002718 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00002719 p++;
bellard34405572004-06-08 00:55:58 +00002720 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03002721 typestr++;
2722 break;
bellard34405572004-06-08 00:55:58 +00002723 }
2724 }
bellard13224a82006-07-14 22:03:35 +00002725 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002726 }
aliguori376253e2009-03-05 23:01:23 +00002727 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00002728 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002729 /* Check if 'i' is greater than 32-bit */
2730 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
Bandan Dasae502122015-06-03 18:38:08 -04002731 monitor_printf(mon, "\'%s\' has failed: ", cmd->name);
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002732 monitor_printf(mon, "integer is for 32-bit values\n");
2733 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002734 } else if (c == 'M') {
Luiz Capitulino91162842012-04-26 17:34:30 -03002735 if (val < 0) {
2736 monitor_printf(mon, "enter a positive value\n");
2737 goto fail;
2738 }
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002739 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002740 }
Luiz Capitulino53773582009-08-28 15:27:25 -03002741 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00002742 }
2743 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02002744 case 'o':
2745 {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01002746 int64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02002747 char *end;
2748
2749 while (qemu_isspace(*p)) {
2750 p++;
2751 }
2752 if (*typestr == '?') {
2753 typestr++;
2754 if (*p == '\0') {
2755 break;
2756 }
2757 }
Marc-André Lureau4677bb42015-09-16 18:02:56 +02002758 val = qemu_strtosz(p, &end);
Jes Sorensendbc0c672010-10-21 17:15:47 +02002759 if (val < 0) {
2760 monitor_printf(mon, "invalid size\n");
2761 goto fail;
2762 }
2763 qdict_put(qdict, key, qint_from_int(val));
2764 p = end;
2765 }
2766 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01002767 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002768 {
2769 double val;
2770
2771 while (qemu_isspace(*p))
2772 p++;
2773 if (*typestr == '?') {
2774 typestr++;
2775 if (*p == '\0') {
2776 break;
2777 }
2778 }
2779 if (get_double(mon, &val, &p) < 0) {
2780 goto fail;
2781 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02002782 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01002783 switch (*p) {
2784 case 'm':
2785 val /= 1e3; p += 2; break;
2786 case 'u':
2787 val /= 1e6; p += 2; break;
2788 case 'n':
2789 val /= 1e9; p += 2; break;
2790 }
2791 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002792 if (*p && !qemu_isspace(*p)) {
2793 monitor_printf(mon, "Unknown unit suffix\n");
2794 goto fail;
2795 }
2796 qdict_put(qdict, key, qfloat_from_double(val));
2797 }
2798 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002799 case 'b':
2800 {
2801 const char *beg;
Eric Blakefc48ffc2015-05-15 16:24:59 -06002802 bool val;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002803
2804 while (qemu_isspace(*p)) {
2805 p++;
2806 }
2807 beg = p;
2808 while (qemu_isgraph(*p)) {
2809 p++;
2810 }
2811 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06002812 val = true;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002813 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06002814 val = false;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002815 } else {
2816 monitor_printf(mon, "Expected 'on' or 'off'\n");
2817 goto fail;
2818 }
Eric Blakefc48ffc2015-05-15 16:24:59 -06002819 qdict_put(qdict, key, qbool_from_bool(val));
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002820 }
2821 break;
bellard9307c4c2004-04-04 12:57:25 +00002822 case '-':
2823 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002824 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03002825 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00002826 /* option */
ths3b46e622007-09-17 08:09:54 +00002827
bellard9307c4c2004-04-04 12:57:25 +00002828 c = *typestr++;
2829 if (c == '\0')
2830 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00002831 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002832 p++;
bellard9307c4c2004-04-04 12:57:25 +00002833 if (*p == '-') {
2834 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002835 if(c != *p) {
2836 if(!is_valid_option(p, typestr)) {
2837
2838 monitor_printf(mon, "%s: unsupported option -%c\n",
Bandan Dasae502122015-06-03 18:38:08 -04002839 cmd->name, *p);
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002840 goto fail;
2841 } else {
2842 skip_key = 1;
2843 }
bellard9307c4c2004-04-04 12:57:25 +00002844 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002845 if(skip_key) {
2846 p = tmp;
2847 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03002848 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002849 p++;
Eric Blakefc48ffc2015-05-15 16:24:59 -06002850 qdict_put(qdict, key, qbool_from_bool(true));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002851 }
bellard9307c4c2004-04-04 12:57:25 +00002852 }
bellard9307c4c2004-04-04 12:57:25 +00002853 }
2854 break;
Wenchao Xia129be002013-08-27 20:38:26 +08002855 case 'S':
2856 {
2857 /* package all remaining string */
2858 int len;
2859
2860 while (qemu_isspace(*p)) {
2861 p++;
2862 }
2863 if (*typestr == '?') {
2864 typestr++;
2865 if (*p == '\0') {
2866 /* no remaining string: NULL argument */
2867 break;
2868 }
2869 }
2870 len = strlen(p);
2871 if (len <= 0) {
2872 monitor_printf(mon, "%s: string expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002873 cmd->name);
Bandan Dase549d2a2015-06-03 18:38:10 -04002874 goto fail;
Wenchao Xia129be002013-08-27 20:38:26 +08002875 }
2876 qdict_put(qdict, key, qstring_from_str(p));
2877 p += len;
2878 }
2879 break;
bellard9307c4c2004-04-04 12:57:25 +00002880 default:
2881 bad_type:
Bandan Dasae502122015-06-03 18:38:08 -04002882 monitor_printf(mon, "%s: unknown type '%c'\n", cmd->name, c);
bellard9307c4c2004-04-04 12:57:25 +00002883 goto fail;
2884 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002885 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002886 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00002887 }
2888 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00002889 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002890 p++;
2891 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00002892 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
Bandan Dasae502122015-06-03 18:38:08 -04002893 cmd->name);
bellard9307c4c2004-04-04 12:57:25 +00002894 goto fail;
2895 }
2896
Bandan Dasae502122015-06-03 18:38:08 -04002897 return qdict;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02002898
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002899fail:
Bandan Dasae502122015-06-03 18:38:08 -04002900 QDECREF(qdict);
Anthony Liguori7267c092011-08-20 22:09:37 -05002901 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002902 return NULL;
2903}
2904
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01002905static void handle_hmp_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002906{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002907 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05002908 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002909
Bandan Dasae502122015-06-03 18:38:08 -04002910 cmd = monitor_parse_command(mon, &cmdline, mon->cmd_table);
2911 if (!cmd) {
2912 return;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002913 }
2914
Bandan Dasae502122015-06-03 18:38:08 -04002915 qdict = monitor_parse_arguments(mon, &cmdline, cmd);
2916 if (!qdict) {
Bandan Dasdd41eea2015-06-03 18:38:09 -04002917 monitor_printf(mon, "Try \"help %s\" for more information\n",
2918 cmd->name);
Bandan Dasae502122015-06-03 18:38:08 -04002919 return;
2920 }
2921
2922 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002923 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00002924}
2925
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08002926static void cmd_completion(Monitor *mon, const char *name, const char *list)
bellard81d09122004-07-14 17:21:37 +00002927{
2928 const char *p, *pstart;
2929 char cmd[128];
2930 int len;
2931
2932 p = list;
2933 for(;;) {
2934 pstart = p;
2935 p = strchr(p, '|');
2936 if (!p)
2937 p = pstart + strlen(pstart);
2938 len = p - pstart;
2939 if (len > sizeof(cmd) - 2)
2940 len = sizeof(cmd) - 2;
2941 memcpy(cmd, pstart, len);
2942 cmd[len] = '\0';
2943 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08002944 readline_add_completion(mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00002945 }
2946 if (*p == '\0')
2947 break;
2948 p++;
2949 }
2950}
2951
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08002952static void file_completion(Monitor *mon, const char *input)
bellard81d09122004-07-14 17:21:37 +00002953{
2954 DIR *ffs;
2955 struct dirent *d;
2956 char path[1024];
2957 char file[1024], file_prefix[1024];
2958 int input_path_len;
2959 const char *p;
2960
ths5fafdf22007-09-16 21:08:06 +00002961 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00002962 if (!p) {
2963 input_path_len = 0;
2964 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00002965 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00002966 } else {
2967 input_path_len = p - input + 1;
2968 memcpy(path, input, input_path_len);
2969 if (input_path_len > sizeof(path) - 1)
2970 input_path_len = sizeof(path) - 1;
2971 path[input_path_len] = '\0';
2972 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2973 }
Bandan Das19f2db52015-06-03 18:38:07 -04002974
bellard81d09122004-07-14 17:21:37 +00002975 ffs = opendir(path);
2976 if (!ffs)
2977 return;
2978 for(;;) {
2979 struct stat sb;
2980 d = readdir(ffs);
2981 if (!d)
2982 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09002983
2984 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
2985 continue;
2986 }
2987
bellard81d09122004-07-14 17:21:37 +00002988 if (strstart(d->d_name, file_prefix, NULL)) {
2989 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00002990 if (input_path_len < sizeof(file))
2991 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2992 d->d_name);
bellard81d09122004-07-14 17:21:37 +00002993 /* stat the file to find out if it's a directory.
2994 * In that case add a slash to speed up typing long paths
2995 */
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01002996 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
blueswir1363a37d2008-08-21 17:58:08 +00002997 pstrcat(file, sizeof(file), "/");
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01002998 }
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08002999 readline_add_completion(mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003000 }
3001 }
3002 closedir(ffs);
3003}
3004
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003005static const char *next_arg_type(const char *typestr)
3006{
3007 const char *p = strchr(typestr, ':');
3008 return (p != NULL ? ++p : typestr);
3009}
3010
Hani Benhabiles40d19392014-05-07 23:41:30 +01003011static void add_completion_option(ReadLineState *rs, const char *str,
3012 const char *option)
3013{
3014 if (!str || !option) {
3015 return;
3016 }
3017 if (!strncmp(option, str, strlen(str))) {
3018 readline_add_completion(rs, option);
3019 }
3020}
3021
Hani Benhabiles13e315d2014-05-07 23:41:29 +01003022void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
3023{
3024 size_t len;
3025 ChardevBackendInfoList *list, *start;
3026
3027 if (nb_args != 2) {
3028 return;
3029 }
3030 len = strlen(str);
3031 readline_set_completion_index(rs, len);
3032
3033 start = list = qmp_query_chardev_backends(NULL);
3034 while (list) {
3035 const char *chr_name = list->value->name;
3036
3037 if (!strncmp(chr_name, str, len)) {
3038 readline_add_completion(rs, chr_name);
3039 }
3040 list = list->next;
3041 }
3042 qapi_free_ChardevBackendInfoList(start);
3043}
3044
Hani Benhabilesb162b492014-05-07 23:41:31 +01003045void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
3046{
3047 size_t len;
3048 int i;
3049
3050 if (nb_args != 2) {
3051 return;
3052 }
3053 len = strlen(str);
3054 readline_set_completion_index(rs, len);
3055 for (i = 0; NetClientOptionsKind_lookup[i]; i++) {
3056 add_completion_option(rs, str, NetClientOptionsKind_lookup[i]);
3057 }
3058}
3059
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003060void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003061{
3062 GSList *list, *elt;
3063 size_t len;
3064
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003065 if (nb_args != 2) {
3066 return;
3067 }
3068
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003069 len = strlen(str);
3070 readline_set_completion_index(rs, len);
3071 list = elt = object_class_get_list(TYPE_DEVICE, false);
3072 while (elt) {
3073 const char *name;
3074 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
3075 TYPE_DEVICE);
3076 name = object_class_get_name(OBJECT_CLASS(dc));
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003077
3078 if (!dc->cannot_instantiate_with_device_add_yet
3079 && !strncmp(name, str, len)) {
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003080 readline_add_completion(rs, name);
3081 }
3082 elt = elt->next;
3083 }
3084 g_slist_free(list);
3085}
3086
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003087void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003088{
3089 GSList *list, *elt;
3090 size_t len;
3091
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003092 if (nb_args != 2) {
3093 return;
3094 }
3095
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003096 len = strlen(str);
3097 readline_set_completion_index(rs, len);
3098 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
3099 while (elt) {
3100 const char *name;
3101
3102 name = object_class_get_name(OBJECT_CLASS(elt->data));
3103 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
3104 readline_add_completion(rs, name);
3105 }
3106 elt = elt->next;
3107 }
3108 g_slist_free(list);
3109}
3110
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003111static void peripheral_device_del_completion(ReadLineState *rs,
3112 const char *str, size_t len)
3113{
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003114 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
3115 GSList *list, *item;
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003116
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003117 list = qdev_build_hotpluggable_device_list(peripheral);
3118 if (!list) {
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003119 return;
3120 }
3121
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003122 for (item = list; item; item = g_slist_next(item)) {
3123 DeviceState *dev = item->data;
3124
3125 if (dev->id && !strncmp(str, dev->id, len)) {
3126 readline_add_completion(rs, dev->id);
3127 }
3128 }
3129
3130 g_slist_free(list);
3131}
3132
Hani Benhabiles6297d9a2014-05-07 23:41:28 +01003133void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3134{
3135 size_t len;
3136 ChardevInfoList *list, *start;
3137
3138 if (nb_args != 2) {
3139 return;
3140 }
3141 len = strlen(str);
3142 readline_set_completion_index(rs, len);
3143
3144 start = list = qmp_query_chardev(NULL);
3145 while (list) {
3146 ChardevInfo *chr = list->value;
3147
3148 if (!strncmp(chr->label, str, len)) {
3149 readline_add_completion(rs, chr->label);
3150 }
3151 list = list->next;
3152 }
3153 qapi_free_ChardevInfoList(start);
3154}
3155
Hani Benhabiles8e597772014-05-27 23:39:30 +01003156static void ringbuf_completion(ReadLineState *rs, const char *str)
3157{
3158 size_t len;
3159 ChardevInfoList *list, *start;
3160
3161 len = strlen(str);
3162 readline_set_completion_index(rs, len);
3163
3164 start = list = qmp_query_chardev(NULL);
3165 while (list) {
3166 ChardevInfo *chr_info = list->value;
3167
3168 if (!strncmp(chr_info->label, str, len)) {
3169 CharDriverState *chr = qemu_chr_find(chr_info->label);
3170 if (chr && chr_is_ringbuf(chr)) {
3171 readline_add_completion(rs, chr_info->label);
3172 }
3173 }
3174 list = list->next;
3175 }
3176 qapi_free_ChardevInfoList(start);
3177}
3178
Hani Benhabiles8e597772014-05-27 23:39:30 +01003179void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
3180{
3181 if (nb_args != 2) {
3182 return;
3183 }
3184 ringbuf_completion(rs, str);
3185}
3186
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003187void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
3188{
3189 size_t len;
3190
3191 if (nb_args != 2) {
3192 return;
3193 }
3194
3195 len = strlen(str);
3196 readline_set_completion_index(rs, len);
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003197 peripheral_device_del_completion(rs, str, len);
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003198}
3199
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003200void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003201{
3202 ObjectPropertyInfoList *list, *start;
3203 size_t len;
3204
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003205 if (nb_args != 2) {
3206 return;
3207 }
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003208 len = strlen(str);
3209 readline_set_completion_index(rs, len);
3210
3211 start = list = qmp_qom_list("/objects", NULL);
3212 while (list) {
3213 ObjectPropertyInfo *info = list->value;
3214
3215 if (!strncmp(info->type, "child<", 5)
3216 && !strncmp(info->name, str, len)) {
3217 readline_add_completion(rs, info->name);
3218 }
3219 list = list->next;
3220 }
3221 qapi_free_ObjectPropertyInfoList(start);
3222}
3223
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003224void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
3225{
3226 int i;
3227 char *sep;
3228 size_t len;
3229
3230 if (nb_args != 2) {
3231 return;
3232 }
3233 sep = strrchr(str, '-');
3234 if (sep) {
3235 str = sep + 1;
3236 }
3237 len = strlen(str);
3238 readline_set_completion_index(rs, len);
Eric Blake7fb1cf12015-11-18 01:52:57 -07003239 for (i = 0; i < Q_KEY_CODE__MAX; i++) {
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003240 if (!strncmp(str, QKeyCode_lookup[i], len)) {
3241 readline_add_completion(rs, QKeyCode_lookup[i]);
3242 }
3243 }
3244}
3245
Hani Benhabiles40d19392014-05-07 23:41:30 +01003246void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
3247{
3248 size_t len;
3249
3250 len = strlen(str);
3251 readline_set_completion_index(rs, len);
3252 if (nb_args == 2) {
Jason Wangeaed4832015-04-23 14:21:38 +08003253 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles40d19392014-05-07 23:41:30 +01003254 int count, i;
3255 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08003256 NET_CLIENT_OPTIONS_KIND_NONE,
3257 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003258 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles40d19392014-05-07 23:41:30 +01003259 const char *name = ncs[i]->name;
3260 if (!strncmp(str, name, len)) {
3261 readline_add_completion(rs, name);
3262 }
3263 }
3264 } else if (nb_args == 3) {
3265 add_completion_option(rs, str, "on");
3266 add_completion_option(rs, str, "off");
3267 }
3268}
3269
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003270void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
3271{
3272 int len, count, i;
Jason Wangeaed4832015-04-23 14:21:38 +08003273 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003274
3275 if (nb_args != 2) {
3276 return;
3277 }
3278
3279 len = strlen(str);
3280 readline_set_completion_index(rs, len);
3281 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_OPTIONS_KIND_NIC,
Jason Wangeaed4832015-04-23 14:21:38 +08003282 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003283 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003284 QemuOpts *opts;
3285 const char *name = ncs[i]->name;
3286 if (strncmp(str, name, len)) {
3287 continue;
3288 }
3289 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
3290 if (opts) {
3291 readline_add_completion(rs, name);
3292 }
3293 }
3294}
3295
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003296void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
3297{
3298 size_t len;
3299
3300 len = strlen(str);
3301 readline_set_completion_index(rs, len);
3302 if (nb_args == 2) {
3303 TraceEventID id;
3304 for (id = 0; id < trace_event_count(); id++) {
3305 const char *event_name = trace_event_get_name(trace_event_id(id));
3306 if (!strncmp(str, event_name, len)) {
3307 readline_add_completion(rs, event_name);
3308 }
3309 }
3310 } else if (nb_args == 3) {
3311 add_completion_option(rs, str, "on");
3312 add_completion_option(rs, str, "off");
3313 }
3314}
3315
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003316void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
3317{
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003318 int i;
3319
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003320 if (nb_args != 2) {
3321 return;
3322 }
3323 readline_set_completion_index(rs, strlen(str));
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003324 for (i = 0; WatchdogExpirationAction_lookup[i]; i++) {
3325 add_completion_option(rs, str, WatchdogExpirationAction_lookup[i]);
3326 }
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003327}
3328
Hani Benhabilesc68a0402014-05-27 23:39:32 +01003329void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
3330 const char *str)
3331{
3332 size_t len;
3333
3334 len = strlen(str);
3335 readline_set_completion_index(rs, len);
3336 if (nb_args == 2) {
3337 int i;
Eric Blake7fb1cf12015-11-18 01:52:57 -07003338 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Hani Benhabilesc68a0402014-05-27 23:39:32 +01003339 const char *name = MigrationCapability_lookup[i];
3340 if (!strncmp(str, name, len)) {
3341 readline_add_completion(rs, name);
3342 }
3343 }
3344 } else if (nb_args == 3) {
3345 add_completion_option(rs, str, "on");
3346 add_completion_option(rs, str, "off");
3347 }
3348}
3349
Liang Li50e9a622015-03-23 16:32:29 +08003350void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
3351 const char *str)
3352{
3353 size_t len;
3354
3355 len = strlen(str);
3356 readline_set_completion_index(rs, len);
3357 if (nb_args == 2) {
3358 int i;
Eric Blake7fb1cf12015-11-18 01:52:57 -07003359 for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
Liang Li50e9a622015-03-23 16:32:29 +08003360 const char *name = MigrationParameter_lookup[i];
3361 if (!strncmp(str, name, len)) {
3362 readline_add_completion(rs, name);
3363 }
3364 }
3365 }
3366}
3367
Hani Benhabilese3bb5322014-05-27 23:39:34 +01003368void host_net_add_completion(ReadLineState *rs, int nb_args, const char *str)
3369{
3370 int i;
3371 size_t len;
3372 if (nb_args != 2) {
3373 return;
3374 }
3375 len = strlen(str);
3376 readline_set_completion_index(rs, len);
3377 for (i = 0; host_net_devices[i]; i++) {
3378 if (!strncmp(host_net_devices[i], str, len)) {
3379 readline_add_completion(rs, host_net_devices[i]);
3380 }
3381 }
3382}
3383
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003384void host_net_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3385{
Jason Wangeaed4832015-04-23 14:21:38 +08003386 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003387 int count, i, len;
3388
3389 len = strlen(str);
3390 readline_set_completion_index(rs, len);
3391 if (nb_args == 2) {
3392 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08003393 NET_CLIENT_OPTIONS_KIND_NONE,
3394 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003395 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003396 int id;
3397 char name[16];
3398
3399 if (net_hub_id_for_client(ncs[i], &id)) {
3400 continue;
3401 }
3402 snprintf(name, sizeof(name), "%d", id);
3403 if (!strncmp(str, name, len)) {
3404 readline_add_completion(rs, name);
3405 }
3406 }
3407 return;
3408 } else if (nb_args == 3) {
3409 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08003410 NET_CLIENT_OPTIONS_KIND_NIC,
3411 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003412 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Jason Wang2c4681f2015-02-02 15:06:38 +08003413 int id;
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003414 const char *name;
3415
Jason Wang2c4681f2015-02-02 15:06:38 +08003416 if (ncs[i]->info->type == NET_CLIENT_OPTIONS_KIND_HUBPORT ||
3417 net_hub_id_for_client(ncs[i], &id)) {
3418 continue;
3419 }
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003420 name = ncs[i]->name;
3421 if (!strncmp(str, name, len)) {
3422 readline_add_completion(rs, name);
3423 }
3424 }
3425 return;
3426 }
3427}
3428
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003429static void vm_completion(ReadLineState *rs, const char *str)
3430{
3431 size_t len;
3432 BlockDriverState *bs = NULL;
3433
3434 len = strlen(str);
3435 readline_set_completion_index(rs, len);
3436 while ((bs = bdrv_next(bs))) {
3437 SnapshotInfoList *snapshots, *snapshot;
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003438 AioContext *ctx = bdrv_get_aio_context(bs);
3439 bool ok = false;
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003440
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003441 aio_context_acquire(ctx);
3442 if (bdrv_can_snapshot(bs)) {
3443 ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0;
3444 }
3445 aio_context_release(ctx);
3446 if (!ok) {
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003447 continue;
3448 }
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003449
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003450 snapshot = snapshots;
3451 while (snapshot) {
3452 char *completion = snapshot->value->name;
3453 if (!strncmp(str, completion, len)) {
3454 readline_add_completion(rs, completion);
3455 }
3456 completion = snapshot->value->id;
3457 if (!strncmp(str, completion, len)) {
3458 readline_add_completion(rs, completion);
3459 }
3460 snapshot = snapshot->next;
3461 }
3462 qapi_free_SnapshotInfoList(snapshots);
3463 }
3464
3465}
3466
3467void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
3468{
3469 if (nb_args == 2) {
3470 vm_completion(rs, str);
3471 }
3472}
3473
3474void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
3475{
3476 if (nb_args == 2) {
3477 vm_completion(rs, str);
3478 }
3479}
3480
Wenchao Xiac35b6402013-08-27 20:38:24 +08003481static void monitor_find_completion_by_table(Monitor *mon,
3482 const mon_cmd_t *cmd_table,
3483 char **args,
3484 int nb_args)
bellard81d09122004-07-14 17:21:37 +00003485{
3486 const char *cmdname;
Wenchao Xiac35b6402013-08-27 20:38:24 +08003487 int i;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003488 const char *ptype, *str, *name;
Anthony Liguoric227f092009-10-01 16:12:16 -05003489 const mon_cmd_t *cmd;
Max Reitzda27a002016-03-16 19:54:29 +01003490 BlockBackend *blk = NULL;
bellard81d09122004-07-14 17:21:37 +00003491
bellard81d09122004-07-14 17:21:37 +00003492 if (nb_args <= 1) {
3493 /* command completion */
3494 if (nb_args == 0)
3495 cmdname = "";
3496 else
3497 cmdname = args[0];
Wenchao Xiad2674b22013-08-27 20:38:16 +08003498 readline_set_completion_index(mon->rs, strlen(cmdname));
Wenchao Xiac35b6402013-08-27 20:38:24 +08003499 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003500 cmd_completion(mon, cmdname, cmd->name);
bellard81d09122004-07-14 17:21:37 +00003501 }
3502 } else {
3503 /* find the command */
Wenchao Xiac35b6402013-08-27 20:38:24 +08003504 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Jan Kiszka03a63482010-06-16 00:38:33 +02003505 if (compare_cmd(args[0], cmd->name)) {
3506 break;
3507 }
bellard81d09122004-07-14 17:21:37 +00003508 }
Jan Kiszka03a63482010-06-16 00:38:33 +02003509 if (!cmd->name) {
Wenchao Xiac35b6402013-08-27 20:38:24 +08003510 return;
Jan Kiszka03a63482010-06-16 00:38:33 +02003511 }
3512
Wenchao Xiad903a772013-08-27 20:38:25 +08003513 if (cmd->sub_table) {
3514 /* do the job again */
Stefan Weile7ae7712015-03-08 19:30:01 +01003515 monitor_find_completion_by_table(mon, cmd->sub_table,
3516 &args[1], nb_args - 1);
3517 return;
Wenchao Xiad903a772013-08-27 20:38:25 +08003518 }
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003519 if (cmd->command_completion) {
Stefan Weile7ae7712015-03-08 19:30:01 +01003520 cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
3521 return;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003522 }
Wenchao Xiad903a772013-08-27 20:38:25 +08003523
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003524 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003525 for(i = 0; i < nb_args - 2; i++) {
3526 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003527 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003528 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003529 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003530 }
3531 }
3532 str = args[nb_args - 1];
Kevin Wolf48fe86f2014-11-12 16:24:02 +01003533 while (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02003534 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00003535 }
bellard81d09122004-07-14 17:21:37 +00003536 switch(*ptype) {
3537 case 'F':
3538 /* file completion */
Wenchao Xiad2674b22013-08-27 20:38:16 +08003539 readline_set_completion_index(mon->rs, strlen(str));
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003540 file_completion(mon, str);
bellard81d09122004-07-14 17:21:37 +00003541 break;
3542 case 'B':
3543 /* block device name completion */
Wenchao Xia599a9262013-08-27 20:38:15 +08003544 readline_set_completion_index(mon->rs, strlen(str));
Max Reitzda27a002016-03-16 19:54:29 +01003545 while ((blk = blk_next(blk)) != NULL) {
3546 name = blk_name(blk);
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003547 if (str[0] == '\0' ||
3548 !strncmp(name, str, strlen(str))) {
3549 readline_add_completion(mon->rs, name);
3550 }
3551 }
bellard81d09122004-07-14 17:21:37 +00003552 break;
bellard7fe48482004-10-09 18:08:01 +00003553 case 's':
Wenchao Xia129be002013-08-27 20:38:26 +08003554 case 'S':
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003555 if (!strcmp(cmd->name, "help|?")) {
Wenchao Xia7ca0e062013-08-27 20:38:27 +08003556 monitor_find_completion_by_table(mon, cmd_table,
3557 &args[1], nb_args - 1);
bellard7fe48482004-10-09 18:08:01 +00003558 }
3559 break;
bellard81d09122004-07-14 17:21:37 +00003560 default:
3561 break;
3562 }
3563 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08003564}
3565
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003566static void monitor_find_completion(void *opaque,
Wenchao Xiac35b6402013-08-27 20:38:24 +08003567 const char *cmdline)
3568{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003569 Monitor *mon = opaque;
Wenchao Xiac35b6402013-08-27 20:38:24 +08003570 char *args[MAX_ARGS];
3571 int nb_args, len;
3572
3573 /* 1. parse the cmdline */
3574 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
3575 return;
3576 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08003577
3578 /* if the line ends with a space, it means we want to complete the
3579 next arg */
3580 len = strlen(cmdline);
3581 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
3582 if (nb_args >= MAX_ARGS) {
3583 goto cleanup;
3584 }
3585 args[nb_args++] = g_strdup("");
3586 }
3587
3588 /* 2. auto complete according to args */
3589 monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args);
Jan Kiszka03a63482010-06-16 00:38:33 +02003590
3591cleanup:
Wenchao Xiadcc70cd2013-08-27 20:38:22 +08003592 free_cmdline_args(args, nb_args);
bellard81d09122004-07-14 17:21:37 +00003593}
3594
aliguori731b0362009-03-05 23:01:42 +00003595static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003596{
aliguori731b0362009-03-05 23:01:42 +00003597 Monitor *mon = opaque;
3598
Jan Kiszkac62313b2009-12-04 14:05:29 +01003599 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003600}
3601
Markus Armbruster40861822015-05-29 10:27:16 +02003602static bool invalid_qmp_mode(const Monitor *mon, const mon_cmd_t *cmd,
3603 Error **errp)
Luiz Capitulino09069b12010-02-04 18:10:06 -02003604{
Markus Armbruster485febc2015-03-13 17:25:50 +01003605 bool is_cap = cmd->mhandler.cmd_new == qmp_capabilities;
Markus Armbrusterf994b252015-03-06 19:51:51 +01003606
3607 if (is_cap && mon->qmp.in_command_mode) {
Markus Armbruster40861822015-05-29 10:27:16 +02003608 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
3609 "Capabilities negotiation is already complete, command "
3610 "'%s' ignored", cmd->name);
Eric Blake2d5a8342015-04-15 09:19:23 -06003611 return true;
3612 }
Markus Armbrusterf994b252015-03-06 19:51:51 +01003613 if (!is_cap && !mon->qmp.in_command_mode) {
Markus Armbruster40861822015-05-29 10:27:16 +02003614 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
3615 "Expecting capabilities negotiation with "
3616 "'qmp_capabilities' before command '%s'", cmd->name);
Eric Blake2d5a8342015-04-15 09:19:23 -06003617 return true;
3618 }
3619 return false;
Luiz Capitulino09069b12010-02-04 18:10:06 -02003620}
3621
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003622/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03003623 * Argument validation rules:
3624 *
3625 * 1. The argument must exist in cmd_args qdict
3626 * 2. The argument type must be the expected one
3627 *
3628 * Special case: If the argument doesn't exist in cmd_args and
3629 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
3630 * checking is skipped for it.
3631 */
Markus Armbruster326283a2015-03-02 18:39:09 +01003632static void check_client_args_type(const QDict *client_args,
3633 const QDict *cmd_args, int flags,
3634 Error **errp)
Luiz Capitulino4af91932010-06-22 11:44:05 -03003635{
3636 const QDictEntry *ent;
3637
3638 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
3639 QObject *obj;
3640 QString *arg_type;
3641 const QObject *client_arg = qdict_entry_value(ent);
3642 const char *client_arg_name = qdict_entry_key(ent);
3643
3644 obj = qdict_get(cmd_args, client_arg_name);
3645 if (!obj) {
3646 if (flags & QMP_ACCEPT_UNKNOWNS) {
3647 /* handler accepts unknowns */
3648 continue;
3649 }
3650 /* client arg doesn't exist */
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003651 error_setg(errp, QERR_INVALID_PARAMETER, client_arg_name);
Markus Armbruster326283a2015-03-02 18:39:09 +01003652 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003653 }
3654
3655 arg_type = qobject_to_qstring(obj);
3656 assert(arg_type != NULL);
3657
3658 /* check if argument's type is correct */
3659 switch (qstring_get_str(arg_type)[0]) {
3660 case 'F':
3661 case 'B':
3662 case 's':
3663 if (qobject_type(client_arg) != QTYPE_QSTRING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003664 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3665 client_arg_name, "string");
Markus Armbruster326283a2015-03-02 18:39:09 +01003666 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003667 }
3668 break;
3669 case 'i':
3670 case 'l':
3671 case 'M':
Jes Sorensendbc0c672010-10-21 17:15:47 +02003672 case 'o':
Luiz Capitulino4af91932010-06-22 11:44:05 -03003673 if (qobject_type(client_arg) != QTYPE_QINT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003674 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3675 client_arg_name, "int");
Markus Armbruster326283a2015-03-02 18:39:09 +01003676 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003677 }
3678 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003679 case 'T':
3680 if (qobject_type(client_arg) != QTYPE_QINT &&
3681 qobject_type(client_arg) != QTYPE_QFLOAT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003682 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3683 client_arg_name, "number");
Markus Armbruster326283a2015-03-02 18:39:09 +01003684 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003685 }
3686 break;
3687 case 'b':
3688 case '-':
3689 if (qobject_type(client_arg) != QTYPE_QBOOL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003690 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3691 client_arg_name, "bool");
Markus Armbruster326283a2015-03-02 18:39:09 +01003692 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003693 }
3694 break;
3695 case 'O':
3696 assert(flags & QMP_ACCEPT_UNKNOWNS);
3697 break;
Paolo Bonzinib9f89782012-03-22 12:51:11 +01003698 case 'q':
3699 /* Any QObject can be passed. */
3700 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003701 case '/':
3702 case '.':
3703 /*
3704 * These types are not supported by QMP and thus are not
3705 * handled here. Fall through.
3706 */
3707 default:
3708 abort();
3709 }
3710 }
Luiz Capitulino4af91932010-06-22 11:44:05 -03003711}
3712
3713/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003714 * - Check if the client has passed all mandatory args
3715 * - Set special flags for argument validation
3716 */
Markus Armbruster326283a2015-03-02 18:39:09 +01003717static void check_mandatory_args(const QDict *cmd_args,
3718 const QDict *client_args, int *flags,
3719 Error **errp)
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003720{
3721 const QDictEntry *ent;
3722
3723 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
3724 const char *cmd_arg_name = qdict_entry_key(ent);
3725 QString *type = qobject_to_qstring(qdict_entry_value(ent));
3726 assert(type != NULL);
3727
3728 if (qstring_get_str(type)[0] == 'O') {
3729 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
3730 *flags |= QMP_ACCEPT_UNKNOWNS;
3731 } else if (qstring_get_str(type)[0] != '-' &&
3732 qstring_get_str(type)[1] != '?' &&
3733 !qdict_haskey(client_args, cmd_arg_name)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003734 error_setg(errp, QERR_MISSING_PARAMETER, cmd_arg_name);
Markus Armbruster326283a2015-03-02 18:39:09 +01003735 return;
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003736 }
3737 }
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003738}
3739
3740static QDict *qdict_from_args_type(const char *args_type)
3741{
3742 int i;
3743 QDict *qdict;
3744 QString *key, *type, *cur_qs;
3745
3746 assert(args_type != NULL);
3747
3748 qdict = qdict_new();
3749
3750 if (args_type == NULL || args_type[0] == '\0') {
3751 /* no args, empty qdict */
3752 goto out;
3753 }
3754
3755 key = qstring_new();
3756 type = qstring_new();
3757
3758 cur_qs = key;
3759
3760 for (i = 0;; i++) {
3761 switch (args_type[i]) {
3762 case ',':
3763 case '\0':
3764 qdict_put(qdict, qstring_get_str(key), type);
3765 QDECREF(key);
3766 if (args_type[i] == '\0') {
3767 goto out;
3768 }
3769 type = qstring_new(); /* qdict has ref */
3770 cur_qs = key = qstring_new();
3771 break;
3772 case ':':
3773 cur_qs = type;
3774 break;
3775 default:
3776 qstring_append_chr(cur_qs, args_type[i]);
3777 break;
3778 }
3779 }
3780
3781out:
3782 return qdict;
3783}
3784
3785/*
3786 * Client argument checking rules:
3787 *
3788 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03003789 * 2. Each argument provided by the client must be expected
3790 * 3. Each argument provided by the client must have the type expected
3791 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003792 */
Markus Armbruster326283a2015-03-02 18:39:09 +01003793static void qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args,
3794 Error **errp)
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003795{
Markus Armbruster326283a2015-03-02 18:39:09 +01003796 Error *err = NULL;
3797 int flags;
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003798 QDict *cmd_args;
3799
3800 cmd_args = qdict_from_args_type(cmd->args_type);
3801
3802 flags = 0;
Markus Armbruster326283a2015-03-02 18:39:09 +01003803 check_mandatory_args(cmd_args, client_args, &flags, &err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003804 if (err) {
3805 goto out;
3806 }
3807
Markus Armbruster326283a2015-03-02 18:39:09 +01003808 check_client_args_type(client_args, cmd_args, flags, &err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003809
3810out:
Markus Armbruster326283a2015-03-02 18:39:09 +01003811 error_propagate(errp, err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003812 QDECREF(cmd_args);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003813}
3814
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003815/*
3816 * Input object checking rules
3817 *
3818 * 1. Input object must be a dict
3819 * 2. The "execute" key must exist
3820 * 3. The "execute" key must be a string
3821 * 4. If the "arguments" key exists, it must be a dict
3822 * 5. If the "id" key exists, it can be anything (ie. json-value)
3823 * 6. Any argument not listed above is considered invalid
3824 */
Markus Armbrusterba0510a2015-03-02 18:41:43 +01003825static QDict *qmp_check_input_obj(QObject *input_obj, Error **errp)
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003826{
3827 const QDictEntry *ent;
3828 int has_exec_key = 0;
3829 QDict *input_dict;
3830
3831 if (qobject_type(input_obj) != QTYPE_QDICT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003832 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "object");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003833 return NULL;
3834 }
3835
3836 input_dict = qobject_to_qdict(input_obj);
3837
3838 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
3839 const char *arg_name = qdict_entry_key(ent);
3840 const QObject *arg_obj = qdict_entry_value(ent);
3841
3842 if (!strcmp(arg_name, "execute")) {
3843 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003844 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
3845 "execute", "string");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003846 return NULL;
3847 }
3848 has_exec_key = 1;
3849 } else if (!strcmp(arg_name, "arguments")) {
3850 if (qobject_type(arg_obj) != QTYPE_QDICT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003851 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
3852 "arguments", "object");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003853 return NULL;
3854 }
Markus Armbruster779cec42015-06-08 10:44:30 +02003855 } else if (!strcmp(arg_name, "id")) {
3856 /* Any string is acceptable as "id", so nothing to check */
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003857 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003858 error_setg(errp, QERR_QMP_EXTRA_MEMBER, arg_name);
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003859 return NULL;
3860 }
3861 }
3862
3863 if (!has_exec_key) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003864 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "execute");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003865 return NULL;
3866 }
3867
3868 return input_dict;
3869}
3870
Paolo Bonzini95385fe2015-11-25 22:23:31 +01003871static void handle_qmp_command(JSONMessageParser *parser, GQueue *tokens)
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003872{
Markus Armbruster326283a2015-03-02 18:39:09 +01003873 Error *local_err = NULL;
Markus Armbruster84add862015-03-05 16:45:15 +01003874 QObject *obj, *data;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003875 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003876 const mon_cmd_t *cmd;
Luiz Capitulino40e5a012011-10-21 16:15:31 -02003877 const char *cmd_name;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003878 Monitor *mon = cur_mon;
3879
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03003880 args = input = NULL;
Markus Armbruster84add862015-03-05 16:45:15 +01003881 data = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003882
3883 obj = json_parser_parse(tokens, NULL);
3884 if (!obj) {
3885 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003886 error_setg(&local_err, QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003887 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003888 }
3889
Markus Armbrusterba0510a2015-03-02 18:41:43 +01003890 input = qmp_check_input_obj(obj, &local_err);
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003891 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003892 qobject_decref(obj);
3893 goto err_out;
3894 }
3895
Markus Armbruster74358f22015-03-06 19:35:59 +01003896 mon->qmp.id = qdict_get(input, "id");
3897 qobject_incref(mon->qmp.id);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003898
Luiz Capitulino0bbab462010-05-31 17:32:50 -03003899 cmd_name = qdict_get_str(input, "execute");
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +01003900 trace_handle_qmp_command(mon, cmd_name);
Anthony Liguorie3193602011-09-02 12:34:47 -05003901 cmd = qmp_find_cmd(cmd_name);
Eric Blake2d5a8342015-04-15 09:19:23 -06003902 if (!cmd) {
Markus Armbruster710aec92015-03-06 11:28:00 +01003903 error_set(&local_err, ERROR_CLASS_COMMAND_NOT_FOUND,
3904 "The command %s has not been found", cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03003905 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003906 }
Markus Armbruster40861822015-05-29 10:27:16 +02003907 if (invalid_qmp_mode(mon, cmd, &local_err)) {
Eric Blake2d5a8342015-04-15 09:19:23 -06003908 goto err_out;
3909 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003910
3911 obj = qdict_get(input, "arguments");
3912 if (!obj) {
3913 args = qdict_new();
3914 } else {
3915 args = qobject_to_qdict(obj);
3916 QINCREF(args);
3917 }
3918
Markus Armbruster326283a2015-03-02 18:39:09 +01003919 qmp_check_client_args(cmd, args, &local_err);
3920 if (local_err) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003921 goto err_out;
3922 }
3923
Markus Armbruster485febc2015-03-13 17:25:50 +01003924 cmd->mhandler.cmd_new(args, &data, &local_err);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003925
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003926err_out:
Markus Armbruster710aec92015-03-06 11:28:00 +01003927 monitor_protocol_emitter(mon, data, local_err);
Markus Armbruster84add862015-03-05 16:45:15 +01003928 qobject_decref(data);
Markus Armbruster3a81a102015-10-29 12:15:09 +01003929 error_free(local_err);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03003930 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003931 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003932}
3933
Markus Armbrusterc83fe232015-03-06 19:20:51 +01003934static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003935{
3936 Monitor *old_mon = cur_mon;
3937
3938 cur_mon = opaque;
3939
Markus Armbruster74358f22015-03-06 19:35:59 +01003940 json_message_parser_feed(&cur_mon->qmp.parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003941
3942 cur_mon = old_mon;
3943}
3944
aliguori731b0362009-03-05 23:01:42 +00003945static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00003946{
aliguori731b0362009-03-05 23:01:42 +00003947 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00003948 int i;
aliguori376253e2009-03-05 23:01:23 +00003949
aliguori731b0362009-03-05 23:01:42 +00003950 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00003951
aliguoricde76ee2009-03-05 23:01:51 +00003952 if (cur_mon->rs) {
3953 for (i = 0; i < size; i++)
3954 readline_handle_byte(cur_mon->rs, buf[i]);
3955 } else {
3956 if (size == 0 || buf[size - 1] != 0)
3957 monitor_printf(cur_mon, "corrupted command\n");
3958 else
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003959 handle_hmp_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00003960 }
aliguori731b0362009-03-05 23:01:42 +00003961
3962 cur_mon = old_mon;
3963}
aliguorid8f44602008-10-06 13:52:44 +00003964
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003965static void monitor_command_cb(void *opaque, const char *cmdline,
3966 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00003967{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003968 Monitor *mon = opaque;
3969
aliguori731b0362009-03-05 23:01:42 +00003970 monitor_suspend(mon);
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003971 handle_hmp_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00003972 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00003973}
3974
aliguoricde76ee2009-03-05 23:01:51 +00003975int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003976{
aliguoricde76ee2009-03-05 23:01:51 +00003977 if (!mon->rs)
3978 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00003979 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00003980 return 0;
aliguorid8f44602008-10-06 13:52:44 +00003981}
3982
aliguori376253e2009-03-05 23:01:23 +00003983void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003984{
aliguoricde76ee2009-03-05 23:01:51 +00003985 if (!mon->rs)
3986 return;
aliguori731b0362009-03-05 23:01:42 +00003987 if (--mon->suspend_cnt == 0)
3988 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00003989}
3990
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003991static QObject *get_qmp_greeting(void)
3992{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03003993 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003994
Markus Armbruster7fad30f2015-09-16 13:06:19 +02003995 qmp_marshal_query_version(NULL, &ver, NULL);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003996 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
3997}
3998
Markus Armbrusterc83fe232015-03-06 19:20:51 +01003999static void monitor_qmp_event(void *opaque, int event)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004000{
Luiz Capitulino47116d12010-02-08 17:01:30 -02004001 QObject *data;
4002 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004003
Luiz Capitulino47116d12010-02-08 17:01:30 -02004004 switch (event) {
4005 case CHR_EVENT_OPENED:
Markus Armbrusterf994b252015-03-06 19:51:51 +01004006 mon->qmp.in_command_mode = false;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004007 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004008 monitor_json_emitter(mon, data);
4009 qobject_decref(data);
Corey Bryantefb87c12012-08-14 16:43:48 -04004010 mon_refcount++;
Luiz Capitulino47116d12010-02-08 17:01:30 -02004011 break;
4012 case CHR_EVENT_CLOSED:
Markus Armbruster74358f22015-03-06 19:35:59 +01004013 json_message_parser_destroy(&mon->qmp.parser);
4014 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Corey Bryantefb87c12012-08-14 16:43:48 -04004015 mon_refcount--;
4016 monitor_fdsets_cleanup();
Luiz Capitulino47116d12010-02-08 17:01:30 -02004017 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004018 }
4019}
4020
aliguori731b0362009-03-05 23:01:42 +00004021static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004022{
aliguori376253e2009-03-05 23:01:23 +00004023 Monitor *mon = opaque;
4024
aliguori2724b182009-03-05 23:01:47 +00004025 switch (event) {
4026 case CHR_EVENT_MUX_IN:
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02004027 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004028 mon->mux_out = 0;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02004029 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004030 if (mon->reset_seen) {
4031 readline_restart(mon->rs);
4032 monitor_resume(mon);
4033 monitor_flush(mon);
4034 } else {
4035 mon->suspend_cnt = 0;
4036 }
aliguori2724b182009-03-05 23:01:47 +00004037 break;
ths86e94de2007-01-05 22:01:59 +00004038
aliguori2724b182009-03-05 23:01:47 +00004039 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004040 if (mon->reset_seen) {
4041 if (mon->suspend_cnt == 0) {
4042 monitor_printf(mon, "\n");
4043 }
4044 monitor_flush(mon);
4045 monitor_suspend(mon);
4046 } else {
4047 mon->suspend_cnt++;
4048 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02004049 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004050 mon->mux_out = 1;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02004051 qemu_mutex_unlock(&mon->out_lock);
aliguori2724b182009-03-05 23:01:47 +00004052 break;
4053
Amit Shahb6b8df52009-10-07 18:31:16 +05304054 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004055 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4056 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004057 if (!mon->mux_out) {
Stratos Psomadakise5554e22014-09-15 15:34:57 +03004058 readline_restart(mon->rs);
aliguori2724b182009-03-05 23:01:47 +00004059 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004060 }
4061 mon->reset_seen = 1;
Corey Bryantefb87c12012-08-14 16:43:48 -04004062 mon_refcount++;
4063 break;
4064
4065 case CHR_EVENT_CLOSED:
4066 mon_refcount--;
4067 monitor_fdsets_cleanup();
aliguori2724b182009-03-05 23:01:47 +00004068 break;
4069 }
ths86e94de2007-01-05 22:01:59 +00004070}
4071
Wayne Xia816f8922011-10-12 11:32:41 +08004072static int
4073compare_mon_cmd(const void *a, const void *b)
4074{
4075 return strcmp(((const mon_cmd_t *)a)->name,
4076 ((const mon_cmd_t *)b)->name);
4077}
4078
4079static void sortcmdlist(void)
4080{
4081 int array_num;
4082 int elem_size = sizeof(mon_cmd_t);
4083
4084 array_num = sizeof(mon_cmds)/elem_size-1;
4085 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
4086
4087 array_num = sizeof(info_cmds)/elem_size-1;
4088 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
4089}
4090
aliguori76655d62009-03-06 20:27:37 +00004091
4092/*
4093 * Local variables:
4094 * c-indent-level: 4
4095 * c-basic-offset: 4
4096 * tab-width: 8
4097 * End:
4098 */
4099
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004100/* These functions just adapt the readline interface in a typesafe way. We
4101 * could cast function pointers but that discards compiler checks.
4102 */
Stefan Weild5d15072014-01-25 18:18:23 +01004103static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
4104 const char *fmt, ...)
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004105{
4106 va_list ap;
4107 va_start(ap, fmt);
4108 monitor_vprintf(opaque, fmt, ap);
4109 va_end(ap);
4110}
4111
4112static void monitor_readline_flush(void *opaque)
4113{
4114 monitor_flush(opaque);
4115}
4116
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004117static void __attribute__((constructor)) monitor_lock_init(void)
4118{
4119 qemu_mutex_init(&monitor_lock);
4120}
4121
aliguori731b0362009-03-05 23:01:42 +00004122void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004123{
aliguori731b0362009-03-05 23:01:42 +00004124 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004125 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004126
4127 if (is_first_init) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +02004128 monitor_qapi_event_init();
Wenchao Xiad0383172013-08-27 20:38:18 +08004129 sortcmdlist();
ths20d8a3e2007-02-18 17:04:49 +00004130 is_first_init = 0;
4131 }
aliguori87127162009-03-05 23:01:29 +00004132
Wenchao Xiab01fe892013-08-27 20:38:19 +08004133 mon = g_malloc(sizeof(*mon));
4134 monitor_data_init(mon);
ths20d8a3e2007-02-18 17:04:49 +00004135
aliguori87127162009-03-05 23:01:29 +00004136 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00004137 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004138 if (flags & MONITOR_USE_READLINE) {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004139 mon->rs = readline_init(monitor_readline_printf,
4140 monitor_readline_flush,
4141 mon,
4142 monitor_find_completion);
aliguoricde76ee2009-03-05 23:01:51 +00004143 monitor_read_command(mon, 0);
4144 }
aliguori87127162009-03-05 23:01:29 +00004145
Markus Armbruster9f3982f2015-03-06 19:56:38 +01004146 if (monitor_is_qmp(mon)) {
Markus Armbrusterc83fe232015-03-06 19:20:51 +01004147 qemu_chr_add_handlers(chr, monitor_can_read, monitor_qmp_read,
4148 monitor_qmp_event, mon);
Anthony Liguori15f31512011-08-15 11:17:35 -05004149 qemu_chr_fe_set_echo(chr, true);
Markus Armbruster74358f22015-03-06 19:35:59 +01004150 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004151 } else {
4152 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4153 monitor_event, mon);
4154 }
aliguori87127162009-03-05 23:01:29 +00004155
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004156 qemu_mutex_lock(&monitor_lock);
Blue Swirl72cf2d42009-09-12 07:36:22 +00004157 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004158 qemu_mutex_unlock(&monitor_lock);
bellard7e2515e2004-08-01 21:52:19 +00004159}
4160
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004161static void bdrv_password_cb(void *opaque, const char *password,
4162 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00004163{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004164 Monitor *mon = opaque;
4165 BlockDriverState *bs = readline_opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004166 int ret = 0;
Markus Armbruster4d2855a2015-01-29 10:37:00 +01004167 Error *local_err = NULL;
bellard7e2515e2004-08-01 21:52:19 +00004168
Markus Armbruster4d2855a2015-01-29 10:37:00 +01004169 bdrv_add_key(bs, password, &local_err);
4170 if (local_err) {
Markus Armbruster193227f2015-12-18 16:35:06 +01004171 error_report_err(local_err);
aliguoribb5fc202009-03-05 23:01:15 +00004172 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004173 }
aliguori731b0362009-03-05 23:01:42 +00004174 if (mon->password_completion_cb)
4175 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004176
aliguori731b0362009-03-05 23:01:42 +00004177 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004178}
aliguoric0f4ce72009-03-05 23:01:01 +00004179
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004180int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
Markus Armbruster097310b2014-10-07 13:59:15 +02004181 BlockCompletionFunc *completion_cb,
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004182 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004183{
aliguoricde76ee2009-03-05 23:01:51 +00004184 int err;
4185
aliguori376253e2009-03-05 23:01:23 +00004186 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4187 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00004188
aliguori731b0362009-03-05 23:01:42 +00004189 mon->password_completion_cb = completion_cb;
4190 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004191
aliguoricde76ee2009-03-05 23:01:51 +00004192 err = monitor_read_password(mon, bdrv_password_cb, bs);
4193
4194 if (err && completion_cb)
4195 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004196
4197 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00004198}
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004199
4200int monitor_read_block_device_key(Monitor *mon, const char *device,
Markus Armbruster097310b2014-10-07 13:59:15 +02004201 BlockCompletionFunc *completion_cb,
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004202 void *opaque)
4203{
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01004204 Error *err = NULL;
Fam Zheng55606252015-03-02 19:36:46 +08004205 BlockBackend *blk;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004206
Fam Zheng55606252015-03-02 19:36:46 +08004207 blk = blk_by_name(device);
4208 if (!blk) {
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004209 monitor_printf(mon, "Device not found %s\n", device);
4210 return -1;
4211 }
Max Reitz5433c242015-10-19 17:53:29 +02004212 if (!blk_bs(blk)) {
4213 monitor_printf(mon, "Device '%s' has no medium\n", device);
4214 return -1;
4215 }
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004216
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01004217 bdrv_add_key(blk_bs(blk), NULL, &err);
4218 if (err) {
4219 error_free(err);
4220 return monitor_read_bdrv_key_start(mon, blk_bs(blk), completion_cb, opaque);
4221 }
4222
4223 if (completion_cb) {
4224 completion_cb(opaque, 0);
4225 }
4226 return 0;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004227}
Paolo Bonzini4d454572012-11-26 16:03:42 +01004228
4229QemuOptsList qemu_mon_opts = {
4230 .name = "mon",
4231 .implied_opt_name = "chardev",
4232 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
4233 .desc = {
4234 {
4235 .name = "mode",
4236 .type = QEMU_OPT_STRING,
4237 },{
4238 .name = "chardev",
4239 .type = QEMU_OPT_STRING,
4240 },{
4241 .name = "default",
4242 .type = QEMU_OPT_BOOL,
4243 },{
4244 .name = "pretty",
4245 .type = QEMU_OPT_BOOL,
4246 },
4247 { /* end of list */ }
4248 },
4249};
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004250
4251#ifndef TARGET_I386
4252void qmp_rtc_reset_reinjection(Error **errp)
4253{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004254 error_setg(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection");
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004255}
4256#endif
Jason J. Herne7ee0c3e2015-06-26 14:03:16 -04004257
4258#ifndef TARGET_S390X
4259void qmp_dump_skeys(const char *filename, Error **errp)
4260{
4261 error_setg(errp, QERR_FEATURE_DISABLED, "dump-skeys");
4262}
4263#endif
Peter Xuae50a772016-03-30 17:27:24 +01004264
4265#ifndef TARGET_ARM
4266GICCapabilityList *qmp_query_gic_capabilities(Error **errp)
4267{
4268 error_setg(errp, QERR_FEATURE_DISABLED, "query-gic-capabilities");
4269 return NULL;
4270}
4271#endif