blob: a27dc8003fa46f228260b145c3e85af227728188 [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"
Paolo Bonzinid2528bd2017-03-03 12:01:16 +010080#include "sysemu/cpus.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020081#include "qemu/cutils.h"
Marc-André Lureauedcfaef2016-09-12 13:19:00 +040082#include "qapi/qmp/dispatch.h"
ths6a5bd302007-12-03 17:05:38 +000083
Jason J. Hernea4538a52015-06-26 14:07:21 -040084#if defined(TARGET_S390X)
85#include "hw/s390x/storage-keys.h"
86#endif
87
bellard9307c4c2004-04-04 12:57:25 +000088/*
89 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000090 *
bellard9307c4c2004-04-04 12:57:25 +000091 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000092 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000093 * 's' string (accept optional quote)
Wenchao Xia129be002013-08-27 20:38:26 +080094 * 'S' it just appends the rest of the string (accept optional quote)
Markus Armbruster361127d2010-02-10 20:24:35 +010095 * 'O' option string of the form NAME=VALUE,...
96 * parsed according to QemuOptsList given by its name
97 * Example: 'device:O' uses qemu_device_opts.
98 * Restriction: only lists with empty desc are supported
99 * TODO lift the restriction
bellard92a31b12005-02-10 22:00:52 +0000100 * 'i' 32 bit integer
101 * 'l' target long (32 or 64 bit)
Luiz Capitulino91162842012-04-26 17:34:30 -0300102 * 'M' Non-negative target long (32 or 64 bit), in user mode the
103 * value is multiplied by 2^20 (think Mebibyte)
Jes Sorensendbc0c672010-10-21 17:15:47 +0200104 * 'o' octets (aka bytes)
Kevin Wolf5e009842013-06-05 14:19:27 +0200105 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
106 * K, k suffix, which multiplies the value by 2^60 for suffixes E
107 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
108 * 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 +0100109 * 'T' double
110 * user mode accepts an optional ms, us, ns suffix,
111 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +0000112 * '/' optional gdb-like print format (like "/10x")
113 *
Luiz Capitulinofb466602009-08-28 15:27:27 -0300114 * '?' optional type (for all types, except '/')
115 * '.' other form of optional type (for 'i' and 'l')
Markus Armbruster942cd1f2010-03-26 09:07:09 +0100116 * 'b' boolean
117 * user mode accepts "on" or "off"
Luiz Capitulinofb466602009-08-28 15:27:27 -0300118 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +0000119 *
120 */
121
Anthony Liguoric227f092009-10-01 16:12:16 -0500122typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +0000123 const char *name;
bellard9307c4c2004-04-04 12:57:25 +0000124 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +0000125 const char *params;
126 const char *help;
Marc-André Lureau2b9e3572016-09-12 13:19:06 +0400127 void (*cmd)(Monitor *mon, const QDict *qdict);
128 /* @sub_table is a list of 2nd level of commands. If it does not exist,
129 * cmd should be used. If it exists, sub_table[?].cmd should be
130 * used, and cmd of 1st level plays the role of help function.
Wenchao Xia5f3d3352013-01-14 14:06:27 +0800131 */
132 struct mon_cmd_t *sub_table;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +0100133 void (*command_completion)(ReadLineState *rs, int nb_args, const char *str);
Anthony Liguoric227f092009-10-01 16:12:16 -0500134} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000135
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100136/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500137typedef struct mon_fd_t mon_fd_t;
138struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100139 char *name;
140 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500141 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100142};
143
Corey Bryantba1c0482012-08-14 16:43:43 -0400144/* file descriptor associated with a file descriptor set */
145typedef struct MonFdsetFd MonFdsetFd;
146struct MonFdsetFd {
147 int fd;
148 bool removed;
149 char *opaque;
150 QLIST_ENTRY(MonFdsetFd) next;
151};
152
153/* file descriptor set containing fds passed via SCM_RIGHTS */
154typedef struct MonFdset MonFdset;
155struct MonFdset {
156 int64_t id;
157 QLIST_HEAD(, MonFdsetFd) fds;
Corey Bryantadb696f2012-08-14 16:43:47 -0400158 QLIST_HEAD(, MonFdsetFd) dup_fds;
Corey Bryantba1c0482012-08-14 16:43:43 -0400159 QLIST_ENTRY(MonFdset) next;
160};
161
Markus Armbruster74358f22015-03-06 19:35:59 +0100162typedef struct {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200163 JSONMessageParser parser;
Markus Armbrusterf994b252015-03-06 19:51:51 +0100164 /*
165 * When a client connects, we're in capabilities negotiation mode.
166 * When command qmp_capabilities succeeds, we go into command
167 * mode.
168 */
Markus Armbruster635db182017-03-03 13:32:27 +0100169 QmpCommandList *commands;
Markus Armbruster74358f22015-03-06 19:35:59 +0100170} MonitorQMP;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200171
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100172/*
173 * To prevent flooding clients, events can be throttled. The
174 * throttling is calculated globally, rather than per-Monitor
175 * instance.
176 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200177typedef struct MonitorQAPIEventState {
Markus Armbruster7de0be62015-10-15 17:08:35 +0200178 QAPIEvent event; /* Throttling state for this event type and... */
179 QDict *data; /* ... data, see qapi_event_throttle_equal() */
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100180 QEMUTimer *timer; /* Timer for handling delayed events */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200181 QDict *qdict; /* Delayed event (if any) */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200182} MonitorQAPIEventState;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100183
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200184typedef struct {
185 int64_t rate; /* Minimum time (in ns) between two events */
186} MonitorQAPIEventConf;
187
aliguori87127162009-03-05 23:01:29 +0000188struct Monitor {
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300189 CharBackend chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200190 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000191 int flags;
192 int suspend_cnt;
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400193 bool skip_flush;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200194
195 QemuMutex out_lock;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400196 QString *outbuf;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200197 guint out_watch;
198
199 /* Read under either BQL or out_lock, written with BQL+out_lock. */
200 int mux_out;
201
aliguori731b0362009-03-05 23:01:42 +0000202 ReadLineState *rs;
Markus Armbruster74358f22015-03-06 19:35:59 +0100203 MonitorQMP qmp;
Andreas Färbercb446ec2013-05-01 14:24:52 +0200204 CPUState *mon_cpu;
Markus Armbruster097310b2014-10-07 13:59:15 +0200205 BlockCompletionFunc *password_completion_cb;
aliguori731b0362009-03-05 23:01:42 +0000206 void *password_opaque;
Wenchao Xia77172392013-08-27 20:38:20 +0800207 mon_cmd_t *cmd_table;
Anthony Liguoric227f092009-10-01 16:12:16 -0500208 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000209 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000210};
211
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300212/* QMP checker flags */
213#define QMP_ACCEPT_UNKNOWNS 1
214
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200215/* Protects mon_list, monitor_event_state. */
216static QemuMutex monitor_lock;
217
Blue Swirl72cf2d42009-09-12 07:36:22 +0000218static QLIST_HEAD(mon_list, Monitor) mon_list;
Corey Bryantba1c0482012-08-14 16:43:43 -0400219static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
Corey Bryantefb87c12012-08-14 16:43:48 -0400220static int mon_refcount;
bellard7e2515e2004-08-01 21:52:19 +0000221
Wayne Xia816f8922011-10-12 11:32:41 +0800222static mon_cmd_t mon_cmds[];
223static mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000224
Markus Armbruster635db182017-03-03 13:32:27 +0100225QmpCommandList qmp_commands, qmp_cap_negotiation_commands;
Markus Armbruster1527bad2017-03-03 13:32:25 +0100226
Markus Armbruster8631b602010-02-18 11:41:55 +0100227Monitor *cur_mon;
aliguori376253e2009-03-05 23:01:23 +0000228
Alberto Garciadc599972016-03-10 13:55:26 +0200229static QEMUClockType event_clock_type = QEMU_CLOCK_REALTIME;
230
Stefan Hajnoczic60bf332013-11-14 11:54:14 +0100231static void monitor_command_cb(void *opaque, const char *cmdline,
232 void *readline_opaque);
aliguori83ab7952008-08-19 14:44:22 +0000233
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100234/**
235 * Is @mon a QMP monitor?
236 */
237static inline bool monitor_is_qmp(const Monitor *mon)
Luiz Capitulino418173c2009-11-26 22:58:51 -0200238{
239 return (mon->flags & MONITOR_USE_CONTROL);
240}
241
Markus Armbruster489653b2015-03-06 20:01:05 +0100242/**
243 * Is the current monitor, if any, a QMP monitor?
244 */
245bool monitor_cur_is_qmp(void)
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100246{
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100247 return cur_mon && monitor_is_qmp(cur_mon);
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100248}
249
Anthony Liguori7060b472011-09-02 12:34:50 -0500250void monitor_read_command(Monitor *mon, int show_prompt)
aliguori731b0362009-03-05 23:01:42 +0000251{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200252 if (!mon->rs)
253 return;
254
aliguori731b0362009-03-05 23:01:42 +0000255 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
256 if (show_prompt)
257 readline_show_prompt(mon->rs);
258}
bellard6a00d602005-11-21 23:25:50 +0000259
Anthony Liguori7060b472011-09-02 12:34:50 -0500260int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
261 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000262{
Markus Armbrusterbcf5d192015-03-12 17:26:46 +0100263 if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000264 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
265 /* prompt is printed on return from the command handler */
266 return 0;
267 } else {
268 monitor_printf(mon, "terminal does not support password prompting\n");
269 return -ENOTTY;
270 }
aliguoribb5fc202009-03-05 23:01:15 +0000271}
272
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200273static void monitor_flush_locked(Monitor *mon);
274
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100275static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
276 void *opaque)
277{
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200278 Monitor *mon = opaque;
279
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200280 qemu_mutex_lock(&mon->out_lock);
281 mon->out_watch = 0;
282 monitor_flush_locked(mon);
283 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100284 return FALSE;
285}
286
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200287/* Called with mon->out_lock held. */
288static void monitor_flush_locked(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000289{
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100290 int rc;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400291 size_t len;
292 const char *buf;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100293
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400294 if (mon->skip_flush) {
295 return;
296 }
297
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400298 buf = qstring_get_str(mon->outbuf);
299 len = qstring_get_length(mon->outbuf);
300
Paolo Bonzinia4cc73d2013-05-31 14:00:27 +0200301 if (len && !mon->mux_out) {
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300302 rc = qemu_chr_fe_write(&mon->chr, (const uint8_t *) buf, len);
Stratos Psomadakis056f49f2014-01-27 12:30:15 +0200303 if ((rc < 0 && errno != EAGAIN) || (rc == len)) {
304 /* all flushed or error */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400305 QDECREF(mon->outbuf);
306 mon->outbuf = qstring_new();
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100307 return;
308 }
309 if (rc > 0) {
Eric Blake3b7c78c2016-05-17 16:00:15 -0600310 /* partial write */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400311 QString *tmp = qstring_from_str(buf + rc);
312 QDECREF(mon->outbuf);
313 mon->outbuf = tmp;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100314 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200315 if (mon->out_watch == 0) {
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300316 mon->out_watch =
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300317 qemu_chr_fe_add_watch(&mon->chr, G_IO_OUT | G_IO_HUP,
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300318 monitor_unblocked, mon);
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200319 }
bellard7e2515e2004-08-01 21:52:19 +0000320 }
321}
322
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200323void monitor_flush(Monitor *mon)
324{
325 qemu_mutex_lock(&mon->out_lock);
326 monitor_flush_locked(mon);
327 qemu_mutex_unlock(&mon->out_lock);
328}
329
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400330/* flush at every end of line */
aliguori376253e2009-03-05 23:01:23 +0000331static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000332{
ths60fe76f2007-12-16 03:02:09 +0000333 char c;
aliguori731b0362009-03-05 23:01:42 +0000334
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200335 qemu_mutex_lock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000336 for(;;) {
337 c = *str++;
338 if (c == '\0')
339 break;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400340 if (c == '\n') {
341 qstring_append_chr(mon->outbuf, '\r');
342 }
343 qstring_append_chr(mon->outbuf, c);
344 if (c == '\n') {
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200345 monitor_flush_locked(mon);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400346 }
bellard7e2515e2004-08-01 21:52:19 +0000347 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200348 qemu_mutex_unlock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000349}
350
aliguori376253e2009-03-05 23:01:23 +0000351void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000352{
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400353 char *buf;
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200354
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200355 if (!mon)
356 return;
357
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100358 if (monitor_is_qmp(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200359 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200360 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200361
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400362 buf = g_strdup_vprintf(fmt, ap);
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200363 monitor_puts(mon, buf);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400364 g_free(buf);
bellard7e2515e2004-08-01 21:52:19 +0000365}
366
aliguori376253e2009-03-05 23:01:23 +0000367void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000368{
369 va_list ap;
370 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000371 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000372 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000373}
374
Pavel Butsykincaf15312015-09-22 16:18:17 +0300375int monitor_fprintf(FILE *stream, const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000376{
377 va_list ap;
378 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000379 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000380 va_end(ap);
381 return 0;
382}
383
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200384static void monitor_json_emitter(Monitor *mon, const QObject *data)
385{
386 QString *json;
387
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200388 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
389 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200390 assert(json != NULL);
391
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200392 qstring_append_chr(json, '\n');
393 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200394
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200395 QDECREF(json);
396}
397
Eric Blake7fb1cf12015-11-18 01:52:57 -0700398static MonitorQAPIEventConf monitor_qapi_event_conf[QAPI_EVENT__MAX] = {
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200399 /* Limit guest-triggerable events to 1 per second */
400 [QAPI_EVENT_RTC_CHANGE] = { 1000 * SCALE_MS },
401 [QAPI_EVENT_WATCHDOG] = { 1000 * SCALE_MS },
402 [QAPI_EVENT_BALLOON_CHANGE] = { 1000 * SCALE_MS },
403 [QAPI_EVENT_QUORUM_REPORT_BAD] = { 1000 * SCALE_MS },
404 [QAPI_EVENT_QUORUM_FAILURE] = { 1000 * SCALE_MS },
405 [QAPI_EVENT_VSERPORT_CHANGE] = { 1000 * SCALE_MS },
406};
407
Markus Armbrustera24712a2015-10-15 17:08:34 +0200408GHashTable *monitor_qapi_event_state;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100409
410/*
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200411 * Emits the event to every monitor instance, @event is only used for trace
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200412 * Called with monitor_lock held.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100413 */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200414static void monitor_qapi_event_emit(QAPIEvent event, QDict *qdict)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100415{
416 Monitor *mon;
417
Markus Armbruster688b4b72015-10-15 17:08:30 +0200418 trace_monitor_protocol_event_emit(event, qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100419 QLIST_FOREACH(mon, &mon_list, entry) {
Markus Armbruster635db182017-03-03 13:32:27 +0100420 if (monitor_is_qmp(mon)
421 && mon->qmp.commands != &qmp_cap_negotiation_commands) {
Markus Armbruster688b4b72015-10-15 17:08:30 +0200422 monitor_json_emitter(mon, QOBJECT(qdict));
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100423 }
424 }
425}
426
Markus Armbrustera24712a2015-10-15 17:08:34 +0200427static void monitor_qapi_event_handler(void *opaque);
428
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100429/*
430 * Queue a new event for emission to Monitor instances,
431 * applying any rate limiting if required.
432 */
433static void
Markus Armbruster688b4b72015-10-15 17:08:30 +0200434monitor_qapi_event_queue(QAPIEvent event, QDict *qdict, Error **errp)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100435{
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200436 MonitorQAPIEventConf *evconf;
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200437 MonitorQAPIEventState *evstate;
Markus Armbruster93f8f982015-10-15 17:08:31 +0200438
Eric Blake7fb1cf12015-11-18 01:52:57 -0700439 assert(event < QAPI_EVENT__MAX);
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200440 evconf = &monitor_qapi_event_conf[event];
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200441 trace_monitor_protocol_event_queue(event, qdict, evconf->rate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100442
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200443 qemu_mutex_lock(&monitor_lock);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200444
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200445 if (!evconf->rate) {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200446 /* Unthrottled event */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200447 monitor_qapi_event_emit(event, qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100448 } else {
Markus Armbruster7de0be62015-10-15 17:08:35 +0200449 QDict *data = qobject_to_qdict(qdict_get(qdict, "data"));
450 MonitorQAPIEventState key = { .event = event, .data = data };
Markus Armbrustera24712a2015-10-15 17:08:34 +0200451
452 evstate = g_hash_table_lookup(monitor_qapi_event_state, &key);
453 assert(!evstate || timer_pending(evstate->timer));
454
455 if (evstate) {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200456 /*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200457 * Timer is pending for (at least) evconf->rate ns after
Markus Armbruster93f8f982015-10-15 17:08:31 +0200458 * last send. Store event for sending when timer fires,
459 * replacing a prior stored event if any.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100460 */
Markus Armbruster93f8f982015-10-15 17:08:31 +0200461 QDECREF(evstate->qdict);
Markus Armbruster688b4b72015-10-15 17:08:30 +0200462 evstate->qdict = qdict;
463 QINCREF(evstate->qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100464 } else {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200465 /*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200466 * Last send was (at least) evconf->rate ns ago.
Markus Armbruster93f8f982015-10-15 17:08:31 +0200467 * Send immediately, and arm the timer to call
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200468 * monitor_qapi_event_handler() in evconf->rate ns. Any
Markus Armbruster93f8f982015-10-15 17:08:31 +0200469 * events arriving before then will be delayed until then.
470 */
Alberto Garciadc599972016-03-10 13:55:26 +0200471 int64_t now = qemu_clock_get_ns(event_clock_type);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200472
Markus Armbruster688b4b72015-10-15 17:08:30 +0200473 monitor_qapi_event_emit(event, qdict);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200474
475 evstate = g_new(MonitorQAPIEventState, 1);
476 evstate->event = event;
Markus Armbruster7de0be62015-10-15 17:08:35 +0200477 evstate->data = data;
478 QINCREF(evstate->data);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200479 evstate->qdict = NULL;
Alberto Garciadc599972016-03-10 13:55:26 +0200480 evstate->timer = timer_new_ns(event_clock_type,
Markus Armbrustera24712a2015-10-15 17:08:34 +0200481 monitor_qapi_event_handler,
482 evstate);
483 g_hash_table_add(monitor_qapi_event_state, evstate);
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200484 timer_mod_ns(evstate->timer, now + evconf->rate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100485 }
486 }
Markus Armbruster93f8f982015-10-15 17:08:31 +0200487
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200488 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100489}
490
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100491/*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200492 * This function runs evconf->rate ns after sending a throttled
Markus Armbruster93f8f982015-10-15 17:08:31 +0200493 * event.
494 * If another event has since been stored, send it.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100495 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200496static void monitor_qapi_event_handler(void *opaque)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100497{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200498 MonitorQAPIEventState *evstate = opaque;
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200499 MonitorQAPIEventConf *evconf = &monitor_qapi_event_conf[evstate->event];
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100500
Markus Armbruster93f8f982015-10-15 17:08:31 +0200501 trace_monitor_protocol_event_handler(evstate->event, evstate->qdict);
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200502 qemu_mutex_lock(&monitor_lock);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200503
Markus Armbruster688b4b72015-10-15 17:08:30 +0200504 if (evstate->qdict) {
Alberto Garciadc599972016-03-10 13:55:26 +0200505 int64_t now = qemu_clock_get_ns(event_clock_type);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200506
Markus Armbruster688b4b72015-10-15 17:08:30 +0200507 monitor_qapi_event_emit(evstate->event, evstate->qdict);
508 QDECREF(evstate->qdict);
509 evstate->qdict = NULL;
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200510 timer_mod_ns(evstate->timer, now + evconf->rate);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200511 } else {
512 g_hash_table_remove(monitor_qapi_event_state, evstate);
Markus Armbruster7de0be62015-10-15 17:08:35 +0200513 QDECREF(evstate->data);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200514 timer_free(evstate->timer);
515 g_free(evstate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100516 }
Markus Armbruster93f8f982015-10-15 17:08:31 +0200517
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200518 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100519}
520
Markus Armbrustera24712a2015-10-15 17:08:34 +0200521static unsigned int qapi_event_throttle_hash(const void *key)
522{
523 const MonitorQAPIEventState *evstate = key;
Markus Armbruster7de0be62015-10-15 17:08:35 +0200524 unsigned int hash = evstate->event * 255;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200525
Markus Armbruster7de0be62015-10-15 17:08:35 +0200526 if (evstate->event == QAPI_EVENT_VSERPORT_CHANGE) {
527 hash += g_str_hash(qdict_get_str(evstate->data, "id"));
528 }
529
Alberto Garcia6d425eb2016-03-10 13:55:25 +0200530 if (evstate->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
531 hash += g_str_hash(qdict_get_str(evstate->data, "node-name"));
532 }
533
Markus Armbruster7de0be62015-10-15 17:08:35 +0200534 return hash;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200535}
536
537static gboolean qapi_event_throttle_equal(const void *a, const void *b)
538{
539 const MonitorQAPIEventState *eva = a;
540 const MonitorQAPIEventState *evb = b;
541
Markus Armbruster7de0be62015-10-15 17:08:35 +0200542 if (eva->event != evb->event) {
543 return FALSE;
544 }
545
546 if (eva->event == QAPI_EVENT_VSERPORT_CHANGE) {
547 return !strcmp(qdict_get_str(eva->data, "id"),
548 qdict_get_str(evb->data, "id"));
549 }
550
Alberto Garcia6d425eb2016-03-10 13:55:25 +0200551 if (eva->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
552 return !strcmp(qdict_get_str(eva->data, "node-name"),
553 qdict_get_str(evb->data, "node-name"));
554 }
555
Markus Armbruster7de0be62015-10-15 17:08:35 +0200556 return TRUE;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200557}
558
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200559static void monitor_qapi_event_init(void)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100560{
Alberto Garciadc599972016-03-10 13:55:26 +0200561 if (qtest_enabled()) {
562 event_clock_type = QEMU_CLOCK_VIRTUAL;
563 }
564
Markus Armbrustera24712a2015-10-15 17:08:34 +0200565 monitor_qapi_event_state = g_hash_table_new(qapi_event_throttle_hash,
566 qapi_event_throttle_equal);
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200567 qmp_event_set_func_emit(monitor_qapi_event_queue);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200568}
569
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100570static void handle_hmp_command(Monitor *mon, const char *cmdline);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200571
Wenchao Xiab01fe892013-08-27 20:38:19 +0800572static void monitor_data_init(Monitor *mon)
573{
574 memset(mon, 0, sizeof(Monitor));
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200575 qemu_mutex_init(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800576 mon->outbuf = qstring_new();
Wenchao Xia77172392013-08-27 20:38:20 +0800577 /* Use *mon_cmds by default. */
578 mon->cmd_table = mon_cmds;
Wenchao Xiab01fe892013-08-27 20:38:19 +0800579}
580
581static void monitor_data_destroy(Monitor *mon)
582{
Marc-André Lureauc39860e2016-10-22 12:52:58 +0300583 qemu_chr_fe_deinit(&mon->chr);
Marc-André Lureau2ef45712016-08-01 15:23:42 +0400584 if (monitor_is_qmp(mon)) {
585 json_message_parser_destroy(&mon->qmp.parser);
586 }
587 g_free(mon->rs);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800588 QDECREF(mon->outbuf);
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200589 qemu_mutex_destroy(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800590}
591
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200592char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
593 int64_t cpu_index, Error **errp)
Luiz Capitulino0268d972010-10-22 10:08:02 -0200594{
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200595 char *output = NULL;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200596 Monitor *old_mon, hmp;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200597
Wenchao Xiab01fe892013-08-27 20:38:19 +0800598 monitor_data_init(&hmp);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400599 hmp.skip_flush = true;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200600
601 old_mon = cur_mon;
602 cur_mon = &hmp;
603
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200604 if (has_cpu_index) {
605 int ret = monitor_set_cpu(cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200606 if (ret < 0) {
607 cur_mon = old_mon;
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100608 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
609 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200610 goto out;
611 }
612 }
613
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100614 handle_hmp_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200615 cur_mon = old_mon;
616
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200617 qemu_mutex_lock(&hmp.out_lock);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400618 if (qstring_get_length(hmp.outbuf) > 0) {
619 output = g_strdup(qstring_get_str(hmp.outbuf));
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200620 } else {
621 output = g_strdup("");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200622 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200623 qemu_mutex_unlock(&hmp.out_lock);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200624
625out:
Wenchao Xiab01fe892013-08-27 20:38:19 +0800626 monitor_data_destroy(&hmp);
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200627 return output;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200628}
629
bellard9dc39cb2004-03-14 21:38:27 +0000630static int compare_cmd(const char *name, const char *list)
631{
632 const char *p, *pstart;
633 int len;
634 len = strlen(name);
635 p = list;
636 for(;;) {
637 pstart = p;
638 p = strchr(p, '|');
639 if (!p)
640 p = pstart + strlen(pstart);
641 if ((p - pstart) == len && !memcmp(pstart, name, len))
642 return 1;
643 if (*p == '\0')
644 break;
645 p++;
646 }
647 return 0;
648}
649
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800650static int get_str(char *buf, int buf_size, const char **pp)
651{
652 const char *p;
653 char *q;
654 int c;
655
656 q = buf;
657 p = *pp;
658 while (qemu_isspace(*p)) {
659 p++;
660 }
661 if (*p == '\0') {
662 fail:
663 *q = '\0';
664 *pp = p;
665 return -1;
666 }
667 if (*p == '\"') {
668 p++;
669 while (*p != '\0' && *p != '\"') {
670 if (*p == '\\') {
671 p++;
672 c = *p++;
673 switch (c) {
674 case 'n':
675 c = '\n';
676 break;
677 case 'r':
678 c = '\r';
679 break;
680 case '\\':
681 case '\'':
682 case '\"':
683 break;
684 default:
Peter Maydell71baf782015-08-19 16:20:19 +0100685 printf("unsupported escape code: '\\%c'\n", c);
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800686 goto fail;
687 }
688 if ((q - buf) < buf_size - 1) {
689 *q++ = c;
690 }
691 } else {
692 if ((q - buf) < buf_size - 1) {
693 *q++ = *p;
694 }
695 p++;
696 }
697 }
698 if (*p != '\"') {
Peter Maydell71baf782015-08-19 16:20:19 +0100699 printf("unterminated string\n");
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800700 goto fail;
701 }
702 p++;
703 } else {
704 while (*p != '\0' && !qemu_isspace(*p)) {
705 if ((q - buf) < buf_size - 1) {
706 *q++ = *p;
707 }
708 p++;
709 }
710 }
711 *q = '\0';
712 *pp = p;
713 return 0;
714}
715
716#define MAX_ARGS 16
717
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800718static void free_cmdline_args(char **args, int nb_args)
719{
720 int i;
721
722 assert(nb_args <= MAX_ARGS);
723
724 for (i = 0; i < nb_args; i++) {
725 g_free(args[i]);
726 }
727
728}
729
730/*
731 * Parse the command line to get valid args.
732 * @cmdline: command line to be parsed.
733 * @pnb_args: location to store the number of args, must NOT be NULL.
734 * @args: location to store the args, which should be freed by caller, must
735 * NOT be NULL.
736 *
737 * Returns 0 on success, negative on failure.
738 *
739 * NOTE: this parser is an approximate form of the real command parser. Number
740 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
741 * return with failure.
742 */
743static int parse_cmdline(const char *cmdline,
744 int *pnb_args, char **args)
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800745{
746 const char *p;
747 int nb_args, ret;
748 char buf[1024];
749
750 p = cmdline;
751 nb_args = 0;
752 for (;;) {
753 while (qemu_isspace(*p)) {
754 p++;
755 }
756 if (*p == '\0') {
757 break;
758 }
759 if (nb_args >= MAX_ARGS) {
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800760 goto fail;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800761 }
762 ret = get_str(buf, sizeof(buf), &p);
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800763 if (ret < 0) {
764 goto fail;
765 }
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800766 args[nb_args] = g_strdup(buf);
767 nb_args++;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800768 }
769 *pnb_args = nb_args;
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800770 return 0;
771
772 fail:
773 free_cmdline_args(args, nb_args);
774 return -1;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800775}
776
Wenchao Xia66855492013-08-27 20:38:23 +0800777static void help_cmd_dump_one(Monitor *mon,
778 const mon_cmd_t *cmd,
779 char **prefix_args,
780 int prefix_args_nb)
781{
782 int i;
783
784 for (i = 0; i < prefix_args_nb; i++) {
785 monitor_printf(mon, "%s ", prefix_args[i]);
786 }
787 monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
788}
789
790/* @args[@arg_index] is the valid command need to find in @cmds */
Anthony Liguoric227f092009-10-01 16:12:16 -0500791static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
Wenchao Xia66855492013-08-27 20:38:23 +0800792 char **args, int nb_args, int arg_index)
bellard9dc39cb2004-03-14 21:38:27 +0000793{
Anthony Liguoric227f092009-10-01 16:12:16 -0500794 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000795
Wenchao Xia66855492013-08-27 20:38:23 +0800796 /* No valid arg need to compare with, dump all in *cmds */
797 if (arg_index >= nb_args) {
798 for (cmd = cmds; cmd->name != NULL; cmd++) {
799 help_cmd_dump_one(mon, cmd, args, arg_index);
800 }
801 return;
802 }
803
804 /* Find one entry to dump */
805 for (cmd = cmds; cmd->name != NULL; cmd++) {
806 if (compare_cmd(args[arg_index], cmd->name)) {
807 if (cmd->sub_table) {
808 /* continue with next arg */
809 help_cmd_dump(mon, cmd->sub_table,
810 args, nb_args, arg_index + 1);
811 } else {
812 help_cmd_dump_one(mon, cmd, args, arg_index);
813 }
814 break;
815 }
bellard9dc39cb2004-03-14 21:38:27 +0000816 }
817}
818
aliguori376253e2009-03-05 23:01:23 +0000819static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000820{
Wenchao Xia66855492013-08-27 20:38:23 +0800821 char *args[MAX_ARGS];
822 int nb_args = 0;
823
824 /* 1. parse user input */
825 if (name) {
826 /* special case for log, directly dump and return */
827 if (!strcmp(name, "log")) {
Peter Maydell38dad9e2013-02-11 16:41:25 +0000828 const QEMULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000829 monitor_printf(mon, "Log items (comma separated):\n");
830 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
Peter Maydell38dad9e2013-02-11 16:41:25 +0000831 for (item = qemu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000832 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000833 }
Wenchao Xia66855492013-08-27 20:38:23 +0800834 return;
835 }
836
837 if (parse_cmdline(name, &nb_args, args) < 0) {
838 return;
bellardf193c792004-03-21 17:06:25 +0000839 }
bellard9dc39cb2004-03-14 21:38:27 +0000840 }
Wenchao Xia66855492013-08-27 20:38:23 +0800841
842 /* 2. dump the contents according to parsed args */
843 help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0);
844
845 free_cmdline_args(args, nb_args);
bellard9dc39cb2004-03-14 21:38:27 +0000846}
847
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300848static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300849{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300850 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300851}
852
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100853static void hmp_trace_event(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530854{
855 const char *tp_name = qdict_get_str(qdict, "name");
856 bool new_state = qdict_get_bool(qdict, "option");
Lluís Vilanova77e2b172016-07-11 12:53:57 +0200857 bool has_vcpu = qdict_haskey(qdict, "vcpu");
858 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
Lluís Vilanova14101d02014-08-25 13:20:03 +0200859 Error *local_err = NULL;
Blue Swirlf871d682010-10-13 19:14:29 +0000860
Lluís Vilanova77e2b172016-07-11 12:53:57 +0200861 if (vcpu < 0) {
862 monitor_printf(mon, "argument vcpu must be positive");
863 return;
864 }
865
866 qmp_trace_event_set_state(tp_name, new_state, true, true, has_vcpu, vcpu, &local_err);
Lluís Vilanova14101d02014-08-25 13:20:03 +0200867 if (local_err) {
Markus Armbruster091e38b2015-02-10 15:15:43 +0100868 error_report_err(local_err);
Blue Swirlf871d682010-10-13 19:14:29 +0000869 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530870}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100871
Michael Rothc45a8162011-10-02 08:44:37 -0500872#ifdef CONFIG_TRACE_SIMPLE
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100873static void hmp_trace_file(Monitor *mon, const QDict *qdict)
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100874{
875 const char *op = qdict_get_try_str(qdict, "op");
876 const char *arg = qdict_get_try_str(qdict, "arg");
877
878 if (!op) {
879 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
880 } else if (!strcmp(op, "on")) {
881 st_set_trace_file_enabled(true);
882 } else if (!strcmp(op, "off")) {
883 st_set_trace_file_enabled(false);
884 } else if (!strcmp(op, "flush")) {
885 st_flush_trace_buffer();
886 } else if (!strcmp(op, "set")) {
887 if (arg) {
888 st_set_trace_file(arg);
889 }
890 } else {
891 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
892 help_cmd(mon, "trace-file");
893 }
894}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530895#endif
896
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100897static void hmp_info_help(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000898{
Luiz Capitulino13c74252009-10-07 13:41:55 -0300899 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000900}
901
Marc-André Lureau9e812b62016-09-12 13:19:05 +0400902static void query_commands_cb(QmpCommand *cmd, void *opaque)
bellard9bc9d1c2004-10-10 15:15:51 +0000903{
Marc-André Lureau9e812b62016-09-12 13:19:05 +0400904 CommandInfoList *info, **list = opaque;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200905
Marc-André Lureau9e812b62016-09-12 13:19:05 +0400906 if (!cmd->enabled) {
907 return;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200908 }
909
Marc-André Lureau9e812b62016-09-12 13:19:05 +0400910 info = g_malloc0(sizeof(*info));
911 info->value = g_malloc0(sizeof(*info->value));
912 info->value->name = g_strdup(cmd->name);
913 info->next = *list;
914 *list = info;
915}
916
917CommandInfoList *qmp_query_commands(Error **errp)
918{
919 CommandInfoList *list = NULL;
920
Markus Armbruster635db182017-03-03 13:32:27 +0100921 qmp_for_each_command(cur_mon->qmp.commands, query_commands_cb, &list);
Marc-André Lureau9e812b62016-09-12 13:19:05 +0400922
923 return list;
thsa36e69d2007-12-02 05:18:19 +0000924}
925
Daniel P. Berrange48608532012-05-21 17:59:51 +0100926EventInfoList *qmp_query_events(Error **errp)
927{
928 EventInfoList *info, *ev_list = NULL;
Wenchao Xia75175172014-06-18 08:43:54 +0200929 QAPIEvent e;
Daniel P. Berrange48608532012-05-21 17:59:51 +0100930
Eric Blake7fb1cf12015-11-18 01:52:57 -0700931 for (e = 0 ; e < QAPI_EVENT__MAX ; e++) {
Wenchao Xia75175172014-06-18 08:43:54 +0200932 const char *event_name = QAPIEvent_lookup[e];
Daniel P. Berrange48608532012-05-21 17:59:51 +0100933 assert(event_name != NULL);
934 info = g_malloc0(sizeof(*info));
935 info->value = g_malloc0(sizeof(*info->value));
936 info->value->name = g_strdup(event_name);
937
938 info->next = ev_list;
939 ev_list = info;
940 }
941
942 return ev_list;
943}
944
Markus Armbruster39a18152015-09-16 13:06:28 +0200945/*
946 * Minor hack: generated marshalling suppressed for this command
947 * ('gen': false in the schema) so we can parse the JSON string
948 * directly into QObject instead of first parsing it with
949 * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
950 * to QObject with generated output marshallers, every time. Instead,
Daniel P. Berrangeb3db2112016-09-30 15:45:27 +0100951 * we do it in test-qobject-input-visitor.c, just to make sure
Markus Armbruster39a18152015-09-16 13:06:28 +0200952 * qapi-introspect.py's output actually conforms to the schema.
953 */
954static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
955 Error **errp)
956{
Markus Armbrusterb0f36b22017-02-28 22:27:02 +0100957 *ret_data = qobject_from_json(qmp_schema_json, &error_abort);
Markus Armbruster39a18152015-09-16 13:06:28 +0200958}
959
Marc-André Lureau5032a162016-09-12 13:19:02 +0400960/*
Marc-André Lureau5032a162016-09-12 13:19:02 +0400961 * We used to define commands in qmp-commands.hx in addition to the
962 * QAPI schema. This permitted defining some of them only in certain
963 * configurations. query-commands has always reflected that (good,
964 * because it lets QMP clients figure out what's actually available),
965 * while query-qmp-schema never did (not so good). This function is a
966 * hack to keep the configuration-specific commands defined exactly as
967 * before, even though qmp-commands.hx is gone.
968 *
969 * FIXME Educate the QAPI schema on configuration-specific commands,
970 * and drop this hack.
971 */
972static void qmp_unregister_commands_hack(void)
973{
974#ifndef CONFIG_SPICE
Markus Armbruster1527bad2017-03-03 13:32:25 +0100975 qmp_unregister_command(&qmp_commands, "query-spice");
Marc-André Lureau5032a162016-09-12 13:19:02 +0400976#endif
977#ifndef TARGET_I386
Markus Armbruster1527bad2017-03-03 13:32:25 +0100978 qmp_unregister_command(&qmp_commands, "rtc-reset-reinjection");
Marc-André Lureau5032a162016-09-12 13:19:02 +0400979#endif
980#ifndef TARGET_S390X
Markus Armbruster1527bad2017-03-03 13:32:25 +0100981 qmp_unregister_command(&qmp_commands, "dump-skeys");
Marc-André Lureau5032a162016-09-12 13:19:02 +0400982#endif
983#ifndef TARGET_ARM
Markus Armbruster1527bad2017-03-03 13:32:25 +0100984 qmp_unregister_command(&qmp_commands, "query-gic-capabilities");
Marc-André Lureau5032a162016-09-12 13:19:02 +0400985#endif
Eduardo Habkostf99fd7c2017-02-22 16:00:28 -0300986#if !defined(TARGET_S390X) && !defined(TARGET_I386)
Markus Armbruster1527bad2017-03-03 13:32:25 +0100987 qmp_unregister_command(&qmp_commands, "query-cpu-model-expansion");
Eduardo Habkostf99fd7c2017-02-22 16:00:28 -0300988#endif
989#if !defined(TARGET_S390X)
Markus Armbruster1527bad2017-03-03 13:32:25 +0100990 qmp_unregister_command(&qmp_commands, "query-cpu-model-baseline");
991 qmp_unregister_command(&qmp_commands, "query-cpu-model-comparison");
Eduardo Habkost728b1422016-10-05 16:49:01 -0300992#endif
993#if !defined(TARGET_PPC) && !defined(TARGET_ARM) && !defined(TARGET_I386) \
994 && !defined(TARGET_S390X)
Markus Armbruster1527bad2017-03-03 13:32:25 +0100995 qmp_unregister_command(&qmp_commands, "query-cpu-definitions");
Eduardo Habkost728b1422016-10-05 16:49:01 -0300996#endif
Marc-André Lureau5032a162016-09-12 13:19:02 +0400997}
998
Markus Armbruster05875682017-03-03 13:32:24 +0100999void monitor_init_qmp_commands(void)
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001000{
Markus Armbruster635db182017-03-03 13:32:27 +01001001 /*
1002 * Two command lists:
1003 * - qmp_commands contains all QMP commands
1004 * - qmp_cap_negotiation_commands contains just
1005 * "qmp_capabilities", to enforce capability negotiation
1006 */
1007
Markus Armbruster1527bad2017-03-03 13:32:25 +01001008 qmp_init_marshal(&qmp_commands);
Markus Armbruster05875682017-03-03 13:32:24 +01001009
Markus Armbruster1527bad2017-03-03 13:32:25 +01001010 qmp_register_command(&qmp_commands, "query-qmp-schema",
1011 qmp_query_qmp_schema,
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001012 QCO_NO_OPTIONS);
Markus Armbruster1527bad2017-03-03 13:32:25 +01001013 qmp_register_command(&qmp_commands, "device_add", qmp_device_add,
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001014 QCO_NO_OPTIONS);
Markus Armbruster1527bad2017-03-03 13:32:25 +01001015 qmp_register_command(&qmp_commands, "netdev_add", qmp_netdev_add,
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001016 QCO_NO_OPTIONS);
Marc-André Lureau5032a162016-09-12 13:19:02 +04001017
Markus Armbruster05875682017-03-03 13:32:24 +01001018 qmp_unregister_commands_hack();
Markus Armbruster635db182017-03-03 13:32:27 +01001019
1020 QTAILQ_INIT(&qmp_cap_negotiation_commands);
1021 qmp_register_command(&qmp_cap_negotiation_commands, "qmp_capabilities",
1022 qmp_marshal_qmp_capabilities, QCO_NO_OPTIONS);
1023}
1024
1025void qmp_qmp_capabilities(Error **errp)
1026{
1027 if (cur_mon->qmp.commands == &qmp_commands) {
1028 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
1029 "Capabilities negotiation is already complete, command "
1030 "ignored");
1031 return;
1032 }
1033
1034 cur_mon->qmp.commands = &qmp_commands;
Marc-André Lureauedcfaef2016-09-12 13:19:00 +04001035}
1036
Luiz Capitulinob025c8b2011-10-06 14:02:57 -03001037/* set the current CPU defined by the user */
1038int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +00001039{
Andreas Färber55e5c282012-12-17 06:18:02 +01001040 CPUState *cpu;
bellard6a00d602005-11-21 23:25:50 +00001041
Andreas Färber1c8bb3c2013-02-15 17:01:09 +01001042 cpu = qemu_get_cpu(cpu_index);
1043 if (cpu == NULL) {
1044 return -1;
bellard6a00d602005-11-21 23:25:50 +00001045 }
Andreas Färbercb446ec2013-05-01 14:24:52 +02001046 cur_mon->mon_cpu = cpu;
Andreas Färber1c8bb3c2013-02-15 17:01:09 +01001047 return 0;
bellard6a00d602005-11-21 23:25:50 +00001048}
1049
Pavel Butsykincaf15312015-09-22 16:18:17 +03001050CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +00001051{
aliguori731b0362009-03-05 23:01:42 +00001052 if (!cur_mon->mon_cpu) {
Thomas Huth854e67f2017-01-13 13:12:35 +01001053 if (!first_cpu) {
1054 return NULL;
1055 }
David Gibson27a83f82016-09-21 15:29:26 +10001056 monitor_set_cpu(first_cpu->cpu_index);
bellard6a00d602005-11-21 23:25:50 +00001057 }
Avi Kivity4c0960c2009-08-17 23:19:53 +03001058 cpu_synchronize_state(cur_mon->mon_cpu);
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001059 return cur_mon->mon_cpu;
1060}
1061
Pavel Butsykinbf957282015-09-10 18:38:59 +03001062CPUArchState *mon_get_cpu_env(void)
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001063{
Thomas Huth854e67f2017-01-13 13:12:35 +01001064 CPUState *cs = mon_get_cpu();
1065
1066 return cs ? cs->env_ptr : NULL;
bellard6a00d602005-11-21 23:25:50 +00001067}
1068
Luiz Capitulino99b77962011-10-24 10:53:44 -02001069int monitor_get_cpu_index(void)
1070{
Thomas Huth854e67f2017-01-13 13:12:35 +01001071 CPUState *cs = mon_get_cpu();
1072
1073 return cs ? cs->cpu_index : UNASSIGNED_CPU_INDEX;
Luiz Capitulino99b77962011-10-24 10:53:44 -02001074}
1075
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001076static void hmp_info_registers(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001077{
Thomas Huth854e67f2017-01-13 13:12:35 +01001078 CPUState *cs = mon_get_cpu();
1079
1080 if (!cs) {
1081 monitor_printf(mon, "No CPU available\n");
1082 return;
1083 }
1084 cpu_dump_state(cs, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +00001085}
1086
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001087static void hmp_info_jit(Monitor *mon, const QDict *qdict)
bellarde3db7222005-01-26 22:00:47 +00001088{
aliguori376253e2009-03-05 23:01:23 +00001089 dump_exec_info((FILE *)mon, monitor_fprintf);
Sebastian Tanase27498be2014-07-25 11:56:33 +02001090 dump_drift_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +00001091}
1092
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001093static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
Max Filippov246ae242014-11-02 11:04:18 +03001094{
1095 dump_opcount_info((FILE *)mon, monitor_fprintf);
1096}
1097
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001098static void hmp_info_history(Monitor *mon, const QDict *qdict)
bellardaa455482004-04-04 13:07:25 +00001099{
1100 int i;
bellard7e2515e2004-08-01 21:52:19 +00001101 const char *str;
ths3b46e622007-09-17 08:09:54 +00001102
aliguoricde76ee2009-03-05 23:01:51 +00001103 if (!mon->rs)
1104 return;
bellard7e2515e2004-08-01 21:52:19 +00001105 i = 0;
1106 for(;;) {
aliguori731b0362009-03-05 23:01:42 +00001107 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +00001108 if (!str)
1109 break;
aliguori376253e2009-03-05 23:01:23 +00001110 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +00001111 i++;
bellardaa455482004-04-04 13:07:25 +00001112 }
1113}
1114
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001115static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
j_mayer76a66252007-03-07 08:32:30 +00001116{
Thomas Huth854e67f2017-01-13 13:12:35 +01001117 CPUState *cs = mon_get_cpu();
1118
1119 if (!cs) {
1120 monitor_printf(mon, "No CPU available\n");
1121 return;
1122 }
1123 cpu_dump_statistics(cs, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +00001124}
j_mayer76a66252007-03-07 08:32:30 +00001125
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001126static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301127{
Lluís Vilanovabd712112016-07-11 12:53:51 +02001128 const char *name = qdict_get_try_str(qdict, "name");
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001129 bool has_vcpu = qdict_haskey(qdict, "vcpu");
1130 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
Lluís Vilanovabd712112016-07-11 12:53:51 +02001131 TraceEventInfoList *events;
Lluís Vilanova14101d02014-08-25 13:20:03 +02001132 TraceEventInfoList *elem;
Lluís Vilanovabd712112016-07-11 12:53:51 +02001133 Error *local_err = NULL;
1134
1135 if (name == NULL) {
1136 name = "*";
1137 }
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001138 if (vcpu < 0) {
1139 monitor_printf(mon, "argument vcpu must be positive");
1140 return;
1141 }
Lluís Vilanovabd712112016-07-11 12:53:51 +02001142
Lluís Vilanova77e2b172016-07-11 12:53:57 +02001143 events = qmp_trace_event_get_state(name, has_vcpu, vcpu, &local_err);
Lluís Vilanovabd712112016-07-11 12:53:51 +02001144 if (local_err) {
1145 error_report_err(local_err);
1146 return;
1147 }
Lluís Vilanova14101d02014-08-25 13:20:03 +02001148
1149 for (elem = events; elem != NULL; elem = elem->next) {
1150 monitor_printf(mon, "%s : state %u\n",
1151 elem->value->name,
1152 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
1153 }
1154 qapi_free_TraceEventInfoList(events);
Prerna Saxena22890ab2010-06-24 17:04:53 +05301155}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301156
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001157void qmp_client_migrate_info(const char *protocol, const char *hostname,
1158 bool has_port, int64_t port,
1159 bool has_tls_port, int64_t tls_port,
1160 bool has_cert_subject, const char *cert_subject,
1161 Error **errp)
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001162{
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001163 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001164 if (!qemu_using_spice(errp)) {
1165 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001166 }
1167
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001168 if (!has_port && !has_tls_port) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001169 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001170 return;
Yonit Halperin6ec5dae2012-03-18 09:42:39 +02001171 }
1172
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001173 if (qemu_spice_migrate_info(hostname,
1174 has_port ? port : -1,
1175 has_tls_port ? tls_port : -1,
1176 cert_subject)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001177 error_setg(errp, QERR_UNDEFINED_ERROR);
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001178 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001179 }
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001180 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001181 }
1182
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001183 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001184}
1185
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001186static void hmp_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001187{
Markus Armbrusterdaa76aa2016-06-15 19:27:16 +02001188 Error *err = NULL;
1189
1190 qemu_set_log_filename(qdict_get_str(qdict, "filename"), &err);
1191 if (err) {
1192 error_report_err(err);
1193 }
pbrooke735b912007-06-30 13:53:24 +00001194}
1195
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001196static void hmp_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001197{
1198 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001199 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001200
bellard9307c4c2004-04-04 12:57:25 +00001201 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001202 mask = 0;
1203 } else {
Peter Maydell4fde1eb2013-02-11 16:41:22 +00001204 mask = qemu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001205 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001206 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001207 return;
1208 }
1209 }
Peter Maydell24537a02013-02-11 16:41:23 +00001210 qemu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00001211}
1212
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001213static void hmp_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001214{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001215 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001216 if (!option || !strcmp(option, "on")) {
1217 singlestep = 1;
1218 } else if (!strcmp(option, "off")) {
1219 singlestep = 0;
1220 } else {
1221 monitor_printf(mon, "unexpected option %s\n", option);
1222 }
1223}
1224
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001225static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001226{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001227 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001228 if (!device)
1229 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1230 if (gdbserver_start(device) < 0) {
1231 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1232 device);
1233 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001234 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001235 } else {
aliguori59030a82009-04-05 18:43:41 +00001236 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1237 device);
bellard8a7ddc32004-03-31 19:00:16 +00001238 }
1239}
1240
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001241static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001242{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001243 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001244 if (select_watchdog_action(action) == -1) {
1245 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1246 }
1247}
1248
aliguori376253e2009-03-05 23:01:23 +00001249static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001250{
aliguori376253e2009-03-05 23:01:23 +00001251 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001252 switch(c) {
1253 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001254 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001255 break;
1256 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001257 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001258 break;
1259 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001260 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001261 break;
1262 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001263 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001264 break;
1265 default:
1266 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001267 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001268 } else {
aliguori376253e2009-03-05 23:01:23 +00001269 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001270 }
1271 break;
1272 }
aliguori376253e2009-03-05 23:01:23 +00001273 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001274}
1275
aliguori376253e2009-03-05 23:01:23 +00001276static void memory_dump(Monitor *mon, int count, int format, int wsize,
Avi Kivitya8170e52012-10-23 12:30:10 +02001277 hwaddr addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001278{
Blue Swirl23842aa2010-01-12 20:27:43 +00001279 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001280 uint8_t buf[16];
1281 uint64_t v;
Thomas Huth854e67f2017-01-13 13:12:35 +01001282 CPUState *cs = mon_get_cpu();
1283
1284 if (!cs && (format == 'i' || !is_physical)) {
1285 monitor_printf(mon, "Can not dump without CPU\n");
1286 return;
1287 }
bellard9307c4c2004-04-04 12:57:25 +00001288
1289 if (format == 'i') {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001290 int flags = 0;
bellard9307c4c2004-04-04 12:57:25 +00001291#ifdef TARGET_I386
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001292 CPUArchState *env = mon_get_cpu_env();
bellard4c27ba22004-04-25 18:05:08 +00001293 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001294 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001295 } else if (wsize == 4) {
1296 flags = 0;
1297 } else {
bellard6a15fd12006-04-12 21:07:07 +00001298 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001299 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001300 if (env) {
1301#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001302 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001303 (env->segs[R_CS].flags & DESC_L_MASK))
1304 flags = 2;
1305 else
1306#endif
1307 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1308 flags = 1;
1309 }
bellard4c27ba22004-04-25 18:05:08 +00001310 }
1311#endif
Tom Musta1c38f842014-04-09 14:53:24 -05001312#ifdef TARGET_PPC
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001313 CPUArchState *env = mon_get_cpu_env();
Tom Musta1c38f842014-04-09 14:53:24 -05001314 flags = msr_le << 16;
1315 flags |= env->bfd_mach;
1316#endif
Thomas Huth854e67f2017-01-13 13:12:35 +01001317 monitor_disas(mon, cs, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001318 return;
1319 }
1320
1321 len = wsize * count;
1322 if (wsize == 1)
1323 line_size = 8;
1324 else
1325 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001326 max_digits = 0;
1327
1328 switch(format) {
1329 case 'o':
1330 max_digits = (wsize * 8 + 2) / 3;
1331 break;
1332 default:
1333 case 'x':
1334 max_digits = (wsize * 8) / 4;
1335 break;
1336 case 'u':
1337 case 'd':
1338 max_digits = (wsize * 8 * 10 + 32) / 33;
1339 break;
1340 case 'c':
1341 wsize = 1;
1342 break;
1343 }
1344
1345 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001346 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001347 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001348 else
aliguori376253e2009-03-05 23:01:23 +00001349 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001350 l = len;
1351 if (l > line_size)
1352 l = line_size;
1353 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001354 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001355 } else {
Thomas Huth854e67f2017-01-13 13:12:35 +01001356 if (cpu_memory_rw_debug(cs, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001357 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001358 break;
1359 }
bellard9307c4c2004-04-04 12:57:25 +00001360 }
ths5fafdf22007-09-16 21:08:06 +00001361 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001362 while (i < l) {
1363 switch(wsize) {
1364 default:
1365 case 1:
Peter Maydell24e60302015-01-20 15:19:32 +00001366 v = ldub_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001367 break;
1368 case 2:
Peter Maydell24e60302015-01-20 15:19:32 +00001369 v = lduw_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001370 break;
1371 case 4:
Peter Maydell24e60302015-01-20 15:19:32 +00001372 v = (uint32_t)ldl_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001373 break;
1374 case 8:
Peter Maydell24e60302015-01-20 15:19:32 +00001375 v = ldq_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001376 break;
1377 }
aliguori376253e2009-03-05 23:01:23 +00001378 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001379 switch(format) {
1380 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001381 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001382 break;
1383 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001384 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001385 break;
1386 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001387 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001388 break;
1389 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001390 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001391 break;
1392 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001393 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001394 break;
1395 }
1396 i += wsize;
1397 }
aliguori376253e2009-03-05 23:01:23 +00001398 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001399 addr += l;
1400 len -= l;
1401 }
1402}
1403
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001404static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001405{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001406 int count = qdict_get_int(qdict, "count");
1407 int format = qdict_get_int(qdict, "format");
1408 int size = qdict_get_int(qdict, "size");
1409 target_long addr = qdict_get_int(qdict, "addr");
1410
aliguori376253e2009-03-05 23:01:23 +00001411 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001412}
1413
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001414static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001415{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001416 int count = qdict_get_int(qdict, "count");
1417 int format = qdict_get_int(qdict, "format");
1418 int size = qdict_get_int(qdict, "size");
Avi Kivitya8170e52012-10-23 12:30:10 +02001419 hwaddr addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001420
aliguori376253e2009-03-05 23:01:23 +00001421 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001422}
1423
Paolo Bonzinie9628442017-04-20 15:30:58 +02001424static void *gpa2hva(MemoryRegion **p_mr, hwaddr addr, Error **errp)
1425{
1426 MemoryRegionSection mrs = memory_region_find(get_system_memory(),
1427 addr, 1);
1428
1429 if (!mrs.mr) {
1430 error_setg(errp, "No memory is mapped at address 0x%" HWADDR_PRIx, addr);
1431 return NULL;
1432 }
1433
1434 if (!memory_region_is_ram(mrs.mr) && !memory_region_is_romd(mrs.mr)) {
1435 error_setg(errp, "Memory at address 0x%" HWADDR_PRIx "is not RAM", addr);
1436 memory_region_unref(mrs.mr);
1437 return NULL;
1438 }
1439
1440 *p_mr = mrs.mr;
1441 return qemu_map_ram_ptr(mrs.mr->ram_block, mrs.offset_within_region);
1442}
1443
1444static void hmp_gpa2hva(Monitor *mon, const QDict *qdict)
1445{
1446 hwaddr addr = qdict_get_int(qdict, "addr");
1447 Error *local_err = NULL;
1448 MemoryRegion *mr = NULL;
1449 void *ptr;
1450
1451 ptr = gpa2hva(&mr, addr, &local_err);
1452 if (local_err) {
1453 error_report_err(local_err);
1454 return;
1455 }
1456
1457 monitor_printf(mon, "Host virtual address for 0x%" HWADDR_PRIx
1458 " (%s) is %p\n",
1459 addr, mr->name, ptr);
1460
1461 memory_region_unref(mr);
1462}
1463
1464#ifdef CONFIG_LINUX
1465static uint64_t vtop(void *ptr, Error **errp)
1466{
1467 uint64_t pinfo;
1468 uint64_t ret = -1;
1469 uintptr_t addr = (uintptr_t) ptr;
1470 uintptr_t pagesize = getpagesize();
1471 off_t offset = addr / pagesize * sizeof(pinfo);
1472 int fd;
1473
1474 fd = open("/proc/self/pagemap", O_RDONLY);
1475 if (fd == -1) {
1476 error_setg_errno(errp, errno, "Cannot open /proc/self/pagemap");
1477 return -1;
1478 }
1479
1480 /* Force copy-on-write if necessary. */
1481 atomic_add((uint8_t *)ptr, 0);
1482
1483 if (pread(fd, &pinfo, sizeof(pinfo), offset) != sizeof(pinfo)) {
1484 error_setg_errno(errp, errno, "Cannot read pagemap");
1485 goto out;
1486 }
1487 if ((pinfo & (1ull << 63)) == 0) {
1488 error_setg(errp, "Page not present");
1489 goto out;
1490 }
1491 ret = ((pinfo & 0x007fffffffffffffull) * pagesize) | (addr & (pagesize - 1));
1492
1493out:
1494 close(fd);
1495 return ret;
1496}
1497
1498static void hmp_gpa2hpa(Monitor *mon, const QDict *qdict)
1499{
1500 hwaddr addr = qdict_get_int(qdict, "addr");
1501 Error *local_err = NULL;
1502 MemoryRegion *mr = NULL;
1503 void *ptr;
1504 uint64_t physaddr;
1505
1506 ptr = gpa2hva(&mr, addr, &local_err);
1507 if (local_err) {
1508 error_report_err(local_err);
1509 return;
1510 }
1511
1512 physaddr = vtop(ptr, &local_err);
1513 if (local_err) {
1514 error_report_err(local_err);
1515 } else {
1516 monitor_printf(mon, "Host physical address for 0x%" HWADDR_PRIx
1517 " (%s) is 0x%" PRIx64 "\n",
1518 addr, mr->name, (uint64_t) physaddr);
1519 }
1520
1521 memory_region_unref(mr);
1522}
1523#endif
1524
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001525static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001526{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001527 int format = qdict_get_int(qdict, "format");
Avi Kivitya8170e52012-10-23 12:30:10 +02001528 hwaddr val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001529
bellard9307c4c2004-04-04 12:57:25 +00001530 switch(format) {
1531 case 'o':
Avi Kivitya8170e52012-10-23 12:30:10 +02001532 monitor_printf(mon, "%#" HWADDR_PRIo, val);
bellard9307c4c2004-04-04 12:57:25 +00001533 break;
1534 case 'x':
Avi Kivitya8170e52012-10-23 12:30:10 +02001535 monitor_printf(mon, "%#" HWADDR_PRIx, val);
bellard9307c4c2004-04-04 12:57:25 +00001536 break;
1537 case 'u':
Avi Kivitya8170e52012-10-23 12:30:10 +02001538 monitor_printf(mon, "%" HWADDR_PRIu, val);
bellard9307c4c2004-04-04 12:57:25 +00001539 break;
1540 default:
1541 case 'd':
Avi Kivitya8170e52012-10-23 12:30:10 +02001542 monitor_printf(mon, "%" HWADDR_PRId, val);
bellard9307c4c2004-04-04 12:57:25 +00001543 break;
1544 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001545 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001546 break;
1547 }
aliguori376253e2009-03-05 23:01:23 +00001548 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001549}
1550
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001551static void hmp_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001552{
1553 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001554 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001555 uint32_t start = qdict_get_int(qdict, "start");
1556 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001557
1558 sum = 0;
1559 for(addr = start; addr < (start + size); addr++) {
Peter Maydell42874d32015-04-26 16:49:24 +01001560 uint8_t val = address_space_ldub(&address_space_memory, addr,
1561 MEMTXATTRS_UNSPECIFIED, NULL);
bellarde4cf1ad2005-06-04 20:15:57 +00001562 /* BSD sum algorithm ('sum' Unix command) */
1563 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001564 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001565 }
aliguori376253e2009-03-05 23:01:23 +00001566 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001567}
1568
bellard13224a82006-07-14 22:03:35 +00001569static int mouse_button_state;
1570
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001571static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001572{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001573 int dx, dy, dz, button;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001574 const char *dx_str = qdict_get_str(qdict, "dx_str");
1575 const char *dy_str = qdict_get_str(qdict, "dy_str");
1576 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001577
bellard13224a82006-07-14 22:03:35 +00001578 dx = strtol(dx_str, NULL, 0);
1579 dy = strtol(dy_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001580 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
1581 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
1582
1583 if (dz_str) {
bellard13224a82006-07-14 22:03:35 +00001584 dz = strtol(dz_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001585 if (dz != 0) {
Gerd Hoffmannf22d0af2016-01-12 12:14:12 +01001586 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001587 qemu_input_queue_btn(NULL, button, true);
1588 qemu_input_event_sync();
1589 qemu_input_queue_btn(NULL, button, false);
1590 }
1591 }
1592 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001593}
1594
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001595static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001596{
Eric Blake7fb1cf12015-11-18 01:52:57 -07001597 static uint32_t bmap[INPUT_BUTTON__MAX] = {
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001598 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1599 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
1600 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1601 };
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001602 int button_state = qdict_get_int(qdict, "button_state");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001603
1604 if (mouse_button_state == button_state) {
1605 return;
1606 }
1607 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1608 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001609 mouse_button_state = button_state;
bellard13224a82006-07-14 22:03:35 +00001610}
1611
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001612static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001613{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001614 int size = qdict_get_int(qdict, "size");
1615 int addr = qdict_get_int(qdict, "addr");
1616 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001617 uint32_t val;
1618 int suffix;
1619
1620 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001621 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001622 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001623 addr++;
1624 }
1625 addr &= 0xffff;
1626
1627 switch(size) {
1628 default:
1629 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001630 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001631 suffix = 'b';
1632 break;
1633 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001634 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001635 suffix = 'w';
1636 break;
1637 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001638 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001639 suffix = 'l';
1640 break;
1641 }
aliguori376253e2009-03-05 23:01:23 +00001642 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1643 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001644}
bellarda3a91a32004-06-04 11:06:21 +00001645
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001646static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001647{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001648 int size = qdict_get_int(qdict, "size");
1649 int addr = qdict_get_int(qdict, "addr");
1650 int val = qdict_get_int(qdict, "val");
1651
Jan Kiszkaf1147842009-07-14 10:20:11 +02001652 addr &= IOPORTS_MASK;
1653
1654 switch (size) {
1655 default:
1656 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001657 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001658 break;
1659 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001660 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001661 break;
1662 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001663 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001664 break;
1665 }
1666}
1667
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001668static void hmp_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001669{
Gongleif1839932014-12-03 18:20:58 +00001670 Error *local_err = NULL;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001671 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001672
Gongleif1839932014-12-03 18:20:58 +00001673 qemu_boot_set(bootdevice, &local_err);
1674 if (local_err) {
Markus Armbruster193227f2015-12-18 16:35:06 +01001675 error_report_err(local_err);
aurel320ecdffb2008-05-04 20:11:34 +00001676 } else {
Gongleif1839932014-12-03 18:20:58 +00001677 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001678 }
1679}
1680
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001681static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
Blue Swirl314e2982011-09-11 20:22:05 +00001682{
Peter Xu57bb40c2017-01-16 16:40:05 +08001683 bool flatview = qdict_get_try_bool(qdict, "flatview", false);
1684
1685 mtree_info((fprintf_function)monitor_printf, mon, flatview);
Blue Swirl314e2982011-09-11 20:22:05 +00001686}
1687
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001688static void hmp_info_numa(Monitor *mon, const QDict *qdict)
aliguori030ea372009-04-21 22:30:47 +00001689{
aliguorib28b6232009-04-22 20:20:29 +00001690 int i;
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001691 CPUState *cpu;
zhanghailiang5b009e42014-11-04 19:49:30 +08001692 uint64_t *node_mem;
aliguori030ea372009-04-21 22:30:47 +00001693
zhanghailiang5b009e42014-11-04 19:49:30 +08001694 node_mem = g_new0(uint64_t, nb_numa_nodes);
1695 query_numa_node_mem(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001696 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1697 for (i = 0; i < nb_numa_nodes; i++) {
1698 monitor_printf(mon, "node %d cpus:", i);
Andreas Färberbdc44642013-06-24 23:50:24 +02001699 CPU_FOREACH(cpu) {
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001700 if (cpu->numa_node == i) {
Andreas Färber55e5c282012-12-17 06:18:02 +01001701 monitor_printf(mon, " %d", cpu->cpu_index);
aliguori030ea372009-04-21 22:30:47 +00001702 }
1703 }
1704 monitor_printf(mon, "\n");
1705 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
zhanghailiang5b009e42014-11-04 19:49:30 +08001706 node_mem[i] >> 20);
aliguori030ea372009-04-21 22:30:47 +00001707 }
zhanghailiang5b009e42014-11-04 19:49:30 +08001708 g_free(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001709}
1710
bellard5f1ce942006-02-08 22:40:15 +00001711#ifdef CONFIG_PROFILER
1712
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001713int64_t tcg_time;
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001714int64_t dev_time;
1715
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001716static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001717{
aliguori376253e2009-03-05 23:01:23 +00001718 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Rutuja Shah73bcb242016-03-21 21:32:30 +05301719 dev_time, dev_time / (double)NANOSECONDS_PER_SECOND);
aliguori376253e2009-03-05 23:01:23 +00001720 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Rutuja Shah73bcb242016-03-21 21:32:30 +05301721 tcg_time, tcg_time / (double)NANOSECONDS_PER_SECOND);
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001722 tcg_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001723 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001724}
1725#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001726static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001727{
aliguori376253e2009-03-05 23:01:23 +00001728 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001729}
1730#endif
1731
bellardec36b692006-07-16 18:57:03 +00001732/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001733static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001734
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001735static void hmp_info_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001736{
1737 int i;
1738 CaptureState *s;
1739
1740 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001741 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001742 s->ops.info (s->opaque);
1743 }
1744}
1745
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001746static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001747{
1748 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001749 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001750 CaptureState *s;
1751
1752 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1753 if (i == n) {
1754 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001755 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001756 g_free (s);
bellardec36b692006-07-16 18:57:03 +00001757 return;
1758 }
1759 }
1760}
1761
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001762static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001763{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001764 const char *path = qdict_get_str(qdict, "path");
1765 int has_freq = qdict_haskey(qdict, "freq");
1766 int freq = qdict_get_try_int(qdict, "freq", -1);
1767 int has_bits = qdict_haskey(qdict, "bits");
1768 int bits = qdict_get_try_int(qdict, "bits", -1);
1769 int has_channels = qdict_haskey(qdict, "nchannels");
1770 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001771 CaptureState *s;
1772
Anthony Liguori7267c092011-08-20 22:09:37 -05001773 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001774
1775 freq = has_freq ? freq : 44100;
1776 bits = has_bits ? bits : 16;
1777 nchannels = has_channels ? nchannels : 2;
1778
1779 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001780 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05001781 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001782 return;
bellardec36b692006-07-16 18:57:03 +00001783 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001784 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001785}
bellardec36b692006-07-16 18:57:03 +00001786
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001787static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001788{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001789 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001790
aliguori76655d62009-03-06 20:27:37 +00001791 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001792 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001793 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001794 return acl;
1795}
aliguori76655d62009-03-06 20:27:37 +00001796
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001797static void hmp_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001798{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001799 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001800 qemu_acl *acl = find_acl(mon, aclname);
1801 qemu_acl_entry *entry;
1802 int i = 0;
1803
1804 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001805 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001806 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00001807 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00001808 i++;
1809 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001810 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001811 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001812 }
1813}
1814
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001815static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001816{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001817 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001818 qemu_acl *acl = find_acl(mon, aclname);
1819
1820 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001821 qemu_acl_reset(acl);
1822 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001823 }
1824}
aliguori76655d62009-03-06 20:27:37 +00001825
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001826static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001827{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001828 const char *aclname = qdict_get_str(qdict, "aclname");
1829 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001830 qemu_acl *acl = find_acl(mon, aclname);
1831
1832 if (acl) {
1833 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001834 acl->defaultDeny = 0;
1835 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001836 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001837 acl->defaultDeny = 1;
1838 monitor_printf(mon, "acl: policy set to 'deny'\n");
1839 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001840 monitor_printf(mon, "acl: unknown policy '%s', "
1841 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001842 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001843 }
1844}
aliguori76655d62009-03-06 20:27:37 +00001845
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001846static void hmp_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001847{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001848 const char *aclname = qdict_get_str(qdict, "aclname");
1849 const char *match = qdict_get_str(qdict, "match");
1850 const char *policy = qdict_get_str(qdict, "policy");
1851 int has_index = qdict_haskey(qdict, "index");
1852 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001853 qemu_acl *acl = find_acl(mon, aclname);
1854 int deny, ret;
1855
1856 if (acl) {
1857 if (strcmp(policy, "allow") == 0) {
1858 deny = 0;
1859 } else if (strcmp(policy, "deny") == 0) {
1860 deny = 1;
1861 } else {
1862 monitor_printf(mon, "acl: unknown policy '%s', "
1863 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001864 return;
1865 }
aliguori28a76be2009-03-06 20:27:40 +00001866 if (has_index)
1867 ret = qemu_acl_insert(acl, deny, match, index);
1868 else
1869 ret = qemu_acl_append(acl, deny, match);
1870 if (ret < 0)
1871 monitor_printf(mon, "acl: unable to add acl entry\n");
1872 else
1873 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001874 }
1875}
aliguori76655d62009-03-06 20:27:37 +00001876
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001877static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001878{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001879 const char *aclname = qdict_get_str(qdict, "aclname");
1880 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001881 qemu_acl *acl = find_acl(mon, aclname);
1882 int ret;
aliguori76655d62009-03-06 20:27:37 +00001883
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001884 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001885 ret = qemu_acl_remove(acl, match);
1886 if (ret < 0)
1887 monitor_printf(mon, "acl: no matching acl entry\n");
1888 else
1889 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001890 }
1891}
1892
Corey Bryant208c9d12012-06-22 14:36:09 -04001893void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001894{
Anthony Liguoric227f092009-10-01 16:12:16 -05001895 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001896 int fd;
1897
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03001898 fd = qemu_chr_fe_get_msgfd(&cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001899 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001900 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryant208c9d12012-06-22 14:36:09 -04001901 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001902 }
1903
1904 if (qemu_isdigit(fdname[0])) {
Stefan Hajnoczi0b9f0e22014-04-24 13:58:18 +02001905 close(fd);
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001906 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
1907 "a name not starting with a digit");
Corey Bryant208c9d12012-06-22 14:36:09 -04001908 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001909 }
1910
Corey Bryant208c9d12012-06-22 14:36:09 -04001911 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001912 if (strcmp(monfd->name, fdname) != 0) {
1913 continue;
1914 }
1915
1916 close(monfd->fd);
1917 monfd->fd = fd;
Corey Bryant208c9d12012-06-22 14:36:09 -04001918 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001919 }
1920
Anthony Liguori7267c092011-08-20 22:09:37 -05001921 monfd = g_malloc0(sizeof(mon_fd_t));
1922 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001923 monfd->fd = fd;
1924
Corey Bryant208c9d12012-06-22 14:36:09 -04001925 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001926}
1927
Corey Bryant208c9d12012-06-22 14:36:09 -04001928void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001929{
Anthony Liguoric227f092009-10-01 16:12:16 -05001930 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001931
Corey Bryant208c9d12012-06-22 14:36:09 -04001932 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001933 if (strcmp(monfd->name, fdname) != 0) {
1934 continue;
1935 }
1936
Blue Swirl72cf2d42009-09-12 07:36:22 +00001937 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001938 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001939 g_free(monfd->name);
1940 g_free(monfd);
Corey Bryant208c9d12012-06-22 14:36:09 -04001941 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001942 }
1943
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001944 error_setg(errp, QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001945}
1946
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001947static void hmp_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001948{
Luiz Capitulino13548692011-07-29 15:36:43 -03001949 int saved_vm_running = runstate_is_running();
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001950 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02001951
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001952 vm_stop(RUN_STATE_RESTORE_VM);
Juan Quintelac8d41b22009-08-20 19:42:21 +02001953
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03001954 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02001955 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03001956 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02001957}
1958
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001959int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
Mark McLoughlin7768e042009-07-22 09:11:41 +01001960{
Anthony Liguoric227f092009-10-01 16:12:16 -05001961 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01001962
Blue Swirl72cf2d42009-09-12 07:36:22 +00001963 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01001964 int fd;
1965
1966 if (strcmp(monfd->name, fdname) != 0) {
1967 continue;
1968 }
1969
1970 fd = monfd->fd;
1971
1972 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001973 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05001974 g_free(monfd->name);
1975 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001976
1977 return fd;
1978 }
1979
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001980 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001981 return -1;
1982}
1983
Corey Bryantba1c0482012-08-14 16:43:43 -04001984static void monitor_fdset_cleanup(MonFdset *mon_fdset)
1985{
1986 MonFdsetFd *mon_fdset_fd;
1987 MonFdsetFd *mon_fdset_fd_next;
1988
1989 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantebe52b52012-10-18 15:19:33 -04001990 if ((mon_fdset_fd->removed ||
1991 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
1992 runstate_is_running()) {
Corey Bryantba1c0482012-08-14 16:43:43 -04001993 close(mon_fdset_fd->fd);
1994 g_free(mon_fdset_fd->opaque);
1995 QLIST_REMOVE(mon_fdset_fd, next);
1996 g_free(mon_fdset_fd);
1997 }
1998 }
1999
Corey Bryantadb696f2012-08-14 16:43:47 -04002000 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002001 QLIST_REMOVE(mon_fdset, next);
2002 g_free(mon_fdset);
2003 }
2004}
2005
Corey Bryantefb87c12012-08-14 16:43:48 -04002006static void monitor_fdsets_cleanup(void)
2007{
2008 MonFdset *mon_fdset;
2009 MonFdset *mon_fdset_next;
2010
2011 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
2012 monitor_fdset_cleanup(mon_fdset);
2013 }
2014}
2015
Corey Bryantba1c0482012-08-14 16:43:43 -04002016AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
2017 const char *opaque, Error **errp)
2018{
2019 int fd;
2020 Monitor *mon = cur_mon;
Corey Bryantba1c0482012-08-14 16:43:43 -04002021 AddfdInfo *fdinfo;
2022
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03002023 fd = qemu_chr_fe_get_msgfd(&mon->chr);
Corey Bryantba1c0482012-08-14 16:43:43 -04002024 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002025 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryantba1c0482012-08-14 16:43:43 -04002026 goto error;
2027 }
2028
Corey Bryante446f702012-10-18 15:19:32 -04002029 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
2030 has_opaque, opaque, errp);
2031 if (fdinfo) {
2032 return fdinfo;
Corey Bryant9ac54af2012-10-18 15:19:31 -04002033 }
2034
Corey Bryantba1c0482012-08-14 16:43:43 -04002035error:
2036 if (fd != -1) {
2037 close(fd);
2038 }
2039 return NULL;
2040}
2041
2042void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
2043{
2044 MonFdset *mon_fdset;
2045 MonFdsetFd *mon_fdset_fd;
2046 char fd_str[60];
2047
2048 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2049 if (mon_fdset->id != fdset_id) {
2050 continue;
2051 }
2052 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2053 if (has_fd) {
2054 if (mon_fdset_fd->fd != fd) {
2055 continue;
2056 }
2057 mon_fdset_fd->removed = true;
2058 break;
2059 } else {
2060 mon_fdset_fd->removed = true;
2061 }
2062 }
2063 if (has_fd && !mon_fdset_fd) {
2064 goto error;
2065 }
2066 monitor_fdset_cleanup(mon_fdset);
2067 return;
2068 }
2069
2070error:
2071 if (has_fd) {
2072 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
2073 fdset_id, fd);
2074 } else {
2075 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
2076 }
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002077 error_setg(errp, QERR_FD_NOT_FOUND, fd_str);
Corey Bryantba1c0482012-08-14 16:43:43 -04002078}
2079
2080FdsetInfoList *qmp_query_fdsets(Error **errp)
2081{
2082 MonFdset *mon_fdset;
2083 MonFdsetFd *mon_fdset_fd;
2084 FdsetInfoList *fdset_list = NULL;
2085
2086 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2087 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
2088 FdsetFdInfoList *fdsetfd_list = NULL;
2089
2090 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
2091 fdset_info->value->fdset_id = mon_fdset->id;
2092
2093 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2094 FdsetFdInfoList *fdsetfd_info;
2095
2096 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
2097 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
2098 fdsetfd_info->value->fd = mon_fdset_fd->fd;
2099 if (mon_fdset_fd->opaque) {
2100 fdsetfd_info->value->has_opaque = true;
2101 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
2102 } else {
2103 fdsetfd_info->value->has_opaque = false;
2104 }
2105
2106 fdsetfd_info->next = fdsetfd_list;
2107 fdsetfd_list = fdsetfd_info;
2108 }
2109
2110 fdset_info->value->fds = fdsetfd_list;
2111
2112 fdset_info->next = fdset_list;
2113 fdset_list = fdset_info;
2114 }
2115
2116 return fdset_list;
2117}
2118
Corey Bryante446f702012-10-18 15:19:32 -04002119AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
2120 bool has_opaque, const char *opaque,
2121 Error **errp)
2122{
2123 MonFdset *mon_fdset = NULL;
2124 MonFdsetFd *mon_fdset_fd;
2125 AddfdInfo *fdinfo;
2126
2127 if (has_fdset_id) {
2128 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2129 /* Break if match found or match impossible due to ordering by ID */
2130 if (fdset_id <= mon_fdset->id) {
2131 if (fdset_id < mon_fdset->id) {
2132 mon_fdset = NULL;
2133 }
2134 break;
2135 }
2136 }
2137 }
2138
2139 if (mon_fdset == NULL) {
2140 int64_t fdset_id_prev = -1;
2141 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
2142
2143 if (has_fdset_id) {
2144 if (fdset_id < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002145 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
2146 "a non-negative value");
Corey Bryante446f702012-10-18 15:19:32 -04002147 return NULL;
2148 }
2149 /* Use specified fdset ID */
2150 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2151 mon_fdset_cur = mon_fdset;
2152 if (fdset_id < mon_fdset_cur->id) {
2153 break;
2154 }
2155 }
2156 } else {
2157 /* Use first available fdset ID */
2158 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2159 mon_fdset_cur = mon_fdset;
2160 if (fdset_id_prev == mon_fdset_cur->id - 1) {
2161 fdset_id_prev = mon_fdset_cur->id;
2162 continue;
2163 }
2164 break;
2165 }
2166 }
2167
2168 mon_fdset = g_malloc0(sizeof(*mon_fdset));
2169 if (has_fdset_id) {
2170 mon_fdset->id = fdset_id;
2171 } else {
2172 mon_fdset->id = fdset_id_prev + 1;
2173 }
2174
2175 /* The fdset list is ordered by fdset ID */
2176 if (!mon_fdset_cur) {
2177 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
2178 } else if (mon_fdset->id < mon_fdset_cur->id) {
2179 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
2180 } else {
2181 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
2182 }
2183 }
2184
2185 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
2186 mon_fdset_fd->fd = fd;
2187 mon_fdset_fd->removed = false;
2188 if (has_opaque) {
2189 mon_fdset_fd->opaque = g_strdup(opaque);
2190 }
2191 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
2192
2193 fdinfo = g_malloc0(sizeof(*fdinfo));
2194 fdinfo->fdset_id = mon_fdset->id;
2195 fdinfo->fd = mon_fdset_fd->fd;
2196
2197 return fdinfo;
2198}
2199
Corey Bryantadb696f2012-08-14 16:43:47 -04002200int monitor_fdset_get_fd(int64_t fdset_id, int flags)
2201{
Blue Swirlb2dc64c2012-08-18 20:14:54 +00002202#ifndef _WIN32
Corey Bryantadb696f2012-08-14 16:43:47 -04002203 MonFdset *mon_fdset;
2204 MonFdsetFd *mon_fdset_fd;
2205 int mon_fd_flags;
2206
Corey Bryantadb696f2012-08-14 16:43:47 -04002207 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2208 if (mon_fdset->id != fdset_id) {
2209 continue;
2210 }
2211 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2212 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
2213 if (mon_fd_flags == -1) {
2214 return -1;
2215 }
2216
2217 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
2218 return mon_fdset_fd->fd;
2219 }
2220 }
2221 errno = EACCES;
2222 return -1;
2223 }
2224#endif
2225
2226 errno = ENOENT;
2227 return -1;
2228}
2229
2230int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
2231{
2232 MonFdset *mon_fdset;
2233 MonFdsetFd *mon_fdset_fd_dup;
2234
2235 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2236 if (mon_fdset->id != fdset_id) {
2237 continue;
2238 }
2239 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2240 if (mon_fdset_fd_dup->fd == dup_fd) {
2241 return -1;
2242 }
2243 }
2244 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2245 mon_fdset_fd_dup->fd = dup_fd;
2246 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
2247 return 0;
2248 }
2249 return -1;
2250}
2251
2252static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2253{
2254 MonFdset *mon_fdset;
2255 MonFdsetFd *mon_fdset_fd_dup;
2256
2257 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2258 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2259 if (mon_fdset_fd_dup->fd == dup_fd) {
2260 if (remove) {
2261 QLIST_REMOVE(mon_fdset_fd_dup, next);
2262 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2263 monitor_fdset_cleanup(mon_fdset);
2264 }
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002265 return -1;
2266 } else {
2267 return mon_fdset->id;
Corey Bryantadb696f2012-08-14 16:43:47 -04002268 }
Corey Bryantadb696f2012-08-14 16:43:47 -04002269 }
2270 }
2271 }
2272 return -1;
2273}
2274
2275int monitor_fdset_dup_fd_find(int dup_fd)
2276{
2277 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2278}
2279
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002280void monitor_fdset_dup_fd_remove(int dup_fd)
Corey Bryantadb696f2012-08-14 16:43:47 -04002281{
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002282 monitor_fdset_dup_fd_find_remove(dup_fd, true);
Corey Bryantadb696f2012-08-14 16:43:47 -04002283}
2284
Markus Armbruster1677f4c2015-02-09 14:03:19 +01002285int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
Laszlo Ersek59063662014-04-10 10:24:31 +02002286{
2287 int fd;
2288 Error *local_err = NULL;
2289
2290 if (!qemu_isdigit(fdname[0]) && mon) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002291 fd = monitor_get_fd(mon, fdname, &local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002292 } else {
2293 fd = qemu_parse_fd(fdname);
Laszlo Ersek59063662014-04-10 10:24:31 +02002294 if (fd == -1) {
2295 error_setg(&local_err, "Invalid file descriptor number '%s'",
2296 fdname);
2297 }
2298 }
2299 if (local_err) {
2300 error_propagate(errp, local_err);
2301 assert(fd == -1);
2302 } else {
2303 assert(fd != -1);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002304 }
2305
2306 return fd;
2307}
2308
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002309/* Please update hmp-commands.hx when adding or changing commands */
Wayne Xia816f8922011-10-12 11:32:41 +08002310static mon_cmd_t info_cmds[] = {
Pavel Butsykinda76ee72015-09-10 18:38:58 +03002311#include "hmp-commands-info.h"
2312 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002313};
2314
Wenchao Xiaa13ced52013-01-14 14:06:28 +08002315/* mon_cmds and info_cmds would be sorted at runtime */
2316static mon_cmd_t mon_cmds[] = {
2317#include "hmp-commands.h"
2318 { NULL, NULL, },
2319};
2320
bellard9307c4c2004-04-04 12:57:25 +00002321/*******************************************************************/
2322
2323static const char *pch;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002324static sigjmp_buf expr_env;
bellard9307c4c2004-04-04 12:57:25 +00002325
bellard9307c4c2004-04-04 12:57:25 +00002326
Stefan Weil9c3175c2013-08-22 21:30:09 +02002327static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
2328expr_error(Monitor *mon, const char *fmt, ...)
bellard9dc39cb2004-03-14 21:38:27 +00002329{
Fam Zheng277acfe2013-08-20 10:58:21 +08002330 va_list ap;
2331 va_start(ap, fmt);
2332 monitor_vprintf(mon, fmt, ap);
2333 monitor_printf(mon, "\n");
2334 va_end(ap);
Peter Maydell6ab7e542013-02-20 15:21:09 +00002335 siglongjmp(expr_env, 1);
bellard9307c4c2004-04-04 12:57:25 +00002336}
2337
Markus Armbruster09b94182010-01-20 13:07:30 +01002338/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00002339static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002340{
Pavel Butsykinbf957282015-09-10 18:38:59 +03002341 const MonitorDef *md = target_monitor_defs();
Thomas Huth854e67f2017-01-13 13:12:35 +01002342 CPUState *cs = mon_get_cpu();
bellard92a31b12005-02-10 22:00:52 +00002343 void *ptr;
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002344 uint64_t tmp = 0;
2345 int ret;
bellard92a31b12005-02-10 22:00:52 +00002346
Thomas Huth854e67f2017-01-13 13:12:35 +01002347 if (cs == NULL || md == NULL) {
Pavel Butsykinbf957282015-09-10 18:38:59 +03002348 return -1;
2349 }
2350
2351 for(; md->name != NULL; md++) {
bellard9307c4c2004-04-04 12:57:25 +00002352 if (compare_cmd(name, md->name)) {
2353 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002354 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002355 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002356 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00002357 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002358 switch(md->type) {
2359 case MD_I32:
2360 *pval = *(int32_t *)ptr;
2361 break;
2362 case MD_TLONG:
2363 *pval = *(target_long *)ptr;
2364 break;
2365 default:
2366 *pval = 0;
2367 break;
2368 }
bellard9307c4c2004-04-04 12:57:25 +00002369 }
2370 return 0;
2371 }
2372 }
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002373
Thomas Huth854e67f2017-01-13 13:12:35 +01002374 ret = target_get_monitor_def(cs, name, &tmp);
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002375 if (!ret) {
2376 *pval = (target_long) tmp;
2377 }
2378
2379 return ret;
bellard9307c4c2004-04-04 12:57:25 +00002380}
2381
2382static void next(void)
2383{
Blue Swirl660f11b2009-07-31 21:16:51 +00002384 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002385 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002386 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002387 pch++;
2388 }
2389}
2390
aliguori376253e2009-03-05 23:01:23 +00002391static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002392
aliguori376253e2009-03-05 23:01:23 +00002393static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002394{
blueswir1c2efc952007-09-25 17:28:42 +00002395 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002396 char *p;
bellard6a00d602005-11-21 23:25:50 +00002397 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002398
2399 switch(*pch) {
2400 case '+':
2401 next();
aliguori376253e2009-03-05 23:01:23 +00002402 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002403 break;
2404 case '-':
2405 next();
aliguori376253e2009-03-05 23:01:23 +00002406 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002407 break;
2408 case '~':
2409 next();
aliguori376253e2009-03-05 23:01:23 +00002410 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002411 break;
2412 case '(':
2413 next();
aliguori376253e2009-03-05 23:01:23 +00002414 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002415 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002416 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002417 }
2418 next();
2419 break;
bellard81d09122004-07-14 17:21:37 +00002420 case '\'':
2421 pch++;
2422 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002423 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002424 n = *pch;
2425 pch++;
2426 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002427 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002428 next();
2429 break;
bellard9307c4c2004-04-04 12:57:25 +00002430 case '$':
2431 {
2432 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002433 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002434
bellard9307c4c2004-04-04 12:57:25 +00002435 pch++;
2436 q = buf;
2437 while ((*pch >= 'a' && *pch <= 'z') ||
2438 (*pch >= 'A' && *pch <= 'Z') ||
2439 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002440 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002441 if ((q - buf) < sizeof(buf) - 1)
2442 *q++ = *pch;
2443 pch++;
2444 }
blueswir1cd390082008-11-16 13:53:32 +00002445 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002446 pch++;
2447 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002448 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01002449 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00002450 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00002451 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002452 }
2453 break;
2454 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002455 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002456 n = 0;
2457 break;
2458 default:
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002459 errno = 0;
bellard4f4fbf72006-06-25 18:28:12 +00002460 n = strtoull(pch, &p, 0);
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002461 if (errno == ERANGE) {
2462 expr_error(mon, "number too large");
2463 }
bellard9307c4c2004-04-04 12:57:25 +00002464 if (pch == p) {
Fam Zheng277acfe2013-08-20 10:58:21 +08002465 expr_error(mon, "invalid char '%c' in expression", *p);
bellard9307c4c2004-04-04 12:57:25 +00002466 }
2467 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002468 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002469 pch++;
2470 break;
2471 }
2472 return n;
2473}
2474
2475
aliguori376253e2009-03-05 23:01:23 +00002476static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002477{
blueswir1c2efc952007-09-25 17:28:42 +00002478 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002479 int op;
ths3b46e622007-09-17 08:09:54 +00002480
aliguori376253e2009-03-05 23:01:23 +00002481 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002482 for(;;) {
2483 op = *pch;
2484 if (op != '*' && op != '/' && op != '%')
2485 break;
2486 next();
aliguori376253e2009-03-05 23:01:23 +00002487 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002488 switch(op) {
2489 default:
2490 case '*':
2491 val *= val2;
2492 break;
2493 case '/':
2494 case '%':
ths5fafdf22007-09-16 21:08:06 +00002495 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002496 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002497 if (op == '/')
2498 val /= val2;
2499 else
2500 val %= val2;
2501 break;
2502 }
2503 }
2504 return val;
2505}
2506
aliguori376253e2009-03-05 23:01:23 +00002507static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002508{
blueswir1c2efc952007-09-25 17:28:42 +00002509 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002510 int op;
bellard9307c4c2004-04-04 12:57:25 +00002511
aliguori376253e2009-03-05 23:01:23 +00002512 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002513 for(;;) {
2514 op = *pch;
2515 if (op != '&' && op != '|' && op != '^')
2516 break;
2517 next();
aliguori376253e2009-03-05 23:01:23 +00002518 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002519 switch(op) {
2520 default:
2521 case '&':
2522 val &= val2;
2523 break;
2524 case '|':
2525 val |= val2;
2526 break;
2527 case '^':
2528 val ^= val2;
2529 break;
2530 }
2531 }
2532 return val;
2533}
2534
aliguori376253e2009-03-05 23:01:23 +00002535static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002536{
blueswir1c2efc952007-09-25 17:28:42 +00002537 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002538 int op;
bellard9307c4c2004-04-04 12:57:25 +00002539
aliguori376253e2009-03-05 23:01:23 +00002540 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002541 for(;;) {
2542 op = *pch;
2543 if (op != '+' && op != '-')
2544 break;
2545 next();
aliguori376253e2009-03-05 23:01:23 +00002546 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002547 if (op == '+')
2548 val += val2;
2549 else
2550 val -= val2;
2551 }
2552 return val;
2553}
2554
aliguori376253e2009-03-05 23:01:23 +00002555static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002556{
2557 pch = *pp;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002558 if (sigsetjmp(expr_env, 0)) {
bellard9307c4c2004-04-04 12:57:25 +00002559 *pp = pch;
2560 return -1;
2561 }
blueswir1cd390082008-11-16 13:53:32 +00002562 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002563 pch++;
aliguori376253e2009-03-05 23:01:23 +00002564 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002565 *pp = pch;
2566 return 0;
2567}
2568
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002569static int get_double(Monitor *mon, double *pval, const char **pp)
2570{
2571 const char *p = *pp;
2572 char *tailp;
2573 double d;
2574
2575 d = strtod(p, &tailp);
2576 if (tailp == p) {
2577 monitor_printf(mon, "Number expected\n");
2578 return -1;
2579 }
2580 if (d != d || d - d != 0) {
2581 /* NaN or infinity */
2582 monitor_printf(mon, "Bad number\n");
2583 return -1;
2584 }
2585 *pval = d;
2586 *pp = tailp;
2587 return 0;
2588}
2589
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002590/*
2591 * Store the command-name in cmdname, and return a pointer to
2592 * the remaining of the command string.
2593 */
2594static const char *get_command_name(const char *cmdline,
2595 char *cmdname, size_t nlen)
2596{
2597 size_t len;
2598 const char *p, *pstart;
2599
2600 p = cmdline;
2601 while (qemu_isspace(*p))
2602 p++;
2603 if (*p == '\0')
2604 return NULL;
2605 pstart = p;
2606 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2607 p++;
2608 len = p - pstart;
2609 if (len > nlen - 1)
2610 len = nlen - 1;
2611 memcpy(cmdname, pstart, len);
2612 cmdname[len] = '\0';
2613 return p;
2614}
2615
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002616/**
2617 * Read key of 'type' into 'key' and return the current
2618 * 'type' pointer.
2619 */
2620static char *key_get_info(const char *type, char **key)
2621{
2622 size_t len;
2623 char *p, *str;
2624
2625 if (*type == ',')
2626 type++;
2627
2628 p = strchr(type, ':');
2629 if (!p) {
2630 *key = NULL;
2631 return NULL;
2632 }
2633 len = p - type;
2634
Anthony Liguori7267c092011-08-20 22:09:37 -05002635 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002636 memcpy(str, type, len);
2637 str[len] = '\0';
2638
2639 *key = str;
2640 return ++p;
2641}
2642
bellard9307c4c2004-04-04 12:57:25 +00002643static int default_fmt_format = 'x';
2644static int default_fmt_size = 4;
2645
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002646static int is_valid_option(const char *c, const char *typestr)
2647{
2648 char option[3];
2649
2650 option[0] = '-';
2651 option[1] = *c;
2652 option[2] = '\0';
2653
2654 typestr = strstr(typestr, option);
2655 return (typestr != NULL);
2656}
2657
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03002658static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
2659 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002660{
2661 const mon_cmd_t *cmd;
2662
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03002663 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002664 if (compare_cmd(cmdname, cmd->name)) {
2665 return cmd;
2666 }
2667 }
2668
2669 return NULL;
2670}
2671
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002672/*
Bandan Dasae502122015-06-03 18:38:08 -04002673 * Parse command name from @cmdp according to command table @table.
2674 * If blank, return NULL.
2675 * Else, if no valid command can be found, report to @mon, and return
2676 * NULL.
2677 * Else, change @cmdp to point right behind the name, and return its
2678 * command table entry.
2679 * Do not assume the return value points into @table! It doesn't when
2680 * the command is found in a sub-command table.
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002681 */
Anthony Liguoric227f092009-10-01 16:12:16 -05002682static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Bandan Dasae502122015-06-03 18:38:08 -04002683 const char **cmdp,
2684 mon_cmd_t *table)
bellard9307c4c2004-04-04 12:57:25 +00002685{
Bandan Dasae502122015-06-03 18:38:08 -04002686 const char *p;
Anthony Liguoric227f092009-10-01 16:12:16 -05002687 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002688 char cmdname[256];
bellard9dc39cb2004-03-14 21:38:27 +00002689
bellard9307c4c2004-04-04 12:57:25 +00002690 /* extract the command name */
Bandan Dasae502122015-06-03 18:38:08 -04002691 p = get_command_name(*cmdp, cmdname, sizeof(cmdname));
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002692 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002693 return NULL;
ths3b46e622007-09-17 08:09:54 +00002694
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002695 cmd = search_dispatch_table(table, cmdname);
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002696 if (!cmd) {
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002697 monitor_printf(mon, "unknown command: '%.*s'\n",
Bandan Dasae502122015-06-03 18:38:08 -04002698 (int)(p - *cmdp), *cmdp);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002699 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002700 }
bellard9307c4c2004-04-04 12:57:25 +00002701
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002702 /* filter out following useless space */
2703 while (qemu_isspace(*p)) {
2704 p++;
2705 }
Bandan Dasae502122015-06-03 18:38:08 -04002706
2707 *cmdp = p;
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002708 /* search sub command */
Bandan Dasae502122015-06-03 18:38:08 -04002709 if (cmd->sub_table != NULL && *p != '\0') {
2710 return monitor_parse_command(mon, cmdp, cmd->sub_table);
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002711 }
2712
Bandan Dasae502122015-06-03 18:38:08 -04002713 return cmd;
2714}
2715
2716/*
2717 * Parse arguments for @cmd.
2718 * If it can't be parsed, report to @mon, and return NULL.
2719 * Else, insert command arguments into a QDict, and return it.
2720 * Note: On success, caller has to free the QDict structure.
2721 */
2722
2723static QDict *monitor_parse_arguments(Monitor *mon,
2724 const char **endp,
2725 const mon_cmd_t *cmd)
2726{
2727 const char *typestr;
2728 char *key;
2729 int c;
2730 const char *p = *endp;
2731 char buf[1024];
2732 QDict *qdict = qdict_new();
2733
bellard9307c4c2004-04-04 12:57:25 +00002734 /* parse the parameters */
2735 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00002736 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002737 typestr = key_get_info(typestr, &key);
2738 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00002739 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002740 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00002741 typestr++;
2742 switch(c) {
2743 case 'F':
bellard81d09122004-07-14 17:21:37 +00002744 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002745 case 's':
2746 {
2747 int ret;
ths3b46e622007-09-17 08:09:54 +00002748
blueswir1cd390082008-11-16 13:53:32 +00002749 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002750 p++;
2751 if (*typestr == '?') {
2752 typestr++;
2753 if (*p == '\0') {
2754 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03002755 break;
bellard9307c4c2004-04-04 12:57:25 +00002756 }
2757 }
2758 ret = get_str(buf, sizeof(buf), &p);
2759 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002760 switch(c) {
2761 case 'F':
aliguori376253e2009-03-05 23:01:23 +00002762 monitor_printf(mon, "%s: filename expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002763 cmd->name);
bellard81d09122004-07-14 17:21:37 +00002764 break;
2765 case 'B':
aliguori376253e2009-03-05 23:01:23 +00002766 monitor_printf(mon, "%s: block device name expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002767 cmd->name);
bellard81d09122004-07-14 17:21:37 +00002768 break;
2769 default:
Bandan Dasae502122015-06-03 18:38:08 -04002770 monitor_printf(mon, "%s: string expected\n", cmd->name);
bellard81d09122004-07-14 17:21:37 +00002771 break;
2772 }
bellard9307c4c2004-04-04 12:57:25 +00002773 goto fail;
2774 }
Luiz Capitulino53773582009-08-28 15:27:25 -03002775 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00002776 }
2777 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01002778 case 'O':
2779 {
2780 QemuOptsList *opts_list;
2781 QemuOpts *opts;
2782
2783 opts_list = qemu_find_opts(key);
2784 if (!opts_list || opts_list->desc->name) {
2785 goto bad_type;
2786 }
2787 while (qemu_isspace(*p)) {
2788 p++;
2789 }
2790 if (!*p)
2791 break;
2792 if (get_str(buf, sizeof(buf), &p) < 0) {
2793 goto fail;
2794 }
Markus Armbruster70b94332015-02-13 12:50:26 +01002795 opts = qemu_opts_parse_noisily(opts_list, buf, true);
Markus Armbruster361127d2010-02-10 20:24:35 +01002796 if (!opts) {
2797 goto fail;
2798 }
2799 qemu_opts_to_qdict(opts, qdict);
2800 qemu_opts_del(opts);
2801 }
2802 break;
bellard9307c4c2004-04-04 12:57:25 +00002803 case '/':
2804 {
2805 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002806
blueswir1cd390082008-11-16 13:53:32 +00002807 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002808 p++;
2809 if (*p == '/') {
2810 /* format found */
2811 p++;
2812 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002813 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002814 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002815 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002816 count = count * 10 + (*p - '0');
2817 p++;
2818 }
2819 }
2820 size = -1;
2821 format = -1;
2822 for(;;) {
2823 switch(*p) {
2824 case 'o':
2825 case 'd':
2826 case 'u':
2827 case 'x':
2828 case 'i':
2829 case 'c':
2830 format = *p++;
2831 break;
2832 case 'b':
2833 size = 1;
2834 p++;
2835 break;
2836 case 'h':
2837 size = 2;
2838 p++;
2839 break;
2840 case 'w':
2841 size = 4;
2842 p++;
2843 break;
2844 case 'g':
2845 case 'L':
2846 size = 8;
2847 p++;
2848 break;
2849 default:
2850 goto next;
2851 }
2852 }
2853 next:
blueswir1cd390082008-11-16 13:53:32 +00002854 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00002855 monitor_printf(mon, "invalid char in format: '%c'\n",
2856 *p);
bellard9307c4c2004-04-04 12:57:25 +00002857 goto fail;
2858 }
bellard9307c4c2004-04-04 12:57:25 +00002859 if (format < 0)
2860 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002861 if (format != 'i') {
2862 /* for 'i', not specifying a size gives -1 as size */
2863 if (size < 0)
2864 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002865 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002866 }
bellard9307c4c2004-04-04 12:57:25 +00002867 default_fmt_format = format;
2868 } else {
2869 count = 1;
2870 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002871 if (format != 'i') {
2872 size = default_fmt_size;
2873 } else {
2874 size = -1;
2875 }
bellard9307c4c2004-04-04 12:57:25 +00002876 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002877 qdict_put(qdict, "count", qint_from_int(count));
2878 qdict_put(qdict, "format", qint_from_int(format));
2879 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00002880 }
2881 break;
2882 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002883 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002884 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00002885 {
blueswir1c2efc952007-09-25 17:28:42 +00002886 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00002887
blueswir1cd390082008-11-16 13:53:32 +00002888 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002889 p++;
bellard34405572004-06-08 00:55:58 +00002890 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002891 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03002892 if (*p == '\0') {
2893 typestr++;
2894 break;
2895 }
bellard34405572004-06-08 00:55:58 +00002896 } else {
2897 if (*p == '.') {
2898 p++;
blueswir1cd390082008-11-16 13:53:32 +00002899 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00002900 p++;
bellard34405572004-06-08 00:55:58 +00002901 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03002902 typestr++;
2903 break;
bellard34405572004-06-08 00:55:58 +00002904 }
2905 }
bellard13224a82006-07-14 22:03:35 +00002906 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002907 }
aliguori376253e2009-03-05 23:01:23 +00002908 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00002909 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002910 /* Check if 'i' is greater than 32-bit */
2911 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
Bandan Dasae502122015-06-03 18:38:08 -04002912 monitor_printf(mon, "\'%s\' has failed: ", cmd->name);
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002913 monitor_printf(mon, "integer is for 32-bit values\n");
2914 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002915 } else if (c == 'M') {
Luiz Capitulino91162842012-04-26 17:34:30 -03002916 if (val < 0) {
2917 monitor_printf(mon, "enter a positive value\n");
2918 goto fail;
2919 }
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002920 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002921 }
Luiz Capitulino53773582009-08-28 15:27:25 -03002922 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00002923 }
2924 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02002925 case 'o':
2926 {
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +01002927 int ret;
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +01002928 uint64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02002929 char *end;
2930
2931 while (qemu_isspace(*p)) {
2932 p++;
2933 }
2934 if (*typestr == '?') {
2935 typestr++;
2936 if (*p == '\0') {
2937 break;
2938 }
2939 }
Markus Armbrusterf17fd4f2017-02-21 21:14:06 +01002940 ret = qemu_strtosz_MiB(p, &end, &val);
Markus Armbrusterf46bfdb2017-02-21 21:14:07 +01002941 if (ret < 0 || val > INT64_MAX) {
Jes Sorensendbc0c672010-10-21 17:15:47 +02002942 monitor_printf(mon, "invalid size\n");
2943 goto fail;
2944 }
2945 qdict_put(qdict, key, qint_from_int(val));
2946 p = end;
2947 }
2948 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01002949 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002950 {
2951 double val;
2952
2953 while (qemu_isspace(*p))
2954 p++;
2955 if (*typestr == '?') {
2956 typestr++;
2957 if (*p == '\0') {
2958 break;
2959 }
2960 }
2961 if (get_double(mon, &val, &p) < 0) {
2962 goto fail;
2963 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02002964 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01002965 switch (*p) {
2966 case 'm':
2967 val /= 1e3; p += 2; break;
2968 case 'u':
2969 val /= 1e6; p += 2; break;
2970 case 'n':
2971 val /= 1e9; p += 2; break;
2972 }
2973 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002974 if (*p && !qemu_isspace(*p)) {
2975 monitor_printf(mon, "Unknown unit suffix\n");
2976 goto fail;
2977 }
2978 qdict_put(qdict, key, qfloat_from_double(val));
2979 }
2980 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002981 case 'b':
2982 {
2983 const char *beg;
Eric Blakefc48ffc2015-05-15 16:24:59 -06002984 bool val;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002985
2986 while (qemu_isspace(*p)) {
2987 p++;
2988 }
2989 beg = p;
2990 while (qemu_isgraph(*p)) {
2991 p++;
2992 }
2993 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06002994 val = true;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002995 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06002996 val = false;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002997 } else {
2998 monitor_printf(mon, "Expected 'on' or 'off'\n");
2999 goto fail;
3000 }
Eric Blakefc48ffc2015-05-15 16:24:59 -06003001 qdict_put(qdict, key, qbool_from_bool(val));
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003002 }
3003 break;
bellard9307c4c2004-04-04 12:57:25 +00003004 case '-':
3005 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003006 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003007 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003008 /* option */
ths3b46e622007-09-17 08:09:54 +00003009
bellard9307c4c2004-04-04 12:57:25 +00003010 c = *typestr++;
3011 if (c == '\0')
3012 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003013 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003014 p++;
bellard9307c4c2004-04-04 12:57:25 +00003015 if (*p == '-') {
3016 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003017 if(c != *p) {
3018 if(!is_valid_option(p, typestr)) {
3019
3020 monitor_printf(mon, "%s: unsupported option -%c\n",
Bandan Dasae502122015-06-03 18:38:08 -04003021 cmd->name, *p);
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003022 goto fail;
3023 } else {
3024 skip_key = 1;
3025 }
bellard9307c4c2004-04-04 12:57:25 +00003026 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003027 if(skip_key) {
3028 p = tmp;
3029 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003030 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003031 p++;
Eric Blakefc48ffc2015-05-15 16:24:59 -06003032 qdict_put(qdict, key, qbool_from_bool(true));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003033 }
bellard9307c4c2004-04-04 12:57:25 +00003034 }
bellard9307c4c2004-04-04 12:57:25 +00003035 }
3036 break;
Wenchao Xia129be002013-08-27 20:38:26 +08003037 case 'S':
3038 {
3039 /* package all remaining string */
3040 int len;
3041
3042 while (qemu_isspace(*p)) {
3043 p++;
3044 }
3045 if (*typestr == '?') {
3046 typestr++;
3047 if (*p == '\0') {
3048 /* no remaining string: NULL argument */
3049 break;
3050 }
3051 }
3052 len = strlen(p);
3053 if (len <= 0) {
3054 monitor_printf(mon, "%s: string expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04003055 cmd->name);
Bandan Dase549d2a2015-06-03 18:38:10 -04003056 goto fail;
Wenchao Xia129be002013-08-27 20:38:26 +08003057 }
3058 qdict_put(qdict, key, qstring_from_str(p));
3059 p += len;
3060 }
3061 break;
bellard9307c4c2004-04-04 12:57:25 +00003062 default:
3063 bad_type:
Bandan Dasae502122015-06-03 18:38:08 -04003064 monitor_printf(mon, "%s: unknown type '%c'\n", cmd->name, c);
bellard9307c4c2004-04-04 12:57:25 +00003065 goto fail;
3066 }
Anthony Liguori7267c092011-08-20 22:09:37 -05003067 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003068 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003069 }
3070 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003071 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003072 p++;
3073 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003074 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
Bandan Dasae502122015-06-03 18:38:08 -04003075 cmd->name);
bellard9307c4c2004-04-04 12:57:25 +00003076 goto fail;
3077 }
3078
Bandan Dasae502122015-06-03 18:38:08 -04003079 return qdict;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003080
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003081fail:
Bandan Dasae502122015-06-03 18:38:08 -04003082 QDECREF(qdict);
Anthony Liguori7267c092011-08-20 22:09:37 -05003083 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003084 return NULL;
3085}
3086
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003087static void handle_hmp_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003088{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003089 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003090 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003091
Bandan Dasae502122015-06-03 18:38:08 -04003092 cmd = monitor_parse_command(mon, &cmdline, mon->cmd_table);
3093 if (!cmd) {
3094 return;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003095 }
3096
Bandan Dasae502122015-06-03 18:38:08 -04003097 qdict = monitor_parse_arguments(mon, &cmdline, cmd);
3098 if (!qdict) {
Bandan Dasdd41eea2015-06-03 18:38:09 -04003099 monitor_printf(mon, "Try \"help %s\" for more information\n",
3100 cmd->name);
Bandan Dasae502122015-06-03 18:38:08 -04003101 return;
3102 }
3103
Marc-André Lureau2b9e3572016-09-12 13:19:06 +04003104 cmd->cmd(mon, qdict);
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003105 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00003106}
3107
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003108static void cmd_completion(Monitor *mon, const char *name, const char *list)
bellard81d09122004-07-14 17:21:37 +00003109{
3110 const char *p, *pstart;
3111 char cmd[128];
3112 int len;
3113
3114 p = list;
3115 for(;;) {
3116 pstart = p;
3117 p = strchr(p, '|');
3118 if (!p)
3119 p = pstart + strlen(pstart);
3120 len = p - pstart;
3121 if (len > sizeof(cmd) - 2)
3122 len = sizeof(cmd) - 2;
3123 memcpy(cmd, pstart, len);
3124 cmd[len] = '\0';
3125 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003126 readline_add_completion(mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00003127 }
3128 if (*p == '\0')
3129 break;
3130 p++;
3131 }
3132}
3133
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003134static void file_completion(Monitor *mon, const char *input)
bellard81d09122004-07-14 17:21:37 +00003135{
3136 DIR *ffs;
3137 struct dirent *d;
3138 char path[1024];
3139 char file[1024], file_prefix[1024];
3140 int input_path_len;
3141 const char *p;
3142
ths5fafdf22007-09-16 21:08:06 +00003143 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00003144 if (!p) {
3145 input_path_len = 0;
3146 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00003147 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00003148 } else {
3149 input_path_len = p - input + 1;
3150 memcpy(path, input, input_path_len);
3151 if (input_path_len > sizeof(path) - 1)
3152 input_path_len = sizeof(path) - 1;
3153 path[input_path_len] = '\0';
3154 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3155 }
Bandan Das19f2db52015-06-03 18:38:07 -04003156
bellard81d09122004-07-14 17:21:37 +00003157 ffs = opendir(path);
3158 if (!ffs)
3159 return;
3160 for(;;) {
3161 struct stat sb;
3162 d = readdir(ffs);
3163 if (!d)
3164 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09003165
3166 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
3167 continue;
3168 }
3169
bellard81d09122004-07-14 17:21:37 +00003170 if (strstart(d->d_name, file_prefix, NULL)) {
3171 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00003172 if (input_path_len < sizeof(file))
3173 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3174 d->d_name);
bellard81d09122004-07-14 17:21:37 +00003175 /* stat the file to find out if it's a directory.
3176 * In that case add a slash to speed up typing long paths
3177 */
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01003178 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
blueswir1363a37d2008-08-21 17:58:08 +00003179 pstrcat(file, sizeof(file), "/");
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01003180 }
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003181 readline_add_completion(mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003182 }
3183 }
3184 closedir(ffs);
3185}
3186
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003187static const char *next_arg_type(const char *typestr)
3188{
3189 const char *p = strchr(typestr, ':');
3190 return (p != NULL ? ++p : typestr);
3191}
3192
Hani Benhabiles40d19392014-05-07 23:41:30 +01003193static void add_completion_option(ReadLineState *rs, const char *str,
3194 const char *option)
3195{
3196 if (!str || !option) {
3197 return;
3198 }
3199 if (!strncmp(option, str, strlen(str))) {
3200 readline_add_completion(rs, option);
3201 }
3202}
3203
Hani Benhabiles13e315d2014-05-07 23:41:29 +01003204void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
3205{
3206 size_t len;
3207 ChardevBackendInfoList *list, *start;
3208
3209 if (nb_args != 2) {
3210 return;
3211 }
3212 len = strlen(str);
3213 readline_set_completion_index(rs, len);
3214
3215 start = list = qmp_query_chardev_backends(NULL);
3216 while (list) {
3217 const char *chr_name = list->value->name;
3218
3219 if (!strncmp(chr_name, str, len)) {
3220 readline_add_completion(rs, chr_name);
3221 }
3222 list = list->next;
3223 }
3224 qapi_free_ChardevBackendInfoList(start);
3225}
3226
Hani Benhabilesb162b492014-05-07 23:41:31 +01003227void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
3228{
3229 size_t len;
3230 int i;
3231
3232 if (nb_args != 2) {
3233 return;
3234 }
3235 len = strlen(str);
3236 readline_set_completion_index(rs, len);
Eric Blakef394b2e2016-07-13 21:50:23 -06003237 for (i = 0; NetClientDriver_lookup[i]; i++) {
3238 add_completion_option(rs, str, NetClientDriver_lookup[i]);
Hani Benhabilesb162b492014-05-07 23:41:31 +01003239 }
3240}
3241
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003242void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003243{
3244 GSList *list, *elt;
3245 size_t len;
3246
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003247 if (nb_args != 2) {
3248 return;
3249 }
3250
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003251 len = strlen(str);
3252 readline_set_completion_index(rs, len);
3253 list = elt = object_class_get_list(TYPE_DEVICE, false);
3254 while (elt) {
3255 const char *name;
3256 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
3257 TYPE_DEVICE);
3258 name = object_class_get_name(OBJECT_CLASS(dc));
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003259
3260 if (!dc->cannot_instantiate_with_device_add_yet
3261 && !strncmp(name, str, len)) {
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003262 readline_add_completion(rs, name);
3263 }
3264 elt = elt->next;
3265 }
3266 g_slist_free(list);
3267}
3268
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003269void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003270{
3271 GSList *list, *elt;
3272 size_t len;
3273
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003274 if (nb_args != 2) {
3275 return;
3276 }
3277
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003278 len = strlen(str);
3279 readline_set_completion_index(rs, len);
3280 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
3281 while (elt) {
3282 const char *name;
3283
3284 name = object_class_get_name(OBJECT_CLASS(elt->data));
3285 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
3286 readline_add_completion(rs, name);
3287 }
3288 elt = elt->next;
3289 }
3290 g_slist_free(list);
3291}
3292
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003293static void peripheral_device_del_completion(ReadLineState *rs,
3294 const char *str, size_t len)
3295{
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003296 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
3297 GSList *list, *item;
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003298
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003299 list = qdev_build_hotpluggable_device_list(peripheral);
3300 if (!list) {
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003301 return;
3302 }
3303
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003304 for (item = list; item; item = g_slist_next(item)) {
3305 DeviceState *dev = item->data;
3306
3307 if (dev->id && !strncmp(str, dev->id, len)) {
3308 readline_add_completion(rs, dev->id);
3309 }
3310 }
3311
3312 g_slist_free(list);
3313}
3314
Hani Benhabiles6297d9a2014-05-07 23:41:28 +01003315void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3316{
3317 size_t len;
3318 ChardevInfoList *list, *start;
3319
3320 if (nb_args != 2) {
3321 return;
3322 }
3323 len = strlen(str);
3324 readline_set_completion_index(rs, len);
3325
3326 start = list = qmp_query_chardev(NULL);
3327 while (list) {
3328 ChardevInfo *chr = list->value;
3329
3330 if (!strncmp(chr->label, str, len)) {
3331 readline_add_completion(rs, chr->label);
3332 }
3333 list = list->next;
3334 }
3335 qapi_free_ChardevInfoList(start);
3336}
3337
Hani Benhabiles8e597772014-05-27 23:39:30 +01003338static void ringbuf_completion(ReadLineState *rs, const char *str)
3339{
3340 size_t len;
3341 ChardevInfoList *list, *start;
3342
3343 len = strlen(str);
3344 readline_set_completion_index(rs, len);
3345
3346 start = list = qmp_query_chardev(NULL);
3347 while (list) {
3348 ChardevInfo *chr_info = list->value;
3349
3350 if (!strncmp(chr_info->label, str, len)) {
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03003351 Chardev *chr = qemu_chr_find(chr_info->label);
Marc-André Lureau777357d2016-12-07 18:39:10 +03003352 if (chr && CHARDEV_IS_RINGBUF(chr)) {
Hani Benhabiles8e597772014-05-27 23:39:30 +01003353 readline_add_completion(rs, chr_info->label);
3354 }
3355 }
3356 list = list->next;
3357 }
3358 qapi_free_ChardevInfoList(start);
3359}
3360
Hani Benhabiles8e597772014-05-27 23:39:30 +01003361void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
3362{
3363 if (nb_args != 2) {
3364 return;
3365 }
3366 ringbuf_completion(rs, str);
3367}
3368
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003369void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
3370{
3371 size_t len;
3372
3373 if (nb_args != 2) {
3374 return;
3375 }
3376
3377 len = strlen(str);
3378 readline_set_completion_index(rs, len);
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003379 peripheral_device_del_completion(rs, str, len);
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003380}
3381
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003382void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003383{
3384 ObjectPropertyInfoList *list, *start;
3385 size_t len;
3386
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003387 if (nb_args != 2) {
3388 return;
3389 }
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003390 len = strlen(str);
3391 readline_set_completion_index(rs, len);
3392
3393 start = list = qmp_qom_list("/objects", NULL);
3394 while (list) {
3395 ObjectPropertyInfo *info = list->value;
3396
3397 if (!strncmp(info->type, "child<", 5)
3398 && !strncmp(info->name, str, len)) {
3399 readline_add_completion(rs, info->name);
3400 }
3401 list = list->next;
3402 }
3403 qapi_free_ObjectPropertyInfoList(start);
3404}
3405
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003406void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
3407{
3408 int i;
3409 char *sep;
3410 size_t len;
3411
3412 if (nb_args != 2) {
3413 return;
3414 }
3415 sep = strrchr(str, '-');
3416 if (sep) {
3417 str = sep + 1;
3418 }
3419 len = strlen(str);
3420 readline_set_completion_index(rs, len);
Eric Blake7fb1cf12015-11-18 01:52:57 -07003421 for (i = 0; i < Q_KEY_CODE__MAX; i++) {
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003422 if (!strncmp(str, QKeyCode_lookup[i], len)) {
3423 readline_add_completion(rs, QKeyCode_lookup[i]);
3424 }
3425 }
3426}
3427
Hani Benhabiles40d19392014-05-07 23:41:30 +01003428void set_link_completion(ReadLineState *rs, int nb_args, 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) {
Jason Wangeaed4832015-04-23 14:21:38 +08003435 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles40d19392014-05-07 23:41:30 +01003436 int count, i;
3437 count = qemu_find_net_clients_except(NULL, ncs,
Eric Blakef394b2e2016-07-13 21:50:23 -06003438 NET_CLIENT_DRIVER_NONE,
Jason Wangeaed4832015-04-23 14:21:38 +08003439 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003440 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles40d19392014-05-07 23:41:30 +01003441 const char *name = ncs[i]->name;
3442 if (!strncmp(str, name, len)) {
3443 readline_add_completion(rs, name);
3444 }
3445 }
3446 } else if (nb_args == 3) {
3447 add_completion_option(rs, str, "on");
3448 add_completion_option(rs, str, "off");
3449 }
3450}
3451
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003452void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
3453{
3454 int len, count, i;
Jason Wangeaed4832015-04-23 14:21:38 +08003455 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003456
3457 if (nb_args != 2) {
3458 return;
3459 }
3460
3461 len = strlen(str);
3462 readline_set_completion_index(rs, len);
Eric Blakef394b2e2016-07-13 21:50:23 -06003463 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_DRIVER_NIC,
Jason Wangeaed4832015-04-23 14:21:38 +08003464 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003465 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003466 QemuOpts *opts;
3467 const char *name = ncs[i]->name;
3468 if (strncmp(str, name, len)) {
3469 continue;
3470 }
3471 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
3472 if (opts) {
3473 readline_add_completion(rs, name);
3474 }
3475 }
3476}
3477
Lluís Vilanovabd712112016-07-11 12:53:51 +02003478void info_trace_events_completion(ReadLineState *rs, int nb_args, const char *str)
3479{
3480 size_t len;
3481
3482 len = strlen(str);
3483 readline_set_completion_index(rs, len);
3484 if (nb_args == 2) {
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003485 TraceEventIter iter;
3486 TraceEvent *ev;
3487 char *pattern = g_strdup_printf("%s*", str);
3488 trace_event_iter_init(&iter, pattern);
3489 while ((ev = trace_event_iter_next(&iter)) != NULL) {
3490 readline_add_completion(rs, trace_event_get_name(ev));
Lluís Vilanovabd712112016-07-11 12:53:51 +02003491 }
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003492 g_free(pattern);
Lluís Vilanovabd712112016-07-11 12:53:51 +02003493 }
3494}
3495
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003496void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
3497{
3498 size_t len;
3499
3500 len = strlen(str);
3501 readline_set_completion_index(rs, len);
3502 if (nb_args == 2) {
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003503 TraceEventIter iter;
3504 TraceEvent *ev;
3505 char *pattern = g_strdup_printf("%s*", str);
3506 trace_event_iter_init(&iter, pattern);
3507 while ((ev = trace_event_iter_next(&iter)) != NULL) {
3508 readline_add_completion(rs, trace_event_get_name(ev));
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003509 }
Daniel P. Berrange0d4e9952016-10-04 14:35:43 +01003510 g_free(pattern);
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003511 } else if (nb_args == 3) {
3512 add_completion_option(rs, str, "on");
3513 add_completion_option(rs, str, "off");
3514 }
3515}
3516
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003517void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
3518{
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003519 int i;
3520
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003521 if (nb_args != 2) {
3522 return;
3523 }
3524 readline_set_completion_index(rs, strlen(str));
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003525 for (i = 0; WatchdogExpirationAction_lookup[i]; i++) {
3526 add_completion_option(rs, str, WatchdogExpirationAction_lookup[i]);
3527 }
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003528}
3529
Hani Benhabilesc68a0402014-05-27 23:39:32 +01003530void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
3531 const char *str)
3532{
3533 size_t len;
3534
3535 len = strlen(str);
3536 readline_set_completion_index(rs, len);
3537 if (nb_args == 2) {
3538 int i;
Eric Blake7fb1cf12015-11-18 01:52:57 -07003539 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Hani Benhabilesc68a0402014-05-27 23:39:32 +01003540 const char *name = MigrationCapability_lookup[i];
3541 if (!strncmp(str, name, len)) {
3542 readline_add_completion(rs, name);
3543 }
3544 }
3545 } else if (nb_args == 3) {
3546 add_completion_option(rs, str, "on");
3547 add_completion_option(rs, str, "off");
3548 }
3549}
3550
Liang Li50e9a622015-03-23 16:32:29 +08003551void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
3552 const char *str)
3553{
3554 size_t len;
3555
3556 len = strlen(str);
3557 readline_set_completion_index(rs, len);
3558 if (nb_args == 2) {
3559 int i;
Eric Blake7fb1cf12015-11-18 01:52:57 -07003560 for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
Liang Li50e9a622015-03-23 16:32:29 +08003561 const char *name = MigrationParameter_lookup[i];
3562 if (!strncmp(str, name, len)) {
3563 readline_add_completion(rs, name);
3564 }
3565 }
3566 }
3567}
3568
Hani Benhabilese3bb5322014-05-27 23:39:34 +01003569void host_net_add_completion(ReadLineState *rs, int nb_args, const char *str)
3570{
3571 int i;
3572 size_t len;
3573 if (nb_args != 2) {
3574 return;
3575 }
3576 len = strlen(str);
3577 readline_set_completion_index(rs, len);
3578 for (i = 0; host_net_devices[i]; i++) {
3579 if (!strncmp(host_net_devices[i], str, len)) {
3580 readline_add_completion(rs, host_net_devices[i]);
3581 }
3582 }
3583}
3584
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003585void host_net_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3586{
Jason Wangeaed4832015-04-23 14:21:38 +08003587 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003588 int count, i, len;
3589
3590 len = strlen(str);
3591 readline_set_completion_index(rs, len);
3592 if (nb_args == 2) {
3593 count = qemu_find_net_clients_except(NULL, ncs,
Eric Blakef394b2e2016-07-13 21:50:23 -06003594 NET_CLIENT_DRIVER_NONE,
Jason Wangeaed4832015-04-23 14:21:38 +08003595 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003596 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003597 int id;
3598 char name[16];
3599
3600 if (net_hub_id_for_client(ncs[i], &id)) {
3601 continue;
3602 }
3603 snprintf(name, sizeof(name), "%d", id);
3604 if (!strncmp(str, name, len)) {
3605 readline_add_completion(rs, name);
3606 }
3607 }
3608 return;
3609 } else if (nb_args == 3) {
3610 count = qemu_find_net_clients_except(NULL, ncs,
Eric Blakef394b2e2016-07-13 21:50:23 -06003611 NET_CLIENT_DRIVER_NIC,
Jason Wangeaed4832015-04-23 14:21:38 +08003612 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003613 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Jason Wang2c4681f2015-02-02 15:06:38 +08003614 int id;
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003615 const char *name;
3616
Eric Blakef394b2e2016-07-13 21:50:23 -06003617 if (ncs[i]->info->type == NET_CLIENT_DRIVER_HUBPORT ||
Jason Wang2c4681f2015-02-02 15:06:38 +08003618 net_hub_id_for_client(ncs[i], &id)) {
3619 continue;
3620 }
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003621 name = ncs[i]->name;
3622 if (!strncmp(str, name, len)) {
3623 readline_add_completion(rs, name);
3624 }
3625 }
3626 return;
3627 }
3628}
3629
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003630static void vm_completion(ReadLineState *rs, const char *str)
3631{
3632 size_t len;
Kevin Wolf7c8eece2016-03-22 18:58:50 +01003633 BlockDriverState *bs;
Kevin Wolf88be7b42016-05-20 18:49:07 +02003634 BdrvNextIterator it;
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003635
3636 len = strlen(str);
3637 readline_set_completion_index(rs, len);
Kevin Wolf7c8eece2016-03-22 18:58:50 +01003638
Kevin Wolf88be7b42016-05-20 18:49:07 +02003639 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003640 SnapshotInfoList *snapshots, *snapshot;
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003641 AioContext *ctx = bdrv_get_aio_context(bs);
3642 bool ok = false;
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003643
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003644 aio_context_acquire(ctx);
3645 if (bdrv_can_snapshot(bs)) {
3646 ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0;
3647 }
3648 aio_context_release(ctx);
3649 if (!ok) {
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003650 continue;
3651 }
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003652
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003653 snapshot = snapshots;
3654 while (snapshot) {
3655 char *completion = snapshot->value->name;
3656 if (!strncmp(str, completion, len)) {
3657 readline_add_completion(rs, completion);
3658 }
3659 completion = snapshot->value->id;
3660 if (!strncmp(str, completion, len)) {
3661 readline_add_completion(rs, completion);
3662 }
3663 snapshot = snapshot->next;
3664 }
3665 qapi_free_SnapshotInfoList(snapshots);
3666 }
3667
3668}
3669
3670void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
3671{
3672 if (nb_args == 2) {
3673 vm_completion(rs, str);
3674 }
3675}
3676
3677void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
3678{
3679 if (nb_args == 2) {
3680 vm_completion(rs, str);
3681 }
3682}
3683
Wenchao Xiac35b6402013-08-27 20:38:24 +08003684static void monitor_find_completion_by_table(Monitor *mon,
3685 const mon_cmd_t *cmd_table,
3686 char **args,
3687 int nb_args)
bellard81d09122004-07-14 17:21:37 +00003688{
3689 const char *cmdname;
Wenchao Xiac35b6402013-08-27 20:38:24 +08003690 int i;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003691 const char *ptype, *str, *name;
Anthony Liguoric227f092009-10-01 16:12:16 -05003692 const mon_cmd_t *cmd;
Max Reitzda27a002016-03-16 19:54:29 +01003693 BlockBackend *blk = NULL;
bellard81d09122004-07-14 17:21:37 +00003694
bellard81d09122004-07-14 17:21:37 +00003695 if (nb_args <= 1) {
3696 /* command completion */
3697 if (nb_args == 0)
3698 cmdname = "";
3699 else
3700 cmdname = args[0];
Wenchao Xiad2674b22013-08-27 20:38:16 +08003701 readline_set_completion_index(mon->rs, strlen(cmdname));
Wenchao Xiac35b6402013-08-27 20:38:24 +08003702 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003703 cmd_completion(mon, cmdname, cmd->name);
bellard81d09122004-07-14 17:21:37 +00003704 }
3705 } else {
3706 /* find the command */
Wenchao Xiac35b6402013-08-27 20:38:24 +08003707 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Jan Kiszka03a63482010-06-16 00:38:33 +02003708 if (compare_cmd(args[0], cmd->name)) {
3709 break;
3710 }
bellard81d09122004-07-14 17:21:37 +00003711 }
Jan Kiszka03a63482010-06-16 00:38:33 +02003712 if (!cmd->name) {
Wenchao Xiac35b6402013-08-27 20:38:24 +08003713 return;
Jan Kiszka03a63482010-06-16 00:38:33 +02003714 }
3715
Wenchao Xiad903a772013-08-27 20:38:25 +08003716 if (cmd->sub_table) {
3717 /* do the job again */
Stefan Weile7ae7712015-03-08 19:30:01 +01003718 monitor_find_completion_by_table(mon, cmd->sub_table,
3719 &args[1], nb_args - 1);
3720 return;
Wenchao Xiad903a772013-08-27 20:38:25 +08003721 }
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003722 if (cmd->command_completion) {
Stefan Weile7ae7712015-03-08 19:30:01 +01003723 cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
3724 return;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003725 }
Wenchao Xiad903a772013-08-27 20:38:25 +08003726
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003727 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003728 for(i = 0; i < nb_args - 2; i++) {
3729 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003730 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003731 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003732 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003733 }
3734 }
3735 str = args[nb_args - 1];
Kevin Wolf48fe86f2014-11-12 16:24:02 +01003736 while (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02003737 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00003738 }
bellard81d09122004-07-14 17:21:37 +00003739 switch(*ptype) {
3740 case 'F':
3741 /* file completion */
Wenchao Xiad2674b22013-08-27 20:38:16 +08003742 readline_set_completion_index(mon->rs, strlen(str));
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003743 file_completion(mon, str);
bellard81d09122004-07-14 17:21:37 +00003744 break;
3745 case 'B':
3746 /* block device name completion */
Wenchao Xia599a9262013-08-27 20:38:15 +08003747 readline_set_completion_index(mon->rs, strlen(str));
Max Reitzda27a002016-03-16 19:54:29 +01003748 while ((blk = blk_next(blk)) != NULL) {
3749 name = blk_name(blk);
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003750 if (str[0] == '\0' ||
3751 !strncmp(name, str, strlen(str))) {
3752 readline_add_completion(mon->rs, name);
3753 }
3754 }
bellard81d09122004-07-14 17:21:37 +00003755 break;
bellard7fe48482004-10-09 18:08:01 +00003756 case 's':
Wenchao Xia129be002013-08-27 20:38:26 +08003757 case 'S':
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003758 if (!strcmp(cmd->name, "help|?")) {
Wenchao Xia7ca0e062013-08-27 20:38:27 +08003759 monitor_find_completion_by_table(mon, cmd_table,
3760 &args[1], nb_args - 1);
bellard7fe48482004-10-09 18:08:01 +00003761 }
3762 break;
bellard81d09122004-07-14 17:21:37 +00003763 default:
3764 break;
3765 }
3766 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08003767}
3768
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003769static void monitor_find_completion(void *opaque,
Wenchao Xiac35b6402013-08-27 20:38:24 +08003770 const char *cmdline)
3771{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003772 Monitor *mon = opaque;
Wenchao Xiac35b6402013-08-27 20:38:24 +08003773 char *args[MAX_ARGS];
3774 int nb_args, len;
3775
3776 /* 1. parse the cmdline */
3777 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
3778 return;
3779 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08003780
3781 /* if the line ends with a space, it means we want to complete the
3782 next arg */
3783 len = strlen(cmdline);
3784 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
3785 if (nb_args >= MAX_ARGS) {
3786 goto cleanup;
3787 }
3788 args[nb_args++] = g_strdup("");
3789 }
3790
3791 /* 2. auto complete according to args */
3792 monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args);
Jan Kiszka03a63482010-06-16 00:38:33 +02003793
3794cleanup:
Wenchao Xiadcc70cd2013-08-27 20:38:22 +08003795 free_cmdline_args(args, nb_args);
bellard81d09122004-07-14 17:21:37 +00003796}
3797
aliguori731b0362009-03-05 23:01:42 +00003798static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003799{
aliguori731b0362009-03-05 23:01:42 +00003800 Monitor *mon = opaque;
3801
Jan Kiszkac62313b2009-12-04 14:05:29 +01003802 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003803}
3804
Paolo Bonzini95385fe2015-11-25 22:23:31 +01003805static void handle_qmp_command(JSONMessageParser *parser, GQueue *tokens)
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003806{
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04003807 QObject *req, *rsp = NULL, *id = NULL;
3808 QDict *qdict = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003809 Monitor *mon = cur_mon;
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04003810 Error *err = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003811
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04003812 req = json_parser_parse_err(tokens, NULL, &err);
Markus Armbrusterbbf10282017-02-17 21:38:24 +01003813 if (!req && !err) {
3814 /* json_parser_parse_err() sucks: can fail without setting @err */
3815 error_setg(&err, QERR_JSON_PARSING);
3816 }
3817 if (err) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003818 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003819 }
3820
Markus Armbruster104fc302017-03-03 13:32:28 +01003821 qdict = qobject_to_qdict(req);
3822 if (qdict) {
3823 id = qdict_get(qdict, "id");
3824 qobject_incref(id);
3825 qdict_del(qdict, "id");
3826 } /* else will fail qmp_dispatch() */
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04003827
Markus Armbruster635db182017-03-03 13:32:27 +01003828 rsp = qmp_dispatch(cur_mon->qmp.commands, req);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003829
Markus Armbruster635db182017-03-03 13:32:27 +01003830 if (mon->qmp.commands == &qmp_cap_negotiation_commands) {
3831 qdict = qdict_get_qdict(qobject_to_qdict(rsp), "error");
3832 if (qdict
3833 && !g_strcmp0(qdict_get_try_str(qdict, "class"),
3834 QapiErrorClass_lookup[ERROR_CLASS_COMMAND_NOT_FOUND])) {
3835 /* Provide a more useful error message */
3836 qdict_del(qdict, "desc");
3837 qdict_put(qdict, "desc",
3838 qstring_from_str("Expecting capabilities negotiation"
3839 " with 'qmp_capabilities'"));
3840 }
3841 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003842
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003843err_out:
Marc-André Lureau5c678ee2016-09-12 13:19:10 +04003844 if (err) {
3845 qdict = qdict_new();
3846 qdict_put_obj(qdict, "error", qmp_build_error_object(err));
3847 error_free(err);
3848 rsp = QOBJECT(qdict);
3849 }
3850
3851 if (rsp) {
3852 if (id) {
3853 qdict_put_obj(qobject_to_qdict(rsp), "id", id);
3854 id = NULL;
3855 }
3856
3857 monitor_json_emitter(mon, rsp);
3858 }
3859
3860 qobject_decref(id);
3861 qobject_decref(rsp);
3862 qobject_decref(req);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003863}
3864
Markus Armbrusterc83fe232015-03-06 19:20:51 +01003865static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003866{
3867 Monitor *old_mon = cur_mon;
3868
3869 cur_mon = opaque;
3870
Markus Armbruster74358f22015-03-06 19:35:59 +01003871 json_message_parser_feed(&cur_mon->qmp.parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003872
3873 cur_mon = old_mon;
3874}
3875
aliguori731b0362009-03-05 23:01:42 +00003876static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00003877{
aliguori731b0362009-03-05 23:01:42 +00003878 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00003879 int i;
aliguori376253e2009-03-05 23:01:23 +00003880
aliguori731b0362009-03-05 23:01:42 +00003881 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00003882
aliguoricde76ee2009-03-05 23:01:51 +00003883 if (cur_mon->rs) {
3884 for (i = 0; i < size; i++)
3885 readline_handle_byte(cur_mon->rs, buf[i]);
3886 } else {
3887 if (size == 0 || buf[size - 1] != 0)
3888 monitor_printf(cur_mon, "corrupted command\n");
3889 else
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003890 handle_hmp_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00003891 }
aliguori731b0362009-03-05 23:01:42 +00003892
3893 cur_mon = old_mon;
3894}
aliguorid8f44602008-10-06 13:52:44 +00003895
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003896static void monitor_command_cb(void *opaque, const char *cmdline,
3897 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00003898{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003899 Monitor *mon = opaque;
3900
aliguori731b0362009-03-05 23:01:42 +00003901 monitor_suspend(mon);
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003902 handle_hmp_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00003903 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00003904}
3905
aliguoricde76ee2009-03-05 23:01:51 +00003906int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003907{
aliguoricde76ee2009-03-05 23:01:51 +00003908 if (!mon->rs)
3909 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00003910 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00003911 return 0;
aliguorid8f44602008-10-06 13:52:44 +00003912}
3913
aliguori376253e2009-03-05 23:01:23 +00003914void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003915{
aliguoricde76ee2009-03-05 23:01:51 +00003916 if (!mon->rs)
3917 return;
aliguori731b0362009-03-05 23:01:42 +00003918 if (--mon->suspend_cnt == 0)
3919 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00003920}
3921
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003922static QObject *get_qmp_greeting(void)
3923{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03003924 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003925
Markus Armbruster7fad30f2015-09-16 13:06:19 +02003926 qmp_marshal_query_version(NULL, &ver, NULL);
Marc-André Lureauc8235012016-09-12 13:19:04 +04003927
3928 return qobject_from_jsonf("{'QMP': {'version': %p, 'capabilities': []}}",
3929 ver);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003930}
3931
Markus Armbrusterc83fe232015-03-06 19:20:51 +01003932static void monitor_qmp_event(void *opaque, int event)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003933{
Luiz Capitulino47116d12010-02-08 17:01:30 -02003934 QObject *data;
3935 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003936
Luiz Capitulino47116d12010-02-08 17:01:30 -02003937 switch (event) {
3938 case CHR_EVENT_OPENED:
Markus Armbruster635db182017-03-03 13:32:27 +01003939 mon->qmp.commands = &qmp_cap_negotiation_commands;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003940 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003941 monitor_json_emitter(mon, data);
3942 qobject_decref(data);
Corey Bryantefb87c12012-08-14 16:43:48 -04003943 mon_refcount++;
Luiz Capitulino47116d12010-02-08 17:01:30 -02003944 break;
3945 case CHR_EVENT_CLOSED:
Markus Armbruster74358f22015-03-06 19:35:59 +01003946 json_message_parser_destroy(&mon->qmp.parser);
3947 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Corey Bryantefb87c12012-08-14 16:43:48 -04003948 mon_refcount--;
3949 monitor_fdsets_cleanup();
Luiz Capitulino47116d12010-02-08 17:01:30 -02003950 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003951 }
3952}
3953
aliguori731b0362009-03-05 23:01:42 +00003954static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00003955{
aliguori376253e2009-03-05 23:01:23 +00003956 Monitor *mon = opaque;
3957
aliguori2724b182009-03-05 23:01:47 +00003958 switch (event) {
3959 case CHR_EVENT_MUX_IN:
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003960 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003961 mon->mux_out = 0;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003962 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003963 if (mon->reset_seen) {
3964 readline_restart(mon->rs);
3965 monitor_resume(mon);
3966 monitor_flush(mon);
3967 } else {
3968 mon->suspend_cnt = 0;
3969 }
aliguori2724b182009-03-05 23:01:47 +00003970 break;
ths86e94de2007-01-05 22:01:59 +00003971
aliguori2724b182009-03-05 23:01:47 +00003972 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003973 if (mon->reset_seen) {
3974 if (mon->suspend_cnt == 0) {
3975 monitor_printf(mon, "\n");
3976 }
3977 monitor_flush(mon);
3978 monitor_suspend(mon);
3979 } else {
3980 mon->suspend_cnt++;
3981 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003982 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003983 mon->mux_out = 1;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003984 qemu_mutex_unlock(&mon->out_lock);
aliguori2724b182009-03-05 23:01:47 +00003985 break;
3986
Amit Shahb6b8df52009-10-07 18:31:16 +05303987 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00003988 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3989 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003990 if (!mon->mux_out) {
Stratos Psomadakise5554e22014-09-15 15:34:57 +03003991 readline_restart(mon->rs);
aliguori2724b182009-03-05 23:01:47 +00003992 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003993 }
3994 mon->reset_seen = 1;
Corey Bryantefb87c12012-08-14 16:43:48 -04003995 mon_refcount++;
3996 break;
3997
3998 case CHR_EVENT_CLOSED:
3999 mon_refcount--;
4000 monitor_fdsets_cleanup();
aliguori2724b182009-03-05 23:01:47 +00004001 break;
4002 }
ths86e94de2007-01-05 22:01:59 +00004003}
4004
Wayne Xia816f8922011-10-12 11:32:41 +08004005static int
4006compare_mon_cmd(const void *a, const void *b)
4007{
4008 return strcmp(((const mon_cmd_t *)a)->name,
4009 ((const mon_cmd_t *)b)->name);
4010}
4011
4012static void sortcmdlist(void)
4013{
4014 int array_num;
4015 int elem_size = sizeof(mon_cmd_t);
4016
4017 array_num = sizeof(mon_cmds)/elem_size-1;
4018 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
4019
4020 array_num = sizeof(info_cmds)/elem_size-1;
4021 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
4022}
4023
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004024/* These functions just adapt the readline interface in a typesafe way. We
4025 * could cast function pointers but that discards compiler checks.
4026 */
Stefan Weild5d15072014-01-25 18:18:23 +01004027static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
4028 const char *fmt, ...)
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004029{
4030 va_list ap;
4031 va_start(ap, fmt);
4032 monitor_vprintf(opaque, fmt, ap);
4033 va_end(ap);
4034}
4035
4036static void monitor_readline_flush(void *opaque)
4037{
4038 monitor_flush(opaque);
4039}
4040
Paolo Bonzini397d30e2016-10-24 18:31:02 +02004041/*
4042 * Print to current monitor if we have one, else to stderr.
4043 * TODO should return int, so callers can calculate width, but that
4044 * requires surgery to monitor_vprintf(). Left for another day.
4045 */
4046void error_vprintf(const char *fmt, va_list ap)
4047{
4048 if (cur_mon && !monitor_cur_is_qmp()) {
4049 monitor_vprintf(cur_mon, fmt, ap);
4050 } else {
4051 vfprintf(stderr, fmt, ap);
4052 }
4053}
4054
4055void error_vprintf_unless_qmp(const char *fmt, va_list ap)
4056{
4057 if (cur_mon && !monitor_cur_is_qmp()) {
4058 monitor_vprintf(cur_mon, fmt, ap);
Marc-André Lureau0d6b50d2017-01-05 14:59:57 +01004059 } else if (!cur_mon) {
4060 vfprintf(stderr, fmt, ap);
Paolo Bonzini397d30e2016-10-24 18:31:02 +02004061 }
4062}
4063
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004064static void __attribute__((constructor)) monitor_lock_init(void)
4065{
4066 qemu_mutex_init(&monitor_lock);
4067}
4068
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03004069void monitor_init(Chardev *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004070{
aliguori731b0362009-03-05 23:01:42 +00004071 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004072 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004073
4074 if (is_first_init) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +02004075 monitor_qapi_event_init();
Wenchao Xiad0383172013-08-27 20:38:18 +08004076 sortcmdlist();
ths20d8a3e2007-02-18 17:04:49 +00004077 is_first_init = 0;
4078 }
aliguori87127162009-03-05 23:01:29 +00004079
Wenchao Xiab01fe892013-08-27 20:38:19 +08004080 mon = g_malloc(sizeof(*mon));
4081 monitor_data_init(mon);
ths20d8a3e2007-02-18 17:04:49 +00004082
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03004083 qemu_chr_fe_init(&mon->chr, chr, &error_abort);
aliguori731b0362009-03-05 23:01:42 +00004084 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004085 if (flags & MONITOR_USE_READLINE) {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004086 mon->rs = readline_init(monitor_readline_printf,
4087 monitor_readline_flush,
4088 mon,
4089 monitor_find_completion);
aliguoricde76ee2009-03-05 23:01:51 +00004090 monitor_read_command(mon, 0);
4091 }
aliguori87127162009-03-05 23:01:29 +00004092
Markus Armbruster9f3982f2015-03-06 19:56:38 +01004093 if (monitor_is_qmp(mon)) {
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03004094 qemu_chr_fe_set_handlers(&mon->chr, monitor_can_read, monitor_qmp_read,
Marc-André Lureau39ab61c2016-10-22 12:53:03 +03004095 monitor_qmp_event, mon, NULL, true);
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03004096 qemu_chr_fe_set_echo(&mon->chr, true);
Markus Armbruster74358f22015-03-06 19:35:59 +01004097 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004098 } else {
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03004099 qemu_chr_fe_set_handlers(&mon->chr, monitor_can_read, monitor_read,
Marc-André Lureau39ab61c2016-10-22 12:53:03 +03004100 monitor_event, mon, NULL, true);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004101 }
aliguori87127162009-03-05 23:01:29 +00004102
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004103 qemu_mutex_lock(&monitor_lock);
Blue Swirl72cf2d42009-09-12 07:36:22 +00004104 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004105 qemu_mutex_unlock(&monitor_lock);
bellard7e2515e2004-08-01 21:52:19 +00004106}
4107
Marc-André Lureau2ef45712016-08-01 15:23:42 +04004108void monitor_cleanup(void)
4109{
4110 Monitor *mon, *next;
4111
4112 qemu_mutex_lock(&monitor_lock);
4113 QLIST_FOREACH_SAFE(mon, &mon_list, entry, next) {
4114 QLIST_REMOVE(mon, entry);
4115 monitor_data_destroy(mon);
4116 g_free(mon);
4117 }
4118 qemu_mutex_unlock(&monitor_lock);
4119}
4120
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004121static void bdrv_password_cb(void *opaque, const char *password,
4122 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00004123{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004124 Monitor *mon = opaque;
4125 BlockDriverState *bs = readline_opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004126 int ret = 0;
Markus Armbruster4d2855a2015-01-29 10:37:00 +01004127 Error *local_err = NULL;
bellard7e2515e2004-08-01 21:52:19 +00004128
Markus Armbruster4d2855a2015-01-29 10:37:00 +01004129 bdrv_add_key(bs, password, &local_err);
4130 if (local_err) {
Markus Armbruster193227f2015-12-18 16:35:06 +01004131 error_report_err(local_err);
aliguoribb5fc202009-03-05 23:01:15 +00004132 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004133 }
aliguori731b0362009-03-05 23:01:42 +00004134 if (mon->password_completion_cb)
4135 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004136
aliguori731b0362009-03-05 23:01:42 +00004137 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004138}
aliguoric0f4ce72009-03-05 23:01:01 +00004139
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004140int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
Markus Armbruster097310b2014-10-07 13:59:15 +02004141 BlockCompletionFunc *completion_cb,
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004142 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004143{
aliguoricde76ee2009-03-05 23:01:51 +00004144 int err;
4145
aliguori376253e2009-03-05 23:01:23 +00004146 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4147 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00004148
aliguori731b0362009-03-05 23:01:42 +00004149 mon->password_completion_cb = completion_cb;
4150 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004151
aliguoricde76ee2009-03-05 23:01:51 +00004152 err = monitor_read_password(mon, bdrv_password_cb, bs);
4153
4154 if (err && completion_cb)
4155 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004156
4157 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00004158}
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004159
4160int monitor_read_block_device_key(Monitor *mon, const char *device,
Markus Armbruster097310b2014-10-07 13:59:15 +02004161 BlockCompletionFunc *completion_cb,
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004162 void *opaque)
4163{
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01004164 Error *err = NULL;
Fam Zheng55606252015-03-02 19:36:46 +08004165 BlockBackend *blk;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004166
Fam Zheng55606252015-03-02 19:36:46 +08004167 blk = blk_by_name(device);
4168 if (!blk) {
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004169 monitor_printf(mon, "Device not found %s\n", device);
4170 return -1;
4171 }
Max Reitz5433c242015-10-19 17:53:29 +02004172 if (!blk_bs(blk)) {
4173 monitor_printf(mon, "Device '%s' has no medium\n", device);
4174 return -1;
4175 }
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004176
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01004177 bdrv_add_key(blk_bs(blk), NULL, &err);
4178 if (err) {
4179 error_free(err);
4180 return monitor_read_bdrv_key_start(mon, blk_bs(blk), completion_cb, opaque);
4181 }
4182
4183 if (completion_cb) {
4184 completion_cb(opaque, 0);
4185 }
4186 return 0;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004187}
Paolo Bonzini4d454572012-11-26 16:03:42 +01004188
4189QemuOptsList qemu_mon_opts = {
4190 .name = "mon",
4191 .implied_opt_name = "chardev",
4192 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
4193 .desc = {
4194 {
4195 .name = "mode",
4196 .type = QEMU_OPT_STRING,
4197 },{
4198 .name = "chardev",
4199 .type = QEMU_OPT_STRING,
4200 },{
Marc-André Lureaubdbcb542016-10-11 21:41:21 +04004201 .name = "default", /* deprecated */
Paolo Bonzini4d454572012-11-26 16:03:42 +01004202 .type = QEMU_OPT_BOOL,
4203 },{
4204 .name = "pretty",
4205 .type = QEMU_OPT_BOOL,
4206 },
4207 { /* end of list */ }
4208 },
4209};
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004210
4211#ifndef TARGET_I386
4212void qmp_rtc_reset_reinjection(Error **errp)
4213{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004214 error_setg(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection");
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004215}
4216#endif
Jason J. Herne7ee0c3e2015-06-26 14:03:16 -04004217
4218#ifndef TARGET_S390X
4219void qmp_dump_skeys(const char *filename, Error **errp)
4220{
4221 error_setg(errp, QERR_FEATURE_DISABLED, "dump-skeys");
4222}
4223#endif
Peter Xuae50a772016-03-30 17:27:24 +01004224
4225#ifndef TARGET_ARM
4226GICCapabilityList *qmp_query_gic_capabilities(Error **errp)
4227{
4228 error_setg(errp, QERR_FEATURE_DISABLED, "query-gic-capabilities");
4229 return NULL;
4230}
4231#endif
Igor Mammedovd4633542016-06-10 06:29:06 +05304232
4233HotpluggableCPUList *qmp_query_hotpluggable_cpus(Error **errp)
4234{
4235 MachineState *ms = MACHINE(qdev_get_machine());
4236 MachineClass *mc = MACHINE_GET_CLASS(ms);
4237
Igor Mammedovc5514d02017-02-10 11:20:57 +01004238 if (!mc->has_hotpluggable_cpus) {
Igor Mammedovd4633542016-06-10 06:29:06 +05304239 error_setg(errp, QERR_FEATURE_DISABLED, "query-hotpluggable-cpus");
4240 return NULL;
4241 }
4242
Igor Mammedovc5514d02017-02-10 11:20:57 +01004243 return machine_query_hotpluggable_cpus(ms);
Igor Mammedovd4633542016-06-10 06:29:06 +05304244}