blob: b4578cb8eaa31043b9c08ca693e9a1ec4e2d2c3b [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"
Paolo Bonzinif08b6172014-03-28 19:42:10 +010071#include "exec/cpu_ldst.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050072#include "qmp-commands.h"
73#include "hmp.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010074#include "qemu/thread.h"
Hani Benhabilesb21631f2014-05-27 23:39:37 +010075#include "block/qapi.h"
Wenchao Xia43a14cf2014-06-18 08:43:31 +020076#include "qapi/qmp-event.h"
77#include "qapi-event.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
Stefan Weil8b7968f2010-09-23 21:28:05 +0200375static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream,
376 const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000377{
378 va_list ap;
379 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000380 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000381 va_end(ap);
382 return 0;
383}
384
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200385static void monitor_json_emitter(Monitor *mon, const QObject *data)
386{
387 QString *json;
388
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200389 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
390 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200391 assert(json != NULL);
392
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200393 qstring_append_chr(json, '\n');
394 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200395
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200396 QDECREF(json);
397}
398
Markus Armbruster710aec92015-03-06 11:28:00 +0100399static QDict *build_qmp_error_dict(Error *err)
Luiz Capitulinode253f12012-07-27 16:18:16 -0300400{
401 QObject *obj;
402
Markus Armbruster710aec92015-03-06 11:28:00 +0100403 obj = qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %s } }",
404 ErrorClass_lookup[error_get_class(err)],
405 error_get_pretty(err));
Luiz Capitulinode253f12012-07-27 16:18:16 -0300406
407 return qobject_to_qdict(obj);
408}
409
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100410static void monitor_protocol_emitter(Monitor *mon, QObject *data,
Markus Armbruster710aec92015-03-06 11:28:00 +0100411 Error *err)
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200412{
413 QDict *qmp;
414
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +0100415 trace_monitor_protocol_emitter(mon);
416
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100417 if (!err) {
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200418 /* success response */
Luiz Capitulinode253f12012-07-27 16:18:16 -0300419 qmp = qdict_new();
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200420 if (data) {
421 qobject_incref(data);
422 qdict_put_obj(qmp, "return", data);
423 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200424 /* return an empty QDict by default */
425 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200426 }
427 } else {
428 /* error response */
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100429 qmp = build_qmp_error_dict(err);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200430 }
431
Markus Armbruster74358f22015-03-06 19:35:59 +0100432 if (mon->qmp.id) {
433 qdict_put_obj(qmp, "id", mon->qmp.id);
434 mon->qmp.id = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200435 }
436
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200437 monitor_json_emitter(mon, QOBJECT(qmp));
438 QDECREF(qmp);
439}
440
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200441
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200442static MonitorQAPIEventState monitor_qapi_event_state[QAPI_EVENT_MAX];
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100443
444/*
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200445 * Emits the event to every monitor instance, @event is only used for trace
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200446 * Called with monitor_lock held.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100447 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200448static void monitor_qapi_event_emit(QAPIEvent event, QObject *data)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100449{
450 Monitor *mon;
451
452 trace_monitor_protocol_event_emit(event, data);
453 QLIST_FOREACH(mon, &mon_list, entry) {
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100454 if (monitor_is_qmp(mon) && mon->qmp.in_command_mode) {
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100455 monitor_json_emitter(mon, data);
456 }
457 }
458}
459
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100460/*
461 * Queue a new event for emission to Monitor instances,
462 * applying any rate limiting if required.
463 */
464static void
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200465monitor_qapi_event_queue(QAPIEvent event, QDict *data, Error **errp)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100466{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200467 MonitorQAPIEventState *evstate;
468 assert(event < QAPI_EVENT_MAX);
Alex Blighbc72ad62013-08-21 16:03:08 +0100469 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100470
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200471 evstate = &(monitor_qapi_event_state[event]);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100472 trace_monitor_protocol_event_queue(event,
473 data,
474 evstate->rate,
475 evstate->last,
476 now);
477
478 /* Rate limit of 0 indicates no throttling */
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200479 qemu_mutex_lock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100480 if (!evstate->rate) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200481 monitor_qapi_event_emit(event, QOBJECT(data));
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100482 evstate->last = now;
483 } else {
484 int64_t delta = now - evstate->last;
485 if (evstate->data ||
486 delta < evstate->rate) {
487 /* If there's an existing event pending, replace
488 * it with the new event, otherwise schedule a
489 * timer for delayed emission
490 */
491 if (evstate->data) {
492 qobject_decref(evstate->data);
493 } else {
494 int64_t then = evstate->last + evstate->rate;
Alex Blighbc72ad62013-08-21 16:03:08 +0100495 timer_mod_ns(evstate->timer, then);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100496 }
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200497 evstate->data = QOBJECT(data);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100498 qobject_incref(evstate->data);
499 } else {
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200500 monitor_qapi_event_emit(event, QOBJECT(data));
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100501 evstate->last = now;
502 }
503 }
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200504 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100505}
506
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100507/*
508 * The callback invoked by QemuTimer when a delayed
509 * event is ready to be emitted
510 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200511static void monitor_qapi_event_handler(void *opaque)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100512{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200513 MonitorQAPIEventState *evstate = opaque;
Alex Blighbc72ad62013-08-21 16:03:08 +0100514 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100515
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100516 trace_monitor_protocol_event_handler(evstate->event,
517 evstate->data,
518 evstate->last,
519 now);
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200520 qemu_mutex_lock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100521 if (evstate->data) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200522 monitor_qapi_event_emit(evstate->event, evstate->data);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100523 qobject_decref(evstate->data);
524 evstate->data = NULL;
525 }
526 evstate->last = now;
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200527 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100528}
529
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100530/*
531 * @event: the event ID to be limited
532 * @rate: the rate limit in milliseconds
533 *
534 * Sets a rate limit on a particular event, so no
535 * more than 1 event will be emitted within @rate
536 * milliseconds
537 */
538static void
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200539monitor_qapi_event_throttle(QAPIEvent event, int64_t rate)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100540{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200541 MonitorQAPIEventState *evstate;
542 assert(event < QAPI_EVENT_MAX);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100543
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200544 evstate = &(monitor_qapi_event_state[event]);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100545
546 trace_monitor_protocol_event_throttle(event, rate);
547 evstate->event = event;
Wenchao Xia2f44a082014-06-24 16:34:00 -0700548 assert(rate * SCALE_MS <= INT64_MAX);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100549 evstate->rate = rate * SCALE_MS;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100550 evstate->last = 0;
551 evstate->data = NULL;
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200552 evstate->timer = timer_new(QEMU_CLOCK_REALTIME,
553 SCALE_MS,
554 monitor_qapi_event_handler,
555 evstate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100556}
557
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200558static void monitor_qapi_event_init(void)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100559{
Wenchao Xiae010ad82014-06-18 08:43:41 +0200560 /* Limit guest-triggerable events to 1 per second */
561 monitor_qapi_event_throttle(QAPI_EVENT_RTC_CHANGE, 1000);
Wenchao Xia99eaf092014-06-18 08:43:42 +0200562 monitor_qapi_event_throttle(QAPI_EVENT_WATCHDOG, 1000);
Wenchao Xiaaef9d312014-06-18 08:43:51 +0200563 monitor_qapi_event_throttle(QAPI_EVENT_BALLOON_CHANGE, 1000);
Wenchao Xiafe069d92014-06-18 08:43:53 +0200564 monitor_qapi_event_throttle(QAPI_EVENT_QUORUM_REPORT_BAD, 1000);
565 monitor_qapi_event_throttle(QAPI_EVENT_QUORUM_FAILURE, 1000);
Laszlo Erseke2ae6152014-06-26 17:50:02 +0200566 monitor_qapi_event_throttle(QAPI_EVENT_VSERPORT_CHANGE, 1000);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100567
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200568 qmp_event_set_func_emit(monitor_qapi_event_queue);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200569}
570
Markus Armbruster485febc2015-03-13 17:25:50 +0100571static void qmp_capabilities(QDict *params, QObject **ret_data, Error **errp)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200572{
Markus Armbruster485febc2015-03-13 17:25:50 +0100573 cur_mon->qmp.in_command_mode = true;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200574}
575
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100576static void handle_hmp_command(Monitor *mon, const char *cmdline);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200577
Wenchao Xiab01fe892013-08-27 20:38:19 +0800578static void monitor_data_init(Monitor *mon)
579{
580 memset(mon, 0, sizeof(Monitor));
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200581 qemu_mutex_init(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800582 mon->outbuf = qstring_new();
Wenchao Xia77172392013-08-27 20:38:20 +0800583 /* Use *mon_cmds by default. */
584 mon->cmd_table = mon_cmds;
Wenchao Xiab01fe892013-08-27 20:38:19 +0800585}
586
587static void monitor_data_destroy(Monitor *mon)
588{
589 QDECREF(mon->outbuf);
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200590 qemu_mutex_destroy(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800591}
592
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200593char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
594 int64_t cpu_index, Error **errp)
Luiz Capitulino0268d972010-10-22 10:08:02 -0200595{
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200596 char *output = NULL;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200597 Monitor *old_mon, hmp;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200598
Wenchao Xiab01fe892013-08-27 20:38:19 +0800599 monitor_data_init(&hmp);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400600 hmp.skip_flush = true;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200601
602 old_mon = cur_mon;
603 cur_mon = &hmp;
604
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200605 if (has_cpu_index) {
606 int ret = monitor_set_cpu(cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200607 if (ret < 0) {
608 cur_mon = old_mon;
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100609 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
610 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200611 goto out;
612 }
613 }
614
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100615 handle_hmp_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200616 cur_mon = old_mon;
617
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200618 qemu_mutex_lock(&hmp.out_lock);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400619 if (qstring_get_length(hmp.outbuf) > 0) {
620 output = g_strdup(qstring_get_str(hmp.outbuf));
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200621 } else {
622 output = g_strdup("");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200623 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200624 qemu_mutex_unlock(&hmp.out_lock);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200625
626out:
Wenchao Xiab01fe892013-08-27 20:38:19 +0800627 monitor_data_destroy(&hmp);
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200628 return output;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200629}
630
bellard9dc39cb2004-03-14 21:38:27 +0000631static int compare_cmd(const char *name, const char *list)
632{
633 const char *p, *pstart;
634 int len;
635 len = strlen(name);
636 p = list;
637 for(;;) {
638 pstart = p;
639 p = strchr(p, '|');
640 if (!p)
641 p = pstart + strlen(pstart);
642 if ((p - pstart) == len && !memcmp(pstart, name, len))
643 return 1;
644 if (*p == '\0')
645 break;
646 p++;
647 }
648 return 0;
649}
650
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800651static int get_str(char *buf, int buf_size, const char **pp)
652{
653 const char *p;
654 char *q;
655 int c;
656
657 q = buf;
658 p = *pp;
659 while (qemu_isspace(*p)) {
660 p++;
661 }
662 if (*p == '\0') {
663 fail:
664 *q = '\0';
665 *pp = p;
666 return -1;
667 }
668 if (*p == '\"') {
669 p++;
670 while (*p != '\0' && *p != '\"') {
671 if (*p == '\\') {
672 p++;
673 c = *p++;
674 switch (c) {
675 case 'n':
676 c = '\n';
677 break;
678 case 'r':
679 c = '\r';
680 break;
681 case '\\':
682 case '\'':
683 case '\"':
684 break;
685 default:
Peter Maydell71baf782015-08-19 16:20:19 +0100686 printf("unsupported escape code: '\\%c'\n", c);
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800687 goto fail;
688 }
689 if ((q - buf) < buf_size - 1) {
690 *q++ = c;
691 }
692 } else {
693 if ((q - buf) < buf_size - 1) {
694 *q++ = *p;
695 }
696 p++;
697 }
698 }
699 if (*p != '\"') {
Peter Maydell71baf782015-08-19 16:20:19 +0100700 printf("unterminated string\n");
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800701 goto fail;
702 }
703 p++;
704 } else {
705 while (*p != '\0' && !qemu_isspace(*p)) {
706 if ((q - buf) < buf_size - 1) {
707 *q++ = *p;
708 }
709 p++;
710 }
711 }
712 *q = '\0';
713 *pp = p;
714 return 0;
715}
716
717#define MAX_ARGS 16
718
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800719static void free_cmdline_args(char **args, int nb_args)
720{
721 int i;
722
723 assert(nb_args <= MAX_ARGS);
724
725 for (i = 0; i < nb_args; i++) {
726 g_free(args[i]);
727 }
728
729}
730
731/*
732 * Parse the command line to get valid args.
733 * @cmdline: command line to be parsed.
734 * @pnb_args: location to store the number of args, must NOT be NULL.
735 * @args: location to store the args, which should be freed by caller, must
736 * NOT be NULL.
737 *
738 * Returns 0 on success, negative on failure.
739 *
740 * NOTE: this parser is an approximate form of the real command parser. Number
741 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
742 * return with failure.
743 */
744static int parse_cmdline(const char *cmdline,
745 int *pnb_args, char **args)
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800746{
747 const char *p;
748 int nb_args, ret;
749 char buf[1024];
750
751 p = cmdline;
752 nb_args = 0;
753 for (;;) {
754 while (qemu_isspace(*p)) {
755 p++;
756 }
757 if (*p == '\0') {
758 break;
759 }
760 if (nb_args >= MAX_ARGS) {
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800761 goto fail;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800762 }
763 ret = get_str(buf, sizeof(buf), &p);
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800764 if (ret < 0) {
765 goto fail;
766 }
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800767 args[nb_args] = g_strdup(buf);
768 nb_args++;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800769 }
770 *pnb_args = nb_args;
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800771 return 0;
772
773 fail:
774 free_cmdline_args(args, nb_args);
775 return -1;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800776}
777
Wenchao Xia66855492013-08-27 20:38:23 +0800778static void help_cmd_dump_one(Monitor *mon,
779 const mon_cmd_t *cmd,
780 char **prefix_args,
781 int prefix_args_nb)
782{
783 int i;
784
785 for (i = 0; i < prefix_args_nb; i++) {
786 monitor_printf(mon, "%s ", prefix_args[i]);
787 }
788 monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
789}
790
791/* @args[@arg_index] is the valid command need to find in @cmds */
Anthony Liguoric227f092009-10-01 16:12:16 -0500792static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
Wenchao Xia66855492013-08-27 20:38:23 +0800793 char **args, int nb_args, int arg_index)
bellard9dc39cb2004-03-14 21:38:27 +0000794{
Anthony Liguoric227f092009-10-01 16:12:16 -0500795 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000796
Wenchao Xia66855492013-08-27 20:38:23 +0800797 /* No valid arg need to compare with, dump all in *cmds */
798 if (arg_index >= nb_args) {
799 for (cmd = cmds; cmd->name != NULL; cmd++) {
800 help_cmd_dump_one(mon, cmd, args, arg_index);
801 }
802 return;
803 }
804
805 /* Find one entry to dump */
806 for (cmd = cmds; cmd->name != NULL; cmd++) {
807 if (compare_cmd(args[arg_index], cmd->name)) {
808 if (cmd->sub_table) {
809 /* continue with next arg */
810 help_cmd_dump(mon, cmd->sub_table,
811 args, nb_args, arg_index + 1);
812 } else {
813 help_cmd_dump_one(mon, cmd, args, arg_index);
814 }
815 break;
816 }
bellard9dc39cb2004-03-14 21:38:27 +0000817 }
818}
819
aliguori376253e2009-03-05 23:01:23 +0000820static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000821{
Wenchao Xia66855492013-08-27 20:38:23 +0800822 char *args[MAX_ARGS];
823 int nb_args = 0;
824
825 /* 1. parse user input */
826 if (name) {
827 /* special case for log, directly dump and return */
828 if (!strcmp(name, "log")) {
Peter Maydell38dad9e2013-02-11 16:41:25 +0000829 const QEMULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000830 monitor_printf(mon, "Log items (comma separated):\n");
831 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
Peter Maydell38dad9e2013-02-11 16:41:25 +0000832 for (item = qemu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000833 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000834 }
Wenchao Xia66855492013-08-27 20:38:23 +0800835 return;
836 }
837
838 if (parse_cmdline(name, &nb_args, args) < 0) {
839 return;
bellardf193c792004-03-21 17:06:25 +0000840 }
bellard9dc39cb2004-03-14 21:38:27 +0000841 }
Wenchao Xia66855492013-08-27 20:38:23 +0800842
843 /* 2. dump the contents according to parsed args */
844 help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0);
845
846 free_cmdline_args(args, nb_args);
bellard9dc39cb2004-03-14 21:38:27 +0000847}
848
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300849static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300850{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300851 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300852}
853
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100854static void hmp_trace_event(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530855{
856 const char *tp_name = qdict_get_str(qdict, "name");
857 bool new_state = qdict_get_bool(qdict, "option");
Lluís Vilanova14101d02014-08-25 13:20:03 +0200858 Error *local_err = NULL;
Blue Swirlf871d682010-10-13 19:14:29 +0000859
Lluís Vilanova14101d02014-08-25 13:20:03 +0200860 qmp_trace_event_set_state(tp_name, new_state, true, true, &local_err);
861 if (local_err) {
Markus Armbruster091e38b2015-02-10 15:15:43 +0100862 error_report_err(local_err);
Blue Swirlf871d682010-10-13 19:14:29 +0000863 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530864}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100865
Michael Rothc45a8162011-10-02 08:44:37 -0500866#ifdef CONFIG_TRACE_SIMPLE
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100867static void hmp_trace_file(Monitor *mon, const QDict *qdict)
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100868{
869 const char *op = qdict_get_try_str(qdict, "op");
870 const char *arg = qdict_get_try_str(qdict, "arg");
871
872 if (!op) {
873 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
874 } else if (!strcmp(op, "on")) {
875 st_set_trace_file_enabled(true);
876 } else if (!strcmp(op, "off")) {
877 st_set_trace_file_enabled(false);
878 } else if (!strcmp(op, "flush")) {
879 st_flush_trace_buffer();
880 } else if (!strcmp(op, "set")) {
881 if (arg) {
882 st_set_trace_file(arg);
883 }
884 } else {
885 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
886 help_cmd(mon, "trace-file");
887 }
888}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530889#endif
890
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100891static void hmp_info_help(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000892{
Luiz Capitulino13c74252009-10-07 13:41:55 -0300893 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000894}
895
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300896CommandInfoList *qmp_query_commands(Error **errp)
bellard9bc9d1c2004-10-10 15:15:51 +0000897{
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300898 CommandInfoList *info, *cmd_list = NULL;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200899 const mon_cmd_t *cmd;
900
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300901 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino40e5a012011-10-21 16:15:31 -0200902 info = g_malloc0(sizeof(*info));
903 info->value = g_malloc0(sizeof(*info->value));
904 info->value->name = g_strdup(cmd->name);
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200905
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300906 info->next = cmd_list;
907 cmd_list = info;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200908 }
909
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300910 return cmd_list;
thsa36e69d2007-12-02 05:18:19 +0000911}
912
Daniel P. Berrange48608532012-05-21 17:59:51 +0100913EventInfoList *qmp_query_events(Error **errp)
914{
915 EventInfoList *info, *ev_list = NULL;
Wenchao Xia75175172014-06-18 08:43:54 +0200916 QAPIEvent e;
Daniel P. Berrange48608532012-05-21 17:59:51 +0100917
Wenchao Xia75175172014-06-18 08:43:54 +0200918 for (e = 0 ; e < QAPI_EVENT_MAX ; e++) {
919 const char *event_name = QAPIEvent_lookup[e];
Daniel P. Berrange48608532012-05-21 17:59:51 +0100920 assert(event_name != NULL);
921 info = g_malloc0(sizeof(*info));
922 info->value = g_malloc0(sizeof(*info->value));
923 info->value->name = g_strdup(event_name);
924
925 info->next = ev_list;
926 ev_list = info;
927 }
928
929 return ev_list;
930}
931
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300932/* set the current CPU defined by the user */
933int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000934{
Andreas Färber55e5c282012-12-17 06:18:02 +0100935 CPUState *cpu;
bellard6a00d602005-11-21 23:25:50 +0000936
Andreas Färber1c8bb3c2013-02-15 17:01:09 +0100937 cpu = qemu_get_cpu(cpu_index);
938 if (cpu == NULL) {
939 return -1;
bellard6a00d602005-11-21 23:25:50 +0000940 }
Andreas Färbercb446ec2013-05-01 14:24:52 +0200941 cur_mon->mon_cpu = cpu;
Andreas Färber1c8bb3c2013-02-15 17:01:09 +0100942 return 0;
bellard6a00d602005-11-21 23:25:50 +0000943}
944
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700945static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000946{
aliguori731b0362009-03-05 23:01:42 +0000947 if (!cur_mon->mon_cpu) {
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300948 monitor_set_cpu(0);
bellard6a00d602005-11-21 23:25:50 +0000949 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300950 cpu_synchronize_state(cur_mon->mon_cpu);
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700951 return cur_mon->mon_cpu;
952}
953
Pavel Butsykinbf957282015-09-10 18:38:59 +0300954CPUArchState *mon_get_cpu_env(void)
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700955{
956 return mon_get_cpu()->env_ptr;
bellard6a00d602005-11-21 23:25:50 +0000957}
958
Luiz Capitulino99b77962011-10-24 10:53:44 -0200959int monitor_get_cpu_index(void)
960{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700961 return mon_get_cpu()->cpu_index;
Luiz Capitulino99b77962011-10-24 10:53:44 -0200962}
963
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100964static void hmp_info_registers(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000965{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700966 cpu_dump_state(mon_get_cpu(), (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000967}
968
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100969static void hmp_info_jit(Monitor *mon, const QDict *qdict)
bellarde3db7222005-01-26 22:00:47 +0000970{
aliguori376253e2009-03-05 23:01:23 +0000971 dump_exec_info((FILE *)mon, monitor_fprintf);
Sebastian Tanase27498be2014-07-25 11:56:33 +0200972 dump_drift_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000973}
974
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100975static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
Max Filippov246ae242014-11-02 11:04:18 +0300976{
977 dump_opcount_info((FILE *)mon, monitor_fprintf);
978}
979
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100980static void hmp_info_history(Monitor *mon, const QDict *qdict)
bellardaa455482004-04-04 13:07:25 +0000981{
982 int i;
bellard7e2515e2004-08-01 21:52:19 +0000983 const char *str;
ths3b46e622007-09-17 08:09:54 +0000984
aliguoricde76ee2009-03-05 23:01:51 +0000985 if (!mon->rs)
986 return;
bellard7e2515e2004-08-01 21:52:19 +0000987 i = 0;
988 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000989 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000990 if (!str)
991 break;
aliguori376253e2009-03-05 23:01:23 +0000992 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000993 i++;
bellardaa455482004-04-04 13:07:25 +0000994 }
995}
996
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100997static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
j_mayer76a66252007-03-07 08:32:30 +0000998{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700999 cpu_dump_statistics(mon_get_cpu(), (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +00001000}
j_mayer76a66252007-03-07 08:32:30 +00001001
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001002static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301003{
Lluís Vilanova14101d02014-08-25 13:20:03 +02001004 TraceEventInfoList *events = qmp_trace_event_get_state("*", NULL);
1005 TraceEventInfoList *elem;
1006
1007 for (elem = events; elem != NULL; elem = elem->next) {
1008 monitor_printf(mon, "%s : state %u\n",
1009 elem->value->name,
1010 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
1011 }
1012 qapi_free_TraceEventInfoList(events);
Prerna Saxena22890ab2010-06-24 17:04:53 +05301013}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301014
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001015void qmp_client_migrate_info(const char *protocol, const char *hostname,
1016 bool has_port, int64_t port,
1017 bool has_tls_port, int64_t tls_port,
1018 bool has_cert_subject, const char *cert_subject,
1019 Error **errp)
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001020{
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001021 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001022 if (!qemu_using_spice(errp)) {
1023 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001024 }
1025
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001026 if (!has_port && !has_tls_port) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001027 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001028 return;
Yonit Halperin6ec5dae2012-03-18 09:42:39 +02001029 }
1030
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001031 if (qemu_spice_migrate_info(hostname,
1032 has_port ? port : -1,
1033 has_tls_port ? tls_port : -1,
1034 cert_subject)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001035 error_setg(errp, QERR_UNDEFINED_ERROR);
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001036 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001037 }
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001038 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001039 }
1040
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001041 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001042}
1043
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001044static void hmp_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001045{
Peter Maydell9a7e5422013-02-11 16:41:20 +00001046 qemu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001047}
1048
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001049static void hmp_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001050{
1051 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001052 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001053
bellard9307c4c2004-04-04 12:57:25 +00001054 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001055 mask = 0;
1056 } else {
Peter Maydell4fde1eb2013-02-11 16:41:22 +00001057 mask = qemu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001058 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001059 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001060 return;
1061 }
1062 }
Peter Maydell24537a02013-02-11 16:41:23 +00001063 qemu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00001064}
1065
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001066static void hmp_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001067{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001068 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001069 if (!option || !strcmp(option, "on")) {
1070 singlestep = 1;
1071 } else if (!strcmp(option, "off")) {
1072 singlestep = 0;
1073 } else {
1074 monitor_printf(mon, "unexpected option %s\n", option);
1075 }
1076}
1077
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001078static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001079{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001080 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001081 if (!device)
1082 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1083 if (gdbserver_start(device) < 0) {
1084 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1085 device);
1086 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001087 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001088 } else {
aliguori59030a82009-04-05 18:43:41 +00001089 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1090 device);
bellard8a7ddc32004-03-31 19:00:16 +00001091 }
1092}
1093
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001094static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001095{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001096 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001097 if (select_watchdog_action(action) == -1) {
1098 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1099 }
1100}
1101
aliguori376253e2009-03-05 23:01:23 +00001102static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001103{
aliguori376253e2009-03-05 23:01:23 +00001104 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001105 switch(c) {
1106 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001107 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001108 break;
1109 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001110 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001111 break;
1112 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001113 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001114 break;
1115 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001116 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001117 break;
1118 default:
1119 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001120 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001121 } else {
aliguori376253e2009-03-05 23:01:23 +00001122 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001123 }
1124 break;
1125 }
aliguori376253e2009-03-05 23:01:23 +00001126 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001127}
1128
aliguori376253e2009-03-05 23:01:23 +00001129static void memory_dump(Monitor *mon, int count, int format, int wsize,
Avi Kivitya8170e52012-10-23 12:30:10 +02001130 hwaddr addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001131{
Blue Swirl23842aa2010-01-12 20:27:43 +00001132 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001133 uint8_t buf[16];
1134 uint64_t v;
1135
1136 if (format == 'i') {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001137 int flags = 0;
bellard9307c4c2004-04-04 12:57:25 +00001138#ifdef TARGET_I386
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001139 CPUArchState *env = mon_get_cpu_env();
bellard4c27ba22004-04-25 18:05:08 +00001140 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001141 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001142 } else if (wsize == 4) {
1143 flags = 0;
1144 } else {
bellard6a15fd12006-04-12 21:07:07 +00001145 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001146 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001147 if (env) {
1148#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001149 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001150 (env->segs[R_CS].flags & DESC_L_MASK))
1151 flags = 2;
1152 else
1153#endif
1154 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1155 flags = 1;
1156 }
bellard4c27ba22004-04-25 18:05:08 +00001157 }
1158#endif
Tom Musta1c38f842014-04-09 14:53:24 -05001159#ifdef TARGET_PPC
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001160 CPUArchState *env = mon_get_cpu_env();
Tom Musta1c38f842014-04-09 14:53:24 -05001161 flags = msr_le << 16;
1162 flags |= env->bfd_mach;
1163#endif
Peter Crosthwaited49190c2015-05-24 14:20:41 -07001164 monitor_disas(mon, mon_get_cpu(), addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001165 return;
1166 }
1167
1168 len = wsize * count;
1169 if (wsize == 1)
1170 line_size = 8;
1171 else
1172 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001173 max_digits = 0;
1174
1175 switch(format) {
1176 case 'o':
1177 max_digits = (wsize * 8 + 2) / 3;
1178 break;
1179 default:
1180 case 'x':
1181 max_digits = (wsize * 8) / 4;
1182 break;
1183 case 'u':
1184 case 'd':
1185 max_digits = (wsize * 8 * 10 + 32) / 33;
1186 break;
1187 case 'c':
1188 wsize = 1;
1189 break;
1190 }
1191
1192 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001193 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001194 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001195 else
aliguori376253e2009-03-05 23:01:23 +00001196 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001197 l = len;
1198 if (l > line_size)
1199 l = line_size;
1200 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001201 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001202 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001203 if (cpu_memory_rw_debug(mon_get_cpu(), addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001204 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001205 break;
1206 }
bellard9307c4c2004-04-04 12:57:25 +00001207 }
ths5fafdf22007-09-16 21:08:06 +00001208 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001209 while (i < l) {
1210 switch(wsize) {
1211 default:
1212 case 1:
Peter Maydell24e60302015-01-20 15:19:32 +00001213 v = ldub_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001214 break;
1215 case 2:
Peter Maydell24e60302015-01-20 15:19:32 +00001216 v = lduw_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001217 break;
1218 case 4:
Peter Maydell24e60302015-01-20 15:19:32 +00001219 v = (uint32_t)ldl_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001220 break;
1221 case 8:
Peter Maydell24e60302015-01-20 15:19:32 +00001222 v = ldq_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001223 break;
1224 }
aliguori376253e2009-03-05 23:01:23 +00001225 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001226 switch(format) {
1227 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001228 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001229 break;
1230 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001231 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001232 break;
1233 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001234 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001235 break;
1236 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001237 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001238 break;
1239 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001240 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001241 break;
1242 }
1243 i += wsize;
1244 }
aliguori376253e2009-03-05 23:01:23 +00001245 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001246 addr += l;
1247 len -= l;
1248 }
1249}
1250
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001251static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001252{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001253 int count = qdict_get_int(qdict, "count");
1254 int format = qdict_get_int(qdict, "format");
1255 int size = qdict_get_int(qdict, "size");
1256 target_long addr = qdict_get_int(qdict, "addr");
1257
aliguori376253e2009-03-05 23:01:23 +00001258 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001259}
1260
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001261static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001262{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001263 int count = qdict_get_int(qdict, "count");
1264 int format = qdict_get_int(qdict, "format");
1265 int size = qdict_get_int(qdict, "size");
Avi Kivitya8170e52012-10-23 12:30:10 +02001266 hwaddr addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001267
aliguori376253e2009-03-05 23:01:23 +00001268 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001269}
1270
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001271static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001272{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001273 int format = qdict_get_int(qdict, "format");
Avi Kivitya8170e52012-10-23 12:30:10 +02001274 hwaddr val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001275
bellard9307c4c2004-04-04 12:57:25 +00001276 switch(format) {
1277 case 'o':
Avi Kivitya8170e52012-10-23 12:30:10 +02001278 monitor_printf(mon, "%#" HWADDR_PRIo, val);
bellard9307c4c2004-04-04 12:57:25 +00001279 break;
1280 case 'x':
Avi Kivitya8170e52012-10-23 12:30:10 +02001281 monitor_printf(mon, "%#" HWADDR_PRIx, val);
bellard9307c4c2004-04-04 12:57:25 +00001282 break;
1283 case 'u':
Avi Kivitya8170e52012-10-23 12:30:10 +02001284 monitor_printf(mon, "%" HWADDR_PRIu, val);
bellard9307c4c2004-04-04 12:57:25 +00001285 break;
1286 default:
1287 case 'd':
Avi Kivitya8170e52012-10-23 12:30:10 +02001288 monitor_printf(mon, "%" HWADDR_PRId, val);
bellard9307c4c2004-04-04 12:57:25 +00001289 break;
1290 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001291 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001292 break;
1293 }
aliguori376253e2009-03-05 23:01:23 +00001294 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001295}
1296
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001297static void hmp_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001298{
1299 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001300 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001301 uint32_t start = qdict_get_int(qdict, "start");
1302 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001303
1304 sum = 0;
1305 for(addr = start; addr < (start + size); addr++) {
Peter Maydell42874d32015-04-26 16:49:24 +01001306 uint8_t val = address_space_ldub(&address_space_memory, addr,
1307 MEMTXATTRS_UNSPECIFIED, NULL);
bellarde4cf1ad2005-06-04 20:15:57 +00001308 /* BSD sum algorithm ('sum' Unix command) */
1309 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001310 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001311 }
aliguori376253e2009-03-05 23:01:23 +00001312 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001313}
1314
bellard13224a82006-07-14 22:03:35 +00001315static int mouse_button_state;
1316
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001317static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001318{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001319 int dx, dy, dz, button;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001320 const char *dx_str = qdict_get_str(qdict, "dx_str");
1321 const char *dy_str = qdict_get_str(qdict, "dy_str");
1322 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001323
bellard13224a82006-07-14 22:03:35 +00001324 dx = strtol(dx_str, NULL, 0);
1325 dy = strtol(dy_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001326 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
1327 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
1328
1329 if (dz_str) {
bellard13224a82006-07-14 22:03:35 +00001330 dz = strtol(dz_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001331 if (dz != 0) {
1332 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
1333 qemu_input_queue_btn(NULL, button, true);
1334 qemu_input_event_sync();
1335 qemu_input_queue_btn(NULL, button, false);
1336 }
1337 }
1338 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001339}
1340
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001341static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001342{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001343 static uint32_t bmap[INPUT_BUTTON_MAX] = {
1344 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1345 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
1346 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1347 };
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001348 int button_state = qdict_get_int(qdict, "button_state");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001349
1350 if (mouse_button_state == button_state) {
1351 return;
1352 }
1353 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1354 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001355 mouse_button_state = button_state;
bellard13224a82006-07-14 22:03:35 +00001356}
1357
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001358static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001359{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001360 int size = qdict_get_int(qdict, "size");
1361 int addr = qdict_get_int(qdict, "addr");
1362 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001363 uint32_t val;
1364 int suffix;
1365
1366 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001367 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001368 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001369 addr++;
1370 }
1371 addr &= 0xffff;
1372
1373 switch(size) {
1374 default:
1375 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001376 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001377 suffix = 'b';
1378 break;
1379 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001380 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001381 suffix = 'w';
1382 break;
1383 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001384 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001385 suffix = 'l';
1386 break;
1387 }
aliguori376253e2009-03-05 23:01:23 +00001388 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1389 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001390}
bellarda3a91a32004-06-04 11:06:21 +00001391
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001392static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001393{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001394 int size = qdict_get_int(qdict, "size");
1395 int addr = qdict_get_int(qdict, "addr");
1396 int val = qdict_get_int(qdict, "val");
1397
Jan Kiszkaf1147842009-07-14 10:20:11 +02001398 addr &= IOPORTS_MASK;
1399
1400 switch (size) {
1401 default:
1402 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001403 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001404 break;
1405 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001406 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001407 break;
1408 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001409 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001410 break;
1411 }
1412}
1413
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001414static void hmp_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001415{
Gongleif1839932014-12-03 18:20:58 +00001416 Error *local_err = NULL;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001417 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001418
Gongleif1839932014-12-03 18:20:58 +00001419 qemu_boot_set(bootdevice, &local_err);
1420 if (local_err) {
1421 monitor_printf(mon, "%s\n", error_get_pretty(local_err));
1422 error_free(local_err);
aurel320ecdffb2008-05-04 20:11:34 +00001423 } else {
Gongleif1839932014-12-03 18:20:58 +00001424 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001425 }
1426}
1427
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001428static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
Blue Swirl314e2982011-09-11 20:22:05 +00001429{
1430 mtree_info((fprintf_function)monitor_printf, mon);
1431}
1432
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001433static void hmp_info_numa(Monitor *mon, const QDict *qdict)
aliguori030ea372009-04-21 22:30:47 +00001434{
aliguorib28b6232009-04-22 20:20:29 +00001435 int i;
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001436 CPUState *cpu;
zhanghailiang5b009e42014-11-04 19:49:30 +08001437 uint64_t *node_mem;
aliguori030ea372009-04-21 22:30:47 +00001438
zhanghailiang5b009e42014-11-04 19:49:30 +08001439 node_mem = g_new0(uint64_t, nb_numa_nodes);
1440 query_numa_node_mem(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001441 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1442 for (i = 0; i < nb_numa_nodes; i++) {
1443 monitor_printf(mon, "node %d cpus:", i);
Andreas Färberbdc44642013-06-24 23:50:24 +02001444 CPU_FOREACH(cpu) {
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001445 if (cpu->numa_node == i) {
Andreas Färber55e5c282012-12-17 06:18:02 +01001446 monitor_printf(mon, " %d", cpu->cpu_index);
aliguori030ea372009-04-21 22:30:47 +00001447 }
1448 }
1449 monitor_printf(mon, "\n");
1450 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
zhanghailiang5b009e42014-11-04 19:49:30 +08001451 node_mem[i] >> 20);
aliguori030ea372009-04-21 22:30:47 +00001452 }
zhanghailiang5b009e42014-11-04 19:49:30 +08001453 g_free(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001454}
1455
bellard5f1ce942006-02-08 22:40:15 +00001456#ifdef CONFIG_PROFILER
1457
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001458int64_t tcg_time;
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001459int64_t dev_time;
1460
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001461static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001462{
aliguori376253e2009-03-05 23:01:23 +00001463 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001464 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00001465 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001466 tcg_time, tcg_time / (double)get_ticks_per_sec());
1467 tcg_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001468 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001469}
1470#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001471static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001472{
aliguori376253e2009-03-05 23:01:23 +00001473 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001474}
1475#endif
1476
bellardec36b692006-07-16 18:57:03 +00001477/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001478static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001479
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001480static void hmp_info_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001481{
1482 int i;
1483 CaptureState *s;
1484
1485 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001486 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001487 s->ops.info (s->opaque);
1488 }
1489}
1490
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001491static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001492{
1493 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001494 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001495 CaptureState *s;
1496
1497 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1498 if (i == n) {
1499 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001500 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001501 g_free (s);
bellardec36b692006-07-16 18:57:03 +00001502 return;
1503 }
1504 }
1505}
1506
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001507static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001508{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001509 const char *path = qdict_get_str(qdict, "path");
1510 int has_freq = qdict_haskey(qdict, "freq");
1511 int freq = qdict_get_try_int(qdict, "freq", -1);
1512 int has_bits = qdict_haskey(qdict, "bits");
1513 int bits = qdict_get_try_int(qdict, "bits", -1);
1514 int has_channels = qdict_haskey(qdict, "nchannels");
1515 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001516 CaptureState *s;
1517
Anthony Liguori7267c092011-08-20 22:09:37 -05001518 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001519
1520 freq = has_freq ? freq : 44100;
1521 bits = has_bits ? bits : 16;
1522 nchannels = has_channels ? nchannels : 2;
1523
1524 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001525 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05001526 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001527 return;
bellardec36b692006-07-16 18:57:03 +00001528 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001529 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001530}
bellardec36b692006-07-16 18:57:03 +00001531
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001532static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001533{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001534 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001535
aliguori76655d62009-03-06 20:27:37 +00001536 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001537 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001538 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001539 return acl;
1540}
aliguori76655d62009-03-06 20:27:37 +00001541
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001542static void hmp_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001543{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001544 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001545 qemu_acl *acl = find_acl(mon, aclname);
1546 qemu_acl_entry *entry;
1547 int i = 0;
1548
1549 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001550 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001551 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00001552 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00001553 i++;
1554 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001555 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001556 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001557 }
1558}
1559
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001560static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001561{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001562 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001563 qemu_acl *acl = find_acl(mon, aclname);
1564
1565 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001566 qemu_acl_reset(acl);
1567 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001568 }
1569}
aliguori76655d62009-03-06 20:27:37 +00001570
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001571static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001572{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001573 const char *aclname = qdict_get_str(qdict, "aclname");
1574 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001575 qemu_acl *acl = find_acl(mon, aclname);
1576
1577 if (acl) {
1578 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001579 acl->defaultDeny = 0;
1580 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001581 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001582 acl->defaultDeny = 1;
1583 monitor_printf(mon, "acl: policy set to 'deny'\n");
1584 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001585 monitor_printf(mon, "acl: unknown policy '%s', "
1586 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001587 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001588 }
1589}
aliguori76655d62009-03-06 20:27:37 +00001590
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001591static void hmp_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001592{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001593 const char *aclname = qdict_get_str(qdict, "aclname");
1594 const char *match = qdict_get_str(qdict, "match");
1595 const char *policy = qdict_get_str(qdict, "policy");
1596 int has_index = qdict_haskey(qdict, "index");
1597 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001598 qemu_acl *acl = find_acl(mon, aclname);
1599 int deny, ret;
1600
1601 if (acl) {
1602 if (strcmp(policy, "allow") == 0) {
1603 deny = 0;
1604 } else if (strcmp(policy, "deny") == 0) {
1605 deny = 1;
1606 } else {
1607 monitor_printf(mon, "acl: unknown policy '%s', "
1608 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001609 return;
1610 }
aliguori28a76be2009-03-06 20:27:40 +00001611 if (has_index)
1612 ret = qemu_acl_insert(acl, deny, match, index);
1613 else
1614 ret = qemu_acl_append(acl, deny, match);
1615 if (ret < 0)
1616 monitor_printf(mon, "acl: unable to add acl entry\n");
1617 else
1618 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001619 }
1620}
aliguori76655d62009-03-06 20:27:37 +00001621
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001622static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001623{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001624 const char *aclname = qdict_get_str(qdict, "aclname");
1625 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001626 qemu_acl *acl = find_acl(mon, aclname);
1627 int ret;
aliguori76655d62009-03-06 20:27:37 +00001628
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001629 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001630 ret = qemu_acl_remove(acl, match);
1631 if (ret < 0)
1632 monitor_printf(mon, "acl: no matching acl entry\n");
1633 else
1634 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001635 }
1636}
1637
Corey Bryant208c9d12012-06-22 14:36:09 -04001638void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001639{
Anthony Liguoric227f092009-10-01 16:12:16 -05001640 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001641 int fd;
1642
Corey Bryant208c9d12012-06-22 14:36:09 -04001643 fd = qemu_chr_fe_get_msgfd(cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001644 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001645 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryant208c9d12012-06-22 14:36:09 -04001646 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001647 }
1648
1649 if (qemu_isdigit(fdname[0])) {
Stefan Hajnoczi0b9f0e22014-04-24 13:58:18 +02001650 close(fd);
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001651 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
1652 "a name not starting with a digit");
Corey Bryant208c9d12012-06-22 14:36:09 -04001653 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001654 }
1655
Corey Bryant208c9d12012-06-22 14:36:09 -04001656 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001657 if (strcmp(monfd->name, fdname) != 0) {
1658 continue;
1659 }
1660
1661 close(monfd->fd);
1662 monfd->fd = fd;
Corey Bryant208c9d12012-06-22 14:36:09 -04001663 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001664 }
1665
Anthony Liguori7267c092011-08-20 22:09:37 -05001666 monfd = g_malloc0(sizeof(mon_fd_t));
1667 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001668 monfd->fd = fd;
1669
Corey Bryant208c9d12012-06-22 14:36:09 -04001670 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001671}
1672
Corey Bryant208c9d12012-06-22 14:36:09 -04001673void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001674{
Anthony Liguoric227f092009-10-01 16:12:16 -05001675 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001676
Corey Bryant208c9d12012-06-22 14:36:09 -04001677 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001678 if (strcmp(monfd->name, fdname) != 0) {
1679 continue;
1680 }
1681
Blue Swirl72cf2d42009-09-12 07:36:22 +00001682 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001683 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001684 g_free(monfd->name);
1685 g_free(monfd);
Corey Bryant208c9d12012-06-22 14:36:09 -04001686 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001687 }
1688
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001689 error_setg(errp, QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001690}
1691
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001692static void hmp_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001693{
Luiz Capitulino13548692011-07-29 15:36:43 -03001694 int saved_vm_running = runstate_is_running();
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001695 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02001696
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001697 vm_stop(RUN_STATE_RESTORE_VM);
Juan Quintelac8d41b22009-08-20 19:42:21 +02001698
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03001699 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02001700 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03001701 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02001702}
1703
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001704int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
Mark McLoughlin7768e042009-07-22 09:11:41 +01001705{
Anthony Liguoric227f092009-10-01 16:12:16 -05001706 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01001707
Blue Swirl72cf2d42009-09-12 07:36:22 +00001708 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01001709 int fd;
1710
1711 if (strcmp(monfd->name, fdname) != 0) {
1712 continue;
1713 }
1714
1715 fd = monfd->fd;
1716
1717 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001718 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05001719 g_free(monfd->name);
1720 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001721
1722 return fd;
1723 }
1724
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001725 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001726 return -1;
1727}
1728
Corey Bryantba1c0482012-08-14 16:43:43 -04001729static void monitor_fdset_cleanup(MonFdset *mon_fdset)
1730{
1731 MonFdsetFd *mon_fdset_fd;
1732 MonFdsetFd *mon_fdset_fd_next;
1733
1734 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantebe52b52012-10-18 15:19:33 -04001735 if ((mon_fdset_fd->removed ||
1736 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
1737 runstate_is_running()) {
Corey Bryantba1c0482012-08-14 16:43:43 -04001738 close(mon_fdset_fd->fd);
1739 g_free(mon_fdset_fd->opaque);
1740 QLIST_REMOVE(mon_fdset_fd, next);
1741 g_free(mon_fdset_fd);
1742 }
1743 }
1744
Corey Bryantadb696f2012-08-14 16:43:47 -04001745 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04001746 QLIST_REMOVE(mon_fdset, next);
1747 g_free(mon_fdset);
1748 }
1749}
1750
Corey Bryantefb87c12012-08-14 16:43:48 -04001751static void monitor_fdsets_cleanup(void)
1752{
1753 MonFdset *mon_fdset;
1754 MonFdset *mon_fdset_next;
1755
1756 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
1757 monitor_fdset_cleanup(mon_fdset);
1758 }
1759}
1760
Corey Bryantba1c0482012-08-14 16:43:43 -04001761AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
1762 const char *opaque, Error **errp)
1763{
1764 int fd;
1765 Monitor *mon = cur_mon;
Corey Bryantba1c0482012-08-14 16:43:43 -04001766 AddfdInfo *fdinfo;
1767
1768 fd = qemu_chr_fe_get_msgfd(mon->chr);
1769 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001770 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryantba1c0482012-08-14 16:43:43 -04001771 goto error;
1772 }
1773
Corey Bryante446f702012-10-18 15:19:32 -04001774 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
1775 has_opaque, opaque, errp);
1776 if (fdinfo) {
1777 return fdinfo;
Corey Bryant9ac54af2012-10-18 15:19:31 -04001778 }
1779
Corey Bryantba1c0482012-08-14 16:43:43 -04001780error:
1781 if (fd != -1) {
1782 close(fd);
1783 }
1784 return NULL;
1785}
1786
1787void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
1788{
1789 MonFdset *mon_fdset;
1790 MonFdsetFd *mon_fdset_fd;
1791 char fd_str[60];
1792
1793 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1794 if (mon_fdset->id != fdset_id) {
1795 continue;
1796 }
1797 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1798 if (has_fd) {
1799 if (mon_fdset_fd->fd != fd) {
1800 continue;
1801 }
1802 mon_fdset_fd->removed = true;
1803 break;
1804 } else {
1805 mon_fdset_fd->removed = true;
1806 }
1807 }
1808 if (has_fd && !mon_fdset_fd) {
1809 goto error;
1810 }
1811 monitor_fdset_cleanup(mon_fdset);
1812 return;
1813 }
1814
1815error:
1816 if (has_fd) {
1817 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
1818 fdset_id, fd);
1819 } else {
1820 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
1821 }
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001822 error_setg(errp, QERR_FD_NOT_FOUND, fd_str);
Corey Bryantba1c0482012-08-14 16:43:43 -04001823}
1824
1825FdsetInfoList *qmp_query_fdsets(Error **errp)
1826{
1827 MonFdset *mon_fdset;
1828 MonFdsetFd *mon_fdset_fd;
1829 FdsetInfoList *fdset_list = NULL;
1830
1831 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1832 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
1833 FdsetFdInfoList *fdsetfd_list = NULL;
1834
1835 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
1836 fdset_info->value->fdset_id = mon_fdset->id;
1837
1838 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1839 FdsetFdInfoList *fdsetfd_info;
1840
1841 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
1842 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
1843 fdsetfd_info->value->fd = mon_fdset_fd->fd;
1844 if (mon_fdset_fd->opaque) {
1845 fdsetfd_info->value->has_opaque = true;
1846 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
1847 } else {
1848 fdsetfd_info->value->has_opaque = false;
1849 }
1850
1851 fdsetfd_info->next = fdsetfd_list;
1852 fdsetfd_list = fdsetfd_info;
1853 }
1854
1855 fdset_info->value->fds = fdsetfd_list;
1856
1857 fdset_info->next = fdset_list;
1858 fdset_list = fdset_info;
1859 }
1860
1861 return fdset_list;
1862}
1863
Corey Bryante446f702012-10-18 15:19:32 -04001864AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
1865 bool has_opaque, const char *opaque,
1866 Error **errp)
1867{
1868 MonFdset *mon_fdset = NULL;
1869 MonFdsetFd *mon_fdset_fd;
1870 AddfdInfo *fdinfo;
1871
1872 if (has_fdset_id) {
1873 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1874 /* Break if match found or match impossible due to ordering by ID */
1875 if (fdset_id <= mon_fdset->id) {
1876 if (fdset_id < mon_fdset->id) {
1877 mon_fdset = NULL;
1878 }
1879 break;
1880 }
1881 }
1882 }
1883
1884 if (mon_fdset == NULL) {
1885 int64_t fdset_id_prev = -1;
1886 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
1887
1888 if (has_fdset_id) {
1889 if (fdset_id < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001890 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
1891 "a non-negative value");
Corey Bryante446f702012-10-18 15:19:32 -04001892 return NULL;
1893 }
1894 /* Use specified fdset ID */
1895 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1896 mon_fdset_cur = mon_fdset;
1897 if (fdset_id < mon_fdset_cur->id) {
1898 break;
1899 }
1900 }
1901 } else {
1902 /* Use first available fdset ID */
1903 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1904 mon_fdset_cur = mon_fdset;
1905 if (fdset_id_prev == mon_fdset_cur->id - 1) {
1906 fdset_id_prev = mon_fdset_cur->id;
1907 continue;
1908 }
1909 break;
1910 }
1911 }
1912
1913 mon_fdset = g_malloc0(sizeof(*mon_fdset));
1914 if (has_fdset_id) {
1915 mon_fdset->id = fdset_id;
1916 } else {
1917 mon_fdset->id = fdset_id_prev + 1;
1918 }
1919
1920 /* The fdset list is ordered by fdset ID */
1921 if (!mon_fdset_cur) {
1922 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
1923 } else if (mon_fdset->id < mon_fdset_cur->id) {
1924 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
1925 } else {
1926 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
1927 }
1928 }
1929
1930 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
1931 mon_fdset_fd->fd = fd;
1932 mon_fdset_fd->removed = false;
1933 if (has_opaque) {
1934 mon_fdset_fd->opaque = g_strdup(opaque);
1935 }
1936 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
1937
1938 fdinfo = g_malloc0(sizeof(*fdinfo));
1939 fdinfo->fdset_id = mon_fdset->id;
1940 fdinfo->fd = mon_fdset_fd->fd;
1941
1942 return fdinfo;
1943}
1944
Corey Bryantadb696f2012-08-14 16:43:47 -04001945int monitor_fdset_get_fd(int64_t fdset_id, int flags)
1946{
Blue Swirlb2dc64c2012-08-18 20:14:54 +00001947#ifndef _WIN32
Corey Bryantadb696f2012-08-14 16:43:47 -04001948 MonFdset *mon_fdset;
1949 MonFdsetFd *mon_fdset_fd;
1950 int mon_fd_flags;
1951
Corey Bryantadb696f2012-08-14 16:43:47 -04001952 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1953 if (mon_fdset->id != fdset_id) {
1954 continue;
1955 }
1956 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1957 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
1958 if (mon_fd_flags == -1) {
1959 return -1;
1960 }
1961
1962 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
1963 return mon_fdset_fd->fd;
1964 }
1965 }
1966 errno = EACCES;
1967 return -1;
1968 }
1969#endif
1970
1971 errno = ENOENT;
1972 return -1;
1973}
1974
1975int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
1976{
1977 MonFdset *mon_fdset;
1978 MonFdsetFd *mon_fdset_fd_dup;
1979
1980 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1981 if (mon_fdset->id != fdset_id) {
1982 continue;
1983 }
1984 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
1985 if (mon_fdset_fd_dup->fd == dup_fd) {
1986 return -1;
1987 }
1988 }
1989 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
1990 mon_fdset_fd_dup->fd = dup_fd;
1991 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
1992 return 0;
1993 }
1994 return -1;
1995}
1996
1997static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
1998{
1999 MonFdset *mon_fdset;
2000 MonFdsetFd *mon_fdset_fd_dup;
2001
2002 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2003 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2004 if (mon_fdset_fd_dup->fd == dup_fd) {
2005 if (remove) {
2006 QLIST_REMOVE(mon_fdset_fd_dup, next);
2007 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2008 monitor_fdset_cleanup(mon_fdset);
2009 }
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002010 return -1;
2011 } else {
2012 return mon_fdset->id;
Corey Bryantadb696f2012-08-14 16:43:47 -04002013 }
Corey Bryantadb696f2012-08-14 16:43:47 -04002014 }
2015 }
2016 }
2017 return -1;
2018}
2019
2020int monitor_fdset_dup_fd_find(int dup_fd)
2021{
2022 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2023}
2024
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002025void monitor_fdset_dup_fd_remove(int dup_fd)
Corey Bryantadb696f2012-08-14 16:43:47 -04002026{
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002027 monitor_fdset_dup_fd_find_remove(dup_fd, true);
Corey Bryantadb696f2012-08-14 16:43:47 -04002028}
2029
Markus Armbruster1677f4c2015-02-09 14:03:19 +01002030int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
Laszlo Ersek59063662014-04-10 10:24:31 +02002031{
2032 int fd;
2033 Error *local_err = NULL;
2034
2035 if (!qemu_isdigit(fdname[0]) && mon) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002036 fd = monitor_get_fd(mon, fdname, &local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002037 } else {
2038 fd = qemu_parse_fd(fdname);
Laszlo Ersek59063662014-04-10 10:24:31 +02002039 if (fd == -1) {
2040 error_setg(&local_err, "Invalid file descriptor number '%s'",
2041 fdname);
2042 }
2043 }
2044 if (local_err) {
2045 error_propagate(errp, local_err);
2046 assert(fd == -1);
2047 } else {
2048 assert(fd != -1);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002049 }
2050
2051 return fd;
2052}
2053
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002054/* Please update hmp-commands.hx when adding or changing commands */
Wayne Xia816f8922011-10-12 11:32:41 +08002055static mon_cmd_t info_cmds[] = {
Pavel Butsykinda76ee72015-09-10 18:38:58 +03002056#include "hmp-commands-info.h"
2057 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002058};
2059
Wenchao Xiaa13ced52013-01-14 14:06:28 +08002060/* mon_cmds and info_cmds would be sorted at runtime */
2061static mon_cmd_t mon_cmds[] = {
2062#include "hmp-commands.h"
2063 { NULL, NULL, },
2064};
2065
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002066static const mon_cmd_t qmp_cmds[] = {
Anthony Liguorie3193602011-09-02 12:34:47 -05002067#include "qmp-commands-old.h"
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002068 { /* NULL */ },
2069};
2070
bellard9307c4c2004-04-04 12:57:25 +00002071/*******************************************************************/
2072
2073static const char *pch;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002074static sigjmp_buf expr_env;
bellard9307c4c2004-04-04 12:57:25 +00002075
bellard9307c4c2004-04-04 12:57:25 +00002076
Stefan Weil9c3175c2013-08-22 21:30:09 +02002077static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
2078expr_error(Monitor *mon, const char *fmt, ...)
bellard9dc39cb2004-03-14 21:38:27 +00002079{
Fam Zheng277acfe2013-08-20 10:58:21 +08002080 va_list ap;
2081 va_start(ap, fmt);
2082 monitor_vprintf(mon, fmt, ap);
2083 monitor_printf(mon, "\n");
2084 va_end(ap);
Peter Maydell6ab7e542013-02-20 15:21:09 +00002085 siglongjmp(expr_env, 1);
bellard9307c4c2004-04-04 12:57:25 +00002086}
2087
Markus Armbruster09b94182010-01-20 13:07:30 +01002088/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00002089static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002090{
Pavel Butsykinbf957282015-09-10 18:38:59 +03002091 const MonitorDef *md = target_monitor_defs();
bellard92a31b12005-02-10 22:00:52 +00002092 void *ptr;
2093
Pavel Butsykinbf957282015-09-10 18:38:59 +03002094 if (md == NULL) {
2095 return -1;
2096 }
2097
2098 for(; md->name != NULL; md++) {
bellard9307c4c2004-04-04 12:57:25 +00002099 if (compare_cmd(name, md->name)) {
2100 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002101 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002102 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002103 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00002104 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002105 switch(md->type) {
2106 case MD_I32:
2107 *pval = *(int32_t *)ptr;
2108 break;
2109 case MD_TLONG:
2110 *pval = *(target_long *)ptr;
2111 break;
2112 default:
2113 *pval = 0;
2114 break;
2115 }
bellard9307c4c2004-04-04 12:57:25 +00002116 }
2117 return 0;
2118 }
2119 }
2120 return -1;
2121}
2122
2123static void next(void)
2124{
Blue Swirl660f11b2009-07-31 21:16:51 +00002125 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002126 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002127 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002128 pch++;
2129 }
2130}
2131
aliguori376253e2009-03-05 23:01:23 +00002132static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002133
aliguori376253e2009-03-05 23:01:23 +00002134static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002135{
blueswir1c2efc952007-09-25 17:28:42 +00002136 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002137 char *p;
bellard6a00d602005-11-21 23:25:50 +00002138 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002139
2140 switch(*pch) {
2141 case '+':
2142 next();
aliguori376253e2009-03-05 23:01:23 +00002143 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002144 break;
2145 case '-':
2146 next();
aliguori376253e2009-03-05 23:01:23 +00002147 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002148 break;
2149 case '~':
2150 next();
aliguori376253e2009-03-05 23:01:23 +00002151 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002152 break;
2153 case '(':
2154 next();
aliguori376253e2009-03-05 23:01:23 +00002155 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002156 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002157 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002158 }
2159 next();
2160 break;
bellard81d09122004-07-14 17:21:37 +00002161 case '\'':
2162 pch++;
2163 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002164 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002165 n = *pch;
2166 pch++;
2167 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002168 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002169 next();
2170 break;
bellard9307c4c2004-04-04 12:57:25 +00002171 case '$':
2172 {
2173 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002174 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002175
bellard9307c4c2004-04-04 12:57:25 +00002176 pch++;
2177 q = buf;
2178 while ((*pch >= 'a' && *pch <= 'z') ||
2179 (*pch >= 'A' && *pch <= 'Z') ||
2180 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002181 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002182 if ((q - buf) < sizeof(buf) - 1)
2183 *q++ = *pch;
2184 pch++;
2185 }
blueswir1cd390082008-11-16 13:53:32 +00002186 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002187 pch++;
2188 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002189 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01002190 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00002191 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00002192 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002193 }
2194 break;
2195 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002196 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002197 n = 0;
2198 break;
2199 default:
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002200 errno = 0;
bellard4f4fbf72006-06-25 18:28:12 +00002201 n = strtoull(pch, &p, 0);
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002202 if (errno == ERANGE) {
2203 expr_error(mon, "number too large");
2204 }
bellard9307c4c2004-04-04 12:57:25 +00002205 if (pch == p) {
Fam Zheng277acfe2013-08-20 10:58:21 +08002206 expr_error(mon, "invalid char '%c' in expression", *p);
bellard9307c4c2004-04-04 12:57:25 +00002207 }
2208 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002209 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002210 pch++;
2211 break;
2212 }
2213 return n;
2214}
2215
2216
aliguori376253e2009-03-05 23:01:23 +00002217static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002218{
blueswir1c2efc952007-09-25 17:28:42 +00002219 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002220 int op;
ths3b46e622007-09-17 08:09:54 +00002221
aliguori376253e2009-03-05 23:01:23 +00002222 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002223 for(;;) {
2224 op = *pch;
2225 if (op != '*' && op != '/' && op != '%')
2226 break;
2227 next();
aliguori376253e2009-03-05 23:01:23 +00002228 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002229 switch(op) {
2230 default:
2231 case '*':
2232 val *= val2;
2233 break;
2234 case '/':
2235 case '%':
ths5fafdf22007-09-16 21:08:06 +00002236 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002237 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002238 if (op == '/')
2239 val /= val2;
2240 else
2241 val %= val2;
2242 break;
2243 }
2244 }
2245 return val;
2246}
2247
aliguori376253e2009-03-05 23:01:23 +00002248static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002249{
blueswir1c2efc952007-09-25 17:28:42 +00002250 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002251 int op;
bellard9307c4c2004-04-04 12:57:25 +00002252
aliguori376253e2009-03-05 23:01:23 +00002253 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002254 for(;;) {
2255 op = *pch;
2256 if (op != '&' && op != '|' && op != '^')
2257 break;
2258 next();
aliguori376253e2009-03-05 23:01:23 +00002259 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002260 switch(op) {
2261 default:
2262 case '&':
2263 val &= val2;
2264 break;
2265 case '|':
2266 val |= val2;
2267 break;
2268 case '^':
2269 val ^= val2;
2270 break;
2271 }
2272 }
2273 return val;
2274}
2275
aliguori376253e2009-03-05 23:01:23 +00002276static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002277{
blueswir1c2efc952007-09-25 17:28:42 +00002278 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002279 int op;
bellard9307c4c2004-04-04 12:57:25 +00002280
aliguori376253e2009-03-05 23:01:23 +00002281 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002282 for(;;) {
2283 op = *pch;
2284 if (op != '+' && op != '-')
2285 break;
2286 next();
aliguori376253e2009-03-05 23:01:23 +00002287 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002288 if (op == '+')
2289 val += val2;
2290 else
2291 val -= val2;
2292 }
2293 return val;
2294}
2295
aliguori376253e2009-03-05 23:01:23 +00002296static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002297{
2298 pch = *pp;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002299 if (sigsetjmp(expr_env, 0)) {
bellard9307c4c2004-04-04 12:57:25 +00002300 *pp = pch;
2301 return -1;
2302 }
blueswir1cd390082008-11-16 13:53:32 +00002303 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002304 pch++;
aliguori376253e2009-03-05 23:01:23 +00002305 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002306 *pp = pch;
2307 return 0;
2308}
2309
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002310static int get_double(Monitor *mon, double *pval, const char **pp)
2311{
2312 const char *p = *pp;
2313 char *tailp;
2314 double d;
2315
2316 d = strtod(p, &tailp);
2317 if (tailp == p) {
2318 monitor_printf(mon, "Number expected\n");
2319 return -1;
2320 }
2321 if (d != d || d - d != 0) {
2322 /* NaN or infinity */
2323 monitor_printf(mon, "Bad number\n");
2324 return -1;
2325 }
2326 *pval = d;
2327 *pp = tailp;
2328 return 0;
2329}
2330
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002331/*
2332 * Store the command-name in cmdname, and return a pointer to
2333 * the remaining of the command string.
2334 */
2335static const char *get_command_name(const char *cmdline,
2336 char *cmdname, size_t nlen)
2337{
2338 size_t len;
2339 const char *p, *pstart;
2340
2341 p = cmdline;
2342 while (qemu_isspace(*p))
2343 p++;
2344 if (*p == '\0')
2345 return NULL;
2346 pstart = p;
2347 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2348 p++;
2349 len = p - pstart;
2350 if (len > nlen - 1)
2351 len = nlen - 1;
2352 memcpy(cmdname, pstart, len);
2353 cmdname[len] = '\0';
2354 return p;
2355}
2356
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002357/**
2358 * Read key of 'type' into 'key' and return the current
2359 * 'type' pointer.
2360 */
2361static char *key_get_info(const char *type, char **key)
2362{
2363 size_t len;
2364 char *p, *str;
2365
2366 if (*type == ',')
2367 type++;
2368
2369 p = strchr(type, ':');
2370 if (!p) {
2371 *key = NULL;
2372 return NULL;
2373 }
2374 len = p - type;
2375
Anthony Liguori7267c092011-08-20 22:09:37 -05002376 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002377 memcpy(str, type, len);
2378 str[len] = '\0';
2379
2380 *key = str;
2381 return ++p;
2382}
2383
bellard9307c4c2004-04-04 12:57:25 +00002384static int default_fmt_format = 'x';
2385static int default_fmt_size = 4;
2386
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002387static int is_valid_option(const char *c, const char *typestr)
2388{
2389 char option[3];
2390
2391 option[0] = '-';
2392 option[1] = *c;
2393 option[2] = '\0';
2394
2395 typestr = strstr(typestr, option);
2396 return (typestr != NULL);
2397}
2398
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03002399static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
2400 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002401{
2402 const mon_cmd_t *cmd;
2403
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03002404 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002405 if (compare_cmd(cmdname, cmd->name)) {
2406 return cmd;
2407 }
2408 }
2409
2410 return NULL;
2411}
2412
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03002413static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
2414{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002415 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03002416}
2417
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002418/*
Bandan Dasae502122015-06-03 18:38:08 -04002419 * Parse command name from @cmdp according to command table @table.
2420 * If blank, return NULL.
2421 * Else, if no valid command can be found, report to @mon, and return
2422 * NULL.
2423 * Else, change @cmdp to point right behind the name, and return its
2424 * command table entry.
2425 * Do not assume the return value points into @table! It doesn't when
2426 * the command is found in a sub-command table.
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002427 */
Anthony Liguoric227f092009-10-01 16:12:16 -05002428static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Bandan Dasae502122015-06-03 18:38:08 -04002429 const char **cmdp,
2430 mon_cmd_t *table)
bellard9307c4c2004-04-04 12:57:25 +00002431{
Bandan Dasae502122015-06-03 18:38:08 -04002432 const char *p;
Anthony Liguoric227f092009-10-01 16:12:16 -05002433 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002434 char cmdname[256];
bellard9dc39cb2004-03-14 21:38:27 +00002435
bellard9307c4c2004-04-04 12:57:25 +00002436 /* extract the command name */
Bandan Dasae502122015-06-03 18:38:08 -04002437 p = get_command_name(*cmdp, cmdname, sizeof(cmdname));
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002438 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002439 return NULL;
ths3b46e622007-09-17 08:09:54 +00002440
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002441 cmd = search_dispatch_table(table, cmdname);
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002442 if (!cmd) {
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002443 monitor_printf(mon, "unknown command: '%.*s'\n",
Bandan Dasae502122015-06-03 18:38:08 -04002444 (int)(p - *cmdp), *cmdp);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002445 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002446 }
bellard9307c4c2004-04-04 12:57:25 +00002447
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002448 /* filter out following useless space */
2449 while (qemu_isspace(*p)) {
2450 p++;
2451 }
Bandan Dasae502122015-06-03 18:38:08 -04002452
2453 *cmdp = p;
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002454 /* search sub command */
Bandan Dasae502122015-06-03 18:38:08 -04002455 if (cmd->sub_table != NULL && *p != '\0') {
2456 return monitor_parse_command(mon, cmdp, cmd->sub_table);
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002457 }
2458
Bandan Dasae502122015-06-03 18:38:08 -04002459 return cmd;
2460}
2461
2462/*
2463 * Parse arguments for @cmd.
2464 * If it can't be parsed, report to @mon, and return NULL.
2465 * Else, insert command arguments into a QDict, and return it.
2466 * Note: On success, caller has to free the QDict structure.
2467 */
2468
2469static QDict *monitor_parse_arguments(Monitor *mon,
2470 const char **endp,
2471 const mon_cmd_t *cmd)
2472{
2473 const char *typestr;
2474 char *key;
2475 int c;
2476 const char *p = *endp;
2477 char buf[1024];
2478 QDict *qdict = qdict_new();
2479
bellard9307c4c2004-04-04 12:57:25 +00002480 /* parse the parameters */
2481 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00002482 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002483 typestr = key_get_info(typestr, &key);
2484 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00002485 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002486 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00002487 typestr++;
2488 switch(c) {
2489 case 'F':
bellard81d09122004-07-14 17:21:37 +00002490 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002491 case 's':
2492 {
2493 int ret;
ths3b46e622007-09-17 08:09:54 +00002494
blueswir1cd390082008-11-16 13:53:32 +00002495 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002496 p++;
2497 if (*typestr == '?') {
2498 typestr++;
2499 if (*p == '\0') {
2500 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03002501 break;
bellard9307c4c2004-04-04 12:57:25 +00002502 }
2503 }
2504 ret = get_str(buf, sizeof(buf), &p);
2505 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002506 switch(c) {
2507 case 'F':
aliguori376253e2009-03-05 23:01:23 +00002508 monitor_printf(mon, "%s: filename expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002509 cmd->name);
bellard81d09122004-07-14 17:21:37 +00002510 break;
2511 case 'B':
aliguori376253e2009-03-05 23:01:23 +00002512 monitor_printf(mon, "%s: block device name expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002513 cmd->name);
bellard81d09122004-07-14 17:21:37 +00002514 break;
2515 default:
Bandan Dasae502122015-06-03 18:38:08 -04002516 monitor_printf(mon, "%s: string expected\n", cmd->name);
bellard81d09122004-07-14 17:21:37 +00002517 break;
2518 }
bellard9307c4c2004-04-04 12:57:25 +00002519 goto fail;
2520 }
Luiz Capitulino53773582009-08-28 15:27:25 -03002521 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00002522 }
2523 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01002524 case 'O':
2525 {
2526 QemuOptsList *opts_list;
2527 QemuOpts *opts;
2528
2529 opts_list = qemu_find_opts(key);
2530 if (!opts_list || opts_list->desc->name) {
2531 goto bad_type;
2532 }
2533 while (qemu_isspace(*p)) {
2534 p++;
2535 }
2536 if (!*p)
2537 break;
2538 if (get_str(buf, sizeof(buf), &p) < 0) {
2539 goto fail;
2540 }
Markus Armbruster70b94332015-02-13 12:50:26 +01002541 opts = qemu_opts_parse_noisily(opts_list, buf, true);
Markus Armbruster361127d2010-02-10 20:24:35 +01002542 if (!opts) {
2543 goto fail;
2544 }
2545 qemu_opts_to_qdict(opts, qdict);
2546 qemu_opts_del(opts);
2547 }
2548 break;
bellard9307c4c2004-04-04 12:57:25 +00002549 case '/':
2550 {
2551 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002552
blueswir1cd390082008-11-16 13:53:32 +00002553 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002554 p++;
2555 if (*p == '/') {
2556 /* format found */
2557 p++;
2558 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002559 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002560 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002561 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002562 count = count * 10 + (*p - '0');
2563 p++;
2564 }
2565 }
2566 size = -1;
2567 format = -1;
2568 for(;;) {
2569 switch(*p) {
2570 case 'o':
2571 case 'd':
2572 case 'u':
2573 case 'x':
2574 case 'i':
2575 case 'c':
2576 format = *p++;
2577 break;
2578 case 'b':
2579 size = 1;
2580 p++;
2581 break;
2582 case 'h':
2583 size = 2;
2584 p++;
2585 break;
2586 case 'w':
2587 size = 4;
2588 p++;
2589 break;
2590 case 'g':
2591 case 'L':
2592 size = 8;
2593 p++;
2594 break;
2595 default:
2596 goto next;
2597 }
2598 }
2599 next:
blueswir1cd390082008-11-16 13:53:32 +00002600 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00002601 monitor_printf(mon, "invalid char in format: '%c'\n",
2602 *p);
bellard9307c4c2004-04-04 12:57:25 +00002603 goto fail;
2604 }
bellard9307c4c2004-04-04 12:57:25 +00002605 if (format < 0)
2606 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002607 if (format != 'i') {
2608 /* for 'i', not specifying a size gives -1 as size */
2609 if (size < 0)
2610 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002611 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002612 }
bellard9307c4c2004-04-04 12:57:25 +00002613 default_fmt_format = format;
2614 } else {
2615 count = 1;
2616 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002617 if (format != 'i') {
2618 size = default_fmt_size;
2619 } else {
2620 size = -1;
2621 }
bellard9307c4c2004-04-04 12:57:25 +00002622 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002623 qdict_put(qdict, "count", qint_from_int(count));
2624 qdict_put(qdict, "format", qint_from_int(format));
2625 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00002626 }
2627 break;
2628 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002629 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002630 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00002631 {
blueswir1c2efc952007-09-25 17:28:42 +00002632 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00002633
blueswir1cd390082008-11-16 13:53:32 +00002634 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002635 p++;
bellard34405572004-06-08 00:55:58 +00002636 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002637 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03002638 if (*p == '\0') {
2639 typestr++;
2640 break;
2641 }
bellard34405572004-06-08 00:55:58 +00002642 } else {
2643 if (*p == '.') {
2644 p++;
blueswir1cd390082008-11-16 13:53:32 +00002645 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00002646 p++;
bellard34405572004-06-08 00:55:58 +00002647 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03002648 typestr++;
2649 break;
bellard34405572004-06-08 00:55:58 +00002650 }
2651 }
bellard13224a82006-07-14 22:03:35 +00002652 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002653 }
aliguori376253e2009-03-05 23:01:23 +00002654 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00002655 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002656 /* Check if 'i' is greater than 32-bit */
2657 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
Bandan Dasae502122015-06-03 18:38:08 -04002658 monitor_printf(mon, "\'%s\' has failed: ", cmd->name);
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002659 monitor_printf(mon, "integer is for 32-bit values\n");
2660 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002661 } else if (c == 'M') {
Luiz Capitulino91162842012-04-26 17:34:30 -03002662 if (val < 0) {
2663 monitor_printf(mon, "enter a positive value\n");
2664 goto fail;
2665 }
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002666 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002667 }
Luiz Capitulino53773582009-08-28 15:27:25 -03002668 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00002669 }
2670 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02002671 case 'o':
2672 {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01002673 int64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02002674 char *end;
2675
2676 while (qemu_isspace(*p)) {
2677 p++;
2678 }
2679 if (*typestr == '?') {
2680 typestr++;
2681 if (*p == '\0') {
2682 break;
2683 }
2684 }
2685 val = strtosz(p, &end);
2686 if (val < 0) {
2687 monitor_printf(mon, "invalid size\n");
2688 goto fail;
2689 }
2690 qdict_put(qdict, key, qint_from_int(val));
2691 p = end;
2692 }
2693 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01002694 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002695 {
2696 double val;
2697
2698 while (qemu_isspace(*p))
2699 p++;
2700 if (*typestr == '?') {
2701 typestr++;
2702 if (*p == '\0') {
2703 break;
2704 }
2705 }
2706 if (get_double(mon, &val, &p) < 0) {
2707 goto fail;
2708 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02002709 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01002710 switch (*p) {
2711 case 'm':
2712 val /= 1e3; p += 2; break;
2713 case 'u':
2714 val /= 1e6; p += 2; break;
2715 case 'n':
2716 val /= 1e9; p += 2; break;
2717 }
2718 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002719 if (*p && !qemu_isspace(*p)) {
2720 monitor_printf(mon, "Unknown unit suffix\n");
2721 goto fail;
2722 }
2723 qdict_put(qdict, key, qfloat_from_double(val));
2724 }
2725 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002726 case 'b':
2727 {
2728 const char *beg;
Eric Blakefc48ffc2015-05-15 16:24:59 -06002729 bool val;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002730
2731 while (qemu_isspace(*p)) {
2732 p++;
2733 }
2734 beg = p;
2735 while (qemu_isgraph(*p)) {
2736 p++;
2737 }
2738 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06002739 val = true;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002740 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06002741 val = false;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002742 } else {
2743 monitor_printf(mon, "Expected 'on' or 'off'\n");
2744 goto fail;
2745 }
Eric Blakefc48ffc2015-05-15 16:24:59 -06002746 qdict_put(qdict, key, qbool_from_bool(val));
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002747 }
2748 break;
bellard9307c4c2004-04-04 12:57:25 +00002749 case '-':
2750 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002751 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03002752 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00002753 /* option */
ths3b46e622007-09-17 08:09:54 +00002754
bellard9307c4c2004-04-04 12:57:25 +00002755 c = *typestr++;
2756 if (c == '\0')
2757 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00002758 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002759 p++;
bellard9307c4c2004-04-04 12:57:25 +00002760 if (*p == '-') {
2761 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002762 if(c != *p) {
2763 if(!is_valid_option(p, typestr)) {
2764
2765 monitor_printf(mon, "%s: unsupported option -%c\n",
Bandan Dasae502122015-06-03 18:38:08 -04002766 cmd->name, *p);
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002767 goto fail;
2768 } else {
2769 skip_key = 1;
2770 }
bellard9307c4c2004-04-04 12:57:25 +00002771 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002772 if(skip_key) {
2773 p = tmp;
2774 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03002775 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002776 p++;
Eric Blakefc48ffc2015-05-15 16:24:59 -06002777 qdict_put(qdict, key, qbool_from_bool(true));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002778 }
bellard9307c4c2004-04-04 12:57:25 +00002779 }
bellard9307c4c2004-04-04 12:57:25 +00002780 }
2781 break;
Wenchao Xia129be002013-08-27 20:38:26 +08002782 case 'S':
2783 {
2784 /* package all remaining string */
2785 int len;
2786
2787 while (qemu_isspace(*p)) {
2788 p++;
2789 }
2790 if (*typestr == '?') {
2791 typestr++;
2792 if (*p == '\0') {
2793 /* no remaining string: NULL argument */
2794 break;
2795 }
2796 }
2797 len = strlen(p);
2798 if (len <= 0) {
2799 monitor_printf(mon, "%s: string expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002800 cmd->name);
Bandan Dase549d2a2015-06-03 18:38:10 -04002801 goto fail;
Wenchao Xia129be002013-08-27 20:38:26 +08002802 }
2803 qdict_put(qdict, key, qstring_from_str(p));
2804 p += len;
2805 }
2806 break;
bellard9307c4c2004-04-04 12:57:25 +00002807 default:
2808 bad_type:
Bandan Dasae502122015-06-03 18:38:08 -04002809 monitor_printf(mon, "%s: unknown type '%c'\n", cmd->name, c);
bellard9307c4c2004-04-04 12:57:25 +00002810 goto fail;
2811 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002812 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002813 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00002814 }
2815 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00002816 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002817 p++;
2818 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00002819 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
Bandan Dasae502122015-06-03 18:38:08 -04002820 cmd->name);
bellard9307c4c2004-04-04 12:57:25 +00002821 goto fail;
2822 }
2823
Bandan Dasae502122015-06-03 18:38:08 -04002824 return qdict;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02002825
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002826fail:
Bandan Dasae502122015-06-03 18:38:08 -04002827 QDECREF(qdict);
Anthony Liguori7267c092011-08-20 22:09:37 -05002828 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002829 return NULL;
2830}
2831
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01002832static void handle_hmp_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002833{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002834 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05002835 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002836
Bandan Dasae502122015-06-03 18:38:08 -04002837 cmd = monitor_parse_command(mon, &cmdline, mon->cmd_table);
2838 if (!cmd) {
2839 return;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002840 }
2841
Bandan Dasae502122015-06-03 18:38:08 -04002842 qdict = monitor_parse_arguments(mon, &cmdline, cmd);
2843 if (!qdict) {
Bandan Dasdd41eea2015-06-03 18:38:09 -04002844 monitor_printf(mon, "Try \"help %s\" for more information\n",
2845 cmd->name);
Bandan Dasae502122015-06-03 18:38:08 -04002846 return;
2847 }
2848
2849 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002850 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00002851}
2852
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08002853static void cmd_completion(Monitor *mon, const char *name, const char *list)
bellard81d09122004-07-14 17:21:37 +00002854{
2855 const char *p, *pstart;
2856 char cmd[128];
2857 int len;
2858
2859 p = list;
2860 for(;;) {
2861 pstart = p;
2862 p = strchr(p, '|');
2863 if (!p)
2864 p = pstart + strlen(pstart);
2865 len = p - pstart;
2866 if (len > sizeof(cmd) - 2)
2867 len = sizeof(cmd) - 2;
2868 memcpy(cmd, pstart, len);
2869 cmd[len] = '\0';
2870 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08002871 readline_add_completion(mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00002872 }
2873 if (*p == '\0')
2874 break;
2875 p++;
2876 }
2877}
2878
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08002879static void file_completion(Monitor *mon, const char *input)
bellard81d09122004-07-14 17:21:37 +00002880{
2881 DIR *ffs;
2882 struct dirent *d;
2883 char path[1024];
2884 char file[1024], file_prefix[1024];
2885 int input_path_len;
2886 const char *p;
2887
ths5fafdf22007-09-16 21:08:06 +00002888 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00002889 if (!p) {
2890 input_path_len = 0;
2891 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00002892 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00002893 } else {
2894 input_path_len = p - input + 1;
2895 memcpy(path, input, input_path_len);
2896 if (input_path_len > sizeof(path) - 1)
2897 input_path_len = sizeof(path) - 1;
2898 path[input_path_len] = '\0';
2899 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2900 }
Bandan Das19f2db52015-06-03 18:38:07 -04002901
bellard81d09122004-07-14 17:21:37 +00002902 ffs = opendir(path);
2903 if (!ffs)
2904 return;
2905 for(;;) {
2906 struct stat sb;
2907 d = readdir(ffs);
2908 if (!d)
2909 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09002910
2911 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
2912 continue;
2913 }
2914
bellard81d09122004-07-14 17:21:37 +00002915 if (strstart(d->d_name, file_prefix, NULL)) {
2916 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00002917 if (input_path_len < sizeof(file))
2918 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2919 d->d_name);
bellard81d09122004-07-14 17:21:37 +00002920 /* stat the file to find out if it's a directory.
2921 * In that case add a slash to speed up typing long paths
2922 */
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01002923 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
blueswir1363a37d2008-08-21 17:58:08 +00002924 pstrcat(file, sizeof(file), "/");
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01002925 }
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08002926 readline_add_completion(mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00002927 }
2928 }
2929 closedir(ffs);
2930}
2931
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002932static const char *next_arg_type(const char *typestr)
2933{
2934 const char *p = strchr(typestr, ':');
2935 return (p != NULL ? ++p : typestr);
2936}
2937
Hani Benhabiles40d19392014-05-07 23:41:30 +01002938static void add_completion_option(ReadLineState *rs, const char *str,
2939 const char *option)
2940{
2941 if (!str || !option) {
2942 return;
2943 }
2944 if (!strncmp(option, str, strlen(str))) {
2945 readline_add_completion(rs, option);
2946 }
2947}
2948
Hani Benhabiles13e315d2014-05-07 23:41:29 +01002949void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
2950{
2951 size_t len;
2952 ChardevBackendInfoList *list, *start;
2953
2954 if (nb_args != 2) {
2955 return;
2956 }
2957 len = strlen(str);
2958 readline_set_completion_index(rs, len);
2959
2960 start = list = qmp_query_chardev_backends(NULL);
2961 while (list) {
2962 const char *chr_name = list->value->name;
2963
2964 if (!strncmp(chr_name, str, len)) {
2965 readline_add_completion(rs, chr_name);
2966 }
2967 list = list->next;
2968 }
2969 qapi_free_ChardevBackendInfoList(start);
2970}
2971
Hani Benhabilesb162b492014-05-07 23:41:31 +01002972void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
2973{
2974 size_t len;
2975 int i;
2976
2977 if (nb_args != 2) {
2978 return;
2979 }
2980 len = strlen(str);
2981 readline_set_completion_index(rs, len);
2982 for (i = 0; NetClientOptionsKind_lookup[i]; i++) {
2983 add_completion_option(rs, str, NetClientOptionsKind_lookup[i]);
2984 }
2985}
2986
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01002987void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles992d3e62014-02-06 23:30:11 +01002988{
2989 GSList *list, *elt;
2990 size_t len;
2991
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01002992 if (nb_args != 2) {
2993 return;
2994 }
2995
Hani Benhabiles992d3e62014-02-06 23:30:11 +01002996 len = strlen(str);
2997 readline_set_completion_index(rs, len);
2998 list = elt = object_class_get_list(TYPE_DEVICE, false);
2999 while (elt) {
3000 const char *name;
3001 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
3002 TYPE_DEVICE);
3003 name = object_class_get_name(OBJECT_CLASS(dc));
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003004
3005 if (!dc->cannot_instantiate_with_device_add_yet
3006 && !strncmp(name, str, len)) {
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003007 readline_add_completion(rs, name);
3008 }
3009 elt = elt->next;
3010 }
3011 g_slist_free(list);
3012}
3013
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003014void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003015{
3016 GSList *list, *elt;
3017 size_t len;
3018
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003019 if (nb_args != 2) {
3020 return;
3021 }
3022
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003023 len = strlen(str);
3024 readline_set_completion_index(rs, len);
3025 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
3026 while (elt) {
3027 const char *name;
3028
3029 name = object_class_get_name(OBJECT_CLASS(elt->data));
3030 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
3031 readline_add_completion(rs, name);
3032 }
3033 elt = elt->next;
3034 }
3035 g_slist_free(list);
3036}
3037
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003038static void peripheral_device_del_completion(ReadLineState *rs,
3039 const char *str, size_t len)
3040{
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003041 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
3042 GSList *list, *item;
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003043
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003044 list = qdev_build_hotpluggable_device_list(peripheral);
3045 if (!list) {
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003046 return;
3047 }
3048
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003049 for (item = list; item; item = g_slist_next(item)) {
3050 DeviceState *dev = item->data;
3051
3052 if (dev->id && !strncmp(str, dev->id, len)) {
3053 readline_add_completion(rs, dev->id);
3054 }
3055 }
3056
3057 g_slist_free(list);
3058}
3059
Hani Benhabiles6297d9a2014-05-07 23:41:28 +01003060void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3061{
3062 size_t len;
3063 ChardevInfoList *list, *start;
3064
3065 if (nb_args != 2) {
3066 return;
3067 }
3068 len = strlen(str);
3069 readline_set_completion_index(rs, len);
3070
3071 start = list = qmp_query_chardev(NULL);
3072 while (list) {
3073 ChardevInfo *chr = list->value;
3074
3075 if (!strncmp(chr->label, str, len)) {
3076 readline_add_completion(rs, chr->label);
3077 }
3078 list = list->next;
3079 }
3080 qapi_free_ChardevInfoList(start);
3081}
3082
Hani Benhabiles8e597772014-05-27 23:39:30 +01003083static void ringbuf_completion(ReadLineState *rs, const char *str)
3084{
3085 size_t len;
3086 ChardevInfoList *list, *start;
3087
3088 len = strlen(str);
3089 readline_set_completion_index(rs, len);
3090
3091 start = list = qmp_query_chardev(NULL);
3092 while (list) {
3093 ChardevInfo *chr_info = list->value;
3094
3095 if (!strncmp(chr_info->label, str, len)) {
3096 CharDriverState *chr = qemu_chr_find(chr_info->label);
3097 if (chr && chr_is_ringbuf(chr)) {
3098 readline_add_completion(rs, chr_info->label);
3099 }
3100 }
3101 list = list->next;
3102 }
3103 qapi_free_ChardevInfoList(start);
3104}
3105
Hani Benhabiles8e597772014-05-27 23:39:30 +01003106void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
3107{
3108 if (nb_args != 2) {
3109 return;
3110 }
3111 ringbuf_completion(rs, str);
3112}
3113
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003114void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
3115{
3116 size_t len;
3117
3118 if (nb_args != 2) {
3119 return;
3120 }
3121
3122 len = strlen(str);
3123 readline_set_completion_index(rs, len);
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003124 peripheral_device_del_completion(rs, str, len);
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003125}
3126
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003127void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003128{
3129 ObjectPropertyInfoList *list, *start;
3130 size_t len;
3131
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003132 if (nb_args != 2) {
3133 return;
3134 }
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003135 len = strlen(str);
3136 readline_set_completion_index(rs, len);
3137
3138 start = list = qmp_qom_list("/objects", NULL);
3139 while (list) {
3140 ObjectPropertyInfo *info = list->value;
3141
3142 if (!strncmp(info->type, "child<", 5)
3143 && !strncmp(info->name, str, len)) {
3144 readline_add_completion(rs, info->name);
3145 }
3146 list = list->next;
3147 }
3148 qapi_free_ObjectPropertyInfoList(start);
3149}
3150
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003151void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
3152{
3153 int i;
3154 char *sep;
3155 size_t len;
3156
3157 if (nb_args != 2) {
3158 return;
3159 }
3160 sep = strrchr(str, '-');
3161 if (sep) {
3162 str = sep + 1;
3163 }
3164 len = strlen(str);
3165 readline_set_completion_index(rs, len);
3166 for (i = 0; i < Q_KEY_CODE_MAX; i++) {
3167 if (!strncmp(str, QKeyCode_lookup[i], len)) {
3168 readline_add_completion(rs, QKeyCode_lookup[i]);
3169 }
3170 }
3171}
3172
Hani Benhabiles40d19392014-05-07 23:41:30 +01003173void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
3174{
3175 size_t len;
3176
3177 len = strlen(str);
3178 readline_set_completion_index(rs, len);
3179 if (nb_args == 2) {
Jason Wangeaed4832015-04-23 14:21:38 +08003180 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles40d19392014-05-07 23:41:30 +01003181 int count, i;
3182 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08003183 NET_CLIENT_OPTIONS_KIND_NONE,
3184 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003185 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles40d19392014-05-07 23:41:30 +01003186 const char *name = ncs[i]->name;
3187 if (!strncmp(str, name, len)) {
3188 readline_add_completion(rs, name);
3189 }
3190 }
3191 } else if (nb_args == 3) {
3192 add_completion_option(rs, str, "on");
3193 add_completion_option(rs, str, "off");
3194 }
3195}
3196
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003197void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
3198{
3199 int len, count, i;
Jason Wangeaed4832015-04-23 14:21:38 +08003200 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003201
3202 if (nb_args != 2) {
3203 return;
3204 }
3205
3206 len = strlen(str);
3207 readline_set_completion_index(rs, len);
3208 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_OPTIONS_KIND_NIC,
Jason Wangeaed4832015-04-23 14:21:38 +08003209 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003210 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003211 QemuOpts *opts;
3212 const char *name = ncs[i]->name;
3213 if (strncmp(str, name, len)) {
3214 continue;
3215 }
3216 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
3217 if (opts) {
3218 readline_add_completion(rs, name);
3219 }
3220 }
3221}
3222
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003223void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
3224{
3225 size_t len;
3226
3227 len = strlen(str);
3228 readline_set_completion_index(rs, len);
3229 if (nb_args == 2) {
3230 TraceEventID id;
3231 for (id = 0; id < trace_event_count(); id++) {
3232 const char *event_name = trace_event_get_name(trace_event_id(id));
3233 if (!strncmp(str, event_name, len)) {
3234 readline_add_completion(rs, event_name);
3235 }
3236 }
3237 } else if (nb_args == 3) {
3238 add_completion_option(rs, str, "on");
3239 add_completion_option(rs, str, "off");
3240 }
3241}
3242
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003243void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
3244{
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003245 int i;
3246
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003247 if (nb_args != 2) {
3248 return;
3249 }
3250 readline_set_completion_index(rs, strlen(str));
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003251 for (i = 0; WatchdogExpirationAction_lookup[i]; i++) {
3252 add_completion_option(rs, str, WatchdogExpirationAction_lookup[i]);
3253 }
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003254}
3255
Hani Benhabilesc68a0402014-05-27 23:39:32 +01003256void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
3257 const char *str)
3258{
3259 size_t len;
3260
3261 len = strlen(str);
3262 readline_set_completion_index(rs, len);
3263 if (nb_args == 2) {
3264 int i;
3265 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
3266 const char *name = MigrationCapability_lookup[i];
3267 if (!strncmp(str, name, len)) {
3268 readline_add_completion(rs, name);
3269 }
3270 }
3271 } else if (nb_args == 3) {
3272 add_completion_option(rs, str, "on");
3273 add_completion_option(rs, str, "off");
3274 }
3275}
3276
Liang Li50e9a622015-03-23 16:32:29 +08003277void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
3278 const char *str)
3279{
3280 size_t len;
3281
3282 len = strlen(str);
3283 readline_set_completion_index(rs, len);
3284 if (nb_args == 2) {
3285 int i;
3286 for (i = 0; i < MIGRATION_PARAMETER_MAX; i++) {
3287 const char *name = MigrationParameter_lookup[i];
3288 if (!strncmp(str, name, len)) {
3289 readline_add_completion(rs, name);
3290 }
3291 }
3292 }
3293}
3294
Hani Benhabilese3bb5322014-05-27 23:39:34 +01003295void host_net_add_completion(ReadLineState *rs, int nb_args, const char *str)
3296{
3297 int i;
3298 size_t len;
3299 if (nb_args != 2) {
3300 return;
3301 }
3302 len = strlen(str);
3303 readline_set_completion_index(rs, len);
3304 for (i = 0; host_net_devices[i]; i++) {
3305 if (!strncmp(host_net_devices[i], str, len)) {
3306 readline_add_completion(rs, host_net_devices[i]);
3307 }
3308 }
3309}
3310
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003311void host_net_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3312{
Jason Wangeaed4832015-04-23 14:21:38 +08003313 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003314 int count, i, len;
3315
3316 len = strlen(str);
3317 readline_set_completion_index(rs, len);
3318 if (nb_args == 2) {
3319 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08003320 NET_CLIENT_OPTIONS_KIND_NONE,
3321 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003322 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003323 int id;
3324 char name[16];
3325
3326 if (net_hub_id_for_client(ncs[i], &id)) {
3327 continue;
3328 }
3329 snprintf(name, sizeof(name), "%d", id);
3330 if (!strncmp(str, name, len)) {
3331 readline_add_completion(rs, name);
3332 }
3333 }
3334 return;
3335 } else if (nb_args == 3) {
3336 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08003337 NET_CLIENT_OPTIONS_KIND_NIC,
3338 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003339 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Jason Wang2c4681f2015-02-02 15:06:38 +08003340 int id;
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003341 const char *name;
3342
Jason Wang2c4681f2015-02-02 15:06:38 +08003343 if (ncs[i]->info->type == NET_CLIENT_OPTIONS_KIND_HUBPORT ||
3344 net_hub_id_for_client(ncs[i], &id)) {
3345 continue;
3346 }
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003347 name = ncs[i]->name;
3348 if (!strncmp(str, name, len)) {
3349 readline_add_completion(rs, name);
3350 }
3351 }
3352 return;
3353 }
3354}
3355
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003356static void vm_completion(ReadLineState *rs, const char *str)
3357{
3358 size_t len;
3359 BlockDriverState *bs = NULL;
3360
3361 len = strlen(str);
3362 readline_set_completion_index(rs, len);
3363 while ((bs = bdrv_next(bs))) {
3364 SnapshotInfoList *snapshots, *snapshot;
3365
3366 if (!bdrv_can_snapshot(bs)) {
3367 continue;
3368 }
3369 if (bdrv_query_snapshot_info_list(bs, &snapshots, NULL)) {
3370 continue;
3371 }
3372 snapshot = snapshots;
3373 while (snapshot) {
3374 char *completion = snapshot->value->name;
3375 if (!strncmp(str, completion, len)) {
3376 readline_add_completion(rs, completion);
3377 }
3378 completion = snapshot->value->id;
3379 if (!strncmp(str, completion, len)) {
3380 readline_add_completion(rs, completion);
3381 }
3382 snapshot = snapshot->next;
3383 }
3384 qapi_free_SnapshotInfoList(snapshots);
3385 }
3386
3387}
3388
3389void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
3390{
3391 if (nb_args == 2) {
3392 vm_completion(rs, str);
3393 }
3394}
3395
3396void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
3397{
3398 if (nb_args == 2) {
3399 vm_completion(rs, str);
3400 }
3401}
3402
Wenchao Xiac35b6402013-08-27 20:38:24 +08003403static void monitor_find_completion_by_table(Monitor *mon,
3404 const mon_cmd_t *cmd_table,
3405 char **args,
3406 int nb_args)
bellard81d09122004-07-14 17:21:37 +00003407{
3408 const char *cmdname;
Wenchao Xiac35b6402013-08-27 20:38:24 +08003409 int i;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003410 const char *ptype, *str, *name;
Anthony Liguoric227f092009-10-01 16:12:16 -05003411 const mon_cmd_t *cmd;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003412 BlockDriverState *bs;
bellard81d09122004-07-14 17:21:37 +00003413
bellard81d09122004-07-14 17:21:37 +00003414 if (nb_args <= 1) {
3415 /* command completion */
3416 if (nb_args == 0)
3417 cmdname = "";
3418 else
3419 cmdname = args[0];
Wenchao Xiad2674b22013-08-27 20:38:16 +08003420 readline_set_completion_index(mon->rs, strlen(cmdname));
Wenchao Xiac35b6402013-08-27 20:38:24 +08003421 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003422 cmd_completion(mon, cmdname, cmd->name);
bellard81d09122004-07-14 17:21:37 +00003423 }
3424 } else {
3425 /* find the command */
Wenchao Xiac35b6402013-08-27 20:38:24 +08003426 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Jan Kiszka03a63482010-06-16 00:38:33 +02003427 if (compare_cmd(args[0], cmd->name)) {
3428 break;
3429 }
bellard81d09122004-07-14 17:21:37 +00003430 }
Jan Kiszka03a63482010-06-16 00:38:33 +02003431 if (!cmd->name) {
Wenchao Xiac35b6402013-08-27 20:38:24 +08003432 return;
Jan Kiszka03a63482010-06-16 00:38:33 +02003433 }
3434
Wenchao Xiad903a772013-08-27 20:38:25 +08003435 if (cmd->sub_table) {
3436 /* do the job again */
Stefan Weile7ae7712015-03-08 19:30:01 +01003437 monitor_find_completion_by_table(mon, cmd->sub_table,
3438 &args[1], nb_args - 1);
3439 return;
Wenchao Xiad903a772013-08-27 20:38:25 +08003440 }
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003441 if (cmd->command_completion) {
Stefan Weile7ae7712015-03-08 19:30:01 +01003442 cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
3443 return;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003444 }
Wenchao Xiad903a772013-08-27 20:38:25 +08003445
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003446 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003447 for(i = 0; i < nb_args - 2; i++) {
3448 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003449 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003450 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003451 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003452 }
3453 }
3454 str = args[nb_args - 1];
Kevin Wolf48fe86f2014-11-12 16:24:02 +01003455 while (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02003456 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00003457 }
bellard81d09122004-07-14 17:21:37 +00003458 switch(*ptype) {
3459 case 'F':
3460 /* file completion */
Wenchao Xiad2674b22013-08-27 20:38:16 +08003461 readline_set_completion_index(mon->rs, strlen(str));
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003462 file_completion(mon, str);
bellard81d09122004-07-14 17:21:37 +00003463 break;
3464 case 'B':
3465 /* block device name completion */
Wenchao Xia599a9262013-08-27 20:38:15 +08003466 readline_set_completion_index(mon->rs, strlen(str));
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003467 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
3468 name = bdrv_get_device_name(bs);
3469 if (str[0] == '\0' ||
3470 !strncmp(name, str, strlen(str))) {
3471 readline_add_completion(mon->rs, name);
3472 }
3473 }
bellard81d09122004-07-14 17:21:37 +00003474 break;
bellard7fe48482004-10-09 18:08:01 +00003475 case 's':
Wenchao Xia129be002013-08-27 20:38:26 +08003476 case 'S':
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003477 if (!strcmp(cmd->name, "help|?")) {
Wenchao Xia7ca0e062013-08-27 20:38:27 +08003478 monitor_find_completion_by_table(mon, cmd_table,
3479 &args[1], nb_args - 1);
bellard7fe48482004-10-09 18:08:01 +00003480 }
3481 break;
bellard81d09122004-07-14 17:21:37 +00003482 default:
3483 break;
3484 }
3485 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08003486}
3487
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003488static void monitor_find_completion(void *opaque,
Wenchao Xiac35b6402013-08-27 20:38:24 +08003489 const char *cmdline)
3490{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003491 Monitor *mon = opaque;
Wenchao Xiac35b6402013-08-27 20:38:24 +08003492 char *args[MAX_ARGS];
3493 int nb_args, len;
3494
3495 /* 1. parse the cmdline */
3496 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
3497 return;
3498 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08003499
3500 /* if the line ends with a space, it means we want to complete the
3501 next arg */
3502 len = strlen(cmdline);
3503 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
3504 if (nb_args >= MAX_ARGS) {
3505 goto cleanup;
3506 }
3507 args[nb_args++] = g_strdup("");
3508 }
3509
3510 /* 2. auto complete according to args */
3511 monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args);
Jan Kiszka03a63482010-06-16 00:38:33 +02003512
3513cleanup:
Wenchao Xiadcc70cd2013-08-27 20:38:22 +08003514 free_cmdline_args(args, nb_args);
bellard81d09122004-07-14 17:21:37 +00003515}
3516
aliguori731b0362009-03-05 23:01:42 +00003517static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003518{
aliguori731b0362009-03-05 23:01:42 +00003519 Monitor *mon = opaque;
3520
Jan Kiszkac62313b2009-12-04 14:05:29 +01003521 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003522}
3523
Markus Armbruster40861822015-05-29 10:27:16 +02003524static bool invalid_qmp_mode(const Monitor *mon, const mon_cmd_t *cmd,
3525 Error **errp)
Luiz Capitulino09069b12010-02-04 18:10:06 -02003526{
Markus Armbruster485febc2015-03-13 17:25:50 +01003527 bool is_cap = cmd->mhandler.cmd_new == qmp_capabilities;
Markus Armbrusterf994b252015-03-06 19:51:51 +01003528
3529 if (is_cap && mon->qmp.in_command_mode) {
Markus Armbruster40861822015-05-29 10:27:16 +02003530 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
3531 "Capabilities negotiation is already complete, command "
3532 "'%s' ignored", cmd->name);
Eric Blake2d5a8342015-04-15 09:19:23 -06003533 return true;
3534 }
Markus Armbrusterf994b252015-03-06 19:51:51 +01003535 if (!is_cap && !mon->qmp.in_command_mode) {
Markus Armbruster40861822015-05-29 10:27:16 +02003536 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
3537 "Expecting capabilities negotiation with "
3538 "'qmp_capabilities' before command '%s'", cmd->name);
Eric Blake2d5a8342015-04-15 09:19:23 -06003539 return true;
3540 }
3541 return false;
Luiz Capitulino09069b12010-02-04 18:10:06 -02003542}
3543
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003544/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03003545 * Argument validation rules:
3546 *
3547 * 1. The argument must exist in cmd_args qdict
3548 * 2. The argument type must be the expected one
3549 *
3550 * Special case: If the argument doesn't exist in cmd_args and
3551 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
3552 * checking is skipped for it.
3553 */
Markus Armbruster326283a2015-03-02 18:39:09 +01003554static void check_client_args_type(const QDict *client_args,
3555 const QDict *cmd_args, int flags,
3556 Error **errp)
Luiz Capitulino4af91932010-06-22 11:44:05 -03003557{
3558 const QDictEntry *ent;
3559
3560 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
3561 QObject *obj;
3562 QString *arg_type;
3563 const QObject *client_arg = qdict_entry_value(ent);
3564 const char *client_arg_name = qdict_entry_key(ent);
3565
3566 obj = qdict_get(cmd_args, client_arg_name);
3567 if (!obj) {
3568 if (flags & QMP_ACCEPT_UNKNOWNS) {
3569 /* handler accepts unknowns */
3570 continue;
3571 }
3572 /* client arg doesn't exist */
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003573 error_setg(errp, QERR_INVALID_PARAMETER, client_arg_name);
Markus Armbruster326283a2015-03-02 18:39:09 +01003574 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003575 }
3576
3577 arg_type = qobject_to_qstring(obj);
3578 assert(arg_type != NULL);
3579
3580 /* check if argument's type is correct */
3581 switch (qstring_get_str(arg_type)[0]) {
3582 case 'F':
3583 case 'B':
3584 case 's':
3585 if (qobject_type(client_arg) != QTYPE_QSTRING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003586 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3587 client_arg_name, "string");
Markus Armbruster326283a2015-03-02 18:39:09 +01003588 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003589 }
3590 break;
3591 case 'i':
3592 case 'l':
3593 case 'M':
Jes Sorensendbc0c672010-10-21 17:15:47 +02003594 case 'o':
Luiz Capitulino4af91932010-06-22 11:44:05 -03003595 if (qobject_type(client_arg) != QTYPE_QINT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003596 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3597 client_arg_name, "int");
Markus Armbruster326283a2015-03-02 18:39:09 +01003598 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003599 }
3600 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003601 case 'T':
3602 if (qobject_type(client_arg) != QTYPE_QINT &&
3603 qobject_type(client_arg) != QTYPE_QFLOAT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003604 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3605 client_arg_name, "number");
Markus Armbruster326283a2015-03-02 18:39:09 +01003606 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003607 }
3608 break;
3609 case 'b':
3610 case '-':
3611 if (qobject_type(client_arg) != QTYPE_QBOOL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003612 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3613 client_arg_name, "bool");
Markus Armbruster326283a2015-03-02 18:39:09 +01003614 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003615 }
3616 break;
3617 case 'O':
3618 assert(flags & QMP_ACCEPT_UNKNOWNS);
3619 break;
Paolo Bonzinib9f89782012-03-22 12:51:11 +01003620 case 'q':
3621 /* Any QObject can be passed. */
3622 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003623 case '/':
3624 case '.':
3625 /*
3626 * These types are not supported by QMP and thus are not
3627 * handled here. Fall through.
3628 */
3629 default:
3630 abort();
3631 }
3632 }
Luiz Capitulino4af91932010-06-22 11:44:05 -03003633}
3634
3635/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003636 * - Check if the client has passed all mandatory args
3637 * - Set special flags for argument validation
3638 */
Markus Armbruster326283a2015-03-02 18:39:09 +01003639static void check_mandatory_args(const QDict *cmd_args,
3640 const QDict *client_args, int *flags,
3641 Error **errp)
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003642{
3643 const QDictEntry *ent;
3644
3645 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
3646 const char *cmd_arg_name = qdict_entry_key(ent);
3647 QString *type = qobject_to_qstring(qdict_entry_value(ent));
3648 assert(type != NULL);
3649
3650 if (qstring_get_str(type)[0] == 'O') {
3651 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
3652 *flags |= QMP_ACCEPT_UNKNOWNS;
3653 } else if (qstring_get_str(type)[0] != '-' &&
3654 qstring_get_str(type)[1] != '?' &&
3655 !qdict_haskey(client_args, cmd_arg_name)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003656 error_setg(errp, QERR_MISSING_PARAMETER, cmd_arg_name);
Markus Armbruster326283a2015-03-02 18:39:09 +01003657 return;
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003658 }
3659 }
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003660}
3661
3662static QDict *qdict_from_args_type(const char *args_type)
3663{
3664 int i;
3665 QDict *qdict;
3666 QString *key, *type, *cur_qs;
3667
3668 assert(args_type != NULL);
3669
3670 qdict = qdict_new();
3671
3672 if (args_type == NULL || args_type[0] == '\0') {
3673 /* no args, empty qdict */
3674 goto out;
3675 }
3676
3677 key = qstring_new();
3678 type = qstring_new();
3679
3680 cur_qs = key;
3681
3682 for (i = 0;; i++) {
3683 switch (args_type[i]) {
3684 case ',':
3685 case '\0':
3686 qdict_put(qdict, qstring_get_str(key), type);
3687 QDECREF(key);
3688 if (args_type[i] == '\0') {
3689 goto out;
3690 }
3691 type = qstring_new(); /* qdict has ref */
3692 cur_qs = key = qstring_new();
3693 break;
3694 case ':':
3695 cur_qs = type;
3696 break;
3697 default:
3698 qstring_append_chr(cur_qs, args_type[i]);
3699 break;
3700 }
3701 }
3702
3703out:
3704 return qdict;
3705}
3706
3707/*
3708 * Client argument checking rules:
3709 *
3710 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03003711 * 2. Each argument provided by the client must be expected
3712 * 3. Each argument provided by the client must have the type expected
3713 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003714 */
Markus Armbruster326283a2015-03-02 18:39:09 +01003715static void qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args,
3716 Error **errp)
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003717{
Markus Armbruster326283a2015-03-02 18:39:09 +01003718 Error *err = NULL;
3719 int flags;
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003720 QDict *cmd_args;
3721
3722 cmd_args = qdict_from_args_type(cmd->args_type);
3723
3724 flags = 0;
Markus Armbruster326283a2015-03-02 18:39:09 +01003725 check_mandatory_args(cmd_args, client_args, &flags, &err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003726 if (err) {
3727 goto out;
3728 }
3729
Markus Armbruster326283a2015-03-02 18:39:09 +01003730 check_client_args_type(client_args, cmd_args, flags, &err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003731
3732out:
Markus Armbruster326283a2015-03-02 18:39:09 +01003733 error_propagate(errp, err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003734 QDECREF(cmd_args);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003735}
3736
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003737/*
3738 * Input object checking rules
3739 *
3740 * 1. Input object must be a dict
3741 * 2. The "execute" key must exist
3742 * 3. The "execute" key must be a string
3743 * 4. If the "arguments" key exists, it must be a dict
3744 * 5. If the "id" key exists, it can be anything (ie. json-value)
3745 * 6. Any argument not listed above is considered invalid
3746 */
Markus Armbrusterba0510a2015-03-02 18:41:43 +01003747static QDict *qmp_check_input_obj(QObject *input_obj, Error **errp)
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003748{
3749 const QDictEntry *ent;
3750 int has_exec_key = 0;
3751 QDict *input_dict;
3752
3753 if (qobject_type(input_obj) != QTYPE_QDICT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003754 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "object");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003755 return NULL;
3756 }
3757
3758 input_dict = qobject_to_qdict(input_obj);
3759
3760 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
3761 const char *arg_name = qdict_entry_key(ent);
3762 const QObject *arg_obj = qdict_entry_value(ent);
3763
3764 if (!strcmp(arg_name, "execute")) {
3765 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003766 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
3767 "execute", "string");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003768 return NULL;
3769 }
3770 has_exec_key = 1;
3771 } else if (!strcmp(arg_name, "arguments")) {
3772 if (qobject_type(arg_obj) != QTYPE_QDICT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003773 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
3774 "arguments", "object");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003775 return NULL;
3776 }
Markus Armbruster779cec42015-06-08 10:44:30 +02003777 } else if (!strcmp(arg_name, "id")) {
3778 /* Any string is acceptable as "id", so nothing to check */
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003779 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003780 error_setg(errp, QERR_QMP_EXTRA_MEMBER, arg_name);
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003781 return NULL;
3782 }
3783 }
3784
3785 if (!has_exec_key) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003786 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "execute");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003787 return NULL;
3788 }
3789
3790 return input_dict;
3791}
3792
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003793static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
3794{
Markus Armbruster326283a2015-03-02 18:39:09 +01003795 Error *local_err = NULL;
Markus Armbruster84add862015-03-05 16:45:15 +01003796 QObject *obj, *data;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003797 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003798 const mon_cmd_t *cmd;
Luiz Capitulino40e5a012011-10-21 16:15:31 -02003799 const char *cmd_name;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003800 Monitor *mon = cur_mon;
3801
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03003802 args = input = NULL;
Markus Armbruster84add862015-03-05 16:45:15 +01003803 data = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003804
3805 obj = json_parser_parse(tokens, NULL);
3806 if (!obj) {
3807 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003808 error_setg(&local_err, QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003809 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003810 }
3811
Markus Armbrusterba0510a2015-03-02 18:41:43 +01003812 input = qmp_check_input_obj(obj, &local_err);
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003813 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003814 qobject_decref(obj);
3815 goto err_out;
3816 }
3817
Markus Armbruster74358f22015-03-06 19:35:59 +01003818 mon->qmp.id = qdict_get(input, "id");
3819 qobject_incref(mon->qmp.id);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003820
Luiz Capitulino0bbab462010-05-31 17:32:50 -03003821 cmd_name = qdict_get_str(input, "execute");
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +01003822 trace_handle_qmp_command(mon, cmd_name);
Anthony Liguorie3193602011-09-02 12:34:47 -05003823 cmd = qmp_find_cmd(cmd_name);
Eric Blake2d5a8342015-04-15 09:19:23 -06003824 if (!cmd) {
Markus Armbruster710aec92015-03-06 11:28:00 +01003825 error_set(&local_err, ERROR_CLASS_COMMAND_NOT_FOUND,
3826 "The command %s has not been found", cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03003827 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003828 }
Markus Armbruster40861822015-05-29 10:27:16 +02003829 if (invalid_qmp_mode(mon, cmd, &local_err)) {
Eric Blake2d5a8342015-04-15 09:19:23 -06003830 goto err_out;
3831 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003832
3833 obj = qdict_get(input, "arguments");
3834 if (!obj) {
3835 args = qdict_new();
3836 } else {
3837 args = qobject_to_qdict(obj);
3838 QINCREF(args);
3839 }
3840
Markus Armbruster326283a2015-03-02 18:39:09 +01003841 qmp_check_client_args(cmd, args, &local_err);
3842 if (local_err) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003843 goto err_out;
3844 }
3845
Markus Armbruster485febc2015-03-13 17:25:50 +01003846 cmd->mhandler.cmd_new(args, &data, &local_err);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003847
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003848err_out:
Markus Armbruster710aec92015-03-06 11:28:00 +01003849 monitor_protocol_emitter(mon, data, local_err);
Markus Armbruster84add862015-03-05 16:45:15 +01003850 qobject_decref(data);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03003851 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003852 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003853}
3854
Markus Armbrusterc83fe232015-03-06 19:20:51 +01003855static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003856{
3857 Monitor *old_mon = cur_mon;
3858
3859 cur_mon = opaque;
3860
Markus Armbruster74358f22015-03-06 19:35:59 +01003861 json_message_parser_feed(&cur_mon->qmp.parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003862
3863 cur_mon = old_mon;
3864}
3865
aliguori731b0362009-03-05 23:01:42 +00003866static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00003867{
aliguori731b0362009-03-05 23:01:42 +00003868 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00003869 int i;
aliguori376253e2009-03-05 23:01:23 +00003870
aliguori731b0362009-03-05 23:01:42 +00003871 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00003872
aliguoricde76ee2009-03-05 23:01:51 +00003873 if (cur_mon->rs) {
3874 for (i = 0; i < size; i++)
3875 readline_handle_byte(cur_mon->rs, buf[i]);
3876 } else {
3877 if (size == 0 || buf[size - 1] != 0)
3878 monitor_printf(cur_mon, "corrupted command\n");
3879 else
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003880 handle_hmp_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00003881 }
aliguori731b0362009-03-05 23:01:42 +00003882
3883 cur_mon = old_mon;
3884}
aliguorid8f44602008-10-06 13:52:44 +00003885
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003886static void monitor_command_cb(void *opaque, const char *cmdline,
3887 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00003888{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003889 Monitor *mon = opaque;
3890
aliguori731b0362009-03-05 23:01:42 +00003891 monitor_suspend(mon);
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003892 handle_hmp_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00003893 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00003894}
3895
aliguoricde76ee2009-03-05 23:01:51 +00003896int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003897{
aliguoricde76ee2009-03-05 23:01:51 +00003898 if (!mon->rs)
3899 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00003900 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00003901 return 0;
aliguorid8f44602008-10-06 13:52:44 +00003902}
3903
aliguori376253e2009-03-05 23:01:23 +00003904void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003905{
aliguoricde76ee2009-03-05 23:01:51 +00003906 if (!mon->rs)
3907 return;
aliguori731b0362009-03-05 23:01:42 +00003908 if (--mon->suspend_cnt == 0)
3909 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00003910}
3911
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003912static QObject *get_qmp_greeting(void)
3913{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03003914 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003915
Markus Armbruster485febc2015-03-13 17:25:50 +01003916 qmp_marshal_input_query_version(NULL, &ver, NULL);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003917 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
3918}
3919
Markus Armbrusterc83fe232015-03-06 19:20:51 +01003920static void monitor_qmp_event(void *opaque, int event)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003921{
Luiz Capitulino47116d12010-02-08 17:01:30 -02003922 QObject *data;
3923 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003924
Luiz Capitulino47116d12010-02-08 17:01:30 -02003925 switch (event) {
3926 case CHR_EVENT_OPENED:
Markus Armbrusterf994b252015-03-06 19:51:51 +01003927 mon->qmp.in_command_mode = false;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003928 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003929 monitor_json_emitter(mon, data);
3930 qobject_decref(data);
Corey Bryantefb87c12012-08-14 16:43:48 -04003931 mon_refcount++;
Luiz Capitulino47116d12010-02-08 17:01:30 -02003932 break;
3933 case CHR_EVENT_CLOSED:
Markus Armbruster74358f22015-03-06 19:35:59 +01003934 json_message_parser_destroy(&mon->qmp.parser);
3935 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Corey Bryantefb87c12012-08-14 16:43:48 -04003936 mon_refcount--;
3937 monitor_fdsets_cleanup();
Luiz Capitulino47116d12010-02-08 17:01:30 -02003938 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003939 }
3940}
3941
aliguori731b0362009-03-05 23:01:42 +00003942static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00003943{
aliguori376253e2009-03-05 23:01:23 +00003944 Monitor *mon = opaque;
3945
aliguori2724b182009-03-05 23:01:47 +00003946 switch (event) {
3947 case CHR_EVENT_MUX_IN:
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003948 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003949 mon->mux_out = 0;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003950 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003951 if (mon->reset_seen) {
3952 readline_restart(mon->rs);
3953 monitor_resume(mon);
3954 monitor_flush(mon);
3955 } else {
3956 mon->suspend_cnt = 0;
3957 }
aliguori2724b182009-03-05 23:01:47 +00003958 break;
ths86e94de2007-01-05 22:01:59 +00003959
aliguori2724b182009-03-05 23:01:47 +00003960 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003961 if (mon->reset_seen) {
3962 if (mon->suspend_cnt == 0) {
3963 monitor_printf(mon, "\n");
3964 }
3965 monitor_flush(mon);
3966 monitor_suspend(mon);
3967 } else {
3968 mon->suspend_cnt++;
3969 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003970 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003971 mon->mux_out = 1;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003972 qemu_mutex_unlock(&mon->out_lock);
aliguori2724b182009-03-05 23:01:47 +00003973 break;
3974
Amit Shahb6b8df52009-10-07 18:31:16 +05303975 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00003976 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3977 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003978 if (!mon->mux_out) {
Stratos Psomadakise5554e22014-09-15 15:34:57 +03003979 readline_restart(mon->rs);
aliguori2724b182009-03-05 23:01:47 +00003980 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003981 }
3982 mon->reset_seen = 1;
Corey Bryantefb87c12012-08-14 16:43:48 -04003983 mon_refcount++;
3984 break;
3985
3986 case CHR_EVENT_CLOSED:
3987 mon_refcount--;
3988 monitor_fdsets_cleanup();
aliguori2724b182009-03-05 23:01:47 +00003989 break;
3990 }
ths86e94de2007-01-05 22:01:59 +00003991}
3992
Wayne Xia816f8922011-10-12 11:32:41 +08003993static int
3994compare_mon_cmd(const void *a, const void *b)
3995{
3996 return strcmp(((const mon_cmd_t *)a)->name,
3997 ((const mon_cmd_t *)b)->name);
3998}
3999
4000static void sortcmdlist(void)
4001{
4002 int array_num;
4003 int elem_size = sizeof(mon_cmd_t);
4004
4005 array_num = sizeof(mon_cmds)/elem_size-1;
4006 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
4007
4008 array_num = sizeof(info_cmds)/elem_size-1;
4009 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
4010}
4011
aliguori76655d62009-03-06 20:27:37 +00004012
4013/*
4014 * Local variables:
4015 * c-indent-level: 4
4016 * c-basic-offset: 4
4017 * tab-width: 8
4018 * End:
4019 */
4020
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004021/* These functions just adapt the readline interface in a typesafe way. We
4022 * could cast function pointers but that discards compiler checks.
4023 */
Stefan Weild5d15072014-01-25 18:18:23 +01004024static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
4025 const char *fmt, ...)
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004026{
4027 va_list ap;
4028 va_start(ap, fmt);
4029 monitor_vprintf(opaque, fmt, ap);
4030 va_end(ap);
4031}
4032
4033static void monitor_readline_flush(void *opaque)
4034{
4035 monitor_flush(opaque);
4036}
4037
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004038static void __attribute__((constructor)) monitor_lock_init(void)
4039{
4040 qemu_mutex_init(&monitor_lock);
4041}
4042
aliguori731b0362009-03-05 23:01:42 +00004043void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004044{
aliguori731b0362009-03-05 23:01:42 +00004045 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004046 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004047
4048 if (is_first_init) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +02004049 monitor_qapi_event_init();
Wenchao Xiad0383172013-08-27 20:38:18 +08004050 sortcmdlist();
ths20d8a3e2007-02-18 17:04:49 +00004051 is_first_init = 0;
4052 }
aliguori87127162009-03-05 23:01:29 +00004053
Wenchao Xiab01fe892013-08-27 20:38:19 +08004054 mon = g_malloc(sizeof(*mon));
4055 monitor_data_init(mon);
ths20d8a3e2007-02-18 17:04:49 +00004056
aliguori87127162009-03-05 23:01:29 +00004057 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00004058 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004059 if (flags & MONITOR_USE_READLINE) {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004060 mon->rs = readline_init(monitor_readline_printf,
4061 monitor_readline_flush,
4062 mon,
4063 monitor_find_completion);
aliguoricde76ee2009-03-05 23:01:51 +00004064 monitor_read_command(mon, 0);
4065 }
aliguori87127162009-03-05 23:01:29 +00004066
Markus Armbruster9f3982f2015-03-06 19:56:38 +01004067 if (monitor_is_qmp(mon)) {
Markus Armbrusterc83fe232015-03-06 19:20:51 +01004068 qemu_chr_add_handlers(chr, monitor_can_read, monitor_qmp_read,
4069 monitor_qmp_event, mon);
Anthony Liguori15f31512011-08-15 11:17:35 -05004070 qemu_chr_fe_set_echo(chr, true);
Markus Armbruster74358f22015-03-06 19:35:59 +01004071 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004072 } else {
4073 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4074 monitor_event, mon);
4075 }
aliguori87127162009-03-05 23:01:29 +00004076
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004077 qemu_mutex_lock(&monitor_lock);
Blue Swirl72cf2d42009-09-12 07:36:22 +00004078 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004079 qemu_mutex_unlock(&monitor_lock);
bellard7e2515e2004-08-01 21:52:19 +00004080}
4081
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004082static void bdrv_password_cb(void *opaque, const char *password,
4083 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00004084{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004085 Monitor *mon = opaque;
4086 BlockDriverState *bs = readline_opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004087 int ret = 0;
Markus Armbruster4d2855a2015-01-29 10:37:00 +01004088 Error *local_err = NULL;
bellard7e2515e2004-08-01 21:52:19 +00004089
Markus Armbruster4d2855a2015-01-29 10:37:00 +01004090 bdrv_add_key(bs, password, &local_err);
4091 if (local_err) {
4092 monitor_printf(mon, "%s\n", error_get_pretty(local_err));
4093 error_free(local_err);
aliguoribb5fc202009-03-05 23:01:15 +00004094 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004095 }
aliguori731b0362009-03-05 23:01:42 +00004096 if (mon->password_completion_cb)
4097 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004098
aliguori731b0362009-03-05 23:01:42 +00004099 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004100}
aliguoric0f4ce72009-03-05 23:01:01 +00004101
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004102int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
Markus Armbruster097310b2014-10-07 13:59:15 +02004103 BlockCompletionFunc *completion_cb,
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004104 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004105{
aliguoricde76ee2009-03-05 23:01:51 +00004106 int err;
4107
aliguori376253e2009-03-05 23:01:23 +00004108 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4109 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00004110
aliguori731b0362009-03-05 23:01:42 +00004111 mon->password_completion_cb = completion_cb;
4112 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004113
aliguoricde76ee2009-03-05 23:01:51 +00004114 err = monitor_read_password(mon, bdrv_password_cb, bs);
4115
4116 if (err && completion_cb)
4117 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004118
4119 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00004120}
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004121
4122int monitor_read_block_device_key(Monitor *mon, const char *device,
Markus Armbruster097310b2014-10-07 13:59:15 +02004123 BlockCompletionFunc *completion_cb,
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004124 void *opaque)
4125{
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01004126 Error *err = NULL;
Fam Zheng55606252015-03-02 19:36:46 +08004127 BlockBackend *blk;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004128
Fam Zheng55606252015-03-02 19:36:46 +08004129 blk = blk_by_name(device);
4130 if (!blk) {
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004131 monitor_printf(mon, "Device not found %s\n", device);
4132 return -1;
4133 }
4134
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01004135 bdrv_add_key(blk_bs(blk), NULL, &err);
4136 if (err) {
4137 error_free(err);
4138 return monitor_read_bdrv_key_start(mon, blk_bs(blk), completion_cb, opaque);
4139 }
4140
4141 if (completion_cb) {
4142 completion_cb(opaque, 0);
4143 }
4144 return 0;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004145}
Paolo Bonzini4d454572012-11-26 16:03:42 +01004146
4147QemuOptsList qemu_mon_opts = {
4148 .name = "mon",
4149 .implied_opt_name = "chardev",
4150 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
4151 .desc = {
4152 {
4153 .name = "mode",
4154 .type = QEMU_OPT_STRING,
4155 },{
4156 .name = "chardev",
4157 .type = QEMU_OPT_STRING,
4158 },{
4159 .name = "default",
4160 .type = QEMU_OPT_BOOL,
4161 },{
4162 .name = "pretty",
4163 .type = QEMU_OPT_BOOL,
4164 },
4165 { /* end of list */ }
4166 },
4167};
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004168
4169#ifndef TARGET_I386
4170void qmp_rtc_reset_reinjection(Error **errp)
4171{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004172 error_setg(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection");
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004173}
4174#endif
Jason J. Herne7ee0c3e2015-06-26 14:03:16 -04004175
4176#ifndef TARGET_S390X
4177void qmp_dump_skeys(const char *filename, Error **errp)
4178{
4179 error_setg(errp, QERR_FEATURE_DISABLED, "dump-skeys");
4180}
4181#endif