blob: 4c02f0f18f91739ca87c6b78f5b9ee6c5e04eb6c [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"
ths6a5bd302007-12-03 17:05:38 +000081
Markus Armbruster1ce6be22015-02-06 14:18:24 +010082/* for hmp_info_irq/pic */
Jan Kiszka661f1922011-10-16 11:53:13 +020083#if defined(TARGET_SPARC)
Paolo Bonzini0d09e412013-02-05 17:06:20 +010084#include "hw/sparc/sun4m.h"
Jan Kiszka661f1922011-10-16 11:53:13 +020085#endif
Paolo Bonzini0d09e412013-02-05 17:06:20 +010086#include "hw/lm32/lm32_pic.h"
Jan Kiszka661f1922011-10-16 11:53:13 +020087
Jason J. Hernea4538a52015-06-26 14:07:21 -040088#if defined(TARGET_S390X)
89#include "hw/s390x/storage-keys.h"
90#endif
91
bellard9307c4c2004-04-04 12:57:25 +000092/*
93 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000094 *
bellard9307c4c2004-04-04 12:57:25 +000095 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000096 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000097 * 's' string (accept optional quote)
Wenchao Xia129be002013-08-27 20:38:26 +080098 * 'S' it just appends the rest of the string (accept optional quote)
Markus Armbruster361127d2010-02-10 20:24:35 +010099 * 'O' option string of the form NAME=VALUE,...
100 * parsed according to QemuOptsList given by its name
101 * Example: 'device:O' uses qemu_device_opts.
102 * Restriction: only lists with empty desc are supported
103 * TODO lift the restriction
bellard92a31b12005-02-10 22:00:52 +0000104 * 'i' 32 bit integer
105 * 'l' target long (32 or 64 bit)
Luiz Capitulino91162842012-04-26 17:34:30 -0300106 * 'M' Non-negative target long (32 or 64 bit), in user mode the
107 * value is multiplied by 2^20 (think Mebibyte)
Jes Sorensendbc0c672010-10-21 17:15:47 +0200108 * 'o' octets (aka bytes)
Kevin Wolf5e009842013-06-05 14:19:27 +0200109 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
110 * K, k suffix, which multiplies the value by 2^60 for suffixes E
111 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
112 * 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 +0100113 * 'T' double
114 * user mode accepts an optional ms, us, ns suffix,
115 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +0000116 * '/' optional gdb-like print format (like "/10x")
117 *
Luiz Capitulinofb466602009-08-28 15:27:27 -0300118 * '?' optional type (for all types, except '/')
119 * '.' other form of optional type (for 'i' and 'l')
Markus Armbruster942cd1f2010-03-26 09:07:09 +0100120 * 'b' boolean
121 * user mode accepts "on" or "off"
Luiz Capitulinofb466602009-08-28 15:27:27 -0300122 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +0000123 *
124 */
125
Anthony Liguoric227f092009-10-01 16:12:16 -0500126typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +0000127 const char *name;
bellard9307c4c2004-04-04 12:57:25 +0000128 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +0000129 const char *params;
130 const char *help;
Luiz Capitulino910df892009-10-07 13:41:51 -0300131 union {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -0300132 void (*cmd)(Monitor *mon, const QDict *qdict);
Markus Armbruster485febc2015-03-13 17:25:50 +0100133 void (*cmd_new)(QDict *params, QObject **ret_data, Error **errp);
Luiz Capitulino910df892009-10-07 13:41:51 -0300134 } mhandler;
Wenchao Xia5f3d3352013-01-14 14:06:27 +0800135 /* @sub_table is a list of 2nd level of commands. If it do not exist,
136 * mhandler should be used. If it exist, sub_table[?].mhandler should be
137 * used, and mhandler of 1st level plays the role of help function.
138 */
139 struct mon_cmd_t *sub_table;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +0100140 void (*command_completion)(ReadLineState *rs, int nb_args, const char *str);
Anthony Liguoric227f092009-10-01 16:12:16 -0500141} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000142
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100143/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500144typedef struct mon_fd_t mon_fd_t;
145struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100146 char *name;
147 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500148 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100149};
150
Corey Bryantba1c0482012-08-14 16:43:43 -0400151/* file descriptor associated with a file descriptor set */
152typedef struct MonFdsetFd MonFdsetFd;
153struct MonFdsetFd {
154 int fd;
155 bool removed;
156 char *opaque;
157 QLIST_ENTRY(MonFdsetFd) next;
158};
159
160/* file descriptor set containing fds passed via SCM_RIGHTS */
161typedef struct MonFdset MonFdset;
162struct MonFdset {
163 int64_t id;
164 QLIST_HEAD(, MonFdsetFd) fds;
Corey Bryantadb696f2012-08-14 16:43:47 -0400165 QLIST_HEAD(, MonFdsetFd) dup_fds;
Corey Bryantba1c0482012-08-14 16:43:43 -0400166 QLIST_ENTRY(MonFdset) next;
167};
168
Markus Armbruster74358f22015-03-06 19:35:59 +0100169typedef struct {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200170 QObject *id;
171 JSONMessageParser parser;
Markus Armbrusterf994b252015-03-06 19:51:51 +0100172 /*
173 * When a client connects, we're in capabilities negotiation mode.
174 * When command qmp_capabilities succeeds, we go into command
175 * mode.
176 */
177 bool in_command_mode; /* are we in command mode? */
Markus Armbruster74358f22015-03-06 19:35:59 +0100178} MonitorQMP;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200179
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100180/*
181 * To prevent flooding clients, events can be throttled. The
182 * throttling is calculated globally, rather than per-Monitor
183 * instance.
184 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200185typedef struct MonitorQAPIEventState {
Markus Armbruster7de0be62015-10-15 17:08:35 +0200186 QAPIEvent event; /* Throttling state for this event type and... */
187 QDict *data; /* ... data, see qapi_event_throttle_equal() */
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100188 QEMUTimer *timer; /* Timer for handling delayed events */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200189 QDict *qdict; /* Delayed event (if any) */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200190} MonitorQAPIEventState;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100191
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200192typedef struct {
193 int64_t rate; /* Minimum time (in ns) between two events */
194} MonitorQAPIEventConf;
195
aliguori87127162009-03-05 23:01:29 +0000196struct Monitor {
197 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200198 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000199 int flags;
200 int suspend_cnt;
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400201 bool skip_flush;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200202
203 QemuMutex out_lock;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400204 QString *outbuf;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200205 guint out_watch;
206
207 /* Read under either BQL or out_lock, written with BQL+out_lock. */
208 int mux_out;
209
aliguori731b0362009-03-05 23:01:42 +0000210 ReadLineState *rs;
Markus Armbruster74358f22015-03-06 19:35:59 +0100211 MonitorQMP qmp;
Andreas Färbercb446ec2013-05-01 14:24:52 +0200212 CPUState *mon_cpu;
Markus Armbruster097310b2014-10-07 13:59:15 +0200213 BlockCompletionFunc *password_completion_cb;
aliguori731b0362009-03-05 23:01:42 +0000214 void *password_opaque;
Wenchao Xia77172392013-08-27 20:38:20 +0800215 mon_cmd_t *cmd_table;
Anthony Liguoric227f092009-10-01 16:12:16 -0500216 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000217 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000218};
219
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300220/* QMP checker flags */
221#define QMP_ACCEPT_UNKNOWNS 1
222
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200223/* Protects mon_list, monitor_event_state. */
224static QemuMutex monitor_lock;
225
Blue Swirl72cf2d42009-09-12 07:36:22 +0000226static QLIST_HEAD(mon_list, Monitor) mon_list;
Corey Bryantba1c0482012-08-14 16:43:43 -0400227static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
Corey Bryantefb87c12012-08-14 16:43:48 -0400228static int mon_refcount;
bellard7e2515e2004-08-01 21:52:19 +0000229
Wayne Xia816f8922011-10-12 11:32:41 +0800230static mon_cmd_t mon_cmds[];
231static mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000232
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300233static const mon_cmd_t qmp_cmds[];
234
Markus Armbruster8631b602010-02-18 11:41:55 +0100235Monitor *cur_mon;
aliguori376253e2009-03-05 23:01:23 +0000236
Alberto Garciadc599972016-03-10 13:55:26 +0200237static QEMUClockType event_clock_type = QEMU_CLOCK_REALTIME;
238
Stefan Hajnoczic60bf332013-11-14 11:54:14 +0100239static void monitor_command_cb(void *opaque, const char *cmdline,
240 void *readline_opaque);
aliguori83ab7952008-08-19 14:44:22 +0000241
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100242/**
243 * Is @mon a QMP monitor?
244 */
245static inline bool monitor_is_qmp(const Monitor *mon)
Luiz Capitulino418173c2009-11-26 22:58:51 -0200246{
247 return (mon->flags & MONITOR_USE_CONTROL);
248}
249
Markus Armbruster489653b2015-03-06 20:01:05 +0100250/**
251 * Is the current monitor, if any, a QMP monitor?
252 */
253bool monitor_cur_is_qmp(void)
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100254{
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100255 return cur_mon && monitor_is_qmp(cur_mon);
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100256}
257
Anthony Liguori7060b472011-09-02 12:34:50 -0500258void monitor_read_command(Monitor *mon, int show_prompt)
aliguori731b0362009-03-05 23:01:42 +0000259{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200260 if (!mon->rs)
261 return;
262
aliguori731b0362009-03-05 23:01:42 +0000263 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
264 if (show_prompt)
265 readline_show_prompt(mon->rs);
266}
bellard6a00d602005-11-21 23:25:50 +0000267
Anthony Liguori7060b472011-09-02 12:34:50 -0500268int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
269 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000270{
Markus Armbrusterbcf5d192015-03-12 17:26:46 +0100271 if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000272 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
273 /* prompt is printed on return from the command handler */
274 return 0;
275 } else {
276 monitor_printf(mon, "terminal does not support password prompting\n");
277 return -ENOTTY;
278 }
aliguoribb5fc202009-03-05 23:01:15 +0000279}
280
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200281static void monitor_flush_locked(Monitor *mon);
282
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100283static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
284 void *opaque)
285{
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200286 Monitor *mon = opaque;
287
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200288 qemu_mutex_lock(&mon->out_lock);
289 mon->out_watch = 0;
290 monitor_flush_locked(mon);
291 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100292 return FALSE;
293}
294
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200295/* Called with mon->out_lock held. */
296static void monitor_flush_locked(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000297{
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100298 int rc;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400299 size_t len;
300 const char *buf;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100301
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400302 if (mon->skip_flush) {
303 return;
304 }
305
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400306 buf = qstring_get_str(mon->outbuf);
307 len = qstring_get_length(mon->outbuf);
308
Paolo Bonzinia4cc73d2013-05-31 14:00:27 +0200309 if (len && !mon->mux_out) {
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400310 rc = qemu_chr_fe_write(mon->chr, (const uint8_t *) buf, len);
Stratos Psomadakis056f49f2014-01-27 12:30:15 +0200311 if ((rc < 0 && errno != EAGAIN) || (rc == len)) {
312 /* all flushed or error */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400313 QDECREF(mon->outbuf);
314 mon->outbuf = qstring_new();
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100315 return;
316 }
317 if (rc > 0) {
318 /* partinal write */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400319 QString *tmp = qstring_from_str(buf + rc);
320 QDECREF(mon->outbuf);
321 mon->outbuf = tmp;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100322 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200323 if (mon->out_watch == 0) {
Roger Pau Monnee02bc6d2014-05-23 17:57:49 +0200324 mon->out_watch = qemu_chr_fe_add_watch(mon->chr, G_IO_OUT|G_IO_HUP,
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200325 monitor_unblocked, mon);
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200326 }
bellard7e2515e2004-08-01 21:52:19 +0000327 }
328}
329
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200330void monitor_flush(Monitor *mon)
331{
332 qemu_mutex_lock(&mon->out_lock);
333 monitor_flush_locked(mon);
334 qemu_mutex_unlock(&mon->out_lock);
335}
336
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400337/* flush at every end of line */
aliguori376253e2009-03-05 23:01:23 +0000338static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000339{
ths60fe76f2007-12-16 03:02:09 +0000340 char c;
aliguori731b0362009-03-05 23:01:42 +0000341
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200342 qemu_mutex_lock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000343 for(;;) {
344 c = *str++;
345 if (c == '\0')
346 break;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400347 if (c == '\n') {
348 qstring_append_chr(mon->outbuf, '\r');
349 }
350 qstring_append_chr(mon->outbuf, c);
351 if (c == '\n') {
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200352 monitor_flush_locked(mon);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400353 }
bellard7e2515e2004-08-01 21:52:19 +0000354 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200355 qemu_mutex_unlock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000356}
357
aliguori376253e2009-03-05 23:01:23 +0000358void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000359{
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400360 char *buf;
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200361
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200362 if (!mon)
363 return;
364
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100365 if (monitor_is_qmp(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200366 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200367 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200368
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400369 buf = g_strdup_vprintf(fmt, ap);
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200370 monitor_puts(mon, buf);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400371 g_free(buf);
bellard7e2515e2004-08-01 21:52:19 +0000372}
373
aliguori376253e2009-03-05 23:01:23 +0000374void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000375{
376 va_list ap;
377 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000378 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000379 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000380}
381
Pavel Butsykincaf15312015-09-22 16:18:17 +0300382int monitor_fprintf(FILE *stream, const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000383{
384 va_list ap;
385 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000386 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000387 va_end(ap);
388 return 0;
389}
390
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200391static void monitor_json_emitter(Monitor *mon, const QObject *data)
392{
393 QString *json;
394
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200395 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
396 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200397 assert(json != NULL);
398
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200399 qstring_append_chr(json, '\n');
400 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200401
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200402 QDECREF(json);
403}
404
Markus Armbruster710aec92015-03-06 11:28:00 +0100405static QDict *build_qmp_error_dict(Error *err)
Luiz Capitulinode253f12012-07-27 16:18:16 -0300406{
407 QObject *obj;
408
Markus Armbruster710aec92015-03-06 11:28:00 +0100409 obj = qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %s } }",
Eric Blakef22a28b2015-11-18 01:53:00 -0700410 QapiErrorClass_lookup[error_get_class(err)],
Markus Armbruster710aec92015-03-06 11:28:00 +0100411 error_get_pretty(err));
Luiz Capitulinode253f12012-07-27 16:18:16 -0300412
413 return qobject_to_qdict(obj);
414}
415
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100416static void monitor_protocol_emitter(Monitor *mon, QObject *data,
Markus Armbruster710aec92015-03-06 11:28:00 +0100417 Error *err)
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200418{
419 QDict *qmp;
420
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +0100421 trace_monitor_protocol_emitter(mon);
422
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100423 if (!err) {
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200424 /* success response */
Luiz Capitulinode253f12012-07-27 16:18:16 -0300425 qmp = qdict_new();
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200426 if (data) {
427 qobject_incref(data);
428 qdict_put_obj(qmp, "return", data);
429 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200430 /* return an empty QDict by default */
431 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200432 }
433 } else {
434 /* error response */
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100435 qmp = build_qmp_error_dict(err);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200436 }
437
Markus Armbruster74358f22015-03-06 19:35:59 +0100438 if (mon->qmp.id) {
439 qdict_put_obj(qmp, "id", mon->qmp.id);
440 mon->qmp.id = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200441 }
442
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200443 monitor_json_emitter(mon, QOBJECT(qmp));
444 QDECREF(qmp);
445}
446
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200447
Eric Blake7fb1cf12015-11-18 01:52:57 -0700448static MonitorQAPIEventConf monitor_qapi_event_conf[QAPI_EVENT__MAX] = {
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200449 /* Limit guest-triggerable events to 1 per second */
450 [QAPI_EVENT_RTC_CHANGE] = { 1000 * SCALE_MS },
451 [QAPI_EVENT_WATCHDOG] = { 1000 * SCALE_MS },
452 [QAPI_EVENT_BALLOON_CHANGE] = { 1000 * SCALE_MS },
453 [QAPI_EVENT_QUORUM_REPORT_BAD] = { 1000 * SCALE_MS },
454 [QAPI_EVENT_QUORUM_FAILURE] = { 1000 * SCALE_MS },
455 [QAPI_EVENT_VSERPORT_CHANGE] = { 1000 * SCALE_MS },
456};
457
Markus Armbrustera24712a2015-10-15 17:08:34 +0200458GHashTable *monitor_qapi_event_state;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100459
460/*
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200461 * Emits the event to every monitor instance, @event is only used for trace
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200462 * Called with monitor_lock held.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100463 */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200464static void monitor_qapi_event_emit(QAPIEvent event, QDict *qdict)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100465{
466 Monitor *mon;
467
Markus Armbruster688b4b72015-10-15 17:08:30 +0200468 trace_monitor_protocol_event_emit(event, qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100469 QLIST_FOREACH(mon, &mon_list, entry) {
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100470 if (monitor_is_qmp(mon) && mon->qmp.in_command_mode) {
Markus Armbruster688b4b72015-10-15 17:08:30 +0200471 monitor_json_emitter(mon, QOBJECT(qdict));
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100472 }
473 }
474}
475
Markus Armbrustera24712a2015-10-15 17:08:34 +0200476static void monitor_qapi_event_handler(void *opaque);
477
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100478/*
479 * Queue a new event for emission to Monitor instances,
480 * applying any rate limiting if required.
481 */
482static void
Markus Armbruster688b4b72015-10-15 17:08:30 +0200483monitor_qapi_event_queue(QAPIEvent event, QDict *qdict, Error **errp)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100484{
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200485 MonitorQAPIEventConf *evconf;
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200486 MonitorQAPIEventState *evstate;
Markus Armbruster93f8f982015-10-15 17:08:31 +0200487
Eric Blake7fb1cf12015-11-18 01:52:57 -0700488 assert(event < QAPI_EVENT__MAX);
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200489 evconf = &monitor_qapi_event_conf[event];
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200490 trace_monitor_protocol_event_queue(event, qdict, evconf->rate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100491
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200492 qemu_mutex_lock(&monitor_lock);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200493
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200494 if (!evconf->rate) {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200495 /* Unthrottled event */
Markus Armbruster688b4b72015-10-15 17:08:30 +0200496 monitor_qapi_event_emit(event, qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100497 } else {
Markus Armbruster7de0be62015-10-15 17:08:35 +0200498 QDict *data = qobject_to_qdict(qdict_get(qdict, "data"));
499 MonitorQAPIEventState key = { .event = event, .data = data };
Markus Armbrustera24712a2015-10-15 17:08:34 +0200500
501 evstate = g_hash_table_lookup(monitor_qapi_event_state, &key);
502 assert(!evstate || timer_pending(evstate->timer));
503
504 if (evstate) {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200505 /*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200506 * Timer is pending for (at least) evconf->rate ns after
Markus Armbruster93f8f982015-10-15 17:08:31 +0200507 * last send. Store event for sending when timer fires,
508 * replacing a prior stored event if any.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100509 */
Markus Armbruster93f8f982015-10-15 17:08:31 +0200510 QDECREF(evstate->qdict);
Markus Armbruster688b4b72015-10-15 17:08:30 +0200511 evstate->qdict = qdict;
512 QINCREF(evstate->qdict);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100513 } else {
Markus Armbruster93f8f982015-10-15 17:08:31 +0200514 /*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200515 * Last send was (at least) evconf->rate ns ago.
Markus Armbruster93f8f982015-10-15 17:08:31 +0200516 * Send immediately, and arm the timer to call
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200517 * monitor_qapi_event_handler() in evconf->rate ns. Any
Markus Armbruster93f8f982015-10-15 17:08:31 +0200518 * events arriving before then will be delayed until then.
519 */
Alberto Garciadc599972016-03-10 13:55:26 +0200520 int64_t now = qemu_clock_get_ns(event_clock_type);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200521
Markus Armbruster688b4b72015-10-15 17:08:30 +0200522 monitor_qapi_event_emit(event, qdict);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200523
524 evstate = g_new(MonitorQAPIEventState, 1);
525 evstate->event = event;
Markus Armbruster7de0be62015-10-15 17:08:35 +0200526 evstate->data = data;
527 QINCREF(evstate->data);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200528 evstate->qdict = NULL;
Alberto Garciadc599972016-03-10 13:55:26 +0200529 evstate->timer = timer_new_ns(event_clock_type,
Markus Armbrustera24712a2015-10-15 17:08:34 +0200530 monitor_qapi_event_handler,
531 evstate);
532 g_hash_table_add(monitor_qapi_event_state, evstate);
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200533 timer_mod_ns(evstate->timer, now + evconf->rate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100534 }
535 }
Markus Armbruster93f8f982015-10-15 17:08:31 +0200536
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200537 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100538}
539
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100540/*
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200541 * This function runs evconf->rate ns after sending a throttled
Markus Armbruster93f8f982015-10-15 17:08:31 +0200542 * event.
543 * If another event has since been stored, send it.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100544 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200545static void monitor_qapi_event_handler(void *opaque)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100546{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200547 MonitorQAPIEventState *evstate = opaque;
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200548 MonitorQAPIEventConf *evconf = &monitor_qapi_event_conf[evstate->event];
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100549
Markus Armbruster93f8f982015-10-15 17:08:31 +0200550 trace_monitor_protocol_event_handler(evstate->event, evstate->qdict);
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200551 qemu_mutex_lock(&monitor_lock);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200552
Markus Armbruster688b4b72015-10-15 17:08:30 +0200553 if (evstate->qdict) {
Alberto Garciadc599972016-03-10 13:55:26 +0200554 int64_t now = qemu_clock_get_ns(event_clock_type);
Markus Armbruster93f8f982015-10-15 17:08:31 +0200555
Markus Armbruster688b4b72015-10-15 17:08:30 +0200556 monitor_qapi_event_emit(evstate->event, evstate->qdict);
557 QDECREF(evstate->qdict);
558 evstate->qdict = NULL;
Markus Armbrusterb9b03ab2015-10-15 17:08:33 +0200559 timer_mod_ns(evstate->timer, now + evconf->rate);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200560 } else {
561 g_hash_table_remove(monitor_qapi_event_state, evstate);
Markus Armbruster7de0be62015-10-15 17:08:35 +0200562 QDECREF(evstate->data);
Markus Armbrustera24712a2015-10-15 17:08:34 +0200563 timer_free(evstate->timer);
564 g_free(evstate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100565 }
Markus Armbruster93f8f982015-10-15 17:08:31 +0200566
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200567 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100568}
569
Markus Armbrustera24712a2015-10-15 17:08:34 +0200570static unsigned int qapi_event_throttle_hash(const void *key)
571{
572 const MonitorQAPIEventState *evstate = key;
Markus Armbruster7de0be62015-10-15 17:08:35 +0200573 unsigned int hash = evstate->event * 255;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200574
Markus Armbruster7de0be62015-10-15 17:08:35 +0200575 if (evstate->event == QAPI_EVENT_VSERPORT_CHANGE) {
576 hash += g_str_hash(qdict_get_str(evstate->data, "id"));
577 }
578
Alberto Garcia6d425eb2016-03-10 13:55:25 +0200579 if (evstate->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
580 hash += g_str_hash(qdict_get_str(evstate->data, "node-name"));
581 }
582
Markus Armbruster7de0be62015-10-15 17:08:35 +0200583 return hash;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200584}
585
586static gboolean qapi_event_throttle_equal(const void *a, const void *b)
587{
588 const MonitorQAPIEventState *eva = a;
589 const MonitorQAPIEventState *evb = b;
590
Markus Armbruster7de0be62015-10-15 17:08:35 +0200591 if (eva->event != evb->event) {
592 return FALSE;
593 }
594
595 if (eva->event == QAPI_EVENT_VSERPORT_CHANGE) {
596 return !strcmp(qdict_get_str(eva->data, "id"),
597 qdict_get_str(evb->data, "id"));
598 }
599
Alberto Garcia6d425eb2016-03-10 13:55:25 +0200600 if (eva->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
601 return !strcmp(qdict_get_str(eva->data, "node-name"),
602 qdict_get_str(evb->data, "node-name"));
603 }
604
Markus Armbruster7de0be62015-10-15 17:08:35 +0200605 return TRUE;
Markus Armbrustera24712a2015-10-15 17:08:34 +0200606}
607
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200608static void monitor_qapi_event_init(void)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100609{
Alberto Garciadc599972016-03-10 13:55:26 +0200610 if (qtest_enabled()) {
611 event_clock_type = QEMU_CLOCK_VIRTUAL;
612 }
613
Markus Armbrustera24712a2015-10-15 17:08:34 +0200614 monitor_qapi_event_state = g_hash_table_new(qapi_event_throttle_hash,
615 qapi_event_throttle_equal);
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200616 qmp_event_set_func_emit(monitor_qapi_event_queue);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200617}
618
Markus Armbruster485febc2015-03-13 17:25:50 +0100619static void qmp_capabilities(QDict *params, QObject **ret_data, Error **errp)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200620{
Markus Armbruster485febc2015-03-13 17:25:50 +0100621 cur_mon->qmp.in_command_mode = true;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200622}
623
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100624static void handle_hmp_command(Monitor *mon, const char *cmdline);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200625
Wenchao Xiab01fe892013-08-27 20:38:19 +0800626static void monitor_data_init(Monitor *mon)
627{
628 memset(mon, 0, sizeof(Monitor));
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200629 qemu_mutex_init(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800630 mon->outbuf = qstring_new();
Wenchao Xia77172392013-08-27 20:38:20 +0800631 /* Use *mon_cmds by default. */
632 mon->cmd_table = mon_cmds;
Wenchao Xiab01fe892013-08-27 20:38:19 +0800633}
634
635static void monitor_data_destroy(Monitor *mon)
636{
637 QDECREF(mon->outbuf);
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200638 qemu_mutex_destroy(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800639}
640
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200641char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
642 int64_t cpu_index, Error **errp)
Luiz Capitulino0268d972010-10-22 10:08:02 -0200643{
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200644 char *output = NULL;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200645 Monitor *old_mon, hmp;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200646
Wenchao Xiab01fe892013-08-27 20:38:19 +0800647 monitor_data_init(&hmp);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400648 hmp.skip_flush = true;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200649
650 old_mon = cur_mon;
651 cur_mon = &hmp;
652
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200653 if (has_cpu_index) {
654 int ret = monitor_set_cpu(cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200655 if (ret < 0) {
656 cur_mon = old_mon;
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100657 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
658 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200659 goto out;
660 }
661 }
662
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100663 handle_hmp_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200664 cur_mon = old_mon;
665
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200666 qemu_mutex_lock(&hmp.out_lock);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400667 if (qstring_get_length(hmp.outbuf) > 0) {
668 output = g_strdup(qstring_get_str(hmp.outbuf));
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200669 } else {
670 output = g_strdup("");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200671 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200672 qemu_mutex_unlock(&hmp.out_lock);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200673
674out:
Wenchao Xiab01fe892013-08-27 20:38:19 +0800675 monitor_data_destroy(&hmp);
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200676 return output;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200677}
678
bellard9dc39cb2004-03-14 21:38:27 +0000679static int compare_cmd(const char *name, const char *list)
680{
681 const char *p, *pstart;
682 int len;
683 len = strlen(name);
684 p = list;
685 for(;;) {
686 pstart = p;
687 p = strchr(p, '|');
688 if (!p)
689 p = pstart + strlen(pstart);
690 if ((p - pstart) == len && !memcmp(pstart, name, len))
691 return 1;
692 if (*p == '\0')
693 break;
694 p++;
695 }
696 return 0;
697}
698
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800699static int get_str(char *buf, int buf_size, const char **pp)
700{
701 const char *p;
702 char *q;
703 int c;
704
705 q = buf;
706 p = *pp;
707 while (qemu_isspace(*p)) {
708 p++;
709 }
710 if (*p == '\0') {
711 fail:
712 *q = '\0';
713 *pp = p;
714 return -1;
715 }
716 if (*p == '\"') {
717 p++;
718 while (*p != '\0' && *p != '\"') {
719 if (*p == '\\') {
720 p++;
721 c = *p++;
722 switch (c) {
723 case 'n':
724 c = '\n';
725 break;
726 case 'r':
727 c = '\r';
728 break;
729 case '\\':
730 case '\'':
731 case '\"':
732 break;
733 default:
Peter Maydell71baf782015-08-19 16:20:19 +0100734 printf("unsupported escape code: '\\%c'\n", c);
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800735 goto fail;
736 }
737 if ((q - buf) < buf_size - 1) {
738 *q++ = c;
739 }
740 } else {
741 if ((q - buf) < buf_size - 1) {
742 *q++ = *p;
743 }
744 p++;
745 }
746 }
747 if (*p != '\"') {
Peter Maydell71baf782015-08-19 16:20:19 +0100748 printf("unterminated string\n");
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800749 goto fail;
750 }
751 p++;
752 } else {
753 while (*p != '\0' && !qemu_isspace(*p)) {
754 if ((q - buf) < buf_size - 1) {
755 *q++ = *p;
756 }
757 p++;
758 }
759 }
760 *q = '\0';
761 *pp = p;
762 return 0;
763}
764
765#define MAX_ARGS 16
766
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800767static void free_cmdline_args(char **args, int nb_args)
768{
769 int i;
770
771 assert(nb_args <= MAX_ARGS);
772
773 for (i = 0; i < nb_args; i++) {
774 g_free(args[i]);
775 }
776
777}
778
779/*
780 * Parse the command line to get valid args.
781 * @cmdline: command line to be parsed.
782 * @pnb_args: location to store the number of args, must NOT be NULL.
783 * @args: location to store the args, which should be freed by caller, must
784 * NOT be NULL.
785 *
786 * Returns 0 on success, negative on failure.
787 *
788 * NOTE: this parser is an approximate form of the real command parser. Number
789 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
790 * return with failure.
791 */
792static int parse_cmdline(const char *cmdline,
793 int *pnb_args, char **args)
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800794{
795 const char *p;
796 int nb_args, ret;
797 char buf[1024];
798
799 p = cmdline;
800 nb_args = 0;
801 for (;;) {
802 while (qemu_isspace(*p)) {
803 p++;
804 }
805 if (*p == '\0') {
806 break;
807 }
808 if (nb_args >= MAX_ARGS) {
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800809 goto fail;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800810 }
811 ret = get_str(buf, sizeof(buf), &p);
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800812 if (ret < 0) {
813 goto fail;
814 }
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800815 args[nb_args] = g_strdup(buf);
816 nb_args++;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800817 }
818 *pnb_args = nb_args;
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800819 return 0;
820
821 fail:
822 free_cmdline_args(args, nb_args);
823 return -1;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800824}
825
Wenchao Xia66855492013-08-27 20:38:23 +0800826static void help_cmd_dump_one(Monitor *mon,
827 const mon_cmd_t *cmd,
828 char **prefix_args,
829 int prefix_args_nb)
830{
831 int i;
832
833 for (i = 0; i < prefix_args_nb; i++) {
834 monitor_printf(mon, "%s ", prefix_args[i]);
835 }
836 monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
837}
838
839/* @args[@arg_index] is the valid command need to find in @cmds */
Anthony Liguoric227f092009-10-01 16:12:16 -0500840static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
Wenchao Xia66855492013-08-27 20:38:23 +0800841 char **args, int nb_args, int arg_index)
bellard9dc39cb2004-03-14 21:38:27 +0000842{
Anthony Liguoric227f092009-10-01 16:12:16 -0500843 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000844
Wenchao Xia66855492013-08-27 20:38:23 +0800845 /* No valid arg need to compare with, dump all in *cmds */
846 if (arg_index >= nb_args) {
847 for (cmd = cmds; cmd->name != NULL; cmd++) {
848 help_cmd_dump_one(mon, cmd, args, arg_index);
849 }
850 return;
851 }
852
853 /* Find one entry to dump */
854 for (cmd = cmds; cmd->name != NULL; cmd++) {
855 if (compare_cmd(args[arg_index], cmd->name)) {
856 if (cmd->sub_table) {
857 /* continue with next arg */
858 help_cmd_dump(mon, cmd->sub_table,
859 args, nb_args, arg_index + 1);
860 } else {
861 help_cmd_dump_one(mon, cmd, args, arg_index);
862 }
863 break;
864 }
bellard9dc39cb2004-03-14 21:38:27 +0000865 }
866}
867
aliguori376253e2009-03-05 23:01:23 +0000868static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000869{
Wenchao Xia66855492013-08-27 20:38:23 +0800870 char *args[MAX_ARGS];
871 int nb_args = 0;
872
873 /* 1. parse user input */
874 if (name) {
875 /* special case for log, directly dump and return */
876 if (!strcmp(name, "log")) {
Peter Maydell38dad9e2013-02-11 16:41:25 +0000877 const QEMULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000878 monitor_printf(mon, "Log items (comma separated):\n");
879 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
Peter Maydell38dad9e2013-02-11 16:41:25 +0000880 for (item = qemu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000881 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000882 }
Wenchao Xia66855492013-08-27 20:38:23 +0800883 return;
884 }
885
886 if (parse_cmdline(name, &nb_args, args) < 0) {
887 return;
bellardf193c792004-03-21 17:06:25 +0000888 }
bellard9dc39cb2004-03-14 21:38:27 +0000889 }
Wenchao Xia66855492013-08-27 20:38:23 +0800890
891 /* 2. dump the contents according to parsed args */
892 help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0);
893
894 free_cmdline_args(args, nb_args);
bellard9dc39cb2004-03-14 21:38:27 +0000895}
896
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300897static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300898{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300899 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300900}
901
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100902static void hmp_trace_event(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530903{
904 const char *tp_name = qdict_get_str(qdict, "name");
905 bool new_state = qdict_get_bool(qdict, "option");
Lluís Vilanova14101d02014-08-25 13:20:03 +0200906 Error *local_err = NULL;
Blue Swirlf871d682010-10-13 19:14:29 +0000907
Lluís Vilanova14101d02014-08-25 13:20:03 +0200908 qmp_trace_event_set_state(tp_name, new_state, true, true, &local_err);
909 if (local_err) {
Markus Armbruster091e38b2015-02-10 15:15:43 +0100910 error_report_err(local_err);
Blue Swirlf871d682010-10-13 19:14:29 +0000911 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530912}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100913
Michael Rothc45a8162011-10-02 08:44:37 -0500914#ifdef CONFIG_TRACE_SIMPLE
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100915static void hmp_trace_file(Monitor *mon, const QDict *qdict)
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100916{
917 const char *op = qdict_get_try_str(qdict, "op");
918 const char *arg = qdict_get_try_str(qdict, "arg");
919
920 if (!op) {
921 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
922 } else if (!strcmp(op, "on")) {
923 st_set_trace_file_enabled(true);
924 } else if (!strcmp(op, "off")) {
925 st_set_trace_file_enabled(false);
926 } else if (!strcmp(op, "flush")) {
927 st_flush_trace_buffer();
928 } else if (!strcmp(op, "set")) {
929 if (arg) {
930 st_set_trace_file(arg);
931 }
932 } else {
933 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
934 help_cmd(mon, "trace-file");
935 }
936}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530937#endif
938
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100939static void hmp_info_help(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000940{
Luiz Capitulino13c74252009-10-07 13:41:55 -0300941 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000942}
943
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300944CommandInfoList *qmp_query_commands(Error **errp)
bellard9bc9d1c2004-10-10 15:15:51 +0000945{
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300946 CommandInfoList *info, *cmd_list = NULL;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200947 const mon_cmd_t *cmd;
948
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300949 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino40e5a012011-10-21 16:15:31 -0200950 info = g_malloc0(sizeof(*info));
951 info->value = g_malloc0(sizeof(*info->value));
952 info->value->name = g_strdup(cmd->name);
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200953
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300954 info->next = cmd_list;
955 cmd_list = info;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200956 }
957
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300958 return cmd_list;
thsa36e69d2007-12-02 05:18:19 +0000959}
960
Daniel P. Berrange48608532012-05-21 17:59:51 +0100961EventInfoList *qmp_query_events(Error **errp)
962{
963 EventInfoList *info, *ev_list = NULL;
Wenchao Xia75175172014-06-18 08:43:54 +0200964 QAPIEvent e;
Daniel P. Berrange48608532012-05-21 17:59:51 +0100965
Eric Blake7fb1cf12015-11-18 01:52:57 -0700966 for (e = 0 ; e < QAPI_EVENT__MAX ; e++) {
Wenchao Xia75175172014-06-18 08:43:54 +0200967 const char *event_name = QAPIEvent_lookup[e];
Daniel P. Berrange48608532012-05-21 17:59:51 +0100968 assert(event_name != NULL);
969 info = g_malloc0(sizeof(*info));
970 info->value = g_malloc0(sizeof(*info->value));
971 info->value->name = g_strdup(event_name);
972
973 info->next = ev_list;
974 ev_list = info;
975 }
976
977 return ev_list;
978}
979
Markus Armbruster39a18152015-09-16 13:06:28 +0200980/*
981 * Minor hack: generated marshalling suppressed for this command
982 * ('gen': false in the schema) so we can parse the JSON string
983 * directly into QObject instead of first parsing it with
984 * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
985 * to QObject with generated output marshallers, every time. Instead,
986 * we do it in test-qmp-input-visitor.c, just to make sure
987 * qapi-introspect.py's output actually conforms to the schema.
988 */
989static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
990 Error **errp)
991{
992 *ret_data = qobject_from_json(qmp_schema_json);
993}
994
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300995/* set the current CPU defined by the user */
996int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000997{
Andreas Färber55e5c282012-12-17 06:18:02 +0100998 CPUState *cpu;
bellard6a00d602005-11-21 23:25:50 +0000999
Andreas Färber1c8bb3c2013-02-15 17:01:09 +01001000 cpu = qemu_get_cpu(cpu_index);
1001 if (cpu == NULL) {
1002 return -1;
bellard6a00d602005-11-21 23:25:50 +00001003 }
Andreas Färbercb446ec2013-05-01 14:24:52 +02001004 cur_mon->mon_cpu = cpu;
Andreas Färber1c8bb3c2013-02-15 17:01:09 +01001005 return 0;
bellard6a00d602005-11-21 23:25:50 +00001006}
1007
Pavel Butsykincaf15312015-09-22 16:18:17 +03001008CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +00001009{
aliguori731b0362009-03-05 23:01:42 +00001010 if (!cur_mon->mon_cpu) {
Luiz Capitulinob025c8b2011-10-06 14:02:57 -03001011 monitor_set_cpu(0);
bellard6a00d602005-11-21 23:25:50 +00001012 }
Avi Kivity4c0960c2009-08-17 23:19:53 +03001013 cpu_synchronize_state(cur_mon->mon_cpu);
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001014 return cur_mon->mon_cpu;
1015}
1016
Pavel Butsykinbf957282015-09-10 18:38:59 +03001017CPUArchState *mon_get_cpu_env(void)
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001018{
1019 return mon_get_cpu()->env_ptr;
bellard6a00d602005-11-21 23:25:50 +00001020}
1021
Luiz Capitulino99b77962011-10-24 10:53:44 -02001022int monitor_get_cpu_index(void)
1023{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001024 return mon_get_cpu()->cpu_index;
Luiz Capitulino99b77962011-10-24 10:53:44 -02001025}
1026
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001027static void hmp_info_registers(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001028{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001029 cpu_dump_state(mon_get_cpu(), (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +00001030}
1031
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001032static void hmp_info_jit(Monitor *mon, const QDict *qdict)
bellarde3db7222005-01-26 22:00:47 +00001033{
aliguori376253e2009-03-05 23:01:23 +00001034 dump_exec_info((FILE *)mon, monitor_fprintf);
Sebastian Tanase27498be2014-07-25 11:56:33 +02001035 dump_drift_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +00001036}
1037
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001038static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
Max Filippov246ae242014-11-02 11:04:18 +03001039{
1040 dump_opcount_info((FILE *)mon, monitor_fprintf);
1041}
1042
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001043static void hmp_info_history(Monitor *mon, const QDict *qdict)
bellardaa455482004-04-04 13:07:25 +00001044{
1045 int i;
bellard7e2515e2004-08-01 21:52:19 +00001046 const char *str;
ths3b46e622007-09-17 08:09:54 +00001047
aliguoricde76ee2009-03-05 23:01:51 +00001048 if (!mon->rs)
1049 return;
bellard7e2515e2004-08-01 21:52:19 +00001050 i = 0;
1051 for(;;) {
aliguori731b0362009-03-05 23:01:42 +00001052 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +00001053 if (!str)
1054 break;
aliguori376253e2009-03-05 23:01:23 +00001055 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +00001056 i++;
bellardaa455482004-04-04 13:07:25 +00001057 }
1058}
1059
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001060static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
j_mayer76a66252007-03-07 08:32:30 +00001061{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001062 cpu_dump_statistics(mon_get_cpu(), (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +00001063}
j_mayer76a66252007-03-07 08:32:30 +00001064
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001065static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301066{
Lluís Vilanova14101d02014-08-25 13:20:03 +02001067 TraceEventInfoList *events = qmp_trace_event_get_state("*", NULL);
1068 TraceEventInfoList *elem;
1069
1070 for (elem = events; elem != NULL; elem = elem->next) {
1071 monitor_printf(mon, "%s : state %u\n",
1072 elem->value->name,
1073 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
1074 }
1075 qapi_free_TraceEventInfoList(events);
Prerna Saxena22890ab2010-06-24 17:04:53 +05301076}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301077
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001078void qmp_client_migrate_info(const char *protocol, const char *hostname,
1079 bool has_port, int64_t port,
1080 bool has_tls_port, int64_t tls_port,
1081 bool has_cert_subject, const char *cert_subject,
1082 Error **errp)
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001083{
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001084 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001085 if (!qemu_using_spice(errp)) {
1086 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001087 }
1088
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001089 if (!has_port && !has_tls_port) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001090 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001091 return;
Yonit Halperin6ec5dae2012-03-18 09:42:39 +02001092 }
1093
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001094 if (qemu_spice_migrate_info(hostname,
1095 has_port ? port : -1,
1096 has_tls_port ? tls_port : -1,
1097 cert_subject)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001098 error_setg(errp, QERR_UNDEFINED_ERROR);
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001099 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001100 }
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001101 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001102 }
1103
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001104 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001105}
1106
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001107static void hmp_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001108{
Peter Maydell9a7e5422013-02-11 16:41:20 +00001109 qemu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001110}
1111
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001112static void hmp_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001113{
1114 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001115 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001116
bellard9307c4c2004-04-04 12:57:25 +00001117 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001118 mask = 0;
1119 } else {
Peter Maydell4fde1eb2013-02-11 16:41:22 +00001120 mask = qemu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001121 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001122 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001123 return;
1124 }
1125 }
Peter Maydell24537a02013-02-11 16:41:23 +00001126 qemu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00001127}
1128
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001129static void hmp_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001130{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001131 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001132 if (!option || !strcmp(option, "on")) {
1133 singlestep = 1;
1134 } else if (!strcmp(option, "off")) {
1135 singlestep = 0;
1136 } else {
1137 monitor_printf(mon, "unexpected option %s\n", option);
1138 }
1139}
1140
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001141static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001142{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001143 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001144 if (!device)
1145 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1146 if (gdbserver_start(device) < 0) {
1147 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1148 device);
1149 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001150 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001151 } else {
aliguori59030a82009-04-05 18:43:41 +00001152 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1153 device);
bellard8a7ddc32004-03-31 19:00:16 +00001154 }
1155}
1156
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001157static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001158{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001159 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001160 if (select_watchdog_action(action) == -1) {
1161 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1162 }
1163}
1164
aliguori376253e2009-03-05 23:01:23 +00001165static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001166{
aliguori376253e2009-03-05 23:01:23 +00001167 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001168 switch(c) {
1169 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001170 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001171 break;
1172 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001173 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001174 break;
1175 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001176 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001177 break;
1178 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001179 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001180 break;
1181 default:
1182 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001183 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001184 } else {
aliguori376253e2009-03-05 23:01:23 +00001185 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001186 }
1187 break;
1188 }
aliguori376253e2009-03-05 23:01:23 +00001189 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001190}
1191
aliguori376253e2009-03-05 23:01:23 +00001192static void memory_dump(Monitor *mon, int count, int format, int wsize,
Avi Kivitya8170e52012-10-23 12:30:10 +02001193 hwaddr addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001194{
Blue Swirl23842aa2010-01-12 20:27:43 +00001195 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001196 uint8_t buf[16];
1197 uint64_t v;
1198
1199 if (format == 'i') {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001200 int flags = 0;
bellard9307c4c2004-04-04 12:57:25 +00001201#ifdef TARGET_I386
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001202 CPUArchState *env = mon_get_cpu_env();
bellard4c27ba22004-04-25 18:05:08 +00001203 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001204 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001205 } else if (wsize == 4) {
1206 flags = 0;
1207 } else {
bellard6a15fd12006-04-12 21:07:07 +00001208 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001209 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001210 if (env) {
1211#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001212 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001213 (env->segs[R_CS].flags & DESC_L_MASK))
1214 flags = 2;
1215 else
1216#endif
1217 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1218 flags = 1;
1219 }
bellard4c27ba22004-04-25 18:05:08 +00001220 }
1221#endif
Tom Musta1c38f842014-04-09 14:53:24 -05001222#ifdef TARGET_PPC
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001223 CPUArchState *env = mon_get_cpu_env();
Tom Musta1c38f842014-04-09 14:53:24 -05001224 flags = msr_le << 16;
1225 flags |= env->bfd_mach;
1226#endif
Peter Crosthwaited49190c2015-05-24 14:20:41 -07001227 monitor_disas(mon, mon_get_cpu(), addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001228 return;
1229 }
1230
1231 len = wsize * count;
1232 if (wsize == 1)
1233 line_size = 8;
1234 else
1235 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001236 max_digits = 0;
1237
1238 switch(format) {
1239 case 'o':
1240 max_digits = (wsize * 8 + 2) / 3;
1241 break;
1242 default:
1243 case 'x':
1244 max_digits = (wsize * 8) / 4;
1245 break;
1246 case 'u':
1247 case 'd':
1248 max_digits = (wsize * 8 * 10 + 32) / 33;
1249 break;
1250 case 'c':
1251 wsize = 1;
1252 break;
1253 }
1254
1255 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001256 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001257 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001258 else
aliguori376253e2009-03-05 23:01:23 +00001259 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001260 l = len;
1261 if (l > line_size)
1262 l = line_size;
1263 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001264 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001265 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001266 if (cpu_memory_rw_debug(mon_get_cpu(), addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001267 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001268 break;
1269 }
bellard9307c4c2004-04-04 12:57:25 +00001270 }
ths5fafdf22007-09-16 21:08:06 +00001271 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001272 while (i < l) {
1273 switch(wsize) {
1274 default:
1275 case 1:
Peter Maydell24e60302015-01-20 15:19:32 +00001276 v = ldub_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001277 break;
1278 case 2:
Peter Maydell24e60302015-01-20 15:19:32 +00001279 v = lduw_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001280 break;
1281 case 4:
Peter Maydell24e60302015-01-20 15:19:32 +00001282 v = (uint32_t)ldl_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001283 break;
1284 case 8:
Peter Maydell24e60302015-01-20 15:19:32 +00001285 v = ldq_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001286 break;
1287 }
aliguori376253e2009-03-05 23:01:23 +00001288 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001289 switch(format) {
1290 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001291 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001292 break;
1293 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001294 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001295 break;
1296 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001297 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001298 break;
1299 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001300 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001301 break;
1302 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001303 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001304 break;
1305 }
1306 i += wsize;
1307 }
aliguori376253e2009-03-05 23:01:23 +00001308 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001309 addr += l;
1310 len -= l;
1311 }
1312}
1313
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001314static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001315{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001316 int count = qdict_get_int(qdict, "count");
1317 int format = qdict_get_int(qdict, "format");
1318 int size = qdict_get_int(qdict, "size");
1319 target_long addr = qdict_get_int(qdict, "addr");
1320
aliguori376253e2009-03-05 23:01:23 +00001321 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001322}
1323
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001324static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001325{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001326 int count = qdict_get_int(qdict, "count");
1327 int format = qdict_get_int(qdict, "format");
1328 int size = qdict_get_int(qdict, "size");
Avi Kivitya8170e52012-10-23 12:30:10 +02001329 hwaddr addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001330
aliguori376253e2009-03-05 23:01:23 +00001331 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001332}
1333
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001334static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001335{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001336 int format = qdict_get_int(qdict, "format");
Avi Kivitya8170e52012-10-23 12:30:10 +02001337 hwaddr val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001338
bellard9307c4c2004-04-04 12:57:25 +00001339 switch(format) {
1340 case 'o':
Avi Kivitya8170e52012-10-23 12:30:10 +02001341 monitor_printf(mon, "%#" HWADDR_PRIo, val);
bellard9307c4c2004-04-04 12:57:25 +00001342 break;
1343 case 'x':
Avi Kivitya8170e52012-10-23 12:30:10 +02001344 monitor_printf(mon, "%#" HWADDR_PRIx, val);
bellard9307c4c2004-04-04 12:57:25 +00001345 break;
1346 case 'u':
Avi Kivitya8170e52012-10-23 12:30:10 +02001347 monitor_printf(mon, "%" HWADDR_PRIu, val);
bellard9307c4c2004-04-04 12:57:25 +00001348 break;
1349 default:
1350 case 'd':
Avi Kivitya8170e52012-10-23 12:30:10 +02001351 monitor_printf(mon, "%" HWADDR_PRId, val);
bellard9307c4c2004-04-04 12:57:25 +00001352 break;
1353 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001354 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001355 break;
1356 }
aliguori376253e2009-03-05 23:01:23 +00001357 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001358}
1359
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001360static void hmp_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001361{
1362 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001363 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001364 uint32_t start = qdict_get_int(qdict, "start");
1365 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001366
1367 sum = 0;
1368 for(addr = start; addr < (start + size); addr++) {
Peter Maydell42874d32015-04-26 16:49:24 +01001369 uint8_t val = address_space_ldub(&address_space_memory, addr,
1370 MEMTXATTRS_UNSPECIFIED, NULL);
bellarde4cf1ad2005-06-04 20:15:57 +00001371 /* BSD sum algorithm ('sum' Unix command) */
1372 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001373 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001374 }
aliguori376253e2009-03-05 23:01:23 +00001375 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001376}
1377
bellard13224a82006-07-14 22:03:35 +00001378static int mouse_button_state;
1379
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001380static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001381{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001382 int dx, dy, dz, button;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001383 const char *dx_str = qdict_get_str(qdict, "dx_str");
1384 const char *dy_str = qdict_get_str(qdict, "dy_str");
1385 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001386
bellard13224a82006-07-14 22:03:35 +00001387 dx = strtol(dx_str, NULL, 0);
1388 dy = strtol(dy_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001389 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
1390 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
1391
1392 if (dz_str) {
bellard13224a82006-07-14 22:03:35 +00001393 dz = strtol(dz_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001394 if (dz != 0) {
Gerd Hoffmannf22d0af2016-01-12 12:14:12 +01001395 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001396 qemu_input_queue_btn(NULL, button, true);
1397 qemu_input_event_sync();
1398 qemu_input_queue_btn(NULL, button, false);
1399 }
1400 }
1401 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001402}
1403
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001404static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001405{
Eric Blake7fb1cf12015-11-18 01:52:57 -07001406 static uint32_t bmap[INPUT_BUTTON__MAX] = {
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001407 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1408 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
1409 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1410 };
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001411 int button_state = qdict_get_int(qdict, "button_state");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001412
1413 if (mouse_button_state == button_state) {
1414 return;
1415 }
1416 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1417 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001418 mouse_button_state = button_state;
bellard13224a82006-07-14 22:03:35 +00001419}
1420
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001421static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001422{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001423 int size = qdict_get_int(qdict, "size");
1424 int addr = qdict_get_int(qdict, "addr");
1425 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001426 uint32_t val;
1427 int suffix;
1428
1429 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001430 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001431 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001432 addr++;
1433 }
1434 addr &= 0xffff;
1435
1436 switch(size) {
1437 default:
1438 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001439 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001440 suffix = 'b';
1441 break;
1442 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001443 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001444 suffix = 'w';
1445 break;
1446 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001447 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001448 suffix = 'l';
1449 break;
1450 }
aliguori376253e2009-03-05 23:01:23 +00001451 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1452 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001453}
bellarda3a91a32004-06-04 11:06:21 +00001454
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001455static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001456{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001457 int size = qdict_get_int(qdict, "size");
1458 int addr = qdict_get_int(qdict, "addr");
1459 int val = qdict_get_int(qdict, "val");
1460
Jan Kiszkaf1147842009-07-14 10:20:11 +02001461 addr &= IOPORTS_MASK;
1462
1463 switch (size) {
1464 default:
1465 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001466 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001467 break;
1468 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001469 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001470 break;
1471 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001472 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001473 break;
1474 }
1475}
1476
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001477static void hmp_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001478{
Gongleif1839932014-12-03 18:20:58 +00001479 Error *local_err = NULL;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001480 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001481
Gongleif1839932014-12-03 18:20:58 +00001482 qemu_boot_set(bootdevice, &local_err);
1483 if (local_err) {
Markus Armbruster193227f2015-12-18 16:35:06 +01001484 error_report_err(local_err);
aurel320ecdffb2008-05-04 20:11:34 +00001485 } else {
Gongleif1839932014-12-03 18:20:58 +00001486 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001487 }
1488}
1489
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001490static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
Blue Swirl314e2982011-09-11 20:22:05 +00001491{
1492 mtree_info((fprintf_function)monitor_printf, mon);
1493}
1494
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001495static void hmp_info_numa(Monitor *mon, const QDict *qdict)
aliguori030ea372009-04-21 22:30:47 +00001496{
aliguorib28b6232009-04-22 20:20:29 +00001497 int i;
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001498 CPUState *cpu;
zhanghailiang5b009e42014-11-04 19:49:30 +08001499 uint64_t *node_mem;
aliguori030ea372009-04-21 22:30:47 +00001500
zhanghailiang5b009e42014-11-04 19:49:30 +08001501 node_mem = g_new0(uint64_t, nb_numa_nodes);
1502 query_numa_node_mem(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001503 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1504 for (i = 0; i < nb_numa_nodes; i++) {
1505 monitor_printf(mon, "node %d cpus:", i);
Andreas Färberbdc44642013-06-24 23:50:24 +02001506 CPU_FOREACH(cpu) {
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001507 if (cpu->numa_node == i) {
Andreas Färber55e5c282012-12-17 06:18:02 +01001508 monitor_printf(mon, " %d", cpu->cpu_index);
aliguori030ea372009-04-21 22:30:47 +00001509 }
1510 }
1511 monitor_printf(mon, "\n");
1512 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
zhanghailiang5b009e42014-11-04 19:49:30 +08001513 node_mem[i] >> 20);
aliguori030ea372009-04-21 22:30:47 +00001514 }
zhanghailiang5b009e42014-11-04 19:49:30 +08001515 g_free(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001516}
1517
bellard5f1ce942006-02-08 22:40:15 +00001518#ifdef CONFIG_PROFILER
1519
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001520int64_t tcg_time;
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001521int64_t dev_time;
1522
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001523static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001524{
aliguori376253e2009-03-05 23:01:23 +00001525 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001526 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00001527 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001528 tcg_time, tcg_time / (double)get_ticks_per_sec());
1529 tcg_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001530 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001531}
1532#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001533static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001534{
aliguori376253e2009-03-05 23:01:23 +00001535 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001536}
1537#endif
1538
bellardec36b692006-07-16 18:57:03 +00001539/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001540static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001541
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001542static void hmp_info_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001543{
1544 int i;
1545 CaptureState *s;
1546
1547 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001548 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001549 s->ops.info (s->opaque);
1550 }
1551}
1552
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001553static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001554{
1555 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001556 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001557 CaptureState *s;
1558
1559 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1560 if (i == n) {
1561 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001562 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001563 g_free (s);
bellardec36b692006-07-16 18:57:03 +00001564 return;
1565 }
1566 }
1567}
1568
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001569static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001570{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001571 const char *path = qdict_get_str(qdict, "path");
1572 int has_freq = qdict_haskey(qdict, "freq");
1573 int freq = qdict_get_try_int(qdict, "freq", -1);
1574 int has_bits = qdict_haskey(qdict, "bits");
1575 int bits = qdict_get_try_int(qdict, "bits", -1);
1576 int has_channels = qdict_haskey(qdict, "nchannels");
1577 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001578 CaptureState *s;
1579
Anthony Liguori7267c092011-08-20 22:09:37 -05001580 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001581
1582 freq = has_freq ? freq : 44100;
1583 bits = has_bits ? bits : 16;
1584 nchannels = has_channels ? nchannels : 2;
1585
1586 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001587 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05001588 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001589 return;
bellardec36b692006-07-16 18:57:03 +00001590 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001591 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001592}
bellardec36b692006-07-16 18:57:03 +00001593
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001594static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001595{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001596 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001597
aliguori76655d62009-03-06 20:27:37 +00001598 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001599 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001600 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001601 return acl;
1602}
aliguori76655d62009-03-06 20:27:37 +00001603
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001604static void hmp_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001605{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001606 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001607 qemu_acl *acl = find_acl(mon, aclname);
1608 qemu_acl_entry *entry;
1609 int i = 0;
1610
1611 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001612 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001613 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00001614 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00001615 i++;
1616 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001617 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001618 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001619 }
1620}
1621
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001622static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001623{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001624 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001625 qemu_acl *acl = find_acl(mon, aclname);
1626
1627 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001628 qemu_acl_reset(acl);
1629 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001630 }
1631}
aliguori76655d62009-03-06 20:27:37 +00001632
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001633static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001634{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001635 const char *aclname = qdict_get_str(qdict, "aclname");
1636 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001637 qemu_acl *acl = find_acl(mon, aclname);
1638
1639 if (acl) {
1640 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001641 acl->defaultDeny = 0;
1642 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001643 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001644 acl->defaultDeny = 1;
1645 monitor_printf(mon, "acl: policy set to 'deny'\n");
1646 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001647 monitor_printf(mon, "acl: unknown policy '%s', "
1648 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001649 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001650 }
1651}
aliguori76655d62009-03-06 20:27:37 +00001652
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001653static void hmp_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001654{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001655 const char *aclname = qdict_get_str(qdict, "aclname");
1656 const char *match = qdict_get_str(qdict, "match");
1657 const char *policy = qdict_get_str(qdict, "policy");
1658 int has_index = qdict_haskey(qdict, "index");
1659 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001660 qemu_acl *acl = find_acl(mon, aclname);
1661 int deny, ret;
1662
1663 if (acl) {
1664 if (strcmp(policy, "allow") == 0) {
1665 deny = 0;
1666 } else if (strcmp(policy, "deny") == 0) {
1667 deny = 1;
1668 } else {
1669 monitor_printf(mon, "acl: unknown policy '%s', "
1670 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001671 return;
1672 }
aliguori28a76be2009-03-06 20:27:40 +00001673 if (has_index)
1674 ret = qemu_acl_insert(acl, deny, match, index);
1675 else
1676 ret = qemu_acl_append(acl, deny, match);
1677 if (ret < 0)
1678 monitor_printf(mon, "acl: unable to add acl entry\n");
1679 else
1680 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001681 }
1682}
aliguori76655d62009-03-06 20:27:37 +00001683
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001684static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001685{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001686 const char *aclname = qdict_get_str(qdict, "aclname");
1687 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001688 qemu_acl *acl = find_acl(mon, aclname);
1689 int ret;
aliguori76655d62009-03-06 20:27:37 +00001690
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001691 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001692 ret = qemu_acl_remove(acl, match);
1693 if (ret < 0)
1694 monitor_printf(mon, "acl: no matching acl entry\n");
1695 else
1696 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001697 }
1698}
1699
Corey Bryant208c9d12012-06-22 14:36:09 -04001700void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001701{
Anthony Liguoric227f092009-10-01 16:12:16 -05001702 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001703 int fd;
1704
Corey Bryant208c9d12012-06-22 14:36:09 -04001705 fd = qemu_chr_fe_get_msgfd(cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001706 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001707 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryant208c9d12012-06-22 14:36:09 -04001708 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001709 }
1710
1711 if (qemu_isdigit(fdname[0])) {
Stefan Hajnoczi0b9f0e22014-04-24 13:58:18 +02001712 close(fd);
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001713 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
1714 "a name not starting with a digit");
Corey Bryant208c9d12012-06-22 14:36:09 -04001715 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001716 }
1717
Corey Bryant208c9d12012-06-22 14:36:09 -04001718 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001719 if (strcmp(monfd->name, fdname) != 0) {
1720 continue;
1721 }
1722
1723 close(monfd->fd);
1724 monfd->fd = fd;
Corey Bryant208c9d12012-06-22 14:36:09 -04001725 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001726 }
1727
Anthony Liguori7267c092011-08-20 22:09:37 -05001728 monfd = g_malloc0(sizeof(mon_fd_t));
1729 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001730 monfd->fd = fd;
1731
Corey Bryant208c9d12012-06-22 14:36:09 -04001732 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001733}
1734
Corey Bryant208c9d12012-06-22 14:36:09 -04001735void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001736{
Anthony Liguoric227f092009-10-01 16:12:16 -05001737 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001738
Corey Bryant208c9d12012-06-22 14:36:09 -04001739 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001740 if (strcmp(monfd->name, fdname) != 0) {
1741 continue;
1742 }
1743
Blue Swirl72cf2d42009-09-12 07:36:22 +00001744 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001745 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001746 g_free(monfd->name);
1747 g_free(monfd);
Corey Bryant208c9d12012-06-22 14:36:09 -04001748 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001749 }
1750
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001751 error_setg(errp, QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001752}
1753
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001754static void hmp_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001755{
Luiz Capitulino13548692011-07-29 15:36:43 -03001756 int saved_vm_running = runstate_is_running();
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001757 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02001758
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001759 vm_stop(RUN_STATE_RESTORE_VM);
Juan Quintelac8d41b22009-08-20 19:42:21 +02001760
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03001761 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02001762 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03001763 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02001764}
1765
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001766int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
Mark McLoughlin7768e042009-07-22 09:11:41 +01001767{
Anthony Liguoric227f092009-10-01 16:12:16 -05001768 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01001769
Blue Swirl72cf2d42009-09-12 07:36:22 +00001770 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01001771 int fd;
1772
1773 if (strcmp(monfd->name, fdname) != 0) {
1774 continue;
1775 }
1776
1777 fd = monfd->fd;
1778
1779 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001780 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05001781 g_free(monfd->name);
1782 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001783
1784 return fd;
1785 }
1786
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001787 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001788 return -1;
1789}
1790
Corey Bryantba1c0482012-08-14 16:43:43 -04001791static void monitor_fdset_cleanup(MonFdset *mon_fdset)
1792{
1793 MonFdsetFd *mon_fdset_fd;
1794 MonFdsetFd *mon_fdset_fd_next;
1795
1796 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantebe52b52012-10-18 15:19:33 -04001797 if ((mon_fdset_fd->removed ||
1798 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
1799 runstate_is_running()) {
Corey Bryantba1c0482012-08-14 16:43:43 -04001800 close(mon_fdset_fd->fd);
1801 g_free(mon_fdset_fd->opaque);
1802 QLIST_REMOVE(mon_fdset_fd, next);
1803 g_free(mon_fdset_fd);
1804 }
1805 }
1806
Corey Bryantadb696f2012-08-14 16:43:47 -04001807 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04001808 QLIST_REMOVE(mon_fdset, next);
1809 g_free(mon_fdset);
1810 }
1811}
1812
Corey Bryantefb87c12012-08-14 16:43:48 -04001813static void monitor_fdsets_cleanup(void)
1814{
1815 MonFdset *mon_fdset;
1816 MonFdset *mon_fdset_next;
1817
1818 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
1819 monitor_fdset_cleanup(mon_fdset);
1820 }
1821}
1822
Corey Bryantba1c0482012-08-14 16:43:43 -04001823AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
1824 const char *opaque, Error **errp)
1825{
1826 int fd;
1827 Monitor *mon = cur_mon;
Corey Bryantba1c0482012-08-14 16:43:43 -04001828 AddfdInfo *fdinfo;
1829
1830 fd = qemu_chr_fe_get_msgfd(mon->chr);
1831 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001832 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryantba1c0482012-08-14 16:43:43 -04001833 goto error;
1834 }
1835
Corey Bryante446f702012-10-18 15:19:32 -04001836 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
1837 has_opaque, opaque, errp);
1838 if (fdinfo) {
1839 return fdinfo;
Corey Bryant9ac54af2012-10-18 15:19:31 -04001840 }
1841
Corey Bryantba1c0482012-08-14 16:43:43 -04001842error:
1843 if (fd != -1) {
1844 close(fd);
1845 }
1846 return NULL;
1847}
1848
1849void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
1850{
1851 MonFdset *mon_fdset;
1852 MonFdsetFd *mon_fdset_fd;
1853 char fd_str[60];
1854
1855 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1856 if (mon_fdset->id != fdset_id) {
1857 continue;
1858 }
1859 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1860 if (has_fd) {
1861 if (mon_fdset_fd->fd != fd) {
1862 continue;
1863 }
1864 mon_fdset_fd->removed = true;
1865 break;
1866 } else {
1867 mon_fdset_fd->removed = true;
1868 }
1869 }
1870 if (has_fd && !mon_fdset_fd) {
1871 goto error;
1872 }
1873 monitor_fdset_cleanup(mon_fdset);
1874 return;
1875 }
1876
1877error:
1878 if (has_fd) {
1879 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
1880 fdset_id, fd);
1881 } else {
1882 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
1883 }
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001884 error_setg(errp, QERR_FD_NOT_FOUND, fd_str);
Corey Bryantba1c0482012-08-14 16:43:43 -04001885}
1886
1887FdsetInfoList *qmp_query_fdsets(Error **errp)
1888{
1889 MonFdset *mon_fdset;
1890 MonFdsetFd *mon_fdset_fd;
1891 FdsetInfoList *fdset_list = NULL;
1892
1893 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1894 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
1895 FdsetFdInfoList *fdsetfd_list = NULL;
1896
1897 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
1898 fdset_info->value->fdset_id = mon_fdset->id;
1899
1900 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1901 FdsetFdInfoList *fdsetfd_info;
1902
1903 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
1904 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
1905 fdsetfd_info->value->fd = mon_fdset_fd->fd;
1906 if (mon_fdset_fd->opaque) {
1907 fdsetfd_info->value->has_opaque = true;
1908 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
1909 } else {
1910 fdsetfd_info->value->has_opaque = false;
1911 }
1912
1913 fdsetfd_info->next = fdsetfd_list;
1914 fdsetfd_list = fdsetfd_info;
1915 }
1916
1917 fdset_info->value->fds = fdsetfd_list;
1918
1919 fdset_info->next = fdset_list;
1920 fdset_list = fdset_info;
1921 }
1922
1923 return fdset_list;
1924}
1925
Corey Bryante446f702012-10-18 15:19:32 -04001926AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
1927 bool has_opaque, const char *opaque,
1928 Error **errp)
1929{
1930 MonFdset *mon_fdset = NULL;
1931 MonFdsetFd *mon_fdset_fd;
1932 AddfdInfo *fdinfo;
1933
1934 if (has_fdset_id) {
1935 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1936 /* Break if match found or match impossible due to ordering by ID */
1937 if (fdset_id <= mon_fdset->id) {
1938 if (fdset_id < mon_fdset->id) {
1939 mon_fdset = NULL;
1940 }
1941 break;
1942 }
1943 }
1944 }
1945
1946 if (mon_fdset == NULL) {
1947 int64_t fdset_id_prev = -1;
1948 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
1949
1950 if (has_fdset_id) {
1951 if (fdset_id < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001952 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
1953 "a non-negative value");
Corey Bryante446f702012-10-18 15:19:32 -04001954 return NULL;
1955 }
1956 /* Use specified fdset ID */
1957 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1958 mon_fdset_cur = mon_fdset;
1959 if (fdset_id < mon_fdset_cur->id) {
1960 break;
1961 }
1962 }
1963 } else {
1964 /* Use first available fdset ID */
1965 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1966 mon_fdset_cur = mon_fdset;
1967 if (fdset_id_prev == mon_fdset_cur->id - 1) {
1968 fdset_id_prev = mon_fdset_cur->id;
1969 continue;
1970 }
1971 break;
1972 }
1973 }
1974
1975 mon_fdset = g_malloc0(sizeof(*mon_fdset));
1976 if (has_fdset_id) {
1977 mon_fdset->id = fdset_id;
1978 } else {
1979 mon_fdset->id = fdset_id_prev + 1;
1980 }
1981
1982 /* The fdset list is ordered by fdset ID */
1983 if (!mon_fdset_cur) {
1984 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
1985 } else if (mon_fdset->id < mon_fdset_cur->id) {
1986 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
1987 } else {
1988 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
1989 }
1990 }
1991
1992 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
1993 mon_fdset_fd->fd = fd;
1994 mon_fdset_fd->removed = false;
1995 if (has_opaque) {
1996 mon_fdset_fd->opaque = g_strdup(opaque);
1997 }
1998 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
1999
2000 fdinfo = g_malloc0(sizeof(*fdinfo));
2001 fdinfo->fdset_id = mon_fdset->id;
2002 fdinfo->fd = mon_fdset_fd->fd;
2003
2004 return fdinfo;
2005}
2006
Corey Bryantadb696f2012-08-14 16:43:47 -04002007int monitor_fdset_get_fd(int64_t fdset_id, int flags)
2008{
Blue Swirlb2dc64c2012-08-18 20:14:54 +00002009#ifndef _WIN32
Corey Bryantadb696f2012-08-14 16:43:47 -04002010 MonFdset *mon_fdset;
2011 MonFdsetFd *mon_fdset_fd;
2012 int mon_fd_flags;
2013
Corey Bryantadb696f2012-08-14 16:43:47 -04002014 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2015 if (mon_fdset->id != fdset_id) {
2016 continue;
2017 }
2018 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2019 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
2020 if (mon_fd_flags == -1) {
2021 return -1;
2022 }
2023
2024 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
2025 return mon_fdset_fd->fd;
2026 }
2027 }
2028 errno = EACCES;
2029 return -1;
2030 }
2031#endif
2032
2033 errno = ENOENT;
2034 return -1;
2035}
2036
2037int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
2038{
2039 MonFdset *mon_fdset;
2040 MonFdsetFd *mon_fdset_fd_dup;
2041
2042 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2043 if (mon_fdset->id != fdset_id) {
2044 continue;
2045 }
2046 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2047 if (mon_fdset_fd_dup->fd == dup_fd) {
2048 return -1;
2049 }
2050 }
2051 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2052 mon_fdset_fd_dup->fd = dup_fd;
2053 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
2054 return 0;
2055 }
2056 return -1;
2057}
2058
2059static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2060{
2061 MonFdset *mon_fdset;
2062 MonFdsetFd *mon_fdset_fd_dup;
2063
2064 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2065 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2066 if (mon_fdset_fd_dup->fd == dup_fd) {
2067 if (remove) {
2068 QLIST_REMOVE(mon_fdset_fd_dup, next);
2069 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2070 monitor_fdset_cleanup(mon_fdset);
2071 }
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002072 return -1;
2073 } else {
2074 return mon_fdset->id;
Corey Bryantadb696f2012-08-14 16:43:47 -04002075 }
Corey Bryantadb696f2012-08-14 16:43:47 -04002076 }
2077 }
2078 }
2079 return -1;
2080}
2081
2082int monitor_fdset_dup_fd_find(int dup_fd)
2083{
2084 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2085}
2086
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002087void monitor_fdset_dup_fd_remove(int dup_fd)
Corey Bryantadb696f2012-08-14 16:43:47 -04002088{
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002089 monitor_fdset_dup_fd_find_remove(dup_fd, true);
Corey Bryantadb696f2012-08-14 16:43:47 -04002090}
2091
Markus Armbruster1677f4c2015-02-09 14:03:19 +01002092int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
Laszlo Ersek59063662014-04-10 10:24:31 +02002093{
2094 int fd;
2095 Error *local_err = NULL;
2096
2097 if (!qemu_isdigit(fdname[0]) && mon) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002098 fd = monitor_get_fd(mon, fdname, &local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002099 } else {
2100 fd = qemu_parse_fd(fdname);
Laszlo Ersek59063662014-04-10 10:24:31 +02002101 if (fd == -1) {
2102 error_setg(&local_err, "Invalid file descriptor number '%s'",
2103 fdname);
2104 }
2105 }
2106 if (local_err) {
2107 error_propagate(errp, local_err);
2108 assert(fd == -1);
2109 } else {
2110 assert(fd != -1);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002111 }
2112
2113 return fd;
2114}
2115
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002116/* Please update hmp-commands.hx when adding or changing commands */
Wayne Xia816f8922011-10-12 11:32:41 +08002117static mon_cmd_t info_cmds[] = {
Pavel Butsykinda76ee72015-09-10 18:38:58 +03002118#include "hmp-commands-info.h"
2119 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002120};
2121
Wenchao Xiaa13ced52013-01-14 14:06:28 +08002122/* mon_cmds and info_cmds would be sorted at runtime */
2123static mon_cmd_t mon_cmds[] = {
2124#include "hmp-commands.h"
2125 { NULL, NULL, },
2126};
2127
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002128static const mon_cmd_t qmp_cmds[] = {
Anthony Liguorie3193602011-09-02 12:34:47 -05002129#include "qmp-commands-old.h"
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002130 { /* NULL */ },
2131};
2132
bellard9307c4c2004-04-04 12:57:25 +00002133/*******************************************************************/
2134
2135static const char *pch;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002136static sigjmp_buf expr_env;
bellard9307c4c2004-04-04 12:57:25 +00002137
bellard9307c4c2004-04-04 12:57:25 +00002138
Stefan Weil9c3175c2013-08-22 21:30:09 +02002139static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
2140expr_error(Monitor *mon, const char *fmt, ...)
bellard9dc39cb2004-03-14 21:38:27 +00002141{
Fam Zheng277acfe2013-08-20 10:58:21 +08002142 va_list ap;
2143 va_start(ap, fmt);
2144 monitor_vprintf(mon, fmt, ap);
2145 monitor_printf(mon, "\n");
2146 va_end(ap);
Peter Maydell6ab7e542013-02-20 15:21:09 +00002147 siglongjmp(expr_env, 1);
bellard9307c4c2004-04-04 12:57:25 +00002148}
2149
Markus Armbruster09b94182010-01-20 13:07:30 +01002150/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00002151static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002152{
Pavel Butsykinbf957282015-09-10 18:38:59 +03002153 const MonitorDef *md = target_monitor_defs();
bellard92a31b12005-02-10 22:00:52 +00002154 void *ptr;
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002155 uint64_t tmp = 0;
2156 int ret;
bellard92a31b12005-02-10 22:00:52 +00002157
Pavel Butsykinbf957282015-09-10 18:38:59 +03002158 if (md == NULL) {
2159 return -1;
2160 }
2161
2162 for(; md->name != NULL; md++) {
bellard9307c4c2004-04-04 12:57:25 +00002163 if (compare_cmd(name, md->name)) {
2164 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002165 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002166 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002167 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00002168 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002169 switch(md->type) {
2170 case MD_I32:
2171 *pval = *(int32_t *)ptr;
2172 break;
2173 case MD_TLONG:
2174 *pval = *(target_long *)ptr;
2175 break;
2176 default:
2177 *pval = 0;
2178 break;
2179 }
bellard9307c4c2004-04-04 12:57:25 +00002180 }
2181 return 0;
2182 }
2183 }
Alexey Kardashevskiy0a9516c2015-11-12 14:44:23 +11002184
2185 ret = target_get_monitor_def(mon_get_cpu(), name, &tmp);
2186 if (!ret) {
2187 *pval = (target_long) tmp;
2188 }
2189
2190 return ret;
bellard9307c4c2004-04-04 12:57:25 +00002191}
2192
2193static void next(void)
2194{
Blue Swirl660f11b2009-07-31 21:16:51 +00002195 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002196 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002197 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002198 pch++;
2199 }
2200}
2201
aliguori376253e2009-03-05 23:01:23 +00002202static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002203
aliguori376253e2009-03-05 23:01:23 +00002204static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002205{
blueswir1c2efc952007-09-25 17:28:42 +00002206 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002207 char *p;
bellard6a00d602005-11-21 23:25:50 +00002208 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002209
2210 switch(*pch) {
2211 case '+':
2212 next();
aliguori376253e2009-03-05 23:01:23 +00002213 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002214 break;
2215 case '-':
2216 next();
aliguori376253e2009-03-05 23:01:23 +00002217 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002218 break;
2219 case '~':
2220 next();
aliguori376253e2009-03-05 23:01:23 +00002221 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002222 break;
2223 case '(':
2224 next();
aliguori376253e2009-03-05 23:01:23 +00002225 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002226 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002227 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002228 }
2229 next();
2230 break;
bellard81d09122004-07-14 17:21:37 +00002231 case '\'':
2232 pch++;
2233 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002234 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002235 n = *pch;
2236 pch++;
2237 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002238 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002239 next();
2240 break;
bellard9307c4c2004-04-04 12:57:25 +00002241 case '$':
2242 {
2243 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002244 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002245
bellard9307c4c2004-04-04 12:57:25 +00002246 pch++;
2247 q = buf;
2248 while ((*pch >= 'a' && *pch <= 'z') ||
2249 (*pch >= 'A' && *pch <= 'Z') ||
2250 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002251 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002252 if ((q - buf) < sizeof(buf) - 1)
2253 *q++ = *pch;
2254 pch++;
2255 }
blueswir1cd390082008-11-16 13:53:32 +00002256 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002257 pch++;
2258 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002259 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01002260 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00002261 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00002262 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002263 }
2264 break;
2265 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002266 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002267 n = 0;
2268 break;
2269 default:
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002270 errno = 0;
bellard4f4fbf72006-06-25 18:28:12 +00002271 n = strtoull(pch, &p, 0);
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002272 if (errno == ERANGE) {
2273 expr_error(mon, "number too large");
2274 }
bellard9307c4c2004-04-04 12:57:25 +00002275 if (pch == p) {
Fam Zheng277acfe2013-08-20 10:58:21 +08002276 expr_error(mon, "invalid char '%c' in expression", *p);
bellard9307c4c2004-04-04 12:57:25 +00002277 }
2278 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002279 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002280 pch++;
2281 break;
2282 }
2283 return n;
2284}
2285
2286
aliguori376253e2009-03-05 23:01:23 +00002287static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002288{
blueswir1c2efc952007-09-25 17:28:42 +00002289 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002290 int op;
ths3b46e622007-09-17 08:09:54 +00002291
aliguori376253e2009-03-05 23:01:23 +00002292 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002293 for(;;) {
2294 op = *pch;
2295 if (op != '*' && op != '/' && op != '%')
2296 break;
2297 next();
aliguori376253e2009-03-05 23:01:23 +00002298 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002299 switch(op) {
2300 default:
2301 case '*':
2302 val *= val2;
2303 break;
2304 case '/':
2305 case '%':
ths5fafdf22007-09-16 21:08:06 +00002306 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002307 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002308 if (op == '/')
2309 val /= val2;
2310 else
2311 val %= val2;
2312 break;
2313 }
2314 }
2315 return val;
2316}
2317
aliguori376253e2009-03-05 23:01:23 +00002318static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002319{
blueswir1c2efc952007-09-25 17:28:42 +00002320 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002321 int op;
bellard9307c4c2004-04-04 12:57:25 +00002322
aliguori376253e2009-03-05 23:01:23 +00002323 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002324 for(;;) {
2325 op = *pch;
2326 if (op != '&' && op != '|' && op != '^')
2327 break;
2328 next();
aliguori376253e2009-03-05 23:01:23 +00002329 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002330 switch(op) {
2331 default:
2332 case '&':
2333 val &= val2;
2334 break;
2335 case '|':
2336 val |= val2;
2337 break;
2338 case '^':
2339 val ^= val2;
2340 break;
2341 }
2342 }
2343 return val;
2344}
2345
aliguori376253e2009-03-05 23:01:23 +00002346static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002347{
blueswir1c2efc952007-09-25 17:28:42 +00002348 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002349 int op;
bellard9307c4c2004-04-04 12:57:25 +00002350
aliguori376253e2009-03-05 23:01:23 +00002351 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002352 for(;;) {
2353 op = *pch;
2354 if (op != '+' && op != '-')
2355 break;
2356 next();
aliguori376253e2009-03-05 23:01:23 +00002357 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002358 if (op == '+')
2359 val += val2;
2360 else
2361 val -= val2;
2362 }
2363 return val;
2364}
2365
aliguori376253e2009-03-05 23:01:23 +00002366static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002367{
2368 pch = *pp;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002369 if (sigsetjmp(expr_env, 0)) {
bellard9307c4c2004-04-04 12:57:25 +00002370 *pp = pch;
2371 return -1;
2372 }
blueswir1cd390082008-11-16 13:53:32 +00002373 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002374 pch++;
aliguori376253e2009-03-05 23:01:23 +00002375 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002376 *pp = pch;
2377 return 0;
2378}
2379
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002380static int get_double(Monitor *mon, double *pval, const char **pp)
2381{
2382 const char *p = *pp;
2383 char *tailp;
2384 double d;
2385
2386 d = strtod(p, &tailp);
2387 if (tailp == p) {
2388 monitor_printf(mon, "Number expected\n");
2389 return -1;
2390 }
2391 if (d != d || d - d != 0) {
2392 /* NaN or infinity */
2393 monitor_printf(mon, "Bad number\n");
2394 return -1;
2395 }
2396 *pval = d;
2397 *pp = tailp;
2398 return 0;
2399}
2400
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002401/*
2402 * Store the command-name in cmdname, and return a pointer to
2403 * the remaining of the command string.
2404 */
2405static const char *get_command_name(const char *cmdline,
2406 char *cmdname, size_t nlen)
2407{
2408 size_t len;
2409 const char *p, *pstart;
2410
2411 p = cmdline;
2412 while (qemu_isspace(*p))
2413 p++;
2414 if (*p == '\0')
2415 return NULL;
2416 pstart = p;
2417 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2418 p++;
2419 len = p - pstart;
2420 if (len > nlen - 1)
2421 len = nlen - 1;
2422 memcpy(cmdname, pstart, len);
2423 cmdname[len] = '\0';
2424 return p;
2425}
2426
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002427/**
2428 * Read key of 'type' into 'key' and return the current
2429 * 'type' pointer.
2430 */
2431static char *key_get_info(const char *type, char **key)
2432{
2433 size_t len;
2434 char *p, *str;
2435
2436 if (*type == ',')
2437 type++;
2438
2439 p = strchr(type, ':');
2440 if (!p) {
2441 *key = NULL;
2442 return NULL;
2443 }
2444 len = p - type;
2445
Anthony Liguori7267c092011-08-20 22:09:37 -05002446 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002447 memcpy(str, type, len);
2448 str[len] = '\0';
2449
2450 *key = str;
2451 return ++p;
2452}
2453
bellard9307c4c2004-04-04 12:57:25 +00002454static int default_fmt_format = 'x';
2455static int default_fmt_size = 4;
2456
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002457static int is_valid_option(const char *c, const char *typestr)
2458{
2459 char option[3];
2460
2461 option[0] = '-';
2462 option[1] = *c;
2463 option[2] = '\0';
2464
2465 typestr = strstr(typestr, option);
2466 return (typestr != NULL);
2467}
2468
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03002469static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
2470 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002471{
2472 const mon_cmd_t *cmd;
2473
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03002474 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002475 if (compare_cmd(cmdname, cmd->name)) {
2476 return cmd;
2477 }
2478 }
2479
2480 return NULL;
2481}
2482
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03002483static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
2484{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002485 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03002486}
2487
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002488/*
Bandan Dasae502122015-06-03 18:38:08 -04002489 * Parse command name from @cmdp according to command table @table.
2490 * If blank, return NULL.
2491 * Else, if no valid command can be found, report to @mon, and return
2492 * NULL.
2493 * Else, change @cmdp to point right behind the name, and return its
2494 * command table entry.
2495 * Do not assume the return value points into @table! It doesn't when
2496 * the command is found in a sub-command table.
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002497 */
Anthony Liguoric227f092009-10-01 16:12:16 -05002498static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Bandan Dasae502122015-06-03 18:38:08 -04002499 const char **cmdp,
2500 mon_cmd_t *table)
bellard9307c4c2004-04-04 12:57:25 +00002501{
Bandan Dasae502122015-06-03 18:38:08 -04002502 const char *p;
Anthony Liguoric227f092009-10-01 16:12:16 -05002503 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002504 char cmdname[256];
bellard9dc39cb2004-03-14 21:38:27 +00002505
bellard9307c4c2004-04-04 12:57:25 +00002506 /* extract the command name */
Bandan Dasae502122015-06-03 18:38:08 -04002507 p = get_command_name(*cmdp, cmdname, sizeof(cmdname));
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002508 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002509 return NULL;
ths3b46e622007-09-17 08:09:54 +00002510
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002511 cmd = search_dispatch_table(table, cmdname);
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002512 if (!cmd) {
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002513 monitor_printf(mon, "unknown command: '%.*s'\n",
Bandan Dasae502122015-06-03 18:38:08 -04002514 (int)(p - *cmdp), *cmdp);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002515 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002516 }
bellard9307c4c2004-04-04 12:57:25 +00002517
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002518 /* filter out following useless space */
2519 while (qemu_isspace(*p)) {
2520 p++;
2521 }
Bandan Dasae502122015-06-03 18:38:08 -04002522
2523 *cmdp = p;
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002524 /* search sub command */
Bandan Dasae502122015-06-03 18:38:08 -04002525 if (cmd->sub_table != NULL && *p != '\0') {
2526 return monitor_parse_command(mon, cmdp, cmd->sub_table);
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002527 }
2528
Bandan Dasae502122015-06-03 18:38:08 -04002529 return cmd;
2530}
2531
2532/*
2533 * Parse arguments for @cmd.
2534 * If it can't be parsed, report to @mon, and return NULL.
2535 * Else, insert command arguments into a QDict, and return it.
2536 * Note: On success, caller has to free the QDict structure.
2537 */
2538
2539static QDict *monitor_parse_arguments(Monitor *mon,
2540 const char **endp,
2541 const mon_cmd_t *cmd)
2542{
2543 const char *typestr;
2544 char *key;
2545 int c;
2546 const char *p = *endp;
2547 char buf[1024];
2548 QDict *qdict = qdict_new();
2549
bellard9307c4c2004-04-04 12:57:25 +00002550 /* parse the parameters */
2551 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00002552 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002553 typestr = key_get_info(typestr, &key);
2554 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00002555 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002556 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00002557 typestr++;
2558 switch(c) {
2559 case 'F':
bellard81d09122004-07-14 17:21:37 +00002560 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002561 case 's':
2562 {
2563 int ret;
ths3b46e622007-09-17 08:09:54 +00002564
blueswir1cd390082008-11-16 13:53:32 +00002565 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002566 p++;
2567 if (*typestr == '?') {
2568 typestr++;
2569 if (*p == '\0') {
2570 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03002571 break;
bellard9307c4c2004-04-04 12:57:25 +00002572 }
2573 }
2574 ret = get_str(buf, sizeof(buf), &p);
2575 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002576 switch(c) {
2577 case 'F':
aliguori376253e2009-03-05 23:01:23 +00002578 monitor_printf(mon, "%s: filename expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002579 cmd->name);
bellard81d09122004-07-14 17:21:37 +00002580 break;
2581 case 'B':
aliguori376253e2009-03-05 23:01:23 +00002582 monitor_printf(mon, "%s: block device name expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002583 cmd->name);
bellard81d09122004-07-14 17:21:37 +00002584 break;
2585 default:
Bandan Dasae502122015-06-03 18:38:08 -04002586 monitor_printf(mon, "%s: string expected\n", cmd->name);
bellard81d09122004-07-14 17:21:37 +00002587 break;
2588 }
bellard9307c4c2004-04-04 12:57:25 +00002589 goto fail;
2590 }
Luiz Capitulino53773582009-08-28 15:27:25 -03002591 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00002592 }
2593 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01002594 case 'O':
2595 {
2596 QemuOptsList *opts_list;
2597 QemuOpts *opts;
2598
2599 opts_list = qemu_find_opts(key);
2600 if (!opts_list || opts_list->desc->name) {
2601 goto bad_type;
2602 }
2603 while (qemu_isspace(*p)) {
2604 p++;
2605 }
2606 if (!*p)
2607 break;
2608 if (get_str(buf, sizeof(buf), &p) < 0) {
2609 goto fail;
2610 }
Markus Armbruster70b94332015-02-13 12:50:26 +01002611 opts = qemu_opts_parse_noisily(opts_list, buf, true);
Markus Armbruster361127d2010-02-10 20:24:35 +01002612 if (!opts) {
2613 goto fail;
2614 }
2615 qemu_opts_to_qdict(opts, qdict);
2616 qemu_opts_del(opts);
2617 }
2618 break;
bellard9307c4c2004-04-04 12:57:25 +00002619 case '/':
2620 {
2621 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002622
blueswir1cd390082008-11-16 13:53:32 +00002623 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002624 p++;
2625 if (*p == '/') {
2626 /* format found */
2627 p++;
2628 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002629 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002630 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002631 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002632 count = count * 10 + (*p - '0');
2633 p++;
2634 }
2635 }
2636 size = -1;
2637 format = -1;
2638 for(;;) {
2639 switch(*p) {
2640 case 'o':
2641 case 'd':
2642 case 'u':
2643 case 'x':
2644 case 'i':
2645 case 'c':
2646 format = *p++;
2647 break;
2648 case 'b':
2649 size = 1;
2650 p++;
2651 break;
2652 case 'h':
2653 size = 2;
2654 p++;
2655 break;
2656 case 'w':
2657 size = 4;
2658 p++;
2659 break;
2660 case 'g':
2661 case 'L':
2662 size = 8;
2663 p++;
2664 break;
2665 default:
2666 goto next;
2667 }
2668 }
2669 next:
blueswir1cd390082008-11-16 13:53:32 +00002670 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00002671 monitor_printf(mon, "invalid char in format: '%c'\n",
2672 *p);
bellard9307c4c2004-04-04 12:57:25 +00002673 goto fail;
2674 }
bellard9307c4c2004-04-04 12:57:25 +00002675 if (format < 0)
2676 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002677 if (format != 'i') {
2678 /* for 'i', not specifying a size gives -1 as size */
2679 if (size < 0)
2680 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002681 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002682 }
bellard9307c4c2004-04-04 12:57:25 +00002683 default_fmt_format = format;
2684 } else {
2685 count = 1;
2686 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002687 if (format != 'i') {
2688 size = default_fmt_size;
2689 } else {
2690 size = -1;
2691 }
bellard9307c4c2004-04-04 12:57:25 +00002692 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002693 qdict_put(qdict, "count", qint_from_int(count));
2694 qdict_put(qdict, "format", qint_from_int(format));
2695 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00002696 }
2697 break;
2698 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002699 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002700 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00002701 {
blueswir1c2efc952007-09-25 17:28:42 +00002702 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00002703
blueswir1cd390082008-11-16 13:53:32 +00002704 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002705 p++;
bellard34405572004-06-08 00:55:58 +00002706 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002707 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03002708 if (*p == '\0') {
2709 typestr++;
2710 break;
2711 }
bellard34405572004-06-08 00:55:58 +00002712 } else {
2713 if (*p == '.') {
2714 p++;
blueswir1cd390082008-11-16 13:53:32 +00002715 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00002716 p++;
bellard34405572004-06-08 00:55:58 +00002717 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03002718 typestr++;
2719 break;
bellard34405572004-06-08 00:55:58 +00002720 }
2721 }
bellard13224a82006-07-14 22:03:35 +00002722 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002723 }
aliguori376253e2009-03-05 23:01:23 +00002724 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00002725 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002726 /* Check if 'i' is greater than 32-bit */
2727 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
Bandan Dasae502122015-06-03 18:38:08 -04002728 monitor_printf(mon, "\'%s\' has failed: ", cmd->name);
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002729 monitor_printf(mon, "integer is for 32-bit values\n");
2730 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002731 } else if (c == 'M') {
Luiz Capitulino91162842012-04-26 17:34:30 -03002732 if (val < 0) {
2733 monitor_printf(mon, "enter a positive value\n");
2734 goto fail;
2735 }
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002736 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002737 }
Luiz Capitulino53773582009-08-28 15:27:25 -03002738 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00002739 }
2740 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02002741 case 'o':
2742 {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01002743 int64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02002744 char *end;
2745
2746 while (qemu_isspace(*p)) {
2747 p++;
2748 }
2749 if (*typestr == '?') {
2750 typestr++;
2751 if (*p == '\0') {
2752 break;
2753 }
2754 }
Marc-André Lureau4677bb42015-09-16 18:02:56 +02002755 val = qemu_strtosz(p, &end);
Jes Sorensendbc0c672010-10-21 17:15:47 +02002756 if (val < 0) {
2757 monitor_printf(mon, "invalid size\n");
2758 goto fail;
2759 }
2760 qdict_put(qdict, key, qint_from_int(val));
2761 p = end;
2762 }
2763 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01002764 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002765 {
2766 double val;
2767
2768 while (qemu_isspace(*p))
2769 p++;
2770 if (*typestr == '?') {
2771 typestr++;
2772 if (*p == '\0') {
2773 break;
2774 }
2775 }
2776 if (get_double(mon, &val, &p) < 0) {
2777 goto fail;
2778 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02002779 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01002780 switch (*p) {
2781 case 'm':
2782 val /= 1e3; p += 2; break;
2783 case 'u':
2784 val /= 1e6; p += 2; break;
2785 case 'n':
2786 val /= 1e9; p += 2; break;
2787 }
2788 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002789 if (*p && !qemu_isspace(*p)) {
2790 monitor_printf(mon, "Unknown unit suffix\n");
2791 goto fail;
2792 }
2793 qdict_put(qdict, key, qfloat_from_double(val));
2794 }
2795 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002796 case 'b':
2797 {
2798 const char *beg;
Eric Blakefc48ffc2015-05-15 16:24:59 -06002799 bool val;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002800
2801 while (qemu_isspace(*p)) {
2802 p++;
2803 }
2804 beg = p;
2805 while (qemu_isgraph(*p)) {
2806 p++;
2807 }
2808 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06002809 val = true;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002810 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06002811 val = false;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002812 } else {
2813 monitor_printf(mon, "Expected 'on' or 'off'\n");
2814 goto fail;
2815 }
Eric Blakefc48ffc2015-05-15 16:24:59 -06002816 qdict_put(qdict, key, qbool_from_bool(val));
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002817 }
2818 break;
bellard9307c4c2004-04-04 12:57:25 +00002819 case '-':
2820 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002821 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03002822 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00002823 /* option */
ths3b46e622007-09-17 08:09:54 +00002824
bellard9307c4c2004-04-04 12:57:25 +00002825 c = *typestr++;
2826 if (c == '\0')
2827 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00002828 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002829 p++;
bellard9307c4c2004-04-04 12:57:25 +00002830 if (*p == '-') {
2831 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002832 if(c != *p) {
2833 if(!is_valid_option(p, typestr)) {
2834
2835 monitor_printf(mon, "%s: unsupported option -%c\n",
Bandan Dasae502122015-06-03 18:38:08 -04002836 cmd->name, *p);
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002837 goto fail;
2838 } else {
2839 skip_key = 1;
2840 }
bellard9307c4c2004-04-04 12:57:25 +00002841 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002842 if(skip_key) {
2843 p = tmp;
2844 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03002845 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002846 p++;
Eric Blakefc48ffc2015-05-15 16:24:59 -06002847 qdict_put(qdict, key, qbool_from_bool(true));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002848 }
bellard9307c4c2004-04-04 12:57:25 +00002849 }
bellard9307c4c2004-04-04 12:57:25 +00002850 }
2851 break;
Wenchao Xia129be002013-08-27 20:38:26 +08002852 case 'S':
2853 {
2854 /* package all remaining string */
2855 int len;
2856
2857 while (qemu_isspace(*p)) {
2858 p++;
2859 }
2860 if (*typestr == '?') {
2861 typestr++;
2862 if (*p == '\0') {
2863 /* no remaining string: NULL argument */
2864 break;
2865 }
2866 }
2867 len = strlen(p);
2868 if (len <= 0) {
2869 monitor_printf(mon, "%s: string expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002870 cmd->name);
Bandan Dase549d2a2015-06-03 18:38:10 -04002871 goto fail;
Wenchao Xia129be002013-08-27 20:38:26 +08002872 }
2873 qdict_put(qdict, key, qstring_from_str(p));
2874 p += len;
2875 }
2876 break;
bellard9307c4c2004-04-04 12:57:25 +00002877 default:
2878 bad_type:
Bandan Dasae502122015-06-03 18:38:08 -04002879 monitor_printf(mon, "%s: unknown type '%c'\n", cmd->name, c);
bellard9307c4c2004-04-04 12:57:25 +00002880 goto fail;
2881 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002882 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002883 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00002884 }
2885 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00002886 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002887 p++;
2888 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00002889 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
Bandan Dasae502122015-06-03 18:38:08 -04002890 cmd->name);
bellard9307c4c2004-04-04 12:57:25 +00002891 goto fail;
2892 }
2893
Bandan Dasae502122015-06-03 18:38:08 -04002894 return qdict;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02002895
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002896fail:
Bandan Dasae502122015-06-03 18:38:08 -04002897 QDECREF(qdict);
Anthony Liguori7267c092011-08-20 22:09:37 -05002898 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002899 return NULL;
2900}
2901
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01002902static void handle_hmp_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002903{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002904 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05002905 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002906
Bandan Dasae502122015-06-03 18:38:08 -04002907 cmd = monitor_parse_command(mon, &cmdline, mon->cmd_table);
2908 if (!cmd) {
2909 return;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002910 }
2911
Bandan Dasae502122015-06-03 18:38:08 -04002912 qdict = monitor_parse_arguments(mon, &cmdline, cmd);
2913 if (!qdict) {
Bandan Dasdd41eea2015-06-03 18:38:09 -04002914 monitor_printf(mon, "Try \"help %s\" for more information\n",
2915 cmd->name);
Bandan Dasae502122015-06-03 18:38:08 -04002916 return;
2917 }
2918
2919 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002920 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00002921}
2922
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08002923static void cmd_completion(Monitor *mon, const char *name, const char *list)
bellard81d09122004-07-14 17:21:37 +00002924{
2925 const char *p, *pstart;
2926 char cmd[128];
2927 int len;
2928
2929 p = list;
2930 for(;;) {
2931 pstart = p;
2932 p = strchr(p, '|');
2933 if (!p)
2934 p = pstart + strlen(pstart);
2935 len = p - pstart;
2936 if (len > sizeof(cmd) - 2)
2937 len = sizeof(cmd) - 2;
2938 memcpy(cmd, pstart, len);
2939 cmd[len] = '\0';
2940 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08002941 readline_add_completion(mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00002942 }
2943 if (*p == '\0')
2944 break;
2945 p++;
2946 }
2947}
2948
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08002949static void file_completion(Monitor *mon, const char *input)
bellard81d09122004-07-14 17:21:37 +00002950{
2951 DIR *ffs;
2952 struct dirent *d;
2953 char path[1024];
2954 char file[1024], file_prefix[1024];
2955 int input_path_len;
2956 const char *p;
2957
ths5fafdf22007-09-16 21:08:06 +00002958 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00002959 if (!p) {
2960 input_path_len = 0;
2961 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00002962 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00002963 } else {
2964 input_path_len = p - input + 1;
2965 memcpy(path, input, input_path_len);
2966 if (input_path_len > sizeof(path) - 1)
2967 input_path_len = sizeof(path) - 1;
2968 path[input_path_len] = '\0';
2969 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2970 }
Bandan Das19f2db52015-06-03 18:38:07 -04002971
bellard81d09122004-07-14 17:21:37 +00002972 ffs = opendir(path);
2973 if (!ffs)
2974 return;
2975 for(;;) {
2976 struct stat sb;
2977 d = readdir(ffs);
2978 if (!d)
2979 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09002980
2981 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
2982 continue;
2983 }
2984
bellard81d09122004-07-14 17:21:37 +00002985 if (strstart(d->d_name, file_prefix, NULL)) {
2986 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00002987 if (input_path_len < sizeof(file))
2988 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2989 d->d_name);
bellard81d09122004-07-14 17:21:37 +00002990 /* stat the file to find out if it's a directory.
2991 * In that case add a slash to speed up typing long paths
2992 */
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01002993 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
blueswir1363a37d2008-08-21 17:58:08 +00002994 pstrcat(file, sizeof(file), "/");
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01002995 }
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08002996 readline_add_completion(mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00002997 }
2998 }
2999 closedir(ffs);
3000}
3001
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003002static const char *next_arg_type(const char *typestr)
3003{
3004 const char *p = strchr(typestr, ':');
3005 return (p != NULL ? ++p : typestr);
3006}
3007
Hani Benhabiles40d19392014-05-07 23:41:30 +01003008static void add_completion_option(ReadLineState *rs, const char *str,
3009 const char *option)
3010{
3011 if (!str || !option) {
3012 return;
3013 }
3014 if (!strncmp(option, str, strlen(str))) {
3015 readline_add_completion(rs, option);
3016 }
3017}
3018
Hani Benhabiles13e315d2014-05-07 23:41:29 +01003019void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
3020{
3021 size_t len;
3022 ChardevBackendInfoList *list, *start;
3023
3024 if (nb_args != 2) {
3025 return;
3026 }
3027 len = strlen(str);
3028 readline_set_completion_index(rs, len);
3029
3030 start = list = qmp_query_chardev_backends(NULL);
3031 while (list) {
3032 const char *chr_name = list->value->name;
3033
3034 if (!strncmp(chr_name, str, len)) {
3035 readline_add_completion(rs, chr_name);
3036 }
3037 list = list->next;
3038 }
3039 qapi_free_ChardevBackendInfoList(start);
3040}
3041
Hani Benhabilesb162b492014-05-07 23:41:31 +01003042void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
3043{
3044 size_t len;
3045 int i;
3046
3047 if (nb_args != 2) {
3048 return;
3049 }
3050 len = strlen(str);
3051 readline_set_completion_index(rs, len);
3052 for (i = 0; NetClientOptionsKind_lookup[i]; i++) {
3053 add_completion_option(rs, str, NetClientOptionsKind_lookup[i]);
3054 }
3055}
3056
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003057void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003058{
3059 GSList *list, *elt;
3060 size_t len;
3061
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003062 if (nb_args != 2) {
3063 return;
3064 }
3065
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003066 len = strlen(str);
3067 readline_set_completion_index(rs, len);
3068 list = elt = object_class_get_list(TYPE_DEVICE, false);
3069 while (elt) {
3070 const char *name;
3071 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
3072 TYPE_DEVICE);
3073 name = object_class_get_name(OBJECT_CLASS(dc));
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003074
3075 if (!dc->cannot_instantiate_with_device_add_yet
3076 && !strncmp(name, str, len)) {
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003077 readline_add_completion(rs, name);
3078 }
3079 elt = elt->next;
3080 }
3081 g_slist_free(list);
3082}
3083
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003084void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003085{
3086 GSList *list, *elt;
3087 size_t len;
3088
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003089 if (nb_args != 2) {
3090 return;
3091 }
3092
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003093 len = strlen(str);
3094 readline_set_completion_index(rs, len);
3095 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
3096 while (elt) {
3097 const char *name;
3098
3099 name = object_class_get_name(OBJECT_CLASS(elt->data));
3100 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
3101 readline_add_completion(rs, name);
3102 }
3103 elt = elt->next;
3104 }
3105 g_slist_free(list);
3106}
3107
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003108static void peripheral_device_del_completion(ReadLineState *rs,
3109 const char *str, size_t len)
3110{
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003111 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
3112 GSList *list, *item;
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003113
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003114 list = qdev_build_hotpluggable_device_list(peripheral);
3115 if (!list) {
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003116 return;
3117 }
3118
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003119 for (item = list; item; item = g_slist_next(item)) {
3120 DeviceState *dev = item->data;
3121
3122 if (dev->id && !strncmp(str, dev->id, len)) {
3123 readline_add_completion(rs, dev->id);
3124 }
3125 }
3126
3127 g_slist_free(list);
3128}
3129
Hani Benhabiles6297d9a2014-05-07 23:41:28 +01003130void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3131{
3132 size_t len;
3133 ChardevInfoList *list, *start;
3134
3135 if (nb_args != 2) {
3136 return;
3137 }
3138 len = strlen(str);
3139 readline_set_completion_index(rs, len);
3140
3141 start = list = qmp_query_chardev(NULL);
3142 while (list) {
3143 ChardevInfo *chr = list->value;
3144
3145 if (!strncmp(chr->label, str, len)) {
3146 readline_add_completion(rs, chr->label);
3147 }
3148 list = list->next;
3149 }
3150 qapi_free_ChardevInfoList(start);
3151}
3152
Hani Benhabiles8e597772014-05-27 23:39:30 +01003153static void ringbuf_completion(ReadLineState *rs, const char *str)
3154{
3155 size_t len;
3156 ChardevInfoList *list, *start;
3157
3158 len = strlen(str);
3159 readline_set_completion_index(rs, len);
3160
3161 start = list = qmp_query_chardev(NULL);
3162 while (list) {
3163 ChardevInfo *chr_info = list->value;
3164
3165 if (!strncmp(chr_info->label, str, len)) {
3166 CharDriverState *chr = qemu_chr_find(chr_info->label);
3167 if (chr && chr_is_ringbuf(chr)) {
3168 readline_add_completion(rs, chr_info->label);
3169 }
3170 }
3171 list = list->next;
3172 }
3173 qapi_free_ChardevInfoList(start);
3174}
3175
Hani Benhabiles8e597772014-05-27 23:39:30 +01003176void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
3177{
3178 if (nb_args != 2) {
3179 return;
3180 }
3181 ringbuf_completion(rs, str);
3182}
3183
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003184void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
3185{
3186 size_t len;
3187
3188 if (nb_args != 2) {
3189 return;
3190 }
3191
3192 len = strlen(str);
3193 readline_set_completion_index(rs, len);
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003194 peripheral_device_del_completion(rs, str, len);
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003195}
3196
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003197void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003198{
3199 ObjectPropertyInfoList *list, *start;
3200 size_t len;
3201
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003202 if (nb_args != 2) {
3203 return;
3204 }
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003205 len = strlen(str);
3206 readline_set_completion_index(rs, len);
3207
3208 start = list = qmp_qom_list("/objects", NULL);
3209 while (list) {
3210 ObjectPropertyInfo *info = list->value;
3211
3212 if (!strncmp(info->type, "child<", 5)
3213 && !strncmp(info->name, str, len)) {
3214 readline_add_completion(rs, info->name);
3215 }
3216 list = list->next;
3217 }
3218 qapi_free_ObjectPropertyInfoList(start);
3219}
3220
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003221void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
3222{
3223 int i;
3224 char *sep;
3225 size_t len;
3226
3227 if (nb_args != 2) {
3228 return;
3229 }
3230 sep = strrchr(str, '-');
3231 if (sep) {
3232 str = sep + 1;
3233 }
3234 len = strlen(str);
3235 readline_set_completion_index(rs, len);
Eric Blake7fb1cf12015-11-18 01:52:57 -07003236 for (i = 0; i < Q_KEY_CODE__MAX; i++) {
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003237 if (!strncmp(str, QKeyCode_lookup[i], len)) {
3238 readline_add_completion(rs, QKeyCode_lookup[i]);
3239 }
3240 }
3241}
3242
Hani Benhabiles40d19392014-05-07 23:41:30 +01003243void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
3244{
3245 size_t len;
3246
3247 len = strlen(str);
3248 readline_set_completion_index(rs, len);
3249 if (nb_args == 2) {
Jason Wangeaed4832015-04-23 14:21:38 +08003250 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles40d19392014-05-07 23:41:30 +01003251 int count, i;
3252 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08003253 NET_CLIENT_OPTIONS_KIND_NONE,
3254 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003255 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles40d19392014-05-07 23:41:30 +01003256 const char *name = ncs[i]->name;
3257 if (!strncmp(str, name, len)) {
3258 readline_add_completion(rs, name);
3259 }
3260 }
3261 } else if (nb_args == 3) {
3262 add_completion_option(rs, str, "on");
3263 add_completion_option(rs, str, "off");
3264 }
3265}
3266
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003267void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
3268{
3269 int len, count, i;
Jason Wangeaed4832015-04-23 14:21:38 +08003270 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003271
3272 if (nb_args != 2) {
3273 return;
3274 }
3275
3276 len = strlen(str);
3277 readline_set_completion_index(rs, len);
3278 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_OPTIONS_KIND_NIC,
Jason Wangeaed4832015-04-23 14:21:38 +08003279 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003280 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003281 QemuOpts *opts;
3282 const char *name = ncs[i]->name;
3283 if (strncmp(str, name, len)) {
3284 continue;
3285 }
3286 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
3287 if (opts) {
3288 readline_add_completion(rs, name);
3289 }
3290 }
3291}
3292
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003293void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
3294{
3295 size_t len;
3296
3297 len = strlen(str);
3298 readline_set_completion_index(rs, len);
3299 if (nb_args == 2) {
3300 TraceEventID id;
3301 for (id = 0; id < trace_event_count(); id++) {
3302 const char *event_name = trace_event_get_name(trace_event_id(id));
3303 if (!strncmp(str, event_name, len)) {
3304 readline_add_completion(rs, event_name);
3305 }
3306 }
3307 } else if (nb_args == 3) {
3308 add_completion_option(rs, str, "on");
3309 add_completion_option(rs, str, "off");
3310 }
3311}
3312
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003313void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
3314{
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003315 int i;
3316
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003317 if (nb_args != 2) {
3318 return;
3319 }
3320 readline_set_completion_index(rs, strlen(str));
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003321 for (i = 0; WatchdogExpirationAction_lookup[i]; i++) {
3322 add_completion_option(rs, str, WatchdogExpirationAction_lookup[i]);
3323 }
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003324}
3325
Hani Benhabilesc68a0402014-05-27 23:39:32 +01003326void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
3327 const char *str)
3328{
3329 size_t len;
3330
3331 len = strlen(str);
3332 readline_set_completion_index(rs, len);
3333 if (nb_args == 2) {
3334 int i;
Eric Blake7fb1cf12015-11-18 01:52:57 -07003335 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
Hani Benhabilesc68a0402014-05-27 23:39:32 +01003336 const char *name = MigrationCapability_lookup[i];
3337 if (!strncmp(str, name, len)) {
3338 readline_add_completion(rs, name);
3339 }
3340 }
3341 } else if (nb_args == 3) {
3342 add_completion_option(rs, str, "on");
3343 add_completion_option(rs, str, "off");
3344 }
3345}
3346
Liang Li50e9a622015-03-23 16:32:29 +08003347void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
3348 const char *str)
3349{
3350 size_t len;
3351
3352 len = strlen(str);
3353 readline_set_completion_index(rs, len);
3354 if (nb_args == 2) {
3355 int i;
Eric Blake7fb1cf12015-11-18 01:52:57 -07003356 for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
Liang Li50e9a622015-03-23 16:32:29 +08003357 const char *name = MigrationParameter_lookup[i];
3358 if (!strncmp(str, name, len)) {
3359 readline_add_completion(rs, name);
3360 }
3361 }
3362 }
3363}
3364
Hani Benhabilese3bb5322014-05-27 23:39:34 +01003365void host_net_add_completion(ReadLineState *rs, int nb_args, const char *str)
3366{
3367 int i;
3368 size_t len;
3369 if (nb_args != 2) {
3370 return;
3371 }
3372 len = strlen(str);
3373 readline_set_completion_index(rs, len);
3374 for (i = 0; host_net_devices[i]; i++) {
3375 if (!strncmp(host_net_devices[i], str, len)) {
3376 readline_add_completion(rs, host_net_devices[i]);
3377 }
3378 }
3379}
3380
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003381void host_net_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3382{
Jason Wangeaed4832015-04-23 14:21:38 +08003383 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003384 int count, i, len;
3385
3386 len = strlen(str);
3387 readline_set_completion_index(rs, len);
3388 if (nb_args == 2) {
3389 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08003390 NET_CLIENT_OPTIONS_KIND_NONE,
3391 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003392 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003393 int id;
3394 char name[16];
3395
3396 if (net_hub_id_for_client(ncs[i], &id)) {
3397 continue;
3398 }
3399 snprintf(name, sizeof(name), "%d", id);
3400 if (!strncmp(str, name, len)) {
3401 readline_add_completion(rs, name);
3402 }
3403 }
3404 return;
3405 } else if (nb_args == 3) {
3406 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08003407 NET_CLIENT_OPTIONS_KIND_NIC,
3408 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003409 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Jason Wang2c4681f2015-02-02 15:06:38 +08003410 int id;
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003411 const char *name;
3412
Jason Wang2c4681f2015-02-02 15:06:38 +08003413 if (ncs[i]->info->type == NET_CLIENT_OPTIONS_KIND_HUBPORT ||
3414 net_hub_id_for_client(ncs[i], &id)) {
3415 continue;
3416 }
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003417 name = ncs[i]->name;
3418 if (!strncmp(str, name, len)) {
3419 readline_add_completion(rs, name);
3420 }
3421 }
3422 return;
3423 }
3424}
3425
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003426static void vm_completion(ReadLineState *rs, const char *str)
3427{
3428 size_t len;
3429 BlockDriverState *bs = NULL;
3430
3431 len = strlen(str);
3432 readline_set_completion_index(rs, len);
3433 while ((bs = bdrv_next(bs))) {
3434 SnapshotInfoList *snapshots, *snapshot;
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003435 AioContext *ctx = bdrv_get_aio_context(bs);
3436 bool ok = false;
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003437
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003438 aio_context_acquire(ctx);
3439 if (bdrv_can_snapshot(bs)) {
3440 ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0;
3441 }
3442 aio_context_release(ctx);
3443 if (!ok) {
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003444 continue;
3445 }
Denis V. Lunev6bf1faa2015-11-04 20:19:42 +03003446
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003447 snapshot = snapshots;
3448 while (snapshot) {
3449 char *completion = snapshot->value->name;
3450 if (!strncmp(str, completion, len)) {
3451 readline_add_completion(rs, completion);
3452 }
3453 completion = snapshot->value->id;
3454 if (!strncmp(str, completion, len)) {
3455 readline_add_completion(rs, completion);
3456 }
3457 snapshot = snapshot->next;
3458 }
3459 qapi_free_SnapshotInfoList(snapshots);
3460 }
3461
3462}
3463
3464void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
3465{
3466 if (nb_args == 2) {
3467 vm_completion(rs, str);
3468 }
3469}
3470
3471void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
3472{
3473 if (nb_args == 2) {
3474 vm_completion(rs, str);
3475 }
3476}
3477
Wenchao Xiac35b6402013-08-27 20:38:24 +08003478static void monitor_find_completion_by_table(Monitor *mon,
3479 const mon_cmd_t *cmd_table,
3480 char **args,
3481 int nb_args)
bellard81d09122004-07-14 17:21:37 +00003482{
3483 const char *cmdname;
Wenchao Xiac35b6402013-08-27 20:38:24 +08003484 int i;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003485 const char *ptype, *str, *name;
Anthony Liguoric227f092009-10-01 16:12:16 -05003486 const mon_cmd_t *cmd;
Max Reitzda27a002016-03-16 19:54:29 +01003487 BlockBackend *blk = NULL;
bellard81d09122004-07-14 17:21:37 +00003488
bellard81d09122004-07-14 17:21:37 +00003489 if (nb_args <= 1) {
3490 /* command completion */
3491 if (nb_args == 0)
3492 cmdname = "";
3493 else
3494 cmdname = args[0];
Wenchao Xiad2674b22013-08-27 20:38:16 +08003495 readline_set_completion_index(mon->rs, strlen(cmdname));
Wenchao Xiac35b6402013-08-27 20:38:24 +08003496 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003497 cmd_completion(mon, cmdname, cmd->name);
bellard81d09122004-07-14 17:21:37 +00003498 }
3499 } else {
3500 /* find the command */
Wenchao Xiac35b6402013-08-27 20:38:24 +08003501 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Jan Kiszka03a63482010-06-16 00:38:33 +02003502 if (compare_cmd(args[0], cmd->name)) {
3503 break;
3504 }
bellard81d09122004-07-14 17:21:37 +00003505 }
Jan Kiszka03a63482010-06-16 00:38:33 +02003506 if (!cmd->name) {
Wenchao Xiac35b6402013-08-27 20:38:24 +08003507 return;
Jan Kiszka03a63482010-06-16 00:38:33 +02003508 }
3509
Wenchao Xiad903a772013-08-27 20:38:25 +08003510 if (cmd->sub_table) {
3511 /* do the job again */
Stefan Weile7ae7712015-03-08 19:30:01 +01003512 monitor_find_completion_by_table(mon, cmd->sub_table,
3513 &args[1], nb_args - 1);
3514 return;
Wenchao Xiad903a772013-08-27 20:38:25 +08003515 }
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003516 if (cmd->command_completion) {
Stefan Weile7ae7712015-03-08 19:30:01 +01003517 cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
3518 return;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003519 }
Wenchao Xiad903a772013-08-27 20:38:25 +08003520
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003521 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003522 for(i = 0; i < nb_args - 2; i++) {
3523 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003524 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003525 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003526 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003527 }
3528 }
3529 str = args[nb_args - 1];
Kevin Wolf48fe86f2014-11-12 16:24:02 +01003530 while (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02003531 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00003532 }
bellard81d09122004-07-14 17:21:37 +00003533 switch(*ptype) {
3534 case 'F':
3535 /* file completion */
Wenchao Xiad2674b22013-08-27 20:38:16 +08003536 readline_set_completion_index(mon->rs, strlen(str));
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003537 file_completion(mon, str);
bellard81d09122004-07-14 17:21:37 +00003538 break;
3539 case 'B':
3540 /* block device name completion */
Wenchao Xia599a9262013-08-27 20:38:15 +08003541 readline_set_completion_index(mon->rs, strlen(str));
Max Reitzda27a002016-03-16 19:54:29 +01003542 while ((blk = blk_next(blk)) != NULL) {
3543 name = blk_name(blk);
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003544 if (str[0] == '\0' ||
3545 !strncmp(name, str, strlen(str))) {
3546 readline_add_completion(mon->rs, name);
3547 }
3548 }
bellard81d09122004-07-14 17:21:37 +00003549 break;
bellard7fe48482004-10-09 18:08:01 +00003550 case 's':
Wenchao Xia129be002013-08-27 20:38:26 +08003551 case 'S':
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003552 if (!strcmp(cmd->name, "help|?")) {
Wenchao Xia7ca0e062013-08-27 20:38:27 +08003553 monitor_find_completion_by_table(mon, cmd_table,
3554 &args[1], nb_args - 1);
bellard7fe48482004-10-09 18:08:01 +00003555 }
3556 break;
bellard81d09122004-07-14 17:21:37 +00003557 default:
3558 break;
3559 }
3560 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08003561}
3562
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003563static void monitor_find_completion(void *opaque,
Wenchao Xiac35b6402013-08-27 20:38:24 +08003564 const char *cmdline)
3565{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003566 Monitor *mon = opaque;
Wenchao Xiac35b6402013-08-27 20:38:24 +08003567 char *args[MAX_ARGS];
3568 int nb_args, len;
3569
3570 /* 1. parse the cmdline */
3571 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
3572 return;
3573 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08003574
3575 /* if the line ends with a space, it means we want to complete the
3576 next arg */
3577 len = strlen(cmdline);
3578 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
3579 if (nb_args >= MAX_ARGS) {
3580 goto cleanup;
3581 }
3582 args[nb_args++] = g_strdup("");
3583 }
3584
3585 /* 2. auto complete according to args */
3586 monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args);
Jan Kiszka03a63482010-06-16 00:38:33 +02003587
3588cleanup:
Wenchao Xiadcc70cd2013-08-27 20:38:22 +08003589 free_cmdline_args(args, nb_args);
bellard81d09122004-07-14 17:21:37 +00003590}
3591
aliguori731b0362009-03-05 23:01:42 +00003592static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003593{
aliguori731b0362009-03-05 23:01:42 +00003594 Monitor *mon = opaque;
3595
Jan Kiszkac62313b2009-12-04 14:05:29 +01003596 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003597}
3598
Markus Armbruster40861822015-05-29 10:27:16 +02003599static bool invalid_qmp_mode(const Monitor *mon, const mon_cmd_t *cmd,
3600 Error **errp)
Luiz Capitulino09069b12010-02-04 18:10:06 -02003601{
Markus Armbruster485febc2015-03-13 17:25:50 +01003602 bool is_cap = cmd->mhandler.cmd_new == qmp_capabilities;
Markus Armbrusterf994b252015-03-06 19:51:51 +01003603
3604 if (is_cap && mon->qmp.in_command_mode) {
Markus Armbruster40861822015-05-29 10:27:16 +02003605 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
3606 "Capabilities negotiation is already complete, command "
3607 "'%s' ignored", cmd->name);
Eric Blake2d5a8342015-04-15 09:19:23 -06003608 return true;
3609 }
Markus Armbrusterf994b252015-03-06 19:51:51 +01003610 if (!is_cap && !mon->qmp.in_command_mode) {
Markus Armbruster40861822015-05-29 10:27:16 +02003611 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
3612 "Expecting capabilities negotiation with "
3613 "'qmp_capabilities' before command '%s'", cmd->name);
Eric Blake2d5a8342015-04-15 09:19:23 -06003614 return true;
3615 }
3616 return false;
Luiz Capitulino09069b12010-02-04 18:10:06 -02003617}
3618
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003619/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03003620 * Argument validation rules:
3621 *
3622 * 1. The argument must exist in cmd_args qdict
3623 * 2. The argument type must be the expected one
3624 *
3625 * Special case: If the argument doesn't exist in cmd_args and
3626 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
3627 * checking is skipped for it.
3628 */
Markus Armbruster326283a2015-03-02 18:39:09 +01003629static void check_client_args_type(const QDict *client_args,
3630 const QDict *cmd_args, int flags,
3631 Error **errp)
Luiz Capitulino4af91932010-06-22 11:44:05 -03003632{
3633 const QDictEntry *ent;
3634
3635 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
3636 QObject *obj;
3637 QString *arg_type;
3638 const QObject *client_arg = qdict_entry_value(ent);
3639 const char *client_arg_name = qdict_entry_key(ent);
3640
3641 obj = qdict_get(cmd_args, client_arg_name);
3642 if (!obj) {
3643 if (flags & QMP_ACCEPT_UNKNOWNS) {
3644 /* handler accepts unknowns */
3645 continue;
3646 }
3647 /* client arg doesn't exist */
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003648 error_setg(errp, QERR_INVALID_PARAMETER, client_arg_name);
Markus Armbruster326283a2015-03-02 18:39:09 +01003649 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003650 }
3651
3652 arg_type = qobject_to_qstring(obj);
3653 assert(arg_type != NULL);
3654
3655 /* check if argument's type is correct */
3656 switch (qstring_get_str(arg_type)[0]) {
3657 case 'F':
3658 case 'B':
3659 case 's':
3660 if (qobject_type(client_arg) != QTYPE_QSTRING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003661 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3662 client_arg_name, "string");
Markus Armbruster326283a2015-03-02 18:39:09 +01003663 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003664 }
3665 break;
3666 case 'i':
3667 case 'l':
3668 case 'M':
Jes Sorensendbc0c672010-10-21 17:15:47 +02003669 case 'o':
Luiz Capitulino4af91932010-06-22 11:44:05 -03003670 if (qobject_type(client_arg) != QTYPE_QINT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003671 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3672 client_arg_name, "int");
Markus Armbruster326283a2015-03-02 18:39:09 +01003673 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003674 }
3675 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003676 case 'T':
3677 if (qobject_type(client_arg) != QTYPE_QINT &&
3678 qobject_type(client_arg) != QTYPE_QFLOAT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003679 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3680 client_arg_name, "number");
Markus Armbruster326283a2015-03-02 18:39:09 +01003681 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003682 }
3683 break;
3684 case 'b':
3685 case '-':
3686 if (qobject_type(client_arg) != QTYPE_QBOOL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003687 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3688 client_arg_name, "bool");
Markus Armbruster326283a2015-03-02 18:39:09 +01003689 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003690 }
3691 break;
3692 case 'O':
3693 assert(flags & QMP_ACCEPT_UNKNOWNS);
3694 break;
Paolo Bonzinib9f89782012-03-22 12:51:11 +01003695 case 'q':
3696 /* Any QObject can be passed. */
3697 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003698 case '/':
3699 case '.':
3700 /*
3701 * These types are not supported by QMP and thus are not
3702 * handled here. Fall through.
3703 */
3704 default:
3705 abort();
3706 }
3707 }
Luiz Capitulino4af91932010-06-22 11:44:05 -03003708}
3709
3710/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003711 * - Check if the client has passed all mandatory args
3712 * - Set special flags for argument validation
3713 */
Markus Armbruster326283a2015-03-02 18:39:09 +01003714static void check_mandatory_args(const QDict *cmd_args,
3715 const QDict *client_args, int *flags,
3716 Error **errp)
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003717{
3718 const QDictEntry *ent;
3719
3720 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
3721 const char *cmd_arg_name = qdict_entry_key(ent);
3722 QString *type = qobject_to_qstring(qdict_entry_value(ent));
3723 assert(type != NULL);
3724
3725 if (qstring_get_str(type)[0] == 'O') {
3726 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
3727 *flags |= QMP_ACCEPT_UNKNOWNS;
3728 } else if (qstring_get_str(type)[0] != '-' &&
3729 qstring_get_str(type)[1] != '?' &&
3730 !qdict_haskey(client_args, cmd_arg_name)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003731 error_setg(errp, QERR_MISSING_PARAMETER, cmd_arg_name);
Markus Armbruster326283a2015-03-02 18:39:09 +01003732 return;
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003733 }
3734 }
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003735}
3736
3737static QDict *qdict_from_args_type(const char *args_type)
3738{
3739 int i;
3740 QDict *qdict;
3741 QString *key, *type, *cur_qs;
3742
3743 assert(args_type != NULL);
3744
3745 qdict = qdict_new();
3746
3747 if (args_type == NULL || args_type[0] == '\0') {
3748 /* no args, empty qdict */
3749 goto out;
3750 }
3751
3752 key = qstring_new();
3753 type = qstring_new();
3754
3755 cur_qs = key;
3756
3757 for (i = 0;; i++) {
3758 switch (args_type[i]) {
3759 case ',':
3760 case '\0':
3761 qdict_put(qdict, qstring_get_str(key), type);
3762 QDECREF(key);
3763 if (args_type[i] == '\0') {
3764 goto out;
3765 }
3766 type = qstring_new(); /* qdict has ref */
3767 cur_qs = key = qstring_new();
3768 break;
3769 case ':':
3770 cur_qs = type;
3771 break;
3772 default:
3773 qstring_append_chr(cur_qs, args_type[i]);
3774 break;
3775 }
3776 }
3777
3778out:
3779 return qdict;
3780}
3781
3782/*
3783 * Client argument checking rules:
3784 *
3785 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03003786 * 2. Each argument provided by the client must be expected
3787 * 3. Each argument provided by the client must have the type expected
3788 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003789 */
Markus Armbruster326283a2015-03-02 18:39:09 +01003790static void qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args,
3791 Error **errp)
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003792{
Markus Armbruster326283a2015-03-02 18:39:09 +01003793 Error *err = NULL;
3794 int flags;
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003795 QDict *cmd_args;
3796
3797 cmd_args = qdict_from_args_type(cmd->args_type);
3798
3799 flags = 0;
Markus Armbruster326283a2015-03-02 18:39:09 +01003800 check_mandatory_args(cmd_args, client_args, &flags, &err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003801 if (err) {
3802 goto out;
3803 }
3804
Markus Armbruster326283a2015-03-02 18:39:09 +01003805 check_client_args_type(client_args, cmd_args, flags, &err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003806
3807out:
Markus Armbruster326283a2015-03-02 18:39:09 +01003808 error_propagate(errp, err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003809 QDECREF(cmd_args);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003810}
3811
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003812/*
3813 * Input object checking rules
3814 *
3815 * 1. Input object must be a dict
3816 * 2. The "execute" key must exist
3817 * 3. The "execute" key must be a string
3818 * 4. If the "arguments" key exists, it must be a dict
3819 * 5. If the "id" key exists, it can be anything (ie. json-value)
3820 * 6. Any argument not listed above is considered invalid
3821 */
Markus Armbrusterba0510a2015-03-02 18:41:43 +01003822static QDict *qmp_check_input_obj(QObject *input_obj, Error **errp)
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003823{
3824 const QDictEntry *ent;
3825 int has_exec_key = 0;
3826 QDict *input_dict;
3827
3828 if (qobject_type(input_obj) != QTYPE_QDICT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003829 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "object");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003830 return NULL;
3831 }
3832
3833 input_dict = qobject_to_qdict(input_obj);
3834
3835 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
3836 const char *arg_name = qdict_entry_key(ent);
3837 const QObject *arg_obj = qdict_entry_value(ent);
3838
3839 if (!strcmp(arg_name, "execute")) {
3840 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003841 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
3842 "execute", "string");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003843 return NULL;
3844 }
3845 has_exec_key = 1;
3846 } else if (!strcmp(arg_name, "arguments")) {
3847 if (qobject_type(arg_obj) != QTYPE_QDICT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003848 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
3849 "arguments", "object");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003850 return NULL;
3851 }
Markus Armbruster779cec42015-06-08 10:44:30 +02003852 } else if (!strcmp(arg_name, "id")) {
3853 /* Any string is acceptable as "id", so nothing to check */
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003854 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003855 error_setg(errp, QERR_QMP_EXTRA_MEMBER, arg_name);
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003856 return NULL;
3857 }
3858 }
3859
3860 if (!has_exec_key) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003861 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "execute");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003862 return NULL;
3863 }
3864
3865 return input_dict;
3866}
3867
Paolo Bonzini95385fe2015-11-25 22:23:31 +01003868static void handle_qmp_command(JSONMessageParser *parser, GQueue *tokens)
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003869{
Markus Armbruster326283a2015-03-02 18:39:09 +01003870 Error *local_err = NULL;
Markus Armbruster84add862015-03-05 16:45:15 +01003871 QObject *obj, *data;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003872 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003873 const mon_cmd_t *cmd;
Luiz Capitulino40e5a012011-10-21 16:15:31 -02003874 const char *cmd_name;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003875 Monitor *mon = cur_mon;
3876
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03003877 args = input = NULL;
Markus Armbruster84add862015-03-05 16:45:15 +01003878 data = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003879
3880 obj = json_parser_parse(tokens, NULL);
3881 if (!obj) {
3882 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003883 error_setg(&local_err, QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003884 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003885 }
3886
Markus Armbrusterba0510a2015-03-02 18:41:43 +01003887 input = qmp_check_input_obj(obj, &local_err);
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003888 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003889 qobject_decref(obj);
3890 goto err_out;
3891 }
3892
Markus Armbruster74358f22015-03-06 19:35:59 +01003893 mon->qmp.id = qdict_get(input, "id");
3894 qobject_incref(mon->qmp.id);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003895
Luiz Capitulino0bbab462010-05-31 17:32:50 -03003896 cmd_name = qdict_get_str(input, "execute");
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +01003897 trace_handle_qmp_command(mon, cmd_name);
Anthony Liguorie3193602011-09-02 12:34:47 -05003898 cmd = qmp_find_cmd(cmd_name);
Eric Blake2d5a8342015-04-15 09:19:23 -06003899 if (!cmd) {
Markus Armbruster710aec92015-03-06 11:28:00 +01003900 error_set(&local_err, ERROR_CLASS_COMMAND_NOT_FOUND,
3901 "The command %s has not been found", cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03003902 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003903 }
Markus Armbruster40861822015-05-29 10:27:16 +02003904 if (invalid_qmp_mode(mon, cmd, &local_err)) {
Eric Blake2d5a8342015-04-15 09:19:23 -06003905 goto err_out;
3906 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003907
3908 obj = qdict_get(input, "arguments");
3909 if (!obj) {
3910 args = qdict_new();
3911 } else {
3912 args = qobject_to_qdict(obj);
3913 QINCREF(args);
3914 }
3915
Markus Armbruster326283a2015-03-02 18:39:09 +01003916 qmp_check_client_args(cmd, args, &local_err);
3917 if (local_err) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003918 goto err_out;
3919 }
3920
Markus Armbruster485febc2015-03-13 17:25:50 +01003921 cmd->mhandler.cmd_new(args, &data, &local_err);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003922
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003923err_out:
Markus Armbruster710aec92015-03-06 11:28:00 +01003924 monitor_protocol_emitter(mon, data, local_err);
Markus Armbruster84add862015-03-05 16:45:15 +01003925 qobject_decref(data);
Markus Armbruster3a81a102015-10-29 12:15:09 +01003926 error_free(local_err);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03003927 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003928 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003929}
3930
Markus Armbrusterc83fe232015-03-06 19:20:51 +01003931static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003932{
3933 Monitor *old_mon = cur_mon;
3934
3935 cur_mon = opaque;
3936
Markus Armbruster74358f22015-03-06 19:35:59 +01003937 json_message_parser_feed(&cur_mon->qmp.parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003938
3939 cur_mon = old_mon;
3940}
3941
aliguori731b0362009-03-05 23:01:42 +00003942static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00003943{
aliguori731b0362009-03-05 23:01:42 +00003944 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00003945 int i;
aliguori376253e2009-03-05 23:01:23 +00003946
aliguori731b0362009-03-05 23:01:42 +00003947 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00003948
aliguoricde76ee2009-03-05 23:01:51 +00003949 if (cur_mon->rs) {
3950 for (i = 0; i < size; i++)
3951 readline_handle_byte(cur_mon->rs, buf[i]);
3952 } else {
3953 if (size == 0 || buf[size - 1] != 0)
3954 monitor_printf(cur_mon, "corrupted command\n");
3955 else
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003956 handle_hmp_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00003957 }
aliguori731b0362009-03-05 23:01:42 +00003958
3959 cur_mon = old_mon;
3960}
aliguorid8f44602008-10-06 13:52:44 +00003961
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003962static void monitor_command_cb(void *opaque, const char *cmdline,
3963 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00003964{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003965 Monitor *mon = opaque;
3966
aliguori731b0362009-03-05 23:01:42 +00003967 monitor_suspend(mon);
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003968 handle_hmp_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00003969 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00003970}
3971
aliguoricde76ee2009-03-05 23:01:51 +00003972int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003973{
aliguoricde76ee2009-03-05 23:01:51 +00003974 if (!mon->rs)
3975 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00003976 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00003977 return 0;
aliguorid8f44602008-10-06 13:52:44 +00003978}
3979
aliguori376253e2009-03-05 23:01:23 +00003980void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003981{
aliguoricde76ee2009-03-05 23:01:51 +00003982 if (!mon->rs)
3983 return;
aliguori731b0362009-03-05 23:01:42 +00003984 if (--mon->suspend_cnt == 0)
3985 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00003986}
3987
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003988static QObject *get_qmp_greeting(void)
3989{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03003990 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003991
Markus Armbruster7fad30f2015-09-16 13:06:19 +02003992 qmp_marshal_query_version(NULL, &ver, NULL);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003993 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
3994}
3995
Markus Armbrusterc83fe232015-03-06 19:20:51 +01003996static void monitor_qmp_event(void *opaque, int event)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003997{
Luiz Capitulino47116d12010-02-08 17:01:30 -02003998 QObject *data;
3999 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004000
Luiz Capitulino47116d12010-02-08 17:01:30 -02004001 switch (event) {
4002 case CHR_EVENT_OPENED:
Markus Armbrusterf994b252015-03-06 19:51:51 +01004003 mon->qmp.in_command_mode = false;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004004 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004005 monitor_json_emitter(mon, data);
4006 qobject_decref(data);
Corey Bryantefb87c12012-08-14 16:43:48 -04004007 mon_refcount++;
Luiz Capitulino47116d12010-02-08 17:01:30 -02004008 break;
4009 case CHR_EVENT_CLOSED:
Markus Armbruster74358f22015-03-06 19:35:59 +01004010 json_message_parser_destroy(&mon->qmp.parser);
4011 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Corey Bryantefb87c12012-08-14 16:43:48 -04004012 mon_refcount--;
4013 monitor_fdsets_cleanup();
Luiz Capitulino47116d12010-02-08 17:01:30 -02004014 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004015 }
4016}
4017
aliguori731b0362009-03-05 23:01:42 +00004018static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004019{
aliguori376253e2009-03-05 23:01:23 +00004020 Monitor *mon = opaque;
4021
aliguori2724b182009-03-05 23:01:47 +00004022 switch (event) {
4023 case CHR_EVENT_MUX_IN:
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02004024 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004025 mon->mux_out = 0;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02004026 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004027 if (mon->reset_seen) {
4028 readline_restart(mon->rs);
4029 monitor_resume(mon);
4030 monitor_flush(mon);
4031 } else {
4032 mon->suspend_cnt = 0;
4033 }
aliguori2724b182009-03-05 23:01:47 +00004034 break;
ths86e94de2007-01-05 22:01:59 +00004035
aliguori2724b182009-03-05 23:01:47 +00004036 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004037 if (mon->reset_seen) {
4038 if (mon->suspend_cnt == 0) {
4039 monitor_printf(mon, "\n");
4040 }
4041 monitor_flush(mon);
4042 monitor_suspend(mon);
4043 } else {
4044 mon->suspend_cnt++;
4045 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02004046 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004047 mon->mux_out = 1;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02004048 qemu_mutex_unlock(&mon->out_lock);
aliguori2724b182009-03-05 23:01:47 +00004049 break;
4050
Amit Shahb6b8df52009-10-07 18:31:16 +05304051 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004052 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4053 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004054 if (!mon->mux_out) {
Stratos Psomadakise5554e22014-09-15 15:34:57 +03004055 readline_restart(mon->rs);
aliguori2724b182009-03-05 23:01:47 +00004056 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004057 }
4058 mon->reset_seen = 1;
Corey Bryantefb87c12012-08-14 16:43:48 -04004059 mon_refcount++;
4060 break;
4061
4062 case CHR_EVENT_CLOSED:
4063 mon_refcount--;
4064 monitor_fdsets_cleanup();
aliguori2724b182009-03-05 23:01:47 +00004065 break;
4066 }
ths86e94de2007-01-05 22:01:59 +00004067}
4068
Wayne Xia816f8922011-10-12 11:32:41 +08004069static int
4070compare_mon_cmd(const void *a, const void *b)
4071{
4072 return strcmp(((const mon_cmd_t *)a)->name,
4073 ((const mon_cmd_t *)b)->name);
4074}
4075
4076static void sortcmdlist(void)
4077{
4078 int array_num;
4079 int elem_size = sizeof(mon_cmd_t);
4080
4081 array_num = sizeof(mon_cmds)/elem_size-1;
4082 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
4083
4084 array_num = sizeof(info_cmds)/elem_size-1;
4085 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
4086}
4087
aliguori76655d62009-03-06 20:27:37 +00004088
4089/*
4090 * Local variables:
4091 * c-indent-level: 4
4092 * c-basic-offset: 4
4093 * tab-width: 8
4094 * End:
4095 */
4096
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004097/* These functions just adapt the readline interface in a typesafe way. We
4098 * could cast function pointers but that discards compiler checks.
4099 */
Stefan Weild5d15072014-01-25 18:18:23 +01004100static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
4101 const char *fmt, ...)
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004102{
4103 va_list ap;
4104 va_start(ap, fmt);
4105 monitor_vprintf(opaque, fmt, ap);
4106 va_end(ap);
4107}
4108
4109static void monitor_readline_flush(void *opaque)
4110{
4111 monitor_flush(opaque);
4112}
4113
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004114static void __attribute__((constructor)) monitor_lock_init(void)
4115{
4116 qemu_mutex_init(&monitor_lock);
4117}
4118
aliguori731b0362009-03-05 23:01:42 +00004119void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004120{
aliguori731b0362009-03-05 23:01:42 +00004121 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004122 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004123
4124 if (is_first_init) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +02004125 monitor_qapi_event_init();
Wenchao Xiad0383172013-08-27 20:38:18 +08004126 sortcmdlist();
ths20d8a3e2007-02-18 17:04:49 +00004127 is_first_init = 0;
4128 }
aliguori87127162009-03-05 23:01:29 +00004129
Wenchao Xiab01fe892013-08-27 20:38:19 +08004130 mon = g_malloc(sizeof(*mon));
4131 monitor_data_init(mon);
ths20d8a3e2007-02-18 17:04:49 +00004132
aliguori87127162009-03-05 23:01:29 +00004133 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00004134 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004135 if (flags & MONITOR_USE_READLINE) {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004136 mon->rs = readline_init(monitor_readline_printf,
4137 monitor_readline_flush,
4138 mon,
4139 monitor_find_completion);
aliguoricde76ee2009-03-05 23:01:51 +00004140 monitor_read_command(mon, 0);
4141 }
aliguori87127162009-03-05 23:01:29 +00004142
Markus Armbruster9f3982f2015-03-06 19:56:38 +01004143 if (monitor_is_qmp(mon)) {
Markus Armbrusterc83fe232015-03-06 19:20:51 +01004144 qemu_chr_add_handlers(chr, monitor_can_read, monitor_qmp_read,
4145 monitor_qmp_event, mon);
Anthony Liguori15f31512011-08-15 11:17:35 -05004146 qemu_chr_fe_set_echo(chr, true);
Markus Armbruster74358f22015-03-06 19:35:59 +01004147 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004148 } else {
4149 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4150 monitor_event, mon);
4151 }
aliguori87127162009-03-05 23:01:29 +00004152
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004153 qemu_mutex_lock(&monitor_lock);
Blue Swirl72cf2d42009-09-12 07:36:22 +00004154 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004155 qemu_mutex_unlock(&monitor_lock);
bellard7e2515e2004-08-01 21:52:19 +00004156}
4157
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004158static void bdrv_password_cb(void *opaque, const char *password,
4159 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00004160{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004161 Monitor *mon = opaque;
4162 BlockDriverState *bs = readline_opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004163 int ret = 0;
Markus Armbruster4d2855a2015-01-29 10:37:00 +01004164 Error *local_err = NULL;
bellard7e2515e2004-08-01 21:52:19 +00004165
Markus Armbruster4d2855a2015-01-29 10:37:00 +01004166 bdrv_add_key(bs, password, &local_err);
4167 if (local_err) {
Markus Armbruster193227f2015-12-18 16:35:06 +01004168 error_report_err(local_err);
aliguoribb5fc202009-03-05 23:01:15 +00004169 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004170 }
aliguori731b0362009-03-05 23:01:42 +00004171 if (mon->password_completion_cb)
4172 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004173
aliguori731b0362009-03-05 23:01:42 +00004174 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004175}
aliguoric0f4ce72009-03-05 23:01:01 +00004176
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004177int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
Markus Armbruster097310b2014-10-07 13:59:15 +02004178 BlockCompletionFunc *completion_cb,
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004179 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004180{
aliguoricde76ee2009-03-05 23:01:51 +00004181 int err;
4182
aliguori376253e2009-03-05 23:01:23 +00004183 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4184 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00004185
aliguori731b0362009-03-05 23:01:42 +00004186 mon->password_completion_cb = completion_cb;
4187 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004188
aliguoricde76ee2009-03-05 23:01:51 +00004189 err = monitor_read_password(mon, bdrv_password_cb, bs);
4190
4191 if (err && completion_cb)
4192 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004193
4194 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00004195}
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004196
4197int monitor_read_block_device_key(Monitor *mon, const char *device,
Markus Armbruster097310b2014-10-07 13:59:15 +02004198 BlockCompletionFunc *completion_cb,
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004199 void *opaque)
4200{
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01004201 Error *err = NULL;
Fam Zheng55606252015-03-02 19:36:46 +08004202 BlockBackend *blk;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004203
Fam Zheng55606252015-03-02 19:36:46 +08004204 blk = blk_by_name(device);
4205 if (!blk) {
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004206 monitor_printf(mon, "Device not found %s\n", device);
4207 return -1;
4208 }
Max Reitz5433c242015-10-19 17:53:29 +02004209 if (!blk_bs(blk)) {
4210 monitor_printf(mon, "Device '%s' has no medium\n", device);
4211 return -1;
4212 }
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004213
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01004214 bdrv_add_key(blk_bs(blk), NULL, &err);
4215 if (err) {
4216 error_free(err);
4217 return monitor_read_bdrv_key_start(mon, blk_bs(blk), completion_cb, opaque);
4218 }
4219
4220 if (completion_cb) {
4221 completion_cb(opaque, 0);
4222 }
4223 return 0;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004224}
Paolo Bonzini4d454572012-11-26 16:03:42 +01004225
4226QemuOptsList qemu_mon_opts = {
4227 .name = "mon",
4228 .implied_opt_name = "chardev",
4229 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
4230 .desc = {
4231 {
4232 .name = "mode",
4233 .type = QEMU_OPT_STRING,
4234 },{
4235 .name = "chardev",
4236 .type = QEMU_OPT_STRING,
4237 },{
4238 .name = "default",
4239 .type = QEMU_OPT_BOOL,
4240 },{
4241 .name = "pretty",
4242 .type = QEMU_OPT_BOOL,
4243 },
4244 { /* end of list */ }
4245 },
4246};
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004247
4248#ifndef TARGET_I386
4249void qmp_rtc_reset_reinjection(Error **errp)
4250{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004251 error_setg(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection");
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004252}
4253#endif
Jason J. Herne7ee0c3e2015-06-26 14:03:16 -04004254
4255#ifndef TARGET_S390X
4256void qmp_dump_skeys(const char *filename, Error **errp)
4257{
4258 error_setg(errp, QERR_FEATURE_DISABLED, "dump-skeys");
4259}
4260#endif