blob: 6d040e620f7b79583490654a247f409cd7f2b942 [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"
Marc-André Lureau4d43a602017-01-26 18:26:44 +040038#include "chardev/char-fe.h"
Gerd Hoffmann75721502010-10-07 12:22:54 +020039#include "ui/qemu-spice.h"
Eduardo Habkoste35704b2015-02-08 16:51:16 -020040#include "sysemu/numa.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010041#include "monitor/monitor.h"
Marc-André Lureau213dcb02016-12-12 20:22:24 +030042#include "qemu/config-file.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"
Vincent Palatinb3946622017-01-10 11:59:55 +010052#include "sysemu/hw_accel.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010053#include "qemu/acl.h"
Paolo Bonzinibdee56f2013-04-02 18:28:41 +020054#include "sysemu/tpm.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010055#include "qapi/qmp/qerror.h"
Eric Blakec7eb39c2016-06-09 10:48:32 -060056#include "qapi/qmp/types.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010057#include "qapi/qmp/qjson.h"
58#include "qapi/qmp/json-streamer.h"
59#include "qapi/qmp/json-parser.h"
Markus Armbrustera9c94272016-06-22 19:11:19 +020060#include "qom/object_interfaces.h"
Daniel P. Berrange0ab8ed12017-01-25 16:14:15 +000061#include "trace-root.h"
Lluís31965ae2011-08-31 20:31:24 +020062#include "trace/control.h"
Pavel Butsykinbf957282015-09-10 18:38:59 +030063#include "monitor/hmp-target.h"
Lluís6d8a7642011-08-31 20:30:43 +020064#ifdef CONFIG_TRACE_SIMPLE
Lluís31965ae2011-08-31 20:31:24 +020065#include "trace/simple.h"
Prerna Saxena22890ab2010-06-24 17:04:53 +053066#endif
Paolo Bonzini022c62c2012-12-17 18:19:49 +010067#include "exec/memory.h"
Paolo Bonzini63c91552016-03-15 13:18:37 +010068#include "exec/exec-all.h"
Paolo Bonzini03dd0242015-12-15 13:16:16 +010069#include "qemu/log.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050070#include "qmp-commands.h"
71#include "hmp.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010072#include "qemu/thread.h"
Hani Benhabilesb21631f2014-05-27 23:39:37 +010073#include "block/qapi.h"
Wenchao Xia43a14cf2014-06-18 08:43:31 +020074#include "qapi/qmp-event.h"
75#include "qapi-event.h"
Markus Armbruster39a18152015-09-16 13:06:28 +020076#include "qmp-introspect.h"
Alberto Garciadc599972016-03-10 13:55:26 +020077#include "sysemu/qtest.h"
Paolo Bonzinid2528bd2017-03-03 12:01:16 +010078#include "sysemu/cpus.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"
Claudio Imbrendaf860d492016-08-15 18:44:04 +020084#include "hw/s390x/storage-attributes.h"
Jason J. Hernea4538a52015-06-26 14:07:21 -040085#endif
86
bellard9307c4c2004-04-04 12:57:25 +000087/*
88 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000089 *
bellard9307c4c2004-04-04 12:57:25 +000090 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000091 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000092 * 's' string (accept optional quote)
Wenchao Xia129be002013-08-27 20:38:26 +080093 * 'S' it just appends the rest of the string (accept optional quote)
Markus Armbruster361127d2010-02-10 20:24:35 +010094 * 'O' option string of the form NAME=VALUE,...
95 * parsed according to QemuOptsList given by its name
96 * Example: 'device:O' uses qemu_device_opts.
97 * Restriction: only lists with empty desc are supported
98 * TODO lift the restriction
bellard92a31b12005-02-10 22:00:52 +000099 * 'i' 32 bit integer
100 * 'l' target long (32 or 64 bit)
Luiz Capitulino91162842012-04-26 17:34:30 -0300101 * 'M' Non-negative target long (32 or 64 bit), in user mode the
102 * value is multiplied by 2^20 (think Mebibyte)
Jes Sorensendbc0c672010-10-21 17:15:47 +0200103 * 'o' octets (aka bytes)
Kevin Wolf5e009842013-06-05 14:19:27 +0200104 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
105 * K, k suffix, which multiplies the value by 2^60 for suffixes E
106 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
107 * 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 +0100108 * 'T' double
109 * user mode accepts an optional ms, us, ns suffix,
110 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +0000111 * '/' optional gdb-like print format (like "/10x")
112 *
Luiz Capitulinofb466602009-08-28 15:27:27 -0300113 * '?' optional type (for all types, except '/')
114 * '.' other form of optional type (for 'i' and 'l')
Markus Armbruster942cd1f2010-03-26 09:07:09 +0100115 * 'b' boolean
116 * user mode accepts "on" or "off"
Luiz Capitulinofb466602009-08-28 15:27:27 -0300117 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +0000118 *
119 */
120
Anthony Liguoric227f092009-10-01 16:12:16 -0500121typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +0000122 const char *name;
bellard9307c4c2004-04-04 12:57:25 +0000123 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +0000124 const char *params;
125 const char *help;
Marc-André Lureau2b9e3572016-09-12 13:19:06 +0400126 void (*cmd)(Monitor *mon, const QDict *qdict);
127 /* @sub_table is a list of 2nd level of commands. If it does not exist,
128 * cmd should be used. If it exists, sub_table[?].cmd should be
129 * used, and cmd of 1st level plays the role of help function.
Wenchao Xia5f3d3352013-01-14 14:06:27 +0800130 */
131 struct mon_cmd_t *sub_table;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +0100132 void (*command_completion)(ReadLineState *rs, int nb_args, const char *str);
Anthony Liguoric227f092009-10-01 16:12:16 -0500133} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000134
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100135/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500136typedef struct mon_fd_t mon_fd_t;
137struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100138 char *name;
139 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500140 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100141};
142
Corey Bryantba1c0482012-08-14 16:43:43 -0400143/* file descriptor associated with a file descriptor set */
144typedef struct MonFdsetFd MonFdsetFd;
145struct MonFdsetFd {
146 int fd;
147 bool removed;
148 char *opaque;
149 QLIST_ENTRY(MonFdsetFd) next;
150};
151
152/* file descriptor set containing fds passed via SCM_RIGHTS */
153typedef struct MonFdset MonFdset;
154struct MonFdset {
155 int64_t id;
156 QLIST_HEAD(, MonFdsetFd) fds;
Corey Bryantadb696f2012-08-14 16:43:47 -0400157 QLIST_HEAD(, MonFdsetFd) dup_fds;
Corey Bryantba1c0482012-08-14 16:43:43 -0400158 QLIST_ENTRY(MonFdset) next;
159};
160
Markus Armbruster74358f22015-03-06 19:35:59 +0100161typedef struct {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200162 JSONMessageParser parser;
Markus Armbrusterf994b252015-03-06 19:51:51 +0100163 /*
164 * When a client connects, we're in capabilities negotiation mode.
165 * When command qmp_capabilities succeeds, we go into command
166 * mode.
167 */
Markus Armbruster635db182017-03-03 13:32:27 +0100168 QmpCommandList *commands;
Markus Armbruster74358f22015-03-06 19:35:59 +0100169} MonitorQMP;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200170
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100171/*
172 * To prevent flooding clients, events can be throttled. The
173 * throttling is calculated globally, rather than per-Monitor
174 * instance.
175 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200176typedef struct MonitorQAPIEventState {
Markus Armbruster7de0be62015-10-15 17:08:35 +0200177 QAPIEvent event; /* Throttling state for this event type and... */
178 QDict *data; /* ... data, see qapi_event_throttle_equal() */
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100179 QEMUTimer *timer; /* Timer for handling delayed events */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200180 QDict *qdict; /* Delayed event (if any) */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200181} MonitorQAPIEventState;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100182
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200183typedef struct {
184 int64_t rate; /* Minimum time (in ns) between two events */
185} MonitorQAPIEventConf;
186
aliguori87127162009-03-05 23:01:29 +0000187struct Monitor {
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300188 CharBackend chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200189 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000190 int flags;
191 int suspend_cnt;
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400192 bool skip_flush;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200193
194 QemuMutex out_lock;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400195 QString *outbuf;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200196 guint out_watch;
197
198 /* Read under either BQL or out_lock, written with BQL+out_lock. */
199 int mux_out;
200
aliguori731b0362009-03-05 23:01:42 +0000201 ReadLineState *rs;
Markus Armbruster74358f22015-03-06 19:35:59 +0100202 MonitorQMP qmp;
Andreas Färbercb446ec2013-05-01 14:24:52 +0200203 CPUState *mon_cpu;
Markus Armbruster097310b2014-10-07 13:59:15 +0200204 BlockCompletionFunc *password_completion_cb;
aliguori731b0362009-03-05 23:01:42 +0000205 void *password_opaque;
Wenchao Xia77172392013-08-27 20:38:20 +0800206 mon_cmd_t *cmd_table;
Anthony Liguoric227f092009-10-01 16:12:16 -0500207 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000208 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000209};
210
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300211/* QMP checker flags */
212#define QMP_ACCEPT_UNKNOWNS 1
213
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200214/* Protects mon_list, monitor_event_state. */
215static QemuMutex monitor_lock;
216
Blue Swirl72cf2d42009-09-12 07:36:22 +0000217static QLIST_HEAD(mon_list, Monitor) mon_list;
Corey Bryantba1c0482012-08-14 16:43:43 -0400218static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
Corey Bryantefb87c12012-08-14 16:43:48 -0400219static int mon_refcount;
bellard7e2515e2004-08-01 21:52:19 +0000220
Wayne Xia816f8922011-10-12 11:32:41 +0800221static mon_cmd_t mon_cmds[];
222static mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000223
Markus Armbruster635db182017-03-03 13:32:27 +0100224QmpCommandList qmp_commands, qmp_cap_negotiation_commands;
Markus Armbruster1527bad2017-03-03 13:32:25 +0100225
Markus Armbruster8631b602010-02-18 11:41:55 +0100226Monitor *cur_mon;
aliguori376253e2009-03-05 23:01:23 +0000227
Alberto Garciadc599972016-03-10 13:55:26 +0200228static QEMUClockType event_clock_type = QEMU_CLOCK_REALTIME;
229
Stefan Hajnoczic60bf332013-11-14 11:54:14 +0100230static void monitor_command_cb(void *opaque, const char *cmdline,
231 void *readline_opaque);
aliguori83ab7952008-08-19 14:44:22 +0000232
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100233/**
234 * Is @mon a QMP monitor?
235 */
236static inline bool monitor_is_qmp(const Monitor *mon)
Luiz Capitulino418173c2009-11-26 22:58:51 -0200237{
238 return (mon->flags & MONITOR_USE_CONTROL);
239}
240
Markus Armbruster489653b2015-03-06 20:01:05 +0100241/**
242 * Is the current monitor, if any, a QMP monitor?
243 */
244bool monitor_cur_is_qmp(void)
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100245{
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100246 return cur_mon && monitor_is_qmp(cur_mon);
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100247}
248
Anthony Liguori7060b472011-09-02 12:34:50 -0500249void monitor_read_command(Monitor *mon, int show_prompt)
aliguori731b0362009-03-05 23:01:42 +0000250{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200251 if (!mon->rs)
252 return;
253
aliguori731b0362009-03-05 23:01:42 +0000254 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
255 if (show_prompt)
256 readline_show_prompt(mon->rs);
257}
bellard6a00d602005-11-21 23:25:50 +0000258
Anthony Liguori7060b472011-09-02 12:34:50 -0500259int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
260 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000261{
Markus Armbrusterbcf5d192015-03-12 17:26:46 +0100262 if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000263 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
264 /* prompt is printed on return from the command handler */
265 return 0;
266 } else {
267 monitor_printf(mon, "terminal does not support password prompting\n");
268 return -ENOTTY;
269 }
aliguoribb5fc202009-03-05 23:01:15 +0000270}
271
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200272static void monitor_flush_locked(Monitor *mon);
273
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100274static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
275 void *opaque)
276{
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200277 Monitor *mon = opaque;
278
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200279 qemu_mutex_lock(&mon->out_lock);
280 mon->out_watch = 0;
281 monitor_flush_locked(mon);
282 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100283 return FALSE;
284}
285
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200286/* Called with mon->out_lock held. */
287static void monitor_flush_locked(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000288{
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100289 int rc;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400290 size_t len;
291 const char *buf;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100292
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400293 if (mon->skip_flush) {
294 return;
295 }
296
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400297 buf = qstring_get_str(mon->outbuf);
298 len = qstring_get_length(mon->outbuf);
299
Paolo Bonzinia4cc73d2013-05-31 14:00:27 +0200300 if (len && !mon->mux_out) {
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300301 rc = qemu_chr_fe_write(&mon->chr, (const uint8_t *) buf, len);
Stratos Psomadakis056f49f2014-01-27 12:30:15 +0200302 if ((rc < 0 && errno != EAGAIN) || (rc == len)) {
303 /* all flushed or error */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400304 QDECREF(mon->outbuf);
305 mon->outbuf = qstring_new();
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100306 return;
307 }
308 if (rc > 0) {
Eric Blake3b7c78c2016-05-17 16:00:15 -0600309 /* partial write */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400310 QString *tmp = qstring_from_str(buf + rc);
311 QDECREF(mon->outbuf);
312 mon->outbuf = tmp;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100313 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200314 if (mon->out_watch == 0) {
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300315 mon->out_watch =
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300316 qemu_chr_fe_add_watch(&mon->chr, G_IO_OUT | G_IO_HUP,
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300317 monitor_unblocked, mon);
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200318 }
bellard7e2515e2004-08-01 21:52:19 +0000319 }
320}
321
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200322void monitor_flush(Monitor *mon)
323{
324 qemu_mutex_lock(&mon->out_lock);
325 monitor_flush_locked(mon);
326 qemu_mutex_unlock(&mon->out_lock);
327}
328
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400329/* flush at every end of line */
aliguori376253e2009-03-05 23:01:23 +0000330static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000331{
ths60fe76f2007-12-16 03:02:09 +0000332 char c;
aliguori731b0362009-03-05 23:01:42 +0000333
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200334 qemu_mutex_lock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000335 for(;;) {
336 c = *str++;
337 if (c == '\0')
338 break;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400339 if (c == '\n') {
340 qstring_append_chr(mon->outbuf, '\r');
341 }
342 qstring_append_chr(mon->outbuf, c);
343 if (c == '\n') {
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200344 monitor_flush_locked(mon);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400345 }
bellard7e2515e2004-08-01 21:52:19 +0000346 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200347 qemu_mutex_unlock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000348}
349
aliguori376253e2009-03-05 23:01:23 +0000350void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000351{
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400352 char *buf;
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200353
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200354 if (!mon)
355 return;
356
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100357 if (monitor_is_qmp(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200358 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200359 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200360
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400361 buf = g_strdup_vprintf(fmt, ap);
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200362 monitor_puts(mon, buf);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400363 g_free(buf);
bellard7e2515e2004-08-01 21:52:19 +0000364}
365
aliguori376253e2009-03-05 23:01:23 +0000366void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000367{
368 va_list ap;
369 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000370 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000371 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000372}
373
Pavel Butsykincaf15312015-09-22 16:18:17 +0300374int monitor_fprintf(FILE *stream, const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000375{
376 va_list ap;
377 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000378 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000379 va_end(ap);
380 return 0;
381}
382
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200383static void monitor_json_emitter(Monitor *mon, const QObject *data)
384{
385 QString *json;
386
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200387 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
388 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200389 assert(json != NULL);
390
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200391 qstring_append_chr(json, '\n');
392 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200393
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200394 QDECREF(json);
395}
396
Eric Blake7fb1cf12015-11-18 01:52:57 -0700397static MonitorQAPIEventConf monitor_qapi_event_conf[QAPI_EVENT__MAX] = {
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200398 /* Limit guest-triggerable events to 1 per second */
399 [QAPI_EVENT_RTC_CHANGE] = { 1000 * SCALE_MS },
400 [QAPI_EVENT_WATCHDOG] = { 1000 * SCALE_MS },
401 [QAPI_EVENT_BALLOON_CHANGE] = { 1000 * SCALE_MS },
402 [QAPI_EVENT_QUORUM_REPORT_BAD] = { 1000 * SCALE_MS },
403 [QAPI_EVENT_QUORUM_FAILURE] = { 1000 * SCALE_MS },
404 [QAPI_EVENT_VSERPORT_CHANGE] = { 1000 * SCALE_MS },
405};
406
Markus Armbrustera24712a2015-10-15 17:08:34 +0200407GHashTable *monitor_qapi_event_state;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100408
409/*
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200410 * Emits the event to every monitor instance, @event is only used for trace
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200411 * Called with monitor_lock held.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100412 */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200413static void monitor_qapi_event_emit(QAPIEvent event, QDict *qdict)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100414{
415 Monitor *mon;
416
Markus Armbruster688b4b72015-10-15 17:08:30 +0200417 trace_monitor_protocol_event_emit(event, qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100418 QLIST_FOREACH(mon, &mon_list, entry) {
Markus Armbruster635db182017-03-03 13:32:27 +0100419 if (monitor_is_qmp(mon)
420 && mon->qmp.commands != &qmp_cap_negotiation_commands) {
Markus Armbruster688b4b72015-10-15 17:08:30 +0200421 monitor_json_emitter(mon, QOBJECT(qdict));
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100422 }
423 }
424}
425
Markus Armbrustera24712a2015-10-15 17:08:34 +0200426static void monitor_qapi_event_handler(void *opaque);
427
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100428/*
429 * Queue a new event for emission to Monitor instances,
430 * applying any rate limiting if required.
431 */
432static void
Markus Armbruster688b4b72015-10-15 17:08:30 +0200433monitor_qapi_event_queue(QAPIEvent event, QDict *qdict, Error **errp)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100434{
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200435 MonitorQAPIEventConf *evconf;
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200436 MonitorQAPIEventState *evstate;
Markus Armbruster93f8f982015-10-15 17:08:31 +0200437
Eric Blake7fb1cf12015-11-18 01:52:57 -0700438 assert(event < QAPI_EVENT__MAX);
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200439 evconf = &monitor_qapi_event_conf[event];
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200440 trace_monitor_protocol_event_queue(event, qdict, evconf->rate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100441
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200442 qemu_mutex_lock(&monitor_lock);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200443
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200444 if (!evconf->rate) {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200445 /* Unthrottled event */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200446 monitor_qapi_event_emit(event, qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100447 } else {
Markus Armbruster7de0be62015-10-15 17:08:35 +0200448 QDict *data = qobject_to_qdict(qdict_get(qdict, "data"));
449 MonitorQAPIEventState key = { .event = event, .data = data };
Markus Armbrustera24712a2015-10-15 17:08:34 +0200450
451 evstate = g_hash_table_lookup(monitor_qapi_event_state, &key);
452 assert(!evstate || timer_pending(evstate->timer));
453
454 if (evstate) {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200455 /*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200456 * Timer is pending for (at least) evconf->rate ns after
Markus Armbruster93f8f982015-10-15 17:08:31 +0200457 * last send. Store event for sending when timer fires,
458 * replacing a prior stored event if any.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100459 */
Markus Armbruster93f8f982015-10-15 17:08:31 +0200460 QDECREF(evstate->qdict);
Markus Armbruster688b4b72015-10-15 17:08:30 +0200461 evstate->qdict = qdict;
462 QINCREF(evstate->qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100463 } else {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200464 /*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200465 * Last send was (at least) evconf->rate ns ago.
Markus Armbruster93f8f982015-10-15 17:08:31 +0200466 * Send immediately, and arm the timer to call
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200467 * monitor_qapi_event_handler() in evconf->rate ns. Any
Markus Armbruster93f8f982015-10-15 17:08:31 +0200468 * events arriving before then will be delayed until then.
469 */
Alberto Garciadc599972016-03-10 13:55:26 +0200470 int64_t now = qemu_clock_get_ns(event_clock_type);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200471
Markus Armbruster688b4b72015-10-15 17:08:30 +0200472 monitor_qapi_event_emit(event, qdict);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200473
474 evstate = g_new(MonitorQAPIEventState, 1);
475 evstate->event = event;
Markus Armbruster7de0be62015-10-15 17:08:35 +0200476 evstate->data = data;
477 QINCREF(evstate->data);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200478 evstate->qdict = NULL;
Alberto Garciadc599972016-03-10 13:55:26 +0200479 evstate->timer = timer_new_ns(event_clock_type,
Markus Armbrustera24712a2015-10-15 17:08:34 +0200480 monitor_qapi_event_handler,
481 evstate);
482 g_hash_table_add(monitor_qapi_event_state, evstate);
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200483 timer_mod_ns(evstate->timer, now + evconf->rate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100484 }
485 }
Markus Armbruster93f8f982015-10-15 17:08:31 +0200486
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200487 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100488}
489
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100490/*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200491 * This function runs evconf->rate ns after sending a throttled
Markus Armbruster93f8f982015-10-15 17:08:31 +0200492 * event.
493 * If another event has since been stored, send it.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100494 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200495static void monitor_qapi_event_handler(void *opaque)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100496{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200497 MonitorQAPIEventState *evstate = opaque;
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200498 MonitorQAPIEventConf *evconf = &monitor_qapi_event_conf[evstate->event];
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100499
Markus Armbruster93f8f982015-10-15 17:08:31 +0200500 trace_monitor_protocol_event_handler(evstate->event, evstate->qdict);
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200501 qemu_mutex_lock(&monitor_lock);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200502
Markus Armbruster688b4b72015-10-15 17:08:30 +0200503 if (evstate->qdict) {
Alberto Garciadc599972016-03-10 13:55:26 +0200504 int64_t now = qemu_clock_get_ns(event_clock_type);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200505
Markus Armbruster688b4b72015-10-15 17:08:30 +0200506 monitor_qapi_event_emit(evstate->event, evstate->qdict);
507 QDECREF(evstate->qdict);
508 evstate->qdict = NULL;
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200509 timer_mod_ns(evstate->timer, now + evconf->rate);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200510 } else {
511 g_hash_table_remove(monitor_qapi_event_state, evstate);
Markus Armbruster7de0be62015-10-15 17:08:35 +0200512 QDECREF(evstate->data);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200513 timer_free(evstate->timer);
514 g_free(evstate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100515 }
Markus Armbruster93f8f982015-10-15 17:08:31 +0200516
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200517 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100518}
519
Markus Armbrustera24712a2015-10-15 17:08:34 +0200520static unsigned int qapi_event_throttle_hash(const void *key)
521{
522 const MonitorQAPIEventState *evstate = key;
Markus Armbruster7de0be62015-10-15 17:08:35 +0200523 unsigned int hash = evstate->event * 255;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200524
Markus Armbruster7de0be62015-10-15 17:08:35 +0200525 if (evstate->event == QAPI_EVENT_VSERPORT_CHANGE) {
526 hash += g_str_hash(qdict_get_str(evstate->data, "id"));
527 }
528
Alberto Garcia6d425eb2016-03-10 13:55:25 +0200529 if (evstate->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
530 hash += g_str_hash(qdict_get_str(evstate->data, "node-name"));
531 }
532
Markus Armbruster7de0be62015-10-15 17:08:35 +0200533 return hash;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200534}
535
536static gboolean qapi_event_throttle_equal(const void *a, const void *b)
537{
538 const MonitorQAPIEventState *eva = a;
539 const MonitorQAPIEventState *evb = b;
540
Markus Armbruster7de0be62015-10-15 17:08:35 +0200541 if (eva->event != evb->event) {
542 return FALSE;
543 }
544
545 if (eva->event == QAPI_EVENT_VSERPORT_CHANGE) {
546 return !strcmp(qdict_get_str(eva->data, "id"),
547 qdict_get_str(evb->data, "id"));
548 }
549
Alberto Garcia6d425eb2016-03-10 13:55:25 +0200550 if (eva->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
551 return !strcmp(qdict_get_str(eva->data, "node-name"),
552 qdict_get_str(evb->data, "node-name"));
553 }
554
Markus Armbruster7de0be62015-10-15 17:08:35 +0200555 return TRUE;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200556}
557
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200558static void monitor_qapi_event_init(void)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100559{
Alberto Garciadc599972016-03-10 13:55:26 +0200560 if (qtest_enabled()) {
561 event_clock_type = QEMU_CLOCK_VIRTUAL;
562 }
563
Markus Armbrustera24712a2015-10-15 17:08:34 +0200564 monitor_qapi_event_state = g_hash_table_new(qapi_event_throttle_hash,
565 qapi_event_throttle_equal);
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200566 qmp_event_set_func_emit(monitor_qapi_event_queue);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200567}
568
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100569static void handle_hmp_command(Monitor *mon, const char *cmdline);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200570
Wenchao Xiab01fe892013-08-27 20:38:19 +0800571static void monitor_data_init(Monitor *mon)
572{
573 memset(mon, 0, sizeof(Monitor));
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200574 qemu_mutex_init(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800575 mon->outbuf = qstring_new();
Wenchao Xia77172392013-08-27 20:38:20 +0800576 /* Use *mon_cmds by default. */
577 mon->cmd_table = mon_cmds;
Wenchao Xiab01fe892013-08-27 20:38:19 +0800578}
579
580static void monitor_data_destroy(Monitor *mon)
581{
Marc-André Lureau1ce26102017-01-27 00:49:13 +0400582 qemu_chr_fe_deinit(&mon->chr, false);
Marc-André Lureau2ef45712016-08-01 15:23:42 +0400583 if (monitor_is_qmp(mon)) {
584 json_message_parser_destroy(&mon->qmp.parser);
585 }
586 g_free(mon->rs);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800587 QDECREF(mon->outbuf);
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200588 qemu_mutex_destroy(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800589}
590
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200591char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
592 int64_t cpu_index, Error **errp)
Luiz Capitulino0268d972010-10-22 10:08:02 -0200593{
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200594 char *output = NULL;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200595 Monitor *old_mon, hmp;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200596
Wenchao Xiab01fe892013-08-27 20:38:19 +0800597 monitor_data_init(&hmp);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400598 hmp.skip_flush = true;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200599
600 old_mon = cur_mon;
601 cur_mon = &hmp;
602
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200603 if (has_cpu_index) {
604 int ret = monitor_set_cpu(cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200605 if (ret < 0) {
606 cur_mon = old_mon;
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100607 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
608 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200609 goto out;
610 }
611 }
612
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100613 handle_hmp_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200614 cur_mon = old_mon;
615
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200616 qemu_mutex_lock(&hmp.out_lock);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400617 if (qstring_get_length(hmp.outbuf) > 0) {
618 output = g_strdup(qstring_get_str(hmp.outbuf));
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200619 } else {
620 output = g_strdup("");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200621 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200622 qemu_mutex_unlock(&hmp.out_lock);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200623
624out:
Wenchao Xiab01fe892013-08-27 20:38:19 +0800625 monitor_data_destroy(&hmp);
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200626 return output;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200627}
628
bellard9dc39cb2004-03-14 21:38:27 +0000629static int compare_cmd(const char *name, const char *list)
630{
631 const char *p, *pstart;
632 int len;
633 len = strlen(name);
634 p = list;
635 for(;;) {
636 pstart = p;
637 p = strchr(p, '|');
638 if (!p)
639 p = pstart + strlen(pstart);
640 if ((p - pstart) == len && !memcmp(pstart, name, len))
641 return 1;
642 if (*p == '\0')
643 break;
644 p++;
645 }
646 return 0;
647}
648
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800649static int get_str(char *buf, int buf_size, const char **pp)
650{
651 const char *p;
652 char *q;
653 int c;
654
655 q = buf;
656 p = *pp;
657 while (qemu_isspace(*p)) {
658 p++;
659 }
660 if (*p == '\0') {
661 fail:
662 *q = '\0';
663 *pp = p;
664 return -1;
665 }
666 if (*p == '\"') {
667 p++;
668 while (*p != '\0' && *p != '\"') {
669 if (*p == '\\') {
670 p++;
671 c = *p++;
672 switch (c) {
673 case 'n':
674 c = '\n';
675 break;
676 case 'r':
677 c = '\r';
678 break;
679 case '\\':
680 case '\'':
681 case '\"':
682 break;
683 default:
Peter Maydell71baf782015-08-19 16:20:19 +0100684 printf("unsupported escape code: '\\%c'\n", c);
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800685 goto fail;
686 }
687 if ((q - buf) < buf_size - 1) {
688 *q++ = c;
689 }
690 } else {
691 if ((q - buf) < buf_size - 1) {
692 *q++ = *p;
693 }
694 p++;
695 }
696 }
697 if (*p != '\"') {
Peter Maydell71baf782015-08-19 16:20:19 +0100698 printf("unterminated string\n");
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800699 goto fail;
700 }
701 p++;
702 } else {
703 while (*p != '\0' && !qemu_isspace(*p)) {
704 if ((q - buf) < buf_size - 1) {
705 *q++ = *p;
706 }
707 p++;
708 }
709 }
710 *q = '\0';
711 *pp = p;
712 return 0;
713}
714
715#define MAX_ARGS 16
716
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800717static void free_cmdline_args(char **args, int nb_args)
718{
719 int i;
720
721 assert(nb_args <= MAX_ARGS);
722
723 for (i = 0; i < nb_args; i++) {
724 g_free(args[i]);
725 }
726
727}
728
729/*
730 * Parse the command line to get valid args.
731 * @cmdline: command line to be parsed.
732 * @pnb_args: location to store the number of args, must NOT be NULL.
733 * @args: location to store the args, which should be freed by caller, must
734 * NOT be NULL.
735 *
736 * Returns 0 on success, negative on failure.
737 *
738 * NOTE: this parser is an approximate form of the real command parser. Number
739 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
740 * return with failure.
741 */
742static int parse_cmdline(const char *cmdline,
743 int *pnb_args, char **args)
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800744{
745 const char *p;
746 int nb_args, ret;
747 char buf[1024];
748
749 p = cmdline;
750 nb_args = 0;
751 for (;;) {
752 while (qemu_isspace(*p)) {
753 p++;
754 }
755 if (*p == '\0') {
756 break;
757 }
758 if (nb_args >= MAX_ARGS) {
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800759 goto fail;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800760 }
761 ret = get_str(buf, sizeof(buf), &p);
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800762 if (ret < 0) {
763 goto fail;
764 }
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800765 args[nb_args] = g_strdup(buf);
766 nb_args++;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800767 }
768 *pnb_args = nb_args;
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800769 return 0;
770
771 fail:
772 free_cmdline_args(args, nb_args);
773 return -1;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800774}
775
Wenchao Xia66855492013-08-27 20:38:23 +0800776static void help_cmd_dump_one(Monitor *mon,
777 const mon_cmd_t *cmd,
778 char **prefix_args,
779 int prefix_args_nb)
780{
781 int i;
782
783 for (i = 0; i < prefix_args_nb; i++) {
784 monitor_printf(mon, "%s ", prefix_args[i]);
785 }
786 monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
787}
788
789/* @args[@arg_index] is the valid command need to find in @cmds */
Anthony Liguoric227f092009-10-01 16:12:16 -0500790static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
Wenchao Xia66855492013-08-27 20:38:23 +0800791 char **args, int nb_args, int arg_index)
bellard9dc39cb2004-03-14 21:38:27 +0000792{
Anthony Liguoric227f092009-10-01 16:12:16 -0500793 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000794
Wenchao Xia66855492013-08-27 20:38:23 +0800795 /* No valid arg need to compare with, dump all in *cmds */
796 if (arg_index >= nb_args) {
797 for (cmd = cmds; cmd->name != NULL; cmd++) {
798 help_cmd_dump_one(mon, cmd, args, arg_index);
799 }
800 return;
801 }
802
803 /* Find one entry to dump */
804 for (cmd = cmds; cmd->name != NULL; cmd++) {
805 if (compare_cmd(args[arg_index], cmd->name)) {
806 if (cmd->sub_table) {
807 /* continue with next arg */
808 help_cmd_dump(mon, cmd->sub_table,
809 args, nb_args, arg_index + 1);
810 } else {
811 help_cmd_dump_one(mon, cmd, args, arg_index);
812 }
813 break;
814 }
bellard9dc39cb2004-03-14 21:38:27 +0000815 }
816}
817
aliguori376253e2009-03-05 23:01:23 +0000818static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000819{
Wenchao Xia66855492013-08-27 20:38:23 +0800820 char *args[MAX_ARGS];
821 int nb_args = 0;
822
823 /* 1. parse user input */
824 if (name) {
825 /* special case for log, directly dump and return */
826 if (!strcmp(name, "log")) {
Peter Maydell38dad9e2013-02-11 16:41:25 +0000827 const QEMULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000828 monitor_printf(mon, "Log items (comma separated):\n");
829 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
Peter Maydell38dad9e2013-02-11 16:41:25 +0000830 for (item = qemu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000831 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000832 }
Wenchao Xia66855492013-08-27 20:38:23 +0800833 return;
834 }
835
836 if (parse_cmdline(name, &nb_args, args) < 0) {
837 return;
bellardf193c792004-03-21 17:06:25 +0000838 }
bellard9dc39cb2004-03-14 21:38:27 +0000839 }
Wenchao Xia66855492013-08-27 20:38:23 +0800840
841 /* 2. dump the contents according to parsed args */
842 help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0);
843
844 free_cmdline_args(args, nb_args);
bellard9dc39cb2004-03-14 21:38:27 +0000845}
846
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300847static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300848{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300849 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300850}
851
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100852static void hmp_trace_event(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530853{
854 const char *tp_name = qdict_get_str(qdict, "name");
855 bool new_state = qdict_get_bool(qdict, "option");
Lluís Vilanova77e2b172016-07-11 12:53:57 +0200856 bool has_vcpu = qdict_haskey(qdict, "vcpu");
857 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
Lluís Vilanova14101d02014-08-25 13:20:03 +0200858 Error *local_err = NULL;
Blue Swirlf871d682010-10-13 19:14:29 +0000859
Lluís Vilanova77e2b172016-07-11 12:53:57 +0200860 if (vcpu < 0) {
861 monitor_printf(mon, "argument vcpu must be positive");
862 return;
863 }
864
865 qmp_trace_event_set_state(tp_name, new_state, true, true, has_vcpu, vcpu, &local_err);
Lluís Vilanova14101d02014-08-25 13:20:03 +0200866 if (local_err) {
Markus Armbruster091e38b2015-02-10 15:15:43 +0100867 error_report_err(local_err);
Blue Swirlf871d682010-10-13 19:14:29 +0000868 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530869}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100870
Michael Rothc45a8162011-10-02 08:44:37 -0500871#ifdef CONFIG_TRACE_SIMPLE
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100872static void hmp_trace_file(Monitor *mon, const QDict *qdict)
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100873{
874 const char *op = qdict_get_try_str(qdict, "op");
875 const char *arg = qdict_get_try_str(qdict, "arg");
876
877 if (!op) {
878 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
879 } else if (!strcmp(op, "on")) {
880 st_set_trace_file_enabled(true);
881 } else if (!strcmp(op, "off")) {
882 st_set_trace_file_enabled(false);
883 } else if (!strcmp(op, "flush")) {
884 st_flush_trace_buffer();
885 } else if (!strcmp(op, "set")) {
886 if (arg) {
887 st_set_trace_file(arg);
888 }
889 } else {
890 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
891 help_cmd(mon, "trace-file");
892 }
893}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530894#endif
895
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100896static void hmp_info_help(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000897{
Luiz Capitulino13c74252009-10-07 13:41:55 -0300898 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000899}
900
Marc-André Lureau9e812b62016-09-12 13:19:05 +0400901static void query_commands_cb(QmpCommand *cmd, void *opaque)
bellard9bc9d1c2004-10-10 15:15:51 +0000902{
Marc-André Lureau9e812b62016-09-12 13:19:05 +0400903 CommandInfoList *info, **list = opaque;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200904
Marc-André Lureau9e812b62016-09-12 13:19:05 +0400905 if (!cmd->enabled) {
906 return;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200907 }
908
Marc-André Lureau9e812b62016-09-12 13:19:05 +0400909 info = g_malloc0(sizeof(*info));
910 info->value = g_malloc0(sizeof(*info->value));
911 info->value->name = g_strdup(cmd->name);
912 info->next = *list;
913 *list = info;
914}
915
916CommandInfoList *qmp_query_commands(Error **errp)
917{
918 CommandInfoList *list = NULL;
919
Markus Armbruster635db182017-03-03 13:32:27 +0100920 qmp_for_each_command(cur_mon->qmp.commands, query_commands_cb, &list);
Marc-André Lureau9e812b62016-09-12 13:19:05 +0400921
922 return list;
thsa36e69d2007-12-02 05:18:19 +0000923}
924
Daniel P. Berrange48608532012-05-21 17:59:51 +0100925EventInfoList *qmp_query_events(Error **errp)
926{
927 EventInfoList *info, *ev_list = NULL;
Wenchao Xia75175172014-06-18 08:43:54 +0200928 QAPIEvent e;
Daniel P. Berrange48608532012-05-21 17:59:51 +0100929
Eric Blake7fb1cf12015-11-18 01:52:57 -0700930 for (e = 0 ; e < QAPI_EVENT__MAX ; e++) {
Wenchao Xia75175172014-06-18 08:43:54 +0200931 const char *event_name = QAPIEvent_lookup[e];
Daniel P. Berrange48608532012-05-21 17:59:51 +0100932 assert(event_name != NULL);
933 info = g_malloc0(sizeof(*info));
934 info->value = g_malloc0(sizeof(*info->value));
935 info->value->name = g_strdup(event_name);
936
937 info->next = ev_list;
938 ev_list = info;
939 }
940
941 return ev_list;
942}
943
Markus Armbruster39a18152015-09-16 13:06:28 +0200944/*
945 * Minor hack: generated marshalling suppressed for this command
946 * ('gen': false in the schema) so we can parse the JSON string
947 * directly into QObject instead of first parsing it with
948 * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
949 * to QObject with generated output marshallers, every time. Instead,
Daniel P. Berrangeb3db2112016-09-30 15:45:27 +0100950 * we do it in test-qobject-input-visitor.c, just to make sure
Markus Armbruster39a18152015-09-16 13:06:28 +0200951 * qapi-introspect.py's output actually conforms to the schema.
952 */
953static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
954 Error **errp)
955{
Markus Armbrusterb0f36b22017-02-28 22:27:02 +0100956 *ret_data = qobject_from_json(qmp_schema_json, &error_abort);
Markus Armbruster39a18152015-09-16 13:06:28 +0200957}
958
Marc-André Lureau5032a162016-09-12 13:19:02 +0400959/*
Marc-André Lureau5032a162016-09-12 13:19:02 +0400960 * We used to define commands in qmp-commands.hx in addition to the
961 * QAPI schema. This permitted defining some of them only in certain
962 * configurations. query-commands has always reflected that (good,
963 * because it lets QMP clients figure out what's actually available),
964 * while query-qmp-schema never did (not so good). This function is a
965 * hack to keep the configuration-specific commands defined exactly as
966 * before, even though qmp-commands.hx is gone.
967 *
968 * FIXME Educate the QAPI schema on configuration-specific commands,
969 * and drop this hack.
970 */
971static void qmp_unregister_commands_hack(void)
972{
973#ifndef CONFIG_SPICE
Markus Armbruster1527bad2017-03-03 13:32:25 +0100974 qmp_unregister_command(&qmp_commands, "query-spice");
Marc-André Lureau5032a162016-09-12 13:19:02 +0400975#endif
Markus Armbruster38bb54f2017-04-27 15:00:53 +0200976#ifndef CONFIG_REPLICATION
977 qmp_unregister_command(&qmp_commands, "xen-set-replication");
978 qmp_unregister_command(&qmp_commands, "query-xen-replication-status");
979 qmp_unregister_command(&qmp_commands, "xen-colo-do-checkpoint");
980#endif
Marc-André Lureau5032a162016-09-12 13:19:02 +0400981#ifndef TARGET_I386
Markus Armbruster1527bad2017-03-03 13:32:25 +0100982 qmp_unregister_command(&qmp_commands, "rtc-reset-reinjection");
Marc-André Lureau5032a162016-09-12 13:19:02 +0400983#endif
984#ifndef TARGET_S390X
Markus Armbruster1527bad2017-03-03 13:32:25 +0100985 qmp_unregister_command(&qmp_commands, "dump-skeys");
Marc-André Lureau5032a162016-09-12 13:19:02 +0400986#endif
987#ifndef TARGET_ARM
Markus Armbruster1527bad2017-03-03 13:32:25 +0100988 qmp_unregister_command(&qmp_commands, "query-gic-capabilities");
Marc-André Lureau5032a162016-09-12 13:19:02 +0400989#endif
Eduardo Habkostf99fd7c2017-02-22 16:00:28 -0300990#if !defined(TARGET_S390X) && !defined(TARGET_I386)
Markus Armbruster1527bad2017-03-03 13:32:25 +0100991 qmp_unregister_command(&qmp_commands, "query-cpu-model-expansion");
Eduardo Habkostf99fd7c2017-02-22 16:00:28 -0300992#endif
993#if !defined(TARGET_S390X)
Markus Armbruster1527bad2017-03-03 13:32:25 +0100994 qmp_unregister_command(&qmp_commands, "query-cpu-model-baseline");
995 qmp_unregister_command(&qmp_commands, "query-cpu-model-comparison");
Eduardo Habkost728b1422016-10-05 16:49:01 -0300996#endif
997#if !defined(TARGET_PPC) && !defined(TARGET_ARM) && !defined(TARGET_I386) \
998 && !defined(TARGET_S390X)
Markus Armbruster1527bad2017-03-03 13:32:25 +0100999 qmp_unregister_command(&qmp_commands, "query-cpu-definitions");
Eduardo Habkost728b1422016-10-05 16:49:01 -03001000#endif
Marc-André Lureau5032a162016-09-12 13:19:02 +04001001}
1002
Markus Armbruster05875682017-03-03 13:32:24 +01001003void monitor_init_qmp_commands(void)
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001004{
Markus Armbruster635db182017-03-03 13:32:27 +01001005 /*
1006 * Two command lists:
1007 * - qmp_commands contains all QMP commands
1008 * - qmp_cap_negotiation_commands contains just
1009 * "qmp_capabilities", to enforce capability negotiation
1010 */
1011
Markus Armbruster1527bad2017-03-03 13:32:25 +01001012 qmp_init_marshal(&qmp_commands);
Markus Armbruster05875682017-03-03 13:32:24 +01001013
Markus Armbruster1527bad2017-03-03 13:32:25 +01001014 qmp_register_command(&qmp_commands, "query-qmp-schema",
1015 qmp_query_qmp_schema,
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001016 QCO_NO_OPTIONS);
Markus Armbruster1527bad2017-03-03 13:32:25 +01001017 qmp_register_command(&qmp_commands, "device_add", qmp_device_add,
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001018 QCO_NO_OPTIONS);
Markus Armbruster1527bad2017-03-03 13:32:25 +01001019 qmp_register_command(&qmp_commands, "netdev_add", qmp_netdev_add,
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001020 QCO_NO_OPTIONS);
Marc-André Lureau5032a162016-09-12 13:19:02 +04001021
Markus Armbruster05875682017-03-03 13:32:24 +01001022 qmp_unregister_commands_hack();
Markus Armbruster635db182017-03-03 13:32:27 +01001023
1024 QTAILQ_INIT(&qmp_cap_negotiation_commands);
1025 qmp_register_command(&qmp_cap_negotiation_commands, "qmp_capabilities",
1026 qmp_marshal_qmp_capabilities, QCO_NO_OPTIONS);
1027}
1028
1029void qmp_qmp_capabilities(Error **errp)
1030{
1031 if (cur_mon->qmp.commands == &qmp_commands) {
1032 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
1033 "Capabilities negotiation is already complete, command "
1034 "ignored");
1035 return;
1036 }
1037
1038 cur_mon->qmp.commands = &qmp_commands;
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001039}
1040
Luiz Capitulinob025c8b2011-10-06 14:02:57 -03001041/* set the current CPU defined by the user */
1042int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +00001043{
Andreas Färber55e5c282012-12-17 06:18:02 +01001044 CPUState *cpu;
bellard6a00d602005-11-21 23:25:50 +00001045
Andreas Färber1c8bb3c2013-02-15 17:01:09 +01001046 cpu = qemu_get_cpu(cpu_index);
1047 if (cpu == NULL) {
1048 return -1;
bellard6a00d602005-11-21 23:25:50 +00001049 }
Andreas Färbercb446ec2013-05-01 14:24:52 +02001050 cur_mon->mon_cpu = cpu;
Andreas Färber1c8bb3c2013-02-15 17:01:09 +01001051 return 0;
bellard6a00d602005-11-21 23:25:50 +00001052}
1053
Pavel Butsykincaf15312015-09-22 16:18:17 +03001054CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +00001055{
aliguori731b0362009-03-05 23:01:42 +00001056 if (!cur_mon->mon_cpu) {
Thomas Huth854e67f2017-01-13 13:12:35 +01001057 if (!first_cpu) {
1058 return NULL;
1059 }
David Gibson27a83f82016-09-21 15:29:26 +10001060 monitor_set_cpu(first_cpu->cpu_index);
bellard6a00d602005-11-21 23:25:50 +00001061 }
Avi Kivity4c0960c2009-08-17 23:19:53 +03001062 cpu_synchronize_state(cur_mon->mon_cpu);
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001063 return cur_mon->mon_cpu;
1064}
1065
Pavel Butsykinbf957282015-09-10 18:38:59 +03001066CPUArchState *mon_get_cpu_env(void)
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001067{
Thomas Huth854e67f2017-01-13 13:12:35 +01001068 CPUState *cs = mon_get_cpu();
1069
1070 return cs ? cs->env_ptr : NULL;
bellard6a00d602005-11-21 23:25:50 +00001071}
1072
Luiz Capitulino99b77962011-10-24 10:53:44 -02001073int monitor_get_cpu_index(void)
1074{
Thomas Huth854e67f2017-01-13 13:12:35 +01001075 CPUState *cs = mon_get_cpu();
1076
1077 return cs ? cs->cpu_index : UNASSIGNED_CPU_INDEX;
Luiz Capitulino99b77962011-10-24 10:53:44 -02001078}
1079
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001080static void hmp_info_registers(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001081{
Suraj Jitindar Singh18f08282017-06-08 15:41:16 +10001082 bool all_cpus = qdict_get_try_bool(qdict, "cpustate_all", false);
1083 CPUState *cs;
Thomas Huth854e67f2017-01-13 13:12:35 +01001084
Suraj Jitindar Singh18f08282017-06-08 15:41:16 +10001085 if (all_cpus) {
1086 CPU_FOREACH(cs) {
1087 monitor_printf(mon, "\nCPU#%d\n", cs->cpu_index);
1088 cpu_dump_state(cs, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
1089 }
1090 } else {
1091 cs = mon_get_cpu();
1092
1093 if (!cs) {
1094 monitor_printf(mon, "No CPU available\n");
1095 return;
1096 }
1097
1098 cpu_dump_state(cs, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
Thomas Huth854e67f2017-01-13 13:12:35 +01001099 }
bellard9307c4c2004-04-04 12:57:25 +00001100}
1101
Paolo Bonzinif0d14a92012-09-17 13:42:41 +02001102#ifdef CONFIG_TCG
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001103static void hmp_info_jit(Monitor *mon, const QDict *qdict)
bellarde3db7222005-01-26 22:00:47 +00001104{
Thomas Huthb7da97e2017-04-26 06:11:47 +02001105 if (!tcg_enabled()) {
1106 error_report("JIT information is only available with accel=tcg");
1107 return;
1108 }
1109
aliguori376253e2009-03-05 23:01:23 +00001110 dump_exec_info((FILE *)mon, monitor_fprintf);
Sebastian Tanase27498be2014-07-25 11:56:33 +02001111 dump_drift_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +00001112}
1113
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001114static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
Max Filippov246ae242014-11-02 11:04:18 +03001115{
1116 dump_opcount_info((FILE *)mon, monitor_fprintf);
1117}
Paolo Bonzinif0d14a92012-09-17 13:42:41 +02001118#endif
Max Filippov246ae242014-11-02 11:04:18 +03001119
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001120static void hmp_info_history(Monitor *mon, const QDict *qdict)
bellardaa455482004-04-04 13:07:25 +00001121{
1122 int i;
bellard7e2515e2004-08-01 21:52:19 +00001123 const char *str;
ths3b46e622007-09-17 08:09:54 +00001124
aliguoricde76ee2009-03-05 23:01:51 +00001125 if (!mon->rs)
1126 return;
bellard7e2515e2004-08-01 21:52:19 +00001127 i = 0;
1128 for(;;) {
aliguori731b0362009-03-05 23:01:42 +00001129 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +00001130 if (!str)
1131 break;
aliguori376253e2009-03-05 23:01:23 +00001132 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +00001133 i++;
bellardaa455482004-04-04 13:07:25 +00001134 }
1135}
1136
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001137static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
j_mayer76a66252007-03-07 08:32:30 +00001138{
Thomas Huth854e67f2017-01-13 13:12:35 +01001139 CPUState *cs = mon_get_cpu();
1140
1141 if (!cs) {
1142 monitor_printf(mon, "No CPU available\n");
1143 return;
1144 }
1145 cpu_dump_statistics(cs, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +00001146}
j_mayer76a66252007-03-07 08:32:30 +00001147
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001148static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301149{
Lluís Vilanovabd712112016-07-11 12:53:51 +02001150 const char *name = qdict_get_try_str(qdict, "name");
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001151 bool has_vcpu = qdict_haskey(qdict, "vcpu");
1152 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
Lluís Vilanovabd712112016-07-11 12:53:51 +02001153 TraceEventInfoList *events;
Lluís Vilanova14101d02014-08-25 13:20:03 +02001154 TraceEventInfoList *elem;
Lluís Vilanovabd712112016-07-11 12:53:51 +02001155 Error *local_err = NULL;
1156
1157 if (name == NULL) {
1158 name = "*";
1159 }
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001160 if (vcpu < 0) {
1161 monitor_printf(mon, "argument vcpu must be positive");
1162 return;
1163 }
Lluís Vilanovabd712112016-07-11 12:53:51 +02001164
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001165 events = qmp_trace_event_get_state(name, has_vcpu, vcpu, &local_err);
Lluís Vilanovabd712112016-07-11 12:53:51 +02001166 if (local_err) {
1167 error_report_err(local_err);
1168 return;
1169 }
Lluís Vilanova14101d02014-08-25 13:20:03 +02001170
1171 for (elem = events; elem != NULL; elem = elem->next) {
1172 monitor_printf(mon, "%s : state %u\n",
1173 elem->value->name,
1174 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
1175 }
1176 qapi_free_TraceEventInfoList(events);
Prerna Saxena22890ab2010-06-24 17:04:53 +05301177}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301178
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001179void qmp_client_migrate_info(const char *protocol, const char *hostname,
1180 bool has_port, int64_t port,
1181 bool has_tls_port, int64_t tls_port,
1182 bool has_cert_subject, const char *cert_subject,
1183 Error **errp)
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001184{
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001185 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001186 if (!qemu_using_spice(errp)) {
1187 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001188 }
1189
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001190 if (!has_port && !has_tls_port) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001191 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001192 return;
Yonit Halperin6ec5dae2012-03-18 09:42:39 +02001193 }
1194
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001195 if (qemu_spice_migrate_info(hostname,
1196 has_port ? port : -1,
1197 has_tls_port ? tls_port : -1,
1198 cert_subject)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001199 error_setg(errp, QERR_UNDEFINED_ERROR);
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001200 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001201 }
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001202 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001203 }
1204
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001205 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001206}
1207
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001208static void hmp_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001209{
Markus Armbrusterdaa76aa2016-06-15 19:27:16 +02001210 Error *err = NULL;
1211
1212 qemu_set_log_filename(qdict_get_str(qdict, "filename"), &err);
1213 if (err) {
1214 error_report_err(err);
1215 }
pbrooke735b912007-06-30 13:53:24 +00001216}
1217
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001218static void hmp_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001219{
1220 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001221 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001222
bellard9307c4c2004-04-04 12:57:25 +00001223 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001224 mask = 0;
1225 } else {
Peter Maydell4fde1eb2013-02-11 16:41:22 +00001226 mask = qemu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001227 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001228 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001229 return;
1230 }
1231 }
Peter Maydell24537a02013-02-11 16:41:23 +00001232 qemu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00001233}
1234
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001235static void hmp_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001236{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001237 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001238 if (!option || !strcmp(option, "on")) {
1239 singlestep = 1;
1240 } else if (!strcmp(option, "off")) {
1241 singlestep = 0;
1242 } else {
1243 monitor_printf(mon, "unexpected option %s\n", option);
1244 }
1245}
1246
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001247static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001248{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001249 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001250 if (!device)
1251 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1252 if (gdbserver_start(device) < 0) {
1253 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1254 device);
1255 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001256 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001257 } else {
aliguori59030a82009-04-05 18:43:41 +00001258 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1259 device);
bellard8a7ddc32004-03-31 19:00:16 +00001260 }
1261}
1262
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001263static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001264{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001265 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001266 if (select_watchdog_action(action) == -1) {
1267 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1268 }
1269}
1270
aliguori376253e2009-03-05 23:01:23 +00001271static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001272{
aliguori376253e2009-03-05 23:01:23 +00001273 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001274 switch(c) {
1275 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001276 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001277 break;
1278 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001279 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001280 break;
1281 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001282 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001283 break;
1284 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001285 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001286 break;
1287 default:
1288 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001289 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001290 } else {
aliguori376253e2009-03-05 23:01:23 +00001291 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001292 }
1293 break;
1294 }
aliguori376253e2009-03-05 23:01:23 +00001295 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001296}
1297
aliguori376253e2009-03-05 23:01:23 +00001298static void memory_dump(Monitor *mon, int count, int format, int wsize,
Avi Kivitya8170e52012-10-23 12:30:10 +02001299 hwaddr addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001300{
Blue Swirl23842aa2010-01-12 20:27:43 +00001301 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001302 uint8_t buf[16];
1303 uint64_t v;
Thomas Huth854e67f2017-01-13 13:12:35 +01001304 CPUState *cs = mon_get_cpu();
1305
1306 if (!cs && (format == 'i' || !is_physical)) {
1307 monitor_printf(mon, "Can not dump without CPU\n");
1308 return;
1309 }
bellard9307c4c2004-04-04 12:57:25 +00001310
1311 if (format == 'i') {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001312 int flags = 0;
bellard9307c4c2004-04-04 12:57:25 +00001313#ifdef TARGET_I386
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001314 CPUArchState *env = mon_get_cpu_env();
bellard4c27ba22004-04-25 18:05:08 +00001315 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001316 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001317 } else if (wsize == 4) {
1318 flags = 0;
1319 } else {
bellard6a15fd12006-04-12 21:07:07 +00001320 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001321 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001322 if (env) {
1323#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001324 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001325 (env->segs[R_CS].flags & DESC_L_MASK))
1326 flags = 2;
1327 else
1328#endif
1329 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1330 flags = 1;
1331 }
bellard4c27ba22004-04-25 18:05:08 +00001332 }
1333#endif
Tom Musta1c38f842014-04-09 14:53:24 -05001334#ifdef TARGET_PPC
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001335 CPUArchState *env = mon_get_cpu_env();
Tom Musta1c38f842014-04-09 14:53:24 -05001336 flags = msr_le << 16;
1337 flags |= env->bfd_mach;
1338#endif
Thomas Huth854e67f2017-01-13 13:12:35 +01001339 monitor_disas(mon, cs, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001340 return;
1341 }
1342
1343 len = wsize * count;
1344 if (wsize == 1)
1345 line_size = 8;
1346 else
1347 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001348 max_digits = 0;
1349
1350 switch(format) {
1351 case 'o':
1352 max_digits = (wsize * 8 + 2) / 3;
1353 break;
1354 default:
1355 case 'x':
1356 max_digits = (wsize * 8) / 4;
1357 break;
1358 case 'u':
1359 case 'd':
1360 max_digits = (wsize * 8 * 10 + 32) / 33;
1361 break;
1362 case 'c':
1363 wsize = 1;
1364 break;
1365 }
1366
1367 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001368 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001369 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001370 else
aliguori376253e2009-03-05 23:01:23 +00001371 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001372 l = len;
1373 if (l > line_size)
1374 l = line_size;
1375 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001376 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001377 } else {
Thomas Huth854e67f2017-01-13 13:12:35 +01001378 if (cpu_memory_rw_debug(cs, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001379 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001380 break;
1381 }
bellard9307c4c2004-04-04 12:57:25 +00001382 }
ths5fafdf22007-09-16 21:08:06 +00001383 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001384 while (i < l) {
1385 switch(wsize) {
1386 default:
1387 case 1:
Peter Maydell24e60302015-01-20 15:19:32 +00001388 v = ldub_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001389 break;
1390 case 2:
Peter Maydell24e60302015-01-20 15:19:32 +00001391 v = lduw_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001392 break;
1393 case 4:
Peter Maydell24e60302015-01-20 15:19:32 +00001394 v = (uint32_t)ldl_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001395 break;
1396 case 8:
Peter Maydell24e60302015-01-20 15:19:32 +00001397 v = ldq_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001398 break;
1399 }
aliguori376253e2009-03-05 23:01:23 +00001400 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001401 switch(format) {
1402 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001403 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001404 break;
1405 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001406 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001407 break;
1408 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001409 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001410 break;
1411 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001412 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001413 break;
1414 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001415 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001416 break;
1417 }
1418 i += wsize;
1419 }
aliguori376253e2009-03-05 23:01:23 +00001420 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001421 addr += l;
1422 len -= l;
1423 }
1424}
1425
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001426static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001427{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001428 int count = qdict_get_int(qdict, "count");
1429 int format = qdict_get_int(qdict, "format");
1430 int size = qdict_get_int(qdict, "size");
1431 target_long addr = qdict_get_int(qdict, "addr");
1432
aliguori376253e2009-03-05 23:01:23 +00001433 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001434}
1435
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001436static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001437{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001438 int count = qdict_get_int(qdict, "count");
1439 int format = qdict_get_int(qdict, "format");
1440 int size = qdict_get_int(qdict, "size");
Avi Kivitya8170e52012-10-23 12:30:10 +02001441 hwaddr addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001442
aliguori376253e2009-03-05 23:01:23 +00001443 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001444}
1445
Paolo Bonzinie9628442017-04-20 15:30:58 +02001446static void *gpa2hva(MemoryRegion **p_mr, hwaddr addr, Error **errp)
1447{
1448 MemoryRegionSection mrs = memory_region_find(get_system_memory(),
1449 addr, 1);
1450
1451 if (!mrs.mr) {
1452 error_setg(errp, "No memory is mapped at address 0x%" HWADDR_PRIx, addr);
1453 return NULL;
1454 }
1455
1456 if (!memory_region_is_ram(mrs.mr) && !memory_region_is_romd(mrs.mr)) {
1457 error_setg(errp, "Memory at address 0x%" HWADDR_PRIx "is not RAM", addr);
1458 memory_region_unref(mrs.mr);
1459 return NULL;
1460 }
1461
1462 *p_mr = mrs.mr;
1463 return qemu_map_ram_ptr(mrs.mr->ram_block, mrs.offset_within_region);
1464}
1465
1466static void hmp_gpa2hva(Monitor *mon, const QDict *qdict)
1467{
1468 hwaddr addr = qdict_get_int(qdict, "addr");
1469 Error *local_err = NULL;
1470 MemoryRegion *mr = NULL;
1471 void *ptr;
1472
1473 ptr = gpa2hva(&mr, addr, &local_err);
1474 if (local_err) {
1475 error_report_err(local_err);
1476 return;
1477 }
1478
1479 monitor_printf(mon, "Host virtual address for 0x%" HWADDR_PRIx
1480 " (%s) is %p\n",
1481 addr, mr->name, ptr);
1482
1483 memory_region_unref(mr);
1484}
1485
1486#ifdef CONFIG_LINUX
1487static uint64_t vtop(void *ptr, Error **errp)
1488{
1489 uint64_t pinfo;
1490 uint64_t ret = -1;
1491 uintptr_t addr = (uintptr_t) ptr;
1492 uintptr_t pagesize = getpagesize();
1493 off_t offset = addr / pagesize * sizeof(pinfo);
1494 int fd;
1495
1496 fd = open("/proc/self/pagemap", O_RDONLY);
1497 if (fd == -1) {
1498 error_setg_errno(errp, errno, "Cannot open /proc/self/pagemap");
1499 return -1;
1500 }
1501
1502 /* Force copy-on-write if necessary. */
1503 atomic_add((uint8_t *)ptr, 0);
1504
1505 if (pread(fd, &pinfo, sizeof(pinfo), offset) != sizeof(pinfo)) {
1506 error_setg_errno(errp, errno, "Cannot read pagemap");
1507 goto out;
1508 }
1509 if ((pinfo & (1ull << 63)) == 0) {
1510 error_setg(errp, "Page not present");
1511 goto out;
1512 }
1513 ret = ((pinfo & 0x007fffffffffffffull) * pagesize) | (addr & (pagesize - 1));
1514
1515out:
1516 close(fd);
1517 return ret;
1518}
1519
1520static void hmp_gpa2hpa(Monitor *mon, const QDict *qdict)
1521{
1522 hwaddr addr = qdict_get_int(qdict, "addr");
1523 Error *local_err = NULL;
1524 MemoryRegion *mr = NULL;
1525 void *ptr;
1526 uint64_t physaddr;
1527
1528 ptr = gpa2hva(&mr, addr, &local_err);
1529 if (local_err) {
1530 error_report_err(local_err);
1531 return;
1532 }
1533
1534 physaddr = vtop(ptr, &local_err);
1535 if (local_err) {
1536 error_report_err(local_err);
1537 } else {
1538 monitor_printf(mon, "Host physical address for 0x%" HWADDR_PRIx
1539 " (%s) is 0x%" PRIx64 "\n",
1540 addr, mr->name, (uint64_t) physaddr);
1541 }
1542
1543 memory_region_unref(mr);
1544}
1545#endif
1546
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001547static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001548{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001549 int format = qdict_get_int(qdict, "format");
Avi Kivitya8170e52012-10-23 12:30:10 +02001550 hwaddr val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001551
bellard9307c4c2004-04-04 12:57:25 +00001552 switch(format) {
1553 case 'o':
Avi Kivitya8170e52012-10-23 12:30:10 +02001554 monitor_printf(mon, "%#" HWADDR_PRIo, val);
bellard9307c4c2004-04-04 12:57:25 +00001555 break;
1556 case 'x':
Avi Kivitya8170e52012-10-23 12:30:10 +02001557 monitor_printf(mon, "%#" HWADDR_PRIx, val);
bellard9307c4c2004-04-04 12:57:25 +00001558 break;
1559 case 'u':
Avi Kivitya8170e52012-10-23 12:30:10 +02001560 monitor_printf(mon, "%" HWADDR_PRIu, val);
bellard9307c4c2004-04-04 12:57:25 +00001561 break;
1562 default:
1563 case 'd':
Avi Kivitya8170e52012-10-23 12:30:10 +02001564 monitor_printf(mon, "%" HWADDR_PRId, val);
bellard9307c4c2004-04-04 12:57:25 +00001565 break;
1566 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001567 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001568 break;
1569 }
aliguori376253e2009-03-05 23:01:23 +00001570 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001571}
1572
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001573static void hmp_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001574{
1575 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001576 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001577 uint32_t start = qdict_get_int(qdict, "start");
1578 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001579
1580 sum = 0;
1581 for(addr = start; addr < (start + size); addr++) {
Peter Maydell42874d32015-04-26 16:49:24 +01001582 uint8_t val = address_space_ldub(&address_space_memory, addr,
1583 MEMTXATTRS_UNSPECIFIED, NULL);
bellarde4cf1ad2005-06-04 20:15:57 +00001584 /* BSD sum algorithm ('sum' Unix command) */
1585 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001586 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001587 }
aliguori376253e2009-03-05 23:01:23 +00001588 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001589}
1590
bellard13224a82006-07-14 22:03:35 +00001591static int mouse_button_state;
1592
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001593static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001594{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001595 int dx, dy, dz, button;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001596 const char *dx_str = qdict_get_str(qdict, "dx_str");
1597 const char *dy_str = qdict_get_str(qdict, "dy_str");
1598 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001599
bellard13224a82006-07-14 22:03:35 +00001600 dx = strtol(dx_str, NULL, 0);
1601 dy = strtol(dy_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001602 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
1603 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
1604
1605 if (dz_str) {
bellard13224a82006-07-14 22:03:35 +00001606 dz = strtol(dz_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001607 if (dz != 0) {
Gerd Hoffmannf22d0af2016-01-12 12:14:12 +01001608 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001609 qemu_input_queue_btn(NULL, button, true);
1610 qemu_input_event_sync();
1611 qemu_input_queue_btn(NULL, button, false);
1612 }
1613 }
1614 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001615}
1616
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001617static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001618{
Eric Blake7fb1cf12015-11-18 01:52:57 -07001619 static uint32_t bmap[INPUT_BUTTON__MAX] = {
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001620 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1621 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
1622 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1623 };
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001624 int button_state = qdict_get_int(qdict, "button_state");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001625
1626 if (mouse_button_state == button_state) {
1627 return;
1628 }
1629 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1630 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001631 mouse_button_state = button_state;
bellard13224a82006-07-14 22:03:35 +00001632}
1633
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001634static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001635{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001636 int size = qdict_get_int(qdict, "size");
1637 int addr = qdict_get_int(qdict, "addr");
1638 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001639 uint32_t val;
1640 int suffix;
1641
1642 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001643 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001644 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001645 addr++;
1646 }
1647 addr &= 0xffff;
1648
1649 switch(size) {
1650 default:
1651 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001652 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001653 suffix = 'b';
1654 break;
1655 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001656 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001657 suffix = 'w';
1658 break;
1659 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001660 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001661 suffix = 'l';
1662 break;
1663 }
aliguori376253e2009-03-05 23:01:23 +00001664 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1665 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001666}
bellarda3a91a32004-06-04 11:06:21 +00001667
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001668static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001669{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001670 int size = qdict_get_int(qdict, "size");
1671 int addr = qdict_get_int(qdict, "addr");
1672 int val = qdict_get_int(qdict, "val");
1673
Jan Kiszkaf1147842009-07-14 10:20:11 +02001674 addr &= IOPORTS_MASK;
1675
1676 switch (size) {
1677 default:
1678 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001679 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001680 break;
1681 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001682 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001683 break;
1684 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001685 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001686 break;
1687 }
1688}
1689
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001690static void hmp_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001691{
Gongleif1839932014-12-03 18:20:58 +00001692 Error *local_err = NULL;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001693 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001694
Gongleif1839932014-12-03 18:20:58 +00001695 qemu_boot_set(bootdevice, &local_err);
1696 if (local_err) {
Markus Armbruster193227f2015-12-18 16:35:06 +01001697 error_report_err(local_err);
aurel320ecdffb2008-05-04 20:11:34 +00001698 } else {
Gongleif1839932014-12-03 18:20:58 +00001699 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001700 }
1701}
1702
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001703static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
Blue Swirl314e2982011-09-11 20:22:05 +00001704{
Peter Xu57bb40c2017-01-16 16:40:05 +08001705 bool flatview = qdict_get_try_bool(qdict, "flatview", false);
1706
1707 mtree_info((fprintf_function)monitor_printf, mon, flatview);
Blue Swirl314e2982011-09-11 20:22:05 +00001708}
1709
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001710static void hmp_info_numa(Monitor *mon, const QDict *qdict)
aliguori030ea372009-04-21 22:30:47 +00001711{
aliguorib28b6232009-04-22 20:20:29 +00001712 int i;
zhanghailiang5b009e42014-11-04 19:49:30 +08001713 uint64_t *node_mem;
Igor Mammedovf75cd442017-05-30 18:23:59 +02001714 CpuInfoList *cpu_list, *cpu;
aliguori030ea372009-04-21 22:30:47 +00001715
Igor Mammedovf75cd442017-05-30 18:23:59 +02001716 cpu_list = qmp_query_cpus(&error_abort);
zhanghailiang5b009e42014-11-04 19:49:30 +08001717 node_mem = g_new0(uint64_t, nb_numa_nodes);
1718 query_numa_node_mem(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001719 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1720 for (i = 0; i < nb_numa_nodes; i++) {
1721 monitor_printf(mon, "node %d cpus:", i);
Igor Mammedovf75cd442017-05-30 18:23:59 +02001722 for (cpu = cpu_list; cpu; cpu = cpu->next) {
1723 if (cpu->value->has_props && cpu->value->props->has_node_id &&
1724 cpu->value->props->node_id == i) {
1725 monitor_printf(mon, " %" PRIi64, cpu->value->CPU);
aliguori030ea372009-04-21 22:30:47 +00001726 }
1727 }
1728 monitor_printf(mon, "\n");
1729 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
zhanghailiang5b009e42014-11-04 19:49:30 +08001730 node_mem[i] >> 20);
aliguori030ea372009-04-21 22:30:47 +00001731 }
Igor Mammedovf75cd442017-05-30 18:23:59 +02001732 qapi_free_CpuInfoList(cpu_list);
zhanghailiang5b009e42014-11-04 19:49:30 +08001733 g_free(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001734}
1735
bellard5f1ce942006-02-08 22:40:15 +00001736#ifdef CONFIG_PROFILER
1737
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001738int64_t tcg_time;
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001739int64_t dev_time;
1740
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001741static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001742{
aliguori376253e2009-03-05 23:01:23 +00001743 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Rutuja Shah73bcb242016-03-21 21:32:30 +05301744 dev_time, dev_time / (double)NANOSECONDS_PER_SECOND);
aliguori376253e2009-03-05 23:01:23 +00001745 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Rutuja Shah73bcb242016-03-21 21:32:30 +05301746 tcg_time, tcg_time / (double)NANOSECONDS_PER_SECOND);
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001747 tcg_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001748 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001749}
1750#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001751static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001752{
aliguori376253e2009-03-05 23:01:23 +00001753 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001754}
1755#endif
1756
bellardec36b692006-07-16 18:57:03 +00001757/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001758static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001759
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001760static void hmp_info_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001761{
1762 int i;
1763 CaptureState *s;
1764
1765 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001766 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001767 s->ops.info (s->opaque);
1768 }
1769}
1770
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001771static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001772{
1773 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001774 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001775 CaptureState *s;
1776
1777 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1778 if (i == n) {
1779 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001780 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001781 g_free (s);
bellardec36b692006-07-16 18:57:03 +00001782 return;
1783 }
1784 }
1785}
1786
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001787static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001788{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001789 const char *path = qdict_get_str(qdict, "path");
1790 int has_freq = qdict_haskey(qdict, "freq");
1791 int freq = qdict_get_try_int(qdict, "freq", -1);
1792 int has_bits = qdict_haskey(qdict, "bits");
1793 int bits = qdict_get_try_int(qdict, "bits", -1);
1794 int has_channels = qdict_haskey(qdict, "nchannels");
1795 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001796 CaptureState *s;
1797
Anthony Liguori7267c092011-08-20 22:09:37 -05001798 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001799
1800 freq = has_freq ? freq : 44100;
1801 bits = has_bits ? bits : 16;
1802 nchannels = has_channels ? nchannels : 2;
1803
1804 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001805 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05001806 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001807 return;
bellardec36b692006-07-16 18:57:03 +00001808 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001809 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001810}
bellardec36b692006-07-16 18:57:03 +00001811
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001812static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001813{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001814 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001815
aliguori76655d62009-03-06 20:27:37 +00001816 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001817 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001818 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001819 return acl;
1820}
aliguori76655d62009-03-06 20:27:37 +00001821
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001822static void hmp_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001823{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001824 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001825 qemu_acl *acl = find_acl(mon, aclname);
1826 qemu_acl_entry *entry;
1827 int i = 0;
1828
1829 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001830 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001831 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00001832 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00001833 i++;
1834 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001835 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001836 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001837 }
1838}
1839
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001840static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001841{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001842 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001843 qemu_acl *acl = find_acl(mon, aclname);
1844
1845 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001846 qemu_acl_reset(acl);
1847 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001848 }
1849}
aliguori76655d62009-03-06 20:27:37 +00001850
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001851static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001852{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001853 const char *aclname = qdict_get_str(qdict, "aclname");
1854 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001855 qemu_acl *acl = find_acl(mon, aclname);
1856
1857 if (acl) {
1858 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001859 acl->defaultDeny = 0;
1860 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001861 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001862 acl->defaultDeny = 1;
1863 monitor_printf(mon, "acl: policy set to 'deny'\n");
1864 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001865 monitor_printf(mon, "acl: unknown policy '%s', "
1866 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001867 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001868 }
1869}
aliguori76655d62009-03-06 20:27:37 +00001870
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001871static void hmp_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001872{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001873 const char *aclname = qdict_get_str(qdict, "aclname");
1874 const char *match = qdict_get_str(qdict, "match");
1875 const char *policy = qdict_get_str(qdict, "policy");
1876 int has_index = qdict_haskey(qdict, "index");
1877 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001878 qemu_acl *acl = find_acl(mon, aclname);
1879 int deny, ret;
1880
1881 if (acl) {
1882 if (strcmp(policy, "allow") == 0) {
1883 deny = 0;
1884 } else if (strcmp(policy, "deny") == 0) {
1885 deny = 1;
1886 } else {
1887 monitor_printf(mon, "acl: unknown policy '%s', "
1888 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001889 return;
1890 }
aliguori28a76be2009-03-06 20:27:40 +00001891 if (has_index)
1892 ret = qemu_acl_insert(acl, deny, match, index);
1893 else
1894 ret = qemu_acl_append(acl, deny, match);
1895 if (ret < 0)
1896 monitor_printf(mon, "acl: unable to add acl entry\n");
1897 else
1898 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001899 }
1900}
aliguori76655d62009-03-06 20:27:37 +00001901
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001902static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001903{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001904 const char *aclname = qdict_get_str(qdict, "aclname");
1905 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001906 qemu_acl *acl = find_acl(mon, aclname);
1907 int ret;
aliguori76655d62009-03-06 20:27:37 +00001908
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001909 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001910 ret = qemu_acl_remove(acl, match);
1911 if (ret < 0)
1912 monitor_printf(mon, "acl: no matching acl entry\n");
1913 else
1914 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001915 }
1916}
1917
Corey Bryant208c9d12012-06-22 14:36:09 -04001918void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001919{
Anthony Liguoric227f092009-10-01 16:12:16 -05001920 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001921 int fd;
1922
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03001923 fd = qemu_chr_fe_get_msgfd(&cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001924 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001925 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryant208c9d12012-06-22 14:36:09 -04001926 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001927 }
1928
1929 if (qemu_isdigit(fdname[0])) {
Stefan Hajnoczi0b9f0e22014-04-24 13:58:18 +02001930 close(fd);
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001931 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
1932 "a name not starting with a digit");
Corey Bryant208c9d12012-06-22 14:36:09 -04001933 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001934 }
1935
Corey Bryant208c9d12012-06-22 14:36:09 -04001936 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001937 if (strcmp(monfd->name, fdname) != 0) {
1938 continue;
1939 }
1940
1941 close(monfd->fd);
1942 monfd->fd = fd;
Corey Bryant208c9d12012-06-22 14:36:09 -04001943 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001944 }
1945
Anthony Liguori7267c092011-08-20 22:09:37 -05001946 monfd = g_malloc0(sizeof(mon_fd_t));
1947 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001948 monfd->fd = fd;
1949
Corey Bryant208c9d12012-06-22 14:36:09 -04001950 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001951}
1952
Corey Bryant208c9d12012-06-22 14:36:09 -04001953void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001954{
Anthony Liguoric227f092009-10-01 16:12:16 -05001955 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001956
Corey Bryant208c9d12012-06-22 14:36:09 -04001957 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001958 if (strcmp(monfd->name, fdname) != 0) {
1959 continue;
1960 }
1961
Blue Swirl72cf2d42009-09-12 07:36:22 +00001962 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001963 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001964 g_free(monfd->name);
1965 g_free(monfd);
Corey Bryant208c9d12012-06-22 14:36:09 -04001966 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001967 }
1968
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001969 error_setg(errp, QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001970}
1971
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001972int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
Mark McLoughlin7768e042009-07-22 09:11:41 +01001973{
Anthony Liguoric227f092009-10-01 16:12:16 -05001974 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01001975
Blue Swirl72cf2d42009-09-12 07:36:22 +00001976 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01001977 int fd;
1978
1979 if (strcmp(monfd->name, fdname) != 0) {
1980 continue;
1981 }
1982
1983 fd = monfd->fd;
1984
1985 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001986 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05001987 g_free(monfd->name);
1988 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001989
1990 return fd;
1991 }
1992
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001993 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001994 return -1;
1995}
1996
Corey Bryantba1c0482012-08-14 16:43:43 -04001997static void monitor_fdset_cleanup(MonFdset *mon_fdset)
1998{
1999 MonFdsetFd *mon_fdset_fd;
2000 MonFdsetFd *mon_fdset_fd_next;
2001
2002 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantebe52b52012-10-18 15:19:33 -04002003 if ((mon_fdset_fd->removed ||
2004 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
2005 runstate_is_running()) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002006 close(mon_fdset_fd->fd);
2007 g_free(mon_fdset_fd->opaque);
2008 QLIST_REMOVE(mon_fdset_fd, next);
2009 g_free(mon_fdset_fd);
2010 }
2011 }
2012
Corey Bryantadb696f2012-08-14 16:43:47 -04002013 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002014 QLIST_REMOVE(mon_fdset, next);
2015 g_free(mon_fdset);
2016 }
2017}
2018
Corey Bryantefb87c12012-08-14 16:43:48 -04002019static void monitor_fdsets_cleanup(void)
2020{
2021 MonFdset *mon_fdset;
2022 MonFdset *mon_fdset_next;
2023
2024 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
2025 monitor_fdset_cleanup(mon_fdset);
2026 }
2027}
2028
Corey Bryantba1c0482012-08-14 16:43:43 -04002029AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
2030 const char *opaque, Error **errp)
2031{
2032 int fd;
2033 Monitor *mon = cur_mon;
Corey Bryantba1c0482012-08-14 16:43:43 -04002034 AddfdInfo *fdinfo;
2035
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03002036 fd = qemu_chr_fe_get_msgfd(&mon->chr);
Corey Bryantba1c0482012-08-14 16:43:43 -04002037 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002038 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryantba1c0482012-08-14 16:43:43 -04002039 goto error;
2040 }
2041
Corey Bryante446f702012-10-18 15:19:32 -04002042 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
2043 has_opaque, opaque, errp);
2044 if (fdinfo) {
2045 return fdinfo;
Corey Bryant9ac54af2012-10-18 15:19:31 -04002046 }
2047
Corey Bryantba1c0482012-08-14 16:43:43 -04002048error:
2049 if (fd != -1) {
2050 close(fd);
2051 }
2052 return NULL;
2053}
2054
2055void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
2056{
2057 MonFdset *mon_fdset;
2058 MonFdsetFd *mon_fdset_fd;
2059 char fd_str[60];
2060
2061 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2062 if (mon_fdset->id != fdset_id) {
2063 continue;
2064 }
2065 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2066 if (has_fd) {
2067 if (mon_fdset_fd->fd != fd) {
2068 continue;
2069 }
2070 mon_fdset_fd->removed = true;
2071 break;
2072 } else {
2073 mon_fdset_fd->removed = true;
2074 }
2075 }
2076 if (has_fd && !mon_fdset_fd) {
2077 goto error;
2078 }
2079 monitor_fdset_cleanup(mon_fdset);
2080 return;
2081 }
2082
2083error:
2084 if (has_fd) {
2085 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
2086 fdset_id, fd);
2087 } else {
2088 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
2089 }
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002090 error_setg(errp, QERR_FD_NOT_FOUND, fd_str);
Corey Bryantba1c0482012-08-14 16:43:43 -04002091}
2092
2093FdsetInfoList *qmp_query_fdsets(Error **errp)
2094{
2095 MonFdset *mon_fdset;
2096 MonFdsetFd *mon_fdset_fd;
2097 FdsetInfoList *fdset_list = NULL;
2098
2099 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2100 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
2101 FdsetFdInfoList *fdsetfd_list = NULL;
2102
2103 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
2104 fdset_info->value->fdset_id = mon_fdset->id;
2105
2106 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2107 FdsetFdInfoList *fdsetfd_info;
2108
2109 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
2110 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
2111 fdsetfd_info->value->fd = mon_fdset_fd->fd;
2112 if (mon_fdset_fd->opaque) {
2113 fdsetfd_info->value->has_opaque = true;
2114 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
2115 } else {
2116 fdsetfd_info->value->has_opaque = false;
2117 }
2118
2119 fdsetfd_info->next = fdsetfd_list;
2120 fdsetfd_list = fdsetfd_info;
2121 }
2122
2123 fdset_info->value->fds = fdsetfd_list;
2124
2125 fdset_info->next = fdset_list;
2126 fdset_list = fdset_info;
2127 }
2128
2129 return fdset_list;
2130}
2131
Corey Bryante446f702012-10-18 15:19:32 -04002132AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
2133 bool has_opaque, const char *opaque,
2134 Error **errp)
2135{
2136 MonFdset *mon_fdset = NULL;
2137 MonFdsetFd *mon_fdset_fd;
2138 AddfdInfo *fdinfo;
2139
2140 if (has_fdset_id) {
2141 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2142 /* Break if match found or match impossible due to ordering by ID */
2143 if (fdset_id <= mon_fdset->id) {
2144 if (fdset_id < mon_fdset->id) {
2145 mon_fdset = NULL;
2146 }
2147 break;
2148 }
2149 }
2150 }
2151
2152 if (mon_fdset == NULL) {
2153 int64_t fdset_id_prev = -1;
2154 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
2155
2156 if (has_fdset_id) {
2157 if (fdset_id < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002158 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
2159 "a non-negative value");
Corey Bryante446f702012-10-18 15:19:32 -04002160 return NULL;
2161 }
2162 /* Use specified fdset ID */
2163 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2164 mon_fdset_cur = mon_fdset;
2165 if (fdset_id < mon_fdset_cur->id) {
2166 break;
2167 }
2168 }
2169 } else {
2170 /* Use first available fdset ID */
2171 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2172 mon_fdset_cur = mon_fdset;
2173 if (fdset_id_prev == mon_fdset_cur->id - 1) {
2174 fdset_id_prev = mon_fdset_cur->id;
2175 continue;
2176 }
2177 break;
2178 }
2179 }
2180
2181 mon_fdset = g_malloc0(sizeof(*mon_fdset));
2182 if (has_fdset_id) {
2183 mon_fdset->id = fdset_id;
2184 } else {
2185 mon_fdset->id = fdset_id_prev + 1;
2186 }
2187
2188 /* The fdset list is ordered by fdset ID */
2189 if (!mon_fdset_cur) {
2190 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
2191 } else if (mon_fdset->id < mon_fdset_cur->id) {
2192 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
2193 } else {
2194 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
2195 }
2196 }
2197
2198 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
2199 mon_fdset_fd->fd = fd;
2200 mon_fdset_fd->removed = false;
2201 if (has_opaque) {
2202 mon_fdset_fd->opaque = g_strdup(opaque);
2203 }
2204 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
2205
2206 fdinfo = g_malloc0(sizeof(*fdinfo));
2207 fdinfo->fdset_id = mon_fdset->id;
2208 fdinfo->fd = mon_fdset_fd->fd;
2209
2210 return fdinfo;
2211}
2212
Corey Bryantadb696f2012-08-14 16:43:47 -04002213int monitor_fdset_get_fd(int64_t fdset_id, int flags)
2214{
Blue Swirlb2dc64c2012-08-18 20:14:54 +00002215#ifndef _WIN32
Corey Bryantadb696f2012-08-14 16:43:47 -04002216 MonFdset *mon_fdset;
2217 MonFdsetFd *mon_fdset_fd;
2218 int mon_fd_flags;
2219
Corey Bryantadb696f2012-08-14 16:43:47 -04002220 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2221 if (mon_fdset->id != fdset_id) {
2222 continue;
2223 }
2224 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2225 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
2226 if (mon_fd_flags == -1) {
2227 return -1;
2228 }
2229
2230 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
2231 return mon_fdset_fd->fd;
2232 }
2233 }
2234 errno = EACCES;
2235 return -1;
2236 }
2237#endif
2238
2239 errno = ENOENT;
2240 return -1;
2241}
2242
2243int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
2244{
2245 MonFdset *mon_fdset;
2246 MonFdsetFd *mon_fdset_fd_dup;
2247
2248 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2249 if (mon_fdset->id != fdset_id) {
2250 continue;
2251 }
2252 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2253 if (mon_fdset_fd_dup->fd == dup_fd) {
2254 return -1;
2255 }
2256 }
2257 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2258 mon_fdset_fd_dup->fd = dup_fd;
2259 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
2260 return 0;
2261 }
2262 return -1;
2263}
2264
2265static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2266{
2267 MonFdset *mon_fdset;
2268 MonFdsetFd *mon_fdset_fd_dup;
2269
2270 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2271 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2272 if (mon_fdset_fd_dup->fd == dup_fd) {
2273 if (remove) {
2274 QLIST_REMOVE(mon_fdset_fd_dup, next);
2275 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2276 monitor_fdset_cleanup(mon_fdset);
2277 }
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002278 return -1;
2279 } else {
2280 return mon_fdset->id;
Corey Bryantadb696f2012-08-14 16:43:47 -04002281 }
Corey Bryantadb696f2012-08-14 16:43:47 -04002282 }
2283 }
2284 }
2285 return -1;
2286}
2287
2288int monitor_fdset_dup_fd_find(int dup_fd)
2289{
2290 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2291}
2292
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002293void monitor_fdset_dup_fd_remove(int dup_fd)
Corey Bryantadb696f2012-08-14 16:43:47 -04002294{
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002295 monitor_fdset_dup_fd_find_remove(dup_fd, true);
Corey Bryantadb696f2012-08-14 16:43:47 -04002296}
2297
Markus Armbruster1677f4c2015-02-09 14:03:19 +01002298int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
Laszlo Ersek59063662014-04-10 10:24:31 +02002299{
2300 int fd;
2301 Error *local_err = NULL;
2302
2303 if (!qemu_isdigit(fdname[0]) && mon) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002304 fd = monitor_get_fd(mon, fdname, &local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002305 } else {
2306 fd = qemu_parse_fd(fdname);
Laszlo Ersek59063662014-04-10 10:24:31 +02002307 if (fd == -1) {
2308 error_setg(&local_err, "Invalid file descriptor number '%s'",
2309 fdname);
2310 }
2311 }
2312 if (local_err) {
2313 error_propagate(errp, local_err);
2314 assert(fd == -1);
2315 } else {
2316 assert(fd != -1);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002317 }
2318
2319 return fd;
2320}
2321
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002322/* Please update hmp-commands.hx when adding or changing commands */
Wayne Xia816f8922011-10-12 11:32:41 +08002323static mon_cmd_t info_cmds[] = {
Pavel Butsykinda76ee72015-09-10 18:38:58 +03002324#include "hmp-commands-info.h"
2325 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002326};
2327
Wenchao Xiaa13ced52013-01-14 14:06:28 +08002328/* mon_cmds and info_cmds would be sorted at runtime */
2329static mon_cmd_t mon_cmds[] = {
2330#include "hmp-commands.h"
2331 { NULL, NULL, },
2332};
2333
bellard9307c4c2004-04-04 12:57:25 +00002334/*******************************************************************/
2335
2336static const char *pch;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002337static sigjmp_buf expr_env;
bellard9307c4c2004-04-04 12:57:25 +00002338
bellard9307c4c2004-04-04 12:57:25 +00002339
Stefan Weil9c3175c2013-08-22 21:30:09 +02002340static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
2341expr_error(Monitor *mon, const char *fmt, ...)
bellard9dc39cb2004-03-14 21:38:27 +00002342{
Fam Zheng277acfe2013-08-20 10:58:21 +08002343 va_list ap;
2344 va_start(ap, fmt);
2345 monitor_vprintf(mon, fmt, ap);
2346 monitor_printf(mon, "\n");
2347 va_end(ap);
Peter Maydell6ab7e542013-02-20 15:21:09 +00002348 siglongjmp(expr_env, 1);
bellard9307c4c2004-04-04 12:57:25 +00002349}
2350
Markus Armbruster09b94182010-01-20 13:07:30 +01002351/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00002352static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002353{
Pavel Butsykinbf957282015-09-10 18:38:59 +03002354 const MonitorDef *md = target_monitor_defs();
Thomas Huth854e67f2017-01-13 13:12:35 +01002355 CPUState *cs = mon_get_cpu();
bellard92a31b12005-02-10 22:00:52 +00002356 void *ptr;
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002357 uint64_t tmp = 0;
2358 int ret;
bellard92a31b12005-02-10 22:00:52 +00002359
Thomas Huth854e67f2017-01-13 13:12:35 +01002360 if (cs == NULL || md == NULL) {
Pavel Butsykinbf957282015-09-10 18:38:59 +03002361 return -1;
2362 }
2363
2364 for(; md->name != NULL; md++) {
bellard9307c4c2004-04-04 12:57:25 +00002365 if (compare_cmd(name, md->name)) {
2366 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002367 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002368 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002369 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00002370 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002371 switch(md->type) {
2372 case MD_I32:
2373 *pval = *(int32_t *)ptr;
2374 break;
2375 case MD_TLONG:
2376 *pval = *(target_long *)ptr;
2377 break;
2378 default:
2379 *pval = 0;
2380 break;
2381 }
bellard9307c4c2004-04-04 12:57:25 +00002382 }
2383 return 0;
2384 }
2385 }
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002386
Thomas Huth854e67f2017-01-13 13:12:35 +01002387 ret = target_get_monitor_def(cs, name, &tmp);
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002388 if (!ret) {
2389 *pval = (target_long) tmp;
2390 }
2391
2392 return ret;
bellard9307c4c2004-04-04 12:57:25 +00002393}
2394
2395static void next(void)
2396{
Blue Swirl660f11b2009-07-31 21:16:51 +00002397 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002398 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002399 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002400 pch++;
2401 }
2402}
2403
aliguori376253e2009-03-05 23:01:23 +00002404static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002405
aliguori376253e2009-03-05 23:01:23 +00002406static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002407{
blueswir1c2efc952007-09-25 17:28:42 +00002408 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002409 char *p;
bellard6a00d602005-11-21 23:25:50 +00002410 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002411
2412 switch(*pch) {
2413 case '+':
2414 next();
aliguori376253e2009-03-05 23:01:23 +00002415 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002416 break;
2417 case '-':
2418 next();
aliguori376253e2009-03-05 23:01:23 +00002419 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002420 break;
2421 case '~':
2422 next();
aliguori376253e2009-03-05 23:01:23 +00002423 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002424 break;
2425 case '(':
2426 next();
aliguori376253e2009-03-05 23:01:23 +00002427 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002428 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002429 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002430 }
2431 next();
2432 break;
bellard81d09122004-07-14 17:21:37 +00002433 case '\'':
2434 pch++;
2435 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002436 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002437 n = *pch;
2438 pch++;
2439 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002440 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002441 next();
2442 break;
bellard9307c4c2004-04-04 12:57:25 +00002443 case '$':
2444 {
2445 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002446 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002447
bellard9307c4c2004-04-04 12:57:25 +00002448 pch++;
2449 q = buf;
2450 while ((*pch >= 'a' && *pch <= 'z') ||
2451 (*pch >= 'A' && *pch <= 'Z') ||
2452 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002453 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002454 if ((q - buf) < sizeof(buf) - 1)
2455 *q++ = *pch;
2456 pch++;
2457 }
blueswir1cd390082008-11-16 13:53:32 +00002458 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002459 pch++;
2460 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002461 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01002462 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00002463 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00002464 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002465 }
2466 break;
2467 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002468 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002469 n = 0;
2470 break;
2471 default:
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002472 errno = 0;
bellard4f4fbf72006-06-25 18:28:12 +00002473 n = strtoull(pch, &p, 0);
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002474 if (errno == ERANGE) {
2475 expr_error(mon, "number too large");
2476 }
bellard9307c4c2004-04-04 12:57:25 +00002477 if (pch == p) {
Fam Zheng277acfe2013-08-20 10:58:21 +08002478 expr_error(mon, "invalid char '%c' in expression", *p);
bellard9307c4c2004-04-04 12:57:25 +00002479 }
2480 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002481 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002482 pch++;
2483 break;
2484 }
2485 return n;
2486}
2487
2488
aliguori376253e2009-03-05 23:01:23 +00002489static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002490{
blueswir1c2efc952007-09-25 17:28:42 +00002491 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002492 int op;
ths3b46e622007-09-17 08:09:54 +00002493
aliguori376253e2009-03-05 23:01:23 +00002494 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002495 for(;;) {
2496 op = *pch;
2497 if (op != '*' && op != '/' && op != '%')
2498 break;
2499 next();
aliguori376253e2009-03-05 23:01:23 +00002500 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002501 switch(op) {
2502 default:
2503 case '*':
2504 val *= val2;
2505 break;
2506 case '/':
2507 case '%':
ths5fafdf22007-09-16 21:08:06 +00002508 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002509 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002510 if (op == '/')
2511 val /= val2;
2512 else
2513 val %= val2;
2514 break;
2515 }
2516 }
2517 return val;
2518}
2519
aliguori376253e2009-03-05 23:01:23 +00002520static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002521{
blueswir1c2efc952007-09-25 17:28:42 +00002522 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002523 int op;
bellard9307c4c2004-04-04 12:57:25 +00002524
aliguori376253e2009-03-05 23:01:23 +00002525 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002526 for(;;) {
2527 op = *pch;
2528 if (op != '&' && op != '|' && op != '^')
2529 break;
2530 next();
aliguori376253e2009-03-05 23:01:23 +00002531 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002532 switch(op) {
2533 default:
2534 case '&':
2535 val &= val2;
2536 break;
2537 case '|':
2538 val |= val2;
2539 break;
2540 case '^':
2541 val ^= val2;
2542 break;
2543 }
2544 }
2545 return val;
2546}
2547
aliguori376253e2009-03-05 23:01:23 +00002548static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002549{
blueswir1c2efc952007-09-25 17:28:42 +00002550 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002551 int op;
bellard9307c4c2004-04-04 12:57:25 +00002552
aliguori376253e2009-03-05 23:01:23 +00002553 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002554 for(;;) {
2555 op = *pch;
2556 if (op != '+' && op != '-')
2557 break;
2558 next();
aliguori376253e2009-03-05 23:01:23 +00002559 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002560 if (op == '+')
2561 val += val2;
2562 else
2563 val -= val2;
2564 }
2565 return val;
2566}
2567
aliguori376253e2009-03-05 23:01:23 +00002568static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002569{
2570 pch = *pp;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002571 if (sigsetjmp(expr_env, 0)) {
bellard9307c4c2004-04-04 12:57:25 +00002572 *pp = pch;
2573 return -1;
2574 }
blueswir1cd390082008-11-16 13:53:32 +00002575 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002576 pch++;
aliguori376253e2009-03-05 23:01:23 +00002577 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002578 *pp = pch;
2579 return 0;
2580}
2581
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002582static int get_double(Monitor *mon, double *pval, const char **pp)
2583{
2584 const char *p = *pp;
2585 char *tailp;
2586 double d;
2587
2588 d = strtod(p, &tailp);
2589 if (tailp == p) {
2590 monitor_printf(mon, "Number expected\n");
2591 return -1;
2592 }
2593 if (d != d || d - d != 0) {
2594 /* NaN or infinity */
2595 monitor_printf(mon, "Bad number\n");
2596 return -1;
2597 }
2598 *pval = d;
2599 *pp = tailp;
2600 return 0;
2601}
2602
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002603/*
2604 * Store the command-name in cmdname, and return a pointer to
2605 * the remaining of the command string.
2606 */
2607static const char *get_command_name(const char *cmdline,
2608 char *cmdname, size_t nlen)
2609{
2610 size_t len;
2611 const char *p, *pstart;
2612
2613 p = cmdline;
2614 while (qemu_isspace(*p))
2615 p++;
2616 if (*p == '\0')
2617 return NULL;
2618 pstart = p;
2619 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2620 p++;
2621 len = p - pstart;
2622 if (len > nlen - 1)
2623 len = nlen - 1;
2624 memcpy(cmdname, pstart, len);
2625 cmdname[len] = '\0';
2626 return p;
2627}
2628
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002629/**
2630 * Read key of 'type' into 'key' and return the current
2631 * 'type' pointer.
2632 */
2633static char *key_get_info(const char *type, char **key)
2634{
2635 size_t len;
2636 char *p, *str;
2637
2638 if (*type == ',')
2639 type++;
2640
2641 p = strchr(type, ':');
2642 if (!p) {
2643 *key = NULL;
2644 return NULL;
2645 }
2646 len = p - type;
2647
Anthony Liguori7267c092011-08-20 22:09:37 -05002648 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002649 memcpy(str, type, len);
2650 str[len] = '\0';
2651
2652 *key = str;
2653 return ++p;
2654}
2655
bellard9307c4c2004-04-04 12:57:25 +00002656static int default_fmt_format = 'x';
2657static int default_fmt_size = 4;
2658
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002659static int is_valid_option(const char *c, const char *typestr)
2660{
2661 char option[3];
2662
2663 option[0] = '-';
2664 option[1] = *c;
2665 option[2] = '\0';
2666
2667 typestr = strstr(typestr, option);
2668 return (typestr != NULL);
2669}
2670
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03002671static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
2672 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002673{
2674 const mon_cmd_t *cmd;
2675
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03002676 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002677 if (compare_cmd(cmdname, cmd->name)) {
2678 return cmd;
2679 }
2680 }
2681
2682 return NULL;
2683}
2684
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002685/*
Bandan Dasae502122015-06-03 18:38:08 -04002686 * Parse command name from @cmdp according to command table @table.
2687 * If blank, return NULL.
2688 * Else, if no valid command can be found, report to @mon, and return
2689 * NULL.
2690 * Else, change @cmdp to point right behind the name, and return its
2691 * command table entry.
2692 * Do not assume the return value points into @table! It doesn't when
2693 * the command is found in a sub-command table.
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002694 */
Anthony Liguoric227f092009-10-01 16:12:16 -05002695static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Bandan Dasae502122015-06-03 18:38:08 -04002696 const char **cmdp,
2697 mon_cmd_t *table)
bellard9307c4c2004-04-04 12:57:25 +00002698{
Bandan Dasae502122015-06-03 18:38:08 -04002699 const char *p;
Anthony Liguoric227f092009-10-01 16:12:16 -05002700 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002701 char cmdname[256];
bellard9dc39cb2004-03-14 21:38:27 +00002702
bellard9307c4c2004-04-04 12:57:25 +00002703 /* extract the command name */
Bandan Dasae502122015-06-03 18:38:08 -04002704 p = get_command_name(*cmdp, cmdname, sizeof(cmdname));
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002705 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002706 return NULL;
ths3b46e622007-09-17 08:09:54 +00002707
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002708 cmd = search_dispatch_table(table, cmdname);
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002709 if (!cmd) {
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002710 monitor_printf(mon, "unknown command: '%.*s'\n",
Bandan Dasae502122015-06-03 18:38:08 -04002711 (int)(p - *cmdp), *cmdp);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002712 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002713 }
bellard9307c4c2004-04-04 12:57:25 +00002714
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002715 /* filter out following useless space */
2716 while (qemu_isspace(*p)) {
2717 p++;
2718 }
Bandan Dasae502122015-06-03 18:38:08 -04002719
2720 *cmdp = p;
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002721 /* search sub command */
Bandan Dasae502122015-06-03 18:38:08 -04002722 if (cmd->sub_table != NULL && *p != '\0') {
2723 return monitor_parse_command(mon, cmdp, cmd->sub_table);
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002724 }
2725
Bandan Dasae502122015-06-03 18:38:08 -04002726 return cmd;
2727}
2728
2729/*
2730 * Parse arguments for @cmd.
2731 * If it can't be parsed, report to @mon, and return NULL.
2732 * Else, insert command arguments into a QDict, and return it.
2733 * Note: On success, caller has to free the QDict structure.
2734 */
2735
2736static QDict *monitor_parse_arguments(Monitor *mon,
2737 const char **endp,
2738 const mon_cmd_t *cmd)
2739{
2740 const char *typestr;
2741 char *key;
2742 int c;
2743 const char *p = *endp;
2744 char buf[1024];
2745 QDict *qdict = qdict_new();
2746
bellard9307c4c2004-04-04 12:57:25 +00002747 /* parse the parameters */
2748 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00002749 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002750 typestr = key_get_info(typestr, &key);
2751 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00002752 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002753 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00002754 typestr++;
2755 switch(c) {
2756 case 'F':
bellard81d09122004-07-14 17:21:37 +00002757 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002758 case 's':
2759 {
2760 int ret;
ths3b46e622007-09-17 08:09:54 +00002761
blueswir1cd390082008-11-16 13:53:32 +00002762 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002763 p++;
2764 if (*typestr == '?') {
2765 typestr++;
2766 if (*p == '\0') {
2767 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03002768 break;
bellard9307c4c2004-04-04 12:57:25 +00002769 }
2770 }
2771 ret = get_str(buf, sizeof(buf), &p);
2772 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002773 switch(c) {
2774 case 'F':
aliguori376253e2009-03-05 23:01:23 +00002775 monitor_printf(mon, "%s: filename expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002776 cmd->name);
bellard81d09122004-07-14 17:21:37 +00002777 break;
2778 case 'B':
aliguori376253e2009-03-05 23:01:23 +00002779 monitor_printf(mon, "%s: block device name expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002780 cmd->name);
bellard81d09122004-07-14 17:21:37 +00002781 break;
2782 default:
Bandan Dasae502122015-06-03 18:38:08 -04002783 monitor_printf(mon, "%s: string expected\n", cmd->name);
bellard81d09122004-07-14 17:21:37 +00002784 break;
2785 }
bellard9307c4c2004-04-04 12:57:25 +00002786 goto fail;
2787 }
Eric Blake46f5ac22017-04-27 16:58:17 -05002788 qdict_put_str(qdict, key, buf);
bellard9307c4c2004-04-04 12:57:25 +00002789 }
2790 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01002791 case 'O':
2792 {
2793 QemuOptsList *opts_list;
2794 QemuOpts *opts;
2795
2796 opts_list = qemu_find_opts(key);
2797 if (!opts_list || opts_list->desc->name) {
2798 goto bad_type;
2799 }
2800 while (qemu_isspace(*p)) {
2801 p++;
2802 }
2803 if (!*p)
2804 break;
2805 if (get_str(buf, sizeof(buf), &p) < 0) {
2806 goto fail;
2807 }
Markus Armbruster70b94332015-02-13 12:50:26 +01002808 opts = qemu_opts_parse_noisily(opts_list, buf, true);
Markus Armbruster361127d2010-02-10 20:24:35 +01002809 if (!opts) {
2810 goto fail;
2811 }
2812 qemu_opts_to_qdict(opts, qdict);
2813 qemu_opts_del(opts);
2814 }
2815 break;
bellard9307c4c2004-04-04 12:57:25 +00002816 case '/':
2817 {
2818 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002819
blueswir1cd390082008-11-16 13:53:32 +00002820 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002821 p++;
2822 if (*p == '/') {
2823 /* format found */
2824 p++;
2825 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002826 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002827 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002828 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002829 count = count * 10 + (*p - '0');
2830 p++;
2831 }
2832 }
2833 size = -1;
2834 format = -1;
2835 for(;;) {
2836 switch(*p) {
2837 case 'o':
2838 case 'd':
2839 case 'u':
2840 case 'x':
2841 case 'i':
2842 case 'c':
2843 format = *p++;
2844 break;
2845 case 'b':
2846 size = 1;
2847 p++;
2848 break;
2849 case 'h':
2850 size = 2;
2851 p++;
2852 break;
2853 case 'w':
2854 size = 4;
2855 p++;
2856 break;
2857 case 'g':
2858 case 'L':
2859 size = 8;
2860 p++;
2861 break;
2862 default:
2863 goto next;
2864 }
2865 }
2866 next:
blueswir1cd390082008-11-16 13:53:32 +00002867 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00002868 monitor_printf(mon, "invalid char in format: '%c'\n",
2869 *p);
bellard9307c4c2004-04-04 12:57:25 +00002870 goto fail;
2871 }
bellard9307c4c2004-04-04 12:57:25 +00002872 if (format < 0)
2873 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002874 if (format != 'i') {
2875 /* for 'i', not specifying a size gives -1 as size */
2876 if (size < 0)
2877 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002878 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002879 }
bellard9307c4c2004-04-04 12:57:25 +00002880 default_fmt_format = format;
2881 } else {
2882 count = 1;
2883 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002884 if (format != 'i') {
2885 size = default_fmt_size;
2886 } else {
2887 size = -1;
2888 }
bellard9307c4c2004-04-04 12:57:25 +00002889 }
Eric Blake46f5ac22017-04-27 16:58:17 -05002890 qdict_put_int(qdict, "count", count);
2891 qdict_put_int(qdict, "format", format);
2892 qdict_put_int(qdict, "size", size);
bellard9307c4c2004-04-04 12:57:25 +00002893 }
2894 break;
2895 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002896 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002897 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00002898 {
blueswir1c2efc952007-09-25 17:28:42 +00002899 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00002900
blueswir1cd390082008-11-16 13:53:32 +00002901 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002902 p++;
bellard34405572004-06-08 00:55:58 +00002903 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002904 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03002905 if (*p == '\0') {
2906 typestr++;
2907 break;
2908 }
bellard34405572004-06-08 00:55:58 +00002909 } else {
2910 if (*p == '.') {
2911 p++;
blueswir1cd390082008-11-16 13:53:32 +00002912 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00002913 p++;
bellard34405572004-06-08 00:55:58 +00002914 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03002915 typestr++;
2916 break;
bellard34405572004-06-08 00:55:58 +00002917 }
2918 }
bellard13224a82006-07-14 22:03:35 +00002919 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002920 }
aliguori376253e2009-03-05 23:01:23 +00002921 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00002922 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002923 /* Check if 'i' is greater than 32-bit */
2924 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
Bandan Dasae502122015-06-03 18:38:08 -04002925 monitor_printf(mon, "\'%s\' has failed: ", cmd->name);
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002926 monitor_printf(mon, "integer is for 32-bit values\n");
2927 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002928 } else if (c == 'M') {
Luiz Capitulino91162842012-04-26 17:34:30 -03002929 if (val < 0) {
2930 monitor_printf(mon, "enter a positive value\n");
2931 goto fail;
2932 }
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002933 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002934 }
Eric Blake46f5ac22017-04-27 16:58:17 -05002935 qdict_put_int(qdict, key, val);
bellard9307c4c2004-04-04 12:57:25 +00002936 }
2937 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02002938 case 'o':
2939 {
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +01002940 int ret;
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +01002941 uint64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02002942 char *end;
2943
2944 while (qemu_isspace(*p)) {
2945 p++;
2946 }
2947 if (*typestr == '?') {
2948 typestr++;
2949 if (*p == '\0') {
2950 break;
2951 }
2952 }
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +01002953 ret = qemu_strtosz_MiB(p, &end, &val);
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +01002954 if (ret < 0 || val > INT64_MAX) {
Jes Sorensendbc0c672010-10-21 17:15:47 +02002955 monitor_printf(mon, "invalid size\n");
2956 goto fail;
2957 }
Eric Blake46f5ac22017-04-27 16:58:17 -05002958 qdict_put_int(qdict, key, val);
Jes Sorensendbc0c672010-10-21 17:15:47 +02002959 p = end;
2960 }
2961 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01002962 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002963 {
2964 double val;
2965
2966 while (qemu_isspace(*p))
2967 p++;
2968 if (*typestr == '?') {
2969 typestr++;
2970 if (*p == '\0') {
2971 break;
2972 }
2973 }
2974 if (get_double(mon, &val, &p) < 0) {
2975 goto fail;
2976 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02002977 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01002978 switch (*p) {
2979 case 'm':
2980 val /= 1e3; p += 2; break;
2981 case 'u':
2982 val /= 1e6; p += 2; break;
2983 case 'n':
2984 val /= 1e9; p += 2; break;
2985 }
2986 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002987 if (*p && !qemu_isspace(*p)) {
2988 monitor_printf(mon, "Unknown unit suffix\n");
2989 goto fail;
2990 }
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +04002991 qdict_put(qdict, key, qnum_from_double(val));
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002992 }
2993 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002994 case 'b':
2995 {
2996 const char *beg;
Eric Blakefc48ffc2015-05-15 16:24:59 -06002997 bool val;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002998
2999 while (qemu_isspace(*p)) {
3000 p++;
3001 }
3002 beg = p;
3003 while (qemu_isgraph(*p)) {
3004 p++;
3005 }
3006 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06003007 val = true;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003008 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06003009 val = false;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003010 } else {
3011 monitor_printf(mon, "Expected 'on' or 'off'\n");
3012 goto fail;
3013 }
Eric Blake46f5ac22017-04-27 16:58:17 -05003014 qdict_put_bool(qdict, key, val);
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003015 }
3016 break;
bellard9307c4c2004-04-04 12:57:25 +00003017 case '-':
3018 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003019 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003020 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003021 /* option */
ths3b46e622007-09-17 08:09:54 +00003022
bellard9307c4c2004-04-04 12:57:25 +00003023 c = *typestr++;
3024 if (c == '\0')
3025 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003026 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003027 p++;
bellard9307c4c2004-04-04 12:57:25 +00003028 if (*p == '-') {
3029 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003030 if(c != *p) {
3031 if(!is_valid_option(p, typestr)) {
3032
3033 monitor_printf(mon, "%s: unsupported option -%c\n",
Bandan Dasae502122015-06-03 18:38:08 -04003034 cmd->name, *p);
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003035 goto fail;
3036 } else {
3037 skip_key = 1;
3038 }
bellard9307c4c2004-04-04 12:57:25 +00003039 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003040 if(skip_key) {
3041 p = tmp;
3042 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003043 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003044 p++;
Eric Blake46f5ac22017-04-27 16:58:17 -05003045 qdict_put_bool(qdict, key, true);
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003046 }
bellard9307c4c2004-04-04 12:57:25 +00003047 }
bellard9307c4c2004-04-04 12:57:25 +00003048 }
3049 break;
Wenchao Xia129be002013-08-27 20:38:26 +08003050 case 'S':
3051 {
3052 /* package all remaining string */
3053 int len;
3054
3055 while (qemu_isspace(*p)) {
3056 p++;
3057 }
3058 if (*typestr == '?') {
3059 typestr++;
3060 if (*p == '\0') {
3061 /* no remaining string: NULL argument */
3062 break;
3063 }
3064 }
3065 len = strlen(p);
3066 if (len <= 0) {
3067 monitor_printf(mon, "%s: string expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04003068 cmd->name);
Bandan Dase549d2a2015-06-03 18:38:10 -04003069 goto fail;
Wenchao Xia129be002013-08-27 20:38:26 +08003070 }
Eric Blake46f5ac22017-04-27 16:58:17 -05003071 qdict_put_str(qdict, key, p);
Wenchao Xia129be002013-08-27 20:38:26 +08003072 p += len;
3073 }
3074 break;
bellard9307c4c2004-04-04 12:57:25 +00003075 default:
3076 bad_type:
Bandan Dasae502122015-06-03 18:38:08 -04003077 monitor_printf(mon, "%s: unknown type '%c'\n", cmd->name, c);
bellard9307c4c2004-04-04 12:57:25 +00003078 goto fail;
3079 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003080 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003081 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003082 }
3083 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003084 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003085 p++;
3086 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003087 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
Bandan Dasae502122015-06-03 18:38:08 -04003088 cmd->name);
bellard9307c4c2004-04-04 12:57:25 +00003089 goto fail;
3090 }
3091
Bandan Dasae502122015-06-03 18:38:08 -04003092 return qdict;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003093
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003094fail:
Bandan Dasae502122015-06-03 18:38:08 -04003095 QDECREF(qdict);
Anthony Liguori7267c092011-08-20 22:09:37 -05003096 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003097 return NULL;
3098}
3099
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003100static void handle_hmp_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003101{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003102 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003103 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003104
Stefan Hajnoczi79cad8b2017-06-05 11:42:15 +01003105 trace_handle_hmp_command(mon, cmdline);
3106
Bandan Dasae502122015-06-03 18:38:08 -04003107 cmd = monitor_parse_command(mon, &cmdline, mon->cmd_table);
3108 if (!cmd) {
3109 return;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003110 }
3111
Bandan Dasae502122015-06-03 18:38:08 -04003112 qdict = monitor_parse_arguments(mon, &cmdline, cmd);
3113 if (!qdict) {
Bandan Dasdd41eea2015-06-03 18:38:09 -04003114 monitor_printf(mon, "Try \"help %s\" for more information\n",
3115 cmd->name);
Bandan Dasae502122015-06-03 18:38:08 -04003116 return;
3117 }
3118
Marc-André Lureau2b9e3572016-09-12 13:19:06 +04003119 cmd->cmd(mon, qdict);
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003120 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00003121}
3122
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003123static void cmd_completion(Monitor *mon, const char *name, const char *list)
bellard81d09122004-07-14 17:21:37 +00003124{
3125 const char *p, *pstart;
3126 char cmd[128];
3127 int len;
3128
3129 p = list;
3130 for(;;) {
3131 pstart = p;
3132 p = strchr(p, '|');
3133 if (!p)
3134 p = pstart + strlen(pstart);
3135 len = p - pstart;
3136 if (len > sizeof(cmd) - 2)
3137 len = sizeof(cmd) - 2;
3138 memcpy(cmd, pstart, len);
3139 cmd[len] = '\0';
3140 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003141 readline_add_completion(mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00003142 }
3143 if (*p == '\0')
3144 break;
3145 p++;
3146 }
3147}
3148
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003149static void file_completion(Monitor *mon, const char *input)
bellard81d09122004-07-14 17:21:37 +00003150{
3151 DIR *ffs;
3152 struct dirent *d;
3153 char path[1024];
3154 char file[1024], file_prefix[1024];
3155 int input_path_len;
3156 const char *p;
3157
ths5fafdf22007-09-16 21:08:06 +00003158 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00003159 if (!p) {
3160 input_path_len = 0;
3161 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00003162 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00003163 } else {
3164 input_path_len = p - input + 1;
3165 memcpy(path, input, input_path_len);
3166 if (input_path_len > sizeof(path) - 1)
3167 input_path_len = sizeof(path) - 1;
3168 path[input_path_len] = '\0';
3169 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3170 }
Bandan Das19f2db52015-06-03 18:38:07 -04003171
bellard81d09122004-07-14 17:21:37 +00003172 ffs = opendir(path);
3173 if (!ffs)
3174 return;
3175 for(;;) {
3176 struct stat sb;
3177 d = readdir(ffs);
3178 if (!d)
3179 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09003180
3181 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
3182 continue;
3183 }
3184
bellard81d09122004-07-14 17:21:37 +00003185 if (strstart(d->d_name, file_prefix, NULL)) {
3186 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00003187 if (input_path_len < sizeof(file))
3188 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3189 d->d_name);
bellard81d09122004-07-14 17:21:37 +00003190 /* stat the file to find out if it's a directory.
3191 * In that case add a slash to speed up typing long paths
3192 */
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01003193 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
blueswir1363a37d2008-08-21 17:58:08 +00003194 pstrcat(file, sizeof(file), "/");
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01003195 }
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003196 readline_add_completion(mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003197 }
3198 }
3199 closedir(ffs);
3200}
3201
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003202static const char *next_arg_type(const char *typestr)
3203{
3204 const char *p = strchr(typestr, ':');
3205 return (p != NULL ? ++p : typestr);
3206}
3207
Hani Benhabiles40d19392014-05-07 23:41:30 +01003208static void add_completion_option(ReadLineState *rs, const char *str,
3209 const char *option)
3210{
3211 if (!str || !option) {
3212 return;
3213 }
3214 if (!strncmp(option, str, strlen(str))) {
3215 readline_add_completion(rs, option);
3216 }
3217}
3218
Hani Benhabiles13e315d2014-05-07 23:41:29 +01003219void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
3220{
3221 size_t len;
3222 ChardevBackendInfoList *list, *start;
3223
3224 if (nb_args != 2) {
3225 return;
3226 }
3227 len = strlen(str);
3228 readline_set_completion_index(rs, len);
3229
3230 start = list = qmp_query_chardev_backends(NULL);
3231 while (list) {
3232 const char *chr_name = list->value->name;
3233
3234 if (!strncmp(chr_name, str, len)) {
3235 readline_add_completion(rs, chr_name);
3236 }
3237 list = list->next;
3238 }
3239 qapi_free_ChardevBackendInfoList(start);
3240}
3241
Hani Benhabilesb162b492014-05-07 23:41:31 +01003242void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
3243{
3244 size_t len;
3245 int i;
3246
3247 if (nb_args != 2) {
3248 return;
3249 }
3250 len = strlen(str);
3251 readline_set_completion_index(rs, len);
Eric Blakef394b2e2016-07-13 21:50:23 -06003252 for (i = 0; NetClientDriver_lookup[i]; i++) {
3253 add_completion_option(rs, str, NetClientDriver_lookup[i]);
Hani Benhabilesb162b492014-05-07 23:41:31 +01003254 }
3255}
3256
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003257void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003258{
3259 GSList *list, *elt;
3260 size_t len;
3261
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003262 if (nb_args != 2) {
3263 return;
3264 }
3265
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003266 len = strlen(str);
3267 readline_set_completion_index(rs, len);
3268 list = elt = object_class_get_list(TYPE_DEVICE, false);
3269 while (elt) {
3270 const char *name;
3271 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
3272 TYPE_DEVICE);
3273 name = object_class_get_name(OBJECT_CLASS(dc));
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003274
Eduardo Habkoste90f2a82017-05-03 17:35:44 -03003275 if (dc->user_creatable
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003276 && !strncmp(name, str, len)) {
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003277 readline_add_completion(rs, name);
3278 }
3279 elt = elt->next;
3280 }
3281 g_slist_free(list);
3282}
3283
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003284void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003285{
3286 GSList *list, *elt;
3287 size_t len;
3288
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003289 if (nb_args != 2) {
3290 return;
3291 }
3292
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003293 len = strlen(str);
3294 readline_set_completion_index(rs, len);
3295 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
3296 while (elt) {
3297 const char *name;
3298
3299 name = object_class_get_name(OBJECT_CLASS(elt->data));
3300 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
3301 readline_add_completion(rs, name);
3302 }
3303 elt = elt->next;
3304 }
3305 g_slist_free(list);
3306}
3307
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003308static void peripheral_device_del_completion(ReadLineState *rs,
3309 const char *str, size_t len)
3310{
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003311 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
3312 GSList *list, *item;
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003313
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003314 list = qdev_build_hotpluggable_device_list(peripheral);
3315 if (!list) {
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003316 return;
3317 }
3318
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003319 for (item = list; item; item = g_slist_next(item)) {
3320 DeviceState *dev = item->data;
3321
3322 if (dev->id && !strncmp(str, dev->id, len)) {
3323 readline_add_completion(rs, dev->id);
3324 }
3325 }
3326
3327 g_slist_free(list);
3328}
3329
Hani Benhabiles6297d9a2014-05-07 23:41:28 +01003330void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3331{
3332 size_t len;
3333 ChardevInfoList *list, *start;
3334
3335 if (nb_args != 2) {
3336 return;
3337 }
3338 len = strlen(str);
3339 readline_set_completion_index(rs, len);
3340
3341 start = list = qmp_query_chardev(NULL);
3342 while (list) {
3343 ChardevInfo *chr = list->value;
3344
3345 if (!strncmp(chr->label, str, len)) {
3346 readline_add_completion(rs, chr->label);
3347 }
3348 list = list->next;
3349 }
3350 qapi_free_ChardevInfoList(start);
3351}
3352
Hani Benhabiles8e597772014-05-27 23:39:30 +01003353static void ringbuf_completion(ReadLineState *rs, const char *str)
3354{
3355 size_t len;
3356 ChardevInfoList *list, *start;
3357
3358 len = strlen(str);
3359 readline_set_completion_index(rs, len);
3360
3361 start = list = qmp_query_chardev(NULL);
3362 while (list) {
3363 ChardevInfo *chr_info = list->value;
3364
3365 if (!strncmp(chr_info->label, str, len)) {
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03003366 Chardev *chr = qemu_chr_find(chr_info->label);
Marc-André Lureau777357d2016-12-07 18:39:10 +03003367 if (chr && CHARDEV_IS_RINGBUF(chr)) {
Hani Benhabiles8e597772014-05-27 23:39:30 +01003368 readline_add_completion(rs, chr_info->label);
3369 }
3370 }
3371 list = list->next;
3372 }
3373 qapi_free_ChardevInfoList(start);
3374}
3375
Hani Benhabiles8e597772014-05-27 23:39:30 +01003376void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
3377{
3378 if (nb_args != 2) {
3379 return;
3380 }
3381 ringbuf_completion(rs, str);
3382}
3383
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003384void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
3385{
3386 size_t len;
3387
3388 if (nb_args != 2) {
3389 return;
3390 }
3391
3392 len = strlen(str);
3393 readline_set_completion_index(rs, len);
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003394 peripheral_device_del_completion(rs, str, len);
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003395}
3396
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003397void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003398{
3399 ObjectPropertyInfoList *list, *start;
3400 size_t len;
3401
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003402 if (nb_args != 2) {
3403 return;
3404 }
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003405 len = strlen(str);
3406 readline_set_completion_index(rs, len);
3407
3408 start = list = qmp_qom_list("/objects", NULL);
3409 while (list) {
3410 ObjectPropertyInfo *info = list->value;
3411
3412 if (!strncmp(info->type, "child<", 5)
3413 && !strncmp(info->name, str, len)) {
3414 readline_add_completion(rs, info->name);
3415 }
3416 list = list->next;
3417 }
3418 qapi_free_ObjectPropertyInfoList(start);
3419}
3420
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003421void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
3422{
3423 int i;
3424 char *sep;
3425 size_t len;
3426
3427 if (nb_args != 2) {
3428 return;
3429 }
3430 sep = strrchr(str, '-');
3431 if (sep) {
3432 str = sep + 1;
3433 }
3434 len = strlen(str);
3435 readline_set_completion_index(rs, len);
Eric Blake7fb1cf12015-11-18 01:52:57 -07003436 for (i = 0; i < Q_KEY_CODE__MAX; i++) {
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003437 if (!strncmp(str, QKeyCode_lookup[i], len)) {
3438 readline_add_completion(rs, QKeyCode_lookup[i]);
3439 }
3440 }
3441}
3442
Hani Benhabiles40d19392014-05-07 23:41:30 +01003443void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
3444{
3445 size_t len;
3446
3447 len = strlen(str);
3448 readline_set_completion_index(rs, len);
3449 if (nb_args == 2) {
Jason Wangeaed4832015-04-23 14:21:38 +08003450 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles40d19392014-05-07 23:41:30 +01003451 int count, i;
3452 count = qemu_find_net_clients_except(NULL, ncs,
Eric Blakef394b2e2016-07-13 21:50:23 -06003453 NET_CLIENT_DRIVER_NONE,
Jason Wangeaed4832015-04-23 14:21:38 +08003454 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003455 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles40d19392014-05-07 23:41:30 +01003456 const char *name = ncs[i]->name;
3457 if (!strncmp(str, name, len)) {
3458 readline_add_completion(rs, name);
3459 }
3460 }
3461 } else if (nb_args == 3) {
3462 add_completion_option(rs, str, "on");
3463 add_completion_option(rs, str, "off");
3464 }
3465}
3466
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003467void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
3468{
3469 int len, count, i;
Jason Wangeaed4832015-04-23 14:21:38 +08003470 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003471
3472 if (nb_args != 2) {
3473 return;
3474 }
3475
3476 len = strlen(str);
3477 readline_set_completion_index(rs, len);
Eric Blakef394b2e2016-07-13 21:50:23 -06003478 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_DRIVER_NIC,
Jason Wangeaed4832015-04-23 14:21:38 +08003479 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003480 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003481 QemuOpts *opts;
3482 const char *name = ncs[i]->name;
3483 if (strncmp(str, name, len)) {
3484 continue;
3485 }
3486 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
3487 if (opts) {
3488 readline_add_completion(rs, name);
3489 }
3490 }
3491}
3492
Lluís Vilanovabd712112016-07-11 12:53:51 +02003493void info_trace_events_completion(ReadLineState *rs, int nb_args, const char *str)
3494{
3495 size_t len;
3496
3497 len = strlen(str);
3498 readline_set_completion_index(rs, len);
3499 if (nb_args == 2) {
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003500 TraceEventIter iter;
3501 TraceEvent *ev;
3502 char *pattern = g_strdup_printf("%s*", str);
3503 trace_event_iter_init(&iter, pattern);
3504 while ((ev = trace_event_iter_next(&iter)) != NULL) {
3505 readline_add_completion(rs, trace_event_get_name(ev));
Lluís Vilanovabd712112016-07-11 12:53:51 +02003506 }
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003507 g_free(pattern);
Lluís Vilanovabd712112016-07-11 12:53:51 +02003508 }
3509}
3510
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003511void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
3512{
3513 size_t len;
3514
3515 len = strlen(str);
3516 readline_set_completion_index(rs, len);
3517 if (nb_args == 2) {
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003518 TraceEventIter iter;
3519 TraceEvent *ev;
3520 char *pattern = g_strdup_printf("%s*", str);
3521 trace_event_iter_init(&iter, pattern);
3522 while ((ev = trace_event_iter_next(&iter)) != NULL) {
3523 readline_add_completion(rs, trace_event_get_name(ev));
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003524 }
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003525 g_free(pattern);
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003526 } else if (nb_args == 3) {
3527 add_completion_option(rs, str, "on");
3528 add_completion_option(rs, str, "off");
3529 }
3530}
3531
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003532void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
3533{
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003534 int i;
3535
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003536 if (nb_args != 2) {
3537 return;
3538 }
3539 readline_set_completion_index(rs, strlen(str));
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003540 for (i = 0; WatchdogExpirationAction_lookup[i]; i++) {
3541 add_completion_option(rs, str, WatchdogExpirationAction_lookup[i]);
3542 }
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003543}
3544
Hani Benhabilesc68a0402014-05-27 23:39:32 +01003545void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
3546 const char *str)
3547{
3548 size_t len;
3549
3550 len = strlen(str);
3551 readline_set_completion_index(rs, len);
3552 if (nb_args == 2) {
3553 int i;
Eric Blake7fb1cf12015-11-18 01:52:57 -07003554 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Hani Benhabilesc68a0402014-05-27 23:39:32 +01003555 const char *name = MigrationCapability_lookup[i];
3556 if (!strncmp(str, name, len)) {
3557 readline_add_completion(rs, name);
3558 }
3559 }
3560 } else if (nb_args == 3) {
3561 add_completion_option(rs, str, "on");
3562 add_completion_option(rs, str, "off");
3563 }
3564}
3565
Liang Li50e9a622015-03-23 16:32:29 +08003566void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
3567 const char *str)
3568{
3569 size_t len;
3570
3571 len = strlen(str);
3572 readline_set_completion_index(rs, len);
3573 if (nb_args == 2) {
3574 int i;
Eric Blake7fb1cf12015-11-18 01:52:57 -07003575 for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
Liang Li50e9a622015-03-23 16:32:29 +08003576 const char *name = MigrationParameter_lookup[i];
3577 if (!strncmp(str, name, len)) {
3578 readline_add_completion(rs, name);
3579 }
3580 }
3581 }
3582}
3583
Hani Benhabilese3bb5322014-05-27 23:39:34 +01003584void host_net_add_completion(ReadLineState *rs, int nb_args, const char *str)
3585{
3586 int i;
3587 size_t len;
3588 if (nb_args != 2) {
3589 return;
3590 }
3591 len = strlen(str);
3592 readline_set_completion_index(rs, len);
3593 for (i = 0; host_net_devices[i]; i++) {
3594 if (!strncmp(host_net_devices[i], str, len)) {
3595 readline_add_completion(rs, host_net_devices[i]);
3596 }
3597 }
3598}
3599
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003600void host_net_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3601{
Jason Wangeaed4832015-04-23 14:21:38 +08003602 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003603 int count, i, len;
3604
3605 len = strlen(str);
3606 readline_set_completion_index(rs, len);
3607 if (nb_args == 2) {
3608 count = qemu_find_net_clients_except(NULL, ncs,
Eric Blakef394b2e2016-07-13 21:50:23 -06003609 NET_CLIENT_DRIVER_NONE,
Jason Wangeaed4832015-04-23 14:21:38 +08003610 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003611 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003612 int id;
3613 char name[16];
3614
3615 if (net_hub_id_for_client(ncs[i], &id)) {
3616 continue;
3617 }
3618 snprintf(name, sizeof(name), "%d", id);
3619 if (!strncmp(str, name, len)) {
3620 readline_add_completion(rs, name);
3621 }
3622 }
3623 return;
3624 } else if (nb_args == 3) {
3625 count = qemu_find_net_clients_except(NULL, ncs,
Eric Blakef394b2e2016-07-13 21:50:23 -06003626 NET_CLIENT_DRIVER_NIC,
Jason Wangeaed4832015-04-23 14:21:38 +08003627 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003628 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Jason Wang2c4681f2015-02-02 15:06:38 +08003629 int id;
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003630 const char *name;
3631
Eric Blakef394b2e2016-07-13 21:50:23 -06003632 if (ncs[i]->info->type == NET_CLIENT_DRIVER_HUBPORT ||
Jason Wang2c4681f2015-02-02 15:06:38 +08003633 net_hub_id_for_client(ncs[i], &id)) {
3634 continue;
3635 }
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003636 name = ncs[i]->name;
3637 if (!strncmp(str, name, len)) {
3638 readline_add_completion(rs, name);
3639 }
3640 }
3641 return;
3642 }
3643}
3644
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003645static void vm_completion(ReadLineState *rs, const char *str)
3646{
3647 size_t len;
Kevin Wolf7c8eece2016-03-22 18:58:50 +01003648 BlockDriverState *bs;
Kevin Wolf88be7b42016-05-20 18:49:07 +02003649 BdrvNextIterator it;
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003650
3651 len = strlen(str);
3652 readline_set_completion_index(rs, len);
Kevin Wolf7c8eece2016-03-22 18:58:50 +01003653
Kevin Wolf88be7b42016-05-20 18:49:07 +02003654 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003655 SnapshotInfoList *snapshots, *snapshot;
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003656 AioContext *ctx = bdrv_get_aio_context(bs);
3657 bool ok = false;
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003658
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003659 aio_context_acquire(ctx);
3660 if (bdrv_can_snapshot(bs)) {
3661 ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0;
3662 }
3663 aio_context_release(ctx);
3664 if (!ok) {
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003665 continue;
3666 }
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003667
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003668 snapshot = snapshots;
3669 while (snapshot) {
3670 char *completion = snapshot->value->name;
3671 if (!strncmp(str, completion, len)) {
3672 readline_add_completion(rs, completion);
3673 }
3674 completion = snapshot->value->id;
3675 if (!strncmp(str, completion, len)) {
3676 readline_add_completion(rs, completion);
3677 }
3678 snapshot = snapshot->next;
3679 }
3680 qapi_free_SnapshotInfoList(snapshots);
3681 }
3682
3683}
3684
3685void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
3686{
3687 if (nb_args == 2) {
3688 vm_completion(rs, str);
3689 }
3690}
3691
3692void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
3693{
3694 if (nb_args == 2) {
3695 vm_completion(rs, str);
3696 }
3697}
3698
Wenchao Xiac35b6402013-08-27 20:38:24 +08003699static void monitor_find_completion_by_table(Monitor *mon,
3700 const mon_cmd_t *cmd_table,
3701 char **args,
3702 int nb_args)
bellard81d09122004-07-14 17:21:37 +00003703{
3704 const char *cmdname;
Wenchao Xiac35b6402013-08-27 20:38:24 +08003705 int i;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003706 const char *ptype, *str, *name;
Anthony Liguoric227f092009-10-01 16:12:16 -05003707 const mon_cmd_t *cmd;
Max Reitzda27a002016-03-16 19:54:29 +01003708 BlockBackend *blk = NULL;
bellard81d09122004-07-14 17:21:37 +00003709
bellard81d09122004-07-14 17:21:37 +00003710 if (nb_args <= 1) {
3711 /* command completion */
3712 if (nb_args == 0)
3713 cmdname = "";
3714 else
3715 cmdname = args[0];
Wenchao Xiad2674b22013-08-27 20:38:16 +08003716 readline_set_completion_index(mon->rs, strlen(cmdname));
Wenchao Xiac35b6402013-08-27 20:38:24 +08003717 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003718 cmd_completion(mon, cmdname, cmd->name);
bellard81d09122004-07-14 17:21:37 +00003719 }
3720 } else {
3721 /* find the command */
Wenchao Xiac35b6402013-08-27 20:38:24 +08003722 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Jan Kiszka03a63482010-06-16 00:38:33 +02003723 if (compare_cmd(args[0], cmd->name)) {
3724 break;
3725 }
bellard81d09122004-07-14 17:21:37 +00003726 }
Jan Kiszka03a63482010-06-16 00:38:33 +02003727 if (!cmd->name) {
Wenchao Xiac35b6402013-08-27 20:38:24 +08003728 return;
Jan Kiszka03a63482010-06-16 00:38:33 +02003729 }
3730
Wenchao Xiad903a772013-08-27 20:38:25 +08003731 if (cmd->sub_table) {
3732 /* do the job again */
Stefan Weile7ae7712015-03-08 19:30:01 +01003733 monitor_find_completion_by_table(mon, cmd->sub_table,
3734 &args[1], nb_args - 1);
3735 return;
Wenchao Xiad903a772013-08-27 20:38:25 +08003736 }
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003737 if (cmd->command_completion) {
Stefan Weile7ae7712015-03-08 19:30:01 +01003738 cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
3739 return;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003740 }
Wenchao Xiad903a772013-08-27 20:38:25 +08003741
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003742 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003743 for(i = 0; i < nb_args - 2; i++) {
3744 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003745 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003746 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003747 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003748 }
3749 }
3750 str = args[nb_args - 1];
Kevin Wolf48fe86f2014-11-12 16:24:02 +01003751 while (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02003752 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00003753 }
bellard81d09122004-07-14 17:21:37 +00003754 switch(*ptype) {
3755 case 'F':
3756 /* file completion */
Wenchao Xiad2674b22013-08-27 20:38:16 +08003757 readline_set_completion_index(mon->rs, strlen(str));
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003758 file_completion(mon, str);
bellard81d09122004-07-14 17:21:37 +00003759 break;
3760 case 'B':
3761 /* block device name completion */
Wenchao Xia599a9262013-08-27 20:38:15 +08003762 readline_set_completion_index(mon->rs, strlen(str));
Max Reitzda27a002016-03-16 19:54:29 +01003763 while ((blk = blk_next(blk)) != NULL) {
3764 name = blk_name(blk);
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003765 if (str[0] == '\0' ||
3766 !strncmp(name, str, strlen(str))) {
3767 readline_add_completion(mon->rs, name);
3768 }
3769 }
bellard81d09122004-07-14 17:21:37 +00003770 break;
bellard7fe48482004-10-09 18:08:01 +00003771 case 's':
Wenchao Xia129be002013-08-27 20:38:26 +08003772 case 'S':
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003773 if (!strcmp(cmd->name, "help|?")) {
Wenchao Xia7ca0e062013-08-27 20:38:27 +08003774 monitor_find_completion_by_table(mon, cmd_table,
3775 &args[1], nb_args - 1);
bellard7fe48482004-10-09 18:08:01 +00003776 }
3777 break;
bellard81d09122004-07-14 17:21:37 +00003778 default:
3779 break;
3780 }
3781 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08003782}
3783
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003784static void monitor_find_completion(void *opaque,
Wenchao Xiac35b6402013-08-27 20:38:24 +08003785 const char *cmdline)
3786{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003787 Monitor *mon = opaque;
Wenchao Xiac35b6402013-08-27 20:38:24 +08003788 char *args[MAX_ARGS];
3789 int nb_args, len;
3790
3791 /* 1. parse the cmdline */
3792 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
3793 return;
3794 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08003795
3796 /* if the line ends with a space, it means we want to complete the
3797 next arg */
3798 len = strlen(cmdline);
3799 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
3800 if (nb_args >= MAX_ARGS) {
3801 goto cleanup;
3802 }
3803 args[nb_args++] = g_strdup("");
3804 }
3805
3806 /* 2. auto complete according to args */
3807 monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args);
Jan Kiszka03a63482010-06-16 00:38:33 +02003808
3809cleanup:
Wenchao Xiadcc70cd2013-08-27 20:38:22 +08003810 free_cmdline_args(args, nb_args);
bellard81d09122004-07-14 17:21:37 +00003811}
3812
aliguori731b0362009-03-05 23:01:42 +00003813static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003814{
aliguori731b0362009-03-05 23:01:42 +00003815 Monitor *mon = opaque;
3816
Jan Kiszkac62313b2009-12-04 14:05:29 +01003817 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003818}
3819
Paolo Bonzini95385fe2015-11-25 22:23:31 +01003820static void handle_qmp_command(JSONMessageParser *parser, GQueue *tokens)
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003821{
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04003822 QObject *req, *rsp = NULL, *id = NULL;
3823 QDict *qdict = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003824 Monitor *mon = cur_mon;
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04003825 Error *err = NULL;
Stefan Hajnoczib097efc2017-06-05 11:42:16 +01003826 QString *req_json;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003827
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04003828 req = json_parser_parse_err(tokens, NULL, &err);
Markus Armbrusterbbf10282017-02-17 21:38:24 +01003829 if (!req && !err) {
3830 /* json_parser_parse_err() sucks: can fail without setting @err */
3831 error_setg(&err, QERR_JSON_PARSING);
3832 }
3833 if (err) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003834 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003835 }
3836
Markus Armbruster104fc302017-03-03 13:32:28 +01003837 qdict = qobject_to_qdict(req);
3838 if (qdict) {
3839 id = qdict_get(qdict, "id");
3840 qobject_incref(id);
3841 qdict_del(qdict, "id");
3842 } /* else will fail qmp_dispatch() */
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04003843
Stefan Hajnoczib097efc2017-06-05 11:42:16 +01003844 req_json = qobject_to_json(req);
3845 trace_handle_qmp_command(mon, qstring_get_str(req_json));
Eric Blake8a4613a2017-06-24 12:10:08 -06003846 QDECREF(req_json);
Stefan Hajnoczib097efc2017-06-05 11:42:16 +01003847
Markus Armbruster635db182017-03-03 13:32:27 +01003848 rsp = qmp_dispatch(cur_mon->qmp.commands, req);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003849
Markus Armbruster635db182017-03-03 13:32:27 +01003850 if (mon->qmp.commands == &qmp_cap_negotiation_commands) {
3851 qdict = qdict_get_qdict(qobject_to_qdict(rsp), "error");
3852 if (qdict
3853 && !g_strcmp0(qdict_get_try_str(qdict, "class"),
3854 QapiErrorClass_lookup[ERROR_CLASS_COMMAND_NOT_FOUND])) {
3855 /* Provide a more useful error message */
3856 qdict_del(qdict, "desc");
Eric Blake46f5ac22017-04-27 16:58:17 -05003857 qdict_put_str(qdict, "desc", "Expecting capabilities negotiation"
3858 " with 'qmp_capabilities'");
Markus Armbruster635db182017-03-03 13:32:27 +01003859 }
3860 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003861
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003862err_out:
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04003863 if (err) {
3864 qdict = qdict_new();
3865 qdict_put_obj(qdict, "error", qmp_build_error_object(err));
3866 error_free(err);
3867 rsp = QOBJECT(qdict);
3868 }
3869
3870 if (rsp) {
3871 if (id) {
3872 qdict_put_obj(qobject_to_qdict(rsp), "id", id);
3873 id = NULL;
3874 }
3875
3876 monitor_json_emitter(mon, rsp);
3877 }
3878
3879 qobject_decref(id);
3880 qobject_decref(rsp);
3881 qobject_decref(req);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003882}
3883
Markus Armbrusterc83fe232015-03-06 19:20:51 +01003884static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003885{
3886 Monitor *old_mon = cur_mon;
3887
3888 cur_mon = opaque;
3889
Markus Armbruster74358f22015-03-06 19:35:59 +01003890 json_message_parser_feed(&cur_mon->qmp.parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003891
3892 cur_mon = old_mon;
3893}
3894
aliguori731b0362009-03-05 23:01:42 +00003895static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00003896{
aliguori731b0362009-03-05 23:01:42 +00003897 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00003898 int i;
aliguori376253e2009-03-05 23:01:23 +00003899
aliguori731b0362009-03-05 23:01:42 +00003900 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00003901
aliguoricde76ee2009-03-05 23:01:51 +00003902 if (cur_mon->rs) {
3903 for (i = 0; i < size; i++)
3904 readline_handle_byte(cur_mon->rs, buf[i]);
3905 } else {
3906 if (size == 0 || buf[size - 1] != 0)
3907 monitor_printf(cur_mon, "corrupted command\n");
3908 else
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003909 handle_hmp_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00003910 }
aliguori731b0362009-03-05 23:01:42 +00003911
3912 cur_mon = old_mon;
3913}
aliguorid8f44602008-10-06 13:52:44 +00003914
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003915static void monitor_command_cb(void *opaque, const char *cmdline,
3916 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00003917{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003918 Monitor *mon = opaque;
3919
aliguori731b0362009-03-05 23:01:42 +00003920 monitor_suspend(mon);
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003921 handle_hmp_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00003922 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00003923}
3924
aliguoricde76ee2009-03-05 23:01:51 +00003925int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003926{
aliguoricde76ee2009-03-05 23:01:51 +00003927 if (!mon->rs)
3928 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00003929 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00003930 return 0;
aliguorid8f44602008-10-06 13:52:44 +00003931}
3932
aliguori376253e2009-03-05 23:01:23 +00003933void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003934{
aliguoricde76ee2009-03-05 23:01:51 +00003935 if (!mon->rs)
3936 return;
aliguori731b0362009-03-05 23:01:42 +00003937 if (--mon->suspend_cnt == 0)
3938 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00003939}
3940
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003941static QObject *get_qmp_greeting(void)
3942{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03003943 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003944
Markus Armbruster7fad30f2015-09-16 13:06:19 +02003945 qmp_marshal_query_version(NULL, &ver, NULL);
Marc-André Lureauc8235012016-09-12 13:19:04 +04003946
3947 return qobject_from_jsonf("{'QMP': {'version': %p, 'capabilities': []}}",
3948 ver);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003949}
3950
Markus Armbrusterc83fe232015-03-06 19:20:51 +01003951static void monitor_qmp_event(void *opaque, int event)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003952{
Luiz Capitulino47116d12010-02-08 17:01:30 -02003953 QObject *data;
3954 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003955
Luiz Capitulino47116d12010-02-08 17:01:30 -02003956 switch (event) {
3957 case CHR_EVENT_OPENED:
Markus Armbruster635db182017-03-03 13:32:27 +01003958 mon->qmp.commands = &qmp_cap_negotiation_commands;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003959 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003960 monitor_json_emitter(mon, data);
3961 qobject_decref(data);
Corey Bryantefb87c12012-08-14 16:43:48 -04003962 mon_refcount++;
Luiz Capitulino47116d12010-02-08 17:01:30 -02003963 break;
3964 case CHR_EVENT_CLOSED:
Markus Armbruster74358f22015-03-06 19:35:59 +01003965 json_message_parser_destroy(&mon->qmp.parser);
3966 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Corey Bryantefb87c12012-08-14 16:43:48 -04003967 mon_refcount--;
3968 monitor_fdsets_cleanup();
Luiz Capitulino47116d12010-02-08 17:01:30 -02003969 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003970 }
3971}
3972
aliguori731b0362009-03-05 23:01:42 +00003973static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00003974{
aliguori376253e2009-03-05 23:01:23 +00003975 Monitor *mon = opaque;
3976
aliguori2724b182009-03-05 23:01:47 +00003977 switch (event) {
3978 case CHR_EVENT_MUX_IN:
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003979 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003980 mon->mux_out = 0;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003981 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003982 if (mon->reset_seen) {
3983 readline_restart(mon->rs);
3984 monitor_resume(mon);
3985 monitor_flush(mon);
3986 } else {
3987 mon->suspend_cnt = 0;
3988 }
aliguori2724b182009-03-05 23:01:47 +00003989 break;
ths86e94de2007-01-05 22:01:59 +00003990
aliguori2724b182009-03-05 23:01:47 +00003991 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003992 if (mon->reset_seen) {
3993 if (mon->suspend_cnt == 0) {
3994 monitor_printf(mon, "\n");
3995 }
3996 monitor_flush(mon);
3997 monitor_suspend(mon);
3998 } else {
3999 mon->suspend_cnt++;
4000 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02004001 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004002 mon->mux_out = 1;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02004003 qemu_mutex_unlock(&mon->out_lock);
aliguori2724b182009-03-05 23:01:47 +00004004 break;
4005
Amit Shahb6b8df52009-10-07 18:31:16 +05304006 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004007 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4008 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004009 if (!mon->mux_out) {
Stratos Psomadakise5554e22014-09-15 15:34:57 +03004010 readline_restart(mon->rs);
aliguori2724b182009-03-05 23:01:47 +00004011 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004012 }
4013 mon->reset_seen = 1;
Corey Bryantefb87c12012-08-14 16:43:48 -04004014 mon_refcount++;
4015 break;
4016
4017 case CHR_EVENT_CLOSED:
4018 mon_refcount--;
4019 monitor_fdsets_cleanup();
aliguori2724b182009-03-05 23:01:47 +00004020 break;
4021 }
ths86e94de2007-01-05 22:01:59 +00004022}
4023
Wayne Xia816f8922011-10-12 11:32:41 +08004024static int
4025compare_mon_cmd(const void *a, const void *b)
4026{
4027 return strcmp(((const mon_cmd_t *)a)->name,
4028 ((const mon_cmd_t *)b)->name);
4029}
4030
4031static void sortcmdlist(void)
4032{
4033 int array_num;
4034 int elem_size = sizeof(mon_cmd_t);
4035
4036 array_num = sizeof(mon_cmds)/elem_size-1;
4037 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
4038
4039 array_num = sizeof(info_cmds)/elem_size-1;
4040 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
4041}
4042
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004043/* These functions just adapt the readline interface in a typesafe way. We
4044 * could cast function pointers but that discards compiler checks.
4045 */
Stefan Weild5d15072014-01-25 18:18:23 +01004046static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
4047 const char *fmt, ...)
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004048{
4049 va_list ap;
4050 va_start(ap, fmt);
4051 monitor_vprintf(opaque, fmt, ap);
4052 va_end(ap);
4053}
4054
4055static void monitor_readline_flush(void *opaque)
4056{
4057 monitor_flush(opaque);
4058}
4059
Paolo Bonzini397d30e2016-10-24 18:31:02 +02004060/*
4061 * Print to current monitor if we have one, else to stderr.
4062 * TODO should return int, so callers can calculate width, but that
4063 * requires surgery to monitor_vprintf(). Left for another day.
4064 */
4065void error_vprintf(const char *fmt, va_list ap)
4066{
4067 if (cur_mon && !monitor_cur_is_qmp()) {
4068 monitor_vprintf(cur_mon, fmt, ap);
4069 } else {
4070 vfprintf(stderr, fmt, ap);
4071 }
4072}
4073
4074void error_vprintf_unless_qmp(const char *fmt, va_list ap)
4075{
4076 if (cur_mon && !monitor_cur_is_qmp()) {
4077 monitor_vprintf(cur_mon, fmt, ap);
Marc-André Lureau0d6b50d2017-01-05 14:59:57 +01004078 } else if (!cur_mon) {
4079 vfprintf(stderr, fmt, ap);
Paolo Bonzini397d30e2016-10-24 18:31:02 +02004080 }
4081}
4082
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004083static void __attribute__((constructor)) monitor_lock_init(void)
4084{
4085 qemu_mutex_init(&monitor_lock);
4086}
4087
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03004088void monitor_init(Chardev *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004089{
aliguori731b0362009-03-05 23:01:42 +00004090 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004091 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004092
4093 if (is_first_init) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +02004094 monitor_qapi_event_init();
Wenchao Xiad0383172013-08-27 20:38:18 +08004095 sortcmdlist();
ths20d8a3e2007-02-18 17:04:49 +00004096 is_first_init = 0;
4097 }
aliguori87127162009-03-05 23:01:29 +00004098
Wenchao Xiab01fe892013-08-27 20:38:19 +08004099 mon = g_malloc(sizeof(*mon));
4100 monitor_data_init(mon);
ths20d8a3e2007-02-18 17:04:49 +00004101
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03004102 qemu_chr_fe_init(&mon->chr, chr, &error_abort);
aliguori731b0362009-03-05 23:01:42 +00004103 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004104 if (flags & MONITOR_USE_READLINE) {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004105 mon->rs = readline_init(monitor_readline_printf,
4106 monitor_readline_flush,
4107 mon,
4108 monitor_find_completion);
aliguoricde76ee2009-03-05 23:01:51 +00004109 monitor_read_command(mon, 0);
4110 }
aliguori87127162009-03-05 23:01:29 +00004111
Markus Armbruster9f3982f2015-03-06 19:56:38 +01004112 if (monitor_is_qmp(mon)) {
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03004113 qemu_chr_fe_set_handlers(&mon->chr, monitor_can_read, monitor_qmp_read,
Anton Nefedov81517ba2017-07-06 15:08:49 +03004114 monitor_qmp_event, NULL, mon, NULL, true);
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03004115 qemu_chr_fe_set_echo(&mon->chr, true);
Markus Armbruster74358f22015-03-06 19:35:59 +01004116 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004117 } else {
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03004118 qemu_chr_fe_set_handlers(&mon->chr, monitor_can_read, monitor_read,
Anton Nefedov81517ba2017-07-06 15:08:49 +03004119 monitor_event, NULL, mon, NULL, true);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004120 }
aliguori87127162009-03-05 23:01:29 +00004121
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004122 qemu_mutex_lock(&monitor_lock);
Blue Swirl72cf2d42009-09-12 07:36:22 +00004123 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004124 qemu_mutex_unlock(&monitor_lock);
bellard7e2515e2004-08-01 21:52:19 +00004125}
4126
Marc-André Lureau2ef45712016-08-01 15:23:42 +04004127void monitor_cleanup(void)
4128{
4129 Monitor *mon, *next;
4130
4131 qemu_mutex_lock(&monitor_lock);
4132 QLIST_FOREACH_SAFE(mon, &mon_list, entry, next) {
4133 QLIST_REMOVE(mon, entry);
4134 monitor_data_destroy(mon);
4135 g_free(mon);
4136 }
4137 qemu_mutex_unlock(&monitor_lock);
4138}
4139
Paolo Bonzini4d454572012-11-26 16:03:42 +01004140QemuOptsList qemu_mon_opts = {
4141 .name = "mon",
4142 .implied_opt_name = "chardev",
4143 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
4144 .desc = {
4145 {
4146 .name = "mode",
4147 .type = QEMU_OPT_STRING,
4148 },{
4149 .name = "chardev",
4150 .type = QEMU_OPT_STRING,
4151 },{
Marc-André Lureaubdbcb542016-10-11 21:41:21 +04004152 .name = "default", /* deprecated */
Paolo Bonzini4d454572012-11-26 16:03:42 +01004153 .type = QEMU_OPT_BOOL,
4154 },{
4155 .name = "pretty",
4156 .type = QEMU_OPT_BOOL,
4157 },
4158 { /* end of list */ }
4159 },
4160};
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004161
4162#ifndef TARGET_I386
4163void qmp_rtc_reset_reinjection(Error **errp)
4164{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004165 error_setg(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection");
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004166}
4167#endif
Jason J. Herne7ee0c3e2015-06-26 14:03:16 -04004168
4169#ifndef TARGET_S390X
4170void qmp_dump_skeys(const char *filename, Error **errp)
4171{
4172 error_setg(errp, QERR_FEATURE_DISABLED, "dump-skeys");
4173}
4174#endif
Peter Xuae50a772016-03-30 17:27:24 +01004175
4176#ifndef TARGET_ARM
4177GICCapabilityList *qmp_query_gic_capabilities(Error **errp)
4178{
4179 error_setg(errp, QERR_FEATURE_DISABLED, "query-gic-capabilities");
4180 return NULL;
4181}
4182#endif
Igor Mammedovd4633542016-06-10 06:29:06 +05304183
4184HotpluggableCPUList *qmp_query_hotpluggable_cpus(Error **errp)
4185{
4186 MachineState *ms = MACHINE(qdev_get_machine());
4187 MachineClass *mc = MACHINE_GET_CLASS(ms);
4188
Igor Mammedovc5514d02017-02-10 11:20:57 +01004189 if (!mc->has_hotpluggable_cpus) {
Igor Mammedovd4633542016-06-10 06:29:06 +05304190 error_setg(errp, QERR_FEATURE_DISABLED, "query-hotpluggable-cpus");
4191 return NULL;
4192 }
4193
Igor Mammedovc5514d02017-02-10 11:20:57 +01004194 return machine_query_hotpluggable_cpus(ms);
Igor Mammedovd4633542016-06-10 06:29:06 +05304195}