blob: d1c193013e3657e167c72ca18b106567800cb5aa [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>
pbrook87ecb682007-11-17 17:14:51 +000026#include "hw/hw.h"
Paolo Bonzinib4a42f82013-02-04 11:37:52 +010027#include "monitor/qdev.h"
pbrook87ecb682007-11-17 17:14:51 +000028#include "hw/usb.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010029#include "hw/i386/pc.h"
Michael S. Tsirkina2cb15b2012-12-12 14:24:50 +020030#include "hw/pci/pci.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010031#include "sysemu/watchdog.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +020032#include "hw/loader.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010033#include "exec/gdbstub.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020034#include "net/net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000035#include "net/slirp.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020036#include "sysemu/char.h"
Gerd Hoffmann75721502010-10-07 12:22:54 +020037#include "ui/qemu-spice.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010038#include "sysemu/sysemu.h"
Eduardo Habkoste35704b2015-02-08 16:51:16 -020039#include "sysemu/numa.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010040#include "monitor/monitor.h"
Stefan Hajnoczi0150cd82013-11-14 11:54:15 +010041#include "qemu/readline.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010042#include "ui/console.h"
Gerd Hoffmannc751a742013-12-04 15:02:28 +010043#include "ui/input.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010044#include "sysemu/blockdev.h"
Max Reitzda27a002016-03-16 19:54:29 +010045#include "sysemu/block-backend.h"
pbrook87ecb682007-11-17 17:14:51 +000046#include "audio/audio.h"
Paolo Bonzini76cad712012-10-24 11:12:21 +020047#include "disas/disas.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010048#include "sysemu/balloon.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010049#include "qemu/timer.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010050#include "migration/migration.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010051#include "sysemu/kvm.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010052#include "qemu/acl.h"
Paolo Bonzinibdee56f2013-04-02 18:28:41 +020053#include "sysemu/tpm.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010054#include "qapi/qmp/qerror.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010055#include "qapi/qmp/qint.h"
56#include "qapi/qmp/qfloat.h"
57#include "qapi/qmp/qlist.h"
58#include "qapi/qmp/qbool.h"
59#include "qapi/qmp/qstring.h"
60#include "qapi/qmp/qjson.h"
61#include "qapi/qmp/json-streamer.h"
62#include "qapi/qmp/json-parser.h"
Hani Benhabiles1094fd32014-02-06 23:30:13 +010063#include <qom/object_interfaces.h>
Blue Swirl2b41f102011-06-19 20:38:22 +000064#include "cpu.h"
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +010065#include "trace.h"
Lluís31965ae2011-08-31 20:31:24 +020066#include "trace/control.h"
Pavel Butsykinbf957282015-09-10 18:38:59 +030067#include "monitor/hmp-target.h"
Lluís6d8a7642011-08-31 20:30:43 +020068#ifdef CONFIG_TRACE_SIMPLE
Lluís31965ae2011-08-31 20:31:24 +020069#include "trace/simple.h"
Prerna Saxena22890ab2010-06-24 17:04:53 +053070#endif
Paolo Bonzini022c62c2012-12-17 18:19:49 +010071#include "exec/memory.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"
Fam Zheng55606252015-03-02 19:36:46 +080079#include "sysemu/block-backend.h"
Alberto Garciadc599972016-03-10 13:55:26 +020080#include "sysemu/qtest.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020081#include "qemu/cutils.h"
ths6a5bd302007-12-03 17:05:38 +000082
Markus Armbruster1ce6be22015-02-06 14:18:24 +010083/* for hmp_info_irq/pic */
Jan Kiszka661f1922011-10-16 11:53:13 +020084#if defined(TARGET_SPARC)
Paolo Bonzini0d09e412013-02-05 17:06:20 +010085#include "hw/sparc/sun4m.h"
Jan Kiszka661f1922011-10-16 11:53:13 +020086#endif
Paolo Bonzini0d09e412013-02-05 17:06:20 +010087#include "hw/lm32/lm32_pic.h"
Jan Kiszka661f1922011-10-16 11:53:13 +020088
Jason J. Hernea4538a52015-06-26 14:07:21 -040089#if defined(TARGET_S390X)
90#include "hw/s390x/storage-keys.h"
91#endif
92
bellard9307c4c2004-04-04 12:57:25 +000093/*
94 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000095 *
bellard9307c4c2004-04-04 12:57:25 +000096 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000097 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000098 * 's' string (accept optional quote)
Wenchao Xia129be002013-08-27 20:38:26 +080099 * 'S' it just appends the rest of the string (accept optional quote)
Markus Armbruster361127d2010-02-10 20:24:35 +0100100 * 'O' option string of the form NAME=VALUE,...
101 * parsed according to QemuOptsList given by its name
102 * Example: 'device:O' uses qemu_device_opts.
103 * Restriction: only lists with empty desc are supported
104 * TODO lift the restriction
bellard92a31b12005-02-10 22:00:52 +0000105 * 'i' 32 bit integer
106 * 'l' target long (32 or 64 bit)
Luiz Capitulino91162842012-04-26 17:34:30 -0300107 * 'M' Non-negative target long (32 or 64 bit), in user mode the
108 * value is multiplied by 2^20 (think Mebibyte)
Jes Sorensendbc0c672010-10-21 17:15:47 +0200109 * 'o' octets (aka bytes)
Kevin Wolf5e009842013-06-05 14:19:27 +0200110 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
111 * K, k suffix, which multiplies the value by 2^60 for suffixes E
112 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
113 * 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 +0100114 * 'T' double
115 * user mode accepts an optional ms, us, ns suffix,
116 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +0000117 * '/' optional gdb-like print format (like "/10x")
118 *
Luiz Capitulinofb466602009-08-28 15:27:27 -0300119 * '?' optional type (for all types, except '/')
120 * '.' other form of optional type (for 'i' and 'l')
Markus Armbruster942cd1f2010-03-26 09:07:09 +0100121 * 'b' boolean
122 * user mode accepts "on" or "off"
Luiz Capitulinofb466602009-08-28 15:27:27 -0300123 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +0000124 *
125 */
126
Anthony Liguoric227f092009-10-01 16:12:16 -0500127typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +0000128 const char *name;
bellard9307c4c2004-04-04 12:57:25 +0000129 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +0000130 const char *params;
131 const char *help;
Luiz Capitulino910df892009-10-07 13:41:51 -0300132 union {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -0300133 void (*cmd)(Monitor *mon, const QDict *qdict);
Markus Armbruster485febc2015-03-13 17:25:50 +0100134 void (*cmd_new)(QDict *params, QObject **ret_data, Error **errp);
Luiz Capitulino910df892009-10-07 13:41:51 -0300135 } mhandler;
Wenchao Xia5f3d3352013-01-14 14:06:27 +0800136 /* @sub_table is a list of 2nd level of commands. If it do not exist,
137 * mhandler should be used. If it exist, sub_table[?].mhandler should be
138 * used, and mhandler of 1st level plays the role of help function.
139 */
140 struct mon_cmd_t *sub_table;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +0100141 void (*command_completion)(ReadLineState *rs, int nb_args, const char *str);
Anthony Liguoric227f092009-10-01 16:12:16 -0500142} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000143
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100144/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500145typedef struct mon_fd_t mon_fd_t;
146struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100147 char *name;
148 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500149 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100150};
151
Corey Bryantba1c0482012-08-14 16:43:43 -0400152/* file descriptor associated with a file descriptor set */
153typedef struct MonFdsetFd MonFdsetFd;
154struct MonFdsetFd {
155 int fd;
156 bool removed;
157 char *opaque;
158 QLIST_ENTRY(MonFdsetFd) next;
159};
160
161/* file descriptor set containing fds passed via SCM_RIGHTS */
162typedef struct MonFdset MonFdset;
163struct MonFdset {
164 int64_t id;
165 QLIST_HEAD(, MonFdsetFd) fds;
Corey Bryantadb696f2012-08-14 16:43:47 -0400166 QLIST_HEAD(, MonFdsetFd) dup_fds;
Corey Bryantba1c0482012-08-14 16:43:43 -0400167 QLIST_ENTRY(MonFdset) next;
168};
169
Markus Armbruster74358f22015-03-06 19:35:59 +0100170typedef struct {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200171 QObject *id;
172 JSONMessageParser parser;
Markus Armbrusterf994b252015-03-06 19:51:51 +0100173 /*
174 * When a client connects, we're in capabilities negotiation mode.
175 * When command qmp_capabilities succeeds, we go into command
176 * mode.
177 */
178 bool in_command_mode; /* are we in command mode? */
Markus Armbruster74358f22015-03-06 19:35:59 +0100179} MonitorQMP;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200180
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100181/*
182 * To prevent flooding clients, events can be throttled. The
183 * throttling is calculated globally, rather than per-Monitor
184 * instance.
185 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200186typedef struct MonitorQAPIEventState {
Markus Armbruster7de0be62015-10-15 17:08:35 +0200187 QAPIEvent event; /* Throttling state for this event type and... */
188 QDict *data; /* ... data, see qapi_event_throttle_equal() */
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100189 QEMUTimer *timer; /* Timer for handling delayed events */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200190 QDict *qdict; /* Delayed event (if any) */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200191} MonitorQAPIEventState;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100192
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200193typedef struct {
194 int64_t rate; /* Minimum time (in ns) between two events */
195} MonitorQAPIEventConf;
196
aliguori87127162009-03-05 23:01:29 +0000197struct Monitor {
198 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200199 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000200 int flags;
201 int suspend_cnt;
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400202 bool skip_flush;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200203
204 QemuMutex out_lock;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400205 QString *outbuf;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200206 guint out_watch;
207
208 /* Read under either BQL or out_lock, written with BQL+out_lock. */
209 int mux_out;
210
aliguori731b0362009-03-05 23:01:42 +0000211 ReadLineState *rs;
Markus Armbruster74358f22015-03-06 19:35:59 +0100212 MonitorQMP qmp;
Andreas Färbercb446ec2013-05-01 14:24:52 +0200213 CPUState *mon_cpu;
Markus Armbruster097310b2014-10-07 13:59:15 +0200214 BlockCompletionFunc *password_completion_cb;
aliguori731b0362009-03-05 23:01:42 +0000215 void *password_opaque;
Wenchao Xia77172392013-08-27 20:38:20 +0800216 mon_cmd_t *cmd_table;
Anthony Liguoric227f092009-10-01 16:12:16 -0500217 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000218 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000219};
220
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300221/* QMP checker flags */
222#define QMP_ACCEPT_UNKNOWNS 1
223
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200224/* Protects mon_list, monitor_event_state. */
225static QemuMutex monitor_lock;
226
Blue Swirl72cf2d42009-09-12 07:36:22 +0000227static QLIST_HEAD(mon_list, Monitor) mon_list;
Corey Bryantba1c0482012-08-14 16:43:43 -0400228static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
Corey Bryantefb87c12012-08-14 16:43:48 -0400229static int mon_refcount;
bellard7e2515e2004-08-01 21:52:19 +0000230
Wayne Xia816f8922011-10-12 11:32:41 +0800231static mon_cmd_t mon_cmds[];
232static mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000233
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300234static const mon_cmd_t qmp_cmds[];
235
Markus Armbruster8631b602010-02-18 11:41:55 +0100236Monitor *cur_mon;
aliguori376253e2009-03-05 23:01:23 +0000237
Alberto Garciadc599972016-03-10 13:55:26 +0200238static QEMUClockType event_clock_type = QEMU_CLOCK_REALTIME;
239
Stefan Hajnoczic60bf332013-11-14 11:54:14 +0100240static void monitor_command_cb(void *opaque, const char *cmdline,
241 void *readline_opaque);
aliguori83ab7952008-08-19 14:44:22 +0000242
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100243/**
244 * Is @mon a QMP monitor?
245 */
246static inline bool monitor_is_qmp(const Monitor *mon)
Luiz Capitulino418173c2009-11-26 22:58:51 -0200247{
248 return (mon->flags & MONITOR_USE_CONTROL);
249}
250
Markus Armbruster489653b2015-03-06 20:01:05 +0100251/**
252 * Is the current monitor, if any, a QMP monitor?
253 */
254bool monitor_cur_is_qmp(void)
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100255{
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100256 return cur_mon && monitor_is_qmp(cur_mon);
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100257}
258
Anthony Liguori7060b472011-09-02 12:34:50 -0500259void monitor_read_command(Monitor *mon, int show_prompt)
aliguori731b0362009-03-05 23:01:42 +0000260{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200261 if (!mon->rs)
262 return;
263
aliguori731b0362009-03-05 23:01:42 +0000264 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
265 if (show_prompt)
266 readline_show_prompt(mon->rs);
267}
bellard6a00d602005-11-21 23:25:50 +0000268
Anthony Liguori7060b472011-09-02 12:34:50 -0500269int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
270 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000271{
Markus Armbrusterbcf5d192015-03-12 17:26:46 +0100272 if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000273 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
274 /* prompt is printed on return from the command handler */
275 return 0;
276 } else {
277 monitor_printf(mon, "terminal does not support password prompting\n");
278 return -ENOTTY;
279 }
aliguoribb5fc202009-03-05 23:01:15 +0000280}
281
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200282static void monitor_flush_locked(Monitor *mon);
283
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100284static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
285 void *opaque)
286{
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200287 Monitor *mon = opaque;
288
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200289 qemu_mutex_lock(&mon->out_lock);
290 mon->out_watch = 0;
291 monitor_flush_locked(mon);
292 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100293 return FALSE;
294}
295
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200296/* Called with mon->out_lock held. */
297static void monitor_flush_locked(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000298{
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100299 int rc;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400300 size_t len;
301 const char *buf;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100302
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400303 if (mon->skip_flush) {
304 return;
305 }
306
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400307 buf = qstring_get_str(mon->outbuf);
308 len = qstring_get_length(mon->outbuf);
309
Paolo Bonzinia4cc73d2013-05-31 14:00:27 +0200310 if (len && !mon->mux_out) {
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400311 rc = qemu_chr_fe_write(mon->chr, (const uint8_t *) buf, len);
Stratos Psomadakis056f49f2014-01-27 12:30:15 +0200312 if ((rc < 0 && errno != EAGAIN) || (rc == len)) {
313 /* all flushed or error */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400314 QDECREF(mon->outbuf);
315 mon->outbuf = qstring_new();
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100316 return;
317 }
318 if (rc > 0) {
319 /* partinal write */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400320 QString *tmp = qstring_from_str(buf + rc);
321 QDECREF(mon->outbuf);
322 mon->outbuf = tmp;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100323 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200324 if (mon->out_watch == 0) {
Roger Pau Monnee02bc6d2014-05-23 17:57:49 +0200325 mon->out_watch = qemu_chr_fe_add_watch(mon->chr, G_IO_OUT|G_IO_HUP,
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200326 monitor_unblocked, mon);
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200327 }
bellard7e2515e2004-08-01 21:52:19 +0000328 }
329}
330
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200331void monitor_flush(Monitor *mon)
332{
333 qemu_mutex_lock(&mon->out_lock);
334 monitor_flush_locked(mon);
335 qemu_mutex_unlock(&mon->out_lock);
336}
337
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400338/* flush at every end of line */
aliguori376253e2009-03-05 23:01:23 +0000339static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000340{
ths60fe76f2007-12-16 03:02:09 +0000341 char c;
aliguori731b0362009-03-05 23:01:42 +0000342
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200343 qemu_mutex_lock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000344 for(;;) {
345 c = *str++;
346 if (c == '\0')
347 break;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400348 if (c == '\n') {
349 qstring_append_chr(mon->outbuf, '\r');
350 }
351 qstring_append_chr(mon->outbuf, c);
352 if (c == '\n') {
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200353 monitor_flush_locked(mon);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400354 }
bellard7e2515e2004-08-01 21:52:19 +0000355 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200356 qemu_mutex_unlock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000357}
358
aliguori376253e2009-03-05 23:01:23 +0000359void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000360{
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400361 char *buf;
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200362
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200363 if (!mon)
364 return;
365
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100366 if (monitor_is_qmp(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200367 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200368 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200369
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400370 buf = g_strdup_vprintf(fmt, ap);
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200371 monitor_puts(mon, buf);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400372 g_free(buf);
bellard7e2515e2004-08-01 21:52:19 +0000373}
374
aliguori376253e2009-03-05 23:01:23 +0000375void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000376{
377 va_list ap;
378 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000379 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000380 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000381}
382
Pavel Butsykincaf15312015-09-22 16:18:17 +0300383int monitor_fprintf(FILE *stream, const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000384{
385 va_list ap;
386 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000387 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000388 va_end(ap);
389 return 0;
390}
391
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200392static void monitor_json_emitter(Monitor *mon, const QObject *data)
393{
394 QString *json;
395
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200396 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
397 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200398 assert(json != NULL);
399
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200400 qstring_append_chr(json, '\n');
401 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200402
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200403 QDECREF(json);
404}
405
Markus Armbruster710aec92015-03-06 11:28:00 +0100406static QDict *build_qmp_error_dict(Error *err)
Luiz Capitulinode253f12012-07-27 16:18:16 -0300407{
408 QObject *obj;
409
Markus Armbruster710aec92015-03-06 11:28:00 +0100410 obj = qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %s } }",
Eric Blakef22a28b2015-11-18 01:53:00 -0700411 QapiErrorClass_lookup[error_get_class(err)],
Markus Armbruster710aec92015-03-06 11:28:00 +0100412 error_get_pretty(err));
Luiz Capitulinode253f12012-07-27 16:18:16 -0300413
414 return qobject_to_qdict(obj);
415}
416
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100417static void monitor_protocol_emitter(Monitor *mon, QObject *data,
Markus Armbruster710aec92015-03-06 11:28:00 +0100418 Error *err)
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200419{
420 QDict *qmp;
421
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +0100422 trace_monitor_protocol_emitter(mon);
423
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100424 if (!err) {
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200425 /* success response */
Luiz Capitulinode253f12012-07-27 16:18:16 -0300426 qmp = qdict_new();
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200427 if (data) {
428 qobject_incref(data);
429 qdict_put_obj(qmp, "return", data);
430 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200431 /* return an empty QDict by default */
432 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200433 }
434 } else {
435 /* error response */
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100436 qmp = build_qmp_error_dict(err);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200437 }
438
Markus Armbruster74358f22015-03-06 19:35:59 +0100439 if (mon->qmp.id) {
440 qdict_put_obj(qmp, "id", mon->qmp.id);
441 mon->qmp.id = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200442 }
443
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200444 monitor_json_emitter(mon, QOBJECT(qmp));
445 QDECREF(qmp);
446}
447
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200448
Eric Blake7fb1cf12015-11-18 01:52:57 -0700449static MonitorQAPIEventConf monitor_qapi_event_conf[QAPI_EVENT__MAX] = {
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200450 /* Limit guest-triggerable events to 1 per second */
451 [QAPI_EVENT_RTC_CHANGE] = { 1000 * SCALE_MS },
452 [QAPI_EVENT_WATCHDOG] = { 1000 * SCALE_MS },
453 [QAPI_EVENT_BALLOON_CHANGE] = { 1000 * SCALE_MS },
454 [QAPI_EVENT_QUORUM_REPORT_BAD] = { 1000 * SCALE_MS },
455 [QAPI_EVENT_QUORUM_FAILURE] = { 1000 * SCALE_MS },
456 [QAPI_EVENT_VSERPORT_CHANGE] = { 1000 * SCALE_MS },
457};
458
Markus Armbrustera24712a2015-10-15 17:08:34 +0200459GHashTable *monitor_qapi_event_state;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100460
461/*
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200462 * Emits the event to every monitor instance, @event is only used for trace
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200463 * Called with monitor_lock held.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100464 */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200465static void monitor_qapi_event_emit(QAPIEvent event, QDict *qdict)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100466{
467 Monitor *mon;
468
Markus Armbruster688b4b72015-10-15 17:08:30 +0200469 trace_monitor_protocol_event_emit(event, qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100470 QLIST_FOREACH(mon, &mon_list, entry) {
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100471 if (monitor_is_qmp(mon) && mon->qmp.in_command_mode) {
Markus Armbruster688b4b72015-10-15 17:08:30 +0200472 monitor_json_emitter(mon, QOBJECT(qdict));
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100473 }
474 }
475}
476
Markus Armbrustera24712a2015-10-15 17:08:34 +0200477static void monitor_qapi_event_handler(void *opaque);
478
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100479/*
480 * Queue a new event for emission to Monitor instances,
481 * applying any rate limiting if required.
482 */
483static void
Markus Armbruster688b4b72015-10-15 17:08:30 +0200484monitor_qapi_event_queue(QAPIEvent event, QDict *qdict, Error **errp)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100485{
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200486 MonitorQAPIEventConf *evconf;
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200487 MonitorQAPIEventState *evstate;
Markus Armbruster93f8f982015-10-15 17:08:31 +0200488
Eric Blake7fb1cf12015-11-18 01:52:57 -0700489 assert(event < QAPI_EVENT__MAX);
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200490 evconf = &monitor_qapi_event_conf[event];
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200491 trace_monitor_protocol_event_queue(event, qdict, evconf->rate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100492
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200493 qemu_mutex_lock(&monitor_lock);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200494
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200495 if (!evconf->rate) {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200496 /* Unthrottled event */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200497 monitor_qapi_event_emit(event, qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100498 } else {
Markus Armbruster7de0be62015-10-15 17:08:35 +0200499 QDict *data = qobject_to_qdict(qdict_get(qdict, "data"));
500 MonitorQAPIEventState key = { .event = event, .data = data };
Markus Armbrustera24712a2015-10-15 17:08:34 +0200501
502 evstate = g_hash_table_lookup(monitor_qapi_event_state, &key);
503 assert(!evstate || timer_pending(evstate->timer));
504
505 if (evstate) {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200506 /*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200507 * Timer is pending for (at least) evconf->rate ns after
Markus Armbruster93f8f982015-10-15 17:08:31 +0200508 * last send. Store event for sending when timer fires,
509 * replacing a prior stored event if any.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100510 */
Markus Armbruster93f8f982015-10-15 17:08:31 +0200511 QDECREF(evstate->qdict);
Markus Armbruster688b4b72015-10-15 17:08:30 +0200512 evstate->qdict = qdict;
513 QINCREF(evstate->qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100514 } else {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200515 /*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200516 * Last send was (at least) evconf->rate ns ago.
Markus Armbruster93f8f982015-10-15 17:08:31 +0200517 * Send immediately, and arm the timer to call
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200518 * monitor_qapi_event_handler() in evconf->rate ns. Any
Markus Armbruster93f8f982015-10-15 17:08:31 +0200519 * events arriving before then will be delayed until then.
520 */
Alberto Garciadc599972016-03-10 13:55:26 +0200521 int64_t now = qemu_clock_get_ns(event_clock_type);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200522
Markus Armbruster688b4b72015-10-15 17:08:30 +0200523 monitor_qapi_event_emit(event, qdict);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200524
525 evstate = g_new(MonitorQAPIEventState, 1);
526 evstate->event = event;
Markus Armbruster7de0be62015-10-15 17:08:35 +0200527 evstate->data = data;
528 QINCREF(evstate->data);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200529 evstate->qdict = NULL;
Alberto Garciadc599972016-03-10 13:55:26 +0200530 evstate->timer = timer_new_ns(event_clock_type,
Markus Armbrustera24712a2015-10-15 17:08:34 +0200531 monitor_qapi_event_handler,
532 evstate);
533 g_hash_table_add(monitor_qapi_event_state, evstate);
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200534 timer_mod_ns(evstate->timer, now + evconf->rate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100535 }
536 }
Markus Armbruster93f8f982015-10-15 17:08:31 +0200537
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200538 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100539}
540
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100541/*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200542 * This function runs evconf->rate ns after sending a throttled
Markus Armbruster93f8f982015-10-15 17:08:31 +0200543 * event.
544 * If another event has since been stored, send it.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100545 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200546static void monitor_qapi_event_handler(void *opaque)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100547{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200548 MonitorQAPIEventState *evstate = opaque;
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200549 MonitorQAPIEventConf *evconf = &monitor_qapi_event_conf[evstate->event];
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100550
Markus Armbruster93f8f982015-10-15 17:08:31 +0200551 trace_monitor_protocol_event_handler(evstate->event, evstate->qdict);
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200552 qemu_mutex_lock(&monitor_lock);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200553
Markus Armbruster688b4b72015-10-15 17:08:30 +0200554 if (evstate->qdict) {
Alberto Garciadc599972016-03-10 13:55:26 +0200555 int64_t now = qemu_clock_get_ns(event_clock_type);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200556
Markus Armbruster688b4b72015-10-15 17:08:30 +0200557 monitor_qapi_event_emit(evstate->event, evstate->qdict);
558 QDECREF(evstate->qdict);
559 evstate->qdict = NULL;
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200560 timer_mod_ns(evstate->timer, now + evconf->rate);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200561 } else {
562 g_hash_table_remove(monitor_qapi_event_state, evstate);
Markus Armbruster7de0be62015-10-15 17:08:35 +0200563 QDECREF(evstate->data);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200564 timer_free(evstate->timer);
565 g_free(evstate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100566 }
Markus Armbruster93f8f982015-10-15 17:08:31 +0200567
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200568 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100569}
570
Markus Armbrustera24712a2015-10-15 17:08:34 +0200571static unsigned int qapi_event_throttle_hash(const void *key)
572{
573 const MonitorQAPIEventState *evstate = key;
Markus Armbruster7de0be62015-10-15 17:08:35 +0200574 unsigned int hash = evstate->event * 255;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200575
Markus Armbruster7de0be62015-10-15 17:08:35 +0200576 if (evstate->event == QAPI_EVENT_VSERPORT_CHANGE) {
577 hash += g_str_hash(qdict_get_str(evstate->data, "id"));
578 }
579
Alberto Garcia6d425eb2016-03-10 13:55:25 +0200580 if (evstate->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
581 hash += g_str_hash(qdict_get_str(evstate->data, "node-name"));
582 }
583
Markus Armbruster7de0be62015-10-15 17:08:35 +0200584 return hash;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200585}
586
587static gboolean qapi_event_throttle_equal(const void *a, const void *b)
588{
589 const MonitorQAPIEventState *eva = a;
590 const MonitorQAPIEventState *evb = b;
591
Markus Armbruster7de0be62015-10-15 17:08:35 +0200592 if (eva->event != evb->event) {
593 return FALSE;
594 }
595
596 if (eva->event == QAPI_EVENT_VSERPORT_CHANGE) {
597 return !strcmp(qdict_get_str(eva->data, "id"),
598 qdict_get_str(evb->data, "id"));
599 }
600
Alberto Garcia6d425eb2016-03-10 13:55:25 +0200601 if (eva->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
602 return !strcmp(qdict_get_str(eva->data, "node-name"),
603 qdict_get_str(evb->data, "node-name"));
604 }
605
Markus Armbruster7de0be62015-10-15 17:08:35 +0200606 return TRUE;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200607}
608
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200609static void monitor_qapi_event_init(void)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100610{
Alberto Garciadc599972016-03-10 13:55:26 +0200611 if (qtest_enabled()) {
612 event_clock_type = QEMU_CLOCK_VIRTUAL;
613 }
614
Markus Armbrustera24712a2015-10-15 17:08:34 +0200615 monitor_qapi_event_state = g_hash_table_new(qapi_event_throttle_hash,
616 qapi_event_throttle_equal);
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200617 qmp_event_set_func_emit(monitor_qapi_event_queue);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200618}
619
Markus Armbruster485febc2015-03-13 17:25:50 +0100620static void qmp_capabilities(QDict *params, QObject **ret_data, Error **errp)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200621{
Markus Armbruster485febc2015-03-13 17:25:50 +0100622 cur_mon->qmp.in_command_mode = true;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200623}
624
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100625static void handle_hmp_command(Monitor *mon, const char *cmdline);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200626
Wenchao Xiab01fe892013-08-27 20:38:19 +0800627static void monitor_data_init(Monitor *mon)
628{
629 memset(mon, 0, sizeof(Monitor));
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200630 qemu_mutex_init(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800631 mon->outbuf = qstring_new();
Wenchao Xia77172392013-08-27 20:38:20 +0800632 /* Use *mon_cmds by default. */
633 mon->cmd_table = mon_cmds;
Wenchao Xiab01fe892013-08-27 20:38:19 +0800634}
635
636static void monitor_data_destroy(Monitor *mon)
637{
638 QDECREF(mon->outbuf);
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200639 qemu_mutex_destroy(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800640}
641
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200642char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
643 int64_t cpu_index, Error **errp)
Luiz Capitulino0268d972010-10-22 10:08:02 -0200644{
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200645 char *output = NULL;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200646 Monitor *old_mon, hmp;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200647
Wenchao Xiab01fe892013-08-27 20:38:19 +0800648 monitor_data_init(&hmp);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400649 hmp.skip_flush = true;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200650
651 old_mon = cur_mon;
652 cur_mon = &hmp;
653
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200654 if (has_cpu_index) {
655 int ret = monitor_set_cpu(cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200656 if (ret < 0) {
657 cur_mon = old_mon;
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100658 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
659 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200660 goto out;
661 }
662 }
663
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100664 handle_hmp_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200665 cur_mon = old_mon;
666
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200667 qemu_mutex_lock(&hmp.out_lock);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400668 if (qstring_get_length(hmp.outbuf) > 0) {
669 output = g_strdup(qstring_get_str(hmp.outbuf));
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200670 } else {
671 output = g_strdup("");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200672 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200673 qemu_mutex_unlock(&hmp.out_lock);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200674
675out:
Wenchao Xiab01fe892013-08-27 20:38:19 +0800676 monitor_data_destroy(&hmp);
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200677 return output;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200678}
679
bellard9dc39cb2004-03-14 21:38:27 +0000680static int compare_cmd(const char *name, const char *list)
681{
682 const char *p, *pstart;
683 int len;
684 len = strlen(name);
685 p = list;
686 for(;;) {
687 pstart = p;
688 p = strchr(p, '|');
689 if (!p)
690 p = pstart + strlen(pstart);
691 if ((p - pstart) == len && !memcmp(pstart, name, len))
692 return 1;
693 if (*p == '\0')
694 break;
695 p++;
696 }
697 return 0;
698}
699
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800700static int get_str(char *buf, int buf_size, const char **pp)
701{
702 const char *p;
703 char *q;
704 int c;
705
706 q = buf;
707 p = *pp;
708 while (qemu_isspace(*p)) {
709 p++;
710 }
711 if (*p == '\0') {
712 fail:
713 *q = '\0';
714 *pp = p;
715 return -1;
716 }
717 if (*p == '\"') {
718 p++;
719 while (*p != '\0' && *p != '\"') {
720 if (*p == '\\') {
721 p++;
722 c = *p++;
723 switch (c) {
724 case 'n':
725 c = '\n';
726 break;
727 case 'r':
728 c = '\r';
729 break;
730 case '\\':
731 case '\'':
732 case '\"':
733 break;
734 default:
Peter Maydell71baf782015-08-19 16:20:19 +0100735 printf("unsupported escape code: '\\%c'\n", c);
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800736 goto fail;
737 }
738 if ((q - buf) < buf_size - 1) {
739 *q++ = c;
740 }
741 } else {
742 if ((q - buf) < buf_size - 1) {
743 *q++ = *p;
744 }
745 p++;
746 }
747 }
748 if (*p != '\"') {
Peter Maydell71baf782015-08-19 16:20:19 +0100749 printf("unterminated string\n");
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800750 goto fail;
751 }
752 p++;
753 } else {
754 while (*p != '\0' && !qemu_isspace(*p)) {
755 if ((q - buf) < buf_size - 1) {
756 *q++ = *p;
757 }
758 p++;
759 }
760 }
761 *q = '\0';
762 *pp = p;
763 return 0;
764}
765
766#define MAX_ARGS 16
767
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800768static void free_cmdline_args(char **args, int nb_args)
769{
770 int i;
771
772 assert(nb_args <= MAX_ARGS);
773
774 for (i = 0; i < nb_args; i++) {
775 g_free(args[i]);
776 }
777
778}
779
780/*
781 * Parse the command line to get valid args.
782 * @cmdline: command line to be parsed.
783 * @pnb_args: location to store the number of args, must NOT be NULL.
784 * @args: location to store the args, which should be freed by caller, must
785 * NOT be NULL.
786 *
787 * Returns 0 on success, negative on failure.
788 *
789 * NOTE: this parser is an approximate form of the real command parser. Number
790 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
791 * return with failure.
792 */
793static int parse_cmdline(const char *cmdline,
794 int *pnb_args, char **args)
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800795{
796 const char *p;
797 int nb_args, ret;
798 char buf[1024];
799
800 p = cmdline;
801 nb_args = 0;
802 for (;;) {
803 while (qemu_isspace(*p)) {
804 p++;
805 }
806 if (*p == '\0') {
807 break;
808 }
809 if (nb_args >= MAX_ARGS) {
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800810 goto fail;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800811 }
812 ret = get_str(buf, sizeof(buf), &p);
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800813 if (ret < 0) {
814 goto fail;
815 }
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800816 args[nb_args] = g_strdup(buf);
817 nb_args++;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800818 }
819 *pnb_args = nb_args;
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800820 return 0;
821
822 fail:
823 free_cmdline_args(args, nb_args);
824 return -1;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800825}
826
Wenchao Xia66855492013-08-27 20:38:23 +0800827static void help_cmd_dump_one(Monitor *mon,
828 const mon_cmd_t *cmd,
829 char **prefix_args,
830 int prefix_args_nb)
831{
832 int i;
833
834 for (i = 0; i < prefix_args_nb; i++) {
835 monitor_printf(mon, "%s ", prefix_args[i]);
836 }
837 monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
838}
839
840/* @args[@arg_index] is the valid command need to find in @cmds */
Anthony Liguoric227f092009-10-01 16:12:16 -0500841static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
Wenchao Xia66855492013-08-27 20:38:23 +0800842 char **args, int nb_args, int arg_index)
bellard9dc39cb2004-03-14 21:38:27 +0000843{
Anthony Liguoric227f092009-10-01 16:12:16 -0500844 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000845
Wenchao Xia66855492013-08-27 20:38:23 +0800846 /* No valid arg need to compare with, dump all in *cmds */
847 if (arg_index >= nb_args) {
848 for (cmd = cmds; cmd->name != NULL; cmd++) {
849 help_cmd_dump_one(mon, cmd, args, arg_index);
850 }
851 return;
852 }
853
854 /* Find one entry to dump */
855 for (cmd = cmds; cmd->name != NULL; cmd++) {
856 if (compare_cmd(args[arg_index], cmd->name)) {
857 if (cmd->sub_table) {
858 /* continue with next arg */
859 help_cmd_dump(mon, cmd->sub_table,
860 args, nb_args, arg_index + 1);
861 } else {
862 help_cmd_dump_one(mon, cmd, args, arg_index);
863 }
864 break;
865 }
bellard9dc39cb2004-03-14 21:38:27 +0000866 }
867}
868
aliguori376253e2009-03-05 23:01:23 +0000869static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000870{
Wenchao Xia66855492013-08-27 20:38:23 +0800871 char *args[MAX_ARGS];
872 int nb_args = 0;
873
874 /* 1. parse user input */
875 if (name) {
876 /* special case for log, directly dump and return */
877 if (!strcmp(name, "log")) {
Peter Maydell38dad9e2013-02-11 16:41:25 +0000878 const QEMULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000879 monitor_printf(mon, "Log items (comma separated):\n");
880 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
Peter Maydell38dad9e2013-02-11 16:41:25 +0000881 for (item = qemu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000882 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000883 }
Wenchao Xia66855492013-08-27 20:38:23 +0800884 return;
885 }
886
887 if (parse_cmdline(name, &nb_args, args) < 0) {
888 return;
bellardf193c792004-03-21 17:06:25 +0000889 }
bellard9dc39cb2004-03-14 21:38:27 +0000890 }
Wenchao Xia66855492013-08-27 20:38:23 +0800891
892 /* 2. dump the contents according to parsed args */
893 help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0);
894
895 free_cmdline_args(args, nb_args);
bellard9dc39cb2004-03-14 21:38:27 +0000896}
897
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300898static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300899{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300900 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300901}
902
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100903static void hmp_trace_event(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530904{
905 const char *tp_name = qdict_get_str(qdict, "name");
906 bool new_state = qdict_get_bool(qdict, "option");
Lluís Vilanova14101d02014-08-25 13:20:03 +0200907 Error *local_err = NULL;
Blue Swirlf871d682010-10-13 19:14:29 +0000908
Lluís Vilanova14101d02014-08-25 13:20:03 +0200909 qmp_trace_event_set_state(tp_name, new_state, true, true, &local_err);
910 if (local_err) {
Markus Armbruster091e38b2015-02-10 15:15:43 +0100911 error_report_err(local_err);
Blue Swirlf871d682010-10-13 19:14:29 +0000912 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530913}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100914
Michael Rothc45a8162011-10-02 08:44:37 -0500915#ifdef CONFIG_TRACE_SIMPLE
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100916static void hmp_trace_file(Monitor *mon, const QDict *qdict)
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100917{
918 const char *op = qdict_get_try_str(qdict, "op");
919 const char *arg = qdict_get_try_str(qdict, "arg");
920
921 if (!op) {
922 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
923 } else if (!strcmp(op, "on")) {
924 st_set_trace_file_enabled(true);
925 } else if (!strcmp(op, "off")) {
926 st_set_trace_file_enabled(false);
927 } else if (!strcmp(op, "flush")) {
928 st_flush_trace_buffer();
929 } else if (!strcmp(op, "set")) {
930 if (arg) {
931 st_set_trace_file(arg);
932 }
933 } else {
934 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
935 help_cmd(mon, "trace-file");
936 }
937}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530938#endif
939
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100940static void hmp_info_help(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000941{
Luiz Capitulino13c74252009-10-07 13:41:55 -0300942 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000943}
944
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300945CommandInfoList *qmp_query_commands(Error **errp)
bellard9bc9d1c2004-10-10 15:15:51 +0000946{
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300947 CommandInfoList *info, *cmd_list = NULL;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200948 const mon_cmd_t *cmd;
949
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300950 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino40e5a012011-10-21 16:15:31 -0200951 info = g_malloc0(sizeof(*info));
952 info->value = g_malloc0(sizeof(*info->value));
953 info->value->name = g_strdup(cmd->name);
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200954
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300955 info->next = cmd_list;
956 cmd_list = info;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200957 }
958
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300959 return cmd_list;
thsa36e69d2007-12-02 05:18:19 +0000960}
961
Daniel P. Berrange48608532012-05-21 17:59:51 +0100962EventInfoList *qmp_query_events(Error **errp)
963{
964 EventInfoList *info, *ev_list = NULL;
Wenchao Xia75175172014-06-18 08:43:54 +0200965 QAPIEvent e;
Daniel P. Berrange48608532012-05-21 17:59:51 +0100966
Eric Blake7fb1cf12015-11-18 01:52:57 -0700967 for (e = 0 ; e < QAPI_EVENT__MAX ; e++) {
Wenchao Xia75175172014-06-18 08:43:54 +0200968 const char *event_name = QAPIEvent_lookup[e];
Daniel P. Berrange48608532012-05-21 17:59:51 +0100969 assert(event_name != NULL);
970 info = g_malloc0(sizeof(*info));
971 info->value = g_malloc0(sizeof(*info->value));
972 info->value->name = g_strdup(event_name);
973
974 info->next = ev_list;
975 ev_list = info;
976 }
977
978 return ev_list;
979}
980
Markus Armbruster39a18152015-09-16 13:06:28 +0200981/*
982 * Minor hack: generated marshalling suppressed for this command
983 * ('gen': false in the schema) so we can parse the JSON string
984 * directly into QObject instead of first parsing it with
985 * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
986 * to QObject with generated output marshallers, every time. Instead,
987 * we do it in test-qmp-input-visitor.c, just to make sure
988 * qapi-introspect.py's output actually conforms to the schema.
989 */
990static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
991 Error **errp)
992{
993 *ret_data = qobject_from_json(qmp_schema_json);
994}
995
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300996/* set the current CPU defined by the user */
997int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000998{
Andreas Färber55e5c282012-12-17 06:18:02 +0100999 CPUState *cpu;
bellard6a00d602005-11-21 23:25:50 +00001000
Andreas Färber1c8bb3c2013-02-15 17:01:09 +01001001 cpu = qemu_get_cpu(cpu_index);
1002 if (cpu == NULL) {
1003 return -1;
bellard6a00d602005-11-21 23:25:50 +00001004 }
Andreas Färbercb446ec2013-05-01 14:24:52 +02001005 cur_mon->mon_cpu = cpu;
Andreas Färber1c8bb3c2013-02-15 17:01:09 +01001006 return 0;
bellard6a00d602005-11-21 23:25:50 +00001007}
1008
Pavel Butsykincaf15312015-09-22 16:18:17 +03001009CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +00001010{
aliguori731b0362009-03-05 23:01:42 +00001011 if (!cur_mon->mon_cpu) {
Luiz Capitulinob025c8b2011-10-06 14:02:57 -03001012 monitor_set_cpu(0);
bellard6a00d602005-11-21 23:25:50 +00001013 }
Avi Kivity4c0960c2009-08-17 23:19:53 +03001014 cpu_synchronize_state(cur_mon->mon_cpu);
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001015 return cur_mon->mon_cpu;
1016}
1017
Pavel Butsykinbf957282015-09-10 18:38:59 +03001018CPUArchState *mon_get_cpu_env(void)
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001019{
1020 return mon_get_cpu()->env_ptr;
bellard6a00d602005-11-21 23:25:50 +00001021}
1022
Luiz Capitulino99b77962011-10-24 10:53:44 -02001023int monitor_get_cpu_index(void)
1024{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001025 return mon_get_cpu()->cpu_index;
Luiz Capitulino99b77962011-10-24 10:53:44 -02001026}
1027
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001028static void hmp_info_registers(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001029{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001030 cpu_dump_state(mon_get_cpu(), (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +00001031}
1032
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001033static void hmp_info_jit(Monitor *mon, const QDict *qdict)
bellarde3db7222005-01-26 22:00:47 +00001034{
aliguori376253e2009-03-05 23:01:23 +00001035 dump_exec_info((FILE *)mon, monitor_fprintf);
Sebastian Tanase27498be2014-07-25 11:56:33 +02001036 dump_drift_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +00001037}
1038
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001039static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
Max Filippov246ae242014-11-02 11:04:18 +03001040{
1041 dump_opcount_info((FILE *)mon, monitor_fprintf);
1042}
1043
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001044static void hmp_info_history(Monitor *mon, const QDict *qdict)
bellardaa455482004-04-04 13:07:25 +00001045{
1046 int i;
bellard7e2515e2004-08-01 21:52:19 +00001047 const char *str;
ths3b46e622007-09-17 08:09:54 +00001048
aliguoricde76ee2009-03-05 23:01:51 +00001049 if (!mon->rs)
1050 return;
bellard7e2515e2004-08-01 21:52:19 +00001051 i = 0;
1052 for(;;) {
aliguori731b0362009-03-05 23:01:42 +00001053 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +00001054 if (!str)
1055 break;
aliguori376253e2009-03-05 23:01:23 +00001056 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +00001057 i++;
bellardaa455482004-04-04 13:07:25 +00001058 }
1059}
1060
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001061static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
j_mayer76a66252007-03-07 08:32:30 +00001062{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001063 cpu_dump_statistics(mon_get_cpu(), (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +00001064}
j_mayer76a66252007-03-07 08:32:30 +00001065
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001066static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301067{
Lluís Vilanova14101d02014-08-25 13:20:03 +02001068 TraceEventInfoList *events = qmp_trace_event_get_state("*", NULL);
1069 TraceEventInfoList *elem;
1070
1071 for (elem = events; elem != NULL; elem = elem->next) {
1072 monitor_printf(mon, "%s : state %u\n",
1073 elem->value->name,
1074 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
1075 }
1076 qapi_free_TraceEventInfoList(events);
Prerna Saxena22890ab2010-06-24 17:04:53 +05301077}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301078
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001079void qmp_client_migrate_info(const char *protocol, const char *hostname,
1080 bool has_port, int64_t port,
1081 bool has_tls_port, int64_t tls_port,
1082 bool has_cert_subject, const char *cert_subject,
1083 Error **errp)
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001084{
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001085 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001086 if (!qemu_using_spice(errp)) {
1087 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001088 }
1089
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001090 if (!has_port && !has_tls_port) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001091 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001092 return;
Yonit Halperin6ec5dae2012-03-18 09:42:39 +02001093 }
1094
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001095 if (qemu_spice_migrate_info(hostname,
1096 has_port ? port : -1,
1097 has_tls_port ? tls_port : -1,
1098 cert_subject)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001099 error_setg(errp, QERR_UNDEFINED_ERROR);
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001100 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001101 }
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001102 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001103 }
1104
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001105 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001106}
1107
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001108static void hmp_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001109{
Peter Maydell9a7e5422013-02-11 16:41:20 +00001110 qemu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001111}
1112
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001113static void hmp_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001114{
1115 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001116 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001117
bellard9307c4c2004-04-04 12:57:25 +00001118 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001119 mask = 0;
1120 } else {
Peter Maydell4fde1eb2013-02-11 16:41:22 +00001121 mask = qemu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001122 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001123 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001124 return;
1125 }
1126 }
Peter Maydell24537a02013-02-11 16:41:23 +00001127 qemu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00001128}
1129
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001130static void hmp_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001131{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001132 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001133 if (!option || !strcmp(option, "on")) {
1134 singlestep = 1;
1135 } else if (!strcmp(option, "off")) {
1136 singlestep = 0;
1137 } else {
1138 monitor_printf(mon, "unexpected option %s\n", option);
1139 }
1140}
1141
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001142static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001143{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001144 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001145 if (!device)
1146 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1147 if (gdbserver_start(device) < 0) {
1148 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1149 device);
1150 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001151 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001152 } else {
aliguori59030a82009-04-05 18:43:41 +00001153 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1154 device);
bellard8a7ddc32004-03-31 19:00:16 +00001155 }
1156}
1157
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001158static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001159{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001160 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001161 if (select_watchdog_action(action) == -1) {
1162 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1163 }
1164}
1165
aliguori376253e2009-03-05 23:01:23 +00001166static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001167{
aliguori376253e2009-03-05 23:01:23 +00001168 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001169 switch(c) {
1170 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001171 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001172 break;
1173 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001174 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001175 break;
1176 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001177 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001178 break;
1179 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001180 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001181 break;
1182 default:
1183 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001184 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001185 } else {
aliguori376253e2009-03-05 23:01:23 +00001186 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001187 }
1188 break;
1189 }
aliguori376253e2009-03-05 23:01:23 +00001190 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001191}
1192
aliguori376253e2009-03-05 23:01:23 +00001193static void memory_dump(Monitor *mon, int count, int format, int wsize,
Avi Kivitya8170e52012-10-23 12:30:10 +02001194 hwaddr addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001195{
Blue Swirl23842aa2010-01-12 20:27:43 +00001196 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001197 uint8_t buf[16];
1198 uint64_t v;
1199
1200 if (format == 'i') {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001201 int flags = 0;
bellard9307c4c2004-04-04 12:57:25 +00001202#ifdef TARGET_I386
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001203 CPUArchState *env = mon_get_cpu_env();
bellard4c27ba22004-04-25 18:05:08 +00001204 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001205 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001206 } else if (wsize == 4) {
1207 flags = 0;
1208 } else {
bellard6a15fd12006-04-12 21:07:07 +00001209 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001210 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001211 if (env) {
1212#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001213 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001214 (env->segs[R_CS].flags & DESC_L_MASK))
1215 flags = 2;
1216 else
1217#endif
1218 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1219 flags = 1;
1220 }
bellard4c27ba22004-04-25 18:05:08 +00001221 }
1222#endif
Tom Musta1c38f842014-04-09 14:53:24 -05001223#ifdef TARGET_PPC
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001224 CPUArchState *env = mon_get_cpu_env();
Tom Musta1c38f842014-04-09 14:53:24 -05001225 flags = msr_le << 16;
1226 flags |= env->bfd_mach;
1227#endif
Peter Crosthwaited49190c2015-05-24 14:20:41 -07001228 monitor_disas(mon, mon_get_cpu(), addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001229 return;
1230 }
1231
1232 len = wsize * count;
1233 if (wsize == 1)
1234 line_size = 8;
1235 else
1236 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001237 max_digits = 0;
1238
1239 switch(format) {
1240 case 'o':
1241 max_digits = (wsize * 8 + 2) / 3;
1242 break;
1243 default:
1244 case 'x':
1245 max_digits = (wsize * 8) / 4;
1246 break;
1247 case 'u':
1248 case 'd':
1249 max_digits = (wsize * 8 * 10 + 32) / 33;
1250 break;
1251 case 'c':
1252 wsize = 1;
1253 break;
1254 }
1255
1256 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001257 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001258 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001259 else
aliguori376253e2009-03-05 23:01:23 +00001260 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001261 l = len;
1262 if (l > line_size)
1263 l = line_size;
1264 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001265 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001266 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001267 if (cpu_memory_rw_debug(mon_get_cpu(), addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001268 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001269 break;
1270 }
bellard9307c4c2004-04-04 12:57:25 +00001271 }
ths5fafdf22007-09-16 21:08:06 +00001272 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001273 while (i < l) {
1274 switch(wsize) {
1275 default:
1276 case 1:
Peter Maydell24e60302015-01-20 15:19:32 +00001277 v = ldub_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001278 break;
1279 case 2:
Peter Maydell24e60302015-01-20 15:19:32 +00001280 v = lduw_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001281 break;
1282 case 4:
Peter Maydell24e60302015-01-20 15:19:32 +00001283 v = (uint32_t)ldl_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001284 break;
1285 case 8:
Peter Maydell24e60302015-01-20 15:19:32 +00001286 v = ldq_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001287 break;
1288 }
aliguori376253e2009-03-05 23:01:23 +00001289 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001290 switch(format) {
1291 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001292 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001293 break;
1294 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001295 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001296 break;
1297 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001298 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001299 break;
1300 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001301 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001302 break;
1303 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001304 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001305 break;
1306 }
1307 i += wsize;
1308 }
aliguori376253e2009-03-05 23:01:23 +00001309 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001310 addr += l;
1311 len -= l;
1312 }
1313}
1314
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001315static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001316{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001317 int count = qdict_get_int(qdict, "count");
1318 int format = qdict_get_int(qdict, "format");
1319 int size = qdict_get_int(qdict, "size");
1320 target_long addr = qdict_get_int(qdict, "addr");
1321
aliguori376253e2009-03-05 23:01:23 +00001322 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001323}
1324
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001325static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001326{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001327 int count = qdict_get_int(qdict, "count");
1328 int format = qdict_get_int(qdict, "format");
1329 int size = qdict_get_int(qdict, "size");
Avi Kivitya8170e52012-10-23 12:30:10 +02001330 hwaddr addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001331
aliguori376253e2009-03-05 23:01:23 +00001332 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001333}
1334
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001335static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001336{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001337 int format = qdict_get_int(qdict, "format");
Avi Kivitya8170e52012-10-23 12:30:10 +02001338 hwaddr val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001339
bellard9307c4c2004-04-04 12:57:25 +00001340 switch(format) {
1341 case 'o':
Avi Kivitya8170e52012-10-23 12:30:10 +02001342 monitor_printf(mon, "%#" HWADDR_PRIo, val);
bellard9307c4c2004-04-04 12:57:25 +00001343 break;
1344 case 'x':
Avi Kivitya8170e52012-10-23 12:30:10 +02001345 monitor_printf(mon, "%#" HWADDR_PRIx, val);
bellard9307c4c2004-04-04 12:57:25 +00001346 break;
1347 case 'u':
Avi Kivitya8170e52012-10-23 12:30:10 +02001348 monitor_printf(mon, "%" HWADDR_PRIu, val);
bellard9307c4c2004-04-04 12:57:25 +00001349 break;
1350 default:
1351 case 'd':
Avi Kivitya8170e52012-10-23 12:30:10 +02001352 monitor_printf(mon, "%" HWADDR_PRId, val);
bellard9307c4c2004-04-04 12:57:25 +00001353 break;
1354 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001355 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001356 break;
1357 }
aliguori376253e2009-03-05 23:01:23 +00001358 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001359}
1360
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001361static void hmp_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001362{
1363 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001364 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001365 uint32_t start = qdict_get_int(qdict, "start");
1366 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001367
1368 sum = 0;
1369 for(addr = start; addr < (start + size); addr++) {
Peter Maydell42874d32015-04-26 16:49:24 +01001370 uint8_t val = address_space_ldub(&address_space_memory, addr,
1371 MEMTXATTRS_UNSPECIFIED, NULL);
bellarde4cf1ad2005-06-04 20:15:57 +00001372 /* BSD sum algorithm ('sum' Unix command) */
1373 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001374 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001375 }
aliguori376253e2009-03-05 23:01:23 +00001376 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001377}
1378
bellard13224a82006-07-14 22:03:35 +00001379static int mouse_button_state;
1380
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001381static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001382{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001383 int dx, dy, dz, button;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001384 const char *dx_str = qdict_get_str(qdict, "dx_str");
1385 const char *dy_str = qdict_get_str(qdict, "dy_str");
1386 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001387
bellard13224a82006-07-14 22:03:35 +00001388 dx = strtol(dx_str, NULL, 0);
1389 dy = strtol(dy_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001390 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
1391 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
1392
1393 if (dz_str) {
bellard13224a82006-07-14 22:03:35 +00001394 dz = strtol(dz_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001395 if (dz != 0) {
Gerd Hoffmannf22d0af2016-01-12 12:14:12 +01001396 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001397 qemu_input_queue_btn(NULL, button, true);
1398 qemu_input_event_sync();
1399 qemu_input_queue_btn(NULL, button, false);
1400 }
1401 }
1402 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001403}
1404
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001405static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001406{
Eric Blake7fb1cf12015-11-18 01:52:57 -07001407 static uint32_t bmap[INPUT_BUTTON__MAX] = {
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001408 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1409 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
1410 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1411 };
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001412 int button_state = qdict_get_int(qdict, "button_state");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001413
1414 if (mouse_button_state == button_state) {
1415 return;
1416 }
1417 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1418 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001419 mouse_button_state = button_state;
bellard13224a82006-07-14 22:03:35 +00001420}
1421
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001422static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001423{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001424 int size = qdict_get_int(qdict, "size");
1425 int addr = qdict_get_int(qdict, "addr");
1426 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001427 uint32_t val;
1428 int suffix;
1429
1430 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001431 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001432 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001433 addr++;
1434 }
1435 addr &= 0xffff;
1436
1437 switch(size) {
1438 default:
1439 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001440 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001441 suffix = 'b';
1442 break;
1443 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001444 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001445 suffix = 'w';
1446 break;
1447 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001448 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001449 suffix = 'l';
1450 break;
1451 }
aliguori376253e2009-03-05 23:01:23 +00001452 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1453 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001454}
bellarda3a91a32004-06-04 11:06:21 +00001455
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001456static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001457{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001458 int size = qdict_get_int(qdict, "size");
1459 int addr = qdict_get_int(qdict, "addr");
1460 int val = qdict_get_int(qdict, "val");
1461
Jan Kiszkaf1147842009-07-14 10:20:11 +02001462 addr &= IOPORTS_MASK;
1463
1464 switch (size) {
1465 default:
1466 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001467 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001468 break;
1469 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001470 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001471 break;
1472 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001473 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001474 break;
1475 }
1476}
1477
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001478static void hmp_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001479{
Gongleif1839932014-12-03 18:20:58 +00001480 Error *local_err = NULL;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001481 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001482
Gongleif1839932014-12-03 18:20:58 +00001483 qemu_boot_set(bootdevice, &local_err);
1484 if (local_err) {
Markus Armbruster193227f2015-12-18 16:35:06 +01001485 error_report_err(local_err);
aurel320ecdffb2008-05-04 20:11:34 +00001486 } else {
Gongleif1839932014-12-03 18:20:58 +00001487 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001488 }
1489}
1490
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001491static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
Blue Swirl314e2982011-09-11 20:22:05 +00001492{
1493 mtree_info((fprintf_function)monitor_printf, mon);
1494}
1495
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001496static void hmp_info_numa(Monitor *mon, const QDict *qdict)
aliguori030ea372009-04-21 22:30:47 +00001497{
aliguorib28b6232009-04-22 20:20:29 +00001498 int i;
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001499 CPUState *cpu;
zhanghailiang5b009e42014-11-04 19:49:30 +08001500 uint64_t *node_mem;
aliguori030ea372009-04-21 22:30:47 +00001501
zhanghailiang5b009e42014-11-04 19:49:30 +08001502 node_mem = g_new0(uint64_t, nb_numa_nodes);
1503 query_numa_node_mem(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001504 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1505 for (i = 0; i < nb_numa_nodes; i++) {
1506 monitor_printf(mon, "node %d cpus:", i);
Andreas Färberbdc44642013-06-24 23:50:24 +02001507 CPU_FOREACH(cpu) {
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001508 if (cpu->numa_node == i) {
Andreas Färber55e5c282012-12-17 06:18:02 +01001509 monitor_printf(mon, " %d", cpu->cpu_index);
aliguori030ea372009-04-21 22:30:47 +00001510 }
1511 }
1512 monitor_printf(mon, "\n");
1513 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
zhanghailiang5b009e42014-11-04 19:49:30 +08001514 node_mem[i] >> 20);
aliguori030ea372009-04-21 22:30:47 +00001515 }
zhanghailiang5b009e42014-11-04 19:49:30 +08001516 g_free(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001517}
1518
bellard5f1ce942006-02-08 22:40:15 +00001519#ifdef CONFIG_PROFILER
1520
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001521int64_t tcg_time;
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001522int64_t dev_time;
1523
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001524static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001525{
aliguori376253e2009-03-05 23:01:23 +00001526 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Rutuja Shah73bcb242016-03-21 21:32:30 +05301527 dev_time, dev_time / (double)NANOSECONDS_PER_SECOND);
aliguori376253e2009-03-05 23:01:23 +00001528 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Rutuja Shah73bcb242016-03-21 21:32:30 +05301529 tcg_time, tcg_time / (double)NANOSECONDS_PER_SECOND);
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001530 tcg_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001531 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001532}
1533#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001534static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001535{
aliguori376253e2009-03-05 23:01:23 +00001536 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001537}
1538#endif
1539
bellardec36b692006-07-16 18:57:03 +00001540/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001541static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001542
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001543static void hmp_info_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001544{
1545 int i;
1546 CaptureState *s;
1547
1548 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001549 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001550 s->ops.info (s->opaque);
1551 }
1552}
1553
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001554static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001555{
1556 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001557 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001558 CaptureState *s;
1559
1560 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1561 if (i == n) {
1562 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001563 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001564 g_free (s);
bellardec36b692006-07-16 18:57:03 +00001565 return;
1566 }
1567 }
1568}
1569
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001570static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001571{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001572 const char *path = qdict_get_str(qdict, "path");
1573 int has_freq = qdict_haskey(qdict, "freq");
1574 int freq = qdict_get_try_int(qdict, "freq", -1);
1575 int has_bits = qdict_haskey(qdict, "bits");
1576 int bits = qdict_get_try_int(qdict, "bits", -1);
1577 int has_channels = qdict_haskey(qdict, "nchannels");
1578 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001579 CaptureState *s;
1580
Anthony Liguori7267c092011-08-20 22:09:37 -05001581 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001582
1583 freq = has_freq ? freq : 44100;
1584 bits = has_bits ? bits : 16;
1585 nchannels = has_channels ? nchannels : 2;
1586
1587 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001588 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05001589 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001590 return;
bellardec36b692006-07-16 18:57:03 +00001591 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001592 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001593}
bellardec36b692006-07-16 18:57:03 +00001594
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001595static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001596{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001597 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001598
aliguori76655d62009-03-06 20:27:37 +00001599 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001600 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001601 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001602 return acl;
1603}
aliguori76655d62009-03-06 20:27:37 +00001604
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001605static void hmp_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001606{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001607 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001608 qemu_acl *acl = find_acl(mon, aclname);
1609 qemu_acl_entry *entry;
1610 int i = 0;
1611
1612 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001613 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001614 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00001615 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00001616 i++;
1617 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001618 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001619 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001620 }
1621}
1622
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001623static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001624{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001625 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001626 qemu_acl *acl = find_acl(mon, aclname);
1627
1628 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001629 qemu_acl_reset(acl);
1630 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001631 }
1632}
aliguori76655d62009-03-06 20:27:37 +00001633
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001634static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001635{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001636 const char *aclname = qdict_get_str(qdict, "aclname");
1637 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001638 qemu_acl *acl = find_acl(mon, aclname);
1639
1640 if (acl) {
1641 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001642 acl->defaultDeny = 0;
1643 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001644 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001645 acl->defaultDeny = 1;
1646 monitor_printf(mon, "acl: policy set to 'deny'\n");
1647 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001648 monitor_printf(mon, "acl: unknown policy '%s', "
1649 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001650 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001651 }
1652}
aliguori76655d62009-03-06 20:27:37 +00001653
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001654static void hmp_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001655{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001656 const char *aclname = qdict_get_str(qdict, "aclname");
1657 const char *match = qdict_get_str(qdict, "match");
1658 const char *policy = qdict_get_str(qdict, "policy");
1659 int has_index = qdict_haskey(qdict, "index");
1660 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001661 qemu_acl *acl = find_acl(mon, aclname);
1662 int deny, ret;
1663
1664 if (acl) {
1665 if (strcmp(policy, "allow") == 0) {
1666 deny = 0;
1667 } else if (strcmp(policy, "deny") == 0) {
1668 deny = 1;
1669 } else {
1670 monitor_printf(mon, "acl: unknown policy '%s', "
1671 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001672 return;
1673 }
aliguori28a76be2009-03-06 20:27:40 +00001674 if (has_index)
1675 ret = qemu_acl_insert(acl, deny, match, index);
1676 else
1677 ret = qemu_acl_append(acl, deny, match);
1678 if (ret < 0)
1679 monitor_printf(mon, "acl: unable to add acl entry\n");
1680 else
1681 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001682 }
1683}
aliguori76655d62009-03-06 20:27:37 +00001684
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001685static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001686{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001687 const char *aclname = qdict_get_str(qdict, "aclname");
1688 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001689 qemu_acl *acl = find_acl(mon, aclname);
1690 int ret;
aliguori76655d62009-03-06 20:27:37 +00001691
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001692 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001693 ret = qemu_acl_remove(acl, match);
1694 if (ret < 0)
1695 monitor_printf(mon, "acl: no matching acl entry\n");
1696 else
1697 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001698 }
1699}
1700
Corey Bryant208c9d12012-06-22 14:36:09 -04001701void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001702{
Anthony Liguoric227f092009-10-01 16:12:16 -05001703 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001704 int fd;
1705
Corey Bryant208c9d12012-06-22 14:36:09 -04001706 fd = qemu_chr_fe_get_msgfd(cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001707 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001708 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryant208c9d12012-06-22 14:36:09 -04001709 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001710 }
1711
1712 if (qemu_isdigit(fdname[0])) {
Stefan Hajnoczi0b9f0e22014-04-24 13:58:18 +02001713 close(fd);
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001714 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
1715 "a name not starting with a digit");
Corey Bryant208c9d12012-06-22 14:36:09 -04001716 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001717 }
1718
Corey Bryant208c9d12012-06-22 14:36:09 -04001719 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001720 if (strcmp(monfd->name, fdname) != 0) {
1721 continue;
1722 }
1723
1724 close(monfd->fd);
1725 monfd->fd = fd;
Corey Bryant208c9d12012-06-22 14:36:09 -04001726 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001727 }
1728
Anthony Liguori7267c092011-08-20 22:09:37 -05001729 monfd = g_malloc0(sizeof(mon_fd_t));
1730 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001731 monfd->fd = fd;
1732
Corey Bryant208c9d12012-06-22 14:36:09 -04001733 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001734}
1735
Corey Bryant208c9d12012-06-22 14:36:09 -04001736void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001737{
Anthony Liguoric227f092009-10-01 16:12:16 -05001738 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001739
Corey Bryant208c9d12012-06-22 14:36:09 -04001740 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001741 if (strcmp(monfd->name, fdname) != 0) {
1742 continue;
1743 }
1744
Blue Swirl72cf2d42009-09-12 07:36:22 +00001745 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001746 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001747 g_free(monfd->name);
1748 g_free(monfd);
Corey Bryant208c9d12012-06-22 14:36:09 -04001749 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001750 }
1751
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001752 error_setg(errp, QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001753}
1754
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001755static void hmp_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001756{
Luiz Capitulino13548692011-07-29 15:36:43 -03001757 int saved_vm_running = runstate_is_running();
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001758 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02001759
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001760 vm_stop(RUN_STATE_RESTORE_VM);
Juan Quintelac8d41b22009-08-20 19:42:21 +02001761
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03001762 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02001763 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03001764 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02001765}
1766
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001767int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
Mark McLoughlin7768e042009-07-22 09:11:41 +01001768{
Anthony Liguoric227f092009-10-01 16:12:16 -05001769 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01001770
Blue Swirl72cf2d42009-09-12 07:36:22 +00001771 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01001772 int fd;
1773
1774 if (strcmp(monfd->name, fdname) != 0) {
1775 continue;
1776 }
1777
1778 fd = monfd->fd;
1779
1780 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001781 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05001782 g_free(monfd->name);
1783 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001784
1785 return fd;
1786 }
1787
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001788 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001789 return -1;
1790}
1791
Corey Bryantba1c0482012-08-14 16:43:43 -04001792static void monitor_fdset_cleanup(MonFdset *mon_fdset)
1793{
1794 MonFdsetFd *mon_fdset_fd;
1795 MonFdsetFd *mon_fdset_fd_next;
1796
1797 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantebe52b52012-10-18 15:19:33 -04001798 if ((mon_fdset_fd->removed ||
1799 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
1800 runstate_is_running()) {
Corey Bryantba1c0482012-08-14 16:43:43 -04001801 close(mon_fdset_fd->fd);
1802 g_free(mon_fdset_fd->opaque);
1803 QLIST_REMOVE(mon_fdset_fd, next);
1804 g_free(mon_fdset_fd);
1805 }
1806 }
1807
Corey Bryantadb696f2012-08-14 16:43:47 -04001808 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04001809 QLIST_REMOVE(mon_fdset, next);
1810 g_free(mon_fdset);
1811 }
1812}
1813
Corey Bryantefb87c12012-08-14 16:43:48 -04001814static void monitor_fdsets_cleanup(void)
1815{
1816 MonFdset *mon_fdset;
1817 MonFdset *mon_fdset_next;
1818
1819 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
1820 monitor_fdset_cleanup(mon_fdset);
1821 }
1822}
1823
Corey Bryantba1c0482012-08-14 16:43:43 -04001824AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
1825 const char *opaque, Error **errp)
1826{
1827 int fd;
1828 Monitor *mon = cur_mon;
Corey Bryantba1c0482012-08-14 16:43:43 -04001829 AddfdInfo *fdinfo;
1830
1831 fd = qemu_chr_fe_get_msgfd(mon->chr);
1832 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001833 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryantba1c0482012-08-14 16:43:43 -04001834 goto error;
1835 }
1836
Corey Bryante446f702012-10-18 15:19:32 -04001837 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
1838 has_opaque, opaque, errp);
1839 if (fdinfo) {
1840 return fdinfo;
Corey Bryant9ac54af2012-10-18 15:19:31 -04001841 }
1842
Corey Bryantba1c0482012-08-14 16:43:43 -04001843error:
1844 if (fd != -1) {
1845 close(fd);
1846 }
1847 return NULL;
1848}
1849
1850void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
1851{
1852 MonFdset *mon_fdset;
1853 MonFdsetFd *mon_fdset_fd;
1854 char fd_str[60];
1855
1856 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1857 if (mon_fdset->id != fdset_id) {
1858 continue;
1859 }
1860 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1861 if (has_fd) {
1862 if (mon_fdset_fd->fd != fd) {
1863 continue;
1864 }
1865 mon_fdset_fd->removed = true;
1866 break;
1867 } else {
1868 mon_fdset_fd->removed = true;
1869 }
1870 }
1871 if (has_fd && !mon_fdset_fd) {
1872 goto error;
1873 }
1874 monitor_fdset_cleanup(mon_fdset);
1875 return;
1876 }
1877
1878error:
1879 if (has_fd) {
1880 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
1881 fdset_id, fd);
1882 } else {
1883 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
1884 }
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001885 error_setg(errp, QERR_FD_NOT_FOUND, fd_str);
Corey Bryantba1c0482012-08-14 16:43:43 -04001886}
1887
1888FdsetInfoList *qmp_query_fdsets(Error **errp)
1889{
1890 MonFdset *mon_fdset;
1891 MonFdsetFd *mon_fdset_fd;
1892 FdsetInfoList *fdset_list = NULL;
1893
1894 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1895 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
1896 FdsetFdInfoList *fdsetfd_list = NULL;
1897
1898 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
1899 fdset_info->value->fdset_id = mon_fdset->id;
1900
1901 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1902 FdsetFdInfoList *fdsetfd_info;
1903
1904 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
1905 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
1906 fdsetfd_info->value->fd = mon_fdset_fd->fd;
1907 if (mon_fdset_fd->opaque) {
1908 fdsetfd_info->value->has_opaque = true;
1909 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
1910 } else {
1911 fdsetfd_info->value->has_opaque = false;
1912 }
1913
1914 fdsetfd_info->next = fdsetfd_list;
1915 fdsetfd_list = fdsetfd_info;
1916 }
1917
1918 fdset_info->value->fds = fdsetfd_list;
1919
1920 fdset_info->next = fdset_list;
1921 fdset_list = fdset_info;
1922 }
1923
1924 return fdset_list;
1925}
1926
Corey Bryante446f702012-10-18 15:19:32 -04001927AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
1928 bool has_opaque, const char *opaque,
1929 Error **errp)
1930{
1931 MonFdset *mon_fdset = NULL;
1932 MonFdsetFd *mon_fdset_fd;
1933 AddfdInfo *fdinfo;
1934
1935 if (has_fdset_id) {
1936 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1937 /* Break if match found or match impossible due to ordering by ID */
1938 if (fdset_id <= mon_fdset->id) {
1939 if (fdset_id < mon_fdset->id) {
1940 mon_fdset = NULL;
1941 }
1942 break;
1943 }
1944 }
1945 }
1946
1947 if (mon_fdset == NULL) {
1948 int64_t fdset_id_prev = -1;
1949 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
1950
1951 if (has_fdset_id) {
1952 if (fdset_id < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001953 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
1954 "a non-negative value");
Corey Bryante446f702012-10-18 15:19:32 -04001955 return NULL;
1956 }
1957 /* Use specified fdset ID */
1958 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1959 mon_fdset_cur = mon_fdset;
1960 if (fdset_id < mon_fdset_cur->id) {
1961 break;
1962 }
1963 }
1964 } else {
1965 /* Use first available fdset ID */
1966 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1967 mon_fdset_cur = mon_fdset;
1968 if (fdset_id_prev == mon_fdset_cur->id - 1) {
1969 fdset_id_prev = mon_fdset_cur->id;
1970 continue;
1971 }
1972 break;
1973 }
1974 }
1975
1976 mon_fdset = g_malloc0(sizeof(*mon_fdset));
1977 if (has_fdset_id) {
1978 mon_fdset->id = fdset_id;
1979 } else {
1980 mon_fdset->id = fdset_id_prev + 1;
1981 }
1982
1983 /* The fdset list is ordered by fdset ID */
1984 if (!mon_fdset_cur) {
1985 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
1986 } else if (mon_fdset->id < mon_fdset_cur->id) {
1987 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
1988 } else {
1989 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
1990 }
1991 }
1992
1993 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
1994 mon_fdset_fd->fd = fd;
1995 mon_fdset_fd->removed = false;
1996 if (has_opaque) {
1997 mon_fdset_fd->opaque = g_strdup(opaque);
1998 }
1999 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
2000
2001 fdinfo = g_malloc0(sizeof(*fdinfo));
2002 fdinfo->fdset_id = mon_fdset->id;
2003 fdinfo->fd = mon_fdset_fd->fd;
2004
2005 return fdinfo;
2006}
2007
Corey Bryantadb696f2012-08-14 16:43:47 -04002008int monitor_fdset_get_fd(int64_t fdset_id, int flags)
2009{
Blue Swirlb2dc64c2012-08-18 20:14:54 +00002010#ifndef _WIN32
Corey Bryantadb696f2012-08-14 16:43:47 -04002011 MonFdset *mon_fdset;
2012 MonFdsetFd *mon_fdset_fd;
2013 int mon_fd_flags;
2014
Corey Bryantadb696f2012-08-14 16:43:47 -04002015 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2016 if (mon_fdset->id != fdset_id) {
2017 continue;
2018 }
2019 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2020 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
2021 if (mon_fd_flags == -1) {
2022 return -1;
2023 }
2024
2025 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
2026 return mon_fdset_fd->fd;
2027 }
2028 }
2029 errno = EACCES;
2030 return -1;
2031 }
2032#endif
2033
2034 errno = ENOENT;
2035 return -1;
2036}
2037
2038int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
2039{
2040 MonFdset *mon_fdset;
2041 MonFdsetFd *mon_fdset_fd_dup;
2042
2043 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2044 if (mon_fdset->id != fdset_id) {
2045 continue;
2046 }
2047 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2048 if (mon_fdset_fd_dup->fd == dup_fd) {
2049 return -1;
2050 }
2051 }
2052 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2053 mon_fdset_fd_dup->fd = dup_fd;
2054 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
2055 return 0;
2056 }
2057 return -1;
2058}
2059
2060static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2061{
2062 MonFdset *mon_fdset;
2063 MonFdsetFd *mon_fdset_fd_dup;
2064
2065 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2066 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2067 if (mon_fdset_fd_dup->fd == dup_fd) {
2068 if (remove) {
2069 QLIST_REMOVE(mon_fdset_fd_dup, next);
2070 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2071 monitor_fdset_cleanup(mon_fdset);
2072 }
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002073 return -1;
2074 } else {
2075 return mon_fdset->id;
Corey Bryantadb696f2012-08-14 16:43:47 -04002076 }
Corey Bryantadb696f2012-08-14 16:43:47 -04002077 }
2078 }
2079 }
2080 return -1;
2081}
2082
2083int monitor_fdset_dup_fd_find(int dup_fd)
2084{
2085 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2086}
2087
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002088void monitor_fdset_dup_fd_remove(int dup_fd)
Corey Bryantadb696f2012-08-14 16:43:47 -04002089{
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002090 monitor_fdset_dup_fd_find_remove(dup_fd, true);
Corey Bryantadb696f2012-08-14 16:43:47 -04002091}
2092
Markus Armbruster1677f4c2015-02-09 14:03:19 +01002093int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
Laszlo Ersek59063662014-04-10 10:24:31 +02002094{
2095 int fd;
2096 Error *local_err = NULL;
2097
2098 if (!qemu_isdigit(fdname[0]) && mon) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002099 fd = monitor_get_fd(mon, fdname, &local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002100 } else {
2101 fd = qemu_parse_fd(fdname);
Laszlo Ersek59063662014-04-10 10:24:31 +02002102 if (fd == -1) {
2103 error_setg(&local_err, "Invalid file descriptor number '%s'",
2104 fdname);
2105 }
2106 }
2107 if (local_err) {
2108 error_propagate(errp, local_err);
2109 assert(fd == -1);
2110 } else {
2111 assert(fd != -1);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002112 }
2113
2114 return fd;
2115}
2116
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002117/* Please update hmp-commands.hx when adding or changing commands */
Wayne Xia816f8922011-10-12 11:32:41 +08002118static mon_cmd_t info_cmds[] = {
Pavel Butsykinda76ee72015-09-10 18:38:58 +03002119#include "hmp-commands-info.h"
2120 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002121};
2122
Wenchao Xiaa13ced52013-01-14 14:06:28 +08002123/* mon_cmds and info_cmds would be sorted at runtime */
2124static mon_cmd_t mon_cmds[] = {
2125#include "hmp-commands.h"
2126 { NULL, NULL, },
2127};
2128
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002129static const mon_cmd_t qmp_cmds[] = {
Anthony Liguorie3193602011-09-02 12:34:47 -05002130#include "qmp-commands-old.h"
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002131 { /* NULL */ },
2132};
2133
bellard9307c4c2004-04-04 12:57:25 +00002134/*******************************************************************/
2135
2136static const char *pch;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002137static sigjmp_buf expr_env;
bellard9307c4c2004-04-04 12:57:25 +00002138
bellard9307c4c2004-04-04 12:57:25 +00002139
Stefan Weil9c3175c2013-08-22 21:30:09 +02002140static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
2141expr_error(Monitor *mon, const char *fmt, ...)
bellard9dc39cb2004-03-14 21:38:27 +00002142{
Fam Zheng277acfe2013-08-20 10:58:21 +08002143 va_list ap;
2144 va_start(ap, fmt);
2145 monitor_vprintf(mon, fmt, ap);
2146 monitor_printf(mon, "\n");
2147 va_end(ap);
Peter Maydell6ab7e542013-02-20 15:21:09 +00002148 siglongjmp(expr_env, 1);
bellard9307c4c2004-04-04 12:57:25 +00002149}
2150
Markus Armbruster09b94182010-01-20 13:07:30 +01002151/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00002152static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002153{
Pavel Butsykinbf957282015-09-10 18:38:59 +03002154 const MonitorDef *md = target_monitor_defs();
bellard92a31b12005-02-10 22:00:52 +00002155 void *ptr;
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002156 uint64_t tmp = 0;
2157 int ret;
bellard92a31b12005-02-10 22:00:52 +00002158
Pavel Butsykinbf957282015-09-10 18:38:59 +03002159 if (md == NULL) {
2160 return -1;
2161 }
2162
2163 for(; md->name != NULL; md++) {
bellard9307c4c2004-04-04 12:57:25 +00002164 if (compare_cmd(name, md->name)) {
2165 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002166 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002167 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002168 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00002169 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002170 switch(md->type) {
2171 case MD_I32:
2172 *pval = *(int32_t *)ptr;
2173 break;
2174 case MD_TLONG:
2175 *pval = *(target_long *)ptr;
2176 break;
2177 default:
2178 *pval = 0;
2179 break;
2180 }
bellard9307c4c2004-04-04 12:57:25 +00002181 }
2182 return 0;
2183 }
2184 }
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002185
2186 ret = target_get_monitor_def(mon_get_cpu(), name, &tmp);
2187 if (!ret) {
2188 *pval = (target_long) tmp;
2189 }
2190
2191 return ret;
bellard9307c4c2004-04-04 12:57:25 +00002192}
2193
2194static void next(void)
2195{
Blue Swirl660f11b2009-07-31 21:16:51 +00002196 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002197 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002198 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002199 pch++;
2200 }
2201}
2202
aliguori376253e2009-03-05 23:01:23 +00002203static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002204
aliguori376253e2009-03-05 23:01:23 +00002205static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002206{
blueswir1c2efc952007-09-25 17:28:42 +00002207 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002208 char *p;
bellard6a00d602005-11-21 23:25:50 +00002209 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002210
2211 switch(*pch) {
2212 case '+':
2213 next();
aliguori376253e2009-03-05 23:01:23 +00002214 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002215 break;
2216 case '-':
2217 next();
aliguori376253e2009-03-05 23:01:23 +00002218 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002219 break;
2220 case '~':
2221 next();
aliguori376253e2009-03-05 23:01:23 +00002222 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002223 break;
2224 case '(':
2225 next();
aliguori376253e2009-03-05 23:01:23 +00002226 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002227 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002228 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002229 }
2230 next();
2231 break;
bellard81d09122004-07-14 17:21:37 +00002232 case '\'':
2233 pch++;
2234 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002235 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002236 n = *pch;
2237 pch++;
2238 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002239 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002240 next();
2241 break;
bellard9307c4c2004-04-04 12:57:25 +00002242 case '$':
2243 {
2244 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002245 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002246
bellard9307c4c2004-04-04 12:57:25 +00002247 pch++;
2248 q = buf;
2249 while ((*pch >= 'a' && *pch <= 'z') ||
2250 (*pch >= 'A' && *pch <= 'Z') ||
2251 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002252 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002253 if ((q - buf) < sizeof(buf) - 1)
2254 *q++ = *pch;
2255 pch++;
2256 }
blueswir1cd390082008-11-16 13:53:32 +00002257 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002258 pch++;
2259 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002260 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01002261 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00002262 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00002263 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002264 }
2265 break;
2266 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002267 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002268 n = 0;
2269 break;
2270 default:
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002271 errno = 0;
bellard4f4fbf72006-06-25 18:28:12 +00002272 n = strtoull(pch, &p, 0);
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002273 if (errno == ERANGE) {
2274 expr_error(mon, "number too large");
2275 }
bellard9307c4c2004-04-04 12:57:25 +00002276 if (pch == p) {
Fam Zheng277acfe2013-08-20 10:58:21 +08002277 expr_error(mon, "invalid char '%c' in expression", *p);
bellard9307c4c2004-04-04 12:57:25 +00002278 }
2279 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002280 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002281 pch++;
2282 break;
2283 }
2284 return n;
2285}
2286
2287
aliguori376253e2009-03-05 23:01:23 +00002288static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002289{
blueswir1c2efc952007-09-25 17:28:42 +00002290 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002291 int op;
ths3b46e622007-09-17 08:09:54 +00002292
aliguori376253e2009-03-05 23:01:23 +00002293 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002294 for(;;) {
2295 op = *pch;
2296 if (op != '*' && op != '/' && op != '%')
2297 break;
2298 next();
aliguori376253e2009-03-05 23:01:23 +00002299 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002300 switch(op) {
2301 default:
2302 case '*':
2303 val *= val2;
2304 break;
2305 case '/':
2306 case '%':
ths5fafdf22007-09-16 21:08:06 +00002307 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002308 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002309 if (op == '/')
2310 val /= val2;
2311 else
2312 val %= val2;
2313 break;
2314 }
2315 }
2316 return val;
2317}
2318
aliguori376253e2009-03-05 23:01:23 +00002319static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002320{
blueswir1c2efc952007-09-25 17:28:42 +00002321 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002322 int op;
bellard9307c4c2004-04-04 12:57:25 +00002323
aliguori376253e2009-03-05 23:01:23 +00002324 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002325 for(;;) {
2326 op = *pch;
2327 if (op != '&' && op != '|' && op != '^')
2328 break;
2329 next();
aliguori376253e2009-03-05 23:01:23 +00002330 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002331 switch(op) {
2332 default:
2333 case '&':
2334 val &= val2;
2335 break;
2336 case '|':
2337 val |= val2;
2338 break;
2339 case '^':
2340 val ^= val2;
2341 break;
2342 }
2343 }
2344 return val;
2345}
2346
aliguori376253e2009-03-05 23:01:23 +00002347static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002348{
blueswir1c2efc952007-09-25 17:28:42 +00002349 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002350 int op;
bellard9307c4c2004-04-04 12:57:25 +00002351
aliguori376253e2009-03-05 23:01:23 +00002352 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002353 for(;;) {
2354 op = *pch;
2355 if (op != '+' && op != '-')
2356 break;
2357 next();
aliguori376253e2009-03-05 23:01:23 +00002358 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002359 if (op == '+')
2360 val += val2;
2361 else
2362 val -= val2;
2363 }
2364 return val;
2365}
2366
aliguori376253e2009-03-05 23:01:23 +00002367static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002368{
2369 pch = *pp;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002370 if (sigsetjmp(expr_env, 0)) {
bellard9307c4c2004-04-04 12:57:25 +00002371 *pp = pch;
2372 return -1;
2373 }
blueswir1cd390082008-11-16 13:53:32 +00002374 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002375 pch++;
aliguori376253e2009-03-05 23:01:23 +00002376 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002377 *pp = pch;
2378 return 0;
2379}
2380
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002381static int get_double(Monitor *mon, double *pval, const char **pp)
2382{
2383 const char *p = *pp;
2384 char *tailp;
2385 double d;
2386
2387 d = strtod(p, &tailp);
2388 if (tailp == p) {
2389 monitor_printf(mon, "Number expected\n");
2390 return -1;
2391 }
2392 if (d != d || d - d != 0) {
2393 /* NaN or infinity */
2394 monitor_printf(mon, "Bad number\n");
2395 return -1;
2396 }
2397 *pval = d;
2398 *pp = tailp;
2399 return 0;
2400}
2401
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002402/*
2403 * Store the command-name in cmdname, and return a pointer to
2404 * the remaining of the command string.
2405 */
2406static const char *get_command_name(const char *cmdline,
2407 char *cmdname, size_t nlen)
2408{
2409 size_t len;
2410 const char *p, *pstart;
2411
2412 p = cmdline;
2413 while (qemu_isspace(*p))
2414 p++;
2415 if (*p == '\0')
2416 return NULL;
2417 pstart = p;
2418 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2419 p++;
2420 len = p - pstart;
2421 if (len > nlen - 1)
2422 len = nlen - 1;
2423 memcpy(cmdname, pstart, len);
2424 cmdname[len] = '\0';
2425 return p;
2426}
2427
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002428/**
2429 * Read key of 'type' into 'key' and return the current
2430 * 'type' pointer.
2431 */
2432static char *key_get_info(const char *type, char **key)
2433{
2434 size_t len;
2435 char *p, *str;
2436
2437 if (*type == ',')
2438 type++;
2439
2440 p = strchr(type, ':');
2441 if (!p) {
2442 *key = NULL;
2443 return NULL;
2444 }
2445 len = p - type;
2446
Anthony Liguori7267c092011-08-20 22:09:37 -05002447 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002448 memcpy(str, type, len);
2449 str[len] = '\0';
2450
2451 *key = str;
2452 return ++p;
2453}
2454
bellard9307c4c2004-04-04 12:57:25 +00002455static int default_fmt_format = 'x';
2456static int default_fmt_size = 4;
2457
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002458static int is_valid_option(const char *c, const char *typestr)
2459{
2460 char option[3];
2461
2462 option[0] = '-';
2463 option[1] = *c;
2464 option[2] = '\0';
2465
2466 typestr = strstr(typestr, option);
2467 return (typestr != NULL);
2468}
2469
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03002470static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
2471 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002472{
2473 const mon_cmd_t *cmd;
2474
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03002475 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002476 if (compare_cmd(cmdname, cmd->name)) {
2477 return cmd;
2478 }
2479 }
2480
2481 return NULL;
2482}
2483
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03002484static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
2485{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002486 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03002487}
2488
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002489/*
Bandan Dasae502122015-06-03 18:38:08 -04002490 * Parse command name from @cmdp according to command table @table.
2491 * If blank, return NULL.
2492 * Else, if no valid command can be found, report to @mon, and return
2493 * NULL.
2494 * Else, change @cmdp to point right behind the name, and return its
2495 * command table entry.
2496 * Do not assume the return value points into @table! It doesn't when
2497 * the command is found in a sub-command table.
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002498 */
Anthony Liguoric227f092009-10-01 16:12:16 -05002499static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Bandan Dasae502122015-06-03 18:38:08 -04002500 const char **cmdp,
2501 mon_cmd_t *table)
bellard9307c4c2004-04-04 12:57:25 +00002502{
Bandan Dasae502122015-06-03 18:38:08 -04002503 const char *p;
Anthony Liguoric227f092009-10-01 16:12:16 -05002504 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002505 char cmdname[256];
bellard9dc39cb2004-03-14 21:38:27 +00002506
bellard9307c4c2004-04-04 12:57:25 +00002507 /* extract the command name */
Bandan Dasae502122015-06-03 18:38:08 -04002508 p = get_command_name(*cmdp, cmdname, sizeof(cmdname));
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002509 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002510 return NULL;
ths3b46e622007-09-17 08:09:54 +00002511
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002512 cmd = search_dispatch_table(table, cmdname);
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002513 if (!cmd) {
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002514 monitor_printf(mon, "unknown command: '%.*s'\n",
Bandan Dasae502122015-06-03 18:38:08 -04002515 (int)(p - *cmdp), *cmdp);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002516 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002517 }
bellard9307c4c2004-04-04 12:57:25 +00002518
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002519 /* filter out following useless space */
2520 while (qemu_isspace(*p)) {
2521 p++;
2522 }
Bandan Dasae502122015-06-03 18:38:08 -04002523
2524 *cmdp = p;
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002525 /* search sub command */
Bandan Dasae502122015-06-03 18:38:08 -04002526 if (cmd->sub_table != NULL && *p != '\0') {
2527 return monitor_parse_command(mon, cmdp, cmd->sub_table);
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002528 }
2529
Bandan Dasae502122015-06-03 18:38:08 -04002530 return cmd;
2531}
2532
2533/*
2534 * Parse arguments for @cmd.
2535 * If it can't be parsed, report to @mon, and return NULL.
2536 * Else, insert command arguments into a QDict, and return it.
2537 * Note: On success, caller has to free the QDict structure.
2538 */
2539
2540static QDict *monitor_parse_arguments(Monitor *mon,
2541 const char **endp,
2542 const mon_cmd_t *cmd)
2543{
2544 const char *typestr;
2545 char *key;
2546 int c;
2547 const char *p = *endp;
2548 char buf[1024];
2549 QDict *qdict = qdict_new();
2550
bellard9307c4c2004-04-04 12:57:25 +00002551 /* parse the parameters */
2552 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00002553 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002554 typestr = key_get_info(typestr, &key);
2555 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00002556 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002557 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00002558 typestr++;
2559 switch(c) {
2560 case 'F':
bellard81d09122004-07-14 17:21:37 +00002561 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002562 case 's':
2563 {
2564 int ret;
ths3b46e622007-09-17 08:09:54 +00002565
blueswir1cd390082008-11-16 13:53:32 +00002566 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002567 p++;
2568 if (*typestr == '?') {
2569 typestr++;
2570 if (*p == '\0') {
2571 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03002572 break;
bellard9307c4c2004-04-04 12:57:25 +00002573 }
2574 }
2575 ret = get_str(buf, sizeof(buf), &p);
2576 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002577 switch(c) {
2578 case 'F':
aliguori376253e2009-03-05 23:01:23 +00002579 monitor_printf(mon, "%s: filename expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002580 cmd->name);
bellard81d09122004-07-14 17:21:37 +00002581 break;
2582 case 'B':
aliguori376253e2009-03-05 23:01:23 +00002583 monitor_printf(mon, "%s: block device name expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002584 cmd->name);
bellard81d09122004-07-14 17:21:37 +00002585 break;
2586 default:
Bandan Dasae502122015-06-03 18:38:08 -04002587 monitor_printf(mon, "%s: string expected\n", cmd->name);
bellard81d09122004-07-14 17:21:37 +00002588 break;
2589 }
bellard9307c4c2004-04-04 12:57:25 +00002590 goto fail;
2591 }
Luiz Capitulino53773582009-08-28 15:27:25 -03002592 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00002593 }
2594 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01002595 case 'O':
2596 {
2597 QemuOptsList *opts_list;
2598 QemuOpts *opts;
2599
2600 opts_list = qemu_find_opts(key);
2601 if (!opts_list || opts_list->desc->name) {
2602 goto bad_type;
2603 }
2604 while (qemu_isspace(*p)) {
2605 p++;
2606 }
2607 if (!*p)
2608 break;
2609 if (get_str(buf, sizeof(buf), &p) < 0) {
2610 goto fail;
2611 }
Markus Armbruster70b94332015-02-13 12:50:26 +01002612 opts = qemu_opts_parse_noisily(opts_list, buf, true);
Markus Armbruster361127d2010-02-10 20:24:35 +01002613 if (!opts) {
2614 goto fail;
2615 }
2616 qemu_opts_to_qdict(opts, qdict);
2617 qemu_opts_del(opts);
2618 }
2619 break;
bellard9307c4c2004-04-04 12:57:25 +00002620 case '/':
2621 {
2622 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002623
blueswir1cd390082008-11-16 13:53:32 +00002624 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002625 p++;
2626 if (*p == '/') {
2627 /* format found */
2628 p++;
2629 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002630 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002631 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002632 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002633 count = count * 10 + (*p - '0');
2634 p++;
2635 }
2636 }
2637 size = -1;
2638 format = -1;
2639 for(;;) {
2640 switch(*p) {
2641 case 'o':
2642 case 'd':
2643 case 'u':
2644 case 'x':
2645 case 'i':
2646 case 'c':
2647 format = *p++;
2648 break;
2649 case 'b':
2650 size = 1;
2651 p++;
2652 break;
2653 case 'h':
2654 size = 2;
2655 p++;
2656 break;
2657 case 'w':
2658 size = 4;
2659 p++;
2660 break;
2661 case 'g':
2662 case 'L':
2663 size = 8;
2664 p++;
2665 break;
2666 default:
2667 goto next;
2668 }
2669 }
2670 next:
blueswir1cd390082008-11-16 13:53:32 +00002671 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00002672 monitor_printf(mon, "invalid char in format: '%c'\n",
2673 *p);
bellard9307c4c2004-04-04 12:57:25 +00002674 goto fail;
2675 }
bellard9307c4c2004-04-04 12:57:25 +00002676 if (format < 0)
2677 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002678 if (format != 'i') {
2679 /* for 'i', not specifying a size gives -1 as size */
2680 if (size < 0)
2681 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002682 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002683 }
bellard9307c4c2004-04-04 12:57:25 +00002684 default_fmt_format = format;
2685 } else {
2686 count = 1;
2687 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002688 if (format != 'i') {
2689 size = default_fmt_size;
2690 } else {
2691 size = -1;
2692 }
bellard9307c4c2004-04-04 12:57:25 +00002693 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002694 qdict_put(qdict, "count", qint_from_int(count));
2695 qdict_put(qdict, "format", qint_from_int(format));
2696 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00002697 }
2698 break;
2699 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002700 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002701 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00002702 {
blueswir1c2efc952007-09-25 17:28:42 +00002703 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00002704
blueswir1cd390082008-11-16 13:53:32 +00002705 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002706 p++;
bellard34405572004-06-08 00:55:58 +00002707 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002708 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03002709 if (*p == '\0') {
2710 typestr++;
2711 break;
2712 }
bellard34405572004-06-08 00:55:58 +00002713 } else {
2714 if (*p == '.') {
2715 p++;
blueswir1cd390082008-11-16 13:53:32 +00002716 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00002717 p++;
bellard34405572004-06-08 00:55:58 +00002718 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03002719 typestr++;
2720 break;
bellard34405572004-06-08 00:55:58 +00002721 }
2722 }
bellard13224a82006-07-14 22:03:35 +00002723 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002724 }
aliguori376253e2009-03-05 23:01:23 +00002725 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00002726 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002727 /* Check if 'i' is greater than 32-bit */
2728 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
Bandan Dasae502122015-06-03 18:38:08 -04002729 monitor_printf(mon, "\'%s\' has failed: ", cmd->name);
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002730 monitor_printf(mon, "integer is for 32-bit values\n");
2731 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002732 } else if (c == 'M') {
Luiz Capitulino91162842012-04-26 17:34:30 -03002733 if (val < 0) {
2734 monitor_printf(mon, "enter a positive value\n");
2735 goto fail;
2736 }
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002737 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002738 }
Luiz Capitulino53773582009-08-28 15:27:25 -03002739 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00002740 }
2741 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02002742 case 'o':
2743 {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01002744 int64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02002745 char *end;
2746
2747 while (qemu_isspace(*p)) {
2748 p++;
2749 }
2750 if (*typestr == '?') {
2751 typestr++;
2752 if (*p == '\0') {
2753 break;
2754 }
2755 }
Marc-André Lureau4677bb42015-09-16 18:02:56 +02002756 val = qemu_strtosz(p, &end);
Jes Sorensendbc0c672010-10-21 17:15:47 +02002757 if (val < 0) {
2758 monitor_printf(mon, "invalid size\n");
2759 goto fail;
2760 }
2761 qdict_put(qdict, key, qint_from_int(val));
2762 p = end;
2763 }
2764 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01002765 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002766 {
2767 double val;
2768
2769 while (qemu_isspace(*p))
2770 p++;
2771 if (*typestr == '?') {
2772 typestr++;
2773 if (*p == '\0') {
2774 break;
2775 }
2776 }
2777 if (get_double(mon, &val, &p) < 0) {
2778 goto fail;
2779 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02002780 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01002781 switch (*p) {
2782 case 'm':
2783 val /= 1e3; p += 2; break;
2784 case 'u':
2785 val /= 1e6; p += 2; break;
2786 case 'n':
2787 val /= 1e9; p += 2; break;
2788 }
2789 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002790 if (*p && !qemu_isspace(*p)) {
2791 monitor_printf(mon, "Unknown unit suffix\n");
2792 goto fail;
2793 }
2794 qdict_put(qdict, key, qfloat_from_double(val));
2795 }
2796 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002797 case 'b':
2798 {
2799 const char *beg;
Eric Blakefc48ffc2015-05-15 16:24:59 -06002800 bool val;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002801
2802 while (qemu_isspace(*p)) {
2803 p++;
2804 }
2805 beg = p;
2806 while (qemu_isgraph(*p)) {
2807 p++;
2808 }
2809 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06002810 val = true;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002811 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06002812 val = false;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002813 } else {
2814 monitor_printf(mon, "Expected 'on' or 'off'\n");
2815 goto fail;
2816 }
Eric Blakefc48ffc2015-05-15 16:24:59 -06002817 qdict_put(qdict, key, qbool_from_bool(val));
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002818 }
2819 break;
bellard9307c4c2004-04-04 12:57:25 +00002820 case '-':
2821 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002822 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03002823 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00002824 /* option */
ths3b46e622007-09-17 08:09:54 +00002825
bellard9307c4c2004-04-04 12:57:25 +00002826 c = *typestr++;
2827 if (c == '\0')
2828 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00002829 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002830 p++;
bellard9307c4c2004-04-04 12:57:25 +00002831 if (*p == '-') {
2832 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002833 if(c != *p) {
2834 if(!is_valid_option(p, typestr)) {
2835
2836 monitor_printf(mon, "%s: unsupported option -%c\n",
Bandan Dasae502122015-06-03 18:38:08 -04002837 cmd->name, *p);
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002838 goto fail;
2839 } else {
2840 skip_key = 1;
2841 }
bellard9307c4c2004-04-04 12:57:25 +00002842 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002843 if(skip_key) {
2844 p = tmp;
2845 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03002846 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002847 p++;
Eric Blakefc48ffc2015-05-15 16:24:59 -06002848 qdict_put(qdict, key, qbool_from_bool(true));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002849 }
bellard9307c4c2004-04-04 12:57:25 +00002850 }
bellard9307c4c2004-04-04 12:57:25 +00002851 }
2852 break;
Wenchao Xia129be002013-08-27 20:38:26 +08002853 case 'S':
2854 {
2855 /* package all remaining string */
2856 int len;
2857
2858 while (qemu_isspace(*p)) {
2859 p++;
2860 }
2861 if (*typestr == '?') {
2862 typestr++;
2863 if (*p == '\0') {
2864 /* no remaining string: NULL argument */
2865 break;
2866 }
2867 }
2868 len = strlen(p);
2869 if (len <= 0) {
2870 monitor_printf(mon, "%s: string expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002871 cmd->name);
Bandan Dase549d2a2015-06-03 18:38:10 -04002872 goto fail;
Wenchao Xia129be002013-08-27 20:38:26 +08002873 }
2874 qdict_put(qdict, key, qstring_from_str(p));
2875 p += len;
2876 }
2877 break;
bellard9307c4c2004-04-04 12:57:25 +00002878 default:
2879 bad_type:
Bandan Dasae502122015-06-03 18:38:08 -04002880 monitor_printf(mon, "%s: unknown type '%c'\n", cmd->name, c);
bellard9307c4c2004-04-04 12:57:25 +00002881 goto fail;
2882 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002883 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002884 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00002885 }
2886 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00002887 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002888 p++;
2889 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00002890 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
Bandan Dasae502122015-06-03 18:38:08 -04002891 cmd->name);
bellard9307c4c2004-04-04 12:57:25 +00002892 goto fail;
2893 }
2894
Bandan Dasae502122015-06-03 18:38:08 -04002895 return qdict;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02002896
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002897fail:
Bandan Dasae502122015-06-03 18:38:08 -04002898 QDECREF(qdict);
Anthony Liguori7267c092011-08-20 22:09:37 -05002899 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002900 return NULL;
2901}
2902
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01002903static void handle_hmp_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002904{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002905 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05002906 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002907
Bandan Dasae502122015-06-03 18:38:08 -04002908 cmd = monitor_parse_command(mon, &cmdline, mon->cmd_table);
2909 if (!cmd) {
2910 return;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002911 }
2912
Bandan Dasae502122015-06-03 18:38:08 -04002913 qdict = monitor_parse_arguments(mon, &cmdline, cmd);
2914 if (!qdict) {
Bandan Dasdd41eea2015-06-03 18:38:09 -04002915 monitor_printf(mon, "Try \"help %s\" for more information\n",
2916 cmd->name);
Bandan Dasae502122015-06-03 18:38:08 -04002917 return;
2918 }
2919
2920 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002921 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00002922}
2923
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08002924static void cmd_completion(Monitor *mon, const char *name, const char *list)
bellard81d09122004-07-14 17:21:37 +00002925{
2926 const char *p, *pstart;
2927 char cmd[128];
2928 int len;
2929
2930 p = list;
2931 for(;;) {
2932 pstart = p;
2933 p = strchr(p, '|');
2934 if (!p)
2935 p = pstart + strlen(pstart);
2936 len = p - pstart;
2937 if (len > sizeof(cmd) - 2)
2938 len = sizeof(cmd) - 2;
2939 memcpy(cmd, pstart, len);
2940 cmd[len] = '\0';
2941 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08002942 readline_add_completion(mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00002943 }
2944 if (*p == '\0')
2945 break;
2946 p++;
2947 }
2948}
2949
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08002950static void file_completion(Monitor *mon, const char *input)
bellard81d09122004-07-14 17:21:37 +00002951{
2952 DIR *ffs;
2953 struct dirent *d;
2954 char path[1024];
2955 char file[1024], file_prefix[1024];
2956 int input_path_len;
2957 const char *p;
2958
ths5fafdf22007-09-16 21:08:06 +00002959 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00002960 if (!p) {
2961 input_path_len = 0;
2962 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00002963 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00002964 } else {
2965 input_path_len = p - input + 1;
2966 memcpy(path, input, input_path_len);
2967 if (input_path_len > sizeof(path) - 1)
2968 input_path_len = sizeof(path) - 1;
2969 path[input_path_len] = '\0';
2970 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2971 }
Bandan Das19f2db52015-06-03 18:38:07 -04002972
bellard81d09122004-07-14 17:21:37 +00002973 ffs = opendir(path);
2974 if (!ffs)
2975 return;
2976 for(;;) {
2977 struct stat sb;
2978 d = readdir(ffs);
2979 if (!d)
2980 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09002981
2982 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
2983 continue;
2984 }
2985
bellard81d09122004-07-14 17:21:37 +00002986 if (strstart(d->d_name, file_prefix, NULL)) {
2987 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00002988 if (input_path_len < sizeof(file))
2989 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2990 d->d_name);
bellard81d09122004-07-14 17:21:37 +00002991 /* stat the file to find out if it's a directory.
2992 * In that case add a slash to speed up typing long paths
2993 */
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01002994 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
blueswir1363a37d2008-08-21 17:58:08 +00002995 pstrcat(file, sizeof(file), "/");
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01002996 }
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08002997 readline_add_completion(mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00002998 }
2999 }
3000 closedir(ffs);
3001}
3002
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003003static const char *next_arg_type(const char *typestr)
3004{
3005 const char *p = strchr(typestr, ':');
3006 return (p != NULL ? ++p : typestr);
3007}
3008
Hani Benhabiles40d19392014-05-07 23:41:30 +01003009static void add_completion_option(ReadLineState *rs, const char *str,
3010 const char *option)
3011{
3012 if (!str || !option) {
3013 return;
3014 }
3015 if (!strncmp(option, str, strlen(str))) {
3016 readline_add_completion(rs, option);
3017 }
3018}
3019
Hani Benhabiles13e315d2014-05-07 23:41:29 +01003020void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
3021{
3022 size_t len;
3023 ChardevBackendInfoList *list, *start;
3024
3025 if (nb_args != 2) {
3026 return;
3027 }
3028 len = strlen(str);
3029 readline_set_completion_index(rs, len);
3030
3031 start = list = qmp_query_chardev_backends(NULL);
3032 while (list) {
3033 const char *chr_name = list->value->name;
3034
3035 if (!strncmp(chr_name, str, len)) {
3036 readline_add_completion(rs, chr_name);
3037 }
3038 list = list->next;
3039 }
3040 qapi_free_ChardevBackendInfoList(start);
3041}
3042
Hani Benhabilesb162b492014-05-07 23:41:31 +01003043void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
3044{
3045 size_t len;
3046 int i;
3047
3048 if (nb_args != 2) {
3049 return;
3050 }
3051 len = strlen(str);
3052 readline_set_completion_index(rs, len);
3053 for (i = 0; NetClientOptionsKind_lookup[i]; i++) {
3054 add_completion_option(rs, str, NetClientOptionsKind_lookup[i]);
3055 }
3056}
3057
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003058void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003059{
3060 GSList *list, *elt;
3061 size_t len;
3062
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003063 if (nb_args != 2) {
3064 return;
3065 }
3066
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003067 len = strlen(str);
3068 readline_set_completion_index(rs, len);
3069 list = elt = object_class_get_list(TYPE_DEVICE, false);
3070 while (elt) {
3071 const char *name;
3072 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
3073 TYPE_DEVICE);
3074 name = object_class_get_name(OBJECT_CLASS(dc));
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003075
3076 if (!dc->cannot_instantiate_with_device_add_yet
3077 && !strncmp(name, str, len)) {
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003078 readline_add_completion(rs, name);
3079 }
3080 elt = elt->next;
3081 }
3082 g_slist_free(list);
3083}
3084
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003085void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003086{
3087 GSList *list, *elt;
3088 size_t len;
3089
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003090 if (nb_args != 2) {
3091 return;
3092 }
3093
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003094 len = strlen(str);
3095 readline_set_completion_index(rs, len);
3096 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
3097 while (elt) {
3098 const char *name;
3099
3100 name = object_class_get_name(OBJECT_CLASS(elt->data));
3101 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
3102 readline_add_completion(rs, name);
3103 }
3104 elt = elt->next;
3105 }
3106 g_slist_free(list);
3107}
3108
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003109static void peripheral_device_del_completion(ReadLineState *rs,
3110 const char *str, size_t len)
3111{
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003112 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
3113 GSList *list, *item;
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003114
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003115 list = qdev_build_hotpluggable_device_list(peripheral);
3116 if (!list) {
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003117 return;
3118 }
3119
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003120 for (item = list; item; item = g_slist_next(item)) {
3121 DeviceState *dev = item->data;
3122
3123 if (dev->id && !strncmp(str, dev->id, len)) {
3124 readline_add_completion(rs, dev->id);
3125 }
3126 }
3127
3128 g_slist_free(list);
3129}
3130
Hani Benhabiles6297d9a2014-05-07 23:41:28 +01003131void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3132{
3133 size_t len;
3134 ChardevInfoList *list, *start;
3135
3136 if (nb_args != 2) {
3137 return;
3138 }
3139 len = strlen(str);
3140 readline_set_completion_index(rs, len);
3141
3142 start = list = qmp_query_chardev(NULL);
3143 while (list) {
3144 ChardevInfo *chr = list->value;
3145
3146 if (!strncmp(chr->label, str, len)) {
3147 readline_add_completion(rs, chr->label);
3148 }
3149 list = list->next;
3150 }
3151 qapi_free_ChardevInfoList(start);
3152}
3153
Hani Benhabiles8e597772014-05-27 23:39:30 +01003154static void ringbuf_completion(ReadLineState *rs, const char *str)
3155{
3156 size_t len;
3157 ChardevInfoList *list, *start;
3158
3159 len = strlen(str);
3160 readline_set_completion_index(rs, len);
3161
3162 start = list = qmp_query_chardev(NULL);
3163 while (list) {
3164 ChardevInfo *chr_info = list->value;
3165
3166 if (!strncmp(chr_info->label, str, len)) {
3167 CharDriverState *chr = qemu_chr_find(chr_info->label);
3168 if (chr && chr_is_ringbuf(chr)) {
3169 readline_add_completion(rs, chr_info->label);
3170 }
3171 }
3172 list = list->next;
3173 }
3174 qapi_free_ChardevInfoList(start);
3175}
3176
Hani Benhabiles8e597772014-05-27 23:39:30 +01003177void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
3178{
3179 if (nb_args != 2) {
3180 return;
3181 }
3182 ringbuf_completion(rs, str);
3183}
3184
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003185void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
3186{
3187 size_t len;
3188
3189 if (nb_args != 2) {
3190 return;
3191 }
3192
3193 len = strlen(str);
3194 readline_set_completion_index(rs, len);
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003195 peripheral_device_del_completion(rs, str, len);
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003196}
3197
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003198void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003199{
3200 ObjectPropertyInfoList *list, *start;
3201 size_t len;
3202
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003203 if (nb_args != 2) {
3204 return;
3205 }
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003206 len = strlen(str);
3207 readline_set_completion_index(rs, len);
3208
3209 start = list = qmp_qom_list("/objects", NULL);
3210 while (list) {
3211 ObjectPropertyInfo *info = list->value;
3212
3213 if (!strncmp(info->type, "child<", 5)
3214 && !strncmp(info->name, str, len)) {
3215 readline_add_completion(rs, info->name);
3216 }
3217 list = list->next;
3218 }
3219 qapi_free_ObjectPropertyInfoList(start);
3220}
3221
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003222void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
3223{
3224 int i;
3225 char *sep;
3226 size_t len;
3227
3228 if (nb_args != 2) {
3229 return;
3230 }
3231 sep = strrchr(str, '-');
3232 if (sep) {
3233 str = sep + 1;
3234 }
3235 len = strlen(str);
3236 readline_set_completion_index(rs, len);
Eric Blake7fb1cf12015-11-18 01:52:57 -07003237 for (i = 0; i < Q_KEY_CODE__MAX; i++) {
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003238 if (!strncmp(str, QKeyCode_lookup[i], len)) {
3239 readline_add_completion(rs, QKeyCode_lookup[i]);
3240 }
3241 }
3242}
3243
Hani Benhabiles40d19392014-05-07 23:41:30 +01003244void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
3245{
3246 size_t len;
3247
3248 len = strlen(str);
3249 readline_set_completion_index(rs, len);
3250 if (nb_args == 2) {
Jason Wangeaed4832015-04-23 14:21:38 +08003251 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles40d19392014-05-07 23:41:30 +01003252 int count, i;
3253 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08003254 NET_CLIENT_OPTIONS_KIND_NONE,
3255 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003256 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles40d19392014-05-07 23:41:30 +01003257 const char *name = ncs[i]->name;
3258 if (!strncmp(str, name, len)) {
3259 readline_add_completion(rs, name);
3260 }
3261 }
3262 } else if (nb_args == 3) {
3263 add_completion_option(rs, str, "on");
3264 add_completion_option(rs, str, "off");
3265 }
3266}
3267
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003268void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
3269{
3270 int len, count, i;
Jason Wangeaed4832015-04-23 14:21:38 +08003271 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003272
3273 if (nb_args != 2) {
3274 return;
3275 }
3276
3277 len = strlen(str);
3278 readline_set_completion_index(rs, len);
3279 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_OPTIONS_KIND_NIC,
Jason Wangeaed4832015-04-23 14:21:38 +08003280 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003281 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003282 QemuOpts *opts;
3283 const char *name = ncs[i]->name;
3284 if (strncmp(str, name, len)) {
3285 continue;
3286 }
3287 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
3288 if (opts) {
3289 readline_add_completion(rs, name);
3290 }
3291 }
3292}
3293
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003294void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
3295{
3296 size_t len;
3297
3298 len = strlen(str);
3299 readline_set_completion_index(rs, len);
3300 if (nb_args == 2) {
3301 TraceEventID id;
3302 for (id = 0; id < trace_event_count(); id++) {
3303 const char *event_name = trace_event_get_name(trace_event_id(id));
3304 if (!strncmp(str, event_name, len)) {
3305 readline_add_completion(rs, event_name);
3306 }
3307 }
3308 } else if (nb_args == 3) {
3309 add_completion_option(rs, str, "on");
3310 add_completion_option(rs, str, "off");
3311 }
3312}
3313
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003314void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
3315{
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003316 int i;
3317
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003318 if (nb_args != 2) {
3319 return;
3320 }
3321 readline_set_completion_index(rs, strlen(str));
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003322 for (i = 0; WatchdogExpirationAction_lookup[i]; i++) {
3323 add_completion_option(rs, str, WatchdogExpirationAction_lookup[i]);
3324 }
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003325}
3326
Hani Benhabilesc68a0402014-05-27 23:39:32 +01003327void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
3328 const char *str)
3329{
3330 size_t len;
3331
3332 len = strlen(str);
3333 readline_set_completion_index(rs, len);
3334 if (nb_args == 2) {
3335 int i;
Eric Blake7fb1cf12015-11-18 01:52:57 -07003336 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Hani Benhabilesc68a0402014-05-27 23:39:32 +01003337 const char *name = MigrationCapability_lookup[i];
3338 if (!strncmp(str, name, len)) {
3339 readline_add_completion(rs, name);
3340 }
3341 }
3342 } else if (nb_args == 3) {
3343 add_completion_option(rs, str, "on");
3344 add_completion_option(rs, str, "off");
3345 }
3346}
3347
Liang Li50e9a622015-03-23 16:32:29 +08003348void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
3349 const char *str)
3350{
3351 size_t len;
3352
3353 len = strlen(str);
3354 readline_set_completion_index(rs, len);
3355 if (nb_args == 2) {
3356 int i;
Eric Blake7fb1cf12015-11-18 01:52:57 -07003357 for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
Liang Li50e9a622015-03-23 16:32:29 +08003358 const char *name = MigrationParameter_lookup[i];
3359 if (!strncmp(str, name, len)) {
3360 readline_add_completion(rs, name);
3361 }
3362 }
3363 }
3364}
3365
Hani Benhabilese3bb5322014-05-27 23:39:34 +01003366void host_net_add_completion(ReadLineState *rs, int nb_args, const char *str)
3367{
3368 int i;
3369 size_t len;
3370 if (nb_args != 2) {
3371 return;
3372 }
3373 len = strlen(str);
3374 readline_set_completion_index(rs, len);
3375 for (i = 0; host_net_devices[i]; i++) {
3376 if (!strncmp(host_net_devices[i], str, len)) {
3377 readline_add_completion(rs, host_net_devices[i]);
3378 }
3379 }
3380}
3381
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003382void host_net_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3383{
Jason Wangeaed4832015-04-23 14:21:38 +08003384 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003385 int count, i, len;
3386
3387 len = strlen(str);
3388 readline_set_completion_index(rs, len);
3389 if (nb_args == 2) {
3390 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08003391 NET_CLIENT_OPTIONS_KIND_NONE,
3392 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003393 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003394 int id;
3395 char name[16];
3396
3397 if (net_hub_id_for_client(ncs[i], &id)) {
3398 continue;
3399 }
3400 snprintf(name, sizeof(name), "%d", id);
3401 if (!strncmp(str, name, len)) {
3402 readline_add_completion(rs, name);
3403 }
3404 }
3405 return;
3406 } else if (nb_args == 3) {
3407 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08003408 NET_CLIENT_OPTIONS_KIND_NIC,
3409 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003410 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Jason Wang2c4681f2015-02-02 15:06:38 +08003411 int id;
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003412 const char *name;
3413
Jason Wang2c4681f2015-02-02 15:06:38 +08003414 if (ncs[i]->info->type == NET_CLIENT_OPTIONS_KIND_HUBPORT ||
3415 net_hub_id_for_client(ncs[i], &id)) {
3416 continue;
3417 }
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003418 name = ncs[i]->name;
3419 if (!strncmp(str, name, len)) {
3420 readline_add_completion(rs, name);
3421 }
3422 }
3423 return;
3424 }
3425}
3426
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003427static void vm_completion(ReadLineState *rs, const char *str)
3428{
3429 size_t len;
3430 BlockDriverState *bs = NULL;
3431
3432 len = strlen(str);
3433 readline_set_completion_index(rs, len);
3434 while ((bs = bdrv_next(bs))) {
3435 SnapshotInfoList *snapshots, *snapshot;
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003436 AioContext *ctx = bdrv_get_aio_context(bs);
3437 bool ok = false;
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003438
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003439 aio_context_acquire(ctx);
3440 if (bdrv_can_snapshot(bs)) {
3441 ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0;
3442 }
3443 aio_context_release(ctx);
3444 if (!ok) {
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003445 continue;
3446 }
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003447
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003448 snapshot = snapshots;
3449 while (snapshot) {
3450 char *completion = snapshot->value->name;
3451 if (!strncmp(str, completion, len)) {
3452 readline_add_completion(rs, completion);
3453 }
3454 completion = snapshot->value->id;
3455 if (!strncmp(str, completion, len)) {
3456 readline_add_completion(rs, completion);
3457 }
3458 snapshot = snapshot->next;
3459 }
3460 qapi_free_SnapshotInfoList(snapshots);
3461 }
3462
3463}
3464
3465void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
3466{
3467 if (nb_args == 2) {
3468 vm_completion(rs, str);
3469 }
3470}
3471
3472void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
3473{
3474 if (nb_args == 2) {
3475 vm_completion(rs, str);
3476 }
3477}
3478
Wenchao Xiac35b6402013-08-27 20:38:24 +08003479static void monitor_find_completion_by_table(Monitor *mon,
3480 const mon_cmd_t *cmd_table,
3481 char **args,
3482 int nb_args)
bellard81d09122004-07-14 17:21:37 +00003483{
3484 const char *cmdname;
Wenchao Xiac35b6402013-08-27 20:38:24 +08003485 int i;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003486 const char *ptype, *str, *name;
Anthony Liguoric227f092009-10-01 16:12:16 -05003487 const mon_cmd_t *cmd;
Max Reitzda27a002016-03-16 19:54:29 +01003488 BlockBackend *blk = NULL;
bellard81d09122004-07-14 17:21:37 +00003489
bellard81d09122004-07-14 17:21:37 +00003490 if (nb_args <= 1) {
3491 /* command completion */
3492 if (nb_args == 0)
3493 cmdname = "";
3494 else
3495 cmdname = args[0];
Wenchao Xiad2674b22013-08-27 20:38:16 +08003496 readline_set_completion_index(mon->rs, strlen(cmdname));
Wenchao Xiac35b6402013-08-27 20:38:24 +08003497 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003498 cmd_completion(mon, cmdname, cmd->name);
bellard81d09122004-07-14 17:21:37 +00003499 }
3500 } else {
3501 /* find the command */
Wenchao Xiac35b6402013-08-27 20:38:24 +08003502 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Jan Kiszka03a63482010-06-16 00:38:33 +02003503 if (compare_cmd(args[0], cmd->name)) {
3504 break;
3505 }
bellard81d09122004-07-14 17:21:37 +00003506 }
Jan Kiszka03a63482010-06-16 00:38:33 +02003507 if (!cmd->name) {
Wenchao Xiac35b6402013-08-27 20:38:24 +08003508 return;
Jan Kiszka03a63482010-06-16 00:38:33 +02003509 }
3510
Wenchao Xiad903a772013-08-27 20:38:25 +08003511 if (cmd->sub_table) {
3512 /* do the job again */
Stefan Weile7ae7712015-03-08 19:30:01 +01003513 monitor_find_completion_by_table(mon, cmd->sub_table,
3514 &args[1], nb_args - 1);
3515 return;
Wenchao Xiad903a772013-08-27 20:38:25 +08003516 }
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003517 if (cmd->command_completion) {
Stefan Weile7ae7712015-03-08 19:30:01 +01003518 cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
3519 return;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003520 }
Wenchao Xiad903a772013-08-27 20:38:25 +08003521
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003522 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003523 for(i = 0; i < nb_args - 2; i++) {
3524 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003525 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003526 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003527 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003528 }
3529 }
3530 str = args[nb_args - 1];
Kevin Wolf48fe86f2014-11-12 16:24:02 +01003531 while (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02003532 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00003533 }
bellard81d09122004-07-14 17:21:37 +00003534 switch(*ptype) {
3535 case 'F':
3536 /* file completion */
Wenchao Xiad2674b22013-08-27 20:38:16 +08003537 readline_set_completion_index(mon->rs, strlen(str));
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003538 file_completion(mon, str);
bellard81d09122004-07-14 17:21:37 +00003539 break;
3540 case 'B':
3541 /* block device name completion */
Wenchao Xia599a9262013-08-27 20:38:15 +08003542 readline_set_completion_index(mon->rs, strlen(str));
Max Reitzda27a002016-03-16 19:54:29 +01003543 while ((blk = blk_next(blk)) != NULL) {
3544 name = blk_name(blk);
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003545 if (str[0] == '\0' ||
3546 !strncmp(name, str, strlen(str))) {
3547 readline_add_completion(mon->rs, name);
3548 }
3549 }
bellard81d09122004-07-14 17:21:37 +00003550 break;
bellard7fe48482004-10-09 18:08:01 +00003551 case 's':
Wenchao Xia129be002013-08-27 20:38:26 +08003552 case 'S':
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003553 if (!strcmp(cmd->name, "help|?")) {
Wenchao Xia7ca0e062013-08-27 20:38:27 +08003554 monitor_find_completion_by_table(mon, cmd_table,
3555 &args[1], nb_args - 1);
bellard7fe48482004-10-09 18:08:01 +00003556 }
3557 break;
bellard81d09122004-07-14 17:21:37 +00003558 default:
3559 break;
3560 }
3561 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08003562}
3563
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003564static void monitor_find_completion(void *opaque,
Wenchao Xiac35b6402013-08-27 20:38:24 +08003565 const char *cmdline)
3566{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003567 Monitor *mon = opaque;
Wenchao Xiac35b6402013-08-27 20:38:24 +08003568 char *args[MAX_ARGS];
3569 int nb_args, len;
3570
3571 /* 1. parse the cmdline */
3572 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
3573 return;
3574 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08003575
3576 /* if the line ends with a space, it means we want to complete the
3577 next arg */
3578 len = strlen(cmdline);
3579 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
3580 if (nb_args >= MAX_ARGS) {
3581 goto cleanup;
3582 }
3583 args[nb_args++] = g_strdup("");
3584 }
3585
3586 /* 2. auto complete according to args */
3587 monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args);
Jan Kiszka03a63482010-06-16 00:38:33 +02003588
3589cleanup:
Wenchao Xiadcc70cd2013-08-27 20:38:22 +08003590 free_cmdline_args(args, nb_args);
bellard81d09122004-07-14 17:21:37 +00003591}
3592
aliguori731b0362009-03-05 23:01:42 +00003593static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003594{
aliguori731b0362009-03-05 23:01:42 +00003595 Monitor *mon = opaque;
3596
Jan Kiszkac62313b2009-12-04 14:05:29 +01003597 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003598}
3599
Markus Armbruster40861822015-05-29 10:27:16 +02003600static bool invalid_qmp_mode(const Monitor *mon, const mon_cmd_t *cmd,
3601 Error **errp)
Luiz Capitulino09069b12010-02-04 18:10:06 -02003602{
Markus Armbruster485febc2015-03-13 17:25:50 +01003603 bool is_cap = cmd->mhandler.cmd_new == qmp_capabilities;
Markus Armbrusterf994b252015-03-06 19:51:51 +01003604
3605 if (is_cap && mon->qmp.in_command_mode) {
Markus Armbruster40861822015-05-29 10:27:16 +02003606 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
3607 "Capabilities negotiation is already complete, command "
3608 "'%s' ignored", cmd->name);
Eric Blake2d5a8342015-04-15 09:19:23 -06003609 return true;
3610 }
Markus Armbrusterf994b252015-03-06 19:51:51 +01003611 if (!is_cap && !mon->qmp.in_command_mode) {
Markus Armbruster40861822015-05-29 10:27:16 +02003612 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
3613 "Expecting capabilities negotiation with "
3614 "'qmp_capabilities' before command '%s'", cmd->name);
Eric Blake2d5a8342015-04-15 09:19:23 -06003615 return true;
3616 }
3617 return false;
Luiz Capitulino09069b12010-02-04 18:10:06 -02003618}
3619
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003620/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03003621 * Argument validation rules:
3622 *
3623 * 1. The argument must exist in cmd_args qdict
3624 * 2. The argument type must be the expected one
3625 *
3626 * Special case: If the argument doesn't exist in cmd_args and
3627 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
3628 * checking is skipped for it.
3629 */
Markus Armbruster326283a2015-03-02 18:39:09 +01003630static void check_client_args_type(const QDict *client_args,
3631 const QDict *cmd_args, int flags,
3632 Error **errp)
Luiz Capitulino4af91932010-06-22 11:44:05 -03003633{
3634 const QDictEntry *ent;
3635
3636 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
3637 QObject *obj;
3638 QString *arg_type;
3639 const QObject *client_arg = qdict_entry_value(ent);
3640 const char *client_arg_name = qdict_entry_key(ent);
3641
3642 obj = qdict_get(cmd_args, client_arg_name);
3643 if (!obj) {
3644 if (flags & QMP_ACCEPT_UNKNOWNS) {
3645 /* handler accepts unknowns */
3646 continue;
3647 }
3648 /* client arg doesn't exist */
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003649 error_setg(errp, QERR_INVALID_PARAMETER, client_arg_name);
Markus Armbruster326283a2015-03-02 18:39:09 +01003650 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003651 }
3652
3653 arg_type = qobject_to_qstring(obj);
3654 assert(arg_type != NULL);
3655
3656 /* check if argument's type is correct */
3657 switch (qstring_get_str(arg_type)[0]) {
3658 case 'F':
3659 case 'B':
3660 case 's':
3661 if (qobject_type(client_arg) != QTYPE_QSTRING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003662 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3663 client_arg_name, "string");
Markus Armbruster326283a2015-03-02 18:39:09 +01003664 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003665 }
3666 break;
3667 case 'i':
3668 case 'l':
3669 case 'M':
Jes Sorensendbc0c672010-10-21 17:15:47 +02003670 case 'o':
Luiz Capitulino4af91932010-06-22 11:44:05 -03003671 if (qobject_type(client_arg) != QTYPE_QINT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003672 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3673 client_arg_name, "int");
Markus Armbruster326283a2015-03-02 18:39:09 +01003674 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003675 }
3676 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003677 case 'T':
3678 if (qobject_type(client_arg) != QTYPE_QINT &&
3679 qobject_type(client_arg) != QTYPE_QFLOAT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003680 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3681 client_arg_name, "number");
Markus Armbruster326283a2015-03-02 18:39:09 +01003682 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003683 }
3684 break;
3685 case 'b':
3686 case '-':
3687 if (qobject_type(client_arg) != QTYPE_QBOOL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003688 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3689 client_arg_name, "bool");
Markus Armbruster326283a2015-03-02 18:39:09 +01003690 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003691 }
3692 break;
3693 case 'O':
3694 assert(flags & QMP_ACCEPT_UNKNOWNS);
3695 break;
Paolo Bonzinib9f89782012-03-22 12:51:11 +01003696 case 'q':
3697 /* Any QObject can be passed. */
3698 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003699 case '/':
3700 case '.':
3701 /*
3702 * These types are not supported by QMP and thus are not
3703 * handled here. Fall through.
3704 */
3705 default:
3706 abort();
3707 }
3708 }
Luiz Capitulino4af91932010-06-22 11:44:05 -03003709}
3710
3711/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003712 * - Check if the client has passed all mandatory args
3713 * - Set special flags for argument validation
3714 */
Markus Armbruster326283a2015-03-02 18:39:09 +01003715static void check_mandatory_args(const QDict *cmd_args,
3716 const QDict *client_args, int *flags,
3717 Error **errp)
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003718{
3719 const QDictEntry *ent;
3720
3721 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
3722 const char *cmd_arg_name = qdict_entry_key(ent);
3723 QString *type = qobject_to_qstring(qdict_entry_value(ent));
3724 assert(type != NULL);
3725
3726 if (qstring_get_str(type)[0] == 'O') {
3727 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
3728 *flags |= QMP_ACCEPT_UNKNOWNS;
3729 } else if (qstring_get_str(type)[0] != '-' &&
3730 qstring_get_str(type)[1] != '?' &&
3731 !qdict_haskey(client_args, cmd_arg_name)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003732 error_setg(errp, QERR_MISSING_PARAMETER, cmd_arg_name);
Markus Armbruster326283a2015-03-02 18:39:09 +01003733 return;
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003734 }
3735 }
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003736}
3737
3738static QDict *qdict_from_args_type(const char *args_type)
3739{
3740 int i;
3741 QDict *qdict;
3742 QString *key, *type, *cur_qs;
3743
3744 assert(args_type != NULL);
3745
3746 qdict = qdict_new();
3747
3748 if (args_type == NULL || args_type[0] == '\0') {
3749 /* no args, empty qdict */
3750 goto out;
3751 }
3752
3753 key = qstring_new();
3754 type = qstring_new();
3755
3756 cur_qs = key;
3757
3758 for (i = 0;; i++) {
3759 switch (args_type[i]) {
3760 case ',':
3761 case '\0':
3762 qdict_put(qdict, qstring_get_str(key), type);
3763 QDECREF(key);
3764 if (args_type[i] == '\0') {
3765 goto out;
3766 }
3767 type = qstring_new(); /* qdict has ref */
3768 cur_qs = key = qstring_new();
3769 break;
3770 case ':':
3771 cur_qs = type;
3772 break;
3773 default:
3774 qstring_append_chr(cur_qs, args_type[i]);
3775 break;
3776 }
3777 }
3778
3779out:
3780 return qdict;
3781}
3782
3783/*
3784 * Client argument checking rules:
3785 *
3786 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03003787 * 2. Each argument provided by the client must be expected
3788 * 3. Each argument provided by the client must have the type expected
3789 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003790 */
Markus Armbruster326283a2015-03-02 18:39:09 +01003791static void qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args,
3792 Error **errp)
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003793{
Markus Armbruster326283a2015-03-02 18:39:09 +01003794 Error *err = NULL;
3795 int flags;
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003796 QDict *cmd_args;
3797
3798 cmd_args = qdict_from_args_type(cmd->args_type);
3799
3800 flags = 0;
Markus Armbruster326283a2015-03-02 18:39:09 +01003801 check_mandatory_args(cmd_args, client_args, &flags, &err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003802 if (err) {
3803 goto out;
3804 }
3805
Markus Armbruster326283a2015-03-02 18:39:09 +01003806 check_client_args_type(client_args, cmd_args, flags, &err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003807
3808out:
Markus Armbruster326283a2015-03-02 18:39:09 +01003809 error_propagate(errp, err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003810 QDECREF(cmd_args);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003811}
3812
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003813/*
3814 * Input object checking rules
3815 *
3816 * 1. Input object must be a dict
3817 * 2. The "execute" key must exist
3818 * 3. The "execute" key must be a string
3819 * 4. If the "arguments" key exists, it must be a dict
3820 * 5. If the "id" key exists, it can be anything (ie. json-value)
3821 * 6. Any argument not listed above is considered invalid
3822 */
Markus Armbrusterba0510a2015-03-02 18:41:43 +01003823static QDict *qmp_check_input_obj(QObject *input_obj, Error **errp)
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003824{
3825 const QDictEntry *ent;
3826 int has_exec_key = 0;
3827 QDict *input_dict;
3828
3829 if (qobject_type(input_obj) != QTYPE_QDICT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003830 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "object");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003831 return NULL;
3832 }
3833
3834 input_dict = qobject_to_qdict(input_obj);
3835
3836 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
3837 const char *arg_name = qdict_entry_key(ent);
3838 const QObject *arg_obj = qdict_entry_value(ent);
3839
3840 if (!strcmp(arg_name, "execute")) {
3841 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003842 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
3843 "execute", "string");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003844 return NULL;
3845 }
3846 has_exec_key = 1;
3847 } else if (!strcmp(arg_name, "arguments")) {
3848 if (qobject_type(arg_obj) != QTYPE_QDICT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003849 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
3850 "arguments", "object");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003851 return NULL;
3852 }
Markus Armbruster779cec42015-06-08 10:44:30 +02003853 } else if (!strcmp(arg_name, "id")) {
3854 /* Any string is acceptable as "id", so nothing to check */
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003855 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003856 error_setg(errp, QERR_QMP_EXTRA_MEMBER, arg_name);
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003857 return NULL;
3858 }
3859 }
3860
3861 if (!has_exec_key) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003862 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "execute");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003863 return NULL;
3864 }
3865
3866 return input_dict;
3867}
3868
Paolo Bonzini95385fe2015-11-25 22:23:31 +01003869static void handle_qmp_command(JSONMessageParser *parser, GQueue *tokens)
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003870{
Markus Armbruster326283a2015-03-02 18:39:09 +01003871 Error *local_err = NULL;
Markus Armbruster84add862015-03-05 16:45:15 +01003872 QObject *obj, *data;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003873 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003874 const mon_cmd_t *cmd;
Luiz Capitulino40e5a012011-10-21 16:15:31 -02003875 const char *cmd_name;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003876 Monitor *mon = cur_mon;
3877
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03003878 args = input = NULL;
Markus Armbruster84add862015-03-05 16:45:15 +01003879 data = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003880
3881 obj = json_parser_parse(tokens, NULL);
3882 if (!obj) {
3883 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003884 error_setg(&local_err, QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003885 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003886 }
3887
Markus Armbrusterba0510a2015-03-02 18:41:43 +01003888 input = qmp_check_input_obj(obj, &local_err);
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003889 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003890 qobject_decref(obj);
3891 goto err_out;
3892 }
3893
Markus Armbruster74358f22015-03-06 19:35:59 +01003894 mon->qmp.id = qdict_get(input, "id");
3895 qobject_incref(mon->qmp.id);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003896
Luiz Capitulino0bbab462010-05-31 17:32:50 -03003897 cmd_name = qdict_get_str(input, "execute");
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +01003898 trace_handle_qmp_command(mon, cmd_name);
Anthony Liguorie3193602011-09-02 12:34:47 -05003899 cmd = qmp_find_cmd(cmd_name);
Eric Blake2d5a8342015-04-15 09:19:23 -06003900 if (!cmd) {
Markus Armbruster710aec92015-03-06 11:28:00 +01003901 error_set(&local_err, ERROR_CLASS_COMMAND_NOT_FOUND,
3902 "The command %s has not been found", cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03003903 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003904 }
Markus Armbruster40861822015-05-29 10:27:16 +02003905 if (invalid_qmp_mode(mon, cmd, &local_err)) {
Eric Blake2d5a8342015-04-15 09:19:23 -06003906 goto err_out;
3907 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003908
3909 obj = qdict_get(input, "arguments");
3910 if (!obj) {
3911 args = qdict_new();
3912 } else {
3913 args = qobject_to_qdict(obj);
3914 QINCREF(args);
3915 }
3916
Markus Armbruster326283a2015-03-02 18:39:09 +01003917 qmp_check_client_args(cmd, args, &local_err);
3918 if (local_err) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003919 goto err_out;
3920 }
3921
Markus Armbruster485febc2015-03-13 17:25:50 +01003922 cmd->mhandler.cmd_new(args, &data, &local_err);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003923
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003924err_out:
Markus Armbruster710aec92015-03-06 11:28:00 +01003925 monitor_protocol_emitter(mon, data, local_err);
Markus Armbruster84add862015-03-05 16:45:15 +01003926 qobject_decref(data);
Markus Armbruster3a81a102015-10-29 12:15:09 +01003927 error_free(local_err);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03003928 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003929 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003930}
3931
Markus Armbrusterc83fe232015-03-06 19:20:51 +01003932static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003933{
3934 Monitor *old_mon = cur_mon;
3935
3936 cur_mon = opaque;
3937
Markus Armbruster74358f22015-03-06 19:35:59 +01003938 json_message_parser_feed(&cur_mon->qmp.parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003939
3940 cur_mon = old_mon;
3941}
3942
aliguori731b0362009-03-05 23:01:42 +00003943static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00003944{
aliguori731b0362009-03-05 23:01:42 +00003945 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00003946 int i;
aliguori376253e2009-03-05 23:01:23 +00003947
aliguori731b0362009-03-05 23:01:42 +00003948 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00003949
aliguoricde76ee2009-03-05 23:01:51 +00003950 if (cur_mon->rs) {
3951 for (i = 0; i < size; i++)
3952 readline_handle_byte(cur_mon->rs, buf[i]);
3953 } else {
3954 if (size == 0 || buf[size - 1] != 0)
3955 monitor_printf(cur_mon, "corrupted command\n");
3956 else
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003957 handle_hmp_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00003958 }
aliguori731b0362009-03-05 23:01:42 +00003959
3960 cur_mon = old_mon;
3961}
aliguorid8f44602008-10-06 13:52:44 +00003962
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003963static void monitor_command_cb(void *opaque, const char *cmdline,
3964 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00003965{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003966 Monitor *mon = opaque;
3967
aliguori731b0362009-03-05 23:01:42 +00003968 monitor_suspend(mon);
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003969 handle_hmp_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00003970 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00003971}
3972
aliguoricde76ee2009-03-05 23:01:51 +00003973int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003974{
aliguoricde76ee2009-03-05 23:01:51 +00003975 if (!mon->rs)
3976 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00003977 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00003978 return 0;
aliguorid8f44602008-10-06 13:52:44 +00003979}
3980
aliguori376253e2009-03-05 23:01:23 +00003981void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003982{
aliguoricde76ee2009-03-05 23:01:51 +00003983 if (!mon->rs)
3984 return;
aliguori731b0362009-03-05 23:01:42 +00003985 if (--mon->suspend_cnt == 0)
3986 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00003987}
3988
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003989static QObject *get_qmp_greeting(void)
3990{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03003991 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003992
Markus Armbruster7fad30f2015-09-16 13:06:19 +02003993 qmp_marshal_query_version(NULL, &ver, NULL);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003994 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
3995}
3996
Markus Armbrusterc83fe232015-03-06 19:20:51 +01003997static void monitor_qmp_event(void *opaque, int event)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003998{
Luiz Capitulino47116d12010-02-08 17:01:30 -02003999 QObject *data;
4000 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004001
Luiz Capitulino47116d12010-02-08 17:01:30 -02004002 switch (event) {
4003 case CHR_EVENT_OPENED:
Markus Armbrusterf994b252015-03-06 19:51:51 +01004004 mon->qmp.in_command_mode = false;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004005 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004006 monitor_json_emitter(mon, data);
4007 qobject_decref(data);
Corey Bryantefb87c12012-08-14 16:43:48 -04004008 mon_refcount++;
Luiz Capitulino47116d12010-02-08 17:01:30 -02004009 break;
4010 case CHR_EVENT_CLOSED:
Markus Armbruster74358f22015-03-06 19:35:59 +01004011 json_message_parser_destroy(&mon->qmp.parser);
4012 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Corey Bryantefb87c12012-08-14 16:43:48 -04004013 mon_refcount--;
4014 monitor_fdsets_cleanup();
Luiz Capitulino47116d12010-02-08 17:01:30 -02004015 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004016 }
4017}
4018
aliguori731b0362009-03-05 23:01:42 +00004019static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004020{
aliguori376253e2009-03-05 23:01:23 +00004021 Monitor *mon = opaque;
4022
aliguori2724b182009-03-05 23:01:47 +00004023 switch (event) {
4024 case CHR_EVENT_MUX_IN:
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02004025 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004026 mon->mux_out = 0;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02004027 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004028 if (mon->reset_seen) {
4029 readline_restart(mon->rs);
4030 monitor_resume(mon);
4031 monitor_flush(mon);
4032 } else {
4033 mon->suspend_cnt = 0;
4034 }
aliguori2724b182009-03-05 23:01:47 +00004035 break;
ths86e94de2007-01-05 22:01:59 +00004036
aliguori2724b182009-03-05 23:01:47 +00004037 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004038 if (mon->reset_seen) {
4039 if (mon->suspend_cnt == 0) {
4040 monitor_printf(mon, "\n");
4041 }
4042 monitor_flush(mon);
4043 monitor_suspend(mon);
4044 } else {
4045 mon->suspend_cnt++;
4046 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02004047 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004048 mon->mux_out = 1;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02004049 qemu_mutex_unlock(&mon->out_lock);
aliguori2724b182009-03-05 23:01:47 +00004050 break;
4051
Amit Shahb6b8df52009-10-07 18:31:16 +05304052 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004053 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4054 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004055 if (!mon->mux_out) {
Stratos Psomadakise5554e22014-09-15 15:34:57 +03004056 readline_restart(mon->rs);
aliguori2724b182009-03-05 23:01:47 +00004057 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004058 }
4059 mon->reset_seen = 1;
Corey Bryantefb87c12012-08-14 16:43:48 -04004060 mon_refcount++;
4061 break;
4062
4063 case CHR_EVENT_CLOSED:
4064 mon_refcount--;
4065 monitor_fdsets_cleanup();
aliguori2724b182009-03-05 23:01:47 +00004066 break;
4067 }
ths86e94de2007-01-05 22:01:59 +00004068}
4069
Wayne Xia816f8922011-10-12 11:32:41 +08004070static int
4071compare_mon_cmd(const void *a, const void *b)
4072{
4073 return strcmp(((const mon_cmd_t *)a)->name,
4074 ((const mon_cmd_t *)b)->name);
4075}
4076
4077static void sortcmdlist(void)
4078{
4079 int array_num;
4080 int elem_size = sizeof(mon_cmd_t);
4081
4082 array_num = sizeof(mon_cmds)/elem_size-1;
4083 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
4084
4085 array_num = sizeof(info_cmds)/elem_size-1;
4086 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
4087}
4088
aliguori76655d62009-03-06 20:27:37 +00004089
4090/*
4091 * Local variables:
4092 * c-indent-level: 4
4093 * c-basic-offset: 4
4094 * tab-width: 8
4095 * End:
4096 */
4097
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004098/* These functions just adapt the readline interface in a typesafe way. We
4099 * could cast function pointers but that discards compiler checks.
4100 */
Stefan Weild5d15072014-01-25 18:18:23 +01004101static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
4102 const char *fmt, ...)
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004103{
4104 va_list ap;
4105 va_start(ap, fmt);
4106 monitor_vprintf(opaque, fmt, ap);
4107 va_end(ap);
4108}
4109
4110static void monitor_readline_flush(void *opaque)
4111{
4112 monitor_flush(opaque);
4113}
4114
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004115static void __attribute__((constructor)) monitor_lock_init(void)
4116{
4117 qemu_mutex_init(&monitor_lock);
4118}
4119
aliguori731b0362009-03-05 23:01:42 +00004120void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004121{
aliguori731b0362009-03-05 23:01:42 +00004122 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004123 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004124
4125 if (is_first_init) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +02004126 monitor_qapi_event_init();
Wenchao Xiad0383172013-08-27 20:38:18 +08004127 sortcmdlist();
ths20d8a3e2007-02-18 17:04:49 +00004128 is_first_init = 0;
4129 }
aliguori87127162009-03-05 23:01:29 +00004130
Wenchao Xiab01fe892013-08-27 20:38:19 +08004131 mon = g_malloc(sizeof(*mon));
4132 monitor_data_init(mon);
ths20d8a3e2007-02-18 17:04:49 +00004133
aliguori87127162009-03-05 23:01:29 +00004134 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00004135 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004136 if (flags & MONITOR_USE_READLINE) {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004137 mon->rs = readline_init(monitor_readline_printf,
4138 monitor_readline_flush,
4139 mon,
4140 monitor_find_completion);
aliguoricde76ee2009-03-05 23:01:51 +00004141 monitor_read_command(mon, 0);
4142 }
aliguori87127162009-03-05 23:01:29 +00004143
Markus Armbruster9f3982f2015-03-06 19:56:38 +01004144 if (monitor_is_qmp(mon)) {
Markus Armbrusterc83fe232015-03-06 19:20:51 +01004145 qemu_chr_add_handlers(chr, monitor_can_read, monitor_qmp_read,
4146 monitor_qmp_event, mon);
Anthony Liguori15f31512011-08-15 11:17:35 -05004147 qemu_chr_fe_set_echo(chr, true);
Markus Armbruster74358f22015-03-06 19:35:59 +01004148 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004149 } else {
4150 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4151 monitor_event, mon);
4152 }
aliguori87127162009-03-05 23:01:29 +00004153
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004154 qemu_mutex_lock(&monitor_lock);
Blue Swirl72cf2d42009-09-12 07:36:22 +00004155 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004156 qemu_mutex_unlock(&monitor_lock);
bellard7e2515e2004-08-01 21:52:19 +00004157}
4158
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004159static void bdrv_password_cb(void *opaque, const char *password,
4160 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00004161{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004162 Monitor *mon = opaque;
4163 BlockDriverState *bs = readline_opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004164 int ret = 0;
Markus Armbruster4d2855a2015-01-29 10:37:00 +01004165 Error *local_err = NULL;
bellard7e2515e2004-08-01 21:52:19 +00004166
Markus Armbruster4d2855a2015-01-29 10:37:00 +01004167 bdrv_add_key(bs, password, &local_err);
4168 if (local_err) {
Markus Armbruster193227f2015-12-18 16:35:06 +01004169 error_report_err(local_err);
aliguoribb5fc202009-03-05 23:01:15 +00004170 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004171 }
aliguori731b0362009-03-05 23:01:42 +00004172 if (mon->password_completion_cb)
4173 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004174
aliguori731b0362009-03-05 23:01:42 +00004175 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004176}
aliguoric0f4ce72009-03-05 23:01:01 +00004177
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004178int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
Markus Armbruster097310b2014-10-07 13:59:15 +02004179 BlockCompletionFunc *completion_cb,
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004180 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004181{
aliguoricde76ee2009-03-05 23:01:51 +00004182 int err;
4183
aliguori376253e2009-03-05 23:01:23 +00004184 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4185 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00004186
aliguori731b0362009-03-05 23:01:42 +00004187 mon->password_completion_cb = completion_cb;
4188 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004189
aliguoricde76ee2009-03-05 23:01:51 +00004190 err = monitor_read_password(mon, bdrv_password_cb, bs);
4191
4192 if (err && completion_cb)
4193 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004194
4195 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00004196}
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004197
4198int monitor_read_block_device_key(Monitor *mon, const char *device,
Markus Armbruster097310b2014-10-07 13:59:15 +02004199 BlockCompletionFunc *completion_cb,
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004200 void *opaque)
4201{
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01004202 Error *err = NULL;
Fam Zheng55606252015-03-02 19:36:46 +08004203 BlockBackend *blk;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004204
Fam Zheng55606252015-03-02 19:36:46 +08004205 blk = blk_by_name(device);
4206 if (!blk) {
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004207 monitor_printf(mon, "Device not found %s\n", device);
4208 return -1;
4209 }
Max Reitz5433c242015-10-19 17:53:29 +02004210 if (!blk_bs(blk)) {
4211 monitor_printf(mon, "Device '%s' has no medium\n", device);
4212 return -1;
4213 }
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004214
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01004215 bdrv_add_key(blk_bs(blk), NULL, &err);
4216 if (err) {
4217 error_free(err);
4218 return monitor_read_bdrv_key_start(mon, blk_bs(blk), completion_cb, opaque);
4219 }
4220
4221 if (completion_cb) {
4222 completion_cb(opaque, 0);
4223 }
4224 return 0;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004225}
Paolo Bonzini4d454572012-11-26 16:03:42 +01004226
4227QemuOptsList qemu_mon_opts = {
4228 .name = "mon",
4229 .implied_opt_name = "chardev",
4230 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
4231 .desc = {
4232 {
4233 .name = "mode",
4234 .type = QEMU_OPT_STRING,
4235 },{
4236 .name = "chardev",
4237 .type = QEMU_OPT_STRING,
4238 },{
4239 .name = "default",
4240 .type = QEMU_OPT_BOOL,
4241 },{
4242 .name = "pretty",
4243 .type = QEMU_OPT_BOOL,
4244 },
4245 { /* end of list */ }
4246 },
4247};
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004248
4249#ifndef TARGET_I386
4250void qmp_rtc_reset_reinjection(Error **errp)
4251{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004252 error_setg(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection");
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004253}
4254#endif
Jason J. Herne7ee0c3e2015-06-26 14:03:16 -04004255
4256#ifndef TARGET_S390X
4257void qmp_dump_skeys(const char *filename, Error **errp)
4258{
4259 error_setg(errp, QERR_FEATURE_DISABLED, "dump-skeys");
4260}
4261#endif
Peter Xuae50a772016-03-30 17:27:24 +01004262
4263#ifndef TARGET_ARM
4264GICCapabilityList *qmp_query_gic_capabilities(Error **errp)
4265{
4266 error_setg(errp, QERR_FEATURE_DISABLED, "query-gic-capabilities");
4267 return NULL;
4268}
4269#endif