blob: d0edb3bb15416bb7fc68aeecbf36084f0ddc9e2f [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 */
blueswir1511d2b12009-03-07 15:32:56 +000024#include <dirent.h>
pbrook87ecb682007-11-17 17:14:51 +000025#include "hw/hw.h"
Paolo Bonzinib4a42f82013-02-04 11:37:52 +010026#include "monitor/qdev.h"
pbrook87ecb682007-11-17 17:14:51 +000027#include "hw/usb.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010028#include "hw/i386/pc.h"
Michael S. Tsirkina2cb15b2012-12-12 14:24:50 +020029#include "hw/pci/pci.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010030#include "sysemu/watchdog.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +020031#include "hw/loader.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010032#include "exec/gdbstub.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020033#include "net/net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000034#include "net/slirp.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020035#include "sysemu/char.h"
Gerd Hoffmann75721502010-10-07 12:22:54 +020036#include "ui/qemu-spice.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010037#include "sysemu/sysemu.h"
Eduardo Habkoste35704b2015-02-08 16:51:16 -020038#include "sysemu/numa.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010039#include "monitor/monitor.h"
Stefan Hajnoczi0150cd82013-11-14 11:54:15 +010040#include "qemu/readline.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010041#include "ui/console.h"
Gerd Hoffmannc751a742013-12-04 15:02:28 +010042#include "ui/input.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010043#include "sysemu/blockdev.h"
pbrook87ecb682007-11-17 17:14:51 +000044#include "audio/audio.h"
Paolo Bonzini76cad712012-10-24 11:12:21 +020045#include "disas/disas.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010046#include "sysemu/balloon.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010047#include "qemu/timer.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010048#include "migration/migration.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010049#include "sysemu/kvm.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010050#include "qemu/acl.h"
Paolo Bonzinibdee56f2013-04-02 18:28:41 +020051#include "sysemu/tpm.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010052#include "qapi/qmp/qerror.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010053#include "qapi/qmp/qint.h"
54#include "qapi/qmp/qfloat.h"
55#include "qapi/qmp/qlist.h"
56#include "qapi/qmp/qbool.h"
57#include "qapi/qmp/qstring.h"
58#include "qapi/qmp/qjson.h"
59#include "qapi/qmp/json-streamer.h"
60#include "qapi/qmp/json-parser.h"
Hani Benhabiles1094fd32014-02-06 23:30:13 +010061#include <qom/object_interfaces.h>
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010062#include "qemu/osdep.h"
Blue Swirl2b41f102011-06-19 20:38:22 +000063#include "cpu.h"
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +010064#include "trace.h"
Lluís31965ae2011-08-31 20:31:24 +020065#include "trace/control.h"
Pavel Butsykinbf957282015-09-10 18:38:59 +030066#include "monitor/hmp-target.h"
Lluís6d8a7642011-08-31 20:30:43 +020067#ifdef CONFIG_TRACE_SIMPLE
Lluís31965ae2011-08-31 20:31:24 +020068#include "trace/simple.h"
Prerna Saxena22890ab2010-06-24 17:04:53 +053069#endif
Paolo Bonzini022c62c2012-12-17 18:19:49 +010070#include "exec/memory.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050071#include "qmp-commands.h"
72#include "hmp.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010073#include "qemu/thread.h"
Hani Benhabilesb21631f2014-05-27 23:39:37 +010074#include "block/qapi.h"
Wenchao Xia43a14cf2014-06-18 08:43:31 +020075#include "qapi/qmp-event.h"
76#include "qapi-event.h"
Markus Armbruster39a18152015-09-16 13:06:28 +020077#include "qmp-introspect.h"
Fam Zheng55606252015-03-02 19:36:46 +080078#include "sysemu/block-backend.h"
ths6a5bd302007-12-03 17:05:38 +000079
Markus Armbruster1ce6be22015-02-06 14:18:24 +010080/* for hmp_info_irq/pic */
Jan Kiszka661f1922011-10-16 11:53:13 +020081#if defined(TARGET_SPARC)
Paolo Bonzini0d09e412013-02-05 17:06:20 +010082#include "hw/sparc/sun4m.h"
Jan Kiszka661f1922011-10-16 11:53:13 +020083#endif
Paolo Bonzini0d09e412013-02-05 17:06:20 +010084#include "hw/lm32/lm32_pic.h"
Jan Kiszka661f1922011-10-16 11:53:13 +020085
Jason J. Hernea4538a52015-06-26 14:07:21 -040086#if defined(TARGET_S390X)
87#include "hw/s390x/storage-keys.h"
88#endif
89
bellard9307c4c2004-04-04 12:57:25 +000090/*
91 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000092 *
bellard9307c4c2004-04-04 12:57:25 +000093 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000094 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000095 * 's' string (accept optional quote)
Wenchao Xia129be002013-08-27 20:38:26 +080096 * 'S' it just appends the rest of the string (accept optional quote)
Markus Armbruster361127d2010-02-10 20:24:35 +010097 * 'O' option string of the form NAME=VALUE,...
98 * parsed according to QemuOptsList given by its name
99 * Example: 'device:O' uses qemu_device_opts.
100 * Restriction: only lists with empty desc are supported
101 * TODO lift the restriction
bellard92a31b12005-02-10 22:00:52 +0000102 * 'i' 32 bit integer
103 * 'l' target long (32 or 64 bit)
Luiz Capitulino91162842012-04-26 17:34:30 -0300104 * 'M' Non-negative target long (32 or 64 bit), in user mode the
105 * value is multiplied by 2^20 (think Mebibyte)
Jes Sorensendbc0c672010-10-21 17:15:47 +0200106 * 'o' octets (aka bytes)
Kevin Wolf5e009842013-06-05 14:19:27 +0200107 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
108 * K, k suffix, which multiplies the value by 2^60 for suffixes E
109 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
110 * 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 +0100111 * 'T' double
112 * user mode accepts an optional ms, us, ns suffix,
113 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +0000114 * '/' optional gdb-like print format (like "/10x")
115 *
Luiz Capitulinofb466602009-08-28 15:27:27 -0300116 * '?' optional type (for all types, except '/')
117 * '.' other form of optional type (for 'i' and 'l')
Markus Armbruster942cd1f2010-03-26 09:07:09 +0100118 * 'b' boolean
119 * user mode accepts "on" or "off"
Luiz Capitulinofb466602009-08-28 15:27:27 -0300120 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +0000121 *
122 */
123
Anthony Liguoric227f092009-10-01 16:12:16 -0500124typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +0000125 const char *name;
bellard9307c4c2004-04-04 12:57:25 +0000126 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +0000127 const char *params;
128 const char *help;
Luiz Capitulino910df892009-10-07 13:41:51 -0300129 union {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -0300130 void (*cmd)(Monitor *mon, const QDict *qdict);
Markus Armbruster485febc2015-03-13 17:25:50 +0100131 void (*cmd_new)(QDict *params, QObject **ret_data, Error **errp);
Luiz Capitulino910df892009-10-07 13:41:51 -0300132 } mhandler;
Wenchao Xia5f3d3352013-01-14 14:06:27 +0800133 /* @sub_table is a list of 2nd level of commands. If it do not exist,
134 * mhandler should be used. If it exist, sub_table[?].mhandler should be
135 * used, and mhandler of 1st level plays the role of help function.
136 */
137 struct mon_cmd_t *sub_table;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +0100138 void (*command_completion)(ReadLineState *rs, int nb_args, const char *str);
Anthony Liguoric227f092009-10-01 16:12:16 -0500139} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000140
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100141/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500142typedef struct mon_fd_t mon_fd_t;
143struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100144 char *name;
145 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500146 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100147};
148
Corey Bryantba1c0482012-08-14 16:43:43 -0400149/* file descriptor associated with a file descriptor set */
150typedef struct MonFdsetFd MonFdsetFd;
151struct MonFdsetFd {
152 int fd;
153 bool removed;
154 char *opaque;
155 QLIST_ENTRY(MonFdsetFd) next;
156};
157
158/* file descriptor set containing fds passed via SCM_RIGHTS */
159typedef struct MonFdset MonFdset;
160struct MonFdset {
161 int64_t id;
162 QLIST_HEAD(, MonFdsetFd) fds;
Corey Bryantadb696f2012-08-14 16:43:47 -0400163 QLIST_HEAD(, MonFdsetFd) dup_fds;
Corey Bryantba1c0482012-08-14 16:43:43 -0400164 QLIST_ENTRY(MonFdset) next;
165};
166
Markus Armbruster74358f22015-03-06 19:35:59 +0100167typedef struct {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200168 QObject *id;
169 JSONMessageParser parser;
Markus Armbrusterf994b252015-03-06 19:51:51 +0100170 /*
171 * When a client connects, we're in capabilities negotiation mode.
172 * When command qmp_capabilities succeeds, we go into command
173 * mode.
174 */
175 bool in_command_mode; /* are we in command mode? */
Markus Armbruster74358f22015-03-06 19:35:59 +0100176} MonitorQMP;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200177
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100178/*
179 * To prevent flooding clients, events can be throttled. The
180 * throttling is calculated globally, rather than per-Monitor
181 * instance.
182 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200183typedef struct MonitorQAPIEventState {
184 QAPIEvent event; /* Event being tracked */
185 int64_t rate; /* Minimum time (in ns) between two events */
186 int64_t last; /* QEMU_CLOCK_REALTIME value at last emission */
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100187 QEMUTimer *timer; /* Timer for handling delayed events */
188 QObject *data; /* Event pending delayed dispatch */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200189} MonitorQAPIEventState;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100190
aliguori87127162009-03-05 23:01:29 +0000191struct Monitor {
192 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200193 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000194 int flags;
195 int suspend_cnt;
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400196 bool skip_flush;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200197
198 QemuMutex out_lock;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400199 QString *outbuf;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200200 guint out_watch;
201
202 /* Read under either BQL or out_lock, written with BQL+out_lock. */
203 int mux_out;
204
aliguori731b0362009-03-05 23:01:42 +0000205 ReadLineState *rs;
Markus Armbruster74358f22015-03-06 19:35:59 +0100206 MonitorQMP qmp;
Andreas Färbercb446ec2013-05-01 14:24:52 +0200207 CPUState *mon_cpu;
Markus Armbruster097310b2014-10-07 13:59:15 +0200208 BlockCompletionFunc *password_completion_cb;
aliguori731b0362009-03-05 23:01:42 +0000209 void *password_opaque;
Wenchao Xia77172392013-08-27 20:38:20 +0800210 mon_cmd_t *cmd_table;
Anthony Liguoric227f092009-10-01 16:12:16 -0500211 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000212 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000213};
214
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300215/* QMP checker flags */
216#define QMP_ACCEPT_UNKNOWNS 1
217
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200218/* Protects mon_list, monitor_event_state. */
219static QemuMutex monitor_lock;
220
Blue Swirl72cf2d42009-09-12 07:36:22 +0000221static QLIST_HEAD(mon_list, Monitor) mon_list;
Corey Bryantba1c0482012-08-14 16:43:43 -0400222static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
Corey Bryantefb87c12012-08-14 16:43:48 -0400223static int mon_refcount;
bellard7e2515e2004-08-01 21:52:19 +0000224
Wayne Xia816f8922011-10-12 11:32:41 +0800225static mon_cmd_t mon_cmds[];
226static mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000227
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300228static const mon_cmd_t qmp_cmds[];
229
Markus Armbruster8631b602010-02-18 11:41:55 +0100230Monitor *cur_mon;
aliguori376253e2009-03-05 23:01:23 +0000231
Stefan Hajnoczic60bf332013-11-14 11:54:14 +0100232static void monitor_command_cb(void *opaque, const char *cmdline,
233 void *readline_opaque);
aliguori83ab7952008-08-19 14:44:22 +0000234
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100235/**
236 * Is @mon a QMP monitor?
237 */
238static inline bool monitor_is_qmp(const Monitor *mon)
Luiz Capitulino418173c2009-11-26 22:58:51 -0200239{
240 return (mon->flags & MONITOR_USE_CONTROL);
241}
242
Markus Armbruster489653b2015-03-06 20:01:05 +0100243/**
244 * Is the current monitor, if any, a QMP monitor?
245 */
246bool monitor_cur_is_qmp(void)
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100247{
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100248 return cur_mon && monitor_is_qmp(cur_mon);
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100249}
250
Anthony Liguori7060b472011-09-02 12:34:50 -0500251void monitor_read_command(Monitor *mon, int show_prompt)
aliguori731b0362009-03-05 23:01:42 +0000252{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200253 if (!mon->rs)
254 return;
255
aliguori731b0362009-03-05 23:01:42 +0000256 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
257 if (show_prompt)
258 readline_show_prompt(mon->rs);
259}
bellard6a00d602005-11-21 23:25:50 +0000260
Anthony Liguori7060b472011-09-02 12:34:50 -0500261int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
262 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000263{
Markus Armbrusterbcf5d192015-03-12 17:26:46 +0100264 if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000265 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
266 /* prompt is printed on return from the command handler */
267 return 0;
268 } else {
269 monitor_printf(mon, "terminal does not support password prompting\n");
270 return -ENOTTY;
271 }
aliguoribb5fc202009-03-05 23:01:15 +0000272}
273
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200274static void monitor_flush_locked(Monitor *mon);
275
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100276static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
277 void *opaque)
278{
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200279 Monitor *mon = opaque;
280
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200281 qemu_mutex_lock(&mon->out_lock);
282 mon->out_watch = 0;
283 monitor_flush_locked(mon);
284 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100285 return FALSE;
286}
287
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200288/* Called with mon->out_lock held. */
289static void monitor_flush_locked(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000290{
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100291 int rc;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400292 size_t len;
293 const char *buf;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100294
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400295 if (mon->skip_flush) {
296 return;
297 }
298
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400299 buf = qstring_get_str(mon->outbuf);
300 len = qstring_get_length(mon->outbuf);
301
Paolo Bonzinia4cc73d2013-05-31 14:00:27 +0200302 if (len && !mon->mux_out) {
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400303 rc = qemu_chr_fe_write(mon->chr, (const uint8_t *) buf, len);
Stratos Psomadakis056f49f2014-01-27 12:30:15 +0200304 if ((rc < 0 && errno != EAGAIN) || (rc == len)) {
305 /* all flushed or error */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400306 QDECREF(mon->outbuf);
307 mon->outbuf = qstring_new();
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100308 return;
309 }
310 if (rc > 0) {
311 /* partinal write */
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400312 QString *tmp = qstring_from_str(buf + rc);
313 QDECREF(mon->outbuf);
314 mon->outbuf = tmp;
Gerd Hoffmannf6289262013-03-19 10:57:56 +0100315 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200316 if (mon->out_watch == 0) {
Roger Pau Monnee02bc6d2014-05-23 17:57:49 +0200317 mon->out_watch = qemu_chr_fe_add_watch(mon->chr, G_IO_OUT|G_IO_HUP,
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200318 monitor_unblocked, mon);
Laszlo Ersek293d2a02013-07-16 20:19:41 +0200319 }
bellard7e2515e2004-08-01 21:52:19 +0000320 }
321}
322
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200323void monitor_flush(Monitor *mon)
324{
325 qemu_mutex_lock(&mon->out_lock);
326 monitor_flush_locked(mon);
327 qemu_mutex_unlock(&mon->out_lock);
328}
329
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400330/* flush at every end of line */
aliguori376253e2009-03-05 23:01:23 +0000331static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000332{
ths60fe76f2007-12-16 03:02:09 +0000333 char c;
aliguori731b0362009-03-05 23:01:42 +0000334
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200335 qemu_mutex_lock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000336 for(;;) {
337 c = *str++;
338 if (c == '\0')
339 break;
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400340 if (c == '\n') {
341 qstring_append_chr(mon->outbuf, '\r');
342 }
343 qstring_append_chr(mon->outbuf, c);
344 if (c == '\n') {
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200345 monitor_flush_locked(mon);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400346 }
bellard7e2515e2004-08-01 21:52:19 +0000347 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200348 qemu_mutex_unlock(&mon->out_lock);
bellard7e2515e2004-08-01 21:52:19 +0000349}
350
aliguori376253e2009-03-05 23:01:23 +0000351void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000352{
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400353 char *buf;
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200354
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200355 if (!mon)
356 return;
357
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100358 if (monitor_is_qmp(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200359 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200360 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200361
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400362 buf = g_strdup_vprintf(fmt, ap);
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200363 monitor_puts(mon, buf);
Luiz Capitulinoe1f26412013-03-25 13:52:26 -0400364 g_free(buf);
bellard7e2515e2004-08-01 21:52:19 +0000365}
366
aliguori376253e2009-03-05 23:01:23 +0000367void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000368{
369 va_list ap;
370 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000371 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000372 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000373}
374
Stefan Weil8b7968f2010-09-23 21:28:05 +0200375static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream,
376 const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000377{
378 va_list ap;
379 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000380 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000381 va_end(ap);
382 return 0;
383}
384
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200385static void monitor_json_emitter(Monitor *mon, const QObject *data)
386{
387 QString *json;
388
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200389 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
390 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200391 assert(json != NULL);
392
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200393 qstring_append_chr(json, '\n');
394 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200395
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200396 QDECREF(json);
397}
398
Markus Armbruster710aec92015-03-06 11:28:00 +0100399static QDict *build_qmp_error_dict(Error *err)
Luiz Capitulinode253f12012-07-27 16:18:16 -0300400{
401 QObject *obj;
402
Markus Armbruster710aec92015-03-06 11:28:00 +0100403 obj = qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %s } }",
404 ErrorClass_lookup[error_get_class(err)],
405 error_get_pretty(err));
Luiz Capitulinode253f12012-07-27 16:18:16 -0300406
407 return qobject_to_qdict(obj);
408}
409
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100410static void monitor_protocol_emitter(Monitor *mon, QObject *data,
Markus Armbruster710aec92015-03-06 11:28:00 +0100411 Error *err)
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200412{
413 QDict *qmp;
414
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +0100415 trace_monitor_protocol_emitter(mon);
416
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100417 if (!err) {
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200418 /* success response */
Luiz Capitulinode253f12012-07-27 16:18:16 -0300419 qmp = qdict_new();
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200420 if (data) {
421 qobject_incref(data);
422 qdict_put_obj(qmp, "return", data);
423 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200424 /* return an empty QDict by default */
425 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200426 }
427 } else {
428 /* error response */
Markus Armbruster70ea0c52015-03-06 10:47:08 +0100429 qmp = build_qmp_error_dict(err);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200430 }
431
Markus Armbruster74358f22015-03-06 19:35:59 +0100432 if (mon->qmp.id) {
433 qdict_put_obj(qmp, "id", mon->qmp.id);
434 mon->qmp.id = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200435 }
436
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200437 monitor_json_emitter(mon, QOBJECT(qmp));
438 QDECREF(qmp);
439}
440
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200441
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200442static MonitorQAPIEventState monitor_qapi_event_state[QAPI_EVENT_MAX];
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100443
444/*
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200445 * Emits the event to every monitor instance, @event is only used for trace
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200446 * Called with monitor_lock held.
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100447 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200448static void monitor_qapi_event_emit(QAPIEvent event, QObject *data)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100449{
450 Monitor *mon;
451
452 trace_monitor_protocol_event_emit(event, data);
453 QLIST_FOREACH(mon, &mon_list, entry) {
Markus Armbruster9f3982f2015-03-06 19:56:38 +0100454 if (monitor_is_qmp(mon) && mon->qmp.in_command_mode) {
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100455 monitor_json_emitter(mon, data);
456 }
457 }
458}
459
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100460/*
461 * Queue a new event for emission to Monitor instances,
462 * applying any rate limiting if required.
463 */
464static void
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200465monitor_qapi_event_queue(QAPIEvent event, QDict *data, Error **errp)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100466{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200467 MonitorQAPIEventState *evstate;
468 assert(event < QAPI_EVENT_MAX);
Alex Blighbc72ad62013-08-21 16:03:08 +0100469 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100470
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200471 evstate = &(monitor_qapi_event_state[event]);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100472 trace_monitor_protocol_event_queue(event,
473 data,
474 evstate->rate,
475 evstate->last,
476 now);
477
478 /* Rate limit of 0 indicates no throttling */
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200479 qemu_mutex_lock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100480 if (!evstate->rate) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200481 monitor_qapi_event_emit(event, QOBJECT(data));
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100482 evstate->last = now;
483 } else {
484 int64_t delta = now - evstate->last;
485 if (evstate->data ||
486 delta < evstate->rate) {
487 /* If there's an existing event pending, replace
488 * it with the new event, otherwise schedule a
489 * timer for delayed emission
490 */
491 if (evstate->data) {
492 qobject_decref(evstate->data);
493 } else {
494 int64_t then = evstate->last + evstate->rate;
Alex Blighbc72ad62013-08-21 16:03:08 +0100495 timer_mod_ns(evstate->timer, then);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100496 }
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200497 evstate->data = QOBJECT(data);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100498 qobject_incref(evstate->data);
499 } else {
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200500 monitor_qapi_event_emit(event, QOBJECT(data));
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100501 evstate->last = now;
502 }
503 }
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200504 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100505}
506
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100507/*
508 * The callback invoked by QemuTimer when a delayed
509 * event is ready to be emitted
510 */
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200511static void monitor_qapi_event_handler(void *opaque)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100512{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200513 MonitorQAPIEventState *evstate = opaque;
Alex Blighbc72ad62013-08-21 16:03:08 +0100514 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100515
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100516 trace_monitor_protocol_event_handler(evstate->event,
517 evstate->data,
518 evstate->last,
519 now);
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200520 qemu_mutex_lock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100521 if (evstate->data) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200522 monitor_qapi_event_emit(evstate->event, evstate->data);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100523 qobject_decref(evstate->data);
524 evstate->data = NULL;
525 }
526 evstate->last = now;
Paolo Bonzinid622cb52014-06-18 08:44:00 +0200527 qemu_mutex_unlock(&monitor_lock);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100528}
529
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100530/*
531 * @event: the event ID to be limited
532 * @rate: the rate limit in milliseconds
533 *
534 * Sets a rate limit on a particular event, so no
535 * more than 1 event will be emitted within @rate
536 * milliseconds
537 */
538static void
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200539monitor_qapi_event_throttle(QAPIEvent event, int64_t rate)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100540{
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200541 MonitorQAPIEventState *evstate;
542 assert(event < QAPI_EVENT_MAX);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100543
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200544 evstate = &(monitor_qapi_event_state[event]);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100545
546 trace_monitor_protocol_event_throttle(event, rate);
547 evstate->event = event;
Wenchao Xia2f44a082014-06-24 16:34:00 -0700548 assert(rate * SCALE_MS <= INT64_MAX);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100549 evstate->rate = rate * SCALE_MS;
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100550 evstate->last = 0;
551 evstate->data = NULL;
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200552 evstate->timer = timer_new(QEMU_CLOCK_REALTIME,
553 SCALE_MS,
554 monitor_qapi_event_handler,
555 evstate);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100556}
557
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200558static void monitor_qapi_event_init(void)
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100559{
Wenchao Xiae010ad82014-06-18 08:43:41 +0200560 /* Limit guest-triggerable events to 1 per second */
561 monitor_qapi_event_throttle(QAPI_EVENT_RTC_CHANGE, 1000);
Wenchao Xia99eaf092014-06-18 08:43:42 +0200562 monitor_qapi_event_throttle(QAPI_EVENT_WATCHDOG, 1000);
Wenchao Xiaaef9d312014-06-18 08:43:51 +0200563 monitor_qapi_event_throttle(QAPI_EVENT_BALLOON_CHANGE, 1000);
Wenchao Xiafe069d92014-06-18 08:43:53 +0200564 monitor_qapi_event_throttle(QAPI_EVENT_QUORUM_REPORT_BAD, 1000);
565 monitor_qapi_event_throttle(QAPI_EVENT_QUORUM_FAILURE, 1000);
Laszlo Erseke2ae6152014-06-26 17:50:02 +0200566 monitor_qapi_event_throttle(QAPI_EVENT_VSERPORT_CHANGE, 1000);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100567
Wenchao Xia43a14cf2014-06-18 08:43:31 +0200568 qmp_event_set_func_emit(monitor_qapi_event_queue);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200569}
570
Markus Armbruster485febc2015-03-13 17:25:50 +0100571static void qmp_capabilities(QDict *params, QObject **ret_data, Error **errp)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200572{
Markus Armbruster485febc2015-03-13 17:25:50 +0100573 cur_mon->qmp.in_command_mode = true;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200574}
575
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100576static void handle_hmp_command(Monitor *mon, const char *cmdline);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200577
Wenchao Xiab01fe892013-08-27 20:38:19 +0800578static void monitor_data_init(Monitor *mon)
579{
580 memset(mon, 0, sizeof(Monitor));
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200581 qemu_mutex_init(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800582 mon->outbuf = qstring_new();
Wenchao Xia77172392013-08-27 20:38:20 +0800583 /* Use *mon_cmds by default. */
584 mon->cmd_table = mon_cmds;
Wenchao Xiab01fe892013-08-27 20:38:19 +0800585}
586
587static void monitor_data_destroy(Monitor *mon)
588{
589 QDECREF(mon->outbuf);
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200590 qemu_mutex_destroy(&mon->out_lock);
Wenchao Xiab01fe892013-08-27 20:38:19 +0800591}
592
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200593char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
594 int64_t cpu_index, Error **errp)
Luiz Capitulino0268d972010-10-22 10:08:02 -0200595{
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200596 char *output = NULL;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200597 Monitor *old_mon, hmp;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200598
Wenchao Xiab01fe892013-08-27 20:38:19 +0800599 monitor_data_init(&hmp);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400600 hmp.skip_flush = true;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200601
602 old_mon = cur_mon;
603 cur_mon = &hmp;
604
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200605 if (has_cpu_index) {
606 int ret = monitor_set_cpu(cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200607 if (ret < 0) {
608 cur_mon = old_mon;
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100609 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
610 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200611 goto out;
612 }
613 }
614
Markus Armbruster7ef6cf62015-03-06 19:12:36 +0100615 handle_hmp_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200616 cur_mon = old_mon;
617
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200618 qemu_mutex_lock(&hmp.out_lock);
Luiz Capitulino48c043d2013-04-02 15:07:33 -0400619 if (qstring_get_length(hmp.outbuf) > 0) {
620 output = g_strdup(qstring_get_str(hmp.outbuf));
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200621 } else {
622 output = g_strdup("");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200623 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +0200624 qemu_mutex_unlock(&hmp.out_lock);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200625
626out:
Wenchao Xiab01fe892013-08-27 20:38:19 +0800627 monitor_data_destroy(&hmp);
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200628 return output;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200629}
630
bellard9dc39cb2004-03-14 21:38:27 +0000631static int compare_cmd(const char *name, const char *list)
632{
633 const char *p, *pstart;
634 int len;
635 len = strlen(name);
636 p = list;
637 for(;;) {
638 pstart = p;
639 p = strchr(p, '|');
640 if (!p)
641 p = pstart + strlen(pstart);
642 if ((p - pstart) == len && !memcmp(pstart, name, len))
643 return 1;
644 if (*p == '\0')
645 break;
646 p++;
647 }
648 return 0;
649}
650
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800651static int get_str(char *buf, int buf_size, const char **pp)
652{
653 const char *p;
654 char *q;
655 int c;
656
657 q = buf;
658 p = *pp;
659 while (qemu_isspace(*p)) {
660 p++;
661 }
662 if (*p == '\0') {
663 fail:
664 *q = '\0';
665 *pp = p;
666 return -1;
667 }
668 if (*p == '\"') {
669 p++;
670 while (*p != '\0' && *p != '\"') {
671 if (*p == '\\') {
672 p++;
673 c = *p++;
674 switch (c) {
675 case 'n':
676 c = '\n';
677 break;
678 case 'r':
679 c = '\r';
680 break;
681 case '\\':
682 case '\'':
683 case '\"':
684 break;
685 default:
Peter Maydell71baf782015-08-19 16:20:19 +0100686 printf("unsupported escape code: '\\%c'\n", c);
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800687 goto fail;
688 }
689 if ((q - buf) < buf_size - 1) {
690 *q++ = c;
691 }
692 } else {
693 if ((q - buf) < buf_size - 1) {
694 *q++ = *p;
695 }
696 p++;
697 }
698 }
699 if (*p != '\"') {
Peter Maydell71baf782015-08-19 16:20:19 +0100700 printf("unterminated string\n");
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800701 goto fail;
702 }
703 p++;
704 } else {
705 while (*p != '\0' && !qemu_isspace(*p)) {
706 if ((q - buf) < buf_size - 1) {
707 *q++ = *p;
708 }
709 p++;
710 }
711 }
712 *q = '\0';
713 *pp = p;
714 return 0;
715}
716
717#define MAX_ARGS 16
718
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800719static void free_cmdline_args(char **args, int nb_args)
720{
721 int i;
722
723 assert(nb_args <= MAX_ARGS);
724
725 for (i = 0; i < nb_args; i++) {
726 g_free(args[i]);
727 }
728
729}
730
731/*
732 * Parse the command line to get valid args.
733 * @cmdline: command line to be parsed.
734 * @pnb_args: location to store the number of args, must NOT be NULL.
735 * @args: location to store the args, which should be freed by caller, must
736 * NOT be NULL.
737 *
738 * Returns 0 on success, negative on failure.
739 *
740 * NOTE: this parser is an approximate form of the real command parser. Number
741 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
742 * return with failure.
743 */
744static int parse_cmdline(const char *cmdline,
745 int *pnb_args, char **args)
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800746{
747 const char *p;
748 int nb_args, ret;
749 char buf[1024];
750
751 p = cmdline;
752 nb_args = 0;
753 for (;;) {
754 while (qemu_isspace(*p)) {
755 p++;
756 }
757 if (*p == '\0') {
758 break;
759 }
760 if (nb_args >= MAX_ARGS) {
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800761 goto fail;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800762 }
763 ret = get_str(buf, sizeof(buf), &p);
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800764 if (ret < 0) {
765 goto fail;
766 }
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800767 args[nb_args] = g_strdup(buf);
768 nb_args++;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800769 }
770 *pnb_args = nb_args;
Wenchao Xiadcc70cd2013-08-27 20:38:22 +0800771 return 0;
772
773 fail:
774 free_cmdline_args(args, nb_args);
775 return -1;
Wenchao Xiaf5438c02013-08-27 20:38:21 +0800776}
777
Wenchao Xia66855492013-08-27 20:38:23 +0800778static void help_cmd_dump_one(Monitor *mon,
779 const mon_cmd_t *cmd,
780 char **prefix_args,
781 int prefix_args_nb)
782{
783 int i;
784
785 for (i = 0; i < prefix_args_nb; i++) {
786 monitor_printf(mon, "%s ", prefix_args[i]);
787 }
788 monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
789}
790
791/* @args[@arg_index] is the valid command need to find in @cmds */
Anthony Liguoric227f092009-10-01 16:12:16 -0500792static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
Wenchao Xia66855492013-08-27 20:38:23 +0800793 char **args, int nb_args, int arg_index)
bellard9dc39cb2004-03-14 21:38:27 +0000794{
Anthony Liguoric227f092009-10-01 16:12:16 -0500795 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000796
Wenchao Xia66855492013-08-27 20:38:23 +0800797 /* No valid arg need to compare with, dump all in *cmds */
798 if (arg_index >= nb_args) {
799 for (cmd = cmds; cmd->name != NULL; cmd++) {
800 help_cmd_dump_one(mon, cmd, args, arg_index);
801 }
802 return;
803 }
804
805 /* Find one entry to dump */
806 for (cmd = cmds; cmd->name != NULL; cmd++) {
807 if (compare_cmd(args[arg_index], cmd->name)) {
808 if (cmd->sub_table) {
809 /* continue with next arg */
810 help_cmd_dump(mon, cmd->sub_table,
811 args, nb_args, arg_index + 1);
812 } else {
813 help_cmd_dump_one(mon, cmd, args, arg_index);
814 }
815 break;
816 }
bellard9dc39cb2004-03-14 21:38:27 +0000817 }
818}
819
aliguori376253e2009-03-05 23:01:23 +0000820static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000821{
Wenchao Xia66855492013-08-27 20:38:23 +0800822 char *args[MAX_ARGS];
823 int nb_args = 0;
824
825 /* 1. parse user input */
826 if (name) {
827 /* special case for log, directly dump and return */
828 if (!strcmp(name, "log")) {
Peter Maydell38dad9e2013-02-11 16:41:25 +0000829 const QEMULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000830 monitor_printf(mon, "Log items (comma separated):\n");
831 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
Peter Maydell38dad9e2013-02-11 16:41:25 +0000832 for (item = qemu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000833 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000834 }
Wenchao Xia66855492013-08-27 20:38:23 +0800835 return;
836 }
837
838 if (parse_cmdline(name, &nb_args, args) < 0) {
839 return;
bellardf193c792004-03-21 17:06:25 +0000840 }
bellard9dc39cb2004-03-14 21:38:27 +0000841 }
Wenchao Xia66855492013-08-27 20:38:23 +0800842
843 /* 2. dump the contents according to parsed args */
844 help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0);
845
846 free_cmdline_args(args, nb_args);
bellard9dc39cb2004-03-14 21:38:27 +0000847}
848
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300849static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300850{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300851 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300852}
853
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100854static void hmp_trace_event(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530855{
856 const char *tp_name = qdict_get_str(qdict, "name");
857 bool new_state = qdict_get_bool(qdict, "option");
Lluís Vilanova14101d02014-08-25 13:20:03 +0200858 Error *local_err = NULL;
Blue Swirlf871d682010-10-13 19:14:29 +0000859
Lluís Vilanova14101d02014-08-25 13:20:03 +0200860 qmp_trace_event_set_state(tp_name, new_state, true, true, &local_err);
861 if (local_err) {
Markus Armbruster091e38b2015-02-10 15:15:43 +0100862 error_report_err(local_err);
Blue Swirlf871d682010-10-13 19:14:29 +0000863 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530864}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100865
Michael Rothc45a8162011-10-02 08:44:37 -0500866#ifdef CONFIG_TRACE_SIMPLE
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100867static void hmp_trace_file(Monitor *mon, const QDict *qdict)
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100868{
869 const char *op = qdict_get_try_str(qdict, "op");
870 const char *arg = qdict_get_try_str(qdict, "arg");
871
872 if (!op) {
873 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
874 } else if (!strcmp(op, "on")) {
875 st_set_trace_file_enabled(true);
876 } else if (!strcmp(op, "off")) {
877 st_set_trace_file_enabled(false);
878 } else if (!strcmp(op, "flush")) {
879 st_flush_trace_buffer();
880 } else if (!strcmp(op, "set")) {
881 if (arg) {
882 st_set_trace_file(arg);
883 }
884 } else {
885 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
886 help_cmd(mon, "trace-file");
887 }
888}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530889#endif
890
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100891static void hmp_info_help(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000892{
Luiz Capitulino13c74252009-10-07 13:41:55 -0300893 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000894}
895
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300896CommandInfoList *qmp_query_commands(Error **errp)
bellard9bc9d1c2004-10-10 15:15:51 +0000897{
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300898 CommandInfoList *info, *cmd_list = NULL;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200899 const mon_cmd_t *cmd;
900
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300901 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino40e5a012011-10-21 16:15:31 -0200902 info = g_malloc0(sizeof(*info));
903 info->value = g_malloc0(sizeof(*info->value));
904 info->value->name = g_strdup(cmd->name);
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200905
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300906 info->next = cmd_list;
907 cmd_list = info;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200908 }
909
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300910 return cmd_list;
thsa36e69d2007-12-02 05:18:19 +0000911}
912
Daniel P. Berrange48608532012-05-21 17:59:51 +0100913EventInfoList *qmp_query_events(Error **errp)
914{
915 EventInfoList *info, *ev_list = NULL;
Wenchao Xia75175172014-06-18 08:43:54 +0200916 QAPIEvent e;
Daniel P. Berrange48608532012-05-21 17:59:51 +0100917
Wenchao Xia75175172014-06-18 08:43:54 +0200918 for (e = 0 ; e < QAPI_EVENT_MAX ; e++) {
919 const char *event_name = QAPIEvent_lookup[e];
Daniel P. Berrange48608532012-05-21 17:59:51 +0100920 assert(event_name != NULL);
921 info = g_malloc0(sizeof(*info));
922 info->value = g_malloc0(sizeof(*info->value));
923 info->value->name = g_strdup(event_name);
924
925 info->next = ev_list;
926 ev_list = info;
927 }
928
929 return ev_list;
930}
931
Markus Armbruster39a18152015-09-16 13:06:28 +0200932/*
933 * Minor hack: generated marshalling suppressed for this command
934 * ('gen': false in the schema) so we can parse the JSON string
935 * directly into QObject instead of first parsing it with
936 * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
937 * to QObject with generated output marshallers, every time. Instead,
938 * we do it in test-qmp-input-visitor.c, just to make sure
939 * qapi-introspect.py's output actually conforms to the schema.
940 */
941static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
942 Error **errp)
943{
944 *ret_data = qobject_from_json(qmp_schema_json);
945}
946
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300947/* set the current CPU defined by the user */
948int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000949{
Andreas Färber55e5c282012-12-17 06:18:02 +0100950 CPUState *cpu;
bellard6a00d602005-11-21 23:25:50 +0000951
Andreas Färber1c8bb3c2013-02-15 17:01:09 +0100952 cpu = qemu_get_cpu(cpu_index);
953 if (cpu == NULL) {
954 return -1;
bellard6a00d602005-11-21 23:25:50 +0000955 }
Andreas Färbercb446ec2013-05-01 14:24:52 +0200956 cur_mon->mon_cpu = cpu;
Andreas Färber1c8bb3c2013-02-15 17:01:09 +0100957 return 0;
bellard6a00d602005-11-21 23:25:50 +0000958}
959
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700960static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000961{
aliguori731b0362009-03-05 23:01:42 +0000962 if (!cur_mon->mon_cpu) {
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300963 monitor_set_cpu(0);
bellard6a00d602005-11-21 23:25:50 +0000964 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300965 cpu_synchronize_state(cur_mon->mon_cpu);
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700966 return cur_mon->mon_cpu;
967}
968
Pavel Butsykinbf957282015-09-10 18:38:59 +0300969CPUArchState *mon_get_cpu_env(void)
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700970{
971 return mon_get_cpu()->env_ptr;
bellard6a00d602005-11-21 23:25:50 +0000972}
973
Luiz Capitulino99b77962011-10-24 10:53:44 -0200974int monitor_get_cpu_index(void)
975{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700976 return mon_get_cpu()->cpu_index;
Luiz Capitulino99b77962011-10-24 10:53:44 -0200977}
978
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100979static void hmp_info_registers(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000980{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -0700981 cpu_dump_state(mon_get_cpu(), (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000982}
983
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100984static void hmp_info_jit(Monitor *mon, const QDict *qdict)
bellarde3db7222005-01-26 22:00:47 +0000985{
aliguori376253e2009-03-05 23:01:23 +0000986 dump_exec_info((FILE *)mon, monitor_fprintf);
Sebastian Tanase27498be2014-07-25 11:56:33 +0200987 dump_drift_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000988}
989
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100990static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
Max Filippov246ae242014-11-02 11:04:18 +0300991{
992 dump_opcount_info((FILE *)mon, monitor_fprintf);
993}
994
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100995static void hmp_info_history(Monitor *mon, const QDict *qdict)
bellardaa455482004-04-04 13:07:25 +0000996{
997 int i;
bellard7e2515e2004-08-01 21:52:19 +0000998 const char *str;
ths3b46e622007-09-17 08:09:54 +0000999
aliguoricde76ee2009-03-05 23:01:51 +00001000 if (!mon->rs)
1001 return;
bellard7e2515e2004-08-01 21:52:19 +00001002 i = 0;
1003 for(;;) {
aliguori731b0362009-03-05 23:01:42 +00001004 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +00001005 if (!str)
1006 break;
aliguori376253e2009-03-05 23:01:23 +00001007 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +00001008 i++;
bellardaa455482004-04-04 13:07:25 +00001009 }
1010}
1011
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001012static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
j_mayer76a66252007-03-07 08:32:30 +00001013{
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001014 cpu_dump_statistics(mon_get_cpu(), (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +00001015}
j_mayer76a66252007-03-07 08:32:30 +00001016
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001017static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +05301018{
Lluís Vilanova14101d02014-08-25 13:20:03 +02001019 TraceEventInfoList *events = qmp_trace_event_get_state("*", NULL);
1020 TraceEventInfoList *elem;
1021
1022 for (elem = events; elem != NULL; elem = elem->next) {
1023 monitor_printf(mon, "%s : state %u\n",
1024 elem->value->name,
1025 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
1026 }
1027 qapi_free_TraceEventInfoList(events);
Prerna Saxena22890ab2010-06-24 17:04:53 +05301028}
Prerna Saxena22890ab2010-06-24 17:04:53 +05301029
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001030void qmp_client_migrate_info(const char *protocol, const char *hostname,
1031 bool has_port, int64_t port,
1032 bool has_tls_port, int64_t tls_port,
1033 bool has_cert_subject, const char *cert_subject,
1034 Error **errp)
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001035{
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001036 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001037 if (!qemu_using_spice(errp)) {
1038 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001039 }
1040
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001041 if (!has_port && !has_tls_port) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001042 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001043 return;
Yonit Halperin6ec5dae2012-03-18 09:42:39 +02001044 }
1045
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001046 if (qemu_spice_migrate_info(hostname,
1047 has_port ? port : -1,
1048 has_tls_port ? tls_port : -1,
1049 cert_subject)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001050 error_setg(errp, QERR_UNDEFINED_ERROR);
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001051 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001052 }
Markus Armbrusterb8a185b2015-03-05 17:29:02 +01001053 return;
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001054 }
1055
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001056 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001057}
1058
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001059static void hmp_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001060{
Peter Maydell9a7e5422013-02-11 16:41:20 +00001061 qemu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001062}
1063
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001064static void hmp_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001065{
1066 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001067 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001068
bellard9307c4c2004-04-04 12:57:25 +00001069 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001070 mask = 0;
1071 } else {
Peter Maydell4fde1eb2013-02-11 16:41:22 +00001072 mask = qemu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001073 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001074 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001075 return;
1076 }
1077 }
Peter Maydell24537a02013-02-11 16:41:23 +00001078 qemu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00001079}
1080
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001081static void hmp_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001082{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001083 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001084 if (!option || !strcmp(option, "on")) {
1085 singlestep = 1;
1086 } else if (!strcmp(option, "off")) {
1087 singlestep = 0;
1088 } else {
1089 monitor_printf(mon, "unexpected option %s\n", option);
1090 }
1091}
1092
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001093static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001094{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001095 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001096 if (!device)
1097 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1098 if (gdbserver_start(device) < 0) {
1099 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1100 device);
1101 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001102 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001103 } else {
aliguori59030a82009-04-05 18:43:41 +00001104 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1105 device);
bellard8a7ddc32004-03-31 19:00:16 +00001106 }
1107}
1108
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001109static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001110{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001111 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001112 if (select_watchdog_action(action) == -1) {
1113 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1114 }
1115}
1116
aliguori376253e2009-03-05 23:01:23 +00001117static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001118{
aliguori376253e2009-03-05 23:01:23 +00001119 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001120 switch(c) {
1121 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001122 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001123 break;
1124 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001125 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001126 break;
1127 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001128 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001129 break;
1130 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001131 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001132 break;
1133 default:
1134 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001135 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001136 } else {
aliguori376253e2009-03-05 23:01:23 +00001137 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001138 }
1139 break;
1140 }
aliguori376253e2009-03-05 23:01:23 +00001141 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001142}
1143
aliguori376253e2009-03-05 23:01:23 +00001144static void memory_dump(Monitor *mon, int count, int format, int wsize,
Avi Kivitya8170e52012-10-23 12:30:10 +02001145 hwaddr addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001146{
Blue Swirl23842aa2010-01-12 20:27:43 +00001147 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001148 uint8_t buf[16];
1149 uint64_t v;
1150
1151 if (format == 'i') {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001152 int flags = 0;
bellard9307c4c2004-04-04 12:57:25 +00001153#ifdef TARGET_I386
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001154 CPUArchState *env = mon_get_cpu_env();
bellard4c27ba22004-04-25 18:05:08 +00001155 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001156 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001157 } else if (wsize == 4) {
1158 flags = 0;
1159 } else {
bellard6a15fd12006-04-12 21:07:07 +00001160 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001161 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001162 if (env) {
1163#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001164 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001165 (env->segs[R_CS].flags & DESC_L_MASK))
1166 flags = 2;
1167 else
1168#endif
1169 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1170 flags = 1;
1171 }
bellard4c27ba22004-04-25 18:05:08 +00001172 }
1173#endif
Tom Musta1c38f842014-04-09 14:53:24 -05001174#ifdef TARGET_PPC
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001175 CPUArchState *env = mon_get_cpu_env();
Tom Musta1c38f842014-04-09 14:53:24 -05001176 flags = msr_le << 16;
1177 flags |= env->bfd_mach;
1178#endif
Peter Crosthwaited49190c2015-05-24 14:20:41 -07001179 monitor_disas(mon, mon_get_cpu(), addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001180 return;
1181 }
1182
1183 len = wsize * count;
1184 if (wsize == 1)
1185 line_size = 8;
1186 else
1187 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001188 max_digits = 0;
1189
1190 switch(format) {
1191 case 'o':
1192 max_digits = (wsize * 8 + 2) / 3;
1193 break;
1194 default:
1195 case 'x':
1196 max_digits = (wsize * 8) / 4;
1197 break;
1198 case 'u':
1199 case 'd':
1200 max_digits = (wsize * 8 * 10 + 32) / 33;
1201 break;
1202 case 'c':
1203 wsize = 1;
1204 break;
1205 }
1206
1207 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001208 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001209 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001210 else
aliguori376253e2009-03-05 23:01:23 +00001211 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001212 l = len;
1213 if (l > line_size)
1214 l = line_size;
1215 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001216 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001217 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07001218 if (cpu_memory_rw_debug(mon_get_cpu(), addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001219 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001220 break;
1221 }
bellard9307c4c2004-04-04 12:57:25 +00001222 }
ths5fafdf22007-09-16 21:08:06 +00001223 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001224 while (i < l) {
1225 switch(wsize) {
1226 default:
1227 case 1:
Peter Maydell24e60302015-01-20 15:19:32 +00001228 v = ldub_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001229 break;
1230 case 2:
Peter Maydell24e60302015-01-20 15:19:32 +00001231 v = lduw_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001232 break;
1233 case 4:
Peter Maydell24e60302015-01-20 15:19:32 +00001234 v = (uint32_t)ldl_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001235 break;
1236 case 8:
Peter Maydell24e60302015-01-20 15:19:32 +00001237 v = ldq_p(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001238 break;
1239 }
aliguori376253e2009-03-05 23:01:23 +00001240 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001241 switch(format) {
1242 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001243 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001244 break;
1245 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001246 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001247 break;
1248 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001249 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001250 break;
1251 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001252 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001253 break;
1254 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001255 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001256 break;
1257 }
1258 i += wsize;
1259 }
aliguori376253e2009-03-05 23:01:23 +00001260 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001261 addr += l;
1262 len -= l;
1263 }
1264}
1265
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001266static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001267{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001268 int count = qdict_get_int(qdict, "count");
1269 int format = qdict_get_int(qdict, "format");
1270 int size = qdict_get_int(qdict, "size");
1271 target_long addr = qdict_get_int(qdict, "addr");
1272
aliguori376253e2009-03-05 23:01:23 +00001273 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001274}
1275
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001276static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001277{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001278 int count = qdict_get_int(qdict, "count");
1279 int format = qdict_get_int(qdict, "format");
1280 int size = qdict_get_int(qdict, "size");
Avi Kivitya8170e52012-10-23 12:30:10 +02001281 hwaddr addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001282
aliguori376253e2009-03-05 23:01:23 +00001283 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001284}
1285
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001286static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001287{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001288 int format = qdict_get_int(qdict, "format");
Avi Kivitya8170e52012-10-23 12:30:10 +02001289 hwaddr val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001290
bellard9307c4c2004-04-04 12:57:25 +00001291 switch(format) {
1292 case 'o':
Avi Kivitya8170e52012-10-23 12:30:10 +02001293 monitor_printf(mon, "%#" HWADDR_PRIo, val);
bellard9307c4c2004-04-04 12:57:25 +00001294 break;
1295 case 'x':
Avi Kivitya8170e52012-10-23 12:30:10 +02001296 monitor_printf(mon, "%#" HWADDR_PRIx, val);
bellard9307c4c2004-04-04 12:57:25 +00001297 break;
1298 case 'u':
Avi Kivitya8170e52012-10-23 12:30:10 +02001299 monitor_printf(mon, "%" HWADDR_PRIu, val);
bellard9307c4c2004-04-04 12:57:25 +00001300 break;
1301 default:
1302 case 'd':
Avi Kivitya8170e52012-10-23 12:30:10 +02001303 monitor_printf(mon, "%" HWADDR_PRId, val);
bellard9307c4c2004-04-04 12:57:25 +00001304 break;
1305 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001306 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001307 break;
1308 }
aliguori376253e2009-03-05 23:01:23 +00001309 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001310}
1311
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001312static void hmp_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001313{
1314 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001315 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001316 uint32_t start = qdict_get_int(qdict, "start");
1317 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001318
1319 sum = 0;
1320 for(addr = start; addr < (start + size); addr++) {
Peter Maydell42874d32015-04-26 16:49:24 +01001321 uint8_t val = address_space_ldub(&address_space_memory, addr,
1322 MEMTXATTRS_UNSPECIFIED, NULL);
bellarde4cf1ad2005-06-04 20:15:57 +00001323 /* BSD sum algorithm ('sum' Unix command) */
1324 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001325 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001326 }
aliguori376253e2009-03-05 23:01:23 +00001327 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001328}
1329
bellard13224a82006-07-14 22:03:35 +00001330static int mouse_button_state;
1331
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001332static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001333{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001334 int dx, dy, dz, button;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001335 const char *dx_str = qdict_get_str(qdict, "dx_str");
1336 const char *dy_str = qdict_get_str(qdict, "dy_str");
1337 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001338
bellard13224a82006-07-14 22:03:35 +00001339 dx = strtol(dx_str, NULL, 0);
1340 dy = strtol(dy_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001341 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
1342 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
1343
1344 if (dz_str) {
bellard13224a82006-07-14 22:03:35 +00001345 dz = strtol(dz_str, NULL, 0);
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001346 if (dz != 0) {
1347 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
1348 qemu_input_queue_btn(NULL, button, true);
1349 qemu_input_event_sync();
1350 qemu_input_queue_btn(NULL, button, false);
1351 }
1352 }
1353 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001354}
1355
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001356static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001357{
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001358 static uint32_t bmap[INPUT_BUTTON_MAX] = {
1359 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1360 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
1361 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1362 };
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001363 int button_state = qdict_get_int(qdict, "button_state");
Gerd Hoffmannc751a742013-12-04 15:02:28 +01001364
1365 if (mouse_button_state == button_state) {
1366 return;
1367 }
1368 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1369 qemu_input_event_sync();
bellard13224a82006-07-14 22:03:35 +00001370 mouse_button_state = button_state;
bellard13224a82006-07-14 22:03:35 +00001371}
1372
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001373static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001374{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001375 int size = qdict_get_int(qdict, "size");
1376 int addr = qdict_get_int(qdict, "addr");
1377 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001378 uint32_t val;
1379 int suffix;
1380
1381 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001382 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001383 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001384 addr++;
1385 }
1386 addr &= 0xffff;
1387
1388 switch(size) {
1389 default:
1390 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001391 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001392 suffix = 'b';
1393 break;
1394 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001395 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001396 suffix = 'w';
1397 break;
1398 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001399 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001400 suffix = 'l';
1401 break;
1402 }
aliguori376253e2009-03-05 23:01:23 +00001403 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1404 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001405}
bellarda3a91a32004-06-04 11:06:21 +00001406
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001407static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001408{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001409 int size = qdict_get_int(qdict, "size");
1410 int addr = qdict_get_int(qdict, "addr");
1411 int val = qdict_get_int(qdict, "val");
1412
Jan Kiszkaf1147842009-07-14 10:20:11 +02001413 addr &= IOPORTS_MASK;
1414
1415 switch (size) {
1416 default:
1417 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001418 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001419 break;
1420 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001421 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001422 break;
1423 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001424 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001425 break;
1426 }
1427}
1428
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001429static void hmp_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001430{
Gongleif1839932014-12-03 18:20:58 +00001431 Error *local_err = NULL;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001432 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001433
Gongleif1839932014-12-03 18:20:58 +00001434 qemu_boot_set(bootdevice, &local_err);
1435 if (local_err) {
1436 monitor_printf(mon, "%s\n", error_get_pretty(local_err));
1437 error_free(local_err);
aurel320ecdffb2008-05-04 20:11:34 +00001438 } else {
Gongleif1839932014-12-03 18:20:58 +00001439 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001440 }
1441}
1442
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001443static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
Blue Swirl314e2982011-09-11 20:22:05 +00001444{
1445 mtree_info((fprintf_function)monitor_printf, mon);
1446}
1447
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001448static void hmp_info_numa(Monitor *mon, const QDict *qdict)
aliguori030ea372009-04-21 22:30:47 +00001449{
aliguorib28b6232009-04-22 20:20:29 +00001450 int i;
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001451 CPUState *cpu;
zhanghailiang5b009e42014-11-04 19:49:30 +08001452 uint64_t *node_mem;
aliguori030ea372009-04-21 22:30:47 +00001453
zhanghailiang5b009e42014-11-04 19:49:30 +08001454 node_mem = g_new0(uint64_t, nb_numa_nodes);
1455 query_numa_node_mem(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001456 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1457 for (i = 0; i < nb_numa_nodes; i++) {
1458 monitor_printf(mon, "node %d cpus:", i);
Andreas Färberbdc44642013-06-24 23:50:24 +02001459 CPU_FOREACH(cpu) {
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001460 if (cpu->numa_node == i) {
Andreas Färber55e5c282012-12-17 06:18:02 +01001461 monitor_printf(mon, " %d", cpu->cpu_index);
aliguori030ea372009-04-21 22:30:47 +00001462 }
1463 }
1464 monitor_printf(mon, "\n");
1465 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
zhanghailiang5b009e42014-11-04 19:49:30 +08001466 node_mem[i] >> 20);
aliguori030ea372009-04-21 22:30:47 +00001467 }
zhanghailiang5b009e42014-11-04 19:49:30 +08001468 g_free(node_mem);
aliguori030ea372009-04-21 22:30:47 +00001469}
1470
bellard5f1ce942006-02-08 22:40:15 +00001471#ifdef CONFIG_PROFILER
1472
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001473int64_t tcg_time;
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001474int64_t dev_time;
1475
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001476static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001477{
aliguori376253e2009-03-05 23:01:23 +00001478 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001479 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00001480 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001481 tcg_time, tcg_time / (double)get_ticks_per_sec());
1482 tcg_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001483 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001484}
1485#else
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001486static void hmp_info_profile(Monitor *mon, const QDict *qdict)
bellard5f1ce942006-02-08 22:40:15 +00001487{
aliguori376253e2009-03-05 23:01:23 +00001488 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001489}
1490#endif
1491
bellardec36b692006-07-16 18:57:03 +00001492/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001493static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001494
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001495static void hmp_info_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001496{
1497 int i;
1498 CaptureState *s;
1499
1500 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001501 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001502 s->ops.info (s->opaque);
1503 }
1504}
1505
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001506static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001507{
1508 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001509 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001510 CaptureState *s;
1511
1512 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1513 if (i == n) {
1514 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001515 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001516 g_free (s);
bellardec36b692006-07-16 18:57:03 +00001517 return;
1518 }
1519 }
1520}
1521
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001522static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001523{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001524 const char *path = qdict_get_str(qdict, "path");
1525 int has_freq = qdict_haskey(qdict, "freq");
1526 int freq = qdict_get_try_int(qdict, "freq", -1);
1527 int has_bits = qdict_haskey(qdict, "bits");
1528 int bits = qdict_get_try_int(qdict, "bits", -1);
1529 int has_channels = qdict_haskey(qdict, "nchannels");
1530 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001531 CaptureState *s;
1532
Anthony Liguori7267c092011-08-20 22:09:37 -05001533 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001534
1535 freq = has_freq ? freq : 44100;
1536 bits = has_bits ? bits : 16;
1537 nchannels = has_channels ? nchannels : 2;
1538
1539 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001540 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05001541 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09001542 return;
bellardec36b692006-07-16 18:57:03 +00001543 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001544 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001545}
bellardec36b692006-07-16 18:57:03 +00001546
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001547static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001548{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001549 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001550
aliguori76655d62009-03-06 20:27:37 +00001551 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001552 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001553 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001554 return acl;
1555}
aliguori76655d62009-03-06 20:27:37 +00001556
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001557static void hmp_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001558{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001559 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001560 qemu_acl *acl = find_acl(mon, aclname);
1561 qemu_acl_entry *entry;
1562 int i = 0;
1563
1564 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001565 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001566 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00001567 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00001568 i++;
1569 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001570 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001571 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001572 }
1573}
1574
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001575static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001576{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001577 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001578 qemu_acl *acl = find_acl(mon, aclname);
1579
1580 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001581 qemu_acl_reset(acl);
1582 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001583 }
1584}
aliguori76655d62009-03-06 20:27:37 +00001585
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001586static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001587{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001588 const char *aclname = qdict_get_str(qdict, "aclname");
1589 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001590 qemu_acl *acl = find_acl(mon, aclname);
1591
1592 if (acl) {
1593 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001594 acl->defaultDeny = 0;
1595 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001596 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001597 acl->defaultDeny = 1;
1598 monitor_printf(mon, "acl: policy set to 'deny'\n");
1599 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001600 monitor_printf(mon, "acl: unknown policy '%s', "
1601 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001602 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001603 }
1604}
aliguori76655d62009-03-06 20:27:37 +00001605
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001606static void hmp_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001607{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001608 const char *aclname = qdict_get_str(qdict, "aclname");
1609 const char *match = qdict_get_str(qdict, "match");
1610 const char *policy = qdict_get_str(qdict, "policy");
1611 int has_index = qdict_haskey(qdict, "index");
1612 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001613 qemu_acl *acl = find_acl(mon, aclname);
1614 int deny, ret;
1615
1616 if (acl) {
1617 if (strcmp(policy, "allow") == 0) {
1618 deny = 0;
1619 } else if (strcmp(policy, "deny") == 0) {
1620 deny = 1;
1621 } else {
1622 monitor_printf(mon, "acl: unknown policy '%s', "
1623 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001624 return;
1625 }
aliguori28a76be2009-03-06 20:27:40 +00001626 if (has_index)
1627 ret = qemu_acl_insert(acl, deny, match, index);
1628 else
1629 ret = qemu_acl_append(acl, deny, match);
1630 if (ret < 0)
1631 monitor_printf(mon, "acl: unable to add acl entry\n");
1632 else
1633 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001634 }
1635}
aliguori76655d62009-03-06 20:27:37 +00001636
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001637static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001638{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001639 const char *aclname = qdict_get_str(qdict, "aclname");
1640 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001641 qemu_acl *acl = find_acl(mon, aclname);
1642 int ret;
aliguori76655d62009-03-06 20:27:37 +00001643
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001644 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001645 ret = qemu_acl_remove(acl, match);
1646 if (ret < 0)
1647 monitor_printf(mon, "acl: no matching acl entry\n");
1648 else
1649 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001650 }
1651}
1652
Corey Bryant208c9d12012-06-22 14:36:09 -04001653void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001654{
Anthony Liguoric227f092009-10-01 16:12:16 -05001655 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001656 int fd;
1657
Corey Bryant208c9d12012-06-22 14:36:09 -04001658 fd = qemu_chr_fe_get_msgfd(cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001659 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001660 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryant208c9d12012-06-22 14:36:09 -04001661 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001662 }
1663
1664 if (qemu_isdigit(fdname[0])) {
Stefan Hajnoczi0b9f0e22014-04-24 13:58:18 +02001665 close(fd);
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001666 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
1667 "a name not starting with a digit");
Corey Bryant208c9d12012-06-22 14:36:09 -04001668 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001669 }
1670
Corey Bryant208c9d12012-06-22 14:36:09 -04001671 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001672 if (strcmp(monfd->name, fdname) != 0) {
1673 continue;
1674 }
1675
1676 close(monfd->fd);
1677 monfd->fd = fd;
Corey Bryant208c9d12012-06-22 14:36:09 -04001678 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001679 }
1680
Anthony Liguori7267c092011-08-20 22:09:37 -05001681 monfd = g_malloc0(sizeof(mon_fd_t));
1682 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001683 monfd->fd = fd;
1684
Corey Bryant208c9d12012-06-22 14:36:09 -04001685 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001686}
1687
Corey Bryant208c9d12012-06-22 14:36:09 -04001688void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001689{
Anthony Liguoric227f092009-10-01 16:12:16 -05001690 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001691
Corey Bryant208c9d12012-06-22 14:36:09 -04001692 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001693 if (strcmp(monfd->name, fdname) != 0) {
1694 continue;
1695 }
1696
Blue Swirl72cf2d42009-09-12 07:36:22 +00001697 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001698 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05001699 g_free(monfd->name);
1700 g_free(monfd);
Corey Bryant208c9d12012-06-22 14:36:09 -04001701 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001702 }
1703
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001704 error_setg(errp, QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001705}
1706
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001707static void hmp_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001708{
Luiz Capitulino13548692011-07-29 15:36:43 -03001709 int saved_vm_running = runstate_is_running();
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001710 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02001711
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001712 vm_stop(RUN_STATE_RESTORE_VM);
Juan Quintelac8d41b22009-08-20 19:42:21 +02001713
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03001714 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02001715 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03001716 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02001717}
1718
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001719int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
Mark McLoughlin7768e042009-07-22 09:11:41 +01001720{
Anthony Liguoric227f092009-10-01 16:12:16 -05001721 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01001722
Blue Swirl72cf2d42009-09-12 07:36:22 +00001723 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01001724 int fd;
1725
1726 if (strcmp(monfd->name, fdname) != 0) {
1727 continue;
1728 }
1729
1730 fd = monfd->fd;
1731
1732 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001733 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05001734 g_free(monfd->name);
1735 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001736
1737 return fd;
1738 }
1739
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001740 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001741 return -1;
1742}
1743
Corey Bryantba1c0482012-08-14 16:43:43 -04001744static void monitor_fdset_cleanup(MonFdset *mon_fdset)
1745{
1746 MonFdsetFd *mon_fdset_fd;
1747 MonFdsetFd *mon_fdset_fd_next;
1748
1749 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
Corey Bryantebe52b52012-10-18 15:19:33 -04001750 if ((mon_fdset_fd->removed ||
1751 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
1752 runstate_is_running()) {
Corey Bryantba1c0482012-08-14 16:43:43 -04001753 close(mon_fdset_fd->fd);
1754 g_free(mon_fdset_fd->opaque);
1755 QLIST_REMOVE(mon_fdset_fd, next);
1756 g_free(mon_fdset_fd);
1757 }
1758 }
1759
Corey Bryantadb696f2012-08-14 16:43:47 -04001760 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04001761 QLIST_REMOVE(mon_fdset, next);
1762 g_free(mon_fdset);
1763 }
1764}
1765
Corey Bryantefb87c12012-08-14 16:43:48 -04001766static void monitor_fdsets_cleanup(void)
1767{
1768 MonFdset *mon_fdset;
1769 MonFdset *mon_fdset_next;
1770
1771 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
1772 monitor_fdset_cleanup(mon_fdset);
1773 }
1774}
1775
Corey Bryantba1c0482012-08-14 16:43:43 -04001776AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
1777 const char *opaque, Error **errp)
1778{
1779 int fd;
1780 Monitor *mon = cur_mon;
Corey Bryantba1c0482012-08-14 16:43:43 -04001781 AddfdInfo *fdinfo;
1782
1783 fd = qemu_chr_fe_get_msgfd(mon->chr);
1784 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001785 error_setg(errp, QERR_FD_NOT_SUPPLIED);
Corey Bryantba1c0482012-08-14 16:43:43 -04001786 goto error;
1787 }
1788
Corey Bryante446f702012-10-18 15:19:32 -04001789 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
1790 has_opaque, opaque, errp);
1791 if (fdinfo) {
1792 return fdinfo;
Corey Bryant9ac54af2012-10-18 15:19:31 -04001793 }
1794
Corey Bryantba1c0482012-08-14 16:43:43 -04001795error:
1796 if (fd != -1) {
1797 close(fd);
1798 }
1799 return NULL;
1800}
1801
1802void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
1803{
1804 MonFdset *mon_fdset;
1805 MonFdsetFd *mon_fdset_fd;
1806 char fd_str[60];
1807
1808 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1809 if (mon_fdset->id != fdset_id) {
1810 continue;
1811 }
1812 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1813 if (has_fd) {
1814 if (mon_fdset_fd->fd != fd) {
1815 continue;
1816 }
1817 mon_fdset_fd->removed = true;
1818 break;
1819 } else {
1820 mon_fdset_fd->removed = true;
1821 }
1822 }
1823 if (has_fd && !mon_fdset_fd) {
1824 goto error;
1825 }
1826 monitor_fdset_cleanup(mon_fdset);
1827 return;
1828 }
1829
1830error:
1831 if (has_fd) {
1832 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
1833 fdset_id, fd);
1834 } else {
1835 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
1836 }
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001837 error_setg(errp, QERR_FD_NOT_FOUND, fd_str);
Corey Bryantba1c0482012-08-14 16:43:43 -04001838}
1839
1840FdsetInfoList *qmp_query_fdsets(Error **errp)
1841{
1842 MonFdset *mon_fdset;
1843 MonFdsetFd *mon_fdset_fd;
1844 FdsetInfoList *fdset_list = NULL;
1845
1846 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1847 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
1848 FdsetFdInfoList *fdsetfd_list = NULL;
1849
1850 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
1851 fdset_info->value->fdset_id = mon_fdset->id;
1852
1853 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1854 FdsetFdInfoList *fdsetfd_info;
1855
1856 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
1857 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
1858 fdsetfd_info->value->fd = mon_fdset_fd->fd;
1859 if (mon_fdset_fd->opaque) {
1860 fdsetfd_info->value->has_opaque = true;
1861 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
1862 } else {
1863 fdsetfd_info->value->has_opaque = false;
1864 }
1865
1866 fdsetfd_info->next = fdsetfd_list;
1867 fdsetfd_list = fdsetfd_info;
1868 }
1869
1870 fdset_info->value->fds = fdsetfd_list;
1871
1872 fdset_info->next = fdset_list;
1873 fdset_list = fdset_info;
1874 }
1875
1876 return fdset_list;
1877}
1878
Corey Bryante446f702012-10-18 15:19:32 -04001879AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
1880 bool has_opaque, const char *opaque,
1881 Error **errp)
1882{
1883 MonFdset *mon_fdset = NULL;
1884 MonFdsetFd *mon_fdset_fd;
1885 AddfdInfo *fdinfo;
1886
1887 if (has_fdset_id) {
1888 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1889 /* Break if match found or match impossible due to ordering by ID */
1890 if (fdset_id <= mon_fdset->id) {
1891 if (fdset_id < mon_fdset->id) {
1892 mon_fdset = NULL;
1893 }
1894 break;
1895 }
1896 }
1897 }
1898
1899 if (mon_fdset == NULL) {
1900 int64_t fdset_id_prev = -1;
1901 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
1902
1903 if (has_fdset_id) {
1904 if (fdset_id < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001905 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
1906 "a non-negative value");
Corey Bryante446f702012-10-18 15:19:32 -04001907 return NULL;
1908 }
1909 /* Use specified fdset ID */
1910 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1911 mon_fdset_cur = mon_fdset;
1912 if (fdset_id < mon_fdset_cur->id) {
1913 break;
1914 }
1915 }
1916 } else {
1917 /* Use first available fdset ID */
1918 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1919 mon_fdset_cur = mon_fdset;
1920 if (fdset_id_prev == mon_fdset_cur->id - 1) {
1921 fdset_id_prev = mon_fdset_cur->id;
1922 continue;
1923 }
1924 break;
1925 }
1926 }
1927
1928 mon_fdset = g_malloc0(sizeof(*mon_fdset));
1929 if (has_fdset_id) {
1930 mon_fdset->id = fdset_id;
1931 } else {
1932 mon_fdset->id = fdset_id_prev + 1;
1933 }
1934
1935 /* The fdset list is ordered by fdset ID */
1936 if (!mon_fdset_cur) {
1937 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
1938 } else if (mon_fdset->id < mon_fdset_cur->id) {
1939 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
1940 } else {
1941 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
1942 }
1943 }
1944
1945 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
1946 mon_fdset_fd->fd = fd;
1947 mon_fdset_fd->removed = false;
1948 if (has_opaque) {
1949 mon_fdset_fd->opaque = g_strdup(opaque);
1950 }
1951 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
1952
1953 fdinfo = g_malloc0(sizeof(*fdinfo));
1954 fdinfo->fdset_id = mon_fdset->id;
1955 fdinfo->fd = mon_fdset_fd->fd;
1956
1957 return fdinfo;
1958}
1959
Corey Bryantadb696f2012-08-14 16:43:47 -04001960int monitor_fdset_get_fd(int64_t fdset_id, int flags)
1961{
Blue Swirlb2dc64c2012-08-18 20:14:54 +00001962#ifndef _WIN32
Corey Bryantadb696f2012-08-14 16:43:47 -04001963 MonFdset *mon_fdset;
1964 MonFdsetFd *mon_fdset_fd;
1965 int mon_fd_flags;
1966
Corey Bryantadb696f2012-08-14 16:43:47 -04001967 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1968 if (mon_fdset->id != fdset_id) {
1969 continue;
1970 }
1971 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1972 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
1973 if (mon_fd_flags == -1) {
1974 return -1;
1975 }
1976
1977 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
1978 return mon_fdset_fd->fd;
1979 }
1980 }
1981 errno = EACCES;
1982 return -1;
1983 }
1984#endif
1985
1986 errno = ENOENT;
1987 return -1;
1988}
1989
1990int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
1991{
1992 MonFdset *mon_fdset;
1993 MonFdsetFd *mon_fdset_fd_dup;
1994
1995 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1996 if (mon_fdset->id != fdset_id) {
1997 continue;
1998 }
1999 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2000 if (mon_fdset_fd_dup->fd == dup_fd) {
2001 return -1;
2002 }
2003 }
2004 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2005 mon_fdset_fd_dup->fd = dup_fd;
2006 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
2007 return 0;
2008 }
2009 return -1;
2010}
2011
2012static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2013{
2014 MonFdset *mon_fdset;
2015 MonFdsetFd *mon_fdset_fd_dup;
2016
2017 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2018 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2019 if (mon_fdset_fd_dup->fd == dup_fd) {
2020 if (remove) {
2021 QLIST_REMOVE(mon_fdset_fd_dup, next);
2022 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2023 monitor_fdset_cleanup(mon_fdset);
2024 }
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002025 return -1;
2026 } else {
2027 return mon_fdset->id;
Corey Bryantadb696f2012-08-14 16:43:47 -04002028 }
Corey Bryantadb696f2012-08-14 16:43:47 -04002029 }
2030 }
2031 }
2032 return -1;
2033}
2034
2035int monitor_fdset_dup_fd_find(int dup_fd)
2036{
2037 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2038}
2039
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002040void monitor_fdset_dup_fd_remove(int dup_fd)
Corey Bryantadb696f2012-08-14 16:43:47 -04002041{
Michael S. Tsirkinb3dd1b82014-08-17 11:45:17 +02002042 monitor_fdset_dup_fd_find_remove(dup_fd, true);
Corey Bryantadb696f2012-08-14 16:43:47 -04002043}
2044
Markus Armbruster1677f4c2015-02-09 14:03:19 +01002045int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
Laszlo Ersek59063662014-04-10 10:24:31 +02002046{
2047 int fd;
2048 Error *local_err = NULL;
2049
2050 if (!qemu_isdigit(fdname[0]) && mon) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02002051 fd = monitor_get_fd(mon, fdname, &local_err);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002052 } else {
2053 fd = qemu_parse_fd(fdname);
Laszlo Ersek59063662014-04-10 10:24:31 +02002054 if (fd == -1) {
2055 error_setg(&local_err, "Invalid file descriptor number '%s'",
2056 fdname);
2057 }
2058 }
2059 if (local_err) {
2060 error_propagate(errp, local_err);
2061 assert(fd == -1);
2062 } else {
2063 assert(fd != -1);
Nicholas Bellingera96ed022012-08-21 20:52:07 +00002064 }
2065
2066 return fd;
2067}
2068
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002069/* Please update hmp-commands.hx when adding or changing commands */
Wayne Xia816f8922011-10-12 11:32:41 +08002070static mon_cmd_t info_cmds[] = {
Pavel Butsykinda76ee72015-09-10 18:38:58 +03002071#include "hmp-commands-info.h"
2072 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002073};
2074
Wenchao Xiaa13ced52013-01-14 14:06:28 +08002075/* mon_cmds and info_cmds would be sorted at runtime */
2076static mon_cmd_t mon_cmds[] = {
2077#include "hmp-commands.h"
2078 { NULL, NULL, },
2079};
2080
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002081static const mon_cmd_t qmp_cmds[] = {
Anthony Liguorie3193602011-09-02 12:34:47 -05002082#include "qmp-commands-old.h"
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002083 { /* NULL */ },
2084};
2085
bellard9307c4c2004-04-04 12:57:25 +00002086/*******************************************************************/
2087
2088static const char *pch;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002089static sigjmp_buf expr_env;
bellard9307c4c2004-04-04 12:57:25 +00002090
bellard9307c4c2004-04-04 12:57:25 +00002091
Stefan Weil9c3175c2013-08-22 21:30:09 +02002092static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
2093expr_error(Monitor *mon, const char *fmt, ...)
bellard9dc39cb2004-03-14 21:38:27 +00002094{
Fam Zheng277acfe2013-08-20 10:58:21 +08002095 va_list ap;
2096 va_start(ap, fmt);
2097 monitor_vprintf(mon, fmt, ap);
2098 monitor_printf(mon, "\n");
2099 va_end(ap);
Peter Maydell6ab7e542013-02-20 15:21:09 +00002100 siglongjmp(expr_env, 1);
bellard9307c4c2004-04-04 12:57:25 +00002101}
2102
Markus Armbruster09b94182010-01-20 13:07:30 +01002103/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00002104static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002105{
Pavel Butsykinbf957282015-09-10 18:38:59 +03002106 const MonitorDef *md = target_monitor_defs();
bellard92a31b12005-02-10 22:00:52 +00002107 void *ptr;
2108
Pavel Butsykinbf957282015-09-10 18:38:59 +03002109 if (md == NULL) {
2110 return -1;
2111 }
2112
2113 for(; md->name != NULL; md++) {
bellard9307c4c2004-04-04 12:57:25 +00002114 if (compare_cmd(name, md->name)) {
2115 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002116 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002117 } else {
Peter Crosthwaite5bcda5f2015-05-24 14:20:40 -07002118 CPUArchState *env = mon_get_cpu_env();
bellard6a00d602005-11-21 23:25:50 +00002119 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002120 switch(md->type) {
2121 case MD_I32:
2122 *pval = *(int32_t *)ptr;
2123 break;
2124 case MD_TLONG:
2125 *pval = *(target_long *)ptr;
2126 break;
2127 default:
2128 *pval = 0;
2129 break;
2130 }
bellard9307c4c2004-04-04 12:57:25 +00002131 }
2132 return 0;
2133 }
2134 }
2135 return -1;
2136}
2137
2138static void next(void)
2139{
Blue Swirl660f11b2009-07-31 21:16:51 +00002140 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002141 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002142 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002143 pch++;
2144 }
2145}
2146
aliguori376253e2009-03-05 23:01:23 +00002147static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002148
aliguori376253e2009-03-05 23:01:23 +00002149static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002150{
blueswir1c2efc952007-09-25 17:28:42 +00002151 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002152 char *p;
bellard6a00d602005-11-21 23:25:50 +00002153 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002154
2155 switch(*pch) {
2156 case '+':
2157 next();
aliguori376253e2009-03-05 23:01:23 +00002158 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002159 break;
2160 case '-':
2161 next();
aliguori376253e2009-03-05 23:01:23 +00002162 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002163 break;
2164 case '~':
2165 next();
aliguori376253e2009-03-05 23:01:23 +00002166 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002167 break;
2168 case '(':
2169 next();
aliguori376253e2009-03-05 23:01:23 +00002170 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002171 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002172 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002173 }
2174 next();
2175 break;
bellard81d09122004-07-14 17:21:37 +00002176 case '\'':
2177 pch++;
2178 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002179 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002180 n = *pch;
2181 pch++;
2182 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002183 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002184 next();
2185 break;
bellard9307c4c2004-04-04 12:57:25 +00002186 case '$':
2187 {
2188 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002189 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002190
bellard9307c4c2004-04-04 12:57:25 +00002191 pch++;
2192 q = buf;
2193 while ((*pch >= 'a' && *pch <= 'z') ||
2194 (*pch >= 'A' && *pch <= 'Z') ||
2195 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002196 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002197 if ((q - buf) < sizeof(buf) - 1)
2198 *q++ = *pch;
2199 pch++;
2200 }
blueswir1cd390082008-11-16 13:53:32 +00002201 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002202 pch++;
2203 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002204 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01002205 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00002206 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00002207 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002208 }
2209 break;
2210 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002211 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002212 n = 0;
2213 break;
2214 default:
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002215 errno = 0;
bellard4f4fbf72006-06-25 18:28:12 +00002216 n = strtoull(pch, &p, 0);
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03002217 if (errno == ERANGE) {
2218 expr_error(mon, "number too large");
2219 }
bellard9307c4c2004-04-04 12:57:25 +00002220 if (pch == p) {
Fam Zheng277acfe2013-08-20 10:58:21 +08002221 expr_error(mon, "invalid char '%c' in expression", *p);
bellard9307c4c2004-04-04 12:57:25 +00002222 }
2223 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002224 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002225 pch++;
2226 break;
2227 }
2228 return n;
2229}
2230
2231
aliguori376253e2009-03-05 23:01:23 +00002232static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002233{
blueswir1c2efc952007-09-25 17:28:42 +00002234 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002235 int op;
ths3b46e622007-09-17 08:09:54 +00002236
aliguori376253e2009-03-05 23:01:23 +00002237 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002238 for(;;) {
2239 op = *pch;
2240 if (op != '*' && op != '/' && op != '%')
2241 break;
2242 next();
aliguori376253e2009-03-05 23:01:23 +00002243 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002244 switch(op) {
2245 default:
2246 case '*':
2247 val *= val2;
2248 break;
2249 case '/':
2250 case '%':
ths5fafdf22007-09-16 21:08:06 +00002251 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002252 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002253 if (op == '/')
2254 val /= val2;
2255 else
2256 val %= val2;
2257 break;
2258 }
2259 }
2260 return val;
2261}
2262
aliguori376253e2009-03-05 23:01:23 +00002263static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002264{
blueswir1c2efc952007-09-25 17:28:42 +00002265 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002266 int op;
bellard9307c4c2004-04-04 12:57:25 +00002267
aliguori376253e2009-03-05 23:01:23 +00002268 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002269 for(;;) {
2270 op = *pch;
2271 if (op != '&' && op != '|' && op != '^')
2272 break;
2273 next();
aliguori376253e2009-03-05 23:01:23 +00002274 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002275 switch(op) {
2276 default:
2277 case '&':
2278 val &= val2;
2279 break;
2280 case '|':
2281 val |= val2;
2282 break;
2283 case '^':
2284 val ^= val2;
2285 break;
2286 }
2287 }
2288 return val;
2289}
2290
aliguori376253e2009-03-05 23:01:23 +00002291static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002292{
blueswir1c2efc952007-09-25 17:28:42 +00002293 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002294 int op;
bellard9307c4c2004-04-04 12:57:25 +00002295
aliguori376253e2009-03-05 23:01:23 +00002296 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002297 for(;;) {
2298 op = *pch;
2299 if (op != '+' && op != '-')
2300 break;
2301 next();
aliguori376253e2009-03-05 23:01:23 +00002302 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002303 if (op == '+')
2304 val += val2;
2305 else
2306 val -= val2;
2307 }
2308 return val;
2309}
2310
aliguori376253e2009-03-05 23:01:23 +00002311static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002312{
2313 pch = *pp;
Peter Maydell6ab7e542013-02-20 15:21:09 +00002314 if (sigsetjmp(expr_env, 0)) {
bellard9307c4c2004-04-04 12:57:25 +00002315 *pp = pch;
2316 return -1;
2317 }
blueswir1cd390082008-11-16 13:53:32 +00002318 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002319 pch++;
aliguori376253e2009-03-05 23:01:23 +00002320 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002321 *pp = pch;
2322 return 0;
2323}
2324
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002325static int get_double(Monitor *mon, double *pval, const char **pp)
2326{
2327 const char *p = *pp;
2328 char *tailp;
2329 double d;
2330
2331 d = strtod(p, &tailp);
2332 if (tailp == p) {
2333 monitor_printf(mon, "Number expected\n");
2334 return -1;
2335 }
2336 if (d != d || d - d != 0) {
2337 /* NaN or infinity */
2338 monitor_printf(mon, "Bad number\n");
2339 return -1;
2340 }
2341 *pval = d;
2342 *pp = tailp;
2343 return 0;
2344}
2345
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002346/*
2347 * Store the command-name in cmdname, and return a pointer to
2348 * the remaining of the command string.
2349 */
2350static const char *get_command_name(const char *cmdline,
2351 char *cmdname, size_t nlen)
2352{
2353 size_t len;
2354 const char *p, *pstart;
2355
2356 p = cmdline;
2357 while (qemu_isspace(*p))
2358 p++;
2359 if (*p == '\0')
2360 return NULL;
2361 pstart = p;
2362 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2363 p++;
2364 len = p - pstart;
2365 if (len > nlen - 1)
2366 len = nlen - 1;
2367 memcpy(cmdname, pstart, len);
2368 cmdname[len] = '\0';
2369 return p;
2370}
2371
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002372/**
2373 * Read key of 'type' into 'key' and return the current
2374 * 'type' pointer.
2375 */
2376static char *key_get_info(const char *type, char **key)
2377{
2378 size_t len;
2379 char *p, *str;
2380
2381 if (*type == ',')
2382 type++;
2383
2384 p = strchr(type, ':');
2385 if (!p) {
2386 *key = NULL;
2387 return NULL;
2388 }
2389 len = p - type;
2390
Anthony Liguori7267c092011-08-20 22:09:37 -05002391 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002392 memcpy(str, type, len);
2393 str[len] = '\0';
2394
2395 *key = str;
2396 return ++p;
2397}
2398
bellard9307c4c2004-04-04 12:57:25 +00002399static int default_fmt_format = 'x';
2400static int default_fmt_size = 4;
2401
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002402static int is_valid_option(const char *c, const char *typestr)
2403{
2404 char option[3];
2405
2406 option[0] = '-';
2407 option[1] = *c;
2408 option[2] = '\0';
2409
2410 typestr = strstr(typestr, option);
2411 return (typestr != NULL);
2412}
2413
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03002414static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
2415 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002416{
2417 const mon_cmd_t *cmd;
2418
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03002419 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002420 if (compare_cmd(cmdname, cmd->name)) {
2421 return cmd;
2422 }
2423 }
2424
2425 return NULL;
2426}
2427
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03002428static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
2429{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002430 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03002431}
2432
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002433/*
Bandan Dasae502122015-06-03 18:38:08 -04002434 * Parse command name from @cmdp according to command table @table.
2435 * If blank, return NULL.
2436 * Else, if no valid command can be found, report to @mon, and return
2437 * NULL.
2438 * Else, change @cmdp to point right behind the name, and return its
2439 * command table entry.
2440 * Do not assume the return value points into @table! It doesn't when
2441 * the command is found in a sub-command table.
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002442 */
Anthony Liguoric227f092009-10-01 16:12:16 -05002443static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Bandan Dasae502122015-06-03 18:38:08 -04002444 const char **cmdp,
2445 mon_cmd_t *table)
bellard9307c4c2004-04-04 12:57:25 +00002446{
Bandan Dasae502122015-06-03 18:38:08 -04002447 const char *p;
Anthony Liguoric227f092009-10-01 16:12:16 -05002448 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002449 char cmdname[256];
bellard9dc39cb2004-03-14 21:38:27 +00002450
bellard9307c4c2004-04-04 12:57:25 +00002451 /* extract the command name */
Bandan Dasae502122015-06-03 18:38:08 -04002452 p = get_command_name(*cmdp, cmdname, sizeof(cmdname));
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002453 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002454 return NULL;
ths3b46e622007-09-17 08:09:54 +00002455
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002456 cmd = search_dispatch_table(table, cmdname);
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02002457 if (!cmd) {
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002458 monitor_printf(mon, "unknown command: '%.*s'\n",
Bandan Dasae502122015-06-03 18:38:08 -04002459 (int)(p - *cmdp), *cmdp);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002460 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002461 }
bellard9307c4c2004-04-04 12:57:25 +00002462
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002463 /* filter out following useless space */
2464 while (qemu_isspace(*p)) {
2465 p++;
2466 }
Bandan Dasae502122015-06-03 18:38:08 -04002467
2468 *cmdp = p;
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002469 /* search sub command */
Bandan Dasae502122015-06-03 18:38:08 -04002470 if (cmd->sub_table != NULL && *p != '\0') {
2471 return monitor_parse_command(mon, cmdp, cmd->sub_table);
Wenchao Xia5f3d3352013-01-14 14:06:27 +08002472 }
2473
Bandan Dasae502122015-06-03 18:38:08 -04002474 return cmd;
2475}
2476
2477/*
2478 * Parse arguments for @cmd.
2479 * If it can't be parsed, report to @mon, and return NULL.
2480 * Else, insert command arguments into a QDict, and return it.
2481 * Note: On success, caller has to free the QDict structure.
2482 */
2483
2484static QDict *monitor_parse_arguments(Monitor *mon,
2485 const char **endp,
2486 const mon_cmd_t *cmd)
2487{
2488 const char *typestr;
2489 char *key;
2490 int c;
2491 const char *p = *endp;
2492 char buf[1024];
2493 QDict *qdict = qdict_new();
2494
bellard9307c4c2004-04-04 12:57:25 +00002495 /* parse the parameters */
2496 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00002497 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002498 typestr = key_get_info(typestr, &key);
2499 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00002500 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002501 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00002502 typestr++;
2503 switch(c) {
2504 case 'F':
bellard81d09122004-07-14 17:21:37 +00002505 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002506 case 's':
2507 {
2508 int ret;
ths3b46e622007-09-17 08:09:54 +00002509
blueswir1cd390082008-11-16 13:53:32 +00002510 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002511 p++;
2512 if (*typestr == '?') {
2513 typestr++;
2514 if (*p == '\0') {
2515 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03002516 break;
bellard9307c4c2004-04-04 12:57:25 +00002517 }
2518 }
2519 ret = get_str(buf, sizeof(buf), &p);
2520 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002521 switch(c) {
2522 case 'F':
aliguori376253e2009-03-05 23:01:23 +00002523 monitor_printf(mon, "%s: filename expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002524 cmd->name);
bellard81d09122004-07-14 17:21:37 +00002525 break;
2526 case 'B':
aliguori376253e2009-03-05 23:01:23 +00002527 monitor_printf(mon, "%s: block device name expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002528 cmd->name);
bellard81d09122004-07-14 17:21:37 +00002529 break;
2530 default:
Bandan Dasae502122015-06-03 18:38:08 -04002531 monitor_printf(mon, "%s: string expected\n", cmd->name);
bellard81d09122004-07-14 17:21:37 +00002532 break;
2533 }
bellard9307c4c2004-04-04 12:57:25 +00002534 goto fail;
2535 }
Luiz Capitulino53773582009-08-28 15:27:25 -03002536 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00002537 }
2538 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01002539 case 'O':
2540 {
2541 QemuOptsList *opts_list;
2542 QemuOpts *opts;
2543
2544 opts_list = qemu_find_opts(key);
2545 if (!opts_list || opts_list->desc->name) {
2546 goto bad_type;
2547 }
2548 while (qemu_isspace(*p)) {
2549 p++;
2550 }
2551 if (!*p)
2552 break;
2553 if (get_str(buf, sizeof(buf), &p) < 0) {
2554 goto fail;
2555 }
Markus Armbruster70b94332015-02-13 12:50:26 +01002556 opts = qemu_opts_parse_noisily(opts_list, buf, true);
Markus Armbruster361127d2010-02-10 20:24:35 +01002557 if (!opts) {
2558 goto fail;
2559 }
2560 qemu_opts_to_qdict(opts, qdict);
2561 qemu_opts_del(opts);
2562 }
2563 break;
bellard9307c4c2004-04-04 12:57:25 +00002564 case '/':
2565 {
2566 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002567
blueswir1cd390082008-11-16 13:53:32 +00002568 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002569 p++;
2570 if (*p == '/') {
2571 /* format found */
2572 p++;
2573 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002574 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002575 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002576 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002577 count = count * 10 + (*p - '0');
2578 p++;
2579 }
2580 }
2581 size = -1;
2582 format = -1;
2583 for(;;) {
2584 switch(*p) {
2585 case 'o':
2586 case 'd':
2587 case 'u':
2588 case 'x':
2589 case 'i':
2590 case 'c':
2591 format = *p++;
2592 break;
2593 case 'b':
2594 size = 1;
2595 p++;
2596 break;
2597 case 'h':
2598 size = 2;
2599 p++;
2600 break;
2601 case 'w':
2602 size = 4;
2603 p++;
2604 break;
2605 case 'g':
2606 case 'L':
2607 size = 8;
2608 p++;
2609 break;
2610 default:
2611 goto next;
2612 }
2613 }
2614 next:
blueswir1cd390082008-11-16 13:53:32 +00002615 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00002616 monitor_printf(mon, "invalid char in format: '%c'\n",
2617 *p);
bellard9307c4c2004-04-04 12:57:25 +00002618 goto fail;
2619 }
bellard9307c4c2004-04-04 12:57:25 +00002620 if (format < 0)
2621 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002622 if (format != 'i') {
2623 /* for 'i', not specifying a size gives -1 as size */
2624 if (size < 0)
2625 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002626 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002627 }
bellard9307c4c2004-04-04 12:57:25 +00002628 default_fmt_format = format;
2629 } else {
2630 count = 1;
2631 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002632 if (format != 'i') {
2633 size = default_fmt_size;
2634 } else {
2635 size = -1;
2636 }
bellard9307c4c2004-04-04 12:57:25 +00002637 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002638 qdict_put(qdict, "count", qint_from_int(count));
2639 qdict_put(qdict, "format", qint_from_int(format));
2640 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00002641 }
2642 break;
2643 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002644 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002645 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00002646 {
blueswir1c2efc952007-09-25 17:28:42 +00002647 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00002648
blueswir1cd390082008-11-16 13:53:32 +00002649 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002650 p++;
bellard34405572004-06-08 00:55:58 +00002651 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002652 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03002653 if (*p == '\0') {
2654 typestr++;
2655 break;
2656 }
bellard34405572004-06-08 00:55:58 +00002657 } else {
2658 if (*p == '.') {
2659 p++;
blueswir1cd390082008-11-16 13:53:32 +00002660 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00002661 p++;
bellard34405572004-06-08 00:55:58 +00002662 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03002663 typestr++;
2664 break;
bellard34405572004-06-08 00:55:58 +00002665 }
2666 }
bellard13224a82006-07-14 22:03:35 +00002667 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002668 }
aliguori376253e2009-03-05 23:01:23 +00002669 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00002670 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002671 /* Check if 'i' is greater than 32-bit */
2672 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
Bandan Dasae502122015-06-03 18:38:08 -04002673 monitor_printf(mon, "\'%s\' has failed: ", cmd->name);
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002674 monitor_printf(mon, "integer is for 32-bit values\n");
2675 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002676 } else if (c == 'M') {
Luiz Capitulino91162842012-04-26 17:34:30 -03002677 if (val < 0) {
2678 monitor_printf(mon, "enter a positive value\n");
2679 goto fail;
2680 }
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02002681 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002682 }
Luiz Capitulino53773582009-08-28 15:27:25 -03002683 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00002684 }
2685 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02002686 case 'o':
2687 {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01002688 int64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02002689 char *end;
2690
2691 while (qemu_isspace(*p)) {
2692 p++;
2693 }
2694 if (*typestr == '?') {
2695 typestr++;
2696 if (*p == '\0') {
2697 break;
2698 }
2699 }
2700 val = strtosz(p, &end);
2701 if (val < 0) {
2702 monitor_printf(mon, "invalid size\n");
2703 goto fail;
2704 }
2705 qdict_put(qdict, key, qint_from_int(val));
2706 p = end;
2707 }
2708 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01002709 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002710 {
2711 double val;
2712
2713 while (qemu_isspace(*p))
2714 p++;
2715 if (*typestr == '?') {
2716 typestr++;
2717 if (*p == '\0') {
2718 break;
2719 }
2720 }
2721 if (get_double(mon, &val, &p) < 0) {
2722 goto fail;
2723 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02002724 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01002725 switch (*p) {
2726 case 'm':
2727 val /= 1e3; p += 2; break;
2728 case 'u':
2729 val /= 1e6; p += 2; break;
2730 case 'n':
2731 val /= 1e9; p += 2; break;
2732 }
2733 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01002734 if (*p && !qemu_isspace(*p)) {
2735 monitor_printf(mon, "Unknown unit suffix\n");
2736 goto fail;
2737 }
2738 qdict_put(qdict, key, qfloat_from_double(val));
2739 }
2740 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002741 case 'b':
2742 {
2743 const char *beg;
Eric Blakefc48ffc2015-05-15 16:24:59 -06002744 bool val;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002745
2746 while (qemu_isspace(*p)) {
2747 p++;
2748 }
2749 beg = p;
2750 while (qemu_isgraph(*p)) {
2751 p++;
2752 }
2753 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06002754 val = true;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002755 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
Eric Blakefc48ffc2015-05-15 16:24:59 -06002756 val = false;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002757 } else {
2758 monitor_printf(mon, "Expected 'on' or 'off'\n");
2759 goto fail;
2760 }
Eric Blakefc48ffc2015-05-15 16:24:59 -06002761 qdict_put(qdict, key, qbool_from_bool(val));
Markus Armbruster942cd1f2010-03-26 09:07:09 +01002762 }
2763 break;
bellard9307c4c2004-04-04 12:57:25 +00002764 case '-':
2765 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002766 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03002767 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00002768 /* option */
ths3b46e622007-09-17 08:09:54 +00002769
bellard9307c4c2004-04-04 12:57:25 +00002770 c = *typestr++;
2771 if (c == '\0')
2772 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00002773 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002774 p++;
bellard9307c4c2004-04-04 12:57:25 +00002775 if (*p == '-') {
2776 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002777 if(c != *p) {
2778 if(!is_valid_option(p, typestr)) {
2779
2780 monitor_printf(mon, "%s: unsupported option -%c\n",
Bandan Dasae502122015-06-03 18:38:08 -04002781 cmd->name, *p);
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002782 goto fail;
2783 } else {
2784 skip_key = 1;
2785 }
bellard9307c4c2004-04-04 12:57:25 +00002786 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002787 if(skip_key) {
2788 p = tmp;
2789 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03002790 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002791 p++;
Eric Blakefc48ffc2015-05-15 16:24:59 -06002792 qdict_put(qdict, key, qbool_from_bool(true));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02002793 }
bellard9307c4c2004-04-04 12:57:25 +00002794 }
bellard9307c4c2004-04-04 12:57:25 +00002795 }
2796 break;
Wenchao Xia129be002013-08-27 20:38:26 +08002797 case 'S':
2798 {
2799 /* package all remaining string */
2800 int len;
2801
2802 while (qemu_isspace(*p)) {
2803 p++;
2804 }
2805 if (*typestr == '?') {
2806 typestr++;
2807 if (*p == '\0') {
2808 /* no remaining string: NULL argument */
2809 break;
2810 }
2811 }
2812 len = strlen(p);
2813 if (len <= 0) {
2814 monitor_printf(mon, "%s: string expected\n",
Bandan Dasae502122015-06-03 18:38:08 -04002815 cmd->name);
Bandan Dase549d2a2015-06-03 18:38:10 -04002816 goto fail;
Wenchao Xia129be002013-08-27 20:38:26 +08002817 }
2818 qdict_put(qdict, key, qstring_from_str(p));
2819 p += len;
2820 }
2821 break;
bellard9307c4c2004-04-04 12:57:25 +00002822 default:
2823 bad_type:
Bandan Dasae502122015-06-03 18:38:08 -04002824 monitor_printf(mon, "%s: unknown type '%c'\n", cmd->name, c);
bellard9307c4c2004-04-04 12:57:25 +00002825 goto fail;
2826 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002827 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002828 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00002829 }
2830 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00002831 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002832 p++;
2833 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00002834 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
Bandan Dasae502122015-06-03 18:38:08 -04002835 cmd->name);
bellard9307c4c2004-04-04 12:57:25 +00002836 goto fail;
2837 }
2838
Bandan Dasae502122015-06-03 18:38:08 -04002839 return qdict;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02002840
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002841fail:
Bandan Dasae502122015-06-03 18:38:08 -04002842 QDECREF(qdict);
Anthony Liguori7267c092011-08-20 22:09:37 -05002843 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002844 return NULL;
2845}
2846
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01002847static void handle_hmp_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002848{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002849 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05002850 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002851
Bandan Dasae502122015-06-03 18:38:08 -04002852 cmd = monitor_parse_command(mon, &cmdline, mon->cmd_table);
2853 if (!cmd) {
2854 return;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002855 }
2856
Bandan Dasae502122015-06-03 18:38:08 -04002857 qdict = monitor_parse_arguments(mon, &cmdline, cmd);
2858 if (!qdict) {
Bandan Dasdd41eea2015-06-03 18:38:09 -04002859 monitor_printf(mon, "Try \"help %s\" for more information\n",
2860 cmd->name);
Bandan Dasae502122015-06-03 18:38:08 -04002861 return;
2862 }
2863
2864 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002865 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00002866}
2867
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08002868static void cmd_completion(Monitor *mon, const char *name, const char *list)
bellard81d09122004-07-14 17:21:37 +00002869{
2870 const char *p, *pstart;
2871 char cmd[128];
2872 int len;
2873
2874 p = list;
2875 for(;;) {
2876 pstart = p;
2877 p = strchr(p, '|');
2878 if (!p)
2879 p = pstart + strlen(pstart);
2880 len = p - pstart;
2881 if (len > sizeof(cmd) - 2)
2882 len = sizeof(cmd) - 2;
2883 memcpy(cmd, pstart, len);
2884 cmd[len] = '\0';
2885 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08002886 readline_add_completion(mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00002887 }
2888 if (*p == '\0')
2889 break;
2890 p++;
2891 }
2892}
2893
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08002894static void file_completion(Monitor *mon, const char *input)
bellard81d09122004-07-14 17:21:37 +00002895{
2896 DIR *ffs;
2897 struct dirent *d;
2898 char path[1024];
2899 char file[1024], file_prefix[1024];
2900 int input_path_len;
2901 const char *p;
2902
ths5fafdf22007-09-16 21:08:06 +00002903 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00002904 if (!p) {
2905 input_path_len = 0;
2906 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00002907 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00002908 } else {
2909 input_path_len = p - input + 1;
2910 memcpy(path, input, input_path_len);
2911 if (input_path_len > sizeof(path) - 1)
2912 input_path_len = sizeof(path) - 1;
2913 path[input_path_len] = '\0';
2914 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2915 }
Bandan Das19f2db52015-06-03 18:38:07 -04002916
bellard81d09122004-07-14 17:21:37 +00002917 ffs = opendir(path);
2918 if (!ffs)
2919 return;
2920 for(;;) {
2921 struct stat sb;
2922 d = readdir(ffs);
2923 if (!d)
2924 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09002925
2926 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
2927 continue;
2928 }
2929
bellard81d09122004-07-14 17:21:37 +00002930 if (strstart(d->d_name, file_prefix, NULL)) {
2931 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00002932 if (input_path_len < sizeof(file))
2933 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2934 d->d_name);
bellard81d09122004-07-14 17:21:37 +00002935 /* stat the file to find out if it's a directory.
2936 * In that case add a slash to speed up typing long paths
2937 */
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01002938 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
blueswir1363a37d2008-08-21 17:58:08 +00002939 pstrcat(file, sizeof(file), "/");
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01002940 }
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08002941 readline_add_completion(mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00002942 }
2943 }
2944 closedir(ffs);
2945}
2946
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002947static const char *next_arg_type(const char *typestr)
2948{
2949 const char *p = strchr(typestr, ':');
2950 return (p != NULL ? ++p : typestr);
2951}
2952
Hani Benhabiles40d19392014-05-07 23:41:30 +01002953static void add_completion_option(ReadLineState *rs, const char *str,
2954 const char *option)
2955{
2956 if (!str || !option) {
2957 return;
2958 }
2959 if (!strncmp(option, str, strlen(str))) {
2960 readline_add_completion(rs, option);
2961 }
2962}
2963
Hani Benhabiles13e315d2014-05-07 23:41:29 +01002964void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
2965{
2966 size_t len;
2967 ChardevBackendInfoList *list, *start;
2968
2969 if (nb_args != 2) {
2970 return;
2971 }
2972 len = strlen(str);
2973 readline_set_completion_index(rs, len);
2974
2975 start = list = qmp_query_chardev_backends(NULL);
2976 while (list) {
2977 const char *chr_name = list->value->name;
2978
2979 if (!strncmp(chr_name, str, len)) {
2980 readline_add_completion(rs, chr_name);
2981 }
2982 list = list->next;
2983 }
2984 qapi_free_ChardevBackendInfoList(start);
2985}
2986
Hani Benhabilesb162b492014-05-07 23:41:31 +01002987void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
2988{
2989 size_t len;
2990 int i;
2991
2992 if (nb_args != 2) {
2993 return;
2994 }
2995 len = strlen(str);
2996 readline_set_completion_index(rs, len);
2997 for (i = 0; NetClientOptionsKind_lookup[i]; i++) {
2998 add_completion_option(rs, str, NetClientOptionsKind_lookup[i]);
2999 }
3000}
3001
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003002void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003003{
3004 GSList *list, *elt;
3005 size_t len;
3006
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003007 if (nb_args != 2) {
3008 return;
3009 }
3010
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003011 len = strlen(str);
3012 readline_set_completion_index(rs, len);
3013 list = elt = object_class_get_list(TYPE_DEVICE, false);
3014 while (elt) {
3015 const char *name;
3016 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
3017 TYPE_DEVICE);
3018 name = object_class_get_name(OBJECT_CLASS(dc));
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003019
3020 if (!dc->cannot_instantiate_with_device_add_yet
3021 && !strncmp(name, str, len)) {
Hani Benhabiles992d3e62014-02-06 23:30:11 +01003022 readline_add_completion(rs, name);
3023 }
3024 elt = elt->next;
3025 }
3026 g_slist_free(list);
3027}
3028
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003029void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003030{
3031 GSList *list, *elt;
3032 size_t len;
3033
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003034 if (nb_args != 2) {
3035 return;
3036 }
3037
Hani Benhabiles1094fd32014-02-06 23:30:13 +01003038 len = strlen(str);
3039 readline_set_completion_index(rs, len);
3040 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
3041 while (elt) {
3042 const char *name;
3043
3044 name = object_class_get_name(OBJECT_CLASS(elt->data));
3045 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
3046 readline_add_completion(rs, name);
3047 }
3048 elt = elt->next;
3049 }
3050 g_slist_free(list);
3051}
3052
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003053static void peripheral_device_del_completion(ReadLineState *rs,
3054 const char *str, size_t len)
3055{
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003056 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
3057 GSList *list, *item;
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003058
Marcel Apfelbaum4cae4d52014-11-26 13:50:01 +02003059 list = qdev_build_hotpluggable_device_list(peripheral);
3060 if (!list) {
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003061 return;
3062 }
3063
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003064 for (item = list; item; item = g_slist_next(item)) {
3065 DeviceState *dev = item->data;
3066
3067 if (dev->id && !strncmp(str, dev->id, len)) {
3068 readline_add_completion(rs, dev->id);
3069 }
3070 }
3071
3072 g_slist_free(list);
3073}
3074
Hani Benhabiles6297d9a2014-05-07 23:41:28 +01003075void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3076{
3077 size_t len;
3078 ChardevInfoList *list, *start;
3079
3080 if (nb_args != 2) {
3081 return;
3082 }
3083 len = strlen(str);
3084 readline_set_completion_index(rs, len);
3085
3086 start = list = qmp_query_chardev(NULL);
3087 while (list) {
3088 ChardevInfo *chr = list->value;
3089
3090 if (!strncmp(chr->label, str, len)) {
3091 readline_add_completion(rs, chr->label);
3092 }
3093 list = list->next;
3094 }
3095 qapi_free_ChardevInfoList(start);
3096}
3097
Hani Benhabiles8e597772014-05-27 23:39:30 +01003098static void ringbuf_completion(ReadLineState *rs, const char *str)
3099{
3100 size_t len;
3101 ChardevInfoList *list, *start;
3102
3103 len = strlen(str);
3104 readline_set_completion_index(rs, len);
3105
3106 start = list = qmp_query_chardev(NULL);
3107 while (list) {
3108 ChardevInfo *chr_info = list->value;
3109
3110 if (!strncmp(chr_info->label, str, len)) {
3111 CharDriverState *chr = qemu_chr_find(chr_info->label);
3112 if (chr && chr_is_ringbuf(chr)) {
3113 readline_add_completion(rs, chr_info->label);
3114 }
3115 }
3116 list = list->next;
3117 }
3118 qapi_free_ChardevInfoList(start);
3119}
3120
Hani Benhabiles8e597772014-05-27 23:39:30 +01003121void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
3122{
3123 if (nb_args != 2) {
3124 return;
3125 }
3126 ringbuf_completion(rs, str);
3127}
3128
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003129void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
3130{
3131 size_t len;
3132
3133 if (nb_args != 2) {
3134 return;
3135 }
3136
3137 len = strlen(str);
3138 readline_set_completion_index(rs, len);
Zhu Guihua6a1fa9f2014-10-21 19:46:05 +08003139 peripheral_device_del_completion(rs, str, len);
Hani Benhabiles2da1b3a2014-04-13 16:25:07 +01003140}
3141
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003142void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003143{
3144 ObjectPropertyInfoList *list, *start;
3145 size_t len;
3146
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003147 if (nb_args != 2) {
3148 return;
3149 }
Hani Benhabilesb48fa072014-02-06 23:30:12 +01003150 len = strlen(str);
3151 readline_set_completion_index(rs, len);
3152
3153 start = list = qmp_qom_list("/objects", NULL);
3154 while (list) {
3155 ObjectPropertyInfo *info = list->value;
3156
3157 if (!strncmp(info->type, "child<", 5)
3158 && !strncmp(info->name, str, len)) {
3159 readline_add_completion(rs, info->name);
3160 }
3161 list = list->next;
3162 }
3163 qapi_free_ObjectPropertyInfoList(start);
3164}
3165
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003166void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
3167{
3168 int i;
3169 char *sep;
3170 size_t len;
3171
3172 if (nb_args != 2) {
3173 return;
3174 }
3175 sep = strrchr(str, '-');
3176 if (sep) {
3177 str = sep + 1;
3178 }
3179 len = strlen(str);
3180 readline_set_completion_index(rs, len);
3181 for (i = 0; i < Q_KEY_CODE_MAX; i++) {
3182 if (!strncmp(str, QKeyCode_lookup[i], len)) {
3183 readline_add_completion(rs, QKeyCode_lookup[i]);
3184 }
3185 }
3186}
3187
Hani Benhabiles40d19392014-05-07 23:41:30 +01003188void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
3189{
3190 size_t len;
3191
3192 len = strlen(str);
3193 readline_set_completion_index(rs, len);
3194 if (nb_args == 2) {
Jason Wangeaed4832015-04-23 14:21:38 +08003195 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles40d19392014-05-07 23:41:30 +01003196 int count, i;
3197 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08003198 NET_CLIENT_OPTIONS_KIND_NONE,
3199 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003200 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles40d19392014-05-07 23:41:30 +01003201 const char *name = ncs[i]->name;
3202 if (!strncmp(str, name, len)) {
3203 readline_add_completion(rs, name);
3204 }
3205 }
3206 } else if (nb_args == 3) {
3207 add_completion_option(rs, str, "on");
3208 add_completion_option(rs, str, "off");
3209 }
3210}
3211
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003212void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
3213{
3214 int len, count, i;
Jason Wangeaed4832015-04-23 14:21:38 +08003215 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003216
3217 if (nb_args != 2) {
3218 return;
3219 }
3220
3221 len = strlen(str);
3222 readline_set_completion_index(rs, len);
3223 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_OPTIONS_KIND_NIC,
Jason Wangeaed4832015-04-23 14:21:38 +08003224 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003225 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabiles11b389f2014-05-07 23:41:32 +01003226 QemuOpts *opts;
3227 const char *name = ncs[i]->name;
3228 if (strncmp(str, name, len)) {
3229 continue;
3230 }
3231 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
3232 if (opts) {
3233 readline_add_completion(rs, name);
3234 }
3235 }
3236}
3237
Dr. David Alan Gilbert987bd272015-08-14 11:27:43 +01003238void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
3239{
3240 size_t len;
3241
3242 len = strlen(str);
3243 readline_set_completion_index(rs, len);
3244 if (nb_args == 2) {
3245 TraceEventID id;
3246 for (id = 0; id < trace_event_count(); id++) {
3247 const char *event_name = trace_event_get_name(trace_event_id(id));
3248 if (!strncmp(str, event_name, len)) {
3249 readline_add_completion(rs, event_name);
3250 }
3251 }
3252 } else if (nb_args == 3) {
3253 add_completion_option(rs, str, "on");
3254 add_completion_option(rs, str, "off");
3255 }
3256}
3257
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003258void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
3259{
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003260 int i;
3261
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003262 if (nb_args != 2) {
3263 return;
3264 }
3265 readline_set_completion_index(rs, strlen(str));
Hani Benhabiles4bb08af2014-07-29 23:22:40 +01003266 for (i = 0; WatchdogExpirationAction_lookup[i]; i++) {
3267 add_completion_option(rs, str, WatchdogExpirationAction_lookup[i]);
3268 }
Hani Benhabilesd0ece342014-05-27 23:39:31 +01003269}
3270
Hani Benhabilesc68a0402014-05-27 23:39:32 +01003271void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
3272 const char *str)
3273{
3274 size_t len;
3275
3276 len = strlen(str);
3277 readline_set_completion_index(rs, len);
3278 if (nb_args == 2) {
3279 int i;
3280 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
3281 const char *name = MigrationCapability_lookup[i];
3282 if (!strncmp(str, name, len)) {
3283 readline_add_completion(rs, name);
3284 }
3285 }
3286 } else if (nb_args == 3) {
3287 add_completion_option(rs, str, "on");
3288 add_completion_option(rs, str, "off");
3289 }
3290}
3291
Liang Li50e9a622015-03-23 16:32:29 +08003292void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
3293 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 int i;
3301 for (i = 0; i < MIGRATION_PARAMETER_MAX; i++) {
3302 const char *name = MigrationParameter_lookup[i];
3303 if (!strncmp(str, name, len)) {
3304 readline_add_completion(rs, name);
3305 }
3306 }
3307 }
3308}
3309
Hani Benhabilese3bb5322014-05-27 23:39:34 +01003310void host_net_add_completion(ReadLineState *rs, int nb_args, const char *str)
3311{
3312 int i;
3313 size_t len;
3314 if (nb_args != 2) {
3315 return;
3316 }
3317 len = strlen(str);
3318 readline_set_completion_index(rs, len);
3319 for (i = 0; host_net_devices[i]; i++) {
3320 if (!strncmp(host_net_devices[i], str, len)) {
3321 readline_add_completion(rs, host_net_devices[i]);
3322 }
3323 }
3324}
3325
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003326void host_net_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3327{
Jason Wangeaed4832015-04-23 14:21:38 +08003328 NetClientState *ncs[MAX_QUEUE_NUM];
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003329 int count, i, len;
3330
3331 len = strlen(str);
3332 readline_set_completion_index(rs, len);
3333 if (nb_args == 2) {
3334 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08003335 NET_CLIENT_OPTIONS_KIND_NONE,
3336 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003337 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003338 int id;
3339 char name[16];
3340
3341 if (net_hub_id_for_client(ncs[i], &id)) {
3342 continue;
3343 }
3344 snprintf(name, sizeof(name), "%d", id);
3345 if (!strncmp(str, name, len)) {
3346 readline_add_completion(rs, name);
3347 }
3348 }
3349 return;
3350 } else if (nb_args == 3) {
3351 count = qemu_find_net_clients_except(NULL, ncs,
Jason Wangeaed4832015-04-23 14:21:38 +08003352 NET_CLIENT_OPTIONS_KIND_NIC,
3353 MAX_QUEUE_NUM);
Jason Wangbcfa4d62015-04-23 14:21:39 +08003354 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
Jason Wang2c4681f2015-02-02 15:06:38 +08003355 int id;
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003356 const char *name;
3357
Jason Wang2c4681f2015-02-02 15:06:38 +08003358 if (ncs[i]->info->type == NET_CLIENT_OPTIONS_KIND_HUBPORT ||
3359 net_hub_id_for_client(ncs[i], &id)) {
3360 continue;
3361 }
Hani Benhabilesddd6b452014-05-27 23:39:36 +01003362 name = ncs[i]->name;
3363 if (!strncmp(str, name, len)) {
3364 readline_add_completion(rs, name);
3365 }
3366 }
3367 return;
3368 }
3369}
3370
Hani Benhabilesb21631f2014-05-27 23:39:37 +01003371static void vm_completion(ReadLineState *rs, const char *str)
3372{
3373 size_t len;
3374 BlockDriverState *bs = NULL;
3375
3376 len = strlen(str);
3377 readline_set_completion_index(rs, len);
3378 while ((bs = bdrv_next(bs))) {
3379 SnapshotInfoList *snapshots, *snapshot;
3380
3381 if (!bdrv_can_snapshot(bs)) {
3382 continue;
3383 }
3384 if (bdrv_query_snapshot_info_list(bs, &snapshots, NULL)) {
3385 continue;
3386 }
3387 snapshot = snapshots;
3388 while (snapshot) {
3389 char *completion = snapshot->value->name;
3390 if (!strncmp(str, completion, len)) {
3391 readline_add_completion(rs, completion);
3392 }
3393 completion = snapshot->value->id;
3394 if (!strncmp(str, completion, len)) {
3395 readline_add_completion(rs, completion);
3396 }
3397 snapshot = snapshot->next;
3398 }
3399 qapi_free_SnapshotInfoList(snapshots);
3400 }
3401
3402}
3403
3404void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
3405{
3406 if (nb_args == 2) {
3407 vm_completion(rs, str);
3408 }
3409}
3410
3411void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
3412{
3413 if (nb_args == 2) {
3414 vm_completion(rs, str);
3415 }
3416}
3417
Wenchao Xiac35b6402013-08-27 20:38:24 +08003418static void monitor_find_completion_by_table(Monitor *mon,
3419 const mon_cmd_t *cmd_table,
3420 char **args,
3421 int nb_args)
bellard81d09122004-07-14 17:21:37 +00003422{
3423 const char *cmdname;
Wenchao Xiac35b6402013-08-27 20:38:24 +08003424 int i;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003425 const char *ptype, *str, *name;
Anthony Liguoric227f092009-10-01 16:12:16 -05003426 const mon_cmd_t *cmd;
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003427 BlockDriverState *bs;
bellard81d09122004-07-14 17:21:37 +00003428
bellard81d09122004-07-14 17:21:37 +00003429 if (nb_args <= 1) {
3430 /* command completion */
3431 if (nb_args == 0)
3432 cmdname = "";
3433 else
3434 cmdname = args[0];
Wenchao Xiad2674b22013-08-27 20:38:16 +08003435 readline_set_completion_index(mon->rs, strlen(cmdname));
Wenchao Xiac35b6402013-08-27 20:38:24 +08003436 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Wenchao Xiacd5c6bb2013-08-27 20:38:13 +08003437 cmd_completion(mon, cmdname, cmd->name);
bellard81d09122004-07-14 17:21:37 +00003438 }
3439 } else {
3440 /* find the command */
Wenchao Xiac35b6402013-08-27 20:38:24 +08003441 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
Jan Kiszka03a63482010-06-16 00:38:33 +02003442 if (compare_cmd(args[0], cmd->name)) {
3443 break;
3444 }
bellard81d09122004-07-14 17:21:37 +00003445 }
Jan Kiszka03a63482010-06-16 00:38:33 +02003446 if (!cmd->name) {
Wenchao Xiac35b6402013-08-27 20:38:24 +08003447 return;
Jan Kiszka03a63482010-06-16 00:38:33 +02003448 }
3449
Wenchao Xiad903a772013-08-27 20:38:25 +08003450 if (cmd->sub_table) {
3451 /* do the job again */
Stefan Weile7ae7712015-03-08 19:30:01 +01003452 monitor_find_completion_by_table(mon, cmd->sub_table,
3453 &args[1], nb_args - 1);
3454 return;
Wenchao Xiad903a772013-08-27 20:38:25 +08003455 }
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003456 if (cmd->command_completion) {
Stefan Weile7ae7712015-03-08 19:30:01 +01003457 cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
3458 return;
Hani Benhabilesbfa40f72014-04-13 16:25:06 +01003459 }
Wenchao Xiad903a772013-08-27 20:38:25 +08003460
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003461 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003462 for(i = 0; i < nb_args - 2; i++) {
3463 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003464 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003465 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003466 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003467 }
3468 }
3469 str = args[nb_args - 1];
Kevin Wolf48fe86f2014-11-12 16:24:02 +01003470 while (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02003471 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00003472 }
bellard81d09122004-07-14 17:21:37 +00003473 switch(*ptype) {
3474 case 'F':
3475 /* file completion */
Wenchao Xiad2674b22013-08-27 20:38:16 +08003476 readline_set_completion_index(mon->rs, strlen(str));
Wenchao Xiacb8f68b2013-08-27 20:38:14 +08003477 file_completion(mon, str);
bellard81d09122004-07-14 17:21:37 +00003478 break;
3479 case 'B':
3480 /* block device name completion */
Wenchao Xia599a9262013-08-27 20:38:15 +08003481 readline_set_completion_index(mon->rs, strlen(str));
Markus Armbrusterfea68bb2014-10-07 13:59:10 +02003482 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
3483 name = bdrv_get_device_name(bs);
3484 if (str[0] == '\0' ||
3485 !strncmp(name, str, strlen(str))) {
3486 readline_add_completion(mon->rs, name);
3487 }
3488 }
bellard81d09122004-07-14 17:21:37 +00003489 break;
bellard7fe48482004-10-09 18:08:01 +00003490 case 's':
Wenchao Xia129be002013-08-27 20:38:26 +08003491 case 'S':
Hani Benhabiles29136cd2014-05-07 23:41:27 +01003492 if (!strcmp(cmd->name, "help|?")) {
Wenchao Xia7ca0e062013-08-27 20:38:27 +08003493 monitor_find_completion_by_table(mon, cmd_table,
3494 &args[1], nb_args - 1);
bellard7fe48482004-10-09 18:08:01 +00003495 }
3496 break;
bellard81d09122004-07-14 17:21:37 +00003497 default:
3498 break;
3499 }
3500 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08003501}
3502
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003503static void monitor_find_completion(void *opaque,
Wenchao Xiac35b6402013-08-27 20:38:24 +08003504 const char *cmdline)
3505{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003506 Monitor *mon = opaque;
Wenchao Xiac35b6402013-08-27 20:38:24 +08003507 char *args[MAX_ARGS];
3508 int nb_args, len;
3509
3510 /* 1. parse the cmdline */
3511 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
3512 return;
3513 }
Wenchao Xiac35b6402013-08-27 20:38:24 +08003514
3515 /* if the line ends with a space, it means we want to complete the
3516 next arg */
3517 len = strlen(cmdline);
3518 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
3519 if (nb_args >= MAX_ARGS) {
3520 goto cleanup;
3521 }
3522 args[nb_args++] = g_strdup("");
3523 }
3524
3525 /* 2. auto complete according to args */
3526 monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args);
Jan Kiszka03a63482010-06-16 00:38:33 +02003527
3528cleanup:
Wenchao Xiadcc70cd2013-08-27 20:38:22 +08003529 free_cmdline_args(args, nb_args);
bellard81d09122004-07-14 17:21:37 +00003530}
3531
aliguori731b0362009-03-05 23:01:42 +00003532static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003533{
aliguori731b0362009-03-05 23:01:42 +00003534 Monitor *mon = opaque;
3535
Jan Kiszkac62313b2009-12-04 14:05:29 +01003536 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003537}
3538
Markus Armbruster40861822015-05-29 10:27:16 +02003539static bool invalid_qmp_mode(const Monitor *mon, const mon_cmd_t *cmd,
3540 Error **errp)
Luiz Capitulino09069b12010-02-04 18:10:06 -02003541{
Markus Armbruster485febc2015-03-13 17:25:50 +01003542 bool is_cap = cmd->mhandler.cmd_new == qmp_capabilities;
Markus Armbrusterf994b252015-03-06 19:51:51 +01003543
3544 if (is_cap && mon->qmp.in_command_mode) {
Markus Armbruster40861822015-05-29 10:27:16 +02003545 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
3546 "Capabilities negotiation is already complete, command "
3547 "'%s' ignored", cmd->name);
Eric Blake2d5a8342015-04-15 09:19:23 -06003548 return true;
3549 }
Markus Armbrusterf994b252015-03-06 19:51:51 +01003550 if (!is_cap && !mon->qmp.in_command_mode) {
Markus Armbruster40861822015-05-29 10:27:16 +02003551 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
3552 "Expecting capabilities negotiation with "
3553 "'qmp_capabilities' before command '%s'", cmd->name);
Eric Blake2d5a8342015-04-15 09:19:23 -06003554 return true;
3555 }
3556 return false;
Luiz Capitulino09069b12010-02-04 18:10:06 -02003557}
3558
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003559/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03003560 * Argument validation rules:
3561 *
3562 * 1. The argument must exist in cmd_args qdict
3563 * 2. The argument type must be the expected one
3564 *
3565 * Special case: If the argument doesn't exist in cmd_args and
3566 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
3567 * checking is skipped for it.
3568 */
Markus Armbruster326283a2015-03-02 18:39:09 +01003569static void check_client_args_type(const QDict *client_args,
3570 const QDict *cmd_args, int flags,
3571 Error **errp)
Luiz Capitulino4af91932010-06-22 11:44:05 -03003572{
3573 const QDictEntry *ent;
3574
3575 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
3576 QObject *obj;
3577 QString *arg_type;
3578 const QObject *client_arg = qdict_entry_value(ent);
3579 const char *client_arg_name = qdict_entry_key(ent);
3580
3581 obj = qdict_get(cmd_args, client_arg_name);
3582 if (!obj) {
3583 if (flags & QMP_ACCEPT_UNKNOWNS) {
3584 /* handler accepts unknowns */
3585 continue;
3586 }
3587 /* client arg doesn't exist */
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003588 error_setg(errp, QERR_INVALID_PARAMETER, client_arg_name);
Markus Armbruster326283a2015-03-02 18:39:09 +01003589 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003590 }
3591
3592 arg_type = qobject_to_qstring(obj);
3593 assert(arg_type != NULL);
3594
3595 /* check if argument's type is correct */
3596 switch (qstring_get_str(arg_type)[0]) {
3597 case 'F':
3598 case 'B':
3599 case 's':
3600 if (qobject_type(client_arg) != QTYPE_QSTRING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003601 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3602 client_arg_name, "string");
Markus Armbruster326283a2015-03-02 18:39:09 +01003603 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003604 }
3605 break;
3606 case 'i':
3607 case 'l':
3608 case 'M':
Jes Sorensendbc0c672010-10-21 17:15:47 +02003609 case 'o':
Luiz Capitulino4af91932010-06-22 11:44:05 -03003610 if (qobject_type(client_arg) != QTYPE_QINT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003611 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3612 client_arg_name, "int");
Markus Armbruster326283a2015-03-02 18:39:09 +01003613 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003614 }
3615 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003616 case 'T':
3617 if (qobject_type(client_arg) != QTYPE_QINT &&
3618 qobject_type(client_arg) != QTYPE_QFLOAT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003619 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3620 client_arg_name, "number");
Markus Armbruster326283a2015-03-02 18:39:09 +01003621 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003622 }
3623 break;
3624 case 'b':
3625 case '-':
3626 if (qobject_type(client_arg) != QTYPE_QBOOL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003627 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3628 client_arg_name, "bool");
Markus Armbruster326283a2015-03-02 18:39:09 +01003629 return;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003630 }
3631 break;
3632 case 'O':
3633 assert(flags & QMP_ACCEPT_UNKNOWNS);
3634 break;
Paolo Bonzinib9f89782012-03-22 12:51:11 +01003635 case 'q':
3636 /* Any QObject can be passed. */
3637 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03003638 case '/':
3639 case '.':
3640 /*
3641 * These types are not supported by QMP and thus are not
3642 * handled here. Fall through.
3643 */
3644 default:
3645 abort();
3646 }
3647 }
Luiz Capitulino4af91932010-06-22 11:44:05 -03003648}
3649
3650/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003651 * - Check if the client has passed all mandatory args
3652 * - Set special flags for argument validation
3653 */
Markus Armbruster326283a2015-03-02 18:39:09 +01003654static void check_mandatory_args(const QDict *cmd_args,
3655 const QDict *client_args, int *flags,
3656 Error **errp)
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003657{
3658 const QDictEntry *ent;
3659
3660 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
3661 const char *cmd_arg_name = qdict_entry_key(ent);
3662 QString *type = qobject_to_qstring(qdict_entry_value(ent));
3663 assert(type != NULL);
3664
3665 if (qstring_get_str(type)[0] == 'O') {
3666 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
3667 *flags |= QMP_ACCEPT_UNKNOWNS;
3668 } else if (qstring_get_str(type)[0] != '-' &&
3669 qstring_get_str(type)[1] != '?' &&
3670 !qdict_haskey(client_args, cmd_arg_name)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003671 error_setg(errp, QERR_MISSING_PARAMETER, cmd_arg_name);
Markus Armbruster326283a2015-03-02 18:39:09 +01003672 return;
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003673 }
3674 }
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003675}
3676
3677static QDict *qdict_from_args_type(const char *args_type)
3678{
3679 int i;
3680 QDict *qdict;
3681 QString *key, *type, *cur_qs;
3682
3683 assert(args_type != NULL);
3684
3685 qdict = qdict_new();
3686
3687 if (args_type == NULL || args_type[0] == '\0') {
3688 /* no args, empty qdict */
3689 goto out;
3690 }
3691
3692 key = qstring_new();
3693 type = qstring_new();
3694
3695 cur_qs = key;
3696
3697 for (i = 0;; i++) {
3698 switch (args_type[i]) {
3699 case ',':
3700 case '\0':
3701 qdict_put(qdict, qstring_get_str(key), type);
3702 QDECREF(key);
3703 if (args_type[i] == '\0') {
3704 goto out;
3705 }
3706 type = qstring_new(); /* qdict has ref */
3707 cur_qs = key = qstring_new();
3708 break;
3709 case ':':
3710 cur_qs = type;
3711 break;
3712 default:
3713 qstring_append_chr(cur_qs, args_type[i]);
3714 break;
3715 }
3716 }
3717
3718out:
3719 return qdict;
3720}
3721
3722/*
3723 * Client argument checking rules:
3724 *
3725 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03003726 * 2. Each argument provided by the client must be expected
3727 * 3. Each argument provided by the client must have the type expected
3728 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003729 */
Markus Armbruster326283a2015-03-02 18:39:09 +01003730static void qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args,
3731 Error **errp)
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003732{
Markus Armbruster326283a2015-03-02 18:39:09 +01003733 Error *err = NULL;
3734 int flags;
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003735 QDict *cmd_args;
3736
3737 cmd_args = qdict_from_args_type(cmd->args_type);
3738
3739 flags = 0;
Markus Armbruster326283a2015-03-02 18:39:09 +01003740 check_mandatory_args(cmd_args, client_args, &flags, &err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003741 if (err) {
3742 goto out;
3743 }
3744
Markus Armbruster326283a2015-03-02 18:39:09 +01003745 check_client_args_type(client_args, cmd_args, flags, &err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003746
3747out:
Markus Armbruster326283a2015-03-02 18:39:09 +01003748 error_propagate(errp, err);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003749 QDECREF(cmd_args);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003750}
3751
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003752/*
3753 * Input object checking rules
3754 *
3755 * 1. Input object must be a dict
3756 * 2. The "execute" key must exist
3757 * 3. The "execute" key must be a string
3758 * 4. If the "arguments" key exists, it must be a dict
3759 * 5. If the "id" key exists, it can be anything (ie. json-value)
3760 * 6. Any argument not listed above is considered invalid
3761 */
Markus Armbrusterba0510a2015-03-02 18:41:43 +01003762static QDict *qmp_check_input_obj(QObject *input_obj, Error **errp)
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003763{
3764 const QDictEntry *ent;
3765 int has_exec_key = 0;
3766 QDict *input_dict;
3767
3768 if (qobject_type(input_obj) != QTYPE_QDICT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003769 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "object");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003770 return NULL;
3771 }
3772
3773 input_dict = qobject_to_qdict(input_obj);
3774
3775 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
3776 const char *arg_name = qdict_entry_key(ent);
3777 const QObject *arg_obj = qdict_entry_value(ent);
3778
3779 if (!strcmp(arg_name, "execute")) {
3780 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003781 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
3782 "execute", "string");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003783 return NULL;
3784 }
3785 has_exec_key = 1;
3786 } else if (!strcmp(arg_name, "arguments")) {
3787 if (qobject_type(arg_obj) != QTYPE_QDICT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003788 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
3789 "arguments", "object");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003790 return NULL;
3791 }
Markus Armbruster779cec42015-06-08 10:44:30 +02003792 } else if (!strcmp(arg_name, "id")) {
3793 /* Any string is acceptable as "id", so nothing to check */
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003794 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003795 error_setg(errp, QERR_QMP_EXTRA_MEMBER, arg_name);
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003796 return NULL;
3797 }
3798 }
3799
3800 if (!has_exec_key) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003801 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "execute");
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003802 return NULL;
3803 }
3804
3805 return input_dict;
3806}
3807
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003808static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
3809{
Markus Armbruster326283a2015-03-02 18:39:09 +01003810 Error *local_err = NULL;
Markus Armbruster84add862015-03-05 16:45:15 +01003811 QObject *obj, *data;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003812 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003813 const mon_cmd_t *cmd;
Luiz Capitulino40e5a012011-10-21 16:15:31 -02003814 const char *cmd_name;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003815 Monitor *mon = cur_mon;
3816
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03003817 args = input = NULL;
Markus Armbruster84add862015-03-05 16:45:15 +01003818 data = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003819
3820 obj = json_parser_parse(tokens, NULL);
3821 if (!obj) {
3822 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01003823 error_setg(&local_err, QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003824 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003825 }
3826
Markus Armbrusterba0510a2015-03-02 18:41:43 +01003827 input = qmp_check_input_obj(obj, &local_err);
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03003828 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003829 qobject_decref(obj);
3830 goto err_out;
3831 }
3832
Markus Armbruster74358f22015-03-06 19:35:59 +01003833 mon->qmp.id = qdict_get(input, "id");
3834 qobject_incref(mon->qmp.id);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003835
Luiz Capitulino0bbab462010-05-31 17:32:50 -03003836 cmd_name = qdict_get_str(input, "execute");
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +01003837 trace_handle_qmp_command(mon, cmd_name);
Anthony Liguorie3193602011-09-02 12:34:47 -05003838 cmd = qmp_find_cmd(cmd_name);
Eric Blake2d5a8342015-04-15 09:19:23 -06003839 if (!cmd) {
Markus Armbruster710aec92015-03-06 11:28:00 +01003840 error_set(&local_err, ERROR_CLASS_COMMAND_NOT_FOUND,
3841 "The command %s has not been found", cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03003842 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003843 }
Markus Armbruster40861822015-05-29 10:27:16 +02003844 if (invalid_qmp_mode(mon, cmd, &local_err)) {
Eric Blake2d5a8342015-04-15 09:19:23 -06003845 goto err_out;
3846 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003847
3848 obj = qdict_get(input, "arguments");
3849 if (!obj) {
3850 args = qdict_new();
3851 } else {
3852 args = qobject_to_qdict(obj);
3853 QINCREF(args);
3854 }
3855
Markus Armbruster326283a2015-03-02 18:39:09 +01003856 qmp_check_client_args(cmd, args, &local_err);
3857 if (local_err) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003858 goto err_out;
3859 }
3860
Markus Armbruster485febc2015-03-13 17:25:50 +01003861 cmd->mhandler.cmd_new(args, &data, &local_err);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003862
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003863err_out:
Markus Armbruster710aec92015-03-06 11:28:00 +01003864 monitor_protocol_emitter(mon, data, local_err);
Markus Armbruster84add862015-03-05 16:45:15 +01003865 qobject_decref(data);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03003866 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003867 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003868}
3869
Markus Armbrusterc83fe232015-03-06 19:20:51 +01003870static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003871{
3872 Monitor *old_mon = cur_mon;
3873
3874 cur_mon = opaque;
3875
Markus Armbruster74358f22015-03-06 19:35:59 +01003876 json_message_parser_feed(&cur_mon->qmp.parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003877
3878 cur_mon = old_mon;
3879}
3880
aliguori731b0362009-03-05 23:01:42 +00003881static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00003882{
aliguori731b0362009-03-05 23:01:42 +00003883 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00003884 int i;
aliguori376253e2009-03-05 23:01:23 +00003885
aliguori731b0362009-03-05 23:01:42 +00003886 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00003887
aliguoricde76ee2009-03-05 23:01:51 +00003888 if (cur_mon->rs) {
3889 for (i = 0; i < size; i++)
3890 readline_handle_byte(cur_mon->rs, buf[i]);
3891 } else {
3892 if (size == 0 || buf[size - 1] != 0)
3893 monitor_printf(cur_mon, "corrupted command\n");
3894 else
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003895 handle_hmp_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00003896 }
aliguori731b0362009-03-05 23:01:42 +00003897
3898 cur_mon = old_mon;
3899}
aliguorid8f44602008-10-06 13:52:44 +00003900
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003901static void monitor_command_cb(void *opaque, const char *cmdline,
3902 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00003903{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01003904 Monitor *mon = opaque;
3905
aliguori731b0362009-03-05 23:01:42 +00003906 monitor_suspend(mon);
Markus Armbruster7ef6cf62015-03-06 19:12:36 +01003907 handle_hmp_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00003908 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00003909}
3910
aliguoricde76ee2009-03-05 23:01:51 +00003911int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003912{
aliguoricde76ee2009-03-05 23:01:51 +00003913 if (!mon->rs)
3914 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00003915 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00003916 return 0;
aliguorid8f44602008-10-06 13:52:44 +00003917}
3918
aliguori376253e2009-03-05 23:01:23 +00003919void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003920{
aliguoricde76ee2009-03-05 23:01:51 +00003921 if (!mon->rs)
3922 return;
aliguori731b0362009-03-05 23:01:42 +00003923 if (--mon->suspend_cnt == 0)
3924 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00003925}
3926
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003927static QObject *get_qmp_greeting(void)
3928{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03003929 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003930
Markus Armbruster7fad30f2015-09-16 13:06:19 +02003931 qmp_marshal_query_version(NULL, &ver, NULL);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003932 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
3933}
3934
Markus Armbrusterc83fe232015-03-06 19:20:51 +01003935static void monitor_qmp_event(void *opaque, int event)
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003936{
Luiz Capitulino47116d12010-02-08 17:01:30 -02003937 QObject *data;
3938 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003939
Luiz Capitulino47116d12010-02-08 17:01:30 -02003940 switch (event) {
3941 case CHR_EVENT_OPENED:
Markus Armbrusterf994b252015-03-06 19:51:51 +01003942 mon->qmp.in_command_mode = false;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02003943 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003944 monitor_json_emitter(mon, data);
3945 qobject_decref(data);
Corey Bryantefb87c12012-08-14 16:43:48 -04003946 mon_refcount++;
Luiz Capitulino47116d12010-02-08 17:01:30 -02003947 break;
3948 case CHR_EVENT_CLOSED:
Markus Armbruster74358f22015-03-06 19:35:59 +01003949 json_message_parser_destroy(&mon->qmp.parser);
3950 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Corey Bryantefb87c12012-08-14 16:43:48 -04003951 mon_refcount--;
3952 monitor_fdsets_cleanup();
Luiz Capitulino47116d12010-02-08 17:01:30 -02003953 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003954 }
3955}
3956
aliguori731b0362009-03-05 23:01:42 +00003957static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00003958{
aliguori376253e2009-03-05 23:01:23 +00003959 Monitor *mon = opaque;
3960
aliguori2724b182009-03-05 23:01:47 +00003961 switch (event) {
3962 case CHR_EVENT_MUX_IN:
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003963 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003964 mon->mux_out = 0;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003965 qemu_mutex_unlock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003966 if (mon->reset_seen) {
3967 readline_restart(mon->rs);
3968 monitor_resume(mon);
3969 monitor_flush(mon);
3970 } else {
3971 mon->suspend_cnt = 0;
3972 }
aliguori2724b182009-03-05 23:01:47 +00003973 break;
ths86e94de2007-01-05 22:01:59 +00003974
aliguori2724b182009-03-05 23:01:47 +00003975 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003976 if (mon->reset_seen) {
3977 if (mon->suspend_cnt == 0) {
3978 monitor_printf(mon, "\n");
3979 }
3980 monitor_flush(mon);
3981 monitor_suspend(mon);
3982 } else {
3983 mon->suspend_cnt++;
3984 }
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003985 qemu_mutex_lock(&mon->out_lock);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003986 mon->mux_out = 1;
Paolo Bonzini6cff3e82014-06-18 08:43:59 +02003987 qemu_mutex_unlock(&mon->out_lock);
aliguori2724b182009-03-05 23:01:47 +00003988 break;
3989
Amit Shahb6b8df52009-10-07 18:31:16 +05303990 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00003991 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3992 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003993 if (!mon->mux_out) {
Stratos Psomadakise5554e22014-09-15 15:34:57 +03003994 readline_restart(mon->rs);
aliguori2724b182009-03-05 23:01:47 +00003995 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003996 }
3997 mon->reset_seen = 1;
Corey Bryantefb87c12012-08-14 16:43:48 -04003998 mon_refcount++;
3999 break;
4000
4001 case CHR_EVENT_CLOSED:
4002 mon_refcount--;
4003 monitor_fdsets_cleanup();
aliguori2724b182009-03-05 23:01:47 +00004004 break;
4005 }
ths86e94de2007-01-05 22:01:59 +00004006}
4007
Wayne Xia816f8922011-10-12 11:32:41 +08004008static int
4009compare_mon_cmd(const void *a, const void *b)
4010{
4011 return strcmp(((const mon_cmd_t *)a)->name,
4012 ((const mon_cmd_t *)b)->name);
4013}
4014
4015static void sortcmdlist(void)
4016{
4017 int array_num;
4018 int elem_size = sizeof(mon_cmd_t);
4019
4020 array_num = sizeof(mon_cmds)/elem_size-1;
4021 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
4022
4023 array_num = sizeof(info_cmds)/elem_size-1;
4024 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
4025}
4026
aliguori76655d62009-03-06 20:27:37 +00004027
4028/*
4029 * Local variables:
4030 * c-indent-level: 4
4031 * c-basic-offset: 4
4032 * tab-width: 8
4033 * End:
4034 */
4035
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004036/* These functions just adapt the readline interface in a typesafe way. We
4037 * could cast function pointers but that discards compiler checks.
4038 */
Stefan Weild5d15072014-01-25 18:18:23 +01004039static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
4040 const char *fmt, ...)
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004041{
4042 va_list ap;
4043 va_start(ap, fmt);
4044 monitor_vprintf(opaque, fmt, ap);
4045 va_end(ap);
4046}
4047
4048static void monitor_readline_flush(void *opaque)
4049{
4050 monitor_flush(opaque);
4051}
4052
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004053static void __attribute__((constructor)) monitor_lock_init(void)
4054{
4055 qemu_mutex_init(&monitor_lock);
4056}
4057
aliguori731b0362009-03-05 23:01:42 +00004058void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004059{
aliguori731b0362009-03-05 23:01:42 +00004060 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004061 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004062
4063 if (is_first_init) {
Wenchao Xia43a14cf2014-06-18 08:43:31 +02004064 monitor_qapi_event_init();
Wenchao Xiad0383172013-08-27 20:38:18 +08004065 sortcmdlist();
ths20d8a3e2007-02-18 17:04:49 +00004066 is_first_init = 0;
4067 }
aliguori87127162009-03-05 23:01:29 +00004068
Wenchao Xiab01fe892013-08-27 20:38:19 +08004069 mon = g_malloc(sizeof(*mon));
4070 monitor_data_init(mon);
ths20d8a3e2007-02-18 17:04:49 +00004071
aliguori87127162009-03-05 23:01:29 +00004072 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00004073 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004074 if (flags & MONITOR_USE_READLINE) {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004075 mon->rs = readline_init(monitor_readline_printf,
4076 monitor_readline_flush,
4077 mon,
4078 monitor_find_completion);
aliguoricde76ee2009-03-05 23:01:51 +00004079 monitor_read_command(mon, 0);
4080 }
aliguori87127162009-03-05 23:01:29 +00004081
Markus Armbruster9f3982f2015-03-06 19:56:38 +01004082 if (monitor_is_qmp(mon)) {
Markus Armbrusterc83fe232015-03-06 19:20:51 +01004083 qemu_chr_add_handlers(chr, monitor_can_read, monitor_qmp_read,
4084 monitor_qmp_event, mon);
Anthony Liguori15f31512011-08-15 11:17:35 -05004085 qemu_chr_fe_set_echo(chr, true);
Markus Armbruster74358f22015-03-06 19:35:59 +01004086 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004087 } else {
4088 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4089 monitor_event, mon);
4090 }
aliguori87127162009-03-05 23:01:29 +00004091
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004092 qemu_mutex_lock(&monitor_lock);
Blue Swirl72cf2d42009-09-12 07:36:22 +00004093 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Paolo Bonzinid622cb52014-06-18 08:44:00 +02004094 qemu_mutex_unlock(&monitor_lock);
bellard7e2515e2004-08-01 21:52:19 +00004095}
4096
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004097static void bdrv_password_cb(void *opaque, const char *password,
4098 void *readline_opaque)
bellard7e2515e2004-08-01 21:52:19 +00004099{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01004100 Monitor *mon = opaque;
4101 BlockDriverState *bs = readline_opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004102 int ret = 0;
Markus Armbruster4d2855a2015-01-29 10:37:00 +01004103 Error *local_err = NULL;
bellard7e2515e2004-08-01 21:52:19 +00004104
Markus Armbruster4d2855a2015-01-29 10:37:00 +01004105 bdrv_add_key(bs, password, &local_err);
4106 if (local_err) {
4107 monitor_printf(mon, "%s\n", error_get_pretty(local_err));
4108 error_free(local_err);
aliguoribb5fc202009-03-05 23:01:15 +00004109 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004110 }
aliguori731b0362009-03-05 23:01:42 +00004111 if (mon->password_completion_cb)
4112 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004113
aliguori731b0362009-03-05 23:01:42 +00004114 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004115}
aliguoric0f4ce72009-03-05 23:01:01 +00004116
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004117int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
Markus Armbruster097310b2014-10-07 13:59:15 +02004118 BlockCompletionFunc *completion_cb,
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004119 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004120{
aliguoricde76ee2009-03-05 23:01:51 +00004121 int err;
4122
aliguori376253e2009-03-05 23:01:23 +00004123 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4124 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00004125
aliguori731b0362009-03-05 23:01:42 +00004126 mon->password_completion_cb = completion_cb;
4127 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004128
aliguoricde76ee2009-03-05 23:01:51 +00004129 err = monitor_read_password(mon, bdrv_password_cb, bs);
4130
4131 if (err && completion_cb)
4132 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004133
4134 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00004135}
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004136
4137int monitor_read_block_device_key(Monitor *mon, const char *device,
Markus Armbruster097310b2014-10-07 13:59:15 +02004138 BlockCompletionFunc *completion_cb,
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004139 void *opaque)
4140{
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01004141 Error *err = NULL;
Fam Zheng55606252015-03-02 19:36:46 +08004142 BlockBackend *blk;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004143
Fam Zheng55606252015-03-02 19:36:46 +08004144 blk = blk_by_name(device);
4145 if (!blk) {
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004146 monitor_printf(mon, "Device not found %s\n", device);
4147 return -1;
4148 }
4149
Markus Armbruster9b14e0e2015-03-12 17:26:48 +01004150 bdrv_add_key(blk_bs(blk), NULL, &err);
4151 if (err) {
4152 error_free(err);
4153 return monitor_read_bdrv_key_start(mon, blk_bs(blk), completion_cb, opaque);
4154 }
4155
4156 if (completion_cb) {
4157 completion_cb(opaque, 0);
4158 }
4159 return 0;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004160}
Paolo Bonzini4d454572012-11-26 16:03:42 +01004161
4162QemuOptsList qemu_mon_opts = {
4163 .name = "mon",
4164 .implied_opt_name = "chardev",
4165 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
4166 .desc = {
4167 {
4168 .name = "mode",
4169 .type = QEMU_OPT_STRING,
4170 },{
4171 .name = "chardev",
4172 .type = QEMU_OPT_STRING,
4173 },{
4174 .name = "default",
4175 .type = QEMU_OPT_BOOL,
4176 },{
4177 .name = "pretty",
4178 .type = QEMU_OPT_BOOL,
4179 },
4180 { /* end of list */ }
4181 },
4182};
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004183
4184#ifndef TARGET_I386
4185void qmp_rtc_reset_reinjection(Error **errp)
4186{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01004187 error_setg(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection");
Marcelo Tosattif2ae8ab2014-06-24 18:55:11 -03004188}
4189#endif
Jason J. Herne7ee0c3e2015-06-26 14:03:16 -04004190
4191#ifndef TARGET_S390X
4192void qmp_dump_skeys(const char *filename, Error **errp)
4193{
4194 error_setg(errp, QERR_FEATURE_DISABLED, "dump-skeys");
4195}
4196#endif