blob: 90c5bafcc35ceba0434516e41ead45d277d4ffef [file] [log] [blame]
bellard9dc39cb2004-03-14 21:38:27 +00001/*
2 * QEMU monitor
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard9dc39cb2004-03-14 21:38:27 +00004 * Copyright (c) 2003-2004 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard9dc39cb2004-03-14 21:38:27 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
Peter Maydelld38ea872016-01-29 17:50:05 +000024#include "qemu/osdep.h"
blueswir1511d2b12009-03-07 15:32:56 +000025#include <dirent.h>
Paolo Bonzini33c11872016-03-15 16:58:45 +010026#include "qemu-common.h"
27#include "cpu.h"
pbrook87ecb682007-11-17 17:14:51 +000028#include "hw/hw.h"
Paolo Bonzinib4a42f82013-02-04 11:37:52 +010029#include "monitor/qdev.h"
pbrook87ecb682007-11-17 17:14:51 +000030#include "hw/usb.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010031#include "hw/i386/pc.h"
Michael S. Tsirkina2cb15b2012-12-12 14:24:50 +020032#include "hw/pci/pci.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010033#include "sysemu/watchdog.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +020034#include "hw/loader.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010035#include "exec/gdbstub.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020036#include "net/net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000037#include "net/slirp.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020038#include "sysemu/char.h"
Gerd Hoffmann75721502010-10-07 12:22:54 +020039#include "ui/qemu-spice.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010040#include "sysemu/sysemu.h"
Eduardo Habkoste35704b2015-02-08 16:51:16 -020041#include "sysemu/numa.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010042#include "monitor/monitor.h"
Stefan Hajnoczi0150cd82013-11-14 11:54:15 +010043#include "qemu/readline.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010044#include "ui/console.h"
Gerd Hoffmannc751a742013-12-04 15:02:28 +010045#include "ui/input.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010046#include "sysemu/blockdev.h"
Max Reitzda27a002016-03-16 19:54:29 +010047#include "sysemu/block-backend.h"
pbrook87ecb682007-11-17 17:14:51 +000048#include "audio/audio.h"
Paolo Bonzini76cad712012-10-24 11:12:21 +020049#include "disas/disas.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010050#include "sysemu/balloon.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010051#include "qemu/timer.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010052#include "migration/migration.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010053#include "sysemu/kvm.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010054#include "qemu/acl.h"
Paolo Bonzinibdee56f2013-04-02 18:28:41 +020055#include "sysemu/tpm.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010056#include "qapi/qmp/qerror.h"
Eric Blakec7eb39c2016-06-09 10:48:32 -060057#include "qapi/qmp/types.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010058#include "qapi/qmp/qjson.h"
59#include "qapi/qmp/json-streamer.h"
60#include "qapi/qmp/json-parser.h"
Markus Armbrustera9c94272016-06-22 19:11:19 +020061#include "qom/object_interfaces.h"
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +010062#include "trace.h"
Lluís31965ae2011-08-31 20:31:24 +020063#include "trace/control.h"
Pavel Butsykinbf957282015-09-10 18:38:59 +030064#include "monitor/hmp-target.h"
Lluís6d8a7642011-08-31 20:30:43 +020065#ifdef CONFIG_TRACE_SIMPLE
Lluís31965ae2011-08-31 20:31:24 +020066#include "trace/simple.h"
Prerna Saxena22890ab2010-06-24 17:04:53 +053067#endif
Paolo Bonzini022c62c2012-12-17 18:19:49 +010068#include "exec/memory.h"
Paolo Bonzini63c91552016-03-15 13:18:37 +010069#include "exec/exec-all.h"
Paolo Bonzini03dd0242015-12-15 13:16:16 +010070#include "qemu/log.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050071#include "qmp-commands.h"
72#include "hmp.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010073#include "qemu/thread.h"
Hani Benhabilesb21631f2014-05-27 23:39:37 +010074#include "block/qapi.h"
Wenchao Xia43a14cf2014-06-18 08:43:31 +020075#include "qapi/qmp-event.h"
76#include "qapi-event.h"
Markus Armbruster39a18152015-09-16 13:06:28 +020077#include "qmp-introspect.h"
Alberto Garciadc599972016-03-10 13:55:26 +020078#include "sysemu/qtest.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020079#include "qemu/cutils.h"
Marc-André Lureauedcfaef2016-09-12 13:19:00 +040080#include "qapi/qmp/dispatch.h"
ths6a5bd302007-12-03 17:05:38 +000081
Jason J. Hernea4538a52015-06-26 14:07:21 -040082#if defined(TARGET_S390X)
83#include "hw/s390x/storage-keys.h"
84#endif
85
bellard9307c4c2004-04-04 12:57:25 +000086/*
87 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000088 *
bellard9307c4c2004-04-04 12:57:25 +000089 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000090 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000091 * 's' string (accept optional quote)
Wenchao Xia129be002013-08-27 20:38:26 +080092 * 'S' it just appends the rest of the string (accept optional quote)
Markus Armbruster361127d2010-02-10 20:24:35 +010093 * 'O' option string of the form NAME=VALUE,...
94 * parsed according to QemuOptsList given by its name
95 * Example: 'device:O' uses qemu_device_opts.
96 * Restriction: only lists with empty desc are supported
97 * TODO lift the restriction
bellard92a31b12005-02-10 22:00:52 +000098 * 'i' 32 bit integer
99 * 'l' target long (32 or 64 bit)
Luiz Capitulino91162842012-04-26 17:34:30 -0300100 * 'M' Non-negative target long (32 or 64 bit), in user mode the
101 * value is multiplied by 2^20 (think Mebibyte)
Jes Sorensendbc0c672010-10-21 17:15:47 +0200102 * 'o' octets (aka bytes)
Kevin Wolf5e009842013-06-05 14:19:27 +0200103 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
104 * K, k suffix, which multiplies the value by 2^60 for suffixes E
105 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
106 * 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 +0100107 * 'T' double
108 * user mode accepts an optional ms, us, ns suffix,
109 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +0000110 * '/' optional gdb-like print format (like "/10x")
111 *
Luiz Capitulinofb466602009-08-28 15:27:27 -0300112 * '?' optional type (for all types, except '/')
113 * '.' other form of optional type (for 'i' and 'l')
Markus Armbruster942cd1f2010-03-26 09:07:09 +0100114 * 'b' boolean
115 * user mode accepts "on" or "off"
Luiz Capitulinofb466602009-08-28 15:27:27 -0300116 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +0000117 *
118 */
119
Anthony Liguoric227f092009-10-01 16:12:16 -0500120typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +0000121 const char *name;
bellard9307c4c2004-04-04 12:57:25 +0000122 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +0000123 const char *params;
124 const char *help;
Marc-André Lureau2b9e3572016-09-12 13:19:06 +0400125 void (*cmd)(Monitor *mon, const QDict *qdict);
126 /* @sub_table is a list of 2nd level of commands. If it does not exist,
127 * cmd should be used. If it exists, sub_table[?].cmd should be
128 * used, and cmd of 1st level plays the role of help function.
Wenchao Xia5f3d3352013-01-14 14:06:27 +0800129 */
130 struct mon_cmd_t *sub_table;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +0100131 void (*command_completion)(ReadLineState *rs, int nb_args, const char *str);
Anthony Liguoric227f092009-10-01 16:12:16 -0500132} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000133
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100134/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500135typedef struct mon_fd_t mon_fd_t;
136struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100137 char *name;
138 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500139 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100140};
141
Corey Bryantba1c0482012-08-14 16:43:43 -0400142/* file descriptor associated with a file descriptor set */
143typedef struct MonFdsetFd MonFdsetFd;
144struct MonFdsetFd {
145 int fd;
146 bool removed;
147 char *opaque;
148 QLIST_ENTRY(MonFdsetFd) next;
149};
150
151/* file descriptor set containing fds passed via SCM_RIGHTS */
152typedef struct MonFdset MonFdset;
153struct MonFdset {
154 int64_t id;
155 QLIST_HEAD(, MonFdsetFd) fds;
Corey Bryantadb696f2012-08-14 16:43:47 -0400156 QLIST_HEAD(, MonFdsetFd) dup_fds;
Corey Bryantba1c0482012-08-14 16:43:43 -0400157 QLIST_ENTRY(MonFdset) next;
158};
159
Markus Armbruster74358f22015-03-06 19:35:59 +0100160typedef struct {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200161 JSONMessageParser parser;
Markus Armbrusterf994b252015-03-06 19:51:51 +0100162 /*
163 * When a client connects, we're in capabilities negotiation mode.
164 * When command qmp_capabilities succeeds, we go into command
165 * mode.
166 */
167 bool in_command_mode; /* are we in command mode? */
Markus Armbruster74358f22015-03-06 19:35:59 +0100168} MonitorQMP;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200169
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100170/*
171 * To prevent flooding clients, events can be throttled. The
172 * throttling is calculated globally, rather than per-Monitor
173 * instance.
174 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200175typedef struct MonitorQAPIEventState {
Markus Armbruster7de0be62015-10-15 17:08:35 +0200176 QAPIEvent event; /* Throttling state for this event type and... */
177 QDict *data; /* ... data, see qapi_event_throttle_equal() */
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100178 QEMUTimer *timer; /* Timer for handling delayed events */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200179 QDict *qdict; /* Delayed event (if any) */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200180} MonitorQAPIEventState;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100181
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200182typedef struct {
183 int64_t rate; /* Minimum time (in ns) between two events */
184} MonitorQAPIEventConf;
185
aliguori87127162009-03-05 23:01:29 +0000186struct Monitor {
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300187 CharBackend chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200188 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000189 int flags;
190 int suspend_cnt;
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400191 bool skip_flush;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200192
193 QemuMutex out_lock;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400194 QString *outbuf;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200195 guint out_watch;
196
197 /* Read under either BQL or out_lock, written with BQL+out_lock. */
198 int mux_out;
199
aliguori731b0362009-03-05 23:01:42 +0000200 ReadLineState *rs;
Markus Armbruster74358f22015-03-06 19:35:59 +0100201 MonitorQMP qmp;
Andreas Färbercb446ec2013-05-01 14:24:52 +0200202 CPUState *mon_cpu;
Markus Armbruster097310b2014-10-07 13:59:15 +0200203 BlockCompletionFunc *password_completion_cb;
aliguori731b0362009-03-05 23:01:42 +0000204 void *password_opaque;
Wenchao Xia77172392013-08-27 20:38:20 +0800205 mon_cmd_t *cmd_table;
Anthony Liguoric227f092009-10-01 16:12:16 -0500206 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000207 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000208};
209
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300210/* QMP checker flags */
211#define QMP_ACCEPT_UNKNOWNS 1
212
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200213/* Protects mon_list, monitor_event_state. */
214static QemuMutex monitor_lock;
215
Blue Swirl72cf2d42009-09-12 07:36:22 +0000216static QLIST_HEAD(mon_list, Monitor) mon_list;
Corey Bryantba1c0482012-08-14 16:43:43 -0400217static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
Corey Bryantefb87c12012-08-14 16:43:48 -0400218static int mon_refcount;
bellard7e2515e2004-08-01 21:52:19 +0000219
Wayne Xia816f8922011-10-12 11:32:41 +0800220static mon_cmd_t mon_cmds[];
221static mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000222
Markus Armbruster8631b602010-02-18 11:41:55 +0100223Monitor *cur_mon;
aliguori376253e2009-03-05 23:01:23 +0000224
Alberto Garciadc599972016-03-10 13:55:26 +0200225static QEMUClockType event_clock_type = QEMU_CLOCK_REALTIME;
226
Stefan Hajnoczic60bf332013-11-14 11:54:14 +0100227static void monitor_command_cb(void *opaque, const char *cmdline,
228 void *readline_opaque);
aliguori83ab7952008-08-19 14:44:22 +0000229
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100230/**
231 * Is @mon a QMP monitor?
232 */
233static inline bool monitor_is_qmp(const Monitor *mon)
Luiz Capitulino418173c2009-11-26 22:58:51 -0200234{
235 return (mon->flags & MONITOR_USE_CONTROL);
236}
237
Markus Armbruster489653b2015-03-06 20:01:05 +0100238/**
239 * Is the current monitor, if any, a QMP monitor?
240 */
241bool monitor_cur_is_qmp(void)
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100242{
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100243 return cur_mon && monitor_is_qmp(cur_mon);
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100244}
245
Anthony Liguori7060b472011-09-02 12:34:50 -0500246void monitor_read_command(Monitor *mon, int show_prompt)
aliguori731b0362009-03-05 23:01:42 +0000247{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200248 if (!mon->rs)
249 return;
250
aliguori731b0362009-03-05 23:01:42 +0000251 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
252 if (show_prompt)
253 readline_show_prompt(mon->rs);
254}
bellard6a00d602005-11-21 23:25:50 +0000255
Anthony Liguori7060b472011-09-02 12:34:50 -0500256int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
257 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000258{
Markus Armbrusterbcf5d192015-03-12 17:26:46 +0100259 if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000260 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
261 /* prompt is printed on return from the command handler */
262 return 0;
263 } else {
264 monitor_printf(mon, "terminal does not support password prompting\n");
265 return -ENOTTY;
266 }
aliguoribb5fc202009-03-05 23:01:15 +0000267}
268
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200269static void monitor_flush_locked(Monitor *mon);
270
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100271static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
272 void *opaque)
273{
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200274 Monitor *mon = opaque;
275
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200276 qemu_mutex_lock(&mon->out_lock);
277 mon->out_watch = 0;
278 monitor_flush_locked(mon);
279 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100280 return FALSE;
281}
282
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200283/* Called with mon->out_lock held. */
284static void monitor_flush_locked(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000285{
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100286 int rc;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400287 size_t len;
288 const char *buf;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100289
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400290 if (mon->skip_flush) {
291 return;
292 }
293
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400294 buf = qstring_get_str(mon->outbuf);
295 len = qstring_get_length(mon->outbuf);
296
Paolo Bonzinia4cc73d2013-05-31 14:00:27 +0200297 if (len && !mon->mux_out) {
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300298 rc = qemu_chr_fe_write(&mon->chr, (const uint8_t *) buf, len);
Stratos Psomadakis056f49f2014-01-27 12:30:15 +0200299 if ((rc < 0 && errno != EAGAIN) || (rc == len)) {
300 /* all flushed or error */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400301 QDECREF(mon->outbuf);
302 mon->outbuf = qstring_new();
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100303 return;
304 }
305 if (rc > 0) {
Eric Blake3b7c78c2016-05-17 16:00:15 -0600306 /* partial write */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400307 QString *tmp = qstring_from_str(buf + rc);
308 QDECREF(mon->outbuf);
309 mon->outbuf = tmp;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100310 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200311 if (mon->out_watch == 0) {
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300312 mon->out_watch =
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300313 qemu_chr_fe_add_watch(&mon->chr, G_IO_OUT | G_IO_HUP,
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300314 monitor_unblocked, mon);
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200315 }
bellard7e2515e2004-08-01 21:52:19 +0000316 }
317}
318
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200319void monitor_flush(Monitor *mon)
320{
321 qemu_mutex_lock(&mon->out_lock);
322 monitor_flush_locked(mon);
323 qemu_mutex_unlock(&mon->out_lock);
324}
325
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400326/* flush at every end of line */
aliguori376253e2009-03-05 23:01:23 +0000327static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000328{
ths60fe76f2007-12-16 03:02:09 +0000329 char c;
aliguori731b0362009-03-05 23:01:42 +0000330
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200331 qemu_mutex_lock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000332 for(;;) {
333 c = *str++;
334 if (c == '\0')
335 break;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400336 if (c == '\n') {
337 qstring_append_chr(mon->outbuf, '\r');
338 }
339 qstring_append_chr(mon->outbuf, c);
340 if (c == '\n') {
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200341 monitor_flush_locked(mon);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400342 }
bellard7e2515e2004-08-01 21:52:19 +0000343 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200344 qemu_mutex_unlock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000345}
346
aliguori376253e2009-03-05 23:01:23 +0000347void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000348{
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400349 char *buf;
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200350
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200351 if (!mon)
352 return;
353
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100354 if (monitor_is_qmp(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200355 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200356 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200357
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400358 buf = g_strdup_vprintf(fmt, ap);
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200359 monitor_puts(mon, buf);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400360 g_free(buf);
bellard7e2515e2004-08-01 21:52:19 +0000361}
362
aliguori376253e2009-03-05 23:01:23 +0000363void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000364{
365 va_list ap;
366 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000367 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000368 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000369}
370
Pavel Butsykincaf15312015-09-22 16:18:17 +0300371int monitor_fprintf(FILE *stream, const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000372{
373 va_list ap;
374 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000375 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000376 va_end(ap);
377 return 0;
378}
379
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200380static void monitor_json_emitter(Monitor *mon, const QObject *data)
381{
382 QString *json;
383
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200384 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
385 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200386 assert(json != NULL);
387
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200388 qstring_append_chr(json, '\n');
389 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200390
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200391 QDECREF(json);
392}
393
Eric Blake7fb1cf12015-11-18 01:52:57 -0700394static MonitorQAPIEventConf monitor_qapi_event_conf[QAPI_EVENT__MAX] = {
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200395 /* Limit guest-triggerable events to 1 per second */
396 [QAPI_EVENT_RTC_CHANGE] = { 1000 * SCALE_MS },
397 [QAPI_EVENT_WATCHDOG] = { 1000 * SCALE_MS },
398 [QAPI_EVENT_BALLOON_CHANGE] = { 1000 * SCALE_MS },
399 [QAPI_EVENT_QUORUM_REPORT_BAD] = { 1000 * SCALE_MS },
400 [QAPI_EVENT_QUORUM_FAILURE] = { 1000 * SCALE_MS },
401 [QAPI_EVENT_VSERPORT_CHANGE] = { 1000 * SCALE_MS },
402};
403
Markus Armbrustera24712a2015-10-15 17:08:34 +0200404GHashTable *monitor_qapi_event_state;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100405
406/*
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200407 * Emits the event to every monitor instance, @event is only used for trace
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200408 * Called with monitor_lock held.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100409 */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200410static void monitor_qapi_event_emit(QAPIEvent event, QDict *qdict)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100411{
412 Monitor *mon;
413
Markus Armbruster688b4b72015-10-15 17:08:30 +0200414 trace_monitor_protocol_event_emit(event, qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100415 QLIST_FOREACH(mon, &mon_list, entry) {
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100416 if (monitor_is_qmp(mon) && mon->qmp.in_command_mode) {
Markus Armbruster688b4b72015-10-15 17:08:30 +0200417 monitor_json_emitter(mon, QOBJECT(qdict));
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100418 }
419 }
420}
421
Markus Armbrustera24712a2015-10-15 17:08:34 +0200422static void monitor_qapi_event_handler(void *opaque);
423
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100424/*
425 * Queue a new event for emission to Monitor instances,
426 * applying any rate limiting if required.
427 */
428static void
Markus Armbruster688b4b72015-10-15 17:08:30 +0200429monitor_qapi_event_queue(QAPIEvent event, QDict *qdict, Error **errp)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100430{
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200431 MonitorQAPIEventConf *evconf;
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200432 MonitorQAPIEventState *evstate;
Markus Armbruster93f8f982015-10-15 17:08:31 +0200433
Eric Blake7fb1cf12015-11-18 01:52:57 -0700434 assert(event < QAPI_EVENT__MAX);
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200435 evconf = &monitor_qapi_event_conf[event];
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200436 trace_monitor_protocol_event_queue(event, qdict, evconf->rate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100437
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200438 qemu_mutex_lock(&monitor_lock);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200439
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200440 if (!evconf->rate) {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200441 /* Unthrottled event */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200442 monitor_qapi_event_emit(event, qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100443 } else {
Markus Armbruster7de0be62015-10-15 17:08:35 +0200444 QDict *data = qobject_to_qdict(qdict_get(qdict, "data"));
445 MonitorQAPIEventState key = { .event = event, .data = data };
Markus Armbrustera24712a2015-10-15 17:08:34 +0200446
447 evstate = g_hash_table_lookup(monitor_qapi_event_state, &key);
448 assert(!evstate || timer_pending(evstate->timer));
449
450 if (evstate) {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200451 /*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200452 * Timer is pending for (at least) evconf->rate ns after
Markus Armbruster93f8f982015-10-15 17:08:31 +0200453 * last send. Store event for sending when timer fires,
454 * replacing a prior stored event if any.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100455 */
Markus Armbruster93f8f982015-10-15 17:08:31 +0200456 QDECREF(evstate->qdict);
Markus Armbruster688b4b72015-10-15 17:08:30 +0200457 evstate->qdict = qdict;
458 QINCREF(evstate->qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100459 } else {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200460 /*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200461 * Last send was (at least) evconf->rate ns ago.
Markus Armbruster93f8f982015-10-15 17:08:31 +0200462 * Send immediately, and arm the timer to call
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200463 * monitor_qapi_event_handler() in evconf->rate ns. Any
Markus Armbruster93f8f982015-10-15 17:08:31 +0200464 * events arriving before then will be delayed until then.
465 */
Alberto Garciadc599972016-03-10 13:55:26 +0200466 int64_t now = qemu_clock_get_ns(event_clock_type);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200467
Markus Armbruster688b4b72015-10-15 17:08:30 +0200468 monitor_qapi_event_emit(event, qdict);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200469
470 evstate = g_new(MonitorQAPIEventState, 1);
471 evstate->event = event;
Markus Armbruster7de0be62015-10-15 17:08:35 +0200472 evstate->data = data;
473 QINCREF(evstate->data);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200474 evstate->qdict = NULL;
Alberto Garciadc599972016-03-10 13:55:26 +0200475 evstate->timer = timer_new_ns(event_clock_type,
Markus Armbrustera24712a2015-10-15 17:08:34 +0200476 monitor_qapi_event_handler,
477 evstate);
478 g_hash_table_add(monitor_qapi_event_state, evstate);
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200479 timer_mod_ns(evstate->timer, now + evconf->rate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100480 }
481 }
Markus Armbruster93f8f982015-10-15 17:08:31 +0200482
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200483 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100484}
485
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100486/*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200487 * This function runs evconf->rate ns after sending a throttled
Markus Armbruster93f8f982015-10-15 17:08:31 +0200488 * event.
489 * If another event has since been stored, send it.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100490 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200491static void monitor_qapi_event_handler(void *opaque)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100492{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200493 MonitorQAPIEventState *evstate = opaque;
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200494 MonitorQAPIEventConf *evconf = &monitor_qapi_event_conf[evstate->event];
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100495
Markus Armbruster93f8f982015-10-15 17:08:31 +0200496 trace_monitor_protocol_event_handler(evstate->event, evstate->qdict);
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200497 qemu_mutex_lock(&monitor_lock);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200498
Markus Armbruster688b4b72015-10-15 17:08:30 +0200499 if (evstate->qdict) {
Alberto Garciadc599972016-03-10 13:55:26 +0200500 int64_t now = qemu_clock_get_ns(event_clock_type);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200501
Markus Armbruster688b4b72015-10-15 17:08:30 +0200502 monitor_qapi_event_emit(evstate->event, evstate->qdict);
503 QDECREF(evstate->qdict);
504 evstate->qdict = NULL;
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200505 timer_mod_ns(evstate->timer, now + evconf->rate);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200506 } else {
507 g_hash_table_remove(monitor_qapi_event_state, evstate);
Markus Armbruster7de0be62015-10-15 17:08:35 +0200508 QDECREF(evstate->data);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200509 timer_free(evstate->timer);
510 g_free(evstate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100511 }
Markus Armbruster93f8f982015-10-15 17:08:31 +0200512
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200513 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100514}
515
Markus Armbrustera24712a2015-10-15 17:08:34 +0200516static unsigned int qapi_event_throttle_hash(const void *key)
517{
518 const MonitorQAPIEventState *evstate = key;
Markus Armbruster7de0be62015-10-15 17:08:35 +0200519 unsigned int hash = evstate->event * 255;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200520
Markus Armbruster7de0be62015-10-15 17:08:35 +0200521 if (evstate->event == QAPI_EVENT_VSERPORT_CHANGE) {
522 hash += g_str_hash(qdict_get_str(evstate->data, "id"));
523 }
524
Alberto Garcia6d425eb2016-03-10 13:55:25 +0200525 if (evstate->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
526 hash += g_str_hash(qdict_get_str(evstate->data, "node-name"));
527 }
528
Markus Armbruster7de0be62015-10-15 17:08:35 +0200529 return hash;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200530}
531
532static gboolean qapi_event_throttle_equal(const void *a, const void *b)
533{
534 const MonitorQAPIEventState *eva = a;
535 const MonitorQAPIEventState *evb = b;
536
Markus Armbruster7de0be62015-10-15 17:08:35 +0200537 if (eva->event != evb->event) {
538 return FALSE;
539 }
540
541 if (eva->event == QAPI_EVENT_VSERPORT_CHANGE) {
542 return !strcmp(qdict_get_str(eva->data, "id"),
543 qdict_get_str(evb->data, "id"));
544 }
545
Alberto Garcia6d425eb2016-03-10 13:55:25 +0200546 if (eva->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
547 return !strcmp(qdict_get_str(eva->data, "node-name"),
548 qdict_get_str(evb->data, "node-name"));
549 }
550
Markus Armbruster7de0be62015-10-15 17:08:35 +0200551 return TRUE;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200552}
553
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200554static void monitor_qapi_event_init(void)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100555{
Alberto Garciadc599972016-03-10 13:55:26 +0200556 if (qtest_enabled()) {
557 event_clock_type = QEMU_CLOCK_VIRTUAL;
558 }
559
Markus Armbrustera24712a2015-10-15 17:08:34 +0200560 monitor_qapi_event_state = g_hash_table_new(qapi_event_throttle_hash,
561 qapi_event_throttle_equal);
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200562 qmp_event_set_func_emit(monitor_qapi_event_queue);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200563}
564
Marc-André Lureau119ebac2016-09-12 13:18:57 +0400565void qmp_qmp_capabilities(Error **errp)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200566{
Markus Armbruster485febc2015-03-13 17:25:50 +0100567 cur_mon->qmp.in_command_mode = true;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200568}
569
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100570static void handle_hmp_command(Monitor *mon, const char *cmdline);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200571
Wenchao Xiab01fe892013-08-27 20:38:19 +0800572static void monitor_data_init(Monitor *mon)
573{
574 memset(mon, 0, sizeof(Monitor));
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200575 qemu_mutex_init(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800576 mon->outbuf = qstring_new();
Wenchao Xia77172392013-08-27 20:38:20 +0800577 /* Use *mon_cmds by default. */
578 mon->cmd_table = mon_cmds;
Wenchao Xiab01fe892013-08-27 20:38:19 +0800579}
580
581static void monitor_data_destroy(Monitor *mon)
582{
Marc-André Lureauc39860e2016-10-22 12:52:58 +0300583 qemu_chr_fe_deinit(&mon->chr);
Marc-André Lureau2ef45712016-08-01 15:23:42 +0400584 if (monitor_is_qmp(mon)) {
585 json_message_parser_destroy(&mon->qmp.parser);
586 }
587 g_free(mon->rs);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800588 QDECREF(mon->outbuf);
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200589 qemu_mutex_destroy(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800590}
591
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200592char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
593 int64_t cpu_index, Error **errp)
Luiz Capitulino0268d972010-10-22 10:08:02 -0200594{
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200595 char *output = NULL;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200596 Monitor *old_mon, hmp;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200597
Wenchao Xiab01fe892013-08-27 20:38:19 +0800598 monitor_data_init(&hmp);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400599 hmp.skip_flush = true;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200600
601 old_mon = cur_mon;
602 cur_mon = &hmp;
603
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200604 if (has_cpu_index) {
605 int ret = monitor_set_cpu(cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200606 if (ret < 0) {
607 cur_mon = old_mon;
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100608 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
609 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200610 goto out;
611 }
612 }
613
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100614 handle_hmp_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200615 cur_mon = old_mon;
616
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200617 qemu_mutex_lock(&hmp.out_lock);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400618 if (qstring_get_length(hmp.outbuf) > 0) {
619 output = g_strdup(qstring_get_str(hmp.outbuf));
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200620 } else {
621 output = g_strdup("");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200622 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200623 qemu_mutex_unlock(&hmp.out_lock);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200624
625out:
Wenchao Xiab01fe892013-08-27 20:38:19 +0800626 monitor_data_destroy(&hmp);
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200627 return output;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200628}
629
bellard9dc39cb2004-03-14 21:38:27 +0000630static int compare_cmd(const char *name, const char *list)
631{
632 const char *p, *pstart;
633 int len;
634 len = strlen(name);
635 p = list;
636 for(;;) {
637 pstart = p;
638 p = strchr(p, '|');
639 if (!p)
640 p = pstart + strlen(pstart);
641 if ((p - pstart) == len && !memcmp(pstart, name, len))
642 return 1;
643 if (*p == '\0')
644 break;
645 p++;
646 }
647 return 0;
648}
649
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800650static int get_str(char *buf, int buf_size, const char **pp)
651{
652 const char *p;
653 char *q;
654 int c;
655
656 q = buf;
657 p = *pp;
658 while (qemu_isspace(*p)) {
659 p++;
660 }
661 if (*p == '\0') {
662 fail:
663 *q = '\0';
664 *pp = p;
665 return -1;
666 }
667 if (*p == '\"') {
668 p++;
669 while (*p != '\0' && *p != '\"') {
670 if (*p == '\\') {
671 p++;
672 c = *p++;
673 switch (c) {
674 case 'n':
675 c = '\n';
676 break;
677 case 'r':
678 c = '\r';
679 break;
680 case '\\':
681 case '\'':
682 case '\"':
683 break;
684 default:
Peter Maydell71baf782015-08-19 16:20:19 +0100685 printf("unsupported escape code: '\\%c'\n", c);
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800686 goto fail;
687 }
688 if ((q - buf) < buf_size - 1) {
689 *q++ = c;
690 }
691 } else {
692 if ((q - buf) < buf_size - 1) {
693 *q++ = *p;
694 }
695 p++;
696 }
697 }
698 if (*p != '\"') {
Peter Maydell71baf782015-08-19 16:20:19 +0100699 printf("unterminated string\n");
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800700 goto fail;
701 }
702 p++;
703 } else {
704 while (*p != '\0' && !qemu_isspace(*p)) {
705 if ((q - buf) < buf_size - 1) {
706 *q++ = *p;
707 }
708 p++;
709 }
710 }
711 *q = '\0';
712 *pp = p;
713 return 0;
714}
715
716#define MAX_ARGS 16
717
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800718static void free_cmdline_args(char **args, int nb_args)
719{
720 int i;
721
722 assert(nb_args <= MAX_ARGS);
723
724 for (i = 0; i < nb_args; i++) {
725 g_free(args[i]);
726 }
727
728}
729
730/*
731 * Parse the command line to get valid args.
732 * @cmdline: command line to be parsed.
733 * @pnb_args: location to store the number of args, must NOT be NULL.
734 * @args: location to store the args, which should be freed by caller, must
735 * NOT be NULL.
736 *
737 * Returns 0 on success, negative on failure.
738 *
739 * NOTE: this parser is an approximate form of the real command parser. Number
740 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
741 * return with failure.
742 */
743static int parse_cmdline(const char *cmdline,
744 int *pnb_args, char **args)
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800745{
746 const char *p;
747 int nb_args, ret;
748 char buf[1024];
749
750 p = cmdline;
751 nb_args = 0;
752 for (;;) {
753 while (qemu_isspace(*p)) {
754 p++;
755 }
756 if (*p == '\0') {
757 break;
758 }
759 if (nb_args >= MAX_ARGS) {
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800760 goto fail;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800761 }
762 ret = get_str(buf, sizeof(buf), &p);
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800763 if (ret < 0) {
764 goto fail;
765 }
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800766 args[nb_args] = g_strdup(buf);
767 nb_args++;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800768 }
769 *pnb_args = nb_args;
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800770 return 0;
771
772 fail:
773 free_cmdline_args(args, nb_args);
774 return -1;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800775}
776
Wenchao Xia66855492013-08-27 20:38:23 +0800777static void help_cmd_dump_one(Monitor *mon,
778 const mon_cmd_t *cmd,
779 char **prefix_args,
780 int prefix_args_nb)
781{
782 int i;
783
784 for (i = 0; i < prefix_args_nb; i++) {
785 monitor_printf(mon, "%s ", prefix_args[i]);
786 }
787 monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
788}
789
790/* @args[@arg_index] is the valid command need to find in @cmds */
Anthony Liguoric227f092009-10-01 16:12:16 -0500791static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
Wenchao Xia66855492013-08-27 20:38:23 +0800792 char **args, int nb_args, int arg_index)
bellard9dc39cb2004-03-14 21:38:27 +0000793{
Anthony Liguoric227f092009-10-01 16:12:16 -0500794 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000795
Wenchao Xia66855492013-08-27 20:38:23 +0800796 /* No valid arg need to compare with, dump all in *cmds */
797 if (arg_index >= nb_args) {
798 for (cmd = cmds; cmd->name != NULL; cmd++) {
799 help_cmd_dump_one(mon, cmd, args, arg_index);
800 }
801 return;
802 }
803
804 /* Find one entry to dump */
805 for (cmd = cmds; cmd->name != NULL; cmd++) {
806 if (compare_cmd(args[arg_index], cmd->name)) {
807 if (cmd->sub_table) {
808 /* continue with next arg */
809 help_cmd_dump(mon, cmd->sub_table,
810 args, nb_args, arg_index + 1);
811 } else {
812 help_cmd_dump_one(mon, cmd, args, arg_index);
813 }
814 break;
815 }
bellard9dc39cb2004-03-14 21:38:27 +0000816 }
817}
818
aliguori376253e2009-03-05 23:01:23 +0000819static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000820{
Wenchao Xia66855492013-08-27 20:38:23 +0800821 char *args[MAX_ARGS];
822 int nb_args = 0;
823
824 /* 1. parse user input */
825 if (name) {
826 /* special case for log, directly dump and return */
827 if (!strcmp(name, "log")) {
Peter Maydell38dad9e2013-02-11 16:41:25 +0000828 const QEMULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000829 monitor_printf(mon, "Log items (comma separated):\n");
830 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
Peter Maydell38dad9e2013-02-11 16:41:25 +0000831 for (item = qemu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000832 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000833 }
Wenchao Xia66855492013-08-27 20:38:23 +0800834 return;
835 }
836
837 if (parse_cmdline(name, &nb_args, args) < 0) {
838 return;
bellardf193c792004-03-21 17:06:25 +0000839 }
bellard9dc39cb2004-03-14 21:38:27 +0000840 }
Wenchao Xia66855492013-08-27 20:38:23 +0800841
842 /* 2. dump the contents according to parsed args */
843 help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0);
844
845 free_cmdline_args(args, nb_args);
bellard9dc39cb2004-03-14 21:38:27 +0000846}
847
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300848static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300849{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300850 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300851}
852
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100853static void hmp_trace_event(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530854{
855 const char *tp_name = qdict_get_str(qdict, "name");
856 bool new_state = qdict_get_bool(qdict, "option");
Lluís Vilanova77e2b172016-07-11 12:53:57 +0200857 bool has_vcpu = qdict_haskey(qdict, "vcpu");
858 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
Lluís Vilanova14101d02014-08-25 13:20:03 +0200859 Error *local_err = NULL;
Blue Swirlf871d682010-10-13 19:14:29 +0000860
Lluís Vilanova77e2b172016-07-11 12:53:57 +0200861 if (vcpu < 0) {
862 monitor_printf(mon, "argument vcpu must be positive");
863 return;
864 }
865
866 qmp_trace_event_set_state(tp_name, new_state, true, true, has_vcpu, vcpu, &local_err);
Lluís Vilanova14101d02014-08-25 13:20:03 +0200867 if (local_err) {
Markus Armbruster091e38b2015-02-10 15:15:43 +0100868 error_report_err(local_err);
Blue Swirlf871d682010-10-13 19:14:29 +0000869 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530870}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100871
Michael Rothc45a8162011-10-02 08:44:37 -0500872#ifdef CONFIG_TRACE_SIMPLE
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100873static void hmp_trace_file(Monitor *mon, const QDict *qdict)
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100874{
875 const char *op = qdict_get_try_str(qdict, "op");
876 const char *arg = qdict_get_try_str(qdict, "arg");
877
878 if (!op) {
879 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
880 } else if (!strcmp(op, "on")) {
881 st_set_trace_file_enabled(true);
882 } else if (!strcmp(op, "off")) {
883 st_set_trace_file_enabled(false);
884 } else if (!strcmp(op, "flush")) {
885 st_flush_trace_buffer();
886 } else if (!strcmp(op, "set")) {
887 if (arg) {
888 st_set_trace_file(arg);
889 }
890 } else {
891 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
892 help_cmd(mon, "trace-file");
893 }
894}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530895#endif
896
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100897static void hmp_info_help(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000898{
Luiz Capitulino13c74252009-10-07 13:41:55 -0300899 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000900}
901
Marc-André Lureau9e812b62016-09-12 13:19:05 +0400902static void query_commands_cb(QmpCommand *cmd, void *opaque)
bellard9bc9d1c2004-10-10 15:15:51 +0000903{
Marc-André Lureau9e812b62016-09-12 13:19:05 +0400904 CommandInfoList *info, **list = opaque;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200905
Marc-André Lureau9e812b62016-09-12 13:19:05 +0400906 if (!cmd->enabled) {
907 return;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200908 }
909
Marc-André Lureau9e812b62016-09-12 13:19:05 +0400910 info = g_malloc0(sizeof(*info));
911 info->value = g_malloc0(sizeof(*info->value));
912 info->value->name = g_strdup(cmd->name);
913 info->next = *list;
914 *list = info;
915}
916
917CommandInfoList *qmp_query_commands(Error **errp)
918{
919 CommandInfoList *list = NULL;
920
921 qmp_for_each_command(query_commands_cb, &list);
922
923 return list;
thsa36e69d2007-12-02 05:18:19 +0000924}
925
Daniel P. Berrange48608532012-05-21 17:59:51 +0100926EventInfoList *qmp_query_events(Error **errp)
927{
928 EventInfoList *info, *ev_list = NULL;
Wenchao Xia75175172014-06-18 08:43:54 +0200929 QAPIEvent e;
Daniel P. Berrange48608532012-05-21 17:59:51 +0100930
Eric Blake7fb1cf12015-11-18 01:52:57 -0700931 for (e = 0 ; e < QAPI_EVENT__MAX ; e++) {
Wenchao Xia75175172014-06-18 08:43:54 +0200932 const char *event_name = QAPIEvent_lookup[e];
Daniel P. Berrange48608532012-05-21 17:59:51 +0100933 assert(event_name != NULL);
934 info = g_malloc0(sizeof(*info));
935 info->value = g_malloc0(sizeof(*info->value));
936 info->value->name = g_strdup(event_name);
937
938 info->next = ev_list;
939 ev_list = info;
940 }
941
942 return ev_list;
943}
944
Markus Armbruster39a18152015-09-16 13:06:28 +0200945/*
946 * Minor hack: generated marshalling suppressed for this command
947 * ('gen': false in the schema) so we can parse the JSON string
948 * directly into QObject instead of first parsing it with
949 * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
950 * to QObject with generated output marshallers, every time. Instead,
Daniel P. Berrangeb3db2112016-09-30 15:45:27 +0100951 * we do it in test-qobject-input-visitor.c, just to make sure
Markus Armbruster39a18152015-09-16 13:06:28 +0200952 * qapi-introspect.py's output actually conforms to the schema.
953 */
954static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
955 Error **errp)
956{
957 *ret_data = qobject_from_json(qmp_schema_json);
958}
959
Marc-André Lureau5032a162016-09-12 13:19:02 +0400960/*
Marc-André Lureau5032a162016-09-12 13:19:02 +0400961 * We used to define commands in qmp-commands.hx in addition to the
962 * QAPI schema. This permitted defining some of them only in certain
963 * configurations. query-commands has always reflected that (good,
964 * because it lets QMP clients figure out what's actually available),
965 * while query-qmp-schema never did (not so good). This function is a
966 * hack to keep the configuration-specific commands defined exactly as
967 * before, even though qmp-commands.hx is gone.
968 *
969 * FIXME Educate the QAPI schema on configuration-specific commands,
970 * and drop this hack.
971 */
972static void qmp_unregister_commands_hack(void)
973{
974#ifndef CONFIG_SPICE
975 qmp_unregister_command("query-spice");
976#endif
977#ifndef TARGET_I386
978 qmp_unregister_command("rtc-reset-reinjection");
979#endif
980#ifndef TARGET_S390X
981 qmp_unregister_command("dump-skeys");
982#endif
983#ifndef TARGET_ARM
984 qmp_unregister_command("query-gic-capabilities");
985#endif
Eduardo Habkost728b1422016-10-05 16:49:01 -0300986#if !defined(TARGET_S390X)
987 qmp_unregister_command("query-cpu-model-expansion");
988 qmp_unregister_command("query-cpu-model-baseline");
989 qmp_unregister_command("query-cpu-model-comparison");
990#endif
991#if !defined(TARGET_PPC) && !defined(TARGET_ARM) && !defined(TARGET_I386) \
992 && !defined(TARGET_S390X)
993 qmp_unregister_command("query-cpu-definitions");
994#endif
Marc-André Lureau5032a162016-09-12 13:19:02 +0400995}
996
Marc-André Lureauedcfaef2016-09-12 13:19:00 +0400997static void qmp_init_marshal(void)
998{
999 qmp_register_command("query-qmp-schema", qmp_query_qmp_schema,
1000 QCO_NO_OPTIONS);
1001 qmp_register_command("device_add", qmp_device_add,
1002 QCO_NO_OPTIONS);
1003 qmp_register_command("netdev_add", qmp_netdev_add,
1004 QCO_NO_OPTIONS);
Marc-André Lureau5032a162016-09-12 13:19:02 +04001005
1006 /* call it after the rest of qapi_init() */
1007 register_module_init(qmp_unregister_commands_hack, MODULE_INIT_QAPI);
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001008}
1009
1010qapi_init(qmp_init_marshal);
1011
Luiz Capitulinob025c8b2011-10-06 14:02:57 -03001012/* set the current CPU defined by the user */
1013int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +00001014{
Andreas Färber55e5c282012-12-17 06:18:02 +01001015 CPUState *cpu;
bellard6a00d602005-11-21 23:25:50 +00001016
Andreas Färber1c8bb3c2013-02-15 17:01:09 +01001017 cpu = qemu_get_cpu(cpu_index);
1018 if (cpu == NULL) {
1019 return -1;
bellard6a00d602005-11-21 23:25:50 +00001020 }
Andreas Färbercb446ec2013-05-01 14:24:52 +02001021 cur_mon->mon_cpu = cpu;
Andreas Färber1c8bb3c2013-02-15 17:01:09 +01001022 return 0;
bellard6a00d602005-11-21 23:25:50 +00001023}
1024
Pavel Butsykincaf15312015-09-22 16:18:17 +03001025CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +00001026{
aliguori731b0362009-03-05 23:01:42 +00001027 if (!cur_mon->mon_cpu) {
David Gibson27a83f82016-09-21 15:29:26 +10001028 monitor_set_cpu(first_cpu->cpu_index);
bellard6a00d602005-11-21 23:25:50 +00001029 }
Avi Kivity4c0960c2009-08-17 23:19:53 +03001030 cpu_synchronize_state(cur_mon->mon_cpu);
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001031 return cur_mon->mon_cpu;
1032}
1033
Pavel Butsykinbf957282015-09-10 18:38:59 +03001034CPUArchState *mon_get_cpu_env(void)
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001035{
1036 return mon_get_cpu()->env_ptr;
bellard6a00d602005-11-21 23:25:50 +00001037}
1038
Luiz Capitulino99b77962011-10-24 10:53:44 -02001039int monitor_get_cpu_index(void)
1040{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001041 return mon_get_cpu()->cpu_index;
Luiz Capitulino99b77962011-10-24 10:53:44 -02001042}
1043
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001044static void hmp_info_registers(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001045{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001046 cpu_dump_state(mon_get_cpu(), (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +00001047}
1048
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001049static void hmp_info_jit(Monitor *mon, const QDict *qdict)
bellarde3db7222005-01-26 22:00:47 +00001050{
aliguori376253e2009-03-05 23:01:23 +00001051 dump_exec_info((FILE *)mon, monitor_fprintf);
Sebastian Tanase27498be2014-07-25 11:56:33 +02001052 dump_drift_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +00001053}
1054
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001055static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
Max Filippov246ae242014-11-02 11:04:18 +03001056{
1057 dump_opcount_info((FILE *)mon, monitor_fprintf);
1058}
1059
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001060static void hmp_info_history(Monitor *mon, const QDict *qdict)
bellardaa455482004-04-04 13:07:25 +00001061{
1062 int i;
bellard7e2515e2004-08-01 21:52:19 +00001063 const char *str;
ths3b46e622007-09-17 08:09:54 +00001064
aliguoricde76ee2009-03-05 23:01:51 +00001065 if (!mon->rs)
1066 return;
bellard7e2515e2004-08-01 21:52:19 +00001067 i = 0;
1068 for(;;) {
aliguori731b0362009-03-05 23:01:42 +00001069 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +00001070 if (!str)
1071 break;
aliguori376253e2009-03-05 23:01:23 +00001072 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +00001073 i++;
bellardaa455482004-04-04 13:07:25 +00001074 }
1075}
1076
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001077static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
j_mayer76a66252007-03-07 08:32:30 +00001078{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001079 cpu_dump_statistics(mon_get_cpu(), (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +00001080}
j_mayer76a66252007-03-07 08:32:30 +00001081
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001082static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301083{
Lluís Vilanovabd712112016-07-11 12:53:51 +02001084 const char *name = qdict_get_try_str(qdict, "name");
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001085 bool has_vcpu = qdict_haskey(qdict, "vcpu");
1086 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
Lluís Vilanovabd712112016-07-11 12:53:51 +02001087 TraceEventInfoList *events;
Lluís Vilanova14101d02014-08-25 13:20:03 +02001088 TraceEventInfoList *elem;
Lluís Vilanovabd712112016-07-11 12:53:51 +02001089 Error *local_err = NULL;
1090
1091 if (name == NULL) {
1092 name = "*";
1093 }
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001094 if (vcpu < 0) {
1095 monitor_printf(mon, "argument vcpu must be positive");
1096 return;
1097 }
Lluís Vilanovabd712112016-07-11 12:53:51 +02001098
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001099 events = qmp_trace_event_get_state(name, has_vcpu, vcpu, &local_err);
Lluís Vilanovabd712112016-07-11 12:53:51 +02001100 if (local_err) {
1101 error_report_err(local_err);
1102 return;
1103 }
Lluís Vilanova14101d02014-08-25 13:20:03 +02001104
1105 for (elem = events; elem != NULL; elem = elem->next) {
1106 monitor_printf(mon, "%s : state %u\n",
1107 elem->value->name,
1108 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
1109 }
1110 qapi_free_TraceEventInfoList(events);
Prerna Saxena22890ab2010-06-24 17:04:53 +05301111}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301112
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001113void qmp_client_migrate_info(const char *protocol, const char *hostname,
1114 bool has_port, int64_t port,
1115 bool has_tls_port, int64_t tls_port,
1116 bool has_cert_subject, const char *cert_subject,
1117 Error **errp)
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001118{
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001119 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001120 if (!qemu_using_spice(errp)) {
1121 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001122 }
1123
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001124 if (!has_port && !has_tls_port) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001125 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001126 return;
Yonit Halperin6ec5dae2012-03-18 09:42:39 +02001127 }
1128
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001129 if (qemu_spice_migrate_info(hostname,
1130 has_port ? port : -1,
1131 has_tls_port ? tls_port : -1,
1132 cert_subject)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001133 error_setg(errp, QERR_UNDEFINED_ERROR);
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001134 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001135 }
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001136 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001137 }
1138
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001139 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001140}
1141
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001142static void hmp_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001143{
Markus Armbrusterdaa76aa2016-06-15 19:27:16 +02001144 Error *err = NULL;
1145
1146 qemu_set_log_filename(qdict_get_str(qdict, "filename"), &err);
1147 if (err) {
1148 error_report_err(err);
1149 }
pbrooke735b912007-06-30 13:53:24 +00001150}
1151
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001152static void hmp_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001153{
1154 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001155 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001156
bellard9307c4c2004-04-04 12:57:25 +00001157 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001158 mask = 0;
1159 } else {
Peter Maydell4fde1eb2013-02-11 16:41:22 +00001160 mask = qemu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001161 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001162 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001163 return;
1164 }
1165 }
Peter Maydell24537a02013-02-11 16:41:23 +00001166 qemu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00001167}
1168
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001169static void hmp_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001170{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001171 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001172 if (!option || !strcmp(option, "on")) {
1173 singlestep = 1;
1174 } else if (!strcmp(option, "off")) {
1175 singlestep = 0;
1176 } else {
1177 monitor_printf(mon, "unexpected option %s\n", option);
1178 }
1179}
1180
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001181static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001182{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001183 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001184 if (!device)
1185 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1186 if (gdbserver_start(device) < 0) {
1187 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1188 device);
1189 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001190 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001191 } else {
aliguori59030a82009-04-05 18:43:41 +00001192 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1193 device);
bellard8a7ddc32004-03-31 19:00:16 +00001194 }
1195}
1196
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001197static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001198{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001199 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001200 if (select_watchdog_action(action) == -1) {
1201 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1202 }
1203}
1204
aliguori376253e2009-03-05 23:01:23 +00001205static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001206{
aliguori376253e2009-03-05 23:01:23 +00001207 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001208 switch(c) {
1209 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001210 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001211 break;
1212 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001213 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001214 break;
1215 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001216 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001217 break;
1218 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001219 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001220 break;
1221 default:
1222 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001223 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001224 } else {
aliguori376253e2009-03-05 23:01:23 +00001225 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001226 }
1227 break;
1228 }
aliguori376253e2009-03-05 23:01:23 +00001229 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001230}
1231
aliguori376253e2009-03-05 23:01:23 +00001232static void memory_dump(Monitor *mon, int count, int format, int wsize,
Avi Kivitya8170e52012-10-23 12:30:10 +02001233 hwaddr addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001234{
Blue Swirl23842aa2010-01-12 20:27:43 +00001235 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001236 uint8_t buf[16];
1237 uint64_t v;
1238
1239 if (format == 'i') {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001240 int flags = 0;
bellard9307c4c2004-04-04 12:57:25 +00001241#ifdef TARGET_I386
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001242 CPUArchState *env = mon_get_cpu_env();
bellard4c27ba22004-04-25 18:05:08 +00001243 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001244 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001245 } else if (wsize == 4) {
1246 flags = 0;
1247 } else {
bellard6a15fd12006-04-12 21:07:07 +00001248 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001249 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001250 if (env) {
1251#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001252 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001253 (env->segs[R_CS].flags & DESC_L_MASK))
1254 flags = 2;
1255 else
1256#endif
1257 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1258 flags = 1;
1259 }
bellard4c27ba22004-04-25 18:05:08 +00001260 }
1261#endif
Tom Musta1c38f842014-04-09 14:53:24 -05001262#ifdef TARGET_PPC
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001263 CPUArchState *env = mon_get_cpu_env();
Tom Musta1c38f842014-04-09 14:53:24 -05001264 flags = msr_le << 16;
1265 flags |= env->bfd_mach;
1266#endif
Peter Crosthwaited49190c2015-05-24 14:20:41 -07001267 monitor_disas(mon, mon_get_cpu(), addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001268 return;
1269 }
1270
1271 len = wsize * count;
1272 if (wsize == 1)
1273 line_size = 8;
1274 else
1275 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001276 max_digits = 0;
1277
1278 switch(format) {
1279 case 'o':
1280 max_digits = (wsize * 8 + 2) / 3;
1281 break;
1282 default:
1283 case 'x':
1284 max_digits = (wsize * 8) / 4;
1285 break;
1286 case 'u':
1287 case 'd':
1288 max_digits = (wsize * 8 * 10 + 32) / 33;
1289 break;
1290 case 'c':
1291 wsize = 1;
1292 break;
1293 }
1294
1295 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001296 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001297 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001298 else
aliguori376253e2009-03-05 23:01:23 +00001299 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001300 l = len;
1301 if (l > line_size)
1302 l = line_size;
1303 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001304 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001305 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001306 if (cpu_memory_rw_debug(mon_get_cpu(), addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001307 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001308 break;
1309 }
bellard9307c4c2004-04-04 12:57:25 +00001310 }
ths5fafdf22007-09-16 21:08:06 +00001311 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001312 while (i < l) {
1313 switch(wsize) {
1314 default:
1315 case 1:
Peter Maydell24e60302015-01-20 15:19:32 +00001316 v = ldub_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001317 break;
1318 case 2:
Peter Maydell24e60302015-01-20 15:19:32 +00001319 v = lduw_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001320 break;
1321 case 4:
Peter Maydell24e60302015-01-20 15:19:32 +00001322 v = (uint32_t)ldl_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001323 break;
1324 case 8:
Peter Maydell24e60302015-01-20 15:19:32 +00001325 v = ldq_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001326 break;
1327 }
aliguori376253e2009-03-05 23:01:23 +00001328 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001329 switch(format) {
1330 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001331 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001332 break;
1333 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001334 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001335 break;
1336 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001337 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001338 break;
1339 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001340 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001341 break;
1342 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001343 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001344 break;
1345 }
1346 i += wsize;
1347 }
aliguori376253e2009-03-05 23:01:23 +00001348 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001349 addr += l;
1350 len -= l;
1351 }
1352}
1353
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001354static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001355{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001356 int count = qdict_get_int(qdict, "count");
1357 int format = qdict_get_int(qdict, "format");
1358 int size = qdict_get_int(qdict, "size");
1359 target_long addr = qdict_get_int(qdict, "addr");
1360
aliguori376253e2009-03-05 23:01:23 +00001361 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001362}
1363
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001364static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001365{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001366 int count = qdict_get_int(qdict, "count");
1367 int format = qdict_get_int(qdict, "format");
1368 int size = qdict_get_int(qdict, "size");
Avi Kivitya8170e52012-10-23 12:30:10 +02001369 hwaddr addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001370
aliguori376253e2009-03-05 23:01:23 +00001371 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001372}
1373
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001374static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001375{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001376 int format = qdict_get_int(qdict, "format");
Avi Kivitya8170e52012-10-23 12:30:10 +02001377 hwaddr val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001378
bellard9307c4c2004-04-04 12:57:25 +00001379 switch(format) {
1380 case 'o':
Avi Kivitya8170e52012-10-23 12:30:10 +02001381 monitor_printf(mon, "%#" HWADDR_PRIo, val);
bellard9307c4c2004-04-04 12:57:25 +00001382 break;
1383 case 'x':
Avi Kivitya8170e52012-10-23 12:30:10 +02001384 monitor_printf(mon, "%#" HWADDR_PRIx, val);
bellard9307c4c2004-04-04 12:57:25 +00001385 break;
1386 case 'u':
Avi Kivitya8170e52012-10-23 12:30:10 +02001387 monitor_printf(mon, "%" HWADDR_PRIu, val);
bellard9307c4c2004-04-04 12:57:25 +00001388 break;
1389 default:
1390 case 'd':
Avi Kivitya8170e52012-10-23 12:30:10 +02001391 monitor_printf(mon, "%" HWADDR_PRId, val);
bellard9307c4c2004-04-04 12:57:25 +00001392 break;
1393 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001394 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001395 break;
1396 }
aliguori376253e2009-03-05 23:01:23 +00001397 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001398}
1399
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001400static void hmp_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001401{
1402 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001403 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001404 uint32_t start = qdict_get_int(qdict, "start");
1405 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001406
1407 sum = 0;
1408 for(addr = start; addr < (start + size); addr++) {
Peter Maydell42874d32015-04-26 16:49:24 +01001409 uint8_t val = address_space_ldub(&address_space_memory, addr,
1410 MEMTXATTRS_UNSPECIFIED, NULL);
bellarde4cf1ad2005-06-04 20:15:57 +00001411 /* BSD sum algorithm ('sum' Unix command) */
1412 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001413 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001414 }
aliguori376253e2009-03-05 23:01:23 +00001415 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001416}
1417
bellard13224a82006-07-14 22:03:35 +00001418static int mouse_button_state;
1419
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001420static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001421{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001422 int dx, dy, dz, button;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001423 const char *dx_str = qdict_get_str(qdict, "dx_str");
1424 const char *dy_str = qdict_get_str(qdict, "dy_str");
1425 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001426
bellard13224a82006-07-14 22:03:35 +00001427 dx = strtol(dx_str, NULL, 0);
1428 dy = strtol(dy_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001429 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
1430 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
1431
1432 if (dz_str) {
bellard13224a82006-07-14 22:03:35 +00001433 dz = strtol(dz_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001434 if (dz != 0) {
Gerd Hoffmannf22d0af2016-01-12 12:14:12 +01001435 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001436 qemu_input_queue_btn(NULL, button, true);
1437 qemu_input_event_sync();
1438 qemu_input_queue_btn(NULL, button, false);
1439 }
1440 }
1441 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001442}
1443
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001444static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001445{
Eric Blake7fb1cf12015-11-18 01:52:57 -07001446 static uint32_t bmap[INPUT_BUTTON__MAX] = {
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001447 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1448 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
1449 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1450 };
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001451 int button_state = qdict_get_int(qdict, "button_state");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001452
1453 if (mouse_button_state == button_state) {
1454 return;
1455 }
1456 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1457 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001458 mouse_button_state = button_state;
bellard13224a82006-07-14 22:03:35 +00001459}
1460
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001461static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001462{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001463 int size = qdict_get_int(qdict, "size");
1464 int addr = qdict_get_int(qdict, "addr");
1465 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001466 uint32_t val;
1467 int suffix;
1468
1469 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001470 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001471 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001472 addr++;
1473 }
1474 addr &= 0xffff;
1475
1476 switch(size) {
1477 default:
1478 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001479 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001480 suffix = 'b';
1481 break;
1482 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001483 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001484 suffix = 'w';
1485 break;
1486 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001487 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001488 suffix = 'l';
1489 break;
1490 }
aliguori376253e2009-03-05 23:01:23 +00001491 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1492 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001493}
bellarda3a91a32004-06-04 11:06:21 +00001494
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001495static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001496{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001497 int size = qdict_get_int(qdict, "size");
1498 int addr = qdict_get_int(qdict, "addr");
1499 int val = qdict_get_int(qdict, "val");
1500
Jan Kiszkaf1147842009-07-14 10:20:11 +02001501 addr &= IOPORTS_MASK;
1502
1503 switch (size) {
1504 default:
1505 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001506 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001507 break;
1508 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001509 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001510 break;
1511 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001512 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001513 break;
1514 }
1515}
1516
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001517static void hmp_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001518{
Gongleif1839932014-12-03 18:20:58 +00001519 Error *local_err = NULL;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001520 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001521
Gongleif1839932014-12-03 18:20:58 +00001522 qemu_boot_set(bootdevice, &local_err);
1523 if (local_err) {
Markus Armbruster193227f2015-12-18 16:35:06 +01001524 error_report_err(local_err);
aurel320ecdffb2008-05-04 20:11:34 +00001525 } else {
Gongleif1839932014-12-03 18:20:58 +00001526 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001527 }
1528}
1529
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001530static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
Blue Swirl314e2982011-09-11 20:22:05 +00001531{
1532 mtree_info((fprintf_function)monitor_printf, mon);
1533}
1534
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001535static void hmp_info_numa(Monitor *mon, const QDict *qdict)
aliguori030ea372009-04-21 22:30:47 +00001536{
aliguorib28b6232009-04-22 20:20:29 +00001537 int i;
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001538 CPUState *cpu;
zhanghailiang5b009e42014-11-04 19:49:30 +08001539 uint64_t *node_mem;
aliguori030ea372009-04-21 22:30:47 +00001540
zhanghailiang5b009e42014-11-04 19:49:30 +08001541 node_mem = g_new0(uint64_t, nb_numa_nodes);
1542 query_numa_node_mem(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001543 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1544 for (i = 0; i < nb_numa_nodes; i++) {
1545 monitor_printf(mon, "node %d cpus:", i);
Andreas Färberbdc44642013-06-24 23:50:24 +02001546 CPU_FOREACH(cpu) {
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001547 if (cpu->numa_node == i) {
Andreas Färber55e5c282012-12-17 06:18:02 +01001548 monitor_printf(mon, " %d", cpu->cpu_index);
aliguori030ea372009-04-21 22:30:47 +00001549 }
1550 }
1551 monitor_printf(mon, "\n");
1552 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
zhanghailiang5b009e42014-11-04 19:49:30 +08001553 node_mem[i] >> 20);
aliguori030ea372009-04-21 22:30:47 +00001554 }
zhanghailiang5b009e42014-11-04 19:49:30 +08001555 g_free(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001556}
1557
bellard5f1ce942006-02-08 22:40:15 +00001558#ifdef CONFIG_PROFILER
1559
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001560int64_t tcg_time;
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001561int64_t dev_time;
1562
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001563static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001564{
aliguori376253e2009-03-05 23:01:23 +00001565 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Rutuja Shah73bcb242016-03-21 21:32:30 +05301566 dev_time, dev_time / (double)NANOSECONDS_PER_SECOND);
aliguori376253e2009-03-05 23:01:23 +00001567 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Rutuja Shah73bcb242016-03-21 21:32:30 +05301568 tcg_time, tcg_time / (double)NANOSECONDS_PER_SECOND);
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001569 tcg_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001570 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001571}
1572#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001573static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001574{
aliguori376253e2009-03-05 23:01:23 +00001575 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001576}
1577#endif
1578
bellardec36b692006-07-16 18:57:03 +00001579/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001580static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001581
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001582static void hmp_info_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001583{
1584 int i;
1585 CaptureState *s;
1586
1587 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001588 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001589 s->ops.info (s->opaque);
1590 }
1591}
1592
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001593static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001594{
1595 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001596 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001597 CaptureState *s;
1598
1599 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1600 if (i == n) {
1601 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001602 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001603 g_free (s);
bellardec36b692006-07-16 18:57:03 +00001604 return;
1605 }
1606 }
1607}
1608
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001609static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001610{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001611 const char *path = qdict_get_str(qdict, "path");
1612 int has_freq = qdict_haskey(qdict, "freq");
1613 int freq = qdict_get_try_int(qdict, "freq", -1);
1614 int has_bits = qdict_haskey(qdict, "bits");
1615 int bits = qdict_get_try_int(qdict, "bits", -1);
1616 int has_channels = qdict_haskey(qdict, "nchannels");
1617 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001618 CaptureState *s;
1619
Anthony Liguori7267c092011-08-20 22:09:37 -05001620 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001621
1622 freq = has_freq ? freq : 44100;
1623 bits = has_bits ? bits : 16;
1624 nchannels = has_channels ? nchannels : 2;
1625
1626 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001627 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05001628 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001629 return;
bellardec36b692006-07-16 18:57:03 +00001630 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001631 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001632}
bellardec36b692006-07-16 18:57:03 +00001633
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001634static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001635{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001636 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001637
aliguori76655d62009-03-06 20:27:37 +00001638 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001639 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001640 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001641 return acl;
1642}
aliguori76655d62009-03-06 20:27:37 +00001643
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001644static void hmp_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001645{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001646 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001647 qemu_acl *acl = find_acl(mon, aclname);
1648 qemu_acl_entry *entry;
1649 int i = 0;
1650
1651 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001652 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001653 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00001654 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00001655 i++;
1656 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001657 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001658 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001659 }
1660}
1661
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001662static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001663{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001664 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001665 qemu_acl *acl = find_acl(mon, aclname);
1666
1667 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001668 qemu_acl_reset(acl);
1669 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001670 }
1671}
aliguori76655d62009-03-06 20:27:37 +00001672
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001673static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001674{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001675 const char *aclname = qdict_get_str(qdict, "aclname");
1676 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001677 qemu_acl *acl = find_acl(mon, aclname);
1678
1679 if (acl) {
1680 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001681 acl->defaultDeny = 0;
1682 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001683 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001684 acl->defaultDeny = 1;
1685 monitor_printf(mon, "acl: policy set to 'deny'\n");
1686 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001687 monitor_printf(mon, "acl: unknown policy '%s', "
1688 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001689 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001690 }
1691}
aliguori76655d62009-03-06 20:27:37 +00001692
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001693static void hmp_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001694{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001695 const char *aclname = qdict_get_str(qdict, "aclname");
1696 const char *match = qdict_get_str(qdict, "match");
1697 const char *policy = qdict_get_str(qdict, "policy");
1698 int has_index = qdict_haskey(qdict, "index");
1699 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001700 qemu_acl *acl = find_acl(mon, aclname);
1701 int deny, ret;
1702
1703 if (acl) {
1704 if (strcmp(policy, "allow") == 0) {
1705 deny = 0;
1706 } else if (strcmp(policy, "deny") == 0) {
1707 deny = 1;
1708 } else {
1709 monitor_printf(mon, "acl: unknown policy '%s', "
1710 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001711 return;
1712 }
aliguori28a76be2009-03-06 20:27:40 +00001713 if (has_index)
1714 ret = qemu_acl_insert(acl, deny, match, index);
1715 else
1716 ret = qemu_acl_append(acl, deny, match);
1717 if (ret < 0)
1718 monitor_printf(mon, "acl: unable to add acl entry\n");
1719 else
1720 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001721 }
1722}
aliguori76655d62009-03-06 20:27:37 +00001723
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001724static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001725{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001726 const char *aclname = qdict_get_str(qdict, "aclname");
1727 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001728 qemu_acl *acl = find_acl(mon, aclname);
1729 int ret;
aliguori76655d62009-03-06 20:27:37 +00001730
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001731 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001732 ret = qemu_acl_remove(acl, match);
1733 if (ret < 0)
1734 monitor_printf(mon, "acl: no matching acl entry\n");
1735 else
1736 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001737 }
1738}
1739
Corey Bryant208c9d12012-06-22 14:36:09 -04001740void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001741{
Anthony Liguoric227f092009-10-01 16:12:16 -05001742 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001743 int fd;
1744
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03001745 fd = qemu_chr_fe_get_msgfd(&cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001746 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001747 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryant208c9d12012-06-22 14:36:09 -04001748 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001749 }
1750
1751 if (qemu_isdigit(fdname[0])) {
Stefan Hajnoczi0b9f0e22014-04-24 13:58:18 +02001752 close(fd);
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001753 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
1754 "a name not starting with a digit");
Corey Bryant208c9d12012-06-22 14:36:09 -04001755 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001756 }
1757
Corey Bryant208c9d12012-06-22 14:36:09 -04001758 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001759 if (strcmp(monfd->name, fdname) != 0) {
1760 continue;
1761 }
1762
1763 close(monfd->fd);
1764 monfd->fd = fd;
Corey Bryant208c9d12012-06-22 14:36:09 -04001765 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001766 }
1767
Anthony Liguori7267c092011-08-20 22:09:37 -05001768 monfd = g_malloc0(sizeof(mon_fd_t));
1769 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001770 monfd->fd = fd;
1771
Corey Bryant208c9d12012-06-22 14:36:09 -04001772 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001773}
1774
Corey Bryant208c9d12012-06-22 14:36:09 -04001775void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001776{
Anthony Liguoric227f092009-10-01 16:12:16 -05001777 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001778
Corey Bryant208c9d12012-06-22 14:36:09 -04001779 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001780 if (strcmp(monfd->name, fdname) != 0) {
1781 continue;
1782 }
1783
Blue Swirl72cf2d42009-09-12 07:36:22 +00001784 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001785 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001786 g_free(monfd->name);
1787 g_free(monfd);
Corey Bryant208c9d12012-06-22 14:36:09 -04001788 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001789 }
1790
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001791 error_setg(errp, QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001792}
1793
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001794static void hmp_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001795{
Luiz Capitulino13548692011-07-29 15:36:43 -03001796 int saved_vm_running = runstate_is_running();
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001797 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02001798
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001799 vm_stop(RUN_STATE_RESTORE_VM);
Juan Quintelac8d41b22009-08-20 19:42:21 +02001800
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03001801 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02001802 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03001803 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02001804}
1805
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001806int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
Mark McLoughlin7768e042009-07-22 09:11:41 +01001807{
Anthony Liguoric227f092009-10-01 16:12:16 -05001808 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01001809
Blue Swirl72cf2d42009-09-12 07:36:22 +00001810 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01001811 int fd;
1812
1813 if (strcmp(monfd->name, fdname) != 0) {
1814 continue;
1815 }
1816
1817 fd = monfd->fd;
1818
1819 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001820 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05001821 g_free(monfd->name);
1822 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001823
1824 return fd;
1825 }
1826
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001827 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001828 return -1;
1829}
1830
Corey Bryantba1c0482012-08-14 16:43:43 -04001831static void monitor_fdset_cleanup(MonFdset *mon_fdset)
1832{
1833 MonFdsetFd *mon_fdset_fd;
1834 MonFdsetFd *mon_fdset_fd_next;
1835
1836 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantebe52b52012-10-18 15:19:33 -04001837 if ((mon_fdset_fd->removed ||
1838 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
1839 runstate_is_running()) {
Corey Bryantba1c0482012-08-14 16:43:43 -04001840 close(mon_fdset_fd->fd);
1841 g_free(mon_fdset_fd->opaque);
1842 QLIST_REMOVE(mon_fdset_fd, next);
1843 g_free(mon_fdset_fd);
1844 }
1845 }
1846
Corey Bryantadb696f2012-08-14 16:43:47 -04001847 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04001848 QLIST_REMOVE(mon_fdset, next);
1849 g_free(mon_fdset);
1850 }
1851}
1852
Corey Bryantefb87c12012-08-14 16:43:48 -04001853static void monitor_fdsets_cleanup(void)
1854{
1855 MonFdset *mon_fdset;
1856 MonFdset *mon_fdset_next;
1857
1858 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
1859 monitor_fdset_cleanup(mon_fdset);
1860 }
1861}
1862
Corey Bryantba1c0482012-08-14 16:43:43 -04001863AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
1864 const char *opaque, Error **errp)
1865{
1866 int fd;
1867 Monitor *mon = cur_mon;
Corey Bryantba1c0482012-08-14 16:43:43 -04001868 AddfdInfo *fdinfo;
1869
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03001870 fd = qemu_chr_fe_get_msgfd(&mon->chr);
Corey Bryantba1c0482012-08-14 16:43:43 -04001871 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001872 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryantba1c0482012-08-14 16:43:43 -04001873 goto error;
1874 }
1875
Corey Bryante446f702012-10-18 15:19:32 -04001876 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
1877 has_opaque, opaque, errp);
1878 if (fdinfo) {
1879 return fdinfo;
Corey Bryant9ac54af2012-10-18 15:19:31 -04001880 }
1881
Corey Bryantba1c0482012-08-14 16:43:43 -04001882error:
1883 if (fd != -1) {
1884 close(fd);
1885 }
1886 return NULL;
1887}
1888
1889void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
1890{
1891 MonFdset *mon_fdset;
1892 MonFdsetFd *mon_fdset_fd;
1893 char fd_str[60];
1894
1895 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1896 if (mon_fdset->id != fdset_id) {
1897 continue;
1898 }
1899 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1900 if (has_fd) {
1901 if (mon_fdset_fd->fd != fd) {
1902 continue;
1903 }
1904 mon_fdset_fd->removed = true;
1905 break;
1906 } else {
1907 mon_fdset_fd->removed = true;
1908 }
1909 }
1910 if (has_fd && !mon_fdset_fd) {
1911 goto error;
1912 }
1913 monitor_fdset_cleanup(mon_fdset);
1914 return;
1915 }
1916
1917error:
1918 if (has_fd) {
1919 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
1920 fdset_id, fd);
1921 } else {
1922 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
1923 }
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001924 error_setg(errp, QERR_FD_NOT_FOUND, fd_str);
Corey Bryantba1c0482012-08-14 16:43:43 -04001925}
1926
1927FdsetInfoList *qmp_query_fdsets(Error **errp)
1928{
1929 MonFdset *mon_fdset;
1930 MonFdsetFd *mon_fdset_fd;
1931 FdsetInfoList *fdset_list = NULL;
1932
1933 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1934 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
1935 FdsetFdInfoList *fdsetfd_list = NULL;
1936
1937 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
1938 fdset_info->value->fdset_id = mon_fdset->id;
1939
1940 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1941 FdsetFdInfoList *fdsetfd_info;
1942
1943 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
1944 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
1945 fdsetfd_info->value->fd = mon_fdset_fd->fd;
1946 if (mon_fdset_fd->opaque) {
1947 fdsetfd_info->value->has_opaque = true;
1948 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
1949 } else {
1950 fdsetfd_info->value->has_opaque = false;
1951 }
1952
1953 fdsetfd_info->next = fdsetfd_list;
1954 fdsetfd_list = fdsetfd_info;
1955 }
1956
1957 fdset_info->value->fds = fdsetfd_list;
1958
1959 fdset_info->next = fdset_list;
1960 fdset_list = fdset_info;
1961 }
1962
1963 return fdset_list;
1964}
1965
Corey Bryante446f702012-10-18 15:19:32 -04001966AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
1967 bool has_opaque, const char *opaque,
1968 Error **errp)
1969{
1970 MonFdset *mon_fdset = NULL;
1971 MonFdsetFd *mon_fdset_fd;
1972 AddfdInfo *fdinfo;
1973
1974 if (has_fdset_id) {
1975 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1976 /* Break if match found or match impossible due to ordering by ID */
1977 if (fdset_id <= mon_fdset->id) {
1978 if (fdset_id < mon_fdset->id) {
1979 mon_fdset = NULL;
1980 }
1981 break;
1982 }
1983 }
1984 }
1985
1986 if (mon_fdset == NULL) {
1987 int64_t fdset_id_prev = -1;
1988 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
1989
1990 if (has_fdset_id) {
1991 if (fdset_id < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001992 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
1993 "a non-negative value");
Corey Bryante446f702012-10-18 15:19:32 -04001994 return NULL;
1995 }
1996 /* Use specified fdset ID */
1997 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1998 mon_fdset_cur = mon_fdset;
1999 if (fdset_id < mon_fdset_cur->id) {
2000 break;
2001 }
2002 }
2003 } else {
2004 /* Use first available fdset ID */
2005 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2006 mon_fdset_cur = mon_fdset;
2007 if (fdset_id_prev == mon_fdset_cur->id - 1) {
2008 fdset_id_prev = mon_fdset_cur->id;
2009 continue;
2010 }
2011 break;
2012 }
2013 }
2014
2015 mon_fdset = g_malloc0(sizeof(*mon_fdset));
2016 if (has_fdset_id) {
2017 mon_fdset->id = fdset_id;
2018 } else {
2019 mon_fdset->id = fdset_id_prev + 1;
2020 }
2021
2022 /* The fdset list is ordered by fdset ID */
2023 if (!mon_fdset_cur) {
2024 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
2025 } else if (mon_fdset->id < mon_fdset_cur->id) {
2026 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
2027 } else {
2028 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
2029 }
2030 }
2031
2032 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
2033 mon_fdset_fd->fd = fd;
2034 mon_fdset_fd->removed = false;
2035 if (has_opaque) {
2036 mon_fdset_fd->opaque = g_strdup(opaque);
2037 }
2038 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
2039
2040 fdinfo = g_malloc0(sizeof(*fdinfo));
2041 fdinfo->fdset_id = mon_fdset->id;
2042 fdinfo->fd = mon_fdset_fd->fd;
2043
2044 return fdinfo;
2045}
2046
Corey Bryantadb696f2012-08-14 16:43:47 -04002047int monitor_fdset_get_fd(int64_t fdset_id, int flags)
2048{
Blue Swirlb2dc64c2012-08-18 20:14:54 +00002049#ifndef _WIN32
Corey Bryantadb696f2012-08-14 16:43:47 -04002050 MonFdset *mon_fdset;
2051 MonFdsetFd *mon_fdset_fd;
2052 int mon_fd_flags;
2053
Corey Bryantadb696f2012-08-14 16:43:47 -04002054 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2055 if (mon_fdset->id != fdset_id) {
2056 continue;
2057 }
2058 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2059 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
2060 if (mon_fd_flags == -1) {
2061 return -1;
2062 }
2063
2064 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
2065 return mon_fdset_fd->fd;
2066 }
2067 }
2068 errno = EACCES;
2069 return -1;
2070 }
2071#endif
2072
2073 errno = ENOENT;
2074 return -1;
2075}
2076
2077int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
2078{
2079 MonFdset *mon_fdset;
2080 MonFdsetFd *mon_fdset_fd_dup;
2081
2082 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2083 if (mon_fdset->id != fdset_id) {
2084 continue;
2085 }
2086 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2087 if (mon_fdset_fd_dup->fd == dup_fd) {
2088 return -1;
2089 }
2090 }
2091 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2092 mon_fdset_fd_dup->fd = dup_fd;
2093 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
2094 return 0;
2095 }
2096 return -1;
2097}
2098
2099static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2100{
2101 MonFdset *mon_fdset;
2102 MonFdsetFd *mon_fdset_fd_dup;
2103
2104 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2105 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2106 if (mon_fdset_fd_dup->fd == dup_fd) {
2107 if (remove) {
2108 QLIST_REMOVE(mon_fdset_fd_dup, next);
2109 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2110 monitor_fdset_cleanup(mon_fdset);
2111 }
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002112 return -1;
2113 } else {
2114 return mon_fdset->id;
Corey Bryantadb696f2012-08-14 16:43:47 -04002115 }
Corey Bryantadb696f2012-08-14 16:43:47 -04002116 }
2117 }
2118 }
2119 return -1;
2120}
2121
2122int monitor_fdset_dup_fd_find(int dup_fd)
2123{
2124 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2125}
2126
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002127void monitor_fdset_dup_fd_remove(int dup_fd)
Corey Bryantadb696f2012-08-14 16:43:47 -04002128{
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002129 monitor_fdset_dup_fd_find_remove(dup_fd, true);
Corey Bryantadb696f2012-08-14 16:43:47 -04002130}
2131
Markus Armbruster1677f4c2015-02-09 14:03:19 +01002132int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
Laszlo Ersek59063662014-04-10 10:24:31 +02002133{
2134 int fd;
2135 Error *local_err = NULL;
2136
2137 if (!qemu_isdigit(fdname[0]) && mon) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002138 fd = monitor_get_fd(mon, fdname, &local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002139 } else {
2140 fd = qemu_parse_fd(fdname);
Laszlo Ersek59063662014-04-10 10:24:31 +02002141 if (fd == -1) {
2142 error_setg(&local_err, "Invalid file descriptor number '%s'",
2143 fdname);
2144 }
2145 }
2146 if (local_err) {
2147 error_propagate(errp, local_err);
2148 assert(fd == -1);
2149 } else {
2150 assert(fd != -1);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002151 }
2152
2153 return fd;
2154}
2155
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002156/* Please update hmp-commands.hx when adding or changing commands */
Wayne Xia816f8922011-10-12 11:32:41 +08002157static mon_cmd_t info_cmds[] = {
Pavel Butsykinda76ee72015-09-10 18:38:58 +03002158#include "hmp-commands-info.h"
2159 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002160};
2161
Wenchao Xiaa13ced52013-01-14 14:06:28 +08002162/* mon_cmds and info_cmds would be sorted at runtime */
2163static mon_cmd_t mon_cmds[] = {
2164#include "hmp-commands.h"
2165 { NULL, NULL, },
2166};
2167
bellard9307c4c2004-04-04 12:57:25 +00002168/*******************************************************************/
2169
2170static const char *pch;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002171static sigjmp_buf expr_env;
bellard9307c4c2004-04-04 12:57:25 +00002172
bellard9307c4c2004-04-04 12:57:25 +00002173
Stefan Weil9c3175c2013-08-22 21:30:09 +02002174static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
2175expr_error(Monitor *mon, const char *fmt, ...)
bellard9dc39cb2004-03-14 21:38:27 +00002176{
Fam Zheng277acfe2013-08-20 10:58:21 +08002177 va_list ap;
2178 va_start(ap, fmt);
2179 monitor_vprintf(mon, fmt, ap);
2180 monitor_printf(mon, "\n");
2181 va_end(ap);
Peter Maydell6ab7e542013-02-20 15:21:09 +00002182 siglongjmp(expr_env, 1);
bellard9307c4c2004-04-04 12:57:25 +00002183}
2184
Markus Armbruster09b94182010-01-20 13:07:30 +01002185/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00002186static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002187{
Pavel Butsykinbf957282015-09-10 18:38:59 +03002188 const MonitorDef *md = target_monitor_defs();
bellard92a31b12005-02-10 22:00:52 +00002189 void *ptr;
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002190 uint64_t tmp = 0;
2191 int ret;
bellard92a31b12005-02-10 22:00:52 +00002192
Pavel Butsykinbf957282015-09-10 18:38:59 +03002193 if (md == NULL) {
2194 return -1;
2195 }
2196
2197 for(; md->name != NULL; md++) {
bellard9307c4c2004-04-04 12:57:25 +00002198 if (compare_cmd(name, md->name)) {
2199 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002200 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002201 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002202 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00002203 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002204 switch(md->type) {
2205 case MD_I32:
2206 *pval = *(int32_t *)ptr;
2207 break;
2208 case MD_TLONG:
2209 *pval = *(target_long *)ptr;
2210 break;
2211 default:
2212 *pval = 0;
2213 break;
2214 }
bellard9307c4c2004-04-04 12:57:25 +00002215 }
2216 return 0;
2217 }
2218 }
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002219
2220 ret = target_get_monitor_def(mon_get_cpu(), name, &tmp);
2221 if (!ret) {
2222 *pval = (target_long) tmp;
2223 }
2224
2225 return ret;
bellard9307c4c2004-04-04 12:57:25 +00002226}
2227
2228static void next(void)
2229{
Blue Swirl660f11b2009-07-31 21:16:51 +00002230 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002231 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002232 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002233 pch++;
2234 }
2235}
2236
aliguori376253e2009-03-05 23:01:23 +00002237static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002238
aliguori376253e2009-03-05 23:01:23 +00002239static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002240{
blueswir1c2efc952007-09-25 17:28:42 +00002241 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002242 char *p;
bellard6a00d602005-11-21 23:25:50 +00002243 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002244
2245 switch(*pch) {
2246 case '+':
2247 next();
aliguori376253e2009-03-05 23:01:23 +00002248 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002249 break;
2250 case '-':
2251 next();
aliguori376253e2009-03-05 23:01:23 +00002252 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002253 break;
2254 case '~':
2255 next();
aliguori376253e2009-03-05 23:01:23 +00002256 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002257 break;
2258 case '(':
2259 next();
aliguori376253e2009-03-05 23:01:23 +00002260 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002261 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002262 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002263 }
2264 next();
2265 break;
bellard81d09122004-07-14 17:21:37 +00002266 case '\'':
2267 pch++;
2268 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002269 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002270 n = *pch;
2271 pch++;
2272 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002273 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002274 next();
2275 break;
bellard9307c4c2004-04-04 12:57:25 +00002276 case '$':
2277 {
2278 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002279 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002280
bellard9307c4c2004-04-04 12:57:25 +00002281 pch++;
2282 q = buf;
2283 while ((*pch >= 'a' && *pch <= 'z') ||
2284 (*pch >= 'A' && *pch <= 'Z') ||
2285 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002286 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002287 if ((q - buf) < sizeof(buf) - 1)
2288 *q++ = *pch;
2289 pch++;
2290 }
blueswir1cd390082008-11-16 13:53:32 +00002291 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002292 pch++;
2293 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002294 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01002295 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00002296 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00002297 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002298 }
2299 break;
2300 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002301 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002302 n = 0;
2303 break;
2304 default:
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002305 errno = 0;
bellard4f4fbf72006-06-25 18:28:12 +00002306 n = strtoull(pch, &p, 0);
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002307 if (errno == ERANGE) {
2308 expr_error(mon, "number too large");
2309 }
bellard9307c4c2004-04-04 12:57:25 +00002310 if (pch == p) {
Fam Zheng277acfe2013-08-20 10:58:21 +08002311 expr_error(mon, "invalid char '%c' in expression", *p);
bellard9307c4c2004-04-04 12:57:25 +00002312 }
2313 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002314 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002315 pch++;
2316 break;
2317 }
2318 return n;
2319}
2320
2321
aliguori376253e2009-03-05 23:01:23 +00002322static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002323{
blueswir1c2efc952007-09-25 17:28:42 +00002324 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002325 int op;
ths3b46e622007-09-17 08:09:54 +00002326
aliguori376253e2009-03-05 23:01:23 +00002327 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002328 for(;;) {
2329 op = *pch;
2330 if (op != '*' && op != '/' && op != '%')
2331 break;
2332 next();
aliguori376253e2009-03-05 23:01:23 +00002333 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002334 switch(op) {
2335 default:
2336 case '*':
2337 val *= val2;
2338 break;
2339 case '/':
2340 case '%':
ths5fafdf22007-09-16 21:08:06 +00002341 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002342 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002343 if (op == '/')
2344 val /= val2;
2345 else
2346 val %= val2;
2347 break;
2348 }
2349 }
2350 return val;
2351}
2352
aliguori376253e2009-03-05 23:01:23 +00002353static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002354{
blueswir1c2efc952007-09-25 17:28:42 +00002355 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002356 int op;
bellard9307c4c2004-04-04 12:57:25 +00002357
aliguori376253e2009-03-05 23:01:23 +00002358 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002359 for(;;) {
2360 op = *pch;
2361 if (op != '&' && op != '|' && op != '^')
2362 break;
2363 next();
aliguori376253e2009-03-05 23:01:23 +00002364 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002365 switch(op) {
2366 default:
2367 case '&':
2368 val &= val2;
2369 break;
2370 case '|':
2371 val |= val2;
2372 break;
2373 case '^':
2374 val ^= val2;
2375 break;
2376 }
2377 }
2378 return val;
2379}
2380
aliguori376253e2009-03-05 23:01:23 +00002381static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002382{
blueswir1c2efc952007-09-25 17:28:42 +00002383 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002384 int op;
bellard9307c4c2004-04-04 12:57:25 +00002385
aliguori376253e2009-03-05 23:01:23 +00002386 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002387 for(;;) {
2388 op = *pch;
2389 if (op != '+' && op != '-')
2390 break;
2391 next();
aliguori376253e2009-03-05 23:01:23 +00002392 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002393 if (op == '+')
2394 val += val2;
2395 else
2396 val -= val2;
2397 }
2398 return val;
2399}
2400
aliguori376253e2009-03-05 23:01:23 +00002401static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002402{
2403 pch = *pp;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002404 if (sigsetjmp(expr_env, 0)) {
bellard9307c4c2004-04-04 12:57:25 +00002405 *pp = pch;
2406 return -1;
2407 }
blueswir1cd390082008-11-16 13:53:32 +00002408 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002409 pch++;
aliguori376253e2009-03-05 23:01:23 +00002410 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002411 *pp = pch;
2412 return 0;
2413}
2414
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002415static int get_double(Monitor *mon, double *pval, const char **pp)
2416{
2417 const char *p = *pp;
2418 char *tailp;
2419 double d;
2420
2421 d = strtod(p, &tailp);
2422 if (tailp == p) {
2423 monitor_printf(mon, "Number expected\n");
2424 return -1;
2425 }
2426 if (d != d || d - d != 0) {
2427 /* NaN or infinity */
2428 monitor_printf(mon, "Bad number\n");
2429 return -1;
2430 }
2431 *pval = d;
2432 *pp = tailp;
2433 return 0;
2434}
2435
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002436/*
2437 * Store the command-name in cmdname, and return a pointer to
2438 * the remaining of the command string.
2439 */
2440static const char *get_command_name(const char *cmdline,
2441 char *cmdname, size_t nlen)
2442{
2443 size_t len;
2444 const char *p, *pstart;
2445
2446 p = cmdline;
2447 while (qemu_isspace(*p))
2448 p++;
2449 if (*p == '\0')
2450 return NULL;
2451 pstart = p;
2452 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2453 p++;
2454 len = p - pstart;
2455 if (len > nlen - 1)
2456 len = nlen - 1;
2457 memcpy(cmdname, pstart, len);
2458 cmdname[len] = '\0';
2459 return p;
2460}
2461
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002462/**
2463 * Read key of 'type' into 'key' and return the current
2464 * 'type' pointer.
2465 */
2466static char *key_get_info(const char *type, char **key)
2467{
2468 size_t len;
2469 char *p, *str;
2470
2471 if (*type == ',')
2472 type++;
2473
2474 p = strchr(type, ':');
2475 if (!p) {
2476 *key = NULL;
2477 return NULL;
2478 }
2479 len = p - type;
2480
Anthony Liguori7267c092011-08-20 22:09:37 -05002481 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002482 memcpy(str, type, len);
2483 str[len] = '\0';
2484
2485 *key = str;
2486 return ++p;
2487}
2488
bellard9307c4c2004-04-04 12:57:25 +00002489static int default_fmt_format = 'x';
2490static int default_fmt_size = 4;
2491
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002492static int is_valid_option(const char *c, const char *typestr)
2493{
2494 char option[3];
2495
2496 option[0] = '-';
2497 option[1] = *c;
2498 option[2] = '\0';
2499
2500 typestr = strstr(typestr, option);
2501 return (typestr != NULL);
2502}
2503
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03002504static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
2505 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002506{
2507 const mon_cmd_t *cmd;
2508
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03002509 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002510 if (compare_cmd(cmdname, cmd->name)) {
2511 return cmd;
2512 }
2513 }
2514
2515 return NULL;
2516}
2517
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002518/*
Bandan Dasae502122015-06-03 18:38:08 -04002519 * Parse command name from @cmdp according to command table @table.
2520 * If blank, return NULL.
2521 * Else, if no valid command can be found, report to @mon, and return
2522 * NULL.
2523 * Else, change @cmdp to point right behind the name, and return its
2524 * command table entry.
2525 * Do not assume the return value points into @table! It doesn't when
2526 * the command is found in a sub-command table.
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002527 */
Anthony Liguoric227f092009-10-01 16:12:16 -05002528static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Bandan Dasae502122015-06-03 18:38:08 -04002529 const char **cmdp,
2530 mon_cmd_t *table)
bellard9307c4c2004-04-04 12:57:25 +00002531{
Bandan Dasae502122015-06-03 18:38:08 -04002532 const char *p;
Anthony Liguoric227f092009-10-01 16:12:16 -05002533 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002534 char cmdname[256];
bellard9dc39cb2004-03-14 21:38:27 +00002535
bellard9307c4c2004-04-04 12:57:25 +00002536 /* extract the command name */
Bandan Dasae502122015-06-03 18:38:08 -04002537 p = get_command_name(*cmdp, cmdname, sizeof(cmdname));
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002538 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002539 return NULL;
ths3b46e622007-09-17 08:09:54 +00002540
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002541 cmd = search_dispatch_table(table, cmdname);
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002542 if (!cmd) {
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002543 monitor_printf(mon, "unknown command: '%.*s'\n",
Bandan Dasae502122015-06-03 18:38:08 -04002544 (int)(p - *cmdp), *cmdp);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002545 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002546 }
bellard9307c4c2004-04-04 12:57:25 +00002547
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002548 /* filter out following useless space */
2549 while (qemu_isspace(*p)) {
2550 p++;
2551 }
Bandan Dasae502122015-06-03 18:38:08 -04002552
2553 *cmdp = p;
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002554 /* search sub command */
Bandan Dasae502122015-06-03 18:38:08 -04002555 if (cmd->sub_table != NULL && *p != '\0') {
2556 return monitor_parse_command(mon, cmdp, cmd->sub_table);
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002557 }
2558
Bandan Dasae502122015-06-03 18:38:08 -04002559 return cmd;
2560}
2561
2562/*
2563 * Parse arguments for @cmd.
2564 * If it can't be parsed, report to @mon, and return NULL.
2565 * Else, insert command arguments into a QDict, and return it.
2566 * Note: On success, caller has to free the QDict structure.
2567 */
2568
2569static QDict *monitor_parse_arguments(Monitor *mon,
2570 const char **endp,
2571 const mon_cmd_t *cmd)
2572{
2573 const char *typestr;
2574 char *key;
2575 int c;
2576 const char *p = *endp;
2577 char buf[1024];
2578 QDict *qdict = qdict_new();
2579
bellard9307c4c2004-04-04 12:57:25 +00002580 /* parse the parameters */
2581 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00002582 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002583 typestr = key_get_info(typestr, &key);
2584 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00002585 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002586 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00002587 typestr++;
2588 switch(c) {
2589 case 'F':
bellard81d09122004-07-14 17:21:37 +00002590 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002591 case 's':
2592 {
2593 int ret;
ths3b46e622007-09-17 08:09:54 +00002594
blueswir1cd390082008-11-16 13:53:32 +00002595 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002596 p++;
2597 if (*typestr == '?') {
2598 typestr++;
2599 if (*p == '\0') {
2600 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03002601 break;
bellard9307c4c2004-04-04 12:57:25 +00002602 }
2603 }
2604 ret = get_str(buf, sizeof(buf), &p);
2605 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002606 switch(c) {
2607 case 'F':
aliguori376253e2009-03-05 23:01:23 +00002608 monitor_printf(mon, "%s: filename expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002609 cmd->name);
bellard81d09122004-07-14 17:21:37 +00002610 break;
2611 case 'B':
aliguori376253e2009-03-05 23:01:23 +00002612 monitor_printf(mon, "%s: block device name expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002613 cmd->name);
bellard81d09122004-07-14 17:21:37 +00002614 break;
2615 default:
Bandan Dasae502122015-06-03 18:38:08 -04002616 monitor_printf(mon, "%s: string expected\n", cmd->name);
bellard81d09122004-07-14 17:21:37 +00002617 break;
2618 }
bellard9307c4c2004-04-04 12:57:25 +00002619 goto fail;
2620 }
Luiz Capitulino53773582009-08-28 15:27:25 -03002621 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00002622 }
2623 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01002624 case 'O':
2625 {
2626 QemuOptsList *opts_list;
2627 QemuOpts *opts;
2628
2629 opts_list = qemu_find_opts(key);
2630 if (!opts_list || opts_list->desc->name) {
2631 goto bad_type;
2632 }
2633 while (qemu_isspace(*p)) {
2634 p++;
2635 }
2636 if (!*p)
2637 break;
2638 if (get_str(buf, sizeof(buf), &p) < 0) {
2639 goto fail;
2640 }
Markus Armbruster70b94332015-02-13 12:50:26 +01002641 opts = qemu_opts_parse_noisily(opts_list, buf, true);
Markus Armbruster361127d2010-02-10 20:24:35 +01002642 if (!opts) {
2643 goto fail;
2644 }
2645 qemu_opts_to_qdict(opts, qdict);
2646 qemu_opts_del(opts);
2647 }
2648 break;
bellard9307c4c2004-04-04 12:57:25 +00002649 case '/':
2650 {
2651 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002652
blueswir1cd390082008-11-16 13:53:32 +00002653 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002654 p++;
2655 if (*p == '/') {
2656 /* format found */
2657 p++;
2658 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002659 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002660 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002661 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002662 count = count * 10 + (*p - '0');
2663 p++;
2664 }
2665 }
2666 size = -1;
2667 format = -1;
2668 for(;;) {
2669 switch(*p) {
2670 case 'o':
2671 case 'd':
2672 case 'u':
2673 case 'x':
2674 case 'i':
2675 case 'c':
2676 format = *p++;
2677 break;
2678 case 'b':
2679 size = 1;
2680 p++;
2681 break;
2682 case 'h':
2683 size = 2;
2684 p++;
2685 break;
2686 case 'w':
2687 size = 4;
2688 p++;
2689 break;
2690 case 'g':
2691 case 'L':
2692 size = 8;
2693 p++;
2694 break;
2695 default:
2696 goto next;
2697 }
2698 }
2699 next:
blueswir1cd390082008-11-16 13:53:32 +00002700 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00002701 monitor_printf(mon, "invalid char in format: '%c'\n",
2702 *p);
bellard9307c4c2004-04-04 12:57:25 +00002703 goto fail;
2704 }
bellard9307c4c2004-04-04 12:57:25 +00002705 if (format < 0)
2706 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002707 if (format != 'i') {
2708 /* for 'i', not specifying a size gives -1 as size */
2709 if (size < 0)
2710 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002711 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002712 }
bellard9307c4c2004-04-04 12:57:25 +00002713 default_fmt_format = format;
2714 } else {
2715 count = 1;
2716 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002717 if (format != 'i') {
2718 size = default_fmt_size;
2719 } else {
2720 size = -1;
2721 }
bellard9307c4c2004-04-04 12:57:25 +00002722 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002723 qdict_put(qdict, "count", qint_from_int(count));
2724 qdict_put(qdict, "format", qint_from_int(format));
2725 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00002726 }
2727 break;
2728 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002729 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002730 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00002731 {
blueswir1c2efc952007-09-25 17:28:42 +00002732 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00002733
blueswir1cd390082008-11-16 13:53:32 +00002734 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002735 p++;
bellard34405572004-06-08 00:55:58 +00002736 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002737 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03002738 if (*p == '\0') {
2739 typestr++;
2740 break;
2741 }
bellard34405572004-06-08 00:55:58 +00002742 } else {
2743 if (*p == '.') {
2744 p++;
blueswir1cd390082008-11-16 13:53:32 +00002745 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00002746 p++;
bellard34405572004-06-08 00:55:58 +00002747 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03002748 typestr++;
2749 break;
bellard34405572004-06-08 00:55:58 +00002750 }
2751 }
bellard13224a82006-07-14 22:03:35 +00002752 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002753 }
aliguori376253e2009-03-05 23:01:23 +00002754 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00002755 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002756 /* Check if 'i' is greater than 32-bit */
2757 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
Bandan Dasae502122015-06-03 18:38:08 -04002758 monitor_printf(mon, "\'%s\' has failed: ", cmd->name);
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002759 monitor_printf(mon, "integer is for 32-bit values\n");
2760 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002761 } else if (c == 'M') {
Luiz Capitulino91162842012-04-26 17:34:30 -03002762 if (val < 0) {
2763 monitor_printf(mon, "enter a positive value\n");
2764 goto fail;
2765 }
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002766 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002767 }
Luiz Capitulino53773582009-08-28 15:27:25 -03002768 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00002769 }
2770 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02002771 case 'o':
2772 {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01002773 int64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02002774 char *end;
2775
2776 while (qemu_isspace(*p)) {
2777 p++;
2778 }
2779 if (*typestr == '?') {
2780 typestr++;
2781 if (*p == '\0') {
2782 break;
2783 }
2784 }
Marc-André Lureau4677bb42015-09-16 18:02:56 +02002785 val = qemu_strtosz(p, &end);
Jes Sorensendbc0c672010-10-21 17:15:47 +02002786 if (val < 0) {
2787 monitor_printf(mon, "invalid size\n");
2788 goto fail;
2789 }
2790 qdict_put(qdict, key, qint_from_int(val));
2791 p = end;
2792 }
2793 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01002794 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002795 {
2796 double val;
2797
2798 while (qemu_isspace(*p))
2799 p++;
2800 if (*typestr == '?') {
2801 typestr++;
2802 if (*p == '\0') {
2803 break;
2804 }
2805 }
2806 if (get_double(mon, &val, &p) < 0) {
2807 goto fail;
2808 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02002809 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01002810 switch (*p) {
2811 case 'm':
2812 val /= 1e3; p += 2; break;
2813 case 'u':
2814 val /= 1e6; p += 2; break;
2815 case 'n':
2816 val /= 1e9; p += 2; break;
2817 }
2818 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002819 if (*p && !qemu_isspace(*p)) {
2820 monitor_printf(mon, "Unknown unit suffix\n");
2821 goto fail;
2822 }
2823 qdict_put(qdict, key, qfloat_from_double(val));
2824 }
2825 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002826 case 'b':
2827 {
2828 const char *beg;
Eric Blakefc48ffc2015-05-15 16:24:59 -06002829 bool val;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002830
2831 while (qemu_isspace(*p)) {
2832 p++;
2833 }
2834 beg = p;
2835 while (qemu_isgraph(*p)) {
2836 p++;
2837 }
2838 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06002839 val = true;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002840 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06002841 val = false;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002842 } else {
2843 monitor_printf(mon, "Expected 'on' or 'off'\n");
2844 goto fail;
2845 }
Eric Blakefc48ffc2015-05-15 16:24:59 -06002846 qdict_put(qdict, key, qbool_from_bool(val));
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002847 }
2848 break;
bellard9307c4c2004-04-04 12:57:25 +00002849 case '-':
2850 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002851 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03002852 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00002853 /* option */
ths3b46e622007-09-17 08:09:54 +00002854
bellard9307c4c2004-04-04 12:57:25 +00002855 c = *typestr++;
2856 if (c == '\0')
2857 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00002858 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002859 p++;
bellard9307c4c2004-04-04 12:57:25 +00002860 if (*p == '-') {
2861 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002862 if(c != *p) {
2863 if(!is_valid_option(p, typestr)) {
2864
2865 monitor_printf(mon, "%s: unsupported option -%c\n",
Bandan Dasae502122015-06-03 18:38:08 -04002866 cmd->name, *p);
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002867 goto fail;
2868 } else {
2869 skip_key = 1;
2870 }
bellard9307c4c2004-04-04 12:57:25 +00002871 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002872 if(skip_key) {
2873 p = tmp;
2874 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03002875 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002876 p++;
Eric Blakefc48ffc2015-05-15 16:24:59 -06002877 qdict_put(qdict, key, qbool_from_bool(true));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002878 }
bellard9307c4c2004-04-04 12:57:25 +00002879 }
bellard9307c4c2004-04-04 12:57:25 +00002880 }
2881 break;
Wenchao Xia129be002013-08-27 20:38:26 +08002882 case 'S':
2883 {
2884 /* package all remaining string */
2885 int len;
2886
2887 while (qemu_isspace(*p)) {
2888 p++;
2889 }
2890 if (*typestr == '?') {
2891 typestr++;
2892 if (*p == '\0') {
2893 /* no remaining string: NULL argument */
2894 break;
2895 }
2896 }
2897 len = strlen(p);
2898 if (len <= 0) {
2899 monitor_printf(mon, "%s: string expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002900 cmd->name);
Bandan Dase549d2a2015-06-03 18:38:10 -04002901 goto fail;
Wenchao Xia129be002013-08-27 20:38:26 +08002902 }
2903 qdict_put(qdict, key, qstring_from_str(p));
2904 p += len;
2905 }
2906 break;
bellard9307c4c2004-04-04 12:57:25 +00002907 default:
2908 bad_type:
Bandan Dasae502122015-06-03 18:38:08 -04002909 monitor_printf(mon, "%s: unknown type '%c'\n", cmd->name, c);
bellard9307c4c2004-04-04 12:57:25 +00002910 goto fail;
2911 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002912 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002913 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00002914 }
2915 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00002916 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002917 p++;
2918 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00002919 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
Bandan Dasae502122015-06-03 18:38:08 -04002920 cmd->name);
bellard9307c4c2004-04-04 12:57:25 +00002921 goto fail;
2922 }
2923
Bandan Dasae502122015-06-03 18:38:08 -04002924 return qdict;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02002925
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002926fail:
Bandan Dasae502122015-06-03 18:38:08 -04002927 QDECREF(qdict);
Anthony Liguori7267c092011-08-20 22:09:37 -05002928 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002929 return NULL;
2930}
2931
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01002932static void handle_hmp_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002933{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002934 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05002935 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002936
Bandan Dasae502122015-06-03 18:38:08 -04002937 cmd = monitor_parse_command(mon, &cmdline, mon->cmd_table);
2938 if (!cmd) {
2939 return;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002940 }
2941
Bandan Dasae502122015-06-03 18:38:08 -04002942 qdict = monitor_parse_arguments(mon, &cmdline, cmd);
2943 if (!qdict) {
Bandan Dasdd41eea2015-06-03 18:38:09 -04002944 monitor_printf(mon, "Try \"help %s\" for more information\n",
2945 cmd->name);
Bandan Dasae502122015-06-03 18:38:08 -04002946 return;
2947 }
2948
Marc-André Lureau2b9e3572016-09-12 13:19:06 +04002949 cmd->cmd(mon, qdict);
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002950 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00002951}
2952
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08002953static void cmd_completion(Monitor *mon, const char *name, const char *list)
bellard81d09122004-07-14 17:21:37 +00002954{
2955 const char *p, *pstart;
2956 char cmd[128];
2957 int len;
2958
2959 p = list;
2960 for(;;) {
2961 pstart = p;
2962 p = strchr(p, '|');
2963 if (!p)
2964 p = pstart + strlen(pstart);
2965 len = p - pstart;
2966 if (len > sizeof(cmd) - 2)
2967 len = sizeof(cmd) - 2;
2968 memcpy(cmd, pstart, len);
2969 cmd[len] = '\0';
2970 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08002971 readline_add_completion(mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00002972 }
2973 if (*p == '\0')
2974 break;
2975 p++;
2976 }
2977}
2978
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08002979static void file_completion(Monitor *mon, const char *input)
bellard81d09122004-07-14 17:21:37 +00002980{
2981 DIR *ffs;
2982 struct dirent *d;
2983 char path[1024];
2984 char file[1024], file_prefix[1024];
2985 int input_path_len;
2986 const char *p;
2987
ths5fafdf22007-09-16 21:08:06 +00002988 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00002989 if (!p) {
2990 input_path_len = 0;
2991 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00002992 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00002993 } else {
2994 input_path_len = p - input + 1;
2995 memcpy(path, input, input_path_len);
2996 if (input_path_len > sizeof(path) - 1)
2997 input_path_len = sizeof(path) - 1;
2998 path[input_path_len] = '\0';
2999 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3000 }
Bandan Das19f2db52015-06-03 18:38:07 -04003001
bellard81d09122004-07-14 17:21:37 +00003002 ffs = opendir(path);
3003 if (!ffs)
3004 return;
3005 for(;;) {
3006 struct stat sb;
3007 d = readdir(ffs);
3008 if (!d)
3009 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09003010
3011 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
3012 continue;
3013 }
3014
bellard81d09122004-07-14 17:21:37 +00003015 if (strstart(d->d_name, file_prefix, NULL)) {
3016 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00003017 if (input_path_len < sizeof(file))
3018 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3019 d->d_name);
bellard81d09122004-07-14 17:21:37 +00003020 /* stat the file to find out if it's a directory.
3021 * In that case add a slash to speed up typing long paths
3022 */
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01003023 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
blueswir1363a37d2008-08-21 17:58:08 +00003024 pstrcat(file, sizeof(file), "/");
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01003025 }
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003026 readline_add_completion(mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003027 }
3028 }
3029 closedir(ffs);
3030}
3031
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003032static const char *next_arg_type(const char *typestr)
3033{
3034 const char *p = strchr(typestr, ':');
3035 return (p != NULL ? ++p : typestr);
3036}
3037
Hani Benhabiles40d19392014-05-07 23:41:30 +01003038static void add_completion_option(ReadLineState *rs, const char *str,
3039 const char *option)
3040{
3041 if (!str || !option) {
3042 return;
3043 }
3044 if (!strncmp(option, str, strlen(str))) {
3045 readline_add_completion(rs, option);
3046 }
3047}
3048
Hani Benhabiles13e315d2014-05-07 23:41:29 +01003049void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
3050{
3051 size_t len;
3052 ChardevBackendInfoList *list, *start;
3053
3054 if (nb_args != 2) {
3055 return;
3056 }
3057 len = strlen(str);
3058 readline_set_completion_index(rs, len);
3059
3060 start = list = qmp_query_chardev_backends(NULL);
3061 while (list) {
3062 const char *chr_name = list->value->name;
3063
3064 if (!strncmp(chr_name, str, len)) {
3065 readline_add_completion(rs, chr_name);
3066 }
3067 list = list->next;
3068 }
3069 qapi_free_ChardevBackendInfoList(start);
3070}
3071
Hani Benhabilesb162b492014-05-07 23:41:31 +01003072void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
3073{
3074 size_t len;
3075 int i;
3076
3077 if (nb_args != 2) {
3078 return;
3079 }
3080 len = strlen(str);
3081 readline_set_completion_index(rs, len);
Eric Blakef394b2e2016-07-13 21:50:23 -06003082 for (i = 0; NetClientDriver_lookup[i]; i++) {
3083 add_completion_option(rs, str, NetClientDriver_lookup[i]);
Hani Benhabilesb162b492014-05-07 23:41:31 +01003084 }
3085}
3086
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003087void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003088{
3089 GSList *list, *elt;
3090 size_t len;
3091
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003092 if (nb_args != 2) {
3093 return;
3094 }
3095
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003096 len = strlen(str);
3097 readline_set_completion_index(rs, len);
3098 list = elt = object_class_get_list(TYPE_DEVICE, false);
3099 while (elt) {
3100 const char *name;
3101 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
3102 TYPE_DEVICE);
3103 name = object_class_get_name(OBJECT_CLASS(dc));
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003104
3105 if (!dc->cannot_instantiate_with_device_add_yet
3106 && !strncmp(name, str, len)) {
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003107 readline_add_completion(rs, name);
3108 }
3109 elt = elt->next;
3110 }
3111 g_slist_free(list);
3112}
3113
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003114void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003115{
3116 GSList *list, *elt;
3117 size_t len;
3118
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003119 if (nb_args != 2) {
3120 return;
3121 }
3122
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003123 len = strlen(str);
3124 readline_set_completion_index(rs, len);
3125 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
3126 while (elt) {
3127 const char *name;
3128
3129 name = object_class_get_name(OBJECT_CLASS(elt->data));
3130 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
3131 readline_add_completion(rs, name);
3132 }
3133 elt = elt->next;
3134 }
3135 g_slist_free(list);
3136}
3137
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003138static void peripheral_device_del_completion(ReadLineState *rs,
3139 const char *str, size_t len)
3140{
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003141 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
3142 GSList *list, *item;
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003143
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003144 list = qdev_build_hotpluggable_device_list(peripheral);
3145 if (!list) {
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003146 return;
3147 }
3148
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003149 for (item = list; item; item = g_slist_next(item)) {
3150 DeviceState *dev = item->data;
3151
3152 if (dev->id && !strncmp(str, dev->id, len)) {
3153 readline_add_completion(rs, dev->id);
3154 }
3155 }
3156
3157 g_slist_free(list);
3158}
3159
Hani Benhabiles6297d9a2014-05-07 23:41:28 +01003160void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3161{
3162 size_t len;
3163 ChardevInfoList *list, *start;
3164
3165 if (nb_args != 2) {
3166 return;
3167 }
3168 len = strlen(str);
3169 readline_set_completion_index(rs, len);
3170
3171 start = list = qmp_query_chardev(NULL);
3172 while (list) {
3173 ChardevInfo *chr = list->value;
3174
3175 if (!strncmp(chr->label, str, len)) {
3176 readline_add_completion(rs, chr->label);
3177 }
3178 list = list->next;
3179 }
3180 qapi_free_ChardevInfoList(start);
3181}
3182
Hani Benhabiles8e597772014-05-27 23:39:30 +01003183static void ringbuf_completion(ReadLineState *rs, const char *str)
3184{
3185 size_t len;
3186 ChardevInfoList *list, *start;
3187
3188 len = strlen(str);
3189 readline_set_completion_index(rs, len);
3190
3191 start = list = qmp_query_chardev(NULL);
3192 while (list) {
3193 ChardevInfo *chr_info = list->value;
3194
3195 if (!strncmp(chr_info->label, str, len)) {
3196 CharDriverState *chr = qemu_chr_find(chr_info->label);
3197 if (chr && chr_is_ringbuf(chr)) {
3198 readline_add_completion(rs, chr_info->label);
3199 }
3200 }
3201 list = list->next;
3202 }
3203 qapi_free_ChardevInfoList(start);
3204}
3205
Hani Benhabiles8e597772014-05-27 23:39:30 +01003206void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
3207{
3208 if (nb_args != 2) {
3209 return;
3210 }
3211 ringbuf_completion(rs, str);
3212}
3213
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003214void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
3215{
3216 size_t len;
3217
3218 if (nb_args != 2) {
3219 return;
3220 }
3221
3222 len = strlen(str);
3223 readline_set_completion_index(rs, len);
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003224 peripheral_device_del_completion(rs, str, len);
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003225}
3226
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003227void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003228{
3229 ObjectPropertyInfoList *list, *start;
3230 size_t len;
3231
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003232 if (nb_args != 2) {
3233 return;
3234 }
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003235 len = strlen(str);
3236 readline_set_completion_index(rs, len);
3237
3238 start = list = qmp_qom_list("/objects", NULL);
3239 while (list) {
3240 ObjectPropertyInfo *info = list->value;
3241
3242 if (!strncmp(info->type, "child<", 5)
3243 && !strncmp(info->name, str, len)) {
3244 readline_add_completion(rs, info->name);
3245 }
3246 list = list->next;
3247 }
3248 qapi_free_ObjectPropertyInfoList(start);
3249}
3250
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003251void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
3252{
3253 int i;
3254 char *sep;
3255 size_t len;
3256
3257 if (nb_args != 2) {
3258 return;
3259 }
3260 sep = strrchr(str, '-');
3261 if (sep) {
3262 str = sep + 1;
3263 }
3264 len = strlen(str);
3265 readline_set_completion_index(rs, len);
Eric Blake7fb1cf12015-11-18 01:52:57 -07003266 for (i = 0; i < Q_KEY_CODE__MAX; i++) {
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003267 if (!strncmp(str, QKeyCode_lookup[i], len)) {
3268 readline_add_completion(rs, QKeyCode_lookup[i]);
3269 }
3270 }
3271}
3272
Hani Benhabiles40d19392014-05-07 23:41:30 +01003273void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
3274{
3275 size_t len;
3276
3277 len = strlen(str);
3278 readline_set_completion_index(rs, len);
3279 if (nb_args == 2) {
Jason Wangeaed4832015-04-23 14:21:38 +08003280 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles40d19392014-05-07 23:41:30 +01003281 int count, i;
3282 count = qemu_find_net_clients_except(NULL, ncs,
Eric Blakef394b2e2016-07-13 21:50:23 -06003283 NET_CLIENT_DRIVER_NONE,
Jason Wangeaed4832015-04-23 14:21:38 +08003284 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003285 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles40d19392014-05-07 23:41:30 +01003286 const char *name = ncs[i]->name;
3287 if (!strncmp(str, name, len)) {
3288 readline_add_completion(rs, name);
3289 }
3290 }
3291 } else if (nb_args == 3) {
3292 add_completion_option(rs, str, "on");
3293 add_completion_option(rs, str, "off");
3294 }
3295}
3296
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003297void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
3298{
3299 int len, count, i;
Jason Wangeaed4832015-04-23 14:21:38 +08003300 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003301
3302 if (nb_args != 2) {
3303 return;
3304 }
3305
3306 len = strlen(str);
3307 readline_set_completion_index(rs, len);
Eric Blakef394b2e2016-07-13 21:50:23 -06003308 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_DRIVER_NIC,
Jason Wangeaed4832015-04-23 14:21:38 +08003309 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003310 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003311 QemuOpts *opts;
3312 const char *name = ncs[i]->name;
3313 if (strncmp(str, name, len)) {
3314 continue;
3315 }
3316 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
3317 if (opts) {
3318 readline_add_completion(rs, name);
3319 }
3320 }
3321}
3322
Lluís Vilanovabd712112016-07-11 12:53:51 +02003323void info_trace_events_completion(ReadLineState *rs, int nb_args, const char *str)
3324{
3325 size_t len;
3326
3327 len = strlen(str);
3328 readline_set_completion_index(rs, len);
3329 if (nb_args == 2) {
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003330 TraceEventIter iter;
3331 TraceEvent *ev;
3332 char *pattern = g_strdup_printf("%s*", str);
3333 trace_event_iter_init(&iter, pattern);
3334 while ((ev = trace_event_iter_next(&iter)) != NULL) {
3335 readline_add_completion(rs, trace_event_get_name(ev));
Lluís Vilanovabd712112016-07-11 12:53:51 +02003336 }
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003337 g_free(pattern);
Lluís Vilanovabd712112016-07-11 12:53:51 +02003338 }
3339}
3340
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003341void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
3342{
3343 size_t len;
3344
3345 len = strlen(str);
3346 readline_set_completion_index(rs, len);
3347 if (nb_args == 2) {
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003348 TraceEventIter iter;
3349 TraceEvent *ev;
3350 char *pattern = g_strdup_printf("%s*", str);
3351 trace_event_iter_init(&iter, pattern);
3352 while ((ev = trace_event_iter_next(&iter)) != NULL) {
3353 readline_add_completion(rs, trace_event_get_name(ev));
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003354 }
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003355 g_free(pattern);
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003356 } else if (nb_args == 3) {
3357 add_completion_option(rs, str, "on");
3358 add_completion_option(rs, str, "off");
3359 }
3360}
3361
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003362void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
3363{
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003364 int i;
3365
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003366 if (nb_args != 2) {
3367 return;
3368 }
3369 readline_set_completion_index(rs, strlen(str));
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003370 for (i = 0; WatchdogExpirationAction_lookup[i]; i++) {
3371 add_completion_option(rs, str, WatchdogExpirationAction_lookup[i]);
3372 }
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003373}
3374
Hani Benhabilesc68a0402014-05-27 23:39:32 +01003375void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
3376 const char *str)
3377{
3378 size_t len;
3379
3380 len = strlen(str);
3381 readline_set_completion_index(rs, len);
3382 if (nb_args == 2) {
3383 int i;
Eric Blake7fb1cf12015-11-18 01:52:57 -07003384 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Hani Benhabilesc68a0402014-05-27 23:39:32 +01003385 const char *name = MigrationCapability_lookup[i];
3386 if (!strncmp(str, name, len)) {
3387 readline_add_completion(rs, name);
3388 }
3389 }
3390 } else if (nb_args == 3) {
3391 add_completion_option(rs, str, "on");
3392 add_completion_option(rs, str, "off");
3393 }
3394}
3395
Liang Li50e9a622015-03-23 16:32:29 +08003396void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
3397 const char *str)
3398{
3399 size_t len;
3400
3401 len = strlen(str);
3402 readline_set_completion_index(rs, len);
3403 if (nb_args == 2) {
3404 int i;
Eric Blake7fb1cf12015-11-18 01:52:57 -07003405 for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
Liang Li50e9a622015-03-23 16:32:29 +08003406 const char *name = MigrationParameter_lookup[i];
3407 if (!strncmp(str, name, len)) {
3408 readline_add_completion(rs, name);
3409 }
3410 }
3411 }
3412}
3413
Hani Benhabilese3bb5322014-05-27 23:39:34 +01003414void host_net_add_completion(ReadLineState *rs, int nb_args, const char *str)
3415{
3416 int i;
3417 size_t len;
3418 if (nb_args != 2) {
3419 return;
3420 }
3421 len = strlen(str);
3422 readline_set_completion_index(rs, len);
3423 for (i = 0; host_net_devices[i]; i++) {
3424 if (!strncmp(host_net_devices[i], str, len)) {
3425 readline_add_completion(rs, host_net_devices[i]);
3426 }
3427 }
3428}
3429
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003430void host_net_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3431{
Jason Wangeaed4832015-04-23 14:21:38 +08003432 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003433 int count, i, len;
3434
3435 len = strlen(str);
3436 readline_set_completion_index(rs, len);
3437 if (nb_args == 2) {
3438 count = qemu_find_net_clients_except(NULL, ncs,
Eric Blakef394b2e2016-07-13 21:50:23 -06003439 NET_CLIENT_DRIVER_NONE,
Jason Wangeaed4832015-04-23 14:21:38 +08003440 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003441 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003442 int id;
3443 char name[16];
3444
3445 if (net_hub_id_for_client(ncs[i], &id)) {
3446 continue;
3447 }
3448 snprintf(name, sizeof(name), "%d", id);
3449 if (!strncmp(str, name, len)) {
3450 readline_add_completion(rs, name);
3451 }
3452 }
3453 return;
3454 } else if (nb_args == 3) {
3455 count = qemu_find_net_clients_except(NULL, ncs,
Eric Blakef394b2e2016-07-13 21:50:23 -06003456 NET_CLIENT_DRIVER_NIC,
Jason Wangeaed4832015-04-23 14:21:38 +08003457 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003458 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Jason Wang2c4681f2015-02-02 15:06:38 +08003459 int id;
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003460 const char *name;
3461
Eric Blakef394b2e2016-07-13 21:50:23 -06003462 if (ncs[i]->info->type == NET_CLIENT_DRIVER_HUBPORT ||
Jason Wang2c4681f2015-02-02 15:06:38 +08003463 net_hub_id_for_client(ncs[i], &id)) {
3464 continue;
3465 }
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003466 name = ncs[i]->name;
3467 if (!strncmp(str, name, len)) {
3468 readline_add_completion(rs, name);
3469 }
3470 }
3471 return;
3472 }
3473}
3474
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003475static void vm_completion(ReadLineState *rs, const char *str)
3476{
3477 size_t len;
Kevin Wolf7c8eece2016-03-22 18:58:50 +01003478 BlockDriverState *bs;
Kevin Wolf88be7b42016-05-20 18:49:07 +02003479 BdrvNextIterator it;
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003480
3481 len = strlen(str);
3482 readline_set_completion_index(rs, len);
Kevin Wolf7c8eece2016-03-22 18:58:50 +01003483
Kevin Wolf88be7b42016-05-20 18:49:07 +02003484 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003485 SnapshotInfoList *snapshots, *snapshot;
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003486 AioContext *ctx = bdrv_get_aio_context(bs);
3487 bool ok = false;
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003488
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003489 aio_context_acquire(ctx);
3490 if (bdrv_can_snapshot(bs)) {
3491 ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0;
3492 }
3493 aio_context_release(ctx);
3494 if (!ok) {
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003495 continue;
3496 }
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003497
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003498 snapshot = snapshots;
3499 while (snapshot) {
3500 char *completion = snapshot->value->name;
3501 if (!strncmp(str, completion, len)) {
3502 readline_add_completion(rs, completion);
3503 }
3504 completion = snapshot->value->id;
3505 if (!strncmp(str, completion, len)) {
3506 readline_add_completion(rs, completion);
3507 }
3508 snapshot = snapshot->next;
3509 }
3510 qapi_free_SnapshotInfoList(snapshots);
3511 }
3512
3513}
3514
3515void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
3516{
3517 if (nb_args == 2) {
3518 vm_completion(rs, str);
3519 }
3520}
3521
3522void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
3523{
3524 if (nb_args == 2) {
3525 vm_completion(rs, str);
3526 }
3527}
3528
Wenchao Xiac35b6402013-08-27 20:38:24 +08003529static void monitor_find_completion_by_table(Monitor *mon,
3530 const mon_cmd_t *cmd_table,
3531 char **args,
3532 int nb_args)
bellard81d09122004-07-14 17:21:37 +00003533{
3534 const char *cmdname;
Wenchao Xiac35b6402013-08-27 20:38:24 +08003535 int i;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003536 const char *ptype, *str, *name;
Anthony Liguoric227f092009-10-01 16:12:16 -05003537 const mon_cmd_t *cmd;
Max Reitzda27a002016-03-16 19:54:29 +01003538 BlockBackend *blk = NULL;
bellard81d09122004-07-14 17:21:37 +00003539
bellard81d09122004-07-14 17:21:37 +00003540 if (nb_args <= 1) {
3541 /* command completion */
3542 if (nb_args == 0)
3543 cmdname = "";
3544 else
3545 cmdname = args[0];
Wenchao Xiad2674b22013-08-27 20:38:16 +08003546 readline_set_completion_index(mon->rs, strlen(cmdname));
Wenchao Xiac35b6402013-08-27 20:38:24 +08003547 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003548 cmd_completion(mon, cmdname, cmd->name);
bellard81d09122004-07-14 17:21:37 +00003549 }
3550 } else {
3551 /* find the command */
Wenchao Xiac35b6402013-08-27 20:38:24 +08003552 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Jan Kiszka03a63482010-06-16 00:38:33 +02003553 if (compare_cmd(args[0], cmd->name)) {
3554 break;
3555 }
bellard81d09122004-07-14 17:21:37 +00003556 }
Jan Kiszka03a63482010-06-16 00:38:33 +02003557 if (!cmd->name) {
Wenchao Xiac35b6402013-08-27 20:38:24 +08003558 return;
Jan Kiszka03a63482010-06-16 00:38:33 +02003559 }
3560
Wenchao Xiad903a772013-08-27 20:38:25 +08003561 if (cmd->sub_table) {
3562 /* do the job again */
Stefan Weile7ae7712015-03-08 19:30:01 +01003563 monitor_find_completion_by_table(mon, cmd->sub_table,
3564 &args[1], nb_args - 1);
3565 return;
Wenchao Xiad903a772013-08-27 20:38:25 +08003566 }
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003567 if (cmd->command_completion) {
Stefan Weile7ae7712015-03-08 19:30:01 +01003568 cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
3569 return;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003570 }
Wenchao Xiad903a772013-08-27 20:38:25 +08003571
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003572 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003573 for(i = 0; i < nb_args - 2; i++) {
3574 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003575 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003576 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003577 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003578 }
3579 }
3580 str = args[nb_args - 1];
Kevin Wolf48fe86f2014-11-12 16:24:02 +01003581 while (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02003582 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00003583 }
bellard81d09122004-07-14 17:21:37 +00003584 switch(*ptype) {
3585 case 'F':
3586 /* file completion */
Wenchao Xiad2674b22013-08-27 20:38:16 +08003587 readline_set_completion_index(mon->rs, strlen(str));
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003588 file_completion(mon, str);
bellard81d09122004-07-14 17:21:37 +00003589 break;
3590 case 'B':
3591 /* block device name completion */
Wenchao Xia599a9262013-08-27 20:38:15 +08003592 readline_set_completion_index(mon->rs, strlen(str));
Max Reitzda27a002016-03-16 19:54:29 +01003593 while ((blk = blk_next(blk)) != NULL) {
3594 name = blk_name(blk);
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003595 if (str[0] == '\0' ||
3596 !strncmp(name, str, strlen(str))) {
3597 readline_add_completion(mon->rs, name);
3598 }
3599 }
bellard81d09122004-07-14 17:21:37 +00003600 break;
bellard7fe48482004-10-09 18:08:01 +00003601 case 's':
Wenchao Xia129be002013-08-27 20:38:26 +08003602 case 'S':
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003603 if (!strcmp(cmd->name, "help|?")) {
Wenchao Xia7ca0e062013-08-27 20:38:27 +08003604 monitor_find_completion_by_table(mon, cmd_table,
3605 &args[1], nb_args - 1);
bellard7fe48482004-10-09 18:08:01 +00003606 }
3607 break;
bellard81d09122004-07-14 17:21:37 +00003608 default:
3609 break;
3610 }
3611 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08003612}
3613
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003614static void monitor_find_completion(void *opaque,
Wenchao Xiac35b6402013-08-27 20:38:24 +08003615 const char *cmdline)
3616{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003617 Monitor *mon = opaque;
Wenchao Xiac35b6402013-08-27 20:38:24 +08003618 char *args[MAX_ARGS];
3619 int nb_args, len;
3620
3621 /* 1. parse the cmdline */
3622 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
3623 return;
3624 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08003625
3626 /* if the line ends with a space, it means we want to complete the
3627 next arg */
3628 len = strlen(cmdline);
3629 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
3630 if (nb_args >= MAX_ARGS) {
3631 goto cleanup;
3632 }
3633 args[nb_args++] = g_strdup("");
3634 }
3635
3636 /* 2. auto complete according to args */
3637 monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args);
Jan Kiszka03a63482010-06-16 00:38:33 +02003638
3639cleanup:
Wenchao Xiadcc70cd2013-08-27 20:38:22 +08003640 free_cmdline_args(args, nb_args);
bellard81d09122004-07-14 17:21:37 +00003641}
3642
aliguori731b0362009-03-05 23:01:42 +00003643static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003644{
aliguori731b0362009-03-05 23:01:42 +00003645 Monitor *mon = opaque;
3646
Jan Kiszkac62313b2009-12-04 14:05:29 +01003647 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003648}
3649
Marc-André Lureaud79bedf2016-09-12 13:18:59 +04003650static bool invalid_qmp_mode(const Monitor *mon, const char *cmd,
Markus Armbruster40861822015-05-29 10:27:16 +02003651 Error **errp)
Luiz Capitulino09069b12010-02-04 18:10:06 -02003652{
Marc-André Lureaud79bedf2016-09-12 13:18:59 +04003653 bool is_cap = g_str_equal(cmd, "qmp_capabilities");
Markus Armbrusterf994b252015-03-06 19:51:51 +01003654
3655 if (is_cap && mon->qmp.in_command_mode) {
Markus Armbruster40861822015-05-29 10:27:16 +02003656 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
3657 "Capabilities negotiation is already complete, command "
Marc-André Lureaud79bedf2016-09-12 13:18:59 +04003658 "'%s' ignored", cmd);
Eric Blake2d5a8342015-04-15 09:19:23 -06003659 return true;
3660 }
Markus Armbrusterf994b252015-03-06 19:51:51 +01003661 if (!is_cap && !mon->qmp.in_command_mode) {
Markus Armbruster40861822015-05-29 10:27:16 +02003662 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
3663 "Expecting capabilities negotiation with "
Marc-André Lureaud79bedf2016-09-12 13:18:59 +04003664 "'qmp_capabilities' before command '%s'", cmd);
Eric Blake2d5a8342015-04-15 09:19:23 -06003665 return true;
3666 }
3667 return false;
Luiz Capitulino09069b12010-02-04 18:10:06 -02003668}
3669
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003670/*
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003671 * Input object checking rules
3672 *
3673 * 1. Input object must be a dict
3674 * 2. The "execute" key must exist
3675 * 3. The "execute" key must be a string
3676 * 4. If the "arguments" key exists, it must be a dict
3677 * 5. If the "id" key exists, it can be anything (ie. json-value)
3678 * 6. Any argument not listed above is considered invalid
3679 */
Markus Armbrusterba0510a2015-03-02 18:41:43 +01003680static QDict *qmp_check_input_obj(QObject *input_obj, Error **errp)
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003681{
3682 const QDictEntry *ent;
3683 int has_exec_key = 0;
3684 QDict *input_dict;
3685
3686 if (qobject_type(input_obj) != QTYPE_QDICT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003687 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "object");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003688 return NULL;
3689 }
3690
3691 input_dict = qobject_to_qdict(input_obj);
3692
3693 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
3694 const char *arg_name = qdict_entry_key(ent);
3695 const QObject *arg_obj = qdict_entry_value(ent);
3696
3697 if (!strcmp(arg_name, "execute")) {
3698 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003699 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
3700 "execute", "string");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003701 return NULL;
3702 }
3703 has_exec_key = 1;
3704 } else if (!strcmp(arg_name, "arguments")) {
3705 if (qobject_type(arg_obj) != QTYPE_QDICT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003706 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
3707 "arguments", "object");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003708 return NULL;
3709 }
Markus Armbruster779cec42015-06-08 10:44:30 +02003710 } else if (!strcmp(arg_name, "id")) {
3711 /* Any string is acceptable as "id", so nothing to check */
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003712 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003713 error_setg(errp, QERR_QMP_EXTRA_MEMBER, arg_name);
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003714 return NULL;
3715 }
3716 }
3717
3718 if (!has_exec_key) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003719 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "execute");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003720 return NULL;
3721 }
3722
3723 return input_dict;
3724}
3725
Paolo Bonzini95385fe2015-11-25 22:23:31 +01003726static void handle_qmp_command(JSONMessageParser *parser, GQueue *tokens)
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003727{
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04003728 QObject *req, *rsp = NULL, *id = NULL;
3729 QDict *qdict = NULL;
Luiz Capitulino40e5a012011-10-21 16:15:31 -02003730 const char *cmd_name;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003731 Monitor *mon = cur_mon;
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04003732 Error *err = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003733
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04003734 req = json_parser_parse_err(tokens, NULL, &err);
3735 if (err || !req || qobject_type(req) != QTYPE_QDICT) {
3736 if (!err) {
3737 error_setg(&err, QERR_JSON_PARSING);
3738 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003739 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003740 }
3741
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04003742 qdict = qmp_check_input_obj(req, &err);
3743 if (!qdict) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003744 goto err_out;
3745 }
3746
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04003747 id = qdict_get(qdict, "id");
3748 qobject_incref(id);
3749 qdict_del(qdict, "id");
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003750
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04003751 cmd_name = qdict_get_str(qdict, "execute");
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +01003752 trace_handle_qmp_command(mon, cmd_name);
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04003753
3754 if (invalid_qmp_mode(mon, cmd_name, &err)) {
Eric Blake2d5a8342015-04-15 09:19:23 -06003755 goto err_out;
3756 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003757
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04003758 rsp = qmp_dispatch(req);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003759
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003760err_out:
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04003761 if (err) {
3762 qdict = qdict_new();
3763 qdict_put_obj(qdict, "error", qmp_build_error_object(err));
3764 error_free(err);
3765 rsp = QOBJECT(qdict);
3766 }
3767
3768 if (rsp) {
3769 if (id) {
3770 qdict_put_obj(qobject_to_qdict(rsp), "id", id);
3771 id = NULL;
3772 }
3773
3774 monitor_json_emitter(mon, rsp);
3775 }
3776
3777 qobject_decref(id);
3778 qobject_decref(rsp);
3779 qobject_decref(req);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003780}
3781
Markus Armbrusterc83fe232015-03-06 19:20:51 +01003782static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003783{
3784 Monitor *old_mon = cur_mon;
3785
3786 cur_mon = opaque;
3787
Markus Armbruster74358f22015-03-06 19:35:59 +01003788 json_message_parser_feed(&cur_mon->qmp.parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003789
3790 cur_mon = old_mon;
3791}
3792
aliguori731b0362009-03-05 23:01:42 +00003793static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00003794{
aliguori731b0362009-03-05 23:01:42 +00003795 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00003796 int i;
aliguori376253e2009-03-05 23:01:23 +00003797
aliguori731b0362009-03-05 23:01:42 +00003798 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00003799
aliguoricde76ee2009-03-05 23:01:51 +00003800 if (cur_mon->rs) {
3801 for (i = 0; i < size; i++)
3802 readline_handle_byte(cur_mon->rs, buf[i]);
3803 } else {
3804 if (size == 0 || buf[size - 1] != 0)
3805 monitor_printf(cur_mon, "corrupted command\n");
3806 else
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003807 handle_hmp_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00003808 }
aliguori731b0362009-03-05 23:01:42 +00003809
3810 cur_mon = old_mon;
3811}
aliguorid8f44602008-10-06 13:52:44 +00003812
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003813static void monitor_command_cb(void *opaque, const char *cmdline,
3814 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00003815{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003816 Monitor *mon = opaque;
3817
aliguori731b0362009-03-05 23:01:42 +00003818 monitor_suspend(mon);
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003819 handle_hmp_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00003820 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00003821}
3822
aliguoricde76ee2009-03-05 23:01:51 +00003823int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003824{
aliguoricde76ee2009-03-05 23:01:51 +00003825 if (!mon->rs)
3826 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00003827 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00003828 return 0;
aliguorid8f44602008-10-06 13:52:44 +00003829}
3830
aliguori376253e2009-03-05 23:01:23 +00003831void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003832{
aliguoricde76ee2009-03-05 23:01:51 +00003833 if (!mon->rs)
3834 return;
aliguori731b0362009-03-05 23:01:42 +00003835 if (--mon->suspend_cnt == 0)
3836 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00003837}
3838
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003839static QObject *get_qmp_greeting(void)
3840{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03003841 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003842
Markus Armbruster7fad30f2015-09-16 13:06:19 +02003843 qmp_marshal_query_version(NULL, &ver, NULL);
Marc-André Lureauc8235012016-09-12 13:19:04 +04003844
3845 return qobject_from_jsonf("{'QMP': {'version': %p, 'capabilities': []}}",
3846 ver);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003847}
3848
Markus Armbrusterc83fe232015-03-06 19:20:51 +01003849static void monitor_qmp_event(void *opaque, int event)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003850{
Luiz Capitulino47116d12010-02-08 17:01:30 -02003851 QObject *data;
3852 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003853
Luiz Capitulino47116d12010-02-08 17:01:30 -02003854 switch (event) {
3855 case CHR_EVENT_OPENED:
Markus Armbrusterf994b252015-03-06 19:51:51 +01003856 mon->qmp.in_command_mode = false;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003857 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003858 monitor_json_emitter(mon, data);
3859 qobject_decref(data);
Corey Bryantefb87c12012-08-14 16:43:48 -04003860 mon_refcount++;
Luiz Capitulino47116d12010-02-08 17:01:30 -02003861 break;
3862 case CHR_EVENT_CLOSED:
Markus Armbruster74358f22015-03-06 19:35:59 +01003863 json_message_parser_destroy(&mon->qmp.parser);
3864 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Corey Bryantefb87c12012-08-14 16:43:48 -04003865 mon_refcount--;
3866 monitor_fdsets_cleanup();
Luiz Capitulino47116d12010-02-08 17:01:30 -02003867 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003868 }
3869}
3870
aliguori731b0362009-03-05 23:01:42 +00003871static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00003872{
aliguori376253e2009-03-05 23:01:23 +00003873 Monitor *mon = opaque;
3874
aliguori2724b182009-03-05 23:01:47 +00003875 switch (event) {
3876 case CHR_EVENT_MUX_IN:
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003877 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003878 mon->mux_out = 0;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003879 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003880 if (mon->reset_seen) {
3881 readline_restart(mon->rs);
3882 monitor_resume(mon);
3883 monitor_flush(mon);
3884 } else {
3885 mon->suspend_cnt = 0;
3886 }
aliguori2724b182009-03-05 23:01:47 +00003887 break;
ths86e94de2007-01-05 22:01:59 +00003888
aliguori2724b182009-03-05 23:01:47 +00003889 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003890 if (mon->reset_seen) {
3891 if (mon->suspend_cnt == 0) {
3892 monitor_printf(mon, "\n");
3893 }
3894 monitor_flush(mon);
3895 monitor_suspend(mon);
3896 } else {
3897 mon->suspend_cnt++;
3898 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003899 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003900 mon->mux_out = 1;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003901 qemu_mutex_unlock(&mon->out_lock);
aliguori2724b182009-03-05 23:01:47 +00003902 break;
3903
Amit Shahb6b8df52009-10-07 18:31:16 +05303904 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00003905 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3906 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003907 if (!mon->mux_out) {
Stratos Psomadakise5554e22014-09-15 15:34:57 +03003908 readline_restart(mon->rs);
aliguori2724b182009-03-05 23:01:47 +00003909 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003910 }
3911 mon->reset_seen = 1;
Corey Bryantefb87c12012-08-14 16:43:48 -04003912 mon_refcount++;
3913 break;
3914
3915 case CHR_EVENT_CLOSED:
3916 mon_refcount--;
3917 monitor_fdsets_cleanup();
aliguori2724b182009-03-05 23:01:47 +00003918 break;
3919 }
ths86e94de2007-01-05 22:01:59 +00003920}
3921
Wayne Xia816f8922011-10-12 11:32:41 +08003922static int
3923compare_mon_cmd(const void *a, const void *b)
3924{
3925 return strcmp(((const mon_cmd_t *)a)->name,
3926 ((const mon_cmd_t *)b)->name);
3927}
3928
3929static void sortcmdlist(void)
3930{
3931 int array_num;
3932 int elem_size = sizeof(mon_cmd_t);
3933
3934 array_num = sizeof(mon_cmds)/elem_size-1;
3935 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
3936
3937 array_num = sizeof(info_cmds)/elem_size-1;
3938 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
3939}
3940
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003941/* These functions just adapt the readline interface in a typesafe way. We
3942 * could cast function pointers but that discards compiler checks.
3943 */
Stefan Weild5d15072014-01-25 18:18:23 +01003944static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
3945 const char *fmt, ...)
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003946{
3947 va_list ap;
3948 va_start(ap, fmt);
3949 monitor_vprintf(opaque, fmt, ap);
3950 va_end(ap);
3951}
3952
3953static void monitor_readline_flush(void *opaque)
3954{
3955 monitor_flush(opaque);
3956}
3957
Paolo Bonzini397d30e2016-10-24 18:31:02 +02003958/*
3959 * Print to current monitor if we have one, else to stderr.
3960 * TODO should return int, so callers can calculate width, but that
3961 * requires surgery to monitor_vprintf(). Left for another day.
3962 */
3963void error_vprintf(const char *fmt, va_list ap)
3964{
3965 if (cur_mon && !monitor_cur_is_qmp()) {
3966 monitor_vprintf(cur_mon, fmt, ap);
3967 } else {
3968 vfprintf(stderr, fmt, ap);
3969 }
3970}
3971
3972void error_vprintf_unless_qmp(const char *fmt, va_list ap)
3973{
3974 if (cur_mon && !monitor_cur_is_qmp()) {
3975 monitor_vprintf(cur_mon, fmt, ap);
Marc-André Lureau0d6b50d2017-01-05 14:59:57 +01003976 } else if (!cur_mon) {
3977 vfprintf(stderr, fmt, ap);
Paolo Bonzini397d30e2016-10-24 18:31:02 +02003978 }
3979}
3980
Paolo Bonzinid622cb52014-06-18 08:44:00 +02003981static void __attribute__((constructor)) monitor_lock_init(void)
3982{
3983 qemu_mutex_init(&monitor_lock);
3984}
3985
aliguori731b0362009-03-05 23:01:42 +00003986void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00003987{
aliguori731b0362009-03-05 23:01:42 +00003988 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00003989 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00003990
3991 if (is_first_init) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +02003992 monitor_qapi_event_init();
Wenchao Xiad0383172013-08-27 20:38:18 +08003993 sortcmdlist();
ths20d8a3e2007-02-18 17:04:49 +00003994 is_first_init = 0;
3995 }
aliguori87127162009-03-05 23:01:29 +00003996
Wenchao Xiab01fe892013-08-27 20:38:19 +08003997 mon = g_malloc(sizeof(*mon));
3998 monitor_data_init(mon);
ths20d8a3e2007-02-18 17:04:49 +00003999
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03004000 qemu_chr_fe_init(&mon->chr, chr, &error_abort);
aliguori731b0362009-03-05 23:01:42 +00004001 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004002 if (flags & MONITOR_USE_READLINE) {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004003 mon->rs = readline_init(monitor_readline_printf,
4004 monitor_readline_flush,
4005 mon,
4006 monitor_find_completion);
aliguoricde76ee2009-03-05 23:01:51 +00004007 monitor_read_command(mon, 0);
4008 }
aliguori87127162009-03-05 23:01:29 +00004009
Markus Armbruster9f3982f2015-03-06 19:56:38 +01004010 if (monitor_is_qmp(mon)) {
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03004011 qemu_chr_fe_set_handlers(&mon->chr, monitor_can_read, monitor_qmp_read,
Marc-André Lureau39ab61c2016-10-22 12:53:03 +03004012 monitor_qmp_event, mon, NULL, true);
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03004013 qemu_chr_fe_set_echo(&mon->chr, true);
Markus Armbruster74358f22015-03-06 19:35:59 +01004014 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004015 } else {
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03004016 qemu_chr_fe_set_handlers(&mon->chr, monitor_can_read, monitor_read,
Marc-André Lureau39ab61c2016-10-22 12:53:03 +03004017 monitor_event, mon, NULL, true);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004018 }
aliguori87127162009-03-05 23:01:29 +00004019
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004020 qemu_mutex_lock(&monitor_lock);
Blue Swirl72cf2d42009-09-12 07:36:22 +00004021 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004022 qemu_mutex_unlock(&monitor_lock);
bellard7e2515e2004-08-01 21:52:19 +00004023}
4024
Marc-André Lureau2ef45712016-08-01 15:23:42 +04004025void monitor_cleanup(void)
4026{
4027 Monitor *mon, *next;
4028
4029 qemu_mutex_lock(&monitor_lock);
4030 QLIST_FOREACH_SAFE(mon, &mon_list, entry, next) {
4031 QLIST_REMOVE(mon, entry);
4032 monitor_data_destroy(mon);
4033 g_free(mon);
4034 }
4035 qemu_mutex_unlock(&monitor_lock);
4036}
4037
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004038static void bdrv_password_cb(void *opaque, const char *password,
4039 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00004040{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004041 Monitor *mon = opaque;
4042 BlockDriverState *bs = readline_opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004043 int ret = 0;
Markus Armbruster4d2855a2015-01-29 10:37:00 +01004044 Error *local_err = NULL;
bellard7e2515e2004-08-01 21:52:19 +00004045
Markus Armbruster4d2855a2015-01-29 10:37:00 +01004046 bdrv_add_key(bs, password, &local_err);
4047 if (local_err) {
Markus Armbruster193227f2015-12-18 16:35:06 +01004048 error_report_err(local_err);
aliguoribb5fc202009-03-05 23:01:15 +00004049 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004050 }
aliguori731b0362009-03-05 23:01:42 +00004051 if (mon->password_completion_cb)
4052 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004053
aliguori731b0362009-03-05 23:01:42 +00004054 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004055}
aliguoric0f4ce72009-03-05 23:01:01 +00004056
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004057int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
Markus Armbruster097310b2014-10-07 13:59:15 +02004058 BlockCompletionFunc *completion_cb,
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004059 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004060{
aliguoricde76ee2009-03-05 23:01:51 +00004061 int err;
4062
aliguori376253e2009-03-05 23:01:23 +00004063 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4064 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00004065
aliguori731b0362009-03-05 23:01:42 +00004066 mon->password_completion_cb = completion_cb;
4067 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004068
aliguoricde76ee2009-03-05 23:01:51 +00004069 err = monitor_read_password(mon, bdrv_password_cb, bs);
4070
4071 if (err && completion_cb)
4072 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004073
4074 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00004075}
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004076
4077int monitor_read_block_device_key(Monitor *mon, const char *device,
Markus Armbruster097310b2014-10-07 13:59:15 +02004078 BlockCompletionFunc *completion_cb,
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004079 void *opaque)
4080{
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01004081 Error *err = NULL;
Fam Zheng55606252015-03-02 19:36:46 +08004082 BlockBackend *blk;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004083
Fam Zheng55606252015-03-02 19:36:46 +08004084 blk = blk_by_name(device);
4085 if (!blk) {
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004086 monitor_printf(mon, "Device not found %s\n", device);
4087 return -1;
4088 }
Max Reitz5433c242015-10-19 17:53:29 +02004089 if (!blk_bs(blk)) {
4090 monitor_printf(mon, "Device '%s' has no medium\n", device);
4091 return -1;
4092 }
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004093
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01004094 bdrv_add_key(blk_bs(blk), NULL, &err);
4095 if (err) {
4096 error_free(err);
4097 return monitor_read_bdrv_key_start(mon, blk_bs(blk), completion_cb, opaque);
4098 }
4099
4100 if (completion_cb) {
4101 completion_cb(opaque, 0);
4102 }
4103 return 0;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004104}
Paolo Bonzini4d454572012-11-26 16:03:42 +01004105
4106QemuOptsList qemu_mon_opts = {
4107 .name = "mon",
4108 .implied_opt_name = "chardev",
4109 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
4110 .desc = {
4111 {
4112 .name = "mode",
4113 .type = QEMU_OPT_STRING,
4114 },{
4115 .name = "chardev",
4116 .type = QEMU_OPT_STRING,
4117 },{
Marc-André Lureaubdbcb542016-10-11 21:41:21 +04004118 .name = "default", /* deprecated */
Paolo Bonzini4d454572012-11-26 16:03:42 +01004119 .type = QEMU_OPT_BOOL,
4120 },{
4121 .name = "pretty",
4122 .type = QEMU_OPT_BOOL,
4123 },
4124 { /* end of list */ }
4125 },
4126};
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004127
4128#ifndef TARGET_I386
4129void qmp_rtc_reset_reinjection(Error **errp)
4130{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004131 error_setg(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection");
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004132}
4133#endif
Jason J. Herne7ee0c3e2015-06-26 14:03:16 -04004134
4135#ifndef TARGET_S390X
4136void qmp_dump_skeys(const char *filename, Error **errp)
4137{
4138 error_setg(errp, QERR_FEATURE_DISABLED, "dump-skeys");
4139}
4140#endif
Peter Xuae50a772016-03-30 17:27:24 +01004141
4142#ifndef TARGET_ARM
4143GICCapabilityList *qmp_query_gic_capabilities(Error **errp)
4144{
4145 error_setg(errp, QERR_FEATURE_DISABLED, "query-gic-capabilities");
4146 return NULL;
4147}
4148#endif
Igor Mammedovd4633542016-06-10 06:29:06 +05304149
4150HotpluggableCPUList *qmp_query_hotpluggable_cpus(Error **errp)
4151{
4152 MachineState *ms = MACHINE(qdev_get_machine());
4153 MachineClass *mc = MACHINE_GET_CLASS(ms);
4154
4155 if (!mc->query_hotpluggable_cpus) {
4156 error_setg(errp, QERR_FEATURE_DISABLED, "query-hotpluggable-cpus");
4157 return NULL;
4158 }
4159
4160 return mc->query_hotpluggable_cpus(ms);
4161}