blob: 4f1ba2f6669726fd93e881b9febc40bc6065a03d [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 */
185 int64_t rate; /* Minimum time (in ns) between two events */
186 int64_t last; /* QEMU_CLOCK_REALTIME value at last emission */
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100187 QEMUTimer *timer; /* Timer for handling delayed events */
188 QObject *data; /* Event pending delayed dispatch */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200189} MonitorQAPIEventState;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100190
aliguori87127162009-03-05 23:01:29 +0000191struct Monitor {
192 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200193 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000194 int flags;
195 int suspend_cnt;
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400196 bool skip_flush;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200197
198 QemuMutex out_lock;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400199 QString *outbuf;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200200 guint out_watch;
201
202 /* Read under either BQL or out_lock, written with BQL+out_lock. */
203 int mux_out;
204
aliguori731b0362009-03-05 23:01:42 +0000205 ReadLineState *rs;
Markus Armbruster74358f22015-03-06 19:35:59 +0100206 MonitorQMP qmp;
Andreas Färbercb446ec2013-05-01 14:24:52 +0200207 CPUState *mon_cpu;
Markus Armbruster097310b2014-10-07 13:59:15 +0200208 BlockCompletionFunc *password_completion_cb;
aliguori731b0362009-03-05 23:01:42 +0000209 void *password_opaque;
Wenchao Xia77172392013-08-27 20:38:20 +0800210 mon_cmd_t *cmd_table;
Anthony Liguoric227f092009-10-01 16:12:16 -0500211 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000212 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000213};
214
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300215/* QMP checker flags */
216#define QMP_ACCEPT_UNKNOWNS 1
217
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200218/* Protects mon_list, monitor_event_state. */
219static QemuMutex monitor_lock;
220
Blue Swirl72cf2d42009-09-12 07:36:22 +0000221static QLIST_HEAD(mon_list, Monitor) mon_list;
Corey Bryantba1c0482012-08-14 16:43:43 -0400222static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
Corey Bryantefb87c12012-08-14 16:43:48 -0400223static int mon_refcount;
bellard7e2515e2004-08-01 21:52:19 +0000224
Wayne Xia816f8922011-10-12 11:32:41 +0800225static mon_cmd_t mon_cmds[];
226static mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000227
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300228static const mon_cmd_t qmp_cmds[];
229
Markus Armbruster8631b602010-02-18 11:41:55 +0100230Monitor *cur_mon;
aliguori376253e2009-03-05 23:01:23 +0000231
Stefan Hajnoczic60bf332013-11-14 11:54:14 +0100232static void monitor_command_cb(void *opaque, const char *cmdline,
233 void *readline_opaque);
aliguori83ab7952008-08-19 14:44:22 +0000234
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100235/**
236 * Is @mon a QMP monitor?
237 */
238static inline bool monitor_is_qmp(const Monitor *mon)
Luiz Capitulino418173c2009-11-26 22:58:51 -0200239{
240 return (mon->flags & MONITOR_USE_CONTROL);
241}
242
Markus Armbruster489653b2015-03-06 20:01:05 +0100243/**
244 * Is the current monitor, if any, a QMP monitor?
245 */
246bool monitor_cur_is_qmp(void)
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100247{
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100248 return cur_mon && monitor_is_qmp(cur_mon);
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100249}
250
Anthony Liguori7060b472011-09-02 12:34:50 -0500251void monitor_read_command(Monitor *mon, int show_prompt)
aliguori731b0362009-03-05 23:01:42 +0000252{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200253 if (!mon->rs)
254 return;
255
aliguori731b0362009-03-05 23:01:42 +0000256 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
257 if (show_prompt)
258 readline_show_prompt(mon->rs);
259}
bellard6a00d602005-11-21 23:25:50 +0000260
Anthony Liguori7060b472011-09-02 12:34:50 -0500261int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
262 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000263{
Markus Armbrusterbcf5d192015-03-12 17:26:46 +0100264 if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000265 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
266 /* prompt is printed on return from the command handler */
267 return 0;
268 } else {
269 monitor_printf(mon, "terminal does not support password prompting\n");
270 return -ENOTTY;
271 }
aliguoribb5fc202009-03-05 23:01:15 +0000272}
273
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200274static void monitor_flush_locked(Monitor *mon);
275
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100276static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
277 void *opaque)
278{
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200279 Monitor *mon = opaque;
280
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200281 qemu_mutex_lock(&mon->out_lock);
282 mon->out_watch = 0;
283 monitor_flush_locked(mon);
284 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100285 return FALSE;
286}
287
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200288/* Called with mon->out_lock held. */
289static void monitor_flush_locked(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000290{
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100291 int rc;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400292 size_t len;
293 const char *buf;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100294
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400295 if (mon->skip_flush) {
296 return;
297 }
298
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400299 buf = qstring_get_str(mon->outbuf);
300 len = qstring_get_length(mon->outbuf);
301
Paolo Bonzinia4cc73d2013-05-31 14:00:27 +0200302 if (len && !mon->mux_out) {
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400303 rc = qemu_chr_fe_write(mon->chr, (const uint8_t *) buf, len);
Stratos Psomadakis056f49f2014-01-27 12:30:15 +0200304 if ((rc < 0 && errno != EAGAIN) || (rc == len)) {
305 /* all flushed or error */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400306 QDECREF(mon->outbuf);
307 mon->outbuf = qstring_new();
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100308 return;
309 }
310 if (rc > 0) {
311 /* partinal write */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400312 QString *tmp = qstring_from_str(buf + rc);
313 QDECREF(mon->outbuf);
314 mon->outbuf = tmp;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100315 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200316 if (mon->out_watch == 0) {
Roger Pau Monnee02bc6d2014-05-23 17:57:49 +0200317 mon->out_watch = qemu_chr_fe_add_watch(mon->chr, G_IO_OUT|G_IO_HUP,
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200318 monitor_unblocked, mon);
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200319 }
bellard7e2515e2004-08-01 21:52:19 +0000320 }
321}
322
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200323void monitor_flush(Monitor *mon)
324{
325 qemu_mutex_lock(&mon->out_lock);
326 monitor_flush_locked(mon);
327 qemu_mutex_unlock(&mon->out_lock);
328}
329
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400330/* flush at every end of line */
aliguori376253e2009-03-05 23:01:23 +0000331static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000332{
ths60fe76f2007-12-16 03:02:09 +0000333 char c;
aliguori731b0362009-03-05 23:01:42 +0000334
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200335 qemu_mutex_lock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000336 for(;;) {
337 c = *str++;
338 if (c == '\0')
339 break;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400340 if (c == '\n') {
341 qstring_append_chr(mon->outbuf, '\r');
342 }
343 qstring_append_chr(mon->outbuf, c);
344 if (c == '\n') {
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200345 monitor_flush_locked(mon);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400346 }
bellard7e2515e2004-08-01 21:52:19 +0000347 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200348 qemu_mutex_unlock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000349}
350
aliguori376253e2009-03-05 23:01:23 +0000351void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000352{
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400353 char *buf;
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200354
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200355 if (!mon)
356 return;
357
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100358 if (monitor_is_qmp(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200359 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200360 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200361
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400362 buf = g_strdup_vprintf(fmt, ap);
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200363 monitor_puts(mon, buf);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400364 g_free(buf);
bellard7e2515e2004-08-01 21:52:19 +0000365}
366
aliguori376253e2009-03-05 23:01:23 +0000367void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000368{
369 va_list ap;
370 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000371 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000372 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000373}
374
Pavel Butsykincaf15312015-09-22 16:18:17 +0300375int monitor_fprintf(FILE *stream, const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000376{
377 va_list ap;
378 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000379 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000380 va_end(ap);
381 return 0;
382}
383
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200384static void monitor_json_emitter(Monitor *mon, const QObject *data)
385{
386 QString *json;
387
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200388 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
389 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200390 assert(json != NULL);
391
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200392 qstring_append_chr(json, '\n');
393 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200394
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200395 QDECREF(json);
396}
397
Markus Armbruster710aec92015-03-06 11:28:00 +0100398static QDict *build_qmp_error_dict(Error *err)
Luiz Capitulinode253f12012-07-27 16:18:16 -0300399{
400 QObject *obj;
401
Markus Armbruster710aec92015-03-06 11:28:00 +0100402 obj = qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %s } }",
403 ErrorClass_lookup[error_get_class(err)],
404 error_get_pretty(err));
Luiz Capitulinode253f12012-07-27 16:18:16 -0300405
406 return qobject_to_qdict(obj);
407}
408
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100409static void monitor_protocol_emitter(Monitor *mon, QObject *data,
Markus Armbruster710aec92015-03-06 11:28:00 +0100410 Error *err)
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200411{
412 QDict *qmp;
413
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +0100414 trace_monitor_protocol_emitter(mon);
415
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100416 if (!err) {
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200417 /* success response */
Luiz Capitulinode253f12012-07-27 16:18:16 -0300418 qmp = qdict_new();
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200419 if (data) {
420 qobject_incref(data);
421 qdict_put_obj(qmp, "return", data);
422 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200423 /* return an empty QDict by default */
424 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200425 }
426 } else {
427 /* error response */
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100428 qmp = build_qmp_error_dict(err);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200429 }
430
Markus Armbruster74358f22015-03-06 19:35:59 +0100431 if (mon->qmp.id) {
432 qdict_put_obj(qmp, "id", mon->qmp.id);
433 mon->qmp.id = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200434 }
435
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200436 monitor_json_emitter(mon, QOBJECT(qmp));
437 QDECREF(qmp);
438}
439
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200440
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200441static MonitorQAPIEventState monitor_qapi_event_state[QAPI_EVENT_MAX];
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100442
443/*
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200444 * Emits the event to every monitor instance, @event is only used for trace
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200445 * Called with monitor_lock held.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100446 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200447static void monitor_qapi_event_emit(QAPIEvent event, QObject *data)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100448{
449 Monitor *mon;
450
451 trace_monitor_protocol_event_emit(event, data);
452 QLIST_FOREACH(mon, &mon_list, entry) {
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100453 if (monitor_is_qmp(mon) && mon->qmp.in_command_mode) {
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100454 monitor_json_emitter(mon, data);
455 }
456 }
457}
458
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100459/*
460 * Queue a new event for emission to Monitor instances,
461 * applying any rate limiting if required.
462 */
463static void
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200464monitor_qapi_event_queue(QAPIEvent event, QDict *data, Error **errp)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100465{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200466 MonitorQAPIEventState *evstate;
467 assert(event < QAPI_EVENT_MAX);
Alex Blighbc72ad62013-08-21 16:03:08 +0100468 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100469
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200470 evstate = &(monitor_qapi_event_state[event]);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100471 trace_monitor_protocol_event_queue(event,
472 data,
473 evstate->rate,
474 evstate->last,
475 now);
476
477 /* Rate limit of 0 indicates no throttling */
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200478 qemu_mutex_lock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100479 if (!evstate->rate) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200480 monitor_qapi_event_emit(event, QOBJECT(data));
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100481 evstate->last = now;
482 } else {
483 int64_t delta = now - evstate->last;
484 if (evstate->data ||
485 delta < evstate->rate) {
486 /* If there's an existing event pending, replace
487 * it with the new event, otherwise schedule a
488 * timer for delayed emission
489 */
490 if (evstate->data) {
491 qobject_decref(evstate->data);
492 } else {
493 int64_t then = evstate->last + evstate->rate;
Alex Blighbc72ad62013-08-21 16:03:08 +0100494 timer_mod_ns(evstate->timer, then);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100495 }
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200496 evstate->data = QOBJECT(data);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100497 qobject_incref(evstate->data);
498 } else {
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200499 monitor_qapi_event_emit(event, QOBJECT(data));
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100500 evstate->last = now;
501 }
502 }
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200503 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100504}
505
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100506/*
507 * The callback invoked by QemuTimer when a delayed
508 * event is ready to be emitted
509 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200510static void monitor_qapi_event_handler(void *opaque)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100511{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200512 MonitorQAPIEventState *evstate = opaque;
Alex Blighbc72ad62013-08-21 16:03:08 +0100513 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100514
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100515 trace_monitor_protocol_event_handler(evstate->event,
516 evstate->data,
517 evstate->last,
518 now);
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200519 qemu_mutex_lock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100520 if (evstate->data) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200521 monitor_qapi_event_emit(evstate->event, evstate->data);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100522 qobject_decref(evstate->data);
523 evstate->data = NULL;
524 }
525 evstate->last = now;
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200526 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100527}
528
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100529/*
530 * @event: the event ID to be limited
531 * @rate: the rate limit in milliseconds
532 *
533 * Sets a rate limit on a particular event, so no
534 * more than 1 event will be emitted within @rate
535 * milliseconds
536 */
537static void
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200538monitor_qapi_event_throttle(QAPIEvent event, int64_t rate)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100539{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200540 MonitorQAPIEventState *evstate;
541 assert(event < QAPI_EVENT_MAX);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100542
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200543 evstate = &(monitor_qapi_event_state[event]);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100544
545 trace_monitor_protocol_event_throttle(event, rate);
546 evstate->event = event;
Wenchao Xia2f44a082014-06-24 16:34:00 -0700547 assert(rate * SCALE_MS <= INT64_MAX);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100548 evstate->rate = rate * SCALE_MS;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100549 evstate->last = 0;
550 evstate->data = NULL;
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200551 evstate->timer = timer_new(QEMU_CLOCK_REALTIME,
552 SCALE_MS,
553 monitor_qapi_event_handler,
554 evstate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100555}
556
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200557static void monitor_qapi_event_init(void)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100558{
Wenchao Xiae010ad82014-06-18 08:43:41 +0200559 /* Limit guest-triggerable events to 1 per second */
560 monitor_qapi_event_throttle(QAPI_EVENT_RTC_CHANGE, 1000);
Wenchao Xia99eaf092014-06-18 08:43:42 +0200561 monitor_qapi_event_throttle(QAPI_EVENT_WATCHDOG, 1000);
Wenchao Xiaaef9d312014-06-18 08:43:51 +0200562 monitor_qapi_event_throttle(QAPI_EVENT_BALLOON_CHANGE, 1000);
Wenchao Xiafe069d92014-06-18 08:43:53 +0200563 monitor_qapi_event_throttle(QAPI_EVENT_QUORUM_REPORT_BAD, 1000);
564 monitor_qapi_event_throttle(QAPI_EVENT_QUORUM_FAILURE, 1000);
Laszlo Erseke2ae6152014-06-26 17:50:02 +0200565 monitor_qapi_event_throttle(QAPI_EVENT_VSERPORT_CHANGE, 1000);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100566
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200567 qmp_event_set_func_emit(monitor_qapi_event_queue);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200568}
569
Markus Armbruster485febc2015-03-13 17:25:50 +0100570static void qmp_capabilities(QDict *params, QObject **ret_data, Error **errp)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200571{
Markus Armbruster485febc2015-03-13 17:25:50 +0100572 cur_mon->qmp.in_command_mode = true;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200573}
574
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100575static void handle_hmp_command(Monitor *mon, const char *cmdline);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200576
Wenchao Xiab01fe892013-08-27 20:38:19 +0800577static void monitor_data_init(Monitor *mon)
578{
579 memset(mon, 0, sizeof(Monitor));
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200580 qemu_mutex_init(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800581 mon->outbuf = qstring_new();
Wenchao Xia77172392013-08-27 20:38:20 +0800582 /* Use *mon_cmds by default. */
583 mon->cmd_table = mon_cmds;
Wenchao Xiab01fe892013-08-27 20:38:19 +0800584}
585
586static void monitor_data_destroy(Monitor *mon)
587{
588 QDECREF(mon->outbuf);
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200589 qemu_mutex_destroy(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800590}
591
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200592char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
593 int64_t cpu_index, Error **errp)
Luiz Capitulino0268d972010-10-22 10:08:02 -0200594{
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200595 char *output = NULL;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200596 Monitor *old_mon, hmp;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200597
Wenchao Xiab01fe892013-08-27 20:38:19 +0800598 monitor_data_init(&hmp);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400599 hmp.skip_flush = true;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200600
601 old_mon = cur_mon;
602 cur_mon = &hmp;
603
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200604 if (has_cpu_index) {
605 int ret = monitor_set_cpu(cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200606 if (ret < 0) {
607 cur_mon = old_mon;
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100608 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
609 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200610 goto out;
611 }
612 }
613
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100614 handle_hmp_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200615 cur_mon = old_mon;
616
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200617 qemu_mutex_lock(&hmp.out_lock);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400618 if (qstring_get_length(hmp.outbuf) > 0) {
619 output = g_strdup(qstring_get_str(hmp.outbuf));
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200620 } else {
621 output = g_strdup("");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200622 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200623 qemu_mutex_unlock(&hmp.out_lock);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200624
625out:
Wenchao Xiab01fe892013-08-27 20:38:19 +0800626 monitor_data_destroy(&hmp);
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200627 return output;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200628}
629
bellard9dc39cb2004-03-14 21:38:27 +0000630static int compare_cmd(const char *name, const char *list)
631{
632 const char *p, *pstart;
633 int len;
634 len = strlen(name);
635 p = list;
636 for(;;) {
637 pstart = p;
638 p = strchr(p, '|');
639 if (!p)
640 p = pstart + strlen(pstart);
641 if ((p - pstart) == len && !memcmp(pstart, name, len))
642 return 1;
643 if (*p == '\0')
644 break;
645 p++;
646 }
647 return 0;
648}
649
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800650static int get_str(char *buf, int buf_size, const char **pp)
651{
652 const char *p;
653 char *q;
654 int c;
655
656 q = buf;
657 p = *pp;
658 while (qemu_isspace(*p)) {
659 p++;
660 }
661 if (*p == '\0') {
662 fail:
663 *q = '\0';
664 *pp = p;
665 return -1;
666 }
667 if (*p == '\"') {
668 p++;
669 while (*p != '\0' && *p != '\"') {
670 if (*p == '\\') {
671 p++;
672 c = *p++;
673 switch (c) {
674 case 'n':
675 c = '\n';
676 break;
677 case 'r':
678 c = '\r';
679 break;
680 case '\\':
681 case '\'':
682 case '\"':
683 break;
684 default:
Peter Maydell71baf782015-08-19 16:20:19 +0100685 printf("unsupported escape code: '\\%c'\n", c);
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800686 goto fail;
687 }
688 if ((q - buf) < buf_size - 1) {
689 *q++ = c;
690 }
691 } else {
692 if ((q - buf) < buf_size - 1) {
693 *q++ = *p;
694 }
695 p++;
696 }
697 }
698 if (*p != '\"') {
Peter Maydell71baf782015-08-19 16:20:19 +0100699 printf("unterminated string\n");
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800700 goto fail;
701 }
702 p++;
703 } else {
704 while (*p != '\0' && !qemu_isspace(*p)) {
705 if ((q - buf) < buf_size - 1) {
706 *q++ = *p;
707 }
708 p++;
709 }
710 }
711 *q = '\0';
712 *pp = p;
713 return 0;
714}
715
716#define MAX_ARGS 16
717
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800718static void free_cmdline_args(char **args, int nb_args)
719{
720 int i;
721
722 assert(nb_args <= MAX_ARGS);
723
724 for (i = 0; i < nb_args; i++) {
725 g_free(args[i]);
726 }
727
728}
729
730/*
731 * Parse the command line to get valid args.
732 * @cmdline: command line to be parsed.
733 * @pnb_args: location to store the number of args, must NOT be NULL.
734 * @args: location to store the args, which should be freed by caller, must
735 * NOT be NULL.
736 *
737 * Returns 0 on success, negative on failure.
738 *
739 * NOTE: this parser is an approximate form of the real command parser. Number
740 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
741 * return with failure.
742 */
743static int parse_cmdline(const char *cmdline,
744 int *pnb_args, char **args)
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800745{
746 const char *p;
747 int nb_args, ret;
748 char buf[1024];
749
750 p = cmdline;
751 nb_args = 0;
752 for (;;) {
753 while (qemu_isspace(*p)) {
754 p++;
755 }
756 if (*p == '\0') {
757 break;
758 }
759 if (nb_args >= MAX_ARGS) {
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800760 goto fail;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800761 }
762 ret = get_str(buf, sizeof(buf), &p);
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800763 if (ret < 0) {
764 goto fail;
765 }
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800766 args[nb_args] = g_strdup(buf);
767 nb_args++;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800768 }
769 *pnb_args = nb_args;
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800770 return 0;
771
772 fail:
773 free_cmdline_args(args, nb_args);
774 return -1;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800775}
776
Wenchao Xia66855492013-08-27 20:38:23 +0800777static void help_cmd_dump_one(Monitor *mon,
778 const mon_cmd_t *cmd,
779 char **prefix_args,
780 int prefix_args_nb)
781{
782 int i;
783
784 for (i = 0; i < prefix_args_nb; i++) {
785 monitor_printf(mon, "%s ", prefix_args[i]);
786 }
787 monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
788}
789
790/* @args[@arg_index] is the valid command need to find in @cmds */
Anthony Liguoric227f092009-10-01 16:12:16 -0500791static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
Wenchao Xia66855492013-08-27 20:38:23 +0800792 char **args, int nb_args, int arg_index)
bellard9dc39cb2004-03-14 21:38:27 +0000793{
Anthony Liguoric227f092009-10-01 16:12:16 -0500794 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000795
Wenchao Xia66855492013-08-27 20:38:23 +0800796 /* No valid arg need to compare with, dump all in *cmds */
797 if (arg_index >= nb_args) {
798 for (cmd = cmds; cmd->name != NULL; cmd++) {
799 help_cmd_dump_one(mon, cmd, args, arg_index);
800 }
801 return;
802 }
803
804 /* Find one entry to dump */
805 for (cmd = cmds; cmd->name != NULL; cmd++) {
806 if (compare_cmd(args[arg_index], cmd->name)) {
807 if (cmd->sub_table) {
808 /* continue with next arg */
809 help_cmd_dump(mon, cmd->sub_table,
810 args, nb_args, arg_index + 1);
811 } else {
812 help_cmd_dump_one(mon, cmd, args, arg_index);
813 }
814 break;
815 }
bellard9dc39cb2004-03-14 21:38:27 +0000816 }
817}
818
aliguori376253e2009-03-05 23:01:23 +0000819static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000820{
Wenchao Xia66855492013-08-27 20:38:23 +0800821 char *args[MAX_ARGS];
822 int nb_args = 0;
823
824 /* 1. parse user input */
825 if (name) {
826 /* special case for log, directly dump and return */
827 if (!strcmp(name, "log")) {
Peter Maydell38dad9e2013-02-11 16:41:25 +0000828 const QEMULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000829 monitor_printf(mon, "Log items (comma separated):\n");
830 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
Peter Maydell38dad9e2013-02-11 16:41:25 +0000831 for (item = qemu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000832 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000833 }
Wenchao Xia66855492013-08-27 20:38:23 +0800834 return;
835 }
836
837 if (parse_cmdline(name, &nb_args, args) < 0) {
838 return;
bellardf193c792004-03-21 17:06:25 +0000839 }
bellard9dc39cb2004-03-14 21:38:27 +0000840 }
Wenchao Xia66855492013-08-27 20:38:23 +0800841
842 /* 2. dump the contents according to parsed args */
843 help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0);
844
845 free_cmdline_args(args, nb_args);
bellard9dc39cb2004-03-14 21:38:27 +0000846}
847
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300848static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300849{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300850 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300851}
852
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100853static void hmp_trace_event(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530854{
855 const char *tp_name = qdict_get_str(qdict, "name");
856 bool new_state = qdict_get_bool(qdict, "option");
Lluís Vilanova14101d02014-08-25 13:20:03 +0200857 Error *local_err = NULL;
Blue Swirlf871d682010-10-13 19:14:29 +0000858
Lluís Vilanova14101d02014-08-25 13:20:03 +0200859 qmp_trace_event_set_state(tp_name, new_state, true, true, &local_err);
860 if (local_err) {
Markus Armbruster091e38b2015-02-10 15:15:43 +0100861 error_report_err(local_err);
Blue Swirlf871d682010-10-13 19:14:29 +0000862 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530863}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100864
Michael Rothc45a8162011-10-02 08:44:37 -0500865#ifdef CONFIG_TRACE_SIMPLE
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100866static void hmp_trace_file(Monitor *mon, const QDict *qdict)
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100867{
868 const char *op = qdict_get_try_str(qdict, "op");
869 const char *arg = qdict_get_try_str(qdict, "arg");
870
871 if (!op) {
872 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
873 } else if (!strcmp(op, "on")) {
874 st_set_trace_file_enabled(true);
875 } else if (!strcmp(op, "off")) {
876 st_set_trace_file_enabled(false);
877 } else if (!strcmp(op, "flush")) {
878 st_flush_trace_buffer();
879 } else if (!strcmp(op, "set")) {
880 if (arg) {
881 st_set_trace_file(arg);
882 }
883 } else {
884 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
885 help_cmd(mon, "trace-file");
886 }
887}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530888#endif
889
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100890static void hmp_info_help(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000891{
Luiz Capitulino13c74252009-10-07 13:41:55 -0300892 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000893}
894
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300895CommandInfoList *qmp_query_commands(Error **errp)
bellard9bc9d1c2004-10-10 15:15:51 +0000896{
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300897 CommandInfoList *info, *cmd_list = NULL;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200898 const mon_cmd_t *cmd;
899
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300900 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino40e5a012011-10-21 16:15:31 -0200901 info = g_malloc0(sizeof(*info));
902 info->value = g_malloc0(sizeof(*info->value));
903 info->value->name = g_strdup(cmd->name);
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200904
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300905 info->next = cmd_list;
906 cmd_list = info;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200907 }
908
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300909 return cmd_list;
thsa36e69d2007-12-02 05:18:19 +0000910}
911
Daniel P. Berrange48608532012-05-21 17:59:51 +0100912EventInfoList *qmp_query_events(Error **errp)
913{
914 EventInfoList *info, *ev_list = NULL;
Wenchao Xia75175172014-06-18 08:43:54 +0200915 QAPIEvent e;
Daniel P. Berrange48608532012-05-21 17:59:51 +0100916
Wenchao Xia75175172014-06-18 08:43:54 +0200917 for (e = 0 ; e < QAPI_EVENT_MAX ; e++) {
918 const char *event_name = QAPIEvent_lookup[e];
Daniel P. Berrange48608532012-05-21 17:59:51 +0100919 assert(event_name != NULL);
920 info = g_malloc0(sizeof(*info));
921 info->value = g_malloc0(sizeof(*info->value));
922 info->value->name = g_strdup(event_name);
923
924 info->next = ev_list;
925 ev_list = info;
926 }
927
928 return ev_list;
929}
930
Markus Armbruster39a18152015-09-16 13:06:28 +0200931/*
932 * Minor hack: generated marshalling suppressed for this command
933 * ('gen': false in the schema) so we can parse the JSON string
934 * directly into QObject instead of first parsing it with
935 * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
936 * to QObject with generated output marshallers, every time. Instead,
937 * we do it in test-qmp-input-visitor.c, just to make sure
938 * qapi-introspect.py's output actually conforms to the schema.
939 */
940static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
941 Error **errp)
942{
943 *ret_data = qobject_from_json(qmp_schema_json);
944}
945
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300946/* set the current CPU defined by the user */
947int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000948{
Andreas Färber55e5c282012-12-17 06:18:02 +0100949 CPUState *cpu;
bellard6a00d602005-11-21 23:25:50 +0000950
Andreas Färber1c8bb3c2013-02-15 17:01:09 +0100951 cpu = qemu_get_cpu(cpu_index);
952 if (cpu == NULL) {
953 return -1;
bellard6a00d602005-11-21 23:25:50 +0000954 }
Andreas Färbercb446ec2013-05-01 14:24:52 +0200955 cur_mon->mon_cpu = cpu;
Andreas Färber1c8bb3c2013-02-15 17:01:09 +0100956 return 0;
bellard6a00d602005-11-21 23:25:50 +0000957}
958
Pavel Butsykincaf15312015-09-22 16:18:17 +0300959CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000960{
aliguori731b0362009-03-05 23:01:42 +0000961 if (!cur_mon->mon_cpu) {
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300962 monitor_set_cpu(0);
bellard6a00d602005-11-21 23:25:50 +0000963 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300964 cpu_synchronize_state(cur_mon->mon_cpu);
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700965 return cur_mon->mon_cpu;
966}
967
Pavel Butsykinbf957282015-09-10 18:38:59 +0300968CPUArchState *mon_get_cpu_env(void)
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700969{
970 return mon_get_cpu()->env_ptr;
bellard6a00d602005-11-21 23:25:50 +0000971}
972
Luiz Capitulino99b77962011-10-24 10:53:44 -0200973int monitor_get_cpu_index(void)
974{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700975 return mon_get_cpu()->cpu_index;
Luiz Capitulino99b77962011-10-24 10:53:44 -0200976}
977
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100978static void hmp_info_registers(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000979{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700980 cpu_dump_state(mon_get_cpu(), (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000981}
982
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100983static void hmp_info_jit(Monitor *mon, const QDict *qdict)
bellarde3db7222005-01-26 22:00:47 +0000984{
aliguori376253e2009-03-05 23:01:23 +0000985 dump_exec_info((FILE *)mon, monitor_fprintf);
Sebastian Tanase27498be2014-07-25 11:56:33 +0200986 dump_drift_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000987}
988
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100989static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
Max Filippov246ae242014-11-02 11:04:18 +0300990{
991 dump_opcount_info((FILE *)mon, monitor_fprintf);
992}
993
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100994static void hmp_info_history(Monitor *mon, const QDict *qdict)
bellardaa455482004-04-04 13:07:25 +0000995{
996 int i;
bellard7e2515e2004-08-01 21:52:19 +0000997 const char *str;
ths3b46e622007-09-17 08:09:54 +0000998
aliguoricde76ee2009-03-05 23:01:51 +0000999 if (!mon->rs)
1000 return;
bellard7e2515e2004-08-01 21:52:19 +00001001 i = 0;
1002 for(;;) {
aliguori731b0362009-03-05 23:01:42 +00001003 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +00001004 if (!str)
1005 break;
aliguori376253e2009-03-05 23:01:23 +00001006 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +00001007 i++;
bellardaa455482004-04-04 13:07:25 +00001008 }
1009}
1010
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001011static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
j_mayer76a66252007-03-07 08:32:30 +00001012{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001013 cpu_dump_statistics(mon_get_cpu(), (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +00001014}
j_mayer76a66252007-03-07 08:32:30 +00001015
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001016static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301017{
Lluís Vilanova14101d02014-08-25 13:20:03 +02001018 TraceEventInfoList *events = qmp_trace_event_get_state("*", NULL);
1019 TraceEventInfoList *elem;
1020
1021 for (elem = events; elem != NULL; elem = elem->next) {
1022 monitor_printf(mon, "%s : state %u\n",
1023 elem->value->name,
1024 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
1025 }
1026 qapi_free_TraceEventInfoList(events);
Prerna Saxena22890ab2010-06-24 17:04:53 +05301027}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301028
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001029void qmp_client_migrate_info(const char *protocol, const char *hostname,
1030 bool has_port, int64_t port,
1031 bool has_tls_port, int64_t tls_port,
1032 bool has_cert_subject, const char *cert_subject,
1033 Error **errp)
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001034{
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001035 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001036 if (!qemu_using_spice(errp)) {
1037 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001038 }
1039
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001040 if (!has_port && !has_tls_port) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001041 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001042 return;
Yonit Halperin6ec5dae2012-03-18 09:42:39 +02001043 }
1044
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001045 if (qemu_spice_migrate_info(hostname,
1046 has_port ? port : -1,
1047 has_tls_port ? tls_port : -1,
1048 cert_subject)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001049 error_setg(errp, QERR_UNDEFINED_ERROR);
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001050 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001051 }
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001052 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001053 }
1054
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001055 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001056}
1057
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001058static void hmp_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001059{
Peter Maydell9a7e5422013-02-11 16:41:20 +00001060 qemu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001061}
1062
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001063static void hmp_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001064{
1065 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001066 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001067
bellard9307c4c2004-04-04 12:57:25 +00001068 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001069 mask = 0;
1070 } else {
Peter Maydell4fde1eb2013-02-11 16:41:22 +00001071 mask = qemu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001072 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001073 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001074 return;
1075 }
1076 }
Peter Maydell24537a02013-02-11 16:41:23 +00001077 qemu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00001078}
1079
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001080static void hmp_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001081{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001082 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001083 if (!option || !strcmp(option, "on")) {
1084 singlestep = 1;
1085 } else if (!strcmp(option, "off")) {
1086 singlestep = 0;
1087 } else {
1088 monitor_printf(mon, "unexpected option %s\n", option);
1089 }
1090}
1091
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001092static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001093{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001094 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001095 if (!device)
1096 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1097 if (gdbserver_start(device) < 0) {
1098 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1099 device);
1100 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001101 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001102 } else {
aliguori59030a82009-04-05 18:43:41 +00001103 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1104 device);
bellard8a7ddc32004-03-31 19:00:16 +00001105 }
1106}
1107
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001108static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001109{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001110 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001111 if (select_watchdog_action(action) == -1) {
1112 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1113 }
1114}
1115
aliguori376253e2009-03-05 23:01:23 +00001116static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001117{
aliguori376253e2009-03-05 23:01:23 +00001118 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001119 switch(c) {
1120 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001121 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001122 break;
1123 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001124 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001125 break;
1126 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001127 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001128 break;
1129 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001130 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001131 break;
1132 default:
1133 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001134 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001135 } else {
aliguori376253e2009-03-05 23:01:23 +00001136 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001137 }
1138 break;
1139 }
aliguori376253e2009-03-05 23:01:23 +00001140 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001141}
1142
aliguori376253e2009-03-05 23:01:23 +00001143static void memory_dump(Monitor *mon, int count, int format, int wsize,
Avi Kivitya8170e52012-10-23 12:30:10 +02001144 hwaddr addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001145{
Blue Swirl23842aa2010-01-12 20:27:43 +00001146 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001147 uint8_t buf[16];
1148 uint64_t v;
1149
1150 if (format == 'i') {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001151 int flags = 0;
bellard9307c4c2004-04-04 12:57:25 +00001152#ifdef TARGET_I386
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001153 CPUArchState *env = mon_get_cpu_env();
bellard4c27ba22004-04-25 18:05:08 +00001154 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001155 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001156 } else if (wsize == 4) {
1157 flags = 0;
1158 } else {
bellard6a15fd12006-04-12 21:07:07 +00001159 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001160 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001161 if (env) {
1162#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001163 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001164 (env->segs[R_CS].flags & DESC_L_MASK))
1165 flags = 2;
1166 else
1167#endif
1168 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1169 flags = 1;
1170 }
bellard4c27ba22004-04-25 18:05:08 +00001171 }
1172#endif
Tom Musta1c38f842014-04-09 14:53:24 -05001173#ifdef TARGET_PPC
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001174 CPUArchState *env = mon_get_cpu_env();
Tom Musta1c38f842014-04-09 14:53:24 -05001175 flags = msr_le << 16;
1176 flags |= env->bfd_mach;
1177#endif
Peter Crosthwaited49190c2015-05-24 14:20:41 -07001178 monitor_disas(mon, mon_get_cpu(), addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001179 return;
1180 }
1181
1182 len = wsize * count;
1183 if (wsize == 1)
1184 line_size = 8;
1185 else
1186 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001187 max_digits = 0;
1188
1189 switch(format) {
1190 case 'o':
1191 max_digits = (wsize * 8 + 2) / 3;
1192 break;
1193 default:
1194 case 'x':
1195 max_digits = (wsize * 8) / 4;
1196 break;
1197 case 'u':
1198 case 'd':
1199 max_digits = (wsize * 8 * 10 + 32) / 33;
1200 break;
1201 case 'c':
1202 wsize = 1;
1203 break;
1204 }
1205
1206 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001207 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001208 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001209 else
aliguori376253e2009-03-05 23:01:23 +00001210 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001211 l = len;
1212 if (l > line_size)
1213 l = line_size;
1214 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001215 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001216 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001217 if (cpu_memory_rw_debug(mon_get_cpu(), addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001218 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001219 break;
1220 }
bellard9307c4c2004-04-04 12:57:25 +00001221 }
ths5fafdf22007-09-16 21:08:06 +00001222 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001223 while (i < l) {
1224 switch(wsize) {
1225 default:
1226 case 1:
Peter Maydell24e60302015-01-20 15:19:32 +00001227 v = ldub_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001228 break;
1229 case 2:
Peter Maydell24e60302015-01-20 15:19:32 +00001230 v = lduw_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001231 break;
1232 case 4:
Peter Maydell24e60302015-01-20 15:19:32 +00001233 v = (uint32_t)ldl_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001234 break;
1235 case 8:
Peter Maydell24e60302015-01-20 15:19:32 +00001236 v = ldq_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001237 break;
1238 }
aliguori376253e2009-03-05 23:01:23 +00001239 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001240 switch(format) {
1241 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001242 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001243 break;
1244 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001245 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001246 break;
1247 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001248 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001249 break;
1250 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001251 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001252 break;
1253 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001254 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001255 break;
1256 }
1257 i += wsize;
1258 }
aliguori376253e2009-03-05 23:01:23 +00001259 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001260 addr += l;
1261 len -= l;
1262 }
1263}
1264
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001265static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001266{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001267 int count = qdict_get_int(qdict, "count");
1268 int format = qdict_get_int(qdict, "format");
1269 int size = qdict_get_int(qdict, "size");
1270 target_long addr = qdict_get_int(qdict, "addr");
1271
aliguori376253e2009-03-05 23:01:23 +00001272 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001273}
1274
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001275static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001276{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001277 int count = qdict_get_int(qdict, "count");
1278 int format = qdict_get_int(qdict, "format");
1279 int size = qdict_get_int(qdict, "size");
Avi Kivitya8170e52012-10-23 12:30:10 +02001280 hwaddr addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001281
aliguori376253e2009-03-05 23:01:23 +00001282 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001283}
1284
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001285static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001286{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001287 int format = qdict_get_int(qdict, "format");
Avi Kivitya8170e52012-10-23 12:30:10 +02001288 hwaddr val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001289
bellard9307c4c2004-04-04 12:57:25 +00001290 switch(format) {
1291 case 'o':
Avi Kivitya8170e52012-10-23 12:30:10 +02001292 monitor_printf(mon, "%#" HWADDR_PRIo, val);
bellard9307c4c2004-04-04 12:57:25 +00001293 break;
1294 case 'x':
Avi Kivitya8170e52012-10-23 12:30:10 +02001295 monitor_printf(mon, "%#" HWADDR_PRIx, val);
bellard9307c4c2004-04-04 12:57:25 +00001296 break;
1297 case 'u':
Avi Kivitya8170e52012-10-23 12:30:10 +02001298 monitor_printf(mon, "%" HWADDR_PRIu, val);
bellard9307c4c2004-04-04 12:57:25 +00001299 break;
1300 default:
1301 case 'd':
Avi Kivitya8170e52012-10-23 12:30:10 +02001302 monitor_printf(mon, "%" HWADDR_PRId, val);
bellard9307c4c2004-04-04 12:57:25 +00001303 break;
1304 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001305 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001306 break;
1307 }
aliguori376253e2009-03-05 23:01:23 +00001308 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001309}
1310
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001311static void hmp_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001312{
1313 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001314 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001315 uint32_t start = qdict_get_int(qdict, "start");
1316 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001317
1318 sum = 0;
1319 for(addr = start; addr < (start + size); addr++) {
Peter Maydell42874d32015-04-26 16:49:24 +01001320 uint8_t val = address_space_ldub(&address_space_memory, addr,
1321 MEMTXATTRS_UNSPECIFIED, NULL);
bellarde4cf1ad2005-06-04 20:15:57 +00001322 /* BSD sum algorithm ('sum' Unix command) */
1323 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001324 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001325 }
aliguori376253e2009-03-05 23:01:23 +00001326 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001327}
1328
bellard13224a82006-07-14 22:03:35 +00001329static int mouse_button_state;
1330
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001331static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001332{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001333 int dx, dy, dz, button;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001334 const char *dx_str = qdict_get_str(qdict, "dx_str");
1335 const char *dy_str = qdict_get_str(qdict, "dy_str");
1336 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001337
bellard13224a82006-07-14 22:03:35 +00001338 dx = strtol(dx_str, NULL, 0);
1339 dy = strtol(dy_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001340 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
1341 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
1342
1343 if (dz_str) {
bellard13224a82006-07-14 22:03:35 +00001344 dz = strtol(dz_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001345 if (dz != 0) {
1346 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
1347 qemu_input_queue_btn(NULL, button, true);
1348 qemu_input_event_sync();
1349 qemu_input_queue_btn(NULL, button, false);
1350 }
1351 }
1352 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001353}
1354
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001355static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001356{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001357 static uint32_t bmap[INPUT_BUTTON_MAX] = {
1358 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1359 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
1360 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1361 };
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001362 int button_state = qdict_get_int(qdict, "button_state");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001363
1364 if (mouse_button_state == button_state) {
1365 return;
1366 }
1367 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1368 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001369 mouse_button_state = button_state;
bellard13224a82006-07-14 22:03:35 +00001370}
1371
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001372static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001373{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001374 int size = qdict_get_int(qdict, "size");
1375 int addr = qdict_get_int(qdict, "addr");
1376 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001377 uint32_t val;
1378 int suffix;
1379
1380 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001381 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001382 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001383 addr++;
1384 }
1385 addr &= 0xffff;
1386
1387 switch(size) {
1388 default:
1389 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001390 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001391 suffix = 'b';
1392 break;
1393 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001394 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001395 suffix = 'w';
1396 break;
1397 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001398 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001399 suffix = 'l';
1400 break;
1401 }
aliguori376253e2009-03-05 23:01:23 +00001402 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1403 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001404}
bellarda3a91a32004-06-04 11:06:21 +00001405
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001406static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001407{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001408 int size = qdict_get_int(qdict, "size");
1409 int addr = qdict_get_int(qdict, "addr");
1410 int val = qdict_get_int(qdict, "val");
1411
Jan Kiszkaf1147842009-07-14 10:20:11 +02001412 addr &= IOPORTS_MASK;
1413
1414 switch (size) {
1415 default:
1416 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001417 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001418 break;
1419 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001420 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001421 break;
1422 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001423 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001424 break;
1425 }
1426}
1427
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001428static void hmp_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001429{
Gongleif1839932014-12-03 18:20:58 +00001430 Error *local_err = NULL;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001431 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001432
Gongleif1839932014-12-03 18:20:58 +00001433 qemu_boot_set(bootdevice, &local_err);
1434 if (local_err) {
1435 monitor_printf(mon, "%s\n", error_get_pretty(local_err));
1436 error_free(local_err);
aurel320ecdffb2008-05-04 20:11:34 +00001437 } else {
Gongleif1839932014-12-03 18:20:58 +00001438 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001439 }
1440}
1441
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001442static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
Blue Swirl314e2982011-09-11 20:22:05 +00001443{
1444 mtree_info((fprintf_function)monitor_printf, mon);
1445}
1446
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001447static void hmp_info_numa(Monitor *mon, const QDict *qdict)
aliguori030ea372009-04-21 22:30:47 +00001448{
aliguorib28b6232009-04-22 20:20:29 +00001449 int i;
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001450 CPUState *cpu;
zhanghailiang5b009e42014-11-04 19:49:30 +08001451 uint64_t *node_mem;
aliguori030ea372009-04-21 22:30:47 +00001452
zhanghailiang5b009e42014-11-04 19:49:30 +08001453 node_mem = g_new0(uint64_t, nb_numa_nodes);
1454 query_numa_node_mem(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001455 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1456 for (i = 0; i < nb_numa_nodes; i++) {
1457 monitor_printf(mon, "node %d cpus:", i);
Andreas Färberbdc44642013-06-24 23:50:24 +02001458 CPU_FOREACH(cpu) {
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001459 if (cpu->numa_node == i) {
Andreas Färber55e5c282012-12-17 06:18:02 +01001460 monitor_printf(mon, " %d", cpu->cpu_index);
aliguori030ea372009-04-21 22:30:47 +00001461 }
1462 }
1463 monitor_printf(mon, "\n");
1464 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
zhanghailiang5b009e42014-11-04 19:49:30 +08001465 node_mem[i] >> 20);
aliguori030ea372009-04-21 22:30:47 +00001466 }
zhanghailiang5b009e42014-11-04 19:49:30 +08001467 g_free(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001468}
1469
bellard5f1ce942006-02-08 22:40:15 +00001470#ifdef CONFIG_PROFILER
1471
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001472int64_t tcg_time;
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001473int64_t dev_time;
1474
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001475static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001476{
aliguori376253e2009-03-05 23:01:23 +00001477 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001478 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00001479 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001480 tcg_time, tcg_time / (double)get_ticks_per_sec());
1481 tcg_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001482 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001483}
1484#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001485static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001486{
aliguori376253e2009-03-05 23:01:23 +00001487 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001488}
1489#endif
1490
bellardec36b692006-07-16 18:57:03 +00001491/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001492static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001493
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001494static void hmp_info_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001495{
1496 int i;
1497 CaptureState *s;
1498
1499 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001500 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001501 s->ops.info (s->opaque);
1502 }
1503}
1504
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001505static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001506{
1507 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001508 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001509 CaptureState *s;
1510
1511 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1512 if (i == n) {
1513 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001514 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001515 g_free (s);
bellardec36b692006-07-16 18:57:03 +00001516 return;
1517 }
1518 }
1519}
1520
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001521static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001522{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001523 const char *path = qdict_get_str(qdict, "path");
1524 int has_freq = qdict_haskey(qdict, "freq");
1525 int freq = qdict_get_try_int(qdict, "freq", -1);
1526 int has_bits = qdict_haskey(qdict, "bits");
1527 int bits = qdict_get_try_int(qdict, "bits", -1);
1528 int has_channels = qdict_haskey(qdict, "nchannels");
1529 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001530 CaptureState *s;
1531
Anthony Liguori7267c092011-08-20 22:09:37 -05001532 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001533
1534 freq = has_freq ? freq : 44100;
1535 bits = has_bits ? bits : 16;
1536 nchannels = has_channels ? nchannels : 2;
1537
1538 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001539 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05001540 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001541 return;
bellardec36b692006-07-16 18:57:03 +00001542 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001543 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001544}
bellardec36b692006-07-16 18:57:03 +00001545
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001546static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001547{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001548 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001549
aliguori76655d62009-03-06 20:27:37 +00001550 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001551 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001552 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001553 return acl;
1554}
aliguori76655d62009-03-06 20:27:37 +00001555
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001556static void hmp_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001557{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001558 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001559 qemu_acl *acl = find_acl(mon, aclname);
1560 qemu_acl_entry *entry;
1561 int i = 0;
1562
1563 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001564 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001565 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00001566 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00001567 i++;
1568 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001569 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001570 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001571 }
1572}
1573
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001574static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001575{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001576 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001577 qemu_acl *acl = find_acl(mon, aclname);
1578
1579 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001580 qemu_acl_reset(acl);
1581 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001582 }
1583}
aliguori76655d62009-03-06 20:27:37 +00001584
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001585static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001586{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001587 const char *aclname = qdict_get_str(qdict, "aclname");
1588 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001589 qemu_acl *acl = find_acl(mon, aclname);
1590
1591 if (acl) {
1592 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001593 acl->defaultDeny = 0;
1594 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001595 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001596 acl->defaultDeny = 1;
1597 monitor_printf(mon, "acl: policy set to 'deny'\n");
1598 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001599 monitor_printf(mon, "acl: unknown policy '%s', "
1600 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001601 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001602 }
1603}
aliguori76655d62009-03-06 20:27:37 +00001604
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001605static void hmp_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001606{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001607 const char *aclname = qdict_get_str(qdict, "aclname");
1608 const char *match = qdict_get_str(qdict, "match");
1609 const char *policy = qdict_get_str(qdict, "policy");
1610 int has_index = qdict_haskey(qdict, "index");
1611 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001612 qemu_acl *acl = find_acl(mon, aclname);
1613 int deny, ret;
1614
1615 if (acl) {
1616 if (strcmp(policy, "allow") == 0) {
1617 deny = 0;
1618 } else if (strcmp(policy, "deny") == 0) {
1619 deny = 1;
1620 } else {
1621 monitor_printf(mon, "acl: unknown policy '%s', "
1622 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001623 return;
1624 }
aliguori28a76be2009-03-06 20:27:40 +00001625 if (has_index)
1626 ret = qemu_acl_insert(acl, deny, match, index);
1627 else
1628 ret = qemu_acl_append(acl, deny, match);
1629 if (ret < 0)
1630 monitor_printf(mon, "acl: unable to add acl entry\n");
1631 else
1632 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001633 }
1634}
aliguori76655d62009-03-06 20:27:37 +00001635
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001636static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001637{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001638 const char *aclname = qdict_get_str(qdict, "aclname");
1639 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001640 qemu_acl *acl = find_acl(mon, aclname);
1641 int ret;
aliguori76655d62009-03-06 20:27:37 +00001642
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001643 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001644 ret = qemu_acl_remove(acl, match);
1645 if (ret < 0)
1646 monitor_printf(mon, "acl: no matching acl entry\n");
1647 else
1648 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001649 }
1650}
1651
Corey Bryant208c9d12012-06-22 14:36:09 -04001652void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001653{
Anthony Liguoric227f092009-10-01 16:12:16 -05001654 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001655 int fd;
1656
Corey Bryant208c9d12012-06-22 14:36:09 -04001657 fd = qemu_chr_fe_get_msgfd(cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001658 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001659 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryant208c9d12012-06-22 14:36:09 -04001660 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001661 }
1662
1663 if (qemu_isdigit(fdname[0])) {
Stefan Hajnoczi0b9f0e22014-04-24 13:58:18 +02001664 close(fd);
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001665 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
1666 "a name not starting with a digit");
Corey Bryant208c9d12012-06-22 14:36:09 -04001667 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001668 }
1669
Corey Bryant208c9d12012-06-22 14:36:09 -04001670 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001671 if (strcmp(monfd->name, fdname) != 0) {
1672 continue;
1673 }
1674
1675 close(monfd->fd);
1676 monfd->fd = fd;
Corey Bryant208c9d12012-06-22 14:36:09 -04001677 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001678 }
1679
Anthony Liguori7267c092011-08-20 22:09:37 -05001680 monfd = g_malloc0(sizeof(mon_fd_t));
1681 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001682 monfd->fd = fd;
1683
Corey Bryant208c9d12012-06-22 14:36:09 -04001684 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001685}
1686
Corey Bryant208c9d12012-06-22 14:36:09 -04001687void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001688{
Anthony Liguoric227f092009-10-01 16:12:16 -05001689 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001690
Corey Bryant208c9d12012-06-22 14:36:09 -04001691 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001692 if (strcmp(monfd->name, fdname) != 0) {
1693 continue;
1694 }
1695
Blue Swirl72cf2d42009-09-12 07:36:22 +00001696 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001697 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001698 g_free(monfd->name);
1699 g_free(monfd);
Corey Bryant208c9d12012-06-22 14:36:09 -04001700 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001701 }
1702
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001703 error_setg(errp, QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001704}
1705
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001706static void hmp_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001707{
Luiz Capitulino13548692011-07-29 15:36:43 -03001708 int saved_vm_running = runstate_is_running();
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001709 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02001710
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001711 vm_stop(RUN_STATE_RESTORE_VM);
Juan Quintelac8d41b22009-08-20 19:42:21 +02001712
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03001713 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02001714 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03001715 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02001716}
1717
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001718int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
Mark McLoughlin7768e042009-07-22 09:11:41 +01001719{
Anthony Liguoric227f092009-10-01 16:12:16 -05001720 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01001721
Blue Swirl72cf2d42009-09-12 07:36:22 +00001722 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01001723 int fd;
1724
1725 if (strcmp(monfd->name, fdname) != 0) {
1726 continue;
1727 }
1728
1729 fd = monfd->fd;
1730
1731 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001732 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05001733 g_free(monfd->name);
1734 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001735
1736 return fd;
1737 }
1738
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001739 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001740 return -1;
1741}
1742
Corey Bryantba1c0482012-08-14 16:43:43 -04001743static void monitor_fdset_cleanup(MonFdset *mon_fdset)
1744{
1745 MonFdsetFd *mon_fdset_fd;
1746 MonFdsetFd *mon_fdset_fd_next;
1747
1748 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantebe52b52012-10-18 15:19:33 -04001749 if ((mon_fdset_fd->removed ||
1750 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
1751 runstate_is_running()) {
Corey Bryantba1c0482012-08-14 16:43:43 -04001752 close(mon_fdset_fd->fd);
1753 g_free(mon_fdset_fd->opaque);
1754 QLIST_REMOVE(mon_fdset_fd, next);
1755 g_free(mon_fdset_fd);
1756 }
1757 }
1758
Corey Bryantadb696f2012-08-14 16:43:47 -04001759 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04001760 QLIST_REMOVE(mon_fdset, next);
1761 g_free(mon_fdset);
1762 }
1763}
1764
Corey Bryantefb87c12012-08-14 16:43:48 -04001765static void monitor_fdsets_cleanup(void)
1766{
1767 MonFdset *mon_fdset;
1768 MonFdset *mon_fdset_next;
1769
1770 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
1771 monitor_fdset_cleanup(mon_fdset);
1772 }
1773}
1774
Corey Bryantba1c0482012-08-14 16:43:43 -04001775AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
1776 const char *opaque, Error **errp)
1777{
1778 int fd;
1779 Monitor *mon = cur_mon;
Corey Bryantba1c0482012-08-14 16:43:43 -04001780 AddfdInfo *fdinfo;
1781
1782 fd = qemu_chr_fe_get_msgfd(mon->chr);
1783 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001784 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryantba1c0482012-08-14 16:43:43 -04001785 goto error;
1786 }
1787
Corey Bryante446f702012-10-18 15:19:32 -04001788 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
1789 has_opaque, opaque, errp);
1790 if (fdinfo) {
1791 return fdinfo;
Corey Bryant9ac54af2012-10-18 15:19:31 -04001792 }
1793
Corey Bryantba1c0482012-08-14 16:43:43 -04001794error:
1795 if (fd != -1) {
1796 close(fd);
1797 }
1798 return NULL;
1799}
1800
1801void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
1802{
1803 MonFdset *mon_fdset;
1804 MonFdsetFd *mon_fdset_fd;
1805 char fd_str[60];
1806
1807 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1808 if (mon_fdset->id != fdset_id) {
1809 continue;
1810 }
1811 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1812 if (has_fd) {
1813 if (mon_fdset_fd->fd != fd) {
1814 continue;
1815 }
1816 mon_fdset_fd->removed = true;
1817 break;
1818 } else {
1819 mon_fdset_fd->removed = true;
1820 }
1821 }
1822 if (has_fd && !mon_fdset_fd) {
1823 goto error;
1824 }
1825 monitor_fdset_cleanup(mon_fdset);
1826 return;
1827 }
1828
1829error:
1830 if (has_fd) {
1831 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
1832 fdset_id, fd);
1833 } else {
1834 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
1835 }
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001836 error_setg(errp, QERR_FD_NOT_FOUND, fd_str);
Corey Bryantba1c0482012-08-14 16:43:43 -04001837}
1838
1839FdsetInfoList *qmp_query_fdsets(Error **errp)
1840{
1841 MonFdset *mon_fdset;
1842 MonFdsetFd *mon_fdset_fd;
1843 FdsetInfoList *fdset_list = NULL;
1844
1845 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1846 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
1847 FdsetFdInfoList *fdsetfd_list = NULL;
1848
1849 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
1850 fdset_info->value->fdset_id = mon_fdset->id;
1851
1852 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1853 FdsetFdInfoList *fdsetfd_info;
1854
1855 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
1856 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
1857 fdsetfd_info->value->fd = mon_fdset_fd->fd;
1858 if (mon_fdset_fd->opaque) {
1859 fdsetfd_info->value->has_opaque = true;
1860 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
1861 } else {
1862 fdsetfd_info->value->has_opaque = false;
1863 }
1864
1865 fdsetfd_info->next = fdsetfd_list;
1866 fdsetfd_list = fdsetfd_info;
1867 }
1868
1869 fdset_info->value->fds = fdsetfd_list;
1870
1871 fdset_info->next = fdset_list;
1872 fdset_list = fdset_info;
1873 }
1874
1875 return fdset_list;
1876}
1877
Corey Bryante446f702012-10-18 15:19:32 -04001878AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
1879 bool has_opaque, const char *opaque,
1880 Error **errp)
1881{
1882 MonFdset *mon_fdset = NULL;
1883 MonFdsetFd *mon_fdset_fd;
1884 AddfdInfo *fdinfo;
1885
1886 if (has_fdset_id) {
1887 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1888 /* Break if match found or match impossible due to ordering by ID */
1889 if (fdset_id <= mon_fdset->id) {
1890 if (fdset_id < mon_fdset->id) {
1891 mon_fdset = NULL;
1892 }
1893 break;
1894 }
1895 }
1896 }
1897
1898 if (mon_fdset == NULL) {
1899 int64_t fdset_id_prev = -1;
1900 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
1901
1902 if (has_fdset_id) {
1903 if (fdset_id < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001904 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
1905 "a non-negative value");
Corey Bryante446f702012-10-18 15:19:32 -04001906 return NULL;
1907 }
1908 /* Use specified fdset ID */
1909 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1910 mon_fdset_cur = mon_fdset;
1911 if (fdset_id < mon_fdset_cur->id) {
1912 break;
1913 }
1914 }
1915 } else {
1916 /* Use first available fdset ID */
1917 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1918 mon_fdset_cur = mon_fdset;
1919 if (fdset_id_prev == mon_fdset_cur->id - 1) {
1920 fdset_id_prev = mon_fdset_cur->id;
1921 continue;
1922 }
1923 break;
1924 }
1925 }
1926
1927 mon_fdset = g_malloc0(sizeof(*mon_fdset));
1928 if (has_fdset_id) {
1929 mon_fdset->id = fdset_id;
1930 } else {
1931 mon_fdset->id = fdset_id_prev + 1;
1932 }
1933
1934 /* The fdset list is ordered by fdset ID */
1935 if (!mon_fdset_cur) {
1936 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
1937 } else if (mon_fdset->id < mon_fdset_cur->id) {
1938 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
1939 } else {
1940 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
1941 }
1942 }
1943
1944 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
1945 mon_fdset_fd->fd = fd;
1946 mon_fdset_fd->removed = false;
1947 if (has_opaque) {
1948 mon_fdset_fd->opaque = g_strdup(opaque);
1949 }
1950 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
1951
1952 fdinfo = g_malloc0(sizeof(*fdinfo));
1953 fdinfo->fdset_id = mon_fdset->id;
1954 fdinfo->fd = mon_fdset_fd->fd;
1955
1956 return fdinfo;
1957}
1958
Corey Bryantadb696f2012-08-14 16:43:47 -04001959int monitor_fdset_get_fd(int64_t fdset_id, int flags)
1960{
Blue Swirlb2dc64c2012-08-18 20:14:54 +00001961#ifndef _WIN32
Corey Bryantadb696f2012-08-14 16:43:47 -04001962 MonFdset *mon_fdset;
1963 MonFdsetFd *mon_fdset_fd;
1964 int mon_fd_flags;
1965
Corey Bryantadb696f2012-08-14 16:43:47 -04001966 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1967 if (mon_fdset->id != fdset_id) {
1968 continue;
1969 }
1970 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1971 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
1972 if (mon_fd_flags == -1) {
1973 return -1;
1974 }
1975
1976 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
1977 return mon_fdset_fd->fd;
1978 }
1979 }
1980 errno = EACCES;
1981 return -1;
1982 }
1983#endif
1984
1985 errno = ENOENT;
1986 return -1;
1987}
1988
1989int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
1990{
1991 MonFdset *mon_fdset;
1992 MonFdsetFd *mon_fdset_fd_dup;
1993
1994 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1995 if (mon_fdset->id != fdset_id) {
1996 continue;
1997 }
1998 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
1999 if (mon_fdset_fd_dup->fd == dup_fd) {
2000 return -1;
2001 }
2002 }
2003 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2004 mon_fdset_fd_dup->fd = dup_fd;
2005 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
2006 return 0;
2007 }
2008 return -1;
2009}
2010
2011static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2012{
2013 MonFdset *mon_fdset;
2014 MonFdsetFd *mon_fdset_fd_dup;
2015
2016 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2017 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2018 if (mon_fdset_fd_dup->fd == dup_fd) {
2019 if (remove) {
2020 QLIST_REMOVE(mon_fdset_fd_dup, next);
2021 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2022 monitor_fdset_cleanup(mon_fdset);
2023 }
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002024 return -1;
2025 } else {
2026 return mon_fdset->id;
Corey Bryantadb696f2012-08-14 16:43:47 -04002027 }
Corey Bryantadb696f2012-08-14 16:43:47 -04002028 }
2029 }
2030 }
2031 return -1;
2032}
2033
2034int monitor_fdset_dup_fd_find(int dup_fd)
2035{
2036 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2037}
2038
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002039void monitor_fdset_dup_fd_remove(int dup_fd)
Corey Bryantadb696f2012-08-14 16:43:47 -04002040{
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002041 monitor_fdset_dup_fd_find_remove(dup_fd, true);
Corey Bryantadb696f2012-08-14 16:43:47 -04002042}
2043
Markus Armbruster1677f4c2015-02-09 14:03:19 +01002044int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
Laszlo Ersek59063662014-04-10 10:24:31 +02002045{
2046 int fd;
2047 Error *local_err = NULL;
2048
2049 if (!qemu_isdigit(fdname[0]) && mon) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002050 fd = monitor_get_fd(mon, fdname, &local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002051 } else {
2052 fd = qemu_parse_fd(fdname);
Laszlo Ersek59063662014-04-10 10:24:31 +02002053 if (fd == -1) {
2054 error_setg(&local_err, "Invalid file descriptor number '%s'",
2055 fdname);
2056 }
2057 }
2058 if (local_err) {
2059 error_propagate(errp, local_err);
2060 assert(fd == -1);
2061 } else {
2062 assert(fd != -1);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002063 }
2064
2065 return fd;
2066}
2067
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002068/* Please update hmp-commands.hx when adding or changing commands */
Wayne Xia816f8922011-10-12 11:32:41 +08002069static mon_cmd_t info_cmds[] = {
Pavel Butsykinda76ee72015-09-10 18:38:58 +03002070#include "hmp-commands-info.h"
2071 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002072};
2073
Wenchao Xiaa13ced52013-01-14 14:06:28 +08002074/* mon_cmds and info_cmds would be sorted at runtime */
2075static mon_cmd_t mon_cmds[] = {
2076#include "hmp-commands.h"
2077 { NULL, NULL, },
2078};
2079
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002080static const mon_cmd_t qmp_cmds[] = {
Anthony Liguorie3193602011-09-02 12:34:47 -05002081#include "qmp-commands-old.h"
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002082 { /* NULL */ },
2083};
2084
bellard9307c4c2004-04-04 12:57:25 +00002085/*******************************************************************/
2086
2087static const char *pch;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002088static sigjmp_buf expr_env;
bellard9307c4c2004-04-04 12:57:25 +00002089
bellard9307c4c2004-04-04 12:57:25 +00002090
Stefan Weil9c3175c2013-08-22 21:30:09 +02002091static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
2092expr_error(Monitor *mon, const char *fmt, ...)
bellard9dc39cb2004-03-14 21:38:27 +00002093{
Fam Zheng277acfe2013-08-20 10:58:21 +08002094 va_list ap;
2095 va_start(ap, fmt);
2096 monitor_vprintf(mon, fmt, ap);
2097 monitor_printf(mon, "\n");
2098 va_end(ap);
Peter Maydell6ab7e542013-02-20 15:21:09 +00002099 siglongjmp(expr_env, 1);
bellard9307c4c2004-04-04 12:57:25 +00002100}
2101
Markus Armbruster09b94182010-01-20 13:07:30 +01002102/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00002103static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002104{
Pavel Butsykinbf957282015-09-10 18:38:59 +03002105 const MonitorDef *md = target_monitor_defs();
bellard92a31b12005-02-10 22:00:52 +00002106 void *ptr;
2107
Pavel Butsykinbf957282015-09-10 18:38:59 +03002108 if (md == NULL) {
2109 return -1;
2110 }
2111
2112 for(; md->name != NULL; md++) {
bellard9307c4c2004-04-04 12:57:25 +00002113 if (compare_cmd(name, md->name)) {
2114 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002115 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002116 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002117 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00002118 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002119 switch(md->type) {
2120 case MD_I32:
2121 *pval = *(int32_t *)ptr;
2122 break;
2123 case MD_TLONG:
2124 *pval = *(target_long *)ptr;
2125 break;
2126 default:
2127 *pval = 0;
2128 break;
2129 }
bellard9307c4c2004-04-04 12:57:25 +00002130 }
2131 return 0;
2132 }
2133 }
2134 return -1;
2135}
2136
2137static void next(void)
2138{
Blue Swirl660f11b2009-07-31 21:16:51 +00002139 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002140 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002141 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002142 pch++;
2143 }
2144}
2145
aliguori376253e2009-03-05 23:01:23 +00002146static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002147
aliguori376253e2009-03-05 23:01:23 +00002148static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002149{
blueswir1c2efc952007-09-25 17:28:42 +00002150 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002151 char *p;
bellard6a00d602005-11-21 23:25:50 +00002152 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002153
2154 switch(*pch) {
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_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002162 break;
2163 case '~':
2164 next();
aliguori376253e2009-03-05 23:01:23 +00002165 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002166 break;
2167 case '(':
2168 next();
aliguori376253e2009-03-05 23:01:23 +00002169 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002170 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002171 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002172 }
2173 next();
2174 break;
bellard81d09122004-07-14 17:21:37 +00002175 case '\'':
2176 pch++;
2177 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002178 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002179 n = *pch;
2180 pch++;
2181 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002182 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002183 next();
2184 break;
bellard9307c4c2004-04-04 12:57:25 +00002185 case '$':
2186 {
2187 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002188 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002189
bellard9307c4c2004-04-04 12:57:25 +00002190 pch++;
2191 q = buf;
2192 while ((*pch >= 'a' && *pch <= 'z') ||
2193 (*pch >= 'A' && *pch <= 'Z') ||
2194 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002195 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002196 if ((q - buf) < sizeof(buf) - 1)
2197 *q++ = *pch;
2198 pch++;
2199 }
blueswir1cd390082008-11-16 13:53:32 +00002200 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002201 pch++;
2202 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002203 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01002204 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00002205 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00002206 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002207 }
2208 break;
2209 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002210 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002211 n = 0;
2212 break;
2213 default:
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002214 errno = 0;
bellard4f4fbf72006-06-25 18:28:12 +00002215 n = strtoull(pch, &p, 0);
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002216 if (errno == ERANGE) {
2217 expr_error(mon, "number too large");
2218 }
bellard9307c4c2004-04-04 12:57:25 +00002219 if (pch == p) {
Fam Zheng277acfe2013-08-20 10:58:21 +08002220 expr_error(mon, "invalid char '%c' in expression", *p);
bellard9307c4c2004-04-04 12:57:25 +00002221 }
2222 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002223 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002224 pch++;
2225 break;
2226 }
2227 return n;
2228}
2229
2230
aliguori376253e2009-03-05 23:01:23 +00002231static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002232{
blueswir1c2efc952007-09-25 17:28:42 +00002233 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002234 int op;
ths3b46e622007-09-17 08:09:54 +00002235
aliguori376253e2009-03-05 23:01:23 +00002236 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002237 for(;;) {
2238 op = *pch;
2239 if (op != '*' && op != '/' && op != '%')
2240 break;
2241 next();
aliguori376253e2009-03-05 23:01:23 +00002242 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002243 switch(op) {
2244 default:
2245 case '*':
2246 val *= val2;
2247 break;
2248 case '/':
2249 case '%':
ths5fafdf22007-09-16 21:08:06 +00002250 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002251 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002252 if (op == '/')
2253 val /= val2;
2254 else
2255 val %= val2;
2256 break;
2257 }
2258 }
2259 return val;
2260}
2261
aliguori376253e2009-03-05 23:01:23 +00002262static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002263{
blueswir1c2efc952007-09-25 17:28:42 +00002264 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002265 int op;
bellard9307c4c2004-04-04 12:57:25 +00002266
aliguori376253e2009-03-05 23:01:23 +00002267 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002268 for(;;) {
2269 op = *pch;
2270 if (op != '&' && op != '|' && op != '^')
2271 break;
2272 next();
aliguori376253e2009-03-05 23:01:23 +00002273 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002274 switch(op) {
2275 default:
2276 case '&':
2277 val &= val2;
2278 break;
2279 case '|':
2280 val |= val2;
2281 break;
2282 case '^':
2283 val ^= val2;
2284 break;
2285 }
2286 }
2287 return val;
2288}
2289
aliguori376253e2009-03-05 23:01:23 +00002290static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002291{
blueswir1c2efc952007-09-25 17:28:42 +00002292 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002293 int op;
bellard9307c4c2004-04-04 12:57:25 +00002294
aliguori376253e2009-03-05 23:01:23 +00002295 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002296 for(;;) {
2297 op = *pch;
2298 if (op != '+' && op != '-')
2299 break;
2300 next();
aliguori376253e2009-03-05 23:01:23 +00002301 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002302 if (op == '+')
2303 val += val2;
2304 else
2305 val -= val2;
2306 }
2307 return val;
2308}
2309
aliguori376253e2009-03-05 23:01:23 +00002310static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002311{
2312 pch = *pp;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002313 if (sigsetjmp(expr_env, 0)) {
bellard9307c4c2004-04-04 12:57:25 +00002314 *pp = pch;
2315 return -1;
2316 }
blueswir1cd390082008-11-16 13:53:32 +00002317 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002318 pch++;
aliguori376253e2009-03-05 23:01:23 +00002319 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002320 *pp = pch;
2321 return 0;
2322}
2323
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002324static int get_double(Monitor *mon, double *pval, const char **pp)
2325{
2326 const char *p = *pp;
2327 char *tailp;
2328 double d;
2329
2330 d = strtod(p, &tailp);
2331 if (tailp == p) {
2332 monitor_printf(mon, "Number expected\n");
2333 return -1;
2334 }
2335 if (d != d || d - d != 0) {
2336 /* NaN or infinity */
2337 monitor_printf(mon, "Bad number\n");
2338 return -1;
2339 }
2340 *pval = d;
2341 *pp = tailp;
2342 return 0;
2343}
2344
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002345/*
2346 * Store the command-name in cmdname, and return a pointer to
2347 * the remaining of the command string.
2348 */
2349static const char *get_command_name(const char *cmdline,
2350 char *cmdname, size_t nlen)
2351{
2352 size_t len;
2353 const char *p, *pstart;
2354
2355 p = cmdline;
2356 while (qemu_isspace(*p))
2357 p++;
2358 if (*p == '\0')
2359 return NULL;
2360 pstart = p;
2361 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2362 p++;
2363 len = p - pstart;
2364 if (len > nlen - 1)
2365 len = nlen - 1;
2366 memcpy(cmdname, pstart, len);
2367 cmdname[len] = '\0';
2368 return p;
2369}
2370
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002371/**
2372 * Read key of 'type' into 'key' and return the current
2373 * 'type' pointer.
2374 */
2375static char *key_get_info(const char *type, char **key)
2376{
2377 size_t len;
2378 char *p, *str;
2379
2380 if (*type == ',')
2381 type++;
2382
2383 p = strchr(type, ':');
2384 if (!p) {
2385 *key = NULL;
2386 return NULL;
2387 }
2388 len = p - type;
2389
Anthony Liguori7267c092011-08-20 22:09:37 -05002390 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002391 memcpy(str, type, len);
2392 str[len] = '\0';
2393
2394 *key = str;
2395 return ++p;
2396}
2397
bellard9307c4c2004-04-04 12:57:25 +00002398static int default_fmt_format = 'x';
2399static int default_fmt_size = 4;
2400
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002401static int is_valid_option(const char *c, const char *typestr)
2402{
2403 char option[3];
2404
2405 option[0] = '-';
2406 option[1] = *c;
2407 option[2] = '\0';
2408
2409 typestr = strstr(typestr, option);
2410 return (typestr != NULL);
2411}
2412
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03002413static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
2414 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002415{
2416 const mon_cmd_t *cmd;
2417
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03002418 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002419 if (compare_cmd(cmdname, cmd->name)) {
2420 return cmd;
2421 }
2422 }
2423
2424 return NULL;
2425}
2426
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03002427static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
2428{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002429 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03002430}
2431
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002432/*
Bandan Dasae502122015-06-03 18:38:08 -04002433 * Parse command name from @cmdp according to command table @table.
2434 * If blank, return NULL.
2435 * Else, if no valid command can be found, report to @mon, and return
2436 * NULL.
2437 * Else, change @cmdp to point right behind the name, and return its
2438 * command table entry.
2439 * Do not assume the return value points into @table! It doesn't when
2440 * the command is found in a sub-command table.
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002441 */
Anthony Liguoric227f092009-10-01 16:12:16 -05002442static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Bandan Dasae502122015-06-03 18:38:08 -04002443 const char **cmdp,
2444 mon_cmd_t *table)
bellard9307c4c2004-04-04 12:57:25 +00002445{
Bandan Dasae502122015-06-03 18:38:08 -04002446 const char *p;
Anthony Liguoric227f092009-10-01 16:12:16 -05002447 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002448 char cmdname[256];
bellard9dc39cb2004-03-14 21:38:27 +00002449
bellard9307c4c2004-04-04 12:57:25 +00002450 /* extract the command name */
Bandan Dasae502122015-06-03 18:38:08 -04002451 p = get_command_name(*cmdp, cmdname, sizeof(cmdname));
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002452 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002453 return NULL;
ths3b46e622007-09-17 08:09:54 +00002454
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002455 cmd = search_dispatch_table(table, cmdname);
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002456 if (!cmd) {
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002457 monitor_printf(mon, "unknown command: '%.*s'\n",
Bandan Dasae502122015-06-03 18:38:08 -04002458 (int)(p - *cmdp), *cmdp);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002459 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002460 }
bellard9307c4c2004-04-04 12:57:25 +00002461
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002462 /* filter out following useless space */
2463 while (qemu_isspace(*p)) {
2464 p++;
2465 }
Bandan Dasae502122015-06-03 18:38:08 -04002466
2467 *cmdp = p;
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002468 /* search sub command */
Bandan Dasae502122015-06-03 18:38:08 -04002469 if (cmd->sub_table != NULL && *p != '\0') {
2470 return monitor_parse_command(mon, cmdp, cmd->sub_table);
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002471 }
2472
Bandan Dasae502122015-06-03 18:38:08 -04002473 return cmd;
2474}
2475
2476/*
2477 * Parse arguments for @cmd.
2478 * If it can't be parsed, report to @mon, and return NULL.
2479 * Else, insert command arguments into a QDict, and return it.
2480 * Note: On success, caller has to free the QDict structure.
2481 */
2482
2483static QDict *monitor_parse_arguments(Monitor *mon,
2484 const char **endp,
2485 const mon_cmd_t *cmd)
2486{
2487 const char *typestr;
2488 char *key;
2489 int c;
2490 const char *p = *endp;
2491 char buf[1024];
2492 QDict *qdict = qdict_new();
2493
bellard9307c4c2004-04-04 12:57:25 +00002494 /* parse the parameters */
2495 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00002496 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002497 typestr = key_get_info(typestr, &key);
2498 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00002499 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002500 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00002501 typestr++;
2502 switch(c) {
2503 case 'F':
bellard81d09122004-07-14 17:21:37 +00002504 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002505 case 's':
2506 {
2507 int ret;
ths3b46e622007-09-17 08:09:54 +00002508
blueswir1cd390082008-11-16 13:53:32 +00002509 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002510 p++;
2511 if (*typestr == '?') {
2512 typestr++;
2513 if (*p == '\0') {
2514 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03002515 break;
bellard9307c4c2004-04-04 12:57:25 +00002516 }
2517 }
2518 ret = get_str(buf, sizeof(buf), &p);
2519 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002520 switch(c) {
2521 case 'F':
aliguori376253e2009-03-05 23:01:23 +00002522 monitor_printf(mon, "%s: filename expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002523 cmd->name);
bellard81d09122004-07-14 17:21:37 +00002524 break;
2525 case 'B':
aliguori376253e2009-03-05 23:01:23 +00002526 monitor_printf(mon, "%s: block device name expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002527 cmd->name);
bellard81d09122004-07-14 17:21:37 +00002528 break;
2529 default:
Bandan Dasae502122015-06-03 18:38:08 -04002530 monitor_printf(mon, "%s: string expected\n", cmd->name);
bellard81d09122004-07-14 17:21:37 +00002531 break;
2532 }
bellard9307c4c2004-04-04 12:57:25 +00002533 goto fail;
2534 }
Luiz Capitulino53773582009-08-28 15:27:25 -03002535 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00002536 }
2537 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01002538 case 'O':
2539 {
2540 QemuOptsList *opts_list;
2541 QemuOpts *opts;
2542
2543 opts_list = qemu_find_opts(key);
2544 if (!opts_list || opts_list->desc->name) {
2545 goto bad_type;
2546 }
2547 while (qemu_isspace(*p)) {
2548 p++;
2549 }
2550 if (!*p)
2551 break;
2552 if (get_str(buf, sizeof(buf), &p) < 0) {
2553 goto fail;
2554 }
Markus Armbruster70b94332015-02-13 12:50:26 +01002555 opts = qemu_opts_parse_noisily(opts_list, buf, true);
Markus Armbruster361127d2010-02-10 20:24:35 +01002556 if (!opts) {
2557 goto fail;
2558 }
2559 qemu_opts_to_qdict(opts, qdict);
2560 qemu_opts_del(opts);
2561 }
2562 break;
bellard9307c4c2004-04-04 12:57:25 +00002563 case '/':
2564 {
2565 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002566
blueswir1cd390082008-11-16 13:53:32 +00002567 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002568 p++;
2569 if (*p == '/') {
2570 /* format found */
2571 p++;
2572 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002573 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002574 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002575 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002576 count = count * 10 + (*p - '0');
2577 p++;
2578 }
2579 }
2580 size = -1;
2581 format = -1;
2582 for(;;) {
2583 switch(*p) {
2584 case 'o':
2585 case 'd':
2586 case 'u':
2587 case 'x':
2588 case 'i':
2589 case 'c':
2590 format = *p++;
2591 break;
2592 case 'b':
2593 size = 1;
2594 p++;
2595 break;
2596 case 'h':
2597 size = 2;
2598 p++;
2599 break;
2600 case 'w':
2601 size = 4;
2602 p++;
2603 break;
2604 case 'g':
2605 case 'L':
2606 size = 8;
2607 p++;
2608 break;
2609 default:
2610 goto next;
2611 }
2612 }
2613 next:
blueswir1cd390082008-11-16 13:53:32 +00002614 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00002615 monitor_printf(mon, "invalid char in format: '%c'\n",
2616 *p);
bellard9307c4c2004-04-04 12:57:25 +00002617 goto fail;
2618 }
bellard9307c4c2004-04-04 12:57:25 +00002619 if (format < 0)
2620 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002621 if (format != 'i') {
2622 /* for 'i', not specifying a size gives -1 as size */
2623 if (size < 0)
2624 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002625 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002626 }
bellard9307c4c2004-04-04 12:57:25 +00002627 default_fmt_format = format;
2628 } else {
2629 count = 1;
2630 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002631 if (format != 'i') {
2632 size = default_fmt_size;
2633 } else {
2634 size = -1;
2635 }
bellard9307c4c2004-04-04 12:57:25 +00002636 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002637 qdict_put(qdict, "count", qint_from_int(count));
2638 qdict_put(qdict, "format", qint_from_int(format));
2639 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00002640 }
2641 break;
2642 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002643 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002644 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00002645 {
blueswir1c2efc952007-09-25 17:28:42 +00002646 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00002647
blueswir1cd390082008-11-16 13:53:32 +00002648 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002649 p++;
bellard34405572004-06-08 00:55:58 +00002650 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002651 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03002652 if (*p == '\0') {
2653 typestr++;
2654 break;
2655 }
bellard34405572004-06-08 00:55:58 +00002656 } else {
2657 if (*p == '.') {
2658 p++;
blueswir1cd390082008-11-16 13:53:32 +00002659 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00002660 p++;
bellard34405572004-06-08 00:55:58 +00002661 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03002662 typestr++;
2663 break;
bellard34405572004-06-08 00:55:58 +00002664 }
2665 }
bellard13224a82006-07-14 22:03:35 +00002666 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002667 }
aliguori376253e2009-03-05 23:01:23 +00002668 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00002669 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002670 /* Check if 'i' is greater than 32-bit */
2671 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
Bandan Dasae502122015-06-03 18:38:08 -04002672 monitor_printf(mon, "\'%s\' has failed: ", cmd->name);
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002673 monitor_printf(mon, "integer is for 32-bit values\n");
2674 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002675 } else if (c == 'M') {
Luiz Capitulino91162842012-04-26 17:34:30 -03002676 if (val < 0) {
2677 monitor_printf(mon, "enter a positive value\n");
2678 goto fail;
2679 }
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002680 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002681 }
Luiz Capitulino53773582009-08-28 15:27:25 -03002682 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00002683 }
2684 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02002685 case 'o':
2686 {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01002687 int64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02002688 char *end;
2689
2690 while (qemu_isspace(*p)) {
2691 p++;
2692 }
2693 if (*typestr == '?') {
2694 typestr++;
2695 if (*p == '\0') {
2696 break;
2697 }
2698 }
Marc-André Lureau4677bb42015-09-16 18:02:56 +02002699 val = qemu_strtosz(p, &end);
Jes Sorensendbc0c672010-10-21 17:15:47 +02002700 if (val < 0) {
2701 monitor_printf(mon, "invalid size\n");
2702 goto fail;
2703 }
2704 qdict_put(qdict, key, qint_from_int(val));
2705 p = end;
2706 }
2707 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01002708 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002709 {
2710 double val;
2711
2712 while (qemu_isspace(*p))
2713 p++;
2714 if (*typestr == '?') {
2715 typestr++;
2716 if (*p == '\0') {
2717 break;
2718 }
2719 }
2720 if (get_double(mon, &val, &p) < 0) {
2721 goto fail;
2722 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02002723 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01002724 switch (*p) {
2725 case 'm':
2726 val /= 1e3; p += 2; break;
2727 case 'u':
2728 val /= 1e6; p += 2; break;
2729 case 'n':
2730 val /= 1e9; p += 2; break;
2731 }
2732 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002733 if (*p && !qemu_isspace(*p)) {
2734 monitor_printf(mon, "Unknown unit suffix\n");
2735 goto fail;
2736 }
2737 qdict_put(qdict, key, qfloat_from_double(val));
2738 }
2739 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002740 case 'b':
2741 {
2742 const char *beg;
Eric Blakefc48ffc2015-05-15 16:24:59 -06002743 bool val;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002744
2745 while (qemu_isspace(*p)) {
2746 p++;
2747 }
2748 beg = p;
2749 while (qemu_isgraph(*p)) {
2750 p++;
2751 }
2752 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06002753 val = true;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002754 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06002755 val = false;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002756 } else {
2757 monitor_printf(mon, "Expected 'on' or 'off'\n");
2758 goto fail;
2759 }
Eric Blakefc48ffc2015-05-15 16:24:59 -06002760 qdict_put(qdict, key, qbool_from_bool(val));
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002761 }
2762 break;
bellard9307c4c2004-04-04 12:57:25 +00002763 case '-':
2764 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002765 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03002766 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00002767 /* option */
ths3b46e622007-09-17 08:09:54 +00002768
bellard9307c4c2004-04-04 12:57:25 +00002769 c = *typestr++;
2770 if (c == '\0')
2771 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00002772 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002773 p++;
bellard9307c4c2004-04-04 12:57:25 +00002774 if (*p == '-') {
2775 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002776 if(c != *p) {
2777 if(!is_valid_option(p, typestr)) {
2778
2779 monitor_printf(mon, "%s: unsupported option -%c\n",
Bandan Dasae502122015-06-03 18:38:08 -04002780 cmd->name, *p);
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002781 goto fail;
2782 } else {
2783 skip_key = 1;
2784 }
bellard9307c4c2004-04-04 12:57:25 +00002785 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002786 if(skip_key) {
2787 p = tmp;
2788 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03002789 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002790 p++;
Eric Blakefc48ffc2015-05-15 16:24:59 -06002791 qdict_put(qdict, key, qbool_from_bool(true));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002792 }
bellard9307c4c2004-04-04 12:57:25 +00002793 }
bellard9307c4c2004-04-04 12:57:25 +00002794 }
2795 break;
Wenchao Xia129be002013-08-27 20:38:26 +08002796 case 'S':
2797 {
2798 /* package all remaining string */
2799 int len;
2800
2801 while (qemu_isspace(*p)) {
2802 p++;
2803 }
2804 if (*typestr == '?') {
2805 typestr++;
2806 if (*p == '\0') {
2807 /* no remaining string: NULL argument */
2808 break;
2809 }
2810 }
2811 len = strlen(p);
2812 if (len <= 0) {
2813 monitor_printf(mon, "%s: string expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002814 cmd->name);
Bandan Dase549d2a2015-06-03 18:38:10 -04002815 goto fail;
Wenchao Xia129be002013-08-27 20:38:26 +08002816 }
2817 qdict_put(qdict, key, qstring_from_str(p));
2818 p += len;
2819 }
2820 break;
bellard9307c4c2004-04-04 12:57:25 +00002821 default:
2822 bad_type:
Bandan Dasae502122015-06-03 18:38:08 -04002823 monitor_printf(mon, "%s: unknown type '%c'\n", cmd->name, c);
bellard9307c4c2004-04-04 12:57:25 +00002824 goto fail;
2825 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002826 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002827 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00002828 }
2829 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00002830 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002831 p++;
2832 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00002833 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
Bandan Dasae502122015-06-03 18:38:08 -04002834 cmd->name);
bellard9307c4c2004-04-04 12:57:25 +00002835 goto fail;
2836 }
2837
Bandan Dasae502122015-06-03 18:38:08 -04002838 return qdict;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02002839
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002840fail:
Bandan Dasae502122015-06-03 18:38:08 -04002841 QDECREF(qdict);
Anthony Liguori7267c092011-08-20 22:09:37 -05002842 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002843 return NULL;
2844}
2845
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01002846static void handle_hmp_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002847{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002848 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05002849 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002850
Bandan Dasae502122015-06-03 18:38:08 -04002851 cmd = monitor_parse_command(mon, &cmdline, mon->cmd_table);
2852 if (!cmd) {
2853 return;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002854 }
2855
Bandan Dasae502122015-06-03 18:38:08 -04002856 qdict = monitor_parse_arguments(mon, &cmdline, cmd);
2857 if (!qdict) {
Bandan Dasdd41eea2015-06-03 18:38:09 -04002858 monitor_printf(mon, "Try \"help %s\" for more information\n",
2859 cmd->name);
Bandan Dasae502122015-06-03 18:38:08 -04002860 return;
2861 }
2862
2863 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002864 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00002865}
2866
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08002867static void cmd_completion(Monitor *mon, const char *name, const char *list)
bellard81d09122004-07-14 17:21:37 +00002868{
2869 const char *p, *pstart;
2870 char cmd[128];
2871 int len;
2872
2873 p = list;
2874 for(;;) {
2875 pstart = p;
2876 p = strchr(p, '|');
2877 if (!p)
2878 p = pstart + strlen(pstart);
2879 len = p - pstart;
2880 if (len > sizeof(cmd) - 2)
2881 len = sizeof(cmd) - 2;
2882 memcpy(cmd, pstart, len);
2883 cmd[len] = '\0';
2884 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08002885 readline_add_completion(mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00002886 }
2887 if (*p == '\0')
2888 break;
2889 p++;
2890 }
2891}
2892
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08002893static void file_completion(Monitor *mon, const char *input)
bellard81d09122004-07-14 17:21:37 +00002894{
2895 DIR *ffs;
2896 struct dirent *d;
2897 char path[1024];
2898 char file[1024], file_prefix[1024];
2899 int input_path_len;
2900 const char *p;
2901
ths5fafdf22007-09-16 21:08:06 +00002902 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00002903 if (!p) {
2904 input_path_len = 0;
2905 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00002906 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00002907 } else {
2908 input_path_len = p - input + 1;
2909 memcpy(path, input, input_path_len);
2910 if (input_path_len > sizeof(path) - 1)
2911 input_path_len = sizeof(path) - 1;
2912 path[input_path_len] = '\0';
2913 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2914 }
Bandan Das19f2db52015-06-03 18:38:07 -04002915
bellard81d09122004-07-14 17:21:37 +00002916 ffs = opendir(path);
2917 if (!ffs)
2918 return;
2919 for(;;) {
2920 struct stat sb;
2921 d = readdir(ffs);
2922 if (!d)
2923 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09002924
2925 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
2926 continue;
2927 }
2928
bellard81d09122004-07-14 17:21:37 +00002929 if (strstart(d->d_name, file_prefix, NULL)) {
2930 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00002931 if (input_path_len < sizeof(file))
2932 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2933 d->d_name);
bellard81d09122004-07-14 17:21:37 +00002934 /* stat the file to find out if it's a directory.
2935 * In that case add a slash to speed up typing long paths
2936 */
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01002937 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
blueswir1363a37d2008-08-21 17:58:08 +00002938 pstrcat(file, sizeof(file), "/");
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01002939 }
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08002940 readline_add_completion(mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00002941 }
2942 }
2943 closedir(ffs);
2944}
2945
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002946static const char *next_arg_type(const char *typestr)
2947{
2948 const char *p = strchr(typestr, ':');
2949 return (p != NULL ? ++p : typestr);
2950}
2951
Hani Benhabiles40d19392014-05-07 23:41:30 +01002952static void add_completion_option(ReadLineState *rs, const char *str,
2953 const char *option)
2954{
2955 if (!str || !option) {
2956 return;
2957 }
2958 if (!strncmp(option, str, strlen(str))) {
2959 readline_add_completion(rs, option);
2960 }
2961}
2962
Hani Benhabiles13e315d2014-05-07 23:41:29 +01002963void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
2964{
2965 size_t len;
2966 ChardevBackendInfoList *list, *start;
2967
2968 if (nb_args != 2) {
2969 return;
2970 }
2971 len = strlen(str);
2972 readline_set_completion_index(rs, len);
2973
2974 start = list = qmp_query_chardev_backends(NULL);
2975 while (list) {
2976 const char *chr_name = list->value->name;
2977
2978 if (!strncmp(chr_name, str, len)) {
2979 readline_add_completion(rs, chr_name);
2980 }
2981 list = list->next;
2982 }
2983 qapi_free_ChardevBackendInfoList(start);
2984}
2985
Hani Benhabilesb162b492014-05-07 23:41:31 +01002986void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
2987{
2988 size_t len;
2989 int i;
2990
2991 if (nb_args != 2) {
2992 return;
2993 }
2994 len = strlen(str);
2995 readline_set_completion_index(rs, len);
2996 for (i = 0; NetClientOptionsKind_lookup[i]; i++) {
2997 add_completion_option(rs, str, NetClientOptionsKind_lookup[i]);
2998 }
2999}
3000
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003001void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003002{
3003 GSList *list, *elt;
3004 size_t len;
3005
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003006 if (nb_args != 2) {
3007 return;
3008 }
3009
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003010 len = strlen(str);
3011 readline_set_completion_index(rs, len);
3012 list = elt = object_class_get_list(TYPE_DEVICE, false);
3013 while (elt) {
3014 const char *name;
3015 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
3016 TYPE_DEVICE);
3017 name = object_class_get_name(OBJECT_CLASS(dc));
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003018
3019 if (!dc->cannot_instantiate_with_device_add_yet
3020 && !strncmp(name, str, len)) {
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003021 readline_add_completion(rs, name);
3022 }
3023 elt = elt->next;
3024 }
3025 g_slist_free(list);
3026}
3027
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003028void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003029{
3030 GSList *list, *elt;
3031 size_t len;
3032
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003033 if (nb_args != 2) {
3034 return;
3035 }
3036
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003037 len = strlen(str);
3038 readline_set_completion_index(rs, len);
3039 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
3040 while (elt) {
3041 const char *name;
3042
3043 name = object_class_get_name(OBJECT_CLASS(elt->data));
3044 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
3045 readline_add_completion(rs, name);
3046 }
3047 elt = elt->next;
3048 }
3049 g_slist_free(list);
3050}
3051
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003052static void peripheral_device_del_completion(ReadLineState *rs,
3053 const char *str, size_t len)
3054{
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003055 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
3056 GSList *list, *item;
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003057
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003058 list = qdev_build_hotpluggable_device_list(peripheral);
3059 if (!list) {
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003060 return;
3061 }
3062
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003063 for (item = list; item; item = g_slist_next(item)) {
3064 DeviceState *dev = item->data;
3065
3066 if (dev->id && !strncmp(str, dev->id, len)) {
3067 readline_add_completion(rs, dev->id);
3068 }
3069 }
3070
3071 g_slist_free(list);
3072}
3073
Hani Benhabiles6297d9a2014-05-07 23:41:28 +01003074void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3075{
3076 size_t len;
3077 ChardevInfoList *list, *start;
3078
3079 if (nb_args != 2) {
3080 return;
3081 }
3082 len = strlen(str);
3083 readline_set_completion_index(rs, len);
3084
3085 start = list = qmp_query_chardev(NULL);
3086 while (list) {
3087 ChardevInfo *chr = list->value;
3088
3089 if (!strncmp(chr->label, str, len)) {
3090 readline_add_completion(rs, chr->label);
3091 }
3092 list = list->next;
3093 }
3094 qapi_free_ChardevInfoList(start);
3095}
3096
Hani Benhabiles8e597772014-05-27 23:39:30 +01003097static void ringbuf_completion(ReadLineState *rs, const char *str)
3098{
3099 size_t len;
3100 ChardevInfoList *list, *start;
3101
3102 len = strlen(str);
3103 readline_set_completion_index(rs, len);
3104
3105 start = list = qmp_query_chardev(NULL);
3106 while (list) {
3107 ChardevInfo *chr_info = list->value;
3108
3109 if (!strncmp(chr_info->label, str, len)) {
3110 CharDriverState *chr = qemu_chr_find(chr_info->label);
3111 if (chr && chr_is_ringbuf(chr)) {
3112 readline_add_completion(rs, chr_info->label);
3113 }
3114 }
3115 list = list->next;
3116 }
3117 qapi_free_ChardevInfoList(start);
3118}
3119
Hani Benhabiles8e597772014-05-27 23:39:30 +01003120void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
3121{
3122 if (nb_args != 2) {
3123 return;
3124 }
3125 ringbuf_completion(rs, str);
3126}
3127
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003128void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
3129{
3130 size_t len;
3131
3132 if (nb_args != 2) {
3133 return;
3134 }
3135
3136 len = strlen(str);
3137 readline_set_completion_index(rs, len);
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003138 peripheral_device_del_completion(rs, str, len);
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003139}
3140
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003141void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003142{
3143 ObjectPropertyInfoList *list, *start;
3144 size_t len;
3145
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003146 if (nb_args != 2) {
3147 return;
3148 }
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003149 len = strlen(str);
3150 readline_set_completion_index(rs, len);
3151
3152 start = list = qmp_qom_list("/objects", NULL);
3153 while (list) {
3154 ObjectPropertyInfo *info = list->value;
3155
3156 if (!strncmp(info->type, "child<", 5)
3157 && !strncmp(info->name, str, len)) {
3158 readline_add_completion(rs, info->name);
3159 }
3160 list = list->next;
3161 }
3162 qapi_free_ObjectPropertyInfoList(start);
3163}
3164
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003165void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
3166{
3167 int i;
3168 char *sep;
3169 size_t len;
3170
3171 if (nb_args != 2) {
3172 return;
3173 }
3174 sep = strrchr(str, '-');
3175 if (sep) {
3176 str = sep + 1;
3177 }
3178 len = strlen(str);
3179 readline_set_completion_index(rs, len);
3180 for (i = 0; i < Q_KEY_CODE_MAX; i++) {
3181 if (!strncmp(str, QKeyCode_lookup[i], len)) {
3182 readline_add_completion(rs, QKeyCode_lookup[i]);
3183 }
3184 }
3185}
3186
Hani Benhabiles40d19392014-05-07 23:41:30 +01003187void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
3188{
3189 size_t len;
3190
3191 len = strlen(str);
3192 readline_set_completion_index(rs, len);
3193 if (nb_args == 2) {
Jason Wangeaed4832015-04-23 14:21:38 +08003194 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles40d19392014-05-07 23:41:30 +01003195 int count, i;
3196 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08003197 NET_CLIENT_OPTIONS_KIND_NONE,
3198 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003199 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles40d19392014-05-07 23:41:30 +01003200 const char *name = ncs[i]->name;
3201 if (!strncmp(str, name, len)) {
3202 readline_add_completion(rs, name);
3203 }
3204 }
3205 } else if (nb_args == 3) {
3206 add_completion_option(rs, str, "on");
3207 add_completion_option(rs, str, "off");
3208 }
3209}
3210
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003211void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
3212{
3213 int len, count, i;
Jason Wangeaed4832015-04-23 14:21:38 +08003214 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003215
3216 if (nb_args != 2) {
3217 return;
3218 }
3219
3220 len = strlen(str);
3221 readline_set_completion_index(rs, len);
3222 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_OPTIONS_KIND_NIC,
Jason Wangeaed4832015-04-23 14:21:38 +08003223 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003224 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003225 QemuOpts *opts;
3226 const char *name = ncs[i]->name;
3227 if (strncmp(str, name, len)) {
3228 continue;
3229 }
3230 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
3231 if (opts) {
3232 readline_add_completion(rs, name);
3233 }
3234 }
3235}
3236
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003237void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
3238{
3239 size_t len;
3240
3241 len = strlen(str);
3242 readline_set_completion_index(rs, len);
3243 if (nb_args == 2) {
3244 TraceEventID id;
3245 for (id = 0; id < trace_event_count(); id++) {
3246 const char *event_name = trace_event_get_name(trace_event_id(id));
3247 if (!strncmp(str, event_name, len)) {
3248 readline_add_completion(rs, event_name);
3249 }
3250 }
3251 } else if (nb_args == 3) {
3252 add_completion_option(rs, str, "on");
3253 add_completion_option(rs, str, "off");
3254 }
3255}
3256
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003257void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
3258{
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003259 int i;
3260
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003261 if (nb_args != 2) {
3262 return;
3263 }
3264 readline_set_completion_index(rs, strlen(str));
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003265 for (i = 0; WatchdogExpirationAction_lookup[i]; i++) {
3266 add_completion_option(rs, str, WatchdogExpirationAction_lookup[i]);
3267 }
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003268}
3269
Hani Benhabilesc68a0402014-05-27 23:39:32 +01003270void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
3271 const char *str)
3272{
3273 size_t len;
3274
3275 len = strlen(str);
3276 readline_set_completion_index(rs, len);
3277 if (nb_args == 2) {
3278 int i;
3279 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
3280 const char *name = MigrationCapability_lookup[i];
3281 if (!strncmp(str, name, len)) {
3282 readline_add_completion(rs, name);
3283 }
3284 }
3285 } else if (nb_args == 3) {
3286 add_completion_option(rs, str, "on");
3287 add_completion_option(rs, str, "off");
3288 }
3289}
3290
Liang Li50e9a622015-03-23 16:32:29 +08003291void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
3292 const char *str)
3293{
3294 size_t len;
3295
3296 len = strlen(str);
3297 readline_set_completion_index(rs, len);
3298 if (nb_args == 2) {
3299 int i;
3300 for (i = 0; i < MIGRATION_PARAMETER_MAX; i++) {
3301 const char *name = MigrationParameter_lookup[i];
3302 if (!strncmp(str, name, len)) {
3303 readline_add_completion(rs, name);
3304 }
3305 }
3306 }
3307}
3308
Hani Benhabilese3bb5322014-05-27 23:39:34 +01003309void host_net_add_completion(ReadLineState *rs, int nb_args, const char *str)
3310{
3311 int i;
3312 size_t len;
3313 if (nb_args != 2) {
3314 return;
3315 }
3316 len = strlen(str);
3317 readline_set_completion_index(rs, len);
3318 for (i = 0; host_net_devices[i]; i++) {
3319 if (!strncmp(host_net_devices[i], str, len)) {
3320 readline_add_completion(rs, host_net_devices[i]);
3321 }
3322 }
3323}
3324
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003325void host_net_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3326{
Jason Wangeaed4832015-04-23 14:21:38 +08003327 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003328 int count, i, len;
3329
3330 len = strlen(str);
3331 readline_set_completion_index(rs, len);
3332 if (nb_args == 2) {
3333 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08003334 NET_CLIENT_OPTIONS_KIND_NONE,
3335 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003336 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003337 int id;
3338 char name[16];
3339
3340 if (net_hub_id_for_client(ncs[i], &id)) {
3341 continue;
3342 }
3343 snprintf(name, sizeof(name), "%d", id);
3344 if (!strncmp(str, name, len)) {
3345 readline_add_completion(rs, name);
3346 }
3347 }
3348 return;
3349 } else if (nb_args == 3) {
3350 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08003351 NET_CLIENT_OPTIONS_KIND_NIC,
3352 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003353 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Jason Wang2c4681f2015-02-02 15:06:38 +08003354 int id;
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003355 const char *name;
3356
Jason Wang2c4681f2015-02-02 15:06:38 +08003357 if (ncs[i]->info->type == NET_CLIENT_OPTIONS_KIND_HUBPORT ||
3358 net_hub_id_for_client(ncs[i], &id)) {
3359 continue;
3360 }
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003361 name = ncs[i]->name;
3362 if (!strncmp(str, name, len)) {
3363 readline_add_completion(rs, name);
3364 }
3365 }
3366 return;
3367 }
3368}
3369
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003370static void vm_completion(ReadLineState *rs, const char *str)
3371{
3372 size_t len;
3373 BlockDriverState *bs = NULL;
3374
3375 len = strlen(str);
3376 readline_set_completion_index(rs, len);
3377 while ((bs = bdrv_next(bs))) {
3378 SnapshotInfoList *snapshots, *snapshot;
3379
3380 if (!bdrv_can_snapshot(bs)) {
3381 continue;
3382 }
3383 if (bdrv_query_snapshot_info_list(bs, &snapshots, NULL)) {
3384 continue;
3385 }
3386 snapshot = snapshots;
3387 while (snapshot) {
3388 char *completion = snapshot->value->name;
3389 if (!strncmp(str, completion, len)) {
3390 readline_add_completion(rs, completion);
3391 }
3392 completion = snapshot->value->id;
3393 if (!strncmp(str, completion, len)) {
3394 readline_add_completion(rs, completion);
3395 }
3396 snapshot = snapshot->next;
3397 }
3398 qapi_free_SnapshotInfoList(snapshots);
3399 }
3400
3401}
3402
3403void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
3404{
3405 if (nb_args == 2) {
3406 vm_completion(rs, str);
3407 }
3408}
3409
3410void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
3411{
3412 if (nb_args == 2) {
3413 vm_completion(rs, str);
3414 }
3415}
3416
Wenchao Xiac35b6402013-08-27 20:38:24 +08003417static void monitor_find_completion_by_table(Monitor *mon,
3418 const mon_cmd_t *cmd_table,
3419 char **args,
3420 int nb_args)
bellard81d09122004-07-14 17:21:37 +00003421{
3422 const char *cmdname;
Wenchao Xiac35b6402013-08-27 20:38:24 +08003423 int i;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003424 const char *ptype, *str, *name;
Anthony Liguoric227f092009-10-01 16:12:16 -05003425 const mon_cmd_t *cmd;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003426 BlockDriverState *bs;
bellard81d09122004-07-14 17:21:37 +00003427
bellard81d09122004-07-14 17:21:37 +00003428 if (nb_args <= 1) {
3429 /* command completion */
3430 if (nb_args == 0)
3431 cmdname = "";
3432 else
3433 cmdname = args[0];
Wenchao Xiad2674b22013-08-27 20:38:16 +08003434 readline_set_completion_index(mon->rs, strlen(cmdname));
Wenchao Xiac35b6402013-08-27 20:38:24 +08003435 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003436 cmd_completion(mon, cmdname, cmd->name);
bellard81d09122004-07-14 17:21:37 +00003437 }
3438 } else {
3439 /* find the command */
Wenchao Xiac35b6402013-08-27 20:38:24 +08003440 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Jan Kiszka03a63482010-06-16 00:38:33 +02003441 if (compare_cmd(args[0], cmd->name)) {
3442 break;
3443 }
bellard81d09122004-07-14 17:21:37 +00003444 }
Jan Kiszka03a63482010-06-16 00:38:33 +02003445 if (!cmd->name) {
Wenchao Xiac35b6402013-08-27 20:38:24 +08003446 return;
Jan Kiszka03a63482010-06-16 00:38:33 +02003447 }
3448
Wenchao Xiad903a772013-08-27 20:38:25 +08003449 if (cmd->sub_table) {
3450 /* do the job again */
Stefan Weile7ae7712015-03-08 19:30:01 +01003451 monitor_find_completion_by_table(mon, cmd->sub_table,
3452 &args[1], nb_args - 1);
3453 return;
Wenchao Xiad903a772013-08-27 20:38:25 +08003454 }
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003455 if (cmd->command_completion) {
Stefan Weile7ae7712015-03-08 19:30:01 +01003456 cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
3457 return;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003458 }
Wenchao Xiad903a772013-08-27 20:38:25 +08003459
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003460 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003461 for(i = 0; i < nb_args - 2; i++) {
3462 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003463 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003464 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003465 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003466 }
3467 }
3468 str = args[nb_args - 1];
Kevin Wolf48fe86f2014-11-12 16:24:02 +01003469 while (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02003470 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00003471 }
bellard81d09122004-07-14 17:21:37 +00003472 switch(*ptype) {
3473 case 'F':
3474 /* file completion */
Wenchao Xiad2674b22013-08-27 20:38:16 +08003475 readline_set_completion_index(mon->rs, strlen(str));
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003476 file_completion(mon, str);
bellard81d09122004-07-14 17:21:37 +00003477 break;
3478 case 'B':
3479 /* block device name completion */
Wenchao Xia599a9262013-08-27 20:38:15 +08003480 readline_set_completion_index(mon->rs, strlen(str));
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003481 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
3482 name = bdrv_get_device_name(bs);
3483 if (str[0] == '\0' ||
3484 !strncmp(name, str, strlen(str))) {
3485 readline_add_completion(mon->rs, name);
3486 }
3487 }
bellard81d09122004-07-14 17:21:37 +00003488 break;
bellard7fe48482004-10-09 18:08:01 +00003489 case 's':
Wenchao Xia129be002013-08-27 20:38:26 +08003490 case 'S':
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003491 if (!strcmp(cmd->name, "help|?")) {
Wenchao Xia7ca0e062013-08-27 20:38:27 +08003492 monitor_find_completion_by_table(mon, cmd_table,
3493 &args[1], nb_args - 1);
bellard7fe48482004-10-09 18:08:01 +00003494 }
3495 break;
bellard81d09122004-07-14 17:21:37 +00003496 default:
3497 break;
3498 }
3499 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08003500}
3501
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003502static void monitor_find_completion(void *opaque,
Wenchao Xiac35b6402013-08-27 20:38:24 +08003503 const char *cmdline)
3504{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003505 Monitor *mon = opaque;
Wenchao Xiac35b6402013-08-27 20:38:24 +08003506 char *args[MAX_ARGS];
3507 int nb_args, len;
3508
3509 /* 1. parse the cmdline */
3510 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
3511 return;
3512 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08003513
3514 /* if the line ends with a space, it means we want to complete the
3515 next arg */
3516 len = strlen(cmdline);
3517 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
3518 if (nb_args >= MAX_ARGS) {
3519 goto cleanup;
3520 }
3521 args[nb_args++] = g_strdup("");
3522 }
3523
3524 /* 2. auto complete according to args */
3525 monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args);
Jan Kiszka03a63482010-06-16 00:38:33 +02003526
3527cleanup:
Wenchao Xiadcc70cd2013-08-27 20:38:22 +08003528 free_cmdline_args(args, nb_args);
bellard81d09122004-07-14 17:21:37 +00003529}
3530
aliguori731b0362009-03-05 23:01:42 +00003531static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003532{
aliguori731b0362009-03-05 23:01:42 +00003533 Monitor *mon = opaque;
3534
Jan Kiszkac62313b2009-12-04 14:05:29 +01003535 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003536}
3537
Markus Armbruster40861822015-05-29 10:27:16 +02003538static bool invalid_qmp_mode(const Monitor *mon, const mon_cmd_t *cmd,
3539 Error **errp)
Luiz Capitulino09069b12010-02-04 18:10:06 -02003540{
Markus Armbruster485febc2015-03-13 17:25:50 +01003541 bool is_cap = cmd->mhandler.cmd_new == qmp_capabilities;
Markus Armbrusterf994b252015-03-06 19:51:51 +01003542
3543 if (is_cap && mon->qmp.in_command_mode) {
Markus Armbruster40861822015-05-29 10:27:16 +02003544 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
3545 "Capabilities negotiation is already complete, command "
3546 "'%s' ignored", cmd->name);
Eric Blake2d5a8342015-04-15 09:19:23 -06003547 return true;
3548 }
Markus Armbrusterf994b252015-03-06 19:51:51 +01003549 if (!is_cap && !mon->qmp.in_command_mode) {
Markus Armbruster40861822015-05-29 10:27:16 +02003550 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
3551 "Expecting capabilities negotiation with "
3552 "'qmp_capabilities' before command '%s'", cmd->name);
Eric Blake2d5a8342015-04-15 09:19:23 -06003553 return true;
3554 }
3555 return false;
Luiz Capitulino09069b12010-02-04 18:10:06 -02003556}
3557
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003558/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03003559 * Argument validation rules:
3560 *
3561 * 1. The argument must exist in cmd_args qdict
3562 * 2. The argument type must be the expected one
3563 *
3564 * Special case: If the argument doesn't exist in cmd_args and
3565 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
3566 * checking is skipped for it.
3567 */
Markus Armbruster326283a2015-03-02 18:39:09 +01003568static void check_client_args_type(const QDict *client_args,
3569 const QDict *cmd_args, int flags,
3570 Error **errp)
Luiz Capitulino4af91932010-06-22 11:44:05 -03003571{
3572 const QDictEntry *ent;
3573
3574 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
3575 QObject *obj;
3576 QString *arg_type;
3577 const QObject *client_arg = qdict_entry_value(ent);
3578 const char *client_arg_name = qdict_entry_key(ent);
3579
3580 obj = qdict_get(cmd_args, client_arg_name);
3581 if (!obj) {
3582 if (flags & QMP_ACCEPT_UNKNOWNS) {
3583 /* handler accepts unknowns */
3584 continue;
3585 }
3586 /* client arg doesn't exist */
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003587 error_setg(errp, QERR_INVALID_PARAMETER, client_arg_name);
Markus Armbruster326283a2015-03-02 18:39:09 +01003588 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003589 }
3590
3591 arg_type = qobject_to_qstring(obj);
3592 assert(arg_type != NULL);
3593
3594 /* check if argument's type is correct */
3595 switch (qstring_get_str(arg_type)[0]) {
3596 case 'F':
3597 case 'B':
3598 case 's':
3599 if (qobject_type(client_arg) != QTYPE_QSTRING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003600 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3601 client_arg_name, "string");
Markus Armbruster326283a2015-03-02 18:39:09 +01003602 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003603 }
3604 break;
3605 case 'i':
3606 case 'l':
3607 case 'M':
Jes Sorensendbc0c672010-10-21 17:15:47 +02003608 case 'o':
Luiz Capitulino4af91932010-06-22 11:44:05 -03003609 if (qobject_type(client_arg) != QTYPE_QINT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003610 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3611 client_arg_name, "int");
Markus Armbruster326283a2015-03-02 18:39:09 +01003612 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003613 }
3614 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003615 case 'T':
3616 if (qobject_type(client_arg) != QTYPE_QINT &&
3617 qobject_type(client_arg) != QTYPE_QFLOAT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003618 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3619 client_arg_name, "number");
Markus Armbruster326283a2015-03-02 18:39:09 +01003620 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003621 }
3622 break;
3623 case 'b':
3624 case '-':
3625 if (qobject_type(client_arg) != QTYPE_QBOOL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003626 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3627 client_arg_name, "bool");
Markus Armbruster326283a2015-03-02 18:39:09 +01003628 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003629 }
3630 break;
3631 case 'O':
3632 assert(flags & QMP_ACCEPT_UNKNOWNS);
3633 break;
Paolo Bonzinib9f89782012-03-22 12:51:11 +01003634 case 'q':
3635 /* Any QObject can be passed. */
3636 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003637 case '/':
3638 case '.':
3639 /*
3640 * These types are not supported by QMP and thus are not
3641 * handled here. Fall through.
3642 */
3643 default:
3644 abort();
3645 }
3646 }
Luiz Capitulino4af91932010-06-22 11:44:05 -03003647}
3648
3649/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003650 * - Check if the client has passed all mandatory args
3651 * - Set special flags for argument validation
3652 */
Markus Armbruster326283a2015-03-02 18:39:09 +01003653static void check_mandatory_args(const QDict *cmd_args,
3654 const QDict *client_args, int *flags,
3655 Error **errp)
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003656{
3657 const QDictEntry *ent;
3658
3659 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
3660 const char *cmd_arg_name = qdict_entry_key(ent);
3661 QString *type = qobject_to_qstring(qdict_entry_value(ent));
3662 assert(type != NULL);
3663
3664 if (qstring_get_str(type)[0] == 'O') {
3665 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
3666 *flags |= QMP_ACCEPT_UNKNOWNS;
3667 } else if (qstring_get_str(type)[0] != '-' &&
3668 qstring_get_str(type)[1] != '?' &&
3669 !qdict_haskey(client_args, cmd_arg_name)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003670 error_setg(errp, QERR_MISSING_PARAMETER, cmd_arg_name);
Markus Armbruster326283a2015-03-02 18:39:09 +01003671 return;
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003672 }
3673 }
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003674}
3675
3676static QDict *qdict_from_args_type(const char *args_type)
3677{
3678 int i;
3679 QDict *qdict;
3680 QString *key, *type, *cur_qs;
3681
3682 assert(args_type != NULL);
3683
3684 qdict = qdict_new();
3685
3686 if (args_type == NULL || args_type[0] == '\0') {
3687 /* no args, empty qdict */
3688 goto out;
3689 }
3690
3691 key = qstring_new();
3692 type = qstring_new();
3693
3694 cur_qs = key;
3695
3696 for (i = 0;; i++) {
3697 switch (args_type[i]) {
3698 case ',':
3699 case '\0':
3700 qdict_put(qdict, qstring_get_str(key), type);
3701 QDECREF(key);
3702 if (args_type[i] == '\0') {
3703 goto out;
3704 }
3705 type = qstring_new(); /* qdict has ref */
3706 cur_qs = key = qstring_new();
3707 break;
3708 case ':':
3709 cur_qs = type;
3710 break;
3711 default:
3712 qstring_append_chr(cur_qs, args_type[i]);
3713 break;
3714 }
3715 }
3716
3717out:
3718 return qdict;
3719}
3720
3721/*
3722 * Client argument checking rules:
3723 *
3724 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03003725 * 2. Each argument provided by the client must be expected
3726 * 3. Each argument provided by the client must have the type expected
3727 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003728 */
Markus Armbruster326283a2015-03-02 18:39:09 +01003729static void qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args,
3730 Error **errp)
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003731{
Markus Armbruster326283a2015-03-02 18:39:09 +01003732 Error *err = NULL;
3733 int flags;
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003734 QDict *cmd_args;
3735
3736 cmd_args = qdict_from_args_type(cmd->args_type);
3737
3738 flags = 0;
Markus Armbruster326283a2015-03-02 18:39:09 +01003739 check_mandatory_args(cmd_args, client_args, &flags, &err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003740 if (err) {
3741 goto out;
3742 }
3743
Markus Armbruster326283a2015-03-02 18:39:09 +01003744 check_client_args_type(client_args, cmd_args, flags, &err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003745
3746out:
Markus Armbruster326283a2015-03-02 18:39:09 +01003747 error_propagate(errp, err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003748 QDECREF(cmd_args);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003749}
3750
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003751/*
3752 * Input object checking rules
3753 *
3754 * 1. Input object must be a dict
3755 * 2. The "execute" key must exist
3756 * 3. The "execute" key must be a string
3757 * 4. If the "arguments" key exists, it must be a dict
3758 * 5. If the "id" key exists, it can be anything (ie. json-value)
3759 * 6. Any argument not listed above is considered invalid
3760 */
Markus Armbrusterba0510a2015-03-02 18:41:43 +01003761static QDict *qmp_check_input_obj(QObject *input_obj, Error **errp)
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003762{
3763 const QDictEntry *ent;
3764 int has_exec_key = 0;
3765 QDict *input_dict;
3766
3767 if (qobject_type(input_obj) != QTYPE_QDICT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003768 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "object");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003769 return NULL;
3770 }
3771
3772 input_dict = qobject_to_qdict(input_obj);
3773
3774 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
3775 const char *arg_name = qdict_entry_key(ent);
3776 const QObject *arg_obj = qdict_entry_value(ent);
3777
3778 if (!strcmp(arg_name, "execute")) {
3779 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003780 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
3781 "execute", "string");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003782 return NULL;
3783 }
3784 has_exec_key = 1;
3785 } else if (!strcmp(arg_name, "arguments")) {
3786 if (qobject_type(arg_obj) != QTYPE_QDICT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003787 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
3788 "arguments", "object");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003789 return NULL;
3790 }
Markus Armbruster779cec42015-06-08 10:44:30 +02003791 } else if (!strcmp(arg_name, "id")) {
3792 /* Any string is acceptable as "id", so nothing to check */
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003793 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003794 error_setg(errp, QERR_QMP_EXTRA_MEMBER, arg_name);
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003795 return NULL;
3796 }
3797 }
3798
3799 if (!has_exec_key) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003800 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "execute");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003801 return NULL;
3802 }
3803
3804 return input_dict;
3805}
3806
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003807static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
3808{
Markus Armbruster326283a2015-03-02 18:39:09 +01003809 Error *local_err = NULL;
Markus Armbruster84add862015-03-05 16:45:15 +01003810 QObject *obj, *data;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003811 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003812 const mon_cmd_t *cmd;
Luiz Capitulino40e5a012011-10-21 16:15:31 -02003813 const char *cmd_name;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003814 Monitor *mon = cur_mon;
3815
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03003816 args = input = NULL;
Markus Armbruster84add862015-03-05 16:45:15 +01003817 data = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003818
3819 obj = json_parser_parse(tokens, NULL);
3820 if (!obj) {
3821 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003822 error_setg(&local_err, QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003823 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003824 }
3825
Markus Armbrusterba0510a2015-03-02 18:41:43 +01003826 input = qmp_check_input_obj(obj, &local_err);
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003827 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003828 qobject_decref(obj);
3829 goto err_out;
3830 }
3831
Markus Armbruster74358f22015-03-06 19:35:59 +01003832 mon->qmp.id = qdict_get(input, "id");
3833 qobject_incref(mon->qmp.id);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003834
Luiz Capitulino0bbab462010-05-31 17:32:50 -03003835 cmd_name = qdict_get_str(input, "execute");
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +01003836 trace_handle_qmp_command(mon, cmd_name);
Anthony Liguorie3193602011-09-02 12:34:47 -05003837 cmd = qmp_find_cmd(cmd_name);
Eric Blake2d5a8342015-04-15 09:19:23 -06003838 if (!cmd) {
Markus Armbruster710aec92015-03-06 11:28:00 +01003839 error_set(&local_err, ERROR_CLASS_COMMAND_NOT_FOUND,
3840 "The command %s has not been found", cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03003841 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003842 }
Markus Armbruster40861822015-05-29 10:27:16 +02003843 if (invalid_qmp_mode(mon, cmd, &local_err)) {
Eric Blake2d5a8342015-04-15 09:19:23 -06003844 goto err_out;
3845 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003846
3847 obj = qdict_get(input, "arguments");
3848 if (!obj) {
3849 args = qdict_new();
3850 } else {
3851 args = qobject_to_qdict(obj);
3852 QINCREF(args);
3853 }
3854
Markus Armbruster326283a2015-03-02 18:39:09 +01003855 qmp_check_client_args(cmd, args, &local_err);
3856 if (local_err) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003857 goto err_out;
3858 }
3859
Markus Armbruster485febc2015-03-13 17:25:50 +01003860 cmd->mhandler.cmd_new(args, &data, &local_err);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003861
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003862err_out:
Markus Armbruster710aec92015-03-06 11:28:00 +01003863 monitor_protocol_emitter(mon, data, local_err);
Markus Armbruster84add862015-03-05 16:45:15 +01003864 qobject_decref(data);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03003865 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003866 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003867}
3868
Markus Armbrusterc83fe232015-03-06 19:20:51 +01003869static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003870{
3871 Monitor *old_mon = cur_mon;
3872
3873 cur_mon = opaque;
3874
Markus Armbruster74358f22015-03-06 19:35:59 +01003875 json_message_parser_feed(&cur_mon->qmp.parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003876
3877 cur_mon = old_mon;
3878}
3879
aliguori731b0362009-03-05 23:01:42 +00003880static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00003881{
aliguori731b0362009-03-05 23:01:42 +00003882 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00003883 int i;
aliguori376253e2009-03-05 23:01:23 +00003884
aliguori731b0362009-03-05 23:01:42 +00003885 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00003886
aliguoricde76ee2009-03-05 23:01:51 +00003887 if (cur_mon->rs) {
3888 for (i = 0; i < size; i++)
3889 readline_handle_byte(cur_mon->rs, buf[i]);
3890 } else {
3891 if (size == 0 || buf[size - 1] != 0)
3892 monitor_printf(cur_mon, "corrupted command\n");
3893 else
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003894 handle_hmp_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00003895 }
aliguori731b0362009-03-05 23:01:42 +00003896
3897 cur_mon = old_mon;
3898}
aliguorid8f44602008-10-06 13:52:44 +00003899
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003900static void monitor_command_cb(void *opaque, const char *cmdline,
3901 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00003902{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003903 Monitor *mon = opaque;
3904
aliguori731b0362009-03-05 23:01:42 +00003905 monitor_suspend(mon);
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003906 handle_hmp_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00003907 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00003908}
3909
aliguoricde76ee2009-03-05 23:01:51 +00003910int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003911{
aliguoricde76ee2009-03-05 23:01:51 +00003912 if (!mon->rs)
3913 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00003914 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00003915 return 0;
aliguorid8f44602008-10-06 13:52:44 +00003916}
3917
aliguori376253e2009-03-05 23:01:23 +00003918void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003919{
aliguoricde76ee2009-03-05 23:01:51 +00003920 if (!mon->rs)
3921 return;
aliguori731b0362009-03-05 23:01:42 +00003922 if (--mon->suspend_cnt == 0)
3923 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00003924}
3925
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003926static QObject *get_qmp_greeting(void)
3927{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03003928 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003929
Markus Armbruster7fad30f2015-09-16 13:06:19 +02003930 qmp_marshal_query_version(NULL, &ver, NULL);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003931 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
3932}
3933
Markus Armbrusterc83fe232015-03-06 19:20:51 +01003934static void monitor_qmp_event(void *opaque, int event)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003935{
Luiz Capitulino47116d12010-02-08 17:01:30 -02003936 QObject *data;
3937 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003938
Luiz Capitulino47116d12010-02-08 17:01:30 -02003939 switch (event) {
3940 case CHR_EVENT_OPENED:
Markus Armbrusterf994b252015-03-06 19:51:51 +01003941 mon->qmp.in_command_mode = false;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003942 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003943 monitor_json_emitter(mon, data);
3944 qobject_decref(data);
Corey Bryantefb87c12012-08-14 16:43:48 -04003945 mon_refcount++;
Luiz Capitulino47116d12010-02-08 17:01:30 -02003946 break;
3947 case CHR_EVENT_CLOSED:
Markus Armbruster74358f22015-03-06 19:35:59 +01003948 json_message_parser_destroy(&mon->qmp.parser);
3949 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Corey Bryantefb87c12012-08-14 16:43:48 -04003950 mon_refcount--;
3951 monitor_fdsets_cleanup();
Luiz Capitulino47116d12010-02-08 17:01:30 -02003952 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003953 }
3954}
3955
aliguori731b0362009-03-05 23:01:42 +00003956static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00003957{
aliguori376253e2009-03-05 23:01:23 +00003958 Monitor *mon = opaque;
3959
aliguori2724b182009-03-05 23:01:47 +00003960 switch (event) {
3961 case CHR_EVENT_MUX_IN:
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003962 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003963 mon->mux_out = 0;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003964 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003965 if (mon->reset_seen) {
3966 readline_restart(mon->rs);
3967 monitor_resume(mon);
3968 monitor_flush(mon);
3969 } else {
3970 mon->suspend_cnt = 0;
3971 }
aliguori2724b182009-03-05 23:01:47 +00003972 break;
ths86e94de2007-01-05 22:01:59 +00003973
aliguori2724b182009-03-05 23:01:47 +00003974 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003975 if (mon->reset_seen) {
3976 if (mon->suspend_cnt == 0) {
3977 monitor_printf(mon, "\n");
3978 }
3979 monitor_flush(mon);
3980 monitor_suspend(mon);
3981 } else {
3982 mon->suspend_cnt++;
3983 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003984 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003985 mon->mux_out = 1;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003986 qemu_mutex_unlock(&mon->out_lock);
aliguori2724b182009-03-05 23:01:47 +00003987 break;
3988
Amit Shahb6b8df52009-10-07 18:31:16 +05303989 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00003990 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3991 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003992 if (!mon->mux_out) {
Stratos Psomadakise5554e22014-09-15 15:34:57 +03003993 readline_restart(mon->rs);
aliguori2724b182009-03-05 23:01:47 +00003994 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003995 }
3996 mon->reset_seen = 1;
Corey Bryantefb87c12012-08-14 16:43:48 -04003997 mon_refcount++;
3998 break;
3999
4000 case CHR_EVENT_CLOSED:
4001 mon_refcount--;
4002 monitor_fdsets_cleanup();
aliguori2724b182009-03-05 23:01:47 +00004003 break;
4004 }
ths86e94de2007-01-05 22:01:59 +00004005}
4006
Wayne Xia816f8922011-10-12 11:32:41 +08004007static int
4008compare_mon_cmd(const void *a, const void *b)
4009{
4010 return strcmp(((const mon_cmd_t *)a)->name,
4011 ((const mon_cmd_t *)b)->name);
4012}
4013
4014static void sortcmdlist(void)
4015{
4016 int array_num;
4017 int elem_size = sizeof(mon_cmd_t);
4018
4019 array_num = sizeof(mon_cmds)/elem_size-1;
4020 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
4021
4022 array_num = sizeof(info_cmds)/elem_size-1;
4023 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
4024}
4025
aliguori76655d62009-03-06 20:27:37 +00004026
4027/*
4028 * Local variables:
4029 * c-indent-level: 4
4030 * c-basic-offset: 4
4031 * tab-width: 8
4032 * End:
4033 */
4034
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004035/* These functions just adapt the readline interface in a typesafe way. We
4036 * could cast function pointers but that discards compiler checks.
4037 */
Stefan Weild5d15072014-01-25 18:18:23 +01004038static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
4039 const char *fmt, ...)
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004040{
4041 va_list ap;
4042 va_start(ap, fmt);
4043 monitor_vprintf(opaque, fmt, ap);
4044 va_end(ap);
4045}
4046
4047static void monitor_readline_flush(void *opaque)
4048{
4049 monitor_flush(opaque);
4050}
4051
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004052static void __attribute__((constructor)) monitor_lock_init(void)
4053{
4054 qemu_mutex_init(&monitor_lock);
4055}
4056
aliguori731b0362009-03-05 23:01:42 +00004057void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004058{
aliguori731b0362009-03-05 23:01:42 +00004059 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004060 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004061
4062 if (is_first_init) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +02004063 monitor_qapi_event_init();
Wenchao Xiad0383172013-08-27 20:38:18 +08004064 sortcmdlist();
ths20d8a3e2007-02-18 17:04:49 +00004065 is_first_init = 0;
4066 }
aliguori87127162009-03-05 23:01:29 +00004067
Wenchao Xiab01fe892013-08-27 20:38:19 +08004068 mon = g_malloc(sizeof(*mon));
4069 monitor_data_init(mon);
ths20d8a3e2007-02-18 17:04:49 +00004070
aliguori87127162009-03-05 23:01:29 +00004071 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00004072 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004073 if (flags & MONITOR_USE_READLINE) {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004074 mon->rs = readline_init(monitor_readline_printf,
4075 monitor_readline_flush,
4076 mon,
4077 monitor_find_completion);
aliguoricde76ee2009-03-05 23:01:51 +00004078 monitor_read_command(mon, 0);
4079 }
aliguori87127162009-03-05 23:01:29 +00004080
Markus Armbruster9f3982f2015-03-06 19:56:38 +01004081 if (monitor_is_qmp(mon)) {
Markus Armbrusterc83fe232015-03-06 19:20:51 +01004082 qemu_chr_add_handlers(chr, monitor_can_read, monitor_qmp_read,
4083 monitor_qmp_event, mon);
Anthony Liguori15f31512011-08-15 11:17:35 -05004084 qemu_chr_fe_set_echo(chr, true);
Markus Armbruster74358f22015-03-06 19:35:59 +01004085 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004086 } else {
4087 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4088 monitor_event, mon);
4089 }
aliguori87127162009-03-05 23:01:29 +00004090
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004091 qemu_mutex_lock(&monitor_lock);
Blue Swirl72cf2d42009-09-12 07:36:22 +00004092 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004093 qemu_mutex_unlock(&monitor_lock);
bellard7e2515e2004-08-01 21:52:19 +00004094}
4095
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004096static void bdrv_password_cb(void *opaque, const char *password,
4097 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00004098{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004099 Monitor *mon = opaque;
4100 BlockDriverState *bs = readline_opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004101 int ret = 0;
Markus Armbruster4d2855a2015-01-29 10:37:00 +01004102 Error *local_err = NULL;
bellard7e2515e2004-08-01 21:52:19 +00004103
Markus Armbruster4d2855a2015-01-29 10:37:00 +01004104 bdrv_add_key(bs, password, &local_err);
4105 if (local_err) {
4106 monitor_printf(mon, "%s\n", error_get_pretty(local_err));
4107 error_free(local_err);
aliguoribb5fc202009-03-05 23:01:15 +00004108 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004109 }
aliguori731b0362009-03-05 23:01:42 +00004110 if (mon->password_completion_cb)
4111 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004112
aliguori731b0362009-03-05 23:01:42 +00004113 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004114}
aliguoric0f4ce72009-03-05 23:01:01 +00004115
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004116int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
Markus Armbruster097310b2014-10-07 13:59:15 +02004117 BlockCompletionFunc *completion_cb,
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004118 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004119{
aliguoricde76ee2009-03-05 23:01:51 +00004120 int err;
4121
aliguori376253e2009-03-05 23:01:23 +00004122 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4123 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00004124
aliguori731b0362009-03-05 23:01:42 +00004125 mon->password_completion_cb = completion_cb;
4126 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004127
aliguoricde76ee2009-03-05 23:01:51 +00004128 err = monitor_read_password(mon, bdrv_password_cb, bs);
4129
4130 if (err && completion_cb)
4131 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004132
4133 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00004134}
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004135
4136int monitor_read_block_device_key(Monitor *mon, const char *device,
Markus Armbruster097310b2014-10-07 13:59:15 +02004137 BlockCompletionFunc *completion_cb,
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004138 void *opaque)
4139{
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01004140 Error *err = NULL;
Fam Zheng55606252015-03-02 19:36:46 +08004141 BlockBackend *blk;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004142
Fam Zheng55606252015-03-02 19:36:46 +08004143 blk = blk_by_name(device);
4144 if (!blk) {
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004145 monitor_printf(mon, "Device not found %s\n", device);
4146 return -1;
4147 }
4148
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01004149 bdrv_add_key(blk_bs(blk), NULL, &err);
4150 if (err) {
4151 error_free(err);
4152 return monitor_read_bdrv_key_start(mon, blk_bs(blk), completion_cb, opaque);
4153 }
4154
4155 if (completion_cb) {
4156 completion_cb(opaque, 0);
4157 }
4158 return 0;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004159}
Paolo Bonzini4d454572012-11-26 16:03:42 +01004160
4161QemuOptsList qemu_mon_opts = {
4162 .name = "mon",
4163 .implied_opt_name = "chardev",
4164 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
4165 .desc = {
4166 {
4167 .name = "mode",
4168 .type = QEMU_OPT_STRING,
4169 },{
4170 .name = "chardev",
4171 .type = QEMU_OPT_STRING,
4172 },{
4173 .name = "default",
4174 .type = QEMU_OPT_BOOL,
4175 },{
4176 .name = "pretty",
4177 .type = QEMU_OPT_BOOL,
4178 },
4179 { /* end of list */ }
4180 },
4181};
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004182
4183#ifndef TARGET_I386
4184void qmp_rtc_reset_reinjection(Error **errp)
4185{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004186 error_setg(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection");
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004187}
4188#endif
Jason J. Herne7ee0c3e2015-06-26 14:03:16 -04004189
4190#ifndef TARGET_S390X
4191void qmp_dump_skeys(const char *filename, Error **errp)
4192{
4193 error_setg(errp, QERR_FEATURE_DISABLED, "dump-skeys");
4194}
4195#endif