blob: 53f5f5a3785502d527b7a13ba6d006a65d67d419 [file] [log] [blame]
bellard9dc39cb2004-03-14 21:38:27 +00001/*
2 * QEMU monitor
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard9dc39cb2004-03-14 21:38:27 +00004 * Copyright (c) 2003-2004 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard9dc39cb2004-03-14 21:38:27 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
Peter Maydelld38ea872016-01-29 17:50:05 +000024#include "qemu/osdep.h"
blueswir1511d2b12009-03-07 15:32:56 +000025#include <dirent.h>
Paolo Bonzini33c11872016-03-15 16:58:45 +010026#include "qemu-common.h"
27#include "cpu.h"
pbrook87ecb682007-11-17 17:14:51 +000028#include "hw/hw.h"
Paolo Bonzinib4a42f82013-02-04 11:37:52 +010029#include "monitor/qdev.h"
pbrook87ecb682007-11-17 17:14:51 +000030#include "hw/usb.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010031#include "hw/i386/pc.h"
Michael S. Tsirkina2cb15b2012-12-12 14:24:50 +020032#include "hw/pci/pci.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010033#include "sysemu/watchdog.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +020034#include "hw/loader.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010035#include "exec/gdbstub.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020036#include "net/net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000037#include "net/slirp.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020038#include "sysemu/char.h"
Gerd Hoffmann75721502010-10-07 12:22:54 +020039#include "ui/qemu-spice.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010040#include "sysemu/sysemu.h"
Eduardo Habkoste35704b2015-02-08 16:51:16 -020041#include "sysemu/numa.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010042#include "monitor/monitor.h"
Marc-André Lureau213dcb02016-12-12 20:22:24 +030043#include "qemu/config-file.h"
Stefan Hajnoczi0150cd82013-11-14 11:54:15 +010044#include "qemu/readline.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010045#include "ui/console.h"
Gerd Hoffmannc751a742013-12-04 15:02:28 +010046#include "ui/input.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010047#include "sysemu/blockdev.h"
Max Reitzda27a002016-03-16 19:54:29 +010048#include "sysemu/block-backend.h"
pbrook87ecb682007-11-17 17:14:51 +000049#include "audio/audio.h"
Paolo Bonzini76cad712012-10-24 11:12:21 +020050#include "disas/disas.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010051#include "sysemu/balloon.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010052#include "qemu/timer.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010053#include "migration/migration.h"
Vincent Palatinb3946622017-01-10 11:59:55 +010054#include "sysemu/hw_accel.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010055#include "qemu/acl.h"
Paolo Bonzinibdee56f2013-04-02 18:28:41 +020056#include "sysemu/tpm.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010057#include "qapi/qmp/qerror.h"
Eric Blakec7eb39c2016-06-09 10:48:32 -060058#include "qapi/qmp/types.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010059#include "qapi/qmp/qjson.h"
60#include "qapi/qmp/json-streamer.h"
61#include "qapi/qmp/json-parser.h"
Markus Armbrustera9c94272016-06-22 19:11:19 +020062#include "qom/object_interfaces.h"
Daniel P. Berrange0ab8ed12017-01-25 16:14:15 +000063#include "trace-root.h"
Lluís31965ae2011-08-31 20:31:24 +020064#include "trace/control.h"
Pavel Butsykinbf957282015-09-10 18:38:59 +030065#include "monitor/hmp-target.h"
Lluís6d8a7642011-08-31 20:30:43 +020066#ifdef CONFIG_TRACE_SIMPLE
Lluís31965ae2011-08-31 20:31:24 +020067#include "trace/simple.h"
Prerna Saxena22890ab2010-06-24 17:04:53 +053068#endif
Paolo Bonzini022c62c2012-12-17 18:19:49 +010069#include "exec/memory.h"
Paolo Bonzini63c91552016-03-15 13:18:37 +010070#include "exec/exec-all.h"
Paolo Bonzini03dd0242015-12-15 13:16:16 +010071#include "qemu/log.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050072#include "qmp-commands.h"
73#include "hmp.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010074#include "qemu/thread.h"
Hani Benhabilesb21631f2014-05-27 23:39:37 +010075#include "block/qapi.h"
Wenchao Xia43a14cf2014-06-18 08:43:31 +020076#include "qapi/qmp-event.h"
77#include "qapi-event.h"
Markus Armbruster39a18152015-09-16 13:06:28 +020078#include "qmp-introspect.h"
Alberto Garciadc599972016-03-10 13:55:26 +020079#include "sysemu/qtest.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020080#include "qemu/cutils.h"
Marc-André Lureauedcfaef2016-09-12 13:19:00 +040081#include "qapi/qmp/dispatch.h"
ths6a5bd302007-12-03 17:05:38 +000082
Jason J. Hernea4538a52015-06-26 14:07:21 -040083#if defined(TARGET_S390X)
84#include "hw/s390x/storage-keys.h"
85#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 */
168 bool in_command_mode; /* are we in command mode? */
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 Armbruster8631b602010-02-18 11:41:55 +0100224Monitor *cur_mon;
aliguori376253e2009-03-05 23:01:23 +0000225
Alberto Garciadc599972016-03-10 13:55:26 +0200226static QEMUClockType event_clock_type = QEMU_CLOCK_REALTIME;
227
Stefan Hajnoczic60bf332013-11-14 11:54:14 +0100228static void monitor_command_cb(void *opaque, const char *cmdline,
229 void *readline_opaque);
aliguori83ab7952008-08-19 14:44:22 +0000230
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100231/**
232 * Is @mon a QMP monitor?
233 */
234static inline bool monitor_is_qmp(const Monitor *mon)
Luiz Capitulino418173c2009-11-26 22:58:51 -0200235{
236 return (mon->flags & MONITOR_USE_CONTROL);
237}
238
Markus Armbruster489653b2015-03-06 20:01:05 +0100239/**
240 * Is the current monitor, if any, a QMP monitor?
241 */
242bool monitor_cur_is_qmp(void)
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100243{
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100244 return cur_mon && monitor_is_qmp(cur_mon);
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100245}
246
Anthony Liguori7060b472011-09-02 12:34:50 -0500247void monitor_read_command(Monitor *mon, int show_prompt)
aliguori731b0362009-03-05 23:01:42 +0000248{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200249 if (!mon->rs)
250 return;
251
aliguori731b0362009-03-05 23:01:42 +0000252 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
253 if (show_prompt)
254 readline_show_prompt(mon->rs);
255}
bellard6a00d602005-11-21 23:25:50 +0000256
Anthony Liguori7060b472011-09-02 12:34:50 -0500257int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
258 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000259{
Markus Armbrusterbcf5d192015-03-12 17:26:46 +0100260 if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000261 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
262 /* prompt is printed on return from the command handler */
263 return 0;
264 } else {
265 monitor_printf(mon, "terminal does not support password prompting\n");
266 return -ENOTTY;
267 }
aliguoribb5fc202009-03-05 23:01:15 +0000268}
269
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200270static void monitor_flush_locked(Monitor *mon);
271
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100272static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
273 void *opaque)
274{
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200275 Monitor *mon = opaque;
276
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200277 qemu_mutex_lock(&mon->out_lock);
278 mon->out_watch = 0;
279 monitor_flush_locked(mon);
280 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100281 return FALSE;
282}
283
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200284/* Called with mon->out_lock held. */
285static void monitor_flush_locked(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000286{
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100287 int rc;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400288 size_t len;
289 const char *buf;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100290
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400291 if (mon->skip_flush) {
292 return;
293 }
294
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400295 buf = qstring_get_str(mon->outbuf);
296 len = qstring_get_length(mon->outbuf);
297
Paolo Bonzinia4cc73d2013-05-31 14:00:27 +0200298 if (len && !mon->mux_out) {
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300299 rc = qemu_chr_fe_write(&mon->chr, (const uint8_t *) buf, len);
Stratos Psomadakis056f49f2014-01-27 12:30:15 +0200300 if ((rc < 0 && errno != EAGAIN) || (rc == len)) {
301 /* all flushed or error */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400302 QDECREF(mon->outbuf);
303 mon->outbuf = qstring_new();
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100304 return;
305 }
306 if (rc > 0) {
Eric Blake3b7c78c2016-05-17 16:00:15 -0600307 /* partial write */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400308 QString *tmp = qstring_from_str(buf + rc);
309 QDECREF(mon->outbuf);
310 mon->outbuf = tmp;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100311 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200312 if (mon->out_watch == 0) {
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300313 mon->out_watch =
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300314 qemu_chr_fe_add_watch(&mon->chr, G_IO_OUT | G_IO_HUP,
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300315 monitor_unblocked, mon);
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200316 }
bellard7e2515e2004-08-01 21:52:19 +0000317 }
318}
319
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200320void monitor_flush(Monitor *mon)
321{
322 qemu_mutex_lock(&mon->out_lock);
323 monitor_flush_locked(mon);
324 qemu_mutex_unlock(&mon->out_lock);
325}
326
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400327/* flush at every end of line */
aliguori376253e2009-03-05 23:01:23 +0000328static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000329{
ths60fe76f2007-12-16 03:02:09 +0000330 char c;
aliguori731b0362009-03-05 23:01:42 +0000331
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200332 qemu_mutex_lock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000333 for(;;) {
334 c = *str++;
335 if (c == '\0')
336 break;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400337 if (c == '\n') {
338 qstring_append_chr(mon->outbuf, '\r');
339 }
340 qstring_append_chr(mon->outbuf, c);
341 if (c == '\n') {
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200342 monitor_flush_locked(mon);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400343 }
bellard7e2515e2004-08-01 21:52:19 +0000344 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200345 qemu_mutex_unlock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000346}
347
aliguori376253e2009-03-05 23:01:23 +0000348void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000349{
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400350 char *buf;
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200351
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200352 if (!mon)
353 return;
354
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100355 if (monitor_is_qmp(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200356 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200357 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200358
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400359 buf = g_strdup_vprintf(fmt, ap);
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200360 monitor_puts(mon, buf);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400361 g_free(buf);
bellard7e2515e2004-08-01 21:52:19 +0000362}
363
aliguori376253e2009-03-05 23:01:23 +0000364void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000365{
366 va_list ap;
367 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000368 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000369 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000370}
371
Pavel Butsykincaf15312015-09-22 16:18:17 +0300372int monitor_fprintf(FILE *stream, const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000373{
374 va_list ap;
375 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000376 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000377 va_end(ap);
378 return 0;
379}
380
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200381static void monitor_json_emitter(Monitor *mon, const QObject *data)
382{
383 QString *json;
384
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200385 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
386 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200387 assert(json != NULL);
388
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200389 qstring_append_chr(json, '\n');
390 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200391
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200392 QDECREF(json);
393}
394
Eric Blake7fb1cf12015-11-18 01:52:57 -0700395static MonitorQAPIEventConf monitor_qapi_event_conf[QAPI_EVENT__MAX] = {
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200396 /* Limit guest-triggerable events to 1 per second */
397 [QAPI_EVENT_RTC_CHANGE] = { 1000 * SCALE_MS },
398 [QAPI_EVENT_WATCHDOG] = { 1000 * SCALE_MS },
399 [QAPI_EVENT_BALLOON_CHANGE] = { 1000 * SCALE_MS },
400 [QAPI_EVENT_QUORUM_REPORT_BAD] = { 1000 * SCALE_MS },
401 [QAPI_EVENT_QUORUM_FAILURE] = { 1000 * SCALE_MS },
402 [QAPI_EVENT_VSERPORT_CHANGE] = { 1000 * SCALE_MS },
403};
404
Markus Armbrustera24712a2015-10-15 17:08:34 +0200405GHashTable *monitor_qapi_event_state;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100406
407/*
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200408 * Emits the event to every monitor instance, @event is only used for trace
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200409 * Called with monitor_lock held.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100410 */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200411static void monitor_qapi_event_emit(QAPIEvent event, QDict *qdict)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100412{
413 Monitor *mon;
414
Markus Armbruster688b4b72015-10-15 17:08:30 +0200415 trace_monitor_protocol_event_emit(event, qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100416 QLIST_FOREACH(mon, &mon_list, entry) {
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100417 if (monitor_is_qmp(mon) && mon->qmp.in_command_mode) {
Markus Armbruster688b4b72015-10-15 17:08:30 +0200418 monitor_json_emitter(mon, QOBJECT(qdict));
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100419 }
420 }
421}
422
Markus Armbrustera24712a2015-10-15 17:08:34 +0200423static void monitor_qapi_event_handler(void *opaque);
424
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100425/*
426 * Queue a new event for emission to Monitor instances,
427 * applying any rate limiting if required.
428 */
429static void
Markus Armbruster688b4b72015-10-15 17:08:30 +0200430monitor_qapi_event_queue(QAPIEvent event, QDict *qdict, Error **errp)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100431{
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200432 MonitorQAPIEventConf *evconf;
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200433 MonitorQAPIEventState *evstate;
Markus Armbruster93f8f982015-10-15 17:08:31 +0200434
Eric Blake7fb1cf12015-11-18 01:52:57 -0700435 assert(event < QAPI_EVENT__MAX);
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200436 evconf = &monitor_qapi_event_conf[event];
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200437 trace_monitor_protocol_event_queue(event, qdict, evconf->rate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100438
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200439 qemu_mutex_lock(&monitor_lock);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200440
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200441 if (!evconf->rate) {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200442 /* Unthrottled event */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200443 monitor_qapi_event_emit(event, qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100444 } else {
Markus Armbruster7de0be62015-10-15 17:08:35 +0200445 QDict *data = qobject_to_qdict(qdict_get(qdict, "data"));
446 MonitorQAPIEventState key = { .event = event, .data = data };
Markus Armbrustera24712a2015-10-15 17:08:34 +0200447
448 evstate = g_hash_table_lookup(monitor_qapi_event_state, &key);
449 assert(!evstate || timer_pending(evstate->timer));
450
451 if (evstate) {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200452 /*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200453 * Timer is pending for (at least) evconf->rate ns after
Markus Armbruster93f8f982015-10-15 17:08:31 +0200454 * last send. Store event for sending when timer fires,
455 * replacing a prior stored event if any.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100456 */
Markus Armbruster93f8f982015-10-15 17:08:31 +0200457 QDECREF(evstate->qdict);
Markus Armbruster688b4b72015-10-15 17:08:30 +0200458 evstate->qdict = qdict;
459 QINCREF(evstate->qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100460 } else {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200461 /*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200462 * Last send was (at least) evconf->rate ns ago.
Markus Armbruster93f8f982015-10-15 17:08:31 +0200463 * Send immediately, and arm the timer to call
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200464 * monitor_qapi_event_handler() in evconf->rate ns. Any
Markus Armbruster93f8f982015-10-15 17:08:31 +0200465 * events arriving before then will be delayed until then.
466 */
Alberto Garciadc599972016-03-10 13:55:26 +0200467 int64_t now = qemu_clock_get_ns(event_clock_type);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200468
Markus Armbruster688b4b72015-10-15 17:08:30 +0200469 monitor_qapi_event_emit(event, qdict);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200470
471 evstate = g_new(MonitorQAPIEventState, 1);
472 evstate->event = event;
Markus Armbruster7de0be62015-10-15 17:08:35 +0200473 evstate->data = data;
474 QINCREF(evstate->data);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200475 evstate->qdict = NULL;
Alberto Garciadc599972016-03-10 13:55:26 +0200476 evstate->timer = timer_new_ns(event_clock_type,
Markus Armbrustera24712a2015-10-15 17:08:34 +0200477 monitor_qapi_event_handler,
478 evstate);
479 g_hash_table_add(monitor_qapi_event_state, evstate);
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200480 timer_mod_ns(evstate->timer, now + evconf->rate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100481 }
482 }
Markus Armbruster93f8f982015-10-15 17:08:31 +0200483
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200484 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100485}
486
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100487/*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200488 * This function runs evconf->rate ns after sending a throttled
Markus Armbruster93f8f982015-10-15 17:08:31 +0200489 * event.
490 * If another event has since been stored, send it.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100491 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200492static void monitor_qapi_event_handler(void *opaque)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100493{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200494 MonitorQAPIEventState *evstate = opaque;
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200495 MonitorQAPIEventConf *evconf = &monitor_qapi_event_conf[evstate->event];
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100496
Markus Armbruster93f8f982015-10-15 17:08:31 +0200497 trace_monitor_protocol_event_handler(evstate->event, evstate->qdict);
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200498 qemu_mutex_lock(&monitor_lock);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200499
Markus Armbruster688b4b72015-10-15 17:08:30 +0200500 if (evstate->qdict) {
Alberto Garciadc599972016-03-10 13:55:26 +0200501 int64_t now = qemu_clock_get_ns(event_clock_type);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200502
Markus Armbruster688b4b72015-10-15 17:08:30 +0200503 monitor_qapi_event_emit(evstate->event, evstate->qdict);
504 QDECREF(evstate->qdict);
505 evstate->qdict = NULL;
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200506 timer_mod_ns(evstate->timer, now + evconf->rate);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200507 } else {
508 g_hash_table_remove(monitor_qapi_event_state, evstate);
Markus Armbruster7de0be62015-10-15 17:08:35 +0200509 QDECREF(evstate->data);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200510 timer_free(evstate->timer);
511 g_free(evstate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100512 }
Markus Armbruster93f8f982015-10-15 17:08:31 +0200513
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200514 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100515}
516
Markus Armbrustera24712a2015-10-15 17:08:34 +0200517static unsigned int qapi_event_throttle_hash(const void *key)
518{
519 const MonitorQAPIEventState *evstate = key;
Markus Armbruster7de0be62015-10-15 17:08:35 +0200520 unsigned int hash = evstate->event * 255;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200521
Markus Armbruster7de0be62015-10-15 17:08:35 +0200522 if (evstate->event == QAPI_EVENT_VSERPORT_CHANGE) {
523 hash += g_str_hash(qdict_get_str(evstate->data, "id"));
524 }
525
Alberto Garcia6d425eb2016-03-10 13:55:25 +0200526 if (evstate->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
527 hash += g_str_hash(qdict_get_str(evstate->data, "node-name"));
528 }
529
Markus Armbruster7de0be62015-10-15 17:08:35 +0200530 return hash;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200531}
532
533static gboolean qapi_event_throttle_equal(const void *a, const void *b)
534{
535 const MonitorQAPIEventState *eva = a;
536 const MonitorQAPIEventState *evb = b;
537
Markus Armbruster7de0be62015-10-15 17:08:35 +0200538 if (eva->event != evb->event) {
539 return FALSE;
540 }
541
542 if (eva->event == QAPI_EVENT_VSERPORT_CHANGE) {
543 return !strcmp(qdict_get_str(eva->data, "id"),
544 qdict_get_str(evb->data, "id"));
545 }
546
Alberto Garcia6d425eb2016-03-10 13:55:25 +0200547 if (eva->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
548 return !strcmp(qdict_get_str(eva->data, "node-name"),
549 qdict_get_str(evb->data, "node-name"));
550 }
551
Markus Armbruster7de0be62015-10-15 17:08:35 +0200552 return TRUE;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200553}
554
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200555static void monitor_qapi_event_init(void)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100556{
Alberto Garciadc599972016-03-10 13:55:26 +0200557 if (qtest_enabled()) {
558 event_clock_type = QEMU_CLOCK_VIRTUAL;
559 }
560
Markus Armbrustera24712a2015-10-15 17:08:34 +0200561 monitor_qapi_event_state = g_hash_table_new(qapi_event_throttle_hash,
562 qapi_event_throttle_equal);
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200563 qmp_event_set_func_emit(monitor_qapi_event_queue);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200564}
565
Marc-André Lureau119ebac2016-09-12 13:18:57 +0400566void qmp_qmp_capabilities(Error **errp)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200567{
Markus Armbruster485febc2015-03-13 17:25:50 +0100568 cur_mon->qmp.in_command_mode = true;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200569}
570
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100571static void handle_hmp_command(Monitor *mon, const char *cmdline);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200572
Wenchao Xiab01fe892013-08-27 20:38:19 +0800573static void monitor_data_init(Monitor *mon)
574{
575 memset(mon, 0, sizeof(Monitor));
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200576 qemu_mutex_init(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800577 mon->outbuf = qstring_new();
Wenchao Xia77172392013-08-27 20:38:20 +0800578 /* Use *mon_cmds by default. */
579 mon->cmd_table = mon_cmds;
Wenchao Xiab01fe892013-08-27 20:38:19 +0800580}
581
582static void monitor_data_destroy(Monitor *mon)
583{
Marc-André Lureauc39860e2016-10-22 12:52:58 +0300584 qemu_chr_fe_deinit(&mon->chr);
Marc-André Lureau2ef45712016-08-01 15:23:42 +0400585 if (monitor_is_qmp(mon)) {
586 json_message_parser_destroy(&mon->qmp.parser);
587 }
588 g_free(mon->rs);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800589 QDECREF(mon->outbuf);
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200590 qemu_mutex_destroy(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800591}
592
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200593char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
594 int64_t cpu_index, Error **errp)
Luiz Capitulino0268d972010-10-22 10:08:02 -0200595{
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200596 char *output = NULL;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200597 Monitor *old_mon, hmp;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200598
Wenchao Xiab01fe892013-08-27 20:38:19 +0800599 monitor_data_init(&hmp);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400600 hmp.skip_flush = true;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200601
602 old_mon = cur_mon;
603 cur_mon = &hmp;
604
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200605 if (has_cpu_index) {
606 int ret = monitor_set_cpu(cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200607 if (ret < 0) {
608 cur_mon = old_mon;
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100609 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
610 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200611 goto out;
612 }
613 }
614
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100615 handle_hmp_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200616 cur_mon = old_mon;
617
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200618 qemu_mutex_lock(&hmp.out_lock);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400619 if (qstring_get_length(hmp.outbuf) > 0) {
620 output = g_strdup(qstring_get_str(hmp.outbuf));
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200621 } else {
622 output = g_strdup("");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200623 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200624 qemu_mutex_unlock(&hmp.out_lock);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200625
626out:
Wenchao Xiab01fe892013-08-27 20:38:19 +0800627 monitor_data_destroy(&hmp);
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200628 return output;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200629}
630
bellard9dc39cb2004-03-14 21:38:27 +0000631static int compare_cmd(const char *name, const char *list)
632{
633 const char *p, *pstart;
634 int len;
635 len = strlen(name);
636 p = list;
637 for(;;) {
638 pstart = p;
639 p = strchr(p, '|');
640 if (!p)
641 p = pstart + strlen(pstart);
642 if ((p - pstart) == len && !memcmp(pstart, name, len))
643 return 1;
644 if (*p == '\0')
645 break;
646 p++;
647 }
648 return 0;
649}
650
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800651static int get_str(char *buf, int buf_size, const char **pp)
652{
653 const char *p;
654 char *q;
655 int c;
656
657 q = buf;
658 p = *pp;
659 while (qemu_isspace(*p)) {
660 p++;
661 }
662 if (*p == '\0') {
663 fail:
664 *q = '\0';
665 *pp = p;
666 return -1;
667 }
668 if (*p == '\"') {
669 p++;
670 while (*p != '\0' && *p != '\"') {
671 if (*p == '\\') {
672 p++;
673 c = *p++;
674 switch (c) {
675 case 'n':
676 c = '\n';
677 break;
678 case 'r':
679 c = '\r';
680 break;
681 case '\\':
682 case '\'':
683 case '\"':
684 break;
685 default:
Peter Maydell71baf782015-08-19 16:20:19 +0100686 printf("unsupported escape code: '\\%c'\n", c);
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800687 goto fail;
688 }
689 if ((q - buf) < buf_size - 1) {
690 *q++ = c;
691 }
692 } else {
693 if ((q - buf) < buf_size - 1) {
694 *q++ = *p;
695 }
696 p++;
697 }
698 }
699 if (*p != '\"') {
Peter Maydell71baf782015-08-19 16:20:19 +0100700 printf("unterminated string\n");
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800701 goto fail;
702 }
703 p++;
704 } else {
705 while (*p != '\0' && !qemu_isspace(*p)) {
706 if ((q - buf) < buf_size - 1) {
707 *q++ = *p;
708 }
709 p++;
710 }
711 }
712 *q = '\0';
713 *pp = p;
714 return 0;
715}
716
717#define MAX_ARGS 16
718
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800719static void free_cmdline_args(char **args, int nb_args)
720{
721 int i;
722
723 assert(nb_args <= MAX_ARGS);
724
725 for (i = 0; i < nb_args; i++) {
726 g_free(args[i]);
727 }
728
729}
730
731/*
732 * Parse the command line to get valid args.
733 * @cmdline: command line to be parsed.
734 * @pnb_args: location to store the number of args, must NOT be NULL.
735 * @args: location to store the args, which should be freed by caller, must
736 * NOT be NULL.
737 *
738 * Returns 0 on success, negative on failure.
739 *
740 * NOTE: this parser is an approximate form of the real command parser. Number
741 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
742 * return with failure.
743 */
744static int parse_cmdline(const char *cmdline,
745 int *pnb_args, char **args)
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800746{
747 const char *p;
748 int nb_args, ret;
749 char buf[1024];
750
751 p = cmdline;
752 nb_args = 0;
753 for (;;) {
754 while (qemu_isspace(*p)) {
755 p++;
756 }
757 if (*p == '\0') {
758 break;
759 }
760 if (nb_args >= MAX_ARGS) {
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800761 goto fail;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800762 }
763 ret = get_str(buf, sizeof(buf), &p);
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800764 if (ret < 0) {
765 goto fail;
766 }
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800767 args[nb_args] = g_strdup(buf);
768 nb_args++;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800769 }
770 *pnb_args = nb_args;
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800771 return 0;
772
773 fail:
774 free_cmdline_args(args, nb_args);
775 return -1;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800776}
777
Wenchao Xia66855492013-08-27 20:38:23 +0800778static void help_cmd_dump_one(Monitor *mon,
779 const mon_cmd_t *cmd,
780 char **prefix_args,
781 int prefix_args_nb)
782{
783 int i;
784
785 for (i = 0; i < prefix_args_nb; i++) {
786 monitor_printf(mon, "%s ", prefix_args[i]);
787 }
788 monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
789}
790
791/* @args[@arg_index] is the valid command need to find in @cmds */
Anthony Liguoric227f092009-10-01 16:12:16 -0500792static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
Wenchao Xia66855492013-08-27 20:38:23 +0800793 char **args, int nb_args, int arg_index)
bellard9dc39cb2004-03-14 21:38:27 +0000794{
Anthony Liguoric227f092009-10-01 16:12:16 -0500795 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000796
Wenchao Xia66855492013-08-27 20:38:23 +0800797 /* No valid arg need to compare with, dump all in *cmds */
798 if (arg_index >= nb_args) {
799 for (cmd = cmds; cmd->name != NULL; cmd++) {
800 help_cmd_dump_one(mon, cmd, args, arg_index);
801 }
802 return;
803 }
804
805 /* Find one entry to dump */
806 for (cmd = cmds; cmd->name != NULL; cmd++) {
807 if (compare_cmd(args[arg_index], cmd->name)) {
808 if (cmd->sub_table) {
809 /* continue with next arg */
810 help_cmd_dump(mon, cmd->sub_table,
811 args, nb_args, arg_index + 1);
812 } else {
813 help_cmd_dump_one(mon, cmd, args, arg_index);
814 }
815 break;
816 }
bellard9dc39cb2004-03-14 21:38:27 +0000817 }
818}
819
aliguori376253e2009-03-05 23:01:23 +0000820static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000821{
Wenchao Xia66855492013-08-27 20:38:23 +0800822 char *args[MAX_ARGS];
823 int nb_args = 0;
824
825 /* 1. parse user input */
826 if (name) {
827 /* special case for log, directly dump and return */
828 if (!strcmp(name, "log")) {
Peter Maydell38dad9e2013-02-11 16:41:25 +0000829 const QEMULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000830 monitor_printf(mon, "Log items (comma separated):\n");
831 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
Peter Maydell38dad9e2013-02-11 16:41:25 +0000832 for (item = qemu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000833 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000834 }
Wenchao Xia66855492013-08-27 20:38:23 +0800835 return;
836 }
837
838 if (parse_cmdline(name, &nb_args, args) < 0) {
839 return;
bellardf193c792004-03-21 17:06:25 +0000840 }
bellard9dc39cb2004-03-14 21:38:27 +0000841 }
Wenchao Xia66855492013-08-27 20:38:23 +0800842
843 /* 2. dump the contents according to parsed args */
844 help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0);
845
846 free_cmdline_args(args, nb_args);
bellard9dc39cb2004-03-14 21:38:27 +0000847}
848
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300849static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300850{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300851 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300852}
853
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100854static void hmp_trace_event(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530855{
856 const char *tp_name = qdict_get_str(qdict, "name");
857 bool new_state = qdict_get_bool(qdict, "option");
Lluís Vilanova77e2b172016-07-11 12:53:57 +0200858 bool has_vcpu = qdict_haskey(qdict, "vcpu");
859 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
Lluís Vilanova14101d02014-08-25 13:20:03 +0200860 Error *local_err = NULL;
Blue Swirlf871d682010-10-13 19:14:29 +0000861
Lluís Vilanova77e2b172016-07-11 12:53:57 +0200862 if (vcpu < 0) {
863 monitor_printf(mon, "argument vcpu must be positive");
864 return;
865 }
866
867 qmp_trace_event_set_state(tp_name, new_state, true, true, has_vcpu, vcpu, &local_err);
Lluís Vilanova14101d02014-08-25 13:20:03 +0200868 if (local_err) {
Markus Armbruster091e38b2015-02-10 15:15:43 +0100869 error_report_err(local_err);
Blue Swirlf871d682010-10-13 19:14:29 +0000870 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530871}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100872
Michael Rothc45a8162011-10-02 08:44:37 -0500873#ifdef CONFIG_TRACE_SIMPLE
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100874static void hmp_trace_file(Monitor *mon, const QDict *qdict)
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100875{
876 const char *op = qdict_get_try_str(qdict, "op");
877 const char *arg = qdict_get_try_str(qdict, "arg");
878
879 if (!op) {
880 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
881 } else if (!strcmp(op, "on")) {
882 st_set_trace_file_enabled(true);
883 } else if (!strcmp(op, "off")) {
884 st_set_trace_file_enabled(false);
885 } else if (!strcmp(op, "flush")) {
886 st_flush_trace_buffer();
887 } else if (!strcmp(op, "set")) {
888 if (arg) {
889 st_set_trace_file(arg);
890 }
891 } else {
892 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
893 help_cmd(mon, "trace-file");
894 }
895}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530896#endif
897
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100898static void hmp_info_help(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000899{
Luiz Capitulino13c74252009-10-07 13:41:55 -0300900 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000901}
902
Marc-André Lureau9e812b62016-09-12 13:19:05 +0400903static void query_commands_cb(QmpCommand *cmd, void *opaque)
bellard9bc9d1c2004-10-10 15:15:51 +0000904{
Marc-André Lureau9e812b62016-09-12 13:19:05 +0400905 CommandInfoList *info, **list = opaque;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200906
Marc-André Lureau9e812b62016-09-12 13:19:05 +0400907 if (!cmd->enabled) {
908 return;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200909 }
910
Marc-André Lureau9e812b62016-09-12 13:19:05 +0400911 info = g_malloc0(sizeof(*info));
912 info->value = g_malloc0(sizeof(*info->value));
913 info->value->name = g_strdup(cmd->name);
914 info->next = *list;
915 *list = info;
916}
917
918CommandInfoList *qmp_query_commands(Error **errp)
919{
920 CommandInfoList *list = NULL;
921
922 qmp_for_each_command(query_commands_cb, &list);
923
924 return list;
thsa36e69d2007-12-02 05:18:19 +0000925}
926
Daniel P. Berrange48608532012-05-21 17:59:51 +0100927EventInfoList *qmp_query_events(Error **errp)
928{
929 EventInfoList *info, *ev_list = NULL;
Wenchao Xia75175172014-06-18 08:43:54 +0200930 QAPIEvent e;
Daniel P. Berrange48608532012-05-21 17:59:51 +0100931
Eric Blake7fb1cf12015-11-18 01:52:57 -0700932 for (e = 0 ; e < QAPI_EVENT__MAX ; e++) {
Wenchao Xia75175172014-06-18 08:43:54 +0200933 const char *event_name = QAPIEvent_lookup[e];
Daniel P. Berrange48608532012-05-21 17:59:51 +0100934 assert(event_name != NULL);
935 info = g_malloc0(sizeof(*info));
936 info->value = g_malloc0(sizeof(*info->value));
937 info->value->name = g_strdup(event_name);
938
939 info->next = ev_list;
940 ev_list = info;
941 }
942
943 return ev_list;
944}
945
Markus Armbruster39a18152015-09-16 13:06:28 +0200946/*
947 * Minor hack: generated marshalling suppressed for this command
948 * ('gen': false in the schema) so we can parse the JSON string
949 * directly into QObject instead of first parsing it with
950 * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
951 * to QObject with generated output marshallers, every time. Instead,
Daniel P. Berrangeb3db2112016-09-30 15:45:27 +0100952 * we do it in test-qobject-input-visitor.c, just to make sure
Markus Armbruster39a18152015-09-16 13:06:28 +0200953 * qapi-introspect.py's output actually conforms to the schema.
954 */
955static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
956 Error **errp)
957{
958 *ret_data = qobject_from_json(qmp_schema_json);
959}
960
Marc-André Lureau5032a162016-09-12 13:19:02 +0400961/*
Marc-André Lureau5032a162016-09-12 13:19:02 +0400962 * We used to define commands in qmp-commands.hx in addition to the
963 * QAPI schema. This permitted defining some of them only in certain
964 * configurations. query-commands has always reflected that (good,
965 * because it lets QMP clients figure out what's actually available),
966 * while query-qmp-schema never did (not so good). This function is a
967 * hack to keep the configuration-specific commands defined exactly as
968 * before, even though qmp-commands.hx is gone.
969 *
970 * FIXME Educate the QAPI schema on configuration-specific commands,
971 * and drop this hack.
972 */
973static void qmp_unregister_commands_hack(void)
974{
975#ifndef CONFIG_SPICE
976 qmp_unregister_command("query-spice");
977#endif
978#ifndef TARGET_I386
979 qmp_unregister_command("rtc-reset-reinjection");
980#endif
981#ifndef TARGET_S390X
982 qmp_unregister_command("dump-skeys");
983#endif
984#ifndef TARGET_ARM
985 qmp_unregister_command("query-gic-capabilities");
986#endif
Eduardo Habkostf99fd7c2017-02-22 16:00:28 -0300987#if !defined(TARGET_S390X) && !defined(TARGET_I386)
Eduardo Habkost728b1422016-10-05 16:49:01 -0300988 qmp_unregister_command("query-cpu-model-expansion");
Eduardo Habkostf99fd7c2017-02-22 16:00:28 -0300989#endif
990#if !defined(TARGET_S390X)
Eduardo Habkost728b1422016-10-05 16:49:01 -0300991 qmp_unregister_command("query-cpu-model-baseline");
992 qmp_unregister_command("query-cpu-model-comparison");
993#endif
994#if !defined(TARGET_PPC) && !defined(TARGET_ARM) && !defined(TARGET_I386) \
995 && !defined(TARGET_S390X)
996 qmp_unregister_command("query-cpu-definitions");
997#endif
Marc-André Lureau5032a162016-09-12 13:19:02 +0400998}
999
Markus Armbruster05875682017-03-03 13:32:24 +01001000void monitor_init_qmp_commands(void)
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001001{
Markus Armbruster05875682017-03-03 13:32:24 +01001002 qmp_init_marshal();
1003
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001004 qmp_register_command("query-qmp-schema", qmp_query_qmp_schema,
1005 QCO_NO_OPTIONS);
1006 qmp_register_command("device_add", qmp_device_add,
1007 QCO_NO_OPTIONS);
1008 qmp_register_command("netdev_add", qmp_netdev_add,
1009 QCO_NO_OPTIONS);
Marc-André Lureau5032a162016-09-12 13:19:02 +04001010
Markus Armbruster05875682017-03-03 13:32:24 +01001011 qmp_unregister_commands_hack();
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001012}
1013
Luiz Capitulinob025c8b2011-10-06 14:02:57 -03001014/* set the current CPU defined by the user */
1015int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +00001016{
Andreas Färber55e5c282012-12-17 06:18:02 +01001017 CPUState *cpu;
bellard6a00d602005-11-21 23:25:50 +00001018
Andreas Färber1c8bb3c2013-02-15 17:01:09 +01001019 cpu = qemu_get_cpu(cpu_index);
1020 if (cpu == NULL) {
1021 return -1;
bellard6a00d602005-11-21 23:25:50 +00001022 }
Andreas Färbercb446ec2013-05-01 14:24:52 +02001023 cur_mon->mon_cpu = cpu;
Andreas Färber1c8bb3c2013-02-15 17:01:09 +01001024 return 0;
bellard6a00d602005-11-21 23:25:50 +00001025}
1026
Pavel Butsykincaf15312015-09-22 16:18:17 +03001027CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +00001028{
aliguori731b0362009-03-05 23:01:42 +00001029 if (!cur_mon->mon_cpu) {
Thomas Huth854e67f2017-01-13 13:12:35 +01001030 if (!first_cpu) {
1031 return NULL;
1032 }
David Gibson27a83f82016-09-21 15:29:26 +10001033 monitor_set_cpu(first_cpu->cpu_index);
bellard6a00d602005-11-21 23:25:50 +00001034 }
Avi Kivity4c0960c2009-08-17 23:19:53 +03001035 cpu_synchronize_state(cur_mon->mon_cpu);
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001036 return cur_mon->mon_cpu;
1037}
1038
Pavel Butsykinbf957282015-09-10 18:38:59 +03001039CPUArchState *mon_get_cpu_env(void)
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001040{
Thomas Huth854e67f2017-01-13 13:12:35 +01001041 CPUState *cs = mon_get_cpu();
1042
1043 return cs ? cs->env_ptr : NULL;
bellard6a00d602005-11-21 23:25:50 +00001044}
1045
Luiz Capitulino99b77962011-10-24 10:53:44 -02001046int monitor_get_cpu_index(void)
1047{
Thomas Huth854e67f2017-01-13 13:12:35 +01001048 CPUState *cs = mon_get_cpu();
1049
1050 return cs ? cs->cpu_index : UNASSIGNED_CPU_INDEX;
Luiz Capitulino99b77962011-10-24 10:53:44 -02001051}
1052
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001053static void hmp_info_registers(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001054{
Thomas Huth854e67f2017-01-13 13:12:35 +01001055 CPUState *cs = mon_get_cpu();
1056
1057 if (!cs) {
1058 monitor_printf(mon, "No CPU available\n");
1059 return;
1060 }
1061 cpu_dump_state(cs, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +00001062}
1063
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001064static void hmp_info_jit(Monitor *mon, const QDict *qdict)
bellarde3db7222005-01-26 22:00:47 +00001065{
aliguori376253e2009-03-05 23:01:23 +00001066 dump_exec_info((FILE *)mon, monitor_fprintf);
Sebastian Tanase27498be2014-07-25 11:56:33 +02001067 dump_drift_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +00001068}
1069
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001070static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
Max Filippov246ae242014-11-02 11:04:18 +03001071{
1072 dump_opcount_info((FILE *)mon, monitor_fprintf);
1073}
1074
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001075static void hmp_info_history(Monitor *mon, const QDict *qdict)
bellardaa455482004-04-04 13:07:25 +00001076{
1077 int i;
bellard7e2515e2004-08-01 21:52:19 +00001078 const char *str;
ths3b46e622007-09-17 08:09:54 +00001079
aliguoricde76ee2009-03-05 23:01:51 +00001080 if (!mon->rs)
1081 return;
bellard7e2515e2004-08-01 21:52:19 +00001082 i = 0;
1083 for(;;) {
aliguori731b0362009-03-05 23:01:42 +00001084 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +00001085 if (!str)
1086 break;
aliguori376253e2009-03-05 23:01:23 +00001087 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +00001088 i++;
bellardaa455482004-04-04 13:07:25 +00001089 }
1090}
1091
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001092static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
j_mayer76a66252007-03-07 08:32:30 +00001093{
Thomas Huth854e67f2017-01-13 13:12:35 +01001094 CPUState *cs = mon_get_cpu();
1095
1096 if (!cs) {
1097 monitor_printf(mon, "No CPU available\n");
1098 return;
1099 }
1100 cpu_dump_statistics(cs, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +00001101}
j_mayer76a66252007-03-07 08:32:30 +00001102
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001103static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301104{
Lluís Vilanovabd712112016-07-11 12:53:51 +02001105 const char *name = qdict_get_try_str(qdict, "name");
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001106 bool has_vcpu = qdict_haskey(qdict, "vcpu");
1107 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
Lluís Vilanovabd712112016-07-11 12:53:51 +02001108 TraceEventInfoList *events;
Lluís Vilanova14101d02014-08-25 13:20:03 +02001109 TraceEventInfoList *elem;
Lluís Vilanovabd712112016-07-11 12:53:51 +02001110 Error *local_err = NULL;
1111
1112 if (name == NULL) {
1113 name = "*";
1114 }
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001115 if (vcpu < 0) {
1116 monitor_printf(mon, "argument vcpu must be positive");
1117 return;
1118 }
Lluís Vilanovabd712112016-07-11 12:53:51 +02001119
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001120 events = qmp_trace_event_get_state(name, has_vcpu, vcpu, &local_err);
Lluís Vilanovabd712112016-07-11 12:53:51 +02001121 if (local_err) {
1122 error_report_err(local_err);
1123 return;
1124 }
Lluís Vilanova14101d02014-08-25 13:20:03 +02001125
1126 for (elem = events; elem != NULL; elem = elem->next) {
1127 monitor_printf(mon, "%s : state %u\n",
1128 elem->value->name,
1129 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
1130 }
1131 qapi_free_TraceEventInfoList(events);
Prerna Saxena22890ab2010-06-24 17:04:53 +05301132}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301133
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001134void qmp_client_migrate_info(const char *protocol, const char *hostname,
1135 bool has_port, int64_t port,
1136 bool has_tls_port, int64_t tls_port,
1137 bool has_cert_subject, const char *cert_subject,
1138 Error **errp)
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001139{
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001140 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001141 if (!qemu_using_spice(errp)) {
1142 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001143 }
1144
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001145 if (!has_port && !has_tls_port) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001146 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001147 return;
Yonit Halperin6ec5dae2012-03-18 09:42:39 +02001148 }
1149
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001150 if (qemu_spice_migrate_info(hostname,
1151 has_port ? port : -1,
1152 has_tls_port ? tls_port : -1,
1153 cert_subject)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001154 error_setg(errp, QERR_UNDEFINED_ERROR);
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001155 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001156 }
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001157 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001158 }
1159
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001160 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001161}
1162
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001163static void hmp_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001164{
Markus Armbrusterdaa76aa2016-06-15 19:27:16 +02001165 Error *err = NULL;
1166
1167 qemu_set_log_filename(qdict_get_str(qdict, "filename"), &err);
1168 if (err) {
1169 error_report_err(err);
1170 }
pbrooke735b912007-06-30 13:53:24 +00001171}
1172
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001173static void hmp_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001174{
1175 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001176 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001177
bellard9307c4c2004-04-04 12:57:25 +00001178 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001179 mask = 0;
1180 } else {
Peter Maydell4fde1eb2013-02-11 16:41:22 +00001181 mask = qemu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001182 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001183 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001184 return;
1185 }
1186 }
Peter Maydell24537a02013-02-11 16:41:23 +00001187 qemu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00001188}
1189
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001190static void hmp_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001191{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001192 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001193 if (!option || !strcmp(option, "on")) {
1194 singlestep = 1;
1195 } else if (!strcmp(option, "off")) {
1196 singlestep = 0;
1197 } else {
1198 monitor_printf(mon, "unexpected option %s\n", option);
1199 }
1200}
1201
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001202static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001203{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001204 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001205 if (!device)
1206 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1207 if (gdbserver_start(device) < 0) {
1208 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1209 device);
1210 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001211 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001212 } else {
aliguori59030a82009-04-05 18:43:41 +00001213 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1214 device);
bellard8a7ddc32004-03-31 19:00:16 +00001215 }
1216}
1217
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001218static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001219{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001220 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001221 if (select_watchdog_action(action) == -1) {
1222 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1223 }
1224}
1225
aliguori376253e2009-03-05 23:01:23 +00001226static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001227{
aliguori376253e2009-03-05 23:01:23 +00001228 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001229 switch(c) {
1230 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001231 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001232 break;
1233 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001234 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001235 break;
1236 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001237 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001238 break;
1239 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001240 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001241 break;
1242 default:
1243 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001244 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001245 } else {
aliguori376253e2009-03-05 23:01:23 +00001246 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001247 }
1248 break;
1249 }
aliguori376253e2009-03-05 23:01:23 +00001250 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001251}
1252
aliguori376253e2009-03-05 23:01:23 +00001253static void memory_dump(Monitor *mon, int count, int format, int wsize,
Avi Kivitya8170e52012-10-23 12:30:10 +02001254 hwaddr addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001255{
Blue Swirl23842aa2010-01-12 20:27:43 +00001256 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001257 uint8_t buf[16];
1258 uint64_t v;
Thomas Huth854e67f2017-01-13 13:12:35 +01001259 CPUState *cs = mon_get_cpu();
1260
1261 if (!cs && (format == 'i' || !is_physical)) {
1262 monitor_printf(mon, "Can not dump without CPU\n");
1263 return;
1264 }
bellard9307c4c2004-04-04 12:57:25 +00001265
1266 if (format == 'i') {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001267 int flags = 0;
bellard9307c4c2004-04-04 12:57:25 +00001268#ifdef TARGET_I386
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001269 CPUArchState *env = mon_get_cpu_env();
bellard4c27ba22004-04-25 18:05:08 +00001270 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001271 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001272 } else if (wsize == 4) {
1273 flags = 0;
1274 } else {
bellard6a15fd12006-04-12 21:07:07 +00001275 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001276 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001277 if (env) {
1278#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001279 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001280 (env->segs[R_CS].flags & DESC_L_MASK))
1281 flags = 2;
1282 else
1283#endif
1284 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1285 flags = 1;
1286 }
bellard4c27ba22004-04-25 18:05:08 +00001287 }
1288#endif
Tom Musta1c38f842014-04-09 14:53:24 -05001289#ifdef TARGET_PPC
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001290 CPUArchState *env = mon_get_cpu_env();
Tom Musta1c38f842014-04-09 14:53:24 -05001291 flags = msr_le << 16;
1292 flags |= env->bfd_mach;
1293#endif
Thomas Huth854e67f2017-01-13 13:12:35 +01001294 monitor_disas(mon, cs, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001295 return;
1296 }
1297
1298 len = wsize * count;
1299 if (wsize == 1)
1300 line_size = 8;
1301 else
1302 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001303 max_digits = 0;
1304
1305 switch(format) {
1306 case 'o':
1307 max_digits = (wsize * 8 + 2) / 3;
1308 break;
1309 default:
1310 case 'x':
1311 max_digits = (wsize * 8) / 4;
1312 break;
1313 case 'u':
1314 case 'd':
1315 max_digits = (wsize * 8 * 10 + 32) / 33;
1316 break;
1317 case 'c':
1318 wsize = 1;
1319 break;
1320 }
1321
1322 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001323 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001324 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001325 else
aliguori376253e2009-03-05 23:01:23 +00001326 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001327 l = len;
1328 if (l > line_size)
1329 l = line_size;
1330 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001331 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001332 } else {
Thomas Huth854e67f2017-01-13 13:12:35 +01001333 if (cpu_memory_rw_debug(cs, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001334 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001335 break;
1336 }
bellard9307c4c2004-04-04 12:57:25 +00001337 }
ths5fafdf22007-09-16 21:08:06 +00001338 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001339 while (i < l) {
1340 switch(wsize) {
1341 default:
1342 case 1:
Peter Maydell24e60302015-01-20 15:19:32 +00001343 v = ldub_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001344 break;
1345 case 2:
Peter Maydell24e60302015-01-20 15:19:32 +00001346 v = lduw_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001347 break;
1348 case 4:
Peter Maydell24e60302015-01-20 15:19:32 +00001349 v = (uint32_t)ldl_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001350 break;
1351 case 8:
Peter Maydell24e60302015-01-20 15:19:32 +00001352 v = ldq_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001353 break;
1354 }
aliguori376253e2009-03-05 23:01:23 +00001355 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001356 switch(format) {
1357 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001358 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001359 break;
1360 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001361 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001362 break;
1363 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001364 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001365 break;
1366 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001367 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001368 break;
1369 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001370 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001371 break;
1372 }
1373 i += wsize;
1374 }
aliguori376253e2009-03-05 23:01:23 +00001375 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001376 addr += l;
1377 len -= l;
1378 }
1379}
1380
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001381static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001382{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001383 int count = qdict_get_int(qdict, "count");
1384 int format = qdict_get_int(qdict, "format");
1385 int size = qdict_get_int(qdict, "size");
1386 target_long addr = qdict_get_int(qdict, "addr");
1387
aliguori376253e2009-03-05 23:01:23 +00001388 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001389}
1390
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001391static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001392{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001393 int count = qdict_get_int(qdict, "count");
1394 int format = qdict_get_int(qdict, "format");
1395 int size = qdict_get_int(qdict, "size");
Avi Kivitya8170e52012-10-23 12:30:10 +02001396 hwaddr addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001397
aliguori376253e2009-03-05 23:01:23 +00001398 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001399}
1400
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001401static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001402{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001403 int format = qdict_get_int(qdict, "format");
Avi Kivitya8170e52012-10-23 12:30:10 +02001404 hwaddr val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001405
bellard9307c4c2004-04-04 12:57:25 +00001406 switch(format) {
1407 case 'o':
Avi Kivitya8170e52012-10-23 12:30:10 +02001408 monitor_printf(mon, "%#" HWADDR_PRIo, val);
bellard9307c4c2004-04-04 12:57:25 +00001409 break;
1410 case 'x':
Avi Kivitya8170e52012-10-23 12:30:10 +02001411 monitor_printf(mon, "%#" HWADDR_PRIx, val);
bellard9307c4c2004-04-04 12:57:25 +00001412 break;
1413 case 'u':
Avi Kivitya8170e52012-10-23 12:30:10 +02001414 monitor_printf(mon, "%" HWADDR_PRIu, val);
bellard9307c4c2004-04-04 12:57:25 +00001415 break;
1416 default:
1417 case 'd':
Avi Kivitya8170e52012-10-23 12:30:10 +02001418 monitor_printf(mon, "%" HWADDR_PRId, val);
bellard9307c4c2004-04-04 12:57:25 +00001419 break;
1420 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001421 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001422 break;
1423 }
aliguori376253e2009-03-05 23:01:23 +00001424 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001425}
1426
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001427static void hmp_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001428{
1429 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001430 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001431 uint32_t start = qdict_get_int(qdict, "start");
1432 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001433
1434 sum = 0;
1435 for(addr = start; addr < (start + size); addr++) {
Peter Maydell42874d32015-04-26 16:49:24 +01001436 uint8_t val = address_space_ldub(&address_space_memory, addr,
1437 MEMTXATTRS_UNSPECIFIED, NULL);
bellarde4cf1ad2005-06-04 20:15:57 +00001438 /* BSD sum algorithm ('sum' Unix command) */
1439 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001440 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001441 }
aliguori376253e2009-03-05 23:01:23 +00001442 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001443}
1444
bellard13224a82006-07-14 22:03:35 +00001445static int mouse_button_state;
1446
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001447static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001448{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001449 int dx, dy, dz, button;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001450 const char *dx_str = qdict_get_str(qdict, "dx_str");
1451 const char *dy_str = qdict_get_str(qdict, "dy_str");
1452 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001453
bellard13224a82006-07-14 22:03:35 +00001454 dx = strtol(dx_str, NULL, 0);
1455 dy = strtol(dy_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001456 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
1457 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
1458
1459 if (dz_str) {
bellard13224a82006-07-14 22:03:35 +00001460 dz = strtol(dz_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001461 if (dz != 0) {
Gerd Hoffmannf22d0af2016-01-12 12:14:12 +01001462 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001463 qemu_input_queue_btn(NULL, button, true);
1464 qemu_input_event_sync();
1465 qemu_input_queue_btn(NULL, button, false);
1466 }
1467 }
1468 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001469}
1470
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001471static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001472{
Eric Blake7fb1cf12015-11-18 01:52:57 -07001473 static uint32_t bmap[INPUT_BUTTON__MAX] = {
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001474 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1475 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
1476 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1477 };
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001478 int button_state = qdict_get_int(qdict, "button_state");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001479
1480 if (mouse_button_state == button_state) {
1481 return;
1482 }
1483 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1484 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001485 mouse_button_state = button_state;
bellard13224a82006-07-14 22:03:35 +00001486}
1487
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001488static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001489{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001490 int size = qdict_get_int(qdict, "size");
1491 int addr = qdict_get_int(qdict, "addr");
1492 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001493 uint32_t val;
1494 int suffix;
1495
1496 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001497 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001498 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001499 addr++;
1500 }
1501 addr &= 0xffff;
1502
1503 switch(size) {
1504 default:
1505 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001506 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001507 suffix = 'b';
1508 break;
1509 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001510 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001511 suffix = 'w';
1512 break;
1513 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001514 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001515 suffix = 'l';
1516 break;
1517 }
aliguori376253e2009-03-05 23:01:23 +00001518 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1519 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001520}
bellarda3a91a32004-06-04 11:06:21 +00001521
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001522static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001523{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001524 int size = qdict_get_int(qdict, "size");
1525 int addr = qdict_get_int(qdict, "addr");
1526 int val = qdict_get_int(qdict, "val");
1527
Jan Kiszkaf1147842009-07-14 10:20:11 +02001528 addr &= IOPORTS_MASK;
1529
1530 switch (size) {
1531 default:
1532 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001533 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001534 break;
1535 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001536 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001537 break;
1538 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001539 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001540 break;
1541 }
1542}
1543
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001544static void hmp_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001545{
Gongleif1839932014-12-03 18:20:58 +00001546 Error *local_err = NULL;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001547 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001548
Gongleif1839932014-12-03 18:20:58 +00001549 qemu_boot_set(bootdevice, &local_err);
1550 if (local_err) {
Markus Armbruster193227f2015-12-18 16:35:06 +01001551 error_report_err(local_err);
aurel320ecdffb2008-05-04 20:11:34 +00001552 } else {
Gongleif1839932014-12-03 18:20:58 +00001553 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001554 }
1555}
1556
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001557static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
Blue Swirl314e2982011-09-11 20:22:05 +00001558{
Peter Xu57bb40c2017-01-16 16:40:05 +08001559 bool flatview = qdict_get_try_bool(qdict, "flatview", false);
1560
1561 mtree_info((fprintf_function)monitor_printf, mon, flatview);
Blue Swirl314e2982011-09-11 20:22:05 +00001562}
1563
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001564static void hmp_info_numa(Monitor *mon, const QDict *qdict)
aliguori030ea372009-04-21 22:30:47 +00001565{
aliguorib28b6232009-04-22 20:20:29 +00001566 int i;
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001567 CPUState *cpu;
zhanghailiang5b009e42014-11-04 19:49:30 +08001568 uint64_t *node_mem;
aliguori030ea372009-04-21 22:30:47 +00001569
zhanghailiang5b009e42014-11-04 19:49:30 +08001570 node_mem = g_new0(uint64_t, nb_numa_nodes);
1571 query_numa_node_mem(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001572 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1573 for (i = 0; i < nb_numa_nodes; i++) {
1574 monitor_printf(mon, "node %d cpus:", i);
Andreas Färberbdc44642013-06-24 23:50:24 +02001575 CPU_FOREACH(cpu) {
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001576 if (cpu->numa_node == i) {
Andreas Färber55e5c282012-12-17 06:18:02 +01001577 monitor_printf(mon, " %d", cpu->cpu_index);
aliguori030ea372009-04-21 22:30:47 +00001578 }
1579 }
1580 monitor_printf(mon, "\n");
1581 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
zhanghailiang5b009e42014-11-04 19:49:30 +08001582 node_mem[i] >> 20);
aliguori030ea372009-04-21 22:30:47 +00001583 }
zhanghailiang5b009e42014-11-04 19:49:30 +08001584 g_free(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001585}
1586
bellard5f1ce942006-02-08 22:40:15 +00001587#ifdef CONFIG_PROFILER
1588
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001589int64_t tcg_time;
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001590int64_t dev_time;
1591
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001592static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001593{
aliguori376253e2009-03-05 23:01:23 +00001594 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Rutuja Shah73bcb242016-03-21 21:32:30 +05301595 dev_time, dev_time / (double)NANOSECONDS_PER_SECOND);
aliguori376253e2009-03-05 23:01:23 +00001596 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Rutuja Shah73bcb242016-03-21 21:32:30 +05301597 tcg_time, tcg_time / (double)NANOSECONDS_PER_SECOND);
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001598 tcg_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001599 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001600}
1601#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001602static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001603{
aliguori376253e2009-03-05 23:01:23 +00001604 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001605}
1606#endif
1607
bellardec36b692006-07-16 18:57:03 +00001608/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001609static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001610
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001611static void hmp_info_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001612{
1613 int i;
1614 CaptureState *s;
1615
1616 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001617 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001618 s->ops.info (s->opaque);
1619 }
1620}
1621
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001622static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001623{
1624 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001625 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001626 CaptureState *s;
1627
1628 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1629 if (i == n) {
1630 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001631 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001632 g_free (s);
bellardec36b692006-07-16 18:57:03 +00001633 return;
1634 }
1635 }
1636}
1637
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001638static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001639{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001640 const char *path = qdict_get_str(qdict, "path");
1641 int has_freq = qdict_haskey(qdict, "freq");
1642 int freq = qdict_get_try_int(qdict, "freq", -1);
1643 int has_bits = qdict_haskey(qdict, "bits");
1644 int bits = qdict_get_try_int(qdict, "bits", -1);
1645 int has_channels = qdict_haskey(qdict, "nchannels");
1646 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001647 CaptureState *s;
1648
Anthony Liguori7267c092011-08-20 22:09:37 -05001649 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001650
1651 freq = has_freq ? freq : 44100;
1652 bits = has_bits ? bits : 16;
1653 nchannels = has_channels ? nchannels : 2;
1654
1655 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001656 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05001657 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001658 return;
bellardec36b692006-07-16 18:57:03 +00001659 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001660 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001661}
bellardec36b692006-07-16 18:57:03 +00001662
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001663static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001664{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001665 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001666
aliguori76655d62009-03-06 20:27:37 +00001667 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001668 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001669 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001670 return acl;
1671}
aliguori76655d62009-03-06 20:27:37 +00001672
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001673static void hmp_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001674{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001675 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001676 qemu_acl *acl = find_acl(mon, aclname);
1677 qemu_acl_entry *entry;
1678 int i = 0;
1679
1680 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001681 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001682 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00001683 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00001684 i++;
1685 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001686 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001687 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001688 }
1689}
1690
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001691static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001692{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001693 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001694 qemu_acl *acl = find_acl(mon, aclname);
1695
1696 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001697 qemu_acl_reset(acl);
1698 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001699 }
1700}
aliguori76655d62009-03-06 20:27:37 +00001701
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001702static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001703{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001704 const char *aclname = qdict_get_str(qdict, "aclname");
1705 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001706 qemu_acl *acl = find_acl(mon, aclname);
1707
1708 if (acl) {
1709 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001710 acl->defaultDeny = 0;
1711 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001712 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001713 acl->defaultDeny = 1;
1714 monitor_printf(mon, "acl: policy set to 'deny'\n");
1715 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001716 monitor_printf(mon, "acl: unknown policy '%s', "
1717 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001718 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001719 }
1720}
aliguori76655d62009-03-06 20:27:37 +00001721
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001722static void hmp_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001723{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001724 const char *aclname = qdict_get_str(qdict, "aclname");
1725 const char *match = qdict_get_str(qdict, "match");
1726 const char *policy = qdict_get_str(qdict, "policy");
1727 int has_index = qdict_haskey(qdict, "index");
1728 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001729 qemu_acl *acl = find_acl(mon, aclname);
1730 int deny, ret;
1731
1732 if (acl) {
1733 if (strcmp(policy, "allow") == 0) {
1734 deny = 0;
1735 } else if (strcmp(policy, "deny") == 0) {
1736 deny = 1;
1737 } else {
1738 monitor_printf(mon, "acl: unknown policy '%s', "
1739 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001740 return;
1741 }
aliguori28a76be2009-03-06 20:27:40 +00001742 if (has_index)
1743 ret = qemu_acl_insert(acl, deny, match, index);
1744 else
1745 ret = qemu_acl_append(acl, deny, match);
1746 if (ret < 0)
1747 monitor_printf(mon, "acl: unable to add acl entry\n");
1748 else
1749 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001750 }
1751}
aliguori76655d62009-03-06 20:27:37 +00001752
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001753static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001754{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001755 const char *aclname = qdict_get_str(qdict, "aclname");
1756 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001757 qemu_acl *acl = find_acl(mon, aclname);
1758 int ret;
aliguori76655d62009-03-06 20:27:37 +00001759
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001760 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001761 ret = qemu_acl_remove(acl, match);
1762 if (ret < 0)
1763 monitor_printf(mon, "acl: no matching acl entry\n");
1764 else
1765 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001766 }
1767}
1768
Corey Bryant208c9d12012-06-22 14:36:09 -04001769void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001770{
Anthony Liguoric227f092009-10-01 16:12:16 -05001771 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001772 int fd;
1773
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03001774 fd = qemu_chr_fe_get_msgfd(&cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001775 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001776 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryant208c9d12012-06-22 14:36:09 -04001777 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001778 }
1779
1780 if (qemu_isdigit(fdname[0])) {
Stefan Hajnoczi0b9f0e22014-04-24 13:58:18 +02001781 close(fd);
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001782 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
1783 "a name not starting with a digit");
Corey Bryant208c9d12012-06-22 14:36:09 -04001784 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001785 }
1786
Corey Bryant208c9d12012-06-22 14:36:09 -04001787 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001788 if (strcmp(monfd->name, fdname) != 0) {
1789 continue;
1790 }
1791
1792 close(monfd->fd);
1793 monfd->fd = fd;
Corey Bryant208c9d12012-06-22 14:36:09 -04001794 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001795 }
1796
Anthony Liguori7267c092011-08-20 22:09:37 -05001797 monfd = g_malloc0(sizeof(mon_fd_t));
1798 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001799 monfd->fd = fd;
1800
Corey Bryant208c9d12012-06-22 14:36:09 -04001801 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001802}
1803
Corey Bryant208c9d12012-06-22 14:36:09 -04001804void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001805{
Anthony Liguoric227f092009-10-01 16:12:16 -05001806 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001807
Corey Bryant208c9d12012-06-22 14:36:09 -04001808 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001809 if (strcmp(monfd->name, fdname) != 0) {
1810 continue;
1811 }
1812
Blue Swirl72cf2d42009-09-12 07:36:22 +00001813 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001814 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001815 g_free(monfd->name);
1816 g_free(monfd);
Corey Bryant208c9d12012-06-22 14:36:09 -04001817 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001818 }
1819
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001820 error_setg(errp, QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001821}
1822
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001823static void hmp_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001824{
Luiz Capitulino13548692011-07-29 15:36:43 -03001825 int saved_vm_running = runstate_is_running();
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001826 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02001827
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001828 vm_stop(RUN_STATE_RESTORE_VM);
Juan Quintelac8d41b22009-08-20 19:42:21 +02001829
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03001830 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02001831 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03001832 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02001833}
1834
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001835int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
Mark McLoughlin7768e042009-07-22 09:11:41 +01001836{
Anthony Liguoric227f092009-10-01 16:12:16 -05001837 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01001838
Blue Swirl72cf2d42009-09-12 07:36:22 +00001839 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01001840 int fd;
1841
1842 if (strcmp(monfd->name, fdname) != 0) {
1843 continue;
1844 }
1845
1846 fd = monfd->fd;
1847
1848 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001849 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05001850 g_free(monfd->name);
1851 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001852
1853 return fd;
1854 }
1855
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001856 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001857 return -1;
1858}
1859
Corey Bryantba1c0482012-08-14 16:43:43 -04001860static void monitor_fdset_cleanup(MonFdset *mon_fdset)
1861{
1862 MonFdsetFd *mon_fdset_fd;
1863 MonFdsetFd *mon_fdset_fd_next;
1864
1865 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantebe52b52012-10-18 15:19:33 -04001866 if ((mon_fdset_fd->removed ||
1867 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
1868 runstate_is_running()) {
Corey Bryantba1c0482012-08-14 16:43:43 -04001869 close(mon_fdset_fd->fd);
1870 g_free(mon_fdset_fd->opaque);
1871 QLIST_REMOVE(mon_fdset_fd, next);
1872 g_free(mon_fdset_fd);
1873 }
1874 }
1875
Corey Bryantadb696f2012-08-14 16:43:47 -04001876 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04001877 QLIST_REMOVE(mon_fdset, next);
1878 g_free(mon_fdset);
1879 }
1880}
1881
Corey Bryantefb87c12012-08-14 16:43:48 -04001882static void monitor_fdsets_cleanup(void)
1883{
1884 MonFdset *mon_fdset;
1885 MonFdset *mon_fdset_next;
1886
1887 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
1888 monitor_fdset_cleanup(mon_fdset);
1889 }
1890}
1891
Corey Bryantba1c0482012-08-14 16:43:43 -04001892AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
1893 const char *opaque, Error **errp)
1894{
1895 int fd;
1896 Monitor *mon = cur_mon;
Corey Bryantba1c0482012-08-14 16:43:43 -04001897 AddfdInfo *fdinfo;
1898
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03001899 fd = qemu_chr_fe_get_msgfd(&mon->chr);
Corey Bryantba1c0482012-08-14 16:43:43 -04001900 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001901 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryantba1c0482012-08-14 16:43:43 -04001902 goto error;
1903 }
1904
Corey Bryante446f702012-10-18 15:19:32 -04001905 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
1906 has_opaque, opaque, errp);
1907 if (fdinfo) {
1908 return fdinfo;
Corey Bryant9ac54af2012-10-18 15:19:31 -04001909 }
1910
Corey Bryantba1c0482012-08-14 16:43:43 -04001911error:
1912 if (fd != -1) {
1913 close(fd);
1914 }
1915 return NULL;
1916}
1917
1918void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
1919{
1920 MonFdset *mon_fdset;
1921 MonFdsetFd *mon_fdset_fd;
1922 char fd_str[60];
1923
1924 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1925 if (mon_fdset->id != fdset_id) {
1926 continue;
1927 }
1928 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1929 if (has_fd) {
1930 if (mon_fdset_fd->fd != fd) {
1931 continue;
1932 }
1933 mon_fdset_fd->removed = true;
1934 break;
1935 } else {
1936 mon_fdset_fd->removed = true;
1937 }
1938 }
1939 if (has_fd && !mon_fdset_fd) {
1940 goto error;
1941 }
1942 monitor_fdset_cleanup(mon_fdset);
1943 return;
1944 }
1945
1946error:
1947 if (has_fd) {
1948 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
1949 fdset_id, fd);
1950 } else {
1951 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
1952 }
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001953 error_setg(errp, QERR_FD_NOT_FOUND, fd_str);
Corey Bryantba1c0482012-08-14 16:43:43 -04001954}
1955
1956FdsetInfoList *qmp_query_fdsets(Error **errp)
1957{
1958 MonFdset *mon_fdset;
1959 MonFdsetFd *mon_fdset_fd;
1960 FdsetInfoList *fdset_list = NULL;
1961
1962 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1963 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
1964 FdsetFdInfoList *fdsetfd_list = NULL;
1965
1966 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
1967 fdset_info->value->fdset_id = mon_fdset->id;
1968
1969 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1970 FdsetFdInfoList *fdsetfd_info;
1971
1972 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
1973 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
1974 fdsetfd_info->value->fd = mon_fdset_fd->fd;
1975 if (mon_fdset_fd->opaque) {
1976 fdsetfd_info->value->has_opaque = true;
1977 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
1978 } else {
1979 fdsetfd_info->value->has_opaque = false;
1980 }
1981
1982 fdsetfd_info->next = fdsetfd_list;
1983 fdsetfd_list = fdsetfd_info;
1984 }
1985
1986 fdset_info->value->fds = fdsetfd_list;
1987
1988 fdset_info->next = fdset_list;
1989 fdset_list = fdset_info;
1990 }
1991
1992 return fdset_list;
1993}
1994
Corey Bryante446f702012-10-18 15:19:32 -04001995AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
1996 bool has_opaque, const char *opaque,
1997 Error **errp)
1998{
1999 MonFdset *mon_fdset = NULL;
2000 MonFdsetFd *mon_fdset_fd;
2001 AddfdInfo *fdinfo;
2002
2003 if (has_fdset_id) {
2004 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2005 /* Break if match found or match impossible due to ordering by ID */
2006 if (fdset_id <= mon_fdset->id) {
2007 if (fdset_id < mon_fdset->id) {
2008 mon_fdset = NULL;
2009 }
2010 break;
2011 }
2012 }
2013 }
2014
2015 if (mon_fdset == NULL) {
2016 int64_t fdset_id_prev = -1;
2017 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
2018
2019 if (has_fdset_id) {
2020 if (fdset_id < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002021 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
2022 "a non-negative value");
Corey Bryante446f702012-10-18 15:19:32 -04002023 return NULL;
2024 }
2025 /* Use specified fdset ID */
2026 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2027 mon_fdset_cur = mon_fdset;
2028 if (fdset_id < mon_fdset_cur->id) {
2029 break;
2030 }
2031 }
2032 } else {
2033 /* Use first available fdset ID */
2034 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2035 mon_fdset_cur = mon_fdset;
2036 if (fdset_id_prev == mon_fdset_cur->id - 1) {
2037 fdset_id_prev = mon_fdset_cur->id;
2038 continue;
2039 }
2040 break;
2041 }
2042 }
2043
2044 mon_fdset = g_malloc0(sizeof(*mon_fdset));
2045 if (has_fdset_id) {
2046 mon_fdset->id = fdset_id;
2047 } else {
2048 mon_fdset->id = fdset_id_prev + 1;
2049 }
2050
2051 /* The fdset list is ordered by fdset ID */
2052 if (!mon_fdset_cur) {
2053 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
2054 } else if (mon_fdset->id < mon_fdset_cur->id) {
2055 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
2056 } else {
2057 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
2058 }
2059 }
2060
2061 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
2062 mon_fdset_fd->fd = fd;
2063 mon_fdset_fd->removed = false;
2064 if (has_opaque) {
2065 mon_fdset_fd->opaque = g_strdup(opaque);
2066 }
2067 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
2068
2069 fdinfo = g_malloc0(sizeof(*fdinfo));
2070 fdinfo->fdset_id = mon_fdset->id;
2071 fdinfo->fd = mon_fdset_fd->fd;
2072
2073 return fdinfo;
2074}
2075
Corey Bryantadb696f2012-08-14 16:43:47 -04002076int monitor_fdset_get_fd(int64_t fdset_id, int flags)
2077{
Blue Swirlb2dc64c2012-08-18 20:14:54 +00002078#ifndef _WIN32
Corey Bryantadb696f2012-08-14 16:43:47 -04002079 MonFdset *mon_fdset;
2080 MonFdsetFd *mon_fdset_fd;
2081 int mon_fd_flags;
2082
Corey Bryantadb696f2012-08-14 16:43:47 -04002083 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2084 if (mon_fdset->id != fdset_id) {
2085 continue;
2086 }
2087 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2088 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
2089 if (mon_fd_flags == -1) {
2090 return -1;
2091 }
2092
2093 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
2094 return mon_fdset_fd->fd;
2095 }
2096 }
2097 errno = EACCES;
2098 return -1;
2099 }
2100#endif
2101
2102 errno = ENOENT;
2103 return -1;
2104}
2105
2106int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
2107{
2108 MonFdset *mon_fdset;
2109 MonFdsetFd *mon_fdset_fd_dup;
2110
2111 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2112 if (mon_fdset->id != fdset_id) {
2113 continue;
2114 }
2115 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2116 if (mon_fdset_fd_dup->fd == dup_fd) {
2117 return -1;
2118 }
2119 }
2120 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2121 mon_fdset_fd_dup->fd = dup_fd;
2122 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
2123 return 0;
2124 }
2125 return -1;
2126}
2127
2128static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2129{
2130 MonFdset *mon_fdset;
2131 MonFdsetFd *mon_fdset_fd_dup;
2132
2133 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2134 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2135 if (mon_fdset_fd_dup->fd == dup_fd) {
2136 if (remove) {
2137 QLIST_REMOVE(mon_fdset_fd_dup, next);
2138 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2139 monitor_fdset_cleanup(mon_fdset);
2140 }
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002141 return -1;
2142 } else {
2143 return mon_fdset->id;
Corey Bryantadb696f2012-08-14 16:43:47 -04002144 }
Corey Bryantadb696f2012-08-14 16:43:47 -04002145 }
2146 }
2147 }
2148 return -1;
2149}
2150
2151int monitor_fdset_dup_fd_find(int dup_fd)
2152{
2153 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2154}
2155
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002156void monitor_fdset_dup_fd_remove(int dup_fd)
Corey Bryantadb696f2012-08-14 16:43:47 -04002157{
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002158 monitor_fdset_dup_fd_find_remove(dup_fd, true);
Corey Bryantadb696f2012-08-14 16:43:47 -04002159}
2160
Markus Armbruster1677f4c2015-02-09 14:03:19 +01002161int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
Laszlo Ersek59063662014-04-10 10:24:31 +02002162{
2163 int fd;
2164 Error *local_err = NULL;
2165
2166 if (!qemu_isdigit(fdname[0]) && mon) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002167 fd = monitor_get_fd(mon, fdname, &local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002168 } else {
2169 fd = qemu_parse_fd(fdname);
Laszlo Ersek59063662014-04-10 10:24:31 +02002170 if (fd == -1) {
2171 error_setg(&local_err, "Invalid file descriptor number '%s'",
2172 fdname);
2173 }
2174 }
2175 if (local_err) {
2176 error_propagate(errp, local_err);
2177 assert(fd == -1);
2178 } else {
2179 assert(fd != -1);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002180 }
2181
2182 return fd;
2183}
2184
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002185/* Please update hmp-commands.hx when adding or changing commands */
Wayne Xia816f8922011-10-12 11:32:41 +08002186static mon_cmd_t info_cmds[] = {
Pavel Butsykinda76ee72015-09-10 18:38:58 +03002187#include "hmp-commands-info.h"
2188 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002189};
2190
Wenchao Xiaa13ced52013-01-14 14:06:28 +08002191/* mon_cmds and info_cmds would be sorted at runtime */
2192static mon_cmd_t mon_cmds[] = {
2193#include "hmp-commands.h"
2194 { NULL, NULL, },
2195};
2196
bellard9307c4c2004-04-04 12:57:25 +00002197/*******************************************************************/
2198
2199static const char *pch;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002200static sigjmp_buf expr_env;
bellard9307c4c2004-04-04 12:57:25 +00002201
bellard9307c4c2004-04-04 12:57:25 +00002202
Stefan Weil9c3175c2013-08-22 21:30:09 +02002203static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
2204expr_error(Monitor *mon, const char *fmt, ...)
bellard9dc39cb2004-03-14 21:38:27 +00002205{
Fam Zheng277acfe2013-08-20 10:58:21 +08002206 va_list ap;
2207 va_start(ap, fmt);
2208 monitor_vprintf(mon, fmt, ap);
2209 monitor_printf(mon, "\n");
2210 va_end(ap);
Peter Maydell6ab7e542013-02-20 15:21:09 +00002211 siglongjmp(expr_env, 1);
bellard9307c4c2004-04-04 12:57:25 +00002212}
2213
Markus Armbruster09b94182010-01-20 13:07:30 +01002214/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00002215static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002216{
Pavel Butsykinbf957282015-09-10 18:38:59 +03002217 const MonitorDef *md = target_monitor_defs();
Thomas Huth854e67f2017-01-13 13:12:35 +01002218 CPUState *cs = mon_get_cpu();
bellard92a31b12005-02-10 22:00:52 +00002219 void *ptr;
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002220 uint64_t tmp = 0;
2221 int ret;
bellard92a31b12005-02-10 22:00:52 +00002222
Thomas Huth854e67f2017-01-13 13:12:35 +01002223 if (cs == NULL || md == NULL) {
Pavel Butsykinbf957282015-09-10 18:38:59 +03002224 return -1;
2225 }
2226
2227 for(; md->name != NULL; md++) {
bellard9307c4c2004-04-04 12:57:25 +00002228 if (compare_cmd(name, md->name)) {
2229 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002230 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002231 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002232 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00002233 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002234 switch(md->type) {
2235 case MD_I32:
2236 *pval = *(int32_t *)ptr;
2237 break;
2238 case MD_TLONG:
2239 *pval = *(target_long *)ptr;
2240 break;
2241 default:
2242 *pval = 0;
2243 break;
2244 }
bellard9307c4c2004-04-04 12:57:25 +00002245 }
2246 return 0;
2247 }
2248 }
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002249
Thomas Huth854e67f2017-01-13 13:12:35 +01002250 ret = target_get_monitor_def(cs, name, &tmp);
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002251 if (!ret) {
2252 *pval = (target_long) tmp;
2253 }
2254
2255 return ret;
bellard9307c4c2004-04-04 12:57:25 +00002256}
2257
2258static void next(void)
2259{
Blue Swirl660f11b2009-07-31 21:16:51 +00002260 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002261 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002262 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002263 pch++;
2264 }
2265}
2266
aliguori376253e2009-03-05 23:01:23 +00002267static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002268
aliguori376253e2009-03-05 23:01:23 +00002269static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002270{
blueswir1c2efc952007-09-25 17:28:42 +00002271 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002272 char *p;
bellard6a00d602005-11-21 23:25:50 +00002273 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002274
2275 switch(*pch) {
2276 case '+':
2277 next();
aliguori376253e2009-03-05 23:01:23 +00002278 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002279 break;
2280 case '-':
2281 next();
aliguori376253e2009-03-05 23:01:23 +00002282 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002283 break;
2284 case '~':
2285 next();
aliguori376253e2009-03-05 23:01:23 +00002286 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002287 break;
2288 case '(':
2289 next();
aliguori376253e2009-03-05 23:01:23 +00002290 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002291 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002292 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002293 }
2294 next();
2295 break;
bellard81d09122004-07-14 17:21:37 +00002296 case '\'':
2297 pch++;
2298 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002299 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002300 n = *pch;
2301 pch++;
2302 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002303 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002304 next();
2305 break;
bellard9307c4c2004-04-04 12:57:25 +00002306 case '$':
2307 {
2308 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002309 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002310
bellard9307c4c2004-04-04 12:57:25 +00002311 pch++;
2312 q = buf;
2313 while ((*pch >= 'a' && *pch <= 'z') ||
2314 (*pch >= 'A' && *pch <= 'Z') ||
2315 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002316 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002317 if ((q - buf) < sizeof(buf) - 1)
2318 *q++ = *pch;
2319 pch++;
2320 }
blueswir1cd390082008-11-16 13:53:32 +00002321 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002322 pch++;
2323 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002324 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01002325 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00002326 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00002327 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002328 }
2329 break;
2330 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002331 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002332 n = 0;
2333 break;
2334 default:
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002335 errno = 0;
bellard4f4fbf72006-06-25 18:28:12 +00002336 n = strtoull(pch, &p, 0);
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002337 if (errno == ERANGE) {
2338 expr_error(mon, "number too large");
2339 }
bellard9307c4c2004-04-04 12:57:25 +00002340 if (pch == p) {
Fam Zheng277acfe2013-08-20 10:58:21 +08002341 expr_error(mon, "invalid char '%c' in expression", *p);
bellard9307c4c2004-04-04 12:57:25 +00002342 }
2343 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002344 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002345 pch++;
2346 break;
2347 }
2348 return n;
2349}
2350
2351
aliguori376253e2009-03-05 23:01:23 +00002352static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002353{
blueswir1c2efc952007-09-25 17:28:42 +00002354 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002355 int op;
ths3b46e622007-09-17 08:09:54 +00002356
aliguori376253e2009-03-05 23:01:23 +00002357 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002358 for(;;) {
2359 op = *pch;
2360 if (op != '*' && op != '/' && op != '%')
2361 break;
2362 next();
aliguori376253e2009-03-05 23:01:23 +00002363 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002364 switch(op) {
2365 default:
2366 case '*':
2367 val *= val2;
2368 break;
2369 case '/':
2370 case '%':
ths5fafdf22007-09-16 21:08:06 +00002371 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002372 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002373 if (op == '/')
2374 val /= val2;
2375 else
2376 val %= val2;
2377 break;
2378 }
2379 }
2380 return val;
2381}
2382
aliguori376253e2009-03-05 23:01:23 +00002383static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002384{
blueswir1c2efc952007-09-25 17:28:42 +00002385 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002386 int op;
bellard9307c4c2004-04-04 12:57:25 +00002387
aliguori376253e2009-03-05 23:01:23 +00002388 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002389 for(;;) {
2390 op = *pch;
2391 if (op != '&' && op != '|' && op != '^')
2392 break;
2393 next();
aliguori376253e2009-03-05 23:01:23 +00002394 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002395 switch(op) {
2396 default:
2397 case '&':
2398 val &= val2;
2399 break;
2400 case '|':
2401 val |= val2;
2402 break;
2403 case '^':
2404 val ^= val2;
2405 break;
2406 }
2407 }
2408 return val;
2409}
2410
aliguori376253e2009-03-05 23:01:23 +00002411static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002412{
blueswir1c2efc952007-09-25 17:28:42 +00002413 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002414 int op;
bellard9307c4c2004-04-04 12:57:25 +00002415
aliguori376253e2009-03-05 23:01:23 +00002416 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002417 for(;;) {
2418 op = *pch;
2419 if (op != '+' && op != '-')
2420 break;
2421 next();
aliguori376253e2009-03-05 23:01:23 +00002422 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002423 if (op == '+')
2424 val += val2;
2425 else
2426 val -= val2;
2427 }
2428 return val;
2429}
2430
aliguori376253e2009-03-05 23:01:23 +00002431static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002432{
2433 pch = *pp;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002434 if (sigsetjmp(expr_env, 0)) {
bellard9307c4c2004-04-04 12:57:25 +00002435 *pp = pch;
2436 return -1;
2437 }
blueswir1cd390082008-11-16 13:53:32 +00002438 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002439 pch++;
aliguori376253e2009-03-05 23:01:23 +00002440 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002441 *pp = pch;
2442 return 0;
2443}
2444
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002445static int get_double(Monitor *mon, double *pval, const char **pp)
2446{
2447 const char *p = *pp;
2448 char *tailp;
2449 double d;
2450
2451 d = strtod(p, &tailp);
2452 if (tailp == p) {
2453 monitor_printf(mon, "Number expected\n");
2454 return -1;
2455 }
2456 if (d != d || d - d != 0) {
2457 /* NaN or infinity */
2458 monitor_printf(mon, "Bad number\n");
2459 return -1;
2460 }
2461 *pval = d;
2462 *pp = tailp;
2463 return 0;
2464}
2465
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002466/*
2467 * Store the command-name in cmdname, and return a pointer to
2468 * the remaining of the command string.
2469 */
2470static const char *get_command_name(const char *cmdline,
2471 char *cmdname, size_t nlen)
2472{
2473 size_t len;
2474 const char *p, *pstart;
2475
2476 p = cmdline;
2477 while (qemu_isspace(*p))
2478 p++;
2479 if (*p == '\0')
2480 return NULL;
2481 pstart = p;
2482 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2483 p++;
2484 len = p - pstart;
2485 if (len > nlen - 1)
2486 len = nlen - 1;
2487 memcpy(cmdname, pstart, len);
2488 cmdname[len] = '\0';
2489 return p;
2490}
2491
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002492/**
2493 * Read key of 'type' into 'key' and return the current
2494 * 'type' pointer.
2495 */
2496static char *key_get_info(const char *type, char **key)
2497{
2498 size_t len;
2499 char *p, *str;
2500
2501 if (*type == ',')
2502 type++;
2503
2504 p = strchr(type, ':');
2505 if (!p) {
2506 *key = NULL;
2507 return NULL;
2508 }
2509 len = p - type;
2510
Anthony Liguori7267c092011-08-20 22:09:37 -05002511 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002512 memcpy(str, type, len);
2513 str[len] = '\0';
2514
2515 *key = str;
2516 return ++p;
2517}
2518
bellard9307c4c2004-04-04 12:57:25 +00002519static int default_fmt_format = 'x';
2520static int default_fmt_size = 4;
2521
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002522static int is_valid_option(const char *c, const char *typestr)
2523{
2524 char option[3];
2525
2526 option[0] = '-';
2527 option[1] = *c;
2528 option[2] = '\0';
2529
2530 typestr = strstr(typestr, option);
2531 return (typestr != NULL);
2532}
2533
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03002534static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
2535 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002536{
2537 const mon_cmd_t *cmd;
2538
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03002539 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002540 if (compare_cmd(cmdname, cmd->name)) {
2541 return cmd;
2542 }
2543 }
2544
2545 return NULL;
2546}
2547
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002548/*
Bandan Dasae502122015-06-03 18:38:08 -04002549 * Parse command name from @cmdp according to command table @table.
2550 * If blank, return NULL.
2551 * Else, if no valid command can be found, report to @mon, and return
2552 * NULL.
2553 * Else, change @cmdp to point right behind the name, and return its
2554 * command table entry.
2555 * Do not assume the return value points into @table! It doesn't when
2556 * the command is found in a sub-command table.
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002557 */
Anthony Liguoric227f092009-10-01 16:12:16 -05002558static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Bandan Dasae502122015-06-03 18:38:08 -04002559 const char **cmdp,
2560 mon_cmd_t *table)
bellard9307c4c2004-04-04 12:57:25 +00002561{
Bandan Dasae502122015-06-03 18:38:08 -04002562 const char *p;
Anthony Liguoric227f092009-10-01 16:12:16 -05002563 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002564 char cmdname[256];
bellard9dc39cb2004-03-14 21:38:27 +00002565
bellard9307c4c2004-04-04 12:57:25 +00002566 /* extract the command name */
Bandan Dasae502122015-06-03 18:38:08 -04002567 p = get_command_name(*cmdp, cmdname, sizeof(cmdname));
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002568 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002569 return NULL;
ths3b46e622007-09-17 08:09:54 +00002570
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002571 cmd = search_dispatch_table(table, cmdname);
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002572 if (!cmd) {
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002573 monitor_printf(mon, "unknown command: '%.*s'\n",
Bandan Dasae502122015-06-03 18:38:08 -04002574 (int)(p - *cmdp), *cmdp);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002575 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002576 }
bellard9307c4c2004-04-04 12:57:25 +00002577
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002578 /* filter out following useless space */
2579 while (qemu_isspace(*p)) {
2580 p++;
2581 }
Bandan Dasae502122015-06-03 18:38:08 -04002582
2583 *cmdp = p;
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002584 /* search sub command */
Bandan Dasae502122015-06-03 18:38:08 -04002585 if (cmd->sub_table != NULL && *p != '\0') {
2586 return monitor_parse_command(mon, cmdp, cmd->sub_table);
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002587 }
2588
Bandan Dasae502122015-06-03 18:38:08 -04002589 return cmd;
2590}
2591
2592/*
2593 * Parse arguments for @cmd.
2594 * If it can't be parsed, report to @mon, and return NULL.
2595 * Else, insert command arguments into a QDict, and return it.
2596 * Note: On success, caller has to free the QDict structure.
2597 */
2598
2599static QDict *monitor_parse_arguments(Monitor *mon,
2600 const char **endp,
2601 const mon_cmd_t *cmd)
2602{
2603 const char *typestr;
2604 char *key;
2605 int c;
2606 const char *p = *endp;
2607 char buf[1024];
2608 QDict *qdict = qdict_new();
2609
bellard9307c4c2004-04-04 12:57:25 +00002610 /* parse the parameters */
2611 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00002612 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002613 typestr = key_get_info(typestr, &key);
2614 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00002615 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002616 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00002617 typestr++;
2618 switch(c) {
2619 case 'F':
bellard81d09122004-07-14 17:21:37 +00002620 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002621 case 's':
2622 {
2623 int ret;
ths3b46e622007-09-17 08:09:54 +00002624
blueswir1cd390082008-11-16 13:53:32 +00002625 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002626 p++;
2627 if (*typestr == '?') {
2628 typestr++;
2629 if (*p == '\0') {
2630 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03002631 break;
bellard9307c4c2004-04-04 12:57:25 +00002632 }
2633 }
2634 ret = get_str(buf, sizeof(buf), &p);
2635 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002636 switch(c) {
2637 case 'F':
aliguori376253e2009-03-05 23:01:23 +00002638 monitor_printf(mon, "%s: filename expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002639 cmd->name);
bellard81d09122004-07-14 17:21:37 +00002640 break;
2641 case 'B':
aliguori376253e2009-03-05 23:01:23 +00002642 monitor_printf(mon, "%s: block device name expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002643 cmd->name);
bellard81d09122004-07-14 17:21:37 +00002644 break;
2645 default:
Bandan Dasae502122015-06-03 18:38:08 -04002646 monitor_printf(mon, "%s: string expected\n", cmd->name);
bellard81d09122004-07-14 17:21:37 +00002647 break;
2648 }
bellard9307c4c2004-04-04 12:57:25 +00002649 goto fail;
2650 }
Luiz Capitulino53773582009-08-28 15:27:25 -03002651 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00002652 }
2653 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01002654 case 'O':
2655 {
2656 QemuOptsList *opts_list;
2657 QemuOpts *opts;
2658
2659 opts_list = qemu_find_opts(key);
2660 if (!opts_list || opts_list->desc->name) {
2661 goto bad_type;
2662 }
2663 while (qemu_isspace(*p)) {
2664 p++;
2665 }
2666 if (!*p)
2667 break;
2668 if (get_str(buf, sizeof(buf), &p) < 0) {
2669 goto fail;
2670 }
Markus Armbruster70b94332015-02-13 12:50:26 +01002671 opts = qemu_opts_parse_noisily(opts_list, buf, true);
Markus Armbruster361127d2010-02-10 20:24:35 +01002672 if (!opts) {
2673 goto fail;
2674 }
2675 qemu_opts_to_qdict(opts, qdict);
2676 qemu_opts_del(opts);
2677 }
2678 break;
bellard9307c4c2004-04-04 12:57:25 +00002679 case '/':
2680 {
2681 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002682
blueswir1cd390082008-11-16 13:53:32 +00002683 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002684 p++;
2685 if (*p == '/') {
2686 /* format found */
2687 p++;
2688 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002689 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002690 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002691 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002692 count = count * 10 + (*p - '0');
2693 p++;
2694 }
2695 }
2696 size = -1;
2697 format = -1;
2698 for(;;) {
2699 switch(*p) {
2700 case 'o':
2701 case 'd':
2702 case 'u':
2703 case 'x':
2704 case 'i':
2705 case 'c':
2706 format = *p++;
2707 break;
2708 case 'b':
2709 size = 1;
2710 p++;
2711 break;
2712 case 'h':
2713 size = 2;
2714 p++;
2715 break;
2716 case 'w':
2717 size = 4;
2718 p++;
2719 break;
2720 case 'g':
2721 case 'L':
2722 size = 8;
2723 p++;
2724 break;
2725 default:
2726 goto next;
2727 }
2728 }
2729 next:
blueswir1cd390082008-11-16 13:53:32 +00002730 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00002731 monitor_printf(mon, "invalid char in format: '%c'\n",
2732 *p);
bellard9307c4c2004-04-04 12:57:25 +00002733 goto fail;
2734 }
bellard9307c4c2004-04-04 12:57:25 +00002735 if (format < 0)
2736 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002737 if (format != 'i') {
2738 /* for 'i', not specifying a size gives -1 as size */
2739 if (size < 0)
2740 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002741 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002742 }
bellard9307c4c2004-04-04 12:57:25 +00002743 default_fmt_format = format;
2744 } else {
2745 count = 1;
2746 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002747 if (format != 'i') {
2748 size = default_fmt_size;
2749 } else {
2750 size = -1;
2751 }
bellard9307c4c2004-04-04 12:57:25 +00002752 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002753 qdict_put(qdict, "count", qint_from_int(count));
2754 qdict_put(qdict, "format", qint_from_int(format));
2755 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00002756 }
2757 break;
2758 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002759 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002760 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00002761 {
blueswir1c2efc952007-09-25 17:28:42 +00002762 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00002763
blueswir1cd390082008-11-16 13:53:32 +00002764 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002765 p++;
bellard34405572004-06-08 00:55:58 +00002766 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002767 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03002768 if (*p == '\0') {
2769 typestr++;
2770 break;
2771 }
bellard34405572004-06-08 00:55:58 +00002772 } else {
2773 if (*p == '.') {
2774 p++;
blueswir1cd390082008-11-16 13:53:32 +00002775 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00002776 p++;
bellard34405572004-06-08 00:55:58 +00002777 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03002778 typestr++;
2779 break;
bellard34405572004-06-08 00:55:58 +00002780 }
2781 }
bellard13224a82006-07-14 22:03:35 +00002782 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002783 }
aliguori376253e2009-03-05 23:01:23 +00002784 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00002785 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002786 /* Check if 'i' is greater than 32-bit */
2787 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
Bandan Dasae502122015-06-03 18:38:08 -04002788 monitor_printf(mon, "\'%s\' has failed: ", cmd->name);
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002789 monitor_printf(mon, "integer is for 32-bit values\n");
2790 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002791 } else if (c == 'M') {
Luiz Capitulino91162842012-04-26 17:34:30 -03002792 if (val < 0) {
2793 monitor_printf(mon, "enter a positive value\n");
2794 goto fail;
2795 }
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002796 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002797 }
Luiz Capitulino53773582009-08-28 15:27:25 -03002798 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00002799 }
2800 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02002801 case 'o':
2802 {
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +01002803 int ret;
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +01002804 uint64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02002805 char *end;
2806
2807 while (qemu_isspace(*p)) {
2808 p++;
2809 }
2810 if (*typestr == '?') {
2811 typestr++;
2812 if (*p == '\0') {
2813 break;
2814 }
2815 }
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +01002816 ret = qemu_strtosz_MiB(p, &end, &val);
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +01002817 if (ret < 0 || val > INT64_MAX) {
Jes Sorensendbc0c672010-10-21 17:15:47 +02002818 monitor_printf(mon, "invalid size\n");
2819 goto fail;
2820 }
2821 qdict_put(qdict, key, qint_from_int(val));
2822 p = end;
2823 }
2824 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01002825 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002826 {
2827 double val;
2828
2829 while (qemu_isspace(*p))
2830 p++;
2831 if (*typestr == '?') {
2832 typestr++;
2833 if (*p == '\0') {
2834 break;
2835 }
2836 }
2837 if (get_double(mon, &val, &p) < 0) {
2838 goto fail;
2839 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02002840 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01002841 switch (*p) {
2842 case 'm':
2843 val /= 1e3; p += 2; break;
2844 case 'u':
2845 val /= 1e6; p += 2; break;
2846 case 'n':
2847 val /= 1e9; p += 2; break;
2848 }
2849 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002850 if (*p && !qemu_isspace(*p)) {
2851 monitor_printf(mon, "Unknown unit suffix\n");
2852 goto fail;
2853 }
2854 qdict_put(qdict, key, qfloat_from_double(val));
2855 }
2856 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002857 case 'b':
2858 {
2859 const char *beg;
Eric Blakefc48ffc2015-05-15 16:24:59 -06002860 bool val;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002861
2862 while (qemu_isspace(*p)) {
2863 p++;
2864 }
2865 beg = p;
2866 while (qemu_isgraph(*p)) {
2867 p++;
2868 }
2869 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06002870 val = true;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002871 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06002872 val = false;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002873 } else {
2874 monitor_printf(mon, "Expected 'on' or 'off'\n");
2875 goto fail;
2876 }
Eric Blakefc48ffc2015-05-15 16:24:59 -06002877 qdict_put(qdict, key, qbool_from_bool(val));
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002878 }
2879 break;
bellard9307c4c2004-04-04 12:57:25 +00002880 case '-':
2881 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002882 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03002883 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00002884 /* option */
ths3b46e622007-09-17 08:09:54 +00002885
bellard9307c4c2004-04-04 12:57:25 +00002886 c = *typestr++;
2887 if (c == '\0')
2888 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00002889 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002890 p++;
bellard9307c4c2004-04-04 12:57:25 +00002891 if (*p == '-') {
2892 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002893 if(c != *p) {
2894 if(!is_valid_option(p, typestr)) {
2895
2896 monitor_printf(mon, "%s: unsupported option -%c\n",
Bandan Dasae502122015-06-03 18:38:08 -04002897 cmd->name, *p);
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002898 goto fail;
2899 } else {
2900 skip_key = 1;
2901 }
bellard9307c4c2004-04-04 12:57:25 +00002902 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002903 if(skip_key) {
2904 p = tmp;
2905 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03002906 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002907 p++;
Eric Blakefc48ffc2015-05-15 16:24:59 -06002908 qdict_put(qdict, key, qbool_from_bool(true));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002909 }
bellard9307c4c2004-04-04 12:57:25 +00002910 }
bellard9307c4c2004-04-04 12:57:25 +00002911 }
2912 break;
Wenchao Xia129be002013-08-27 20:38:26 +08002913 case 'S':
2914 {
2915 /* package all remaining string */
2916 int len;
2917
2918 while (qemu_isspace(*p)) {
2919 p++;
2920 }
2921 if (*typestr == '?') {
2922 typestr++;
2923 if (*p == '\0') {
2924 /* no remaining string: NULL argument */
2925 break;
2926 }
2927 }
2928 len = strlen(p);
2929 if (len <= 0) {
2930 monitor_printf(mon, "%s: string expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002931 cmd->name);
Bandan Dase549d2a2015-06-03 18:38:10 -04002932 goto fail;
Wenchao Xia129be002013-08-27 20:38:26 +08002933 }
2934 qdict_put(qdict, key, qstring_from_str(p));
2935 p += len;
2936 }
2937 break;
bellard9307c4c2004-04-04 12:57:25 +00002938 default:
2939 bad_type:
Bandan Dasae502122015-06-03 18:38:08 -04002940 monitor_printf(mon, "%s: unknown type '%c'\n", cmd->name, c);
bellard9307c4c2004-04-04 12:57:25 +00002941 goto fail;
2942 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002943 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002944 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00002945 }
2946 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00002947 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002948 p++;
2949 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00002950 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
Bandan Dasae502122015-06-03 18:38:08 -04002951 cmd->name);
bellard9307c4c2004-04-04 12:57:25 +00002952 goto fail;
2953 }
2954
Bandan Dasae502122015-06-03 18:38:08 -04002955 return qdict;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02002956
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002957fail:
Bandan Dasae502122015-06-03 18:38:08 -04002958 QDECREF(qdict);
Anthony Liguori7267c092011-08-20 22:09:37 -05002959 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002960 return NULL;
2961}
2962
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01002963static void handle_hmp_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002964{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002965 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05002966 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002967
Bandan Dasae502122015-06-03 18:38:08 -04002968 cmd = monitor_parse_command(mon, &cmdline, mon->cmd_table);
2969 if (!cmd) {
2970 return;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002971 }
2972
Bandan Dasae502122015-06-03 18:38:08 -04002973 qdict = monitor_parse_arguments(mon, &cmdline, cmd);
2974 if (!qdict) {
Bandan Dasdd41eea2015-06-03 18:38:09 -04002975 monitor_printf(mon, "Try \"help %s\" for more information\n",
2976 cmd->name);
Bandan Dasae502122015-06-03 18:38:08 -04002977 return;
2978 }
2979
Marc-André Lureau2b9e3572016-09-12 13:19:06 +04002980 cmd->cmd(mon, qdict);
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002981 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00002982}
2983
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08002984static void cmd_completion(Monitor *mon, const char *name, const char *list)
bellard81d09122004-07-14 17:21:37 +00002985{
2986 const char *p, *pstart;
2987 char cmd[128];
2988 int len;
2989
2990 p = list;
2991 for(;;) {
2992 pstart = p;
2993 p = strchr(p, '|');
2994 if (!p)
2995 p = pstart + strlen(pstart);
2996 len = p - pstart;
2997 if (len > sizeof(cmd) - 2)
2998 len = sizeof(cmd) - 2;
2999 memcpy(cmd, pstart, len);
3000 cmd[len] = '\0';
3001 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003002 readline_add_completion(mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00003003 }
3004 if (*p == '\0')
3005 break;
3006 p++;
3007 }
3008}
3009
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003010static void file_completion(Monitor *mon, const char *input)
bellard81d09122004-07-14 17:21:37 +00003011{
3012 DIR *ffs;
3013 struct dirent *d;
3014 char path[1024];
3015 char file[1024], file_prefix[1024];
3016 int input_path_len;
3017 const char *p;
3018
ths5fafdf22007-09-16 21:08:06 +00003019 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00003020 if (!p) {
3021 input_path_len = 0;
3022 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00003023 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00003024 } else {
3025 input_path_len = p - input + 1;
3026 memcpy(path, input, input_path_len);
3027 if (input_path_len > sizeof(path) - 1)
3028 input_path_len = sizeof(path) - 1;
3029 path[input_path_len] = '\0';
3030 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3031 }
Bandan Das19f2db52015-06-03 18:38:07 -04003032
bellard81d09122004-07-14 17:21:37 +00003033 ffs = opendir(path);
3034 if (!ffs)
3035 return;
3036 for(;;) {
3037 struct stat sb;
3038 d = readdir(ffs);
3039 if (!d)
3040 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09003041
3042 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
3043 continue;
3044 }
3045
bellard81d09122004-07-14 17:21:37 +00003046 if (strstart(d->d_name, file_prefix, NULL)) {
3047 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00003048 if (input_path_len < sizeof(file))
3049 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3050 d->d_name);
bellard81d09122004-07-14 17:21:37 +00003051 /* stat the file to find out if it's a directory.
3052 * In that case add a slash to speed up typing long paths
3053 */
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01003054 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
blueswir1363a37d2008-08-21 17:58:08 +00003055 pstrcat(file, sizeof(file), "/");
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01003056 }
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003057 readline_add_completion(mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003058 }
3059 }
3060 closedir(ffs);
3061}
3062
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003063static const char *next_arg_type(const char *typestr)
3064{
3065 const char *p = strchr(typestr, ':');
3066 return (p != NULL ? ++p : typestr);
3067}
3068
Hani Benhabiles40d19392014-05-07 23:41:30 +01003069static void add_completion_option(ReadLineState *rs, const char *str,
3070 const char *option)
3071{
3072 if (!str || !option) {
3073 return;
3074 }
3075 if (!strncmp(option, str, strlen(str))) {
3076 readline_add_completion(rs, option);
3077 }
3078}
3079
Hani Benhabiles13e315d2014-05-07 23:41:29 +01003080void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
3081{
3082 size_t len;
3083 ChardevBackendInfoList *list, *start;
3084
3085 if (nb_args != 2) {
3086 return;
3087 }
3088 len = strlen(str);
3089 readline_set_completion_index(rs, len);
3090
3091 start = list = qmp_query_chardev_backends(NULL);
3092 while (list) {
3093 const char *chr_name = list->value->name;
3094
3095 if (!strncmp(chr_name, str, len)) {
3096 readline_add_completion(rs, chr_name);
3097 }
3098 list = list->next;
3099 }
3100 qapi_free_ChardevBackendInfoList(start);
3101}
3102
Hani Benhabilesb162b492014-05-07 23:41:31 +01003103void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
3104{
3105 size_t len;
3106 int i;
3107
3108 if (nb_args != 2) {
3109 return;
3110 }
3111 len = strlen(str);
3112 readline_set_completion_index(rs, len);
Eric Blakef394b2e2016-07-13 21:50:23 -06003113 for (i = 0; NetClientDriver_lookup[i]; i++) {
3114 add_completion_option(rs, str, NetClientDriver_lookup[i]);
Hani Benhabilesb162b492014-05-07 23:41:31 +01003115 }
3116}
3117
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003118void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003119{
3120 GSList *list, *elt;
3121 size_t len;
3122
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003123 if (nb_args != 2) {
3124 return;
3125 }
3126
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003127 len = strlen(str);
3128 readline_set_completion_index(rs, len);
3129 list = elt = object_class_get_list(TYPE_DEVICE, false);
3130 while (elt) {
3131 const char *name;
3132 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
3133 TYPE_DEVICE);
3134 name = object_class_get_name(OBJECT_CLASS(dc));
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003135
3136 if (!dc->cannot_instantiate_with_device_add_yet
3137 && !strncmp(name, str, len)) {
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003138 readline_add_completion(rs, name);
3139 }
3140 elt = elt->next;
3141 }
3142 g_slist_free(list);
3143}
3144
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003145void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003146{
3147 GSList *list, *elt;
3148 size_t len;
3149
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003150 if (nb_args != 2) {
3151 return;
3152 }
3153
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003154 len = strlen(str);
3155 readline_set_completion_index(rs, len);
3156 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
3157 while (elt) {
3158 const char *name;
3159
3160 name = object_class_get_name(OBJECT_CLASS(elt->data));
3161 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
3162 readline_add_completion(rs, name);
3163 }
3164 elt = elt->next;
3165 }
3166 g_slist_free(list);
3167}
3168
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003169static void peripheral_device_del_completion(ReadLineState *rs,
3170 const char *str, size_t len)
3171{
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003172 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
3173 GSList *list, *item;
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003174
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003175 list = qdev_build_hotpluggable_device_list(peripheral);
3176 if (!list) {
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003177 return;
3178 }
3179
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003180 for (item = list; item; item = g_slist_next(item)) {
3181 DeviceState *dev = item->data;
3182
3183 if (dev->id && !strncmp(str, dev->id, len)) {
3184 readline_add_completion(rs, dev->id);
3185 }
3186 }
3187
3188 g_slist_free(list);
3189}
3190
Hani Benhabiles6297d9a2014-05-07 23:41:28 +01003191void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3192{
3193 size_t len;
3194 ChardevInfoList *list, *start;
3195
3196 if (nb_args != 2) {
3197 return;
3198 }
3199 len = strlen(str);
3200 readline_set_completion_index(rs, len);
3201
3202 start = list = qmp_query_chardev(NULL);
3203 while (list) {
3204 ChardevInfo *chr = list->value;
3205
3206 if (!strncmp(chr->label, str, len)) {
3207 readline_add_completion(rs, chr->label);
3208 }
3209 list = list->next;
3210 }
3211 qapi_free_ChardevInfoList(start);
3212}
3213
Hani Benhabiles8e597772014-05-27 23:39:30 +01003214static void ringbuf_completion(ReadLineState *rs, const char *str)
3215{
3216 size_t len;
3217 ChardevInfoList *list, *start;
3218
3219 len = strlen(str);
3220 readline_set_completion_index(rs, len);
3221
3222 start = list = qmp_query_chardev(NULL);
3223 while (list) {
3224 ChardevInfo *chr_info = list->value;
3225
3226 if (!strncmp(chr_info->label, str, len)) {
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03003227 Chardev *chr = qemu_chr_find(chr_info->label);
Marc-André Lureau777357d2016-12-07 18:39:10 +03003228 if (chr && CHARDEV_IS_RINGBUF(chr)) {
Hani Benhabiles8e597772014-05-27 23:39:30 +01003229 readline_add_completion(rs, chr_info->label);
3230 }
3231 }
3232 list = list->next;
3233 }
3234 qapi_free_ChardevInfoList(start);
3235}
3236
Hani Benhabiles8e597772014-05-27 23:39:30 +01003237void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
3238{
3239 if (nb_args != 2) {
3240 return;
3241 }
3242 ringbuf_completion(rs, str);
3243}
3244
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003245void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
3246{
3247 size_t len;
3248
3249 if (nb_args != 2) {
3250 return;
3251 }
3252
3253 len = strlen(str);
3254 readline_set_completion_index(rs, len);
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003255 peripheral_device_del_completion(rs, str, len);
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003256}
3257
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003258void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003259{
3260 ObjectPropertyInfoList *list, *start;
3261 size_t len;
3262
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003263 if (nb_args != 2) {
3264 return;
3265 }
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003266 len = strlen(str);
3267 readline_set_completion_index(rs, len);
3268
3269 start = list = qmp_qom_list("/objects", NULL);
3270 while (list) {
3271 ObjectPropertyInfo *info = list->value;
3272
3273 if (!strncmp(info->type, "child<", 5)
3274 && !strncmp(info->name, str, len)) {
3275 readline_add_completion(rs, info->name);
3276 }
3277 list = list->next;
3278 }
3279 qapi_free_ObjectPropertyInfoList(start);
3280}
3281
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003282void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
3283{
3284 int i;
3285 char *sep;
3286 size_t len;
3287
3288 if (nb_args != 2) {
3289 return;
3290 }
3291 sep = strrchr(str, '-');
3292 if (sep) {
3293 str = sep + 1;
3294 }
3295 len = strlen(str);
3296 readline_set_completion_index(rs, len);
Eric Blake7fb1cf12015-11-18 01:52:57 -07003297 for (i = 0; i < Q_KEY_CODE__MAX; i++) {
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003298 if (!strncmp(str, QKeyCode_lookup[i], len)) {
3299 readline_add_completion(rs, QKeyCode_lookup[i]);
3300 }
3301 }
3302}
3303
Hani Benhabiles40d19392014-05-07 23:41:30 +01003304void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
3305{
3306 size_t len;
3307
3308 len = strlen(str);
3309 readline_set_completion_index(rs, len);
3310 if (nb_args == 2) {
Jason Wangeaed4832015-04-23 14:21:38 +08003311 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles40d19392014-05-07 23:41:30 +01003312 int count, i;
3313 count = qemu_find_net_clients_except(NULL, ncs,
Eric Blakef394b2e2016-07-13 21:50:23 -06003314 NET_CLIENT_DRIVER_NONE,
Jason Wangeaed4832015-04-23 14:21:38 +08003315 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003316 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles40d19392014-05-07 23:41:30 +01003317 const char *name = ncs[i]->name;
3318 if (!strncmp(str, name, len)) {
3319 readline_add_completion(rs, name);
3320 }
3321 }
3322 } else if (nb_args == 3) {
3323 add_completion_option(rs, str, "on");
3324 add_completion_option(rs, str, "off");
3325 }
3326}
3327
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003328void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
3329{
3330 int len, count, i;
Jason Wangeaed4832015-04-23 14:21:38 +08003331 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003332
3333 if (nb_args != 2) {
3334 return;
3335 }
3336
3337 len = strlen(str);
3338 readline_set_completion_index(rs, len);
Eric Blakef394b2e2016-07-13 21:50:23 -06003339 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_DRIVER_NIC,
Jason Wangeaed4832015-04-23 14:21:38 +08003340 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003341 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003342 QemuOpts *opts;
3343 const char *name = ncs[i]->name;
3344 if (strncmp(str, name, len)) {
3345 continue;
3346 }
3347 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
3348 if (opts) {
3349 readline_add_completion(rs, name);
3350 }
3351 }
3352}
3353
Lluís Vilanovabd712112016-07-11 12:53:51 +02003354void info_trace_events_completion(ReadLineState *rs, int nb_args, const char *str)
3355{
3356 size_t len;
3357
3358 len = strlen(str);
3359 readline_set_completion_index(rs, len);
3360 if (nb_args == 2) {
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003361 TraceEventIter iter;
3362 TraceEvent *ev;
3363 char *pattern = g_strdup_printf("%s*", str);
3364 trace_event_iter_init(&iter, pattern);
3365 while ((ev = trace_event_iter_next(&iter)) != NULL) {
3366 readline_add_completion(rs, trace_event_get_name(ev));
Lluís Vilanovabd712112016-07-11 12:53:51 +02003367 }
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003368 g_free(pattern);
Lluís Vilanovabd712112016-07-11 12:53:51 +02003369 }
3370}
3371
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003372void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
3373{
3374 size_t len;
3375
3376 len = strlen(str);
3377 readline_set_completion_index(rs, len);
3378 if (nb_args == 2) {
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003379 TraceEventIter iter;
3380 TraceEvent *ev;
3381 char *pattern = g_strdup_printf("%s*", str);
3382 trace_event_iter_init(&iter, pattern);
3383 while ((ev = trace_event_iter_next(&iter)) != NULL) {
3384 readline_add_completion(rs, trace_event_get_name(ev));
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003385 }
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003386 g_free(pattern);
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003387 } else if (nb_args == 3) {
3388 add_completion_option(rs, str, "on");
3389 add_completion_option(rs, str, "off");
3390 }
3391}
3392
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003393void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
3394{
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003395 int i;
3396
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003397 if (nb_args != 2) {
3398 return;
3399 }
3400 readline_set_completion_index(rs, strlen(str));
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003401 for (i = 0; WatchdogExpirationAction_lookup[i]; i++) {
3402 add_completion_option(rs, str, WatchdogExpirationAction_lookup[i]);
3403 }
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003404}
3405
Hani Benhabilesc68a0402014-05-27 23:39:32 +01003406void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
3407 const char *str)
3408{
3409 size_t len;
3410
3411 len = strlen(str);
3412 readline_set_completion_index(rs, len);
3413 if (nb_args == 2) {
3414 int i;
Eric Blake7fb1cf12015-11-18 01:52:57 -07003415 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Hani Benhabilesc68a0402014-05-27 23:39:32 +01003416 const char *name = MigrationCapability_lookup[i];
3417 if (!strncmp(str, name, len)) {
3418 readline_add_completion(rs, name);
3419 }
3420 }
3421 } else if (nb_args == 3) {
3422 add_completion_option(rs, str, "on");
3423 add_completion_option(rs, str, "off");
3424 }
3425}
3426
Liang Li50e9a622015-03-23 16:32:29 +08003427void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
3428 const char *str)
3429{
3430 size_t len;
3431
3432 len = strlen(str);
3433 readline_set_completion_index(rs, len);
3434 if (nb_args == 2) {
3435 int i;
Eric Blake7fb1cf12015-11-18 01:52:57 -07003436 for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
Liang Li50e9a622015-03-23 16:32:29 +08003437 const char *name = MigrationParameter_lookup[i];
3438 if (!strncmp(str, name, len)) {
3439 readline_add_completion(rs, name);
3440 }
3441 }
3442 }
3443}
3444
Hani Benhabilese3bb5322014-05-27 23:39:34 +01003445void host_net_add_completion(ReadLineState *rs, int nb_args, const char *str)
3446{
3447 int i;
3448 size_t len;
3449 if (nb_args != 2) {
3450 return;
3451 }
3452 len = strlen(str);
3453 readline_set_completion_index(rs, len);
3454 for (i = 0; host_net_devices[i]; i++) {
3455 if (!strncmp(host_net_devices[i], str, len)) {
3456 readline_add_completion(rs, host_net_devices[i]);
3457 }
3458 }
3459}
3460
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003461void host_net_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3462{
Jason Wangeaed4832015-04-23 14:21:38 +08003463 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003464 int count, i, len;
3465
3466 len = strlen(str);
3467 readline_set_completion_index(rs, len);
3468 if (nb_args == 2) {
3469 count = qemu_find_net_clients_except(NULL, ncs,
Eric Blakef394b2e2016-07-13 21:50:23 -06003470 NET_CLIENT_DRIVER_NONE,
Jason Wangeaed4832015-04-23 14:21:38 +08003471 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003472 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003473 int id;
3474 char name[16];
3475
3476 if (net_hub_id_for_client(ncs[i], &id)) {
3477 continue;
3478 }
3479 snprintf(name, sizeof(name), "%d", id);
3480 if (!strncmp(str, name, len)) {
3481 readline_add_completion(rs, name);
3482 }
3483 }
3484 return;
3485 } else if (nb_args == 3) {
3486 count = qemu_find_net_clients_except(NULL, ncs,
Eric Blakef394b2e2016-07-13 21:50:23 -06003487 NET_CLIENT_DRIVER_NIC,
Jason Wangeaed4832015-04-23 14:21:38 +08003488 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003489 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Jason Wang2c4681f2015-02-02 15:06:38 +08003490 int id;
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003491 const char *name;
3492
Eric Blakef394b2e2016-07-13 21:50:23 -06003493 if (ncs[i]->info->type == NET_CLIENT_DRIVER_HUBPORT ||
Jason Wang2c4681f2015-02-02 15:06:38 +08003494 net_hub_id_for_client(ncs[i], &id)) {
3495 continue;
3496 }
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003497 name = ncs[i]->name;
3498 if (!strncmp(str, name, len)) {
3499 readline_add_completion(rs, name);
3500 }
3501 }
3502 return;
3503 }
3504}
3505
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003506static void vm_completion(ReadLineState *rs, const char *str)
3507{
3508 size_t len;
Kevin Wolf7c8eece2016-03-22 18:58:50 +01003509 BlockDriverState *bs;
Kevin Wolf88be7b42016-05-20 18:49:07 +02003510 BdrvNextIterator it;
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003511
3512 len = strlen(str);
3513 readline_set_completion_index(rs, len);
Kevin Wolf7c8eece2016-03-22 18:58:50 +01003514
Kevin Wolf88be7b42016-05-20 18:49:07 +02003515 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003516 SnapshotInfoList *snapshots, *snapshot;
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003517 AioContext *ctx = bdrv_get_aio_context(bs);
3518 bool ok = false;
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003519
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003520 aio_context_acquire(ctx);
3521 if (bdrv_can_snapshot(bs)) {
3522 ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0;
3523 }
3524 aio_context_release(ctx);
3525 if (!ok) {
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003526 continue;
3527 }
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003528
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003529 snapshot = snapshots;
3530 while (snapshot) {
3531 char *completion = snapshot->value->name;
3532 if (!strncmp(str, completion, len)) {
3533 readline_add_completion(rs, completion);
3534 }
3535 completion = snapshot->value->id;
3536 if (!strncmp(str, completion, len)) {
3537 readline_add_completion(rs, completion);
3538 }
3539 snapshot = snapshot->next;
3540 }
3541 qapi_free_SnapshotInfoList(snapshots);
3542 }
3543
3544}
3545
3546void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
3547{
3548 if (nb_args == 2) {
3549 vm_completion(rs, str);
3550 }
3551}
3552
3553void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
3554{
3555 if (nb_args == 2) {
3556 vm_completion(rs, str);
3557 }
3558}
3559
Wenchao Xiac35b6402013-08-27 20:38:24 +08003560static void monitor_find_completion_by_table(Monitor *mon,
3561 const mon_cmd_t *cmd_table,
3562 char **args,
3563 int nb_args)
bellard81d09122004-07-14 17:21:37 +00003564{
3565 const char *cmdname;
Wenchao Xiac35b6402013-08-27 20:38:24 +08003566 int i;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003567 const char *ptype, *str, *name;
Anthony Liguoric227f092009-10-01 16:12:16 -05003568 const mon_cmd_t *cmd;
Max Reitzda27a002016-03-16 19:54:29 +01003569 BlockBackend *blk = NULL;
bellard81d09122004-07-14 17:21:37 +00003570
bellard81d09122004-07-14 17:21:37 +00003571 if (nb_args <= 1) {
3572 /* command completion */
3573 if (nb_args == 0)
3574 cmdname = "";
3575 else
3576 cmdname = args[0];
Wenchao Xiad2674b22013-08-27 20:38:16 +08003577 readline_set_completion_index(mon->rs, strlen(cmdname));
Wenchao Xiac35b6402013-08-27 20:38:24 +08003578 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003579 cmd_completion(mon, cmdname, cmd->name);
bellard81d09122004-07-14 17:21:37 +00003580 }
3581 } else {
3582 /* find the command */
Wenchao Xiac35b6402013-08-27 20:38:24 +08003583 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Jan Kiszka03a63482010-06-16 00:38:33 +02003584 if (compare_cmd(args[0], cmd->name)) {
3585 break;
3586 }
bellard81d09122004-07-14 17:21:37 +00003587 }
Jan Kiszka03a63482010-06-16 00:38:33 +02003588 if (!cmd->name) {
Wenchao Xiac35b6402013-08-27 20:38:24 +08003589 return;
Jan Kiszka03a63482010-06-16 00:38:33 +02003590 }
3591
Wenchao Xiad903a772013-08-27 20:38:25 +08003592 if (cmd->sub_table) {
3593 /* do the job again */
Stefan Weile7ae7712015-03-08 19:30:01 +01003594 monitor_find_completion_by_table(mon, cmd->sub_table,
3595 &args[1], nb_args - 1);
3596 return;
Wenchao Xiad903a772013-08-27 20:38:25 +08003597 }
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003598 if (cmd->command_completion) {
Stefan Weile7ae7712015-03-08 19:30:01 +01003599 cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
3600 return;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003601 }
Wenchao Xiad903a772013-08-27 20:38:25 +08003602
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003603 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003604 for(i = 0; i < nb_args - 2; i++) {
3605 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003606 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003607 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003608 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003609 }
3610 }
3611 str = args[nb_args - 1];
Kevin Wolf48fe86f2014-11-12 16:24:02 +01003612 while (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02003613 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00003614 }
bellard81d09122004-07-14 17:21:37 +00003615 switch(*ptype) {
3616 case 'F':
3617 /* file completion */
Wenchao Xiad2674b22013-08-27 20:38:16 +08003618 readline_set_completion_index(mon->rs, strlen(str));
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003619 file_completion(mon, str);
bellard81d09122004-07-14 17:21:37 +00003620 break;
3621 case 'B':
3622 /* block device name completion */
Wenchao Xia599a9262013-08-27 20:38:15 +08003623 readline_set_completion_index(mon->rs, strlen(str));
Max Reitzda27a002016-03-16 19:54:29 +01003624 while ((blk = blk_next(blk)) != NULL) {
3625 name = blk_name(blk);
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003626 if (str[0] == '\0' ||
3627 !strncmp(name, str, strlen(str))) {
3628 readline_add_completion(mon->rs, name);
3629 }
3630 }
bellard81d09122004-07-14 17:21:37 +00003631 break;
bellard7fe48482004-10-09 18:08:01 +00003632 case 's':
Wenchao Xia129be002013-08-27 20:38:26 +08003633 case 'S':
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003634 if (!strcmp(cmd->name, "help|?")) {
Wenchao Xia7ca0e062013-08-27 20:38:27 +08003635 monitor_find_completion_by_table(mon, cmd_table,
3636 &args[1], nb_args - 1);
bellard7fe48482004-10-09 18:08:01 +00003637 }
3638 break;
bellard81d09122004-07-14 17:21:37 +00003639 default:
3640 break;
3641 }
3642 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08003643}
3644
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003645static void monitor_find_completion(void *opaque,
Wenchao Xiac35b6402013-08-27 20:38:24 +08003646 const char *cmdline)
3647{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003648 Monitor *mon = opaque;
Wenchao Xiac35b6402013-08-27 20:38:24 +08003649 char *args[MAX_ARGS];
3650 int nb_args, len;
3651
3652 /* 1. parse the cmdline */
3653 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
3654 return;
3655 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08003656
3657 /* if the line ends with a space, it means we want to complete the
3658 next arg */
3659 len = strlen(cmdline);
3660 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
3661 if (nb_args >= MAX_ARGS) {
3662 goto cleanup;
3663 }
3664 args[nb_args++] = g_strdup("");
3665 }
3666
3667 /* 2. auto complete according to args */
3668 monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args);
Jan Kiszka03a63482010-06-16 00:38:33 +02003669
3670cleanup:
Wenchao Xiadcc70cd2013-08-27 20:38:22 +08003671 free_cmdline_args(args, nb_args);
bellard81d09122004-07-14 17:21:37 +00003672}
3673
aliguori731b0362009-03-05 23:01:42 +00003674static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003675{
aliguori731b0362009-03-05 23:01:42 +00003676 Monitor *mon = opaque;
3677
Jan Kiszkac62313b2009-12-04 14:05:29 +01003678 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003679}
3680
Marc-André Lureaud79bedf2016-09-12 13:18:59 +04003681static bool invalid_qmp_mode(const Monitor *mon, const char *cmd,
Markus Armbruster40861822015-05-29 10:27:16 +02003682 Error **errp)
Luiz Capitulino09069b12010-02-04 18:10:06 -02003683{
Marc-André Lureaud79bedf2016-09-12 13:18:59 +04003684 bool is_cap = g_str_equal(cmd, "qmp_capabilities");
Markus Armbrusterf994b252015-03-06 19:51:51 +01003685
3686 if (is_cap && mon->qmp.in_command_mode) {
Markus Armbruster40861822015-05-29 10:27:16 +02003687 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
3688 "Capabilities negotiation is already complete, command "
Marc-André Lureaud79bedf2016-09-12 13:18:59 +04003689 "'%s' ignored", cmd);
Eric Blake2d5a8342015-04-15 09:19:23 -06003690 return true;
3691 }
Markus Armbrusterf994b252015-03-06 19:51:51 +01003692 if (!is_cap && !mon->qmp.in_command_mode) {
Markus Armbruster40861822015-05-29 10:27:16 +02003693 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
3694 "Expecting capabilities negotiation with "
Marc-André Lureaud79bedf2016-09-12 13:18:59 +04003695 "'qmp_capabilities' before command '%s'", cmd);
Eric Blake2d5a8342015-04-15 09:19:23 -06003696 return true;
3697 }
3698 return false;
Luiz Capitulino09069b12010-02-04 18:10:06 -02003699}
3700
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003701/*
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003702 * Input object checking rules
3703 *
3704 * 1. Input object must be a dict
3705 * 2. The "execute" key must exist
3706 * 3. The "execute" key must be a string
3707 * 4. If the "arguments" key exists, it must be a dict
3708 * 5. If the "id" key exists, it can be anything (ie. json-value)
3709 * 6. Any argument not listed above is considered invalid
3710 */
Markus Armbrusterba0510a2015-03-02 18:41:43 +01003711static QDict *qmp_check_input_obj(QObject *input_obj, Error **errp)
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003712{
3713 const QDictEntry *ent;
3714 int has_exec_key = 0;
3715 QDict *input_dict;
3716
Markus Armbrusterca6b6e12017-02-17 21:38:18 +01003717 input_dict = qobject_to_qdict(input_obj);
3718 if (!input_dict) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003719 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "object");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003720 return NULL;
3721 }
3722
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003723
3724 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
3725 const char *arg_name = qdict_entry_key(ent);
3726 const QObject *arg_obj = qdict_entry_value(ent);
3727
3728 if (!strcmp(arg_name, "execute")) {
3729 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003730 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
3731 "execute", "string");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003732 return NULL;
3733 }
3734 has_exec_key = 1;
3735 } else if (!strcmp(arg_name, "arguments")) {
3736 if (qobject_type(arg_obj) != QTYPE_QDICT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003737 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
3738 "arguments", "object");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003739 return NULL;
3740 }
Markus Armbruster779cec42015-06-08 10:44:30 +02003741 } else if (!strcmp(arg_name, "id")) {
3742 /* Any string is acceptable as "id", so nothing to check */
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003743 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003744 error_setg(errp, QERR_QMP_EXTRA_MEMBER, arg_name);
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003745 return NULL;
3746 }
3747 }
3748
3749 if (!has_exec_key) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003750 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "execute");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003751 return NULL;
3752 }
3753
3754 return input_dict;
3755}
3756
Paolo Bonzini95385fe2015-11-25 22:23:31 +01003757static void handle_qmp_command(JSONMessageParser *parser, GQueue *tokens)
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003758{
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04003759 QObject *req, *rsp = NULL, *id = NULL;
3760 QDict *qdict = NULL;
Luiz Capitulino40e5a012011-10-21 16:15:31 -02003761 const char *cmd_name;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003762 Monitor *mon = cur_mon;
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04003763 Error *err = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003764
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04003765 req = json_parser_parse_err(tokens, NULL, &err);
Markus Armbrusterbbf10282017-02-17 21:38:24 +01003766 if (!req && !err) {
3767 /* json_parser_parse_err() sucks: can fail without setting @err */
3768 error_setg(&err, QERR_JSON_PARSING);
3769 }
3770 if (err) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003771 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003772 }
3773
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04003774 qdict = qmp_check_input_obj(req, &err);
3775 if (!qdict) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003776 goto err_out;
3777 }
3778
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04003779 id = qdict_get(qdict, "id");
3780 qobject_incref(id);
3781 qdict_del(qdict, "id");
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003782
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04003783 cmd_name = qdict_get_str(qdict, "execute");
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +01003784 trace_handle_qmp_command(mon, cmd_name);
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04003785
3786 if (invalid_qmp_mode(mon, cmd_name, &err)) {
Eric Blake2d5a8342015-04-15 09:19:23 -06003787 goto err_out;
3788 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003789
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04003790 rsp = qmp_dispatch(req);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003791
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003792err_out:
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04003793 if (err) {
3794 qdict = qdict_new();
3795 qdict_put_obj(qdict, "error", qmp_build_error_object(err));
3796 error_free(err);
3797 rsp = QOBJECT(qdict);
3798 }
3799
3800 if (rsp) {
3801 if (id) {
3802 qdict_put_obj(qobject_to_qdict(rsp), "id", id);
3803 id = NULL;
3804 }
3805
3806 monitor_json_emitter(mon, rsp);
3807 }
3808
3809 qobject_decref(id);
3810 qobject_decref(rsp);
3811 qobject_decref(req);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003812}
3813
Markus Armbrusterc83fe232015-03-06 19:20:51 +01003814static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003815{
3816 Monitor *old_mon = cur_mon;
3817
3818 cur_mon = opaque;
3819
Markus Armbruster74358f22015-03-06 19:35:59 +01003820 json_message_parser_feed(&cur_mon->qmp.parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003821
3822 cur_mon = old_mon;
3823}
3824
aliguori731b0362009-03-05 23:01:42 +00003825static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00003826{
aliguori731b0362009-03-05 23:01:42 +00003827 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00003828 int i;
aliguori376253e2009-03-05 23:01:23 +00003829
aliguori731b0362009-03-05 23:01:42 +00003830 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00003831
aliguoricde76ee2009-03-05 23:01:51 +00003832 if (cur_mon->rs) {
3833 for (i = 0; i < size; i++)
3834 readline_handle_byte(cur_mon->rs, buf[i]);
3835 } else {
3836 if (size == 0 || buf[size - 1] != 0)
3837 monitor_printf(cur_mon, "corrupted command\n");
3838 else
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003839 handle_hmp_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00003840 }
aliguori731b0362009-03-05 23:01:42 +00003841
3842 cur_mon = old_mon;
3843}
aliguorid8f44602008-10-06 13:52:44 +00003844
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003845static void monitor_command_cb(void *opaque, const char *cmdline,
3846 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00003847{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003848 Monitor *mon = opaque;
3849
aliguori731b0362009-03-05 23:01:42 +00003850 monitor_suspend(mon);
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003851 handle_hmp_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00003852 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00003853}
3854
aliguoricde76ee2009-03-05 23:01:51 +00003855int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003856{
aliguoricde76ee2009-03-05 23:01:51 +00003857 if (!mon->rs)
3858 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00003859 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00003860 return 0;
aliguorid8f44602008-10-06 13:52:44 +00003861}
3862
aliguori376253e2009-03-05 23:01:23 +00003863void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003864{
aliguoricde76ee2009-03-05 23:01:51 +00003865 if (!mon->rs)
3866 return;
aliguori731b0362009-03-05 23:01:42 +00003867 if (--mon->suspend_cnt == 0)
3868 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00003869}
3870
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003871static QObject *get_qmp_greeting(void)
3872{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03003873 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003874
Markus Armbruster7fad30f2015-09-16 13:06:19 +02003875 qmp_marshal_query_version(NULL, &ver, NULL);
Marc-André Lureauc8235012016-09-12 13:19:04 +04003876
3877 return qobject_from_jsonf("{'QMP': {'version': %p, 'capabilities': []}}",
3878 ver);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003879}
3880
Markus Armbrusterc83fe232015-03-06 19:20:51 +01003881static void monitor_qmp_event(void *opaque, int event)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003882{
Luiz Capitulino47116d12010-02-08 17:01:30 -02003883 QObject *data;
3884 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003885
Luiz Capitulino47116d12010-02-08 17:01:30 -02003886 switch (event) {
3887 case CHR_EVENT_OPENED:
Markus Armbrusterf994b252015-03-06 19:51:51 +01003888 mon->qmp.in_command_mode = false;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003889 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003890 monitor_json_emitter(mon, data);
3891 qobject_decref(data);
Corey Bryantefb87c12012-08-14 16:43:48 -04003892 mon_refcount++;
Luiz Capitulino47116d12010-02-08 17:01:30 -02003893 break;
3894 case CHR_EVENT_CLOSED:
Markus Armbruster74358f22015-03-06 19:35:59 +01003895 json_message_parser_destroy(&mon->qmp.parser);
3896 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Corey Bryantefb87c12012-08-14 16:43:48 -04003897 mon_refcount--;
3898 monitor_fdsets_cleanup();
Luiz Capitulino47116d12010-02-08 17:01:30 -02003899 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003900 }
3901}
3902
aliguori731b0362009-03-05 23:01:42 +00003903static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00003904{
aliguori376253e2009-03-05 23:01:23 +00003905 Monitor *mon = opaque;
3906
aliguori2724b182009-03-05 23:01:47 +00003907 switch (event) {
3908 case CHR_EVENT_MUX_IN:
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003909 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003910 mon->mux_out = 0;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003911 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003912 if (mon->reset_seen) {
3913 readline_restart(mon->rs);
3914 monitor_resume(mon);
3915 monitor_flush(mon);
3916 } else {
3917 mon->suspend_cnt = 0;
3918 }
aliguori2724b182009-03-05 23:01:47 +00003919 break;
ths86e94de2007-01-05 22:01:59 +00003920
aliguori2724b182009-03-05 23:01:47 +00003921 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003922 if (mon->reset_seen) {
3923 if (mon->suspend_cnt == 0) {
3924 monitor_printf(mon, "\n");
3925 }
3926 monitor_flush(mon);
3927 monitor_suspend(mon);
3928 } else {
3929 mon->suspend_cnt++;
3930 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003931 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003932 mon->mux_out = 1;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003933 qemu_mutex_unlock(&mon->out_lock);
aliguori2724b182009-03-05 23:01:47 +00003934 break;
3935
Amit Shahb6b8df52009-10-07 18:31:16 +05303936 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00003937 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3938 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003939 if (!mon->mux_out) {
Stratos Psomadakise5554e22014-09-15 15:34:57 +03003940 readline_restart(mon->rs);
aliguori2724b182009-03-05 23:01:47 +00003941 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003942 }
3943 mon->reset_seen = 1;
Corey Bryantefb87c12012-08-14 16:43:48 -04003944 mon_refcount++;
3945 break;
3946
3947 case CHR_EVENT_CLOSED:
3948 mon_refcount--;
3949 monitor_fdsets_cleanup();
aliguori2724b182009-03-05 23:01:47 +00003950 break;
3951 }
ths86e94de2007-01-05 22:01:59 +00003952}
3953
Wayne Xia816f8922011-10-12 11:32:41 +08003954static int
3955compare_mon_cmd(const void *a, const void *b)
3956{
3957 return strcmp(((const mon_cmd_t *)a)->name,
3958 ((const mon_cmd_t *)b)->name);
3959}
3960
3961static void sortcmdlist(void)
3962{
3963 int array_num;
3964 int elem_size = sizeof(mon_cmd_t);
3965
3966 array_num = sizeof(mon_cmds)/elem_size-1;
3967 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
3968
3969 array_num = sizeof(info_cmds)/elem_size-1;
3970 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
3971}
3972
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003973/* These functions just adapt the readline interface in a typesafe way. We
3974 * could cast function pointers but that discards compiler checks.
3975 */
Stefan Weild5d15072014-01-25 18:18:23 +01003976static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
3977 const char *fmt, ...)
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003978{
3979 va_list ap;
3980 va_start(ap, fmt);
3981 monitor_vprintf(opaque, fmt, ap);
3982 va_end(ap);
3983}
3984
3985static void monitor_readline_flush(void *opaque)
3986{
3987 monitor_flush(opaque);
3988}
3989
Paolo Bonzini397d30e2016-10-24 18:31:02 +02003990/*
3991 * Print to current monitor if we have one, else to stderr.
3992 * TODO should return int, so callers can calculate width, but that
3993 * requires surgery to monitor_vprintf(). Left for another day.
3994 */
3995void error_vprintf(const char *fmt, va_list ap)
3996{
3997 if (cur_mon && !monitor_cur_is_qmp()) {
3998 monitor_vprintf(cur_mon, fmt, ap);
3999 } else {
4000 vfprintf(stderr, fmt, ap);
4001 }
4002}
4003
4004void error_vprintf_unless_qmp(const char *fmt, va_list ap)
4005{
4006 if (cur_mon && !monitor_cur_is_qmp()) {
4007 monitor_vprintf(cur_mon, fmt, ap);
Marc-André Lureau0d6b50d2017-01-05 14:59:57 +01004008 } else if (!cur_mon) {
4009 vfprintf(stderr, fmt, ap);
Paolo Bonzini397d30e2016-10-24 18:31:02 +02004010 }
4011}
4012
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004013static void __attribute__((constructor)) monitor_lock_init(void)
4014{
4015 qemu_mutex_init(&monitor_lock);
4016}
4017
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03004018void monitor_init(Chardev *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004019{
aliguori731b0362009-03-05 23:01:42 +00004020 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004021 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004022
4023 if (is_first_init) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +02004024 monitor_qapi_event_init();
Wenchao Xiad0383172013-08-27 20:38:18 +08004025 sortcmdlist();
ths20d8a3e2007-02-18 17:04:49 +00004026 is_first_init = 0;
4027 }
aliguori87127162009-03-05 23:01:29 +00004028
Wenchao Xiab01fe892013-08-27 20:38:19 +08004029 mon = g_malloc(sizeof(*mon));
4030 monitor_data_init(mon);
ths20d8a3e2007-02-18 17:04:49 +00004031
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03004032 qemu_chr_fe_init(&mon->chr, chr, &error_abort);
aliguori731b0362009-03-05 23:01:42 +00004033 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004034 if (flags & MONITOR_USE_READLINE) {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004035 mon->rs = readline_init(monitor_readline_printf,
4036 monitor_readline_flush,
4037 mon,
4038 monitor_find_completion);
aliguoricde76ee2009-03-05 23:01:51 +00004039 monitor_read_command(mon, 0);
4040 }
aliguori87127162009-03-05 23:01:29 +00004041
Markus Armbruster9f3982f2015-03-06 19:56:38 +01004042 if (monitor_is_qmp(mon)) {
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03004043 qemu_chr_fe_set_handlers(&mon->chr, monitor_can_read, monitor_qmp_read,
Marc-André Lureau39ab61c2016-10-22 12:53:03 +03004044 monitor_qmp_event, mon, NULL, true);
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03004045 qemu_chr_fe_set_echo(&mon->chr, true);
Markus Armbruster74358f22015-03-06 19:35:59 +01004046 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004047 } else {
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03004048 qemu_chr_fe_set_handlers(&mon->chr, monitor_can_read, monitor_read,
Marc-André Lureau39ab61c2016-10-22 12:53:03 +03004049 monitor_event, mon, NULL, true);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004050 }
aliguori87127162009-03-05 23:01:29 +00004051
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004052 qemu_mutex_lock(&monitor_lock);
Blue Swirl72cf2d42009-09-12 07:36:22 +00004053 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004054 qemu_mutex_unlock(&monitor_lock);
bellard7e2515e2004-08-01 21:52:19 +00004055}
4056
Marc-André Lureau2ef45712016-08-01 15:23:42 +04004057void monitor_cleanup(void)
4058{
4059 Monitor *mon, *next;
4060
4061 qemu_mutex_lock(&monitor_lock);
4062 QLIST_FOREACH_SAFE(mon, &mon_list, entry, next) {
4063 QLIST_REMOVE(mon, entry);
4064 monitor_data_destroy(mon);
4065 g_free(mon);
4066 }
4067 qemu_mutex_unlock(&monitor_lock);
4068}
4069
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004070static void bdrv_password_cb(void *opaque, const char *password,
4071 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00004072{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004073 Monitor *mon = opaque;
4074 BlockDriverState *bs = readline_opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004075 int ret = 0;
Markus Armbruster4d2855a2015-01-29 10:37:00 +01004076 Error *local_err = NULL;
bellard7e2515e2004-08-01 21:52:19 +00004077
Markus Armbruster4d2855a2015-01-29 10:37:00 +01004078 bdrv_add_key(bs, password, &local_err);
4079 if (local_err) {
Markus Armbruster193227f2015-12-18 16:35:06 +01004080 error_report_err(local_err);
aliguoribb5fc202009-03-05 23:01:15 +00004081 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004082 }
aliguori731b0362009-03-05 23:01:42 +00004083 if (mon->password_completion_cb)
4084 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004085
aliguori731b0362009-03-05 23:01:42 +00004086 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004087}
aliguoric0f4ce72009-03-05 23:01:01 +00004088
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004089int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
Markus Armbruster097310b2014-10-07 13:59:15 +02004090 BlockCompletionFunc *completion_cb,
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004091 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004092{
aliguoricde76ee2009-03-05 23:01:51 +00004093 int err;
4094
aliguori376253e2009-03-05 23:01:23 +00004095 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4096 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00004097
aliguori731b0362009-03-05 23:01:42 +00004098 mon->password_completion_cb = completion_cb;
4099 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004100
aliguoricde76ee2009-03-05 23:01:51 +00004101 err = monitor_read_password(mon, bdrv_password_cb, bs);
4102
4103 if (err && completion_cb)
4104 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004105
4106 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00004107}
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004108
4109int monitor_read_block_device_key(Monitor *mon, const char *device,
Markus Armbruster097310b2014-10-07 13:59:15 +02004110 BlockCompletionFunc *completion_cb,
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004111 void *opaque)
4112{
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01004113 Error *err = NULL;
Fam Zheng55606252015-03-02 19:36:46 +08004114 BlockBackend *blk;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004115
Fam Zheng55606252015-03-02 19:36:46 +08004116 blk = blk_by_name(device);
4117 if (!blk) {
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004118 monitor_printf(mon, "Device not found %s\n", device);
4119 return -1;
4120 }
Max Reitz5433c242015-10-19 17:53:29 +02004121 if (!blk_bs(blk)) {
4122 monitor_printf(mon, "Device '%s' has no medium\n", device);
4123 return -1;
4124 }
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004125
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01004126 bdrv_add_key(blk_bs(blk), NULL, &err);
4127 if (err) {
4128 error_free(err);
4129 return monitor_read_bdrv_key_start(mon, blk_bs(blk), completion_cb, opaque);
4130 }
4131
4132 if (completion_cb) {
4133 completion_cb(opaque, 0);
4134 }
4135 return 0;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004136}
Paolo Bonzini4d454572012-11-26 16:03:42 +01004137
4138QemuOptsList qemu_mon_opts = {
4139 .name = "mon",
4140 .implied_opt_name = "chardev",
4141 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
4142 .desc = {
4143 {
4144 .name = "mode",
4145 .type = QEMU_OPT_STRING,
4146 },{
4147 .name = "chardev",
4148 .type = QEMU_OPT_STRING,
4149 },{
Marc-André Lureaubdbcb542016-10-11 21:41:21 +04004150 .name = "default", /* deprecated */
Paolo Bonzini4d454572012-11-26 16:03:42 +01004151 .type = QEMU_OPT_BOOL,
4152 },{
4153 .name = "pretty",
4154 .type = QEMU_OPT_BOOL,
4155 },
4156 { /* end of list */ }
4157 },
4158};
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004159
4160#ifndef TARGET_I386
4161void qmp_rtc_reset_reinjection(Error **errp)
4162{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004163 error_setg(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection");
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004164}
4165#endif
Jason J. Herne7ee0c3e2015-06-26 14:03:16 -04004166
4167#ifndef TARGET_S390X
4168void qmp_dump_skeys(const char *filename, Error **errp)
4169{
4170 error_setg(errp, QERR_FEATURE_DISABLED, "dump-skeys");
4171}
4172#endif
Peter Xuae50a772016-03-30 17:27:24 +01004173
4174#ifndef TARGET_ARM
4175GICCapabilityList *qmp_query_gic_capabilities(Error **errp)
4176{
4177 error_setg(errp, QERR_FEATURE_DISABLED, "query-gic-capabilities");
4178 return NULL;
4179}
4180#endif
Igor Mammedovd4633542016-06-10 06:29:06 +05304181
4182HotpluggableCPUList *qmp_query_hotpluggable_cpus(Error **errp)
4183{
4184 MachineState *ms = MACHINE(qdev_get_machine());
4185 MachineClass *mc = MACHINE_GET_CLASS(ms);
4186
Igor Mammedovc5514d02017-02-10 11:20:57 +01004187 if (!mc->has_hotpluggable_cpus) {
Igor Mammedovd4633542016-06-10 06:29:06 +05304188 error_setg(errp, QERR_FEATURE_DISABLED, "query-hotpluggable-cpus");
4189 return NULL;
4190 }
4191
Igor Mammedovc5514d02017-02-10 11:20:57 +01004192 return machine_query_hotpluggable_cpus(ms);
Igor Mammedovd4633542016-06-10 06:29:06 +05304193}