blob: 07a0f763829275841b2f59e6f69dd941b77b5418 [file] [log] [blame]
bellard9dc39cb2004-03-14 21:38:27 +00001/*
2 * QEMU monitor
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard9dc39cb2004-03-14 21:38:27 +00004 * Copyright (c) 2003-2004 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard9dc39cb2004-03-14 21:38:27 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
blueswir1511d2b12009-03-07 15:32:56 +000024#include <dirent.h>
pbrook87ecb682007-11-17 17:14:51 +000025#include "hw/hw.h"
Paolo Bonzinib4a42f82013-02-04 11:37:52 +010026#include "monitor/qdev.h"
pbrook87ecb682007-11-17 17:14:51 +000027#include "hw/usb.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010028#include "hw/i386/pc.h"
Michael S. Tsirkina2cb15b2012-12-12 14:24:50 +020029#include "hw/pci/pci.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010030#include "sysemu/watchdog.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +020031#include "hw/loader.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010032#include "exec/gdbstub.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020033#include "net/net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000034#include "net/slirp.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020035#include "sysemu/char.h"
Gerd Hoffmann75721502010-10-07 12:22:54 +020036#include "ui/qemu-spice.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010037#include "sysemu/sysemu.h"
Eduardo Habkoste35704b2015-02-08 16:51:16 -020038#include "sysemu/numa.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010039#include "monitor/monitor.h"
Stefan Hajnoczi0150cd82013-11-14 11:54:15 +010040#include "qemu/readline.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010041#include "ui/console.h"
Gerd Hoffmannc751a742013-12-04 15:02:28 +010042#include "ui/input.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010043#include "sysemu/blockdev.h"
pbrook87ecb682007-11-17 17:14:51 +000044#include "audio/audio.h"
Paolo Bonzini76cad712012-10-24 11:12:21 +020045#include "disas/disas.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010046#include "sysemu/balloon.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010047#include "qemu/timer.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010048#include "migration/migration.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010049#include "sysemu/kvm.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010050#include "qemu/acl.h"
Paolo Bonzinibdee56f2013-04-02 18:28:41 +020051#include "sysemu/tpm.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010052#include "qapi/qmp/qerror.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010053#include "qapi/qmp/qint.h"
54#include "qapi/qmp/qfloat.h"
55#include "qapi/qmp/qlist.h"
56#include "qapi/qmp/qbool.h"
57#include "qapi/qmp/qstring.h"
58#include "qapi/qmp/qjson.h"
59#include "qapi/qmp/json-streamer.h"
60#include "qapi/qmp/json-parser.h"
Hani Benhabiles1094fd32014-02-06 23:30:13 +010061#include <qom/object_interfaces.h>
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010062#include "qemu/osdep.h"
Blue Swirl2b41f102011-06-19 20:38:22 +000063#include "cpu.h"
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +010064#include "trace.h"
Lluís31965ae2011-08-31 20:31:24 +020065#include "trace/control.h"
Pavel Butsykinbf957282015-09-10 18:38:59 +030066#include "monitor/hmp-target.h"
Lluís6d8a7642011-08-31 20:30:43 +020067#ifdef CONFIG_TRACE_SIMPLE
Lluís31965ae2011-08-31 20:31:24 +020068#include "trace/simple.h"
Prerna Saxena22890ab2010-06-24 17:04:53 +053069#endif
Paolo Bonzini022c62c2012-12-17 18:19:49 +010070#include "exec/memory.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050071#include "qmp-commands.h"
72#include "hmp.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010073#include "qemu/thread.h"
Hani Benhabilesb21631f2014-05-27 23:39:37 +010074#include "block/qapi.h"
Wenchao Xia43a14cf2014-06-18 08:43:31 +020075#include "qapi/qmp-event.h"
76#include "qapi-event.h"
Markus Armbruster39a18152015-09-16 13:06:28 +020077#include "qmp-introspect.h"
Fam Zheng55606252015-03-02 19:36:46 +080078#include "sysemu/block-backend.h"
ths6a5bd302007-12-03 17:05:38 +000079
Markus Armbruster1ce6be22015-02-06 14:18:24 +010080/* for hmp_info_irq/pic */
Jan Kiszka661f1922011-10-16 11:53:13 +020081#if defined(TARGET_SPARC)
Paolo Bonzini0d09e412013-02-05 17:06:20 +010082#include "hw/sparc/sun4m.h"
Jan Kiszka661f1922011-10-16 11:53:13 +020083#endif
Paolo Bonzini0d09e412013-02-05 17:06:20 +010084#include "hw/lm32/lm32_pic.h"
Jan Kiszka661f1922011-10-16 11:53:13 +020085
Jason J. Hernea4538a52015-06-26 14:07:21 -040086#if defined(TARGET_S390X)
87#include "hw/s390x/storage-keys.h"
88#endif
89
bellard9307c4c2004-04-04 12:57:25 +000090/*
91 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000092 *
bellard9307c4c2004-04-04 12:57:25 +000093 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000094 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000095 * 's' string (accept optional quote)
Wenchao Xia129be002013-08-27 20:38:26 +080096 * 'S' it just appends the rest of the string (accept optional quote)
Markus Armbruster361127d2010-02-10 20:24:35 +010097 * 'O' option string of the form NAME=VALUE,...
98 * parsed according to QemuOptsList given by its name
99 * Example: 'device:O' uses qemu_device_opts.
100 * Restriction: only lists with empty desc are supported
101 * TODO lift the restriction
bellard92a31b12005-02-10 22:00:52 +0000102 * 'i' 32 bit integer
103 * 'l' target long (32 or 64 bit)
Luiz Capitulino91162842012-04-26 17:34:30 -0300104 * 'M' Non-negative target long (32 or 64 bit), in user mode the
105 * value is multiplied by 2^20 (think Mebibyte)
Jes Sorensendbc0c672010-10-21 17:15:47 +0200106 * 'o' octets (aka bytes)
Kevin Wolf5e009842013-06-05 14:19:27 +0200107 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
108 * K, k suffix, which multiplies the value by 2^60 for suffixes E
109 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
110 * 2^30 for suffixes G and g, 2^20 for M and m, 2^10 for K and k
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +0100111 * 'T' double
112 * user mode accepts an optional ms, us, ns suffix,
113 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +0000114 * '/' optional gdb-like print format (like "/10x")
115 *
Luiz Capitulinofb466602009-08-28 15:27:27 -0300116 * '?' optional type (for all types, except '/')
117 * '.' other form of optional type (for 'i' and 'l')
Markus Armbruster942cd1f2010-03-26 09:07:09 +0100118 * 'b' boolean
119 * user mode accepts "on" or "off"
Luiz Capitulinofb466602009-08-28 15:27:27 -0300120 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +0000121 *
122 */
123
Anthony Liguoric227f092009-10-01 16:12:16 -0500124typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +0000125 const char *name;
bellard9307c4c2004-04-04 12:57:25 +0000126 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +0000127 const char *params;
128 const char *help;
Luiz Capitulino910df892009-10-07 13:41:51 -0300129 union {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -0300130 void (*cmd)(Monitor *mon, const QDict *qdict);
Markus Armbruster485febc2015-03-13 17:25:50 +0100131 void (*cmd_new)(QDict *params, QObject **ret_data, Error **errp);
Luiz Capitulino910df892009-10-07 13:41:51 -0300132 } mhandler;
Wenchao Xia5f3d3352013-01-14 14:06:27 +0800133 /* @sub_table is a list of 2nd level of commands. If it do not exist,
134 * mhandler should be used. If it exist, sub_table[?].mhandler should be
135 * used, and mhandler of 1st level plays the role of help function.
136 */
137 struct mon_cmd_t *sub_table;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +0100138 void (*command_completion)(ReadLineState *rs, int nb_args, const char *str);
Anthony Liguoric227f092009-10-01 16:12:16 -0500139} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000140
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100141/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500142typedef struct mon_fd_t mon_fd_t;
143struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100144 char *name;
145 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500146 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100147};
148
Corey Bryantba1c0482012-08-14 16:43:43 -0400149/* file descriptor associated with a file descriptor set */
150typedef struct MonFdsetFd MonFdsetFd;
151struct MonFdsetFd {
152 int fd;
153 bool removed;
154 char *opaque;
155 QLIST_ENTRY(MonFdsetFd) next;
156};
157
158/* file descriptor set containing fds passed via SCM_RIGHTS */
159typedef struct MonFdset MonFdset;
160struct MonFdset {
161 int64_t id;
162 QLIST_HEAD(, MonFdsetFd) fds;
Corey Bryantadb696f2012-08-14 16:43:47 -0400163 QLIST_HEAD(, MonFdsetFd) dup_fds;
Corey Bryantba1c0482012-08-14 16:43:43 -0400164 QLIST_ENTRY(MonFdset) next;
165};
166
Markus Armbruster74358f22015-03-06 19:35:59 +0100167typedef struct {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200168 QObject *id;
169 JSONMessageParser parser;
Markus Armbrusterf994b252015-03-06 19:51:51 +0100170 /*
171 * When a client connects, we're in capabilities negotiation mode.
172 * When command qmp_capabilities succeeds, we go into command
173 * mode.
174 */
175 bool in_command_mode; /* are we in command mode? */
Markus Armbruster74358f22015-03-06 19:35:59 +0100176} MonitorQMP;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200177
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100178/*
179 * To prevent flooding clients, events can be throttled. The
180 * throttling is calculated globally, rather than per-Monitor
181 * instance.
182 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200183typedef struct MonitorQAPIEventState {
184 QAPIEvent event; /* Event being tracked */
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100185 QEMUTimer *timer; /* Timer for handling delayed events */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200186 QDict *qdict; /* Delayed event (if any) */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200187} MonitorQAPIEventState;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100188
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200189typedef struct {
190 int64_t rate; /* Minimum time (in ns) between two events */
191} MonitorQAPIEventConf;
192
aliguori87127162009-03-05 23:01:29 +0000193struct Monitor {
194 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200195 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000196 int flags;
197 int suspend_cnt;
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400198 bool skip_flush;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200199
200 QemuMutex out_lock;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400201 QString *outbuf;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200202 guint out_watch;
203
204 /* Read under either BQL or out_lock, written with BQL+out_lock. */
205 int mux_out;
206
aliguori731b0362009-03-05 23:01:42 +0000207 ReadLineState *rs;
Markus Armbruster74358f22015-03-06 19:35:59 +0100208 MonitorQMP qmp;
Andreas Färbercb446ec2013-05-01 14:24:52 +0200209 CPUState *mon_cpu;
Markus Armbruster097310b2014-10-07 13:59:15 +0200210 BlockCompletionFunc *password_completion_cb;
aliguori731b0362009-03-05 23:01:42 +0000211 void *password_opaque;
Wenchao Xia77172392013-08-27 20:38:20 +0800212 mon_cmd_t *cmd_table;
Anthony Liguoric227f092009-10-01 16:12:16 -0500213 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000214 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000215};
216
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300217/* QMP checker flags */
218#define QMP_ACCEPT_UNKNOWNS 1
219
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200220/* Protects mon_list, monitor_event_state. */
221static QemuMutex monitor_lock;
222
Blue Swirl72cf2d42009-09-12 07:36:22 +0000223static QLIST_HEAD(mon_list, Monitor) mon_list;
Corey Bryantba1c0482012-08-14 16:43:43 -0400224static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
Corey Bryantefb87c12012-08-14 16:43:48 -0400225static int mon_refcount;
bellard7e2515e2004-08-01 21:52:19 +0000226
Wayne Xia816f8922011-10-12 11:32:41 +0800227static mon_cmd_t mon_cmds[];
228static mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000229
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300230static const mon_cmd_t qmp_cmds[];
231
Markus Armbruster8631b602010-02-18 11:41:55 +0100232Monitor *cur_mon;
aliguori376253e2009-03-05 23:01:23 +0000233
Stefan Hajnoczic60bf332013-11-14 11:54:14 +0100234static void monitor_command_cb(void *opaque, const char *cmdline,
235 void *readline_opaque);
aliguori83ab7952008-08-19 14:44:22 +0000236
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100237/**
238 * Is @mon a QMP monitor?
239 */
240static inline bool monitor_is_qmp(const Monitor *mon)
Luiz Capitulino418173c2009-11-26 22:58:51 -0200241{
242 return (mon->flags & MONITOR_USE_CONTROL);
243}
244
Markus Armbruster489653b2015-03-06 20:01:05 +0100245/**
246 * Is the current monitor, if any, a QMP monitor?
247 */
248bool monitor_cur_is_qmp(void)
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100249{
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100250 return cur_mon && monitor_is_qmp(cur_mon);
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100251}
252
Anthony Liguori7060b472011-09-02 12:34:50 -0500253void monitor_read_command(Monitor *mon, int show_prompt)
aliguori731b0362009-03-05 23:01:42 +0000254{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200255 if (!mon->rs)
256 return;
257
aliguori731b0362009-03-05 23:01:42 +0000258 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
259 if (show_prompt)
260 readline_show_prompt(mon->rs);
261}
bellard6a00d602005-11-21 23:25:50 +0000262
Anthony Liguori7060b472011-09-02 12:34:50 -0500263int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
264 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000265{
Markus Armbrusterbcf5d192015-03-12 17:26:46 +0100266 if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000267 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
268 /* prompt is printed on return from the command handler */
269 return 0;
270 } else {
271 monitor_printf(mon, "terminal does not support password prompting\n");
272 return -ENOTTY;
273 }
aliguoribb5fc202009-03-05 23:01:15 +0000274}
275
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200276static void monitor_flush_locked(Monitor *mon);
277
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100278static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
279 void *opaque)
280{
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200281 Monitor *mon = opaque;
282
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200283 qemu_mutex_lock(&mon->out_lock);
284 mon->out_watch = 0;
285 monitor_flush_locked(mon);
286 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100287 return FALSE;
288}
289
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200290/* Called with mon->out_lock held. */
291static void monitor_flush_locked(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000292{
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100293 int rc;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400294 size_t len;
295 const char *buf;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100296
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400297 if (mon->skip_flush) {
298 return;
299 }
300
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400301 buf = qstring_get_str(mon->outbuf);
302 len = qstring_get_length(mon->outbuf);
303
Paolo Bonzinia4cc73d2013-05-31 14:00:27 +0200304 if (len && !mon->mux_out) {
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400305 rc = qemu_chr_fe_write(mon->chr, (const uint8_t *) buf, len);
Stratos Psomadakis056f49f2014-01-27 12:30:15 +0200306 if ((rc < 0 && errno != EAGAIN) || (rc == len)) {
307 /* all flushed or error */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400308 QDECREF(mon->outbuf);
309 mon->outbuf = qstring_new();
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100310 return;
311 }
312 if (rc > 0) {
313 /* partinal write */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400314 QString *tmp = qstring_from_str(buf + rc);
315 QDECREF(mon->outbuf);
316 mon->outbuf = tmp;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100317 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200318 if (mon->out_watch == 0) {
Roger Pau Monnee02bc6d2014-05-23 17:57:49 +0200319 mon->out_watch = qemu_chr_fe_add_watch(mon->chr, G_IO_OUT|G_IO_HUP,
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200320 monitor_unblocked, mon);
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200321 }
bellard7e2515e2004-08-01 21:52:19 +0000322 }
323}
324
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200325void monitor_flush(Monitor *mon)
326{
327 qemu_mutex_lock(&mon->out_lock);
328 monitor_flush_locked(mon);
329 qemu_mutex_unlock(&mon->out_lock);
330}
331
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400332/* flush at every end of line */
aliguori376253e2009-03-05 23:01:23 +0000333static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000334{
ths60fe76f2007-12-16 03:02:09 +0000335 char c;
aliguori731b0362009-03-05 23:01:42 +0000336
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200337 qemu_mutex_lock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000338 for(;;) {
339 c = *str++;
340 if (c == '\0')
341 break;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400342 if (c == '\n') {
343 qstring_append_chr(mon->outbuf, '\r');
344 }
345 qstring_append_chr(mon->outbuf, c);
346 if (c == '\n') {
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200347 monitor_flush_locked(mon);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400348 }
bellard7e2515e2004-08-01 21:52:19 +0000349 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200350 qemu_mutex_unlock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000351}
352
aliguori376253e2009-03-05 23:01:23 +0000353void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000354{
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400355 char *buf;
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200356
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200357 if (!mon)
358 return;
359
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100360 if (monitor_is_qmp(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200361 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200362 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200363
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400364 buf = g_strdup_vprintf(fmt, ap);
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200365 monitor_puts(mon, buf);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400366 g_free(buf);
bellard7e2515e2004-08-01 21:52:19 +0000367}
368
aliguori376253e2009-03-05 23:01:23 +0000369void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000370{
371 va_list ap;
372 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000373 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000374 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000375}
376
Pavel Butsykincaf15312015-09-22 16:18:17 +0300377int monitor_fprintf(FILE *stream, const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000378{
379 va_list ap;
380 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000381 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000382 va_end(ap);
383 return 0;
384}
385
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200386static void monitor_json_emitter(Monitor *mon, const QObject *data)
387{
388 QString *json;
389
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200390 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
391 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200392 assert(json != NULL);
393
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200394 qstring_append_chr(json, '\n');
395 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200396
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200397 QDECREF(json);
398}
399
Markus Armbruster710aec92015-03-06 11:28:00 +0100400static QDict *build_qmp_error_dict(Error *err)
Luiz Capitulinode253f12012-07-27 16:18:16 -0300401{
402 QObject *obj;
403
Markus Armbruster710aec92015-03-06 11:28:00 +0100404 obj = qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %s } }",
405 ErrorClass_lookup[error_get_class(err)],
406 error_get_pretty(err));
Luiz Capitulinode253f12012-07-27 16:18:16 -0300407
408 return qobject_to_qdict(obj);
409}
410
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100411static void monitor_protocol_emitter(Monitor *mon, QObject *data,
Markus Armbruster710aec92015-03-06 11:28:00 +0100412 Error *err)
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200413{
414 QDict *qmp;
415
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +0100416 trace_monitor_protocol_emitter(mon);
417
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100418 if (!err) {
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200419 /* success response */
Luiz Capitulinode253f12012-07-27 16:18:16 -0300420 qmp = qdict_new();
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200421 if (data) {
422 qobject_incref(data);
423 qdict_put_obj(qmp, "return", data);
424 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200425 /* return an empty QDict by default */
426 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200427 }
428 } else {
429 /* error response */
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100430 qmp = build_qmp_error_dict(err);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200431 }
432
Markus Armbruster74358f22015-03-06 19:35:59 +0100433 if (mon->qmp.id) {
434 qdict_put_obj(qmp, "id", mon->qmp.id);
435 mon->qmp.id = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200436 }
437
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200438 monitor_json_emitter(mon, QOBJECT(qmp));
439 QDECREF(qmp);
440}
441
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200442
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200443static MonitorQAPIEventConf monitor_qapi_event_conf[QAPI_EVENT_MAX] = {
444 /* Limit guest-triggerable events to 1 per second */
445 [QAPI_EVENT_RTC_CHANGE] = { 1000 * SCALE_MS },
446 [QAPI_EVENT_WATCHDOG] = { 1000 * SCALE_MS },
447 [QAPI_EVENT_BALLOON_CHANGE] = { 1000 * SCALE_MS },
448 [QAPI_EVENT_QUORUM_REPORT_BAD] = { 1000 * SCALE_MS },
449 [QAPI_EVENT_QUORUM_FAILURE] = { 1000 * SCALE_MS },
450 [QAPI_EVENT_VSERPORT_CHANGE] = { 1000 * SCALE_MS },
451};
452
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200453static MonitorQAPIEventState monitor_qapi_event_state[QAPI_EVENT_MAX];
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100454
455/*
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200456 * Emits the event to every monitor instance, @event is only used for trace
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200457 * Called with monitor_lock held.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100458 */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200459static void monitor_qapi_event_emit(QAPIEvent event, QDict *qdict)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100460{
461 Monitor *mon;
462
Markus Armbruster688b4b72015-10-15 17:08:30 +0200463 trace_monitor_protocol_event_emit(event, qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100464 QLIST_FOREACH(mon, &mon_list, entry) {
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100465 if (monitor_is_qmp(mon) && mon->qmp.in_command_mode) {
Markus Armbruster688b4b72015-10-15 17:08:30 +0200466 monitor_json_emitter(mon, QOBJECT(qdict));
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100467 }
468 }
469}
470
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100471/*
472 * Queue a new event for emission to Monitor instances,
473 * applying any rate limiting if required.
474 */
475static void
Markus Armbruster688b4b72015-10-15 17:08:30 +0200476monitor_qapi_event_queue(QAPIEvent event, QDict *qdict, Error **errp)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100477{
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200478 MonitorQAPIEventConf *evconf;
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200479 MonitorQAPIEventState *evstate;
Markus Armbruster93f8f982015-10-15 17:08:31 +0200480
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200481 assert(event < QAPI_EVENT_MAX);
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200482 evconf = &monitor_qapi_event_conf[event];
Markus Armbruster93f8f982015-10-15 17:08:31 +0200483 evstate = &monitor_qapi_event_state[event];
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200484 trace_monitor_protocol_event_queue(event, qdict, evconf->rate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100485
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200486 qemu_mutex_lock(&monitor_lock);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200487
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200488 if (!evconf->rate) {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200489 /* Unthrottled event */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200490 monitor_qapi_event_emit(event, qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100491 } else {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200492 if (timer_pending(evstate->timer)) {
493 /*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200494 * Timer is pending for (at least) evconf->rate ns after
Markus Armbruster93f8f982015-10-15 17:08:31 +0200495 * last send. Store event for sending when timer fires,
496 * replacing a prior stored event if any.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100497 */
Markus Armbruster93f8f982015-10-15 17:08:31 +0200498 QDECREF(evstate->qdict);
Markus Armbruster688b4b72015-10-15 17:08:30 +0200499 evstate->qdict = qdict;
500 QINCREF(evstate->qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100501 } else {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200502 /*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200503 * Last send was (at least) evconf->rate ns ago.
Markus Armbruster93f8f982015-10-15 17:08:31 +0200504 * Send immediately, and arm the timer to call
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200505 * monitor_qapi_event_handler() in evconf->rate ns. Any
Markus Armbruster93f8f982015-10-15 17:08:31 +0200506 * events arriving before then will be delayed until then.
507 */
508 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
509
Markus Armbruster688b4b72015-10-15 17:08:30 +0200510 monitor_qapi_event_emit(event, qdict);
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200511 timer_mod_ns(evstate->timer, now + evconf->rate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100512 }
513 }
Markus Armbruster93f8f982015-10-15 17:08:31 +0200514
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200515 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100516}
517
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100518/*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200519 * This function runs evconf->rate ns after sending a throttled
Markus Armbruster93f8f982015-10-15 17:08:31 +0200520 * event.
521 * If another event has since been stored, send it.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100522 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200523static void monitor_qapi_event_handler(void *opaque)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100524{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200525 MonitorQAPIEventState *evstate = opaque;
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200526 MonitorQAPIEventConf *evconf = &monitor_qapi_event_conf[evstate->event];
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100527
Markus Armbruster93f8f982015-10-15 17:08:31 +0200528 trace_monitor_protocol_event_handler(evstate->event, evstate->qdict);
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200529 qemu_mutex_lock(&monitor_lock);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200530
Markus Armbruster688b4b72015-10-15 17:08:30 +0200531 if (evstate->qdict) {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200532 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
533
Markus Armbruster688b4b72015-10-15 17:08:30 +0200534 monitor_qapi_event_emit(evstate->event, evstate->qdict);
535 QDECREF(evstate->qdict);
536 evstate->qdict = NULL;
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200537 timer_mod_ns(evstate->timer, now + evconf->rate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100538 }
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
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200543static void monitor_qapi_event_init(void)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100544{
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200545 int i;
546 MonitorQAPIEventState *evstate;
547
548 for (i = 0; i < QAPI_EVENT_MAX; i++) {
549 if (monitor_qapi_event_conf[i].rate) {
550 evstate = &monitor_qapi_event_state[i];
551 evstate->event = i;
552 evstate->qdict = NULL;
553 evstate->timer = timer_new_ns(QEMU_CLOCK_REALTIME,
554 monitor_qapi_event_handler,
555 evstate);
556 }
557 }
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100558
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200559 qmp_event_set_func_emit(monitor_qapi_event_queue);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200560}
561
Markus Armbruster485febc2015-03-13 17:25:50 +0100562static void qmp_capabilities(QDict *params, QObject **ret_data, Error **errp)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200563{
Markus Armbruster485febc2015-03-13 17:25:50 +0100564 cur_mon->qmp.in_command_mode = true;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200565}
566
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100567static void handle_hmp_command(Monitor *mon, const char *cmdline);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200568
Wenchao Xiab01fe892013-08-27 20:38:19 +0800569static void monitor_data_init(Monitor *mon)
570{
571 memset(mon, 0, sizeof(Monitor));
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200572 qemu_mutex_init(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800573 mon->outbuf = qstring_new();
Wenchao Xia77172392013-08-27 20:38:20 +0800574 /* Use *mon_cmds by default. */
575 mon->cmd_table = mon_cmds;
Wenchao Xiab01fe892013-08-27 20:38:19 +0800576}
577
578static void monitor_data_destroy(Monitor *mon)
579{
580 QDECREF(mon->outbuf);
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200581 qemu_mutex_destroy(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800582}
583
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200584char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
585 int64_t cpu_index, Error **errp)
Luiz Capitulino0268d972010-10-22 10:08:02 -0200586{
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200587 char *output = NULL;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200588 Monitor *old_mon, hmp;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200589
Wenchao Xiab01fe892013-08-27 20:38:19 +0800590 monitor_data_init(&hmp);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400591 hmp.skip_flush = true;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200592
593 old_mon = cur_mon;
594 cur_mon = &hmp;
595
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200596 if (has_cpu_index) {
597 int ret = monitor_set_cpu(cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200598 if (ret < 0) {
599 cur_mon = old_mon;
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100600 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
601 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200602 goto out;
603 }
604 }
605
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100606 handle_hmp_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200607 cur_mon = old_mon;
608
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200609 qemu_mutex_lock(&hmp.out_lock);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400610 if (qstring_get_length(hmp.outbuf) > 0) {
611 output = g_strdup(qstring_get_str(hmp.outbuf));
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200612 } else {
613 output = g_strdup("");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200614 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200615 qemu_mutex_unlock(&hmp.out_lock);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200616
617out:
Wenchao Xiab01fe892013-08-27 20:38:19 +0800618 monitor_data_destroy(&hmp);
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200619 return output;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200620}
621
bellard9dc39cb2004-03-14 21:38:27 +0000622static int compare_cmd(const char *name, const char *list)
623{
624 const char *p, *pstart;
625 int len;
626 len = strlen(name);
627 p = list;
628 for(;;) {
629 pstart = p;
630 p = strchr(p, '|');
631 if (!p)
632 p = pstart + strlen(pstart);
633 if ((p - pstart) == len && !memcmp(pstart, name, len))
634 return 1;
635 if (*p == '\0')
636 break;
637 p++;
638 }
639 return 0;
640}
641
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800642static int get_str(char *buf, int buf_size, const char **pp)
643{
644 const char *p;
645 char *q;
646 int c;
647
648 q = buf;
649 p = *pp;
650 while (qemu_isspace(*p)) {
651 p++;
652 }
653 if (*p == '\0') {
654 fail:
655 *q = '\0';
656 *pp = p;
657 return -1;
658 }
659 if (*p == '\"') {
660 p++;
661 while (*p != '\0' && *p != '\"') {
662 if (*p == '\\') {
663 p++;
664 c = *p++;
665 switch (c) {
666 case 'n':
667 c = '\n';
668 break;
669 case 'r':
670 c = '\r';
671 break;
672 case '\\':
673 case '\'':
674 case '\"':
675 break;
676 default:
Peter Maydell71baf782015-08-19 16:20:19 +0100677 printf("unsupported escape code: '\\%c'\n", c);
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800678 goto fail;
679 }
680 if ((q - buf) < buf_size - 1) {
681 *q++ = c;
682 }
683 } else {
684 if ((q - buf) < buf_size - 1) {
685 *q++ = *p;
686 }
687 p++;
688 }
689 }
690 if (*p != '\"') {
Peter Maydell71baf782015-08-19 16:20:19 +0100691 printf("unterminated string\n");
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800692 goto fail;
693 }
694 p++;
695 } else {
696 while (*p != '\0' && !qemu_isspace(*p)) {
697 if ((q - buf) < buf_size - 1) {
698 *q++ = *p;
699 }
700 p++;
701 }
702 }
703 *q = '\0';
704 *pp = p;
705 return 0;
706}
707
708#define MAX_ARGS 16
709
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800710static void free_cmdline_args(char **args, int nb_args)
711{
712 int i;
713
714 assert(nb_args <= MAX_ARGS);
715
716 for (i = 0; i < nb_args; i++) {
717 g_free(args[i]);
718 }
719
720}
721
722/*
723 * Parse the command line to get valid args.
724 * @cmdline: command line to be parsed.
725 * @pnb_args: location to store the number of args, must NOT be NULL.
726 * @args: location to store the args, which should be freed by caller, must
727 * NOT be NULL.
728 *
729 * Returns 0 on success, negative on failure.
730 *
731 * NOTE: this parser is an approximate form of the real command parser. Number
732 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
733 * return with failure.
734 */
735static int parse_cmdline(const char *cmdline,
736 int *pnb_args, char **args)
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800737{
738 const char *p;
739 int nb_args, ret;
740 char buf[1024];
741
742 p = cmdline;
743 nb_args = 0;
744 for (;;) {
745 while (qemu_isspace(*p)) {
746 p++;
747 }
748 if (*p == '\0') {
749 break;
750 }
751 if (nb_args >= MAX_ARGS) {
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800752 goto fail;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800753 }
754 ret = get_str(buf, sizeof(buf), &p);
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800755 if (ret < 0) {
756 goto fail;
757 }
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800758 args[nb_args] = g_strdup(buf);
759 nb_args++;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800760 }
761 *pnb_args = nb_args;
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800762 return 0;
763
764 fail:
765 free_cmdline_args(args, nb_args);
766 return -1;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800767}
768
Wenchao Xia66855492013-08-27 20:38:23 +0800769static void help_cmd_dump_one(Monitor *mon,
770 const mon_cmd_t *cmd,
771 char **prefix_args,
772 int prefix_args_nb)
773{
774 int i;
775
776 for (i = 0; i < prefix_args_nb; i++) {
777 monitor_printf(mon, "%s ", prefix_args[i]);
778 }
779 monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
780}
781
782/* @args[@arg_index] is the valid command need to find in @cmds */
Anthony Liguoric227f092009-10-01 16:12:16 -0500783static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
Wenchao Xia66855492013-08-27 20:38:23 +0800784 char **args, int nb_args, int arg_index)
bellard9dc39cb2004-03-14 21:38:27 +0000785{
Anthony Liguoric227f092009-10-01 16:12:16 -0500786 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000787
Wenchao Xia66855492013-08-27 20:38:23 +0800788 /* No valid arg need to compare with, dump all in *cmds */
789 if (arg_index >= nb_args) {
790 for (cmd = cmds; cmd->name != NULL; cmd++) {
791 help_cmd_dump_one(mon, cmd, args, arg_index);
792 }
793 return;
794 }
795
796 /* Find one entry to dump */
797 for (cmd = cmds; cmd->name != NULL; cmd++) {
798 if (compare_cmd(args[arg_index], cmd->name)) {
799 if (cmd->sub_table) {
800 /* continue with next arg */
801 help_cmd_dump(mon, cmd->sub_table,
802 args, nb_args, arg_index + 1);
803 } else {
804 help_cmd_dump_one(mon, cmd, args, arg_index);
805 }
806 break;
807 }
bellard9dc39cb2004-03-14 21:38:27 +0000808 }
809}
810
aliguori376253e2009-03-05 23:01:23 +0000811static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000812{
Wenchao Xia66855492013-08-27 20:38:23 +0800813 char *args[MAX_ARGS];
814 int nb_args = 0;
815
816 /* 1. parse user input */
817 if (name) {
818 /* special case for log, directly dump and return */
819 if (!strcmp(name, "log")) {
Peter Maydell38dad9e2013-02-11 16:41:25 +0000820 const QEMULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000821 monitor_printf(mon, "Log items (comma separated):\n");
822 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
Peter Maydell38dad9e2013-02-11 16:41:25 +0000823 for (item = qemu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000824 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000825 }
Wenchao Xia66855492013-08-27 20:38:23 +0800826 return;
827 }
828
829 if (parse_cmdline(name, &nb_args, args) < 0) {
830 return;
bellardf193c792004-03-21 17:06:25 +0000831 }
bellard9dc39cb2004-03-14 21:38:27 +0000832 }
Wenchao Xia66855492013-08-27 20:38:23 +0800833
834 /* 2. dump the contents according to parsed args */
835 help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0);
836
837 free_cmdline_args(args, nb_args);
bellard9dc39cb2004-03-14 21:38:27 +0000838}
839
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300840static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300841{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300842 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300843}
844
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100845static void hmp_trace_event(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530846{
847 const char *tp_name = qdict_get_str(qdict, "name");
848 bool new_state = qdict_get_bool(qdict, "option");
Lluís Vilanova14101d02014-08-25 13:20:03 +0200849 Error *local_err = NULL;
Blue Swirlf871d682010-10-13 19:14:29 +0000850
Lluís Vilanova14101d02014-08-25 13:20:03 +0200851 qmp_trace_event_set_state(tp_name, new_state, true, true, &local_err);
852 if (local_err) {
Markus Armbruster091e38b2015-02-10 15:15:43 +0100853 error_report_err(local_err);
Blue Swirlf871d682010-10-13 19:14:29 +0000854 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530855}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100856
Michael Rothc45a8162011-10-02 08:44:37 -0500857#ifdef CONFIG_TRACE_SIMPLE
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100858static void hmp_trace_file(Monitor *mon, const QDict *qdict)
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100859{
860 const char *op = qdict_get_try_str(qdict, "op");
861 const char *arg = qdict_get_try_str(qdict, "arg");
862
863 if (!op) {
864 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
865 } else if (!strcmp(op, "on")) {
866 st_set_trace_file_enabled(true);
867 } else if (!strcmp(op, "off")) {
868 st_set_trace_file_enabled(false);
869 } else if (!strcmp(op, "flush")) {
870 st_flush_trace_buffer();
871 } else if (!strcmp(op, "set")) {
872 if (arg) {
873 st_set_trace_file(arg);
874 }
875 } else {
876 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
877 help_cmd(mon, "trace-file");
878 }
879}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530880#endif
881
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100882static void hmp_info_help(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000883{
Luiz Capitulino13c74252009-10-07 13:41:55 -0300884 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000885}
886
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300887CommandInfoList *qmp_query_commands(Error **errp)
bellard9bc9d1c2004-10-10 15:15:51 +0000888{
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300889 CommandInfoList *info, *cmd_list = NULL;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200890 const mon_cmd_t *cmd;
891
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300892 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino40e5a012011-10-21 16:15:31 -0200893 info = g_malloc0(sizeof(*info));
894 info->value = g_malloc0(sizeof(*info->value));
895 info->value->name = g_strdup(cmd->name);
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200896
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300897 info->next = cmd_list;
898 cmd_list = info;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200899 }
900
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300901 return cmd_list;
thsa36e69d2007-12-02 05:18:19 +0000902}
903
Daniel P. Berrange48608532012-05-21 17:59:51 +0100904EventInfoList *qmp_query_events(Error **errp)
905{
906 EventInfoList *info, *ev_list = NULL;
Wenchao Xia75175172014-06-18 08:43:54 +0200907 QAPIEvent e;
Daniel P. Berrange48608532012-05-21 17:59:51 +0100908
Wenchao Xia75175172014-06-18 08:43:54 +0200909 for (e = 0 ; e < QAPI_EVENT_MAX ; e++) {
910 const char *event_name = QAPIEvent_lookup[e];
Daniel P. Berrange48608532012-05-21 17:59:51 +0100911 assert(event_name != NULL);
912 info = g_malloc0(sizeof(*info));
913 info->value = g_malloc0(sizeof(*info->value));
914 info->value->name = g_strdup(event_name);
915
916 info->next = ev_list;
917 ev_list = info;
918 }
919
920 return ev_list;
921}
922
Markus Armbruster39a18152015-09-16 13:06:28 +0200923/*
924 * Minor hack: generated marshalling suppressed for this command
925 * ('gen': false in the schema) so we can parse the JSON string
926 * directly into QObject instead of first parsing it with
927 * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
928 * to QObject with generated output marshallers, every time. Instead,
929 * we do it in test-qmp-input-visitor.c, just to make sure
930 * qapi-introspect.py's output actually conforms to the schema.
931 */
932static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
933 Error **errp)
934{
935 *ret_data = qobject_from_json(qmp_schema_json);
936}
937
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300938/* set the current CPU defined by the user */
939int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000940{
Andreas Färber55e5c282012-12-17 06:18:02 +0100941 CPUState *cpu;
bellard6a00d602005-11-21 23:25:50 +0000942
Andreas Färber1c8bb3c2013-02-15 17:01:09 +0100943 cpu = qemu_get_cpu(cpu_index);
944 if (cpu == NULL) {
945 return -1;
bellard6a00d602005-11-21 23:25:50 +0000946 }
Andreas Färbercb446ec2013-05-01 14:24:52 +0200947 cur_mon->mon_cpu = cpu;
Andreas Färber1c8bb3c2013-02-15 17:01:09 +0100948 return 0;
bellard6a00d602005-11-21 23:25:50 +0000949}
950
Pavel Butsykincaf15312015-09-22 16:18:17 +0300951CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000952{
aliguori731b0362009-03-05 23:01:42 +0000953 if (!cur_mon->mon_cpu) {
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300954 monitor_set_cpu(0);
bellard6a00d602005-11-21 23:25:50 +0000955 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300956 cpu_synchronize_state(cur_mon->mon_cpu);
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700957 return cur_mon->mon_cpu;
958}
959
Pavel Butsykinbf957282015-09-10 18:38:59 +0300960CPUArchState *mon_get_cpu_env(void)
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700961{
962 return mon_get_cpu()->env_ptr;
bellard6a00d602005-11-21 23:25:50 +0000963}
964
Luiz Capitulino99b77962011-10-24 10:53:44 -0200965int monitor_get_cpu_index(void)
966{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700967 return mon_get_cpu()->cpu_index;
Luiz Capitulino99b77962011-10-24 10:53:44 -0200968}
969
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100970static void hmp_info_registers(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000971{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700972 cpu_dump_state(mon_get_cpu(), (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000973}
974
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100975static void hmp_info_jit(Monitor *mon, const QDict *qdict)
bellarde3db7222005-01-26 22:00:47 +0000976{
aliguori376253e2009-03-05 23:01:23 +0000977 dump_exec_info((FILE *)mon, monitor_fprintf);
Sebastian Tanase27498be2014-07-25 11:56:33 +0200978 dump_drift_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000979}
980
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100981static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
Max Filippov246ae242014-11-02 11:04:18 +0300982{
983 dump_opcount_info((FILE *)mon, monitor_fprintf);
984}
985
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100986static void hmp_info_history(Monitor *mon, const QDict *qdict)
bellardaa455482004-04-04 13:07:25 +0000987{
988 int i;
bellard7e2515e2004-08-01 21:52:19 +0000989 const char *str;
ths3b46e622007-09-17 08:09:54 +0000990
aliguoricde76ee2009-03-05 23:01:51 +0000991 if (!mon->rs)
992 return;
bellard7e2515e2004-08-01 21:52:19 +0000993 i = 0;
994 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000995 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000996 if (!str)
997 break;
aliguori376253e2009-03-05 23:01:23 +0000998 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000999 i++;
bellardaa455482004-04-04 13:07:25 +00001000 }
1001}
1002
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001003static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
j_mayer76a66252007-03-07 08:32:30 +00001004{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001005 cpu_dump_statistics(mon_get_cpu(), (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +00001006}
j_mayer76a66252007-03-07 08:32:30 +00001007
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001008static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301009{
Lluís Vilanova14101d02014-08-25 13:20:03 +02001010 TraceEventInfoList *events = qmp_trace_event_get_state("*", NULL);
1011 TraceEventInfoList *elem;
1012
1013 for (elem = events; elem != NULL; elem = elem->next) {
1014 monitor_printf(mon, "%s : state %u\n",
1015 elem->value->name,
1016 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
1017 }
1018 qapi_free_TraceEventInfoList(events);
Prerna Saxena22890ab2010-06-24 17:04:53 +05301019}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301020
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001021void qmp_client_migrate_info(const char *protocol, const char *hostname,
1022 bool has_port, int64_t port,
1023 bool has_tls_port, int64_t tls_port,
1024 bool has_cert_subject, const char *cert_subject,
1025 Error **errp)
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001026{
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001027 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001028 if (!qemu_using_spice(errp)) {
1029 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001030 }
1031
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001032 if (!has_port && !has_tls_port) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001033 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001034 return;
Yonit Halperin6ec5dae2012-03-18 09:42:39 +02001035 }
1036
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001037 if (qemu_spice_migrate_info(hostname,
1038 has_port ? port : -1,
1039 has_tls_port ? tls_port : -1,
1040 cert_subject)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001041 error_setg(errp, QERR_UNDEFINED_ERROR);
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001042 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001043 }
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001044 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001045 }
1046
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001047 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001048}
1049
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001050static void hmp_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001051{
Peter Maydell9a7e5422013-02-11 16:41:20 +00001052 qemu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001053}
1054
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001055static void hmp_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001056{
1057 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001058 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001059
bellard9307c4c2004-04-04 12:57:25 +00001060 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001061 mask = 0;
1062 } else {
Peter Maydell4fde1eb2013-02-11 16:41:22 +00001063 mask = qemu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001064 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001065 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001066 return;
1067 }
1068 }
Peter Maydell24537a02013-02-11 16:41:23 +00001069 qemu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00001070}
1071
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001072static void hmp_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001073{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001074 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001075 if (!option || !strcmp(option, "on")) {
1076 singlestep = 1;
1077 } else if (!strcmp(option, "off")) {
1078 singlestep = 0;
1079 } else {
1080 monitor_printf(mon, "unexpected option %s\n", option);
1081 }
1082}
1083
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001084static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001085{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001086 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001087 if (!device)
1088 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1089 if (gdbserver_start(device) < 0) {
1090 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1091 device);
1092 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001093 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001094 } else {
aliguori59030a82009-04-05 18:43:41 +00001095 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1096 device);
bellard8a7ddc32004-03-31 19:00:16 +00001097 }
1098}
1099
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001100static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001101{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001102 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001103 if (select_watchdog_action(action) == -1) {
1104 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1105 }
1106}
1107
aliguori376253e2009-03-05 23:01:23 +00001108static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001109{
aliguori376253e2009-03-05 23:01:23 +00001110 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001111 switch(c) {
1112 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001113 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001114 break;
1115 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001116 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001117 break;
1118 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001119 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001120 break;
1121 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001122 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001123 break;
1124 default:
1125 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001126 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001127 } else {
aliguori376253e2009-03-05 23:01:23 +00001128 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001129 }
1130 break;
1131 }
aliguori376253e2009-03-05 23:01:23 +00001132 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001133}
1134
aliguori376253e2009-03-05 23:01:23 +00001135static void memory_dump(Monitor *mon, int count, int format, int wsize,
Avi Kivitya8170e52012-10-23 12:30:10 +02001136 hwaddr addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001137{
Blue Swirl23842aa2010-01-12 20:27:43 +00001138 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001139 uint8_t buf[16];
1140 uint64_t v;
1141
1142 if (format == 'i') {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001143 int flags = 0;
bellard9307c4c2004-04-04 12:57:25 +00001144#ifdef TARGET_I386
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001145 CPUArchState *env = mon_get_cpu_env();
bellard4c27ba22004-04-25 18:05:08 +00001146 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001147 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001148 } else if (wsize == 4) {
1149 flags = 0;
1150 } else {
bellard6a15fd12006-04-12 21:07:07 +00001151 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001152 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001153 if (env) {
1154#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001155 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001156 (env->segs[R_CS].flags & DESC_L_MASK))
1157 flags = 2;
1158 else
1159#endif
1160 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1161 flags = 1;
1162 }
bellard4c27ba22004-04-25 18:05:08 +00001163 }
1164#endif
Tom Musta1c38f842014-04-09 14:53:24 -05001165#ifdef TARGET_PPC
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001166 CPUArchState *env = mon_get_cpu_env();
Tom Musta1c38f842014-04-09 14:53:24 -05001167 flags = msr_le << 16;
1168 flags |= env->bfd_mach;
1169#endif
Peter Crosthwaited49190c2015-05-24 14:20:41 -07001170 monitor_disas(mon, mon_get_cpu(), addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001171 return;
1172 }
1173
1174 len = wsize * count;
1175 if (wsize == 1)
1176 line_size = 8;
1177 else
1178 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001179 max_digits = 0;
1180
1181 switch(format) {
1182 case 'o':
1183 max_digits = (wsize * 8 + 2) / 3;
1184 break;
1185 default:
1186 case 'x':
1187 max_digits = (wsize * 8) / 4;
1188 break;
1189 case 'u':
1190 case 'd':
1191 max_digits = (wsize * 8 * 10 + 32) / 33;
1192 break;
1193 case 'c':
1194 wsize = 1;
1195 break;
1196 }
1197
1198 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001199 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001200 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001201 else
aliguori376253e2009-03-05 23:01:23 +00001202 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001203 l = len;
1204 if (l > line_size)
1205 l = line_size;
1206 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001207 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001208 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001209 if (cpu_memory_rw_debug(mon_get_cpu(), addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001210 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001211 break;
1212 }
bellard9307c4c2004-04-04 12:57:25 +00001213 }
ths5fafdf22007-09-16 21:08:06 +00001214 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001215 while (i < l) {
1216 switch(wsize) {
1217 default:
1218 case 1:
Peter Maydell24e60302015-01-20 15:19:32 +00001219 v = ldub_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001220 break;
1221 case 2:
Peter Maydell24e60302015-01-20 15:19:32 +00001222 v = lduw_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001223 break;
1224 case 4:
Peter Maydell24e60302015-01-20 15:19:32 +00001225 v = (uint32_t)ldl_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001226 break;
1227 case 8:
Peter Maydell24e60302015-01-20 15:19:32 +00001228 v = ldq_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001229 break;
1230 }
aliguori376253e2009-03-05 23:01:23 +00001231 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001232 switch(format) {
1233 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001234 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001235 break;
1236 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001237 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001238 break;
1239 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001240 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001241 break;
1242 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001243 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001244 break;
1245 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001246 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001247 break;
1248 }
1249 i += wsize;
1250 }
aliguori376253e2009-03-05 23:01:23 +00001251 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001252 addr += l;
1253 len -= l;
1254 }
1255}
1256
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001257static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001258{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001259 int count = qdict_get_int(qdict, "count");
1260 int format = qdict_get_int(qdict, "format");
1261 int size = qdict_get_int(qdict, "size");
1262 target_long addr = qdict_get_int(qdict, "addr");
1263
aliguori376253e2009-03-05 23:01:23 +00001264 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001265}
1266
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001267static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001268{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001269 int count = qdict_get_int(qdict, "count");
1270 int format = qdict_get_int(qdict, "format");
1271 int size = qdict_get_int(qdict, "size");
Avi Kivitya8170e52012-10-23 12:30:10 +02001272 hwaddr addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001273
aliguori376253e2009-03-05 23:01:23 +00001274 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001275}
1276
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001277static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001278{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001279 int format = qdict_get_int(qdict, "format");
Avi Kivitya8170e52012-10-23 12:30:10 +02001280 hwaddr val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001281
bellard9307c4c2004-04-04 12:57:25 +00001282 switch(format) {
1283 case 'o':
Avi Kivitya8170e52012-10-23 12:30:10 +02001284 monitor_printf(mon, "%#" HWADDR_PRIo, val);
bellard9307c4c2004-04-04 12:57:25 +00001285 break;
1286 case 'x':
Avi Kivitya8170e52012-10-23 12:30:10 +02001287 monitor_printf(mon, "%#" HWADDR_PRIx, val);
bellard9307c4c2004-04-04 12:57:25 +00001288 break;
1289 case 'u':
Avi Kivitya8170e52012-10-23 12:30:10 +02001290 monitor_printf(mon, "%" HWADDR_PRIu, val);
bellard9307c4c2004-04-04 12:57:25 +00001291 break;
1292 default:
1293 case 'd':
Avi Kivitya8170e52012-10-23 12:30:10 +02001294 monitor_printf(mon, "%" HWADDR_PRId, val);
bellard9307c4c2004-04-04 12:57:25 +00001295 break;
1296 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001297 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001298 break;
1299 }
aliguori376253e2009-03-05 23:01:23 +00001300 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001301}
1302
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001303static void hmp_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001304{
1305 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001306 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001307 uint32_t start = qdict_get_int(qdict, "start");
1308 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001309
1310 sum = 0;
1311 for(addr = start; addr < (start + size); addr++) {
Peter Maydell42874d32015-04-26 16:49:24 +01001312 uint8_t val = address_space_ldub(&address_space_memory, addr,
1313 MEMTXATTRS_UNSPECIFIED, NULL);
bellarde4cf1ad2005-06-04 20:15:57 +00001314 /* BSD sum algorithm ('sum' Unix command) */
1315 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001316 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001317 }
aliguori376253e2009-03-05 23:01:23 +00001318 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001319}
1320
bellard13224a82006-07-14 22:03:35 +00001321static int mouse_button_state;
1322
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001323static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001324{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001325 int dx, dy, dz, button;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001326 const char *dx_str = qdict_get_str(qdict, "dx_str");
1327 const char *dy_str = qdict_get_str(qdict, "dy_str");
1328 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001329
bellard13224a82006-07-14 22:03:35 +00001330 dx = strtol(dx_str, NULL, 0);
1331 dy = strtol(dy_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001332 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
1333 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
1334
1335 if (dz_str) {
bellard13224a82006-07-14 22:03:35 +00001336 dz = strtol(dz_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001337 if (dz != 0) {
1338 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
1339 qemu_input_queue_btn(NULL, button, true);
1340 qemu_input_event_sync();
1341 qemu_input_queue_btn(NULL, button, false);
1342 }
1343 }
1344 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001345}
1346
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001347static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001348{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001349 static uint32_t bmap[INPUT_BUTTON_MAX] = {
1350 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1351 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
1352 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1353 };
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001354 int button_state = qdict_get_int(qdict, "button_state");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001355
1356 if (mouse_button_state == button_state) {
1357 return;
1358 }
1359 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1360 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001361 mouse_button_state = button_state;
bellard13224a82006-07-14 22:03:35 +00001362}
1363
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001364static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001365{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001366 int size = qdict_get_int(qdict, "size");
1367 int addr = qdict_get_int(qdict, "addr");
1368 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001369 uint32_t val;
1370 int suffix;
1371
1372 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001373 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001374 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001375 addr++;
1376 }
1377 addr &= 0xffff;
1378
1379 switch(size) {
1380 default:
1381 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001382 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001383 suffix = 'b';
1384 break;
1385 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001386 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001387 suffix = 'w';
1388 break;
1389 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001390 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001391 suffix = 'l';
1392 break;
1393 }
aliguori376253e2009-03-05 23:01:23 +00001394 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1395 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001396}
bellarda3a91a32004-06-04 11:06:21 +00001397
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001398static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001399{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001400 int size = qdict_get_int(qdict, "size");
1401 int addr = qdict_get_int(qdict, "addr");
1402 int val = qdict_get_int(qdict, "val");
1403
Jan Kiszkaf1147842009-07-14 10:20:11 +02001404 addr &= IOPORTS_MASK;
1405
1406 switch (size) {
1407 default:
1408 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001409 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001410 break;
1411 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001412 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001413 break;
1414 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001415 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001416 break;
1417 }
1418}
1419
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001420static void hmp_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001421{
Gongleif1839932014-12-03 18:20:58 +00001422 Error *local_err = NULL;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001423 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001424
Gongleif1839932014-12-03 18:20:58 +00001425 qemu_boot_set(bootdevice, &local_err);
1426 if (local_err) {
1427 monitor_printf(mon, "%s\n", error_get_pretty(local_err));
1428 error_free(local_err);
aurel320ecdffb2008-05-04 20:11:34 +00001429 } else {
Gongleif1839932014-12-03 18:20:58 +00001430 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001431 }
1432}
1433
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001434static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
Blue Swirl314e2982011-09-11 20:22:05 +00001435{
1436 mtree_info((fprintf_function)monitor_printf, mon);
1437}
1438
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001439static void hmp_info_numa(Monitor *mon, const QDict *qdict)
aliguori030ea372009-04-21 22:30:47 +00001440{
aliguorib28b6232009-04-22 20:20:29 +00001441 int i;
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001442 CPUState *cpu;
zhanghailiang5b009e42014-11-04 19:49:30 +08001443 uint64_t *node_mem;
aliguori030ea372009-04-21 22:30:47 +00001444
zhanghailiang5b009e42014-11-04 19:49:30 +08001445 node_mem = g_new0(uint64_t, nb_numa_nodes);
1446 query_numa_node_mem(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001447 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1448 for (i = 0; i < nb_numa_nodes; i++) {
1449 monitor_printf(mon, "node %d cpus:", i);
Andreas Färberbdc44642013-06-24 23:50:24 +02001450 CPU_FOREACH(cpu) {
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001451 if (cpu->numa_node == i) {
Andreas Färber55e5c282012-12-17 06:18:02 +01001452 monitor_printf(mon, " %d", cpu->cpu_index);
aliguori030ea372009-04-21 22:30:47 +00001453 }
1454 }
1455 monitor_printf(mon, "\n");
1456 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
zhanghailiang5b009e42014-11-04 19:49:30 +08001457 node_mem[i] >> 20);
aliguori030ea372009-04-21 22:30:47 +00001458 }
zhanghailiang5b009e42014-11-04 19:49:30 +08001459 g_free(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001460}
1461
bellard5f1ce942006-02-08 22:40:15 +00001462#ifdef CONFIG_PROFILER
1463
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001464int64_t tcg_time;
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001465int64_t dev_time;
1466
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001467static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001468{
aliguori376253e2009-03-05 23:01:23 +00001469 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001470 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00001471 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001472 tcg_time, tcg_time / (double)get_ticks_per_sec());
1473 tcg_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001474 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001475}
1476#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001477static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001478{
aliguori376253e2009-03-05 23:01:23 +00001479 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001480}
1481#endif
1482
bellardec36b692006-07-16 18:57:03 +00001483/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001484static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001485
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001486static void hmp_info_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001487{
1488 int i;
1489 CaptureState *s;
1490
1491 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001492 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001493 s->ops.info (s->opaque);
1494 }
1495}
1496
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001497static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001498{
1499 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001500 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001501 CaptureState *s;
1502
1503 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1504 if (i == n) {
1505 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001506 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001507 g_free (s);
bellardec36b692006-07-16 18:57:03 +00001508 return;
1509 }
1510 }
1511}
1512
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001513static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001514{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001515 const char *path = qdict_get_str(qdict, "path");
1516 int has_freq = qdict_haskey(qdict, "freq");
1517 int freq = qdict_get_try_int(qdict, "freq", -1);
1518 int has_bits = qdict_haskey(qdict, "bits");
1519 int bits = qdict_get_try_int(qdict, "bits", -1);
1520 int has_channels = qdict_haskey(qdict, "nchannels");
1521 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001522 CaptureState *s;
1523
Anthony Liguori7267c092011-08-20 22:09:37 -05001524 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001525
1526 freq = has_freq ? freq : 44100;
1527 bits = has_bits ? bits : 16;
1528 nchannels = has_channels ? nchannels : 2;
1529
1530 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001531 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05001532 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001533 return;
bellardec36b692006-07-16 18:57:03 +00001534 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001535 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001536}
bellardec36b692006-07-16 18:57:03 +00001537
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001538static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001539{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001540 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001541
aliguori76655d62009-03-06 20:27:37 +00001542 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001543 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001544 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001545 return acl;
1546}
aliguori76655d62009-03-06 20:27:37 +00001547
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001548static void hmp_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001549{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001550 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001551 qemu_acl *acl = find_acl(mon, aclname);
1552 qemu_acl_entry *entry;
1553 int i = 0;
1554
1555 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001556 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001557 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00001558 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00001559 i++;
1560 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001561 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001562 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001563 }
1564}
1565
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001566static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001567{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001568 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001569 qemu_acl *acl = find_acl(mon, aclname);
1570
1571 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001572 qemu_acl_reset(acl);
1573 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001574 }
1575}
aliguori76655d62009-03-06 20:27:37 +00001576
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001577static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001578{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001579 const char *aclname = qdict_get_str(qdict, "aclname");
1580 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001581 qemu_acl *acl = find_acl(mon, aclname);
1582
1583 if (acl) {
1584 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001585 acl->defaultDeny = 0;
1586 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001587 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001588 acl->defaultDeny = 1;
1589 monitor_printf(mon, "acl: policy set to 'deny'\n");
1590 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001591 monitor_printf(mon, "acl: unknown policy '%s', "
1592 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001593 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001594 }
1595}
aliguori76655d62009-03-06 20:27:37 +00001596
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001597static void hmp_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001598{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001599 const char *aclname = qdict_get_str(qdict, "aclname");
1600 const char *match = qdict_get_str(qdict, "match");
1601 const char *policy = qdict_get_str(qdict, "policy");
1602 int has_index = qdict_haskey(qdict, "index");
1603 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001604 qemu_acl *acl = find_acl(mon, aclname);
1605 int deny, ret;
1606
1607 if (acl) {
1608 if (strcmp(policy, "allow") == 0) {
1609 deny = 0;
1610 } else if (strcmp(policy, "deny") == 0) {
1611 deny = 1;
1612 } else {
1613 monitor_printf(mon, "acl: unknown policy '%s', "
1614 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001615 return;
1616 }
aliguori28a76be2009-03-06 20:27:40 +00001617 if (has_index)
1618 ret = qemu_acl_insert(acl, deny, match, index);
1619 else
1620 ret = qemu_acl_append(acl, deny, match);
1621 if (ret < 0)
1622 monitor_printf(mon, "acl: unable to add acl entry\n");
1623 else
1624 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001625 }
1626}
aliguori76655d62009-03-06 20:27:37 +00001627
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001628static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001629{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001630 const char *aclname = qdict_get_str(qdict, "aclname");
1631 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001632 qemu_acl *acl = find_acl(mon, aclname);
1633 int ret;
aliguori76655d62009-03-06 20:27:37 +00001634
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001635 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001636 ret = qemu_acl_remove(acl, match);
1637 if (ret < 0)
1638 monitor_printf(mon, "acl: no matching acl entry\n");
1639 else
1640 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001641 }
1642}
1643
Corey Bryant208c9d12012-06-22 14:36:09 -04001644void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001645{
Anthony Liguoric227f092009-10-01 16:12:16 -05001646 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001647 int fd;
1648
Corey Bryant208c9d12012-06-22 14:36:09 -04001649 fd = qemu_chr_fe_get_msgfd(cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001650 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001651 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryant208c9d12012-06-22 14:36:09 -04001652 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001653 }
1654
1655 if (qemu_isdigit(fdname[0])) {
Stefan Hajnoczi0b9f0e22014-04-24 13:58:18 +02001656 close(fd);
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001657 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
1658 "a name not starting with a digit");
Corey Bryant208c9d12012-06-22 14:36:09 -04001659 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001660 }
1661
Corey Bryant208c9d12012-06-22 14:36:09 -04001662 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001663 if (strcmp(monfd->name, fdname) != 0) {
1664 continue;
1665 }
1666
1667 close(monfd->fd);
1668 monfd->fd = fd;
Corey Bryant208c9d12012-06-22 14:36:09 -04001669 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001670 }
1671
Anthony Liguori7267c092011-08-20 22:09:37 -05001672 monfd = g_malloc0(sizeof(mon_fd_t));
1673 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001674 monfd->fd = fd;
1675
Corey Bryant208c9d12012-06-22 14:36:09 -04001676 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001677}
1678
Corey Bryant208c9d12012-06-22 14:36:09 -04001679void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001680{
Anthony Liguoric227f092009-10-01 16:12:16 -05001681 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001682
Corey Bryant208c9d12012-06-22 14:36:09 -04001683 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001684 if (strcmp(monfd->name, fdname) != 0) {
1685 continue;
1686 }
1687
Blue Swirl72cf2d42009-09-12 07:36:22 +00001688 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001689 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001690 g_free(monfd->name);
1691 g_free(monfd);
Corey Bryant208c9d12012-06-22 14:36:09 -04001692 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001693 }
1694
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001695 error_setg(errp, QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001696}
1697
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001698static void hmp_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001699{
Luiz Capitulino13548692011-07-29 15:36:43 -03001700 int saved_vm_running = runstate_is_running();
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001701 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02001702
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001703 vm_stop(RUN_STATE_RESTORE_VM);
Juan Quintelac8d41b22009-08-20 19:42:21 +02001704
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03001705 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02001706 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03001707 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02001708}
1709
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001710int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
Mark McLoughlin7768e042009-07-22 09:11:41 +01001711{
Anthony Liguoric227f092009-10-01 16:12:16 -05001712 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01001713
Blue Swirl72cf2d42009-09-12 07:36:22 +00001714 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01001715 int fd;
1716
1717 if (strcmp(monfd->name, fdname) != 0) {
1718 continue;
1719 }
1720
1721 fd = monfd->fd;
1722
1723 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001724 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05001725 g_free(monfd->name);
1726 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001727
1728 return fd;
1729 }
1730
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001731 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001732 return -1;
1733}
1734
Corey Bryantba1c0482012-08-14 16:43:43 -04001735static void monitor_fdset_cleanup(MonFdset *mon_fdset)
1736{
1737 MonFdsetFd *mon_fdset_fd;
1738 MonFdsetFd *mon_fdset_fd_next;
1739
1740 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantebe52b52012-10-18 15:19:33 -04001741 if ((mon_fdset_fd->removed ||
1742 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
1743 runstate_is_running()) {
Corey Bryantba1c0482012-08-14 16:43:43 -04001744 close(mon_fdset_fd->fd);
1745 g_free(mon_fdset_fd->opaque);
1746 QLIST_REMOVE(mon_fdset_fd, next);
1747 g_free(mon_fdset_fd);
1748 }
1749 }
1750
Corey Bryantadb696f2012-08-14 16:43:47 -04001751 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04001752 QLIST_REMOVE(mon_fdset, next);
1753 g_free(mon_fdset);
1754 }
1755}
1756
Corey Bryantefb87c12012-08-14 16:43:48 -04001757static void monitor_fdsets_cleanup(void)
1758{
1759 MonFdset *mon_fdset;
1760 MonFdset *mon_fdset_next;
1761
1762 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
1763 monitor_fdset_cleanup(mon_fdset);
1764 }
1765}
1766
Corey Bryantba1c0482012-08-14 16:43:43 -04001767AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
1768 const char *opaque, Error **errp)
1769{
1770 int fd;
1771 Monitor *mon = cur_mon;
Corey Bryantba1c0482012-08-14 16:43:43 -04001772 AddfdInfo *fdinfo;
1773
1774 fd = qemu_chr_fe_get_msgfd(mon->chr);
1775 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001776 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryantba1c0482012-08-14 16:43:43 -04001777 goto error;
1778 }
1779
Corey Bryante446f702012-10-18 15:19:32 -04001780 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
1781 has_opaque, opaque, errp);
1782 if (fdinfo) {
1783 return fdinfo;
Corey Bryant9ac54af2012-10-18 15:19:31 -04001784 }
1785
Corey Bryantba1c0482012-08-14 16:43:43 -04001786error:
1787 if (fd != -1) {
1788 close(fd);
1789 }
1790 return NULL;
1791}
1792
1793void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
1794{
1795 MonFdset *mon_fdset;
1796 MonFdsetFd *mon_fdset_fd;
1797 char fd_str[60];
1798
1799 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1800 if (mon_fdset->id != fdset_id) {
1801 continue;
1802 }
1803 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1804 if (has_fd) {
1805 if (mon_fdset_fd->fd != fd) {
1806 continue;
1807 }
1808 mon_fdset_fd->removed = true;
1809 break;
1810 } else {
1811 mon_fdset_fd->removed = true;
1812 }
1813 }
1814 if (has_fd && !mon_fdset_fd) {
1815 goto error;
1816 }
1817 monitor_fdset_cleanup(mon_fdset);
1818 return;
1819 }
1820
1821error:
1822 if (has_fd) {
1823 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
1824 fdset_id, fd);
1825 } else {
1826 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
1827 }
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001828 error_setg(errp, QERR_FD_NOT_FOUND, fd_str);
Corey Bryantba1c0482012-08-14 16:43:43 -04001829}
1830
1831FdsetInfoList *qmp_query_fdsets(Error **errp)
1832{
1833 MonFdset *mon_fdset;
1834 MonFdsetFd *mon_fdset_fd;
1835 FdsetInfoList *fdset_list = NULL;
1836
1837 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1838 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
1839 FdsetFdInfoList *fdsetfd_list = NULL;
1840
1841 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
1842 fdset_info->value->fdset_id = mon_fdset->id;
1843
1844 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1845 FdsetFdInfoList *fdsetfd_info;
1846
1847 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
1848 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
1849 fdsetfd_info->value->fd = mon_fdset_fd->fd;
1850 if (mon_fdset_fd->opaque) {
1851 fdsetfd_info->value->has_opaque = true;
1852 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
1853 } else {
1854 fdsetfd_info->value->has_opaque = false;
1855 }
1856
1857 fdsetfd_info->next = fdsetfd_list;
1858 fdsetfd_list = fdsetfd_info;
1859 }
1860
1861 fdset_info->value->fds = fdsetfd_list;
1862
1863 fdset_info->next = fdset_list;
1864 fdset_list = fdset_info;
1865 }
1866
1867 return fdset_list;
1868}
1869
Corey Bryante446f702012-10-18 15:19:32 -04001870AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
1871 bool has_opaque, const char *opaque,
1872 Error **errp)
1873{
1874 MonFdset *mon_fdset = NULL;
1875 MonFdsetFd *mon_fdset_fd;
1876 AddfdInfo *fdinfo;
1877
1878 if (has_fdset_id) {
1879 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1880 /* Break if match found or match impossible due to ordering by ID */
1881 if (fdset_id <= mon_fdset->id) {
1882 if (fdset_id < mon_fdset->id) {
1883 mon_fdset = NULL;
1884 }
1885 break;
1886 }
1887 }
1888 }
1889
1890 if (mon_fdset == NULL) {
1891 int64_t fdset_id_prev = -1;
1892 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
1893
1894 if (has_fdset_id) {
1895 if (fdset_id < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001896 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
1897 "a non-negative value");
Corey Bryante446f702012-10-18 15:19:32 -04001898 return NULL;
1899 }
1900 /* Use specified fdset ID */
1901 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1902 mon_fdset_cur = mon_fdset;
1903 if (fdset_id < mon_fdset_cur->id) {
1904 break;
1905 }
1906 }
1907 } else {
1908 /* Use first available fdset ID */
1909 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1910 mon_fdset_cur = mon_fdset;
1911 if (fdset_id_prev == mon_fdset_cur->id - 1) {
1912 fdset_id_prev = mon_fdset_cur->id;
1913 continue;
1914 }
1915 break;
1916 }
1917 }
1918
1919 mon_fdset = g_malloc0(sizeof(*mon_fdset));
1920 if (has_fdset_id) {
1921 mon_fdset->id = fdset_id;
1922 } else {
1923 mon_fdset->id = fdset_id_prev + 1;
1924 }
1925
1926 /* The fdset list is ordered by fdset ID */
1927 if (!mon_fdset_cur) {
1928 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
1929 } else if (mon_fdset->id < mon_fdset_cur->id) {
1930 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
1931 } else {
1932 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
1933 }
1934 }
1935
1936 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
1937 mon_fdset_fd->fd = fd;
1938 mon_fdset_fd->removed = false;
1939 if (has_opaque) {
1940 mon_fdset_fd->opaque = g_strdup(opaque);
1941 }
1942 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
1943
1944 fdinfo = g_malloc0(sizeof(*fdinfo));
1945 fdinfo->fdset_id = mon_fdset->id;
1946 fdinfo->fd = mon_fdset_fd->fd;
1947
1948 return fdinfo;
1949}
1950
Corey Bryantadb696f2012-08-14 16:43:47 -04001951int monitor_fdset_get_fd(int64_t fdset_id, int flags)
1952{
Blue Swirlb2dc64c2012-08-18 20:14:54 +00001953#ifndef _WIN32
Corey Bryantadb696f2012-08-14 16:43:47 -04001954 MonFdset *mon_fdset;
1955 MonFdsetFd *mon_fdset_fd;
1956 int mon_fd_flags;
1957
Corey Bryantadb696f2012-08-14 16:43:47 -04001958 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1959 if (mon_fdset->id != fdset_id) {
1960 continue;
1961 }
1962 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1963 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
1964 if (mon_fd_flags == -1) {
1965 return -1;
1966 }
1967
1968 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
1969 return mon_fdset_fd->fd;
1970 }
1971 }
1972 errno = EACCES;
1973 return -1;
1974 }
1975#endif
1976
1977 errno = ENOENT;
1978 return -1;
1979}
1980
1981int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
1982{
1983 MonFdset *mon_fdset;
1984 MonFdsetFd *mon_fdset_fd_dup;
1985
1986 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1987 if (mon_fdset->id != fdset_id) {
1988 continue;
1989 }
1990 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
1991 if (mon_fdset_fd_dup->fd == dup_fd) {
1992 return -1;
1993 }
1994 }
1995 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
1996 mon_fdset_fd_dup->fd = dup_fd;
1997 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
1998 return 0;
1999 }
2000 return -1;
2001}
2002
2003static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2004{
2005 MonFdset *mon_fdset;
2006 MonFdsetFd *mon_fdset_fd_dup;
2007
2008 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2009 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2010 if (mon_fdset_fd_dup->fd == dup_fd) {
2011 if (remove) {
2012 QLIST_REMOVE(mon_fdset_fd_dup, next);
2013 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2014 monitor_fdset_cleanup(mon_fdset);
2015 }
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002016 return -1;
2017 } else {
2018 return mon_fdset->id;
Corey Bryantadb696f2012-08-14 16:43:47 -04002019 }
Corey Bryantadb696f2012-08-14 16:43:47 -04002020 }
2021 }
2022 }
2023 return -1;
2024}
2025
2026int monitor_fdset_dup_fd_find(int dup_fd)
2027{
2028 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2029}
2030
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002031void monitor_fdset_dup_fd_remove(int dup_fd)
Corey Bryantadb696f2012-08-14 16:43:47 -04002032{
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002033 monitor_fdset_dup_fd_find_remove(dup_fd, true);
Corey Bryantadb696f2012-08-14 16:43:47 -04002034}
2035
Markus Armbruster1677f4c2015-02-09 14:03:19 +01002036int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
Laszlo Ersek59063662014-04-10 10:24:31 +02002037{
2038 int fd;
2039 Error *local_err = NULL;
2040
2041 if (!qemu_isdigit(fdname[0]) && mon) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002042 fd = monitor_get_fd(mon, fdname, &local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002043 } else {
2044 fd = qemu_parse_fd(fdname);
Laszlo Ersek59063662014-04-10 10:24:31 +02002045 if (fd == -1) {
2046 error_setg(&local_err, "Invalid file descriptor number '%s'",
2047 fdname);
2048 }
2049 }
2050 if (local_err) {
2051 error_propagate(errp, local_err);
2052 assert(fd == -1);
2053 } else {
2054 assert(fd != -1);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002055 }
2056
2057 return fd;
2058}
2059
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002060/* Please update hmp-commands.hx when adding or changing commands */
Wayne Xia816f8922011-10-12 11:32:41 +08002061static mon_cmd_t info_cmds[] = {
Pavel Butsykinda76ee72015-09-10 18:38:58 +03002062#include "hmp-commands-info.h"
2063 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002064};
2065
Wenchao Xiaa13ced52013-01-14 14:06:28 +08002066/* mon_cmds and info_cmds would be sorted at runtime */
2067static mon_cmd_t mon_cmds[] = {
2068#include "hmp-commands.h"
2069 { NULL, NULL, },
2070};
2071
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002072static const mon_cmd_t qmp_cmds[] = {
Anthony Liguorie3193602011-09-02 12:34:47 -05002073#include "qmp-commands-old.h"
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002074 { /* NULL */ },
2075};
2076
bellard9307c4c2004-04-04 12:57:25 +00002077/*******************************************************************/
2078
2079static const char *pch;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002080static sigjmp_buf expr_env;
bellard9307c4c2004-04-04 12:57:25 +00002081
bellard9307c4c2004-04-04 12:57:25 +00002082
Stefan Weil9c3175c2013-08-22 21:30:09 +02002083static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
2084expr_error(Monitor *mon, const char *fmt, ...)
bellard9dc39cb2004-03-14 21:38:27 +00002085{
Fam Zheng277acfe2013-08-20 10:58:21 +08002086 va_list ap;
2087 va_start(ap, fmt);
2088 monitor_vprintf(mon, fmt, ap);
2089 monitor_printf(mon, "\n");
2090 va_end(ap);
Peter Maydell6ab7e542013-02-20 15:21:09 +00002091 siglongjmp(expr_env, 1);
bellard9307c4c2004-04-04 12:57:25 +00002092}
2093
Markus Armbruster09b94182010-01-20 13:07:30 +01002094/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00002095static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002096{
Pavel Butsykinbf957282015-09-10 18:38:59 +03002097 const MonitorDef *md = target_monitor_defs();
bellard92a31b12005-02-10 22:00:52 +00002098 void *ptr;
2099
Pavel Butsykinbf957282015-09-10 18:38:59 +03002100 if (md == NULL) {
2101 return -1;
2102 }
2103
2104 for(; md->name != NULL; md++) {
bellard9307c4c2004-04-04 12:57:25 +00002105 if (compare_cmd(name, md->name)) {
2106 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002107 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002108 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002109 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00002110 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002111 switch(md->type) {
2112 case MD_I32:
2113 *pval = *(int32_t *)ptr;
2114 break;
2115 case MD_TLONG:
2116 *pval = *(target_long *)ptr;
2117 break;
2118 default:
2119 *pval = 0;
2120 break;
2121 }
bellard9307c4c2004-04-04 12:57:25 +00002122 }
2123 return 0;
2124 }
2125 }
2126 return -1;
2127}
2128
2129static void next(void)
2130{
Blue Swirl660f11b2009-07-31 21:16:51 +00002131 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002132 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002133 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002134 pch++;
2135 }
2136}
2137
aliguori376253e2009-03-05 23:01:23 +00002138static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002139
aliguori376253e2009-03-05 23:01:23 +00002140static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002141{
blueswir1c2efc952007-09-25 17:28:42 +00002142 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002143 char *p;
bellard6a00d602005-11-21 23:25:50 +00002144 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002145
2146 switch(*pch) {
2147 case '+':
2148 next();
aliguori376253e2009-03-05 23:01:23 +00002149 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002150 break;
2151 case '-':
2152 next();
aliguori376253e2009-03-05 23:01:23 +00002153 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002154 break;
2155 case '~':
2156 next();
aliguori376253e2009-03-05 23:01:23 +00002157 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002158 break;
2159 case '(':
2160 next();
aliguori376253e2009-03-05 23:01:23 +00002161 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002162 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002163 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002164 }
2165 next();
2166 break;
bellard81d09122004-07-14 17:21:37 +00002167 case '\'':
2168 pch++;
2169 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002170 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002171 n = *pch;
2172 pch++;
2173 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002174 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002175 next();
2176 break;
bellard9307c4c2004-04-04 12:57:25 +00002177 case '$':
2178 {
2179 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002180 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002181
bellard9307c4c2004-04-04 12:57:25 +00002182 pch++;
2183 q = buf;
2184 while ((*pch >= 'a' && *pch <= 'z') ||
2185 (*pch >= 'A' && *pch <= 'Z') ||
2186 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002187 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002188 if ((q - buf) < sizeof(buf) - 1)
2189 *q++ = *pch;
2190 pch++;
2191 }
blueswir1cd390082008-11-16 13:53:32 +00002192 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002193 pch++;
2194 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002195 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01002196 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00002197 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00002198 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002199 }
2200 break;
2201 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002202 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002203 n = 0;
2204 break;
2205 default:
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002206 errno = 0;
bellard4f4fbf72006-06-25 18:28:12 +00002207 n = strtoull(pch, &p, 0);
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002208 if (errno == ERANGE) {
2209 expr_error(mon, "number too large");
2210 }
bellard9307c4c2004-04-04 12:57:25 +00002211 if (pch == p) {
Fam Zheng277acfe2013-08-20 10:58:21 +08002212 expr_error(mon, "invalid char '%c' in expression", *p);
bellard9307c4c2004-04-04 12:57:25 +00002213 }
2214 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002215 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002216 pch++;
2217 break;
2218 }
2219 return n;
2220}
2221
2222
aliguori376253e2009-03-05 23:01:23 +00002223static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002224{
blueswir1c2efc952007-09-25 17:28:42 +00002225 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002226 int op;
ths3b46e622007-09-17 08:09:54 +00002227
aliguori376253e2009-03-05 23:01:23 +00002228 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002229 for(;;) {
2230 op = *pch;
2231 if (op != '*' && op != '/' && op != '%')
2232 break;
2233 next();
aliguori376253e2009-03-05 23:01:23 +00002234 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002235 switch(op) {
2236 default:
2237 case '*':
2238 val *= val2;
2239 break;
2240 case '/':
2241 case '%':
ths5fafdf22007-09-16 21:08:06 +00002242 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002243 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002244 if (op == '/')
2245 val /= val2;
2246 else
2247 val %= val2;
2248 break;
2249 }
2250 }
2251 return val;
2252}
2253
aliguori376253e2009-03-05 23:01:23 +00002254static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002255{
blueswir1c2efc952007-09-25 17:28:42 +00002256 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002257 int op;
bellard9307c4c2004-04-04 12:57:25 +00002258
aliguori376253e2009-03-05 23:01:23 +00002259 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002260 for(;;) {
2261 op = *pch;
2262 if (op != '&' && op != '|' && op != '^')
2263 break;
2264 next();
aliguori376253e2009-03-05 23:01:23 +00002265 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002266 switch(op) {
2267 default:
2268 case '&':
2269 val &= val2;
2270 break;
2271 case '|':
2272 val |= val2;
2273 break;
2274 case '^':
2275 val ^= val2;
2276 break;
2277 }
2278 }
2279 return val;
2280}
2281
aliguori376253e2009-03-05 23:01:23 +00002282static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002283{
blueswir1c2efc952007-09-25 17:28:42 +00002284 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002285 int op;
bellard9307c4c2004-04-04 12:57:25 +00002286
aliguori376253e2009-03-05 23:01:23 +00002287 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002288 for(;;) {
2289 op = *pch;
2290 if (op != '+' && op != '-')
2291 break;
2292 next();
aliguori376253e2009-03-05 23:01:23 +00002293 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002294 if (op == '+')
2295 val += val2;
2296 else
2297 val -= val2;
2298 }
2299 return val;
2300}
2301
aliguori376253e2009-03-05 23:01:23 +00002302static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002303{
2304 pch = *pp;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002305 if (sigsetjmp(expr_env, 0)) {
bellard9307c4c2004-04-04 12:57:25 +00002306 *pp = pch;
2307 return -1;
2308 }
blueswir1cd390082008-11-16 13:53:32 +00002309 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002310 pch++;
aliguori376253e2009-03-05 23:01:23 +00002311 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002312 *pp = pch;
2313 return 0;
2314}
2315
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002316static int get_double(Monitor *mon, double *pval, const char **pp)
2317{
2318 const char *p = *pp;
2319 char *tailp;
2320 double d;
2321
2322 d = strtod(p, &tailp);
2323 if (tailp == p) {
2324 monitor_printf(mon, "Number expected\n");
2325 return -1;
2326 }
2327 if (d != d || d - d != 0) {
2328 /* NaN or infinity */
2329 monitor_printf(mon, "Bad number\n");
2330 return -1;
2331 }
2332 *pval = d;
2333 *pp = tailp;
2334 return 0;
2335}
2336
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002337/*
2338 * Store the command-name in cmdname, and return a pointer to
2339 * the remaining of the command string.
2340 */
2341static const char *get_command_name(const char *cmdline,
2342 char *cmdname, size_t nlen)
2343{
2344 size_t len;
2345 const char *p, *pstart;
2346
2347 p = cmdline;
2348 while (qemu_isspace(*p))
2349 p++;
2350 if (*p == '\0')
2351 return NULL;
2352 pstart = p;
2353 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2354 p++;
2355 len = p - pstart;
2356 if (len > nlen - 1)
2357 len = nlen - 1;
2358 memcpy(cmdname, pstart, len);
2359 cmdname[len] = '\0';
2360 return p;
2361}
2362
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002363/**
2364 * Read key of 'type' into 'key' and return the current
2365 * 'type' pointer.
2366 */
2367static char *key_get_info(const char *type, char **key)
2368{
2369 size_t len;
2370 char *p, *str;
2371
2372 if (*type == ',')
2373 type++;
2374
2375 p = strchr(type, ':');
2376 if (!p) {
2377 *key = NULL;
2378 return NULL;
2379 }
2380 len = p - type;
2381
Anthony Liguori7267c092011-08-20 22:09:37 -05002382 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002383 memcpy(str, type, len);
2384 str[len] = '\0';
2385
2386 *key = str;
2387 return ++p;
2388}
2389
bellard9307c4c2004-04-04 12:57:25 +00002390static int default_fmt_format = 'x';
2391static int default_fmt_size = 4;
2392
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002393static int is_valid_option(const char *c, const char *typestr)
2394{
2395 char option[3];
2396
2397 option[0] = '-';
2398 option[1] = *c;
2399 option[2] = '\0';
2400
2401 typestr = strstr(typestr, option);
2402 return (typestr != NULL);
2403}
2404
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03002405static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
2406 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002407{
2408 const mon_cmd_t *cmd;
2409
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03002410 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002411 if (compare_cmd(cmdname, cmd->name)) {
2412 return cmd;
2413 }
2414 }
2415
2416 return NULL;
2417}
2418
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03002419static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
2420{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002421 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03002422}
2423
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002424/*
Bandan Dasae502122015-06-03 18:38:08 -04002425 * Parse command name from @cmdp according to command table @table.
2426 * If blank, return NULL.
2427 * Else, if no valid command can be found, report to @mon, and return
2428 * NULL.
2429 * Else, change @cmdp to point right behind the name, and return its
2430 * command table entry.
2431 * Do not assume the return value points into @table! It doesn't when
2432 * the command is found in a sub-command table.
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002433 */
Anthony Liguoric227f092009-10-01 16:12:16 -05002434static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Bandan Dasae502122015-06-03 18:38:08 -04002435 const char **cmdp,
2436 mon_cmd_t *table)
bellard9307c4c2004-04-04 12:57:25 +00002437{
Bandan Dasae502122015-06-03 18:38:08 -04002438 const char *p;
Anthony Liguoric227f092009-10-01 16:12:16 -05002439 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002440 char cmdname[256];
bellard9dc39cb2004-03-14 21:38:27 +00002441
bellard9307c4c2004-04-04 12:57:25 +00002442 /* extract the command name */
Bandan Dasae502122015-06-03 18:38:08 -04002443 p = get_command_name(*cmdp, cmdname, sizeof(cmdname));
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002444 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002445 return NULL;
ths3b46e622007-09-17 08:09:54 +00002446
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002447 cmd = search_dispatch_table(table, cmdname);
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002448 if (!cmd) {
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002449 monitor_printf(mon, "unknown command: '%.*s'\n",
Bandan Dasae502122015-06-03 18:38:08 -04002450 (int)(p - *cmdp), *cmdp);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002451 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002452 }
bellard9307c4c2004-04-04 12:57:25 +00002453
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002454 /* filter out following useless space */
2455 while (qemu_isspace(*p)) {
2456 p++;
2457 }
Bandan Dasae502122015-06-03 18:38:08 -04002458
2459 *cmdp = p;
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002460 /* search sub command */
Bandan Dasae502122015-06-03 18:38:08 -04002461 if (cmd->sub_table != NULL && *p != '\0') {
2462 return monitor_parse_command(mon, cmdp, cmd->sub_table);
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002463 }
2464
Bandan Dasae502122015-06-03 18:38:08 -04002465 return cmd;
2466}
2467
2468/*
2469 * Parse arguments for @cmd.
2470 * If it can't be parsed, report to @mon, and return NULL.
2471 * Else, insert command arguments into a QDict, and return it.
2472 * Note: On success, caller has to free the QDict structure.
2473 */
2474
2475static QDict *monitor_parse_arguments(Monitor *mon,
2476 const char **endp,
2477 const mon_cmd_t *cmd)
2478{
2479 const char *typestr;
2480 char *key;
2481 int c;
2482 const char *p = *endp;
2483 char buf[1024];
2484 QDict *qdict = qdict_new();
2485
bellard9307c4c2004-04-04 12:57:25 +00002486 /* parse the parameters */
2487 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00002488 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002489 typestr = key_get_info(typestr, &key);
2490 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00002491 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002492 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00002493 typestr++;
2494 switch(c) {
2495 case 'F':
bellard81d09122004-07-14 17:21:37 +00002496 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002497 case 's':
2498 {
2499 int ret;
ths3b46e622007-09-17 08:09:54 +00002500
blueswir1cd390082008-11-16 13:53:32 +00002501 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002502 p++;
2503 if (*typestr == '?') {
2504 typestr++;
2505 if (*p == '\0') {
2506 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03002507 break;
bellard9307c4c2004-04-04 12:57:25 +00002508 }
2509 }
2510 ret = get_str(buf, sizeof(buf), &p);
2511 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002512 switch(c) {
2513 case 'F':
aliguori376253e2009-03-05 23:01:23 +00002514 monitor_printf(mon, "%s: filename expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002515 cmd->name);
bellard81d09122004-07-14 17:21:37 +00002516 break;
2517 case 'B':
aliguori376253e2009-03-05 23:01:23 +00002518 monitor_printf(mon, "%s: block device name expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002519 cmd->name);
bellard81d09122004-07-14 17:21:37 +00002520 break;
2521 default:
Bandan Dasae502122015-06-03 18:38:08 -04002522 monitor_printf(mon, "%s: string expected\n", cmd->name);
bellard81d09122004-07-14 17:21:37 +00002523 break;
2524 }
bellard9307c4c2004-04-04 12:57:25 +00002525 goto fail;
2526 }
Luiz Capitulino53773582009-08-28 15:27:25 -03002527 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00002528 }
2529 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01002530 case 'O':
2531 {
2532 QemuOptsList *opts_list;
2533 QemuOpts *opts;
2534
2535 opts_list = qemu_find_opts(key);
2536 if (!opts_list || opts_list->desc->name) {
2537 goto bad_type;
2538 }
2539 while (qemu_isspace(*p)) {
2540 p++;
2541 }
2542 if (!*p)
2543 break;
2544 if (get_str(buf, sizeof(buf), &p) < 0) {
2545 goto fail;
2546 }
Markus Armbruster70b94332015-02-13 12:50:26 +01002547 opts = qemu_opts_parse_noisily(opts_list, buf, true);
Markus Armbruster361127d2010-02-10 20:24:35 +01002548 if (!opts) {
2549 goto fail;
2550 }
2551 qemu_opts_to_qdict(opts, qdict);
2552 qemu_opts_del(opts);
2553 }
2554 break;
bellard9307c4c2004-04-04 12:57:25 +00002555 case '/':
2556 {
2557 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002558
blueswir1cd390082008-11-16 13:53:32 +00002559 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002560 p++;
2561 if (*p == '/') {
2562 /* format found */
2563 p++;
2564 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002565 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002566 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002567 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002568 count = count * 10 + (*p - '0');
2569 p++;
2570 }
2571 }
2572 size = -1;
2573 format = -1;
2574 for(;;) {
2575 switch(*p) {
2576 case 'o':
2577 case 'd':
2578 case 'u':
2579 case 'x':
2580 case 'i':
2581 case 'c':
2582 format = *p++;
2583 break;
2584 case 'b':
2585 size = 1;
2586 p++;
2587 break;
2588 case 'h':
2589 size = 2;
2590 p++;
2591 break;
2592 case 'w':
2593 size = 4;
2594 p++;
2595 break;
2596 case 'g':
2597 case 'L':
2598 size = 8;
2599 p++;
2600 break;
2601 default:
2602 goto next;
2603 }
2604 }
2605 next:
blueswir1cd390082008-11-16 13:53:32 +00002606 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00002607 monitor_printf(mon, "invalid char in format: '%c'\n",
2608 *p);
bellard9307c4c2004-04-04 12:57:25 +00002609 goto fail;
2610 }
bellard9307c4c2004-04-04 12:57:25 +00002611 if (format < 0)
2612 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002613 if (format != 'i') {
2614 /* for 'i', not specifying a size gives -1 as size */
2615 if (size < 0)
2616 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002617 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002618 }
bellard9307c4c2004-04-04 12:57:25 +00002619 default_fmt_format = format;
2620 } else {
2621 count = 1;
2622 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002623 if (format != 'i') {
2624 size = default_fmt_size;
2625 } else {
2626 size = -1;
2627 }
bellard9307c4c2004-04-04 12:57:25 +00002628 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002629 qdict_put(qdict, "count", qint_from_int(count));
2630 qdict_put(qdict, "format", qint_from_int(format));
2631 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00002632 }
2633 break;
2634 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002635 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002636 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00002637 {
blueswir1c2efc952007-09-25 17:28:42 +00002638 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00002639
blueswir1cd390082008-11-16 13:53:32 +00002640 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002641 p++;
bellard34405572004-06-08 00:55:58 +00002642 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002643 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03002644 if (*p == '\0') {
2645 typestr++;
2646 break;
2647 }
bellard34405572004-06-08 00:55:58 +00002648 } else {
2649 if (*p == '.') {
2650 p++;
blueswir1cd390082008-11-16 13:53:32 +00002651 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00002652 p++;
bellard34405572004-06-08 00:55:58 +00002653 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03002654 typestr++;
2655 break;
bellard34405572004-06-08 00:55:58 +00002656 }
2657 }
bellard13224a82006-07-14 22:03:35 +00002658 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002659 }
aliguori376253e2009-03-05 23:01:23 +00002660 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00002661 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002662 /* Check if 'i' is greater than 32-bit */
2663 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
Bandan Dasae502122015-06-03 18:38:08 -04002664 monitor_printf(mon, "\'%s\' has failed: ", cmd->name);
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002665 monitor_printf(mon, "integer is for 32-bit values\n");
2666 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002667 } else if (c == 'M') {
Luiz Capitulino91162842012-04-26 17:34:30 -03002668 if (val < 0) {
2669 monitor_printf(mon, "enter a positive value\n");
2670 goto fail;
2671 }
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002672 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002673 }
Luiz Capitulino53773582009-08-28 15:27:25 -03002674 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00002675 }
2676 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02002677 case 'o':
2678 {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01002679 int64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02002680 char *end;
2681
2682 while (qemu_isspace(*p)) {
2683 p++;
2684 }
2685 if (*typestr == '?') {
2686 typestr++;
2687 if (*p == '\0') {
2688 break;
2689 }
2690 }
Marc-André Lureau4677bb42015-09-16 18:02:56 +02002691 val = qemu_strtosz(p, &end);
Jes Sorensendbc0c672010-10-21 17:15:47 +02002692 if (val < 0) {
2693 monitor_printf(mon, "invalid size\n");
2694 goto fail;
2695 }
2696 qdict_put(qdict, key, qint_from_int(val));
2697 p = end;
2698 }
2699 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01002700 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002701 {
2702 double val;
2703
2704 while (qemu_isspace(*p))
2705 p++;
2706 if (*typestr == '?') {
2707 typestr++;
2708 if (*p == '\0') {
2709 break;
2710 }
2711 }
2712 if (get_double(mon, &val, &p) < 0) {
2713 goto fail;
2714 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02002715 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01002716 switch (*p) {
2717 case 'm':
2718 val /= 1e3; p += 2; break;
2719 case 'u':
2720 val /= 1e6; p += 2; break;
2721 case 'n':
2722 val /= 1e9; p += 2; break;
2723 }
2724 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002725 if (*p && !qemu_isspace(*p)) {
2726 monitor_printf(mon, "Unknown unit suffix\n");
2727 goto fail;
2728 }
2729 qdict_put(qdict, key, qfloat_from_double(val));
2730 }
2731 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002732 case 'b':
2733 {
2734 const char *beg;
Eric Blakefc48ffc2015-05-15 16:24:59 -06002735 bool val;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002736
2737 while (qemu_isspace(*p)) {
2738 p++;
2739 }
2740 beg = p;
2741 while (qemu_isgraph(*p)) {
2742 p++;
2743 }
2744 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06002745 val = true;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002746 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06002747 val = false;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002748 } else {
2749 monitor_printf(mon, "Expected 'on' or 'off'\n");
2750 goto fail;
2751 }
Eric Blakefc48ffc2015-05-15 16:24:59 -06002752 qdict_put(qdict, key, qbool_from_bool(val));
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002753 }
2754 break;
bellard9307c4c2004-04-04 12:57:25 +00002755 case '-':
2756 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002757 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03002758 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00002759 /* option */
ths3b46e622007-09-17 08:09:54 +00002760
bellard9307c4c2004-04-04 12:57:25 +00002761 c = *typestr++;
2762 if (c == '\0')
2763 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00002764 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002765 p++;
bellard9307c4c2004-04-04 12:57:25 +00002766 if (*p == '-') {
2767 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002768 if(c != *p) {
2769 if(!is_valid_option(p, typestr)) {
2770
2771 monitor_printf(mon, "%s: unsupported option -%c\n",
Bandan Dasae502122015-06-03 18:38:08 -04002772 cmd->name, *p);
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002773 goto fail;
2774 } else {
2775 skip_key = 1;
2776 }
bellard9307c4c2004-04-04 12:57:25 +00002777 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002778 if(skip_key) {
2779 p = tmp;
2780 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03002781 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002782 p++;
Eric Blakefc48ffc2015-05-15 16:24:59 -06002783 qdict_put(qdict, key, qbool_from_bool(true));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002784 }
bellard9307c4c2004-04-04 12:57:25 +00002785 }
bellard9307c4c2004-04-04 12:57:25 +00002786 }
2787 break;
Wenchao Xia129be002013-08-27 20:38:26 +08002788 case 'S':
2789 {
2790 /* package all remaining string */
2791 int len;
2792
2793 while (qemu_isspace(*p)) {
2794 p++;
2795 }
2796 if (*typestr == '?') {
2797 typestr++;
2798 if (*p == '\0') {
2799 /* no remaining string: NULL argument */
2800 break;
2801 }
2802 }
2803 len = strlen(p);
2804 if (len <= 0) {
2805 monitor_printf(mon, "%s: string expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002806 cmd->name);
Bandan Dase549d2a2015-06-03 18:38:10 -04002807 goto fail;
Wenchao Xia129be002013-08-27 20:38:26 +08002808 }
2809 qdict_put(qdict, key, qstring_from_str(p));
2810 p += len;
2811 }
2812 break;
bellard9307c4c2004-04-04 12:57:25 +00002813 default:
2814 bad_type:
Bandan Dasae502122015-06-03 18:38:08 -04002815 monitor_printf(mon, "%s: unknown type '%c'\n", cmd->name, c);
bellard9307c4c2004-04-04 12:57:25 +00002816 goto fail;
2817 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002818 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002819 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00002820 }
2821 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00002822 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002823 p++;
2824 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00002825 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
Bandan Dasae502122015-06-03 18:38:08 -04002826 cmd->name);
bellard9307c4c2004-04-04 12:57:25 +00002827 goto fail;
2828 }
2829
Bandan Dasae502122015-06-03 18:38:08 -04002830 return qdict;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02002831
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002832fail:
Bandan Dasae502122015-06-03 18:38:08 -04002833 QDECREF(qdict);
Anthony Liguori7267c092011-08-20 22:09:37 -05002834 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002835 return NULL;
2836}
2837
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01002838static void handle_hmp_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002839{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002840 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05002841 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002842
Bandan Dasae502122015-06-03 18:38:08 -04002843 cmd = monitor_parse_command(mon, &cmdline, mon->cmd_table);
2844 if (!cmd) {
2845 return;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002846 }
2847
Bandan Dasae502122015-06-03 18:38:08 -04002848 qdict = monitor_parse_arguments(mon, &cmdline, cmd);
2849 if (!qdict) {
Bandan Dasdd41eea2015-06-03 18:38:09 -04002850 monitor_printf(mon, "Try \"help %s\" for more information\n",
2851 cmd->name);
Bandan Dasae502122015-06-03 18:38:08 -04002852 return;
2853 }
2854
2855 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002856 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00002857}
2858
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08002859static void cmd_completion(Monitor *mon, const char *name, const char *list)
bellard81d09122004-07-14 17:21:37 +00002860{
2861 const char *p, *pstart;
2862 char cmd[128];
2863 int len;
2864
2865 p = list;
2866 for(;;) {
2867 pstart = p;
2868 p = strchr(p, '|');
2869 if (!p)
2870 p = pstart + strlen(pstart);
2871 len = p - pstart;
2872 if (len > sizeof(cmd) - 2)
2873 len = sizeof(cmd) - 2;
2874 memcpy(cmd, pstart, len);
2875 cmd[len] = '\0';
2876 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08002877 readline_add_completion(mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00002878 }
2879 if (*p == '\0')
2880 break;
2881 p++;
2882 }
2883}
2884
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08002885static void file_completion(Monitor *mon, const char *input)
bellard81d09122004-07-14 17:21:37 +00002886{
2887 DIR *ffs;
2888 struct dirent *d;
2889 char path[1024];
2890 char file[1024], file_prefix[1024];
2891 int input_path_len;
2892 const char *p;
2893
ths5fafdf22007-09-16 21:08:06 +00002894 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00002895 if (!p) {
2896 input_path_len = 0;
2897 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00002898 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00002899 } else {
2900 input_path_len = p - input + 1;
2901 memcpy(path, input, input_path_len);
2902 if (input_path_len > sizeof(path) - 1)
2903 input_path_len = sizeof(path) - 1;
2904 path[input_path_len] = '\0';
2905 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2906 }
Bandan Das19f2db52015-06-03 18:38:07 -04002907
bellard81d09122004-07-14 17:21:37 +00002908 ffs = opendir(path);
2909 if (!ffs)
2910 return;
2911 for(;;) {
2912 struct stat sb;
2913 d = readdir(ffs);
2914 if (!d)
2915 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09002916
2917 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
2918 continue;
2919 }
2920
bellard81d09122004-07-14 17:21:37 +00002921 if (strstart(d->d_name, file_prefix, NULL)) {
2922 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00002923 if (input_path_len < sizeof(file))
2924 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2925 d->d_name);
bellard81d09122004-07-14 17:21:37 +00002926 /* stat the file to find out if it's a directory.
2927 * In that case add a slash to speed up typing long paths
2928 */
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01002929 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
blueswir1363a37d2008-08-21 17:58:08 +00002930 pstrcat(file, sizeof(file), "/");
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01002931 }
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08002932 readline_add_completion(mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00002933 }
2934 }
2935 closedir(ffs);
2936}
2937
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002938static const char *next_arg_type(const char *typestr)
2939{
2940 const char *p = strchr(typestr, ':');
2941 return (p != NULL ? ++p : typestr);
2942}
2943
Hani Benhabiles40d19392014-05-07 23:41:30 +01002944static void add_completion_option(ReadLineState *rs, const char *str,
2945 const char *option)
2946{
2947 if (!str || !option) {
2948 return;
2949 }
2950 if (!strncmp(option, str, strlen(str))) {
2951 readline_add_completion(rs, option);
2952 }
2953}
2954
Hani Benhabiles13e315d2014-05-07 23:41:29 +01002955void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
2956{
2957 size_t len;
2958 ChardevBackendInfoList *list, *start;
2959
2960 if (nb_args != 2) {
2961 return;
2962 }
2963 len = strlen(str);
2964 readline_set_completion_index(rs, len);
2965
2966 start = list = qmp_query_chardev_backends(NULL);
2967 while (list) {
2968 const char *chr_name = list->value->name;
2969
2970 if (!strncmp(chr_name, str, len)) {
2971 readline_add_completion(rs, chr_name);
2972 }
2973 list = list->next;
2974 }
2975 qapi_free_ChardevBackendInfoList(start);
2976}
2977
Hani Benhabilesb162b492014-05-07 23:41:31 +01002978void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
2979{
2980 size_t len;
2981 int i;
2982
2983 if (nb_args != 2) {
2984 return;
2985 }
2986 len = strlen(str);
2987 readline_set_completion_index(rs, len);
2988 for (i = 0; NetClientOptionsKind_lookup[i]; i++) {
2989 add_completion_option(rs, str, NetClientOptionsKind_lookup[i]);
2990 }
2991}
2992
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01002993void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles992d3e62014-02-06 23:30:11 +01002994{
2995 GSList *list, *elt;
2996 size_t len;
2997
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01002998 if (nb_args != 2) {
2999 return;
3000 }
3001
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003002 len = strlen(str);
3003 readline_set_completion_index(rs, len);
3004 list = elt = object_class_get_list(TYPE_DEVICE, false);
3005 while (elt) {
3006 const char *name;
3007 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
3008 TYPE_DEVICE);
3009 name = object_class_get_name(OBJECT_CLASS(dc));
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003010
3011 if (!dc->cannot_instantiate_with_device_add_yet
3012 && !strncmp(name, str, len)) {
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003013 readline_add_completion(rs, name);
3014 }
3015 elt = elt->next;
3016 }
3017 g_slist_free(list);
3018}
3019
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003020void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003021{
3022 GSList *list, *elt;
3023 size_t len;
3024
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003025 if (nb_args != 2) {
3026 return;
3027 }
3028
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003029 len = strlen(str);
3030 readline_set_completion_index(rs, len);
3031 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
3032 while (elt) {
3033 const char *name;
3034
3035 name = object_class_get_name(OBJECT_CLASS(elt->data));
3036 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
3037 readline_add_completion(rs, name);
3038 }
3039 elt = elt->next;
3040 }
3041 g_slist_free(list);
3042}
3043
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003044static void peripheral_device_del_completion(ReadLineState *rs,
3045 const char *str, size_t len)
3046{
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003047 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
3048 GSList *list, *item;
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003049
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003050 list = qdev_build_hotpluggable_device_list(peripheral);
3051 if (!list) {
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003052 return;
3053 }
3054
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003055 for (item = list; item; item = g_slist_next(item)) {
3056 DeviceState *dev = item->data;
3057
3058 if (dev->id && !strncmp(str, dev->id, len)) {
3059 readline_add_completion(rs, dev->id);
3060 }
3061 }
3062
3063 g_slist_free(list);
3064}
3065
Hani Benhabiles6297d9a2014-05-07 23:41:28 +01003066void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3067{
3068 size_t len;
3069 ChardevInfoList *list, *start;
3070
3071 if (nb_args != 2) {
3072 return;
3073 }
3074 len = strlen(str);
3075 readline_set_completion_index(rs, len);
3076
3077 start = list = qmp_query_chardev(NULL);
3078 while (list) {
3079 ChardevInfo *chr = list->value;
3080
3081 if (!strncmp(chr->label, str, len)) {
3082 readline_add_completion(rs, chr->label);
3083 }
3084 list = list->next;
3085 }
3086 qapi_free_ChardevInfoList(start);
3087}
3088
Hani Benhabiles8e597772014-05-27 23:39:30 +01003089static void ringbuf_completion(ReadLineState *rs, const char *str)
3090{
3091 size_t len;
3092 ChardevInfoList *list, *start;
3093
3094 len = strlen(str);
3095 readline_set_completion_index(rs, len);
3096
3097 start = list = qmp_query_chardev(NULL);
3098 while (list) {
3099 ChardevInfo *chr_info = list->value;
3100
3101 if (!strncmp(chr_info->label, str, len)) {
3102 CharDriverState *chr = qemu_chr_find(chr_info->label);
3103 if (chr && chr_is_ringbuf(chr)) {
3104 readline_add_completion(rs, chr_info->label);
3105 }
3106 }
3107 list = list->next;
3108 }
3109 qapi_free_ChardevInfoList(start);
3110}
3111
Hani Benhabiles8e597772014-05-27 23:39:30 +01003112void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
3113{
3114 if (nb_args != 2) {
3115 return;
3116 }
3117 ringbuf_completion(rs, str);
3118}
3119
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003120void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
3121{
3122 size_t len;
3123
3124 if (nb_args != 2) {
3125 return;
3126 }
3127
3128 len = strlen(str);
3129 readline_set_completion_index(rs, len);
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003130 peripheral_device_del_completion(rs, str, len);
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003131}
3132
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003133void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003134{
3135 ObjectPropertyInfoList *list, *start;
3136 size_t len;
3137
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003138 if (nb_args != 2) {
3139 return;
3140 }
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003141 len = strlen(str);
3142 readline_set_completion_index(rs, len);
3143
3144 start = list = qmp_qom_list("/objects", NULL);
3145 while (list) {
3146 ObjectPropertyInfo *info = list->value;
3147
3148 if (!strncmp(info->type, "child<", 5)
3149 && !strncmp(info->name, str, len)) {
3150 readline_add_completion(rs, info->name);
3151 }
3152 list = list->next;
3153 }
3154 qapi_free_ObjectPropertyInfoList(start);
3155}
3156
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003157void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
3158{
3159 int i;
3160 char *sep;
3161 size_t len;
3162
3163 if (nb_args != 2) {
3164 return;
3165 }
3166 sep = strrchr(str, '-');
3167 if (sep) {
3168 str = sep + 1;
3169 }
3170 len = strlen(str);
3171 readline_set_completion_index(rs, len);
3172 for (i = 0; i < Q_KEY_CODE_MAX; i++) {
3173 if (!strncmp(str, QKeyCode_lookup[i], len)) {
3174 readline_add_completion(rs, QKeyCode_lookup[i]);
3175 }
3176 }
3177}
3178
Hani Benhabiles40d19392014-05-07 23:41:30 +01003179void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
3180{
3181 size_t len;
3182
3183 len = strlen(str);
3184 readline_set_completion_index(rs, len);
3185 if (nb_args == 2) {
Jason Wangeaed4832015-04-23 14:21:38 +08003186 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles40d19392014-05-07 23:41:30 +01003187 int count, i;
3188 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08003189 NET_CLIENT_OPTIONS_KIND_NONE,
3190 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003191 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles40d19392014-05-07 23:41:30 +01003192 const char *name = ncs[i]->name;
3193 if (!strncmp(str, name, len)) {
3194 readline_add_completion(rs, name);
3195 }
3196 }
3197 } else if (nb_args == 3) {
3198 add_completion_option(rs, str, "on");
3199 add_completion_option(rs, str, "off");
3200 }
3201}
3202
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003203void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
3204{
3205 int len, count, i;
Jason Wangeaed4832015-04-23 14:21:38 +08003206 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003207
3208 if (nb_args != 2) {
3209 return;
3210 }
3211
3212 len = strlen(str);
3213 readline_set_completion_index(rs, len);
3214 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_OPTIONS_KIND_NIC,
Jason Wangeaed4832015-04-23 14:21:38 +08003215 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003216 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003217 QemuOpts *opts;
3218 const char *name = ncs[i]->name;
3219 if (strncmp(str, name, len)) {
3220 continue;
3221 }
3222 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
3223 if (opts) {
3224 readline_add_completion(rs, name);
3225 }
3226 }
3227}
3228
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003229void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
3230{
3231 size_t len;
3232
3233 len = strlen(str);
3234 readline_set_completion_index(rs, len);
3235 if (nb_args == 2) {
3236 TraceEventID id;
3237 for (id = 0; id < trace_event_count(); id++) {
3238 const char *event_name = trace_event_get_name(trace_event_id(id));
3239 if (!strncmp(str, event_name, len)) {
3240 readline_add_completion(rs, event_name);
3241 }
3242 }
3243 } else if (nb_args == 3) {
3244 add_completion_option(rs, str, "on");
3245 add_completion_option(rs, str, "off");
3246 }
3247}
3248
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003249void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
3250{
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003251 int i;
3252
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003253 if (nb_args != 2) {
3254 return;
3255 }
3256 readline_set_completion_index(rs, strlen(str));
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003257 for (i = 0; WatchdogExpirationAction_lookup[i]; i++) {
3258 add_completion_option(rs, str, WatchdogExpirationAction_lookup[i]);
3259 }
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003260}
3261
Hani Benhabilesc68a0402014-05-27 23:39:32 +01003262void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
3263 const char *str)
3264{
3265 size_t len;
3266
3267 len = strlen(str);
3268 readline_set_completion_index(rs, len);
3269 if (nb_args == 2) {
3270 int i;
3271 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
3272 const char *name = MigrationCapability_lookup[i];
3273 if (!strncmp(str, name, len)) {
3274 readline_add_completion(rs, name);
3275 }
3276 }
3277 } else if (nb_args == 3) {
3278 add_completion_option(rs, str, "on");
3279 add_completion_option(rs, str, "off");
3280 }
3281}
3282
Liang Li50e9a622015-03-23 16:32:29 +08003283void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
3284 const char *str)
3285{
3286 size_t len;
3287
3288 len = strlen(str);
3289 readline_set_completion_index(rs, len);
3290 if (nb_args == 2) {
3291 int i;
3292 for (i = 0; i < MIGRATION_PARAMETER_MAX; i++) {
3293 const char *name = MigrationParameter_lookup[i];
3294 if (!strncmp(str, name, len)) {
3295 readline_add_completion(rs, name);
3296 }
3297 }
3298 }
3299}
3300
Hani Benhabilese3bb5322014-05-27 23:39:34 +01003301void host_net_add_completion(ReadLineState *rs, int nb_args, const char *str)
3302{
3303 int i;
3304 size_t len;
3305 if (nb_args != 2) {
3306 return;
3307 }
3308 len = strlen(str);
3309 readline_set_completion_index(rs, len);
3310 for (i = 0; host_net_devices[i]; i++) {
3311 if (!strncmp(host_net_devices[i], str, len)) {
3312 readline_add_completion(rs, host_net_devices[i]);
3313 }
3314 }
3315}
3316
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003317void host_net_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3318{
Jason Wangeaed4832015-04-23 14:21:38 +08003319 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003320 int count, i, len;
3321
3322 len = strlen(str);
3323 readline_set_completion_index(rs, len);
3324 if (nb_args == 2) {
3325 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08003326 NET_CLIENT_OPTIONS_KIND_NONE,
3327 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003328 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003329 int id;
3330 char name[16];
3331
3332 if (net_hub_id_for_client(ncs[i], &id)) {
3333 continue;
3334 }
3335 snprintf(name, sizeof(name), "%d", id);
3336 if (!strncmp(str, name, len)) {
3337 readline_add_completion(rs, name);
3338 }
3339 }
3340 return;
3341 } else if (nb_args == 3) {
3342 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08003343 NET_CLIENT_OPTIONS_KIND_NIC,
3344 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003345 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Jason Wang2c4681f2015-02-02 15:06:38 +08003346 int id;
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003347 const char *name;
3348
Jason Wang2c4681f2015-02-02 15:06:38 +08003349 if (ncs[i]->info->type == NET_CLIENT_OPTIONS_KIND_HUBPORT ||
3350 net_hub_id_for_client(ncs[i], &id)) {
3351 continue;
3352 }
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003353 name = ncs[i]->name;
3354 if (!strncmp(str, name, len)) {
3355 readline_add_completion(rs, name);
3356 }
3357 }
3358 return;
3359 }
3360}
3361
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003362static void vm_completion(ReadLineState *rs, const char *str)
3363{
3364 size_t len;
3365 BlockDriverState *bs = NULL;
3366
3367 len = strlen(str);
3368 readline_set_completion_index(rs, len);
3369 while ((bs = bdrv_next(bs))) {
3370 SnapshotInfoList *snapshots, *snapshot;
3371
3372 if (!bdrv_can_snapshot(bs)) {
3373 continue;
3374 }
3375 if (bdrv_query_snapshot_info_list(bs, &snapshots, NULL)) {
3376 continue;
3377 }
3378 snapshot = snapshots;
3379 while (snapshot) {
3380 char *completion = snapshot->value->name;
3381 if (!strncmp(str, completion, len)) {
3382 readline_add_completion(rs, completion);
3383 }
3384 completion = snapshot->value->id;
3385 if (!strncmp(str, completion, len)) {
3386 readline_add_completion(rs, completion);
3387 }
3388 snapshot = snapshot->next;
3389 }
3390 qapi_free_SnapshotInfoList(snapshots);
3391 }
3392
3393}
3394
3395void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
3396{
3397 if (nb_args == 2) {
3398 vm_completion(rs, str);
3399 }
3400}
3401
3402void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
3403{
3404 if (nb_args == 2) {
3405 vm_completion(rs, str);
3406 }
3407}
3408
Wenchao Xiac35b6402013-08-27 20:38:24 +08003409static void monitor_find_completion_by_table(Monitor *mon,
3410 const mon_cmd_t *cmd_table,
3411 char **args,
3412 int nb_args)
bellard81d09122004-07-14 17:21:37 +00003413{
3414 const char *cmdname;
Wenchao Xiac35b6402013-08-27 20:38:24 +08003415 int i;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003416 const char *ptype, *str, *name;
Anthony Liguoric227f092009-10-01 16:12:16 -05003417 const mon_cmd_t *cmd;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003418 BlockDriverState *bs;
bellard81d09122004-07-14 17:21:37 +00003419
bellard81d09122004-07-14 17:21:37 +00003420 if (nb_args <= 1) {
3421 /* command completion */
3422 if (nb_args == 0)
3423 cmdname = "";
3424 else
3425 cmdname = args[0];
Wenchao Xiad2674b22013-08-27 20:38:16 +08003426 readline_set_completion_index(mon->rs, strlen(cmdname));
Wenchao Xiac35b6402013-08-27 20:38:24 +08003427 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003428 cmd_completion(mon, cmdname, cmd->name);
bellard81d09122004-07-14 17:21:37 +00003429 }
3430 } else {
3431 /* find the command */
Wenchao Xiac35b6402013-08-27 20:38:24 +08003432 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Jan Kiszka03a63482010-06-16 00:38:33 +02003433 if (compare_cmd(args[0], cmd->name)) {
3434 break;
3435 }
bellard81d09122004-07-14 17:21:37 +00003436 }
Jan Kiszka03a63482010-06-16 00:38:33 +02003437 if (!cmd->name) {
Wenchao Xiac35b6402013-08-27 20:38:24 +08003438 return;
Jan Kiszka03a63482010-06-16 00:38:33 +02003439 }
3440
Wenchao Xiad903a772013-08-27 20:38:25 +08003441 if (cmd->sub_table) {
3442 /* do the job again */
Stefan Weile7ae7712015-03-08 19:30:01 +01003443 monitor_find_completion_by_table(mon, cmd->sub_table,
3444 &args[1], nb_args - 1);
3445 return;
Wenchao Xiad903a772013-08-27 20:38:25 +08003446 }
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003447 if (cmd->command_completion) {
Stefan Weile7ae7712015-03-08 19:30:01 +01003448 cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
3449 return;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003450 }
Wenchao Xiad903a772013-08-27 20:38:25 +08003451
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003452 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003453 for(i = 0; i < nb_args - 2; i++) {
3454 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003455 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003456 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003457 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003458 }
3459 }
3460 str = args[nb_args - 1];
Kevin Wolf48fe86f2014-11-12 16:24:02 +01003461 while (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02003462 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00003463 }
bellard81d09122004-07-14 17:21:37 +00003464 switch(*ptype) {
3465 case 'F':
3466 /* file completion */
Wenchao Xiad2674b22013-08-27 20:38:16 +08003467 readline_set_completion_index(mon->rs, strlen(str));
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003468 file_completion(mon, str);
bellard81d09122004-07-14 17:21:37 +00003469 break;
3470 case 'B':
3471 /* block device name completion */
Wenchao Xia599a9262013-08-27 20:38:15 +08003472 readline_set_completion_index(mon->rs, strlen(str));
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003473 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
3474 name = bdrv_get_device_name(bs);
3475 if (str[0] == '\0' ||
3476 !strncmp(name, str, strlen(str))) {
3477 readline_add_completion(mon->rs, name);
3478 }
3479 }
bellard81d09122004-07-14 17:21:37 +00003480 break;
bellard7fe48482004-10-09 18:08:01 +00003481 case 's':
Wenchao Xia129be002013-08-27 20:38:26 +08003482 case 'S':
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003483 if (!strcmp(cmd->name, "help|?")) {
Wenchao Xia7ca0e062013-08-27 20:38:27 +08003484 monitor_find_completion_by_table(mon, cmd_table,
3485 &args[1], nb_args - 1);
bellard7fe48482004-10-09 18:08:01 +00003486 }
3487 break;
bellard81d09122004-07-14 17:21:37 +00003488 default:
3489 break;
3490 }
3491 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08003492}
3493
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003494static void monitor_find_completion(void *opaque,
Wenchao Xiac35b6402013-08-27 20:38:24 +08003495 const char *cmdline)
3496{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003497 Monitor *mon = opaque;
Wenchao Xiac35b6402013-08-27 20:38:24 +08003498 char *args[MAX_ARGS];
3499 int nb_args, len;
3500
3501 /* 1. parse the cmdline */
3502 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
3503 return;
3504 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08003505
3506 /* if the line ends with a space, it means we want to complete the
3507 next arg */
3508 len = strlen(cmdline);
3509 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
3510 if (nb_args >= MAX_ARGS) {
3511 goto cleanup;
3512 }
3513 args[nb_args++] = g_strdup("");
3514 }
3515
3516 /* 2. auto complete according to args */
3517 monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args);
Jan Kiszka03a63482010-06-16 00:38:33 +02003518
3519cleanup:
Wenchao Xiadcc70cd2013-08-27 20:38:22 +08003520 free_cmdline_args(args, nb_args);
bellard81d09122004-07-14 17:21:37 +00003521}
3522
aliguori731b0362009-03-05 23:01:42 +00003523static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003524{
aliguori731b0362009-03-05 23:01:42 +00003525 Monitor *mon = opaque;
3526
Jan Kiszkac62313b2009-12-04 14:05:29 +01003527 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003528}
3529
Markus Armbruster40861822015-05-29 10:27:16 +02003530static bool invalid_qmp_mode(const Monitor *mon, const mon_cmd_t *cmd,
3531 Error **errp)
Luiz Capitulino09069b12010-02-04 18:10:06 -02003532{
Markus Armbruster485febc2015-03-13 17:25:50 +01003533 bool is_cap = cmd->mhandler.cmd_new == qmp_capabilities;
Markus Armbrusterf994b252015-03-06 19:51:51 +01003534
3535 if (is_cap && mon->qmp.in_command_mode) {
Markus Armbruster40861822015-05-29 10:27:16 +02003536 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
3537 "Capabilities negotiation is already complete, command "
3538 "'%s' ignored", cmd->name);
Eric Blake2d5a8342015-04-15 09:19:23 -06003539 return true;
3540 }
Markus Armbrusterf994b252015-03-06 19:51:51 +01003541 if (!is_cap && !mon->qmp.in_command_mode) {
Markus Armbruster40861822015-05-29 10:27:16 +02003542 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
3543 "Expecting capabilities negotiation with "
3544 "'qmp_capabilities' before command '%s'", cmd->name);
Eric Blake2d5a8342015-04-15 09:19:23 -06003545 return true;
3546 }
3547 return false;
Luiz Capitulino09069b12010-02-04 18:10:06 -02003548}
3549
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003550/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03003551 * Argument validation rules:
3552 *
3553 * 1. The argument must exist in cmd_args qdict
3554 * 2. The argument type must be the expected one
3555 *
3556 * Special case: If the argument doesn't exist in cmd_args and
3557 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
3558 * checking is skipped for it.
3559 */
Markus Armbruster326283a2015-03-02 18:39:09 +01003560static void check_client_args_type(const QDict *client_args,
3561 const QDict *cmd_args, int flags,
3562 Error **errp)
Luiz Capitulino4af91932010-06-22 11:44:05 -03003563{
3564 const QDictEntry *ent;
3565
3566 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
3567 QObject *obj;
3568 QString *arg_type;
3569 const QObject *client_arg = qdict_entry_value(ent);
3570 const char *client_arg_name = qdict_entry_key(ent);
3571
3572 obj = qdict_get(cmd_args, client_arg_name);
3573 if (!obj) {
3574 if (flags & QMP_ACCEPT_UNKNOWNS) {
3575 /* handler accepts unknowns */
3576 continue;
3577 }
3578 /* client arg doesn't exist */
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003579 error_setg(errp, QERR_INVALID_PARAMETER, client_arg_name);
Markus Armbruster326283a2015-03-02 18:39:09 +01003580 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003581 }
3582
3583 arg_type = qobject_to_qstring(obj);
3584 assert(arg_type != NULL);
3585
3586 /* check if argument's type is correct */
3587 switch (qstring_get_str(arg_type)[0]) {
3588 case 'F':
3589 case 'B':
3590 case 's':
3591 if (qobject_type(client_arg) != QTYPE_QSTRING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003592 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3593 client_arg_name, "string");
Markus Armbruster326283a2015-03-02 18:39:09 +01003594 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003595 }
3596 break;
3597 case 'i':
3598 case 'l':
3599 case 'M':
Jes Sorensendbc0c672010-10-21 17:15:47 +02003600 case 'o':
Luiz Capitulino4af91932010-06-22 11:44:05 -03003601 if (qobject_type(client_arg) != QTYPE_QINT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003602 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3603 client_arg_name, "int");
Markus Armbruster326283a2015-03-02 18:39:09 +01003604 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003605 }
3606 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003607 case 'T':
3608 if (qobject_type(client_arg) != QTYPE_QINT &&
3609 qobject_type(client_arg) != QTYPE_QFLOAT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003610 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3611 client_arg_name, "number");
Markus Armbruster326283a2015-03-02 18:39:09 +01003612 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003613 }
3614 break;
3615 case 'b':
3616 case '-':
3617 if (qobject_type(client_arg) != QTYPE_QBOOL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003618 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3619 client_arg_name, "bool");
Markus Armbruster326283a2015-03-02 18:39:09 +01003620 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003621 }
3622 break;
3623 case 'O':
3624 assert(flags & QMP_ACCEPT_UNKNOWNS);
3625 break;
Paolo Bonzinib9f89782012-03-22 12:51:11 +01003626 case 'q':
3627 /* Any QObject can be passed. */
3628 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003629 case '/':
3630 case '.':
3631 /*
3632 * These types are not supported by QMP and thus are not
3633 * handled here. Fall through.
3634 */
3635 default:
3636 abort();
3637 }
3638 }
Luiz Capitulino4af91932010-06-22 11:44:05 -03003639}
3640
3641/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003642 * - Check if the client has passed all mandatory args
3643 * - Set special flags for argument validation
3644 */
Markus Armbruster326283a2015-03-02 18:39:09 +01003645static void check_mandatory_args(const QDict *cmd_args,
3646 const QDict *client_args, int *flags,
3647 Error **errp)
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003648{
3649 const QDictEntry *ent;
3650
3651 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
3652 const char *cmd_arg_name = qdict_entry_key(ent);
3653 QString *type = qobject_to_qstring(qdict_entry_value(ent));
3654 assert(type != NULL);
3655
3656 if (qstring_get_str(type)[0] == 'O') {
3657 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
3658 *flags |= QMP_ACCEPT_UNKNOWNS;
3659 } else if (qstring_get_str(type)[0] != '-' &&
3660 qstring_get_str(type)[1] != '?' &&
3661 !qdict_haskey(client_args, cmd_arg_name)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003662 error_setg(errp, QERR_MISSING_PARAMETER, cmd_arg_name);
Markus Armbruster326283a2015-03-02 18:39:09 +01003663 return;
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003664 }
3665 }
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003666}
3667
3668static QDict *qdict_from_args_type(const char *args_type)
3669{
3670 int i;
3671 QDict *qdict;
3672 QString *key, *type, *cur_qs;
3673
3674 assert(args_type != NULL);
3675
3676 qdict = qdict_new();
3677
3678 if (args_type == NULL || args_type[0] == '\0') {
3679 /* no args, empty qdict */
3680 goto out;
3681 }
3682
3683 key = qstring_new();
3684 type = qstring_new();
3685
3686 cur_qs = key;
3687
3688 for (i = 0;; i++) {
3689 switch (args_type[i]) {
3690 case ',':
3691 case '\0':
3692 qdict_put(qdict, qstring_get_str(key), type);
3693 QDECREF(key);
3694 if (args_type[i] == '\0') {
3695 goto out;
3696 }
3697 type = qstring_new(); /* qdict has ref */
3698 cur_qs = key = qstring_new();
3699 break;
3700 case ':':
3701 cur_qs = type;
3702 break;
3703 default:
3704 qstring_append_chr(cur_qs, args_type[i]);
3705 break;
3706 }
3707 }
3708
3709out:
3710 return qdict;
3711}
3712
3713/*
3714 * Client argument checking rules:
3715 *
3716 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03003717 * 2. Each argument provided by the client must be expected
3718 * 3. Each argument provided by the client must have the type expected
3719 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003720 */
Markus Armbruster326283a2015-03-02 18:39:09 +01003721static void qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args,
3722 Error **errp)
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003723{
Markus Armbruster326283a2015-03-02 18:39:09 +01003724 Error *err = NULL;
3725 int flags;
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003726 QDict *cmd_args;
3727
3728 cmd_args = qdict_from_args_type(cmd->args_type);
3729
3730 flags = 0;
Markus Armbruster326283a2015-03-02 18:39:09 +01003731 check_mandatory_args(cmd_args, client_args, &flags, &err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003732 if (err) {
3733 goto out;
3734 }
3735
Markus Armbruster326283a2015-03-02 18:39:09 +01003736 check_client_args_type(client_args, cmd_args, flags, &err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003737
3738out:
Markus Armbruster326283a2015-03-02 18:39:09 +01003739 error_propagate(errp, err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003740 QDECREF(cmd_args);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003741}
3742
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003743/*
3744 * Input object checking rules
3745 *
3746 * 1. Input object must be a dict
3747 * 2. The "execute" key must exist
3748 * 3. The "execute" key must be a string
3749 * 4. If the "arguments" key exists, it must be a dict
3750 * 5. If the "id" key exists, it can be anything (ie. json-value)
3751 * 6. Any argument not listed above is considered invalid
3752 */
Markus Armbrusterba0510a2015-03-02 18:41:43 +01003753static QDict *qmp_check_input_obj(QObject *input_obj, Error **errp)
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003754{
3755 const QDictEntry *ent;
3756 int has_exec_key = 0;
3757 QDict *input_dict;
3758
3759 if (qobject_type(input_obj) != QTYPE_QDICT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003760 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "object");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003761 return NULL;
3762 }
3763
3764 input_dict = qobject_to_qdict(input_obj);
3765
3766 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
3767 const char *arg_name = qdict_entry_key(ent);
3768 const QObject *arg_obj = qdict_entry_value(ent);
3769
3770 if (!strcmp(arg_name, "execute")) {
3771 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003772 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
3773 "execute", "string");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003774 return NULL;
3775 }
3776 has_exec_key = 1;
3777 } else if (!strcmp(arg_name, "arguments")) {
3778 if (qobject_type(arg_obj) != QTYPE_QDICT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003779 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
3780 "arguments", "object");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003781 return NULL;
3782 }
Markus Armbruster779cec42015-06-08 10:44:30 +02003783 } else if (!strcmp(arg_name, "id")) {
3784 /* Any string is acceptable as "id", so nothing to check */
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003785 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003786 error_setg(errp, QERR_QMP_EXTRA_MEMBER, arg_name);
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003787 return NULL;
3788 }
3789 }
3790
3791 if (!has_exec_key) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003792 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "execute");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003793 return NULL;
3794 }
3795
3796 return input_dict;
3797}
3798
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003799static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
3800{
Markus Armbruster326283a2015-03-02 18:39:09 +01003801 Error *local_err = NULL;
Markus Armbruster84add862015-03-05 16:45:15 +01003802 QObject *obj, *data;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003803 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003804 const mon_cmd_t *cmd;
Luiz Capitulino40e5a012011-10-21 16:15:31 -02003805 const char *cmd_name;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003806 Monitor *mon = cur_mon;
3807
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03003808 args = input = NULL;
Markus Armbruster84add862015-03-05 16:45:15 +01003809 data = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003810
3811 obj = json_parser_parse(tokens, NULL);
3812 if (!obj) {
3813 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003814 error_setg(&local_err, QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003815 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003816 }
3817
Markus Armbrusterba0510a2015-03-02 18:41:43 +01003818 input = qmp_check_input_obj(obj, &local_err);
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003819 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003820 qobject_decref(obj);
3821 goto err_out;
3822 }
3823
Markus Armbruster74358f22015-03-06 19:35:59 +01003824 mon->qmp.id = qdict_get(input, "id");
3825 qobject_incref(mon->qmp.id);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003826
Luiz Capitulino0bbab462010-05-31 17:32:50 -03003827 cmd_name = qdict_get_str(input, "execute");
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +01003828 trace_handle_qmp_command(mon, cmd_name);
Anthony Liguorie3193602011-09-02 12:34:47 -05003829 cmd = qmp_find_cmd(cmd_name);
Eric Blake2d5a8342015-04-15 09:19:23 -06003830 if (!cmd) {
Markus Armbruster710aec92015-03-06 11:28:00 +01003831 error_set(&local_err, ERROR_CLASS_COMMAND_NOT_FOUND,
3832 "The command %s has not been found", cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03003833 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003834 }
Markus Armbruster40861822015-05-29 10:27:16 +02003835 if (invalid_qmp_mode(mon, cmd, &local_err)) {
Eric Blake2d5a8342015-04-15 09:19:23 -06003836 goto err_out;
3837 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003838
3839 obj = qdict_get(input, "arguments");
3840 if (!obj) {
3841 args = qdict_new();
3842 } else {
3843 args = qobject_to_qdict(obj);
3844 QINCREF(args);
3845 }
3846
Markus Armbruster326283a2015-03-02 18:39:09 +01003847 qmp_check_client_args(cmd, args, &local_err);
3848 if (local_err) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003849 goto err_out;
3850 }
3851
Markus Armbruster485febc2015-03-13 17:25:50 +01003852 cmd->mhandler.cmd_new(args, &data, &local_err);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003853
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003854err_out:
Markus Armbruster710aec92015-03-06 11:28:00 +01003855 monitor_protocol_emitter(mon, data, local_err);
Markus Armbruster84add862015-03-05 16:45:15 +01003856 qobject_decref(data);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03003857 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003858 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003859}
3860
Markus Armbrusterc83fe232015-03-06 19:20:51 +01003861static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003862{
3863 Monitor *old_mon = cur_mon;
3864
3865 cur_mon = opaque;
3866
Markus Armbruster74358f22015-03-06 19:35:59 +01003867 json_message_parser_feed(&cur_mon->qmp.parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003868
3869 cur_mon = old_mon;
3870}
3871
aliguori731b0362009-03-05 23:01:42 +00003872static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00003873{
aliguori731b0362009-03-05 23:01:42 +00003874 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00003875 int i;
aliguori376253e2009-03-05 23:01:23 +00003876
aliguori731b0362009-03-05 23:01:42 +00003877 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00003878
aliguoricde76ee2009-03-05 23:01:51 +00003879 if (cur_mon->rs) {
3880 for (i = 0; i < size; i++)
3881 readline_handle_byte(cur_mon->rs, buf[i]);
3882 } else {
3883 if (size == 0 || buf[size - 1] != 0)
3884 monitor_printf(cur_mon, "corrupted command\n");
3885 else
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003886 handle_hmp_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00003887 }
aliguori731b0362009-03-05 23:01:42 +00003888
3889 cur_mon = old_mon;
3890}
aliguorid8f44602008-10-06 13:52:44 +00003891
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003892static void monitor_command_cb(void *opaque, const char *cmdline,
3893 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00003894{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003895 Monitor *mon = opaque;
3896
aliguori731b0362009-03-05 23:01:42 +00003897 monitor_suspend(mon);
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003898 handle_hmp_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00003899 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00003900}
3901
aliguoricde76ee2009-03-05 23:01:51 +00003902int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003903{
aliguoricde76ee2009-03-05 23:01:51 +00003904 if (!mon->rs)
3905 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00003906 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00003907 return 0;
aliguorid8f44602008-10-06 13:52:44 +00003908}
3909
aliguori376253e2009-03-05 23:01:23 +00003910void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003911{
aliguoricde76ee2009-03-05 23:01:51 +00003912 if (!mon->rs)
3913 return;
aliguori731b0362009-03-05 23:01:42 +00003914 if (--mon->suspend_cnt == 0)
3915 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00003916}
3917
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003918static QObject *get_qmp_greeting(void)
3919{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03003920 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003921
Markus Armbruster7fad30f2015-09-16 13:06:19 +02003922 qmp_marshal_query_version(NULL, &ver, NULL);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003923 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
3924}
3925
Markus Armbrusterc83fe232015-03-06 19:20:51 +01003926static void monitor_qmp_event(void *opaque, int event)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003927{
Luiz Capitulino47116d12010-02-08 17:01:30 -02003928 QObject *data;
3929 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003930
Luiz Capitulino47116d12010-02-08 17:01:30 -02003931 switch (event) {
3932 case CHR_EVENT_OPENED:
Markus Armbrusterf994b252015-03-06 19:51:51 +01003933 mon->qmp.in_command_mode = false;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003934 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003935 monitor_json_emitter(mon, data);
3936 qobject_decref(data);
Corey Bryantefb87c12012-08-14 16:43:48 -04003937 mon_refcount++;
Luiz Capitulino47116d12010-02-08 17:01:30 -02003938 break;
3939 case CHR_EVENT_CLOSED:
Markus Armbruster74358f22015-03-06 19:35:59 +01003940 json_message_parser_destroy(&mon->qmp.parser);
3941 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Corey Bryantefb87c12012-08-14 16:43:48 -04003942 mon_refcount--;
3943 monitor_fdsets_cleanup();
Luiz Capitulino47116d12010-02-08 17:01:30 -02003944 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003945 }
3946}
3947
aliguori731b0362009-03-05 23:01:42 +00003948static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00003949{
aliguori376253e2009-03-05 23:01:23 +00003950 Monitor *mon = opaque;
3951
aliguori2724b182009-03-05 23:01:47 +00003952 switch (event) {
3953 case CHR_EVENT_MUX_IN:
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003954 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003955 mon->mux_out = 0;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003956 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003957 if (mon->reset_seen) {
3958 readline_restart(mon->rs);
3959 monitor_resume(mon);
3960 monitor_flush(mon);
3961 } else {
3962 mon->suspend_cnt = 0;
3963 }
aliguori2724b182009-03-05 23:01:47 +00003964 break;
ths86e94de2007-01-05 22:01:59 +00003965
aliguori2724b182009-03-05 23:01:47 +00003966 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003967 if (mon->reset_seen) {
3968 if (mon->suspend_cnt == 0) {
3969 monitor_printf(mon, "\n");
3970 }
3971 monitor_flush(mon);
3972 monitor_suspend(mon);
3973 } else {
3974 mon->suspend_cnt++;
3975 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003976 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003977 mon->mux_out = 1;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003978 qemu_mutex_unlock(&mon->out_lock);
aliguori2724b182009-03-05 23:01:47 +00003979 break;
3980
Amit Shahb6b8df52009-10-07 18:31:16 +05303981 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00003982 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3983 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003984 if (!mon->mux_out) {
Stratos Psomadakise5554e22014-09-15 15:34:57 +03003985 readline_restart(mon->rs);
aliguori2724b182009-03-05 23:01:47 +00003986 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003987 }
3988 mon->reset_seen = 1;
Corey Bryantefb87c12012-08-14 16:43:48 -04003989 mon_refcount++;
3990 break;
3991
3992 case CHR_EVENT_CLOSED:
3993 mon_refcount--;
3994 monitor_fdsets_cleanup();
aliguori2724b182009-03-05 23:01:47 +00003995 break;
3996 }
ths86e94de2007-01-05 22:01:59 +00003997}
3998
Wayne Xia816f8922011-10-12 11:32:41 +08003999static int
4000compare_mon_cmd(const void *a, const void *b)
4001{
4002 return strcmp(((const mon_cmd_t *)a)->name,
4003 ((const mon_cmd_t *)b)->name);
4004}
4005
4006static void sortcmdlist(void)
4007{
4008 int array_num;
4009 int elem_size = sizeof(mon_cmd_t);
4010
4011 array_num = sizeof(mon_cmds)/elem_size-1;
4012 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
4013
4014 array_num = sizeof(info_cmds)/elem_size-1;
4015 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
4016}
4017
aliguori76655d62009-03-06 20:27:37 +00004018
4019/*
4020 * Local variables:
4021 * c-indent-level: 4
4022 * c-basic-offset: 4
4023 * tab-width: 8
4024 * End:
4025 */
4026
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004027/* These functions just adapt the readline interface in a typesafe way. We
4028 * could cast function pointers but that discards compiler checks.
4029 */
Stefan Weild5d15072014-01-25 18:18:23 +01004030static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
4031 const char *fmt, ...)
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004032{
4033 va_list ap;
4034 va_start(ap, fmt);
4035 monitor_vprintf(opaque, fmt, ap);
4036 va_end(ap);
4037}
4038
4039static void monitor_readline_flush(void *opaque)
4040{
4041 monitor_flush(opaque);
4042}
4043
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004044static void __attribute__((constructor)) monitor_lock_init(void)
4045{
4046 qemu_mutex_init(&monitor_lock);
4047}
4048
aliguori731b0362009-03-05 23:01:42 +00004049void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004050{
aliguori731b0362009-03-05 23:01:42 +00004051 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004052 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004053
4054 if (is_first_init) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +02004055 monitor_qapi_event_init();
Wenchao Xiad0383172013-08-27 20:38:18 +08004056 sortcmdlist();
ths20d8a3e2007-02-18 17:04:49 +00004057 is_first_init = 0;
4058 }
aliguori87127162009-03-05 23:01:29 +00004059
Wenchao Xiab01fe892013-08-27 20:38:19 +08004060 mon = g_malloc(sizeof(*mon));
4061 monitor_data_init(mon);
ths20d8a3e2007-02-18 17:04:49 +00004062
aliguori87127162009-03-05 23:01:29 +00004063 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00004064 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004065 if (flags & MONITOR_USE_READLINE) {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004066 mon->rs = readline_init(monitor_readline_printf,
4067 monitor_readline_flush,
4068 mon,
4069 monitor_find_completion);
aliguoricde76ee2009-03-05 23:01:51 +00004070 monitor_read_command(mon, 0);
4071 }
aliguori87127162009-03-05 23:01:29 +00004072
Markus Armbruster9f3982f2015-03-06 19:56:38 +01004073 if (monitor_is_qmp(mon)) {
Markus Armbrusterc83fe232015-03-06 19:20:51 +01004074 qemu_chr_add_handlers(chr, monitor_can_read, monitor_qmp_read,
4075 monitor_qmp_event, mon);
Anthony Liguori15f31512011-08-15 11:17:35 -05004076 qemu_chr_fe_set_echo(chr, true);
Markus Armbruster74358f22015-03-06 19:35:59 +01004077 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004078 } else {
4079 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4080 monitor_event, mon);
4081 }
aliguori87127162009-03-05 23:01:29 +00004082
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004083 qemu_mutex_lock(&monitor_lock);
Blue Swirl72cf2d42009-09-12 07:36:22 +00004084 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004085 qemu_mutex_unlock(&monitor_lock);
bellard7e2515e2004-08-01 21:52:19 +00004086}
4087
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004088static void bdrv_password_cb(void *opaque, const char *password,
4089 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00004090{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004091 Monitor *mon = opaque;
4092 BlockDriverState *bs = readline_opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004093 int ret = 0;
Markus Armbruster4d2855a2015-01-29 10:37:00 +01004094 Error *local_err = NULL;
bellard7e2515e2004-08-01 21:52:19 +00004095
Markus Armbruster4d2855a2015-01-29 10:37:00 +01004096 bdrv_add_key(bs, password, &local_err);
4097 if (local_err) {
4098 monitor_printf(mon, "%s\n", error_get_pretty(local_err));
4099 error_free(local_err);
aliguoribb5fc202009-03-05 23:01:15 +00004100 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004101 }
aliguori731b0362009-03-05 23:01:42 +00004102 if (mon->password_completion_cb)
4103 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004104
aliguori731b0362009-03-05 23:01:42 +00004105 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004106}
aliguoric0f4ce72009-03-05 23:01:01 +00004107
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004108int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
Markus Armbruster097310b2014-10-07 13:59:15 +02004109 BlockCompletionFunc *completion_cb,
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004110 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004111{
aliguoricde76ee2009-03-05 23:01:51 +00004112 int err;
4113
aliguori376253e2009-03-05 23:01:23 +00004114 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4115 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00004116
aliguori731b0362009-03-05 23:01:42 +00004117 mon->password_completion_cb = completion_cb;
4118 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004119
aliguoricde76ee2009-03-05 23:01:51 +00004120 err = monitor_read_password(mon, bdrv_password_cb, bs);
4121
4122 if (err && completion_cb)
4123 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004124
4125 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00004126}
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004127
4128int monitor_read_block_device_key(Monitor *mon, const char *device,
Markus Armbruster097310b2014-10-07 13:59:15 +02004129 BlockCompletionFunc *completion_cb,
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004130 void *opaque)
4131{
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01004132 Error *err = NULL;
Fam Zheng55606252015-03-02 19:36:46 +08004133 BlockBackend *blk;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004134
Fam Zheng55606252015-03-02 19:36:46 +08004135 blk = blk_by_name(device);
4136 if (!blk) {
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004137 monitor_printf(mon, "Device not found %s\n", device);
4138 return -1;
4139 }
Max Reitz5433c242015-10-19 17:53:29 +02004140 if (!blk_bs(blk)) {
4141 monitor_printf(mon, "Device '%s' has no medium\n", device);
4142 return -1;
4143 }
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004144
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01004145 bdrv_add_key(blk_bs(blk), NULL, &err);
4146 if (err) {
4147 error_free(err);
4148 return monitor_read_bdrv_key_start(mon, blk_bs(blk), completion_cb, opaque);
4149 }
4150
4151 if (completion_cb) {
4152 completion_cb(opaque, 0);
4153 }
4154 return 0;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004155}
Paolo Bonzini4d454572012-11-26 16:03:42 +01004156
4157QemuOptsList qemu_mon_opts = {
4158 .name = "mon",
4159 .implied_opt_name = "chardev",
4160 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
4161 .desc = {
4162 {
4163 .name = "mode",
4164 .type = QEMU_OPT_STRING,
4165 },{
4166 .name = "chardev",
4167 .type = QEMU_OPT_STRING,
4168 },{
4169 .name = "default",
4170 .type = QEMU_OPT_BOOL,
4171 },{
4172 .name = "pretty",
4173 .type = QEMU_OPT_BOOL,
4174 },
4175 { /* end of list */ }
4176 },
4177};
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004178
4179#ifndef TARGET_I386
4180void qmp_rtc_reset_reinjection(Error **errp)
4181{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004182 error_setg(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection");
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004183}
4184#endif
Jason J. Herne7ee0c3e2015-06-26 14:03:16 -04004185
4186#ifndef TARGET_S390X
4187void qmp_dump_skeys(const char *filename, Error **errp)
4188{
4189 error_setg(errp, QERR_FEATURE_DISABLED, "dump-skeys");
4190}
4191#endif